Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
514ee713
Commit
514ee713
authored
Dec 15, 2015
by
antirez
Browse files
Cluster: allows abbreviated node IDs with rebalance --weight option.
parent
025628bd
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/redis-trib.rb
View file @
514ee713
...
@@ -316,6 +316,7 @@ class RedisTrib
...
@@ -316,6 +316,7 @@ class RedisTrib
xputs msg
xputs msg
end
end
# Return the node with the specified ID or Nil.
def get_node_by_name(name)
def get_node_by_name(name)
@nodes.each{|n|
@nodes.each{|n|
return n if n.info[:name] == name.downcase
return n if n.info[:name] == name.downcase
...
@@ -323,6 +324,21 @@ class RedisTrib
...
@@ -323,6 +324,21 @@ class RedisTrib
return nil
return nil
end
end
# Like get_node_by_name but the specified name can be just the first
# part of the node ID as long as the prefix in unique across the
# cluster.
def get_node_by_abbreviated_name(name)
l = name.length
candidates = []
@nodes.each{|n|
if n.info[:name][0...l] == name.downcase
candidates << n
end
}
return nil if candidates.length != 1
candidates[0]
end
# This function returns the master that has the least number of replicas
# This function returns the master that has the least number of replicas
# in the cluster. If there are multiple masters with the same smaller
# in the cluster. If there are multiple masters with the same smaller
# number of replicas, one at random is returned.
# number of replicas, one at random is returned.
...
@@ -347,7 +363,7 @@ class RedisTrib
...
@@ -347,7 +363,7 @@ class RedisTrib
keys = 0
keys = 0
@nodes.each{|n|
@nodes.each{|n|
if n.has_flag?("master")
if n.has_flag?("master")
puts "#{n} -> #{n.r.dbsize} keys | #{n.slots.length} slots | "+
puts "#{n}
(#{n.info[:name][0...8]}...)
-> #{n.r.dbsize} keys | #{n.slots.length} slots | "+
"#{n.info[:replicas].length} slaves."
"#{n.info[:replicas].length} slaves."
masters += 1
masters += 1
keys += n.r.dbsize
keys += n.r.dbsize
...
@@ -900,12 +916,12 @@ class RedisTrib
...
@@ -900,12 +916,12 @@ class RedisTrib
weights = {}
weights = {}
opt['weight'].each{|w|
opt['weight'].each{|w|
fields = w.split("=")
fields = w.split("=")
node = get_node_by_name(fields[0])
node = get_node_by_
abbreviated_
name(fields[0])
if !node || !node.has_flag?("master")
if !node || !node.has_flag?("master")
puts "*** No such master node #{fields[0]}"
puts "*** No such master node #{fields[0]}"
exit 1
exit 1
end
end
weights[
fields[0
]] = fields[1].to_f
weights[
node.info[:name
]] = fields[1].to_f
} if opt['weight']
} if opt['weight']
useempty = opt['use-empty-masters']
useempty = opt['use-empty-masters']
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment