Class: Mongo::Server

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Event::Publisher, Monitoring::Publishable
Defined in:
lib/mongo/server.rb,
lib/mongo/server/context.rb,
lib/mongo/server/monitor.rb,
lib/mongo/server/connection.rb,
lib/mongo/server/connectable.rb,
lib/mongo/server/description.rb,
lib/mongo/server/app_metadata.rb,
lib/mongo/server/connection_base.rb,
lib/mongo/server/connection_pool.rb,
lib/mongo/server/connection_common.rb,
lib/mongo/server/monitor/connection.rb,
lib/mongo/server/pending_connection.rb,
lib/mongo/server/description/features.rb,
lib/mongo/server/monitor/app_metadata.rb,
lib/mongo/server/round_trip_time_averager.rb,
lib/mongo/server/connection_pool/populator.rb

Overview

Represents a single server on the server side that can be standalone, part of a replica set, or a mongos.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Connectable Classes: AppMetadata, Connection, ConnectionBase, ConnectionCommon, ConnectionPool, Context, Description, Monitor, PendingConnection, Populator, RoundTripTimeAverager

Constant Summary collapse

CONNECT_TIMEOUT =

The default time in seconds to timeout a connection attempt.

Since:

  • 2.4.3

10.freeze

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Attributes included from Event::Publisher

#event_listeners

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Event::Publisher

#publish

Methods included from Monitoring::Publishable

#publish_cmap_event, #publish_event, #publish_sdam_event

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(address, cluster, monitoring, event_listeners, options = {}) ⇒ Server

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Server must never be directly instantiated outside of a Cluster.

Instantiate a new server object. Will start the background refresh and subscribe to the appropriate events.

Examples:

Initialize the server.

Mongo::Server.new('127.0.0.1:27017', cluster, monitoring, listeners)

Parameters:

  • address (Address)

    The host:port address to connect to.

  • cluster (Cluster)

    The cluster the server belongs to.

  • monitoring (Monitoring)

    The monitoring.

  • event_listeners (Event::Listeners)

    The event listeners.

  • options (Hash) (defaults to: {})

    The server options.

Options Hash (options):

  • :monitor (Boolean)

    For internal driver use only: whether to monitor the server after instantiating it.

  • :monitoring_io (true, false)

    For internal driver use only. Set to false to prevent SDAM-related I/O from being done by this server. Note: setting this option to false will make the server non-functional. It is intended for use in tests which manually invoke SDAM state transitions.

Since:

  • 2.0.0



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

def initialize(address, cluster, monitoring, event_listeners, options = {})
  @address = address
  @cluster = cluster
  @monitoring = monitoring
  options = options.dup
  _monitor = options.delete(:monitor)
  @options = options.freeze
  @event_listeners = event_listeners
  @connection_id_gen = Class.new do
    include Id
  end
  @scan_semaphore = Semaphore.new
  @round_trip_time_averager = RoundTripTimeAverager.new
  @description = Description.new(address, {})
  @last_scan = nil
  unless options[:monitoring_io] == false
    @monitor = Monitor.new(self, event_listeners, monitoring,
      options.merge(
        app_metadata: Monitor::AppMetadata.new(cluster.options),
    ))
    unless _monitor == false
      start_monitoring
    end
  end
  @connected = true
  @pool_lock = Mutex.new
end

Instance Attribute Details

#addressString (readonly)

Returns The configured address for the server.

Returns:

  • (String)

    The configured address for the server.

Since:

  • 2.0.0



85
86
87
# File 'lib/mongo/server.rb', line 85

def address
  @address
end

#clusterCluster (readonly)

Returns cluster The server cluster.

Returns:

  • (Cluster)

    cluster The server cluster.

Since:

  • 2.0.0



88
89
90
# File 'lib/mongo/server.rb', line 88

def cluster
  @cluster
end

#descriptionServer::Description (readonly)

Returns description The server description the monitor refreshes.

Returns:

Since:

  • 2.0.0



102
103
104
# File 'lib/mongo/server.rb', line 102

def description
  @description
end

#monitornil | Monitor (readonly)

