Top Level Namespace

Defined Under Namespace

Modules: Flare

Constant Summary collapse

ZOK =
Zookeeper::ZOK

Instance Method Summary collapse

Instance Method Details

#clear_nodemap(z, path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 82

def clear_nodemap z, path
  path_nodemap = "#{path}/index/nodemap"
  xml = <<EOS
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!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) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 51

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) ⇒ Object



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
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 180

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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 18

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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 60

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) ⇒ Object



104
105
106
107
108
109
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 104

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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 71

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) ⇒ Object



111
112
113
114
115
116
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 111

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) ⇒ Object



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
170
171
172
173
174
175
176
177
178
# File 'lib/flare/tools/cli/flare_zkadmin.rb', line 118

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