Class: Flare::Tools::Cli::Slave

Inherits:
SubCommand show all
Includes:
IndexServerConfig, Flare::Tools::Common, Util::Constant, Util::Conversion
Defined in:
lib/flare/tools/cli/slave.rb

Constant Summary collapse

DefaultRetry =
10

Constants included from IndexServerConfig

IndexServerConfig::Entity, IndexServerConfig::FLARE_INDEX_SERVER, IndexServerConfig::FLARE_INDEX_SERVERS

Constants included from Util::Constant

Util::Constant::DefalutBwlimit, Util::Constant::DefaultIndexServerName, Util::Constant::DefaultIndexServerPort, Util::Constant::DefaultNodePort, Util::Constant::DefaultTimeout, Util::Constant::STATUS_NG, Util::Constant::STATUS_OK

Constants included from Flare::Tools::Common

Flare::Tools::Common::NodeListFormat, Flare::Tools::Common::NodeListHeader

Constants inherited from SubCommand

Flare::Tools::Cli::SubCommand::S_NG, Flare::Tools::Cli::SubCommand::S_OK

Constants included from Util::Interruption

Util::Interruption::InterruptionTargets

Instance Attribute Summary

Attributes included from Option

#optp

Instance Method Summary collapse

Methods included from Util::Logging

#debug, #error, #fatal, #info, logger, #puts, set_logger, #trace, #warn

Methods included from Flare::Tools::Common

#address_of_hostname, #fetch_cluster, #hostname_of_address, #nodekey_of, #string_of_nodelist, #user_confirmed, #wait_for_master_construction, #wait_for_servers, #wait_for_slave_construction

Methods included from Util::Conversion

#short_desc_of_second

Methods inherited from SubCommand

desc, #execute_subcommand, myname, #myname, to_s, to_sym, usage

Methods included from Util::Interruption

included, #initialize_interruption, #interrupt, #interrupt_, interrupt_all, #interrupted?, #interruptible, #interruptible?

Methods included from Option

#option_init, #parse_options, #set_option_dry_run, #set_option_force, #set_option_global, #set_option_index_server

Constructor Details

#initializeSlave

Returns a new instance of Slave.



43
44
45
46
47
48
# File 'lib/flare/tools/cli/slave.rb', line 43

def initialize
  super
  @force = false
  @retry = DefaultRetry
  @without_clean = false
end

Instance Method Details

#execute(config, args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/flare/tools/cli/slave.rb', line 50

def execute(config, args)
  parse_index_server(config, args)
  return S_NG if args.empty?

  hosts = args.map do |arg|
    hostname, port, balance, partition, rest = arg.split(':', 5)
    if rest != nil || balance.nil?
      error "invalid argument '#{arg}'. it must be hostname:port:balance:partition."
      return S_NG
    end
    [hostname, port, balance.to_i, partition.to_i]
  end

  Flare::Tools::IndexServer.open(config[:index_server_hostname], config[:index_server_port], @timeout) do |s|
    cluster = fetch_cluster(s)

    hosts.each do |hostname,port,balance,partition|
      nodekey = nodekey_of hostname, port

      unless cluster.has_nodekey? nodekey
        error "invalid 'hostname:port' pair: #{nodekey}"
        return S_NG
      end

      node = cluster.node_stat(nodekey)

      if node['role'] != 'proxy'
        puts "#{nodekey} is not a proxy."
        next
      end

      exec = @force
      unless exec
        clean_notice_base = "\nitems stored in the node will be cleaned up (exec flush_all) before constructing it"
        clean_notice = @without_clean ? clean_notice_base : ''
        STDERR.print "making node slave (node=#{nodekey}, role=#{node['role']} -> slave)#{clean_notice} (y/n): "
        interruptible do
          exec = true if gets.chomp.upcase == "Y"
        end
      end
      if exec && !@dry_run
        unless @without_clean
          resp = false
          Flare::Tools::Node.open(hostname, port, @timeout) do |n|
            resp = n.flush_all
          end
          unless resp
            STDERR.print "executing flush_all failed."
            return S_NG
          end
          puts "executed flush_all command before constructing the slave node."
        end

        nretry = 0
        resp = false
        while resp == false && nretry < @retry
          resp = s.set_role(hostname, port, 'slave', 0, partition)
          if resp
            puts "started constructing slave node..."
          else
            nretry += 1
            puts "waiting #{nretry} sec..."
            sleep nretry
            puts "retrying..."
          end
        end
        if resp
          wait_for_slave_construction(s, nodekey, @timeout)
          if balance > 0
            unless @force
              STDERR.print "changing node's balance (node=#{nodekey}, balance=0 -> #{balance}) (y/n): "
              exec = interruptible {(gets.chomp.upcase == "Y")}
            end
            if exec
              s.set_role(hostname, port, 'slave', balance, partition)
            end
          end
        else
          error "failed to change the state."
          return S_NG
        end
      end
    end
    STDOUT.puts string_of_nodelist(s.stats_nodes, hosts.map {|x| x[0..1].join(':')})
  end

  return S_OK
end

#setupObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flare/tools/cli/slave.rb', line 31

def setup
  super
  set_option_index_server
  set_option_dry_run
  set_option_force
  @optp.on('--retry=COUNT', "specify retry count(default:#{@retry})") {|v| @retry = v.to_i}
  @optp.on('--without-clean', "don't clear datastore before construction") { @without_clean = true }
  @optp.on('--clean', '[obsolete] now slave command clear datastore before construction by default.') do
    # do nothing
  end
end