Skip to content

Commit

Permalink
limit vnode count to 2^15
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Sep 13, 2024
1 parent 7d9b1bd commit 34bdd9b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/common/src/hash/consistent_hash/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ impl VirtualNode {

impl VirtualNode {
/// The maximum count of virtual nodes that fits in [`VirtualNodeInner`].
pub const MAX_COUNT: usize = 1 << VirtualNodeInner::BITS;
///
/// Note that the most significant bit is not used. This is because we use signed integers (`i16`)
/// for the scalar representation, where overflow can be confusing in terms of ordering.
// TODO(var-vnode): the only usage is in log-store, shall we update it by storing the vnode as
// bytea to enable 2^16 vnodes?
pub const MAX_COUNT: usize = 1 << (VirtualNodeInner::BITS - 1);
/// The maximum value of the virtual node that can be represented.
///
/// Note that this is **NOT** the maximum value of the virtual node, which depends on the configuration.
Expand Down Expand Up @@ -100,6 +105,7 @@ impl VirtualNode {

/// Creates a virtual node from the given scalar representation. Used by `VNODE` expression.
pub const fn from_scalar(scalar: i16) -> Self {
debug_assert!(scalar >= 0);
Self(scalar as _)
}

Expand All @@ -119,6 +125,7 @@ impl VirtualNode {
/// Creates a virtual node from the given big-endian bytes representation.
pub const fn from_be_bytes(bytes: [u8; Self::SIZE]) -> Self {
let inner = VirtualNodeInner::from_be_bytes(bytes);
debug_assert!((inner as usize) < Self::MAX_COUNT);
Self(inner)
}

Expand Down

0 comments on commit 34bdd9b

Please sign in to comment.