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



224
225
226
227
# File 'lib/mogilefs/admin.rb', line 224

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


281
282
283
284
285
286
287
288
289
# File 'lib/mogilefs/admin.rb', line 281

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.



174
175
176
# File 'lib/mogilefs/admin.rb', line 174

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.



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

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.

Raises:

  • (ArgumentError)


198
199
200
201
202
203
# File 'lib/mogilefs/admin.rb', line 198

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.



190
191
192
# File 'lib/mogilefs/admin.rb', line 190

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.



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

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.



215
216
217
218
# File 'lib/mogilefs/admin.rb', line 215

def delete_host(host)
  raise MogileFS::ReadOnlyError if readonly?
  ! @backend.delete_host(:host => host).nil?
end

#each_fidObject

Enumerates fids using #list_fids.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mogilefs/admin.rb', line 12

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

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

#get_domainsObject

Returns the domains present in the mogilefs.

admin.get_domains

Returns:

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


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

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


43
44
45
46
# File 'lib/mogilefs/admin.rb', line 43

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


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

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


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

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.



235
236
237
238
239
240
241
# File 'lib/mogilefs/admin.rb', line 235

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.



247
248
249
250
# File 'lib/mogilefs/admin.rb', line 247

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.



182
183
184
# File 'lib/mogilefs/admin.rb', line 182

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.



208
209
210
# File 'lib/mogilefs/admin.rb', line 208

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