summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_donor_kill_op_retry.js
blob: 99b5a436177f040f23992ee2b4852ddabc7d61c3 (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
/**
 * Tests that the donor will retry its steps if its OperationContext is interrupted by a killOp.
 *
 * @tags: [requires_fcv_47, requires_majority_read_concern, incompatible_with_eft,
 * incompatible_with_windows_tls, incompatible_with_macos, requires_persistence]
 */

(function() {
"use strict";

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

const kGarbageCollectionDelayMS = 5 * 1000;
const kDelayMS = 100000;  // Set some arbitrarily large blockTimeMS to let recipientSyncData command
                          // hang until we use kill op to kill it.
const kTenantIdPrefix = "testTenantId";
let testNum = 0;
const migrationX509Options = TenantMigrationUtil.makeX509OptionsForTest();
const garbageCollectionOpts = {
    // Set the delay before a donor state doc is garbage collected to be short to speed
    // up the test.
    tenantMigrationGarbageCollectionDelayMS: kGarbageCollectionDelayMS,
    ttlMonitorSleepSecs: 1
};

function makeTenantId() {
    return kTenantIdPrefix + testNum++;
}

const tenantMigrationTest = new TenantMigrationTest({name: jsTestName()});
if (!tenantMigrationTest.isFeatureFlagEnabled()) {
    jsTestLog("Skipping test because the tenant migrations feature flag is disabled");
    return;
}

{
    // This section tests behavior in the middle of a tenant migration.
    let fpNames = [
        "pauseTenantMigrationBeforeInsertingDonorStateDoc",
        "pauseTenantMigrationDonorWhileUpdatingStateDoc",
        "pauseTenantMigrationBeforeStoringExternalClusterTimeKeyDocs"
    ];
    for (let fpName of fpNames) {
        jsTestLog("Setting failpoint \"" + fpName +
                  "\" to test that the migration will retry the " +
                  "operation at the failpoint if a killOp is issued.");

        const migrationOpts = {
            migrationIdString: extractUUIDFromObject(UUID()),
            recipientConnString: tenantMigrationTest.getRecipientConnString(),
            tenantId: makeTenantId(),

        };
        const donorPrimary = tenantMigrationTest.getDonorPrimary();
        let fp = configureFailPoint(donorPrimary, fpName);

        const donorRstArgs = TenantMigrationUtil.createRstArgs(tenantMigrationTest.getDonorRst());

        const runMigrationThread =
            new Thread(TenantMigrationUtil.runMigrationAsync, migrationOpts, donorRstArgs);
        runMigrationThread.start();
        fp.wait();

        const res = assert.commandWorked(donorPrimary.adminCommand({
            currentOp: true,
            $all: true,
            desc: {$regex: 'TenantMigrationDonorService'},
            opid: {$exists: true}
        }));

        const opid = res.inprog[0].opid;
        assert.commandWorked(donorPrimary.adminCommand({killOp: 1, op: opid}));

        fp.off();
        runMigrationThread.join();

        TenantMigrationTest.assertCommitted(runMigrationThread.returnData());
        assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));
    }
}

{
    jsTestLog(
        "Test that killing the recipientSyncData command on the recipient will trigger the donor " +
        "to retry sending recipientSyncData command.");

    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(UUID()),
        recipientConnString: tenantMigrationTest.getRecipientConnString(),
        tenantId: makeTenantId(),

    };

    let fp = configureFailPoint(tenantMigrationTest.getRecipientPrimary(), "failCommand", {
        failInternalCommands: true,
        blockConnection: true,
        blockTimeMS: kDelayMS,
        failCommands: ["recipientSyncData"],
    });

    const donorRstArgs = TenantMigrationUtil.createRstArgs(tenantMigrationTest.getDonorRst());

    const runMigrationThread =
        new Thread(TenantMigrationUtil.runMigrationAsync, migrationOpts, donorRstArgs);
    runMigrationThread.start();
    fp.wait();

    const res = assert.commandWorked(tenantMigrationTest.getRecipientPrimary().adminCommand({
        currentOp: true,
        $or: [
            {"command.$truncated": {$exists: true}},
            {"command.recipientSyncData": {$exists: true}}
        ]
    }));

    // If the recipientSyncData command has been truncated, we check if the truncated command
    // contains "recipientSyncData".
    let opid;
    for (let op of res.inprog) {
        if (op.command.recipientSyncData) {
            opid = op.opid;
        } else {
            if (op.command.$truncated.includes("recipientSyncData")) {
                opid = op.opid;
            }
        }
    }
    assert(opid);

    assert.commandWorked(
        tenantMigrationTest.getRecipientPrimary().adminCommand({killOp: 1, op: opid}));

    fp.off();
    runMigrationThread.join();

    TenantMigrationTest.assertCommitted(runMigrationThread.returnData());
    assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));
}

