summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_retry_session_migration.js
blob: dcf98d09438621ca6993aaee91214af0b4b64e34 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * Tests that retrying a failed tenant migration works even if the config.transactions on the
 * recipient is not cleaned up after the failed migration.
 *
 * TODO SERVER-61231: aborts migration after sending recipientSyncData and starting
 * cloning on recipient, adapt this test to handle file cleanup on recipient.
 *
 * @tags: [
 *   incompatible_with_macos,
 *   incompatible_with_shard_merge,
 *   incompatible_with_windows_tls,
 *   requires_majority_read_concern,
 *   requires_persistence,
 *   serverless,
 * ]
 */

(function() {
"use strict";

load("jstests/replsets/libs/tenant_migration_test.js");
load("jstests/replsets/libs/tenant_migration_util.js");
load("jstests/replsets/rslib.js");
load("jstests/libs/uuid_util.js");

const tenantMigrationTest =
    new TenantMigrationTest({name: jsTestName(), quickGarbageCollection: true});

const kTenantId = "testTenantId";
const kDbName = tenantMigrationTest.tenantDB(kTenantId, "testDB");
const kCollName = "testColl";

const donorPrimary = tenantMigrationTest.getDonorPrimary();
const recipientPrimary = tenantMigrationTest.getRecipientPrimary();

tenantMigrationTest.insertDonorDB(kDbName, kCollName, [{_id: 1}, {_id: 2}]);

let waitBeforeFetchingTransactions =
    configureFailPoint(recipientPrimary, "fpBeforeFetchingCommittedTransactions", {action: "hang"});

const migrationId = UUID();
const migrationOpts = {
    migrationIdString: extractUUIDFromObject(migrationId),
    tenantId: kTenantId,
};
tenantMigrationTest.startMigration(migrationOpts);

waitBeforeFetchingTransactions.wait();

// Run transactions against the donor while the migration is running.
const session1 = donorPrimary.startSession();
const session2 = donorPrimary.startSession();
for (const session of [session1, session2]) {
    jsTestLog("Running transaction with session " + tojson(session.getSessionId()));
    session.startTransaction({writeConcern: {w: "majority"}});
    assert.commandWorked(session.getDatabase(kDbName)[kCollName].updateMany(
        {}, {$push: {transactions: session.getSessionId()}}));
    assert.commandWorked(session.commitTransaction_forTesting());
}

// Run retryable writes against the donor while the migration is running.
const lsid1 = {
    id: UUID()
};
const lsid2 = {
    id: UUID()
};
for (const lsid of [lsid1, lsid2]) {
    jsTestLog("Running retryable writes with lsid " + tojson(lsid));
    assert.commandWorked(donorPrimary.getDB(kDbName).runCommand({
        update: kCollName,
        updates: [
            {q: {_id: 1}, u: {$push: {retryableWrites: lsid}}},
            {q: {_id: 2}, u: {$push: {retryableWrites: lsid}}},
        ],
        txnNumber: NumberLong(0),
        lsid: lsid
    }));
}

// Abort the first migration.
const abortFp = configureFailPoint(donorPrimary, "abortTenantMigrationBeforeLeavingBlockingState");
waitBeforeFetchingTransactions.off();

TenantMigrationTest.assertAborted(tenantMigrationTest.waitForMigrationToComplete(migrationOpts));
tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString);
tenantMigrationTest.waitForMigrationGarbageCollection(migrationId, kTenantId);

abortFp.off();

// Clean up tenant data after a failed migration.
assert.commandWorked(recipientPrimary.getDB(kDbName).dropDatabase());

waitBeforeFetchingTransactions =
    configureFailPoint(recipientPrimary, "fpBeforeFetchingCommittedTransactions", {action: "hang"});

// Retry the migration.
tenantMigrationTest.startMigration(migrationOpts);

waitBeforeFetchingTransactions.wait();

// Run a newer transaction on session2 during the migration.
session2.startTransaction({writeConcern: {w: "majority"}});
assert.commandWorked(session2.getDatabase(kDbName)[kCollName].updateMany(
    {}, {$push: {retryMigration: session2.getSessionId()}}));
assert.commandWorked(session2.commitTransaction_forTesting());

// Run a newer retryable write with lsid2 during the migration.
assert.commandWorked(donorPrimary.getDB(kDbName).runCommand({
    update: kCollName,
    updates: [
        {q: {_id: 1}, u: {$push: {retryMigration: lsid2}}},
        {q: {_id: 2}, u: {$push: {retryMigration: lsid2}}},
    ],
    txnNumber: NumberLong(1),
    lsid: lsid2
}));

waitBeforeFetchingTransactions.off();

TenantMigrationTest.assertCommitted(tenantMigrationTest.waitForMigrationToComplete(migrationOpts));

// Retrying commitTransaction against the recipient.
assert.commandWorked(recipientPrimary.adminCommand({
    commitTransaction: 1,
    lsid: session1.getSessionId(),
    txnNumber: NumberLong(0),
    autocommit: false
}));
assert.commandWorked(recipientPrimary.adminCommand({
    commitTransaction: 1,
    lsid: session2.getSessionId(),
    txnNumber: NumberLong(1),
    autocommit: false
}));

// Retrying retryable writes against the recipient.
assert.commandWorked(recipientPrimary.getDB(kDbName).runCommand({
    update: kCollName,
    updates: [
        {q: {_id: 1}, u: {$push: {retryableWrites: lsid1}}},
        {q: {_id: 2}, u: {$push: {retryableWrites: lsid1}}},
    ],
    txnNumber: NumberLong(0),
    lsid: lsid1
}));
assert.commandWorked(recipientPrimary.getDB(kDbName).runCommand({
    update: kCollName,
    updates: [
        {q: {_id: 1}, u: {$push: {retryMigration: lsid2}}},
        {q: {_id: 2}, u: {$push: {retryMigration: lsid2}}},
    ],
    txnNumber: NumberLong(1),
    lsid: lsid2
}));

// The dbhash between the donor and the recipient should still match after retrying
// commitTransaction and the retryable writes because they should be noop.
TenantMigrationUtil.checkTenantDBHashes(
    tenantMigrationTest.getDonorRst(), tenantMigrationTest.getRecipientRst(), kTenantId);

tenantMigrationTest.stop();
})();