]>
git.saurik.com Git - redis.git/blob - src/redis-trib.rb
9859b16c02b3fca5215475ba29e8e8f8379c4f92
6 ClusterHashSlots
= 4096
17 puts
"Invalid node name #{addr}"
25 @dirty = false # True if we need to flush slots info into node.
38 "#{@info[:host]}:#{@info[:port]}"
43 xputs
"Connecting to node #{self}: "
45 @r = Redis
.new(:host => @info[:host], :port => @info[: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")
88 @info = @info.merge(info
)
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
))
99 @r.cluster("info").split("\n").each
{|e
|
102 if k !
= :cluster_state
116 @info[:slots][s
] = :new
121 def flush_node_config
124 @info[:slots].each
{|s
,val
|
127 @info[:slots][s
] = true
130 @r.cluster("addslots",*new
)
135 # We want to display the hash slots assigned to this node
136 # as ranges, like in: "1-5,8-9,20-25,30"
138 # Note: this could be easily written without side effects,
139 # we use 'slots' just to split the computation into steps.
141 # First step: we want an increasing array of integers
142 # for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
143 slots
= @info[:slots].keys
.sort
145 # As we want to aggregate adiacent slots we convert all the
146 # slot integers into ranges (with just one element)
147 # So we have something like [1..1,2..2, ... and so forth.
150 # Finally we group ranges with adiacent elements.
151 slots
= slots
.reduce([]) {|a
,b
|
152 if !a
.empty
? && b
.first
== (a
[-1].last
)+
1
153 a
[0..-2] +
[(a
[-1].first
)..(b
.last
)]
159 # Now our task is easy, we just convert ranges with just one
160 # element into a number, and a real range into a start-end format.
161 # Finally we join the array using the comma as separator.
162 slots
= slots
.map
{|x
|
163 x
.count
== 1 ? x
.first
.to_s
: "#{x.first}-#{x.last}"
166 "#{self.to_s.ljust(25)} slots:#{slots}"
187 def check_arity(req_args
, num_args
)
188 if ((req_args
> 0 and num_args !
= req_args
) ||
189 (req_args
< 0 and num_args
< req_args
.abs
))
190 puts
"Wrong number of arguments for specified sub command"
200 puts
"Performing Cluster Check (using node #{@nodes[0]})"
202 # Check if all the slots are covered
205 slots
= slots
.merge(n
.slots
)
207 if slots
.length
== 4096
208 puts
"[OK] All 4096 slots covered."
210 puts
"[ERR] Not all 4096 slots are covered by nodes."
215 slots_per_node
= ClusterHashSlots
/@nodes.length
218 first
= i
*slots_per_node
219 last
= first+slots_per_node-1
220 last
= ClusterHashSlots-1
if i
== @nodes.length-1
221 n
.add_slots first
..last
226 def flush_nodes_config
239 # We use a brute force approach to make sure the node will meet
240 # each other, that is, sending CLUSTER MEET messages to all the nodes
241 # about the very same node.
242 # Thanks to gossip this information should propagate across all the
243 # cluster in a matter of seconds.
246 if !first
then first
= n
.info
; next; end # Skip the first node
247 n
.r
.cluster("meet",first
[:host],first
[:port])
252 print
"#{msg} (type 'yes' to accept): "
254 if !
(STDIN.gets
.chomp
.downcase
== "yes")
260 # redis-trib subcommands implementations
262 def check_cluster_cmd
263 node
= ClusterNode
.new(ARGV[1])
264 node
.connect(:abort => true)
266 node
.load_info(:getfriends => true)
268 node
.friends
.each
{|f
|
269 fnode
= ClusterNode
.new(f
[:addr])
277 def create_cluster_cmd
278 puts
"Creating cluster"
280 node
= ClusterNode
.new(n
)
281 node
.connect(:abort => true)
286 puts
"Performing hash slots allocation on #{@nodes.length} nodes..."
289 yes_or_die
"Can I set the above configuration?"
291 puts
"** Nodes configuration updated"
292 puts
"** Sending CLUSTER MEET messages to join the cluster"
299 "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
300 "check" => ["check_cluster_cmd", 2, "host:port"]
305 puts
"Usage: redis-trib <command> <arguments ...>"
308 puts
" #{k.ljust(20)} #{v[2]}"
315 cmd_spec
= COMMANDS
[ARGV[0].downcase
]
317 puts
"Unknown redis-trib subcommand '#{ARGV[0]}'"
320 rt
.check_arity(cmd_spec
[1],ARGV.length
)