Returns monitor The server monitor. nil if the servenr was created with monitoring_io: false option.

Returns:

  • (nil | Monitor)

    monitor The server monitor. nil if the servenr was created with monitoring_io: false option.

Since:

  • 2.0.0



92
93
94
# File 'lib/mongo/server.rb', line 92

def monitor
  @monitor
end

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.0.0



98
99
100
# File 'lib/mongo/server.rb', line 98

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns The options hash.

Returns:

  • (Hash)

    The options hash.

Since:

  • 2.0.0



95
96
97
# File 'lib/mongo/server.rb', line 95

def options
  @options
end

#round_trip_time_averagerRoundTripTimeAverager (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Round trip time averager object.

Returns:

Since:

  • 2.0.0



183
184
185
# File 'lib/mongo/server.rb', line 183

def round_trip_time_averager
  @round_trip_time_averager
end

#scan_semaphoreSemaphore (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Semaphore to signal to request an immediate scan of this server by its monitor, if one is running.

Returns:

  • (Semaphore)

    Semaphore to signal to request an immediate scan of this server by its monitor, if one is running.

Since:

  • 2.0.0



179
180
181
# File 'lib/mongo/server.rb', line 179

def scan_semaphore
  @scan_semaphore
end

Class Method Details

.finalize(monitor) ⇒ Object

When the server is flagged for garbage collection, stop the monitor thread.

Examples:

Finalize the object.

Server.finalize(monitor)

Parameters:

Since:

  • 2.2.0



281
282
283
# File 'lib/mongo/server.rb', line 281

def self.finalize(monitor)
  proc { monitor.stop! }
end

Instance Method Details

#==(other) ⇒ true, false

Is this server equal to another?

Examples:

Is the server equal to the other?

server == other

Parameters:

  • other (Object)

    The object to compare to.

Returns:

  • (true, false)

    If the servers are equal.

Since:

  • 2.0.0



195
196
197
198
# File 'lib/mongo/server.rb', line 195

def ==(other)
  return false unless other.is_a?(Server)
  address == other.address
end

#clear_connection_poolObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



509
510
511
512
513
514
515
# File 'lib/mongo/server.rb', line 509

def clear_connection_pool
  @pool_lock.synchronize do
    if @pool
      @pool.disconnect!
    end
  end
end

#compressorString | nil

Deprecated.
Note:

Compression is negotiated for each connection separately.

The compressor negotiated by the server monitor, if any.

This attribute is nil if no server check has not yet completed, and if no compression was negatiated.

Returns:

  • (String | nil)

    The negotiated compressor.

Since:

  • 2.0.0



137
138
139
140
141
142
143
# File 'lib/mongo/server.rb', line 137

def compressor
  if monitor
    monitor.compressor
  else
    nil
  end
end

#connectable?true, false

Deprecated.

No longer necessary with Server Selection specification.

Determine if a connection to the server is able to be established and messages can be sent to it.

Examples:

Is the server connectable?

server.connectable?

Returns:

  • (true, false)

    If the server is connectable.

Since:

  • 2.1.0



225
# File 'lib/mongo/server.rb', line 225

def connectable?; end

#connected?true|false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the server is connected.

Returns:

  • (true|false)

    Whether the server is connected.

Since:

  • 2.7.0



268
269
270
# File 'lib/mongo/server.rb', line 268

def connected?
  @connected
end

#contextMongo::Server::Context

Deprecated.

Will be removed in version 3.0

Get a new context for this server in which to send messages.

Examples:

Get the server context.

server.context

Returns:

Since:

  • 2.0.0



210
211
212
# File 'lib/mongo/server.rb', line 210

def context
  Context.new(self)
end

#disconnect!true

Disconnect the driver from this server.

Disconnects all idle connections to this server in its connection pool, if any exist. Stops the populator of the connection pool, if it is running. Does not immediately close connections which are presently checked out (i.e. in use) - such connections will be closed when they are returned to their respective connection pools. Stop the server's background monitor.

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/mongo/server.rb', line 239

