summaryrefslogtreecommitdiff
path: root/jstests/replsets/oplog_sampling.js
blob: 62a5989bec2e2c537515ec7eb734e84047c5ed54 (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
/**
 * Ensure serverStatus reports the total time spent sampling the oplog for all storage engines that
 * support OplogTruncateMarkers.
 * @tags: [ requires_persistence ]
 */
(function() {
"use strict";

// Force oplog sampling to occur on start up for small numbers of oplog inserts.
const replSet = new ReplSetTest({
    nodes: 1,
    nodeOptions: {
        setParameter: {
            "maxOplogTruncationPointsDuringStartup": 10,
            logComponentVerbosity: tojson({storage: {verbosity: 3}}),
        }
    }
});
replSet.startSet();
replSet.initiate();

let coll = replSet.getPrimary().getDB("test").getCollection("testcoll");

let res = replSet.getPrimary().getDB("test").serverStatus();
assert.commandWorked(res);

// Small (or empty) oplogs should be processed by scanning.
assert.gte(res.oplogTruncation.totalTimeProcessingMicros, 0);
assert.eq(res.oplogTruncation.processingMethod, "scanning");

// Insert enough documents to force oplog sampling to occur on the following start up.
// Ensure that fast count of oplog collection increases while we insert the documents.
const oplogColl = replSet.getPrimary().getDB("local").getCollection("oplog.rs");
let oplogFastCount = oplogColl.count();
const maxOplogDocsForScanning = 2000;
jsTestLog("Inserting " + maxOplogDocsForScanning + " documents to force oplog sampling on restart");
for (let i = 0; i < maxOplogDocsForScanning + 1; i++) {
    let doc = {m: 1 + i};
    assert.commandWorked(coll.insert(doc), "failed to insert " + tojson(doc));

    let newOplogFastCount = oplogColl.count();
    assert.gt(
        newOplogFastCount,
        oplogFastCount,
        "fast count of oplog collection did not increase after successfully inserting " +
            tojson(doc) + ". Previous fast count of oplog: " + oplogFastCount +
            ". New fast count: " + newOplogFastCount + ". Last 5 oplog entries: " +
            tojson(replSet.findOplog(replSet.getPrimary(), /*query=*/ {}, /*limit=*/ 5).toArray()));
    oplogFastCount = newOplogFastCount;
}

// Do not proceed with test if the oplog collection has a lower than expected fast count that
// will result in an oplog scan on restart.
assert.gt(
    oplogFastCount,
    maxOplogDocsForScanning,
    "fast count of oplog collection is not large enough to trigger oplog sampling on restart");

// Restart replica set to load entries from the oplog for sampling.
jsTestLog("Inserted " + maxOplogDocsForScanning + " documents. Oplog fast count: " +
          oplogFastCount + ". Restarting server to force oplog sampling.");
replSet.stopSet(null /* signal */, true /* forRestart */);
replSet.startSet({restart: true});

res = replSet.getPrimary().getDB("test").serverStatus();
assert.commandWorked(res);

assert.gt(res.oplogTruncation.totalTimeProcessingMicros, 0);
assert.eq(res.oplogTruncation.processingMethod, "sampling");

replSet.stopSet();
})();