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

(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));
        fp.wait();
    } else {
        TenantMigrationTest.assertCommitted(tenantMigrationTest.runMigration(
            migrationOptsBeforeDrop, {automaticForgetMigration: false}));
    }

    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, {automaticForgetMigration: false});
    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));
    }
}

jsTest.log("Test dropping donor and recipient state doc collections during a migration.");
kMigrationFpNames.forEach(fpName => {
    const tenantMigrationTest = new TenantMigrationTest({
        name: jsTestName(),
        quickGarbageCollection: true,
        initiateRstWithHighElectionTimeout: false
    });

    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
    });

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

    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();
});
})();