2011-04-11 10:41:06 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'redis'
|
|
|
|
|
2011-04-12 11:06:33 -04:00
|
|
|
ClusterHashSlots = 4096
|
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
def xputs(s)
|
|
|
|
printf s
|
|
|
|
STDOUT.flush
|
|
|
|
end
|
2011-04-11 10:41:06 -04:00
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
class ClusterNode
|
|
|
|
def initialize(addr)
|
|
|
|
s = addr.split(":")
|
2011-04-11 10:41:06 -04:00
|
|
|
if s.length != 2
|
2011-04-13 12:50:50 -04:00
|
|
|
puts "Invalid node name #{addr}"
|
2011-04-11 10:41:06 -04:00
|
|
|
exit 1
|
|
|
|
end
|
2011-04-13 12:40:51 -04:00
|
|
|
@r = nil
|
2011-09-29 08:44:15 -04:00
|
|
|
@info = {}
|
|
|
|
@info[:host] = s[0]
|
|
|
|
@info[:port] = s[1]
|
|
|
|
@info[:slots] = {}
|
|
|
|
@dirty = false # True if we need to flush slots info into node.
|
2011-09-28 13:08:35 -04:00
|
|
|
@friends = []
|
2011-04-11 10:41:06 -04:00
|
|
|
end
|
|
|
|
|
2011-09-29 06:50:46 -04:00
|
|
|
def friends
|
|
|
|
@friends
|
|
|
|
end
|
|
|
|
|
|
|
|
def slots
|
2011-09-29 08:44:15 -04:00
|
|
|
@info[:slots]
|
2011-09-29 06:50:46 -04:00
|
|
|
end
|
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
def to_s
|
2011-09-29 08:44:15 -04:00
|
|
|
"#{@info[:host]}:#{@info[:port]}"
|
2011-04-11 10:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 12:40:51 -04:00
|
|
|
def connect(o={})
|
2011-09-28 13:08:35 -04:00
|
|
|
return if @r
|
2011-04-11 10:58:47 -04:00
|
|
|
xputs "Connecting to node #{self}: "
|
2011-04-11 10:41:06 -04:00
|
|
|
begin
|
2011-09-29 08:44:15 -04:00
|
|
|
@r = Redis.new(:host => @info[:host], :port => @info[:port])
|
2011-04-11 10:58:47 -04:00
|
|
|
@r.ping
|
2011-04-11 10:41:06 -04:00
|
|
|
rescue
|
|
|
|
puts "ERROR"
|
2011-04-11 10:58:47 -04:00
|
|
|
puts "Sorry, can't connect to node #{self}"
|
2011-04-13 12:40:51 -04:00
|
|
|
exit 1 if o[:abort]
|
|
|
|
@r = nil
|
2011-04-11 10:41:06 -04:00
|
|
|
end
|
|
|
|
puts "OK"
|
|
|
|
end
|
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
def assert_cluster
|
|
|
|
info = @r.info
|
|
|
|
if !info["cluster_enabled"] || info["cluster_enabled"].to_i == 0
|
|
|
|
puts "Error: Node #{self} is not configured as a cluster node."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-11 12:26:00 -04:00
|
|
|
def assert_empty
|
|
|
|
if !(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) ||
|
|
|
|
(@r.info['db0'])
|
|
|
|
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."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-28 13:08:35 -04:00
|
|
|
def load_info(o={})
|
|
|
|
self.connect
|
|
|
|
nodes = @r.cluster("nodes").split("\n")
|
|
|
|
nodes.each{|n|
|
|
|
|
# name addr flags role ping_sent ping_recv link_status slots
|
|
|
|
name,addr,flags,role,ping_sent,ping_recv,link_status,slots = n.split(" ")
|
|
|
|
info = {
|
|
|
|
:name => name,
|
|
|
|
:addr => addr,
|
|
|
|
:flags => flags.split(","),
|
|
|
|
:role => role,
|
|
|
|
:ping_sent => ping_sent.to_i,
|
|
|
|
:ping_recv => ping_recv.to_i,
|
|
|
|
:link_status => link_status
|
|
|
|
}
|
|
|
|
if info[:flags].index("myself")
|
2011-09-29 08:44:15 -04:00
|
|
|
@info = @info.merge(info)
|
|
|
|
@info[:slots] = {}
|
2011-09-28 13:08:35 -04:00
|
|
|
slots.split(",").each{|s|
|
|
|
|
if s.index("-")
|
|
|
|
start,stop = s.split("-")
|
|
|
|
self.add_slots((start.to_i)..(stop.to_i))
|
|
|
|
else
|
|
|
|
self.add_slots((s.to_i)..(s.to_i))
|
|
|
|
end
|
2011-09-29 09:31:02 -04:00
|
|
|
} if slots
|
2011-09-28 13:08:35 -04:00
|
|
|
@dirty = false
|
2011-09-29 07:18:09 -04:00
|
|
|
@r.cluster("info").split("\n").each{|e|
|
|
|
|
k,v=e.split(":")
|
|
|
|
k = k.to_sym
|
2011-09-29 09:04:23 -04:00
|
|
|
v.chop!
|
2011-09-29 07:18:09 -04:00
|
|
|
if k != :cluster_state
|
|
|
|
@info[k] = v.to_i
|
|
|
|
else
|
|
|
|
@info[k] = v
|
|
|
|
end
|
|
|
|
}
|
2011-09-28 13:08:35 -04:00
|
|
|
elsif o[:getfriends]
|
|
|
|
@friends << info
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-04-12 11:06:33 -04:00
|
|
|
def add_slots(slots)
|
|
|
|
slots.each{|s|
|
2011-09-29 08:44:15 -04:00
|
|
|
@info[:slots][s] = :new
|
2011-04-12 11:06:33 -04:00
|
|
|
}
|
|
|
|
@dirty = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush_node_config
|
|
|
|
return if !@dirty
|
|
|
|
new = []
|
2011-09-29 08:44:15 -04:00
|
|
|
@info[:slots].each{|s,val|
|
2011-04-12 11:06:33 -04:00
|
|
|
if val == :new
|
|
|
|
new << s
|
2011-09-29 08:44:15 -04:00
|
|
|
@info[:slots][s] = true
|
2011-04-12 11:06:33 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
@r.cluster("addslots",*new)
|
|
|
|
@dirty = false
|
|
|
|
end
|
|
|
|
|
2011-04-13 09:58:05 -04:00
|
|
|
def info_string
|
2011-04-13 12:40:51 -04:00
|
|
|
# We want to display the hash slots assigned to this node
|
2011-04-13 14:17:11 -04:00
|
|
|
# as ranges, like in: "1-5,8-9,20-25,30"
|
2011-04-13 12:40:51 -04:00
|
|
|
#
|
|
|
|
# Note: this could be easily written without side effects,
|
|
|
|
# we use 'slots' just to split the computation into steps.
|
|
|
|
|
|
|
|
# First step: we want an increasing array of integers
|
|
|
|
# for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
|
2011-09-29 08:44:15 -04:00
|
|
|
slots = @info[:slots].keys.sort
|
2011-04-13 12:40:51 -04:00
|
|
|
|
|
|
|
# As we want to aggregate adiacent slots we convert all the
|
|
|
|
# slot integers into ranges (with just one element)
|
|
|
|
# So we have something like [1..1,2..2, ... and so forth.
|
2011-04-13 12:46:29 -04:00
|
|
|
slots.map!{|x| x..x}
|
2011-04-13 12:40:51 -04:00
|
|
|
|
|
|
|
# Finally we group ranges with adiacent elements.
|
|
|
|
slots = slots.reduce([]) {|a,b|
|
|
|
|
if !a.empty? && b.first == (a[-1].last)+1
|
|
|
|
a[0..-2] + [(a[-1].first)..(b.last)]
|
2011-04-12 11:06:33 -04:00
|
|
|
else
|
2011-04-13 12:40:51 -04:00
|
|
|
a + [b]
|
2011-04-12 11:06:33 -04:00
|
|
|
end
|
2011-04-13 12:40:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Now our task is easy, we just convert ranges with just one
|
|
|
|
# element into a number, and a real range into a start-end format.
|
|
|
|
# Finally we join the array using the comma as separator.
|
|
|
|
slots = slots.map{|x|
|
|
|
|
x.count == 1 ? x.first.to_s : "#{x.first}-#{x.last}"
|
2011-04-12 11:06:33 -04:00
|
|
|
}.join(",")
|
2011-04-13 12:40:51 -04:00
|
|
|
|
2011-09-29 10:06:49 -04:00
|
|
|
"[#{@info[:cluster_state].upcase}] #{self.info[:name]} #{self.to_s.ljust(25)} slots:#{slots}"
|
2011-04-12 11:06:33 -04:00
|
|
|
end
|
2011-04-13 09:58:05 -04:00
|
|
|
|
|
|
|
def info
|
2011-09-29 08:44:15 -04:00
|
|
|
@info
|
2011-04-13 09:58:05 -04:00
|
|
|
end
|
2011-04-12 11:06:33 -04:00
|
|
|
|
|
|
|
def is_dirty?
|
|
|
|
@dirty
|
|
|
|
end
|
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
def r
|
|
|
|
@r
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedisTrib
|
2011-04-12 11:06:33 -04:00
|
|
|
def initialize
|
|
|
|
@nodes = []
|
|
|
|
end
|
|
|
|
|
2011-04-11 10:58:47 -04:00
|
|
|
def check_arity(req_args, num_args)
|
|
|
|
if ((req_args > 0 and num_args != req_args) ||
|
|
|
|
(req_args < 0 and num_args < req_args.abs))
|
|
|
|
puts "Wrong number of arguments for specified sub command"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-13 12:40:51 -04:00
|
|
|
def add_node(node)
|
|
|
|
@nodes << node
|
|
|
|
end
|
|
|
|
|
2011-09-29 10:06:49 -04:00
|
|
|
def get_node_by_name(name)
|
|
|
|
@nodes.each{|n|
|
|
|
|
return n if n.info[:name] == name.downcase
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2011-04-13 09:58:05 -04:00
|
|
|
def check_cluster
|
2011-04-14 03:41:22 -04:00
|
|
|
puts "Performing Cluster Check (using node #{@nodes[0]})"
|
2011-09-29 10:06:49 -04:00
|
|
|
errors = []
|
2011-04-13 12:40:51 -04:00
|
|
|
show_nodes
|
2011-09-29 06:50:46 -04:00
|
|
|
# Check if all the slots are covered
|
|
|
|
slots = {}
|
|
|
|
@nodes.each{|n|
|
|
|
|
slots = slots.merge(n.slots)
|
|
|
|
}
|
|
|
|
if slots.length == 4096
|
|
|
|
puts "[OK] All 4096 slots covered."
|
|
|
|
else
|
2011-09-29 10:06:49 -04:00
|
|
|
errors << "[ERR] Not all 4096 slots are covered by nodes."
|
|
|
|
puts errors[-1]
|
2011-09-29 06:50:46 -04:00
|
|
|
end
|
2011-09-29 10:06:49 -04:00
|
|
|
return errors
|
2011-04-12 11:06:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def alloc_slots
|
|
|
|
slots_per_node = ClusterHashSlots/@nodes.length
|
|
|
|
i = 0
|
|
|
|
@nodes.each{|n|
|
|
|
|
first = i*slots_per_node
|
|
|
|
last = first+slots_per_node-1
|
|
|
|
last = ClusterHashSlots-1 if i == @nodes.length-1
|
|
|
|
n.add_slots first..last
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush_nodes_config
|
|
|
|
@nodes.each{|n|
|
|
|
|
n.flush_node_config
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_nodes
|
|
|
|
@nodes.each{|n|
|
2011-04-13 09:58:05 -04:00
|
|
|
puts n.info_string
|
2011-04-12 11:06:33 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def join_cluster
|
2011-04-13 09:58:05 -04:00
|
|
|
# We use a brute force approach to make sure the node will meet
|
|
|
|
# each other, that is, sending CLUSTER MEET messages to all the nodes
|
|
|
|
# about the very same node.
|
|
|
|
# Thanks to gossip this information should propagate across all the
|
|
|
|
# cluster in a matter of seconds.
|
|
|
|
first = false
|
|
|
|
@nodes.each{|n|
|
|
|
|
if !first then first = n.info; next; end # Skip the first node
|
|
|
|
n.r.cluster("meet",first[:host],first[:port])
|
|
|
|
}
|
2011-04-12 11:06:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def yes_or_die(msg)
|
|
|
|
print "#{msg} (type 'yes' to accept): "
|
|
|
|
STDOUT.flush
|
|
|
|
if !(STDIN.gets.chomp.downcase == "yes")
|
|
|
|
puts "Aborting..."
|
|
|
|
exit 1
|
|
|
|
end
|
2011-04-11 10:41:06 -04:00
|
|
|
end
|
2011-04-14 03:41:22 -04:00
|
|
|
|
2011-09-29 09:45:10 -04:00
|
|
|
def load_cluster_info_from_node(nodeaddr)
|
2011-04-14 03:41:22 -04:00
|
|
|
node = ClusterNode.new(ARGV[1])
|
|
|
|
node.connect(:abort => true)
|
|
|
|
node.assert_cluster
|
2011-09-29 06:50:46 -04:00
|
|
|
node.load_info(:getfriends => true)
|
2011-04-14 03:41:22 -04:00
|
|
|
add_node(node)
|
2011-09-29 06:50:46 -04:00
|
|
|
node.friends.each{|f|
|
|
|
|
fnode = ClusterNode.new(f[:addr])
|
|
|
|
fnode.connect()
|
|
|
|
fnode.load_info()
|
|
|
|
add_node(fnode)
|
|
|
|
}
|
2011-09-29 09:45:10 -04:00
|
|
|
end
|
|
|
|
|
2011-09-30 12:28:51 -04:00
|
|
|
# Given a list of source nodes return a "resharding plan"
|
|
|
|
# with what slots to move in order to move "numslots" slots to another
|
|
|
|
# instance.
|
|
|
|
def compute_reshard_table(sources,numslots)
|
|
|
|
moved = []
|
2011-09-30 12:41:25 -04:00
|
|
|
# Sort from bigger to smaller instance, for two reasons:
|
|
|
|
# 1) If we take less slots than instanes it is better to start getting from
|
|
|
|
# the biggest instances.
|
|
|
|
# 2) We take one slot more from the first instance in the case of not perfect
|
|
|
|
# divisibility. Like we have 3 nodes and need to get 10 slots, we take
|
|
|
|
# 4 from the first, and 3 from the rest. So the biggest is always the first.
|
|
|
|
sources = sources.sort{|a,b| b.slots.length <=> a.slots.length}
|
|
|
|
sources.each_with_index{|s,i|
|
2011-09-30 12:28:51 -04:00
|
|
|
# Every node will provide a number of slots proportional to the
|
|
|
|
# slots it has assigned.
|
2011-09-30 12:41:25 -04:00
|
|
|
n = (numslots.to_f/4096*s.slots.length)
|
|
|
|
if i == 0
|
|
|
|
n = n.ceil
|
|
|
|
else
|
|
|
|
n = n.floor
|
|
|
|
end
|
2011-09-30 12:28:51 -04:00
|
|
|
s.slots.keys.sort[(0...n)].each{|slot|
|
|
|
|
if moved.length < numslots
|
|
|
|
moved << {:source => s, :slot => slot}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return moved
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_reshard_table(table)
|
|
|
|
table.each{|e|
|
2011-09-30 13:21:36 -04:00
|
|
|
puts " Moving slot #{e[:slot]} from #{e[:source].info[:name]}"
|
2011-09-30 12:28:51 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-09-30 13:21:36 -04:00
|
|
|
def move_slot(source,target,slot)
|
|
|
|
# We start marking the slot as importing in the destination node,
|
|
|
|
# and the slot as migrating in the target host. Note that the order of
|
|
|
|
# the operations is important, as otherwise a client may be redirected to
|
|
|
|
# the target node that does not yet know it is importing this slot.
|
|
|
|
target.r("cluster","setslot",slot,"importing",source.info[:name])
|
|
|
|
source.r("cluster","setslot",slot,"migrating",source.info[:name])
|
|
|
|
# Migrate all the keys from source to target using the MIGRATE command
|
|
|
|
end
|
|
|
|
|
2011-09-29 09:45:10 -04:00
|
|
|
# redis-trib subcommands implementations
|
|
|
|
|
|
|
|
def check_cluster_cmd
|
|
|
|
load_cluster_info_from_node(ARGV[1])
|
2011-04-14 03:41:22 -04:00
|
|
|
check_cluster
|
|
|
|
end
|
|
|
|
|
2011-09-29 10:06:49 -04:00
|
|
|
def reshard_cluster_cmd
|
|
|
|
load_cluster_info_from_node(ARGV[1])
|
|
|
|
errors = check_cluster
|
|
|
|
if errors.length != 0
|
|
|
|
puts "Please fix your cluster problems before resharding."
|
|
|
|
exit 1
|
|
|
|
end
|
2011-09-30 12:28:51 -04:00
|
|
|
numslots = 0
|
|
|
|
while numslots <= 0 or numslots > 4096
|
2011-09-30 13:21:36 -04:00
|
|
|
print "How many slots do you want to move (from 1 to 4096)? "
|
2011-09-30 12:28:51 -04:00
|
|
|
numslots = STDIN.gets.to_i
|
|
|
|
end
|
|
|
|
target = nil
|
|
|
|
while not target
|
|
|
|
print "What is the receiving node ID? "
|
|
|
|
target = get_node_by_name(STDIN.gets.chop)
|
|
|
|
if not target
|
|
|
|
puts "The specified node is not known, please retry."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
sources = []
|
|
|
|
puts "Please enter all the source node IDs."
|
|
|
|
puts " Type 'all' to use all the nodes as source nodes for the hash slots."
|
|
|
|
puts " Type 'done' once you entered all the source nodes IDs."
|
|
|
|
while true
|
|
|
|
print "Source node ##{sources.length+1}:"
|
|
|
|
line = STDIN.gets.chop
|
|
|
|
src = get_node_by_name(line)
|
|
|
|
if line == "done"
|
|
|
|
if sources.length == 0
|
|
|
|
puts "No source nodes given, operation aborted"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
elsif line == "all"
|
|
|
|
@nodes.each{|n|
|
|
|
|
next if n.info[:name] == target.info[:name]
|
|
|
|
sources << n
|
|
|
|
}
|
|
|
|
break
|
|
|
|
elsif not src
|
|
|
|
puts "The specified node is not known, please retry."
|
|
|
|
elsif src.info[:name] == target.info[:name]
|
|
|
|
puts "It is not possible to use the target node as source node."
|
|
|
|
else
|
|
|
|
sources << src
|
|
|
|
end
|
2011-09-29 10:06:49 -04:00
|
|
|
end
|
2011-09-30 12:28:51 -04:00
|
|
|
puts "\nReady to move #{numslots} slots."
|
|
|
|
puts " Source nodes:"
|
|
|
|
sources.each{|s| puts " "+s.info_string}
|
|
|
|
puts " Destination node:"
|
|
|
|
puts " #{target.info_string}"
|
|
|
|
reshard_table = compute_reshard_table(sources,numslots)
|
2011-09-30 13:21:36 -04:00
|
|
|
puts " Resharding plan:"
|
2011-09-30 12:28:51 -04:00
|
|
|
show_reshard_table(reshard_table)
|
2011-09-30 13:21:36 -04:00
|
|
|
print "Do you want to proceed with the proposed reshard plan (yes/no)? "
|
|
|
|
yesno = STDIN.gets.chop
|
|
|
|
exit(1) if (yesno != "yes")
|
|
|
|
reshard_table.each{|e|
|
|
|
|
move_slot(e[:source],target,e[:slot])
|
|
|
|
}
|
2011-09-29 10:06:49 -04:00
|
|
|
end
|
|
|
|
|
2011-04-14 03:41:22 -04:00
|
|
|
def create_cluster_cmd
|
|
|
|
puts "Creating cluster"
|
|
|
|
ARGV[1..-1].each{|n|
|
|
|
|
node = ClusterNode.new(n)
|
|
|
|
node.connect(:abort => true)
|
|
|
|
node.assert_cluster
|
|
|
|
node.assert_empty
|
|
|
|
add_node(node)
|
|
|
|
}
|
|
|
|
puts "Performing hash slots allocation on #{@nodes.length} nodes..."
|
|
|
|
alloc_slots
|
|
|
|
show_nodes
|
|
|
|
yes_or_die "Can I set the above configuration?"
|
|
|
|
flush_nodes_config
|
|
|
|
puts "** Nodes configuration updated"
|
|
|
|
puts "** Sending CLUSTER MEET messages to join the cluster"
|
|
|
|
join_cluster
|
|
|
|
check_cluster
|
|
|
|
end
|
2011-04-11 10:41:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
COMMANDS={
|
2011-04-14 03:41:22 -04:00
|
|
|
"create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
|
2011-09-29 10:06:49 -04:00
|
|
|
"check" => ["check_cluster_cmd", 2, "host:port"],
|
|
|
|
"reshard" => ["reshard_cluster_cmd", 2, "host:port"]
|
2011-04-11 10:41:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
if ARGV.length == 0
|
|
|
|
puts "Usage: redis-trib <command> <arguments ...>"
|
2011-04-13 04:38:56 -04:00
|
|
|
puts
|
|
|
|
COMMANDS.each{|k,v|
|
|
|
|
puts " #{k.ljust(20)} #{v[2]}"
|
|
|
|
}
|
|
|
|
puts
|
2011-04-11 10:41:06 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
rt = RedisTrib.new
|
|
|
|
cmd_spec = COMMANDS[ARGV[0].downcase]
|
|
|
|
if !cmd_spec
|
|
|
|
puts "Unknown redis-trib subcommand '#{ARGV[0]}'"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
rt.check_arity(cmd_spec[1],ARGV.length)
|
|
|
|
|
|
|
|
# Dispatch
|
|
|
|
rt.send(cmd_spec[0])
|