Skip to content

Instantly share code, notes, and snippets.

@alk
Last active February 22, 2016 11:40
Show Gist options
  • Select an option

  • Save alk/93f6ce34f0707dcf601a to your computer and use it in GitHub Desktop.

Select an option

Save alk/93f6ce34f0707dcf601a to your computer and use it in GitHub Desktop.
maps-balancedness.rb
#!/usr/bin/ruby
# coding: utf-8
require 'json'
require 'pp'
require 'optparse'
require 'rubygems'
require 'restclient'
$opts = {:username => "Administrator",
:password => "asdasd",
:url => "http://127.0.0.1:9000/"}
OptionParser.new do |opts|
$opts.each_key do |optname|
opts.on("--#{optname}=#{optname.to_s.upcase}", "#{optname} (defaults to #{$opts[optname]})") {|value| $opts[optname] = value}
end
end.parse!
$username = $opts[:username]
$password = $opts[:password]
def req!(method, path, payload = nil, headers = nil)
opts = {
:method => method, :url => $opts[:url].chomp("/") + path,
:payload => payload, :headers => headers || {},
:user => $opts[:username],
:password => $opts[:password]
}
RestClient::Request.execute(opts)
end
def get!(path, headers = nil)
req!(:get, path, nil, headers)
end
def vb_counts_mismatch(vbmap)
balances = (0..3).map do |turn|
histo = vbmap.group_by {|chain| chain[turn]}.tap {|h| h.delete nil}.map {|(n, g)| [n, g.size]}.sort_by(&:last)
# puts "for turn #{turn} max group is #{histo.last.inspect} and min group is #{histo.first.inspect}"
(histo.last || [0]).last - (histo.first || [0]).last
end
total_histo = vbmap.flatten.compact.group_by(&:dup).map(&:last).map(&:size)
total_balance = (total_histo.max || 0) - (total_histo.min || 0)
rep_histo = vbmap.collect_concat {|chain| chain[1..-1].compact}.group_by(&:dup).map(&:last).map(&:size)
rep_balance = (rep_histo.max || 0) - (rep_histo.min || 0)
[total_balance, rep_balance, *balances]
end
JSON.parse(get!("/pools/default/buckets")).each do |details|
m = details["vBucketServerMap"]
servers = m["serverList"]
vbm = m["vBucketMap"].map {|chain| chain.map {|idx| idx >= 0 ? servers[idx] : nil}}
mismatch = vb_counts_mismatch(vbm)
m = mismatch.zip(-2..3).map do |mis, idx|
case idx
when -2
"total vbuckets count mismatch #{mis}"
when -1
"total replica vbuckets count mismatch #{mis}"
when 0
"master vbuckets count mismatch #{mis}"
else
"replica №#{idx} mismatch #{mis} (only valid for chain topology)"
end
end
m = m[0,details["replicaNumber"]+3]
puts "for bucket `#{details["name"]}':\n #{m.join("\n ")}\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment