summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_fetch_committed_transactions_retry.js
blob: 2e5dce38cf7a14af03a42a097b6206207c0ac707 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 * Tests the retry logic for fetching committed transactions. We test the following scenarios:
 *
 * 1) Retrying after the migration has already fetched and updated its transactions entries.
 * 2) Retrying while the migration is actively updating its transactions entries.
 * 3) Retrying while the migration is updating, and the donor starts a new transaction on an
 *    existing session.
 *
 * TODO SERVER-61231: shard merge can't handle restart, adapt this test.
 *
 * @tags: [
 *   incompatible_with_macos,
 *   incompatible_with_shard_merge,
 *   incompatible_with_windows_tls,
 *   requires_majority_read_concern,
 *   requires_persistence,
 *   serverless,
 * ]
 */

import {TenantMigrationTest} from "jstests/replsets/libs/tenant_migration_test.js";
load("jstests/aggregation/extras/utils.js");
load("jstests/libs/fail_point_util.js");
load("jstests/libs/uuid_util.js");

let tenantMigrationTest = new TenantMigrationTest({name: jsTestName(), sharedOptions: {nodes: 1}});

const tenantId = "testTenantId";
const collName = "testColl";
const transactionsNS = "config.transactions";

const runTransaction = (donorPrimary, tenantDB, collName, session) => {
    jsTestLog("Run and commit a transaction prior to the migration");
    if (!session) {
        session = donorPrimary.startSession({causalConsistency: false});
    }
    const sessionDb = session.getDatabase(tenantDB);
    const sessionColl = sessionDb.getCollection(collName);

    session.startTransaction({writeConcern: {w: "majority"}});
    sessionColl.insert({x: 0});
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    return donorPrimary.getCollection(transactionsNS).find().toArray();
};

const assertTransactionEntries = (donorTxnEntries, recipientTxnEntries) => {
    assert.eq(
        donorTxnEntries.length,
        recipientTxnEntries.length,
        `donor txn entries: ${donorTxnEntries}; recipient txn entries: ${recipientTxnEntries}`);
    for (const entry of [...donorTxnEntries, ...recipientTxnEntries]) {
        // We expect the following fields to be overwritten by the recipient, so we can remove
        // them when comparing entries between the donor and recipient.
        delete entry.lastWriteOpTime;
        delete entry.lastWriteDate;
    }
    assertArrayEq({actual: donorTxnEntries, expected: recipientTxnEntries});
};

(() => {
    jsTestLog("Test retrying after successfully updating entries");

    const tenantDB = tenantMigrationTest.tenantDB(tenantId, "testDB");
    const donorRst = tenantMigrationTest.getDonorRst();
    const donorPrimary = tenantMigrationTest.getDonorPrimary();
    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();
    const recipientConfigColl =
        recipientPrimary.getCollection(TenantMigrationTest.kConfigRecipientsNS);

    const donorTxnEntries = runTransaction(donorPrimary, tenantDB, collName);
    assert.eq(1, donorTxnEntries.length);
    const donorTxnEntry = donorTxnEntries[0];

    // Hang the migration after it has fetched and updated its 'config.transactions' entries.
    const fpAfterFetchingCommittedTransactions = configureFailPoint(
        recipientPrimary, "fpAfterFetchingCommittedTransactions", {action: "hang"});

    jsTestLog("Starting a migration");
    const migrationId = UUID();
    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(migrationId),
        tenantId,
    };
    assert.commandWorked(tenantMigrationTest.startMigration(migrationOpts));

    fpAfterFetchingCommittedTransactions.wait();

    // Verify that the transaction was updated correctly.
    let recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assert.eq(1, recipientTxnEntries.length);
    const recipientTxnEntry = recipientTxnEntries[0];
    assert.eq(donorTxnEntry._id, recipientTxnEntry._id);
    assert.eq(donorTxnEntry.txnNum, recipientTxnEntry.txnNum);

    // Restart the donor primary. This will cause the migration to restart.
    jsTestLog("Restarting donor primary");
    donorRst.restart(donorPrimary);

    // Let the migration restart and hang before it tries to re-fetch committed transactions.
    const fpBeforeFetchingTransactions = configureFailPoint(
        recipientPrimary, "fpBeforeFetchingCommittedTransactions", {action: "hang"});
    fpAfterFetchingCommittedTransactions.off();
    fpBeforeFetchingTransactions.wait();

    // The recipient should indicate that the migration has restarted.
    let recipientDoc;
    assert.soon(() => {
        recipientDoc = recipientConfigColl.find({"_id": migrationId}).toArray();
        return recipientDoc[0].numRestartsDueToDonorConnectionFailure == 1;
    });
    // The state doc should indicate that the migration has already updated 'config.transaction'
    // entries.
    assert.eq(true, recipientDoc[0].completedUpdatingTransactionsBeforeStartOpTime);
    fpBeforeFetchingTransactions.off();

    // Verify that the migration completes successfully.
    TenantMigrationTest.assertCommitted(
        tenantMigrationTest.waitForMigrationToComplete(migrationOpts));
    recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assertTransactionEntries(donorTxnEntries, recipientTxnEntries);

    tenantMigrationTest.stop();
})();

