sensu-plugins-mongodb-mrtrotl 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +22 -0
- data/README.md +27 -0
- data/bin/check-mongodb-metric.rb +144 -0
- data/bin/check-mongodb-query-count.rb +267 -0
- data/bin/check-mongodb.py +1644 -0
- data/bin/check-mongodb.rb +5 -0
- data/bin/metrics-mongodb-replication.rb +254 -0
- data/bin/metrics-mongodb.rb +133 -0
- data/lib/bson/__init__.py +1347 -0
- data/lib/bson/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/_helpers.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/binary.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/code.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/codec_options.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/dbref.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/decimal128.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/int64.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/json_util.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/max_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/min_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/objectid.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/raw_bson.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/regex.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/son.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/timestamp.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/tz_util.cpython-310.pyc +0 -0
- data/lib/bson/_cbson.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/bson/_helpers.py +41 -0
- data/lib/bson/binary.py +364 -0
- data/lib/bson/code.py +101 -0
- data/lib/bson/codec_options.py +414 -0
- data/lib/bson/codec_options.pyi +100 -0
- data/lib/bson/dbref.py +133 -0
- data/lib/bson/decimal128.py +314 -0
- data/lib/bson/errors.py +35 -0
- data/lib/bson/int64.py +39 -0
- data/lib/bson/json_util.py +874 -0
- data/lib/bson/max_key.py +55 -0
- data/lib/bson/min_key.py +55 -0
- data/lib/bson/objectid.py +286 -0
- data/lib/bson/py.typed +2 -0
- data/lib/bson/raw_bson.py +175 -0
- data/lib/bson/regex.py +135 -0
- data/lib/bson/son.py +208 -0
- data/lib/bson/timestamp.py +124 -0
- data/lib/bson/tz_util.py +52 -0
- data/lib/gridfs/__init__.py +1015 -0
- data/lib/gridfs/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/grid_file.cpython-310.pyc +0 -0
- data/lib/gridfs/errors.py +33 -0
- data/lib/gridfs/grid_file.py +907 -0
- data/lib/gridfs/py.typed +2 -0
- data/lib/pymongo/__init__.py +185 -0
- data/lib/pymongo/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/_csot.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/aggregation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth_aws.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/bulk.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/change_stream.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_session.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collection.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/command_cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/common.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/compression_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/daemon.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/database.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/driver_info.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/event_loggers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/hello.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/helpers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/max_staleness_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/message.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/mongo_client.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitoring.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/network.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_cache.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/operations.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/periodic_executor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pool.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pyopenssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_preferences.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/response.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/results.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/saslprep.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_api.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_type.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/settings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/socket_checker.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/srv_resolver.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/typings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/uri_parser.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/write_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/_cmessage.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/pymongo/_csot.py +118 -0
- data/lib/pymongo/aggregation.py +229 -0
- data/lib/pymongo/auth.py +549 -0
- data/lib/pymongo/auth_aws.py +94 -0
- data/lib/pymongo/bulk.py +513 -0
- data/lib/pymongo/change_stream.py +457 -0
- data/lib/pymongo/client_options.py +302 -0
- data/lib/pymongo/client_session.py +1112 -0
- data/lib/pymongo/collation.py +224 -0
- data/lib/pymongo/collection.py +3204 -0
- data/lib/pymongo/command_cursor.py +353 -0
- data/lib/pymongo/common.py +984 -0
- data/lib/pymongo/compression_support.py +149 -0
- data/lib/pymongo/cursor.py +1345 -0
- data/lib/pymongo/daemon.py +141 -0
- data/lib/pymongo/database.py +1202 -0
- data/lib/pymongo/driver_info.py +42 -0
- data/lib/pymongo/encryption.py +884 -0
- data/lib/pymongo/encryption_options.py +221 -0
- data/lib/pymongo/errors.py +365 -0
- data/lib/pymongo/event_loggers.py +221 -0
- data/lib/pymongo/hello.py +219 -0
- data/lib/pymongo/helpers.py +259 -0
- data/lib/pymongo/max_staleness_selectors.py +114 -0
- data/lib/pymongo/message.py +1440 -0
- data/lib/pymongo/mongo_client.py +2144 -0
- data/lib/pymongo/monitor.py +440 -0
- data/lib/pymongo/monitoring.py +1801 -0
- data/lib/pymongo/network.py +311 -0
- data/lib/pymongo/ocsp_cache.py +87 -0
- data/lib/pymongo/ocsp_support.py +372 -0
- data/lib/pymongo/operations.py +507 -0
- data/lib/pymongo/periodic_executor.py +183 -0
- data/lib/pymongo/pool.py +1660 -0
- data/lib/pymongo/py.typed +2 -0
- data/lib/pymongo/pyopenssl_context.py +383 -0
- data/lib/pymongo/read_concern.py +75 -0
- data/lib/pymongo/read_preferences.py +609 -0
- data/lib/pymongo/response.py +109 -0
- data/lib/pymongo/results.py +217 -0
- data/lib/pymongo/saslprep.py +113 -0
- data/lib/pymongo/server.py +247 -0
- data/lib/pymongo/server_api.py +170 -0
- data/lib/pymongo/server_description.py +285 -0
- data/lib/pymongo/server_selectors.py +153 -0
- data/lib/pymongo/server_type.py +32 -0
- data/lib/pymongo/settings.py +159 -0
- data/lib/pymongo/socket_checker.py +104 -0
- data/lib/pymongo/srv_resolver.py +126 -0
- data/lib/pymongo/ssl_context.py +39 -0
- data/lib/pymongo/ssl_support.py +99 -0
- data/lib/pymongo/topology.py +890 -0
- data/lib/pymongo/topology_description.py +639 -0
- data/lib/pymongo/typings.py +39 -0
- data/lib/pymongo/uri_parser.py +624 -0
- data/lib/pymongo/write_concern.py +129 -0
- data/lib/pymongo-4.2.0.dist-info/INSTALLER +1 -0
- data/lib/pymongo-4.2.0.dist-info/LICENSE +201 -0
- data/lib/pymongo-4.2.0.dist-info/METADATA +250 -0
- data/lib/pymongo-4.2.0.dist-info/RECORD +167 -0
- data/lib/pymongo-4.2.0.dist-info/REQUESTED +0 -0
- data/lib/pymongo-4.2.0.dist-info/WHEEL +6 -0
- data/lib/pymongo-4.2.0.dist-info/top_level.txt +3 -0
- data/lib/sensu-plugins-mongodb/metrics.rb +391 -0
- data/lib/sensu-plugins-mongodb/version.rb +9 -0
- data/lib/sensu-plugins-mongodb.rb +1 -0
- metadata +407 -0
@@ -0,0 +1,440 @@
|
|
1
|
+
# Copyright 2014-present MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you
|
4
|
+
# may not use this file except in compliance with the License. You
|
5
|
+
# may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
12
|
+
# implied. See the License for the specific language governing
|
13
|
+
# permissions and limitations under the License.
|
14
|
+
|
15
|
+
"""Class to monitor a MongoDB server on a background thread."""
|
16
|
+
|
17
|
+
import atexit
|
18
|
+
import threading
|
19
|
+
import time
|
20
|
+
import weakref
|
21
|
+
from typing import Any, Mapping, cast
|
22
|
+
|
23
|
+
from pymongo import common, periodic_executor
|
24
|
+
from pymongo.errors import NotPrimaryError, OperationFailure, _OperationCancelled
|
25
|
+
from pymongo.hello import Hello
|
26
|
+
from pymongo.periodic_executor import _shutdown_executors
|
27
|
+
from pymongo.read_preferences import MovingAverage
|
28
|
+
from pymongo.server_description import ServerDescription
|
29
|
+
from pymongo.srv_resolver import _SrvResolver
|
30
|
+
|
31
|
+
|
32
|
+
def _sanitize(error):
|
33
|
+
"""PYTHON-2433 Clear error traceback info."""
|
34
|
+
error.__traceback__ = None
|
35
|
+
error.__context__ = None
|
36
|
+
error.__cause__ = None
|
37
|
+
|
38
|
+
|
39
|
+
class MonitorBase(object):
|
40
|
+
def __init__(self, topology, name, interval, min_interval):
|
41
|
+
"""Base class to do periodic work on a background thread.
|
42
|
+
|
43
|
+
The the background thread is signaled to stop when the Topology or
|
44
|
+
this instance is freed.
|
45
|
+
"""
|
46
|
+
# We strongly reference the executor and it weakly references us via
|
47
|
+
# this closure. When the monitor is freed, stop the executor soon.
|
48
|
+
def target():
|
49
|
+
monitor = self_ref()
|
50
|
+
if monitor is None:
|
51
|
+
return False # Stop the executor.
|
52
|
+
monitor._run() # type:ignore[attr-defined]
|
53
|
+
return True
|
54
|
+
|
55
|
+
executor = periodic_executor.PeriodicExecutor(
|
56
|
+
interval=interval, min_interval=min_interval, target=target, name=name
|
57
|
+
)
|
58
|
+
|
59
|
+
self._executor = executor
|
60
|
+
|
61
|
+
def _on_topology_gc(dummy=None):
|
62
|
+
# This prevents GC from waiting 10 seconds for hello to complete
|
63
|
+
# See test_cleanup_executors_on_client_del.
|
64
|
+
monitor = self_ref()
|
65
|
+
if monitor:
|
66
|
+
monitor.gc_safe_close()
|
67
|
+
|
68
|
+
# Avoid cycles. When self or topology is freed, stop executor soon.
|
69
|
+
self_ref = weakref.ref(self, executor.close)
|
70
|
+
self._topology = weakref.proxy(topology, _on_topology_gc)
|
71
|
+
_register(self)
|
72
|
+
|
73
|
+
def open(self):
|
74
|
+
"""Start monitoring, or restart after a fork.
|
75
|
+
|
76
|
+
Multiple calls have no effect.
|
77
|
+
"""
|
78
|
+
self._executor.open()
|
79
|
+
|
80
|
+
def gc_safe_close(self):
|
81
|
+
"""GC safe close."""
|
82
|
+
self._executor.close()
|
83
|
+
|
84
|
+
def close(self):
|
85
|
+
"""Close and stop monitoring.
|
86
|
+
|
87
|
+
open() restarts the monitor after closing.
|
88
|
+
"""
|
89
|
+
self.gc_safe_close()
|
90
|
+
|
91
|
+
def join(self, timeout=None):
|
92
|
+
"""Wait for the monitor to stop."""
|
93
|
+
self._executor.join(timeout)
|
94
|
+
|
95
|
+
def request_check(self):
|
96
|
+
"""If the monitor is sleeping, wake it soon."""
|
97
|
+
self._executor.wake()
|
98
|
+
|
99
|
+
|
100
|
+
class Monitor(MonitorBase):
|
101
|
+
def __init__(self, server_description, topology, pool, topology_settings):
|
102
|
+
"""Class to monitor a MongoDB server on a background thread.
|
103
|
+
|
104
|
+
Pass an initial ServerDescription, a Topology, a Pool, and
|
105
|
+
TopologySettings.
|
106
|
+
|
107
|
+
The Topology is weakly referenced. The Pool must be exclusive to this
|
108
|
+
Monitor.
|
109
|
+
"""
|
110
|
+
super(Monitor, self).__init__(
|
111
|
+
topology,
|
112
|
+
"pymongo_server_monitor_thread",
|
113
|
+
topology_settings.heartbeat_frequency,
|
114
|
+
common.MIN_HEARTBEAT_INTERVAL,
|
115
|
+
)
|
116
|
+
self._server_description = server_description
|
117
|
+
self._pool = pool
|
118
|
+
self._settings = topology_settings
|
119
|
+
self._listeners = self._settings._pool_options._event_listeners
|
120
|
+
pub = self._listeners is not None
|
121
|
+
self._publish = pub and self._listeners.enabled_for_server_heartbeat
|
122
|
+
self._cancel_context = None
|
123
|
+
self._rtt_monitor = _RttMonitor(
|
124
|
+
topology,
|
125
|
+
topology_settings,
|
126
|
+
topology._create_pool_for_monitor(server_description.address),
|
127
|
+
)
|
128
|
+
self.heartbeater = None
|
129
|
+
|
130
|
+
def cancel_check(self):
|
131
|
+
"""Cancel any concurrent hello check.
|
132
|
+
|
133
|
+
Note: this is called from a weakref.proxy callback and MUST NOT take
|
134
|
+
any locks.
|
135
|
+
"""
|
136
|
+
context = self._cancel_context
|
137
|
+
if context:
|
138
|
+
# Note: we cannot close the socket because doing so may cause
|
139
|
+
# concurrent reads/writes to hang until a timeout occurs
|
140
|
+
# (depending on the platform).
|
141
|
+
context.cancel()
|
142
|
+
|
143
|
+
def _start_rtt_monitor(self):
|
144
|
+
"""Start an _RttMonitor that periodically runs ping."""
|
145
|
+
# If this monitor is closed directly before (or during) this open()
|
146
|
+
# call, the _RttMonitor will not be closed. Checking if this monitor
|
147
|
+
# was closed directly after resolves the race.
|
148
|
+
self._rtt_monitor.open()
|
149
|
+
if self._executor._stopped:
|
150
|
+
self._rtt_monitor.close()
|
151
|
+
|
152
|
+
def gc_safe_close(self):
|
153
|
+
self._executor.close()
|
154
|
+
self._rtt_monitor.gc_safe_close()
|
155
|
+
self.cancel_check()
|
156
|
+
|
157
|
+
def close(self):
|
158
|
+
self.gc_safe_close()
|
159
|
+
self._rtt_monitor.close()
|
160
|
+
# Increment the generation and maybe close the socket. If the executor
|
161
|
+
# thread has the socket checked out, it will be closed when checked in.
|
162
|
+
self._reset_connection()
|
163
|
+
|
164
|
+
def _reset_connection(self):
|
165
|
+
# Clear our pooled connection.
|
166
|
+
self._pool.reset()
|
167
|
+
|
168
|
+
def _run(self):
|
169
|
+
try:
|
170
|
+
prev_sd = self._server_description
|
171
|
+
try:
|
172
|
+
self._server_description = self._check_server()
|
173
|
+
except _OperationCancelled as exc:
|
174
|
+
_sanitize(exc)
|
175
|
+
# Already closed the connection, wait for the next check.
|
176
|
+
self._server_description = ServerDescription(
|
177
|
+
self._server_description.address, error=exc
|
178
|
+
)
|
179
|
+
if prev_sd.is_server_type_known:
|
180
|
+
# Immediately retry since we've already waited 500ms to
|
181
|
+
# discover that we've been cancelled.
|
182
|
+
self._executor.skip_sleep()
|
183
|
+
return
|
184
|
+
|
185
|
+
# Update the Topology and clear the server pool on error.
|
186
|
+
self._topology.on_change(
|
187
|
+
self._server_description, reset_pool=self._server_description.error
|
188
|
+
)
|
189
|
+
|
190
|
+
if (
|
191
|
+
self._server_description.is_server_type_known
|
192
|
+
and self._server_description.topology_version
|
193
|
+
):
|
194
|
+
self._start_rtt_monitor()
|
195
|
+
# Immediately check for the next streaming response.
|
196
|
+
self._executor.skip_sleep()
|
197
|
+
|
198
|
+
if self._server_description.error and prev_sd.is_server_type_known:
|
199
|
+
# Immediately retry on network errors.
|
200
|
+
self._executor.skip_sleep()
|
201
|
+
except ReferenceError:
|
202
|
+
# Topology was garbage-collected.
|
203
|
+
self.close()
|
204
|
+
|
205
|
+
def _check_server(self):
|
206
|
+
"""Call hello or read the next streaming response.
|
207
|
+
|
208
|
+
Returns a ServerDescription.
|
209
|
+
"""
|
210
|
+
start = time.monotonic()
|
211
|
+
try:
|
212
|
+
try:
|
213
|
+
return self._check_once()
|
214
|
+
except (OperationFailure, NotPrimaryError) as exc:
|
215
|
+
# Update max cluster time even when hello fails.
|
216
|
+
details = cast(Mapping[str, Any], exc.details)
|
217
|
+
self._topology.receive_cluster_time(details.get("$clusterTime"))
|
218
|
+
raise
|
219
|
+
except ReferenceError:
|
220
|
+
raise
|
221
|
+
except Exception as error:
|
222
|
+
_sanitize(error)
|
223
|
+
sd = self._server_description
|
224
|
+
address = sd.address
|
225
|
+
duration = time.monotonic() - start
|
226
|
+
if self._publish:
|
227
|
+
awaited = sd.is_server_type_known and sd.topology_version
|
228
|
+
self._listeners.publish_server_heartbeat_failed(address, duration, error, awaited)
|
229
|
+
self._reset_connection()
|
230
|
+
if isinstance(error, _OperationCancelled):
|
231
|
+
raise
|
232
|
+
self._rtt_monitor.reset()
|
233
|
+
# Server type defaults to Unknown.
|
234
|
+
return ServerDescription(address, error=error)
|
235
|
+
|
236
|
+
def _check_once(self):
|
237
|
+
"""A single attempt to call hello.
|
238
|
+
|
239
|
+
Returns a ServerDescription, or raises an exception.
|
240
|
+
"""
|
241
|
+
address = self._server_description.address
|
242
|
+
if self._publish:
|
243
|
+
self._listeners.publish_server_heartbeat_started(address)
|
244
|
+
|
245
|
+
if self._cancel_context and self._cancel_context.cancelled:
|
246
|
+
self._reset_connection()
|
247
|
+
with self._pool.get_socket() as sock_info:
|
248
|
+
self._cancel_context = sock_info.cancel_context
|
249
|
+
response, round_trip_time = self._check_with_socket(sock_info)
|
250
|
+
if not response.awaitable:
|
251
|
+
self._rtt_monitor.add_sample(round_trip_time)
|
252
|
+
|
253
|
+
sd = ServerDescription(address, response, self._rtt_monitor.average())
|
254
|
+
if self._publish:
|
255
|
+
self._listeners.publish_server_heartbeat_succeeded(
|
256
|
+
address, round_trip_time, response, response.awaitable
|
257
|
+
)
|
258
|
+
return sd
|
259
|
+
|
260
|
+
def _check_with_socket(self, conn):
|
261
|
+
"""Return (Hello, round_trip_time).
|
262
|
+
|
263
|
+
Can raise ConnectionFailure or OperationFailure.
|
264
|
+
"""
|
265
|
+
cluster_time = self._topology.max_cluster_time()
|
266
|
+
start = time.monotonic()
|
267
|
+
if conn.more_to_come:
|
268
|
+
# Read the next streaming hello (MongoDB 4.4+).
|
269
|
+
response = Hello(conn._next_reply(), awaitable=True)
|
270
|
+
elif conn.performed_handshake and self._server_description.topology_version:
|
271
|
+
# Initiate streaming hello (MongoDB 4.4+).
|
272
|
+
response = conn._hello(
|
273
|
+
cluster_time,
|
274
|
+
self._server_description.topology_version,
|
275
|
+
self._settings.heartbeat_frequency,
|
276
|
+
)
|
277
|
+
else:
|
278
|
+
# New connection handshake or polling hello (MongoDB <4.4).
|
279
|
+
response = conn._hello(cluster_time, None, None)
|
280
|
+
return response, time.monotonic() - start
|
281
|
+
|
282
|
+
|
283
|
+
class SrvMonitor(MonitorBase):
|
284
|
+
def __init__(self, topology, topology_settings):
|
285
|
+
"""Class to poll SRV records on a background thread.
|
286
|
+
|
287
|
+
Pass a Topology and a TopologySettings.
|
288
|
+
|
289
|
+
The Topology is weakly referenced.
|
290
|
+
"""
|
291
|
+
super(SrvMonitor, self).__init__(
|
292
|
+
topology,
|
293
|
+
"pymongo_srv_polling_thread",
|
294
|
+
common.MIN_SRV_RESCAN_INTERVAL,
|
295
|
+
topology_settings.heartbeat_frequency,
|
296
|
+
)
|
297
|
+
self._settings = topology_settings
|
298
|
+
self._seedlist = self._settings._seeds
|
299
|
+
self._fqdn = self._settings.fqdn
|
300
|
+
|
301
|
+
def _run(self):
|
302
|
+
seedlist = self._get_seedlist()
|
303
|
+
if seedlist:
|
304
|
+
self._seedlist = seedlist
|
305
|
+
try:
|
306
|
+
self._topology.on_srv_update(self._seedlist)
|
307
|
+
except ReferenceError:
|
308
|
+
# Topology was garbage-collected.
|
309
|
+
self.close()
|
310
|
+
|
311
|
+
def _get_seedlist(self):
|
312
|
+
"""Poll SRV records for a seedlist.
|
313
|
+
|
314
|
+
Returns a list of ServerDescriptions.
|
315
|
+
"""
|
316
|
+
try:
|
317
|
+
resolver = _SrvResolver(
|
318
|
+
self._fqdn,
|
319
|
+
self._settings.pool_options.connect_timeout,
|
320
|
+
self._settings.srv_service_name,
|
321
|
+
)
|
322
|
+
seedlist, ttl = resolver.get_hosts_and_min_ttl()
|
323
|
+
if len(seedlist) == 0:
|
324
|
+
# As per the spec: this should be treated as a failure.
|
325
|
+
raise Exception
|
326
|
+
except Exception:
|
327
|
+
# As per the spec, upon encountering an error:
|
328
|
+
# - An error must not be raised
|
329
|
+
# - SRV records must be rescanned every heartbeatFrequencyMS
|
330
|
+
# - Topology must be left unchanged
|
331
|
+
self.request_check()
|
332
|
+
return None
|
333
|
+
else:
|
334
|
+
self._executor.update_interval(max(ttl, common.MIN_SRV_RESCAN_INTERVAL))
|
335
|
+
return seedlist
|
336
|
+
|
337
|
+
|
338
|
+
class _RttMonitor(MonitorBase):
|
339
|
+
def __init__(self, topology, topology_settings, pool):
|
340
|
+
"""Maintain round trip times for a server.
|
341
|
+
|
342
|
+
The Topology is weakly referenced.
|
343
|
+
"""
|
344
|
+
super(_RttMonitor, self).__init__(
|
345
|
+
topology,
|
346
|
+
"pymongo_server_rtt_thread",
|
347
|
+
topology_settings.heartbeat_frequency,
|
348
|
+
common.MIN_HEARTBEAT_INTERVAL,
|
349
|
+
)
|
350
|
+
|
351
|
+
self._pool = pool
|
352
|
+
self._moving_average = MovingAverage()
|
353
|
+
self._lock = threading.Lock()
|
354
|
+
|
355
|
+
def close(self):
|
356
|
+
self.gc_safe_close()
|
357
|
+
# Increment the generation and maybe close the socket. If the executor
|
358
|
+
# thread has the socket checked out, it will be closed when checked in.
|
359
|
+
self._pool.reset()
|
360
|
+
|
361
|
+
def add_sample(self, sample):
|
362
|
+
"""Add a RTT sample."""
|
363
|
+
with self._lock:
|
364
|
+
self._moving_average.add_sample(sample)
|
365
|
+
|
366
|
+
def average(self):
|
367
|
+
"""Get the calculated average, or None if no samples yet."""
|
368
|
+
with self._lock:
|
369
|
+
return self._moving_average.get()
|
370
|
+
|
371
|
+
def reset(self):
|
372
|
+
"""Reset the average RTT."""
|
373
|
+
with self._lock:
|
374
|
+
return self._moving_average.reset()
|
375
|
+
|
376
|
+
def _run(self):
|
377
|
+
try:
|
378
|
+
# NOTE: This thread is only run when when using the streaming
|
379
|
+
# heartbeat protocol (MongoDB 4.4+).
|
380
|
+
# XXX: Skip check if the server is unknown?
|
381
|
+
rtt = self._ping()
|
382
|
+
self.add_sample(rtt)
|
383
|
+
except ReferenceError:
|
384
|
+
# Topology was garbage-collected.
|
385
|
+
self.close()
|
386
|
+
except Exception:
|
387
|
+
self._pool.reset()
|
388
|
+
|
389
|
+
def _ping(self):
|
390
|
+
"""Run a "hello" command and return the RTT."""
|
391
|
+
with self._pool.get_socket() as sock_info:
|
392
|
+
if self._executor._stopped:
|
393
|
+
raise Exception("_RttMonitor closed")
|
394
|
+
start = time.monotonic()
|
395
|
+
sock_info.hello()
|
396
|
+
return time.monotonic() - start
|
397
|
+
|
398
|
+
|
399
|
+
# Close monitors to cancel any in progress streaming checks before joining
|
400
|
+
# executor threads. For an explanation of how this works see the comment
|
401
|
+
# about _EXECUTORS in periodic_executor.py.
|
402
|
+
_MONITORS = set()
|
403
|
+
|
404
|
+
|
405
|
+
def _register(monitor):
|
406
|
+
ref = weakref.ref(monitor, _unregister)
|
407
|
+
_MONITORS.add(ref)
|
408
|
+
|
409
|
+
|
410
|
+
def _unregister(monitor_ref):
|
411
|
+
_MONITORS.remove(monitor_ref)
|
412
|
+
|
413
|
+
|
414
|
+
def _shutdown_monitors():
|
415
|
+
if _MONITORS is None:
|
416
|
+
return
|
417
|
+
|
418
|
+
# Copy the set. Closing monitors removes them.
|
419
|
+
monitors = list(_MONITORS)
|
420
|
+
|
421
|
+
# Close all monitors.
|
422
|
+
for ref in monitors:
|
423
|
+
monitor = ref()
|
424
|
+
if monitor:
|
425
|
+
monitor.gc_safe_close()
|
426
|
+
|
427
|
+
monitor = None
|
428
|
+
|
429
|
+
|
430
|
+
def _shutdown_resources():
|
431
|
+
# _shutdown_monitors/_shutdown_executors may already be GC'd at shutdown.
|
432
|
+
shutdown = _shutdown_monitors
|
433
|
+
if shutdown:
|
434
|
+
shutdown()
|
435
|
+
shutdown = _shutdown_executors
|
436
|
+
if shutdown:
|
437
|
+
shutdown()
|
438
|
+
|
439
|
+
|
440
|
+
atexit.register(_shutdown_resources)
|