summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/drop_connections_replSet.js
blob: d8e07397afe19f36e75db19e87bcbaa557bddeea (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
/**
 * verify dropConnections command works for replica sets
 * @tags: [requires_replication]
 */

(function() {
    "use strict";

    const rst = new ReplSetTest({nodes: 3});
    rst.startSet();
    rst.initiate();

    const primary = rst.getPrimary();
    rst.awaitSecondaryNodes();

    function getConnPoolHosts() {
        const ret = primary.adminCommand({connPoolStats: 1});
        assert.commandWorked(ret);
        jsTestLog("Connection pool stats by host: " + tojson(ret.hosts));
        return ret.hosts;
    }

    // To test the dropConnections command, first remove the secondary. This should have no effect
    // on the existing connection pool, but it'll prevent the primary from reconnecting to it after
    // dropConnections. Then, execute dropConnections and check that the primary has 0 connections
    // to the secondary.
    const cfg = primary.getDB('local').system.replset.findOne();
    const memberHost = cfg.members[2].host;
    assert.eq(memberHost in getConnPoolHosts(), true);

    const removedMember = cfg.members.splice(2, 1);
    assert.eq(removedMember[0].host, memberHost);
    cfg.version++;

    jsTestLog("Reconfiguring to omit " + memberHost);
    assert.commandWorked(primary.adminCommand({replSetReconfig: cfg}));

    // Reconfig did not affect the connection pool
    assert.eq(memberHost in getConnPoolHosts(), true);

    // Test dropConnections
    jsTestLog("Dropping connections to " + memberHost);
    assert.commandWorked(primary.adminCommand({dropConnections: 1, hostAndPort: [memberHost]}));
    assert.soon(() => {
        return !(memberHost in getConnPoolHosts());
    });

    // Need to re-add removed node, or the test complains about the replset config
    cfg.members.push(removedMember[0]);
    cfg.version++;
    assert.commandWorked(primary.adminCommand({replSetReconfig: cfg}));

    rst.stopSet();
})();