summaryrefslogtreecommitdiff
path: root/jstests/serverless/change_stream_state_commands.js
blob: 373bad342b5b3d60a85c7807ce1d1ab016044ab2 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Test that the 'setChangeStreamState' and 'getChangeStreamState' commands work as expected in the
// multi-tenant replica sets environment for various cases.
// @tags: [
//   requires_fcv_62,
//   __TEMPORARILY_DISABLED__
// ]
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");         // For configureFailPoint.
load('jstests/libs/parallel_shell_helpers.js');  // For funWithArgs.

const replSetTest = new ReplSetTest({nodes: 2});

// TODO SERVER-67267 Add 'serverless' flag.
// TODO SERVER-68947 Add 'featureFlagRequireTenantID' flag.
// TODO SERVER-69115 Remove '__TEMPORARILY_DISABLED__'
replSetTest.startSet({
    setParameter: {
        featureFlagServerlessChangeStreams: true,
        multitenancySupport: true,
        featureFlagMongoStore: true
    }
});

replSetTest.initiate();

// Sets the change stream state for the provided tenant id.
function setChangeStreamState(tenantId, enabled) {
    assert.commandWorked(replSetTest.getPrimary().getDB("admin").runCommand(
        {setChangeStreamState: 1, $tenant: tenantId, enabled: enabled}));
}

// Verifies that the required change stream state is set for the provided tenant id both in the
// primary and the secondary and the command 'getChangeStreamState' returns the same state.
function assertChangeStreamState(tenantId, enabled) {
    assert.eq(assert
                  .commandWorked(replSetTest.getPrimary().getDB("admin").runCommand(
                      {getChangeStreamState: 1, $tenant: tenantId}))
                  .enabled,
              enabled);

    const primaryColls = replSetTest.getPrimary().getDB("config").getCollectionNames();
    const secondaryColls = replSetTest.getSecondary().getDB("config").getCollectionNames();

    // Verify that the change collection exists both in the primary and the secondary.
    assert.eq(primaryColls.includes("system.change_collection"), enabled);
    assert.eq(secondaryColls.includes("system.change_collection"), enabled);

    // Verify that the pre-images collection exists both in the primary and the secondary.
    assert.eq(primaryColls.includes("system.preimages"), enabled);
    assert.eq(secondaryColls.includes("system.preimages"), enabled);
}

const firstOrgTenantId = ObjectId();
const secondOrgTenantId = ObjectId();

// Tests that the 'setChangeStreamState' command works for the basic cases.
(function basicTest() {
    jsTestLog("Running basic tests");

    // Verify that the 'setChangeStreamState' command cannot be run with db other than the 'admin'
    // db.
    assert.commandFailedWithCode(
        replSetTest.getPrimary().getDB("config").runCommand(
            {setChangeStreamState: 1, enabled: true, $tenant: firstOrgTenantId}),
        ErrorCodes.Unauthorized);

    // Verify that the 'getChangeStreamState' command cannot be run with db other than the 'admin'
    // db.
    assert.commandFailedWithCode(replSetTest.getPrimary().getDB("config").runCommand(
                                     {getChangeStreamState: 1, $tenant: firstOrgTenantId}),
                                 ErrorCodes.Unauthorized);

    // Verify that the change stream is enabled for the tenant.
    setChangeStreamState(firstOrgTenantId, true);
    assertChangeStreamState(firstOrgTenantId, true);

    // Verify that the change stream is disabled for the tenant.
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);

    // Verify that enabling change stream multiple times has not side-effects.
    setChangeStreamState(firstOrgTenantId, true);
    setChangeStreamState(firstOrgTenantId, true);
    assertChangeStreamState(firstOrgTenantId, true);

    // Verify that disabling change stream multiple times has not side-effects.
    setChangeStreamState(firstOrgTenantId, false);
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);
})();

// Tests that the 'setChangeStreamState' command tolerates the primary step-down and can
// successfully resume after the new primary comes up.
(function resumabilityTest() {
    jsTestLog("Verifying resumability");

    // Reset the change stream state to disabled before starting the test case.
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);

    const primary = replSetTest.getPrimary();
    const secondary = replSetTest.getSecondary();

    // Hang the 'SetChangeStreamStateCoordinator' before processing the command request.
    const fpHangBeforeCmdProcessor =
        configureFailPoint(primary, "hangSetChangeStreamStateCoordinatorBeforeCommandProcessor");

    // While the failpoint is active, issue a request to enable change stream. This command will
    // hang at the fail point.
    const shellReturn = startParallelShell(() => {
        db.getSiblingDB("admin").runCommand({setChangeStreamState: 1, enabled: true});
    }, primary.port);

    // Wait until the fail point is hit.
    fpHangBeforeCmdProcessor.wait();

    // Verify that the change stream is still disabled at this point.
    assertChangeStreamState(firstOrgTenantId, false);

    // Force primary to step down such that the secondary gets elected as a new leader.
    assert.commandWorked(primary.adminCommand({replSetStepDown: 60, force: true}));

    // The hung command at the point must have been interrupted and shell must have returned the
    // error code.
    shellReturn();

    // Wait until the secondary becomes the new primary.
    replSetTest.waitForState(secondary, ReplSetTest.State.PRIMARY);

    // Disable the fail point as it is no longer needed.
    fpHangBeforeCmdProcessor.off();

    // Get the new primary and the secondary.
    const newPrimary = replSetTest.getPrimary();

    // Verify that the new primary resumed the command and change stream is now enabled.
    assert.soon(() => {
        const collNames = newPrimary.getDB("config").getCollectionNames();
        return collNames.includes("system.change_collection") &&
            collNames.includes("system.preimages");
    });
    assertChangeStreamState(firstOrgTenantId, true);
})();