def disconnect!
  if monitor
    monitor.stop!
  end
  _pool = @pool_lock.synchronize do
    @pool
  end
  if _pool
    # For backwards compatibility we disconnect/clear the pool rather
    # than close it here. We also stop the populator which allows the
    # the pool to continue providing connections but stops it from
    # connecting in background on clients/servers that are in fact
    # intended to be closed and no longer used.
    begin
      _pool.disconnect!(stop_populator: true)
    rescue Error::PoolClosedError
      # If the pool was already closed, we don't need to do anything here.
    end
  end
  @connected = false
  true
end

#handle_auth_failure!Object

Handle authentication failure.

Examples:

Handle possible authentication failure.

server.handle_auth_failure! do
  Auth.get(user).(self)
end

Returns:

  • (Object)

    The result of the block execution.

Raises:

Since:

  • 2.3.0



448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/mongo/server.rb', line 448

def handle_auth_failure!
  yield
rescue Mongo::Error::SocketTimeoutError
  # possibly cluster is slow, do not give up on it
  raise
rescue Mongo::Error::SocketError
  # non-timeout network error
  unknown!
  raise
rescue Auth::Unauthorized
  # auth error, keep server description and topology as they are
  pool.disconnect!
  raise
end

#handle_handshake_failure!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle handshake failure.

Since:

  • 2.7.0



429
430
431
432
433
434
# File 'lib/mongo/server.rb', line 429

def handle_handshake_failure!
  yield
rescue Mongo::Error::SocketError, Mongo::Error::SocketTimeoutError
  unknown!
  raise
end

#heartbeat_frequencyObject Also known as: heartbeat_frequency_seconds

Deprecated.

Since:

  • 2.0.0



117
118
119
# File 'lib/mongo/server.rb', line 117

def heartbeat_frequency
  cluster.heartbeat_interval
end

#inspectString

Get a pretty printed server inspection.

Examples:

Get the server inspection.

server.inspect

Returns:

  • (String)

    The nice inspection string.

Since:

  • 2.0.0



310
311
312
# File 'lib/mongo/server.rb', line 310

def inspect
  "#<Mongo::Server:0x#{object_id} address=#{address.host}:#{address.port}>"
end

#last_scanTime | nil

Returns last_scan The time when the last server scan completed, or nil if the server has not been scanned yet.

Returns:

  • (Time | nil)

    last_scan The time when the last server scan completed, or nil if the server has not been scanned yet.

Since:

  • 2.4.0



108
109
110
111
112
113
114
# File 'lib/mongo/server.rb', line 108

def last_scan
  if description && !description.config.empty?
    description.last_update_time
  else
    @last_scan
  end
end

#matches_tag_set?(tag_set) ⇒ true, false

Determine if the provided tags are a subset of the server's tags.

Examples:

Are the provided tags a subset of the server's tags.

server.matches_tag_set?({ 'rack' => 'a', 'dc' => 'nyc' })

Parameters:

  • tag_set (Hash)

    The tag set to compare to the server's tags.

Returns:

  • (true, false)

    If the provided tags are a subset of the server's tags.

Since:

  • 2.0.0



389
390
391
392
393
# File 'lib/mongo/server.rb', line 389

def matches_tag_set?(tag_set)
  tag_set.keys.all? do |k|
    tags[k] && tags[k] == tag_set[k]
  end
end

#next_connection_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



518
519
520
# File 'lib/mongo/server.rb', line 518

def next_connection_id
  @connection_id_gen.next_id
end

#poolMongo::Server::ConnectionPool

Get the connection pool for this server.

Examples:

Get the connection pool for the server.

server.pool

Returns:

Since:

  • 2.0.0



373
374
375
376
377
# File 'lib/mongo/server.rb', line 373

def pool
  @pool_lock.synchronize do
    @pool ||= ConnectionPool.new(self, options)
  end
end

#reconnect!true

Restart the server monitor.

Examples:

Restart the server monitor.

server.reconnect!

Returns:

  • (true)

    Always true.

Since:

  • 2.1.0



403
404
405
406
407
408
# File 'lib/mongo/server.rb', line 403

