(function() {
|
"use strict";
|
|
load("jstests/sharding/libs/create_sharded_collection_util.js");
|
|
const st = new ShardingTest({mongos: 1, config: 1, shards: 2, rs: {nodes: 1}});
|
|
const db = st.s.getDB("test");
|
const collection = db.getCollection("mycoll");
|
CreateShardedCollectionUtil.shardCollectionWithChunks(collection, {x: 1}, [
|
{min: {x: MinKey}, max: {x: 0}, shard: st.shard0.shardName},
|
{min: {x: 0}, max: {x: 10}, shard: st.shard0.shardName},
|
{min: {x: 10}, max: {x: 20}, shard: st.shard1.shardName},
|
{min: {x: 20}, max: {x: MaxKey}, shard: st.shard1.shardName},
|
]);
|
|
assert.commandWorked(collection.insert({_id: 0, x: 5, counter: 0}));
|
|
const session1 = st.s.startSession({causalConsistency: false, retryWrites: false});
|
const sessionCollection1 = session1.getDatabase(db.getName()).getCollection(collection.getName());
|
|
const session2 = st.s.startSession({causalConsistency: false, retryWrites: false});
|
const sessionCollection2 = session2.getDatabase(db.getName()).getCollection(collection.getName());
|
|
// Updates by _id are broadcasted to all shards which own chunks for the collection. Session
|
// information from the retryable write which touched the document isn't migrated when the
|
// document's shard key value is updated. This allows the new owning shard to execute the statement
|
// a second time.
|
const updateCmd = {
|
updates: [
|
{q: {_id: 0}, u: {$inc: {counter: 1}}},
|
{q: {_id: 10000}, u: {$inc: {counter: 1}}},
|
],
|
txnNumber: NumberLong(0),
|
};
|
|
const firstRes = sessionCollection1.runCommand("update", updateCmd);
|
assert.eq({n: firstRes.n, nModified: firstRes.nModified}, {n: 1, nModified: 1});
|
|
session2.startTransaction();
|
assert.commandWorked(sessionCollection2.update({x: 5}, {$set: {x: 25}}));
|
assert.commandWorked(session2.commitTransaction_forTesting());
|
|
const secondRes = sessionCollection1.runCommand("update", updateCmd);
|
print(`secondRes: ${tojsononeline(secondRes)}`);
|
assert.eq(collection.findOne({_id: 0}), {_id: 0, x: 25, counter: 1});
|
|
st.stop();
|
})();
|