summaryrefslogtreecommitdiff
path: root/jstests/sharding/sharding_non_transaction_snapshot_aggregate.js
blob: 137cb5f815110dfc2bcac9291fdcb0dd0e5d9f0e (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
/**
 * Tests aggregate $out, $merge, $lookup and $unionWith under readConcern level snapshot outside of
 * transactions.
 *
 * @tags: [
 *   requires_majority_read_concern,
 *   requires_persistence,
 *   disabled_due_to_server_58295
 * ]
 */

(function() {
"use strict";

load("jstests/libs/global_snapshot_reads_util.js");
load("jstests/sharding/libs/sharded_transactions_helpers.js");
load("jstests/sharding/libs/find_chunks_util.js");

const nodeOptions = {
    // Set a large snapshot window of 10 minutes for the test.
    setParameter: {minSnapshotHistoryWindowInSeconds: 600}
};

const dbName = "test";
// The collection is not sharded.
const unshardedColl1 = "unshardedColl1";
const unshardedColl2 = "unshardedColl2";
// The sharded collection lives in a single shard.
const singleShardedColl1 = "singleShardedColl1";
const singleShardedColl2 = "singleShardedColl2";
// The sharded collection lives in 2 out of the 3 shards.
const someShardedColl1 = "someShardedColl1";
const someShardedColl2 = "someShardedColl2";
// The sharded collection lives in all 3 shards.
const allShardedColl1 = "allShardedColl1";
const allShardedColl2 = "allShardedColl2";

const st = new ShardingTest({
    shards: {
        rs0: {nodes: 2},
        rs1: {nodes: 2},
        rs2: {nodes: 2},
    },
    mongos: 1,
    config: 1,
    other: {configOptions: nodeOptions, rsOptions: nodeOptions}
});
// Config sharded collections.
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
assert.commandWorked(st.s.adminCommand(
    {shardCollection: st.s.getDB(dbName)[singleShardedColl1] + "", key: {_id: 1}}));
assert.commandWorked(st.s.adminCommand(
    {shardCollection: st.s.getDB(dbName)[singleShardedColl2] + "", key: {_id: 1}}));
assert.commandWorked(
    st.s.adminCommand({shardCollection: st.s.getDB(dbName)[someShardedColl1] + "", key: {_id: 1}}));
assert.commandWorked(
    st.s.adminCommand({shardCollection: st.s.getDB(dbName)[someShardedColl2] + "", key: {_id: 1}}));
assert.commandWorked(
    st.s.adminCommand({shardCollection: st.s.getDB(dbName)[allShardedColl1] + "", key: {_id: 1}}));
assert.commandWorked(
    st.s.adminCommand({shardCollection: st.s.getDB(dbName)[allShardedColl2] + "", key: {_id: 1}}));

const mongos = st.s0;

// Set up someShardedColl.
const setupSomeShardedColl = (collName) => {
    let ns = dbName + '.' + collName;
    // snapshotReadsTest() inserts ids 0-9 and tries snapshot reads on the collection.
    assert.commandWorked(st.splitAt(ns, {_id: 5}));
    assert.commandWorked(
        mongos.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard1.shardName}));
    assert.commandWorked(
        mongos.adminCommand({moveChunk: ns, find: {_id: 7}, to: st.shard2.shardName}));

    assert.eq(
        0,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard0.shardName}));
    assert.eq(
        1,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard1.shardName}));
    assert.eq(
        1,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard2.shardName}));
    flushRoutersAndRefreshShardMetadata(st, {ns});
};
setupSomeShardedColl(someShardedColl1);
setupSomeShardedColl(someShardedColl2);

// Set up allShardedColl.
const setupAllShardedColl = (collName) => {
    let ns = dbName + '.' + collName;
    // snapshotReadsTest() inserts ids 0-9 and tries snapshot reads on the collection.
    assert.commandWorked(st.splitAt(ns, {_id: 4}));
    assert.commandWorked(st.splitAt(ns, {_id: 7}));

    assert.commandWorked(
        mongos.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard0.shardName}));
    assert.commandWorked(
        mongos.adminCommand({moveChunk: ns, find: {_id: 4}, to: st.shard1.shardName}));
    assert.commandWorked(
        mongos.adminCommand({moveChunk: ns, find: {_id: 7}, to: st.shard2.shardName}));

    assert.eq(
        1,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard0.shardName}));
    assert.eq(
        1,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard1.shardName}));
    assert.eq(
        1,
        findChunksUtil.countChunksForNs(mongos.getDB('config'), ns, {shard: st.shard2.shardName}));
    flushRoutersAndRefreshShardMetadata(st, {ns});
};
setupAllShardedColl(allShardedColl1);
setupAllShardedColl(allShardedColl2);

function awaitCommittedFn() {
    for (let i = 0; st['rs' + i] !== undefined; i++) {
        st['rs' + i].awaitLastOpCommitted();
    }
}

// Pass the same DB handle as "primaryDB" and "secondaryDB" params; the test functions will
// send readPreference to mongos to target primary/secondary shard servers.
let db = st.s.getDB(dbName);
let snapshotReadsTest =
    new SnapshotReadsTest({primaryDB: db, secondaryDB: db, awaitCommittedFn: awaitCommittedFn});

for (let coll1 of [unshardedColl1, singleShardedColl1, someShardedColl1, allShardedColl1]) {
    for (let coll2 of [unshardedColl2, singleShardedColl2, someShardedColl2, allShardedColl2]) {
        const scenarioName = `${coll1}+${coll2}`;
        snapshotReadsTest.outAndMergeTest({
            testScenarioName: scenarioName,
            coll: coll1,
            outColl: coll2,
            isOutCollSharded: (coll2 !== unshardedColl2)
        });
        snapshotReadsTest.lookupAndUnionWithTest({
            testScenarioName: scenarioName,
            coll1: coll1,
            coll2: coll2,
            isColl2Sharded: (coll2 !== unshardedColl2)
        });
    }
}

st.stop();
})();