summaryrefslogtreecommitdiff
path: root/jstests/sharding/collection_uuid_aggregate.js
blob: 3607ff6bb61dc65c3b24c02e22934ea18b1bd5ff (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
/**
 * Tests the collectionUUID parameter of the aggregate command when not all shards own chunks for
 * the collection.
 *
 * @tags: [
 *   requires_fcv_60,
 * ]
 */
(function() {
'use strict';

const st = new ShardingTest({shards: 3});

const db = st.s.getDB(jsTestName());
assert.commandWorked(st.s.adminCommand({enableSharding: db.getName()}));
st.ensurePrimaryShard(db.getName(), st.shard0.shardName);

const shardedColl1 = db.sharded_1;
const shardedColl2 = db.sharded_2;
const unshardedColl = db.unsharded;

assert.commandWorked(shardedColl1.insert({_id: 0}));
assert.commandWorked(shardedColl2.insert({_id: 1}));
assert.commandWorked(unshardedColl.insert({_id: 2}));

const uuid = function(coll) {
    return assert.commandWorked(db.runCommand({listCollections: 1}))
        .cursor.firstBatch.find(c => c.name === coll.getName())
        .info.uuid;
};

assert.commandWorked(
    st.s.adminCommand({shardCollection: shardedColl1.getFullName(), key: {_id: 1}}));
assert.commandWorked(
    st.s.adminCommand({shardCollection: shardedColl2.getFullName(), key: {_id: 1}}));

// Move shardedColl1's chunk to shard1 and shardedColl2's chunk to shard2.
assert.commandWorked(st.s.adminCommand(
    {moveChunk: shardedColl1.getFullName(), find: {_id: 0}, to: st.shard1.shardName}));
assert.commandWorked(st.s.adminCommand(
    {moveChunk: shardedColl2.getFullName(), find: {_id: 0}, to: st.shard2.shardName}));

// Cannot use the collectionUUID parameter with a pipeline that executes entirely on mongos.
assert.commandFailedWithCode(db.adminCommand({
    aggregate: 1,
    pipeline: [{$currentOp: {localOps: true}}],
    cursor: {},
    collectionUUID: uuid(unshardedColl),
}),
                             6487500);

// Run an aggregate which will only target shard1, since shard0 and shard2 do not own any chunks.
let res = assert.commandFailedWithCode(db.runCommand({
    aggregate: shardedColl1.getName(),
    pipeline: [{$indexStats: {}}],
    cursor: {},
    collectionUUID: uuid(unshardedColl),
}),
                                       ErrorCodes.CollectionUUIDMismatch);
jsTestLog('$indexStats result: ' + tojson(res));
assert.eq(res.db, db.getName());
assert.eq(res.collectionUUID, uuid(unshardedColl));
assert.eq(res.expectedCollection, shardedColl1.getName());
assert.eq(res.actualCollection, unshardedColl.getName());

res = assert.commandFailedWithCode(db.runCommand({
    aggregate: shardedColl1.getName(),
    pipeline: [{$indexStats: {}}],
    cursor: {},
    collectionUUID: uuid(shardedColl2),
}),
                                   ErrorCodes.CollectionUUIDMismatch);
jsTestLog('$indexStats result: ' + tojson(res));
assert.eq(res.db, db.getName());
assert.eq(res.collectionUUID, uuid(shardedColl2));
assert.eq(res.expectedCollection, shardedColl1.getName());
assert.eq(res.actualCollection, shardedColl2.getName());

// Run an aggregate which will only target shard1, since that is the shard on which the chunk
// containing the matching document lives.
res = assert.commandFailedWithCode(db.runCommand({
    aggregate: shardedColl1.getName(),
    pipeline: [{$match: {_id: 0}}],
    cursor: {},
    collectionUUID: uuid(unshardedColl),
}),
                                   ErrorCodes.CollectionUUIDMismatch);
jsTestLog('$match result: ' + tojson(res));
assert.eq(res.db, db.getName());
assert.eq(res.collectionUUID, uuid(unshardedColl));
assert.eq(res.expectedCollection, shardedColl1.getName());
assert.eq(res.actualCollection, unshardedColl.getName());

res = assert.commandFailedWithCode(db.runCommand({
    aggregate: shardedColl1.getName(),
    pipeline: [{$match: {_id: 0}}],
    cursor: {},
    collectionUUID: uuid(shardedColl2),
}),
                                   ErrorCodes.CollectionUUIDMismatch);
jsTestLog('$match result: ' + tojson(res));
assert.eq(res.db, db.getName());
assert.eq(res.collectionUUID, uuid(shardedColl2));
assert.eq(res.expectedCollection, shardedColl1.getName());
assert.eq(res.actualCollection, shardedColl2.getName());

st.stop();
})();