diff options
-rw-r--r-- | src/mongo/client/dbclient.cpp | 12 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.h | 4 | ||||
-rw-r--r-- | src/mongo/client/dbclientinterface.h | 7 | ||||
-rw-r--r-- | src/mongo/db/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/db/dbdirectclient.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/dbdirectclient.h | 2 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/mongo.cpp | 15 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/mongo.h | 4 |
8 files changed, 52 insertions, 1 deletions
diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp index d85032af84b..2ce3628e7de 100644 --- a/src/mongo/client/dbclient.cpp +++ b/src/mongo/client/dbclient.cpp @@ -861,6 +861,18 @@ Status DBClientConnection::connect(const HostAndPort& serverAddress, StringData } { + // The Server Discovery and Monitoring (SDAM) specification identifies a replica set member + // as either (a) having a "setName" field in the isMaster response, or (b) having + // "isreplicaset: true" in the isMaster response. + // + // https://github.com/mongodb/specifications/blob/c386e23724318e2fa82f4f7663d77581b755b2c3/ + // source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#type + const bool hasSetNameField = swIsMasterReply.data.hasField("setName"); + const bool isReplicaSetField = swIsMasterReply.data.getBoolField("isreplicaset"); + _isReplicaSetMember = hasSetNameField || isReplicaSetField; + } + + { std::string msgField; auto msgFieldExtractStatus = bsonExtractStringField(swIsMasterReply.data, "msg", &msgField); diff --git a/src/mongo/client/dbclient_rs.h b/src/mongo/client/dbclient_rs.h index 2cc259e558a..05a5e09e4a9 100644 --- a/src/mongo/client/dbclient_rs.h +++ b/src/mongo/client/dbclient_rs.h @@ -219,6 +219,10 @@ public: */ virtual void reset(); + bool isReplicaSetMember() const override { + return true; + } + bool isMongos() const override { return false; } diff --git a/src/mongo/client/dbclientinterface.h b/src/mongo/client/dbclientinterface.h index 8c6e02f6c21..fdfc816b126 100644 --- a/src/mongo/client/dbclientinterface.h +++ b/src/mongo/client/dbclientinterface.h @@ -791,6 +791,8 @@ public: virtual void reset() {} + virtual bool isReplicaSetMember() const = 0; + virtual bool isMongos() const = 0; /** @@ -1047,6 +1049,10 @@ public: _checkConnection(); } + bool isReplicaSetMember() const override { + return _isReplicaSetMember; + } + bool isMongos() const override { return _isMongos; } @@ -1054,6 +1060,7 @@ public: protected: int _minWireVersion{0}; int _maxWireVersion{0}; + bool _isReplicaSetMember = false; bool _isMongos = false; virtual void _auth(const BSONObj& params); diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index 73a03f0e499..5c358768737 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -552,6 +552,9 @@ env.Library( 'curop', 'lasterror', ], + LIBDEPS_PRIVATE=[ + '$BUILD_DIR/mongo/db/repl/repl_coordinator_interface', + ], ) env.Library( diff --git a/src/mongo/db/dbdirectclient.cpp b/src/mongo/db/dbdirectclient.cpp index 76f389b8434..c644f2dd631 100644 --- a/src/mongo/db/dbdirectclient.cpp +++ b/src/mongo/db/dbdirectclient.cpp @@ -38,6 +38,7 @@ #include "mongo/db/commands.h" #include "mongo/db/curop.h" #include "mongo/db/operation_context.h" +#include "mongo/db/repl/replication_coordinator.h" #include "mongo/db/wire_version.h" #include "mongo/rpc/get_status_from_command_result.h" #include "mongo/transport/service_entry_point.h" @@ -102,6 +103,11 @@ int DBDirectClient::getMaxWireVersion() { return WireSpec::instance().incoming.maxWireVersion; } +bool DBDirectClient::isReplicaSetMember() const { + auto const* replCoord = repl::ReplicationCoordinator::get(_opCtx); + return replCoord && replCoord->isReplEnabled(); +} + ConnectionString::ConnectionType DBDirectClient::type() const { return ConnectionString::MASTER; } diff --git a/src/mongo/db/dbdirectclient.h b/src/mongo/db/dbdirectclient.h index 4432d775e23..b410613cca5 100644 --- a/src/mongo/db/dbdirectclient.h +++ b/src/mongo/db/dbdirectclient.h @@ -97,6 +97,8 @@ public: int getMinWireVersion() final; int getMaxWireVersion() final; + bool isReplicaSetMember() const final; + bool isMongos() const final { return false; } diff --git a/src/mongo/scripting/mozjs/mongo.cpp b/src/mongo/scripting/mozjs/mongo.cpp index 84c93aa786f..922a6931891 100644 --- a/src/mongo/scripting/mozjs/mongo.cpp +++ b/src/mongo/scripting/mozjs/mongo.cpp @@ -84,6 +84,9 @@ const JSFunctionSpec MongoBase::methods[] = { getMinWireVersion, MongoLocalInfo, MongoExternalInfo), MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO( getMaxWireVersion, MongoLocalInfo, MongoExternalInfo), + MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO( + isReplicaSetMember, MongoLocalInfo, MongoExternalInfo), + MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(isMongos, MongoLocalInfo, MongoExternalInfo), MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(getClusterTime, MongoLocalInfo, MongoExternalInfo), MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(setClusterTime, MongoLocalInfo, MongoExternalInfo), MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(_startSession, MongoLocalInfo, MongoExternalInfo), @@ -766,6 +769,18 @@ void MongoBase::Functions::getMaxWireVersion::call(JSContext* cx, JS::CallArgs a args.rval().setInt32(conn->getMaxWireVersion()); } +void MongoBase::Functions::isReplicaSetMember::call(JSContext* cx, JS::CallArgs args) { + auto conn = getConnection(args); + + args.rval().setBoolean(conn->isReplicaSetMember()); +} + +void MongoBase::Functions::isMongos::call(JSContext* cx, JS::CallArgs args) { + auto conn = getConnection(args); + + args.rval().setBoolean(conn->isMongos()); +} + void MongoBase::Functions::getClusterTime::call(JSContext* cx, JS::CallArgs args) { auto ct = MongoBase::getClusterTime(); diff --git a/src/mongo/scripting/mozjs/mongo.h b/src/mongo/scripting/mozjs/mongo.h index 6d8426520b3..f6c6a535c1c 100644 --- a/src/mongo/scripting/mozjs/mongo.h +++ b/src/mongo/scripting/mozjs/mongo.h @@ -62,12 +62,14 @@ struct MongoBase : public BaseInfo { MONGO_DECLARE_JS_FUNCTION(update); MONGO_DECLARE_JS_FUNCTION(getMinWireVersion); MONGO_DECLARE_JS_FUNCTION(getMaxWireVersion); + MONGO_DECLARE_JS_FUNCTION(isReplicaSetMember); + MONGO_DECLARE_JS_FUNCTION(isMongos); MONGO_DECLARE_JS_FUNCTION(getClusterTime); MONGO_DECLARE_JS_FUNCTION(setClusterTime); MONGO_DECLARE_JS_FUNCTION(_startSession); }; - static const JSFunctionSpec methods[22]; + static const JSFunctionSpec methods[24]; static const char* const className; static const unsigned classFlags = JSCLASS_HAS_PRIVATE; |