summaryrefslogtreecommitdiff
path: root/jstests/sharding/in_memory_sort_limit.js
blob: 6be5d38f0132c6aee3aee8b8e784f570f5b30fc6 (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
// Test SERVER-14306.  Do a query directly against a mongod with an in-memory sort and a limit that
// doesn't cause the in-memory sort limit to be reached, then make sure the same limit also doesn't
// cause the in-memory sort limit to be reached when running through a mongos.
(function() {
'use strict';

var st = new ShardingTest({
    shards: 2,
    other: {
        shardOptions:
            {setParameter: {internalQueryMaxBlockingSortMemoryUsageBytes: 32 * 1024 * 1024}}
    }
});
assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.shardName);

// Make sure that at least 1 chunk is on another shard so that mongos doesn't treat this as a
// single-shard query (which doesn't exercise the bug)
assert.commandWorked(
    st.s.adminCommand({shardCollection: 'test.skip', key: {_id: 'hashed'}, numInitialChunks: 64}));

var mongosCol = st.s.getDB('test').getCollection('skip');
var shardCol = st.shard0.getDB('test').getCollection('skip');

// Create enough data to exceed the 32MB in-memory sort limit (per shard)
var filler = new Array(10240).toString();
var bulkOp = mongosCol.initializeOrderedBulkOp();
for (var i = 0; i < 12800; i++) {
    bulkOp.insert({x: i, str: filler});
}
assert.commandWorked(bulkOp.execute());

var passLimit = 2000;
var failLimit = 4000;

// Test on MongoD
jsTestLog("Test no error with limit of " + passLimit + " on mongod");
assert.eq(passLimit, shardCol.find().sort({x: 1}).allowDiskUse(false).limit(passLimit).itcount());

jsTestLog("Test error with limit of " + failLimit + " on mongod");
assert.throwsWithCode(
    () => shardCol.find().sort({x: 1}).allowDiskUse(false).limit(failLimit).itcount(),
    ErrorCodes.QueryExceededMemoryLimitNoDiskUseAllowed);

// Test on MongoS
jsTestLog("Test no error with limit of " + passLimit + " on mongos");
assert.eq(passLimit, mongosCol.find().sort({x: 1}).allowDiskUse(false).limit(passLimit).itcount());

jsTestLog("Test error with limit of " + failLimit + " on mongos");
assert.throwsWithCode(
    () => mongosCol.find().sort({x: 1}).allowDiskUse(false).limit(failLimit).itcount(),
    ErrorCodes.QueryExceededMemoryLimitNoDiskUseAllowed);

st.stop();
})();