summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_recipient_ttl.js
blob: e1558c85af991d44d7e0a94a43a7a143f2c024ca (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
/**
 * Tests to check whether the TTL index is being created and is functioning correctly on the tenant
 * migration recipient.
 *
 * @tags: [
 *   incompatible_with_eft,
 *   incompatible_with_macos,
 *   incompatible_with_windows_tls,
 *   requires_persistence,
 * ]
 */

(function() {

"use strict";
load("jstests/libs/uuid_util.js");  // For extractUUIDFromObject().
load("jstests/replsets/libs/tenant_migration_test.js");
load("jstests/replsets/libs/tenant_migration_util.js");

const kGarbageCollectionParams = {
    // Set the delay to 30s so that we can see the document vanish.
    tenantMigrationGarbageCollectionDelayMS: 30 * 1000,

    // Set the TTL monitor to run at a smaller interval to speed up the test.
    ttlMonitorSleepSecs: 1
};

const tenantMigrationTest = new TenantMigrationTest(
    {name: jsTestName(), sharedOptions: {setParameter: kGarbageCollectionParams}});

if (!tenantMigrationTest.isFeatureFlagEnabled()) {
    jsTestLog("Skipping test because the tenant migrations feature flag is disabled");
    tenantMigrationTest.stop();
    return;
}

const kRecipientTTLIndexName = "TenantMigrationRecipientTTLIndex";

const kMigrationId = UUID();
const kTenantId = 'testTenantId';
const migrationOpts = {
    migrationIdString: extractUUIDFromObject(kMigrationId),
    tenantId: kTenantId,
    readPreference: {mode: "primary"}
};

const recipientPrimary = tenantMigrationTest.getRecipientPrimary();
const configDB = recipientPrimary.getDB("config");
const tenantMigrationRecipientStateColl = configDB["tenantMigrationRecipients"];

jsTestLog("Ensure the TTL index was created.");
const indexes = tenantMigrationRecipientStateColl.getIndexes();
let i = 0;
for (; i < indexes.length; i++) {
    if (indexes[i].name == kRecipientTTLIndexName) {
        assert.eq(indexes[i].key.expireAt, 1, tojson(indexes));
        assert.eq(indexes[i].expireAfterSeconds, 0, tojson(indexes));
        break;
    }
}
// A TTL index must be found on the primary.
assert.neq(i, indexes.length, tojson(indexes));

jsTestLog("Starting and completing a tenant migration with migrationId: " + kMigrationId +
          ", tenantId: " + kTenantId);
assert.commandWorked(tenantMigrationTest.startMigration(migrationOpts));
TenantMigrationTest.assertCommitted(tenantMigrationTest.waitForMigrationToComplete(migrationOpts));

// The migration's document will not be marked as garbage collectable until forgetMigration. The
// document should exist in the collection now, without an expireAt field.
jsTestLog("Making sure migration state document exists.");
let stateDocQuery = tenantMigrationRecipientStateColl.find({_id: kMigrationId}).toArray();
assert.eq(stateDocQuery.length, 1, tojson(stateDocQuery));
assert(!stateDocQuery[0].hasOwnProperty("expireAt"), tojson(stateDocQuery));

jsTestLog("Forgetting the migration.");
assert.commandWorked(tenantMigrationTest.forgetMigration(migrationOpts.migrationIdString));

// The state document should now have the expireAt field.
jsTestLog("Expect to find expireAt field.");
stateDocQuery = tenantMigrationRecipientStateColl.find({_id: kMigrationId}).toArray();
assert.eq(stateDocQuery.length, 1, tojson(stateDocQuery));
assert(stateDocQuery[0].hasOwnProperty("expireAt"), tojson(stateDocQuery));

// Sleep past the garbage collection delay time, and then make sure the state document for our
// migration does not exist.
jsTestLog("Sleeping and then expecting the state document to have been deleted.");
sleep(30000);  // The garbage collection delay is 30s.
tenantMigrationTest.waitForMigrationGarbageCollection(kMigrationId, kTenantId);

tenantMigrationTest.stop();
})();