Class: Mongo::Server
- Inherits:
-
Object
- Object
- Mongo::Server
- Extended by:
- Forwardable
- Includes:
- Event::Publisher, Monitoring::Publishable
- Defined in:
- lib/mongo/server.rb,
lib/mongo/server/monitor.rb,
lib/mongo/server/connection.rb,
lib/mongo/server/description.rb,
lib/mongo/server/app_metadata.rb,
lib/mongo/server/push_monitor.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/app_metadata/platform.rb,
lib/mongo/server/app_metadata/truncator.rb,
lib/mongo/server/push_monitor/connection.rb,
lib/mongo/server/app_metadata/environment.rb,
lib/mongo/server/connection_pool/populator.rb,
lib/mongo/server/description/load_balancer.rb,
lib/mongo/server/round_trip_time_calculator.rb,
lib/mongo/server/connection_pool/generation_manager.rb
Overview
Represents a single server on the server side that can be standalone, part of a replica set, or a mongos.
Defined Under Namespace
Classes: AppMetadata, Connection, ConnectionBase, ConnectionCommon, ConnectionPool, Description, Monitor, PendingConnection, PushMonitor, RoundTripTimeCalculator
Constant Summary collapse
- CONNECT_TIMEOUT =
The default time in seconds to timeout a connection attempt.
10
Constants included from Loggable
Instance Attribute Summary collapse
-
#address ⇒ String
readonly
The configured address for the server.
-
#cluster ⇒ Cluster
readonly
Cluster The server cluster.
-
#description ⇒ Server::Description
readonly
Description The server description the monitor refreshes.
-
#monitor ⇒ nil | Monitor
readonly
Monitor The server monitor.
-
#monitoring ⇒ Monitoring
readonly
Monitoring The monitoring.
-
#options ⇒ Hash
readonly
The options hash.
-
#round_trip_time_calculator ⇒ RoundTripTimeCalculator
readonly
private
Round trip time calculator object.
-
#scan_semaphore ⇒ Semaphore
readonly
private
Semaphore to signal to request an immediate scan of this server by its monitor, if one is running.
Attributes included from Event::Publisher
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Is this server equal to another?.
- #clear_connection_pool(service_id: nil, interrupt_in_use_connections: false) ⇒ Object private
-
#clear_description ⇒ Object
private
Clear the servers description so that it is considered unknown and can be safely disconnected.
- #close ⇒ Object
- #compressor ⇒ String | nil deprecated Deprecated.
-
#connectable? ⇒ true, false
deprecated
Deprecated.
No longer necessary with Server Selection specification.
-
#connected? ⇒ true|false
private
Whether the server is connected.
-
#disconnect! ⇒ true
Disconnect the driver from this server.
-
#force_load_balancer? ⇒ true | false
private
Returns whether this server is forced to be a load balancer.
-
#handle_auth_failure! ⇒ Object
Handle authentication failure.
-
#handle_handshake_failure! ⇒ Object
private
Handle handshake failure.
- #heartbeat_frequency ⇒ Object (also: #heartbeat_frequency_seconds) deprecated Deprecated.
-
#initialize(address, cluster, monitoring, event_listeners, options = {}) ⇒ Server
constructor
private
Instantiate a new server object.
-
#inspect ⇒ String
Get a pretty printed server inspection.
-
#last_scan ⇒ Time | nil
Last_scan The time when the last server scan completed, or nil if the server has not been scanned yet.
-
#last_scan_monotime ⇒ Float | nil
private
Last_scan_monotime The monotonic time when the last server scan completed, or nil if the server has not been scanned yet.
-
#matches_tag_set?(tag_set) ⇒ true, false
Determine if the provided tags are a subset of the server’s tags.
- #next_connection_id ⇒ Object private
-
#pool ⇒ Mongo::Server::ConnectionPool
Get the connection pool for this server.
-
#pool_internal ⇒ Server::ConnectionPool | nil
private
Internal driver method to retrieve the connection pool for this server.
-
#publish_opening_event ⇒ Object
private
Publishes the server opening event.
-
#reconnect! ⇒ true
Restart the server monitor.
-
#retry_reads? ⇒ Boolean
private
Whether the server supports modern read retries.
-
#retry_writes? ⇒ true, false
Will writes sent to this server be retried.
-
#start_monitoring ⇒ Object
private
Start monitoring the server.
-
#status ⇒ String
private
String representing server status (e.g. PRIMARY).
- #summary ⇒ Object
-
#unknown!(options = {}) ⇒ Object
Marks server unknown and publishes the associated SDAM event (server description changed).
- #update_description(description) ⇒ Object private
- #update_last_scan ⇒ Object private
-
#with_connection(connection_global_id: nil, context: nil, &block) ⇒ Object
Execute a block of code with a connection, that is checked out of the server’s pool and then checked back in.
Methods included from Event::Publisher
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.
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.
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 |
# File 'lib/mongo/server.rb', line 69 def initialize(address, cluster, monitoring, event_listeners, = {}) @address = address @cluster = cluster @monitoring = monitoring = .dup _monitor = .delete(:monitor) @options = .freeze @event_listeners = event_listeners @connection_id_gen = Class.new do include Id end @scan_semaphore = DistinguishingSemaphore.new @round_trip_time_calculator = RoundTripTimeCalculator.new @description = Description.new(address, {}, load_balancer: !!@options[:load_balancer], force_load_balancer: force_load_balancer?) @last_scan = nil @last_scan_monotime = nil unless [:monitoring_io] == false @monitor = Monitor.new(self, event_listeners, monitoring, .merge( app_metadata: cluster., push_monitor_app_metadata: cluster., heartbeat_interval: cluster.heartbeat_interval )) start_monitoring unless _monitor == false end @connected = true @pool_lock = Mutex.new end |
Instance Attribute Details
#address ⇒ String (readonly)
Returns The configured address for the server.
101 102 103 |
# File 'lib/mongo/server.rb', line 101 def address @address end |
#cluster ⇒ Cluster (readonly)
Returns cluster The server cluster.
104 105 106 |
# File 'lib/mongo/server.rb', line 104 def cluster @cluster end |
#description ⇒ Server::Description (readonly)
Returns description The server description the monitor refreshes.
118 119 120 |
# File 'lib/mongo/server.rb', line 118 def description @description end |
#monitor ⇒ nil | Monitor (readonly)
Returns monitor The server monitor. nil if the server was created with monitoring_io: false option.
108 109 110 |
# File 'lib/mongo/server.rb', line 108 def monitor @monitor end |
#monitoring ⇒ Monitoring (readonly)
Returns monitoring The monitoring.
114 115 116 |
# File 'lib/mongo/server.rb', line 114 def monitoring @monitoring end |
#options ⇒ Hash (readonly)
Returns The options hash.
111 112 113 |
# File 'lib/mongo/server.rb', line 111 def @options end |
#round_trip_time_calculator ⇒ RoundTripTimeCalculator (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 calculator object.
227 228 229 |
# File 'lib/mongo/server.rb', line 227 def round_trip_time_calculator @round_trip_time_calculator end |
#scan_semaphore ⇒ Semaphore (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.
223 224 225 |
# File 'lib/mongo/server.rb', line 223 def scan_semaphore @scan_semaphore end |
Instance Method Details
#==(other) ⇒ true, false
Is this server equal to another?
239 240 241 242 243 |
# File 'lib/mongo/server.rb', line 239 def ==(other) return false unless other.is_a?(Server) address == other.address end |
#clear_connection_pool(service_id: nil, interrupt_in_use_connections: false) ⇒ 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.
642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/mongo/server.rb', line 642 def clear_connection_pool(service_id: nil, interrupt_in_use_connections: false) @pool_lock.synchronize do # A server being marked unknown after it is closed is technically # incorrect but it does not meaningfully alter any state. # Because historically the driver permitted servers to be marked # unknown at any time, continue doing so even if the pool is closed. if @pool && !@pool.closed? @pool.disconnect!(service_id: service_id, interrupt_in_use_connections: interrupt_in_use_connections) end end end |
#clear_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.
Clear the servers description so that it is considered unknown and can be safely disconnected.
632 633 634 |
# File 'lib/mongo/server.rb', line 632 def clear_description @description = Mongo::Server::Description.new(address, {}) end |
#close ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/mongo/server.rb', line 287 def close monitor.stop! if monitor @connected = false _pool = nil @pool_lock.synchronize do _pool, @pool = @pool, nil end # TODO: change this to _pool.close in RUBY-3174. # Clear the pool. If the server is not unknown then the # pool will stay ready. Stop the background populator thread. _pool&.close(stay_ready: true) nil end |
#compressor ⇒ String | nil
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 yet completed, and if no compression was negotiated.
175 176 177 178 179 |
# File 'lib/mongo/server.rb', line 175 def compressor return unless monitor monitor.compressor end |
#connectable? ⇒ true, false
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.
256 |
# File 'lib/mongo/server.rb', line 256 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.
311 312 313 |
# File 'lib/mongo/server.rb', line 311 def connected? @connected 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/mongo/server.rb', line 270 def disconnect! monitor.stop! if monitor @connected = false # The current CMAP spec requires a pool to be mostly unusable # if its server is unknown (or, therefore, disconnected). # However any outstanding operations should continue to completion, # and their connections need to be checked into the pool to be # torn down. Because of this cleanup requirement we cannot just # close the pool and set it to nil here, to be recreated the next # time the server is discovered. pool_internal&.clear true end |
#force_load_balancer? ⇒ 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.
Returns whether this server is forced to be a load balancer.
125 126 127 |
# File 'lib/mongo/server.rb', line 125 def force_load_balancer? [:connect] == :load_balanced end |
#handle_auth_failure! ⇒ Object
Handle authentication failure.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/mongo/server.rb', line 510 def handle_auth_failure! yield rescue Mongo::Error::SocketTimeoutError # possibly cluster is slow, do not give up on it raise rescue Mongo::Error::SocketError, Auth::Unauthorized => e # non-timeout network error or auth error, clear the pool and mark the # topology as unknown unknown!( generation: e.generation, service_id: e.service_id, stop_push_monitor: true ) 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.
485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/mongo/server.rb', line 485 def handle_handshake_failure! yield rescue Mongo::Error::SocketError, Mongo::Error::SocketTimeoutError => e unless e.label?('SystemOverloadedError') unknown!( generation: e.generation, service_id: e.service_id, stop_push_monitor: true ) end raise end |
#heartbeat_frequency ⇒ Object Also known as: heartbeat_frequency_seconds
153 154 155 |
# File 'lib/mongo/server.rb', line 153 def heartbeat_frequency cluster.heartbeat_interval end |
#inspect ⇒ String
Get a pretty printed server inspection.
346 347 348 |
# File 'lib/mongo/server.rb', line 346 def inspect "#<Mongo::Server:0x#{object_id} address=#{address.host}:#{address.port} #{status}>" end |
#last_scan ⇒ Time | nil
Returns last_scan The time when the last server scan completed, or nil if the server has not been scanned yet.
133 134 135 136 137 138 139 |
# File 'lib/mongo/server.rb', line 133 def last_scan if description && !description.config.empty? description.last_update_time else @last_scan end end |
#last_scan_monotime ⇒ Float | nil
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 last_scan_monotime The monotonic time when the last server scan completed, or nil if the server has not been scanned yet.
144 145 146 147 148 149 150 |
# File 'lib/mongo/server.rb', line 144 def last_scan_monotime if description && !description.config.empty? description.last_update_monotime else @last_scan_monotime end end |
#matches_tag_set?(tag_set) ⇒ true, false
Determine if the provided tags are a subset of the server’s tags.
443 444 445 446 447 |
# File 'lib/mongo/server.rb', line 443 def matches_tag_set?(tag_set) tag_set.keys.all? do |k| [k] && [k] == tag_set[k] end end |
#next_connection_id ⇒ 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.
655 656 657 |
# File 'lib/mongo/server.rb', line 655 def next_connection_id @connection_id_gen.next_id end |
#pool ⇒ Mongo::Server::ConnectionPool
Get the connection pool for this server.
408 409 410 411 412 413 414 415 416 417 |
# File 'lib/mongo/server.rb', line 408 def pool raise Error::ServerNotUsable, address if unknown? @pool_lock.synchronize do opts = connected? ? : .merge(populator_io: false) @pool ||= ConnectionPool.new(self, opts).tap do |pool| pool.ready end end end |
#pool_internal ⇒ Server::ConnectionPool | nil
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.
Internal driver method to retrieve the connection pool for this server.
Unlike pool, pool_internal will not create a pool if one does not already exist.
427 428 429 430 431 |
# File 'lib/mongo/server.rb', line 427 def pool_internal @pool_lock.synchronize do @pool end end |
#publish_opening_event ⇒ 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.
Publishes the server opening event.
331 332 333 334 335 336 |
# File 'lib/mongo/server.rb', line 331 def publish_opening_event publish_sdam_event( Monitoring::SERVER_OPENING, Monitoring::Event::ServerOpening.new(address, cluster.topology) ) end |
#reconnect! ⇒ true
Restart the server monitor.
457 458 459 460 |
# File 'lib/mongo/server.rb', line 457 def reconnect! monitor.restart! if [:monitoring_io] != false @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.
529 530 531 |
# File 'lib/mongo/server.rb', line 529 def retry_reads? !!logical_session_timeout end |
#retry_writes? ⇒ true, false
Retryable writes are only available with sharded clusters or replica sets.
Some of the conditions in this method automatically return false for load balanced topologies. The conditions in this method should always be true, since load-balanced topologies are only available on MongoDB 5.0+, and not for standalone topologies. Therefore, we can assume that retry writes are enabled.
Will writes sent to this server be retried.
549 550 551 |
# File 'lib/mongo/server.rb', line 549 def retry_writes? !!((logical_session_timeout && !standalone?) || load_balancer?) end |
#start_monitoring ⇒ 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.
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.
321 322 323 324 325 326 |
# File 'lib/mongo/server.rb', line 321 def start_monitoring publish_opening_event return unless [:monitoring_io] != false monitor.run! end |
#status ⇒ String
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).
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/mongo/server.rb', line 353 def status if load_balancer? 'LB' elsif primary? 'PRIMARY' elsif secondary? 'SECONDARY' elsif standalone? 'STANDALONE' elsif arbiter? 'ARBITER' elsif ghost? 'GHOST' elsif other? 'OTHER' elsif mongos? 'MONGOS' elsif 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 |
#summary ⇒ Object
This method is experimental and subject to change.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/mongo/server.rb', line 383 def summary status = self.status || '' status += " replica_set=#{replica_set_name}" if replica_set_name status += ' NO-MONITORING' unless monitor&.running? status += " pool=#{@pool.summary}" if @pool 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).
If the generation is passed in options, the server will only be marked unknown if the passed generation is no older than the current generation of the server’s connection pool.
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/mongo/server.rb', line 577 def unknown!( = {}) pool = pool_internal if load_balancer? # When the client is in load-balanced topology, servers (the one and # only that can be) starts out as a load balancer and stays as a # load balancer indefinitely. As such it is not marked unknown. # # However, this method also clears connection pool for the server # when the latter is marked unknown, and this part needs to happen # when the server is a load balancer. # # It is possible for a load balancer server to not have a service id, # for example if there haven't been any successful connections yet to # this server, but the server can still be marked unknown if one # of such connections failed midway through its establishment. if service_id = [:service_id] pool&.disconnect!(service_id: service_id) end return end # NOTE: You cannot use safe navigation here because if pool is nil you end # up trying to evaluate Integer < nil which is invalid. return if [:generation] && pool && [:generation] < pool.generation if [:topology_version] && description.topology_version && ![:topology_version].gt?(description.topology_version) return end monitor&.stop_push_monitor! if [:stop_push_monitor] # SDAM flow will update description on the server without in-place # mutations and invoke SDAM transitions as needed. config = {} config['serviceId'] = [:service_id] if [:service_id] config['topologyVersion'] = [:topology_version] if [:topology_version] new_description = Description.new(address, config, load_balancer: load_balancer?, force_load_balancer: [:connect] == :load_balanced) cluster.run_sdam_flow(description, new_description, ) 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.
622 623 624 625 626 |
# File 'lib/mongo/server.rb', line 622 def update_description(description) pool = pool_internal pool.ready if pool && !description.unknown? @description = description end |
#update_last_scan ⇒ 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.
660 661 662 663 |
# File 'lib/mongo/server.rb', line 660 def update_last_scan @last_scan = Time.now @last_scan_monotime = Utils.monotonic_time end |
#with_connection(connection_global_id: nil, context: nil, &block) ⇒ Object
Execute a block of code with a connection, that is checked out of the server’s pool and then checked back in.
473 474 475 476 477 478 479 |
# File 'lib/mongo/server.rb', line 473 def with_connection(connection_global_id: nil, context: nil, &block) pool.with_connection( connection_global_id: connection_global_id, context: context, &block ) end |