Class: Flare::Tools::Cli::Dump

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

Defined Under Namespace

Classes: CsvDumper, DefaultDumper, Dumper, TchDumper

Constant Summary collapse

Iterators =
[DefaultDumper, CsvDumper]
Formats =
Iterators.map {|n| n.myname}
SizeOfByte =
8

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

#initializeDump

Returns a new instance of Dump.



127
128
129
130
131
132
133
134
135
# File 'lib/flare/tools/cli/dump.rb', line 127

def initialize
  super
  @output = nil
  @format = nil
  @bwlimit = 0
  @all = false
  @raw = false
  @partition_size = 1
end

Instance Method Details

#execute(config, args) ⇒ Object



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/flare/tools/cli/dump.rb', line 137

def execute(config, args)
  parse_index_server(config, args)
  STDERR.puts "please install tokyocabinet via gem command." unless defined? TokyoCabinet

  cluster = nil
  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)
  end
  return S_NG if cluster.nil?

  partition_size = cluster.partition_size

  if @all
    if args.size > 0
      STDERR.puts "don't specify any nodes with --all option."
      return S_NG
    else
      args = cluster.master_nodekeys
    end
  else
    if args.size == 0
      STDERR.puts "please specify --all option to get complete dump."
      return S_NG
    end
  end

  unless Formats.include?(@format)
    STDERR.puts "unknown format: #{@format}"
    return S_NG
  end

  hosts = args.map {|x| x.split(':')}
  hosts.each do |x|
    if x.size == 2
      x << cluster.partition_of_nodename("#{x[0]}:#{x[1]}")
    elsif x.size == 4
      if x[3] =~ /^\d+$/
        STDERR.puts "invalid partition number '#{x.join(':')}'."
        x[3] = x[3].to_i
      else
        STDERR.puts "invalid partition number '#{x.join(':')}'."
        return S_NG
      end
    else
      STDERR.puts "invalid argument '#{x.join(':')}'."
      return S_NG
    end
  end

  dumper = case @format
           when CsvDumper.myname
             CsvDumper.new(@output || STDOUT)
           when TchDumper.myname
             TchDumper.new @output
           else
             DefaultDumper.new(@output || STDOUT)
           end

  hosts.each do |hostname,port,partition|
    Flare::Tools::Node.open(hostname, port.to_i, @timeout, 0, @bwlimit) do |n|
      interval = 0
      part, partsize = if @raw
                         [0, 1]
                       else
                         [partition, partition_size]
                       end
      bwlimit = @bwlimit/1024/SizeOfByte
      count = 0
      STDERR.print "dumping from #{hostname}:#{port}::#{part} of #{partsize} partitions ... "
      n.dump(interval, part, partsize, bwlimit) do |data, key, flag, len, version, expire|
        dumper.write data, key, flag, len, version, expire
        count += 1
        false
      end
      STDERR.puts "#{count}"
    end
  end

  dumper.close

  S_OK
end

#setupObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flare/tools/cli/dump.rb', line 115

def setup
  super
  set_option_index_server
  @optp.on('-o', '--output=FILE',            "output to file") {|v| @output = v}
  @optp.on('-f', '--format=FORMAT',          "specify output format [#{Formats.join(',')}]") {|v| @format = v}
  @optp.on(      '--bwlimit=BANDWIDTH',      "specify bandwidth limit (bps)") {|v|
    @bwlimit = Flare::Util::Bwlimit.bps(v)
  }
  @optp.on('--all',                            "dump from all master nodes") {|v| @all = true}
  @optp.on('--raw',                            "raw dump mode (for debugging)") {|v| @raw = true}
end