summaryrefslogtreecommitdiff
path: root/jstests/replsets/buildindexes_false_commit_quorum.js
blob: 965d0eb31d09579432f5f751f643506919b10727 (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
/**
 * Test that the buildIndexes:false replica set config option behaves correctly with various commit
 * quorum options.
 *
 * @tags: [
 * ]
 */

(function() {
'use strict';

load('jstests/noPassthrough/libs/index_build.js');  // for assertIndexes().

// Skip db hash check because secondary will have different number of indexes due to
// buildIndexes:false.
TestData.skipCheckDBHashes = true;

const replTest = new ReplSetTest({
    name: jsTestName(),
    nodes: [
        {},
        {},
        {},
        {rsConfig: {priority: 0, buildIndexes: false}},
        {rsConfig: {priority: 0, votes: 0, buildIndexes: false}},
    ]
});
replTest.startSet();
replTest.initiate();

const dbName = "buildIndexes";
const collName = "test";
const primaryDb = replTest.getPrimary().getDB(dbName);

// Ensure the collection is populated. Index builds on empty collection circumvent the two-phase
// index build machinery, and we want to exercise that in this test.
for (let i = 0; i < 100; i++) {
    primaryDb[collName].insert({x: 1, y: "abc", c: 1});
}

// The default commit quorum is 'votingMembers', and this index build should fail early because the
// voting buildIndexes:false node will never build the index.
assert.commandFailedWithCode(primaryDb.runCommand({
    createIndexes: collName,
    indexes: [{key: {y: 1}, name: 'y_1_default_commitQuorum'}],
}),
                             ErrorCodes.UnsatisfiableCommitQuorum);

// With a commit quorum that includes 4 nodes, the quorum is unsatisfiable because it includes a
// buildIndexes: false node.
assert.commandFailedWithCode(primaryDb.runCommand({
    createIndexes: collName,
    indexes: [{key: {y: 1}, name: 'y_1_commitQuorum_3'}],
    commitQuorum: 4,
}),
                             ErrorCodes.UnsatisfiableCommitQuorum);

// This commit quorum does not need to include the buildIndexes:false node so it should be
// satisfiable.
const indexName = 'y_1_commitQuorum_majority';
assert.commandWorked(primaryDb.runCommand({
    createIndexes: collName,
    indexes: [{key: {y: 1}, name: indexName}],
    commitQuorum: 'majority',
}));

replTest.awaitReplication();

let secondaryDbs = [];
replTest.getSecondaries().forEach((conn) => {
    conn.setSecondaryOk();
    secondaryDbs.push(conn.getDB(dbName));
});

IndexBuildTest.assertIndexes(secondaryDbs[0][collName], 2, ['_id_', indexName]);
IndexBuildTest.assertIndexes(secondaryDbs[1][collName], 2, ['_id_', indexName]);
IndexBuildTest.assertIndexes(secondaryDbs[2][collName], 1, ['_id_']);
IndexBuildTest.assertIndexes(secondaryDbs[3][collName], 1, ['_id_']);

replTest.stopSet();
}());