summaryrefslogtreecommitdiff
path: root/jstests/change_streams/global_index.js
blob: 9a2b2b396b7c60c82ed02b897500683228d5f33d (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
/**
 * Tests global index maintenance API does not generate change stream events.
 *
 * @tags: [
 *     assumes_against_mongod_not_mongos,
 *     featureFlagGlobalIndexes,
 *     requires_fcv_62,
 *     uses_transactions
 * ]
 */

// TODO (SERVER-69932): once sharding has a working global index implementation, rewrite this test
// to run on mongos (don't use internal commands) and remove assumes_against_mongod_not_mongos.

(function() {
"use strict";

load("jstests/libs/change_stream_util.js");

const adminDB = db.getSiblingDB("admin");
const cst = new ChangeStreamTest(adminDB);

// Drop database and recreate collection to avoid changestream event for collection create and
// make sure collection is empty.
db.getSiblingDB(jsTestName()).dropDatabase();
db.getSiblingDB(jsTestName()).createCollection("coll");

let cursor = cst.startWatchingChanges({
    pipeline: [{
        $changeStream:
            {allChangesForCluster: true, showExpandedEvents: true, showSystemEvents: true}
    }],
    collection: 1
});
const globalIndexUUID = UUID();

// _shardsvrCreateGlobalIndex should not generate a change stream.
assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: globalIndexUUID}));
cst.assertNoChange(cursor);

// Global index insert operations should not generate change stream events.
{
    const session = db.getMongo().startSession();
    session.startTransaction();
    assert.commandWorked(session.getDatabase("system").runCommand(
        {"_shardsvrInsertGlobalIndexKey": globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}));
    session.commitTransaction();
    session.endSession();
}
cst.assertNoChange(cursor);

// Global index delete operations should not generate change stream events.
{
    const session = db.getMongo().startSession();
    session.startTransaction();
    assert.commandWorked(session.getDatabase("system").runCommand(
        {"_shardsvrDeleteGlobalIndexKey": globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}));
    session.commitTransaction();
    session.endSession();
}
cst.assertNoChange(cursor);

// Global index bulk operations should not generate change stream events.
{
    const session = db.getMongo().startSession();
    session.startTransaction();
    assert.commandWorked(session.getDatabase("system").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "abc"},
                docKey: {sk: 123, _id: 987}
            },
            {
                _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                key: {myKey: "abc"},
                docKey: {sk: 123, _id: 987}
            }
        ]
    }));
    session.commitTransaction();
    session.endSession();
}
cst.assertNoChange(cursor);

// Validate behaviour of change streams events for regular operations is preserved when used
// together with global indexes.
{
    const session = db.getMongo().startSession();
    session.startTransaction();
    assert.commandWorked(session.getDatabase("system").runCommand(
        {"_shardsvrInsertGlobalIndexKey": globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}));
    assert.commandWorked(session.getDatabase(jsTestName()).coll.insert({_id: 1, a: 123}));
    session.commitTransaction();
    session.endSession();
}
// getNextChanges will timeout if no change is found.
const nextChange = cst.getNextChanges(cursor, 1)[0];
const expectedInsert = {
    operationType: "insert",
    fullDocument: {_id: 1, a: 123},
    ns: {db: jsTestName(), coll: "coll"},
    documentKey: {_id: 1}
};
assertChangeStreamEventEq(nextChange, expectedInsert);

// _shardsvrDropGlobalIndex should not generate a change stream.
assert.commandWorked(adminDB.runCommand({_shardsvrDropGlobalIndex: globalIndexUUID}));
cst.assertNoChange(cursor);

cst.cleanUp();
}());