summaryrefslogtreecommitdiff
path: root/jstests/sharding/libs/track_unsharded_collections_helpers.js
blob: 739118f9c0b88806ae52f38a551c9489744ee27f (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
/**
 * Common helpers for testing operations on unsharded collections.
 */

function getNewNs(dbName) {
    if (typeof getNewNs.counter == 'undefined') {
        getNewNs.counter = 0;
    }
    getNewNs.counter++;
    const collName = "ns" + getNewNs.counter;
    return [collName, dbName + "." + collName];
}

function checkInStorageCatalog({dbName, collName, type, shardConn}) {
    const query = {name: collName, type};
    assert.eq(1,
              shardConn.getDB(dbName).getCollectionInfos(query).length,
              "Current contents of storage catalog on " + shardConn + ": " +
                  tojson(shardConn.getDB(dbName).getCollectionInfos()) +
                  ", query: " + tojson(query));
}

function checkNotInStorageCatalog({dbName, collName, shardConn}) {
    const query = {name: collName};
    assert.eq(0,
              shardConn.getDB(dbName).getCollectionInfos(query).length,
              "Current contents of storage catalog on " + shardConn + ": " +
                  tojson(shardConn.getDB(dbName).getCollectionInfos()) +
                  ", query: " + tojson(query));
}

function checkInShardingCatalog({ns, shardKey, unique, distributionMode, numChunks, mongosConn}) {
    const configDB = mongosConn.getDB("config");

    // Check the collection entry.
    const collQuery = {};
    collQuery._id = ns;
    collQuery["key." + shardKey] = 1;
    collQuery.unique = unique;
    collQuery.distributionMode = distributionMode;
    assert.neq(null,
               configDB.collections.findOne(collQuery),
               "Current contents of config.collections: " +
                   tojson(configDB.collections.find().toArray()) + ", query: " + tojson(collQuery));

    // Check the chunk entries.
    const chunkQuery = {};
    chunkQuery.ns = ns;
    chunkQuery["min." + shardKey] = {$exists: true};
    chunkQuery["max." + shardKey] = {$exists: true};
    assert.eq(numChunks,
              configDB.chunks.count(chunkQuery),
              "Current contents of config.chunks: " + tojson(configDB.chunks.find().toArray()) +
                  ", query: " + tojson(chunkQuery));
}

function checkNotInShardingCatalog({ns, mongosConn}) {
    const configDB = mongosConn.getDB("config");

    assert.eq(
        null,
        configDB.collections.findOne({_id: ns}),
        "Current contents of config.collections: " + tojson(configDB.collections.find().toArray()));
    assert.eq(0,
              configDB.chunks.count({ns: ns}),
              "Current contents of config.chunks: " + tojson(configDB.chunks.find().toArray()));
}

function setFailpoint(failpointName, conn) {
    assert.commandWorked(conn.adminCommand({
        configureFailPoint: failpointName,
        mode: "alwaysOn",
    }));
}

function unsetFailpoint(failpointName, conn) {
    assert.commandWorked(conn.adminCommand({
        configureFailPoint: failpointName,
        mode: "off",
    }));
}