summaryrefslogtreecommitdiff
path: root/jstests/replsets/initial_sync_reset_oldest_timestamp_after_failed_attempt.js
blob: 96a1663205a12d001e4defcf455a54df19eb634f (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
/**
 * Tests that initial sync resets the oldest timestamp after a failed attempt. The test will turn on
 * a failpoint that causes initial sync to fail partway through its first attempt and makes sure it
 * does not hit a WiredTiger assertion on the second attempt.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */

(function() {
"use strict";

load("jstests/core/txns/libs/prepare_helpers.js");

// Set the number of initial sync attempts to 2 so that the test fails on unplanned failures.
const replTest =
    new ReplSetTest({nodes: 2, nodeOptions: {setParameter: "numInitialSyncAttempts=2"}});
replTest.startSet();

// Increase the election timeout to 24 hours so that we do not accidentally trigger an election
// while the secondary is restarting.
replTest.initiateWithHighElectionTimeout();

const primary = replTest.getPrimary();
let secondary = replTest.getSecondary();

const dbName = "test";
const collName = "initial_sync_reset_oldest_timestamp_after_failed_attempt";
const testDB = primary.getDB(dbName);
const testColl = testDB.getCollection(collName);

assert.commandWorked(testColl.insert({_id: 1}));

const session = primary.startSession();
const sessionDB = session.getDatabase(dbName);
const sessionColl = sessionDB.getCollection(collName);
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: 2}));

// This will be the begin fetching point for both initial sync attempts. After the first initial
// sync attempt fails, if the oldest timestamp isn't reset before the next attempt, the update
// to the transaction table for this prepare will fail a WiredTiger assertion that the commit
// timestamp for a storage transaction cannot be older than the oldest timestamp.
const prepareTimestamp = PrepareHelpers.prepareTransaction(session);

jsTestLog("Prepared a transaction at timestamp: " + prepareTimestamp);

replTest.stop(secondary, undefined, {skipValidation: true});
secondary = replTest.start(
    secondary,
    {
        startClean: true,
        setParameter: {
            // Set the number of operations per batch to be 1 so that we can know exactly how
            // many batches there will be.
            "replBatchLimitOperations": 1,
            "failpoint.initialSyncHangAfterDataCloning": tojson({mode: "alwaysOn"}),
            // Allow the syncing node to write the prepare oplog entry and apply the first update
            // before failing initial sync.
            "failpoint.failInitialSyncBeforeApplyingBatch": tojson({mode: {skip: 2}}),
        }
    },
    true /* wait */);

// Wait for failpoint to be reached so we know that collection cloning is paused.
assert.commandWorked(
    secondary.adminCommand({waitForFailPoint: "initialSyncHangAfterDataCloning", timesEntered: 1}));

jsTestLog("Running operations while collection cloning is paused");

// This command will be in the last batch applied before the first initial sync attempt fails.
// If the oldest timestamp isn't reset on the next attempt, then the timestamp for this update
// will be the oldest timestamp.
assert.commandWorked(testColl.update({_id: 1}, {_id: 1, a: 1}));

// This entry will be applied in its own batch, so the failInitialSyncBeforeApplyingBatch
// failpoint will cause the first initial sync attempt to fail before applying this.
assert.commandWorked(testColl.update({_id: 1}, {_id: 1, b: 2}));

jsTestLog("Resuming initial sync");

assert.commandWorked(
    secondary.adminCommand({configureFailPoint: "initialSyncHangAfterDataCloning", mode: "off"}));

// Wait for this failpoint to be hit before turning it off and causing initial sync to fail.
assert.commandWorked(secondary.adminCommand(
    {waitForFailPoint: "failInitialSyncBeforeApplyingBatch", timesEntered: 1}));

jsTestLog("Failing first initial sync attempt");

// Turn the failpoint off and cause initial sync to fail.
assert.commandWorked(secondary.adminCommand(
    {configureFailPoint: "failInitialSyncBeforeApplyingBatch", mode: "off"}));

replTest.waitForState(secondary, ReplSetTest.State.SECONDARY);

jsTestLog("Initial sync completed");

assert.commandWorked(session.abortTransaction_forTesting());

replTest.stopSet();
})();