summaryrefslogtreecommitdiff
path: root/jstests/replsets/priority_takeover_two_nodes_equal_priority.js
blob: 525d00723f5fe9de33d648948563d1ed9fef4f2e (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
/**
 * Test to ensure that nodes with the highest priorities eventually become PRIMARY.
 *
 * 1. Initiate a 3 node replica set with node priorities of 3, 3 and 1 (default)
 * 2. Make sure that one of the highest priority nodes becomes PRIMARY.
 * 3. Step down the PRIMARY and confirm that the other high priority node becomes PRIMARY.
 */
load('jstests/replsets/rslib.js');

(function() {
'use strict';

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

jsTestLog("Waiting for one of the high priority nodes to become PRIMARY.");
var primary;
var primaryIndex = -1;
var defaultPriorityNodeIndex = 2;
assert.soon(
    function() {
        primary = replTest.getPrimary();
        replTest.nodes.find(function(node, index, array) {
            if (primary.host == node.host) {
                primaryIndex = index;
                return true;
            }
            return false;
        });
        return primaryIndex !== defaultPriorityNodeIndex;
    },
    'Neither of the high priority nodes was elected primary.',
    replTest.kDefaultTimeoutMS,  // timeout
    1000                         // interval
);

jsTestLog("Stepping down the current primary.");
assert.commandWorked(
    primary.adminCommand({replSetStepDown: 10 * 60 * 3, secondaryCatchUpPeriodSecs: 10 * 60}));

// Make sure the primary has stepped down.
assert.neq(primary, replTest.getPrimary());

// We expect the other high priority node to eventually become primary.
var expectedNewPrimaryIndex = (primaryIndex === 0) ? 1 : 0;

jsTestLog("Waiting for the other high priority node to become PRIMARY.");
var expectedNewPrimary = replTest.nodes[expectedNewPrimaryIndex];
replTest.waitForState(expectedNewPrimary, ReplSetTest.State.PRIMARY);
replTest.stopSet();
})();