// Tests that the 'setChangeStreamState' command does not allow parallel non-identical requests from
// the same tenant.
(function parallelNonIdenticalRequestsSameTenantTest() {
    jsTestLog("Verifying parallel non-identical requests from the same tenant");

    // Reset the change stream state to disabled before starting the test case.
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);

    const primary = replSetTest.getPrimary();

    // Hang the 'SetChangeStreamStateCoordinator' before processing the command request.
    const fpHangBeforeCmdProcessor =
        configureFailPoint(primary, "hangSetChangeStreamStateCoordinatorBeforeCommandProcessor");

    // While the failpoint is active, issue a request to enable change stream for the tenant. This
    // command will hang at the fail point.
    const shellReturn = startParallelShell(
        funWithArgs((firstOrgTenantId) => {
            assert.commandWorked(db.getSiblingDB("admin").runCommand(
                {setChangeStreamState: 1, $tenant: firstOrgTenantId, enabled: true}));
        }, firstOrgTenantId), primary.port);

    // Wait until the fail point is hit.
    fpHangBeforeCmdProcessor.wait();

    // While the first command is still hung, issue a request to disable the change stream for the
    // same tenants. This request should bail out with 'ConflictingOperationInProgress' exception.
    assert.throwsWithCode(() => setChangeStreamState(firstOrgTenantId, false),
                          ErrorCodes.ConflictingOperationInProgress);

    // Turn off the fail point.
    fpHangBeforeCmdProcessor.off();

    // Wait for the shell to return.
    shellReturn();

    // Verify that the first command has enabled the change stream now.
    assertChangeStreamState(firstOrgTenantId, true);
})();

// Tests that the 'setChangeStreamState' command allows parallel identical requests from the same
// tenant.
(function parallelIdenticalRequestsSameTenantTest() {
    jsTestLog("Verifying parallel identical requests from the same tenant");

    // Reset the change stream state to disabled before starting the test case.
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);

    const primary = replSetTest.getPrimary();

    // Hang the 'SetChangeStreamStateCoordinator' before processing the command request.
    const fpHangBeforeCmdProcessor =
        configureFailPoint(primary, "hangSetChangeStreamStateCoordinatorBeforeCommandProcessor");

    const shellFn = (firstOrgTenantId) => {
        assert.commandWorked(db.getSiblingDB("admin").runCommand(
            {setChangeStreamState: 1, $tenant: firstOrgTenantId, enabled: true}));
    };

    // While the failpoint is active, issue a request to enable change stream for the tenant. This
    // command will hang at the fail point.
    const shellReturn1 = startParallelShell(funWithArgs(shellFn, firstOrgTenantId), primary.port);

    // Wait for the fail point to be hit.
    fpHangBeforeCmdProcessor.wait();

    // Issue another request to enable the change stream from the same tenant. This should not throw
    // any exception. We will not wait for the fail point because the execution of the same request
    // is already in progress and this request will wait on the completion of the previous
    // enablement request.
    const shellReturn2 = startParallelShell(funWithArgs(shellFn, firstOrgTenantId), primary.port);

    // Turn off the fail point.
    fpHangBeforeCmdProcessor.off();

    // Wait for shells to return.
    shellReturn1();
    shellReturn2();

    // Verify that the first command has enabled the change stream now.
    assertChangeStreamState(firstOrgTenantId, true);
})();

// Tests that parallel requests from different tenants do not interfere with each other and can
// complete successfully.
(function parallelRequestsDifferentTenantsTest() {
    jsTestLog("Verifying parallel requests from different tenants");

    // Reset the change stream state to disable before starting the test case.
    setChangeStreamState(firstOrgTenantId, false);
    assertChangeStreamState(firstOrgTenantId, false);
    setChangeStreamState(secondOrgTenantId, false);
    assertChangeStreamState(secondOrgTenantId, false);

    const primary = replSetTest.getPrimary();

    // Hang the 'SetChangeStreamStateCoordinator' before processing the command request.
    const fpHangBeforeCmdProcessor =
        configureFailPoint(primary, "hangSetChangeStreamStateCoordinatorBeforeCommandProcessor");

    // Enable the change stream for the tenant 'firstOrgTenantId' in parallel.
    const firstTenantShellReturn = startParallelShell(
        funWithArgs((firstOrgTenantId) => {
            assert.commandWorked(db.getSiblingDB("admin").runCommand(
                {setChangeStreamState: 1, $tenant: firstOrgTenantId, enabled: true}));
        }, firstOrgTenantId), primary.port);

    // Wait until the above request hits the fail point.
    fpHangBeforeCmdProcessor.wait({timesEntered: 1});

    // While the first request from the tenant 'firstOrgTenantId' is hung, issue another request but
    // with the tenant 'secondOrgTenantId'.
    const secondTenantShellReturn = startParallelShell(
        funWithArgs((secondOrgTenantId) => {
            assert.commandWorked(db.getSiblingDB("admin").runCommand(
                {setChangeStreamState: 1, $tenant: secondOrgTenantId, enabled: true}));
        }, secondOrgTenantId), primary.port);

    // The request from the 'secondOrgTenantId' will also hang.
    fpHangBeforeCmdProcessor.wait({timesEntered: 2});

    // Now that both the request have hit the fail point, disable it.
    fpHangBeforeCmdProcessor.off();

    // Wait for both shells to return.
    firstTenantShellReturn();
    secondTenantShellReturn();

    // Verify that the change stream state for both tenants is now enabled.
    assertChangeStreamState(firstOrgTenantId, true);
    assertChangeStreamState(secondOrgTenantId, true);
})();

replSetTest.stopSet();
}());