summaryrefslogtreecommitdiff
path: root/jstests/replsets/libs/tenant_migration_util.js
blob: 452088f1c9fea402ca6ffc765d508852e4f1bd97 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * Utilities for testing tenant migrations.
 */
var TenantMigrationUtil = (function() {
    // An object that mirrors the access states for the TenantMigrationAccessBlocker.
    const accessState = {kAllow: 0, kBlockingWrites: 1, kBlockingReadsAndWrites: 2, kReject: 3};

    /**
     * Runs the donorStartMigration command with the given migration options every 'intervalMS'
     * until the migration commits or aborts, or until the command fails. Returns the last command
     * response.
     */
    function startMigration(donorPrimaryHost, migrationOpts, intervalMS = 100) {
        const cmdObj = {
            donorStartMigration: 1,
            migrationId: UUID(migrationOpts.migrationIdString),
            recipientConnectionString: migrationOpts.recipientConnString,
            tenantId: migrationOpts.tenantId,
            readPreference: migrationOpts.readPreference
        };
        const donorPrimary = new Mongo(donorPrimaryHost);

        while (true) {
            const res = donorPrimary.adminCommand(cmdObj);
            if (!res.ok || res.state == "committed" || res.state == "aborted") {
                return res;
            }
            sleep(intervalMS);
        }
    }

    /**
     * Runs the donorForgetMigration command with the given migrationId and returns the response.
     */
    function forgetMigration(donorPrimaryHost, migrationIdString) {
        const donorPrimary = new Mongo(donorPrimaryHost);
        return donorPrimary.adminCommand(
            {donorForgetMigration: 1, migrationId: UUID(migrationIdString)});
    }

    /**
     * Runs the donorStartMigration command with the given migration options every 'intervalMS'
     * until the migration commits or aborts, or until the command fails with error other than
     * NotPrimary or network errors. Returns the last command response.
     */
    function startMigrationRetryOnRetryableErrors(donorRstArgs, migrationOpts, intervalMS = 100) {
        const cmdObj = {
            donorStartMigration: 1,
            migrationId: UUID(migrationOpts.migrationIdString),
            recipientConnectionString: migrationOpts.recipientConnString,
            tenantId: migrationOpts.tenantId,
            readPreference: migrationOpts.readPreference
        };

        const donorRst = new ReplSetTest({rstArgs: donorRstArgs});
        let donorPrimary = donorRst.getPrimary();

        while (true) {
            let res;
            try {
                res = donorPrimary.adminCommand(cmdObj);
            } catch (e) {
                if (isNetworkError(e)) {
                    jsTest.log(`Ignoring network error ${tojson(e)} for command ${tojson(cmdObj)}`);
                    continue;
                }
                throw e;
            }

            if (!res.ok) {
                if (!ErrorCodes.isNotPrimaryError(res.code)) {
                    return res;
                }
                donorPrimary = donorRst.getPrimary();
            } else if (res.state == "committed" || res.state == "aborted") {
                return res;
            }
            sleep(intervalMS);
        }
    }

    /**
     * Runs the donorForgetMigration command with the given migrationId until the command succeeds
     * or fails with error other than NotPrimary or network errors. Returns the last command
     * response.
     */
    function forgetMigrationRetryOnRetryableErrors(donorRstArgs, migrationIdString) {
        const cmdObj = {donorForgetMigration: 1, migrationId: UUID(migrationIdString)};

        const donorRst = new ReplSetTest({rstArgs: donorRstArgs});
        let donorPrimary = donorRst.getPrimary();

        while (true) {
            let res;
            try {
                res = donorPrimary.adminCommand(cmdObj);
            } catch (e) {
                if (isNetworkError(e)) {
                    jsTest.log(`Ignoring network error ${tojson(e)} for command ${tojson(cmdObj)}`);
                    continue;
                }
                throw e;
            }

            if (res.ok || !ErrorCodes.isNotPrimaryError(res.code)) {
                return res;
            }
            donorPrimary = donorRst.getPrimary();
        }
    }

    /**
     * Returns true if the durable and in-memory state for the migration 'migrationId' and
     * 'tenantId' is in state "committed", and false otherwise.
     */
    function isMigrationCommitted(node, migrationId, tenantId) {
        const configDonorsColl = node.getCollection("config.tenantMigrationDonors");
        if (configDonorsColl.findOne({_id: migrationId}).state != "committed") {
            return false;
        }
        const mtabs = node.adminCommand({serverStatus: 1}).tenantMigrationAccessBlocker;
        return mtabs[tenantId].access === TenantMigrationUtil.accessState.kReject;
    }

    /**
     * Returns true if the durable and in-memory state for the migration 'migrationId' and
     * 'tenantId' is in state "aborted", and false otherwise.
     */
    function isMigrationAborted(node, migrationId, tenantId) {
        const configDonorsColl = node.getCollection("config.tenantMigrationDonors");
        if (configDonorsColl.findOne({_id: migrationId}).state != "aborted") {
            return false;
        }
        const mtabs = node.adminCommand({serverStatus: 1}).tenantMigrationAccessBlocker;
        return mtabs[tenantId].access === TenantMigrationUtil.accessState.kAllow;
    }

    /**
     * Asserts that the migration 'migrationId' and 'tenantId' is in state "committed" on all the
     * given nodes.
     */
    function assertMigrationCommitted(nodes, migrationId, tenantId) {
        nodes.forEach(node => {
            assert(isMigrationCommitted(node, migrationId, tenantId));
        });
    }

    /**
     * Asserts that the migration 'migrationId' and 'tenantId' eventually goes to state "committed"
     * on all the given nodes.
     */
    function waitForMigrationToCommit(nodes, migrationId, tenantId) {
        nodes.forEach(node => {
            assert.soon(() => isMigrationCommitted(node, migrationId, tenantId));
        });
    }

    /**
     * Asserts that the migration 'migrationId' and 'tenantId' eventually goes to state "aborted"
     * on all the given nodes.
     */
    function waitForMigrationToAbort(nodes, migrationId, tenantId) {
        nodes.forEach(node => {
            assert.soon(() => isMigrationAborted(node, migrationId, tenantId));
        });
    }

    /**
     * Asserts that durable and in-memory state for the migration 'migrationId' and 'tenantId' is
     * eventually deleted from the given nodes.
     */
    function waitForMigrationGarbageCollection(nodes, migrationId, tenantId) {
        nodes.forEach(node => {
            const configDonorsColl = node.getCollection("config.tenantMigrationDonors");
            assert.soon(() => 0 === configDonorsColl.count({_id: migrationId}));

            assert.soon(() => 0 ===
                            node.adminCommand({serverStatus: 1})
                                .repl.primaryOnlyServices.TenantMigrationDonorService);

            let mtabs;
            assert.soon(() => {
                mtabs = node.adminCommand({serverStatus: 1}).tenantMigrationAccessBlocker;
                return !mtabs || !mtabs[tenantId];
            }, tojson(mtabs));
        });
    }

    /**
     * Returns the TenantMigrationAccessBlocker associated with given the tenantId on the
     * node.
     */
    function getTenantMigrationAccessBlocker(node, tenantId) {
        return node.adminCommand({serverStatus: 1}).tenantMigrationAccessBlocker[tenantId];
    }

    /**
     * Crafts a tenant database name, given the tenantId.
     */
    function tenantDB(tenantId, dbName) {
        return `${tenantId}_${dbName}`;
    }

    /**
     * Crafts a database name that does not belong to the tenant, given the tenantId.
     */
    function nonTenantDB(tenantId, dbName) {
        return `non_${tenantId}_${dbName}`;
    }

    return {
        accessState,
        startMigration,
        forgetMigration,
        startMigrationRetryOnRetryableErrors,
        forgetMigrationRetryOnRetryableErrors,
        assertMigrationCommitted,
        waitForMigrationToCommit,
        waitForMigrationToAbort,
        waitForMigrationGarbageCollection,
        getTenantMigrationAccessBlocker,
        tenantDB,
        nonTenantDB,
    };
})();