-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathindex_build_memory_tracking.js
59 lines (50 loc) · 1.77 KB
/
index_build_memory_tracking.js
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
/**
* Builds an index with many large keys and ensures that we stay below the memory limit.
*/
import {ReplSetTest} from "jstests/libs/replsettest.js";
const maxMemUsageMB = 50;
const replSet = new ReplSetTest({
nodes: 1,
nodeOptions: {
setParameter: {
maxIndexBuildMemoryUsageMegabytes: maxMemUsageMB,
}
},
});
replSet.startSet();
replSet.initiate();
const primary = replSet.getPrimary();
const testDB = primary.getDB('test');
const coll = testDB[jsTestName()];
const docs = 50;
const bigArr = new Array(100000).fill('x'.repeat(10));
const batchSize = 10;
const padding = 'x'.repeat(1000);
let d = 0;
while (d < docs) {
const bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < batchSize; i++) {
bulk.insert({a: (d + i) + padding, arr: bigArr});
}
d += batchSize;
assert.commandWorked(bulk.execute());
}
// While we build the index, we must never exceed our memory limit.
const awaitShell = startParallelShell(() => {
while (!db.getSiblingDB('test').signal.findOne()) {
const memUsage = assert.commandWorked(db.serverStatus()).indexBulkBuilder.memUsage;
print("mem usage: " + memUsage);
// The index build memory usage is actually a high-water mark. Include a buffer into our
// assertion to account of any extra memory usage before spilling.
const memBuffer = 1 * 1024 * 1024;
const maxMemUsage = 50 * 1024 * 1024;
assert.lte(memUsage, maxMemUsage + memBuffer);
sleep(500);
}
}, primary.port);
// Build a wildcard index that generates many large keys, forcing the index build to spill
// multiple times.
assert.commandWorked(coll.createIndex({"$**": 1}));
assert.commandWorked(testDB.signal.insert({done: true}));
awaitShell();
replSet.stopSet();