Class: Flare::Tools::Cli::Master

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

Constant Summary

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

SubCommand::S_NG, 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

#initializeMaster

Returns a new instance of Master.



38
39
40
41
42
43
44
# File 'lib/flare/tools/cli/master.rb', line 38

def initialize
  super
  @force = false
  @retry = 10
  @activate = false
  @without_clean = false
end

Instance Method Details

#execute(config, args) ⇒ Object



46
47
48
49
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/flare/tools/cli/master.rb', line 46

def execute(config, args)
  parse_index_server(config, args)
  status = S_OK

  return S_NG if args.empty?
  hosts = args.map {|x| x.to_s.split(':')}
  hosts.each do |x|
    if x.size != 4
      error "invalid argument '#{x.join(':')}'."
      return S_NG
    end
    if x[2].to_i <= 0
      error "invalid balance '#{x.join(':')}'."
      return S_NG
    end
    if nodekey_of(x[0..1]).nil?
      error "invalid nodekey '#{x.join(':')}'."
      return S_NG
    end
  end
  hosts = hosts.sort_by{|hostname,port,balance,partition| [partition]}

  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,partition|
      role = 'master'
      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)

      partition = if partition == '' then node['partition'].to_i else partition.to_i end
      balance = if balance == '' then node['balance'] else balance.to_i end
      existing_master = cluster.master_in_partition(partition)

      exec = false
      if @force
        exec = true
      elsif node['role'] == role
        info "no need to change the role of #{ipaddr}:#{port}."
      elsif existing_master
        info "the partiton already has a master #{existing_master}."
      elsif node['role'] != 'proxy'
        puts "#{nodekey} is not a proxy."
      else
        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 the node master (node=#{ipaddr}:#{port}, role=#{node['role']} -> #{role})#{clean_notice} (y/n): "
        exec = interruptible {(gets.chomp.upcase == "Y")}
      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 master node."
        end

        nretry = 0
        resp = false
        while resp == false && nretry < @retry
          resp = s.set_role(hostname, port, role, balance, partition)
          if resp
            info "started constructing the master node..."
          else
            nretry += 1
            info "waiting #{nretry} sec..."
            sleep nretry
            info "retrying..."
          end
        end
        if resp
          state = wait_for_master_construction(s, nodekey, @timeout)
          if state == 'ready' && @activate
            unless @force
              node = s.stats_nodes[nodekey]
              STDERR.print "changing node's state (node=#{ipaddr}:#{port}, state=#{node['state']} -> active) (y/n): "
              exec = interruptible {
                (gets.chomp.upcase == "Y")
              }
            end
            if exec
              begin
                resp = s.set_state(hostname, port, 'active')
                unless resp
                  error "failed to activate #{nodekey}"
                  status = S_NG
                end
              rescue Timeout::Error
                error "failed to activate #{nodekey} (timeout)"
                status = S_NG
              end
            end
          end
        else
          error "failed to change the state."
          status = S_NG
        end
      end
    end

    break if status == S_NG
    STDOUT.puts string_of_nodelist(s.stats_nodes, hosts.map {|x| "#{x[0]}:#{x[1]}"})
  end

  status
end

#setupObject



28
29
30
31
32
33
34
35
36
# File 'lib/flare/tools/cli/master.rb', line 28

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('--activate', "change node's state from ready to active") { @activate = true }
  @optp.on('--without-clean', "don't clear datastore before construction") { @without_clean = true }
end