summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_network_error_via_rollback.js
blob: 6a2f84fc892332d952d3a14d0f399f4e3dc553a3 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 * This test does sync source as a way to test that tenant migrations are resilient to transient
 * connection errors between the recipient primary and the sync source at various stages in the
 * process. (Replica set members close connections as part of rollback.)
 *
 * TODO SERVER-61231: shard merge can't handle concurrent rollback, adapt this test.
 *
 * @tags: [
 *   incompatible_with_eft,
 *   incompatible_with_macos,
 *   incompatible_with_shard_merge,
 *   incompatible_with_windows_tls,
 *   requires_majority_read_concern,
 *   requires_persistence,
 * ]
 */

(function() {
"use strict";

load("jstests/libs/fail_point_util.js");
load("jstests/libs/uuid_util.js");  // for 'extractUUIDFromObject'
load("jstests/replsets/libs/tenant_migration_test.js");
load("jstests/replsets/libs/tenant_migration_util.js");
load("jstests/replsets/rslib.js");  // for 'stopServerReplication'

function runTest({failPointName, failPointData = {}, batchSize = 10 * 1000}) {
    const migrationX509Options = TenantMigrationUtil.makeX509OptionsForTest();

    const donorRst = new ReplSetTest({
        name: "recipientRst",
        nodes: [
            {rsConfig: {priority: 1}},  // initial primary
            {},                         // one possible next primary
            {},                         // the other next primary
            {rsConfig: {priority: 0, hidden: true}},
            {rsConfig: {priority: 0, hidden: true}}
        ],
        nodeOptions: Object.assign(migrationX509Options.donor, {
            setParameter: {
                // Allow non-timestamped reads on donor after migration completes for testing.
                'failpoint.tenantMigrationDonorAllowsNonTimestampedReads':
                    tojson({mode: 'alwaysOn'}),
            }
        }),
        settings: {catchUpTimeoutMillis: 0, chainingAllowed: false}
    });
    const donorNodes = donorRst.startSet();
    donorRst.initiate();

    // We make D and E hidden to reduce the number of readable secondaries. Thus, when the data sync
    // begins (with A as primary), we can be sure that the sync source is either B or C.
    const [donorA, donorB, donorC, donorD, donorE] = donorNodes;

    jsTestLog("Setting up node A as initial donor primary");
    assert.eq(donorA, donorRst.getPrimary());
    donorRst.awaitReplication();

    const tenantMigrationTest = new TenantMigrationTest({
        name: jsTestName(),
        donorRst: donorRst,
        sharedOptions: {setParameter: {collectionClonerBatchSize: batchSize}}
    });

    const tenantId = "testTenantId";
    const dbName = tenantMigrationTest.tenantDB(tenantId, "testDB");
    const collName = "testColl";

    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();

    tenantMigrationTest.insertDonorDB(dbName, collName);

    const migrationId = UUID();
    const migrationIdString = extractUUIDFromObject(migrationId);
    const migrationOpts = {
        migrationIdString: migrationIdString,
        recipientConnString: tenantMigrationTest.getRecipientConnString(),
        tenantId: tenantId,
        readPreference: {mode: 'secondary'},
    };

    // Kick off a tenant migration and make sure that the recipient has started syncing.
    const fullData = Object.merge(failPointData, {database: dbName});
    const hangWhileMigrating = configureFailPoint(recipientPrimary, failPointName, fullData);

    jsTestLog("Starting migration and waiting for recipient to hit the failpoint");
    assert.commandWorked(tenantMigrationTest.startMigration(migrationOpts));
    hangWhileMigrating.wait();

    jsTestLog("Determining whether B or C is the sync source");
    let res = recipientPrimary.adminCommand({currentOp: true, desc: "tenant recipient migration"});
    assert.eq(res.inprog.length, 1, () => tojson(res));
    let currOp = res.inprog[0];
    assert.eq(bsonWoCompare(currOp.instanceID, migrationId), 0, () => tojson(res));

    // At this point the sync source should be either B or C.
    let syncSource;
    let nextPrimary;

    if (donorB.host == currOp.donorSyncSource) {
        syncSource = donorB;
        nextPrimary = donorC;
    } else {
        syncSource = donorC;
        nextPrimary = donorB;
    }

    jsTestLog("Sync source: " + syncSource);
    jsTestLog("Next primary: " + nextPrimary);

    stopServerReplication(nextPrimary);
    stopServerReplication(donorD);
    stopServerReplication(donorE);

    jsTestLog("Inserting rollback operations");
    assert.commandWorked(donorA.getDB("rollbackDB").rollbackColl.insert({toBe: 'rolledBack'}, {
        writeConcern: {w: 2}
    }));

    const lastRBID = assert.commandWorked(syncSource.adminCommand("replSetGetRBID")).rbid;
    const rollbackFP = configureFailPoint(syncSource, "rollbackHangBeforeStart");

    jsTestLog("Failing over to next primary");
    assert.commandWorked(
        donorA.adminCommand({replSetStepDown: ReplSetTest.kDefaultTimeoutMS, force: true}));
    assert.commandWorked(nextPrimary.adminCommand({replSetStepUp: ReplSetTest.kDefaultTimeoutMS}));
    assert.eq(nextPrimary, donorRst.getPrimary());
    restartServerReplication(nextPrimary);
    restartServerReplication(donorD);
    restartServerReplication(donorE);

    res = recipientPrimary.adminCommand({currentOp: true, desc: "tenant recipient migration"});
    currOp = res.inprog[0];
    assert.eq(currOp.migrationCompleted, false, () => tojson(currOp));
    assert.eq(currOp.dataSyncCompleted, false, () => tojson(currOp));

    // The sync source has not rolled back, so the recipient has not yet perceived an interruption.
    assert.eq(NumberLong(0), currOp.numRestartsDueToDonorConnectionFailure, () => tojson(currOp));

    jsTestLog("Inserting new documents and waiting for sync source to roll back");
    assert.commandWorked(
        nextPrimary.getDB("otherDB").otherColl.insert({toBe: 'kept'}, {writeConcern: {w: 3}}));

    rollbackFP.wait();
    rollbackFP.off();

    assert.soonNoExcept(function() {
        const rbid = assert.commandWorked(syncSource.adminCommand("replSetGetRBID")).rbid;
        return rbid > lastRBID;
    }, "rbid did not update", ReplSetTest.kDefaultTimeoutMS);

    hangWhileMigrating.off();

    jsTestLog("Waiting for migration to finish");
    TenantMigrationTest.assertCommitted(
        tenantMigrationTest.waitForMigrationToComplete(migrationOpts));
    tenantMigrationTest.waitForDonorNodesToReachState(
        donorRst.nodes, migrationId, tenantId, TenantMigrationTest.DonorState.kCommitted);

    res = recipientPrimary.adminCommand({currentOp: true, desc: "tenant recipient migration"});
    currOp = res.inprog[0];

    // The migration should still be considered active as it is not yet forgotten.
    assert.eq(currOp.migrationCompleted, false, () => tojson(currOp));
    assert.eq(currOp.dataSyncCompleted, false, () => tojson(currOp));

    // A restart was necessary, due to the sync source closing connections on rollback.
    assert.eq(NumberLong(1), currOp.numRestartsDueToDonorConnectionFailure, () => tojson(currOp));

    jsTestLog("Forgetting migration");
    assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));

    donorRst.stopSet();
    tenantMigrationTest.stop();
}

