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
|
// Tests the behavior of change streams on sharded collections.
// @tags: [uses_change_streams]
(function() {
"use strict";
load('jstests/replsets/libs/two_phase_drops.js'); // For TwoPhaseDropCollectionTest.
load('jstests/aggregation/extras/utils.js'); // For assertErrorCode().
// For supportsMajorityReadConcern().
load("jstests/multiVersion/libs/causal_consistency_helpers.js");
if (!supportsMajorityReadConcern()) {
jsTestLog("Skipping test since storage engine doesn't support majority read concern.");
return;
}
const st = new ShardingTest({
shards: 2,
rs: {
nodes: 1,
enableMajorityReadConcern: '',
// Use a higher frequency for periodic noops to speed up the test.
setParameter: {periodicNoopIntervalSecs: 1}
}
});
const mongosDB = st.s0.getDB(jsTestName());
const mongosColl = mongosDB[jsTestName()];
assert.commandWorked(mongosDB.dropDatabase());
// Enable sharding on the test DB and ensure its primary is st.shard0.shardName.
assert.commandWorked(mongosDB.adminCommand({enableSharding: mongosDB.getName()}));
st.ensurePrimaryShard(mongosDB.getName(), st.rs0.getURL());
// Shard the test collection on _id.
assert.commandWorked(
mongosDB.adminCommand({shardCollection: mongosColl.getFullName(), key: {_id: 1}}));
// Split the collection into 2 chunks: [MinKey, 0), [0, MaxKey).
assert.commandWorked(
mongosDB.adminCommand({split: mongosColl.getFullName(), middle: {_id: 0}}));
// Move the [0, MaxKey) chunk to st.shard1.shardName.
assert.commandWorked(mongosDB.adminCommand(
{moveChunk: mongosColl.getFullName(), find: {_id: 1}, to: st.rs1.getURL()}));
// Write a document to each chunk.
assert.writeOK(mongosColl.insert({_id: -1}));
assert.writeOK(mongosColl.insert({_id: 1}));
let changeStream =
mongosColl.aggregate([{$changeStream: {}}, {$project: {_id: 0, clusterTime: 0}}]);
// Test that a change stream can see inserts on shard 0.
assert.writeOK(mongosColl.insert({_id: 1000}));
assert.writeOK(mongosColl.insert({_id: -1000}));
assert.soon(() => changeStream.hasNext(), "expected to be able to see the first insert");
assert.docEq(changeStream.next(), {
documentKey: {_id: 1000},
fullDocument: {_id: 1000},
ns: {db: mongosDB.getName(), coll: mongosColl.getName()},
operationType: "insert",
});
// Now do another write to shard 0, advancing that shard's clock and enabling the stream to
// return the earlier write to shard 1.
assert.writeOK(mongosColl.insert({_id: 1001}));
assert.soon(() => changeStream.hasNext(), "expected to be able to see the second insert");
assert.docEq(changeStream.next(), {
documentKey: {_id: -1000},
fullDocument: {_id: -1000},
ns: {db: mongosDB.getName(), coll: mongosColl.getName()},
operationType: "insert",
});
// Test that all changes are eventually visible due to the periodic noop writer.
assert.commandWorked(
st.rs0.getPrimary().adminCommand({setParameter: 1, writePeriodicNoops: true}));
assert.commandWorked(
st.rs1.getPrimary().adminCommand({setParameter: 1, writePeriodicNoops: true}));
assert.soon(() => changeStream.hasNext());
assert.docEq(changeStream.next(), {
documentKey: {_id: 1001},
fullDocument: {_id: 1001},
ns: {db: mongosDB.getName(), coll: mongosColl.getName()},
operationType: "insert",
});
changeStream.close();
// Test that using change streams with any stages not allowed to run on mongos results in an
// error.
assertErrorCode(
mongosColl, [{$changeStream: {}}, {$out: "shouldntWork"}], ErrorCodes.IllegalOperation);
// Test that it is legal to open a change stream, even if the
// 'internalQueryProhibitMergingOnMongos' parameter is set.
assert.commandWorked(
mongosDB.adminCommand({setParameter: 1, internalQueryProhibitMergingOnMongoS: true}));
let tempCursor = assert.doesNotThrow(() => mongosColl.aggregate([{$changeStream: {}}]));
tempCursor.close();
assert.commandWorked(
mongosDB.adminCommand({setParameter: 1, internalQueryProhibitMergingOnMongoS: false}));
// Test that $sort and $group are banned from running in a $changeStream pipeline.
assertErrorCode(mongosColl,
[{$changeStream: {}}, {$sort: {operationType: 1}}],
ErrorCodes.IllegalOperation);
assertErrorCode(mongosColl,
[{$changeStream: {}}, {$group: {_id: "$documentKey"}}],
ErrorCodes.IllegalOperation);
assert.writeOK(mongosColl.remove({}));
// We awaited the replication of the first write, so the change stream shouldn't return it.
// Use { w: "majority" } to deal with journaling correctly, even though we only have one node.
assert.writeOK(mongosColl.insert({_id: 0, a: 1}, {writeConcern: {w: "majority"}}));
changeStream = mongosColl.aggregate([{$changeStream: {}}, {$project: {"_id.clusterTime": 0}}]);
assert(!changeStream.hasNext());
// Drop the collection and test that we return a "drop" followed by an "invalidate" entry and
// close the cursor.
jsTestLog("Testing getMore command closes cursor for invalidate entries");
mongosColl.drop();
// Wait for the drop to actually happen.
assert.soon(() => !TwoPhaseDropCollectionTest.collectionIsPendingDropInDatabase(
mongosColl.getDB(), mongosColl.getName()));
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "drop");
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "invalidate");
assert(changeStream.isExhausted());
jsTestLog("Testing aggregate command closes cursor for invalidate entries");
// Shard the test collection on _id.
assert.commandWorked(
mongosDB.adminCommand({shardCollection: mongosColl.getFullName(), key: {_id: 1}}));
// Split the collection into 2 chunks: [MinKey, 0), [0, MaxKey).
assert.commandWorked(
mongosDB.adminCommand({split: mongosColl.getFullName(), middle: {_id: 0}}));
// Move the [0, MaxKey) chunk to st.shard1.shardName.
assert.commandWorked(mongosDB.adminCommand(
{moveChunk: mongosColl.getFullName(), find: {_id: 1}, to: st.rs1.getURL()}));
// Write one document to each chunk.
assert.writeOK(mongosColl.insert({_id: -1}, {writeConcern: {w: "majority"}}));
assert.writeOK(mongosColl.insert({_id: 1}, {writeConcern: {w: "majority"}}));
changeStream = mongosColl.aggregate([{$changeStream: {}}]);
assert(!changeStream.hasNext());
// Store a valid resume token before dropping the collection, to be used later in the test.
assert.writeOK(mongosColl.insert({_id: -2}, {writeConcern: {w: "majority"}}));
assert.writeOK(mongosColl.insert({_id: 2}, {writeConcern: {w: "majority"}}));
assert.soon(() => changeStream.hasNext());
const resumeToken = changeStream.next()._id;
mongosColl.drop();
assert.soon(() => changeStream.hasNext());
let next = changeStream.next();
assert.eq(next.operationType, "insert");
assert.eq(next.documentKey._id, 2);
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "drop");
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "invalidate");
// With an explicit collation, test that we can resume from before the collection drop.
changeStream = mongosColl.watch([{$project: {_id: 0}}],
{resumeAfter: resumeToken, collation: {locale: "simple"}});
assert.soon(() => changeStream.hasNext());
next = changeStream.next();
assert.eq(next.operationType, "insert");
assert.eq(next.documentKey, {_id: 2});
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "drop");
assert.soon(() => changeStream.hasNext());
assert.eq(changeStream.next().operationType, "invalidate");
// Without an explicit collation, test that we *cannot* resume from before the collection drop.
assert.commandFailedWithCode(mongosDB.runCommand({
aggregate: mongosColl.getName(),
pipeline: [{$changeStream: {resumeAfter: resumeToken}}],
cursor: {}
}),
ErrorCodes.InvalidResumeToken);
st.stop();
})();
|