summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_drop_collection.js
blob: 8574e0ef3ad585222523114430dea75bab0e3769 (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
/**
 * Tests that TenantCollectionCloner completes without error when a collection is dropped during
 * cloning as part of a tenant migration.
 *
 * @tags: [requires_fcv_47, requires_majority_read_concern, incompatible_with_eft,
 * incompatible_with_windows_tls]
 */

(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");
load('jstests/replsets/libs/two_phase_drops.js');

const checkFlagOnly = new TenantMigrationTest({});
const flagEnabled = checkFlagOnly.isFeatureFlagEnabled();
checkFlagOnly.stop();
if (!flagEnabled) {
    jsTestLog("Skipping test because the tenant migrations feature flag is disabled");
    return;
}

function runDropTest({failPointName, failPointData, expectedLog, createNew}) {
    // Configure batch size for recipient clone.
    const recipientRst = new ReplSetTest({
        nodes: 1,
        name: "recipient",
        nodeOptions: Object.assign(TenantMigrationUtil.makeX509OptionsForTest().recipient,
                                   {setParameter: {collectionClonerBatchSize: 1}})
    });

    recipientRst.startSet();
    recipientRst.initiate();

    const tenantMigrationTest =
        new TenantMigrationTest({name: jsTestName(), recipientRst: recipientRst});

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

    const donorPrimary = tenantMigrationTest.getDonorPrimary();
    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();

    // Create a tenant collection with some data in it.
    tenantMigrationTest.insertDonorDB(dbName, collName);

    // Save the nss and uuid for checking log lines.
    const nss = `${dbName}.${collName}`;
    const uuid =
        extractUUIDFromObject(getUUIDFromListCollections(donorPrimary.getDB(dbName), collName));

    const migrationId = UUID();
    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(migrationId),
        recipientConnString: tenantMigrationTest.getRecipientConnString(),
        tenantId: tenantId,
    };
    const donorRstArgs = TenantMigrationUtil.createRstArgs(tenantMigrationTest.getDonorRst());

    // Set failpoint for recipient.
    const failPoint = configureFailPoint(recipientPrimary, failPointName, failPointData);

    // Start migration and wait for failpoint.
    jsTestLog("Waiting to hit recipient failpoint");
    const migrationThread =
        new Thread(TenantMigrationUtil.runMigrationAsync, migrationOpts, donorRstArgs);
    migrationThread.start();
    failPoint.wait();

    // Drop the collection on the donor while it's being cloned.
    jsTestLog("Dropping collection on donor");
    const donorDB = donorPrimary.getDB(dbName);
    const donorColl = donorDB.getCollection(collName);
    assert(donorColl.drop());
    TwoPhaseDropCollectionTest.waitForDropToComplete(
        donorDB, collName, [{_id: "not the same collection"}]);

    if (createNew) {
        jsTestLog("Creating a new collection with the same name.");
        tenantMigrationTest.insertDonorDB(dbName, collName, [{_id: "not the same collection"}]);
    }

    jsTestLog("Allowing migration to continue");
    failPoint.off();

    if (expectedLog) {
        let expectedLogDict = eval('(' + expectedLog + ')');
        jsTestLog(expectedLogDict);
        checkLog.containsJson(recipientPrimary, expectedLogDict.code, expectedLogDict.attr);
    }

    jsTestLog("Waiting for migration to complete");
    assert.commandWorked(migrationThread.returnData());
    assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));

    const recipientColl = recipientPrimary.getDB(dbName).getCollection(collName);

    if (createNew) {
        assert.eq([{_id: "not the same collection"}], recipientColl.find().toArray());
    } else {
        assert.eq(0, recipientColl.find().itcount());
    }

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

jsTestLog("[1] Testing dropping before count stage.");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "count"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}'
});

jsTestLog("[2] Testing dropping before count stage, with same-name collection created");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "count"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}',
    createNew: true
});

jsTestLog("[3] Testing dropping between count and listIndexes.");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "listIndexes"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}'
});

jsTestLog("[4] Testing dropping between count and listIndexes, with same-name collection created");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "listIndexes"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}',
    createNew: true
});

jsTestLog("[5] Testing dropping between listIndexes and find.");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "query"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}'
});

jsTestLog("[6] Testing dropping between listIndexes and find, with same-name collection created");
runDropTest({
    failPointName: "hangBeforeClonerStage",
    failPointData: {cloner: "TenantCollectionCloner", stage: "query"},
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}',
    createNew: true
});

jsTestLog("[7] Testing dropping between getMore calls.");
runDropTest({
    // Will trigger right after the first batch.
    failPointName: "tenantMigrationHangCollectionClonerAfterHandlingBatchResponse",
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}',
});

jsTestLog("[8] Testing dropping between getMore calls, with same-name collection created");
runDropTest({
    // Will trigger right after the first batch.
    failPointName: "tenantMigrationHangCollectionClonerAfterHandlingBatchResponse",
    expectedLog:
        '{code: 5289701, attr: { namespace: nss, uuid: (x)=>(x.uuid.$uuid === uuid), tenantId: tenantId}}',
    createNew: true
});
})();