Sha256: daa6dc555479b9799567df92d85054e38e037028d0414378cdcb027db8f3d7c5

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

# -*- encoding: binary -*-
# helpers for device lookups
module Regurgitator::Device
  include Regurgitator::ServerSettings

  # :stopdoc:
  REFRESH_DEVICE = <<-EOS
    SELECT d.devid, h.hostip, h.http_port, h.http_get_port
    FROM device d
      LEFT JOIN host h ON d.hostid = h.hostid
    WHERE d.status IN ('readonly','alive','drain') AND h.status = 'alive'
  EOS

  DEVICES_ON = 'SELECT devid FROM file_on WHERE fid = ?'
  # :startdoc:

  def device_init
    server_settings_init
    @device_cache_mtime = Time.at(0)
    @device_cache = nil
    @device_cache_lock = Mutex.new
  end

  def self.extended(obj)
    obj.device_init
  end

  def device_uris!(opts, get_port)
    opts[:port] = get_port if get_port
    [ URI::HTTP.build(opts) ]
  end

  # Returns a hash of device info with the Integer +devid+
  # as the hash key.
  def refresh_device(force = false) # :nodoc:
    @device_cache_lock.synchronize { refresh_device_unlocked(force) }
  end

  def refresh_device_unlocked(force) # :nodoc:
    return @device_cache if ! force && ((Time.now - @device_cache_mtime) < 60)
    tmp = {}.compare_by_identity
    refresh_zone(force)
    @db[REFRESH_DEVICE].each do |x|
      # devices in "drain" status may hit raciness try those as a last resort
      x[:preferred] = !!(x[:d_status] =~ %r{\A(?:readonly|alive)\z})
      hostip = x[:hostip]
      port = x[:http_port] || 80
      get_port = x[:http_get_port]
      x[:ipaddr] = { :hostip => hostip }
      x[:zone] = zone_for(hostip)
      devid = x[:devid]
      o = { :path => "/dev#{devid}", :port => port, :host => hostip }
      x[:uris] = { :pri => device_uris!(o, get_port) }
      tmp[devid] = x
    end
    Regurgitator::Local.refresh_addrs!
    @device_cache_mtime = Time.now
    @device_cache = tmp
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
regurgitator-0.6.0 lib/regurgitator/device.rb
regurgitator-0.5.0 lib/regurgitator/device.rb
regurgitator-0.4.0 lib/regurgitator/device.rb
regurgitator-0.3.0 lib/regurgitator/device.rb