(() => {
    jsTestLog("Test retrying in the middle of updating entries");

    tenantMigrationTest = new TenantMigrationTest({name: jsTestName(), sharedOptions: {nodes: 1}});
    const tenantDB = tenantMigrationTest.tenantDB(tenantId, "testDB");
    const collName1 = `${collName}1`;
    const collName2 = `${collName}2`;

    const donorRst = tenantMigrationTest.getDonorRst();
    const donorPrimary = tenantMigrationTest.getDonorPrimary();
    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();
    const recipientConfigColl =
        recipientPrimary.getCollection(TenantMigrationTest.kConfigRecipientsNS);

    runTransaction(donorPrimary, tenantDB, collName1);
    const donorTxnEntries = runTransaction(donorPrimary, tenantDB, collName2);
    assert.eq(2, donorTxnEntries.length);

    // Hang the recipient after it updates the first transaction entry. Have the failpoint throw
    // a retriable error to avoid the potential race condition where the recipient continues
    // processing transaction entries in the same batch after the failpoint is released.
    const hangAfterUpdatingTransactionEntry = configureFailPoint(
        recipientPrimary, "hangAfterUpdatingTransactionEntry", {"failAfterHanging": true});

    jsTestLog("Starting a migration");
    const migrationId = UUID();
    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(migrationId),
        tenantId,
    };
    assert.commandWorked(tenantMigrationTest.startMigration(migrationOpts));

    hangAfterUpdatingTransactionEntry.wait();

    // Verify that only one transaction was updated correctly.
    let recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assert.eq(1, recipientTxnEntries.length);

    // Restart the donor primary. This will cause the migration to restart.
    jsTestLog("Restarting donor primary");
    donorRst.restart(donorPrimary);

    // Let the migration restart and hang before it tries to re-fetch committed transactions.
    const fpBeforeFetchingTransactions = configureFailPoint(
        recipientPrimary, "fpBeforeFetchingCommittedTransactions", {action: "hang"});
    hangAfterUpdatingTransactionEntry.off();
    fpBeforeFetchingTransactions.wait();

    // The recipient should indicate that the migration has restarted.
    let recipientDoc;
    assert.soon(() => {
        recipientDoc = recipientConfigColl.find({"_id": migrationId}).toArray();
        return recipientDoc[0].numRestartsDueToDonorConnectionFailure == 1;
    });
    // Verify that the 'completedUpdatingTransactionsBeforeStartOpTime' flag is false since the
    // migration was forced to restart before it fully completed fetching.
    assert.eq(false, recipientDoc[0].completedUpdatingTransactionsBeforeStartOpTime);
    fpBeforeFetchingTransactions.off();

    // Verify that the migration completes successfully.
    TenantMigrationTest.assertCommitted(
        tenantMigrationTest.waitForMigrationToComplete(migrationOpts));

    recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assertTransactionEntries(donorTxnEntries, recipientTxnEntries);

    tenantMigrationTest.stop();
})();