{
    // This section tests the behavior during TenantMigrationDonorService creation.
    let fpNames = [
        "pauseTenantMigrationBeforeCreatingStateDocumentTTLIndex",
        "pauseTenantMigrationBeforeCreatingExternalKeysTTLIndex"
    ];
    for (let fpName of fpNames) {
        tenantMigrationTest.getDonorRst().stopSet();
        tenantMigrationTest.getDonorRst().startSet(
            Object.assign(migrationX509Options.donor,
                          {setParameter: {['failpoint.' + fpName]: tojson({mode: 'alwaysOn'})}}));
        tenantMigrationTest.getDonorRst().initiate();
        TenantMigrationUtil.createTenantMigrationRecipientRoleIfNotExist(
            tenantMigrationTest.getDonorRst());

        jsTestLog(
            "Setting failpoint \"" + fpName +
            "\" during the creation of a ReplSetTest to test that the migration will retry the " +
            "operation at the failpoint if a killOp is issued.");

        const migrationOpts = {
            migrationIdString: extractUUIDFromObject(UUID()),
            recipientConnString: tenantMigrationTest.getRecipientConnString(),
            tenantId: makeTenantId(),

        };
        const donorPrimary = tenantMigrationTest.getDonorPrimary();
        const donorRstArgs = TenantMigrationUtil.createRstArgs(tenantMigrationTest.getDonorRst());
        const runMigrationThread =
            new Thread(TenantMigrationUtil.runMigrationAsync, migrationOpts, donorRstArgs);
        runMigrationThread.start();

        const res = assert.commandWorked(donorPrimary.adminCommand({
            currentOp: true,
            $all: true,
            desc: {$regex: 'TenantMigrationDonorService'},
            opid: {$exists: true}
        }));
        const opid = res.inprog[0].opid;
        assert.commandWorked(donorPrimary.adminCommand({killOp: 1, op: opid}));

        assert.commandWorked(donorPrimary.adminCommand({configureFailPoint: fpName, mode: "off"}));

        runMigrationThread.join();

        TenantMigrationTest.assertCommitted(runMigrationThread.returnData());
        assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));
    }
}

{
    // This section is testing behavior during garbage collection.
    tenantMigrationTest.getDonorRst().stopSet();
    tenantMigrationTest.getDonorRst().startSet(
        Object.assign(migrationX509Options.donor, {setParameter: garbageCollectionOpts}));
    tenantMigrationTest.getDonorRst().initiate();
    TenantMigrationUtil.createTenantMigrationRecipientRoleIfNotExist(
        tenantMigrationTest.getDonorRst());

    tenantMigrationTest.getRecipientRst().stopSet();
    tenantMigrationTest.getRecipientRst().startSet(
        Object.assign(migrationX509Options.recipient, {setParameter: garbageCollectionOpts}));
    tenantMigrationTest.getRecipientRst().initiate();
    TenantMigrationUtil.createTenantMigrationDonorRoleIfNotExist(
        tenantMigrationTest.getRecipientRst());

    let fpNames = [
        "pauseTenantMigrationDonorBeforeMarkingStateGarbageCollectable",
        "pauseTenantMigrationBeforeMarkingExternalKeysGarbageCollectable"
    ];
    for (let fpName of fpNames) {
        jsTestLog(
            "Setting failpoint \"" + fpName +
            "\" during migration garbage collection to test that the migration will retry the " +
            "operation at the failpoint if a killOp is issued.");
        const migrationId = UUID();
        const tenantId = makeTenantId();
        const migrationOpts = {
            migrationIdString: extractUUIDFromObject(migrationId),
            recipientConnString: tenantMigrationTest.getRecipientConnString(),
            tenantId: tenantId,
        };

        let fp = configureFailPoint(tenantMigrationTest.getDonorPrimary(), fpName);

        TenantMigrationTest.assertCommitted(
            tenantMigrationTest.runMigration(migrationOpts,
                                             false /* retry on retriable errors */,
                                             false /* Automatically forget migration */));

        const donorPrimary = tenantMigrationTest.getDonorPrimary();
        const donorRstArgs = TenantMigrationUtil.createRstArgs(tenantMigrationTest.getDonorRst());
        const forgetMigrationThread = new Thread(TenantMigrationUtil.forgetMigrationAsync,
                                                 migrationOpts.migrationIdString,
                                                 donorRstArgs);
        forgetMigrationThread.start();

        fp.wait();

        const res = assert.commandWorked(donorPrimary.adminCommand({
            currentOp: true,
            $all: true,
            desc: {$regex: 'TenantMigrationDonorService'},
            opid: {$exists: true}
        }));
        const opid = res.inprog[0].opid;
        assert.commandWorked(donorPrimary.adminCommand({killOp: 1, op: opid}));

        fp.off();
        forgetMigrationThread.join();
        tenantMigrationTest.waitForMigrationGarbageCollection(migrationId, tenantId);
    }
}
tenantMigrationTest.stop();
})();