Skip to content

Commit

Permalink
Support random seed per bloom object by default (configurable)
Browse files Browse the repository at this point in the history
Signed-off-by: Karthik Subbarao <karthikrs2021@gmail.com>
  • Loading branch information
KarthikSubbarao committed Nov 30, 2024
1 parent 7c25468 commit fad4902
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 291 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ homepage = "https://github.com/valkey-io/valkey-bloom"
valkey-module = "0.1.2"
valkey-module-macros = "0"
linkme = "0"
bloomfilter = { version = "1.0.13", features = ["serde"] }
bloomfilter = { version = "2.0.0", features = ["serde"] }
lazy_static = "1.4.0"
libc = "0.2"
serde = { version = "1.0", features = ["derive"] }
Expand Down
6 changes: 6 additions & 0 deletions src/bloom/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ pub fn bloom_filter_add_value(
let fp_rate = configs::BLOOM_FP_RATE_DEFAULT;
let capacity = configs::BLOOM_CAPACITY.load(Ordering::Relaxed) as u32;
let expansion = configs::BLOOM_EXPANSION.load(Ordering::Relaxed) as u32;
let use_random_seed = configs::BLOOM_USE_RANDOM_SEED.load(Ordering::Relaxed);
let mut bloom = match BloomFilterType::new_reserved(
fp_rate,
capacity,
expansion,
use_random_seed,
validate_size_limit,
) {
Ok(bf) => bf,
Expand Down Expand Up @@ -274,12 +276,14 @@ pub fn bloom_filter_reserve(ctx: &Context, input_args: &[ValkeyString]) -> Valke
match value {
Some(_) => Err(ValkeyError::Str(utils::ITEM_EXISTS)),
None => {
let use_random_seed = configs::BLOOM_USE_RANDOM_SEED.load(Ordering::Relaxed);
// Skip bloom filter size validation on replicated cmds.
let validate_size_limit = !ctx.get_flags().contains(ContextFlags::REPLICATED);
let bloom = match BloomFilterType::new_reserved(
fp_rate,
capacity,
expansion,
use_random_seed,
validate_size_limit,
) {
Ok(bf) => bf,
Expand Down Expand Up @@ -403,10 +407,12 @@ pub fn bloom_filter_insert(ctx: &Context, input_args: &[ValkeyString]) -> Valkey
if nocreate {
return Err(ValkeyError::Str(utils::NOT_FOUND));
}
let use_random_seed = configs::BLOOM_USE_RANDOM_SEED.load(Ordering::Relaxed);
let mut bloom = match BloomFilterType::new_reserved(
fp_rate,
capacity,
expansion,
use_random_seed,
validate_size_limit,
) {
Ok(bf) => bf,
Expand Down
39 changes: 32 additions & 7 deletions src/bloom/data_type.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::bloom::utils::BloomFilter;
use crate::bloom::utils::BloomFilterType;
use crate::configs::{
FIXED_SIP_KEY_ONE_A, FIXED_SIP_KEY_ONE_B, FIXED_SIP_KEY_TWO_A, FIXED_SIP_KEY_TWO_B,
};
use crate::configs;
use crate::metrics::BLOOM_NUM_OBJECTS;
use crate::metrics::BLOOM_OBJECT_TOTAL_MEMORY_BYTES;
use crate::wrapper::bloom_callback;
Expand Down Expand Up @@ -75,6 +73,36 @@ impl ValkeyDataType for BloomFilterType {
let Ok(fp_rate) = raw::load_double(rdb) else {
return None;
};
let Ok(is_seed_random_u64) = raw::load_unsigned(rdb) else {
return None;
};
let is_seed_random = is_seed_random_u64 == 1;
let mut sip_key_one_a = configs::FIXED_SIP_KEY_ONE_A;
let mut sip_key_one_b = configs::FIXED_SIP_KEY_ONE_B;
let mut sip_key_two_a = configs::FIXED_SIP_KEY_TWO_A;
let mut sip_key_two_b = configs::FIXED_SIP_KEY_TWO_B;
if is_seed_random {
let Ok(key_one_a) = raw::load_unsigned(rdb) else {
return None;
};
sip_key_one_a = key_one_a;
let Ok(key_one_b) = raw::load_unsigned(rdb) else {
return None;
};
sip_key_one_b = key_one_b;
let Ok(key_two_a) = raw::load_unsigned(rdb) else {
return None;
};
sip_key_two_a = key_two_a;
let Ok(key_two_b) = raw::load_unsigned(rdb) else {
return None;
};
sip_key_two_b = key_two_b;
}
let sip_keys = [
(sip_key_one_a, sip_key_one_b),
(sip_key_two_a, sip_key_two_b),
];
for i in 0..num_filters {
let Ok(bitmap) = raw::load_string_buffer(rdb) else {
return None;
Expand Down Expand Up @@ -102,10 +130,6 @@ impl ValkeyDataType for BloomFilterType {
} else {
capacity
};
let sip_keys = [
(FIXED_SIP_KEY_ONE_A, FIXED_SIP_KEY_ONE_B),
(FIXED_SIP_KEY_TWO_A, FIXED_SIP_KEY_TWO_B),
];
let filter = BloomFilter::from_existing(
bitmap.as_ref(),
number_of_bits,
Expand All @@ -124,6 +148,7 @@ impl ValkeyDataType for BloomFilterType {
let item = BloomFilterType {
expansion: expansion as u32,
fp_rate,
is_seed_random,
filters,
};
Some(item)
Expand Down
Loading

0 comments on commit fad4902

Please sign in to comment.