diff options
author | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2020-03-27 15:31:45 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-03-30 02:21:36 +0000 |
commit | 250cae2fdcd06600435d9f80de79f610e2c84df8 (patch) | |
tree | 55258403f3e84c49413548b9107c7bebcebe754e /src/mongo/rpc | |
parent | ffff3f7fce95d45192c8b6cb65845907d6128922 (diff) | |
download | mongo-250cae2fdcd06600435d9f80de79f610e2c84df8.tar.gz |
SERVER-47096 Add ReplSetMetadata.configTerm
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r-- | src/mongo/rpc/metadata/repl_set_metadata.cpp | 13 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/repl_set_metadata.h | 11 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/repl_set_metadata_test.cpp | 37 |
3 files changed, 44 insertions, 17 deletions
diff --git a/src/mongo/rpc/metadata/repl_set_metadata.cpp b/src/mongo/rpc/metadata/repl_set_metadata.cpp index 9698ded3eb5..2d01a732960 100644 --- a/src/mongo/rpc/metadata/repl_set_metadata.cpp +++ b/src/mongo/rpc/metadata/repl_set_metadata.cpp @@ -49,6 +49,7 @@ const char kLastOpCommittedFieldName[] = "lastOpCommitted"; const char kLastCommittedWallFieldName[] = "lastCommittedWall"; const char kLastOpVisibleFieldName[] = "lastOpVisible"; const char kConfigVersionFieldName[] = "configVersion"; +const char kConfigTermFieldName[] = "configTerm"; const char kReplicaSetIdFieldName[] = "replicaSetId"; const char kPrimaryIndexFieldName[] = "primaryIndex"; const char kSyncSourceIndexFieldName[] = "syncSourceIndex"; @@ -63,6 +64,7 @@ ReplSetMetadata::ReplSetMetadata(long long term, OpTimeAndWallTime committedOpTime, OpTime visibleOpTime, long long configVersion, + long long configTerm, OID id, int currentPrimaryIndex, int currentSyncSourceIndex, @@ -71,6 +73,7 @@ ReplSetMetadata::ReplSetMetadata(long long term, _lastOpVisible(std::move(visibleOpTime)), _currentTerm(term), _configVersion(configVersion), + _configTerm(configTerm), _replicaSetId(id), _currentPrimaryIndex(currentPrimaryIndex), _currentSyncSourceIndex(currentSyncSourceIndex), @@ -90,6 +93,13 @@ StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& met if (!status.isOK()) return status; + // TODO(SERVER-47157): require configTerm. + long long configTerm; + status = bsonExtractIntegerFieldWithDefault( + replMetadataObj, kConfigTermFieldName, OpTime::kUninitializedTerm, &configTerm); + if (!status.isOK()) + return status; + OID id; status = bsonExtractOIDFieldWithDefault(replMetadataObj, kReplicaSetIdFieldName, OID(), &id); if (!status.isOK()) @@ -149,6 +159,7 @@ StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& met lastOpCommitted, lastOpVisible, configVersion, + configTerm, id, primaryIndex, syncSourceIndex, @@ -162,6 +173,7 @@ Status ReplSetMetadata::writeToMetadata(BSONObjBuilder* builder) const { replMetadataBuilder.appendDate(kLastCommittedWallFieldName, _lastOpCommitted.wallTime); _lastOpVisible.append(&replMetadataBuilder, kLastOpVisibleFieldName); replMetadataBuilder.append(kConfigVersionFieldName, _configVersion); + replMetadataBuilder.append(kConfigTermFieldName, _configTerm); replMetadataBuilder.append(kReplicaSetIdFieldName, _replicaSetId); replMetadataBuilder.append(kPrimaryIndexFieldName, _currentPrimaryIndex); replMetadataBuilder.append(kSyncSourceIndexFieldName, _currentSyncSourceIndex); @@ -175,6 +187,7 @@ std::string ReplSetMetadata::toString() const { str::stream output; output << "ReplSetMetadata"; output << " Config Version: " << _configVersion; + output << " Config Term: " << _configTerm; output << " Replicaset ID: " << _replicaSetId; output << " Term: " << _currentTerm; output << " Primary Index: " << _currentPrimaryIndex; diff --git a/src/mongo/rpc/metadata/repl_set_metadata.h b/src/mongo/rpc/metadata/repl_set_metadata.h index 02dc867701e..1a34493d967 100644 --- a/src/mongo/rpc/metadata/repl_set_metadata.h +++ b/src/mongo/rpc/metadata/repl_set_metadata.h @@ -58,7 +58,8 @@ public: repl::OpTimeAndWallTime committedOpTime, repl::OpTime visibleOpTime, long long configVersion, - OID replicaSetId, + long long configTerm, + OID id, int currentPrimaryIndex, int currentSyncSourceIndex, boost::optional<bool> isPrimary); @@ -101,6 +102,13 @@ public: } /** + * Returns the ReplSetConfig term number of the sender. + */ + long long getConfigTerm() const { + return _configTerm; + } + + /** * Returns true if the sender has a replica set ID. */ bool hasReplicaSetId() const { @@ -157,6 +165,7 @@ private: repl::OpTime _lastOpVisible; long long _currentTerm = -1; long long _configVersion = -1; + long long _configTerm = repl::OpTime::kUninitializedTerm; OID _replicaSetId; // TODO(SERVER-47125): remove this member variable post-4.4. int _currentPrimaryIndex = kNoPrimary; diff --git a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp index ca89b0f7e64..7aa199fa046 100644 --- a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp +++ b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp @@ -44,10 +44,10 @@ static const OpTime opTime(Timestamp(1234, 100), 5); static const OpTime opTime2(Timestamp(7777, 100), 6); static const Date_t committedWallTime = Date_t() + Seconds(opTime.getSecs()); static const ReplSetMetadata metadata( - 3, {opTime, committedWallTime}, opTime2, 6, OID("abcdefabcdefabcdefabcdef"), 12, -1, false); + 3, {opTime, committedWallTime}, opTime2, 6, 0, OID("abcdefabcdefabcdefabcdef"), 12, -1, false); TEST(ReplResponseMetadataTest, ReplicaSetIdNotSet) { - ASSERT_FALSE(ReplSetMetadata(3, OpTimeAndWallTime(), OpTime(), 6, OID(), 12, -1, false) + ASSERT_FALSE(ReplSetMetadata(3, OpTimeAndWallTime(), OpTime(), 6, 0, OID(), 12, -1, false) .hasReplicaSetId()); } @@ -60,14 +60,15 @@ TEST(ReplResponseMetadataTest, Roundtrip) { BSONObjBuilder builder; metadata.writeToMetadata(&builder).transitional_ignore(); - BSONObj expectedObj(BSON( - kReplSetMetadataFieldName << BSON( - "term" << 3 << "lastOpCommitted" - << BSON("ts" << opTime.getTimestamp() << "t" << opTime.getTerm()) - << "lastCommittedWall" << committedWallTime << "lastOpVisible" - << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm()) - << "configVersion" << 6 << "replicaSetId" << metadata.getReplicaSetId() - << "primaryIndex" << 12 << "syncSourceIndex" << -1 << "isPrimary" << false))); + BSONObj expectedObj( + BSON(kReplSetMetadataFieldName + << BSON("term" << 3 << "lastOpCommitted" + << BSON("ts" << opTime.getTimestamp() << "t" << opTime.getTerm()) + << "lastCommittedWall" << committedWallTime << "lastOpVisible" + << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm()) + << "configVersion" << 6 << "configTerm" << 0 << "replicaSetId" + << metadata.getReplicaSetId() << "primaryIndex" << 12 + << "syncSourceIndex" << -1 << "isPrimary" << false))); BSONObj serializedObj = builder.obj(); ASSERT_BSONOBJ_EQ(expectedObj, serializedObj); @@ -80,6 +81,7 @@ TEST(ReplResponseMetadataTest, Roundtrip) { ASSERT_EQ(opTime2, clonedMetadata.getLastOpVisible()); ASSERT_EQ(committedWallTime, clonedMetadata.getLastOpCommitted().wallTime); ASSERT_EQ(metadata.getConfigVersion(), clonedMetadata.getConfigVersion()); + ASSERT_EQ(metadata.getConfigTerm(), clonedMetadata.getConfigTerm()); ASSERT_EQ(metadata.getReplicaSetId(), clonedMetadata.getReplicaSetId()); BSONObjBuilder clonedBuilder; @@ -90,22 +92,24 @@ TEST(ReplResponseMetadataTest, Roundtrip) { } TEST(ReplResponseMetadataTest, MetadataCanBeConstructedWhenMissingOplogQueryMetadataFields) { - BSONObj obj(BSON(kReplSetMetadataFieldName - << BSON("term" << 3 << "configVersion" << 6 << "replicaSetId" - << metadata.getReplicaSetId() << "lastCommittedWall" - << committedWallTime << "isPrimary" << false))); + BSONObj obj(BSON(kReplSetMetadataFieldName << BSON( + "term" << 3 << "configVersion" << 6 << "configTerm" << 2 << "replicaSetId" + << metadata.getReplicaSetId() << "lastCommittedWall" + << committedWallTime << "isPrimary" << false))); auto status = ReplSetMetadata::readFromMetadata(obj); ASSERT_OK(status.getStatus()); const auto& metadata = status.getValue(); ASSERT_EQ(metadata.getConfigVersion(), 6); + ASSERT_EQ(metadata.getConfigTerm(), 2); ASSERT_EQ(metadata.getReplicaSetId(), metadata.getReplicaSetId()); ASSERT_EQ(metadata.getTerm(), 3); } -TEST(ReplResponseMetadataTest, MetadataCanBeConstructedWhenMissingIsPrimary) { - // TODO(SERVER-47125): delete this test in 4.6 when we can rely on the isPrimary field. +TEST(ReplResponseMetadataTest, MetadataCanBeConstructedFrom42) { + // TODO(SERVER-47125): delete this test in 4.6 when we can rely on the isPrimary and configTerm + // fields. BSONObj obj( BSON(kReplSetMetadataFieldName << BSON("term" << 3 << "lastOpCommitted" @@ -120,6 +124,7 @@ TEST(ReplResponseMetadataTest, MetadataCanBeConstructedWhenMissingIsPrimary) { const auto& metadata = status.getValue(); ASSERT_FALSE(metadata.getIsPrimary().is_initialized()); + ASSERT_EQUALS(metadata.getConfigTerm(), -1); } } // unnamed namespace |