From 34bdd9be05e569f2c8e4ea66dc2b2800ff52910d Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Fri, 13 Sep 2024 14:12:47 +0800 Subject: [PATCH] limit vnode count to 2^15 Signed-off-by: Bugen Zhao --- src/common/src/hash/consistent_hash/vnode.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common/src/hash/consistent_hash/vnode.rs b/src/common/src/hash/consistent_hash/vnode.rs index 177c104cf5f48..f719515682625 100644 --- a/src/common/src/hash/consistent_hash/vnode.rs +++ b/src/common/src/hash/consistent_hash/vnode.rs @@ -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. @@ -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 _) } @@ -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) }