summaryrefslogtreecommitdiff
path: root/src/mongo/rpc
diff options
context:
space:
mode:
authorWenbin Zhu <wenbin.zhu@mongodb.com>2021-09-02 02:08:11 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-09-02 02:53:55 +0000
commitb46acdbba8ec51810b6f402dbe18ed7ea98fd13d (patch)
treedad1c8a0b7d5cf2a911ea0b43a9429a061e857f4 /src/mongo/rpc
parentda01d3d80716135c2109bb57319bcee4c0bb1afc (diff)
downloadmongo-b46acdbba8ec51810b6f402dbe18ed7ea98fd13d.tar.gz
SERVER-58988 Avoid sync source selection cycle during primary catchup.
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r--src/mongo/rpc/metadata/oplog_query_metadata.cpp20
-rw-r--r--src/mongo/rpc/metadata/oplog_query_metadata.h12
-rw-r--r--src/mongo/rpc/metadata/oplog_query_metadata_test.cpp18
3 files changed, 38 insertions, 12 deletions
diff --git a/src/mongo/rpc/metadata/oplog_query_metadata.cpp b/src/mongo/rpc/metadata/oplog_query_metadata.cpp
index 65d600dab00..f810662ded7 100644
--- a/src/mongo/rpc/metadata/oplog_query_metadata.cpp
+++ b/src/mongo/rpc/metadata/oplog_query_metadata.cpp
@@ -50,6 +50,7 @@ const char kLastCommittedWallFieldName[] = "lastCommittedWall";
const char kLastOpAppliedFieldName[] = "lastOpApplied";
const char kPrimaryIndexFieldName[] = "primaryIndex";
const char kSyncSourceIndexFieldName[] = "syncSourceIndex";
+const char kSyncSourceHostFieldName[] = "syncSourceHost";
const char kRBIDFieldName[] = "rbid";
} // unnamed namespace
@@ -60,12 +61,14 @@ OplogQueryMetadata::OplogQueryMetadata(OpTimeAndWallTime lastOpCommitted,
OpTime lastOpApplied,
int rbid,
int currentPrimaryIndex,
- int currentSyncSourceIndex)
+ int currentSyncSourceIndex,
+ std::string currentSyncSourceHost)
: _lastOpCommitted(std::move(lastOpCommitted)),
_lastOpApplied(std::move(lastOpApplied)),
_rbid(rbid),
_currentPrimaryIndex(currentPrimaryIndex),
- _currentSyncSourceIndex(currentSyncSourceIndex) {}
+ _currentSyncSourceIndex(currentSyncSourceIndex),
+ _currentSyncSourceHost(currentSyncSourceHost) {}
StatusWith<OplogQueryMetadata> OplogQueryMetadata::readFromMetadata(const BSONObj& metadataObj) {
BSONElement oqMetadataElement;
@@ -86,6 +89,14 @@ StatusWith<OplogQueryMetadata> OplogQueryMetadata::readFromMetadata(const BSONOb
if (!status.isOK())
return status;
+ std::string syncSourceHost;
+ status = bsonExtractStringField(oqMetadataObj, kSyncSourceHostFieldName, &syncSourceHost);
+ // SyncSourceHost might not be set in older versions, checking NoSuchKey error
+ // for backward compatibility.
+ // TODO SERVER-59732: Remove the compatibility check once 6.0 is released.
+ if (!status.isOK() && status.code() != ErrorCodes::NoSuchKey)
+ return status;
+
long long rbid;
status = bsonExtractIntegerField(oqMetadataObj, kRBIDFieldName, &rbid);
if (!status.isOK())
@@ -110,7 +121,8 @@ StatusWith<OplogQueryMetadata> OplogQueryMetadata::readFromMetadata(const BSONOb
if (!status.isOK())
return status;
- return OplogQueryMetadata(lastOpCommitted, lastOpApplied, rbid, primaryIndex, syncSourceIndex);
+ return OplogQueryMetadata(
+ lastOpCommitted, lastOpApplied, rbid, primaryIndex, syncSourceIndex, syncSourceHost);
}
Status OplogQueryMetadata::writeToMetadata(BSONObjBuilder* builder) const {
@@ -121,6 +133,7 @@ Status OplogQueryMetadata::writeToMetadata(BSONObjBuilder* builder) const {
oqMetadataBuilder.append(kRBIDFieldName, _rbid);
oqMetadataBuilder.append(kPrimaryIndexFieldName, _currentPrimaryIndex);
oqMetadataBuilder.append(kSyncSourceIndexFieldName, _currentSyncSourceIndex);
+ oqMetadataBuilder.append(kSyncSourceHostFieldName, _currentSyncSourceHost);
oqMetadataBuilder.doneFast();
return Status::OK();
@@ -131,6 +144,7 @@ std::string OplogQueryMetadata::toString() const {
output << "OplogQueryMetadata";
output << " Primary Index: " << _currentPrimaryIndex;
output << " Sync Source Index: " << _currentSyncSourceIndex;
+ output << " Sync Source Host: " << _currentSyncSourceHost;
output << " RBID: " << _rbid;
output << " Last Op Committed: " << _lastOpCommitted.toString();
output << " Last Op Applied: " << _lastOpApplied.toString();
diff --git a/src/mongo/rpc/metadata/oplog_query_metadata.h b/src/mongo/rpc/metadata/oplog_query_metadata.h
index 205569a4f31..bd08f6a8b39 100644
--- a/src/mongo/rpc/metadata/oplog_query_metadata.h
+++ b/src/mongo/rpc/metadata/oplog_query_metadata.h
@@ -57,7 +57,8 @@ public:
repl::OpTime lastOpApplied,
int rbid,
int currentPrimaryIndex,
- int currentSyncSourceIndex);
+ int currentSyncSourceIndex,
+ std::string currentSyncSourceHost);
/**
* format:
@@ -106,6 +107,14 @@ public:
}
/**
+ * Returns the host of the sync source of the sender.
+ * Returns empty string if it has no sync source.
+ */
+ std::string getSyncSourceHost() const {
+ return _currentSyncSourceHost;
+ }
+
+ /**
* Returns the current rbid of the sender.
*/
int getRBID() const {
@@ -123,6 +132,7 @@ private:
int _rbid = -1;
int _currentPrimaryIndex = kNoPrimary;
int _currentSyncSourceIndex = -1;
+ std::string _currentSyncSourceHost;
};
} // namespace rpc
diff --git a/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp b/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp
index 6d7218e83fc..f0515ac100f 100644
--- a/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp
+++ b/src/mongo/rpc/metadata/oplog_query_metadata_test.cpp
@@ -43,7 +43,7 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) {
OpTime opTime1(Timestamp(1234, 100), 5);
Date_t committedWall = Date_t() + Seconds(opTime1.getSecs());
OpTime opTime2(Timestamp(7777, 101), 6);
- OplogQueryMetadata metadata({opTime1, committedWall}, opTime2, 6, 12, -1);
+ OplogQueryMetadata metadata({opTime1, committedWall}, opTime2, 6, 12, -1, "");
ASSERT_EQ(opTime1, metadata.getLastOpCommitted().opTime);
ASSERT_EQ(committedWall, metadata.getLastOpCommitted().wallTime);
@@ -53,12 +53,14 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) {
BSONObjBuilder builder;
metadata.writeToMetadata(&builder).transitional_ignore();
- BSONObj expectedObj(BSON(
- kOplogQueryMetadataFieldName << BSON(
- "lastOpCommitted" << BSON("ts" << opTime1.getTimestamp() << "t" << opTime1.getTerm())
- << "lastCommittedWall" << committedWall << "lastOpApplied"
- << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm())
- << "rbid" << 6 << "primaryIndex" << 12 << "syncSourceIndex" << -1)));
+ BSONObj expectedObj(
+ BSON(kOplogQueryMetadataFieldName
+ << BSON("lastOpCommitted"
+ << BSON("ts" << opTime1.getTimestamp() << "t" << opTime1.getTerm())
+ << "lastCommittedWall" << committedWall << "lastOpApplied"
+ << BSON("ts" << opTime2.getTimestamp() << "t" << opTime2.getTerm()) << "rbid"
+ << 6 << "primaryIndex" << 12 << "syncSourceIndex" << -1 << "syncSourceHost"
+ << "")));
BSONObj serializedObj = builder.obj();
ASSERT_BSONOBJ_EQ(expectedObj, serializedObj);
@@ -89,7 +91,7 @@ TEST(ReplResponseMetadataTest, OplogQueryMetadataRoundtrip) {
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);
+ OplogQueryMetadata oqm({OpTime(), Date_t()}, OpTime(), 1, currentPrimaryIndex, -1, "");
ASSERT_EQUALS(hasPrimaryIndex, oqm.hasPrimaryIndex());
}
}