-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add TIGHTENING Ratio and FP_Rate module string configs and parse to float #31
Conversation
391d947
to
8b94c17
Compare
bc1e47a
to
1befd38
Compare
tests/test_basic.py
Outdated
@@ -299,6 +299,22 @@ def test_debug_cmd(self): | |||
else: | |||
madd_scenario_object_digest == madd_default_object_digest | |||
|
|||
# validates that digest differs on bloom objects after changing the tightening_ratio | |||
client.execute_command('BF.INSERT tightening_ratio ERROR 0.001 CAPACITY 1000 ITEMS item1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: For a simpler example, can we use BF.RESERVE
without any item? Let's use BF.RESERVE for every bloom object creation from line 302 to 316
src/configs.rs
Outdated
config_ctx: &ConfigurationContext, | ||
name: &str, | ||
val: &'static ValkeyGILGuard<ValkeyString>, | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to return an error... Right now this is not the config set handler. With the current syntax, it is the on config change handler.
) { | |
) -> Result<(), ValkeyError> { |
src/configs.rs
Outdated
ValkeyString::create(None, BLOOM_FP_RATE_DEFAULT.to_string()) | ||
); | ||
pub static ref TIGHTENING_RATIO_DEFAULT_STRING: String = TIGHTENING_RATIO.to_string(); | ||
pub static ref TIGHTENING_RATIO_CONFIG: ValkeyGILGuard<ValkeyString> = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub static ref TIGHTENING_RATIO_CONFIG: ValkeyGILGuard<ValkeyString> = | |
pub static ref BLOOM_TIGHTENING_RATIO: ValkeyGILGuard<ValkeyString> = |
src/configs.rs
Outdated
let _: Result<(), ValkeyError> = Err(ValkeyError::Str("Invalid floating-point value")); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to return an error...
let _: Result<(), ValkeyError> = Err(ValkeyError::Str("Invalid floating-point value")); | |
return; | |
return Err(ValkeyError::Str("Invalid value: Unable to parse string as f64.")); |
src/configs.rs
Outdated
let _: Result<(), ValkeyError> = | ||
Err(ValkeyError::Str("Unknown configuration parameter")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to return an error..
let _: Result<(), ValkeyError> = | |
Err(ValkeyError::Str("Unknown configuration parameter")); | |
return Err(ValkeyError::Str("Unknown configuration parameter")); |
tests/test_basic.py
Outdated
# validates that digest differs on bloom objects after changing the fp_rate | ||
client.execute_command('BF.INSERT fp_rate ITEMS item1') | ||
assert self.client.execute_command('CONFIG SET bf.bloom-fp-rate 0.5') == b'OK' | ||
client.execute_command('BF.INSERT fp_rate2 ERROR 0.1 ITEMS item1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client.execute_command('BF.INSERT fp_rate2 ERROR 0.1 ITEMS item1') | |
client.execute_command('BF.RESERVE fp_rate2 0.001 1000') |
tests/test_basic.py
Outdated
assert scenario_tightening_ratio_object_digest != scenario_tightening_ratio2_digest | ||
|
||
# validates that digest differs on bloom objects after changing the fp_rate | ||
client.execute_command('BF.INSERT fp_rate ITEMS item1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client.execute_command('BF.INSERT fp_rate ITEMS item1') | |
client.execute_command('BF.RESERVE fp_rate 0.001 1000) |
tests/test_basic.py
Outdated
# validates that digest differs on bloom objects after changing the tightening_ratio | ||
client.execute_command('BF.INSERT tightening_ratio ERROR 0.001 CAPACITY 1000 ITEMS item1') | ||
assert self.client.execute_command('CONFIG SET bf.bloom-tightening-ratio 0.75') == b'OK' | ||
client.execute_command('BF.INSERT tightening_ratio2 ERROR 0.01 CAPACITY 1000 ITEMS item1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client.execute_command('BF.INSERT tightening_ratio2 ERROR 0.01 CAPACITY 1000 ITEMS item1') | |
client.execute_command('BF.RESERVE tightening_ratio2 0.001 1000') |
tests/test_basic.py
Outdated
@@ -299,6 +299,22 @@ def test_debug_cmd(self): | |||
else: | |||
madd_scenario_object_digest == madd_default_object_digest | |||
|
|||
# validates that digest differs on bloom objects after changing the tightening_ratio | |||
client.execute_command('BF.INSERT tightening_ratio ERROR 0.001 CAPACITY 1000 ITEMS item1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client.execute_command('BF.INSERT tightening_ratio ERROR 0.001 CAPACITY 1000 ITEMS item1') | |
client.execute_command('BF.RESERVE tightening_ratio 0.001 1000') |
src/lib.rs
Outdated
@@ -106,6 +108,8 @@ valkey_module! { | |||
["bloom-memory-limit-per-filter", &*configs::BLOOM_MEMORY_LIMIT_PER_FILTER, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_DEFAULT, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_MIN, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_MAX, ConfigurationFlags::DEFAULT, None], | |||
], | |||
string: [ | |||
["bloom-fp-rate", &*configs::BLOOM_FP_RATE, &*configs::BLOOM_FP_RATE_DEFAULT_STRING, ConfigurationFlags::DEFAULT, Some(Box::new(configs::on_string_config_set))], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not implementing a config set handler. It is implementing a config on change handler. This is how you implement a config set handler:
["bloom-fp-rate", &*configs::BLOOM_FP_RATE, &*configs::BLOOM_FP_RATE_DEFAULT_STRING, ConfigurationFlags::DEFAULT, Some(Box::new(configs::on_string_config_set))], | |
["bloom-fp-rate", &*configs::BLOOM_FP_RATE, &*configs::BLOOM_FP_RATE_DEFAULT_STRING, ConfigurationFlags::DEFAULT, None, Some(Box::new(configs::on_string_config_set))], |
src/lib.rs
Outdated
@@ -106,6 +108,8 @@ valkey_module! { | |||
["bloom-memory-limit-per-filter", &*configs::BLOOM_MEMORY_LIMIT_PER_FILTER, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_DEFAULT, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_MIN, configs::BLOOM_MEMORY_LIMIT_PER_FILTER_MAX, ConfigurationFlags::DEFAULT, None], | |||
], | |||
string: [ | |||
["bloom-fp-rate", &*configs::BLOOM_FP_RATE, &*configs::BLOOM_FP_RATE_DEFAULT_STRING, ConfigurationFlags::DEFAULT, Some(Box::new(configs::on_string_config_set))], | |||
["bloom-tightening-ratio", &*configs::TIGHTENING_RATIO_CONFIG, &*configs::TIGHTENING_RATIO_DEFAULT_STRING, ConfigurationFlags::DEFAULT, Some(Box::new(configs::on_string_config_set))], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, this is not implementing a config set handler. It is implementing a config on change handler. This is how you implement a config set handler:
["bloom-tightening-ratio", &*configs::TIGHTENING_RATIO_CONFIG, &*configs::TIGHTENING_RATIO_DEFAULT_STRING, ConfigurationFlags::DEFAULT, Some(Box::new(configs::on_string_config_set))], | |
["bloom-tightening-ratio", &*configs::TIGHTENING_RATIO_CONFIG, &*configs::TIGHTENING_RATIO_DEFAULT_STRING, ConfigurationFlags::DEFAULT, None, Some(Box::new(configs::on_string_config_set))], |
src/configs.rs
Outdated
@@ -31,6 +39,13 @@ lazy_static! { | |||
AtomicI64::new(BLOOM_MEMORY_LIMIT_PER_FILTER_DEFAULT); | |||
pub static ref BLOOM_USE_RANDOM_SEED: AtomicBool = AtomicBool::default(); | |||
pub static ref BLOOM_DEFRAG: AtomicBool = AtomicBool::new(BLOOM_DEFRAG_DEAFULT); | |||
pub static ref BLOOM_FP_RATE_DEFAULT_STRING: String = BLOOM_FP_RATE_DEFAULT.to_string(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an actual f64 Mutex above this? We need to add this and change the f64 mutex.
pub static ref BLOOM_FP_RATE_DEFAULT_STRING: String = BLOOM_FP_RATE_DEFAULT.to_string(); | |
pub static ref BLOOM_FP_RATE_F64: Mutex<f64> = Mutex::new(BLOOM_FP_RATE_DEFAULT); |
You also don't need BLOOM_FP_RATE_DEFAULT
. We can use to_string in the config macro during creation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I just directly use BLOOM_FP_RATE_DEFAULT rather than using the string, it throws this error
81 | / valkey_module! {
82 | | name: MODULE_NAME,
83 | | version: 1,
84 | | allocator: (valkey_module::alloc::ValkeyAlloc, valkey_module::alloc::ValkeyAlloc),
... |
111 | | ["bloom-fp-rate", &*configs::BLOOM_FP_RATE, &*configs::BLOOM_FP_RATE_DEFAULT.to_string(), ConfigurationFlags::DEFAULT, None, Some(Box::new(configs::on_string_config_set))],
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
... |
121 | | ]
122 | | }
| | -
| | |
| |_temporary value is freed at the end of this statement
| borrow later stored here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change this &*configs::BLOOM_FP_RATE_DEFAULT.to_string()
to a string slice? The macro requires a string slice type for the default value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So do you want me to use this directly:
["bloom-fp-rate", &*configs::BLOOM_FP_RATE, "0.01", ConfigurationFlags::DEFAULT, None, Some(Box::new(configs::on_string_config_set))],
["bloom-tightening-ratio", &*configs::BLOOM_TIGHTENING_RATIO, "0.5", ConfigurationFlags::DEFAULT, None, Some(Box::new(configs::on_string_config_set))],
and avoid using to_string and the string constant we have in configs?
src/configs.rs
Outdated
pub static ref BLOOM_FP_RATE: ValkeyGILGuard<ValkeyString> = ValkeyGILGuard::new( | ||
ValkeyString::create(None, BLOOM_FP_RATE_DEFAULT.to_string()) | ||
); | ||
pub static ref TIGHTENING_RATIO_DEFAULT_STRING: String = TIGHTENING_RATIO.to_string(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an actual f64 Mutex above this? We need to add this and change the f64 mutex.
pub static ref TIGHTENING_RATIO_DEFAULT_STRING: String = TIGHTENING_RATIO.to_string(); | |
pub static ref BLOOM_TIGHTENING_F64: Mutex<f64> = Mutex::new(TIGHTENING_RATIO); |
You also don't need TIGHTENING_RATIO_DEFAULT_STRING
. We can use to_string in the config macro during creation
src/configs.rs
Outdated
let _: Result<(), ValkeyError> = Err(ValkeyError::Str(utils::BAD_ERROR_RATE)); | ||
return; | ||
} | ||
BLOOM_FP_RATE.set(config_ctx, v.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to change the f64 config here...
BLOOM_FP_RATE.set(config_ctx, v.clone()); | |
let mut fp_rate = BLOOM_FP_RATE_F64.lock().expect("We expect the fp_rate static to exist."); | |
*fp_rate = value; |
src/configs.rs
Outdated
let _: Result<(), ValkeyError> = Err(ValkeyError::Str(utils::BAD_ERROR_RATIO)); | ||
return; | ||
} | ||
TIGHTENING_RATIO_CONFIG.set(config_ctx, v.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to change the f64 config here...
TIGHTENING_RATIO_CONFIG.set(config_ctx, v.clone()); | |
let mut tightening = BLOOM_TIGHTENING_RATIO.lock().expect("We expect the tightening_ratio static to exist."); | |
*tightening = value; |
src/bloom/utils.rs
Outdated
@@ -326,7 +326,8 @@ impl BloomFilterType { | |||
|
|||
/// Calculate the false positive rate for the Nth filter using tightening ratio. | |||
pub fn calculate_fp_rate(fp_rate: f64, num_filters: i32) -> Result<f64, BloomError> { | |||
match fp_rate * configs::TIGHTENING_RATIO.powi(num_filters) { | |||
let tightening_ratio = *configs::BLOOM_TIGHTENING_F64.lock().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this function to accept a tightening_ratio parameter?
We should not be using a constant or config here to find the tightening ratio. We should be using what is in the bloom object itself - because tightening ratio is specific to the object at the time of its creation.
There are two places where this function is used (1) add_item
(2) load_from_rdb
. We need to update how this function is called from both places.
For add_item
, provide the tightening ratio from the BloomFilterType structure.
For load_from_rdb
, provide the tightening ratio from the f64 loaded from RDB .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, sure.
I will be changing the implementation as the following diff:
--- a/src/bloom/data_type.rs
+++ b/src/bloom/data_type.rs
@@ -87,15 +87,16 @@ impl ValkeyDataType for BloomFilterType {
let Ok(capacity) = raw::load_unsigned(rdb) else {
return None;
};
- let new_fp_rate = match Self::calculate_fp_rate(fp_rate, num_filters as i32) {
- Ok(rate) => rate,
- Err(_) => {
- logging::log_warning(
- "Failed to restore bloom object: Reached max number of filters",
- );
- return None;
- }
- };
+ let new_fp_rate =
+ match Self::calculate_fp_rate(fp_rate, num_filters as i32, tightening_ratio) {
+ Ok(rate) => rate,
+ Err(_) => {
+ logging::log_warning(
+ "Failed to restore bloom object: Reached max number of filters",
+ );
+ return None;
+ }
+ };
if !BloomFilter::validate_size(capacity as i64, new_fp_rate) {
logging::log_warning("Failed to restore bloom object: Contains a filter larger than the max allowed size limit.");
return None;
diff --git a/src/bloom/utils.rs b/src/bloom/utils.rs
index d8f2556..d808fdc 100644
--- a/src/bloom/utils.rs
+++ b/src/bloom/utils.rs
@@ -265,10 +265,11 @@ impl BloomFilterType {
}
// Scale out by adding a new filter with capacity bounded within the u32 range. false positive rate is also
// bound within the range f64::MIN_POSITIVE <= x < 1.0.
- let new_fp_rate = match Self::calculate_fp_rate(self.fp_rate, num_filters) {
- Ok(rate) => rate,
- Err(e) => return Err(e),
- };
+ let new_fp_rate =
+ match Self::calculate_fp_rate(self.fp_rate, num_filters, self.tightening_ratio) {
+ Ok(rate) => rate,
+ Err(e) => return Err(e),
+ };
let new_capacity = match filter.capacity.checked_mul(self.expansion.into()) {
Some(new_capacity) => new_capacity,
None => {
@@ -325,8 +326,12 @@ impl BloomFilterType {
}
/// Calculate the false positive rate for the Nth filter using tightening ratio.
- pub fn calculate_fp_rate(fp_rate: f64, num_filters: i32) -> Result<f64, BloomError> {
- let tightening_ratio = *configs::BLOOM_TIGHTENING_F64.lock().unwrap();
+ pub fn calculate_fp_rate(
+ fp_rate: f64,
+ num_filters: i32,
+ tightening_ratio: f64,
+ ) -> Result<f64, BloomError> {
match fp_rate * tightening_ratio.powi(num_filters) {
x if x > f64::MIN_POSITIVE => Ok(x),
_ => Err(BloomError::MaxNumScalingFilters),
4f4dd3a
to
46d4795
Compare
tests/test_basic.py
Outdated
@@ -299,6 +299,25 @@ def test_debug_cmd(self): | |||
else: | |||
madd_scenario_object_digest == madd_default_object_digest | |||
|
|||
# validates that digest differs on bloom objects after changing the tightening_ratio |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# validates that digest differs on bloom objects after changing the tightening_ratio | |
# scenario 6 validates that digest differs on bloom objects after changing the tightening_ratio config |
tests/test_basic.py
Outdated
scenario_tightening_ratio2_digest = client.execute_command('DEBUG DIGEST-VALUE tightening_ratio2') | ||
assert scenario_tightening_ratio_object_digest != scenario_tightening_ratio2_digest | ||
|
||
# validates that digest differs on bloom objects after changing the fp_rate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# validates that digest differs on bloom objects after changing the fp_rate | |
# scenario 7 validates that digest differs on bloom objects after changing the fp_rate config |
tests/test_basic.py
Outdated
client.execute_command('BF.RESERVE fp_rate 0.001 1000') | ||
assert self.client.execute_command('CONFIG SET bf.bloom-fp-rate 0.5') == b'OK' | ||
client.execute_command('BF.RESERVE fp_rate2 0.001 1000') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use a BF.INSERT here without specifying the error rate in both cases
tests/test_basic.py
Outdated
self.client.execute_command('CONFIG SET bf.bloom-tightening-ratio 1.75') | ||
assert False | ||
except ResponseError as e: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we validate the error here?
src/configs.rs
Outdated
} | ||
|
||
/// Constants | ||
// Tightening ratio used during scale out for the calculation of fp_rate of every new filter within a bloom object to | ||
// maintain the bloom object's overall fp_rate to the configured value. | ||
pub const TIGHTENING_RATIO: f64 = 0.5; | ||
pub const TIGHTENING_RATIO_DEFAULT: f64 = 0.5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change this to a string if it is not used anywhere. It will help with config creation
src/bloom/command_handler.rs
Outdated
let mut tightening_ratio = configs::TIGHTENING_RATIO; | ||
let mut fp_rate = *configs::BLOOM_FP_RATE_F64 | ||
.lock() | ||
.expect("Failed to lock fp_rate"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("Failed to lock fp_rate"); | |
.expect("Unable to get a lock on fp_rate static"); |
src/configs.rs
Outdated
@@ -31,12 +39,20 @@ lazy_static! { | |||
AtomicI64::new(BLOOM_MEMORY_LIMIT_PER_FILTER_DEFAULT); | |||
pub static ref BLOOM_USE_RANDOM_SEED: AtomicBool = AtomicBool::default(); | |||
pub static ref BLOOM_DEFRAG: AtomicBool = AtomicBool::new(BLOOM_DEFRAG_DEAFULT); | |||
pub static ref BLOOM_FP_RATE_F64: Mutex<f64> = | |||
Mutex::new(BLOOM_FP_RATE_DEFAULT.parse::<f64>().unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use expect instead of unwrap
Mutex::new(BLOOM_FP_RATE_DEFAULT.parse::<f64>().unwrap()); | |
Mutex::new(BLOOM_FP_RATE_DEFAULT.parse::<f64>().expect("Expected valid f64 for fp rate.")); |
src/configs.rs
Outdated
pub static ref BLOOM_FP_RATE: ValkeyGILGuard<ValkeyString> = | ||
ValkeyGILGuard::new(ValkeyString::create(None, BLOOM_FP_RATE_DEFAULT)); | ||
pub static ref BLOOM_TIGHTENING_F64: Mutex<f64> = | ||
Mutex::new(TIGHTENING_RATIO_DEFAULT.parse::<f64>().unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mutex::new(TIGHTENING_RATIO_DEFAULT.parse::<f64>().unwrap()); | |
Mutex::new(TIGHTENING_RATIO_DEFAULT.parse::<f64>().expect("Expected valid f64 for tightening ratio.")); |
src/configs.rs
Outdated
|
||
match name { | ||
"bloom-fp-rate" => { | ||
if !(BLOOM_FP_RATE_MIN..=BLOOM_FP_RATE_MAX).contains(&value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will allow invalid values for fp rate (e.g 0.0 and 1.0) which should be rejected instead.
Can you change this to something like this?
if !(BLOOM_FP_RATE_MIN..=BLOOM_FP_RATE_MAX).contains(&value) { | |
if !(BLOOM_FP_RATE_MIN > value && value < BLOOM_FP_RATE_MAX) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will be removing = to for the range i.e. use
if !(BLOOM_FP_RATE_MIN..BLOOM_FP_RATE_MAX).contains(&value) {
When I use this
if !(BLOOM_FP_RATE_MIN > value && value < BLOOM_FP_RATE_MAX) {
it throws the following error
error: right-hand side of `&&` operator has no effect
--> src/configs.rs:91:17
|
91 | if !(BLOOM_FP_RATE_MIN > value && value < BLOOM_FP_RATE_MAX) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `BLOOM_FP_RATE_MIN > value` evaluates to true, value < BLOOM_FP_RATE_MAX` will always evaluate to true as well
--> src/configs.rs:91:44
src/configs.rs
Outdated
Ok(()) | ||
} | ||
"bloom-tightening-ratio" => { | ||
if !(TIGHTENING_RATIO_MIN..=TIGHTENING_RATIO_MAX).contains(&value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will allow invalid values for tightening ratio (e.g 0.0 and 1.0) which should be rejected instead.
Can you change this to something like this?
if !(TIGHTENING_RATIO_MIN..=TIGHTENING_RATIO_MAX).contains(&value) { | |
if !(TIGHTENING_RATIO_MIN > value && value < TIGHTENING_RATIO_MAX) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will be removing = to for the range i.e. use
if !(BLOOM_TIGHTENING_RATIO_MIN..BLOOM_TIGHTENING_RATIO_MAX).contains(&value) {
When I use this
if !(BLOOM_TIGHTENING_RATIO_MIN > value && value < BLOOM_TIGHTENING_RATIO_MAX) {
it throws the following error
error: right-hand side of `&&` operator has no effect
--> src/configs.rs:101:17
|
101 | if !(BLOOM_TIGHTENING_RATIO_MIN > value && value < BLOOM_TIGHTENING_RATIO_MAX) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `if `BLOOM_TIGHTENING_RATIO_MIN > value` evaluates to true, value < BLOOM_TIGHTENING_RATIO_MAX` will always evaluate to true as well
--> src/configs.rs:101:53
|
101 | if !(BLOOM_TIGHTENING_RATIO_MIN > value && value < BLOOM_TIGHTENING_RATIO_MAX) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is incorrect as it allows 0.0 which is an invalid fp rate.
Correct check:
if !(value > BLOOM_TIGHTENING_RATIO_MIN && value < BLOOM_TIGHTENING_RATIO_MAX) {
```
src/configs.rs
Outdated
} | ||
|
||
/// Constants | ||
// Tightening ratio used during scale out for the calculation of fp_rate of every new filter within a bloom object to | ||
// maintain the bloom object's overall fp_rate to the configured value. | ||
pub const TIGHTENING_RATIO: f64 = 0.5; | ||
pub const TIGHTENING_RATIO_DEFAULT: &str = "0.5"; | ||
pub const TIGHTENING_RATIO_MIN: f64 = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub const TIGHTENING_RATIO_MIN: f64 = 0.0; | |
pub const BLOOM_TIGHTENING_RATIO_MIN: f64 = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
src/configs.rs
Outdated
} | ||
|
||
/// Constants | ||
// Tightening ratio used during scale out for the calculation of fp_rate of every new filter within a bloom object to | ||
// maintain the bloom object's overall fp_rate to the configured value. | ||
pub const TIGHTENING_RATIO: f64 = 0.5; | ||
pub const TIGHTENING_RATIO_DEFAULT: &str = "0.5"; | ||
pub const TIGHTENING_RATIO_MIN: f64 = 0.0; | ||
pub const TIGHTENING_RATIO_MAX: f64 = 1.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub const TIGHTENING_RATIO_MAX: f64 = 1.0; | |
pub const BLOOM_TIGHTENING_RATIO_MAX: f64 = 1.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
tests/test_basic.py
Outdated
#validates config set correctly and invalid values are rejected | ||
def test_on_string_config_set(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#validates config set correctly and invalid values are rejected | |
def test_on_string_config_set(self): | |
def test_bloom_string_config_set(self): | |
""" | |
This is a test that validates the bloom string configuration set logic. | |
""" |
src/configs.rs
Outdated
|
||
let value = match value_str.parse::<f64>() { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return Err(ValkeyError::Str("Invalid floating-point value")); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the extra new lines here to help with readability
let value = match value_str.parse::<f64>() { | |
Ok(v) => v, | |
Err(_) => { | |
return Err(ValkeyError::Str("Invalid floating-point value")); | |
} | |
}; | |
let value = match value_str.parse::<f64>() { | |
Ok(v) => v, | |
Err(_) => { | |
return Err(ValkeyError::Str("Invalid floating-point value")); | |
} | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
src/configs.rs
Outdated
// Tightening ratio used during scale out for the calculation of fp_rate of every new filter within a bloom object to | ||
// maintain the bloom object's overall fp_rate to the configured value. | ||
pub const TIGHTENING_RATIO: f64 = 0.5; | ||
pub const TIGHTENING_RATIO_DEFAULT: &str = "0.5"; | ||
pub const TIGHTENING_RATIO_MIN: f64 = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this block of code up to line 24? This is no longer a constant - it is a configuration, so we can include it in the section of configurations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will move above
Signed-off-by: Nihal Mehta <nnmehta@amazon.com>
Supporting False Positive config and Tightening Ratio Config.
We have already implemented feature for module configs to support validation & rejection of config sets so that we have the right values for False Positive config and Tightening Ratio Config. valkey-io/valkeymodule-rs#144
With these changes, we can fine tune the configs based on user's workloads and requirements