Skip to content

Commit 7ae63ba

Browse files
alicebobdadrus
andauthored
CLUSTER SHARDS (#431)
* CLUSTER SHARDS --------- Co-authored-by: Dimitrij Drus <dimitrij.drus@innoq.com>
1 parent 99437e6 commit 7ae63ba

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

cmd_cluster.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func (m *Miniredis) cmdCluster(c *server.Peer, cmd string, args []string) {
3131
m.cmdClusterKeySlot(c, cmd, args)
3232
case "NODES":
3333
m.cmdClusterNodes(c, cmd, args)
34+
case "SHARDS":
35+
m.cmdClusterShards(c, cmd, args)
3436
default:
3537
setDirty(c)
3638
c.WriteError(fmt.Sprintf("ERR 'CLUSTER %s' not supported", strings.Join(args, " ")))
@@ -68,3 +70,53 @@ func (m *Miniredis) cmdClusterNodes(c *server.Peer, cmd string, args []string) {
6870
c.WriteBulk(fmt.Sprintf("e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca %s@%d myself,master - 0 0 1 connected 0-16383", addr, port))
6971
})
7072
}
73+
74+
// CLUSTER SHARDS
75+
func (m *Miniredis) cmdClusterShards(c *server.Peer, cmd string, args []string) {
76+
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
77+
addr := m.srv.Addr()
78+
host := addr.IP.String()
79+
port := addr.Port
80+
81+
// Array of shards (we return 1 shard)
82+
c.WriteLen(1)
83+
84+
// Shard is a map with 2 keys: "slots" and "nodes"
85+
c.WriteMapLen(2)
86+
87+
// "slots": flat list of start/end pairs (inclusive ranges)
88+
c.WriteBulk("slots")
89+
c.WriteLen(2)
90+
c.WriteInt(0)
91+
c.WriteInt(16383)
92+
93+
// "nodes": array of node maps
94+
c.WriteBulk("nodes")
95+
c.WriteLen(1)
96+
97+
// Node map.
98+
// (id, endpoint, ip, port, role, replication-offset, health)
99+
c.WriteMapLen(6)
100+
101+
c.WriteBulk("id")
102+
c.WriteBulk("13f84e686106847b76671957dd348fde540a77bb")
103+
104+
//c.WriteBulk("endpoint")
105+
//c.WriteBulk(host) // or host:port if your client expects that
106+
107+
c.WriteBulk("ip")
108+
c.WriteBulk(host)
109+
110+
c.WriteBulk("port")
111+
c.WriteInt(port)
112+
113+
c.WriteBulk("role")
114+
c.WriteBulk("master")
115+
116+
c.WriteBulk("replication-offset")
117+
c.WriteInt(0)
118+
119+
c.WriteBulk("health")
120+
c.WriteBulk("online")
121+
})
122+
}

cmd_cluster_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ func TestCluster(t *testing.T) {
3838
)
3939
})
4040

41+
t.Run("shards", func(t *testing.T) {
42+
mustDo(t, c,
43+
"CLUSTER", "SHARDS",
44+
proto.Array(
45+
proto.Array(
46+
proto.String("slots"), proto.Array(
47+
proto.Int(0),
48+
proto.Int(16383),
49+
),
50+
proto.String("nodes"), proto.Array(
51+
proto.Array(
52+
proto.String("id"),
53+
proto.String("13f84e686106847b76671957dd348fde540a77bb"),
54+
55+
proto.String("ip"),
56+
proto.String(s.srv.Addr().IP.String()),
57+
58+
proto.String("port"),
59+
proto.Int(s.srv.Addr().Port),
60+
61+
proto.String("role"),
62+
proto.String("master"),
63+
64+
proto.String("replication-offset"),
65+
proto.Int(0),
66+
67+
proto.String("health"),
68+
proto.String("online"),
69+
),
70+
),
71+
),
72+
),
73+
)
74+
})
75+
4176
t.Run("keyslot", func(t *testing.T) {
4277
mustDo(t, c,
4378
"CLUSTER", "keyslot", "{test_key}",

integration/cluster_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func TestCluster(t *testing.T) {
1010
c.DoLoosely("CLUSTER", "KEYSLOT", "{test}")
1111
c.DoLoosely("CLUSTER", "NODES")
1212
c.Error("wrong number", "CLUSTER")
13+
// c.DoLoosely("CLUSTER", "SHARDS")
1314
},
1415
)
1516
}

0 commit comments

Comments
 (0)