mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob afc2f1aabfed1bc710b064baae07fef03e5cab81 11487 bytes (raw)
$ git show pu:lib/mogilefs/backend.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
 
# -*- encoding: binary -*-
require 'thread'

# This class communicates with the MogileFS trackers.
# You should not have to use this directly unless you are developing
# support for new commands or plugins for MogileFS
class MogileFS::Backend

  # Adds MogileFS commands +names+.
  def self.add_command(*names)
    names.each do |name|
      define_method name do |*args|
        do_request(name, args[0] || {}, false)
      end
    end
  end

  # adds idempotent MogileFS commands +names+, these commands may be retried
  # transparently on a different tracker if there is a network/server error.
  def self.add_idempotent_command(*names)
    names.each do |name|
      define_method name do |*args|
        do_request(name, args[0] || {}, true)
      end
    end
  end

  BACKEND_ERRORS = {} # :nodoc:

  # this converts an error code from a mogilefsd tracker to an exception:
  #
  # Examples of some exceptions that get created:
  #   class AfterMismatchError < MogileFS::Error; end
  #   class DomainNotFoundError < MogileFS::Error; end
  #   class InvalidCharsError < MogileFS::Error; end
  def self.add_error(err_snake)
    err_camel = err_snake.gsub(/(?:^|_)([a-z])/) { $1.upcase }
    err_camel << 'Error' unless /Error\z/ =~ err_camel
    unless const_defined?(err_camel)
      const_set(err_camel, Class.new(MogileFS::Error))
    end
    BACKEND_ERRORS[err_snake] = const_get(err_camel)
  end

  def self.const_missing(name) # :nodoc:
    if /Error\z/ =~ name.to_s
      const_set(name, Class.new(MogileFS::Error))
    else
      super name
    end
  end

  ##
  # The last error

  attr_reader :lasterr

  ##
  # The string attached to the last error

  attr_reader :lasterrstr

  ##
  # Creates a new MogileFS::Backend.
  #
  # :hosts is a required argument and must be an Array containing one or more
  # 'hostname:port' pairs as Strings.
  #
  # :timeout adjusts the request timeout before an error is returned.

  def initialize(args)
    @hosts = args[:hosts]
    @fail_timeout = args[:fail_timeout] || 5
    raise ArgumentError, "must specify at least one host" unless @hosts
    raise ArgumentError, "must specify at least one host" if @hosts.empty?
    unless @hosts == @hosts.select { |h| h =~ /:\d+$/ } then
      raise ArgumentError, ":hosts must be in 'host:port' form"
    end

    @mutex = Mutex.new
    @timeout = args[:timeout] || 3
    @socket = nil
    @lasterr = nil
    @lasterrstr = nil
    @pending = []

    @dead = {}
  end

  ##
  # Closes this backend's socket.

  def shutdown
    @mutex.synchronize { shutdown_unlocked }
  end

  # MogileFS::MogileFS commands

  add_command :create_open
  add_command :create_close
  add_idempotent_command :get_paths
  add_idempotent_command :noop
  add_command :delete
  add_idempotent_command :sleep
  add_command :rename
  add_idempotent_command :list_keys
  add_idempotent_command :file_info
  add_idempotent_command :file_debug

  # MogileFS::Backend commands

  add_idempotent_command :get_hosts
  add_idempotent_command :get_devices
  add_idempotent_command :list_fids
  add_idempotent_command :stats
  add_idempotent_command :get_domains
  add_command :create_device
  add_command :create_domain
  add_command :delete_domain
  add_command :create_class
  add_command :update_class
  add_command :updateclass
  add_command :delete_class
  add_command :create_host
  add_command :update_host
  add_command :delete_host
  add_command :set_state
  add_command :set_weight
  add_command :replicate_now

  def shutdown_unlocked(do_raise = false) # :nodoc:
    @pending = []
    if @socket
      @socket.close rescue nil # ignore errors
      @socket = nil
    end
    raise if do_raise
  end

  def dispatch_unlocked(request, timeout = @timeout) # :nodoc:
    begin
      io = socket
      io.timed_write(request, timeout)
      io
    rescue SystemCallError, MogileFS::RequestTruncatedError  => err
      @dead[@active_host] = [ Time.now, err ]
      shutdown_unlocked
      retry
    end
  end

  def pipeline_gets_unlocked(io, timeout) # :nodoc:
    line = io.timed_gets(timeout) or
      raise MogileFS::PipelineError,
            "EOF with #{@pending.size} requests in-flight"
    ready = @pending.shift
    ready[1].call(parse_response(line, ready[0]))
  end

  def timeout_update(timeout, t0) # :nodoc:
    timeout -= (Time.now - t0)
    timeout < 0 ? 0 : timeout
  end

  # try to read any responses we have pending already before filling
  # the pipeline more requests.  This usually takes very little time,
  # but trackers may return huge responses and we could be on a slow
  # network.
  def pipeline_drain_unlocked(io, timeout) # :nodoc:
    set = [ io ]
    while @pending.size > 0
      t0 = Time.now
      r = IO.select(set, set, nil, timeout)
      timeout = timeout_update(timeout, t0)

      if r && r[0][0]
        t0 = Time.now
        pipeline_gets_unlocked(io, timeout)
        timeout = timeout_update(timeout, t0)
      else
        return timeout
      end
    end
    timeout
  end

  # dispatch a request like do_request, but queue +block+ for execution
  # upon receiving a response.  It is the users' responsibility to ensure
  # &block is executed in the correct order.  Trackers with multiple
  # queryworkers are not guaranteed to return responses in the same
  # order they were requested.
  def pipeline_dispatch(cmd, args, &block) # :nodoc:
    request = make_request(cmd, args)
    timeout = @timeout

    @mutex.synchronize do
      io = socket
      timeout = pipeline_drain_unlocked(io, timeout)

      # send the request out...
      begin
        io.timed_write(request, timeout)
        @pending << [ request, block ]
      rescue SystemCallError, MogileFS::RequestTruncatedError => err
        @dead[@active_host] = [ Time.now, err ]
        shutdown_unlocked(@pending[0])
        io = socket
        retry
      end

      @pending.size
    end
  end

  def pipeline_wait(count = nil) # :nodoc:
    @mutex.synchronize do
      io = socket
      count ||= @pending.size
      @pending.size < count and
        raise MogileFS::Error,
              "pending=#{@pending.size} < expected=#{count} failed"
      begin
        count.times { pipeline_gets_unlocked(io, @timeout) }
      rescue
        shutdown_unlocked(true)
      end
    end
  end

  # Performs the +cmd+ request with +args+.
  def do_request(cmd, args, idempotent = false)
    no_raise = args.delete(:ruby_no_raise)
    request = make_request(cmd, args)
    line = nil
    failed = false
    @mutex.synchronize do
      begin
        io = dispatch_unlocked(request)
        line = io.timed_gets(@timeout)
        break if /\r?\n\z/ =~ line

        line and raise MogileFS::InvalidResponseError,
                       "Invalid response from server: #{line.inspect}"

        idempotent or
          raise EOFError, "end of file reached after: #{request.inspect}"
        # fall through to retry in loop
      rescue SystemCallError,
             MogileFS::InvalidResponseError # truncated response
        # we got a successful timed_write, but not a timed_gets
        if idempotent
          failed = true
          shutdown_unlocked(false)
          retry
        end
        shutdown_unlocked(true)
      rescue MogileFS::UnreadableSocketError, MogileFS::Timeout
        shutdown_unlocked(true)
      rescue
        # we DO NOT want the response we timed out waiting for, to crop up later
        # on, on the same socket, intersperesed with a subsequent request!  we
        # close the socket if there's any error.
        shutdown_unlocked(true)
      end while idempotent
      shutdown_unlocked if failed
    end # @mutex.synchronize
    parse_response(line, no_raise ? request : nil)
  end

  # Makes a new request string for +cmd+ and +args+.
  def make_request(cmd, args)
    "#{cmd} #{url_encode args}\r\n"
  end

  # this converts an error code from a mogilefsd tracker to an exception
  # Most of these exceptions should already be defined, but since the
  # MogileFS server code is liable to change and we may not always be
  # able to keep up with the changes
  def error(err_snake)
    BACKEND_ERRORS[err_snake] || self.class.add_error(err_snake)
  end

  # Turns the +line+ response from the server into a Hash of options, an
  # error, or raises, as appropriate.
  def parse_response(line, request = nil)
    case line
    when /\AOK\s+\d*\s*(\S*)\r?\n\z/
      url_decode($1)
    when /\AERR\s+(\w+)\s*([^\r\n]*)/
      @lasterr = $1
      @lasterrstr = $2 ? url_unescape($2) : nil
      if request
        request = " request=#{request.strip}"
        @lasterrstr = @lasterrstr ? (@lasterrstr << request) : request
        return error(@lasterr).new(@lasterrstr)
      end
      raise error(@lasterr).new(@lasterrstr)
    else
      raise MogileFS::InvalidResponseError,
            "Invalid response from server: #{line.inspect}"
    end
  end

  # this command is special since the cache is per-tracker, so we connect
  # to all backends and not just one
  def clear_cache(types = %w(all))
    opts = {}
    types.each { |type| opts[type] = 1 }

    sockets = @hosts.map do |host|
      MogileFS::Socket.start(*(host.split(/:/))) rescue nil
    end
    sockets.compact!

    wpending = sockets
    rpending = []
    request = make_request("clear_cache", opts)
    while wpending[0] || rpending[0]
      r = IO.select(rpending, wpending, nil, @timeout) or return
      rpending -= r[0]
      wpending -= r[1]
      r[0].each { |io| io.timed_gets(0) rescue nil }
      r[1].each do |io|
        begin
          io.timed_write(request, 0)
          rpending << io
        rescue
        end
      end
    end
    nil
    ensure
      sockets.each { |io| io.close }
  end

  # Returns a socket connected to a MogileFS tracker.
  def socket
    return @socket if @socket and not @socket.closed?

    @hosts.shuffle.each do |host|
      next if dead = @dead[host] and dead[0] > (Time.now - @fail_timeout)

      begin
        addr, port = host.split(/:/)
        @socket = MogileFS::Socket.tcp(addr, port, @timeout)
        @active_host = host
      rescue SystemCallError, MogileFS::Timeout => err
        @dead[host] = [ Time.now, err ]
        next
      end

      return @socket
    end

    errors = @dead.map { |host,(_,e)| "#{host} - #{e.message} (#{e.class})" }
    raise MogileFS::UnreachableBackendError,
          "couldn't connect to any tracker: #{errors.join(', ')}"
  end

  # Turns a url params string into a Hash.
  def url_decode(str) # :nodoc:
    rv = {}
    str.split(/&/).each do |pair|
      k, v = pair.split(/=/, 2).map! { |x| url_unescape(x) }
      rv[k.freeze] = v
    end
    rv
  end

  # :stopdoc:
  # TODO: see if we can use existing URL-escape/unescaping routines
  # in the Ruby standard library, Perl MogileFS seems to NIH these
  #  routines, too
  # :startdoc:

  # Turns a Hash (or Array of pairs) into a url params string.
  def url_encode(params) # :nodoc:
    params.map do |k,v|
      "#{url_escape k.to_s}=#{url_escape v.to_s}"
    end.join("&")
  end

  # Escapes naughty URL characters.
  if ''.respond_to?(:ord) # Ruby 1.9
    def url_escape(str) # :nodoc:
      str.gsub(/([^\w\,\-.\/\\\: ])/) { "%%%02x" % $1.ord }.tr(' ', '+')
    end
  else # Ruby 1.8
    def url_escape(str) # :nodoc:
      str.gsub(/([^\w\,\-.\/\\\: ])/) { "%%%02x" % $1[0] }.tr(' ', '+')
    end
  end

  # Unescapes naughty URL characters.
  def url_unescape(str) # :nodoc:
    str.tr('+', ' ').gsub(/%([a-f0-9][a-f0-9])/i) { [$1.to_i(16)].pack 'C' }
  end
end

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