summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/batched_multi_deletes_params.js
blob: 141164f389a13cc6635a4d586769856f1c5c1b94 (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
/**
 * Validate batched multi-deleter's parameters.
 *
 * @tags: [
 *  requires_fcv_61,
 *  requires_replication,
 * ]
 */

(function() {
"use strict";
load("jstests/libs/fail_point_util.js");  // For 'configureFailPoint()'

const rst = new ReplSetTest({
    nodes: 2,
});
rst.startSet();
rst.initiate();
rst.awaitNodesAgreeOnPrimary();
const conn = rst.getPrimary();

const db = conn.getDB("test");
const coll = db.getCollection("c");

function validateTargetDocsPerBatch() {
    const collCount = 1234;

    assert.commandWorked(db.adminCommand({setParameter: 1, batchedDeletesTargetBatchTimeMS: 0}));

    for (let docsPerBatch of [0, 1, 20, 100]) {
        jsTestLog("Validating targetBatchDocs=" + docsPerBatch);

        coll.drop();
        assert.commandWorked(
            coll.insertMany([...Array(collCount).keys()].map(x => ({_id: x, a: "a".repeat(10)})),
                            {ordered: false}));

        assert.commandWorked(
            db.adminCommand({setParameter: 1, batchedDeletesTargetBatchDocs: docsPerBatch}));

        // batchedDeletesTargetBatchDocs := 0 means no limit.
        const expectedBatches = docsPerBatch ? Math.ceil(collCount / docsPerBatch) : 1;
        const serverStatusBatchesBefore = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsBefore = db.serverStatus()['batchedDeletes']['docs'];

        assert.eq(collCount, coll.find().itcount());
        assert.commandWorked(coll.deleteMany({_id: {$gte: 0}}));
        assert.eq(0, coll.find().itcount());

        const serverStatusBatchesAfter = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsAfter = db.serverStatus()['batchedDeletes']['docs'];
        const serverStatusDocsExpected = serverStatusDocsBefore + collCount;
        const serverStatusBatchesExpected = serverStatusBatchesBefore + expectedBatches;
        assert.eq(serverStatusBatchesAfter, serverStatusBatchesExpected);
        assert.eq(serverStatusDocsAfter, serverStatusDocsExpected);

        rst.awaitReplication();
        rst.checkReplicatedDataHashes();
    }
}

function validateTargetBatchTimeMS() {
    const collCount = 10;

    assert.commandWorked(db.adminCommand({setParameter: 1, batchedDeletesTargetBatchDocs: 0}));

    for (let targetBatchTimeMS of [0, 1000]) {
        jsTestLog("Validating targetBatchTimeMS=" + targetBatchTimeMS);

        coll.drop();
        assert.commandWorked(
            coll.insertMany([...Array(collCount).keys()].map(x => ({_id: x, a: "a".repeat(10)})),
                            {ordered: false}));

        assert.commandWorked(
            db.adminCommand({setParameter: 1, batchedDeletesTargetBatchTimeMS: targetBatchTimeMS}));

        // batchedDeletesTargetBatchTimeMS := 0 means no limit.
        const expectedBatches = targetBatchTimeMS ? collCount : 1;
        const serverStatusBatchesBefore = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsBefore = db.serverStatus()['batchedDeletes']['docs'];

        assert.eq(collCount, coll.find().itcount());

        // Make every delete take >> targetBatchTimeMS.
        const fp = configureFailPoint(db,
                                      "batchedDeleteStageSleepAfterNDocuments",
                                      {nDocs: 1, ns: coll.getFullName(), sleepMs: 2000});

        assert.commandWorked(coll.deleteMany({_id: {$gte: 0}}));
        assert.eq(0, coll.find().itcount());

        fp.off();
        const serverStatusBatchesAfter = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsAfter = db.serverStatus()['batchedDeletes']['docs'];
        const serverStatusDocsExpected = serverStatusDocsBefore + collCount;
        const serverStatusBatchesExpected = serverStatusBatchesBefore + expectedBatches;
        assert.eq(serverStatusBatchesAfter, serverStatusBatchesExpected);
        assert.eq(serverStatusDocsAfter, serverStatusDocsExpected);

        rst.awaitReplication();
        rst.checkReplicatedDataHashes();
    }
}

function validateTargetStagedDocsBytes() {
    const collCount = 10000;
    const docPaddingBytes = 1024;
    const cumulativePaddingBytes = collCount *
        (bsonsize({_id: ObjectId(), a: 'a'}) +
         100 /* allow for getMemUsage() own metadata and overestimation */ + docPaddingBytes);

    assert.commandWorked(db.adminCommand({setParameter: 1, batchedDeletesTargetBatchTimeMS: 0}));
    assert.commandWorked(db.adminCommand({setParameter: 1, batchedDeletesTargetBatchDocs: 0}));

    for (let stagedDocsBytes of [0, 1024 * 1024, 5 * 1024 * 1024]) {
        jsTestLog("Validating stagedDocsBytes=" + stagedDocsBytes);

        assert.commandWorked(db.adminCommand(
            {setParameter: 1, batchedDeletesTargetStagedDocBytes: stagedDocsBytes}));

        coll.drop();
        assert.commandWorked(coll.insertMany(
            [...Array(collCount).keys()].map(x => ({a: "a".repeat(docPaddingBytes)})),
            {ordered: false}));

        // batchedDeletesTargetStagedDocsBytes := 0 means no limit.
        const expectedBatches =
            stagedDocsBytes ? Math.ceil(cumulativePaddingBytes / stagedDocsBytes) : 1;
        const serverStatusBatchesBefore = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsBefore = db.serverStatus()['batchedDeletes']['docs'];

        assert.eq(collCount, coll.find().itcount());
        assert.commandWorked(coll.deleteMany({}));
        assert.eq(0, coll.find().itcount());

        const serverStatusBatchesAfter = db.serverStatus()['batchedDeletes']['batches'];
        const serverStatusDocsAfter = db.serverStatus()['batchedDeletes']['docs'];
        const serverStatusDocsExpected = serverStatusDocsBefore + collCount;
        const serverStatusBatchesExpected = serverStatusBatchesBefore + expectedBatches;
        assert.eq(serverStatusBatchesAfter, serverStatusBatchesExpected);
        assert.eq(serverStatusDocsAfter, serverStatusDocsExpected);

        rst.awaitReplication();
        rst.checkReplicatedDataHashes();
    }
}

validateTargetDocsPerBatch();
validateTargetBatchTimeMS();
validateTargetStagedDocsBytes();

rst.stopSet();
})();