summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/hybrid_unique_index_with_updates.js
blob: 65199c9d99cef373302df76c806f8b33e3f99d78 (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
188
189
/**
 * Tests that write operations are accepted and result in correct indexing behavior for each phase
 * of hybrid unique index builds. This test inserts a duplicate document at different phases of an
 * index build to confirm that the resulting behavior is failure.
 *
 * @tags: [
 *   requires_replication,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/logv2_helpers.js");

let replSetTest = new ReplSetTest({name: "hybrid_updates", nodes: 2});
replSetTest.startSet();
replSetTest.initiate();

let conn = replSetTest.getPrimary();
let testDB = conn.getDB('test');

// Enables a failpoint, runs 'hitFailpointFunc' to hit the failpoint, then runs
// 'duringFailpointFunc' while the failpoint is active.
let doDuringFailpoint = function(
    failPointName, structuredLogRegEx, hitFailpointFunc, duringFailpointFunc, stopKey) {
    clearRawMongoProgramOutput();
    assert.commandWorked(testDB.adminCommand({
        configureFailPoint: failPointName,
        mode: "alwaysOn",
        data: {fieldsToMatch: {i: stopKey}}
    }));

    hitFailpointFunc();

    assert.soon(() => structuredLogRegEx.test(rawMongoProgramOutput()));

    duringFailpointFunc();

    assert.commandWorked(testDB.adminCommand({configureFailPoint: failPointName, mode: "off"}));
};

const docsToInsert = 1000;
let setUp = function(coll) {
    coll.drop();

    let bulk = coll.initializeUnorderedBulkOp();
    for (let i = 0; i < docsToInsert; i++) {
        bulk.insert({i: i});
    }
    assert.commandWorked(bulk.execute());
};

let buildIndexInBackground = function(coll, expectDuplicateKeyError) {
    const createIndexFunction = function(collFullName) {
        const coll = db.getMongo().getCollection(collFullName);
        return coll.createIndex({i: 1}, {background: true, unique: true});
    };
    const assertFunction = expectDuplicateKeyError ? function(collFullName) {
        assert.commandFailedWithCode(createIndexFunction(collFullName), ErrorCodes.DuplicateKey);
    } : function(collFullName) {
        assert.commandWorked(createIndexFunction(collFullName));
    };
    return startParallelShell('const createIndexFunction = ' + createIndexFunction + ';\n' +
                                  'const assertFunction = ' + assertFunction + ';\n' +
                                  'assertFunction("' + coll.getFullName() + '")',
                              conn.port);
};

/**
 * Run a background index build on a unique index under different configurations. Introduce
 * duplicate keys on the index that may cause it to fail or succeed, depending on the following
 * optional parameters:
 * {
 *   // Which operation used to introduce a duplicate key.
 *   operation {string}: "insert", "update"
 *
 *   // Whether or not resolve the duplicate key before completing the build.
 *   resolve {bool}
 *
 *   // Which phase of the index build to introduce the duplicate key.
 *   phase {number}: 0-4
 * }
 */
let runTest = function(config) {
    jsTestLog("running test with config: " + tojson(config));

    const collName = Object.keys(config).length
        ? 'hybrid_' + config.operation[0] + '_r' + Number(config.resolve) + '_p' + config.phase
        : 'hybrid';
    const coll = testDB.getCollection(collName);
    setUp(coll);

    // Expect the build to fail with a duplicate key error if we insert a duplicate key and
    // don't resolve it.
    let expectDuplicate = config.resolve === false;

    let awaitBuild;
    let buildIndex = function() {
        awaitBuild = buildIndexInBackground(coll, expectDuplicate);
    };

    // Introduce a duplicate key, either from an insert or update. Optionally, follow-up with an
    // operation that will resolve the duplicate by removing it or updating it.
    const dup = {i: 0};
    let doOperation = function() {
        if ("insert" == config.operation) {
            assert.commandWorked(coll.insert(dup));
            if (config.resolve) {
                assert.commandWorked(coll.deleteOne(dup));
            }
        } else if ("update" == config.operation) {
            assert.commandWorked(coll.update(dup, {i: 1}));
            if (config.resolve) {
                assert.commandWorked(coll.update({i: 1}, dup));
            }
        }
    };

    const stopKey = 0;
    switch (config.phase) {
        // Just build the index without any failpoints.
        case undefined:
            buildIndex();
            break;
        // Hang before scanning the first document.
        case 0:
            doDuringFailpoint(
                "hangIndexBuildDuringCollectionScanPhaseBeforeInsertion",
                new RegExp("\"id\":20386.*\"where\":\"before\",\"doc\":.*\"i\":" + stopKey),
                buildIndex,
                doOperation,
                stopKey);
            break;
        // Hang after scanning the first document.
        case 1:
            doDuringFailpoint(
                "hangIndexBuildDuringCollectionScanPhaseAfterInsertion",
                new RegExp("\"id\":20386.*\"where\":\"after\",\"doc\":.*\"i\":" + stopKey),
                buildIndex,
                doOperation,
                stopKey);
            break;
        // Hang before the first drain and after dumping the keys from the external sorter into
        // the index.
        case 2:
            doDuringFailpoint("hangAfterIndexBuildDumpsInsertsFromBulk",
                              new RegExp("\"id\":20665"),
                              buildIndex,
                              doOperation);
            break;
        // Hang before the second drain.
        case 3:
            doDuringFailpoint("hangAfterIndexBuildFirstDrain",
                              new RegExp("\"id\":20666"),
                              buildIndex,
                              doOperation);
            break;
        // Hang before the final drain and commit.
        case 4:
            doDuringFailpoint("hangAfterIndexBuildSecondDrain",
                              new RegExp("\"id\":20667"),
                              buildIndex,
                              doOperation);
            break;
        default:
            assert(false, "Invalid phase: " + config.phase);
    }

    awaitBuild();

    let expectedDocs = docsToInsert;
    expectedDocs += (config.operation == "insert" && config.resolve === false) ? 1 : 0;

    assert.eq(expectedDocs, coll.count());
    assert.eq(expectedDocs, coll.find().itcount());
    assert.commandWorked(coll.validate({full: true}));
};

runTest({});

for (let i = 0; i <= 4; i++) {
    runTest({operation: "insert", resolve: true, phase: i});
    runTest({operation: "insert", resolve: false, phase: i});
    runTest({operation: "update", resolve: true, phase: i});
    runTest({operation: "update", resolve: false, phase: i});
}

replSetTest.stopSet();
})();