summaryrefslogtreecommitdiff
path: root/jstests/sharding/transactions_causal_consistency.js
diff options
context:
space:
mode:
authorJack Mulrow <jack.mulrow@mongodb.com>2018-11-01 15:30:59 -0400
committerJack Mulrow <jack.mulrow@mongodb.com>2018-12-12 13:28:21 -0500
commitd2573d47786b035d5bcdeaf30207bbfcd58bf14e (patch)
tree7b7483e49b08550fb374a76d21d853910e403104 /jstests/sharding/transactions_causal_consistency.js
parent4a47c79bcba9fe926b2525a92abd34dbc583e2c7 (diff)
downloadmongo-d2573d47786b035d5bcdeaf30207bbfcd58bf14e.tar.gz
SERVER-37872 Add targeted jstest for sharded transactions with afterClusterTime
Diffstat (limited to 'jstests/sharding/transactions_causal_consistency.js')
-rw-r--r--jstests/sharding/transactions_causal_consistency.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/jstests/sharding/transactions_causal_consistency.js b/jstests/sharding/transactions_causal_consistency.js
new file mode 100644
index 00000000000..88a7762d501
--- /dev/null
+++ b/jstests/sharding/transactions_causal_consistency.js
@@ -0,0 +1,78 @@
+// Verifies basic sharded transaction behavior with causal consistency.
+//
+// @tags: [
+// requires_find_command,
+// requires_sharding,
+// uses_multi_shard_transaction,
+// uses_transactions,
+// ]
+(function() {
+ "use strict";
+
+ const dbName = "test";
+ const collName = "foo";
+ const ns = dbName + "." + collName;
+
+ const st = new ShardingTest({shards: 2, mongos: 2});
+
+ // Set up a sharded collection with 2 chunks, [min, 0) and [0, max), one on each shard, with one
+ // document in each.
+
+ assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
+ st.ensurePrimaryShard(dbName, st.shard0.shardName);
+
+ assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
+ assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
+ assert.commandWorked(
+ st.s.adminCommand({moveChunk: ns, find: {_id: 1}, to: st.shard1.shardName}));
+
+ assert.writeOK(st.s.getDB(dbName)[collName].insert({_id: -1}, {writeConcern: {w: "majority"}}));
+ assert.writeOK(st.s.getDB(dbName)[collName].insert({_id: 1}, {writeConcern: {w: "majority"}}));
+
+ // Verifies transactions using causal consistency read all causally prior operations.
+ function runTest(st, readConcern) {
+ jsTestLog("Testing readConcern: " + tojson(readConcern));
+
+ const session = st.s.startSession({causalConsistency: true});
+ const sessionDB = session.getDatabase(dbName);
+
+ // Insert data to one shard in a causally consistent session.
+ const docToInsert = {_id: 5};
+ assert.commandWorked(sessionDB.runCommand({insert: collName, documents: [docToInsert]}));
+
+ // Through a separate router move the chunk that was inserted to, so the original router is
+ // stale when it starts its transaction.
+ const otherRouter = st.s1;
+ assert.commandWorked(
+ otherRouter.adminCommand({moveChunk: ns, find: docToInsert, to: st.shard0.shardName}));
+
+ session.startTransaction({readConcern: readConcern});
+
+ // The transaction should always see the document written earlier through its session,
+ // regardless of the move.
+ //
+ // Note: until transactions can read from secondaries and/or disabling speculative snapshot
+ // is allowed, read concerns that do not require global snapshots (i.e. local and majority)
+ // will always read the inserted document here because the local snapshot established on
+ // this shard will include all currently applied operations, which must include all earlier
+ // acknowledged writes.
+ assert.docEq(docToInsert,
+ sessionDB[collName].findOne(docToInsert),
+ "sharded transaction with read concern " + tojson(readConcern) +
+ " did not see expected document");
+
+ session.commitTransaction();
+
+ // Clean up for the next iteration.
+ assert.commandWorked(
+ st.s.adminCommand({moveChunk: ns, find: docToInsert, to: st.shard1.shardName}));
+ assert.writeOK(sessionDB[collName].remove(docToInsert));
+ }
+
+ const kAllowedReadConcernLevels = ["local", "majority", "snapshot"];
+ for (let readConcernLevel of kAllowedReadConcernLevels) {
+ runTest(st, {level: readConcernLevel});
+ }
+
+ st.stop();
+})();