]>
git.saurik.com Git - redis.git/blob - src/redis-trib.rb
6 ClusterHashSlots
= 4096
17 puts
"Invalid node name #{addr}"
32 xputs
"Connecting to node #{self}: "
34 @r = Redis
.new(:host => @host, :port => @port)
38 puts
"Sorry, can't connect to node #{self}"
47 if !info
["cluster_enabled"] || info
["cluster_enabled"].to_i
== 0
48 puts
"Error: Node #{self} is not configured as a cluster node."
54 if !
(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) ||
56 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."
77 @r.cluster("addslots",*new
)
82 # We want to display the hash slots assigned to this node
83 # as ranges, like in: "1-5,8-9,20-25,30"
85 # Note: this could be easily written without side effects,
86 # we use 'slots' just to split the computation into steps.
88 # First step: we want an increasing array of integers
89 # for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
90 slots
= @slots.keys
.sort
92 # As we want to aggregate adiacent slots we convert all the
93 # slot integers into ranges (with just one element)
94 # So we have something like [1..1,2..2, ... and so forth.
97 # Finally we group ranges with adiacent elements.
98 slots
= slots
.reduce([]) {|a
,b
|
99 if !a
.empty
? && b
.first
== (a
[-1].last
)+
1
100 a
[0..-2] +
[(a
[-1].first
)..(b
.last
)]
106 # Now our task is easy, we just convert ranges with just one
107 # element into a number, and a real range into a start-end format.
108 # Finally we join the array using the comma as separator.
109 slots
= slots
.map
{|x
|
110 x
.count
== 1 ? x
.first
.to_s
: "#{x.first}-#{x.last}"
113 "#{self.to_s.ljust(25)} slots:#{slots}"
139 def check_arity(req_args
, num_args
)
140 if ((req_args
> 0 and num_args !
= req_args
) ||
141 (req_args
< 0 and num_args
< req_args
.abs
))
142 puts
"Wrong number of arguments for specified sub command"
152 puts
"Performing Cluster Check (using node #{@nodes[0]})"
157 slots_per_node
= ClusterHashSlots
/@nodes.length
160 first
= i
*slots_per_node
161 last
= first+slots_per_node-1
162 last
= ClusterHashSlots-1
if i
== @nodes.length-1
163 n
.add_slots first
..last
168 def flush_nodes_config
181 # We use a brute force approach to make sure the node will meet
182 # each other, that is, sending CLUSTER MEET messages to all the nodes
183 # about the very same node.
184 # Thanks to gossip this information should propagate across all the
185 # cluster in a matter of seconds.
188 if !first
then first
= n
.info
; next; end # Skip the first node
189 n
.r
.cluster("meet",first
[:host],first
[:port])
194 print
"#{msg} (type 'yes' to accept): "
196 if !
(STDIN.gets
.chomp
.downcase
== "yes")
202 # redis-trib subcommands implementations
204 def check_cluster_cmd
205 node
= ClusterNode
.new(ARGV[1])
206 node
.connect(:abort => true)
212 def create_cluster_cmd
213 puts
"Creating cluster"
215 node
= ClusterNode
.new(n
)
216 node
.connect(:abort => true)
221 puts
"Performing hash slots allocation on #{@nodes.length} nodes..."
224 yes_or_die
"Can I set the above configuration?"
226 puts
"** Nodes configuration updated"
227 puts
"** Sending CLUSTER MEET messages to join the cluster"
234 "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
235 "check" => ["check_cluster_cmd", 2, "host:port"]
240 puts
"Usage: redis-trib <command> <arguments ...>"
243 puts
" #{k.ljust(20)} #{v[2]}"
250 cmd_spec
= COMMANDS
[ARGV[0].downcase
]
252 puts
"Unknown redis-trib subcommand '#{ARGV[0]}'"
255 rt
.check_arity(cmd_spec
[1],ARGV.length
)