1 # -*- encoding: binary -*-
3 # \MogileFS administration client, this has little real-world usage
4 # and is considered a work-in-progress
5 class MogileFS::Admin < MogileFS::Client
8 # Enumerates fids using #list_fids. Returns the number of valid fids
16 fids.each { |fid| yield fid }
18 end while last = fids[-1] and low = last["fid"]
23 # Returns an Array of host status Hashes. If +hostid+ is given only that
30 # [{"status"=>"alive",
31 # "http_get_port"=>nil,
35 # "hostname"=>"rur-1",
39 def get_hosts(hostid = nil)
40 to_i = %w(hostid http_port http_get_port)
41 want = %w(status hostip hostname altip altmask).concat(to_i)
42 rv = @backend.get_hosts(hostid ? { :hostid => hostid } : {})
43 clean('hosts', 'host', rv, true, to_i, want)
47 # Returns an Array of device status Hashes. If devid is given only that
54 # [{"status"=>"alive",
60 # "mb_total"=>666666}]
62 def get_devices(devid = nil)
63 to_i = %w(mb_asof mb_free mb_used mb_total devid weight hostid)
64 want = %w(status).concat(to_i)
65 rv = @backend.get_devices(devid ? { :devid => devid } : {})
66 rv = clean('devices', 'dev', rv, true, to_i, want)
68 u = row["utilization"] and
69 row["utilization"] = nil == u ? nil : u.to_f
70 case row["reject_bad_md5"]
72 row["reject_bad_md5"] = true
74 row["reject_bad_md5"] = false
80 # Returns an Array of fid Hashes from +from_fid+, limited to +count+
82 # admin.list_fids 0, 100
97 # "key"=>"new_new_key"}]
99 def list_fids(from_fid, count = 100)
100 to_i = %w(fid devcount length)
101 want = %w(domain class key).concat(to_i)
102 rv = @backend.list_fids(:from => from_fid, :to => count)
103 # :to is now :count internally in mogilefsd
104 clean('fid_count', 'fid_', rv, true, to_i, want)
108 # Returns a statistics structure representing the state of mogilefs.
110 # *** This command no longer works with recent versions of MogileFS ***
111 # *** Use mogstats(1) from the MogileFS::Utils package on CPAN ***
117 # {"fids"=>{"max"=>"99", "count"=>"2"},
119 # [{"status"=>"alive", "files"=>"2", "id"=>"1", "host"=>"rur-1"},
120 # {"status"=>"alive", "files"=>"2", "id"=>"2", "host"=>"rur-2"}],
122 # [{"files"=>"2", "class"=>"normal", "devcount"=>"2", "domain"=>"test"}],
123 # "file"=>[{"files"=>"2", "class"=>"normal", "domain"=>"test"}]}
125 def get_stats(type = 'all')
126 res = @backend.stats type => 1
129 stats['device'] = clean 'devicescount', 'devices', res, false
130 stats['file'] = clean 'filescount', 'files', res, false
131 stats['replication'] = clean 'replicationcount', 'replication', res, false
133 if res['fidmax'] or res['fidcount'] then
135 'max' => res['fidmax'].to_i,
136 'count' => res['fidcount'].to_i
140 stats.delete 'device' if stats['device'].empty?
141 stats.delete 'file' if stats['file'].empty?
142 stats.delete 'replication' if stats['replication'].empty?
148 # Returns the domains and classes, and their policies present in the mogilefs.
152 # Returns (on newer MogileFS servers):
156 # "mindevcount" => 2,
157 # "replpolicy" => "MultipleHosts()",
163 # Returns (on older MogileFS servers without replication policies):
165 # {"test"=>{"normal"=>3, "default"=>2}}
168 res = @backend.get_domains
169 have_replpolicy = false
172 to_i = %w(mindevcount)
173 want = %w(name replpolicy hashtype mindevcount)
174 (1..res['domains'].to_i).each do |i|
175 domain = clean("domain#{i}classes", "domain#{i}class", res, false, to_i,
177 tmp = domains[res["domain#{i}"]] = {}
179 tmp[d.delete("name")] = d
180 have_replpolicy ||= d.include?("replpolicy")
184 # only for MogileFS 1.x?, maybe we can drop support for this...
185 unless have_replpolicy
186 domains.each do |namespace, class_data|
187 class_data.each do |class_name, data|
188 class_data[class_name] = data["mindevcount"]
197 # Creates a new domain named +domain+. Returns nil if creation failed.
199 def create_domain(domain)
200 raise MogileFS::ReadOnlyError if readonly?
201 res = @backend.create_domain :domain => domain
202 res ? res['domain'] : nil
206 # Deletes +domain+. Returns true if successful, raises
207 # MogileFS::Backend::DomainNotFoundError if not
209 def delete_domain(domain)
210 raise MogileFS::ReadOnlyError if readonly?
211 ! @backend.delete_domain(:domain => domain).nil?
215 # Creates a new class in +domain+ named +klass+ with +policy+ for
216 # replication. Raises on failure.
218 def create_class(domain, klass, policy)
219 modify_class(domain, klass, policy, :create)
223 # Updates class +klass+ in +domain+ with +policy+ for replication.
226 def update_class(domain, klass, policy)
227 modify_class(domain, klass, policy, :update)
231 # Removes class +klass+ from +domain+. Returns true if successful.
234 def delete_class(domain, klass)
235 ! @backend.delete_class(:domain => domain, :class => klass).nil?
239 # Creates a new host named +host+. +args+ must contain :ip and :port.
240 # Returns true if successful, false if not.
242 def create_host(host, args = {})
243 raise ArgumentError, "Must specify ip and port" unless \
244 args.include? :ip and args.include? :port
246 modify_host(host, args, 'create')
250 # Updates +host+ with +args+. Returns true if successful, false if not.
252 def update_host(host, args = {})
253 modify_host(host, args, 'update')
257 # Deletes host +host+. Returns nil on failure.
259 def delete_host(host)
260 raise MogileFS::ReadOnlyError if readonly?
261 ! @backend.delete_host(:host => host).nil?
265 # Creates device with Integer +devid+ on +host+
266 # +host+ may be an integer for hostid or String for hostname
267 def create_device(host, devid, opts = {})
268 raise MogileFS::ReadOnlyError if readonly?
275 opts[:hostname] = host
277 raise ArgumentError, "host=#{host.inspect} is not a String or Integer"
281 ! @backend.create_device(opts).nil?
285 # Changes the device status of +device+ on +host+ to +state+ which can be
286 # 'alive', 'down', or 'dead'.
288 def change_device_state(host, device, state)
289 raise MogileFS::ReadOnlyError if readonly?
290 ! @backend.set_state(:host => host, :device => device, :state => state).nil?
294 # Changes the device weight of +device+ on +host+ to +weight+.
295 # +weight+ should be a non-negative Integer. Devices with higher
296 # +weight+ values are more likely to be chosen for reads and writes.
297 def change_device_weight(host, device, weight)
298 raise MogileFS::ReadOnlyError if readonly?
299 opts = { :host => host, :device => device, :weight => weight }
300 ! @backend.set_weight(opts).nil?
303 # reschedules all deferred replication, returns a hash with the number
304 # of files rescheduled:
306 # admin.replicate_now => { "count" => 5 }
308 rv = @backend.replicate_now
309 rv["count"] = rv["count"].to_i
313 # Clears the tracker caches. Not implemented in all versions of MogileFS
318 protected unless defined? $TESTING
321 # Modifies +klass+ on +domain+ to store files on +mindevcount+ devices via
322 # +action+. Returns the class name if successful, raises if not
324 def modify_class(domain, klass, policy, action)
325 raise MogileFS::ReadOnlyError if readonly?
326 args = { :domain => domain, :class => klass }
329 args[:mindevcount] = policy
331 args[:replpolicy] = policy
336 "policy=#{policy.inspect} not understood for #{action}_class"
338 @backend.__send__("#{action}_class", args)["class"]
342 # Modifies +host+ using +args+ via +action+. Returns true if successful,
345 def modify_host(host, args = {}, action = 'create')
347 ! @backend.__send__("#{action}_host", args).nil?
351 # Turns the response +res+ from the backend into an Array of Hashes from 1
352 # to res[+count+]. If +underscore+ is true then a '_' character is assumed
353 # between the prefix and the hash key value.
355 # res = {"host1_remoteroot"=>"/mnt/mogilefs/rur-1",
356 # "host1_hostname"=>"rur-1",
357 # "host1_hostid"=>"1",
358 # "host1_http_get_port"=>"",
361 # "host1_hostip"=>"",
362 # "host1_http_port"=>"",
363 # "host1_status"=>"alive",
364 # "host1_altmask"=>""}
365 # admin.clean 'hosts', 'host', res
369 # [{"status"=>"alive",
370 # "http_get_port"=>nil,
373 # "hostip"=>"192.168.1.3",
374 # "hostname"=>"rur-1",
378 def clean(count, prefix, res, underscore = true, to_i = [], want = nil)
380 underscore = underscore ? '_' : empty
382 # convert array to hash for O(1) lookups
383 to_i = to_i.inject({}) { |m,k| m[k] = m }
385 (1..res[count].to_i).map do |i|
388 v = res["#{prefix}#{i}#{underscore}#{k}"] or next
389 row[k] = to_i.include?(k) ? (empty == v ? nil : v.to_i) : v
395 (1..res[count].to_i).map do |i|
396 re = /^#{prefix}#{i}#{underscore}/
398 keys.grep(re).each do |k|
401 row[k] = to_i.include?(k) ? (empty == v ? nil : v.to_i) : v