summaryrefslogtreecommitdiff
path: root/jstests/replsets/priority_takeover_two_nodes_equal_priority.js
blob: b6e8cc25b77dbb1938cb7572c0ce9109c5df547a (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
// 2 nodes with non-default priority.
// 3-node replica set with priorities 3, 3 and 1 (default)
// Start replica set. Ensure that highest priority node becomes primary eventually.
// Step down the primary and confirm that the next highest priority node becomes primary.
load('jstests/replsets/rslib.js');

(function() {
    'use strict';

    var name = 'priority_takeover_two_nodes_equal_priority';
    var replSet = new ReplSetTest({
        name: name,
        nodes: [
            {rsConfig: {priority: 3}},
            {rsConfig: {priority: 3}},
            {},
        ]
    });
    replSet.startSet();
    replSet.initiate();

    var primary;
    var primaryIndex = -1;
    var defaultPriorityNodeIndex = 2;
    assert.soon(
        function() {
            primary = replSet.getPrimary();
            replSet.nodes.find(function(node, index, array) {
                if (primary.host == node.host) {
                    primaryIndex = index;
                    return true;
                }
                return false;
            });
            return primaryIndex !== defaultPriorityNodeIndex;
        },
        'neither of the priority 3 nodes was elected primary',
        60000,  // timeout
        1000    // interval
        );

    try {
        assert.commandWorked(primary.getDB('admin').runCommand({replSetStepDown: 90}));
    } catch (x) {
        // expected
    }
    var newPrimaryIndex = primaryIndex === 0 ? 1 : 0;

    // Refresh connections to nodes.
    replSet.status();

    assert.commandWorked(replSet.nodes[newPrimaryIndex].adminCommand({
        replSetTest: 1,
        waitForMemberState: ReplSetTest.State.PRIMARY,
        timeoutMillis: 60 * 1000,
    }),
                         'node ' + newPrimaryIndex + ' ' + replSet.nodes[newPrimaryIndex].host +
                             ' failed to become primary');

})();