Mongo::MongoShardedClient

Instantiates and manages connections to a MongoDB sharded cluster for high availability.

Attributes

manager[R]
refresh_interval[R]
refresh_mode[R]
refresh_version[R]
seeds[R]

Public Class Methods

from_uri(uri, options = {}) click to toggle source

Initialize a connection to MongoDB using the MongoDB URI spec.

@param uri [ String ] string of the format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]

@param opts [ Hash ] Any of the options available for MongoShardedClient.new

@return [ Mongo::MongoShardedClient ] The sharded client.

# File lib/mongo/mongo_sharded_client.rb, line 153
def self.from_uri(uri, options = {})
  uri ||= ENV['MONGODB_URI']
  URIParser.new(uri).connection(options, false, true)
end
new(*args) click to toggle source
# File lib/mongo/mongo_sharded_client.rb, line 26
def initialize(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}

  nodes = args.flatten

  if nodes.empty? and ENV.has_key?('MONGODB_URI')
    parser = URIParser.new ENV['MONGODB_URI']
    opts = parser.connection_options.merge! opts
    nodes = parser.node_strings
  end

  unless nodes.length > 0
    raise MongoArgumentError, "A MongoShardedClient requires at least one seed node."
  end

  @seeds = nodes.map do |host_port|
    Support.normalize_seeds(host_port)
  end

  # TODO: add a method for replacing this list of node.
  @seeds.freeze

  # Refresh
  @last_refresh = Time.now
  @refresh_version = 0

  # No connection manager by default.
  @manager = nil

  # Lock for request ids.
  @id_lock = Mutex.new

  @connected = false

  @connect_mutex = Mutex.new

  @mongos        = true

  check_opts(opts)
  setup(opts)
end

Public Instance Methods

checkout(&block) click to toggle source
# File lib/mongo/mongo_sharded_client.rb, line 135
def checkout(&block)
  tries = 0
  begin
    super(&block)
  rescue ConnectionFailure
    tries +=1
    tries < 2 ? retry : raise
  end
end
connect(force = !connected?) click to toggle source

Initiate a connection to the sharded cluster.

# File lib/mongo/mongo_sharded_client.rb, line 78
def connect(force = !connected?)
  return unless force
  log(:info, "Connecting...")

  # Prevent recursive connection attempts from the same thread.
  # This is done rather than using a Monitor to prevent potentially recursing
  # infinitely while attempting to connect and continually failing. Instead, fail fast.
  raise ConnectionFailure, "Failed to get node data." if thread_local[:locks][:connecting]

  @connect_mutex.synchronize do
    begin
      thread_local[:locks][:connecting] = true
      if @manager
        thread_local[:managers][self] = @manager
        @manager.refresh! @seeds
      else
        @manager = ShardingPoolManager.new(self, @seeds)
        ensure_manager
        @manager.connect
      end
    ensure
      thread_local[:locks][:connecting] = false
    end

    @refresh_version += 1
    @last_refresh = Time.now
    @connected = true
  end
end
connected?() click to toggle source
# File lib/mongo/mongo_sharded_client.rb, line 120
def connected?
  !!(@connected && @manager.primary_pool)
end
hard_refresh!() click to toggle source

Force a hard refresh of this connection’s view of the sharded cluster.

@return [Boolean] true if hard refresh

occurred. +false+ is returned when unable
to get the refresh lock.
# File lib/mongo/mongo_sharded_client.rb, line 114
def hard_refresh!
  log(:info, "Initiating hard refresh...")
  connect(true)
  return true
end
inspect() click to toggle source
# File lib/mongo/mongo_sharded_client.rb, line 72
def inspect
  "<Mongo::MongoShardedClient:0x#{self.object_id.to_s(16)} @seeds=#{@seeds.inspect} " +
      "@connected=#{@connected}>"
end
slave_ok?() click to toggle source

Returns true if it’s okay to read from a secondary node. Since this is a sharded cluster, this must always be false.

This method exist primarily so that Cursor objects will generate query messages with a slaveOkay value of true.

@return [Boolean] true

# File lib/mongo/mongo_sharded_client.rb, line 131
def slave_ok?
  false
end
valid_opts() click to toggle source
# File lib/mongo/mongo_sharded_client.rb, line 68
def valid_opts
  super + SHARDED_CLUSTER_OPTS
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.