// Only pick 1 test per run to save machine time.
const caseNum = Math.floor(Math.random() * 11) + 1;

switch (caseNum) {
    case 1:
        jsTestLog("[1] Testing rollback before fetching clusterTime keys.");
        runTest({
            failPointName: "fpBeforeFetchingDonorClusterTimeKeys",
            failPointData: {
                action: "hang",
            },
        });
        break;

    case 2:
        jsTestLog("[2] Testing rollback after connecting instances.");
        runTest({
            failPointName: "fpAfterConnectingTenantMigrationRecipientInstance",
            failPointData: {
                action: "hang",
            },
        });
        break;

    case 3:
        jsTestLog("[3] Testing rollback after comparing FCV against donor.");
        runTest({
            failPointName: "fpAfterComparingRecipientAndDonorFCV",
            failPointData: {
                action: "hang",
            },
        });
        break;

    case 4:
        jsTestLog("[4] Testing rollback after fetching start opTime.");
        runTest({
            failPointName: "fpAfterRetrievingStartOpTimesMigrationRecipientInstance",
            failPointData: {
                action: "hang",
            },
        });
        break;

    case 5:
        jsTestLog("[5] Testing rollback before starting oplog fetcher.");
        runTest({
            failPointName: "fpAfterFetchingRetryableWritesEntriesBeforeStartOpTime",
            failPointData: {
                action: "hang",
            },
        });
        break;

    case 6:
        jsTestLog("[6] Testing rollback during cloning, after starting data sync.");
        runTest({
            failPointName: "hangBeforeClonerStage",
            failPointData: {
                cloner: "TenantAllDatabaseCloner",
                stage: "listDatabases",
            }
        });
        break;

    case 7:
        jsTestLog("[7] Testing rollback during cloning, before cloning databases.");
        runTest({
            failPointName: "hangBeforeClonerStage",
            failPointData: {
                cloner: "TenantDatabaseCloner",
                stage: "listCollections",
            }
        });
        break;

    case 8:
        jsTestLog("[8] Testing rollback during cloning, before listing indexes.");
        runTest({
            failPointName: "hangBeforeClonerStage",
            failPointData: {
                cloner: "TenantCollectionCloner",
                stage: "listIndexes",
            }
        });
        break;

    case 9:
        jsTestLog("[9] Testing rollback during cloning, between listIndexes and find.");
        runTest({
            failPointName: "hangBeforeClonerStage",
            failPointData: {
                cloner: "TenantCollectionCloner",
                stage: "query",
            }
        });
        break;

    case 10:
        jsTestLog("[10] Testing rollback during cloning, between getMores.");
        runTest({
            failPointName: "tenantMigrationHangCollectionClonerAfterHandlingBatchResponse",
            batchSize: 1,
        });
        break;

    case 11:
        jsTestLog("[11] Testing rollback after finishing cloning.");
        runTest({
            failPointName: "fpAfterCollectionClonerDone",
            failPointData: {
                action: "hang",
            },
        });
        break;
    default:
        // Unreachable.
        assert(false);
}
})();