Class: Mongo::Server::Monitor
- Inherits:
-
Object
- Object
- Mongo::Server::Monitor
- Extended by:
- Forwardable
- Includes:
- Event::Publisher, Loggable
- Defined in:
- lib/mongo/server/monitor.rb,
lib/mongo/server/monitor/connection.rb,
lib/mongo/server/monitor/app_metadata.rb
Overview
This object is responsible for keeping server status up to date, running in a separate thread as to not disrupt other operations.
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.
-
#description ⇒ Server::Description
readonly
Description The server description the monitor refreshes.
-
#last_scan ⇒ Time
readonly
Last_scan The time when the last server scan started.
-
#monitoring ⇒ Monitoring
readonly
Monitoring The monitoring.
-
#options ⇒ Hash
readonly
Options The server options.
- #round_trip_time_averager ⇒ Object readonly private
- #scan_semaphore ⇒ Object readonly private
Attributes included from Event::Publisher
Instance Method Summary collapse
-
#heartbeat_frequency ⇒ Integer
Get the refresh interval for the server.
-
#initialize(address, event_listeners, monitoring, options = {}) ⇒ Monitor
constructor
private
Create the new server monitor.
-
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
-
#run! ⇒ Thread
Runs the server monitor.
-
#scan! ⇒ Description
Perform a check of the server with throttling, and update the server’s description and average round trip time.
-
#stop!(wait = false) ⇒ Boolean
Stops the server monitor.
Methods included from Event::Publisher
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(address, 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mongo/server/monitor.rb', line 60 def initialize(address, event_listeners, monitoring, = {}) unless monitoring.is_a?(Monitoring) raise ArgumentError, "Wrong monitoring type: #{monitoring.inspect}" end @description = Description.new(address, {}) @event_listeners = event_listeners @monitoring = monitoring @options = .freeze @round_trip_time_averager = RoundTripTimeAverager.new @scan_semaphore = Semaphore.new # This is a Mongo::Server::Monitor::Connection @connection = Connection.new(address, ) @last_scan = nil @mutex = Mutex.new end |
Instance Attribute Details
#connection ⇒ Mongo::Server::Monitor::Connection (readonly)
Returns connection The connection to use.
77 78 79 |
# File 'lib/mongo/server/monitor.rb', line 77 def connection @connection end |
#description ⇒ Server::Description (readonly)
Returns description The server description the monitor refreshes.
81 82 83 |
# File 'lib/mongo/server/monitor.rb', line 81 def description @description end |
#last_scan ⇒ Time (readonly)
Returns last_scan The time when the last server scan started.
89 90 91 |
# File 'lib/mongo/server/monitor.rb', line 89 def last_scan @last_scan end |
#monitoring ⇒ Monitoring (readonly)
Returns monitoring The monitoring.
96 97 98 |
# File 'lib/mongo/server/monitor.rb', line 96 def monitoring @monitoring end |
#options ⇒ Hash (readonly)
Returns options The server options.
84 85 86 |
# File 'lib/mongo/server/monitor.rb', line 84 def @options end |
#round_trip_time_averager ⇒ Object (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.
221 222 223 |
# File 'lib/mongo/server/monitor.rb', line 221 def round_trip_time_averager @round_trip_time_averager end |
#scan_semaphore ⇒ Object (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.
112 113 114 |
# File 'lib/mongo/server/monitor.rb', line 112 def scan_semaphore @scan_semaphore end |
Instance Method Details
#heartbeat_frequency ⇒ Integer
Get the refresh interval for the server. This will be defined via an option or will default to 10.
107 108 109 |
# File 'lib/mongo/server/monitor.rb', line 107 def heartbeat_frequency @heartbeat_frequency ||= [:heartbeat_frequency] || HEARTBEAT_FREQUENCY end |
#restart! ⇒ Thread
Restarts the server monitor unless the current thread is alive.
212 213 214 215 216 217 218 |
# File 'lib/mongo/server/monitor.rb', line 212 def restart! if @thread && @thread.alive? @thread else run! end end |
#run! ⇒ Thread
Runs the server monitor. Refreshing happens on a separate thread per server.
123 124 125 126 127 128 129 130 |
# File 'lib/mongo/server/monitor.rb', line 123 def run! @thread = Thread.new(heartbeat_frequency) do |i| loop do scan! @scan_semaphore.wait(i) end 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.
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.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mongo/server/monitor.rb', line 150 def scan! throttle_scan_frequency! result = ismaster new_description = Description.new(description.address, result, @round_trip_time_averager.average_round_trip_time) publish(Event::DESCRIPTION_CHANGED, description, new_description) # If this server's response has a mismatched me, or for other reasons, # this server may be removed from topology. When this happens the # monitor thread gets killed. As a result, any code after the publish # call may not run in a particular monitor instance, hence there # shouldn't be any code here. @description = new_description # This call can be after the publish event because if the # monitoring thread gets killed the server is closed and no client # should be waiting for it if [:server_selection_semaphore] [:server_selection_semaphore].broadcast end @description end |
#stop!(wait = false) ⇒ Boolean
Stops the server monitor. Kills the thread so it doesn’t continue taking memory and sending commands to the connection.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/mongo/server/monitor.rb', line 183 def stop!(wait=false) # Although disconnect! documentation implies a possibility of # failure, all of our disconnects always return true if connection.disconnect! if @thread @thread.kill if wait @thread.join @thread = nil true else !@thread.alive? end else true end else false end end |