jstests/sharding/transactions_causal_consistency.js
// Verifies transactions using causal consistency read all causally prior operations.
Verifies transactions using causal consistency read all causally prior operations.
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);
    const docToInsert = {_id: 5};
    assert.commandWorked(sessionDB.runCommand({insert: collName, documents: [docToInsert]}));
    const otherRouter = st.s1;
    assert.commandWorked(
        otherRouter.adminCommand({moveChunk: ns, find: docToInsert, to: st.shard0.shardName}));
    session.startTransaction({readConcern: readConcern});
     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");
    assert.commandWorked(session.commitTransaction_forTesting());
     Clean up for the next iteration.
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: docToInsert, to: st.shard1.shardName}));
    assert.commandWorked(sessionDB[collName].remove(docToInsert));
}
function runTest ( st , readConcern ) { jsTestLog ( "Testing readConcern: " + tojson ( readConcern ) ) ; const session = st . s . startSession ( { causalConsistency : true } ) ; const sessionDB = session . getDatabase ( dbName ) ; const docToInsert = { _id : 5 } ; assert . commandWorked ( sessionDB . runCommand ( { insert : collName , documents : [ docToInsert ] } ) ) ; const otherRouter = st . s1 ; assert . commandWorked ( otherRouter . adminCommand ( { moveChunk : ns , find : docToInsert , to : st . shard0 . shardName } ) ) ; session . startTransaction ( { readConcern : readConcern } ) ; assert . docEq ( docToInsert , sessionDB [ collName ] . findOne ( docToInsert ) , "sharded transaction with read concern " + tojson ( readConcern ) + " did not see expected document" ) ; assert . commandWorked ( session . commitTransaction_forTesting ( ) ) ; assert . commandWorked ( st . s . adminCommand ( { moveChunk : ns , find : docToInsert , to : st . shard1 . shardName } ) ) ; assert . commandWorked ( sessionDB [ collName ] . remove ( docToInsert ) ) ; }
Verifies transactions using causal consistency read all causally prior operations.
Verifies transactions using causal consistency read all causally prior operations .
// Insert data to one shard in a causally consistent session. // Through a separate router move the chunk that was inserted to, so the original router is // stale when it starts its transaction. // 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. // Clean up for the next iteration.