]> git.saurik.com Git - redis.git/commitdiff
initial node loading info code for redis-trib
authorantirez <antirez@gmail.com>
Wed, 28 Sep 2011 17:08:35 +0000 (19:08 +0200)
committerantirez <antirez@gmail.com>
Wed, 28 Sep 2011 17:08:35 +0000 (19:08 +0200)
src/redis-trib.rb

index 31fc5230550852a5fe6848d2b0c9064990b4cdc0..c8daac3767d8dd5425eb733589a1df4caa634fa0 100755 (executable)
@@ -22,6 +22,8 @@ class ClusterNode
         @port = s[1]
         @slots = {}
         @dirty = false
+        @info = nil
+        @friends = []
     end
 
     def to_s
@@ -29,6 +31,7 @@ class ClusterNode
     end
 
     def connect(o={})
+        return if @r
         xputs "Connecting to node #{self}: "
         begin
             @r = Redis.new(:host => @host, :port => @port)
@@ -58,6 +61,39 @@ 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
+                @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
+                }
+                @dirty = false
+            elsif o[:getfriends]
+                @friends << info
+            end
+        }
+    end
+
     def add_slots(slots)
         slots.each{|s|
             @slots[s] = :new
@@ -205,6 +241,7 @@ class RedisTrib
         node = ClusterNode.new(ARGV[1])
         node.connect(:abort => true)
         node.assert_cluster
+        node.load_info
         add_node(node)
         check_cluster
     end