summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorBobby Morck <bobby.morck@mongodb.com>2021-09-25 17:34:13 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-10-08 19:45:19 +0000
commitfedd7fa7eaf29751d3573ff39f18c3f09abbf06c (patch)
tree570b1cdca9fb07785b231557f15c90aa45d61bd6 /src/mongo
parent230477b7740ec1aa111ce5035a5f8f6a1cd2dd7e (diff)
downloadmongo-fedd7fa7eaf29751d3573ff39f18c3f09abbf06c.tar.gz
SERVER-56127 Fixing retryable writes on update and delete commands to not execute more than once
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/repl/oplog_entry.cpp17
-rw-r--r--src/mongo/db/repl/oplog_entry.h6
-rw-r--r--src/mongo/db/s/session_catalog_migration_source.cpp3
-rw-r--r--src/mongo/s/shard_key_pattern.cpp14
-rw-r--r--src/mongo/s/shard_key_pattern.h21
-rw-r--r--src/mongo/s/shard_key_pattern_test.cpp104
6 files changed, 163 insertions, 2 deletions
diff --git a/src/mongo/db/repl/oplog_entry.cpp b/src/mongo/db/repl/oplog_entry.cpp
index 9e15d205d02..3f3c88cd359 100644
--- a/src/mongo/db/repl/oplog_entry.cpp
+++ b/src/mongo/db/repl/oplog_entry.cpp
@@ -369,6 +369,20 @@ bool DurableOplogEntry::isCrudOpType() const {
return isCrudOpType(getOpType());
}
+bool DurableOplogEntry::isUpdateOrDelete() const {
+ auto opType = getOpType();
+ switch (opType) {
+ case OpTypeEnum::kDelete:
+ case OpTypeEnum::kUpdate:
+ return true;
+ case OpTypeEnum::kInsert:
+ case OpTypeEnum::kCommand:
+ case OpTypeEnum::kNoop:
+ return false;
+ }
+ MONGO_UNREACHABLE;
+}
+
bool DurableOplogEntry::shouldPrepare() const {
return getCommandType() == CommandType::kApplyOps &&
getObject()[ApplyOpsCommandInfoBase::kPrepareFieldName].booleanSafe();
@@ -711,6 +725,9 @@ bool OplogEntry::isSingleOplogEntryTransactionWithCommand() const {
bool OplogEntry::isCrudOpType() const {
return _entry.isCrudOpType();
}
+bool OplogEntry::isUpdateOrDelete() const {
+ return _entry.isUpdateOrDelete();
+}
bool OplogEntry::isIndexCommandType() const {
return _entry.isIndexCommandType();
diff --git a/src/mongo/db/repl/oplog_entry.h b/src/mongo/db/repl/oplog_entry.h
index 021261c64d6..c13fd9265df 100644
--- a/src/mongo/db/repl/oplog_entry.h
+++ b/src/mongo/db/repl/oplog_entry.h
@@ -386,6 +386,11 @@ public:
bool isCrudOpType() const;
/**
+ * Returns true if the oplog entry is for an Update or Delete operation.
+ */
+ bool isUpdateOrDelete() const;
+
+ /**
* Returns true if the oplog entry is for a command related to indexes.
* i.e createIndexes, dropIndexes, startIndexBuild, commitIndexBuild, abortIndexBuild.
*/
@@ -554,6 +559,7 @@ public:
bool isSingleOplogEntryTransaction() const;
bool isSingleOplogEntryTransactionWithCommand() const;
bool isCrudOpType() const;
+ bool isUpdateOrDelete() const;
bool isIndexCommandType() const;
bool shouldPrepare() const;
BSONElement getIdElement() const;
diff --git a/src/mongo/db/s/session_catalog_migration_source.cpp b/src/mongo/db/s/session_catalog_migration_source.cpp
index c257fc08404..c33424ee7e9 100644
--- a/src/mongo/db/s/session_catalog_migration_source.cpp
+++ b/src/mongo/db/s/session_catalog_migration_source.cpp
@@ -335,8 +335,7 @@ bool SessionCatalogMigrationSource::_handleWriteHistory(WithLock, OperationConte
}
if (nextOplog->isCrudOpType()) {
- auto shardKey =
- _keyPattern.extractShardKeyFromDoc(nextOplog->getObjectContainingDocumentKey());
+ auto shardKey = _keyPattern.extractShardKeyFromOplogEntry(*nextOplog);
if (!_chunkRange.containsKey(shardKey)) {
continue;
}
diff --git a/src/mongo/s/shard_key_pattern.cpp b/src/mongo/s/shard_key_pattern.cpp
index 27ddb80e6ad..db90c4a53f0 100644
--- a/src/mongo/s/shard_key_pattern.cpp
+++ b/src/mongo/s/shard_key_pattern.cpp
@@ -429,6 +429,20 @@ BSONObj ShardKeyPattern::extractShardKeyFromDocThrows(const BSONObj& doc) const
return shardKey;
}
+BSONObj ShardKeyPattern::extractShardKeyFromOplogEntry(const repl::OplogEntry& entry) const {
+ if (!entry.isCrudOpType()) {
+ return BSONObj();
+ }
+
+ auto objWithDocumentKey = entry.getObjectContainingDocumentKey();
+
+ if (!entry.isUpdateOrDelete()) {
+ return extractShardKeyFromDoc(objWithDocumentKey);
+ }
+
+ return extractShardKeyFromDocumentKey(objWithDocumentKey);
+}
+
BSONObj ShardKeyPattern::emplaceMissingShardKeyValuesForDocument(const BSONObj doc) const {
BSONObjBuilder fullDocBuilder(doc);
for (const auto& skField : _keyPattern.toBSON()) {
diff --git a/src/mongo/s/shard_key_pattern.h b/src/mongo/s/shard_key_pattern.h
index 7f194c8ab1e..d154dfe119b 100644
--- a/src/mongo/s/shard_key_pattern.h
+++ b/src/mongo/s/shard_key_pattern.h
@@ -38,6 +38,7 @@
#include "mongo/db/jsobj.h"
#include "mongo/db/keypattern.h"
#include "mongo/db/query/index_bounds.h"
+#include "mongo/db/repl/oplog_entry.h"
namespace mongo {
@@ -213,6 +214,26 @@ public:
BSONObj extractShardKeyFromDocThrows(const BSONObj& doc) const;
/**
+ * Given an Oplog entry, extracts the shard key corresponding to the key pattern for insert,
+ * update, and delete op types. If the op type is not a CRUD operation, an empty BSONObj()
+ * will be returned.
+ *
+ * For update and delete operations, the Oplog entry will contain an object with the document
+ * key.
+ *
+ * For insert operations, the Oplog entry will contain the original document from which the
+ * document key must be extracted
+ *
+ * Examples:
+ * For KeyPattern {'a.b': 1}
+ * If the oplog entries contains field op='i'
+ * oplog contains: { a : { b : "1" } }
+ * If the oplog entries contains field op='u' or op='d'
+ * oplog contains: { 'a.b': "1" }
+ */
+ BSONObj extractShardKeyFromOplogEntry(const repl::OplogEntry& entry) const;
+
+ /**
* Returns the document with missing shard key values set to null.
*/
BSONObj emplaceMissingShardKeyValuesForDocument(BSONObj doc) const;
diff --git a/src/mongo/s/shard_key_pattern_test.cpp b/src/mongo/s/shard_key_pattern_test.cpp
index c67f9958378..2809ff17d86 100644
--- a/src/mongo/s/shard_key_pattern_test.cpp
+++ b/src/mongo/s/shard_key_pattern_test.cpp
@@ -62,6 +62,36 @@ protected:
OperationContext* _opCtx;
};
+/**
+ * Creates OplogEntry with given field values.
+ */
+repl::OplogEntry makeOplogEntry(repl::OpTime opTime,
+ repl::OpTypeEnum opType,
+ NamespaceString nss,
+ BSONObj oField,
+ boost::optional<BSONObj> o2Field = boost::none) {
+ return {
+ repl::DurableOplogEntry(opTime, // optime
+ boost::none, // hash
+ opType, // opType
+ nss, // namespace
+ boost::none, // uuid
+ boost::none, // fromMigrate
+ repl::OplogEntry::kOplogVersion, // version
+ oField, // o
+ o2Field, // o2
+ {}, // sessionInfo
+ boost::none, // upsert
+ Date_t(), // wall clock time
+ {}, // statement ids
+ boost::none, // optime of previous write within same transaction
+ boost::none, // pre-image optime
+ boost::none, // post-image optime
+ boost::none, // ShardId of resharding recipient
+ boost::none, // _id
+ boost::none)}; // needsRetryImage
+}
+
TEST_F(ShardKeyPatternTest, SingleFieldShardKeyPatternsValidityCheck) {
ShardKeyPattern s1(BSON("a" << 1));
ShardKeyPattern s2(BSON("a" << 1.0f));
@@ -145,6 +175,10 @@ static BSONObj docKey(const ShardKeyPattern& pattern, const BSONObj& doc) {
return pattern.extractShardKeyFromDoc(doc);
}
+static BSONObj docKeyFromOplog(const ShardKeyPattern& pattern, const repl::OplogEntry& entry) {
+ return pattern.extractShardKeyFromOplogEntry(entry);
+}
+
TEST_F(ShardKeyPatternTest, ExtractDocShardKeySingle) {
//
// Single field ShardKeyPatterns
@@ -236,6 +270,76 @@ TEST_F(ShardKeyPatternTest, ExtractDocShardKeyNested) {
ASSERT_BSONOBJ_EQ(docKey(pattern, fromjson("{a:{b:[10, 20]}, c:30}")), BSONObj());
}
+TEST_F(ShardKeyPatternTest, ExtractShardKeyFromOplogUnnested) {
+ //
+ // Unnested ShardKeyPatterns from oplog entries with CRUD operation
+ //
+
+ ShardKeyPattern pattern(BSON("a" << 1));
+ auto deleteOplog = makeOplogEntry(repl::OpTime(Timestamp(50, 10), 1), // optime
+ repl::OpTypeEnum::kDelete, // op type
+ NamespaceString("a"), // namespace
+ BSON("_id" << 1 << "a" << 5)); // o
+ auto insertOplog = makeOplogEntry(repl::OpTime(Timestamp(60, 10), 1), // optime
+ repl::OpTypeEnum::kInsert, // op type
+ NamespaceString("a"), // namespace
+ BSON("_id" << 2 << "a" << 6)); // o
+ auto updateOplog = makeOplogEntry(repl::OpTime(Timestamp(70, 10), 1), // optime
+ repl::OpTypeEnum::kUpdate, // op type
+ NamespaceString("a"), // namespace
+ BSON("_id" << 3), // o
+ BSON("_id" << 3 << "a" << 7)); // o2
+
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, deleteOplog), fromjson("{a: 5}"));
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, insertOplog), fromjson("{a: 6}"));
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, updateOplog), fromjson("{a: 7}"));
+}
+
+TEST_F(ShardKeyPatternTest, ExtractShardKeyFromOplogNested) {
+ //
+ // Nested ShardKeyPatterns from oplog entries with CRUD operation
+ //
+
+ ShardKeyPattern pattern(BSON("a.b" << 1));
+ auto deleteOplog = makeOplogEntry(repl::OpTime(Timestamp(50, 10), 1), // optime
+ repl::OpTypeEnum::kDelete, // op type
+ NamespaceString("a.b"), // namespace
+ BSON("_id" << 1 << "a.b" << 5)); // o
+ auto insertOplog = makeOplogEntry(repl::OpTime(Timestamp(60, 10), 1), // optime
+ repl::OpTypeEnum::kInsert, // op type
+ NamespaceString("a.b"), // namespace
+ BSON("_id" << 2 << "a" << BSON("b" << 6))); // o
+ auto updateOplog = makeOplogEntry(repl::OpTime(Timestamp(70, 10), 1), // optime
+ repl::OpTypeEnum::kUpdate, // op type
+ NamespaceString("a.b"), // namespace
+ BSON("_id" << 3), // o
+ BSON("_id" << 3 << "a.b" << 7)); // o2
+
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, deleteOplog), fromjson("{'a.b': 5}"));
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, insertOplog), fromjson("{'a.b': 6}"));
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, updateOplog), fromjson("{'a.b': 7}"));
+}
+
+TEST_F(ShardKeyPatternTest, ExtractShardKeyFromOplogNonCRUD) {
+ //
+ // Oplogs with non-CRUD op types
+ //
+
+ ShardKeyPattern pattern(BSON("a.b" << 1));
+ auto noopOplog = makeOplogEntry(repl::OpTime(Timestamp(50, 10), 1), // optime
+ repl::OpTypeEnum::kNoop, // op type
+ NamespaceString("a.b"), // namespace
+ BSON("_id" << 1 << "a.b" << 5)); // o
+ auto commandOplog = makeOplogEntry(repl::OpTime(Timestamp(60, 10), 1), // optime
+ repl::OpTypeEnum::kCommand, // op type
+ NamespaceString("a.b"), // namespace
+ BSON("create"
+ << "c")); // o
+
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, noopOplog), BSONObj());
+ ASSERT_BSONOBJ_EQ(docKeyFromOplog(pattern, commandOplog), BSONObj());
+}
+
TEST_F(ShardKeyPatternTest, ExtractDocShardKeyDeepNested) {
//
// Deeply nested ShardKeyPatterns