X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/a54d9805ee1c46d574aab8e349fd778d3a4bc67b..4ff34b6adb9f5f9daad1f27e3aac8e016554b86c:/src/redis-trib.rb diff --git a/src/redis-trib.rb b/src/redis-trib.rb index cd35587f..31fc5230 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -14,9 +14,10 @@ class ClusterNode def initialize(addr) s = addr.split(":") if s.length != 2 - puts "Invalid node name #{node}" + puts "Invalid node name #{addr}" exit 1 end + @r = nil @host = s[0] @port = s[1] @slots = {} @@ -27,14 +28,16 @@ class ClusterNode "#{@host}:#{@port}" end - def connect + def connect(o={}) xputs "Connecting to node #{self}: " begin - @r = Redis.new(:host => @ost, :port => @port) + @r = Redis.new(:host => @host, :port => @port) @r.ping rescue puts "ERROR" puts "Sorry, can't connect to node #{self}" + exit 1 if o[:abort] + @r = nil end puts "OK" end @@ -75,20 +78,49 @@ class ClusterNode @dirty = false end - def info - slots = @slots.map{|k,v| k}.reduce{|a,b| - a = [(a..a)] if !a.is_a?(Array) - if b == (a[-1].last)+1 - a[-1] = (a[-1].first)..b - a + def info_string + # We want to display the hash slots assigned to this node + # as ranges, like in: "1-5,8-9,20-25,30" + # + # 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] + slots = @slots.keys.sort + + # 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. + slots.map!{|x| x..x} + + # 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)] else - a << (b..b) + a + [b] end - }.map{|x| - (x.first == x.last) ? x.first.to_s : "#{x.first}-#{x.last}" + } + + # 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}" }.join(",") + "#{self.to_s.ljust(25)} slots:#{slots}" end + + def info + { + :host => @host, + :port => @port, + :slots => @slots, + :dirty => @dirty + } + end def is_dirty? @dirty @@ -112,23 +144,13 @@ class RedisTrib end end - def create_cluster - puts "Creating cluster" - ARGV[1..-1].each{|n| - node = ClusterNode.new(n) - node.connect - node.assert_cluster - node.assert_empty - @nodes << node - } - puts "Performing hash slots allocation on #{@nodes.length} nodes..." - alloc_slots + def add_node(node) + @nodes << node + end + + def check_cluster + puts "Performing Cluster Check (using node #{@nodes[0]})" 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 end def alloc_slots @@ -151,11 +173,21 @@ class RedisTrib def show_nodes @nodes.each{|n| - puts n.info + puts n.info_string } end def join_cluster + # 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]) + } end def yes_or_die(msg) @@ -166,10 +198,41 @@ class RedisTrib exit 1 end end + + # redis-trib subcommands implementations + + def check_cluster_cmd + node = ClusterNode.new(ARGV[1]) + node.connect(:abort => true) + node.assert_cluster + add_node(node) + check_cluster + end + + 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 end COMMANDS={ - "create-cluster" => ["create_cluster", -2, "node1_addr node2_addr ..."] + "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"], + "check" => ["check_cluster_cmd", 2, "host:port"] } # Sanity check