Skip to content

Commit

Permalink
refactor: use u16 representation for virtual node (risingwavelabs#8385)
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: Renjie Liu <liurenjie2008@gmail.com>
  • Loading branch information
BugenZhao and liurenjie1024 authored Mar 14, 2023
1 parent 4f34ade commit fbcd407
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/common/src/hash/consistent_hash/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashCode> for VirtualNode {
Expand All @@ -41,6 +42,9 @@ impl From<HashCode> 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;
Expand Down
13 changes: 7 additions & 6 deletions src/storage/hummock_sdk/src/filter_key_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fbcd407

Please sign in to comment.