Skip to content

Commit a18fce3

Browse files
committed
Expose force_compact
1 parent 860ce0f commit a18fce3

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

kvdb-rocksdb/src/lib.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::{
1414
collections::HashMap,
1515
error, io,
1616
path::{Path, PathBuf},
17-
time::{Duration, Instant},
1817
};
1918

2019
use parking_lot::Mutex;
@@ -271,7 +270,6 @@ pub struct Database {
271270
read_opts: ReadOptions,
272271
block_opts: BlockBasedOptions,
273272
stats: stats::RunningDbStats,
274-
last_compaction: Mutex<Instant>,
275273
}
276274

277275
/// Generate the options for RocksDB, based on the given `DatabaseConfig`.
@@ -362,7 +360,6 @@ impl Database {
362360
write_opts,
363361
block_opts,
364362
stats: stats::RunningDbStats::new(),
365-
last_compaction: Mutex::new(Instant::now()),
366363
})
367364
}
368365

@@ -465,21 +462,7 @@ impl Database {
465462
}
466463
self.stats.tally_bytes_written(stats_total_bytes as u64);
467464

468-
let res = cfs.db.write_opt(batch, &self.write_opts).map_err(other_io_err)?;
469-
470-
// If we have written more data than what we want to have stored in a `sst` file, we force compaction.
471-
// We also ensure that we only compact once per minute.
472-
//
473-
// Otherwise, rocksdb read performance is going down, after e.g. a warp sync.
474-
if stats_total_bytes > self.config.compaction.initial_file_size as usize &&
475-
self.last_compaction.lock().elapsed() > Duration::from_secs(60)
476-
{
477-
self.force_compaction()?;
478-
479-
*self.last_compaction.lock() = Instant::now();
480-
}
481-
482-
Ok(res)
465+
cfs.db.write_opt(batch, &self.write_opts).map_err(other_io_err)
483466
}
484467

485468
/// Get value by key.
@@ -599,25 +582,23 @@ impl Database {
599582
self.inner.db.try_catch_up_with_primary().map_err(other_io_err)
600583
}
601584

602-
/// Force compacting the entire db.
603-
fn force_compaction(&self) -> io::Result<()> {
585+
/// Force compacting a single column.
586+
///
587+
/// After compaction of the column, this may lead to better read performance.
588+
pub fn force_compaction(&self, col: u32) -> io::Result<()> {
604589
let mut compact_options = CompactOptions::default();
605590
compact_options.set_bottommost_level_compaction(rocksdb::BottommostLevelCompaction::Force);
606-
607-
// Don't ask me why we can not just use `compact_range_opt`...
608-
// But we are forced to trigger compaction on every column. Actually we only need this for the `STATE` column,
609-
// but we don't know which one this is here. So, we just iterate all of them.
610-
for col in 0..self.inner.column_names.len() {
611-
self.inner
612-
.db
613-
.compact_range_cf_opt(self.inner.cf(col)?, None::<Vec<u8>>, None::<Vec<u8>>, &compact_options);
614-
}
615-
591+
self.inner.db.compact_range_cf_opt(
592+
self.inner.cf(col as usize)?,
593+
None::<Vec<u8>>,
594+
None::<Vec<u8>>,
595+
&compact_options,
596+
);
616597
Ok(())
617598
}
618599
}
619600

620-
// duplicate declaration of methods here to avoid trait import in certain existing cases
601+
// Duplicate declaration of methods here to avoid trait import in certain existing cases
621602
// at time of addition.
622603
impl KeyValueDB for Database {
623604
fn get(&self, col: u32, key: &[u8]) -> io::Result<Option<DBValue>> {

0 commit comments

Comments
 (0)