summaryrefslogtreecommitdiff
path: root/src/mongo/rpc
diff options
context:
space:
mode:
authormatt dannenberg <matt.dannenberg@10gen.com>2015-08-07 06:36:44 -0400
committermatt dannenberg <matt.dannenberg@10gen.com>2015-08-12 04:22:28 -0400
commit86a3e6352eb27fd2e6115299bcec5103a830fe36 (patch)
tree7f7f42491808e8c487e47a710deb445448d18a58 /src/mongo/rpc
parent7bb09c0377f5160857617c38ab07955f8f4b03f6 (diff)
downloadmongo-86a3e6352eb27fd2e6115299bcec5103a830fe36.tar.gz
SERVER-19554 merge ReplSetMetadata and ReplicationMetadata
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r--src/mongo/rpc/metadata.cpp2
-rw-r--r--src/mongo/rpc/metadata.h2
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata.cpp120
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata.h55
-rw-r--r--src/mongo/rpc/metadata/repl_set_metadata_test.cpp19
5 files changed, 124 insertions, 74 deletions
diff --git a/src/mongo/rpc/metadata.cpp b/src/mongo/rpc/metadata.cpp
index 74d511d5d4e..30b435e2872 100644
--- a/src/mongo/rpc/metadata.cpp
+++ b/src/mongo/rpc/metadata.cpp
@@ -39,8 +39,6 @@
namespace mongo {
namespace rpc {
-const char kReplicationMetadataFieldName[] = "$replData";
-
BSONObj makeEmptyMetadata() {
return BSONObj();
}
diff --git a/src/mongo/rpc/metadata.h b/src/mongo/rpc/metadata.h
index 04cc1606d3e..c2160516d4c 100644
--- a/src/mongo/rpc/metadata.h
+++ b/src/mongo/rpc/metadata.h
@@ -57,8 +57,6 @@ class OperationContext;
*/
namespace rpc {
-extern const char kReplicationMetadataFieldName[];
-
/**
* Returns an empty metadata object.
*/
diff --git a/src/mongo/rpc/metadata/repl_set_metadata.cpp b/src/mongo/rpc/metadata/repl_set_metadata.cpp
index d275dc833da..66780b50633 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata.cpp
+++ b/src/mongo/rpc/metadata/repl_set_metadata.cpp
@@ -28,6 +28,7 @@
#include "mongo/rpc/metadata/repl_set_metadata.h"
+#include "mongo/bson/util/bson_check.h"
#include "mongo/bson/util/bson_extract.h"
#include "mongo/db/jsobj.h"
#include "mongo/rpc/metadata.h"
@@ -37,81 +38,88 @@ namespace rpc {
using repl::OpTime;
+const char kReplSetMetadataFieldName[] = "$replData";
+
namespace {
-const char kTermField[] = "term";
-const char kLastCommittedTSField[] = "lastOpCommittedTimestamp";
-const char kLastCommittedTermField[] = "lastOpCommittedTerm";
-const char kLastCommittedConfigVersionField[] = "configVersion";
-const char kLastCommittedPrimaryIndexField[] = "primaryIndex";
+const char kLastOpCommittedFieldName[] = "lastOpCommitted";
+const char kLastOpVisibleFieldName[] = "lastOpVisible";
+const char kConfigVersionFieldName[] = "configVersion";
+const char kPrimaryIndexFieldName[] = "primaryIndex";
+const char kTimestampFieldName[] = "ts";
+const char kTermFieldName[] = "term";
} // unnamed namespace
-ReplSetMetadata::ReplSetMetadata() = default;
-
ReplSetMetadata::ReplSetMetadata(long long term,
OpTime committedOpTime,
+ OpTime visibleOpTime,
long long configVersion,
int currentPrimaryIndex)
- : _currentTerm(term),
- _committedOpTime(std::move(committedOpTime)),
+ : _lastOpCommitted(std::move(committedOpTime)),
+ _lastOpVisible(std::move(visibleOpTime)),
+ _currentTerm(term),
_configVersion(configVersion),
_currentPrimaryIndex(currentPrimaryIndex) {}
-StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& doc) {
- long long term = 0;
- auto termStatus = bsonExtractIntegerField(doc, kTermField, &term);
-
- if (!termStatus.isOK()) {
- return termStatus;
- }
-
- Timestamp timestamp;
- auto timestampStatus = bsonExtractTimestampField(doc, kLastCommittedTSField, &timestamp);
-
- if (!timestampStatus.isOK()) {
- return timestampStatus;
- }
-
- long long termNumber = 0;
- auto commtedTermStatus = bsonExtractIntegerField(doc, kLastCommittedTermField, &termNumber);
-
- if (!commtedTermStatus.isOK()) {
- return commtedTermStatus;
- }
-
- long long configVersion = 0;
- auto configVersionStatus =
- bsonExtractIntegerField(doc, kLastCommittedConfigVersionField, &configVersion);
+StatusWith<ReplSetMetadata> ReplSetMetadata::readFromMetadata(const BSONObj& metadataObj) {
+ BSONElement replMetadataElement;
+
+ Status status = bsonExtractTypedField(
+ metadataObj, rpc::kReplSetMetadataFieldName, Object, &replMetadataElement);
+ if (!status.isOK())
+ return status;
+ BSONObj replMetadataObj = replMetadataElement.Obj();
+
+ long long configVersion;
+ status = bsonExtractIntegerField(replMetadataObj, kConfigVersionFieldName, &configVersion);
+ if (!status.isOK())
+ return status;
+
+ long long primaryIndex;
+ status = bsonExtractIntegerField(replMetadataObj, kPrimaryIndexFieldName, &primaryIndex);
+ if (!status.isOK())
+ return status;
+
+ long long term;
+ status = bsonExtractIntegerField(replMetadataObj, kTermFieldName, &term);
+ if (!status.isOK())
+ return status;
+
+ repl::OpTime lastOpCommitted;
+ status = bsonExtractOpTimeField(replMetadataObj, kLastOpCommittedFieldName, &lastOpCommitted);
+ if (!status.isOK())
+ return status;
+
+ repl::OpTime lastOpVisible;
+ status = bsonExtractOpTimeField(replMetadataObj, kLastOpVisibleFieldName, &lastOpVisible);
+ if (!status.isOK())
+ return status;
+
+ return ReplSetMetadata(term, lastOpCommitted, lastOpVisible, configVersion, primaryIndex);
+}
- if (!configVersionStatus.isOK()) {
- return configVersionStatus;
- }
+Status ReplSetMetadata::writeToMetadata(BSONObjBuilder* builder) const {
+ BSONObjBuilder replMetadataBuilder(builder->subobjStart(kReplSetMetadataFieldName));
+ replMetadataBuilder.append(kTermFieldName, _currentTerm);
- long long primaryIndex = 0;
- auto primaryIndexStatus =
- bsonExtractIntegerField(doc, kLastCommittedPrimaryIndexField, &primaryIndex);
+ BSONObjBuilder lastOpCommittedBuilder(
+ replMetadataBuilder.subobjStart(kLastOpCommittedFieldName));
+ lastOpCommittedBuilder.append(kTimestampFieldName, _lastOpCommitted.getTimestamp());
+ lastOpCommittedBuilder.append(kTermFieldName, _lastOpCommitted.getTerm());
+ lastOpCommittedBuilder.doneFast();
- if (!primaryIndexStatus.isOK()) {
- return primaryIndexStatus;
- }
+ BSONObjBuilder lastOpVisibleBuilder(replMetadataBuilder.subobjStart(kLastOpVisibleFieldName));
+ lastOpVisibleBuilder.append(kTimestampFieldName, _lastOpVisible.getTimestamp());
+ lastOpVisibleBuilder.append(kTermFieldName, _lastOpVisible.getTerm());
+ lastOpVisibleBuilder.doneFast();
- return ReplSetMetadata(term, OpTime(timestamp, termNumber), configVersion, primaryIndex);
-}
-
-Status ReplSetMetadata::writeToMetadata(BSONObjBuilder* builder) const {
- builder->append(kTermField, _currentTerm);
- builder->append(kLastCommittedTSField, _committedOpTime.getTimestamp());
- builder->append(kLastCommittedTermField, _committedOpTime.getTerm());
- builder->append(kLastCommittedConfigVersionField, _configVersion);
- builder->append(kLastCommittedPrimaryIndexField, _currentPrimaryIndex);
+ replMetadataBuilder.append(kConfigVersionFieldName, _configVersion);
+ replMetadataBuilder.append(kPrimaryIndexFieldName, _currentPrimaryIndex);
+ replMetadataBuilder.doneFast();
return Status::OK();
}
-const OpTime& ReplSetMetadata::getLastCommittedOptime() const {
- return _committedOpTime;
-}
-
} // namespace rpc
} // namespace mongo
diff --git a/src/mongo/rpc/metadata/repl_set_metadata.h b/src/mongo/rpc/metadata/repl_set_metadata.h
index bf34efb9dc7..c96cd14487a 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata.h
+++ b/src/mongo/rpc/metadata/repl_set_metadata.h
@@ -37,14 +37,17 @@ class BSONObjBuilder;
namespace rpc {
+extern const char kReplSetMetadataFieldName[];
+
/**
* Represents the metadata information for $replData.
*/
class ReplSetMetadata {
public:
- ReplSetMetadata();
+ ReplSetMetadata() = default;
ReplSetMetadata(long long term,
repl::OpTime committedOpTime,
+ repl::OpTime visibleOpTime,
long long configVersion,
int currentPrimaryIndex);
@@ -52,8 +55,8 @@ public:
* format:
* {
* term: 0,
- * lastOpCommittedTimestamp: 0,
- * lastOpCommittedTerm: 0,
+ * lastOpCommitted: {ts: Timestamp(0, 0), term: 0}
+ * lastOpVisible: {ts: Timestamp(0, 0), term: 0}
* configVersion: 0,
* primaryIndex: 0
* }
@@ -61,13 +64,49 @@ public:
static StatusWith<ReplSetMetadata> readFromMetadata(const BSONObj& doc);
Status writeToMetadata(BSONObjBuilder* builder) const;
- const repl::OpTime& getLastCommittedOptime() const;
+ /**
+ * Returns the OpTime of the most recent operation with which the client intereacted.
+ */
+ repl::OpTime getLastOpVisible() const {
+ return _lastOpVisible;
+ }
+
+ /**
+ * Returns the OpTime of the most recently committed op of which the sender was aware.
+ */
+ repl::OpTime getLastOpCommitted() const {
+ return _lastOpCommitted;
+ }
+
+ /**
+ * Returns the ReplSetConfig version number of the sender.
+ */
+ long long getConfigVersion() const {
+ return _configVersion;
+ }
+
+ /**
+ * Returns the index of the current primary from the perspective of the sender.
+ */
+ long long getPrimaryIndex() const {
+ return _currentPrimaryIndex;
+ }
+
+ /**
+ * Returns the current term from the perspective of the sender.
+ */
+ long long getTerm() const {
+ return _currentTerm;
+ }
private:
- long long _currentTerm = 0;
- repl::OpTime _committedOpTime;
- long long _configVersion = 0;
- int _currentPrimaryIndex = 0;
+ repl::OpTime _lastOpCommitted =
+ repl::OpTime(Timestamp(0, 0), repl::OpTime::kProtocolVersionV0Term);
+ repl::OpTime _lastOpVisible =
+ repl::OpTime(Timestamp(0, 0), repl::OpTime::kProtocolVersionV0Term);
+ long long _currentTerm = -1;
+ long long _configVersion = -1;
+ int _currentPrimaryIndex = -1;
};
} // namespace rpc
diff --git a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
index 90e197978fc..82bff1f0209 100644
--- a/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
+++ b/src/mongo/rpc/metadata/repl_set_metadata_test.cpp
@@ -38,16 +38,22 @@ using repl::OpTime;
TEST(ReplResponseMetadataTest, Roundtrip) {
OpTime opTime(Timestamp(1234, 100), 5);
- ReplSetMetadata metadata(3, opTime, 6, 12);
+ OpTime opTime2(Timestamp(7777, 100), 6);
+ ReplSetMetadata metadata(3, opTime, opTime2, 6, 12);
- ASSERT_EQ(opTime, metadata.getLastCommittedOptime());
+ ASSERT_EQ(opTime, metadata.getLastOpCommitted());
+ ASSERT_EQ(opTime2, metadata.getLastOpVisible());
BSONObjBuilder builder;
metadata.writeToMetadata(&builder);
- BSONObj expectedObj(BSON("term" << 3 << "lastOpCommittedTimestamp" << opTime.getTimestamp()
- << "lastOpCommittedTerm" << opTime.getTerm() << "configVersion"
- << 6 << "primaryIndex" << 12));
+ BSONObj expectedObj(
+ BSON(kReplSetMetadataFieldName
+ << BSON("term" << 3 << "lastOpCommitted"
+ << BSON("ts" << opTime.getTimestamp() << "term" << opTime.getTerm())
+ << "lastOpVisible"
+ << BSON("ts" << opTime2.getTimestamp() << "term" << opTime2.getTerm())
+ << "configVersion" << 6 << "primaryIndex" << 12)));
BSONObj serializedObj = builder.obj();
ASSERT_EQ(expectedObj, serializedObj);
@@ -56,7 +62,8 @@ TEST(ReplResponseMetadataTest, Roundtrip) {
ASSERT_OK(cloneStatus.getStatus());
const auto& clonedMetadata = cloneStatus.getValue();
- ASSERT_EQ(opTime, clonedMetadata.getLastCommittedOptime());
+ ASSERT_EQ(opTime, clonedMetadata.getLastOpCommitted());
+ ASSERT_EQ(opTime2, clonedMetadata.getLastOpVisible());
BSONObjBuilder clonedBuilder;
clonedMetadata.writeToMetadata(&clonedBuilder);