summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/validate_multikey_stepdown.js
blob: 9ca5012a6564ca896b2df63388084d9e13ec1861 (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
/**
 * Validates multikey index in a collection after retrying a 2dsphere insert.
 * The scenario tested here involves stepping down the node so that the first insert attempt fails.
 * The insert is retried after the node becomes primary again. At this point, we will validate the
 * collection after restarting the node.
 * @tags: [
 *     requires_replication,
 *     requires_persistence,
 * ]
 */
(function() {
'use strict';

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

const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();

let primary = rst.getPrimary();
let testColl = primary.getCollection('test.validate_multikey_stepdown');

assert.commandWorked(testColl.getDB().createCollection(testColl.getName()));

assert.commandWorked(testColl.createIndex({geo: '2dsphere'}));

// Sample polygon and point from geo_s2dedupnear.js
const polygon = {
    type: 'Polygon',
    coordinates: [[
        [100.0, 0.0],
        [101.0, 0.0],
        [101.0, 1.0],
        [100.0, 1.0],
        [100.0, 0.0],
    ]],
};
const point = {
    type: 'Point',
    coordinates: [31, 41],
};
const geoDocToInsert = {
    _id: 1,
    geo: polygon,
};
const geoQuery = {
    geo: {$geoNear: point}
};

const failPoint = configureFailPoint(
    primary, 'hangAfterCollectionInserts', {collectionNS: testColl.getFullName()});

let awaitInsert;
try {
    const args = [testColl.getDB().getName(), testColl.getName(), geoDocToInsert];
    let func = function(args) {
        const [dbName, collName, geoDocToInsert] = args;
        jsTestLog("Insert a document that will hang before the insert completes.");
        const testColl = db.getSiblingDB(dbName)[collName];
        // This should fail with ErrorCodes.InterruptedDueToReplStateChange.
        const result = testColl.insert(geoDocToInsert);
        jsTestLog('Async insert result = ' + tojson(result));
        assert.commandFailedWithCode(result, ErrorCodes.InterruptedDueToReplStateChange);
    };
    awaitInsert = startParallelShell(funWithArgs(func, args), primary.port);

    jsTest.log("Wait for async insert to hit the failpoint.");
    failPoint.wait();

    // Step down the primary. This will interrupt the async insert.
    // Since there is no other electable node, the replSetStepDown command will time out and the
    // node will be re-elected.
    jsTest.log("Step down primary temporarily to interrupt async insert.");
    assert.commandFailedWithCode(primary.adminCommand({replSetStepDown: 10}),
                                 ErrorCodes.ExceededTimeLimit);
} finally {
    // Turn off the failpoint before allowing the test to end, so nothing hangs while the server
    // shuts down or in post-test hooks.
    failPoint.off();
}

// Wait until the async insert is completed.
jsTest.log("Wait for async insert to complete.");
awaitInsert();

jsTest.log("Retrying insert after stepping up.");
assert.commandWorked(testColl.insert(geoDocToInsert));

jsTestLog('Checking documents in collection before restart');
let docs = testColl.find(geoQuery).sort({_id: 1}).toArray();
assert.eq(1, docs.length, 'too many docs in collection: ' + tojson(docs));
assert.eq(
    geoDocToInsert._id, docs[0]._id, 'unexpected document content in collection: ' + tojson(docs));

// For the purpose of reproducing the validation error in geo_2dsphere, it is important to skip
// validation when restarting the primary node. Enabling validation here has an effect on the
// validate command's behavior after restarting.
primary = rst.restart(primary, {skipValidation: true}, /*signal=*/undefined, /*wait=*/true);
testColl = primary.getCollection(testColl.getFullName());

jsTestLog('Checking documents in collection after restart');
rst.awaitReplication();
docs = testColl.find(geoQuery).sort({_id: 1}).toArray();
assert.eq(1, docs.length, 'too many docs in collection: ' + tojson(docs));
assert.eq(
    geoDocToInsert._id, docs[0]._id, 'unexpected document content in collection: ' + tojson(docs));

jsTestLog('Validating collection after restart');
const result = assert.commandWorked(testColl.validate({full: true}));

jsTestLog('Validation result: ' + tojson(result));
assert.eq(testColl.getFullName(), result.ns, tojson(result));
assert.eq(0, result.nInvalidDocuments, tojson(result));
assert.eq(1, result.nrecords, tojson(result));
assert.eq(2, result.nIndexes, tojson(result));

// Check non-geo indexes.
assert.eq(1, result.keysPerIndex._id_, tojson(result));
assert(result.indexDetails._id_.valid, tojson(result));

// Check geo index.
assert.lt(1, result.keysPerIndex.geo_2dsphere, tojson(result));
assert(result.indexDetails.geo_2dsphere.valid, tojson(result));

assert(result.valid, tojson(result));

rst.stopSet();
})();