class Object

Constants

ZOK

Public Instance Methods

clear_nodemap(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 81
def clear_nodemap z, path
  path_nodemap = "#{path}/index/nodemap"
  xml = <<EOS

<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="4">
<node_map class_id="0" tracking_level="0" version="0">
        <count>0</count>
        <item_version>0</item_version>
</node_map>
<thread_type>16</thread_type>
</boost_serialization>
EOS
  STDOUT.print "do you really want to clear nodemap? (y/n):"
  STDOUT.flush
  if gets.chomp.upcase == 'Y'
    result = z.set(:path => path_nodemap, :data => xml)
    rc = result[:rc]
    raise "failed to clear nodemap (#{rc})" if rc != ZOK
  end
end
destroy(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 50
def destroy z, path
  result = z.get_children(:path => path)
  raise "failed to fetch child nodes." if result[:rc] != ZOK
  result[:children].each do |entry|
    destroy z, "#{path}/#{entry}"
  end
  z.delete(:path => path)
end
execute(subc, args, options) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 179
def execute(subc, args, options)
  scheme, userinfo, host, port, registry, path, opaque, query, flagment = URI.split(options[:indexdb])
  # p scheme, userinfo, host, port, registry, path, opaque, query, flagment

  z = case scheme
      when "zookeeper"
        Zookeeper.new("#{host}:#{port}")
      else
        raise "invalid scheme: #{scheme}"
      end
  
  case subc
  when "set-servers"
    set_servers z, path, *args
  when "servers"
    servers z, path
  when "set-nodemap"
    set_nodemap z, path
  when "clear-nodemap"
    clear_nodemap z, path
  when "nodemap"
    nodemap z, path
  when "init"
    init z, path
  when "destroy"
    destroy z, path
  when "show"
    show z, path
  end
  z.close
end
init(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 17
def init z, path
  puts "initializing #{path}"
  path_cluster = ""
  path.split('/').each do |e|
    unless e.empty?
      path_cluster += "/#{e}"
      z.create(:path => path_cluster)
    end
  end
  
  path_index = "#{path_cluster}/index"
  r = z.create(:path => "#{path_index}")
  raise "already initialized." unless r[:rc] == ZOK

  z.create(:path => "#{path_index}/lock")
  z.create(:path => "#{path_index}/primary")
  z.create(:path => "#{path_index}/servers")
  z.create(:path => "#{path_index}/nodemap")

  path_meta = "#{path_index}/meta"
  z.create(:path => path_meta)
  entries = [['partition-size', '1024'],
             ['key-hash-algorithm', 'crc32'],
             ['partition-type', 'modular'],
             ['partition-modular-hint', '1'],
             ['partition-modular-virtual', '65536']]
  entries.each do |k,v|
    r = z.create(:path => "#{path_meta}/#{k}", :data => v)
  end

  clear_nodemap z, path
end
nodemap(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 59
def nodemap z, path
  result = z.get(:path => "#{path}/index/nodemap")
  if result[:rc] == ZOK
    xml = result[:data]
    unless xml.nil?
      cluster = Flare::Tools::Cluster.build xml
      print cluster.serialize
    end
  end
end
servers(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 103
def servers z, path
  result = z.get(:path => "#{path}/index/servers")
  if result[:rc] == ZOK
    puts result[:data].split(',').join(' ')
  end
end
set_nodemap(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 70
def set_nodemap z, path
  path_nodemap = "#{path}/index/nodemap"
  xml = ""
  while line = STDIN.gets
    xml += line
  end
  result = z.set(:path => path_nodemap, :data => xml)
  rc = result[:rc]
  raise "failed to set nodemap (#{rc})" if rc != ZOK
end
set_servers(z, path, *args) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 110
def set_servers z, path, *args
  path_servers = "#{path}/index/servers"
  result = z.set(:path => path_servers, :data => args.join(','))
  rc = result[:rc]
  raise "failed to set nodemap (#{rc})" if rc != ZOK
end
show(z, path) click to toggle source
# File lib/flare/tools/cli/flare_zkadmin.rb, line 117
def show z, path
  path_index = "#{path}/index"
  path_lock = "#{path_index}/lock"
  path_servers = "#{path_index}/servers"
  path_nodemap = "#{path_index}/nodemap"
  path_primary = "#{path_index}/primary"
  path_meta = "#{path_index}/meta"
  result = z.get_children(:path => path_index)
  raise "failed to fetch child nodes." if result[:rc] != ZOK
  result[:children].each do |entry|
    puts "#{entry}:"
    case entry
    when "lock"
      r = z.get_children(:path => path_lock)
      if r[:rc] == ZOK
        r[:children].sort_by {|n| n.split('-').last}.each do |m|
          r2 = z.get(:path => "#{path_lock}/#{m}")
          if r2[:rc] == ZOK
            puts "\t#{m} #{r2[:data]}"
          else
            puts "\t#{m}"
          end
        end
      end
    when "primary"
      r = z.get(:path => path_primary)
      if r[:rc] == ZOK && !r[:data].nil?
        puts "\t#{r[:data]}"
      end
    when "servers"
      r = z.get(:path => path_servers)
      puts "\t#{r[:data]}" if r[:rc] == ZOK && !r[:data].nil?
    when "nodemap"
      role_to_s = ["master", "slave", "proxy"]
      state_to_s = ["active", "prepare", "down", "ready"]
      r = z.get(:path => path_nodemap)
      if r[:rc] == ZOK
        xml = r[:data]
        unless xml.nil?
          cluster = Flare::Tools::Cluster.build xml
          cluster.nodekeys.each do |nodekey|
            n = cluster.node_stat(nodekey)
            p = if n.partition == -1 then "-" else n.partition end
            m = "#{n.server_name}:#{n.server_port}:#{n.balance}:#{p}"
            puts "\t#{m} #{role_to_s[n.role.to_i]} #{state_to_s[n.state.to_i]} #{n.thread_type}"
          end
        end
      end
    when "meta"
      r = z.get_children(:path => path_meta)
      if r[:rc] == ZOK
        r[:children].each do |m|
          r2 = z.get(:path => "#{path_meta}/#{m}")
          puts "\t#{m} #{r2[:data]}" if r2[:rc] == ZOK
        end
      end
    else
      puts "\tunknown entry"
    end
  end
end