class Flare::Tools::Cli::Balance

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/flare/tools/cli/balance.rb, line 33
def initialize
  super
  @force = false
end

Public Instance Methods

execute(config, args) click to toggle source
# File lib/flare/tools/cli/balance.rb, line 38
def execute(config, args)
  parse_index_server(config, args)
  return S_NG if args.empty?

  hosts = args.map {|x| x.to_s.split(':')}
  hosts.each do |x|
    if x.size != 3
      puts "invalid argument '#{x.join(':')}'."
      return S_NG
    end
  end

  Flare::Tools::IndexServer.open(config[:index_server_hostname], config[:index_server_port], @timeout) do |s|
    cluster = Flare::Tools::Cluster.new(s.host, s.port, s.stats_nodes)

    hosts.each do |hostname,port,balance|
      balance = balance.to_i
      nodekey = nodekey_of hostname, port
      ipaddr = address_of_hostname(hostname)

      unless cluster.has_nodekey? nodekey
        error "unknown host: #{nodekey}"
        return S_NG
      end

      node = cluster.node_stat(nodekey)

      exec = false
      if @force
        exec = true
      elsif node['balance'].to_i == balance
        STDERR.puts "no need to change the balance of #{ipaddr}:#{port}."
      else
        interruptible do
          STDERR.print "updating node balance (node=#{ipaddr}:#{port}, balance=#{node['balance']} -> #{balance}) (y/n): "
          exec = true if gets.chomp.upcase == "Y"
        end
      end
      if exec
        s.set_role(hostname, port.to_i, node['role'], balance, node['partition']) unless @dry_run
      end
    end
    STDOUT.puts string_of_nodelist(s.stats_nodes, hosts.map {|x| "#{x[0]}:#{x[1]}"})
  end

  return S_OK
end
setup() click to toggle source
Calls superclass method
# File lib/flare/tools/cli/balance.rb, line 26
def setup
  super
  set_option_index_server
  set_option_dry_run
  set_option_force
end