summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/indexbg2.js
blob: e374a406bfc1a43d2955e64d4af57eb463f99f9e (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
// Test background index creation w/ constraints
// @tags: [SERVER-40561]

(function() {
"use strict";

const conn = MongoRunner.runMongod();
assert.neq(null, conn, "mongod failed to start.");

let db = conn.getDB("test");
let baseName = "jstests_index12";

let parallel = function() {
    return db[baseName + "_parallelStatus"];
};

let resetParallel = function() {
    parallel().drop();
};

// Return the PID to call `waitpid` on for clean shutdown.
let doParallel = function(work) {
    resetParallel();
    return startMongoProgramNoConnect(
        "mongo",
        "--eval",
        work + "; db." + baseName + "_parallelStatus.save( {done:1} );",
        db.getMongo().host);
};

let indexBuild = function() {
    let fullName = "db." + baseName;
    return doParallel(fullName + ".createIndex( {i:1}, {background:true, unique:true} )");
};

let doneParallel = function() {
    return !!parallel().findOne();
};

let waitParallel = function() {
    assert.soon(function() {
        return doneParallel();
    }, "parallel did not finish in time", 300000, 1000);
};

let turnFailPointOn = function(failPointName, i) {
    assert.commandWorked(conn.adminCommand(
        {configureFailPoint: failPointName, mode: "alwaysOn", data: {fieldsToMatch: {i: i}}}));
};

let turnFailPointOff = function(failPointName) {
    assert.commandWorked(conn.adminCommand({configureFailPoint: failPointName, mode: "off"}));
};

// Unique background index build fails when there exists duplicate indexed values
// for the duration of the build.
let failOnExistingDuplicateValue = function(coll) {
    let duplicateKey = 0;
    assert.commandWorked(coll.save({i: duplicateKey}));

    let bgIndexBuildPid = indexBuild();
    waitProgram(bgIndexBuildPid);
    assert.eq(1, coll.getIndexes().length, "Index should fail. There exist duplicate values.");

    // Revert to unique key set
    coll.deleteOne({i: duplicateKey});
};

// Unique background index build fails when started with a unique key set,
// but a document with a duplicate key is inserted prior to that key being indexed.
let failOnInsertedDuplicateValue = function(coll) {
    let duplicateKey = 7;

    turnFailPointOn("hangIndexBuildDuringCollectionScanPhaseBeforeInsertion", duplicateKey);

    let bgIndexBuildPid;
    try {
        bgIndexBuildPid = indexBuild();

        jsTestLog("Waiting to hang index build during collection scan before insertion of {i: " +
                  duplicateKey + "}");
        checkLog.containsJson(conn, 20386, {
            where: "before",
            doc: function(doc) {
                return doc.i === duplicateKey;
            }
        });

        assert.commandWorked(coll.save({i: duplicateKey}));
    } finally {
        turnFailPointOff("hangIndexBuildDuringCollectionScanPhaseBeforeInsertion");
    }

    waitProgram(bgIndexBuildPid);
    assert.eq(1,
              coll.getIndexes().length,
              "Index should fail. Duplicate key is inserted prior to that key being indexed.");

    // Revert to unique key set
    coll.deleteOne({i: duplicateKey});
};

// Unique background index build succeeds:
// 1) when a document is inserted and removed with a key that has already been indexed
// 2) when a document with a key not present in the initial set is inserted and removed
let succeedWithoutWriteErrors = function(coll, newKey) {
    let duplicateKey = 3;

    turnFailPointOn("hangIndexBuildDuringCollectionScanPhaseAfterInsertion", duplicateKey);

    let bgIndexBuildPid;
    try {
        bgIndexBuildPid = indexBuild();

        jsTestLog("Waiting to hang index build during collection scan after insertion of {i: " +
                  duplicateKey + "}");
        checkLog.containsJson(conn, 20386, {
            where: "after",
            doc: function(doc) {
                return doc.i === duplicateKey;
            }
        });

        assert.commandWorked(coll.insert({i: duplicateKey, n: true}));

        // First insert on key not present in initial set.
        assert.commandWorked(coll.insert({i: newKey, n: true}));

        // Remove duplicates before completing the index build.
        assert.commandWorked(coll.deleteOne({i: duplicateKey, n: true}));
        assert.commandWorked(coll.deleteOne({i: newKey, n: true}));

    } finally {
        turnFailPointOff("hangIndexBuildDuringCollectionScanPhaseAfterInsertion");
    }

    waitProgram(bgIndexBuildPid);
    assert.eq(2, coll.getIndexes().length, "Index build should succeed");
};

let doTest = function() {
    "use strict";
    const size = 10;

    let coll = db[baseName];
    coll.drop();

    for (let i = 0; i < size; ++i) {
        assert.commandWorked(coll.save({i: i}));
    }
    assert.eq(size, coll.count());
    assert.eq(1, coll.getIndexes().length, "_id index should already exist");

    failOnExistingDuplicateValue(coll);
    assert.eq(size, coll.count());

    failOnInsertedDuplicateValue(coll);
    assert.eq(size, coll.count());

    succeedWithoutWriteErrors(coll, size);

    waitParallel();
};

doTest();

MongoRunner.stopMongod(conn);
})();