summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/validate_hashed_index.js
blob: e2fed4a37e9201659b45ec1e4499ff22fb3775f1 (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
/**
 * Tests that validate completes without warnings on a collection with a hashed index after a
 * failed insert (due to hashed index restrictions).
 * @tags: [
 *     requires_replication,
 * ]
 */
(function() {
'use strict';

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

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

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

assert.commandWorked(testColl.createIndex({a: 'hashed'}));

// Log WiredTigerRecoveryUnit timestamps during failed insert.
const previousLogLevel = primary.setLogLevel(3, 'storage').was.storage.verbosity;

// 16766 is the error code returned by ExpressionKeysPrivate::getHashKeys() for
// "hashed indexes do not currently support array values".
assert.commandFailedWithCode(testColl.insert({_id: 0, a: ['a', 'b', 'c']}), 16766);

primary.setLogLevel(previousLogLevel, 'storage');

jsTestLog('Checking documents in collection');
let docs = testColl.find().sort({_id: 1}).toArray();
assert.eq(0, docs.length, 'too many docs in collection: ' + tojson(docs));

jsTestLog('Validating collection until we no longer see "transient - collection in use" warnings');
assert.soon(() => {
    const result = assert.commandWorked(testColl.validate({full: true}));
    jsTestLog('Validation result: ' + tojson(result));
    assert.eq(testColl.getFullName(), result.ns);
    assert.eq(0, result.nInvalidDocuments);
    assert.eq(0, result.nrecords);
    assert.eq(2, result.nIndexes);

    assert.eq(0, result.keysPerIndex._id_);
    assert.eq(0, result.keysPerIndex.a_hashed);
    assert(result.indexDetails._id_.valid);
    assert(result.indexDetails.a_hashed.valid);

    assert(result.valid);

    if (result.hasOwnProperty('warnings') && result.warnings.length > 0) {
        jsTestLog('Validation reported warnings - retrying after fsync: ' +
                  tojson(result.warnings));
        assert.commandWorked(primary.adminCommand({fsync: 1}));
        return false;
    }

    return true;
}, 'full validation reported warnings');

rst.stopSet();
})();