summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/conn_pool_csrs.js
blob: 2f6f7aa32ded5455156c327dc5fecde8653bd895 (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
112
113
114
115
116
117
118
119
load("jstests/libs/parallelTester.js");

/**
 * @tags: [requires_replication, requires_sharding]
 *
 * Test that servers can be configured with distinct size limits for
 * connection pools that are connected to a config server.
 *
 * Heavily modified from jstest/noPassthrough/set_step_params.js
 */

(function() {
"use strict";

const poolSizeLogId = 6265600;

const rsMin = 10;
const rsMax = 20;

const baselineParameters = {
    ShardingTaskExecutorPoolMinSize: rsMin,
    ShardingTaskExecutorPoolMaxSize: rsMax,
    ShardingTaskExecutorPoolMinSizeForConfigServers: 4,
    ShardingTaskExecutorPoolMaxSizeForConfigServers: 6,
};

const mongosParameters = Object.assign(
    {logComponentVerbosity: tojson({network: {connectionPool: 5}})}, baselineParameters);

const st = new ShardingTest({
    config: {nodes: 1},
    shards: 1,
    rs0: {nodes: 1},
    mongos: [{setParameter: mongosParameters}],
});
const mongos = st.s0;
const configServer = st.c0;
const mongod = st.rs0.getPrimary();

const adminCmd = req => assert.commandWorked(mongos.adminCommand(req));

const populateTestDb = () => {
    const db = mongos.getDB('test');
    const coll = db.test;
    assert.commandWorked(coll.insert({x: 1}));
    assert.commandWorked(coll.insert({x: 2}));
    assert.commandWorked(coll.insert({x: 3}));
};

const setCSPoolBounds = (min, max) => {
    adminCmd(Object.assign({"setParameter": 1}, {
        ShardingTaskExecutorPoolMinSizeForConfigServers: min,
        ShardingTaskExecutorPoolMaxSizeForConfigServers: max,
    }));
};

// Make mongos open a connection to the config server. This is done by issuing a
// query that mongos will connect to the config server to execute.
const connectConfigServer = () => {
    jsTestLog('Cause Config Server connection');
    assert.commandWorked(mongos.getDB('config').runCommand(
        {find: "databases", limit: 1, "$readPreference": {mode: 'primary'}}));
};

// The (only) mongod has a 'test' database.
const connectMongodServer = () => {
    jsTestLog('Cause Mongod Server connection');
    assert.commandWorked(mongos.getDB('test').runCommand(
        {find: "test", limit: 1, "$readPreference": {mode: 'primary'}}));
};

// Waits until mongos emits a log line indicating that the conn pool
// `targetHost` is being resized. Returns all such lines.
const awaitUpdateHost = targetHost => {
    let hits;
    assert.soon(() => {
        let log = checkLog.getGlobalLog(st.s);
        // jsTestLog(`Fetched log: """${log}"""`);
        hits = checkLog.getGlobalLog(mongos)
                   .map(line => JSON.parse(line))
                   .filter(o => o.id == poolSizeLogId)
                   .filter(o => o.attr.host == targetHost);
        return hits.length > 0;
    }, `log lines id:${poolSizeLogId} for target ${targetHost}`, 10 * 1000, 1 * 1000, {
        runHangAnalyzer: false
    });
    return hits;
};

populateTestDb();

// Try a few {min,max} pairs.
for (const [min, max] of [[4, 6], [10, 20], [2, 4], [-1, -1]]) {
    jsTestLog(`Try ConfigServer pool bounds [${min},${max}]`);
    setCSPoolBounds(min, max);

    adminCmd({dropConnections: 1, hostAndPort: [configServer.host]});
    adminCmd({clearLog: 'global'});
    connectConfigServer();
    for (let o of awaitUpdateHost(configServer.host)) {
        const cascade = (x, fallback) => x >= 0 ? x : fallback;
        assert.eq(o.attr.minConns, cascade(min, rsMin));
        assert.eq(o.attr.maxConns, cascade(max, rsMax));
    }

    // Make sure the setting doesn't affect non-ConfigServer pools.
    adminCmd({dropConnections: 1, hostAndPort: [mongod.host]});
    adminCmd({clearLog: 'global'});
    connectMongodServer();
    for (let o of awaitUpdateHost(mongod.host)) {
        assert.eq(o.attr.minConns, rsMin);
        assert.eq(o.attr.maxConns, rsMax);
    }

    adminCmd(Object.assign({"setParameter": 1}, baselineParameters));
}

st.stop();
})();