summaryrefslogtreecommitdiff
path: root/jstests/replsets/tenant_migration_on_clustered_collection.js
blob: b9d827de0fb39efb95d373ab9142da3d71a0a915 (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
/**
 * Tests that clustered collections (both empty and non-empty) are successfully migrated in a basic
 * tenant migration.
 *
 * @tags: [
 *   requires_fcv_53,
 *   assumes_against_mongod_not_mongos,
 *   assumes_unsharded_collection,
 *   # Basic tags for tenant migration tests.
 *   incompatible_with_macos,
 *   incompatible_with_windows_tls,
 *   requires_majority_read_concern,
 *   requires_persistence,
 *   serverless,
 * ]
 */

import {TenantMigrationTest} from "jstests/replsets/libs/tenant_migration_test.js";
import {
    runMigrationAsync,
} from "jstests/replsets/libs/tenant_migration_util.js";

load("jstests/libs/clustered_collections/clustered_collection_util.js");  // ClusteredCollectionUtil
load("jstests/libs/parallelTester.js");                                   // Thread()
load("jstests/libs/uuid_util.js");                                        // extractUUIDFromObject()
load('jstests/replsets/rslib.js');                                        // 'createRstArgs'

const tenantMigrationTest = new TenantMigrationTest({name: jsTestName()});

const kTenantId = ObjectId().str;
const kDbName = tenantMigrationTest.tenantDB(kTenantId, "testDB");
const kEmptyCollName = "testEmptyColl";
const kNonEmptyCollName = "testNonEmptyColl";

// The documents used to populate the non-empty collection.
const documents = [{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}, {_id: 3, a: 3, b: 3}];

const clusteredCreateOptions = {
    clusteredIndex: {key: {_id: 1}, name: "index_on_id", unique: true}
};

// Generates the clustered collections and populates the non-empty collection.
const createClusteredCollections = () => {
    const donorPrimary = tenantMigrationTest.getDonorPrimary();
    const donorDB = donorPrimary.getDB(kDbName);

    // Create a non-empty clustered collection and store it's original contents.
    assert.commandWorked(donorDB.createCollection(kNonEmptyCollName, clusteredCreateOptions));
    assert.commandWorked(donorDB[kNonEmptyCollName].insert(documents));

    // Create an empty clustered collection.
    assert.commandWorked(donorDB.createCollection(kEmptyCollName, clusteredCreateOptions));

    // Account for test environments that may change default write concern.
    tenantMigrationTest.getDonorRst().awaitReplication();
};

// Runs the entire tenant migration start to finish.
const runTenantMigration = () => {
    const migrationId = UUID();
    const migrationOpts = {
        migrationIdString: extractUUIDFromObject(migrationId),
        recipientConnString: tenantMigrationTest.getRecipientConnString(),
        tenantId: kTenantId,
    };
    const donorRstArgs = createRstArgs(tenantMigrationTest.getDonorRst());
    const migrationThread = new Thread(runMigrationAsync, migrationOpts, donorRstArgs);
    migrationThread.start();

    TenantMigrationTest.assertCommitted(migrationThread.returnData());
};

// Validates the clustered collections migrated to the recipient.
const validateMigrationResults = () => {
    const recipientPrimary = tenantMigrationTest.getRecipientPrimary();
    const recipientDB = recipientPrimary.getDB(kDbName);

    // Confirm the data was transferred correctly.
    const nonEmptyCollDocs = recipientDB[kNonEmptyCollName].find().toArray();
    assert.sameMembers(nonEmptyCollDocs, documents);
    assert.eq(0,
              recipientDB[kEmptyCollName].find().itcount(),
              tojson(recipientDB[kEmptyCollName].find().toArray()));

    ClusteredCollectionUtil.validateListCollections(
        recipientDB, kNonEmptyCollName, clusteredCreateOptions);
    ClusteredCollectionUtil.validateListCollections(
        recipientDB, kEmptyCollName, clusteredCreateOptions);

    ClusteredCollectionUtil.validateListIndexes(
        recipientDB, kNonEmptyCollName, clusteredCreateOptions);
    ClusteredCollectionUtil.validateListIndexes(
        recipientDB, kEmptyCollName, clusteredCreateOptions);
};

createClusteredCollections();
runTenantMigration();
validateMigrationResults();

tenantMigrationTest.stop();