1 # -*- encoding: binary -*-
3 # A MogileFS Administration Client
5 class MogileFS::Admin < MogileFS::Client
8 # Enumerates fids using #list_fids.
13 max = get_stats('fids')['fids']['max']
15 0.step max, 100 do |high|
16 fids = list_fids low, high
17 fids.each { |fid| yield fid }
23 # Returns an Array of host status Hashes. If +hostid+ is given only that
30 # [{"status"=>"alive",
31 # "http_get_port"=>"",
35 # "hostname"=>"rur-1",
36 # "remoteroot"=>"/mnt/mogilefs/rur-1",
40 def get_hosts(hostid = nil)
41 clean('hosts', 'host',
42 @backend.get_hosts(hostid ? { :hostid => hostid } : {}))
46 # Returns an Array of device status Hashes. If devid is given only that
53 # [{"status"=>"alive",
61 def get_devices(devid = nil)
62 clean('devices', 'dev',
63 @backend.get_devices(devid ? { :devid => devid } : {}))
67 # Returns an Array of fid Hashes from +from_fid+ to +to_fid+.
69 # admin.list_fids 0, 100
84 # "key"=>"new_new_key"}]
86 def list_fids(from_fid, to_fid)
87 clean('fid_count', 'fid_',
88 @backend.list_fids(:from => from_fid, :to => to_fid))
92 # Returns a statistics structure representing the state of mogilefs.
98 # {"fids"=>{"max"=>"99", "count"=>"2"},
100 # [{"status"=>"alive", "files"=>"2", "id"=>"1", "host"=>"rur-1"},
101 # {"status"=>"alive", "files"=>"2", "id"=>"2", "host"=>"rur-2"}],
103 # [{"files"=>"2", "class"=>"normal", "devcount"=>"2", "domain"=>"test"}],
104 # "file"=>[{"files"=>"2", "class"=>"normal", "domain"=>"test"}]}
106 def get_stats(type = 'all')
107 res = @backend.stats type => 1
110 stats['device'] = clean 'devicescount', 'devices', res, false
111 stats['file'] = clean 'filescount', 'files', res, false
112 stats['replication'] = clean 'replicationcount', 'replication', res, false
114 if res['fidmax'] or res['fidcount'] then
116 'max' => res['fidmax'].to_i,
117 'count' => res['fidcount'].to_i
121 stats.delete 'device' if stats['device'].empty?
122 stats.delete 'file' if stats['file'].empty?
123 stats.delete 'replication' if stats['replication'].empty?
129 # Returns the domains and classes, and their policies present in the mogilefs.
133 # Returns (on newer MogileFS servers):
137 # "mindevcount" => 2,
138 # "replpolicy" => "MultipleHosts()"
143 # Returns (on older MogileFS servers without replication policies):
145 # {"test"=>{"normal"=>3, "default"=>2}}
148 res = @backend.get_domains
149 have_replpolicy = false
152 (1..res['domains'].to_i).each do |i|
153 domain = clean "domain#{i}classes", "domain#{i}class", res, false
155 tmp = domains[res["domain#{i}"]] = {}
157 tmp[d.delete("name")] = d
158 d["mindevcount"] = d["mindevcount"].to_i
159 have_replpolicy ||= d.include?("replpolicy")
163 # only for MogileFS 1.x?, maybe we can drop support for this...
164 unless have_replpolicy
165 domains.each do |namespace, class_data|
166 class_data.each do |class_name, data|
167 class_data[class_name] = data["mindevcount"]
176 # Creates a new domain named +domain+. Returns nil if creation failed.
178 def create_domain(domain)
179 raise MogileFS::ReadOnlyError if readonly?
180 res = @backend.create_domain :domain => domain
181 res ? res['domain'] : nil
185 # Deletes +domain+. Returns true if successful, raises
186 # MogileFS::Backend::DomainNotFoundError if not
188 def delete_domain(domain)
189 raise MogileFS::ReadOnlyError if readonly?
190 ! @backend.delete_domain(:domain => domain).nil?
194 # Creates a new class in +domain+ named +klass+ with +policy+ for
195 # replication. Raises on failure.
197 def create_class(domain, klass, policy)
198 modify_class(domain, klass, policy, :create)
202 # Updates class +klass+ in +domain+ with +policy+ for replication.
205 def update_class(domain, klass, policy)
206 modify_class(domain, klass, policy, :update)
210 # Removes class +klass+ from +domain+. Returns true if successful.
213 def delete_class(domain, klass)
214 ! @backend.delete_class(:domain => domain, :class => klass).nil?
218 # Creates a new host named +host+. +args+ must contain :ip and :port.
219 # Returns true if successful, false if not.
221 def create_host(host, args = {})
222 raise ArgumentError, "Must specify ip and port" unless \
223 args.include? :ip and args.include? :port
225 modify_host(host, args, 'create')
229 # Updates +host+ with +args+. Returns true if successful, false if not.
231 def update_host(host, args = {})
232 modify_host(host, args, 'update')
236 # Deletes host +host+. Returns nil on failure.
238 def delete_host(host)
239 raise MogileFS::ReadOnlyError if readonly?
240 ! @backend.delete_host(:host => host).nil?
244 # Changes the device status of +device+ on +host+ to +state+ which can be
245 # 'alive', 'down', or 'dead'.
247 def change_device_state(host, device, state)
248 raise MogileFS::ReadOnlyError if readonly?
249 ! @backend.set_state(:host => host, :device => device, :state => state).nil?
252 protected unless defined? $TESTING
255 # Modifies +klass+ on +domain+ to store files on +mindevcount+ devices via
256 # +action+. Returns the class name if successful, raises if not
258 def modify_class(domain, klass, policy, action)
259 raise MogileFS::ReadOnlyError if readonly?
260 args = { :domain => domain, :class => klass }
263 args[:mindevcount] = policy
265 args[:replpolicy] = policy
270 "policy=#{policy.inspect} not understood for #{action}_class"
272 @backend.__send__("#{action}_class", args)["class"]
276 # Modifies +host+ using +args+ via +action+. Returns true if successful,
279 def modify_host(host, args = {}, action = 'create')
281 ! @backend.__send__("#{action}_host", args).nil?
285 # Turns the response +res+ from the backend into an Array of Hashes from 1
286 # to res[+count+]. If +underscore+ is true then a '_' character is assumed
287 # between the prefix and the hash key value.
289 # res = {"host1_remoteroot"=>"/mnt/mogilefs/rur-1",
290 # "host1_hostname"=>"rur-1",
291 # "host1_hostid"=>"1",
292 # "host1_http_get_port"=>"",
295 # "host1_hostip"=>"",
296 # "host1_http_port"=>"",
297 # "host1_status"=>"alive",
298 # "host1_altmask"=>""}
299 # admin.clean 'hosts', 'host', res
303 # [{"status"=>"alive",
304 # "http_get_port"=>"",
308 # "hostname"=>"rur-1",
309 # "remoteroot"=>"/mnt/mogilefs/rur-1",
313 def clean(count, prefix, res, underscore = true)
314 underscore = underscore ? '_' : ''
315 (1..res[count].to_i).map do |i|
316 dev = res.select { |k,_| k =~ /^#{prefix}#{i}#{underscore}/ }.map do |k,v|
317 [k.sub(/^#{prefix}#{i}#{underscore}/, ''), v]