Class: Mongo::Server::Monitor

Inherits:
Object
  • Object
show all
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.

Since:

  • 2.0.0

Defined Under Namespace

Classes: AppMetadata, Connection

Constant Summary collapse

HEARTBEAT_FREQUENCY =

The default time for a server to refresh its status is 10 seconds.

Since:

  • 2.0.0

10.freeze
MIN_SCAN_FREQUENCY =

The minimum time between forced server scans. Is minHeartbeatFrequencyMS in the SDAM spec.

Since:

  • 2.0.0

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.

Since:

  • 2.0.0

0.2.freeze

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Attributes included from Event::Publisher

#event_listeners

Instance Method Summary collapse

Methods included from BackgroundThread

#run!, #running?

Methods included from Loggable

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

Methods included from Event::Publisher

#publish

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.

Note:

Monitor must never be directly instantiated outside of a Server.

Create the new server monitor.

Examples:

Create the server monitor.

Mongo::Server::Monitor.new(address, listeners, monitoring)

Parameters:

  • server (Server)

    The server to monitor.

  • event_listeners (Event::Listeners)

    The event listeners.

  • monitoring (Monitoring)

    The monitoring..

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

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    The timeout, in seconds, to use when establishing the monitoring connection.

  • :logger (Logger)

    A custom logger to use.

  • :socket_timeout (Float)

    The timeout, in seconds, to execute operations on the monitoring connection.

Since:

  • 2.0.0



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, options = {})
  unless monitoring.is_a?(Monitoring)
    raise ArgumentError, "Wrong monitoring type: #{monitoring.inspect}"
  end
  @server = server
  @event_listeners = event_listeners
  @monitoring = monitoring
  @options = options.freeze
  # This is a Mongo::Server::Monitor::Connection
  @connection = Connection.new(server.address, options)
  @mutex = Mutex.new
  @scan_started_at = nil
end

Instance Attribute Details

#connectionMongo::Server::Monitor::Connection (readonly)

Returns connection The connection to use.

Returns:

Since:

  • 2.0.0



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

def connection
  @connection
end

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.0.0



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

def monitoring
  @monitoring
end

#optionsHash (readonly)

Returns options The server options.

Returns:

  • (Hash)

    options The server options.

Since:

  • 2.0.0



90
91
92
# File 'lib/mongo/server/monitor.rb', line 90

def options
  @options
end

#serverServer (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.

Returns:

  • (Server)

    server The server that this monitor is monitoring.

Since:

  • 2.0.0



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

def server
  @server
end

Instance Method Details

#do_workThread

Runs the server monitor. Refreshing happens on a separate thread per server.

Examples:

Run the monitor.

monitor.run

Returns:

  • (Thread)

    The thread the monitor runs on.

Since:

  • 2.0.0



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_frequencyFloat

Deprecated.

Get the refresh interval for the server. This will be defined via an option or will default to 10.

Returns:

  • (Float)

    The heartbeat interval, in seconds.

Since:

  • 2.0.0



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.

Examples:

Restart the monitor.

monitor.restart!

Returns:

  • (Thread)

    The thread the monitor runs on.

Since:

  • 2.1.0



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

Note:

If the system clock is set to a time in the past, this method can sleep for a very long time.

Note:

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.

Examples:

Run a scan.

monitor.scan!

Returns:

Since:

  • 2.0.0



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.

Returns:

  • (true | false)

    Whether the thread was terminated.

Since:

  • 2.0.0



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