summaryrefslogtreecommitdiff
path: root/jstests/replsets/shard_split_test.js
blob: 22302965367e762e173629a1f9cc4707efae9778 (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
/*
 * Test invocation of commitShardSplit command
 * @tags: [requires_fcv_52, featureFlagShardSplit]
 */

load("jstests/libs/fail_point_util.js");
load('jstests/libs/parallel_shell_helpers.js');
load("jstests/serverless/libs/basic_serverless_test.js");

const kMaxTimeMS = 1 * 1000;

function findMigration(primary, uuid) {
    const donorsCollection = primary.getDB("config").getCollection("tenantSplitDonors");
    return donorsCollection.findOne({"_id": uuid});
}

function assertDocumentState(primary, uuid, state) {
    const migrationDoc = findMigration(primary, uuid);
    assert(migrationDoc);
    assert.eq(migrationDoc.state, state);
}

function runAbort() {
    "use strict";

    jsTestLog("Starting runAbort");

    // Skip db hash check because secondary is left with a different config.
    TestData.skipCheckDBHashes = true;

    const test =
        new BasicServerlessTest({recipientTagName: "recipientNode", recipientSetName: "recipient"});
    test.addRecipientNodes();

    const donorPrimary = test.donor.getPrimary();
    const adminDb = donorPrimary.getDB("admin");
    const migrationId = UUID();

    jsTestLog("Asserting no state document exist before command");
    assert.isnull(findMigration(donorPrimary, migrationId));

    jsTestLog("Running abortShardSplit command");
    assert.commandWorked(adminDb.runCommand({abortShardSplit: 1, migrationId}));

    jsTestLog("Asserting state document exist after command");
    assertDocumentState(donorPrimary, migrationId, "aborted");

    test.stop();
}

function runBlocking() {
    "use strict";

    jsTestLog("Starting runBlocking");

    // Skip db hash check because secondary is left with a different config.
    TestData.skipCheckDBHashes = true;

    const test =
        new BasicServerlessTest({recipientTagName: "recipientNode", recipientSetName: "recipient"});
    test.addRecipientNodes();

    const donorPrimary = test.donor.getPrimary();
    const migrationId = UUID();
    const tenantIds = ["test_tenant_1", "test_tenant_2"];

    jsTestLog("Asserting no state document exist before command");
    assert.isnull(findMigration(donorPrimary, migrationId));

    jsTestLog("Asserting we can write before the migration");
    tenantIds.forEach(id => {
        const tenantDB = donorPrimary.getDB(id + "_data");
        let insertedObj = {name: id + "1", payload: "testing_data"};
        assert.commandWorked(tenantDB.runCommand(
            {insert: "testing_collection", documents: [insertedObj], maxTimeMS: kMaxTimeMS}));
    });

    // configure failpoints
    const adminDb = donorPrimary.getDB("admin");
    const blockingFailPoint = configureFailPoint(adminDb, "pauseShardSplitAfterBlocking");

    // TODO(SERVER-63091): remove this when we actually split recipients
    configureFailPoint(adminDb, "skipShardSplitWaitForSplitAcceptance");

    jsTestLog("Running commitShardSplit command");
    const awaitCommand = startParallelShell(
        funWithArgs(
            function(migrationId, recipientTagName, recipientSetName, tenantIds) {
                assert.commandWorked(db.adminCommand({
                    commitShardSplit: 1,
                    migrationId,
                    recipientTagName,
                    recipientSetName,
                    tenantIds
                }));
            },
            migrationId,
            test.recipientTagName,
            test.recipientSetName,
            tenantIds),
        donorPrimary.port);

    blockingFailPoint.wait();

    jsTestLog("Asserting state document is in blocking state");
    assertDocumentState(donorPrimary, migrationId, "blocking");

    jsTestLog("Asserting we cannot write in blocking state");
    tenantIds.forEach(id => {
        const tenantDB = donorPrimary.getDB(id + "_data");
        let insertedObj = {name: id + "2", payload: "testing_data2"};
        let res = tenantDB.runCommand(
            {insert: "testing_collection", documents: [insertedObj], maxTimeMS: kMaxTimeMS});
        assert.commandFailedWithCode(res, ErrorCodes.MaxTimeMSExpired);
    });

    jsTestLog("Disabling failpoints and waiting for command to complete");
    blockingFailPoint.off();
    awaitCommand();

    jsTestLog("Asserting state document exist after command");
    assertDocumentState(donorPrimary, migrationId, "committed");
    test.stop();
}

runAbort();
runBlocking();