summaryrefslogtreecommitdiff
path: root/jstests/sharding/analyze_shard_key/cardinality_and_frequency_index_selection.js
blob: df2749c10fabe6d0772be7b0cbc31f09f6ee63d5 (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
/**
 * Tests that the cardinality and frequency metrics calculation within the analyzeShardKey command
 * prioritizes indexes that allows it to infer if the shard key is unique.
 *
 * @tags: [requires_fcv_70]
 */
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");
load("jstests/libs/uuid_util.js");  // for 'extractUUIDFromObject'
load("jstests/sharding/analyze_shard_key/libs/analyze_shard_key_util.js");

const numNodesPerRS = 2;
const numMostCommonValues = 5;

// The write concern to use when inserting documents into test collections. Waiting for the
// documents to get replicated to all nodes is necessary since mongos runs the analyzeShardKey
// command with readPreference "secondaryPreferred".
const writeConcern = {
    w: numNodesPerRS
};

function testAnalyzeShardKey(conn, {docs, indexSpecs, shardKeys, metrics}) {
    const dbName = "testDb-" + extractUUIDFromObject(UUID());
    const collName = "testColl";
    const ns = dbName + "." + collName;
    jsTest.log(`Testing ${tojson({dbName, collName, docs, indexSpecs, shardKeys, metrics})}`);

    const db = conn.getDB(dbName);
    const coll = db.getCollection(collName);

    assert.commandWorked(db.runCommand({createIndexes: collName, indexes: indexSpecs}));
    assert.commandWorked(coll.insert(docs, {writeConcern}));

    for (let shardKey of shardKeys) {
        const res = assert.commandWorked(conn.adminCommand({analyzeShardKey: ns, key: shardKey}));
        AnalyzeShardKeyUtil.assertKeyCharacteristicsMetrics(res, metrics);
    }
}

function runTest(conn) {
    testAnalyzeShardKey(conn, {
        docs: [{x: -1}, {x: 1}],
        indexSpecs: [
            {
                name: "x_hashed",
                key: {x: "hashed"},
            },
            {
                name: "x_1",
                key: {x: 1},
                unique: true,
            }
        ],
        shardKeys: [{x: 1}, {x: "hashed"}],
        metrics: {
            numDocs: 2,
            isUnique: true,
            numDistinctValues: 2,
            mostCommonValues: [{value: {x: -1}, frequency: 1}, {value: {x: 1}, frequency: 1}],
            numMostCommonValues
        }
    });

    testAnalyzeShardKey(conn, {
        docs: [{x: -1}, {x: 1}],
        indexSpecs: [
            {
                name: "x_1_not_unique",
                key: {x: 1},
            },
            {
                name: "x_1_unique",
                key: {x: 1},
                unique: true,
            }
        ],
        shardKeys: [{x: 1}, {x: "hashed"}],
        metrics: {
            numDocs: 2,
            isUnique: true,
            numDistinctValues: 2,
            mostCommonValues: [{value: {x: -1}, frequency: 1}, {value: {x: 1}, frequency: 1}],
            numMostCommonValues
        }
    });

    testAnalyzeShardKey(conn, {
        docs: [{x: -1, y: -1}, {x: 1, y: 1}],
        indexSpecs: [
            {
                name: "x_1_y_1",
                key: {x: 1, y: 1},
                unique: true,
            },
            {
                name: "x_1",
                key: {x: 1},
            }
        ],
        shardKeys: [{x: 1}, {x: "hashed"}],
        metrics: {
            numDocs: 2,
            isUnique: false,
            numDistinctValues: 2,
            mostCommonValues: [{value: {x: -1}, frequency: 1}, {value: {x: 1}, frequency: 1}],
            numMostCommonValues
        }
    });

    testAnalyzeShardKey(conn, {
        docs: [{x: -1, y: -1, z: -1}, {x: 1, y: 1, z: 1}],
        indexSpecs: [
            {
                name: "x_1_y_1_z_1",
                key: {x: 1, y: 1, z: 1},
                unique: true,
            },
            {
                name: "x_1_y_1",
                key: {x: 1, y: 1},
                unique: true,
            }
        ],
        shardKeys: [{x: 1, y: 1}, {x: "hashed", y: 1}],
        metrics: {
            numDocs: 2,
            isUnique: true,
            numDistinctValues: 2,
            mostCommonValues:
                [{value: {x: -1, y: -1}, frequency: 1}, {value: {x: 1, y: 1}, frequency: 1}],
            numMostCommonValues
        }
    });
}

const setParameterOpts = {
    analyzeShardKeyNumMostCommonValues: numMostCommonValues,
    // Skip calculating the read and write distribution metrics since there are no sampled queries
    // anyway.
    "failpoint.analyzeShardKeySkipCalcalutingReadWriteDistributionMetrics":
        tojson({mode: "alwaysOn"})

};

{
    const st =
        new ShardingTest({shards: 1, rs: {nodes: numNodesPerRS, setParameter: setParameterOpts}});

    runTest(st.s);

    st.stop();
}

{
    const rst =
        new ReplSetTest({nodes: numNodesPerRS, nodeOptions: {setParameter: setParameterOpts}});
    rst.startSet();
    rst.initiate();
    const primary = rst.getPrimary();

    runTest(primary);

    rst.stopSet();
}
})();