def reconnect!
  if options[:monitoring_io] != false
    monitor.restart!
  end
  @connected = true
end

#retry_reads?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the server supports modern read retries.

Returns:

  • (Boolean)

Since:

  • 2.0.0



466
467
468
# File 'lib/mongo/server.rb', line 466

def retry_reads?
  !!(features.sessions_enabled? && logical_session_timeout)
end

#retry_writes?true, false

Note:

Retryable writes are only available on server versions 3.6+ and with sharded clusters or replica sets.

Will writes sent to this server be retried.

Examples:

Will writes be retried.

server.retry_writes?

Returns:

  • (true, false)

    If writes will be retried.

Since:

  • 2.5.0



481
482
483
# File 'lib/mongo/server.rb', line 481

def retry_writes?
  !!(features.sessions_enabled? && logical_session_timeout && !standalone?)
end

#start_monitoringObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Start monitoring the server.

Used internally by the driver to add a server to a cluster while delaying monitoring until the server is in the cluster.

Since:

  • 2.0.0



291
292
293
294
295
296
297
298
299
300
# File 'lib/mongo/server.rb', line 291

def start_monitoring
  publish_sdam_event(
    Monitoring::SERVER_OPENING,
    Monitoring::Event::ServerOpening.new(address, cluster.topology)
  )
  if options[:monitoring_io] != false
    ObjectSpace.define_finalizer(self, self.class.finalize(monitor))
    monitor.run!
  end
end

#statusString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns String representing server status (e.g. PRIMARY).

Returns:

  • (String)

    String representing server status (e.g. PRIMARY).

Since:

  • 2.0.0



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/mongo/server.rb', line 317

def status
  case
  when primary?
    'PRIMARY'
  when secondary?
    'SECONDARY'
  when standalone?
    'STANDALONE'
  when arbiter?
    'ARBITER'
  when ghost?
    'GHOST'
  when other?
    'OTHER'
  when mongos?
    'MONGOS'
  when unknown?
    'UNKNOWN'
  else
    # Since the summary method is often used for debugging, do not raise
    # an exception in case none of the expected types matched
    nil
  end
end

#summaryObject

Note:

This method is experimental and subject to change.

Since:

  • 2.7.0



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/mongo/server.rb', line 346

def summary
  status = self.status || ''
  if replica_set_name
    status += " replica_set=#{replica_set_name}"
  end

  if @pool
    status += " pool=#{@pool.summary}"
  end

  address_bit = if address
    "#{address.host}:#{address.port}"
  else
    'nil'
  end

  "#<Server address=#{address_bit} #{status}>"
end

#unknown!(options = {}) ⇒ Object

Marks server unknown and publishes the associated SDAM event (server description changed).

Parameters:

  • options (Hash) (defaults to: {})

    Options.

Options Hash (options):

  • :keep_connection_pool (true | false)

    Usually when the new server description is unknown, the connection pool on the respective server is cleared. Set this option to true to keep the existing connection pool (required when handling not master errors on 4.2+ servers).

Since:

  • 2.4.0, SDAM events are sent as of version 2.7.0



497
498
499
500
501
# File 'lib/mongo/server.rb', line 497

def unknown!(options = {})
  # SDAM flow will update description on the server without in-place
  # mutations and invoke SDAM transitions as needed.
  cluster.run_sdam_flow(description, Description.new(address), options)
end

#update_description(description) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



504
505
506
# File 'lib/mongo/server.rb', line 504

def update_description(description)
  @description = description
end

#update_last_scanObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



523
524
525
# File 'lib/mongo/server.rb', line 523

def update_last_scan
  @last_scan = Time.now
end

#with_connection(&block) ⇒ Object

Execute a block of code with a connection, that is checked out of the server's pool and then checked back in.

Examples:

Send a message with the connection.

server.with_connection do |connection|
  connection.dispatch([ command ])
end

Returns:

  • (Object)

    The result of the block execution.

Since:

  • 2.3.0



421
422
423
# File 'lib/mongo/server.rb', line 421

def with_connection(&block)
  pool.with_connection(&block)
end