summaryrefslogtreecommitdiff
path: root/src/mongo/rpc
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2016-01-26 16:26:15 -0500
committerBenety Goh <benety@mongodb.com>2016-02-05 13:32:33 -0500
commit1ecf7de7e18df42c154c04b1d9310956c91a75b7 (patch)
tree737f52e7e6ef1c3ca98caba26465ff37e22237a7 /src/mongo/rpc
parent102e832f218ecdec47d8eebf4e827072075325d7 (diff)
downloadmongo-1ecf7de7e18df42c154c04b1d9310956c91a75b7.tar.gz
SERVER-22287 added replicaSetId field to replica set configuration and metadata.
This field is generated when a replica set configuration is created from the replSetInitiate command
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata.cpp11
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata.h18
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata_test.cpp23
3 files changed, 44 insertions, 8 deletions
diff --git a/src/mongo/rpc/metadata/repl_set_metadata.cpp b/src/mongo/rpc/metadata/repl_set_metadata.cpp
index 5e9a1e89f77..da9fc744469 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata.cpp
+++ b/src/mongo/rpc/metadata/repl_set_metadata.cpp
@@ -45,6 +45,7 @@ namespace {
const char kLastOpCommittedFieldName[] = "lastOpCommitted";
const char kLastOpVisibleFieldName[] = "lastOpVisible";
const char kConfigVersionFieldName[] = "configVersion";
+const char kReplicaSetIdFieldName[] = "replicaSetId";
const char kPrimaryIndexFieldName[] = "primaryIndex";
const char kSyncSourceIndexFieldName[] = "syncSourceIndex";
const char kTermFieldName[] = "term";
@@ -59,12 +60,14 @@ ReplSetMetadata::ReplSetMetadata(long long term,
OpTime committedOpTime,
OpTime visibleOpTime,
long long configVersion,
+ OID id,
int currentPrimaryIndex,
int currentSyncSourceIndex)
: _lastOpCommitted(std::move(committedOpTime)),
_lastOpVisible(std::move(visibleOpTime)),
_currentTerm(term),
_configVersion(configVersion),
+ _replicaSetId(id),
_currentPrimaryIndex(currentPrimaryIndex),
_currentSyncSourceIndex(currentSyncSourceIndex) {}
@@ -82,6 +85,11 @@ StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& met
if (!status.isOK())
return status;
+ OID id;
+ status = bsonExtractOIDFieldWithDefault(replMetadataObj, kReplicaSetIdFieldName, OID(), &id);
+ if (!status.isOK())
+ return status;
+
long long primaryIndex;
status = bsonExtractIntegerField(replMetadataObj, kPrimaryIndexFieldName, &primaryIndex);
if (!status.isOK())
@@ -108,7 +116,7 @@ StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& met
return status;
return ReplSetMetadata(
- term, lastOpCommitted, lastOpVisible, configVersion, primaryIndex, syncSourceIndex);
+ term, lastOpCommitted, lastOpVisible, configVersion, id, primaryIndex, syncSourceIndex);
}
Status ReplSetMetadata::writeToMetadata(BSONObjBuilder* builder) const {
@@ -117,6 +125,7 @@ Status ReplSetMetadata::writeToMetadata(BSONObjBuilder* builder) const {
_lastOpCommitted.append(&replMetadataBuilder, kLastOpCommittedFieldName);
_lastOpVisible.append(&replMetadataBuilder, kLastOpVisibleFieldName);
replMetadataBuilder.append(kConfigVersionFieldName, _configVersion);
+ replMetadataBuilder.append(kReplicaSetIdFieldName, _replicaSetId);
replMetadataBuilder.append(kPrimaryIndexFieldName, _currentPrimaryIndex);
replMetadataBuilder.append(kSyncSourceIndexFieldName, _currentSyncSourceIndex);
replMetadataBuilder.doneFast();
diff --git a/src/mongo/rpc/metadata/repl_set_metadata.h b/src/mongo/rpc/metadata/repl_set_metadata.h
index 3e80afc1f9b..24022063a04 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata.h
+++ b/src/mongo/rpc/metadata/repl_set_metadata.h
@@ -28,6 +28,7 @@
#pragma once
+#include "mongo/bson/oid.h"
#include "mongo/db/repl/optime.h"
namespace mongo {
@@ -55,6 +56,7 @@ public:
repl::OpTime committedOpTime,
repl::OpTime visibleOpTime,
long long configVersion,
+ OID replicaSetId,
int currentPrimaryIndex,
int currentSyncSourceIndex);
@@ -65,6 +67,7 @@ public:
* lastOpCommitted: {ts: Timestamp(0, 0), term: 0},
* lastOpVisible: {ts: Timestamp(0, 0), term: 0},
* configVersion: 0,
+ * replicaSetId: ObjectId("..."), // Only present in certain versions and above.
* primaryIndex: 0,
* syncSourceIndex: 0
* }
@@ -94,6 +97,20 @@ public:
}
/**
+ * Returns true if the sender has a replica set ID.
+ */
+ bool hasReplicaSetId() const {
+ return _replicaSetId.isSet();
+ }
+
+ /**
+ * Returns the replica set ID of the sender.
+ */
+ OID getReplicaSetId() const {
+ return _replicaSetId;
+ }
+
+ /**
* Returns the index of the current primary from the perspective of the sender.
* Returns kNoPrimary if there is no primary.
*/
@@ -121,6 +138,7 @@ private:
repl::OpTime _lastOpVisible = repl::OpTime(Timestamp(0, 0), repl::OpTime::kUninitializedTerm);
long long _currentTerm = -1;
long long _configVersion = -1;
+ OID _replicaSetId;
int _currentPrimaryIndex = kNoPrimary;
int _currentSyncSourceIndex = -1;
};
diff --git a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
index 68d073be8cb..58b31f63517 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
+++ b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
@@ -36,23 +36,30 @@ namespace {
using repl::OpTime;
+TEST(ReplResponseMetadataTest, ReplicaSetIdNotSet) {
+ ASSERT_FALSE(ReplSetMetadata(3, OpTime(), OpTime(), 6, OID(), 12, -1).hasReplicaSetId());
+}
+
TEST(ReplResponseMetadataTest, Roundtrip) {
OpTime opTime(Timestamp(1234, 100), 5);
OpTime opTime2(Timestamp(7777, 100), 6);
- ReplSetMetadata metadata(3, opTime, opTime2, 6, 12, -1);
+ ReplSetMetadata metadata(3, opTime, opTime2, 6, OID::gen(), 12, -1);
ASSERT_EQ(opTime, metadata.getLastOpCommitted());
ASSERT_EQ(opTime2, metadata.getLastOpVisible());
+ ASSERT_TRUE(metadata.hasReplicaSetId());
BSONObjBuilder builder;
metadata.writeToMetadata(&builder);
- BSONObj expectedObj(BSON(
- kReplSetMetadataFieldName << BSON(
- "term" << 3 << "lastOpCommitted" << BSON("ts" << opTime.getTimestamp() << "t"
- << opTime.getTerm()) << "lastOpVisible"
- << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm())
- << "configVersion" << 6 << "primaryIndex" << 12 << "syncSourceIndex" << -1)));
+ BSONObj expectedObj(
+ BSON(kReplSetMetadataFieldName
+ << BSON("term" << 3 << "lastOpCommitted"
+ << BSON("ts" << opTime.getTimestamp() << "t" << opTime.getTerm())
+ << "lastOpVisible"
+ << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm())
+ << "configVersion" << 6 << "replicaSetId" << metadata.getReplicaSetId()
+ << "primaryIndex" << 12 << "syncSourceIndex" << -1)));
BSONObj serializedObj = builder.obj();
ASSERT_EQ(expectedObj, serializedObj);
@@ -63,6 +70,8 @@ TEST(ReplResponseMetadataTest, Roundtrip) {
const auto& clonedMetadata = cloneStatus.getValue();
ASSERT_EQ(opTime, clonedMetadata.getLastOpCommitted());
ASSERT_EQ(opTime2, clonedMetadata.getLastOpVisible());
+ ASSERT_EQ(metadata.getConfigVersion(), clonedMetadata.getConfigVersion());
+ ASSERT_EQ(metadata.getReplicaSetId(), clonedMetadata.getReplicaSetId());
BSONObjBuilder clonedBuilder;
clonedMetadata.writeToMetadata(&clonedBuilder);