summaryrefslogtreecommitdiff
path: root/jstests/sharding/prepared_txn_metadata_refresh.js
blob: de669e8a04dbaa82573950436ef260ac487fdb34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * Test to make sure that transactions doesn't block shard version metadata refresh.
 * Test relies on the fact that destination shard does not update it's shard version after a
 * migration when doNotRefreshRecipientAfterCommit failpoint is ON.
 *
 * @tags: [uses_transactions, uses_multi_shard_transaction]
 */

(function() {
"use strict";

load('./jstests/libs/chunk_manipulation_util.js');

var staticMongod = MongoRunner.runMongod({});  // For startParallelOps.

let st = new ShardingTest({shards: 3, other: {shardOptions: {verbose: 1}}});

assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.shardName);
assert.commandWorked(st.s.adminCommand({shardCollection: 'test.user', key: {x: 1}}));
assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: 0}}));
assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: -100}}));
assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: 100}}));
assert.commandWorked(
    st.s.adminCommand({moveChunk: 'test.user', find: {x: 0}, to: st.shard1.shardName}));
assert.commandWorked(
    st.s.adminCommand({moveChunk: 'test.user', find: {x: 100}, to: st.shard2.shardName}));

// Send a normal write to establish the shard versions outside the transaction.
assert.commandWorked(st.s.getDB('test').runCommand({
    insert: 'user',
    documents: [{x: 0}, {x: 100}],
}));

let lsid = {id: UUID()};
let txnNumber = 0;

// Start a migration in parallel to get around the X lock acquisition at the beginning of
// migration at the destination.
assert.commandWorked(st.rs0.getPrimary().getDB('admin').runCommand(
    {configureFailPoint: 'doNotRefreshRecipientAfterCommit', mode: 'alwaysOn'}));

let destPrimary = st.rs2.getPrimary();
pauseMigrateAtStep(destPrimary, migrateStepNames.cloned);
var joinMoveChunk =
    moveChunkParallel(staticMongod, st.s0.host, {x: -100}, null, 'test.user', st.shard2.shardName);
waitForMigrateStep(destPrimary, migrateStepNames.cloned);

assert.commandWorked(st.s.getDB('test').runCommand({
    insert: 'user',
    documents: [{x: 1}, {x: 101}],
    lsid: lsid,
    txnNumber: NumberLong(txnNumber),
    stmtId: NumberInt(0),
    startTransaction: true,
    autocommit: false,
}));

unpauseMigrateAtStep(destPrimary, migrateStepNames.cloned);
joinMoveChunk();

// Make the transaction stay in prepared state so it will hold on to the collection locks.
assert.commandWorked(st.rs1.getPrimary().getDB('admin').runCommand(
    {configureFailPoint: 'hangBeforeWritingDecision', mode: 'alwaysOn'}));
assert.commandWorked(st.rs2.getPrimary().getDB('admin').runCommand(
    {configureFailPoint: 'hangBeforeWritingDecision', mode: 'alwaysOn'}));

const runCommitCode = "db.adminCommand({" +
    "commitTransaction: 1," +
    "lsid: " + tojson(lsid) + "," +
    "txnNumber: NumberLong(" + txnNumber + ")," +
    "stmtId: NumberInt(0)," +
    "autocommit: false," +
    "});";
let commitTxn = startParallelShell(runCommitCode, st.s.port);

// Insert should be able to refresh the sharding metadata even with existing transactions
// holding the collection lock in IX.
assert.commandWorked(
    st.s.getDB('test').runCommand({insert: 'user', documents: [{x: -100}], maxTimeMS: 5 * 1000}));

assert.commandWorked(st.rs1.getPrimary().getDB('admin').runCommand(
    {configureFailPoint: 'hangBeforeWritingDecision', mode: 'off'}));
assert.commandWorked(st.rs2.getPrimary().getDB('admin').runCommand(
    {configureFailPoint: 'hangBeforeWritingDecision', mode: 'off'}));
commitTxn();

st.stop();
MongoRunner.stopMongod(staticMongod);
})();