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

const rst = new ReplSetTest({nodes: 1});
rst.startSet();
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({
            'hello': 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';

try {
    assert.commandWorked(primary.getDB(dbName).adminCommand(
        {'configureFailPoint': 'hangBeforeMajorityReadTransactionStarted', 'mode': 'alwaysOn'}));

    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}));

        // Majority read should eventually see new documents because it will not block on the index
        // build.
        assert.soonNoExcept(() => {
            doMajorityRead(coll, 1);
            return true;
        });

        assert.commandWorked(coll.insert({x: 7, y: 2}));
        assert.commandWorked(coll.runCommand(
            'collMod', {'index': {'keyPattern': {x: 1}, 'expireAfterSeconds': 60 * 60 * 24}}));
        // Majority read should eventually see new documents because it will not block on the index
        // build.
        assert.soonNoExcept(() => {
            doMajorityRead(coll, 2);
            return true;
        });

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

        // Majority read should eventually see new documents because it will not block on the drop.
        assert.soonNoExcept(() => {
            doMajorityRead(coll, 3);
            return true;
        });

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

        coll = primary.getDB(dbName).getCollection(newCollNameI);
        // Majority read should immediately see new documents because it blocks on the rename.
        doMajorityRead(coll, 4);
    }

    jsTestLog('Waiting for logical clock thread to stop.');
    assert.commandWorked(syncColl.insert({t: 'after'}));
    clusterTimeBumper();
} finally {
    assert.commandWorked(primary.getDB(dbName).adminCommand(
        {'configureFailPoint': 'hangBeforeMajorityReadTransactionStarted', 'mode': 'off'}));
}

rst.stopSet();
})();