Skip to content

Commit fbcd407

Browse files
refactor: use u16 representation for virtual node (risingwavelabs#8385)
Signed-off-by: Bugen Zhao <[email protected]> Co-authored-by: Renjie Liu <[email protected]>
1 parent 4f34ade commit fbcd407

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/common/src/hash/consistent_hash/vnode.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ use crate::hash::HashCode;
2020
// TODO: make it a newtype
2121
pub type ParallelUnitId = u32;
2222

23-
/// `VirtualNode` (a.k.a. VNode) is a minimal partition that a set of keys belong to. It is used for
23+
/// `VirtualNode` (a.k.a. Vnode) is a minimal partition that a set of keys belong to. It is used for
2424
/// consistent hashing.
2525
#[repr(transparent)]
2626
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Display)]
2727
#[display("{0}")]
2828
pub struct VirtualNode(VirtualNodeInner);
2929

30-
type VirtualNodeInner = u8;
30+
/// The internal representation of a virtual node id.
31+
type VirtualNodeInner = u16;
3132
static_assertions::const_assert!(VirtualNodeInner::BITS >= VirtualNode::BITS as u32);
3233

3334
impl From<HashCode> for VirtualNode {
@@ -41,6 +42,9 @@ impl From<HashCode> for VirtualNode {
4142

4243
impl VirtualNode {
4344
/// The number of bits used to represent a virtual node.
45+
///
46+
/// Note: Not all bits of the inner representation are used. One should rely on this constant
47+
/// to determine the count of virtual nodes.
4448
pub const BITS: usize = 8;
4549
/// The total count of virtual nodes.
4650
pub const COUNT: usize = 1 << Self::BITS;

src/storage/hummock_sdk/src/filter_key_extractor.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ mod tests {
359359
};
360360
use crate::key::TABLE_PREFIX_LEN;
361361

362+
const fn dummy_vnode() -> [u8; VirtualNode::SIZE] {
363+
VirtualNode::from_index(233).to_be_bytes()
364+
}
365+
362366
#[test]
363367
fn test_default_filter_key_extractor() {
364368
let dummy_filter_key_extractor = DummyFilterKeyExtractor::default();
@@ -492,8 +496,7 @@ mod tests {
492496
buf.to_vec()
493497
};
494498

495-
let vnode_prefix = "v".as_bytes();
496-
assert_eq!(VirtualNode::SIZE, vnode_prefix.len());
499+
let vnode_prefix = &dummy_vnode()[..];
497500

498501
let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat();
499502
let output_key = schema_filter_key_extractor.extract(&full_key);
@@ -527,8 +530,7 @@ mod tests {
527530
buf.to_vec()
528531
};
529532

530-
let vnode_prefix = "v".as_bytes();
531-
assert_eq!(VirtualNode::SIZE, vnode_prefix.len());
533+
let vnode_prefix = &dummy_vnode()[..];
532534

533535
let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat();
534536
let output_key = multi_filter_key_extractor.extract(&full_key);
@@ -565,8 +567,7 @@ mod tests {
565567
buf.to_vec()
566568
};
567569

568-
let vnode_prefix = "v".as_bytes();
569-
assert_eq!(VirtualNode::SIZE, vnode_prefix.len());
570+
let vnode_prefix = &dummy_vnode()[..];
570571

571572
let full_key = [&table_prefix, vnode_prefix, &row_bytes].concat();
572573
let output_key = multi_filter_key_extractor.extract(&full_key);

0 commit comments

Comments
 (0)