summaryrefslogtreecommitdiff
path: root/jstests/sharding/autosplit_configure_collection.js
blob: 83acde55afdf96947f3ac03f8278a044fb00908e (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
/**
 * This test confirms that chunks get split according to a collection specific setting as they grow
 * due to data insertion.
 *
 * @tags: [
 *  requires_fcv_53,
 *  featureFlagPerCollBalancingSettings,
 * ]
 */
(function() {
'use strict';
load('jstests/sharding/autosplit_include.js');
load("jstests/sharding/libs/find_chunks_util.js");
load("jstests/libs/feature_flag_util.js");  // for FeatureFlagUtil.isEnabled

let st = new ShardingTest({
    name: "auto1",
    shards: 2,
    mongos: 1,
    other: {enableAutoSplit: true},
});

// TODO SERVER-66652 remove this test after 7.0 branches out
const noMoreAutoSplitterFeatureFlag =
    FeatureFlagUtil.isEnabled(st.configRS.getPrimary().getDB('admin'), "NoMoreAutoSplitter");
if (noMoreAutoSplitterFeatureFlag) {
    jsTestLog("Skipping as featureFlagNoMoreAutosplitter is enabled");
    st.stop();
    return;
}

const fullNS = "test.foo";
const bigString = "X".repeat(1024 * 1024 / 16);  // 65 KB

assert.commandWorked(st.s0.adminCommand({enablesharding: "test"}));
st.ensurePrimaryShard('test', st.shard1.shardName);
assert.commandWorked(st.s0.adminCommand({shardcollection: fullNS, key: {num: 1}}));

let db = st.getDB("test");
let coll = db.foo;

let i = 0;

// Inserts numDocs documents into the collection, waits for any ongoing
// splits to finish, and then prints some information about the
// collection's chunks
function insertDocsAndWaitForSplit(numDocs) {
    let bulk = coll.initializeUnorderedBulkOp();
    let curMaxKey = i;
    // Increment the global 'i' variable to keep 'num' unique across all
    // documents
    for (; i < curMaxKey + numDocs; i++) {
        bulk.insert({num: i, s: bigString});
    }
    assert.commandWorked(bulk.execute());

    waitForOngoingChunkSplits(st);

    st.printChunks();
    st.printChangeLog();
}

let configDB = db.getSiblingDB('config');

jsTest.log("Testing enableAutoSplitter == false, chunkSize=unset ...");
{
    assert.commandWorked(
        st.s0.adminCommand({configureCollectionBalancing: fullNS, enableAutoSplitter: false}));

    let configColl = configDB.collections.findOne({_id: fullNS});

    // Check that noAutoSplit has been set to 'true' on the configsvr config.collections
    assert.eq(true, configColl.noAutoSplit);
    assert.eq(configColl.maxChunkSizeBytes, undefined);

    // Accumulate ~1MB of documents
    insertDocsAndWaitForSplit(16);

    // No split should have been performed
    assert.eq(
        findChunksUtil.countChunksForNs(st.config, fullNS), 1, "Number of chunks is more than one");
}

jsTest.log("Testing enableAutoSplitter == true, chunkSize=unset ...");
{
    assert.commandWorked(
        st.s0.adminCommand({configureCollectionBalancing: fullNS, enableAutoSplitter: true}));

    let configColl = configDB.collections.findOne({_id: fullNS});

    // Check that noAutoSplit has been set to 'true' on the configsvr config.collections
    assert.eq(false, configColl.noAutoSplit);
    assert.eq(configColl.maxChunkSizeBytes, undefined);

    // Add ~1MB of documents
    insertDocsAndWaitForSplit(16);

    // No split should have been performed
    assert.eq(
        findChunksUtil.countChunksForNs(st.config, fullNS), 1, "Number of chunks is more than one");
}

jsTest.log("Testing enableAutoSplitter == false, chunkSize=1 ...");
{
    assert.commandWorked(st.s0.adminCommand(
        {configureCollectionBalancing: fullNS, enableAutoSplitter: false, chunkSize: 1}));

    let configColl = configDB.collections.findOne({_id: fullNS});

    // Check that noAutoSplit has been set to 'true' on the configsvr config.collections
    assert.eq(true, configColl.noAutoSplit);
    assert.eq(configColl.maxChunkSizeBytes, 1024 * 1024);

    // Reach ~3MB of documents  total
    insertDocsAndWaitForSplit(16);

    assert.eq(16 * 3, db.foo.find().itcount());

    // No split should have been performed
    assert.eq(
        findChunksUtil.countChunksForNs(st.config, fullNS), 1, "Number of chunks is more than one");
}

jsTest.log("Testing enableAutoSplitter == true, chunkSize=10 ...");
{
    assert.commandWorked(st.s0.adminCommand(
        {configureCollectionBalancing: fullNS, enableAutoSplitter: true, chunkSize: 10}));

    let configColl = configDB.collections.findOne({_id: fullNS});

    // Check that noAutoSplit has been unset and chunkSizeBytes is set to 10 MB.
    assert.eq(configColl.noAutoSplit, false);
    assert.eq(configColl.maxChunkSizeBytes, 10 * 1024 * 1024);

    // Add ~20MB of documents
    insertDocsAndWaitForSplit(16 * 20);
    assert.gte(findChunksUtil.countChunksForNs(st.config, fullNS),
               2,
               "Number of chunks is less then 2, no split have been perfomed");
    assert.eq(16 * (2 + 1 + 20), db.foo.find().itcount());

    // Add ~10MB of documents
    insertDocsAndWaitForSplit(16 * 10);
    assert.gte(findChunksUtil.countChunksForNs(st.config, fullNS),
               3,
               "Number of chunks is less then 3, no split have been perfomed");
    assert.eq(16 * (2 + 1 + 20 + 10), db.foo.find().itcount());
}

printjson(db.stats());

st.stop();
})();