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
|
/**
* Tests readConcern level snapshot outside of transactions.
*
* @tags: [
* requires_majority_read_concern,
* requires_persistence,
* ]
*/
(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";
const shardedCollName = "shardedColl";
const unshardedCollName = "unshardedColl";
function setUpAllScenarios(st) {
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
assert.commandWorked(st.s.adminCommand(
{shardCollection: st.s.getDB(dbName)[shardedCollName] + "", key: {_id: 1}}));
}
let shardingScenarios = {
singleShard: {
compatibleCollections: [shardedCollName, unshardedCollName],
setUp: function() {
const st = new ShardingTest({
mongos: 1,
config: 1,
shards: {rs0: {nodes: 2}},
other: {configOptions: nodeOptions, rsOptions: nodeOptions}
});
setUpAllScenarios(st);
return st;
}
},
multiShardAllShardReads: {
compatibleCollections: [shardedCollName],
setUp: function() {
let st = new ShardingTest({
shards: {
rs0: {nodes: 2},
rs1: {nodes: 2},
rs2: {nodes: 2},
},
mongos: 1,
config: 1,
other: {configOptions: nodeOptions, rsOptions: nodeOptions}
});
setUpAllScenarios(st);
const mongos = st.s0;
const ns = dbName + '.' + shardedCollName;
// 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});
return st;
}
},
// Only two out of three shards have documents.
multiShardSomeShardReads: {
compatibleCollections: [shardedCollName],
setUp: function() {
let st = new ShardingTest({
shards: {
rs0: {nodes: 2},
rs1: {nodes: 2},
rs2: {nodes: 2},
},
mongos: 1,
config: 1,
other: {configOptions: nodeOptions, rsOptions: nodeOptions}
});
setUpAllScenarios(st);
const mongos = st.s0;
const ns = dbName + '.' + shardedCollName;
// 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});
return st;
}
}
};
for (let [scenarioName, scenario] of Object.entries(shardingScenarios)) {
scenario.compatibleCollections.forEach(function(collName) {
jsTestLog(`Run scenario ${scenarioName} with collection ${collName}`);
let st = scenario.setUp();
let primaryAdmin = st.rs0.getPrimary().getDB("admin");
assert.eq(assert
.commandWorked(primaryAdmin.runCommand(
{getParameter: 1, minSnapshotHistoryWindowInSeconds: 1}))
.minSnapshotHistoryWindowInSeconds,
600);
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});
snapshotReadsTest.cursorTest({testScenarioName: scenarioName, collName: collName});
if (collName === shardedCollName) {
// "distinct" prohibited on sharded collections.
assert.commandFailedWithCode(
db.runCommand({distinct: collName, key: "_id", readConcern: {level: "snapshot"}}),
ErrorCodes.InvalidOptions);
} else {
snapshotReadsTest.distinctTest({testScenarioName: scenarioName, collName: collName});
}
st.stop();
});
}
})();
|