summaryrefslogtreecommitdiff
path: root/jstests/sharding/count_secondaryok.js
blob: c5fcedf941b175aa518e0d7ab79adb19be0e9f71 (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
/**
 * Tests count and distinct using secondaryOk. Also tests a scenario querying a set where only one
 * secondary is up.
 */

// This test shuts down a shard's node and because of this consistency checking
// cannot be performed on that node, which causes the consistency checker to fail.
TestData.skipCheckingUUIDsConsistentAcrossCluster = true;
TestData.skipCheckingIndexesConsistentAcrossCluster = true;
TestData.skipCheckOrphans = true;

(function() {
'use strict';

load("jstests/replsets/rslib.js");

var st = new ShardingTest({shards: 1, mongos: 1, other: {rs: true, rs0: {nodes: 2}}});
var rst = st.rs0;

// Insert data into replica set
var conn = new Mongo(st.s.host);

var coll = conn.getCollection('test.countSecondaryOk');
coll.drop();

var bulk = coll.initializeUnorderedBulkOp();
for (var i = 0; i < 300; i++) {
    bulk.insert({i: i % 10});
}
assert.commandWorked(bulk.execute());

var connA = conn;
var connB = new Mongo(st.s.host);
var connC = new Mongo(st.s.host);

st.printShardingStatus();

// Wait for client to update itself and replication to finish
rst.awaitReplication();

var primary = rst.getPrimary();
var sec = rst.getSecondary();

// Need to check secondaryOk=true first, since secondaryOk=false will destroy conn in pool when
// primary is down.
conn.setSecondaryOk();

// Do a read concern "local" read so that the secondary refreshes its dbVersion before we shut down
// the primary.
coll.runCommand("find", {readConcern: {level: "local"}});

// Data now inserted... stop the primary, since only two in set, other will still be secondary
rst.stop(rst.getPrimary());
printjson(rst.status());

// Wait for the mongos to recognize the secondary
awaitRSClientHosts(conn, sec, {ok: true, secondary: true});

// Make sure that mongos realizes that primary is already down
awaitRSClientHosts(conn, primary, {ok: false});

// count using the command path
assert.eq(30, coll.find({i: 0}).count());
// count using the query path
assert.eq(30, coll.find({i: 0}).itcount());
assert.eq(10, coll.distinct("i").length);

try {
    conn.setSecondaryOk(false);
    // Should throw exception, since not secondaryOk'd
    coll.find({i: 0}).count();

    print("Should not reach here!");
    assert(false);
} catch (e) {
    print("Non-secondaryOk'd connection failed.");
}

st.stop();
})();