diff options
author | Bobby Morck <bobby.morck@mongodb.com> | 2021-09-25 17:34:13 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-12-22 04:18:28 +0000 |
commit | c52c37bb2ac332cde41047ce1f8c16447b893361 (patch) | |
tree | a6c9c0ee184352f51e704b5cc7ebcda0600edf81 /src/mongo/s | |
parent | bde95276bd632ad5e54a64dab191a6f5f8812f0b (diff) | |
download | mongo-c52c37bb2ac332cde41047ce1f8c16447b893361.tar.gz |
SERVER-56127 Fixing retryable writes on update and delete commands to not execute more than once
(cherry picked from commit 6d8290297b563121037f8e9a9f2d37ec45ddb4bf)
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/shard_key_pattern.cpp | 14 | ||||
-rw-r--r-- | src/mongo/s/shard_key_pattern.h | 21 | ||||
-rw-r--r-- | src/mongo/s/shard_key_pattern_test.cpp | 104 |
3 files changed, 139 insertions, 0 deletions
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 aed37e46889..4dd79692c71 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(const BSONObj doc) const; diff --git a/src/mongo/s/shard_key_pattern_test.cpp b/src/mongo/s/shard_key_pattern_test.cpp index e2cd0a88a83..fde7ad3a592 100644 --- a/src/mongo/s/shard_key_pattern_test.cpp +++ b/src/mongo/s/shard_key_pattern_test.cpp @@ -54,6 +54,36 @@ protected: OperationContext* const _opCtx{_opCtxHolder.get()}; }; +/** + * 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)); @@ -137,6 +167,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 @@ -228,6 +262,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 |