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

Instance Attribute Summary collapse

Attributes inherited from Connection

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

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, #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

  • opts (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:



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

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)
  @slave_okay = 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



196
197
198
199
200
201
# File 'lib/mongo/repl_set_connection.rb', line 196

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

#closeObject

Close the connection to the database.



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mongo/repl_set_connection.rb', line 167

def close
  super
  @read_pool = nil
  @secondary_pools.each do |pool|
    pool.close
  end
  @secondaries     = []
  @secondary_pools = []
  @arbiters        = []
  @nodes_tried  = []
  @nodes_to_try = []
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:



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

def connect
  close
  @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)


139
140
141
# File 'lib/mongo/repl_set_connection.rb', line 139

def connecting?
  @nodes_to_try.length > 0
end

#hostString

The replica set primary’s host name.

Returns:



146
147
148
# File 'lib/mongo/repl_set_connection.rb', line 146

def host
  super
end

#logout_pools(db) ⇒ Object



203
204
205
206
207
208
# File 'lib/mongo/repl_set_connection.rb', line 203

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

#portInteger

The replica set primary’s port.

Returns:

  • (Integer)


153
154
155
# File 'lib/mongo/repl_set_connection.rb', line 153

def port
  super
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)


161
162
163
# File 'lib/mongo/repl_set_connection.rb', line 161

def read_primary?
  !@read_pool
end

#reset_connectionObject

Deprecated.

If a ConnectionFailure is raised, this method will be called to close the connection and reset connection values.



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

def reset_connection
  close
  warn "ReplSetConnection#reset_connection is now deprecated. " +
    "Use ReplSetConnection#close instead."
end

#slave_ok?Boolean

Is it okay to connect to a slave?

Returns:

  • (Boolean)


192
193
194
# File 'lib/mongo/repl_set_connection.rb', line 192

def slave_ok?
  @read_secondary || @slave_ok
end