Class: Mongo::Server
- Inherits:
-
Object
- Object
- Mongo::Server
- Extended by:
- Forwardable
- Includes:
- Event::Publisher, Monitoring::Publishable
- Defined in:
- build/ruby-driver-master/lib/mongo/server.rb,
build/ruby-driver-master/lib/mongo/server/monitor.rb,
build/ruby-driver-master/lib/mongo/server/connection.rb,
build/ruby-driver-master/lib/mongo/server/description.rb,
build/ruby-driver-master/lib/mongo/server/app_metadata.rb,
build/ruby-driver-master/lib/mongo/server/push_monitor.rb,
build/ruby-driver-master/lib/mongo/server/connection_base.rb,
build/ruby-driver-master/lib/mongo/server/connection_pool.rb,
build/ruby-driver-master/lib/mongo/server/connection_common.rb,
build/ruby-driver-master/lib/mongo/server/monitor/connection.rb,
build/ruby-driver-master/lib/mongo/server/pending_connection.rb,
build/ruby-driver-master/lib/mongo/server/description/features.rb,
build/ruby-driver-master/lib/mongo/server/monitor/app_metadata.rb,
build/ruby-driver-master/lib/mongo/server/push_monitor/connection.rb,
build/ruby-driver-master/lib/mongo/server/round_trip_time_averager.rb,
build/ruby-driver-master/lib/mongo/server/connection_pool/populator.rb,
build/ruby-driver-master/lib/mongo/server/description/load_balancer.rb,
build/ruby-driver-master/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, Populator, PushMonitor, RoundTripTimeAverager
Constant Summary collapse
- CONNECT_TIMEOUT =
The default time in seconds to timeout a connection attempt.
10.freeze
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_averager ⇒ RoundTripTimeAverager
readonly
private
Round trip time averager 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) ⇒ Object private
- #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.
-
#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, &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.
62 63 64 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 'build/ruby-driver-master/lib/mongo/server.rb', line 62 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_averager = RoundTripTimeAverager.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, )) unless _monitor == false start_monitoring end end @connected = true @pool_lock = Mutex.new end |
Instance Attribute Details
#address ⇒ String (readonly)
Returns The configured address for the server.
97 98 99 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 97 def address @address end |
#cluster ⇒ Cluster (readonly)
Returns cluster The server cluster.
100 101 102 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 100 def cluster @cluster end |
#description ⇒ Server::Description (readonly)
Returns description The server description the monitor refreshes.
114 115 116 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 114 def description @description end |
#monitor ⇒ nil | Monitor (readonly)
Returns monitor The server monitor. nil if the servenr was created with monitoring_io: false option.
104 105 106 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 104 def monitor @monitor end |
#monitoring ⇒ Monitoring (readonly)
Returns monitoring The monitoring.
110 111 112 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 110 def monitoring @monitoring end |
#options ⇒ Hash (readonly)
Returns The options hash.
107 108 109 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 107 def @options end |
#round_trip_time_averager ⇒ RoundTripTimeAverager (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.
224 225 226 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 224 def round_trip_time_averager @round_trip_time_averager 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.
220 221 222 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 220 def scan_semaphore @scan_semaphore end |
Instance Method Details
#==(other) ⇒ true, false
Is this server equal to another?
236 237 238 239 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 236 def ==(other) return false unless other.is_a?(Server) address == other.address end |
#clear_connection_pool(service_id: nil) ⇒ 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.
599 600 601 602 603 604 605 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 599 def clear_connection_pool(service_id: nil) @pool_lock.synchronize do if @pool @pool.disconnect!(service_id: service_id) end end 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 not yet completed, and if no compression was negatiated.
172 173 174 175 176 177 178 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 172 def compressor if monitor monitor.compressor else nil end 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.
252 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 252 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.
295 296 297 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 295 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.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 266 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 |
#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.
121 122 123 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 121 def force_load_balancer? [:connect] == :load_balanced end |
#handle_auth_failure! ⇒ Object
Handle authentication failure.
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 478 def handle_auth_failure! yield rescue Mongo::Error::SocketTimeoutError # possibly cluster is slow, do not give up on it raise rescue Mongo::Error::SocketError => e # non-timeout network error unknown!( generation: e.generation, service_id: e.service_id, stop_push_monitor: true, ) 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.
455 456 457 458 459 460 461 462 463 464 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 455 def handle_handshake_failure! yield rescue Mongo::Error::SocketError, Mongo::Error::SocketTimeoutError => e unknown!( generation: e.generation, service_id: e.service_id, stop_push_monitor: true, ) raise end |
#heartbeat_frequency ⇒ Object Also known as: heartbeat_frequency_seconds
150 151 152 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 150 def heartbeat_frequency cluster.heartbeat_interval end |
#inspect ⇒ String
Get a pretty printed server inspection.
330 331 332 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 330 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.
129 130 131 132 133 134 135 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 129 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.
140 141 142 143 144 145 146 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 140 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.
415 416 417 418 419 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 415 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.
608 609 610 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 608 def next_connection_id @connection_id_gen.next_id end |
#pool ⇒ Mongo::Server::ConnectionPool
Get the connection pool for this server.
399 400 401 402 403 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 399 def pool @pool_lock.synchronize do @pool ||= ConnectionPool.new(self, ) 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.
315 316 317 318 319 320 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 315 def publish_opening_event publish_sdam_event( Monitoring::SERVER_OPENING, Monitoring::Event::ServerOpening.new(address, cluster.topology) ) end |
#reconnect! ⇒ true
Restart the server monitor.
429 430 431 432 433 434 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 429 def reconnect! if [: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.
500 501 502 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 500 def retry_reads? !!(features.sessions_enabled? && logical_session_timeout) end |
#retry_writes? ⇒ true, false
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.
515 516 517 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 515 def retry_writes? !!(features.sessions_enabled? && logical_session_timeout && !standalone?) 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.
305 306 307 308 309 310 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 305 def start_monitoring publish_opening_event if [:monitoring_io] != false monitor.run! end 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).
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 337 def status case when load_balancer? 'LB' 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 |
#summary ⇒ Object
This method is experimental and subject to change.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 368 def summary status = self.status || '' if replica_set_name status += " replica_set=#{replica_set_name}" end unless monitor&.running? status += " NO-MONITORING" 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).
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.
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 543 def unknown!( = {}) 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. if service_id = [:service_id] pool.disconnect!(service_id: service_id) elsif Lint.enabled? raise Error::LintError, 'Load balancer was asked to be marked unknown without a service id' end return end if [:generation] && [:generation] < pool.generation return end if [:topology_version] && description.topology_version && ![:topology_version].gt?(description.topology_version) then return end if [:stop_push_monitor] monitor&.stop_push_monitor! end # SDAM flow will update description on the server without in-place # mutations and invoke SDAM transitions as needed. config = {} if [:service_id] config['serviceId'] = [:service_id] end if [:topology_version] config['topologyVersion'] = [:topology_version] end 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.
591 592 593 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 591 def update_description(description) @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.
613 614 615 616 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 613 def update_last_scan @last_scan = Time.now @last_scan_monotime = Utils.monotonic_time end |
#with_connection(connection_global_id: 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.
447 448 449 |
# File 'build/ruby-driver-master/lib/mongo/server.rb', line 447 def with_connection(connection_global_id: nil, &block) pool.with_connection(connection_global_id: connection_global_id, &block) end |