summaryrefslogtreecommitdiff
path: root/jstests/sharding/change_stream_no_orphans.js
blob: 98d5fd1bcd6327c75933487ea4bbc94ce7bc44c2 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
 * Verify that write operations on orphaned documents (1) do not show up unexpected events in change
 * streams and (2) have no effect on the persisted data.
 *
 * The behavior is tested in the following scenarios:
 *   - Direct operations to shard on orphaned documents
 *   - Broadcasted operations (from router) on orphaned documents
 *   - Transaction from router updating both orphaned and non-orphaned documents
 *   - Transaction to shard updating both orphaned and non-orphaned documents
 *   - Batched deletes from router and to shard
 *
 * @tags: [
 *   requires_fcv_53,
 *   featureFlagNoChangeStreamEventsDueToOrphans,
 * ]
 */
(function() {
'use strict';

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

const dbName = 'test';
const collName = 'foo';
const collNS = dbName + '.' + collName;

// Enable explicitly the periodic no-op writer to allow the router to process change stream events
// coming from all shards. This is enabled for production clusters by default.
const st = new ShardingTest({
    mongos: 1,
    config: 1,
    shards: 2,
    rs: {nodes: 1, setParameter: {writePeriodicNoops: true}},
    other: {enableBalancer: false}
});

// Suspend the range deletion on the first shard to force the orphaned documents to stay here after
// the chunks have been moved to the second shard.
let suspendRangeDeletionShard0 = configureFailPoint(st.shard0, 'suspendRangeDeletion');

// Create a shard collection with documents having both a key field and a non-key field.
assert.commandWorked(
    st.s.adminCommand({enableSharding: dbName, primaryShard: st.shard0.shardName}));
assert.commandWorked(st.s.adminCommand({shardCollection: collNS, key: {_id: 1}}));
const coll = st.s.getCollection(collNS);
assert.commandWorked(coll.insert({_id: -2, name: 'emma', age: 20}));
assert.commandWorked(coll.insert({_id: -1, name: 'olivia', age: 25}));
assert.commandWorked(coll.insert({_id: 0, name: 'matt', age: 30}));
assert.commandWorked(coll.insert({_id: 1, name: 'john', age: 35}));
assert.commandWorked(coll.insert({_id: 2, name: 'robert', age: 40}));
assert.commandWorked(coll.insert({_id: 3, name: 'robert', age: 45}));
assert.commandWorked(coll.insert({_id: 4, name: 'james', age: 50}));
assert.commandWorked(coll.insert({_id: 5, name: 'liam', age: 55}));

// Move the chunk to the second shard leaving orphaned documents on the first shard.
assert.commandWorked(st.s.adminCommand({split: collNS, middle: {_id: 0}}));
assert.commandWorked(
    st.s.adminCommand({moveChunk: collNS, find: {_id: 0}, to: st.shard1.shardName}));

// Setup a change stream on the collection to receive real-time events on any data changes.
const changeStream = coll.watch([]);

////////////////////////////////////////////////////////////////////////////////////////////////////
// Direct operations to shard on orphaned documents
////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('A direct insert to a shard of an orphaned document does not generate an insert event');
{
    // Direct insert to first shard of an orphaned document.
    assert.commandWorked(st.shard0.getCollection(collNS).insert({_id: 6, name: 'ken', age: 60}));

    // No event is notified.
    assert(!changeStream.hasNext());

    // The orphaned document on first shard has been inserted.
    assert.neq(null, st.shard0.getCollection(collNS).findOne({_id: 6}));
}

jsTest.log('A direct update to a shard of an orphaned document does not generate an update event');
{
    // Send a direct update to first shard on an orphaned document.
    assert.commandWorked(st.shard0.getCollection(collNS).update({name: 'matt'}, {$set: {age: 31}}));

    // No change stream event is generated.
    assert(!changeStream.hasNext());

    // The orphaned document on first shard has been updated.
    assert.eq(31, st.shard0.getCollection(collNS).findOne({_id: 0}).age);
}

jsTest.log('A direct delete to a shard of an orphaned document does generate an update event');
{
    // Send a direct delete to first shard on an orphaned document.
    assert.commandWorked(st.shard0.getCollection(collNS).remove({name: 'matt'}));

    // No change stream event is generated.
    assert(!changeStream.hasNext());

    // The orphaned document on first shard has been removed.
    assert.eq(null, st.shard0.getCollection(collNS).findOne({_id: 0}));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Broadcasted operations (from router) on orphaned documents
////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('A broadcasted update of a single document generates an update event');
{
    // Send a broadcasted update (query on non-key field) on a single document to all the shards.
    assert.commandWorked(coll.update({name: 'john'}, {$set: {age: 36}}, {multi: true}));

    // The document is hosted by the second shard and the update event is notified. The first shard
    // still hosts the orphaned document so no additional event must be notified.
    assert.soon(() => changeStream.hasNext(), 'An update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert(!changeStream.hasNext());

    // The orphaned document on first shard has not been updated, unlike the non-orphaned one on the
    // second shard.
    assert.eq(35, st.shard0.getCollection(collNS).findOne({_id: 1}).age);
    assert.eq(36, st.shard1.getCollection(collNS).findOne({_id: 1}).age);
}

jsTest.log('A broadcasted delete of a single document generates a delete event');
{
    // Send a broadcasted delete (query on non-key field) on a single document to all the shards.
    assert.commandWorked(coll.remove({name: 'john'}));

    // The document is hosted by the second shard and the delete event is notified. The first shard
    // still hosts the orphaned document so no additional event must be notified.
    assert.soon(() => changeStream.hasNext(), 'A delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert(!changeStream.hasNext());

    // The orphaned document on first shard has not been removed, unlike the non-orphaned one on the
    // second shard.
    assert.neq(null, st.shard0.getCollection(collNS).findOne({_id: 1}));
    assert.eq(null, st.shard1.getCollection(collNS).findOne({_id: 1}));
}

jsTest.log('A broadcasted update of multi-documents generates more update events');
{
    // Send a broadcasted update (query on non-key field) on two documents to all the shards.
    assert.commandWorked(coll.update({name: 'robert'}, {$set: {age: 41}}, {multi: true}));

    // The documents are hosted by the second shard and two delete events are notified. The first
    // shard still hosts the orphaned documents so no additional event must be notified.
    assert.soon(() => changeStream.hasNext(), 'A first update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert.soon(() => changeStream.hasNext(), 'A second update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert(!changeStream.hasNext());

    // The orphaned documents on first shard have not been updated, unlike the non-orphaned ones on
    // the second shard.
    assert.eq(40, st.shard0.getCollection(collNS).findOne({_id: 2}).age);
    assert.eq(45, st.shard0.getCollection(collNS).findOne({_id: 3}).age);
    assert.eq(41, st.shard1.getCollection(collNS).findOne({_id: 2}).age);
    assert.eq(41, st.shard1.getCollection(collNS).findOne({_id: 3}).age);
}

jsTest.log('A broadcasted delete of multi-documents generates more delete events');
{
    // Send a broadcasted delete (query on non-key field) on two documents to all the shards.
    assert.commandWorked(coll.remove({name: 'robert'}));

    // The documents are hosted by the second shard and two delete events are notified. The first
    // shard still hosts the orphaned documents so no additional event must be notified.
    assert.soon(() => changeStream.hasNext(), 'A first delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert.soon(() => changeStream.hasNext(), 'A second delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert(!changeStream.hasNext());

    // The orphaned documents on first shard have not been removed, unlike the non-orphaned ones on
    // the second shard.
    assert.neq(null, st.shard0.getCollection(collNS).findOne({_id: 2}));
    assert.neq(null, st.shard0.getCollection(collNS).findOne({_id: 3}));
    assert.eq(null, st.shard1.getCollection(collNS).findOne({_id: 2}));
    assert.eq(null, st.shard1.getCollection(collNS).findOne({_id: 3}));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Transaction from router updating both orphaned and non-orphaned documents
////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('Broadcasted updates (via a transaction through the router) of both orphaned and ' +
           'non-orphaned documents generate events only for operations on non-orphaned documents');
{
    // Send a broadcasted transaction to the router updating both orphaned and non-orphaned
    // documents.
    const session = st.s.startSession();
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);
    session.startTransaction();
    assert.commandWorked(sessionColl.update({name: 'olivia'}, {$set: {age: 26}}, {multi: true}));
    assert.commandWorked(sessionColl.update({name: 'james'}, {$set: {age: 51}}, {multi: true}));
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // The primary shard hosts orphaned (james) and non-orphaned (olivia) documents, whereas the
    // second shard hosts a non-orphaned document (james). Consequently, two update events are
    // notified.
    assert.soon(() => changeStream.hasNext(), 'A first update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert.soon(() => changeStream.hasNext(), 'A second update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert(!changeStream.hasNext());

    // The orphaned document on first shard (james) has not been updated, unlike the non-orphaned
    // ones on both primary and second shards (olivia and james).
    assert.eq(26, st.shard0.getCollection(collNS).findOne({_id: -1}).age);
    assert.eq(50, st.shard0.getCollection(collNS).findOne({_id: 4}).age);
    assert.eq(51, st.shard1.getCollection(collNS).findOne({_id: 4}).age);
}

jsTest.log('Broadcasted deletes (via a transaction through the router) of both orphaned and ' +
           'non-orphaned documents generate events only for operations on non-orphaned documents');
{
    // Send a broadcasted transaction to the router deleting both orphaned and non-orphaned
    // documents.
    const session = st.s.startSession();
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);
    session.startTransaction();
    assert.commandWorked(sessionColl.remove({name: 'olivia'}));
    assert.commandWorked(sessionColl.remove({name: 'james'}));
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // The primary shard hosts orphaned (james) and non-orphaned (olivia) documents, whereas the
    // second shard hosts a non-orphaned document (james). Consequently, two delete events are
    // notified.
    assert.soon(() => changeStream.hasNext(), 'A first delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert.soon(() => changeStream.hasNext(), 'A second delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert(!changeStream.hasNext());

    // The orphaned document on first shard (james) has not been removed, unlike the non-orphaned
    // ones on both primary and second shards (olivia and james).
    assert.eq(null, st.shard0.getCollection(collNS).findOne({_id: -1}));
    assert.neq(null, st.shard0.getCollection(collNS).findOne({_id: 4}));
    assert.eq(null, st.shard1.getCollection(collNS).findOne({_id: 4}));
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Transaction to shard updating both orphaned and non-orphaned documents
////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('Direct updates (via a transaction to a shard) of both orphaned and non-orphaned' +
           'documents generate events only for operations on non-orphaned documents');
{
    // Send a direct transaction to a shard updating both orphaned and non-orphaned documents.
    const session = st.rs0.getPrimary().getDB(dbName).getMongo().startSession();
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);
    session.startTransaction();
    assert.commandWorked(sessionColl.update({name: 'emma'}, {$set: {age: 21}}, {multi: true}));
    assert.commandWorked(sessionColl.update({name: 'liam'}, {$set: {age: 56}}, {multi: true}));
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // The shard hosts both orphaned (liam) and non-orphaned (emma) documents. Consequently, only
    // one update event is notified.
    // TODO (SERVER-65859): The second update event will be filtered out when the ticket is
    // completed.
    assert.soon(() => changeStream.hasNext(), 'A first update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert.soon(() => changeStream.hasNext(), 'A second update event is expected');
    assert.eq(changeStream.next().operationType, 'update');
    assert(!changeStream.hasNext());

    // Both orphaned (liam) and non-orphaned (emma) documents on the shard have been updated.
    assert.eq(21, st.shard0.getCollection(collNS).findOne({_id: -2}).age);
    assert.eq(56, st.shard0.getCollection(collNS).findOne({_id: 5}).age);
}

jsTest.log('Direct deletes (via a transaction to a shard) of both orphaned and non-orphaned' +
           'documents generate events only for operations on non-orphaned documents');
{
    // Send a direct transaction to a shard deleting both orphaned and non-orphaned documents.
    const session = st.rs0.getPrimary().getDB(dbName).getMongo().startSession();
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);
    session.startTransaction();
    assert.commandWorked(sessionColl.remove({name: 'emma'}));
    assert.commandWorked(sessionColl.remove({name: 'liam'}));
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // The shard hosts both orphaned (liam) and non-orphaned (emma) documents. Consequently, only
    // one update event is notified.
    // TODO (SERVER-65859): The second delete event will be filtered out when the ticket is
    // completed.
    assert.soon(() => changeStream.hasNext(), 'A first delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert.soon(() => changeStream.hasNext(), 'A second delete event is expected');
    assert.eq(changeStream.next().operationType, 'delete');
    assert(!changeStream.hasNext());

    // Both orphaned (liam) and non-orphaned (emma) documents on the shard have been removed.
    assert.eq(null, st.shard0.getCollection(collNS).findOne({_id: -2}));
    assert.eq(null, st.shard0.getCollection(collNS).findOne({_id: 5}));
}

////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('The collection drop generates a drop event');
{
    coll.drop();

    // Essentially, this verifies that the operation before dropping the collection did not notify
    // additional and unexpected events.
    assert.soon(() => changeStream.hasNext(), 'A drop event is expected');
    assert.eq(changeStream.next().operationType, 'drop');
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Batched deletes from router and to shard
////////////////////////////////////////////////////////////////////////////////////////////////////

// Set the database to use batched deletes.
const db2 = st.rs0.getPrimary().getDB(dbName);
assert.commandWorked(db2.adminCommand({setParameter: 1, batchedDeletesTargetStagedDocBytes: 0}));
assert.commandWorked(db2.adminCommand({setParameter: 1, batchedDeletesTargetBatchTimeMS: 0}));
assert.commandWorked(db2.adminCommand({setParameter: 1, batchedDeletesTargetBatchDocs: 2}));

// Create a non-sharded collection.
const coll2 = db2.getCollection(collName);

// Setup a change stream on the collection to receive real-time events on any data changes.
const changeStream2 = coll2.watch([]);

jsTest.log('A batched delete from router generates only one delete event');
{
    // Insert two documents in the collection (see 'batchedDeletesTargetBatchDocs') and skip the
    // generated events.
    assert.commandWorked(coll2.insert({_id: 0, name: 'volkswagen'}));
    assert.commandWorked(coll2.insert({_id: 1, name: 'renault'}));
    assert.soon(() => changeStream2.hasNext(), 'A first insert event is expected');
    assert.eq(changeStream2.next().operationType, 'insert');
    assert.soon(() => changeStream2.hasNext(), 'A second insert event is expected');
    assert.eq(changeStream2.next().operationType, 'insert');
    assert(!changeStream2.hasNext());

    // Delete all documents in batch from the collection.
    assert.commandWorked(coll2.deleteMany({_id: {$gte: 0}}));

    // Actually only one delete operation is performed. Consequently, only one delete event is
    // notified.
    // TODO (SERVER-65859): The second delete event will be filtered out when the ticket is
    // completed.
    assert.soon(() => changeStream2.hasNext(), 'A first delete event is expected');
    assert.eq(changeStream2.next().operationType, 'delete');
    assert.soon(() => changeStream2.hasNext(), 'A second delete event is expected for now');
    assert.eq(changeStream2.next().operationType, 'delete');
    assert(!changeStream2.hasNext());

    // All documents have been removed from the collection.
    assert.eq(0, coll2.find().itcount());
}

jsTest.log('A batched delete to shard generates only one delete event');
{
    // Insert two documents in the collection (see 'batchedDeletesTargetBatchDocs') and skip the
    // generated events.
    assert.commandWorked(coll2.insert({_id: 0, name: 'volkswagen'}));
    assert.commandWorked(coll2.insert({_id: 1, name: 'renault'}));
    assert.soon(() => changeStream2.hasNext(), 'A first insert event is expected');
    assert.eq(changeStream2.next().operationType, 'insert');
    assert.soon(() => changeStream2.hasNext(), 'A second insert event is expected');
    assert.eq(changeStream2.next().operationType, 'insert');
    assert(!changeStream2.hasNext());

    // Delete all documents in batch from the collection.
    assert.commandWorked(st.shard0.getCollection(collNS).deleteMany({_id: {$gte: 0}}));

    // Actually only one delete operation is performed. Consequently, only one delete event is
    // notified.
    // TODO (SERVER-65859): The second delete event will be filtered out when the ticket is
    // completed.
    assert.soon(() => changeStream2.hasNext(), 'A first delete event is expected');
    assert.eq(changeStream2.next().operationType, 'delete');
    assert.soon(() => changeStream2.hasNext(), 'A second delete event is expected for now');
    assert.eq(changeStream2.next().operationType, 'delete');
    assert(!changeStream2.hasNext());

    // All documents have been removed from the collection.
    assert.eq(0, coll2.find().itcount());
}

////////////////////////////////////////////////////////////////////////////////////////////////////

jsTest.log('The collection drop generates a drop event');
{
    coll2.drop();

    // Essentially, this verifies that the operation before dropping the collection did not notify
    // additional and unexpected events.
    assert.soon(() => changeStream2.hasNext(), 'A drop event is expected');
    assert.eq(changeStream2.next().operationType, 'drop');
}

suspendRangeDeletionShard0.off();

st.stop();
})();