(() => {
    jsTestLog("Test retrying with a new transaction in the middle of updating entries");

    tenantMigrationTest = new TenantMigrationTest({name: jsTestName(), sharedOptions: {nodes: 1}});
    const tenantDB = tenantMigrationTest.tenantDB(tenantId, "testDB");
    const collName1 = `${collName}1`;
    const collName2 = `${collName}2`;

    const donorRst = tenantMigrationTest.getDonorRst();
    const donorPrimary = tenantMigrationTest.getDonorPrimary();
    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();
    const recipientConfigColl =
        recipientPrimary.getCollection(TenantMigrationTest.kConfigRecipientsNS);

    runTransaction(donorPrimary, tenantDB, collName1);
    // Start a session on the donor. We will run another transaction on this session later.
    const session = donorPrimary.startSession({causalConsistency: false});
    const initialDonorTxnEntries = runTransaction(donorPrimary, tenantDB, collName2, session);
    assert.eq(2, initialDonorTxnEntries.length);

    // Hang the recipient after it updates the first transaction entry. Have the failpoint throw
    // a retriable error to avoid the potential race condition where the recipient continues
    // processing transaction entries in the same batch after the failpoint is released.
    const hangAfterUpdatingTransactionEntry = configureFailPoint(
        recipientPrimary, "hangAfterUpdatingTransactionEntry", {"failAfterHanging": true});

    jsTestLog("Starting a migration");
    const migrationId = UUID();
    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(migrationId),
        tenantId,
    };
    assert.commandWorked(tenantMigrationTest.startMigration(migrationOpts));

    hangAfterUpdatingTransactionEntry.wait();

    // Verify that the recipient receives one of the donor transaction entries.
    let recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assert.eq(1, recipientTxnEntries.length);

    // Run a new transaction with the same session on the donor. This will advance its
    // 'lastWriteOpTime' past the recipient's 'startFetchingDonorOpTime'.
    // Note: This update should be applied via the recipient tenant oplog applier instead of the
    // fetch transactions stage.
    const updatedDonorTxnEntries = runTransaction(donorPrimary, tenantDB, collName2, session);
    assert.eq(2, updatedDonorTxnEntries.length);
    assert.eq(false, arrayEq(initialDonorTxnEntries, updatedDonorTxnEntries));

    // Restart the donor primary. This will cause the migration to restart.
    jsTestLog("Restarting donor primary");
    donorRst.restart(donorPrimary);

    // Let the migration restart and hang before it tries to re-fetch committed transactions.
    const fpBeforeFetchingTransactions = configureFailPoint(
        recipientPrimary, "fpBeforeFetchingCommittedTransactions", {action: "hang"});
    hangAfterUpdatingTransactionEntry.off();
    fpBeforeFetchingTransactions.wait();

    // The recipient should indicate that the migration has restarted.
    let recipientDoc;
    assert.soon(() => {
        recipientDoc = recipientConfigColl.find({"_id": migrationId}).toArray();
        return recipientDoc[0].numRestartsDueToDonorConnectionFailure == 1;
    });
    // Verify that the 'completedUpdatingTransactionsBeforeStartOpTime' flag is false since the
    // migration was forced to restart before it fully completed fetching.
    assert.eq(false, recipientDoc[0].completedUpdatingTransactionsBeforeStartOpTime);
    fpBeforeFetchingTransactions.off();

    // Verify that the migration completes successfully.
    TenantMigrationTest.assertCommitted(
        tenantMigrationTest.waitForMigrationToComplete(migrationOpts));

    recipientTxnEntries = recipientPrimary.getCollection(transactionsNS).find().toArray();
    assertTransactionEntries(updatedDonorTxnEntries, recipientTxnEntries);

    tenantMigrationTest.stop();
})();