Class: Mongo::Server::Monitor
- Inherits:
-
Object
- Object
- Mongo::Server::Monitor
- Extended by:
- Forwardable
- Includes:
- BackgroundThread, Event::Publisher, Loggable
- Defined in:
- lib/mongo/server/monitor.rb,
lib/mongo/server/monitor/connection.rb,
lib/mongo/server/monitor/app_metadata.rb
Overview
Responsible for periodically polling a server via ismaster commands to keep the server’s status up to date.
Does all work in a background thread so as to not interfere with other operations performed by the driver.
Defined Under Namespace
Classes: AppMetadata, Connection
Constant Summary collapse
- HEARTBEAT_FREQUENCY =
The default time for a server to refresh its status is 10 seconds.
10.freeze
- MIN_SCAN_FREQUENCY =
The minimum time between forced server scans. Is minHeartbeatFrequencyMS in the SDAM spec.
0.5.freeze
- RTT_WEIGHT_FACTOR =
Deprecated.
Will be removed in version 3.0.
The weighting factor (alpha) for calculating the average moving round trip time.
0.2.freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#connection ⇒ Mongo::Server::Monitor::Connection
readonly
Connection The connection to use.
-
#monitoring ⇒ Monitoring
readonly
Monitoring The monitoring.
-
#options ⇒ Hash
readonly
Options The server options.
-
#server ⇒ Server
readonly
private
Server The server that this monitor is monitoring.
Attributes included from Event::Publisher
Instance Method Summary collapse
-
#do_work ⇒ Thread
Runs the server monitor.
- #heartbeat_frequency ⇒ Float deprecated Deprecated.
-
#initialize(server, event_listeners, monitoring, options = {}) ⇒ Monitor
constructor
private
Create the new server monitor.
-
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
-
#scan! ⇒ Description
Perform a check of the server with throttling, and update the server’s description and average round trip time.
-
#stop! ⇒ true | false
Stop the background thread and wait for to terminate for a reasonable amount of time.
Methods included from BackgroundThread
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods included from Event::Publisher
Constructor Details
#initialize(server, event_listeners, monitoring, options = {}) ⇒ Monitor
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.
Monitor must never be directly instantiated outside of a Server.
Create the new server monitor.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mongo/server/monitor.rb', line 68 def initialize(server, event_listeners, monitoring, = {}) unless monitoring.is_a?(Monitoring) raise ArgumentError, "Wrong monitoring type: #{monitoring.inspect}" end @server = server @event_listeners = event_listeners @monitoring = monitoring = .freeze # This is a Mongo::Server::Monitor::Connection @connection = Connection.new(server.address, ) @mutex = Mutex.new @scan_started_at = nil end |
Instance Attribute Details
#connection ⇒ Mongo::Server::Monitor::Connection (readonly)
Returns connection The connection to use.
87 88 89 |
# File 'lib/mongo/server/monitor.rb', line 87 def connection @connection end |
#monitoring ⇒ Monitoring (readonly)
Returns monitoring The monitoring.
102 103 104 |
# File 'lib/mongo/server/monitor.rb', line 102 def monitoring @monitoring end |
#options ⇒ Hash (readonly)
Returns options The server options.
90 91 92 |
# File 'lib/mongo/server/monitor.rb', line 90 def end |
#server ⇒ Server (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 server The server that this monitor is monitoring.
84 85 86 |
# File 'lib/mongo/server/monitor.rb', line 84 def server @server end |
Instance Method Details
#do_work ⇒ Thread
Runs the server monitor. Refreshing happens on a separate thread per server.
124 125 126 127 |
# File 'lib/mongo/server/monitor.rb', line 124 def do_work scan! server.scan_semaphore.wait(server.cluster.heartbeat_interval) end |
#heartbeat_frequency ⇒ Float
Get the refresh interval for the server. This will be defined via an option or will default to 10.
111 112 113 |
# File 'lib/mongo/server/monitor.rb', line 111 def heartbeat_frequency server.cluster.heartbeat_interval end |
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
182 183 184 185 186 187 188 |
# File 'lib/mongo/server/monitor.rb', line 182 def restart! if @thread && @thread.alive? @thread else run! end end |
#scan! ⇒ Description
If the system clock is set to a time in the past, this method can sleep for a very long time.
The return value of this method is deprecated. In version 3.0.0 this method will not have a return value.
Perform a check of the server with throttling, and update the server’s description and average round trip time.
If the server was checked less than MIN_SCAN_FREQUENCY seconds ago, sleep until MIN_SCAN_FREQUENCY seconds have passed since the last check. Then perform the check which involves running isMaster on the server being monitored and updating the server description as a result.
165 166 167 168 169 170 171 172 |
# File 'lib/mongo/server/monitor.rb', line 165 def scan! throttle_scan_frequency! result = ismaster new_description = Description.new(server.address, result, server.round_trip_time_averager.average_round_trip_time) server.cluster.run_sdam_flow(server.description, new_description) server.description end |
#stop! ⇒ true | false
Stop the background thread and wait for to terminate for a reasonable amount of time.
135 136 137 138 139 140 141 142 |
# File 'lib/mongo/server/monitor.rb', line 135 def stop! # Forward super's return value super.tap do # Important: disconnect should happen after the background thread # terminated. connection.disconnect! end end |