diff options
author | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2020-04-16 11:25:57 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-04-23 15:13:43 +0000 |
commit | 33adeafe0448512e460b372baf621c8f94d5eb09 (patch) | |
tree | 47ed8bd8f6c76c47c5e8073dfa5c3088883a7fe5 /src/mongo/rpc | |
parent | 906e1a72f98d42106c02914bf9cc1dd3101483ac (diff) | |
download | mongo-33adeafe0448512e460b372baf621c8f94d5eb09.tar.gz |
SERVER-47125 Don't trust OplogQueryMetadata.primaryIndex
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r-- | src/mongo/rpc/metadata/oplog_query_metadata.h | 10 | ||||
-rw-r--r-- | src/mongo/rpc/metadata/oplog_query_metadata_test.cpp | 11 |
2 files changed, 16 insertions, 5 deletions
diff --git a/src/mongo/rpc/metadata/oplog_query_metadata.h b/src/mongo/rpc/metadata/oplog_query_metadata.h index 92ce4e24827..205569a4f31 100644 --- a/src/mongo/rpc/metadata/oplog_query_metadata.h +++ b/src/mongo/rpc/metadata/oplog_query_metadata.h @@ -88,11 +88,13 @@ public: } /** - * Returns the index of the current primary from the perspective of the sender. - * Returns kNoPrimary if there is no primary. + * True if the sender thinks there is a primary. + * + * Note: the $oplogQueryData's primaryIndex isn't safe to use, we don't know which config + * version it's from. All we can safely say is whether the sender thinks there's a primary. */ - int getPrimaryIndex() const { - return _currentPrimaryIndex; + bool hasPrimaryIndex() const { + return _currentPrimaryIndex != kNoPrimary; } /** diff --git a/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp b/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp index bb267ccac59..6d7218e83fc 100644 --- a/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp +++ b/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp @@ -48,6 +48,7 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) { ASSERT_EQ(opTime1, metadata.getLastOpCommitted().opTime); ASSERT_EQ(committedWall, metadata.getLastOpCommitted().wallTime); ASSERT_EQ(opTime2, metadata.getLastOpApplied()); + ASSERT_TRUE(metadata.hasPrimaryIndex()); BSONObjBuilder builder; metadata.writeToMetadata(&builder).transitional_ignore(); @@ -75,8 +76,8 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) { ASSERT_EQ(opTime2, clonedMetadata.getLastOpApplied()); ASSERT_EQ(committedWall, clonedMetadata.getLastOpCommitted().wallTime); ASSERT_EQ(metadata.getRBID(), clonedMetadata.getRBID()); - ASSERT_EQ(metadata.getPrimaryIndex(), clonedMetadata.getPrimaryIndex()); ASSERT_EQ(metadata.getSyncSourceIndex(), clonedMetadata.getSyncSourceIndex()); + ASSERT_TRUE(clonedMetadata.hasPrimaryIndex()); BSONObjBuilder clonedBuilder; clonedMetadata.writeToMetadata(&clonedBuilder).transitional_ignore(); @@ -85,6 +86,14 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) { ASSERT_BSONOBJ_EQ(expectedObj, clonedSerializedObj); } +TEST(ReplResponseMetadataTest, OplogQueryMetadataHasPrimaryIndex) { + for (auto [currentPrimaryIndex, hasPrimaryIndex] : + std::vector<std::pair<int, bool>>{{-1, false}, {0, true}, {1, true}}) { + OplogQueryMetadata oqm({OpTime(), Date_t()}, OpTime(), 1, currentPrimaryIndex, -1); + ASSERT_EQUALS(hasPrimaryIndex, oqm.hasPrimaryIndex()); + } +} + } // unnamed namespace } // namespace rpc } // namespace mongo |