summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/resumable_index_build_clearing_tmp_directory_on_restart.js
blob: c94db0b2ee7ea02a51e6aafddfe66dcc33e724ed (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
/**
 * Tests that a resumable index build clears "_tmp" directory except for files for the build on
 * startup recovery.
 *
 * @tags: [
 *   requires_majority_read_concern,
 *   requires_persistence,
 *   requires_replication,
 * ]
 */
(function() {
"use strict";

load("jstests/noPassthrough/libs/index_build.js");
load("jstests/libs/sbe_util.js");          // For checkSBEEnabled.
load("jstests/libs/columnstore_util.js");  // For setUpServerForColumnStoreIndexTest.

const dbName = "test";

const numDocuments = 100;
const maxIndexBuildMemoryUsageMB = 50;

const rst = new ReplSetTest({
    nodes: 1,
    nodeOptions: {setParameter: {maxIndexBuildMemoryUsageMegabytes: maxIndexBuildMemoryUsageMB}}
});
rst.startSet();
rst.initiate();

// Insert enough data so that the collection scan spills to disk.
const primary = rst.getPrimary();
const columnstoreEnabled =
    checkSBEEnabled(primary.getDB(dbName), ["featureFlagColumnstoreIndexes"], true) &&
    setUpServerForColumnStoreIndexTest(primary.getDB(dbName));
const coll = primary.getDB(dbName).getCollection(jsTestName());
const bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < numDocuments; i++) {
    // Each document is at least 1 MB.
    bulk.insert({a: i.toString().repeat(1024 * 1024)});
}
assert.commandWorked(bulk.execute());

// Manually writes a garbage file to "_tmp" directory.
const tmpDir = primary.dbpath + "/_tmp/";
mkdir(tmpDir);
writeFile(tmpDir + "garbage", "");

// Runs a resumable index build till completed to make sure the spilled files in "_tmp" directory
// are not deleted with the garbage file.

ResumableIndexBuildTest.run(
    rst,
    dbName,
    coll.getName(),
    columnstoreEnabled ? [[{"$**": "columnstore"}]] : [[{a: 1}]],
    [{name: "hangIndexBuildDuringCollectionScanPhaseBeforeInsertion", logIdWithBuildUUID: 20386}],
    // Each document is at least 1 MB, so the index build must have spilled to disk by this point.
    maxIndexBuildMemoryUsageMB,
    ["collection scan"],
    [{numScannedAfterResume: numDocuments - maxIndexBuildMemoryUsageMB}]);

// Asserts the garbage file is deleted.
const files = listFiles(tmpDir);
assert.eq(files.length, 0, files);

rst.stopSet();
})();