aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/support/cluster.tcl
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
commit5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch)
tree1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/tests/support/cluster.tcl
parentc7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff)
downloadcrep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/tests/support/cluster.tcl')
-rw-r--r--examples/redis-unstable/tests/support/cluster.tcl372
1 files changed, 372 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/support/cluster.tcl b/examples/redis-unstable/tests/support/cluster.tcl
new file mode 100644
index 0000000..3a66684
--- /dev/null
+++ b/examples/redis-unstable/tests/support/cluster.tcl
@@ -0,0 +1,372 @@
1# Tcl redis cluster client as a wrapper of redis.rb.
2#
3# Copyright (C) 2014-Present, Redis Ltd.
4# All Rights reserved.
5#
6# Licensed under your choice of (a) the Redis Source Available License 2.0
7# (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
8# GNU Affero General Public License v3 (AGPLv3).
9#
10# Example usage:
11#
12# set c [redis_cluster {127.0.0.1:6379 127.0.0.1:6380}]
13# $c set foo
14# $c get foo
15# $c close
16
17package require Tcl 8.5
18package provide redis_cluster 0.1
19
20namespace eval redis_cluster {}
21set ::redis_cluster::internal_id 0
22set ::redis_cluster::id 0
23array set ::redis_cluster::startup_nodes {}
24array set ::redis_cluster::nodes {}
25array set ::redis_cluster::slots {}
26array set ::redis_cluster::tls {}
27
28# List of "plain" commands, which are commands where the sole key is always
29# the first argument.
30set ::redis_cluster::plain_commands {
31 get set setnx setex psetex append strlen exists setbit getbit
32 setrange getrange substr incr decr rpush lpush rpushx lpushx
33 linsert rpop lpop brpop llen lindex lset lrange ltrim lrem
34 sadd srem sismember smismember scard spop srandmember smembers sscan zadd
35 zincrby zrem zremrangebyscore zremrangebyrank zremrangebylex zrange
36 zrangebyscore zrevrangebyscore zrangebylex zrevrangebylex zcount
37 zlexcount zrevrange zcard zscore zmscore zrank zrevrank zscan hset hsetnx
38 hget hmset hmget hincrby hincrbyfloat hdel hlen hkeys hvals
39 hgetall hexists hscan incrby decrby incrbyfloat getset move
40 expire expireat pexpire pexpireat type ttl pttl persist restore
41 dump bitcount bitpos pfadd pfcount cluster ssubscribe spublish
42 sunsubscribe
43}
44
45# Create a cluster client. The nodes are given as a list of host:port. The TLS
46# parameter (1 or 0) is optional and defaults to the global $::tls.
47proc redis_cluster {nodes {tls -1}} {
48 set id [incr ::redis_cluster::id]
49 set ::redis_cluster::startup_nodes($id) $nodes
50 set ::redis_cluster::nodes($id) {}
51 set ::redis_cluster::slots($id) {}
52 set ::redis_cluster::tls($id) [expr $tls == -1 ? $::tls : $tls]
53 set handle [interp alias {} ::redis_cluster::instance$id {} ::redis_cluster::__dispatch__ $id]
54 $handle refresh_nodes_map
55 return $handle
56}
57
58# Totally reset the slots / nodes state for the client, calls
59# CLUSTER NODES in the first startup node available, populates the
60# list of nodes ::redis_cluster::nodes($id) with an hash mapping node
61# ip:port to a representation of the node (another hash), and finally
62# maps ::redis_cluster::slots($id) with an hash mapping slot numbers
63# to node IDs.
64#
65# This function is called when a new Redis Cluster client is initialized
66# and every time we get a -MOVED redirection error.
67proc ::redis_cluster::__method__refresh_nodes_map {id} {
68 # Contact the first responding startup node.
69 set idx 0; # Index of the node that will respond.
70 set errmsg {}
71 foreach start_node $::redis_cluster::startup_nodes($id) {
72 set ip_port [lindex [split $start_node @] 0]
73 lassign [split $ip_port :] start_host start_port
74 set tls $::redis_cluster::tls($id)
75 if {[catch {
76 set r {}
77 set r [redis $start_host $start_port 0 $tls]
78 set nodes_descr [$r cluster nodes]
79 $r close
80 } e]} {
81 if {$r ne {}} {catch {$r close}}
82 incr idx
83 if {[string length $errmsg] < 200} {
84 append errmsg " $ip_port: $e"
85 }
86 continue ; # Try next.
87 } else {
88 break; # Good node found.
89 }
90 }
91
92 if {$idx == [llength $::redis_cluster::startup_nodes($id)]} {
93 error "No good startup node found. $errmsg"
94 }
95
96 # Put the node that responded as first in the list if it is not
97 # already the first.
98 if {$idx != 0} {
99 set l $::redis_cluster::startup_nodes($id)
100 set left [lrange $l 0 [expr {$idx-1}]]
101 set right [lrange $l [expr {$idx+1}] end]
102 set l [concat [lindex $l $idx] $left $right]
103 set ::redis_cluster::startup_nodes($id) $l
104 }
105
106 # Parse CLUSTER NODES output to populate the nodes description.
107 set nodes {} ; # addr -> node description hash.
108 foreach line [split $nodes_descr "\n"] {
109 set line [string trim $line]
110 if {$line eq {}} continue
111 set args [split $line " "]
112 lassign $args nodeid addr flags slaveof pingsent pongrecv configepoch linkstate
113 set slots [lrange $args 8 end]
114 set addr [lindex [split $addr @] 0]
115 if {$addr eq {:0}} {
116 set addr $start_host:$start_port
117 }
118 lassign [split $addr :] host port
119
120 # Connect to the node
121 set link {}
122 set tls $::redis_cluster::tls($id)
123 catch {set link [redis $host $port 0 $tls]}
124
125 # Build this node description as an hash.
126 set node [dict create \
127 id $nodeid \
128 internal_id $id \
129 addr $addr \
130 host $host \
131 port $port \
132 flags $flags \
133 slaveof $slaveof \
134 slots $slots \
135 link $link \
136 ]
137 dict set nodes $addr $node
138 lappend ::redis_cluster::startup_nodes($id) $addr
139 }
140
141 # Close all the existing links in the old nodes map, and set the new
142 # map as current.
143 foreach n $::redis_cluster::nodes($id) {
144 catch {
145 [dict get $n link] close
146 }
147 }
148 set ::redis_cluster::nodes($id) $nodes
149
150 # Populates the slots -> nodes map.
151 dict for {addr node} $nodes {
152 foreach slotrange [dict get $node slots] {
153 lassign [split $slotrange -] start end
154 if {$end == {}} {set end $start}
155 for {set j $start} {$j <= $end} {incr j} {
156 dict set ::redis_cluster::slots($id) $j $addr
157 }
158 }
159 }
160
161 # Only retain unique entries in the startup nodes list
162 set ::redis_cluster::startup_nodes($id) [lsort -unique $::redis_cluster::startup_nodes($id)]
163}
164
165# Free a redis_cluster handle.
166proc ::redis_cluster::__method__close {id} {
167 catch {
168 set nodes $::redis_cluster::nodes($id)
169 dict for {addr node} $nodes {
170 catch {
171 [dict get $node link] close
172 }
173 }
174 }
175 catch {unset ::redis_cluster::startup_nodes($id)}
176 catch {unset ::redis_cluster::nodes($id)}
177 catch {unset ::redis_cluster::slots($id)}
178 catch {unset ::redis_cluster::tls($id)}
179 catch {interp alias {} ::redis_cluster::instance$id {}}
180}
181
182proc ::redis_cluster::__method__masternode_for_slot {id slot} {
183 # Get the node mapped to this slot.
184 set node_addr [dict get $::redis_cluster::slots($id) $slot]
185 if {$node_addr eq {}} {
186 error "No mapped node for slot $slot."
187 }
188 return [dict get $::redis_cluster::nodes($id) $node_addr]
189}
190
191proc ::redis_cluster::__method__masternode_notfor_slot {id slot} {
192 # Get a node that is not mapped to this slot.
193 set node_addr [dict get $::redis_cluster::slots($id) $slot]
194 set addrs [dict keys $::redis_cluster::nodes($id)]
195 foreach addr [lshuffle $addrs] {
196 set node [dict get $::redis_cluster::nodes($id) $addr]
197 if {$node_addr ne $addr && [dict get $node slaveof] eq "-"} {
198 return $node
199 }
200 }
201 error "Slot $slot is everywhere"
202}
203
204proc ::redis_cluster::__dispatch__ {id method args} {
205 if {[info command ::redis_cluster::__method__$method] eq {}} {
206 # Get the keys from the command.
207 set keys [::redis_cluster::get_keys_from_command $method $args]
208 if {$keys eq {}} {
209 error "Redis command '$method' is not supported by redis_cluster."
210 }
211
212 # Resolve the keys in the corresponding hash slot they hash to.
213 set slot [::redis_cluster::get_slot_from_keys $keys]
214 if {$slot eq {}} {
215 error "Invalid command: multiple keys not hashing to the same slot."
216 }
217
218 # Get the node mapped to this slot.
219 set node_addr [dict get $::redis_cluster::slots($id) $slot]
220 if {$node_addr eq {}} {
221 error "No mapped node for slot $slot."
222 }
223
224 # Execute the command in the node we think is the slot owner.
225 set retry 100
226 set asking 0
227 while {[incr retry -1]} {
228 if {$retry < 5} {after 100}
229 set node [dict get $::redis_cluster::nodes($id) $node_addr]
230 set link [dict get $node link]
231 if {$asking} {
232 $link ASKING
233 set asking 0
234 }
235 if {[catch {$link $method {*}$args} e]} {
236 if {$link eq {} || \
237 [string range $e 0 4] eq {MOVED} || \
238 [string range $e 0 2] eq {I/O} \
239 } {
240 # MOVED redirection.
241 ::redis_cluster::__method__refresh_nodes_map $id
242 set node_addr [dict get $::redis_cluster::slots($id) $slot]
243 continue
244 } elseif {[string range $e 0 2] eq {ASK}} {
245 # ASK redirection.
246 set node_addr [lindex $e 2]
247 set asking 1
248 continue
249 } else {
250 # Non redirecting error.
251 error $e $::errorInfo $::errorCode
252 }
253 } else {
254 # OK query went fine
255 return $e
256 }
257 }
258 error "Too many redirections or failures contacting Redis Cluster."
259 } else {
260 uplevel 1 [list ::redis_cluster::__method__$method $id] $args
261 }
262}
263
264proc ::redis_cluster::get_keys_from_command {cmd argv} {
265 set cmd [string tolower $cmd]
266 # Most Redis commands get just one key as first argument.
267 if {[lsearch -exact $::redis_cluster::plain_commands $cmd] != -1} {
268 return [list [lindex $argv 0]]
269 }
270
271 # Special handling for other commands
272 switch -exact $cmd {
273 mget {return $argv}
274 eval {return [lrange $argv 2 1+[lindex $argv 1]]}
275 evalsha {return [lrange $argv 2 1+[lindex $argv 1]]}
276 spublish {return [list [lindex $argv 1]]}
277 }
278
279 # All the remaining commands are not handled.
280 return {}
281}
282
283# Returns the CRC16 of the specified string.
284# The CRC parameters are described in the Redis Cluster specification.
285set ::redis_cluster::XMODEMCRC16Lookup {
286 0x0000 0x1021 0x2042 0x3063 0x4084 0x50a5 0x60c6 0x70e7
287 0x8108 0x9129 0xa14a 0xb16b 0xc18c 0xd1ad 0xe1ce 0xf1ef
288 0x1231 0x0210 0x3273 0x2252 0x52b5 0x4294 0x72f7 0x62d6
289 0x9339 0x8318 0xb37b 0xa35a 0xd3bd 0xc39c 0xf3ff 0xe3de
290 0x2462 0x3443 0x0420 0x1401 0x64e6 0x74c7 0x44a4 0x5485
291 0xa56a 0xb54b 0x8528 0x9509 0xe5ee 0xf5cf 0xc5ac 0xd58d
292 0x3653 0x2672 0x1611 0x0630 0x76d7 0x66f6 0x5695 0x46b4
293 0xb75b 0xa77a 0x9719 0x8738 0xf7df 0xe7fe 0xd79d 0xc7bc
294 0x48c4 0x58e5 0x6886 0x78a7 0x0840 0x1861 0x2802 0x3823
295 0xc9cc 0xd9ed 0xe98e 0xf9af 0x8948 0x9969 0xa90a 0xb92b
296 0x5af5 0x4ad4 0x7ab7 0x6a96 0x1a71 0x0a50 0x3a33 0x2a12
297 0xdbfd 0xcbdc 0xfbbf 0xeb9e 0x9b79 0x8b58 0xbb3b 0xab1a
298 0x6ca6 0x7c87 0x4ce4 0x5cc5 0x2c22 0x3c03 0x0c60 0x1c41
299 0xedae 0xfd8f 0xcdec 0xddcd 0xad2a 0xbd0b 0x8d68 0x9d49
300 0x7e97 0x6eb6 0x5ed5 0x4ef4 0x3e13 0x2e32 0x1e51 0x0e70
301 0xff9f 0xefbe 0xdfdd 0xcffc 0xbf1b 0xaf3a 0x9f59 0x8f78
302 0x9188 0x81a9 0xb1ca 0xa1eb 0xd10c 0xc12d 0xf14e 0xe16f
303 0x1080 0x00a1 0x30c2 0x20e3 0x5004 0x4025 0x7046 0x6067
304 0x83b9 0x9398 0xa3fb 0xb3da 0xc33d 0xd31c 0xe37f 0xf35e
305 0x02b1 0x1290 0x22f3 0x32d2 0x4235 0x5214 0x6277 0x7256
306 0xb5ea 0xa5cb 0x95a8 0x8589 0xf56e 0xe54f 0xd52c 0xc50d
307 0x34e2 0x24c3 0x14a0 0x0481 0x7466 0x6447 0x5424 0x4405
308 0xa7db 0xb7fa 0x8799 0x97b8 0xe75f 0xf77e 0xc71d 0xd73c
309 0x26d3 0x36f2 0x0691 0x16b0 0x6657 0x7676 0x4615 0x5634
310 0xd94c 0xc96d 0xf90e 0xe92f 0x99c8 0x89e9 0xb98a 0xa9ab
311 0x5844 0x4865 0x7806 0x6827 0x18c0 0x08e1 0x3882 0x28a3
312 0xcb7d 0xdb5c 0xeb3f 0xfb1e 0x8bf9 0x9bd8 0xabbb 0xbb9a
313 0x4a75 0x5a54 0x6a37 0x7a16 0x0af1 0x1ad0 0x2ab3 0x3a92
314 0xfd2e 0xed0f 0xdd6c 0xcd4d 0xbdaa 0xad8b 0x9de8 0x8dc9
315 0x7c26 0x6c07 0x5c64 0x4c45 0x3ca2 0x2c83 0x1ce0 0x0cc1
316 0xef1f 0xff3e 0xcf5d 0xdf7c 0xaf9b 0xbfba 0x8fd9 0x9ff8
317 0x6e17 0x7e36 0x4e55 0x5e74 0x2e93 0x3eb2 0x0ed1 0x1ef0
318}
319
320proc ::redis_cluster::crc16 {s} {
321 set s [encoding convertto ascii $s]
322 set crc 0
323 foreach char [split $s {}] {
324 scan $char %c byte
325 set crc [expr {(($crc<<8)&0xffff) ^ [lindex $::redis_cluster::XMODEMCRC16Lookup [expr {(($crc>>8)^$byte) & 0xff}]]}]
326 }
327 return $crc
328}
329
330# Hash a single key returning the slot it belongs to, Implemented hash
331# tags as described in the Redis Cluster specification.
332proc ::redis_cluster::hash {key} {
333 set keylen [string length $key]
334 set s {}
335 set e {}
336 for {set s 0} {$s < $keylen} {incr s} {
337 if {[string index $key $s] eq "\{"} break
338 }
339
340 if {[expr {$s == $keylen}]} {
341 set res [expr {[crc16 $key] & 16383}]
342 return $res
343 }
344
345 for {set e [expr {$s+1}]} {$e < $keylen} {incr e} {
346 if {[string index $key $e] == "\}"} break
347 }
348
349 if {$e == $keylen || $e == [expr {$s+1}]} {
350 set res [expr {[crc16 $key] & 16383}]
351 return $res
352 }
353
354 set key_sub [string range $key [expr {$s+1}] [expr {$e-1}]]
355 return [expr {[crc16 $key_sub] & 16383}]
356}
357
358# Return the slot the specified keys hash to.
359# If the keys hash to multiple slots, an empty string is returned to
360# signal that the command can't be run in Redis Cluster.
361proc ::redis_cluster::get_slot_from_keys {keys} {
362 set slot {}
363 foreach k $keys {
364 set s [::redis_cluster::hash $k]
365 if {$slot eq {}} {
366 set slot $s
367 } elseif {$slot != $s} {
368 return {} ; # Error
369 }
370 }
371 return $slot
372}