summaryrefslogtreecommitdiff
path: root/jstests/sharding/rs_stepdown_and_pooling.js
blob: 0e34f19636eb196ce5a904f4bd9b74f499789e54 (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
//
// Tests what happens when a replica set primary goes down with pooled connections.
//
(function() {
    "use strict";
    load("jstests/replsets/rslib.js");

    var st = new ShardingTest({shards: {rs0: {nodes: 2}}, mongos: 1});

    // Stop balancer to eliminate weird conn stuff
    st.stopBalancer();

    var mongos = st.s0;
    var coll = mongos.getCollection("foo.bar");
    var db = coll.getDB();

    // Test is not valid for Win32
    var is32Bits = (db.serverBuildInfo().bits == 32);
    if (is32Bits && _isWindows()) {
        // Win32 doesn't provide the polling interface we need to implement the check tested here
        jsTest.log("Test is not valid on Win32 platform.");

    } else {
        // Non-Win32 platform

        var primary = st.rs0.getPrimary();
        var secondary = st.rs0.getSecondary();

        jsTest.log("Creating new connections...");

        // Create a bunch of connections to the primary node through mongos.
        // jstest ->(x10)-> mongos ->(x10)-> primary
        var conns = [];
        for (var i = 0; i < 50; i++) {
            conns.push(new Mongo(mongos.host));
            conns[i].getCollection(coll + "").findOne();
        }

        jsTest.log("Returning the connections back to the pool.");

        for (var i = 0; i < conns.length; i++) {
            conns[i] = null;
        }
        // Make sure we return connections back to the pool
        gc();

        // Don't make test fragile by linking to format of shardConnPoolStats, but this is useful if
        // something goes wrong.
        var connPoolStats = mongos.getDB("admin").runCommand({shardConnPoolStats: 1});
        printjson(connPoolStats);

        jsTest.log("Stepdown primary and then step back up...");

        var stepDown = function(node, timeSecs) {
            var result = null;
            try {
                result = node.getDB("admin").runCommand({replSetStepDown: timeSecs, force: true});
                // Should not get here
            } catch (e) {
                printjson(e);
            }

            if (result != null)
                printjson(result);
            assert.eq(null, result);
        };

        stepDown(primary, 0);

        jsTest.log("Waiting for mongos to acknowledge stepdown...");

        awaitRSClientHosts(mongos,
                           secondary,
                           {ismaster: true},
                           st.rs0,
                           2 * 60 * 1000);  // slow hosts can take longer to recognize sd

        jsTest.log("Stepping back up...");

        stepDown(secondary, 10000);

        jsTest.log("Waiting for mongos to acknowledge step up...");

        awaitRSClientHosts(mongos, primary, {ismaster: true}, st.rs0, 2 * 60 * 1000);

        jsTest.log("Waiting for socket timeout time...");

        // Need to wait longer than the socket polling time.
        sleep(2 * 5000);

        jsTest.log("Run queries using new connections.");

        var numErrors = 0;
        for (var i = 0; i < conns.length; i++) {
            var newConn = new Mongo(mongos.host);
            try {
                printjson(newConn.getCollection("foo.bar").findOne());
            } catch (e) {
                printjson(e);
                numErrors++;
            }
        }

        assert.eq(0, numErrors);

    }  // End Win32 check

    jsTest.log("DONE!");

    st.stop();
}());