Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compaction_filter on a column family not triggered #877

Open
kidcooky opened this issue Apr 4, 2024 · 1 comment
Open

compaction_filter on a column family not triggered #877

kidcooky opened this issue Apr 4, 2024 · 1 comment

Comments

@kidcooky
Copy link

kidcooky commented Apr 4, 2024

I had following code that tries to test a compaction_filter set on a column family that should be triggered at compaction, I manually trigger it in test with compact_range / compact_range_cf but seem the filter function of customized compaction filter is not triggered which leads to the test fails. What did I do wrong?

// This is how I opened the db
pub fn create_db(path: &Path) -> StoreResult<DB> {
    let compaction_filter = move |_level: u32, _key: &[u8], _value: &[u8]| {
        println!("reach");
        CompactionDecision::Remove
    };
    let mut opts = Options::default();
    opts.set_compaction_filter("test", compaction_filter);
    let cf_descriptors = vec![ColumnFamilyDescriptor::new("block", opts)];

    // Configure db opening options
    let mut db_opts = Options::default();
    db_opts.create_if_missing(true);
    db_opts.create_missing_column_families(true);

    let db =
        DB::open_cf_descriptors(&db_opts, path, cf_descriptors).map_err(StoreError::DBError)?;

    Ok(db)
}

// This test fails
#[tokio::test]
async fn test_compact_filter_called() {
    use tokio::time::Duration;
    let db = create_db(tempfile::tempdir().unwrap().path()).unwrap();
    db.put_cf(&db.cf_handle("block").unwrap(), b"a", b"1").unwrap();
    db.compact_range_cf(&db.cf_handle("block").unwrap(),  None::<&[u8]>, None::<&[u8]>);
    db.compact_range(None::<&[u8]>, None::<&[u8]>);
    tokio::time::sleep(Duration::from_secs(5)).await;
    assert!(db.get_cf(&db.cf_handle("block").unwrap(), b"a").unwrap().is_none())
}
@kidcooky kidcooky changed the title How to trigger compaction_filter set on a column family? compaction_filter not triggered defined on a column family with compact_range? Apr 4, 2024
@kidcooky kidcooky changed the title compaction_filter not triggered defined on a column family with compact_range? compaction_filter on a column family not triggered by compact_range(None::<&[u8]>, None::<&[u8]>)? Apr 4, 2024
@kidcooky kidcooky changed the title compaction_filter on a column family not triggered by compact_range(None::<&[u8]>, None::<&[u8]>)? compaction_filter on a column family not triggered Apr 4, 2024
@0xdeafbeef
Copy link
Contributor

@kidcooky, your test code isn't doing what you think it is. Here's the deal:

tempfile::tempdir().unwrap().path()

This line is pretty much the same as doing:

let path = "/tmp/bla";
std::fs::create_dir_all(path);
create_db(path);
std::fs::remove_dir_all(path);

What happens here is that RocksDB sets up all its configurations and dumps them to disk. Then, tempfile drop code nukes the directory and everything in it. When it comes time to write, RocksDB tries to recreate all its settings, but it misses the compaction filter.

Writing your test as

let path = tempfile::tempdir()?;
create_db(path.path());

fixes the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants