summaryrefslogtreecommitdiff
path: root/jstests/replsets/initial_sync_replSetGetStatus.js
blob: a453f33f12af31f8a6431f8b1b4d386155480e7e (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
/**
 * This test tests that replSetGetStatus returns initial sync stats while initial sync is in
 * progress.
 */

(function() {
    "use strict";
    load("jstests/libs/check_log.js");

    var name = 'initial_sync_replSetGetStatus';
    var replSet = new ReplSetTest({
        name: name,
        nodes: 1,
    });

    replSet.startSet();
    replSet.initiate();
    var primary = replSet.getPrimary();

    var coll = primary.getDB('test').foo;
    assert.writeOK(coll.insert({a: 1}));
    assert.writeOK(coll.insert({a: 2}));

    // Add a secondary node but make it hang before copying databases.
    var secondary = replSet.add();
    secondary.setSlaveOk();

    assert.commandWorked(secondary.getDB('admin').runCommand(
        {configureFailPoint: 'initialSyncHangBeforeCopyingDatabases', mode: 'alwaysOn'}));
    assert.commandWorked(secondary.getDB('admin').runCommand(
        {configureFailPoint: 'initialSyncHangBeforeFinish', mode: 'alwaysOn'}));
    replSet.reInitiate();

    // Wait for initial sync to pause before it copies the databases.
    checkLog.contains(secondary,
                      'initial sync - initialSyncHangBeforeCopyingDatabases fail point enabled');

    // Test that replSetGetStatus returns the correct results while initial sync is in progress.
    var res = assert.commandWorked(secondary.adminCommand({replSetGetStatus: 1}));
    assert(!res.initialSyncStatus,
           "Response should not have an 'initialSyncStatus' field: " + tojson(res));

    res = assert.commandWorked(secondary.adminCommand({replSetGetStatus: 1, initialSync: 1}));
    assert(res.initialSyncStatus,
           "Response should have an 'initialSyncStatus' field: " + tojson(res));

    assert.commandFailedWithCode(secondary.adminCommand({replSetGetStatus: 1, initialSync: "t"}),
                                 ErrorCodes.TypeMismatch);

    assert.writeOK(coll.insert({a: 3}));
    assert.writeOK(coll.insert({a: 4}));

    // Let initial sync continue working.
    assert.commandWorked(secondary.getDB('admin').runCommand(
        {configureFailPoint: 'initialSyncHangBeforeCopyingDatabases', mode: 'off'}));

    // Wait for initial sync to pause right before it finishes.
    checkLog.contains(secondary, 'initial sync - initialSyncHangBeforeFinish fail point enabled');

    // Test that replSetGetStatus returns the correct results when initial sync is at the very end.
    res = assert.commandWorked(secondary.adminCommand({replSetGetStatus: 1, initialSync: 1}));
    assert(res.initialSyncStatus, "Response should have an 'initialSyncStatus' field.");
    assert.eq(res.initialSyncStatus.fetchedMissingDocs, 0);
    assert.eq(res.initialSyncStatus.appliedOps, 2);
    assert.eq(res.initialSyncStatus.failedInitialSyncAttempts, 0);
    assert.eq(res.initialSyncStatus.maxFailedInitialSyncAttempts, 1);
    assert.eq(res.initialSyncStatus.databases.databasesCloned, 3);
    assert.eq(res.initialSyncStatus.databases.test.collections, 1);
    assert.eq(res.initialSyncStatus.databases.test.clonedCollections, 1);
    assert.eq(res.initialSyncStatus.databases.test["test.foo"].documentsToCopy, 4);
    assert.eq(res.initialSyncStatus.databases.test["test.foo"].documentsCopied, 4);
    assert.eq(res.initialSyncStatus.databases.test["test.foo"].indexes, 1);
    assert.eq(res.initialSyncStatus.databases.test["test.foo"].fetchedBatches, 1);

    // Let initial sync finish and get into secondary state.
    assert.commandWorked(secondary.getDB('admin').runCommand(
        {configureFailPoint: 'initialSyncHangBeforeFinish', mode: 'off'}));
    replSet.awaitSecondaryNodes(60 * 1000);

    // Test that replSetGetStatus returns the correct results after initial sync is finished.
    res = assert.commandWorked(secondary.adminCommand({replSetGetStatus: 1}));
    assert(!res.initialSyncStatus,
           "Response should not have an 'initialSyncStatus' field: " + tojson(res));

    assert.commandFailedWithCode(secondary.adminCommand({replSetGetStatus: 1, initialSync: "m"}),
                                 ErrorCodes.TypeMismatch);
    assert.eq(0,
              secondary.getDB('local')['temp_oplog_buffer'].find().itcount(),
              "Oplog buffer was not dropped after initial sync");
    replSet.stopSet();
})();