summaryrefslogtreecommitdiff
path: root/jstests/sharding/analyze_shard_key/num_orphan_docs.js
blob: 76e0d49e03a75d6b150240081a88ef7919684fe6 (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
/**
 * Tests that the analyzeShardKey command only returns the number of orphan documents if the
 * collection is sharded.
 *
 * @tags: [requires_fcv_70]
 */
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");
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 testAnalyzeShardKeyUnshardedCollection(conn) {
    const dbName = "testDb";
    const collName = "testCollUnsharded";
    const ns = dbName + "." + collName;
    const coll = conn.getCollection(ns);

    const candidateKey = {candidateKey: 1};
    assert.commandWorked(coll.createIndex(candidateKey));
    const docs = [{candidateKey: 1}];
    assert.commandWorked(coll.insert(docs, {writeConcern}));

    const res = assert.commandWorked(conn.adminCommand({analyzeShardKey: ns, key: candidateKey}));
    AnalyzeShardKeyUtil.assertKeyCharacteristicsMetrics(res, {
        numDocs: 1,
        isUnique: false,
        numDistinctValues: 1,
        mostCommonValues: [{value: {candidateKey: 1}, frequency: 1}],
        numMostCommonValues
    });
    assert(!res.hasOwnProperty("numOrphanDocs"), res);
    assert(!res.hasOwnProperty("note"), res);

    assert(coll.drop());
}

function testAnalyzeShardKeyShardedCollection(st) {
    const dbName = "testDb";
    const collName = "testCollSharded";
    const ns = dbName + "." + collName;
    const coll = st.s.getCollection(ns);
    const currentKey = {currentKey: 1};
    const candidateKey = {candidateKey: 1};
    const docs = [
        {currentKey: -10, candidateKey: -100},
        {currentKey: -5, candidateKey: -50},
        {currentKey: 0, candidateKey: 0},
        {currentKey: 5, candidateKey: 50},
        {currentKey: 10, candidateKey: 100}
    ];

    assert.commandWorked(coll.createIndex(currentKey));
    assert.commandWorked(coll.createIndex(candidateKey));
    assert.commandWorked(coll.insert(docs, {writeConcern}));

    assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
    st.ensurePrimaryShard(dbName, st.shard0.shardName);
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: currentKey}));

    // Analyze a shard key while no shards have orphan documents. Chunk distribution:
    // shard0: [MinKey, 0]
    // shard1: [0, MaxKey]
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {currentKey: 0}}));
    assert.commandWorked(st.s.adminCommand(
        {moveChunk: ns, find: {currentKey: 0}, to: st.shard1.shardName, _waitForDelete: true}));
    let res = assert.commandWorked(st.s.adminCommand({analyzeShardKey: ns, key: candidateKey}));
    AnalyzeShardKeyUtil.assertKeyCharacteristicsMetrics(res, {
        numDocs: 5,
        isUnique: false,
        numDistinctValues: 5,
        mostCommonValues: [
            {value: {candidateKey: -100}, frequency: 1},
            {value: {candidateKey: -50}, frequency: 1},
            {value: {candidateKey: 0}, frequency: 1},
            {value: {candidateKey: 50}, frequency: 1},
            {value: {candidateKey: 100}, frequency: 1}
        ],
        numMostCommonValues
    });
    assert(res.hasOwnProperty("numOrphanDocs"), res);
    assert.eq(res.numOrphanDocs, 0, res);

    // Pause range deletion on both shards.
    let suspendRangeDeletionFp0 = configureFailPoint(st.shard0, "suspendRangeDeletion");
    let suspendRangeDeletionFp1 = configureFailPoint(st.shard1, "suspendRangeDeletion");

    // Analyze a shard key while one shard has orphan documents. Chunk distribution:
    // shard0: [MinKey, -5]
    // shard1: [-5, 0], [0, MaxKey]
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {currentKey: -5}}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {currentKey: -5}, to: st.shard1.shardName}));
    res = assert.commandWorked(st.s.adminCommand({analyzeShardKey: ns, key: candidateKey}));
    AnalyzeShardKeyUtil.assertKeyCharacteristicsMetrics(res, {
        numDocs: 6,
        isUnique: false,
        numDistinctValues: 5,
        mostCommonValues: [
            {value: {candidateKey: -50}, frequency: 2},
            {value: {candidateKey: -100}, frequency: 1},
            {value: {candidateKey: 0}, frequency: 1},
            {value: {candidateKey: 50}, frequency: 1},
            {value: {candidateKey: 100}, frequency: 1}
        ],
        numMostCommonValues
    });
    assert(res.hasOwnProperty("numOrphanDocs"), res);
    assert.eq(res.numOrphanDocs, 1, res);

    // Analyze a shard key while two shards have orphan documents. Chunk distribution:
    // shard0: [MinKey, -5], [5, MaxKey]
    // shard1: [-5, 0], [0, 5]
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {currentKey: 5}}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {currentKey: 5}, to: st.shard0.shardName}));
    res = assert.commandWorked(st.s.adminCommand({analyzeShardKey: ns, key: candidateKey}));
    AnalyzeShardKeyUtil.assertKeyCharacteristicsMetrics(res, {
        numDocs: 8,
        isUnique: false,
        numDistinctValues: 5,
        mostCommonValues: [
            {value: {candidateKey: -50}, frequency: 2},
            {value: {candidateKey: 50}, frequency: 2},
            {value: {candidateKey: 100}, frequency: 2},
            {value: {candidateKey: -100}, frequency: 1},
            {value: {candidateKey: 0}, frequency: 1}
        ],
        numMostCommonValues
    });
    assert(res.hasOwnProperty("numOrphanDocs"), res);
    assert.eq(res.numOrphanDocs, 3, res);
    assert(res.hasOwnProperty("note"), res);

    suspendRangeDeletionFp0.off();
    suspendRangeDeletionFp1.off();
    assert(coll.drop());
}

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: 2, rs: {nodes: numNodesPerRS, setParameter: setParameterOpts}});

    testAnalyzeShardKeyUnshardedCollection(st.s);
    testAnalyzeShardKeyShardedCollection(st);

    st.stop();
}

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

    testAnalyzeShardKeyUnshardedCollection(primary);

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