Class: Flare::Tools::Cli::Reconstruct

Inherits:
SubCommand
  • Object
show all
Includes:
IndexServerConfig, Flare::Tools::Common, Util::Constant, Util::Conversion
Defined in:
lib/flare/tools/cli/reconstruct.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

#initializeReconstruct

Returns a new instance of Reconstruct.



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

def initialize
  super
  @force = false
  @unsafe = false
  @retry = 10
  @all = 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
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
164
165
166
167
168
169
# File 'lib/flare/tools/cli/reconstruct.rb', line 50

def execute(config, args)
  parse_index_server(config, args)

  if @all
    unless args.empty?
      puts "don't specify any nodes with --all option."
      return S_NG
    else
      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)
        args = cluster.master_and_slave_nodekeys
      end
    end
  else
    return S_NG if args.size == 0
  end

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

  status = S_OK

  Flare::Tools::IndexServer.open(config[:index_server_hostname], config[:index_server_port], @timeout) do |s|
    puts string_of_nodelist(s.stats_nodes, hosts.map {|x| nodekey_of(x[0], x[1])})

    hosts.each do |hostname,port|
      nodekey = nodekey_of hostname, port
      cluster = Flare::Tools::Cluster.new(s.host, s.port, s.stats_nodes)

      unless node = cluster.node_stat(nodekey)
        puts "#{nodekey} is not found in this cluster."
        return S_NG
      end
      unless cluster.reconstructable? nodekey
        puts "#{nodekey} is not reconstructable."
        status = S_NG
        next
      end
      is_safe = cluster.safely_reconstructable? nodekey
      if !@unsafe && !is_safe
        puts "The partition needs one more slave to reconstruct #{nodekey} safely."
        status = S_NG
        next
      end

      exec = @force
      unless exec
        puts "you are trying to reconstruct #{nodekey} without redanduncy." unless is_safe
        input = nil
        while input.nil?
          STDERR.print "reconstructing node (node=#{nodekey}, role=#{node['role']}) (y/n/a/q/h:help): "
          input = interruptible do
            gets.chomp.upcase
          end
          case input
          when "A"
            @force = true
            exec = true
          when "N"
          when "Q"
            return S_OK
          when "Y"
            exec = true
          else
            puts "y: execute, n: skip, a: execute all the left nodes, q: quit, h: help"
            input = nil
          end
        end
      end
      if exec && !@dry_run
        puts "turning down..."
        s.set_state(hostname, port, 'down')

        puts "waiting for node to be active again..."
        sleep 3

        Flare::Tools::Node.open(hostname, port, @timeout) do |n|
          n.flush_all
        end

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

    puts string_of_nodelist(s.stats_nodes, hosts.map {|x| "#{x[0]}:#{x[1]}"})
  end # open

  status
end

#setupObject



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

def setup
  super
  set_option_index_server
  set_option_dry_run
  set_option_force
  @optp.on('--unsafe',         "reconstruct a node safely"              ) { @unsafe = true }
  @optp.on('--safe',           "[obsolete] now reconstruct a node safely by default") do
    # do nothing
  end
  @optp.on('--retry=COUNT',    "specify retry count (default:#{@retry})") {|v| @retry = v.to_i }
  @optp.on('--all',            "reconstruct all nodes"                  ) { @all = true }
end