This is an automated email from the ASF dual-hosted git repository.
hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks-controller.git
The following commit(s) were added to refs/heads/unstable by this push:
new 9a5d1b7 Fix wrong slot range check condition when migrating slot
(#208)
9a5d1b7 is described below
commit 9a5d1b7f0b0be8576d4f6e36950ba08ceed3c4c5
Author: hulk <[email protected]>
AuthorDate: Tue Oct 8 20:26:19 2024 +0800
Fix wrong slot range check condition when migrating slot (#208)
---
store/cluster.go | 2 +-
store/cluster_test.go | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/store/cluster.go b/store/cluster.go
index 9173611..f42c402 100644
--- a/store/cluster.go
+++ b/store/cluster.go
@@ -164,7 +164,7 @@ func (cluster *Cluster) Reset(ctx context.Context) error {
}
func (cluster *Cluster) findShardIndexBySlot(slot int) (int, error) {
- if slot < 0 || slot >= MaxSlotID {
+ if slot < 0 || slot > MaxSlotID {
return -1, consts.ErrSlotOutOfRange
}
sourceShardIdx := -1
diff --git a/store/cluster_test.go b/store/cluster_test.go
index fbfe34c..4c39b90 100644
--- a/store/cluster_test.go
+++ b/store/cluster_test.go
@@ -29,6 +29,28 @@ import (
"github.com/apache/kvrocks-controller/consts"
)
+func TestCluster_FindIndexShardBySlot(t *testing.T) {
+ cluster, err := NewCluster("test", []string{"node1", "node2", "node3"},
1)
+ require.NoError(t, err)
+
+ shard, err := cluster.findShardIndexBySlot(0)
+ require.NoError(t, err)
+ require.Equal(t, 0, shard)
+
+ shard, err = cluster.findShardIndexBySlot(MaxSlotID/3 + 1)
+ require.NoError(t, err)
+ require.Equal(t, 1, shard)
+
+ shard, err = cluster.findShardIndexBySlot(MaxSlotID)
+ require.NoError(t, err)
+ require.Equal(t, 2, shard)
+
+ _, err = cluster.findShardIndexBySlot(-1)
+ require.ErrorIs(t, err, consts.ErrSlotOutOfRange)
+ _, err = cluster.findShardIndexBySlot(MaxSlotID + 1)
+ require.ErrorIs(t, err, consts.ErrSlotOutOfRange)
+}
+
func TestCluster_PromoteNewMaster(t *testing.T) {
shard := NewShard()
shard.SlotRanges = []SlotRange{{Start: 0, Stop: 1023}}