Class: MogileFS::Admin

Inherits:
Client
  • Object
show all
Defined in:
lib/mogilefs/admin.rb

Overview

A MogileFS Administration Client

Instance Attribute Summary

Attributes inherited from Client

#backend, #hosts

Instance Method Summary collapse

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’.



228
229
230
231
232
# File 'lib/mogilefs/admin.rb', line 228

def change_device_state(host, device, state)
  raise 'readonly mogilefs' if readonly?
  res = @backend.set_state :host => host, :device => device, :state => state
  return !res.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"=>""}]


287
288
289
290
291
292
293
294
295
# File 'lib/mogilefs/admin.rb', line 287

def clean(count, prefix, res, underscore = true)
  underscore = underscore ? '_' : ''
  return (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.



176
177
178
# File 'lib/mogilefs/admin.rb', line 176

def create_class(domain, klass, mindevcount)
  return modify_class(domain, klass, mindevcount, :create)
end

#create_domain(domain) ⇒ Object

Creates a new domain named domain. Returns nil if creation failed.



157
158
159
160
161
# File 'lib/mogilefs/admin.rb', line 157

def create_domain(domain)
  raise 'readonly mogilefs' if readonly?
  res = @backend.create_domain :domain => domain
  return res['domain'] unless res.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.

Raises:

  • (ArgumentError)


201
202
203
204
205
206
# File 'lib/mogilefs/admin.rb', line 201

def create_host(host, args = {})
  raise ArgumentError, "Must specify ip and port" unless \
    args.include? :ip and args.include? :port

  return modify_host(host, args, 'create')
end

#delete_class(domain, klass) ⇒ Object

Removes class klass from domain. Returns true if successful, false if not.



192
193
194
195
# File 'lib/mogilefs/admin.rb', line 192

def delete_class(domain, klass)
  res = @backend.delete_class :domain => domain, :class => klass
  return !res.nil?
end

#delete_domain(domain) ⇒ Object

Deletes domain. Returns true if successful, false if not.



166
167
168
169
170
# File 'lib/mogilefs/admin.rb', line 166

def delete_domain(domain)
  raise 'readonly mogilefs' if readonly?
  res = @backend.delete_domain :domain => domain
  return !res.nil?
end

#delete_host(host) ⇒ Object

Deletes host host. Returns nil on failure.



218
219
220
221
222
# File 'lib/mogilefs/admin.rb', line 218

def delete_host(host)
  raise 'readonly mogilefs' if readonly?
  res = @backend.delete_host :host => host
  return !res.nil?
end

#each_fidObject

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"=>""}]


64
65
66
67
68
# File 'lib/mogilefs/admin.rb', line 64

def get_devices(devid = nil)
  args = devid ? { :devid => devid } : {}
  res = @backend.get_devices args
  return clean('devices', 'dev', res)
end

#get_domainsObject

Returns the domains present in the mogilefs.

admin.get_domains

Returns:

{"test"=>{"normal"=>3, "default"=>2}}


141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mogilefs/admin.rb', line 141

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

  return 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
46
# File 'lib/mogilefs/admin.rb', line 42

def get_hosts(hostid = nil)
  args = hostid ? { :hostid => hostid } : {}
  res = @backend.get_hosts args
  return clean('hosts', 'host', res)
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"}]}


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mogilefs/admin.rb', line 110

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?

  return 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"}]


90
91
92
93
# File 'lib/mogilefs/admin.rb', line 90

def list_fids(from_fid, to_fid)
  res = @backend.list_fids :from => from_fid, :to => to_fid
  return clean('fid_count', 'fid_', res)
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.



240
241
242
243
244
245
246
# File 'lib/mogilefs/admin.rb', line 240

def modify_class(domain, klass, mindevcount, action)
  raise 'readonly mogilefs' if readonly?
  res = @backend.send("#{action}_class", :domain => domain, :class => klass,
                                        :mindevcount => mindevcount)

  return res['class'] unless res.nil?
end

#modify_host(host, args = {}, action = 'create') ⇒ Object

Modifies host using args via action. Returns true if successful, false if not.



252
253
254
255
256
# File 'lib/mogilefs/admin.rb', line 252

def modify_host(host, args = {}, action = 'create')
  args[:host] = host
  res = @backend.send "#{action}_host", args
  return !res.nil?
end

#update_class(domain, klass, mindevcount) ⇒ Object

Updates class klass in domain to be replicated to mindevcount devices. Returns nil on failure.



184
185
186
# File 'lib/mogilefs/admin.rb', line 184

def update_class(domain, klass, mindevcount)
  return modify_class(domain, klass, mindevcount, :update)
end

#update_host(host, args = {}) ⇒ Object

Updates host with args. Returns true if successful, false if not.



211
212
213
# File 'lib/mogilefs/admin.rb', line 211

def update_host(host, args = {})
  return modify_host(host, args, 'update')
end