summaryrefslogtreecommitdiff
path: root/jstests/sharding/index_commands_during_initial_split.js
blob: 7b5ab80b7282ffcdfe5b75945954e38f658b073b (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
/*
 * Test that the index commands are correctly propagated if they are executed
 * either before, during, or after the initial split critical section.
 * @tags: [requires_fcv_44]
 */
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");
load("jstests/libs/parallelTester.js");
load("jstests/sharding/libs/sharded_index_util.js");

/*
 * Shards the given collection.
 */
function runShardCollection(host, ns, shardKey) {
    const mongos = new Mongo(host);
    return mongos.adminCommand({shardCollection: ns, key: shardKey});
}

/*
 * Defines zones for the given collection, then runs shardCollection and the given command after
 * the given shardCollection fail point is hit. If isBlocked is true, asserts that the command is
 * blocked (behind the initial split critical section). Otherwise, asserts that the command
 * succeeds.
 */
function runCommandDuringShardCollection(st, ns, shardKey, zones, failpointName, cmd, isBlocked) {
    const dbName = ns.split(".")[0];

    // Predefine zones for the collection.
    for (const zone of zones) {
        assert.commandWorked(st.s.adminCommand(
            {updateZoneKeyRange: ns, min: zone.min, max: zone.max, zone: zone.name}));
    }

    // Turn on the fail point on the primary shard and wait for shardCollection to hit the
    // fail point.
    let failPoint = configureFailPoint(st.shard0, failpointName);
    let shardCollThread = new Thread(runShardCollection, st.s.host, ns, shardKey);
    shardCollThread.start();
    failPoint.wait();

    if (isBlocked) {
        // Assert that the command eventually times out.
        assert.commandFailedWithCode(
            st.s.getDB(dbName).runCommand(Object.assign(cmd, {maxTimeMS: 500})),
            ErrorCodes.MaxTimeMSExpired);
    } else {
        assert.commandWorked(st.s.getDB(dbName).runCommand(cmd));
    }

    // Turn off the fail point and wait for shardCollection to complete.
    failPoint.off();
    shardCollThread.join();
    assert.commandWorked(shardCollThread.returnData());
}

const numShards = 4;
const st = new ShardingTest({shards: numShards});
const allShards = [];
for (let i = 0; i < numShards; i++) {
    allShards.push(st["shard" + i]);
}

const dbName = "test";
const testDB = st.s.getDB(dbName);
const shardKey = {
    _id: 1
};
const index = {
    key: {x: 1},
    name: "x_1"
};

const shardToZone = {
    [st.shard1.shardName]: {name: "zone1", min: {_id: MinKey}, max: {_id: 0}},
    [st.shard2.shardName]: {name: "zone2", min: {_id: 0}, max: {_id: MaxKey}}
};
const zones = Object.values(shardToZone);

const failPoints = [
    {
        name: "pauseShardCollectionBeforeCriticalSection",
        expectedAffectedShards: new Set([st.shard0, st.shard1, st.shard2]),
        criticalSectionInProgress: false
    },
    {
        name: "pauseShardCollectionReadOnlyCriticalSection",
        expectedAffectedShards: new Set([st.shard0, st.shard1, st.shard2]),
        criticalSectionInProgress: true
    },
    {
        name: "pauseShardCollectionCommitPhase",
        expectedAffectedShards: new Set([st.shard0, st.shard1, st.shard2]),
        criticalSectionInProgress: true
    },
    {
        name: "pauseShardCollectionAfterCriticalSection",
        expectedAffectedShards: new Set([st.shard1, st.shard2]),
        criticalSectionInProgress: false
    }
];

assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.shardName);
for (const [shardName, zone] of Object.entries(shardToZone)) {
    assert.commandWorked(st.s.adminCommand({addShardToZone: shardName, zone: zone.name}));
}

failPoints.forEach(failPoint => {
    jsTest.log(`Testing createIndexes in step ${failPoint.name}...`);
    const collName = "testCreateIndexes" + failPoint.name;
    const ns = dbName + "." + collName;
    const cmd = {createIndexes: collName, indexes: [index]};
    const isBlocked = failPoint.criticalSectionInProgress;

    assert.commandWorked(st.s.getDB(dbName).createCollection(collName));
    runCommandDuringShardCollection(st, ns, shardKey, zones, failPoint.name, cmd, isBlocked);

    if (isBlocked) {
        return;
    }

    // Assert that the index only exists on the targeted shards.
    allShards.forEach(shard => {
        if (failPoint.expectedAffectedShards.has(shard)) {
            ShardedIndexUtil.assertIndexExistsOnShard(shard, dbName, collName, index.key);
        } else {
            ShardedIndexUtil.assertIndexDoesNotExistOnShard(shard, dbName, collName, index.key);
        }
    });
});

failPoints.forEach(failPoint => {
    jsTest.log(`Testing dropIndexes in step ${failPoint.name}...`);
    const collName = "testDropIndexes" + failPoint.name;
    const ns = dbName + "." + collName;
    const cmd = {dropIndexes: collName, index: index.name};
    const isBlocked = failPoint.criticalSectionInProgress;

    assert.commandWorked(st.s.getDB(dbName).createCollection(collName));
    assert.commandWorked(
        st.s.getDB(dbName).runCommand({createIndexes: collName, indexes: [index]}));
    runCommandDuringShardCollection(st, ns, shardKey, zones, failPoint.name, cmd, isBlocked);

    if (isBlocked) {
        return;
    }

    // Assert that the index does not exist on any shards.
    allShards.forEach(shard => {
        const usedToOwnChunks = (shard.shardName === st.shard0.shardName);
        if (failPoint.expectedAffectedShards.has(shard) || !usedToOwnChunks) {
            ShardedIndexUtil.assertIndexDoesNotExistOnShard(shard, dbName, collName, index.key);
        } else {
            ShardedIndexUtil.assertIndexExistsOnShard(st.shard0, dbName, collName, index.key);
        }
    });
});

failPoints.forEach(failPoint => {
    jsTest.log(`Testing collMod in step ${failPoint.name}...`);
    const collName = "testCollMod" + failPoint.name;
    const ns = dbName + "." + collName;
    const cmd = {collMod: collName, validator: {x: {$type: "string"}}};
    const isBlocked = failPoint.criticalSectionInProgress;

    assert.commandWorked(st.s.getDB(dbName).createCollection(collName));
    runCommandDuringShardCollection(st, ns, shardKey, zones, failPoint.name, cmd, isBlocked);

    if (isBlocked) {
        return;
    }

    // Assert that only the targeted shards do document validation.
    allShards.forEach(shard => {
        if (failPoint.expectedAffectedShards.has(shard)) {
            assert.commandFailedWithCode(shard.getCollection(ns).insert({x: 1}),
                                         ErrorCodes.DocumentValidationFailure);
        } else {
            assert.commandWorked(shard.getCollection(ns).insert({x: 1}));
        }
    });
});

st.stop();
})();