summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaley Connelly <haley.connelly@mongodb.com>2020-01-14 10:02:44 -0500
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2020-01-27 15:40:35 -0500
commit775b2785128829327277ff0842914259c7eb7261 (patch)
tree66c2d5d71d03f33a80146e88b0fc4ca83b57955e
parentd1c1029f6e91f919e03bfab008caedd3c19c3671 (diff)
downloadmongo-775b2785128829327277ff0842914259c7eb7261.tar.gz
SERVER-45521 Define TopologyListener, ServerPingMonitor, and SingleServerPingMonitor classes.
create mode 100644 src/mongo/client/sdam/topology_listener.h create mode 100644 src/mongo/client/server_ping_monitor.cpp create mode 100644 src/mongo/client/server_ping_monitor.h
-rw-r--r--src/mongo/client/SConscript1
-rw-r--r--src/mongo/client/sdam/SConscript2
-rw-r--r--src/mongo/client/sdam/topology_listener.h71
-rw-r--r--src/mongo/client/server_ping_monitor.cpp101
-rw-r--r--src/mongo/client/server_ping_monitor.h136
5 files changed, 310 insertions, 1 deletions
diff --git a/src/mongo/client/SConscript b/src/mongo/client/SConscript
index 09fdd610b1d..be7fdfe56c2 100644
--- a/src/mongo/client/SConscript
+++ b/src/mongo/client/SConscript
@@ -191,6 +191,7 @@ clientDriverEnv.Library(
'replica_set_change_notifier.cpp',
'replica_set_monitor.cpp',
'replica_set_monitor_manager.cpp',
+ 'server_ping_monitor.cpp',
],
LIBDEPS=[
'$BUILD_DIR/mongo/db/write_concern_options',
diff --git a/src/mongo/client/sdam/SConscript b/src/mongo/client/sdam/SConscript
index af9e4095ebe..0cdd2361b4f 100644
--- a/src/mongo/client/sdam/SConscript
+++ b/src/mongo/client/sdam/SConscript
@@ -11,7 +11,7 @@ env.Library(
'server_description.cpp',
'topology_description.cpp',
'topology_state_machine.cpp',
- 'topology_manager.cpp'
+ 'topology_manager.cpp',
],
LIBDEPS=[
'$BUILD_DIR/mongo/base',
diff --git a/src/mongo/client/sdam/topology_listener.h b/src/mongo/client/sdam/topology_listener.h
new file mode 100644
index 00000000000..dff6c540899
--- /dev/null
+++ b/src/mongo/client/sdam/topology_listener.h
@@ -0,0 +1,71 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include "mongo/client/sdam/sdam_datatypes.h"
+#include "mongo/util/uuid.h"
+
+namespace mongo::sdam {
+
+/**
+ * An interface for handling topology related events.
+ */
+class TopologyListener {
+public:
+ /**
+ * Called when a TopologyDescriptionChangedEvent is published - The TopologyDescription changed
+ * and the new TopologyDescription does not match the old.
+ */
+ void onTopologyDescriptionChangedEvent(UUID topologyId,
+ TopologyDescriptionPtr previousDescription,
+ TopologyDescriptionPtr newDescription){};
+
+ /**
+ * Called when a ServerHeartBeatSucceededEvent is published - A heartbeat sent to the server at
+ * hostAndPort succeeded. durationMS is the execution time of the event, including the time it
+ * took to send the message and recieve the reply from the server.
+ */
+ void onServerHeartbeatSucceededEvent(mongo::Milliseconds durationMs,
+ ServerAddress hostAndPort){};
+
+ /*
+ * Called when a ServerPingFailedEvent is published - A monitoring ping to the server at
+ * hostAndPort was not successful.
+ */
+ void onServerPingFailedEvent(const ServerAddress hostAndPort, const Status& status){};
+
+ /**
+ * Called when a ServerPingSucceededEvent is published - A monitoring ping to the server at
+ * hostAndPort was successful. durationMS is the measured RTT (Round Trip Time).
+ */
+ void onServerPingSucceededEvent(mongo::Milliseconds durationMS, ServerAddress hostAndPort){};
+};
+
+} // namespace mongo::sdam
diff --git a/src/mongo/client/server_ping_monitor.cpp b/src/mongo/client/server_ping_monitor.cpp
new file mode 100644
index 00000000000..2e2e8bf2e43
--- /dev/null
+++ b/src/mongo/client/server_ping_monitor.cpp
@@ -0,0 +1,101 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kNetwork
+#include "mongo/client/server_ping_monitor.h"
+
+#include "mongo/executor/network_interface_factory.h"
+#include "mongo/executor/network_interface_thread_pool.h"
+#include "mongo/executor/thread_pool_task_executor.h"
+#include "mongo/rpc/metadata/egress_metadata_hook_list.h"
+#include "mongo/util/log.h"
+
+namespace mongo {
+
+using executor::NetworkInterface;
+using executor::NetworkInterfaceThreadPool;
+using executor::TaskExecutor;
+using executor::ThreadPoolTaskExecutor;
+
+SingleServerPingMonitor::SingleServerPingMonitor(sdam::ServerAddress hostAndPort,
+ sdam::TopologyListener* rttListener,
+ Seconds pingFrequency,
+ std::shared_ptr<TaskExecutor> executor)
+ : _hostAndPort(hostAndPort),
+ _rttListener(rttListener),
+ _pingFrequency(pingFrequency),
+ _executor(executor) {}
+
+void SingleServerPingMonitor::drop() { /** TODO SERVER-45051: Implement drop() functionality. **/
+}
+
+ServerPingMonitor::ServerPingMonitor(sdam::TopologyListener* rttListener,
+ Seconds pingFrequency,
+ boost::optional<std::shared_ptr<TaskExecutor>> executor)
+ : _rttListener(rttListener), _pingFrequency(pingFrequency) {
+ // Create an executor by default. Don't create an executor if one was passed in for testing.
+ _setupTaskExecutor(executor);
+}
+
+void ServerPingMonitor::_setupTaskExecutor(
+ boost::optional<std::shared_ptr<TaskExecutor>> executor) {
+ if (executor) {
+ // An executor was already provided for testing.
+ _executor = std::move(executor.value());
+ } else {
+ auto hookList = std::make_unique<rpc::EgressMetadataHookList>();
+ auto net = executor::makeNetworkInterface(
+ "ServerPingMonitor-TaskExecutor", nullptr, std::move(hookList));
+ auto pool = std::make_unique<NetworkInterfaceThreadPool>(net.get());
+ _executor = std::make_shared<ThreadPoolTaskExecutor>(std::move(pool), std::move(net));
+ }
+ _executor->startup();
+}
+
+void ServerPingMonitor::onServerHandshakeCompleteEvent(sdam::ServerAddress address,
+ OID topologyId) {
+ stdx::lock_guard lk(_mutex);
+ invariant(_serverPingMonitorMap.find(address) == _serverPingMonitorMap.end());
+ _serverPingMonitorMap.emplace(address,
+ std::make_unique<SingleServerPingMonitor>(
+ address, _rttListener, _pingFrequency, _executor));
+ LOG(1) << "ServerPingMonitor is now monitoring " << address;
+}
+
+void ServerPingMonitor::onServerClosedEvent(sdam::ServerAddress address, OID topologyId) {
+ stdx::lock_guard lk(_mutex);
+ auto it = _serverPingMonitorMap.find(address);
+ invariant(it != _serverPingMonitorMap.end());
+ it->second->drop();
+ _serverPingMonitorMap.erase(it);
+ LOG(1) << "ServerPingMonitor stopped monitoring " << address;
+}
+
+
+} // namespace mongo
diff --git a/src/mongo/client/server_ping_monitor.h b/src/mongo/client/server_ping_monitor.h
new file mode 100644
index 00000000000..b0dae527909
--- /dev/null
+++ b/src/mongo/client/server_ping_monitor.h
@@ -0,0 +1,136 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include "mongo/client/sdam/sdam_datatypes.h"
+#include "mongo/client/sdam/topology_listener.h"
+#include "mongo/executor/task_executor.h"
+#include "mongo/util/net/hostandport.h"
+
+namespace mongo {
+
+/**
+ * Manages server monitoring for a single server. Broadcasts the RTT (Round Trip Time) to a
+ * listener.
+ */
+class SingleServerPingMonitor : public std::enable_shared_from_this<SingleServerPingMonitor> {
+public:
+ explicit SingleServerPingMonitor(sdam::ServerAddress hostAndPort,
+ sdam::TopologyListener* rttListener,
+ Seconds pingFrequency,
+ std::shared_ptr<executor::TaskExecutor> executor);
+
+ /**
+ * Signals that the SingleServerPingMonitor has been dropped and should cancel any outstanding
+ * pings scheduled to execute in the future. Contract: Once drop() is completed, the
+ * SingleServerPingMonitor will stop broadcasting results to the listener.
+ */
+ void drop();
+
+private:
+ sdam::ServerAddress _hostAndPort;
+
+ /**
+ * Listens for when new RTT (Round Trip Time) values are published.
+ */
+ sdam::TopologyListener* _rttListener;
+
+ /**
+ * The frequency at which ping requests should be sent to measure the round trip time.
+ */
+ Seconds _pingFrequency;
+
+
+ std::shared_ptr<executor::TaskExecutor> _executor;
+};
+
+
+/**
+ * Monitors the RTT (Round Trip Time) for a set of servers.
+ */
+class ServerPingMonitor {
+ ServerPingMonitor(const ServerPingMonitor&) = delete;
+ ServerPingMonitor& operator=(const ServerPingMonitor&) = delete;
+
+public:
+ /**
+ * Note: The ServerPingMonitor creates its own executor by default. It takes in an executor for
+ * testing only.
+ */
+ ServerPingMonitor(
+ sdam::TopologyListener* rttListener,
+ Seconds pingFrequency,
+ boost::optional<std::shared_ptr<executor::TaskExecutor>> executor = boost::none);
+ ~ServerPingMonitor();
+
+ /**
+ * The first isMaster exchange for a server succeeded. Creates a new
+ * SingleServerPingMonitor to monitor the new replica set member.
+ */
+ void onServerHandshakeCompleteEvent(sdam::ServerAddress address, OID topologyId);
+
+ /**
+ * The connection to the server was closed. Removes the server from the ServerPingMonitorList.
+ */
+ void onServerClosedEvent(sdam::ServerAddress address, OID topologyId);
+
+private:
+ /**
+ * Sets up and starts up the _executor. Creates a new executor if one wasn't provided.
+ */
+ void _setupTaskExecutor(boost::optional<std::shared_ptr<executor::TaskExecutor>> executor);
+
+ /**
+ * Listens for when new RTT (Round Trip Time) values are published.
+ */
+ sdam::TopologyListener* _rttListener;
+
+ /**
+ * Executor for performing server monitoring pings for all of the replica set members.
+ */
+ std::shared_ptr<executor::TaskExecutor> _executor;
+
+ /**
+ * The interval at which ping requests should be sent to measure the RTT (Round Trip Time).
+ */
+ Seconds _pingFrequency;
+
+ mutable Mutex _mutex = MONGO_MAKE_LATCH("ServerPingMonitor::mutex");
+
+ /**
+ * Maps each server to a SingleServerPingMonitor.
+ * Note: SingleServerPingMonitor's drop() should always be called before removing it from the
+ * _serverPingMonitorMap.
+ */
+ stdx::unordered_map<sdam::ServerAddress, std::unique_ptr<SingleServerPingMonitor>>
+ _serverPingMonitorMap;
+};
+
+} // namespace mongo