Skip to content

Commit 2b07865

Browse files
committed
write a LOG file for the database
1 parent cad3ec5 commit 2b07865

File tree

5 files changed

+93
-9
lines changed

5 files changed

+93
-9
lines changed

Cargo.lock

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbo-persistence/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ print_stats = ["stats"]
1414
anyhow = { workspace = true }
1515
pot = "3.0.0"
1616
byteorder = "1.5.0"
17+
chrono = "0.4.40"
1718
lzzzz = "1.1.0"
1819
memmap2 = "0.9.5"
1920
parking_lot = { workspace = true }

turbopack/crates/turbo-persistence/src/db.rs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
any::{Any, TypeId},
33
collections::HashSet,
44
fs::{self, File, OpenOptions, ReadDir},
5-
io::Write,
5+
io::{BufWriter, Write},
66
mem::{swap, transmute, MaybeUninit},
77
path::{Path, PathBuf},
88
sync::{
@@ -13,6 +13,7 @@ use std::{
1313

1414
use anyhow::{bail, Context, Result};
1515
use byteorder::{ReadBytesExt, WriteBytesExt, BE};
16+
use chrono::Local;
1617
use lzzzz::lz4::decompress;
1718
use memmap2::Mmap;
1819
use parking_lot::{Mutex, RwLock};
@@ -287,6 +288,9 @@ impl TurboPersistence {
287288
Some("CURRENT") => {
288289
// Already read
289290
}
291+
Some("LOG") => {
292+
// Ignored, write-only
293+
}
290294
_ => {
291295
if !path
292296
.file_name()
@@ -393,6 +397,16 @@ impl TurboPersistence {
393397
Ok(WriteBatch::new(self.path.clone(), current))
394398
}
395399

400+
fn open_log(&self) -> Result<BufWriter<File>> {
401+
let log_path = self.path.join("LOG");
402+
let log_file = OpenOptions::new()
403+
.write(true)
404+
.create(true)
405+
.append(true)
406+
.open(log_path)?;
407+
Ok(BufWriter::new(log_file))
408+
}
409+
396410
/// Commits a WriteBatch to the database. This will finish writing the data to disk and make it
397411
/// visible to readers.
398412
pub fn commit_write_batch<K: StoreKey + Send + Sync + 'static, const FAMILIES: usize>(
@@ -418,7 +432,7 @@ impl TurboPersistence {
418432
fn commit(
419433
&self,
420434
mut new_sst_files: Vec<(u32, File)>,
421-
new_blob_files: Vec<File>,
435+
new_blob_files: Vec<(u32, File)>,
422436
mut indicies_to_delete: Vec<usize>,
423437
mut seq: u32,
424438
) -> Result<(), anyhow::Error> {
@@ -432,14 +446,44 @@ impl TurboPersistence {
432446
})
433447
.collect::<Result<Vec<_>>>()?;
434448

435-
for file in new_blob_files {
449+
for (_, file) in new_blob_files.iter() {
436450
file.sync_all()?;
437451
}
438452

439453
if !indicies_to_delete.is_empty() {
440454
seq += 1;
441455
}
442456

457+
{
458+
let mut log = self.open_log()?;
459+
let time = Local::now();
460+
writeln!(
461+
log,
462+
"Commit {seq:08} {}",
463+
time.format("%YYYY-%mm-%dd %HH:%MM")
464+
)?;
465+
for sst in new_sst_files.iter() {
466+
let index = sst.sequence_number();
467+
let range = sst.range()?;
468+
let size = sst.size();
469+
writeln!(
470+
log,
471+
"{:08} SST family:{} {:016x}-{:016x} {} MiB",
472+
index,
473+
range.family,
474+
range.min_hash,
475+
range.max_hash,
476+
size / 1024 / 1024
477+
)?;
478+
}
479+
for (seq, _) in new_blob_files.iter() {
480+
writeln!(log, "{:08} BLOB", seq)?;
481+
}
482+
for index in indicies_to_delete.iter() {
483+
writeln!(log, "{:08} DELETED", index)?;
484+
}
485+
}
486+
443487
let removed_ssts;
444488

445489
{
@@ -583,6 +627,7 @@ impl TurboPersistence {
583627
let value_block_cache = &self.value_block_cache;
584628
let path = &self.path;
585629

630+
let log_mutex = Mutex::new(());
586631
let result = sst_by_family
587632
.into_par_iter()
588633
.with_min_len(1)
@@ -604,6 +649,32 @@ impl TurboPersistence {
604649
},
605650
);
606651

652+
if !merge_jobs.is_empty() {
653+
let guard = log_mutex.lock();
654+
let mut log = self.open_log()?;
655+
writeln!(
656+
log,
657+
"Compaction for family {family} (coverage: {coverage}):"
658+
)?;
659+
for job in merge_jobs.iter() {
660+
writeln!(log, " merge")?;
661+
for i in job.iter() {
662+
let index = ssts_with_ranges[*i].index;
663+
let (min, max) = ssts_with_ranges[*i].range();
664+
writeln!(log, " {index:08} {min:016x}-{max:016x}")?;
665+
}
666+
}
667+
if !move_jobs.is_empty() {
668+
writeln!(log, " move")?;
669+
for i in move_jobs.iter() {
670+
let index = ssts_with_ranges[*i].index;
671+
let (min, max) = ssts_with_ranges[*i].range();
672+
writeln!(log, " {index:08} {min:016x}-{max:016x}")?;
673+
}
674+
}
675+
drop(guard);
676+
}
677+
607678
// Later we will remove the merged and moved files
608679
let indicies_to_delete = merge_jobs
609680
.iter()

turbopack/crates/turbo-persistence/src/static_sorted_file.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ impl StaticSortedFile {
138138
self.sequence_number
139139
}
140140

141+
/// The size of this file in bytes.
142+
pub fn size(&self) -> usize {
143+
self.mmap.len()
144+
}
145+
141146
/// Opens an SST file at the given path. This memory maps the file, but does not read it yet.
142147
/// It's lazy read on demand.
143148
pub fn open(sequence_number: u32, path: PathBuf) -> Result<Self> {

turbopack/crates/turbo-persistence/src/write_batch.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ struct ThreadLocalState<K: StoreKey + Send, const FAMILIES: usize> {
3636
/// The collectors for each family.
3737
collectors: [Option<Collector<K, THREAD_LOCAL_SIZE_SHIFT>>; FAMILIES],
3838
/// The list of new blob files that have been created.
39-
new_blob_files: Vec<File>,
39+
new_blob_files: Vec<(u32, File)>,
4040
}
4141

4242
/// The result of a `WriteBatch::finish` operation.
4343
pub(crate) struct FinishResult {
4444
pub(crate) sequence_number: u32,
4545
pub(crate) new_sst_files: Vec<(u32, File)>,
46-
pub(crate) new_blob_files: Vec<File>,
46+
pub(crate) new_blob_files: Vec<(u32, File)>,
4747
}
4848

4949
/// A write batch.
@@ -160,7 +160,7 @@ impl<K: StoreKey + Send + Sync, const FAMILIES: usize> WriteBatch<K, FAMILIES> {
160160
} else {
161161
let (blob, file) = self.create_blob(&value)?;
162162
collector.put_blob(key, blob);
163-
state.new_blob_files.push(file);
163+
state.new_blob_files.push((blob, file));
164164
}
165165
Ok(())
166166
}

0 commit comments

Comments
 (0)