diff options
author | Yuhong Zhang <danielzhangyh@gmail.com> | 2021-05-04 15:31:21 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-05-11 17:01:36 +0000 |
commit | a090d9887e0c13128d058bd1c7faf0d39686b5aa (patch) | |
tree | f3172775c1aa593b81739c27613cb8d484989abb /jstests/noPassthrough/timeseries_retryable_writes_restart.js | |
parent | 25aed16c97b02f7234e20f2284803f99a5fd9571 (diff) | |
download | mongo-a090d9887e0c13128d058bd1c7faf0d39686b5aa.tar.gz |
SERVER-56370 Correctly set prevOpTime of time-series oplog entries by a batched retryable write inserts with {ordered: true}
Diffstat (limited to 'jstests/noPassthrough/timeseries_retryable_writes_restart.js')
-rw-r--r-- | jstests/noPassthrough/timeseries_retryable_writes_restart.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/jstests/noPassthrough/timeseries_retryable_writes_restart.js b/jstests/noPassthrough/timeseries_retryable_writes_restart.js new file mode 100644 index 00000000000..ecaf97725d7 --- /dev/null +++ b/jstests/noPassthrough/timeseries_retryable_writes_restart.js @@ -0,0 +1,97 @@ +/** + * Tests time-series retryable writes oplog entries are correctly chained together so that a retry + * after restarting the server doesn't perform a write that was already executed. + * + * @tags: [ + * requires_replication, + * requires_persistence, + * ] + */ +(function() { +"use strict"; + +load("jstests/core/timeseries/libs/timeseries.js"); + +function testRetryableRestart(ordered) { + const replTest = new ReplSetTest({nodes: 1}); + replTest.startSet(); + replTest.initiate(); + + const primary = replTest.getPrimary(); + + if (!TimeseriesTest.timeseriesCollectionsEnabled(primary)) { + jsTestLog("Skipping test because the time-series collection feature flag is disabled"); + replTest.stopSet(); + return; + } + + const testDB = primary.startSession({retryWrites: true}).getDatabase("test"); + const coll = testDB[jsTestName()]; + + assert.commandWorked(testDB.createCollection( + coll.getName(), {timeseries: {timeField: "time", metaField: "meta"}})); + + function setupRetryableWritesForCollection(collName) { + jsTestLog("Setting up the test collection"); + assert.commandWorked(coll.insert( + [ + {time: ISODate(), x: 0, meta: 0}, + {time: ISODate(), x: 1, meta: 0}, + {time: ISODate(), x: 0, meta: 1}, + {time: ISODate(), x: 1, meta: 1}, + ], + {writeConcern: {w: "majority"}})); + + const insertTag = "retryable insert " + collName; + const updateTag = "retryable update " + collName; + return { + collName: collName, + insertTag: insertTag, + updateTag: updateTag, + retryableInsertCommand: { + insert: collName, + documents: [ + // Batched inserts resulting in "inserts". + {x: 0, time: ISODate(), tag: insertTag, meta: 2}, + {x: 1, time: ISODate(), tag: insertTag, meta: 2}, + {x: 0, time: ISODate(), tag: insertTag, meta: 3}, + {x: 1, time: ISODate(), tag: insertTag, meta: 3}, + // Batched inserts resulting in "updates". + {x: 2, time: ISODate(), tag: updateTag, meta: 0}, + {x: 3, time: ISODate(), tag: updateTag, meta: 0}, + {x: 2, time: ISODate(), tag: updateTag, meta: 1}, + {x: 3, time: ISODate(), tag: updateTag, meta: 1}, + ], + txnNumber: NumberLong(0), + lsid: {id: UUID()}, + ordered: ordered, + }, + }; + } + + function testRetryableWrites(writes) { + const kCollName = writes.collName; + jsTestLog("Testing retryable inserts"); + assert.commandWorked(testDB.runCommand(writes.retryableInsertCommand)); + // If retryable inserts don't work, we will see 8 here. + assert.eq(4, testDB[kCollName].find({tag: writes.insertTag}).itcount()); + assert.eq(4, testDB[kCollName].find({tag: writes.updateTag}).itcount()); + } + + const retryableWrites = setupRetryableWritesForCollection(coll.getName()); + jsTestLog("Run retryable writes"); + assert.commandWorked(testDB.runCommand(retryableWrites.retryableInsertCommand)); + + jsTestLog("Restarting the server to reconstruct retryable writes info"); + replTest.restart(primary); + // Forces to block until the primary becomes writable. + replTest.getPrimary(); + + testRetryableWrites(retryableWrites); + + replTest.stopSet(); +} + +testRetryableRestart(true); +testRetryableRestart(false); +})(); |