/
read_pref_multi_mongos_stale_config.js
39 lines (28 loc) · 1.36 KB
/
read_pref_multi_mongos_stale_config.js
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
//
// Tests that a mongos will correctly retry a stale shard version when read preference is used
//
var st = new ShardingTest({shards : {rs0 : {quiet : ''}, rs1 : {quiet : ''}},
mongos : 2,
other : {mongosOptions : {verbose : 2}}});
var testDB1 = st.s0.getDB('test');
var testDB2 = st.s1.getDB('test');
// Trigger a query on mongos 1 so it will have a view of test.user as being unsharded.
testDB1.user.findOne();
testDB2.adminCommand({ enableSharding: 'test' });
testDB2.adminCommand({ shardCollection: 'test.user', key: { x: 1 }});
testDB2.adminCommand({ split: 'test.user', middle: { x: 100 }});
var configDB2 = st.s1.getDB('config');
var chunkToMove = configDB2.chunks.find().sort({ min: 1 }).next();
var toShard = configDB2.shards.findOne({ _id: { $ne: chunkToMove.shard }})._id;
testDB2.adminCommand({ moveChunk: 'test.user', to: toShard, find: { x: 50 }});
// Insert a document into each chunk
assert.writeOK(testDB2.user.insert({ x: 30 }));
assert.writeOK(testDB2.user.insert({ x: 130 }));
// The testDB1 mongos does not know the chunk has been moved, and will retry
var cursor = testDB1.user.find({ x: 30 }).readPref('primary');
assert(cursor.hasNext());
assert.eq(30, cursor.next().x);
cursor = testDB1.user.find({ x: 130 }).readPref('primary');
assert(cursor.hasNext());
assert.eq(130, cursor.next().x);
st.stop();