]>
git.saurik.com Git - redis.git/blob - src/redis-trib.rb
6 ClusterHashSlots
= 4096
17 puts
"Invalid node name #{addr}"
43 xputs
"Connecting to node #{self}: "
45 @r = Redis
.new(:host => @host, :port => @port)
49 puts
"Sorry, can't connect to node #{self}"
58 if !info
["cluster_enabled"] || info
["cluster_enabled"].to_i
== 0
59 puts
"Error: Node #{self} is not configured as a cluster node."
65 if !
(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) ||
67 puts
"Error: Node #{self} is not empty. Either the node already knows other nodes (check with nodes-info) or contains some key in database 0."
74 nodes
= @r.cluster("nodes").split("\n")
76 # name addr flags role ping_sent ping_recv link_status slots
77 name
,addr
,flags
,role
,ping_sent
,ping_recv
,link_status
,slots
= n
.split(" ")
81 :flags => flags
.split(","),
83 :ping_sent => ping_sent
.to_i
,
84 :ping_recv => ping_recv
.to_i
,
85 :link_status => link_status
87 if info
[:flags].index("myself")
90 slots
.split(",").each
{|s
|
92 start
,stop
= s
.split("-")
93 self.add_slots((start
.to_i
)..(stop
.to_i
))
95 self.add_slots((s
.to_i
)..(s
.to_i
))
112 def flush_node_config
121 @r.cluster("addslots",*new
)
126 # We want to display the hash slots assigned to this node
127 # as ranges, like in: "1-5,8-9,20-25,30"
129 # Note: this could be easily written without side effects,
130 # we use 'slots' just to split the computation into steps.
132 # First step: we want an increasing array of integers
133 # for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
134 slots
= @slots.keys
.sort
136 # As we want to aggregate adiacent slots we convert all the
137 # slot integers into ranges (with just one element)
138 # So we have something like [1..1,2..2, ... and so forth.
141 # Finally we group ranges with adiacent elements.
142 slots
= slots
.reduce([]) {|a
,b
|
143 if !a
.empty
? && b
.first
== (a
[-1].last
)+
1
144 a
[0..-2] +
[(a
[-1].first
)..(b
.last
)]
150 # Now our task is easy, we just convert ranges with just one
151 # element into a number, and a real range into a start-end format.
152 # Finally we join the array using the comma as separator.
153 slots
= slots
.map
{|x
|
154 x
.count
== 1 ? x
.first
.to_s
: "#{x.first}-#{x.last}"
157 "#{self.to_s.ljust(25)} slots:#{slots}"
183 def check_arity(req_args
, num_args
)
184 if ((req_args
> 0 and num_args !
= req_args
) ||
185 (req_args
< 0 and num_args
< req_args
.abs
))
186 puts
"Wrong number of arguments for specified sub command"
196 puts
"Performing Cluster Check (using node #{@nodes[0]})"
198 # Check if all the slots are covered
201 slots
= slots
.merge(n
.slots
)
203 if slots
.length
== 4096
204 puts
"[OK] All 4096 slots covered."
206 puts
"[ERR] Not all 4096 slots are covered by nodes."
211 slots_per_node
= ClusterHashSlots
/@nodes.length
214 first
= i
*slots_per_node
215 last
= first+slots_per_node-1
216 last
= ClusterHashSlots-1
if i
== @nodes.length-1
217 n
.add_slots first
..last
222 def flush_nodes_config
235 # We use a brute force approach to make sure the node will meet
236 # each other, that is, sending CLUSTER MEET messages to all the nodes
237 # about the very same node.
238 # Thanks to gossip this information should propagate across all the
239 # cluster in a matter of seconds.
242 if !first
then first
= n
.info
; next; end # Skip the first node
243 n
.r
.cluster("meet",first
[:host],first
[:port])
248 print
"#{msg} (type 'yes' to accept): "
250 if !
(STDIN.gets
.chomp
.downcase
== "yes")
256 # redis-trib subcommands implementations
258 def check_cluster_cmd
259 node
= ClusterNode
.new(ARGV[1])
260 node
.connect(:abort => true)
262 node
.load_info(:getfriends => true)
264 node
.friends
.each
{|f
|
265 fnode
= ClusterNode
.new(f
[:addr])
273 def create_cluster_cmd
274 puts
"Creating cluster"
276 node
= ClusterNode
.new(n
)
277 node
.connect(:abort => true)
282 puts
"Performing hash slots allocation on #{@nodes.length} nodes..."
285 yes_or_die
"Can I set the above configuration?"
287 puts
"** Nodes configuration updated"
288 puts
"** Sending CLUSTER MEET messages to join the cluster"
295 "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
296 "check" => ["check_cluster_cmd", 2, "host:port"]
301 puts
"Usage: redis-trib <command> <arguments ...>"
304 puts
" #{k.ljust(20)} #{v[2]}"
311 cmd_spec
= COMMANDS
[ARGV[0].downcase
]
313 puts
"Unknown redis-trib subcommand '#{ARGV[0]}'"
316 rt
.check_arity(cmd_spec
[1],ARGV.length
)