Class: MogileFS::Admin
Overview
A MogileFS Administration Client
Instance Attribute Summary
Attributes inherited from Client
Instance Method Summary collapse
-
#change_device_state(host, device, state) ⇒ Object
Changes the device status of
device
onhost
tostate
which can be ‘alive’, ‘down’, or ‘dead’. -
#clean(count, prefix, res, underscore = true) ⇒ Object
Turns the response
res
from the backend into an Array of Hashes from 1 to res[count
]. -
#create_class(domain, klass, mindevcount) ⇒ Object
Creates a new class in
domain
namedklass
with files replicated tomindevcount
devices. -
#create_domain(domain) ⇒ Object
Creates a new domain named
domain
. -
#create_host(host, args = {}) ⇒ Object
Creates a new host named
host
. -
#delete_class(domain, klass) ⇒ Object
Removes class
klass
fromdomain
. -
#delete_domain(domain) ⇒ Object
Deletes
domain
. -
#delete_host(host) ⇒ Object
Deletes host
host
. -
#each_fid ⇒ Object
Enumerates fids using #list_fids.
-
#get_devices(devid = nil) ⇒ Object
Returns an Array of device status Hashes.
-
#get_domains ⇒ Object
Returns the domains present in the mogilefs.
-
#get_hosts(hostid = nil) ⇒ Object
Returns an Array of host status Hashes.
-
#get_stats(type = 'all') ⇒ Object
Returns a statistics structure representing the state of mogilefs.
-
#list_fids(from_fid, to_fid) ⇒ Object
Returns an Array of fid Hashes from
from_fid
toto_fid
. -
#modify_class(domain, klass, mindevcount, action) ⇒ Object
Modifies
klass
ondomain
to store files onmindevcount
devices viaaction
. -
#modify_host(host, args = {}, action = 'create') ⇒ Object
Modifies
host
usingargs
viaaction
. -
#update_class(domain, klass, mindevcount) ⇒ Object
Updates class
klass
indomain
to be replicated tomindevcount
devices. -
#update_host(host, args = {}) ⇒ Object
Updates
host
withargs
.
Methods inherited from Client
#err, #errstr, #initialize, #readonly?, #reload
Constructor Details
This class inherits a constructor from MogileFS::Client
Instance Method Details
#change_device_state(host, device, state) ⇒ Object
Changes the device status of device
on host
to state
which can be ‘alive’, ‘down’, or ‘dead’.
223 224 225 226 |
# File 'lib/mogilefs/admin.rb', line 223 def change_device_state(host, device, state) raise MogileFS::ReadOnlyError if readonly? ! @backend.set_state(:host => host, :device => device, :state => state).nil? end |
#clean(count, prefix, res, underscore = true) ⇒ Object
Turns the response res
from the backend into an Array of Hashes from 1 to res[count
]. If underscore
is true then a ‘_’ character is assumed between the prefix and the hash key value.
res = {"host1_remoteroot"=>"/mnt/mogilefs/rur-1",
"host1_hostname"=>"rur-1",
"host1_hostid"=>"1",
"host1_http_get_port"=>"",
"host1_altip"=>"",
"hosts"=>"1",
"host1_hostip"=>"",
"host1_http_port"=>"",
"host1_status"=>"alive",
"host1_altmask"=>""}
admin.clean 'hosts', 'host', res
Returns:
[{"status"=>"alive",
"http_get_port"=>"",
"http_port"=>"",
"hostid"=>"1",
"hostip"=>"",
"hostname"=>"rur-1",
"remoteroot"=>"/mnt/mogilefs/rur-1",
"altip"=>"",
"altmask"=>""}]
280 281 282 283 284 285 286 287 288 |
# File 'lib/mogilefs/admin.rb', line 280 def clean(count, prefix, res, underscore = true) underscore = underscore ? '_' : '' (1..res[count].to_i).map do |i| dev = res.select { |k,_| k =~ /^#{prefix}#{i}#{underscore}/ }.map do |k,v| [k.sub(/^#{prefix}#{i}#{underscore}/, ''), v] end Hash[*dev.flatten] end end |
#create_class(domain, klass, mindevcount) ⇒ Object
Creates a new class in domain
named klass
with files replicated to mindevcount
devices. Returns nil on failure.
173 174 175 |
# File 'lib/mogilefs/admin.rb', line 173 def create_class(domain, klass, mindevcount) modify_class(domain, klass, mindevcount, :create) end |
#create_domain(domain) ⇒ Object
Creates a new domain named domain
. Returns nil if creation failed.
155 156 157 158 159 |
# File 'lib/mogilefs/admin.rb', line 155 def create_domain(domain) raise MogileFS::ReadOnlyError if readonly? res = @backend.create_domain :domain => domain res ? res['domain'] : nil end |
#create_host(host, args = {}) ⇒ Object
Creates a new host named host
. args
must contain :ip and :port. Returns true if successful, false if not.
197 198 199 200 201 202 |
# File 'lib/mogilefs/admin.rb', line 197 def create_host(host, args = {}) raise ArgumentError, "Must specify ip and port" unless \ args.include? :ip and args.include? :port modify_host(host, args, 'create') end |
#delete_class(domain, klass) ⇒ Object
Removes class klass
from domain
. Returns true if successful, false if not.
189 190 191 |
# File 'lib/mogilefs/admin.rb', line 189 def delete_class(domain, klass) ! @backend.delete_class(:domain => domain, :class => klass).nil? end |
#delete_domain(domain) ⇒ Object
Deletes domain
. Returns true if successful, false if not.
164 165 166 167 |
# File 'lib/mogilefs/admin.rb', line 164 def delete_domain(domain) raise MogileFS::ReadOnlyError if readonly? ! @backend.delete_domain(:domain => domain).nil? end |
#delete_host(host) ⇒ Object
Deletes host host
. Returns nil on failure.
214 215 216 217 |
# File 'lib/mogilefs/admin.rb', line 214 def delete_host(host) raise MogileFS::ReadOnlyError if readonly? ! @backend.delete_host(:host => host).nil? end |
#each_fid ⇒ Object
Enumerates fids using #list_fids.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mogilefs/admin.rb', line 11 def each_fid low = 0 high = nil max = get_stats('fids')['fids']['max'] 0.step max, 100 do |high| fids = list_fids low, high fids.each { |fid| yield fid } low = high + 1 end end |
#get_devices(devid = nil) ⇒ Object
Returns an Array of device status Hashes. If devid is given only that device is returned.
admin.get_devices 1
Returns:
[{"status"=>"alive",
"mb_asof"=>"",
"mb_free"=>"0",
"devid"=>"1",
"hostid"=>"1",
"mb_used"=>"",
"mb_total"=>""}]
63 64 65 66 |
# File 'lib/mogilefs/admin.rb', line 63 def get_devices(devid = nil) clean('devices', 'dev', @backend.get_devices(devid ? { :devid => devid } : {})) end |
#get_domains ⇒ Object
Returns the domains present in the mogilefs.
admin.get_domains
Returns:
{"test"=>{"normal"=>3, "default"=>2}}
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mogilefs/admin.rb', line 139 def get_domains res = @backend.get_domains domains = {} (1..res['domains'].to_i).each do |i| domain = clean "domain#{i}classes", "domain#{i}class", res, false domain = domain.map { |d| [d.values.first, d.values.last.to_i] } domains[res["domain#{i}"]] = Hash[*domain.flatten] end domains end |
#get_hosts(hostid = nil) ⇒ Object
Returns an Array of host status Hashes. If hostid
is given only that host is returned.
admin.get_hosts 1
Returns:
[{"status"=>"alive",
"http_get_port"=>"",
"http_port"=>"",
"hostid"=>"1",
"hostip"=>"",
"hostname"=>"rur-1",
"remoteroot"=>"/mnt/mogilefs/rur-1",
"altip"=>"",
"altmask"=>""}]
42 43 44 45 |
# File 'lib/mogilefs/admin.rb', line 42 def get_hosts(hostid = nil) clean('hosts', 'host', @backend.get_hosts(hostid ? { :hostid => hostid } : {})) end |
#get_stats(type = 'all') ⇒ Object
Returns a statistics structure representing the state of mogilefs.
admin.get_stats
Returns:
{"fids"=>{"max"=>"99", "count"=>"2"},
"device"=>
[{"status"=>"alive", "files"=>"2", "id"=>"1", "host"=>"rur-1"},
{"status"=>"alive", "files"=>"2", "id"=>"2", "host"=>"rur-2"}],
"replication"=>
[{"files"=>"2", "class"=>"normal", "devcount"=>"2", "domain"=>"test"}],
"file"=>[{"files"=>"2", "class"=>"normal", "domain"=>"test"}]}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mogilefs/admin.rb', line 108 def get_stats(type = 'all') res = @backend.stats type => 1 stats = {} stats['device'] = clean 'devicescount', 'devices', res, false stats['file'] = clean 'filescount', 'files', res, false stats['replication'] = clean 'replicationcount', 'replication', res, false if res['fidmax'] or res['fidcount'] then stats['fids'] = { 'max' => res['fidmax'].to_i, 'count' => res['fidcount'].to_i } end stats.delete 'device' if stats['device'].empty? stats.delete 'file' if stats['file'].empty? stats.delete 'replication' if stats['replication'].empty? stats end |
#list_fids(from_fid, to_fid) ⇒ Object
Returns an Array of fid Hashes from from_fid
to to_fid
.
admin.list_fids 0, 100
Returns:
[{"fid"=>"99",
"class"=>"normal",
"domain"=>"test",
"devcount"=>"2",
"length"=>"4",
"key"=>"file_key"},
{"fid"=>"82",
"class"=>"normal",
"devcount"=>"2",
"domain"=>"test",
"length"=>"9",
"key"=>"new_new_key"}]
88 89 90 91 |
# File 'lib/mogilefs/admin.rb', line 88 def list_fids(from_fid, to_fid) clean('fid_count', 'fid_', @backend.list_fids(:from => from_fid, :to => to_fid)) end |
#modify_class(domain, klass, mindevcount, action) ⇒ Object
Modifies klass
on domain
to store files on mindevcount
devices via action
. Returns the class name if successful, nil if not.
234 235 236 237 238 239 240 |
# File 'lib/mogilefs/admin.rb', line 234 def modify_class(domain, klass, mindevcount, action) raise MogileFS::ReadOnlyError if readonly? res = @backend.send("#{action}_class", :domain => domain, :class => klass, :mindevcount => mindevcount) res ? res['class'] : nil end |
#modify_host(host, args = {}, action = 'create') ⇒ Object
Modifies host
using args
via action
. Returns true if successful, false if not.
246 247 248 249 |
# File 'lib/mogilefs/admin.rb', line 246 def modify_host(host, args = {}, action = 'create') args[:host] = host ! @backend.send("#{action}_host", args).nil? end |
#update_class(domain, klass, mindevcount) ⇒ Object
Updates class klass
in domain
to be replicated to mindevcount
devices. Returns nil on failure.
181 182 183 |
# File 'lib/mogilefs/admin.rb', line 181 def update_class(domain, klass, mindevcount) modify_class(domain, klass, mindevcount, :update) end |
#update_host(host, args = {}) ⇒ Object
Updates host
with args
. Returns true if successful, false if not.
207 208 209 |
# File 'lib/mogilefs/admin.rb', line 207 def update_host(host, args = {}) modify_host(host, args, 'update') end |