summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/change_streams_collation_chunk_migration.js
blob: ea7fd26b72e2dfcfadb779786771d25e643444f4 (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
/**
 * Tests that a change stream on a sharded collection with a non-simple default collation is not
 * erroneously invalidated upon chunk migration. Reproduction script for the bug in SERVER-33944.
 * @tags: [
 *   requires_replication,
 * ]
 */
(function() {
load("jstests/libs/collection_drop_recreate.js");  // For assert[Drop|Create]Collection.
load("jstests/libs/change_stream_util.js");        // For 'ChangeStreamTest'.

const st = new ShardingTest({
    shards: 2,
    mongos: 1,
    rs: {
        nodes: 1,
    },
});

const testDB = st.s.getDB(jsTestName());

// Enable sharding on the test database and ensure that the primary is shard0.
assert.commandWorked(testDB.adminCommand({enableSharding: testDB.getName()}));
st.ensurePrimaryShard(testDB.getName(), st.shard0.shardName);

const caseInsensitiveCollectionName = "change_stream_case_insensitive";
const caseInsensitive = {
    locale: "en_US",
    strength: 2
};

// Create the collection with a case-insensitive collation, then shard it on {shardKey: 1}.
const caseInsensitiveCollection = assertDropAndRecreateCollection(
    testDB, caseInsensitiveCollectionName, {collation: caseInsensitive});
assert.commandWorked(
    caseInsensitiveCollection.createIndex({shardKey: 1}, {collation: {locale: "simple"}}));
assert.commandWorked(testDB.adminCommand({
    shardCollection: caseInsensitiveCollection.getFullName(),
    key: {shardKey: 1},
    collation: {locale: "simple"}
}));

// Verify that the collection does not exist on shard1.
assert(!st.shard1.getCollection(caseInsensitiveCollection.getFullName()).exists());

// Now open a change stream on the collection.
const cst = new ChangeStreamTest(testDB);
const csCursor = cst.startWatchingChanges({
    pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey.shardKey"}}],
    collection: caseInsensitiveCollection
});

// Insert some documents into the collection.
assert.commandWorked(caseInsensitiveCollection.insert({shardKey: 0, text: "aBc"}));
assert.commandWorked(caseInsensitiveCollection.insert({shardKey: 1, text: "abc"}));

// Move a chunk from shard0 to shard1. This will create the collection on shard1.
assert.commandWorked(testDB.adminCommand({
    moveChunk: caseInsensitiveCollection.getFullName(),
    find: {shardKey: 1},
    to: st.rs1.getURL(),
    _waitForDelete: false
}));

// Attempt to read from the change stream. We should see both inserts, without an invalidation.
cst.assertNextChangesEqual({cursor: csCursor, expectedChanges: [{docId: 0}, {docId: 1}]});

st.stop();
})();