mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob abacfc8cfc9e6719653e9a7422df4fd6bc0b1409 8446 bytes (raw)
$ git show pipeline:lib/mogilefs/admin.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
 
# -*- encoding: binary -*-

# \MogileFS administration client, this has little real-world usage
# and is considered a work-in-progress
class MogileFS::Admin < MogileFS::Client

  ##
  # Enumerates fids using #list_fids.

  def each_fid
    low = 0

    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

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

  def get_hosts(hostid = nil)
    clean('hosts', 'host',
          @backend.get_hosts(hostid ? { :hostid => hostid } : {}))
  end

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

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

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

  def list_fids(from_fid, to_fid)
    clean('fid_count', 'fid_',
          @backend.list_fids(:from => from_fid, :to => to_fid))
  end

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

  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

  ##
  # Returns the domains and classes, and their policies present in the mogilefs.
  #
  #   admin.get_domains
  #
  # Returns (on newer MogileFS servers):
  #   {
  #     "test" => {
  #       "default" => {
  #         "mindevcount" => 2,
  #         "replpolicy" => "MultipleHosts()"
  #       }
  #     }
  #   }
  #
  # Returns (on older MogileFS servers without replication policies):
  #
  #   {"test"=>{"normal"=>3, "default"=>2}}

  def get_domains
    res = @backend.get_domains
    have_replpolicy = false

    domains = {}
    (1..res['domains'].to_i).each do |i|
      domain = clean "domain#{i}classes", "domain#{i}class", res, false

      tmp = domains[res["domain#{i}"]] = {}
      domain.each do |d|
        tmp[d.delete("name")] = d
        d["mindevcount"] = d["mindevcount"].to_i
        have_replpolicy ||= d.include?("replpolicy")
      end
    end

    # only for MogileFS 1.x?, maybe we can drop support for this...
    unless have_replpolicy
      domains.each do |namespace, class_data|
        class_data.each do |class_name, data|
          class_data[class_name] = data["mindevcount"]
        end
      end
    end

    domains
  end

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

  def create_domain(domain)
    raise MogileFS::ReadOnlyError if readonly?
    res = @backend.create_domain :domain => domain
    res ? res['domain'] : nil
  end

  ##
  # Deletes +domain+.  Returns true if successful, raises
  # MogileFS::Backend::DomainNotFoundError if not

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

  ##
  # Creates a new class in +domain+ named +klass+ with +policy+ for
  # replication.  Raises on failure.

  def create_class(domain, klass, policy)
    modify_class(domain, klass, policy, :create)
  end

  ##
  # Updates class +klass+ in +domain+ with +policy+ for replication.
  # Raises on failure.

  def update_class(domain, klass, policy)
    modify_class(domain, klass, policy, :update)
  end

  ##
  # Removes class +klass+ from +domain+.  Returns true if successful.
  # Raises on failure

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

  ##
  # Creates a new host named +host+.  +args+ must contain :ip and :port.
  # Returns true if successful, false if not.

  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

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

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

  ##
  # Deletes host +host+.  Returns nil on failure.

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

  ##
  # Changes the device status of +device+ on +host+ to +state+ which can be
  # 'alive', 'down', or 'dead'.

  def change_device_state(host, device, state)
    raise MogileFS::ReadOnlyError if readonly?
    ! @backend.set_state(:host => host, :device => device, :state => state).nil?
  end

  protected unless defined? $TESTING

  ##
  # Modifies +klass+ on +domain+ to store files on +mindevcount+ devices via
  # +action+.  Returns the class name if successful, raises if not

  def modify_class(domain, klass, policy, action)
    raise MogileFS::ReadOnlyError if readonly?
    args = { :domain => domain, :class => klass }
    case policy
    when Integer
      args[:mindevcount] = policy
    when String
      args[:replpolicy] = policy
    when Hash
      args.merge!(policy)
    else
      raise ArgumentError,
           "policy=#{policy.inspect} not understood for #{action}_class"
    end
    @backend.__send__("#{action}_class", args)["class"]
  end

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

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

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

  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

end


git clone https://yhbt.net/mogilefs-client.git