summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_advance_stable_ts_after_clone.js
blob: 197e54ef2f42d0abd37e45bdafef73129b24d266 (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
109
110
111
112
113
114
115
116
117
/**
 * Tests that when the recipient's stable timestamp is earlier than the startApplyingDonorOpTime,
 * the recipient advances its stable timestamp.
 *
 * @tags: [
 *   featureFlagShardMerge,
 *   incompatible_with_eft,
 *   incompatible_with_macos,
 *   incompatible_with_windows_tls,
 *   requires_fcv_52,
 *   requires_majority_read_concern,
 *   requires_persistence,
 * ]
 */

(function() {
"use strict";

load("jstests/libs/fail_point_util.js");
load("jstests/replsets/libs/tenant_migration_test.js");
load("jstests/libs/uuid_util.js");  // For extractUUIDFromObject().
load("jstests/replsets/rslib.js");

const kTenantIdPrefix = "testTenantId";
const kUnrelatedDbNameDonor = "unrelatedDBDonor";
const kUnrelatedDbNameRecipient = "unrelatedDBRecipient";
const collName = "foo";
const tenantId = kTenantIdPrefix + "_0";
const migrationId = UUID();
const migrationOpts = {
    migrationIdString: extractUUIDFromObject(migrationId),
    tenantId: tenantId,
};

const tmt = new TenantMigrationTest({name: jsTestName()});
tmt.insertDonorDB(tenantId + "_db", collName);

const donorPrimary = tmt.getDonorPrimary();
const recipientPrimary = tmt.getRecipientPrimary();

// Insert a doc on the recipient with {writeConcern: majority} to advance the stable timestamp. We
// will hold the stable timestamp on the recipient at this ts. This will ensure that when we run the
// tenant migration, the recipient's stable timestamp will be less than the the timestamp it
// receives from the donor to use as the startApplyingDonorOpTime.
let recipientHoldStableTs =
    assert
        .commandWorked(recipientPrimary.getDB(kUnrelatedDbNameRecipient).runCommand({
            insert: collName,
            documents: [{_id: 1}],
            writeConcern: {w: "majority"}
        }))
        .operationTime;

const recipientHoldStablefp = configureFailPoint(
    recipientPrimary, "holdStableTimestampAtSpecificTimestamp", {timestamp: recipientHoldStableTs});

// Advance the stable timestamp on the donor so that it's greater than the timestamp of the
// recipient. Then, force the checkpoint timestamp to advance by calling fsync.
let donorAdvancedStableTs;
assert.soon(function() {
    donorAdvancedStableTs =
        assert
            .commandWorked(donorPrimary.getDB(kUnrelatedDbNameDonor).runCommand({
                insert: collName,
                documents: [{_id: 1}],
                writeConcern: {w: "majority"}
            }))
            .operationTime;

    return bsonWoCompare(donorAdvancedStableTs, recipientHoldStableTs) >= 0;
});
assert.commandWorked(donorPrimary.adminCommand({fsync: 1}));

// Force the tenant migration to hang just before we attempt to advance the stable timestamp on the
// recipient.
const hangBeforeAdvanceStableTsFp =
    configureFailPoint(recipientPrimary, "fpBeforeAdvancingStableTimestamp", {action: "hang"});

// Start the migration.
assert.commandWorked(tmt.startMigration(migrationOpts));

// The recipient's stable timestamp should be less than the timestamp it receives from the donor to
// use as the startApplyingDonorOpTime, so the recipient should advance its stable timestamp. Wait
// until the recipient hits the failpoint just before it advances the stable timestamp. Then, turn
// off both failpoints so that we no longer hold back the stable timestamp on the recipient, and the
// migration continues.
hangBeforeAdvanceStableTsFp.wait();

recipientHoldStablefp.off();
hangBeforeAdvanceStableTsFp.off();

// Wait until we see the no-op oplog entry. We will check that we see a log line indicating the
// recipient advanced its stable timestamp to the timestamp of this write.
let stableTimestamp;
assert.soon(function() {
    let noopEntry = recipientPrimary.getDB("local").oplog.rs.findOne(
        {"o": {"msg": "Merge recipient advancing stable timestamp"}});
    if (noopEntry)
        stableTimestamp = noopEntry.ts;

    return noopEntry;
});

let majorityWriteTs =
    assert
        .commandWorked(recipientPrimary.getDB(kUnrelatedDbNameRecipient).runCommand({
            insert: collName,
            documents: [{_id: 2}],
            writeConcern: {w: "majority"}
        }))
        .operationTime;

assert(bsonWoCompare(majorityWriteTs, donorAdvancedStableTs) >= 0);

TenantMigrationTest.assertCommitted(tmt.waitForMigrationToComplete(migrationOpts));
tmt.stop();
})();