diff options
author | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2017-10-14 21:54:01 -0400 |
---|---|---|
committer | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2017-10-14 21:54:01 -0400 |
commit | 2c5511c8c411c70ac1063d1209764e71f83d0b1a (patch) | |
tree | 17eefbaa6e2a7a0416ae036f26e7822d9cf8d982 /src | |
parent | 7efbb340cd781d097c153c48c64fb442bbe2a22b (diff) | |
download | mongo-2c5511c8c411c70ac1063d1209764e71f83d0b1a.tar.gz |
SERVER-31456 Expose server type on Mongo connection object.
This makes it possible to prevent the mongo shell from gossiping a
clusterTime to stand-alone mongod processes when the mongo shell is also
connected to a replica set or a sharded cluster.
Diffstat (limited to 'src')
-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; |