Skip to content

Commit

Permalink
Add server version validation during module loading
Browse files Browse the repository at this point in the history
Signed-off-by: VanessaTang <[email protected]>
  • Loading branch information
YueTang-Vanessa committed Feb 21, 2025
1 parent 8f1c9f4 commit c53cfbc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/bloom/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bloomfilter::Bloom;
use bloomfilter::{deserialize, serialize};
use serde::{Deserialize, Deserializer, Serialize};
use std::sync::atomic::Ordering;
use valkey_module::raw::Version;

/// KeySpace Notification Events
pub const ADD_EVENT: &str = "bloom.add";
Expand Down Expand Up @@ -708,13 +709,23 @@ impl Drop for BloomFilter {
}
}

pub fn valid_server_version(version: Version) -> bool {
let server_version = &[
version.major.into(),
version.minor.into(),
version.patch.into(),
];
server_version >= configs::BLOOM_MIN_SUPPORTED_VERSION
}

#[cfg(test)]
mod tests {
use super::*;
use crate::configs::TIGHTENING_RATIO_DEFAULT;
use configs;
use rand::{distributions::Alphanumeric, Rng};
use rstest::rstest;
use valkey_module::raw::Version;

/// Returns random string with specified number of characters.
fn random_prefix(len: usize) -> String {
Expand Down Expand Up @@ -1254,4 +1265,44 @@ mod tests {
test_v.push(i);
}
}

#[test]
fn test_valid_server_version() {
let v1 = Version {
major: 8,
minor: 1,
patch: 0,
};
let v2 = Version {
major: 8,
minor: 0,
patch: 1,
};
let v3 = Version {
major: 7,
minor: 1,
patch: 0,
};
let v4 = Version {
major: 7,
minor: 0,
patch: 1,
};
let v5 = Version {
major: 8,
minor: 0,
patch: -1,
};
let v6 = Version {
major: 8,
minor: 0,
patch: 0,
};
assert!(valid_server_version(v1));
assert!(valid_server_version(v2));
assert!(!valid_server_version(v3));
assert!(!valid_server_version(v4));
assert!(!valid_server_version(v5));
assert!(valid_server_version(v6));
}
}
2 changes: 2 additions & 0 deletions src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub const FIXED_SEED: [u8; 32] = [
89, 15, 245, 34, 234, 120, 17, 218, 167, 20, 216, 9, 59, 62, 123, 217, 29, 137, 138, 115, 62,
152, 136, 135, 48, 127, 151, 205, 40, 7, 51, 131,
];
/// Minimal Valkey version that supports Bloom Module
pub const BLOOM_MIN_SUPPORTED_VERSION: &[i64; 3] = &[8, 0, 0];

/// This is a config set handler for the False Positive Rate and Tightening Ratio configs.
pub fn on_string_config_set(
Expand Down
19 changes: 17 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ pub mod metrics;
pub mod wrapper;
use crate::bloom::command_handler;
use crate::bloom::data_type::BLOOM_TYPE;
use crate::bloom::utils::valid_server_version;
use valkey_module_macros::info_command_handler;

pub const MODULE_NAME: &str = "bf";

fn initialize(_ctx: &Context, _args: &[ValkeyString]) -> Status {
Status::Ok
fn initialize(ctx: &Context, _args: &[ValkeyString]) -> Status {
let ver = ctx
.get_redis_version()
.expect("Unable to get server version!");
if !valid_server_version(ver) {
ctx.log_warning(
format!(
"The minimum supported Valkey server version for the valkey-bloom module is {:?}",
configs::BLOOM_MIN_SUPPORTED_VERSION
)
.as_str(),
);
Status::Err
} else {
Status::Ok
}
}

fn deinitialize(_ctx: &Context) -> Status {
Expand Down

0 comments on commit c53cfbc

Please sign in to comment.