Class: Mongo::ReplSetConnection

Inherits:
Connection show all
Defined in:
lib/mongo/repl_set_connection.rb

Overview

Instantiates and manages connections to a MongoDB replica set.

Constant Summary

Constants inherited from Connection

Connection::ConditionVariable, Connection::DEFAULT_PORT, Connection::Mutex, Connection::RESPONSE_HEADER_SIZE, Connection::STANDARD_HEADER_SIZE, Connection::TCPSocket, Connection::UNIXSocket

Instance Attribute Summary collapse

Attributes inherited from Connection

#auths, #host_to_try, #logger, #pool_size, #primary, #primary_pool, #safe, #size, #unix_socket_path

Instance Method Summary collapse

Methods inherited from Connection

#[], #active?, #add_auth, #apply_saved_authentication, #clear_auths, #connected?, #copy_database, #database_info, #database_names, #db, #drop_database, from_uri, #instrument, #lock!, #locked?, #max_bson_size, multi, #ping, #receive_message, #remove_auth, #send_message, #send_message_with_safe_check, #server_info, #server_version, #to_s, #unlock!

Constructor Details

#initialize(*args) ⇒ ReplSetConnection

Create a connection to a MongoDB replica set.

Once connected to a replica set, you can find out which nodes are primary, secondary, and arbiters with the corresponding accessors: Connection#primary, Connection#secondaries, and Connection#arbiters. This is useful if your application needs to connect manually to nodes other than the primary.

Examples:

Connect to a replica set and provide two seed nodes. Note that the number of seed nodes does

not have to be equal to the number of replica set members. The purpose of seed nodes is to permit
the driver to find at least one replica set member even if a member is down.
ReplSetConnection.new(['localhost', 30000], ['localhost', 30001])

Connect to a replica set providing two seed nodes and ensuring a connection to the replica set named ‘prod’:

ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :rs_name => 'prod')

Connect to a replica set providing two seed nodes and allowing reads from a secondary node:

ReplSetConnection.new(['localhost', 30000], ['localhost', 30001], :read_secondary => true)

Parameters:

  • args (Array)

    A list of host-port pairs to be used as seed nodes followed by a hash containing any options. See the examples below for exactly how to use the constructor.

  • options (Hash)

    a customizable set of options

Raises:

  • (ReplicaSetConnectionError)

    This is raised if a replica set name is specified and the driver fails to connect to a replica set with that name.

See Also:



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
# File 'lib/mongo/repl_set_connection.rb', line 65

def initialize(*args)
  if args.last.is_a?(Hash)
    opts = args.pop
  else
    opts = {}
  end

  unless args.length > 0
    raise MongoArgumentError, "A ReplSetConnection requires at least one node."
  end

  # Get seed nodes
  @nodes = args

  # Replica set name
  @replica_set = opts[:rs_name]

  # Cache the various node types when connecting to a replica set.
  @secondaries = []
  @arbiters    = []

  # Connection pools for each secondary node
  @secondary_pools = []
  @read_pool = nil

  # Are we allowing reads from secondaries?
  @read_secondary = opts.fetch(:read_secondary, false)

  setup(opts)
end

Instance Attribute Details

#arbitersObject (readonly)

Returns the value of attribute arbiters.



23
24
25
# File 'lib/mongo/repl_set_connection.rb', line 23

def arbiters
  @arbiters
end

#nodesObject (readonly)

Returns the value of attribute nodes.



23
24
25
# File 'lib/mongo/repl_set_connection.rb', line 23

def nodes
  @nodes
end

#read_poolObject (readonly)

Returns the value of attribute read_pool.



23
24
25
# File 'lib/mongo/repl_set_connection.rb', line 23

def read_pool
  @read_pool
end

#secondariesObject (readonly)

Returns the value of attribute secondaries.



23
24
25
# File 'lib/mongo/repl_set_connection.rb', line 23

def secondaries
  @secondaries
end

#secondary_poolsObject (readonly)

Returns the value of attribute secondary_pools.



23
24
25
# File 'lib/mongo/repl_set_connection.rb', line 23

def secondary_pools
  @secondary_pools
end

Instance Method Details

#authenticate_poolsObject



175
176
177
178
179
180
# File 'lib/mongo/repl_set_connection.rb', line 175

def authenticate_pools
  super
  @secondary_pools.each do |pool|
    pool.authenticate_existing
  end
end

#closeObject

Close the connection to the database.



148
149
150
151
152
153
154
# File 'lib/mongo/repl_set_connection.rb', line 148

def close
  super
  @read_pool = nil
  @secondary_pools.each do |pool|
    pool.close
  end
end

#connectObject Also known as: reconnect

Create a new socket and attempt to connect to master. If successful, sets host and port to master and returns the socket.

If connecting to a replica set, this method will replace the initially-provided seed list with any nodes known to the set.

Raises:



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
# File 'lib/mongo/repl_set_connection.rb', line 103

def connect
  reset_connection
  @nodes_to_try = @nodes.clone

  while connecting?
    node   = @nodes_to_try.shift
    config = check_is_master(node)

    if is_primary?(config)
      set_primary(node)
    else
      set_auxillary(node, config)
    end
  end

  pick_secondary_for_read if @read_secondary

  if connected?
    BSON::BSON_CODER.update_max_bson_size(self)
  else
    if @secondary_pools.empty?
      close # close any existing pools and sockets
      raise ConnectionFailure, "Failed to connect any given host:port"
    else
      close # close any existing pools and sockets
      raise ConnectionFailure, "Failed to connect to primary node."
    end
  end
end

#connecting?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/mongo/repl_set_connection.rb', line 134

def connecting?
  @nodes_to_try.length > 0
end

#logout_pools(db) ⇒ Object



182
183
184
185
186
187
# File 'lib/mongo/repl_set_connection.rb', line 182

def logout_pools(db)
  super
  @secondary_pools.each do |pool|
    pool.logout_existing(db)
  end
end

#read_primary?Boolean Also known as: primary?

Determine whether we’re reading from a primary node. If false, this connection connects to a secondary node and @read_secondaries is true.

Returns:

  • (Boolean)


142
143
144
# File 'lib/mongo/repl_set_connection.rb', line 142

def read_primary?
  !@read_pool
end

#reset_connectionObject

If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values. TODO: what’s the point of this method?



159
160
161
162
163
164
165
166
# File 'lib/mongo/repl_set_connection.rb', line 159

def reset_connection
  super
  @secondaries     = []
  @secondary_pools = []
  @arbiters        = []
  @nodes_tried  = []
  @nodes_to_try = []
end

#slave_ok?Boolean

Is it okay to connect to a slave?

Returns:

  • (Boolean)


171
172
173
# File 'lib/mongo/repl_set_connection.rb', line 171

def slave_ok?
  @read_secondary || @slave_ok
end