summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/cluster_server_parameter_refresher.js
blob: a0be0a5dac95f372d7fffb0cd66d3d59f01ebca0 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * Checks that the mongos cluster server parameter refresh job runs as expected.
 *
 * @tags: [
 *   # Requires all nodes to be running at least 6.3.
 *   requires_fcv_63,
 *   does_not_support_stepdowns,
 *   requires_replication,
 *   requires_sharding
 *  ]
 */
(function() {
'use strict';

load('jstests/libs/cluster_server_parameter_utils.js');

function runTest(st, startupRefreshIntervalMS) {
    // This assert is necessary because we subtract 8000 MS from this value later on, and we don't
    // want the interval to go below 1 second.
    assert(startupRefreshIntervalMS >= 9000);
    // First, check that the mongos logs a refresh attempt within the first refreshIntervalMS
    // milliseconds that finds no documents on the config servers.
    const conn = st.s0;
    const errorMarginMS = 5000;
    const kRefreshLogId = 6226403;
    const startupRefreshIntervalRelaxedMS = startupRefreshIntervalMS + errorMarginMS;
    let expectedParams = {};
    const defaultParams =
        Object.fromEntries(kAllClusterParameterDefaults.map(elem => [elem._id, elem]));

    function assertParams(timeoutMillis) {
        let expectedLogged = Object.keys(expectedParams).map(key => {
            return {_id: key, ...expectedParams[key]};
        });
        expectedLogged.sort(bsonWoCompare);
        assert.soon(function() {
            const logMessages = checkLog.getGlobalLog(conn);
            if (logMessages === null) {
                return false;
            }

            for (let logMsg of logMessages) {
                let obj;
                try {
                    obj = JSON.parse(logMsg);
                } catch (ex) {
                    print('JSON.parse() failed: ' + tojson(ex) + ': ' + logMsg);
                    throw ex;
                }

                if (obj.id === kRefreshLogId) {
                    let cpd = obj.attr.clusterParameterDocuments[0].updatedParameters;
                    cpd.sort(bsonWoCompare);
                    if (bsonWoCompare(cpd, expectedLogged) === 0) {
                        return true;
                    }
                }
            }
            return false;
        }, 'Could not find matching log entry', timeoutMillis, 300, {runHangAnalyzer: false});

        runGetClusterParameterSharded(
            st, '*', Object.values({...defaultParams, ...expectedParams}));
    }

    assertParams(startupRefreshIntervalRelaxedMS);

    // Set a cluster parameter to a different value and then wait.
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testIntClusterParameter: {intData: 2022}}}));
    expectedParams.testIntClusterParameter = {_id: "testIntClusterParameter", intData: 2022};

    // Check that the newly set parameter is refreshed within the interval.
    assertParams(startupRefreshIntervalRelaxedMS);

    // Set another cluster parameter and check that both parameters are refreshed.
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testStrClusterParameter: {strData: "welcome"}}}));
    expectedParams.testStrClusterParameter = {_id: "testStrClusterParameter", strData: "welcome"};

    assertParams(startupRefreshIntervalRelaxedMS);

    // Ensure that updates to the refresh interval take effect correctly.
    const newRefreshIntervalMS = startupRefreshIntervalMS - errorMarginMS - 3000;
    const newRefreshIntervalRelaxedMS = newRefreshIntervalMS + errorMarginMS;
    assert.commandWorked(conn.adminCommand(
        {setParameter: 1, clusterServerParameterRefreshIntervalSecs: newRefreshIntervalMS / 1000}));
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testIntClusterParameter: {intData: 2025}}}));
    expectedParams.testIntClusterParameter.intData = 2025;

    assertParams(newRefreshIntervalRelaxedMS);

    // Restart the mongos and check that it refreshes both of the parameters that have documents on
    // the config server.
    st.restartMongos(0);
    assertParams(newRefreshIntervalRelaxedMS);

    // Check that single parameter updates are caught as expected after restart. Note that the
    // startup refresh interval is used since runtime setParameter updates are not persisted.
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testStrClusterParameter: {strData: "goodbye"}}}));
    expectedParams.testStrClusterParameter.strData = "goodbye";

    assertParams(startupRefreshIntervalRelaxedMS);

    // Check that multiple setClusterParameter invocations within a single refresh cycle
    // are captured and updated.
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testIntClusterParameter: {intData: 2028}}}));
    assert.commandWorked(
        conn.adminCommand({setClusterParameter: {testBoolClusterParameter: {boolData: true}}}));
    expectedParams.testIntClusterParameter.intData = 2028;
    expectedParams.testBoolClusterParameter = {_id: "testBoolClusterParameter", boolData: true};

    assertParams(startupRefreshIntervalRelaxedMS);

    // Ensure that deletes are captured and properly refreshed.
    [st.configRS, ...st._rs.map(rs => rs.test)].forEach(rs => {
        assert.commandWorked(rs.getPrimary().getDB('config').clusterParameters.remove(
            {_id: "testIntClusterParameter"}));
    });
    delete expectedParams.testIntClusterParameter;

    assertParams(startupRefreshIntervalRelaxedMS);

    // Try deleting the whole collection and make sure that it's properly refreshed.
    [st.configRS, ...st._rs.map(rs => rs.test)].forEach(rs => {
        assert(rs.getPrimary().getDB('config').clusterParameters.drop());
    });

    // Perform a dummy write in order to get the config shard's cluster time cached on the mongos.
    st.s.getDB('config').abc.insert({a: "hello"});

    expectedParams = {};
    assertParams(startupRefreshIntervalRelaxedMS);
}

// Test with shortened clusterServerParameterRefreshIntervalSecs at startup.
let options = {
    mongos: 1,
    config: 1,
    shards: 3,
    rs: {
        nodes: 3,
    },
    mongosOptions: {
        setParameter: {
            logLevel: 3,
            clusterServerParameterRefreshIntervalSecs: 10,
        },
    },
};
let st = new ShardingTest(options);
runTest(st, 10000);
st.stop();
})();