]> git.saurik.com Git - redis.git/blobdiff - src/redis-trib.rb
redis-trib: initial slots allocation strategy, still to improve for better balancing...
[redis.git] / src / redis-trib.rb
index 7f83f78ef1f0ad24297f8dcbc89c6a8f7180c5ec..083f9740ffa2a9bd2fc09eddc59e66b2e1e14fbc 100755 (executable)
@@ -14,27 +14,41 @@ 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
-        @host = s[0]
-        @port = s[1]
-        @slots = {}
-        @dirty = false
+        @r = nil
+        @info = {}
+        @info[:host] = s[0]
+        @info[:port] = s[1]
+        @info[:slots] = {}
+        @dirty = false # True if we need to flush slots info into node.
+        @friends = []
+    end
+
+    def friends
+        @friends
+    end
+
+    def slots 
+        @info[:slots]
     end
 
     def to_s
-        "#{@host}:#{@port}"
+        "#{@info[:host]}:#{@info[:port]}"
     end
 
-    def connect
+    def connect(o={})
+        return if @r
         xputs "Connecting to node #{self}: "
         begin
-            @r = Redis.new(:host => @ost, :port => @port)
+            @r = Redis.new(:host => @info[:host], :port => @info[: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
@@ -55,9 +69,52 @@ class ClusterNode
         end
     end
 
+    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")
+                @info = @info.merge(info)
+                @info[:slots] = {}
+                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
+                } if slots
+                @dirty = false
+                @r.cluster("info").split("\n").each{|e|    
+                    k,v=e.split(":")
+                    k = k.to_sym
+                    v.chop!
+                    if k != :cluster_state
+                        @info[k] = v.to_i
+                    else
+                        @info[k] = v
+                    end
+                }
+            elsif o[:getfriends]
+                @friends << info
+            end
+        }
+    end
+
     def add_slots(slots)
         slots.each{|s|
-            @slots[s] = :new
+            @info[:slots][s] = :new
         }
         @dirty = true
     end
@@ -65,10 +122,10 @@ class ClusterNode
     def flush_node_config
         return if !@dirty
         new = []
-        @slots.each{|s,val|
+        @info[:slots].each{|s,val|
             if val == :new
                 new << s
-                @slots[s] = true
+                @info[:slots][s] = true
             end
         }
         @r.cluster("addslots",*new)
@@ -76,27 +133,42 @@ class ClusterNode
     end
 
     def info_string
-        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
+        # 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 = @info[: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}"
+
+        "[#{@info[:cluster_state].upcase}] #{self.info[:name]} #{self.to_s.ljust(25)} slots:#{slots}"
     end
 
     def info
-        {
-            :host => @host,
-            :port => @port,
-            :slots => @slots,
-            :dirty => @dirty
-        }
+        @info
     end
     
     def is_dirty?
@@ -121,28 +193,33 @@ 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
+    def add_node(node)
+        @nodes << node
+    end
+
+    def get_node_by_name(name)
+        @nodes.each{|n|
+            return n if n.info[:name] == name.downcase
         }
-        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
+        return nil
     end
 
     def check_cluster
-        puts "Check if the cluster looks sane"
+        puts "Performing Cluster Check (using node #{@nodes[0]})"
+        errors = []
+        show_nodes
+        # 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
+            errors << "[ERR] Not all 4096 slots are covered by nodes."
+            puts errors[-1]
+        end
+        return errors
     end
 
     def alloc_slots
@@ -190,11 +267,135 @@ class RedisTrib
             exit 1
         end
     end
+
+    def load_cluster_info_from_node(nodeaddr)
+        node = ClusterNode.new(ARGV[1])
+        node.connect(:abort => true)
+        node.assert_cluster
+        node.load_info(:getfriends => true)
+        add_node(node)
+        node.friends.each{|f|
+            fnode = ClusterNode.new(f[:addr])
+            fnode.connect()
+            fnode.load_info()
+            add_node(fnode)
+        }
+    end
+
+    # 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 = []
+        sources.each{|s|
+            # Every node will provide a number of slots proportional to the
+            # slots it has assigned.
+            n = (numslots.to_f/4096*s.slots.length).ceil
+            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|
+            puts "Moving slot #{e[:slot]} from #{e[:source].info[:name]}"
+        }
+    end
+
+    # redis-trib subcommands implementations
+
+    def check_cluster_cmd   
+        load_cluster_info_from_node(ARGV[1])
+        check_cluster
+    end
+
+    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
+        numslots = 0
+        while numslots <= 0 or numslots > 4096
+            print "How many slots do you want to move (from 1 to 4096)?"
+            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
+        end
+        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)
+        show_reshard_table(reshard_table)
+    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" => ["create_cluster", -2, "host1:port host2:port ... hostN:port"],
-    "check" =>  ["check_cluster", 1, "host:port"]
+    "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
+    "check" =>  ["check_cluster_cmd", 2, "host:port"],
+    "reshard" =>  ["reshard_cluster_cmd", 2, "host:port"]
 }
 
 # Sanity check