diff --git a/src/common/src/hash/consistent_hash/vnode.rs b/src/common/src/hash/consistent_hash/vnode.rs index 803969d8c97e9..c2f89566934b2 100644 --- a/src/common/src/hash/consistent_hash/vnode.rs +++ b/src/common/src/hash/consistent_hash/vnode.rs @@ -20,14 +20,15 @@ use crate::hash::HashCode; // TODO: make it a newtype pub type ParallelUnitId = u32; -/// `VirtualNode` (a.k.a. VNode) is a minimal partition that a set of keys belong to. It is used for +/// `VirtualNode` (a.k.a. Vnode) is a minimal partition that a set of keys belong to. It is used for /// consistent hashing. #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Display)] #[display("{0}")] pub struct VirtualNode(VirtualNodeInner); -type VirtualNodeInner = u8; +/// The internal representation of a virtual node id. +type VirtualNodeInner = u16; static_assertions::const_assert!(VirtualNodeInner::BITS >= VirtualNode::BITS as u32); impl From for VirtualNode { @@ -41,6 +42,9 @@ impl From for VirtualNode { impl VirtualNode { /// The number of bits used to represent a virtual node. + /// + /// Note: Not all bits of the inner representation are used. One should rely on this constant + /// to determine the count of virtual nodes. pub const BITS: usize = 8; /// The total count of virtual nodes. pub const COUNT: usize = 1 << Self::BITS; diff --git a/src/storage/hummock_sdk/src/filter_key_extractor.rs b/src/storage/hummock_sdk/src/filter_key_extractor.rs index 359374620a62a..15527fc48e6d9 100644 --- a/src/storage/hummock_sdk/src/filter_key_extractor.rs +++ b/src/storage/hummock_sdk/src/filter_key_extractor.rs @@ -359,6 +359,10 @@ mod tests { }; use crate::key::TABLE_PREFIX_LEN; + const fn dummy_vnode() -> [u8; VirtualNode::SIZE] { + VirtualNode::from_index(233).to_be_bytes() + } + #[test] fn test_default_filter_key_extractor() { let dummy_filter_key_extractor = DummyFilterKeyExtractor::default(); @@ -492,8 +496,7 @@ mod tests { buf.to_vec() }; - let vnode_prefix = "v".as_bytes(); - assert_eq!(VirtualNode::SIZE, vnode_prefix.len()); + let vnode_prefix = &dummy_vnode()[..]; let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat(); let output_key = schema_filter_key_extractor.extract(&full_key); @@ -527,8 +530,7 @@ mod tests { buf.to_vec() }; - let vnode_prefix = "v".as_bytes(); - assert_eq!(VirtualNode::SIZE, vnode_prefix.len()); + let vnode_prefix = &dummy_vnode()[..]; let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat(); let output_key = multi_filter_key_extractor.extract(&full_key); @@ -565,8 +567,7 @@ mod tests { buf.to_vec() }; - let vnode_prefix = "v".as_bytes(); - assert_eq!(VirtualNode::SIZE, vnode_prefix.len()); + let vnode_prefix = &dummy_vnode()[..]; let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat(); let output_key = multi_filter_key_extractor.extract(&full_key);