summaryrefslogtreecommitdiff
path: root/jstests/replsets/minimum_visible_with_cluster_time.js
blob: 7a30c386f739def285c71a4bc103d02bd353267b (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
/**
 *  Tests that majority reads can complete successfully even when the cluster time is being
 *  increased rapidly while ddl operations are happening.
 *
 *  @tags: [requires_replication]
 */
(function() {
    'use strict';
    load("jstests/replsets/rslib.js");  // For startSetIfSupportsReadMajority.

    const rst = new ReplSetTest({nodes: 1});
    if (!startSetIfSupportsReadMajority(rst)) {
        jsTest.log("skipping test since storage engine doesn't support committed reads");
        rst.stopSet();
        return;
    }

    rst.initiate();
    const primary = rst.getPrimary();
    const syncName = 'sync';
    const syncColl = primary.getDB(syncName).getCollection(syncName);
    assert.commandWorked(syncColl.insert({t: 'before'}));

    function bumpClusterTime() {
        jsTestLog('Beginning to bump the logical clock.');
        const syncName = 'sync';
        const syncColl = db.getSiblingDB(syncName).getCollection(syncName);
        assert.eq(syncColl.find().itcount(), 1);
        assert.commandWorked(syncColl.insert({t: 'during'}));
        assert.eq(syncColl.find().itcount(), 2);

        let clusterTime = new Timestamp(1, 1);
        while (true) {
            const higherClusterTime = new Timestamp(clusterTime.getTime() + 20, 1);
            const res = assert.commandWorked(db.adminCommand({
                'isMaster': 1,
                '$clusterTime': {
                    'clusterTime': higherClusterTime,
                    'signature': {
                        'hash': BinData(0, 'AAAAAAAAAAAAAAAAAAAAAAAAAAA='),
                        'keyId': NumberLong(0)
                    }
                }
            }));
            clusterTime = res.$clusterTime.clusterTime;

            if (syncColl.find().itcount() === 3) {
                jsTestLog('Done bumping the logical clock.');
                return;
            }
        }
    }

    const clusterTimeBumper = startParallelShell(bumpClusterTime, primary.port);
    // Wait for the logical clock to begin to be bumped.
    assert.soon(() => syncColl.find().itcount() === 2);

    function doMajorityRead(coll, expectedCount) {
        const res = assert.commandWorked(coll.runCommand('find', {
            'filter': {x: 7},
            'readConcern': {'level': 'majority'},
            'maxTimeMS': rst.kDefaultTimeoutMS
        }));
        // Exhaust the cursor to avoid leaking cursors on the server.
        assert.eq(expectedCount, new DBCommandCursor(coll.getDB(), res).itcount());
    }

    const dbName = 'minimum_visible_with_cluster_time';
    const collName = 'foo';

    for (let i = 0; i < 10; i++) {
        const collNameI = collName + i;
        jsTestLog(`Testing ${dbName}.${collNameI}`);

        assert.commandWorked(primary.getDB(dbName).createCollection(collNameI));
        let coll = primary.getDB(dbName).getCollection(collNameI);

        doMajorityRead(coll, 0);

        assert.commandWorked(coll.insert({x: 7, y: 1}));
        assert.commandWorked(
            coll.createIndex({x: 1}, {'name': 'x_1', 'expireAfterSeconds': 60 * 60 * 23}));

        doMajorityRead(coll, 1);

        assert.commandWorked(coll.insert({x: 7, y: 2}));
        assert.commandWorked(coll.runCommand(
            'collMod', {'index': {'keyPattern': {x: 1}, 'expireAfterSeconds': 60 * 60 * 24}}));
        doMajorityRead(coll, 2);

        assert.commandWorked(coll.insert({x: 7, y: 3}));
        assert.commandWorked(coll.dropIndexes());

        doMajorityRead(coll, 3);

        assert.commandWorked(coll.insert({x: 7, y: 4}));
        const newCollNameI = collNameI + '_new';
        assert.commandWorked(coll.renameCollection(newCollNameI));

        coll = primary.getDB(dbName).getCollection(newCollNameI);
        doMajorityRead(coll, 4);
    }

    jsTestLog('Waiting for logical clock thread to stop.');
    assert.commandWorked(syncColl.insert({t: 'after'}));
    clusterTimeBumper();

    rst.stopSet();
})();