summaryrefslogtreecommitdiff
path: root/jstests/replsets/disconnect_on_legacy_write_to_secondary.js
blob: 1afa8ba0f334130e564ff2c96f99e413bd19080f (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
/**
 * Tests that legacy writes to secondaries result in no answer and a disconnection.
 */
(function() {
    "use strict";

    load("jstests/libs/check_log.js");

    const rst = new ReplSetTest({nodes: [{}, {rsConfig: {priority: 0}}]});
    rst.startSet();
    rst.initiate();

    const primary = rst.getPrimary();
    const secondary = rst.getSecondary();
    const collname = "disconnect_on_legacy_write_to_secondary";
    const coll = primary.getDB("test")[collname];
    const secondaryDb = secondary.getDB("test");
    const secondaryColl = secondaryDb[collname];

    // Never retry on network error, because this test needs to detect the network error.
    TestData.skipRetryOnNetworkError = true;
    secondary.forceWriteMode('legacy');
    assert.commandWorked(coll.insert([{_id: 'deleteme'}, {_id: 'updateme'}]));
    rst.awaitReplication();

    jsTestLog("Trying legacy insert on secondary");
    secondaryColl.insert({_id: 'no_insert_on_secondary'});
    let res = assert.throws(() => secondaryDb.adminCommand({ping: 1}));
    assert(isNetworkError(res));
    // We should automatically reconnect after the failed command.
    assert.commandWorked(secondaryDb.adminCommand({ping: 1}));

    jsTestLog("Trying legacy update on secondary");
    secondaryColl.update({_id: 'updateme'}, {'$set': {x: 1}});
    res = assert.throws(() => secondaryDb.adminCommand({ping: 1}));
    assert(isNetworkError(res));
    // We should automatically reconnect after the failed command.
    assert.commandWorked(secondaryDb.adminCommand({ping: 1}));

    jsTestLog("Trying legacy remove on secondary");
    secondaryColl.remove({_id: 'deleteme'}, {'$set': {x: 1}});
    res = assert.throws(() => secondaryDb.adminCommand({ping: 1}));
    assert(isNetworkError(res));
    // We should automatically reconnect after the failed command.
    assert.commandWorked(secondaryDb.adminCommand({ping: 1}));

    // Do the stepdown tests on a separate connection to avoid interfering with the
    // ReplSetTest mechanism.
    const primaryAdmin = primary.getDB("admin");
    const primaryDataConn = new Mongo(primary.host);
    const primaryDb = primaryDataConn.getDB("test");
    const primaryColl = primaryDb[collname];
    primaryDataConn.forceWriteMode('legacy');

    function getNotMasterLegacyUnackWritesCounter() {
        return assert.commandWorked(primaryAdmin.adminCommand({serverStatus: 1}))
            .metrics.repl.network.notMasterLegacyUnacknowledgedWrites;
    }

    function runStepDownTest({description, failpoint, operation}) {
        jsTestLog("Enabling failpoint to block " + description + "s");
        assert.commandWorked(
            primaryAdmin.adminCommand({configureFailPoint: failpoint, mode: "alwaysOn"}));

        let failedLegacyUnackWritesBefore = getNotMasterLegacyUnackWritesCounter();

        jsTestLog("Trying legacy " + description + " on stepping-down primary");
        operation();
        checkLog.contains(primary, failpoint + " fail point enabled");
        jsTestLog("Within " + description + ": stepping down and disabling failpoint");
        assert.commandWorked(primaryAdmin.adminCommand({replSetStepDown: 60, force: true}));
        rst.waitForState(primary, ReplSetTest.State.SECONDARY);
        assert.commandWorked(
            primaryAdmin.adminCommand({configureFailPoint: failpoint, mode: "off"}));
        res = assert.throws(() => primaryDb.adminCommand({ping: 1}));
        assert(isNetworkError(res));
        // We should automatically reconnect after the failed command.
        assert.commandWorked(primaryDb.adminCommand({ping: 1}));

        // Validate the number of legacy unacknowledged writes failed due to step down resulted
        // in network disconnection.
        let failedLegacyUnackWritesAfter = getNotMasterLegacyUnackWritesCounter();
        assert.eq(failedLegacyUnackWritesAfter, failedLegacyUnackWritesBefore + 1);

        // Allow the primary to be re-elected, and wait for it.
        assert.commandWorked(primaryAdmin.adminCommand({replSetFreeze: 0}));
        rst.getPrimary();
    }
    runStepDownTest({
        description: "insert",
        failpoint: "hangDuringBatchInsert",
        operation: () => primaryColl.insert({_id: 'no_insert_on_stepdown'})
    });

    runStepDownTest({
        description: "update",
        failpoint: "hangDuringBatchUpdate",
        operation: () => primaryColl.update({_id: 'updateme'}, {'$set': {x: 1}})
    });

    runStepDownTest({
        description: "remove",
        failpoint: "hangDuringBatchRemove",
        operation: () => primaryColl.remove({_id: 'deleteme'}, {'$set': {x: 1}})
    });

    rst.stopSet();
})();