summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_drop_state_doc_collection.js
blob: d52fe64bff4942b6e99ca90f0efa472e2c58420e (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
/**
 * Tests dropping the donor and recipient state doc collections in the middle of a tenant migration.
 *
 * @tags: [requires_fcv_47, requires_majority_read_concern, requires_persistence,
 * incompatible_with_eft, incompatible_with_windows_tls]
 */

(function() {
"use strict";

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

const kMigrationFpNames = [
    "pauseTenantMigrationAfterPersistingInitialDonorStateDoc",
    "pauseTenantMigrationBeforeLeavingDataSyncState",
    "pauseTenantMigrationBeforeLeavingBlockingState",
    "abortTenantMigrationBeforeLeavingBlockingState",
    null,
];
const kTenantId = "testTenantId";
let testNum = 0;

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

function makeMigrationOpts(tenantMigrationTest, tenantId) {
    return {
        migrationIdString: extractUUIDFromObject(UUID()),
        tenantId: tenantId,
        recipientConnString: tenantMigrationTest.getRecipientConnString()
    };
}

/**
 * Starts a migration and then either waits for the failpoint or lets the migration run to
 * completion. Next, drops the donor and/or recipient state doc collections and asserts that the
 * migration is no longer running on the donor and/or recipient. Then, retries the migration (with a
 * different migration id if 'retryWithDifferentMigrationId' is true) and verifies that the retry
 * succeeds or fails as expected.
 */
function testDroppingStateDocCollections(tenantMigrationTest, fpName, {
    dropDonorsCollection = false,
    dropRecipientsCollection = false,
    retryWithDifferentMigrationId = false,
    expectedRunMigrationError,
    expectedForgetMigrationError,
    expectedAbortReason
}) {
    assert(dropDonorsCollection || dropRecipientsCollection);

    jsTest.log(`Testing with failpoint: ${fpName} dropDonorsCollection: ${
        dropDonorsCollection}, dropRecipientsCollection: ${
        dropRecipientsCollection}, retryWithDifferentMigrationId: ${
        retryWithDifferentMigrationId}`);

    const tenantId = makeTenantId();
    const migrationOptsBeforeDrop = makeMigrationOpts(tenantMigrationTest, tenantId);
    let donorPrimary = tenantMigrationTest.getDonorPrimary();
    let recipientPrimary = tenantMigrationTest.getRecipientPrimary();

    let fp;
    if (fpName) {
        fp = configureFailPoint(donorPrimary, fpName, {tenantId: tenantId});
        assert.commandWorked(
            tenantMigrationTest.startMigration(migrationOptsBeforeDrop,
                                               false /* retryOnRetryableErrors */,
                                               false /* automaticForgetMigration */));
        fp.wait();
    } else {
        assert.commandWorked(
            tenantMigrationTest.runMigration(migrationOptsBeforeDrop,
                                             false /* retryOnRetryableErrors */,
                                             false /* automaticForgetMigration */));
    }

    if (dropDonorsCollection) {
        assert(donorPrimary.getCollection(TenantMigrationTest.kConfigDonorsNS).drop());
        let donorDoc = donorPrimary.getCollection(TenantMigrationTest.kConfigDonorsNS).findOne({
            tenantId: tenantId
        });
        assert.eq(donorDoc, null);

        const currOpDonor = assert.commandWorked(
            donorPrimary.adminCommand({currentOp: true, desc: "tenant donor migration"}));
        assert.eq(currOpDonor.inprog.length, 0);

        // Trigger stepup to allow the donor service to rebuild.
        assert.commandWorked(donorPrimary.adminCommand({replSetStepDown: 30, force: true}));
        donorPrimary = tenantMigrationTest.getDonorRst().getPrimary();
    }

    if (dropRecipientsCollection) {
        assert(recipientPrimary.getCollection(TenantMigrationTest.kConfigRecipientsNS).drop({
            writeConcern: {w: "majority"}
        }));
        let recipientDoc =
            recipientPrimary.getCollection(TenantMigrationTest.kConfigRecipientsNS).findOne({
                tenantId: tenantId
            });
        assert.eq(recipientDoc, null);
        const currOpRecipient = assert.commandWorked(
            recipientPrimary.adminCommand({currentOp: true, desc: "tenant recipient migration"}));
        assert.eq(currOpRecipient.inprog.length, 0);

        // Trigger stepup to allow the recipient service to rebuild.
        assert.commandWorked(recipientPrimary.adminCommand({replSetStepDown: 30, force: true}));
        recipientPrimary = tenantMigrationTest.getRecipientRst().getPrimary();
    }

    if (fp) {
        fp.off();
    }
    const migrationOptsAfterDrop = retryWithDifferentMigrationId
        ? makeMigrationOpts(tenantMigrationTest, tenantId)
        : migrationOptsBeforeDrop;
    const runMigrationRes = tenantMigrationTest.runMigration(migrationOptsAfterDrop,
                                                             false /* retryOnRetryableErrors */,
                                                             false /* automaticForgetMigration */);
    if (expectedRunMigrationError) {
        assert.commandFailedWithCode(runMigrationRes, expectedRunMigrationError);
    } else {
        assert.commandWorked(runMigrationRes);
        if (expectedAbortReason) {
            assert.eq(runMigrationRes.state, TenantMigrationTest.DonorState.kAborted);
            assert.eq(runMigrationRes.abortReason.code, expectedAbortReason);
        } else {
            assert.eq(runMigrationRes.state, TenantMigrationTest.DonorState.kCommitted);
        }

        const forgetMigrationRes =
            tenantMigrationTest.forgetMigration(migrationOptsAfterDrop.migrationIdString);
        if (expectedForgetMigrationError) {
            assert.commandFailedWithCode(forgetMigrationRes, expectedForgetMigrationError);
        } else {
            assert.commandWorked(forgetMigrationRes);
            tenantMigrationTest.waitForMigrationGarbageCollection(
                UUID(migrationOptsAfterDrop.migrationIdString));
        }
    }

    if (retryWithDifferentMigrationId && !dropDonorsCollection) {
        assert(dropRecipientsCollection);
        // The original migration will still run to completion after the recipient service rebuilds
        // since the donor will retry the recipientSyncData command on Interrupted error. Wait for
        // the migration to complete and clean up to avoid concurrent migrations when the next test
        // case starts.
        assert.commandWorked(
            tenantMigrationTest.waitForMigrationToComplete(migrationOptsBeforeDrop));
        assert.commandWorked(
            tenantMigrationTest.forgetMigration(migrationOptsBeforeDrop.migrationIdString));
        tenantMigrationTest.waitForMigrationGarbageCollection(
            UUID(migrationOptsAfterDrop.migrationIdString));
    }
}

const tenantMigrationTest = new TenantMigrationTest({
    name: jsTestName(),
    sharedOptions: {
        setParameter: {
            tenantMigrationGarbageCollectionDelayMS: 1,
            ttlMonitorSleepSecs: 1,
        }
    },
    initiateRstWithHighElectionTimeout: false
});

if (!tenantMigrationTest.isFeatureFlagEnabled()) {
    jsTestLog("Skipping test because the tenant migrations feature flag is disabled");
    return;
}

jsTest.log("Test dropping donor and recipient state doc collections during a migration.");
kMigrationFpNames.forEach(fpName => {
    testDroppingStateDocCollections(
        tenantMigrationTest, fpName, {dropDonorsCollection: true, dropRecipientsCollection: true});

    testDroppingStateDocCollections(tenantMigrationTest, fpName, {
        dropDonorsCollection: true,
        dropRecipientsCollection: true,
        retryWithDifferentMigrationId: true
    });

    testDroppingStateDocCollections(tenantMigrationTest, fpName, {
        dropDonorsCollection: false,
        dropRecipientsCollection: true,
        expectedAbortReason: (fpName == "abortTenantMigrationBeforeLeavingBlockingState")
            ? ErrorCodes.InternalError
            : null
    });

    testDroppingStateDocCollections(tenantMigrationTest, fpName, {
        dropDonorsCollection: false,
        dropRecipientsCollection: true,
        retryWithDifferentMigrationId: true,
        // The original migration is still running on the donor so the retry is expected to fail
        // with ConflictingOperationInProgress.
        expectedRunMigrationError: ErrorCodes.ConflictingOperationInProgress
    });

    const sentBlockTimestampToRecipient =
        (!fpName || fpName == "pauseTenantMigrationBeforeLeavingBlockingState" ||
         fpName == "abortTenantMigrationBeforeLeavingBlockingState");
    testDroppingStateDocCollections(tenantMigrationTest, fpName, {
        dropDonorsCollection: true,
        dropRecipientsCollection: false,
        // The retry causes the donor to restart the migration and send a different
        // returnAfterReachingTimestamp/blockTimestamp to the recipient, which is illegal.
        expectedAbortReason: sentBlockTimestampToRecipient ? ErrorCodes.IllegalOperation : null
    });

    const originalMigrationStartedOnRecipient =
        fpName != "pauseTenantMigrationAfterPersistingInitialDonorStateDoc";
    testDroppingStateDocCollections(tenantMigrationTest, fpName, {
        dropDonorsCollection: true,
        dropRecipientsCollection: false,
        retryWithDifferentMigrationId: true,
        // If the original migration has started running on the recipient, the retry will lead to
        // a conflicting migration.
        expectedAbortReason:
            originalMigrationStartedOnRecipient ? ErrorCodes.ConflictingOperationInProgress : null,
        expectedForgetMigrationError:
            originalMigrationStartedOnRecipient ? ErrorCodes.ConflictingOperationInProgress : null
    });
});

tenantMigrationTest.stop();
})();