summaryrefslogtreecommitdiff
path: root/jstests/change_streams/whole_cluster_resumability.js
blob: 4a907315fd54f6234adb2bdd75f108495a68fe81 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Basic tests for resuming a $changeStream that is open against all databases in a cluster.
(function() {
    "use strict";

    load("jstests/libs/collection_drop_recreate.js");  // For assert[Drop|Create]Collection.
    load("jstests/libs/change_stream_util.js");        // For ChangeStreamTest.
    load("jstests/libs/fixture_helpers.js");           // For FixtureHelpers.

    // Create two databases, with one collection in each.
    const testDBs = [db.getSiblingDB(jsTestName()), db.getSiblingDB(jsTestName() + "_other")];
    let [db1Coll, db2Coll] = testDBs.map((db) => assertDropAndRecreateCollection(db, "test"));
    const adminDB = db.getSiblingDB("admin");

    let cst = new ChangeStreamTest(adminDB);
    let resumeCursor = cst.startWatchingAllChangesForCluster();

    // Insert a document in the first database and save the resulting change stream.
    assert.writeOK(db1Coll.insert({_id: 1}));
    const firstInsertChangeDoc = cst.getOneChange(resumeCursor);
    assert.docEq(firstInsertChangeDoc.fullDocument, {_id: 1});

    // Test resume after the first insert.
    resumeCursor = cst.startWatchingChanges({
        pipeline:
            [{$changeStream: {resumeAfter: firstInsertChangeDoc._id, allChangesForCluster: true}}],
        collection: 1,
        aggregateOptions: {cursor: {batchSize: 0}},
    });

    // Write the next document into the second database.
    assert.writeOK(db2Coll.insert({_id: 2}));
    const secondInsertChangeDoc = cst.getOneChange(resumeCursor);
    assert.docEq(secondInsertChangeDoc.fullDocument, {_id: 2});

    // Write the third document into the first database again.
    assert.writeOK(db1Coll.insert({_id: 3}));
    const thirdInsertChangeDoc = cst.getOneChange(resumeCursor);
    assert.docEq(thirdInsertChangeDoc.fullDocument, {_id: 3});

    // Test resuming after the first insert again.
    resumeCursor = cst.startWatchingChanges({
        pipeline:
            [{$changeStream: {resumeAfter: firstInsertChangeDoc._id, allChangesForCluster: true}}],
        collection: 1,
        aggregateOptions: {cursor: {batchSize: 0}},
    });
    assert.docEq(cst.getOneChange(resumeCursor), secondInsertChangeDoc);
    assert.docEq(cst.getOneChange(resumeCursor), thirdInsertChangeDoc);

    // Test resume after second insert.
    resumeCursor = cst.startWatchingChanges({
        pipeline:
            [{$changeStream: {resumeAfter: secondInsertChangeDoc._id, allChangesForCluster: true}}],
        collection: 1,
        aggregateOptions: {cursor: {batchSize: 0}},
    });
    assert.docEq(cst.getOneChange(resumeCursor), thirdInsertChangeDoc);

    // Rename the collection and obtain a resume token from the 'rename' notification. Skip this
    // test when running on a sharded collection, since these cannot be renamed.
    if (!FixtureHelpers.isSharded(db1Coll)) {
        assertDropAndRecreateCollection(db1Coll.getDB(), db1Coll.getName());
        const renameColl = db1Coll.getDB().getCollection("rename_coll");
        assertDropCollection(renameColl.getDB(), renameColl.getName());

        resumeCursor = cst.startWatchingChanges({
            collection: 1,
            pipeline: [{$changeStream: {allChangesForCluster: true}}],
            aggregateOptions: {cursor: {batchSize: 0}}
        });
        assert.writeOK(db1Coll.renameCollection(renameColl.getName()));

        const renameChanges = cst.assertNextChangesEqual({
            cursor: resumeCursor,
            expectedChanges: [
                {
                  operationType: "rename",
                  ns: {db: db1Coll.getDB().getName(), coll: db1Coll.getName()},
                  to: {db: renameColl.getDB().getName(), coll: renameColl.getName()}
                },
            ]
        });
        const resumeTokenRename = renameChanges[0]._id;

        // Insert into the renamed collection.
        assert.writeOK(renameColl.insert({_id: "after rename"}));

        // Resume from the rename notification using 'resumeAfter' and verify that the change stream
        // returns the next insert.
        let expectedInsert = {
            operationType: "insert",
            ns: {db: renameColl.getDB().getName(), coll: renameColl.getName()},
            fullDocument: {_id: "after rename"},
            documentKey: {_id: "after rename"}
        };
        resumeCursor = cst.startWatchingChanges({
            collection: 1,
            pipeline:
                [{$changeStream: {resumeAfter: resumeTokenRename, allChangesForCluster: true}}],
            aggregateOptions: {cursor: {batchSize: 0}}
        });
        cst.assertNextChangesEqual({cursor: resumeCursor, expectedChanges: expectedInsert});

        // Resume from the rename notification using 'startAfter' and verify that the change stream
        // returns the next insert.
        expectedInsert = {
            operationType: "insert",
            ns: {db: renameColl.getDB().getName(), coll: renameColl.getName()},
            fullDocument: {_id: "after rename"},
            documentKey: {_id: "after rename"}
        };
        resumeCursor = cst.startWatchingChanges({
            collection: 1,
            pipeline:
                [{$changeStream: {startAfter: resumeTokenRename, allChangesForCluster: true}}],
            aggregateOptions: {cursor: {batchSize: 0}}
        });
        cst.assertNextChangesEqual({cursor: resumeCursor, expectedChanges: expectedInsert});

        // Rename back to the original collection for reliability of the collection drops when
        // dropping the database.
        assert.writeOK(renameColl.renameCollection(db1Coll.getName()));
    }

    // Dropping a database should generate a 'drop' notification for the collection followed by a
    // 'dropDatabase' notification.
    resumeCursor = cst.startWatchingAllChangesForCluster();
    assert.commandWorked(testDBs[0].dropDatabase());
    const dropDbChanges = cst.assertDatabaseDrop({cursor: resumeCursor, db: testDBs[0]});
    const resumeTokenDbDrop = dropDbChanges[dropDbChanges.length - 1]._id;

    // Recreate the collection and insert a document.
    assert.writeOK(db1Coll.insert({_id: "after recreate"}));

    let expectedInsert = {
        operationType: "insert",
        ns: {db: testDBs[0].getName(), coll: db1Coll.getName()},
        fullDocument: {_id: "after recreate"},
        documentKey: {_id: "after recreate"}
    };

    // Resume from the database drop using 'resumeAfter', and verify the change stream picks up
    // the insert.
    resumeCursor = cst.startWatchingChanges({
        collection: 1,
        pipeline: [{$changeStream: {resumeAfter: resumeTokenDbDrop, allChangesForCluster: true}}],
        aggregateOptions: {cursor: {batchSize: 0}}
    });
    cst.consumeDropUpTo({
        cursor: resumeCursor,
        dropType: "dropDatabase",
        expectedNext: expectedInsert,
    });

    // Resume from the database drop using 'startAfter', and verify the change stream picks up the
    // insert.
    resumeCursor = cst.startWatchingChanges({
        collection: 1,
        pipeline: [{$changeStream: {startAfter: resumeTokenDbDrop, allChangesForCluster: true}}],
        aggregateOptions: {cursor: {batchSize: 0}}
    });
    cst.consumeDropUpTo({
        cursor: resumeCursor,
        dropType: "dropDatabase",
        expectedNext: expectedInsert,
    });

    cst.cleanUp();
})();