summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/move_primary_with_crud.js
blob: 7fc503ac2fa5534a2e6644cca21d259cc2944348 (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
'use strict';

/**
 * Randomly performs a series of CRUD and movePrimary operations on unsharded collections, checking
 * for data consistency as a consequence of these operations.
 *
 * @tags: [
 *   requires_sharding,
 *   requires_fcv_70
 *  ]
 */

const $config = (function() {
    const kCollNamePrefix = 'unsharded_coll_';
    const kInitialCollSize = 100;
    const kBatchSizeForDocsLookup = kInitialCollSize * 2;

    /**
     * Utility function that asserts that the specified command is executed successfully, i.e. that
     * no errors occur, or that any error is in `ignorableErrorCodes`. However, if the error is in
     * `retryableErrorCodes`, then the command is retried.
     */
    const assertCommandWorked = function(cmd, retryableErrorCodes, ignorableErrorCodes = []) {
        if (!Array.isArray(retryableErrorCodes)) {
            retryableErrorCodes = [retryableErrorCodes];
        }
        if (!Array.isArray(ignorableErrorCodes)) {
            ignorableErrorCodes = [ignorableErrorCodes];
        }

        let res = undefined;
        assertAlways.soon(() => {
            try {
                res = cmd();
                return true;
            } catch (err) {
                if (err instanceof BulkWriteError && err.hasWriteErrors()) {
                    for (let writeErr of err.getWriteErrors()) {
                        if (retryableErrorCodes.includes(writeErr.code)) {
                            return false;
                        } else if (ignorableErrorCodes.includes(writeErr.code)) {
                            continue;
                        } else {
                            throw err;
                        }
                    }
                    return true;
                } else if (retryableErrorCodes.includes(err.code)) {
                    return false;
                } else if (ignorableErrorCodes.includes(err.code)) {
                    return true;
                }
                throw err;
            }
        });
        return res;
    };

    const data = {
        // In-memory copy of the collection data. Every CRUD operation on the persisted collection
        // is reflected on this object. The collection consistency check is performed by comparing
        // its data with those managed by this copy.
        collMirror: {},

        // ID of the last document inserted into the collection. It's used as a generator of unique
        // IDs for new documents to insert.
        lastId: undefined,

        getRandomDoc: function() {
            const keys = Object.keys(this.collMirror);
            return this.collMirror[keys[Random.randInt(keys.length)]];
        }
    };

    const states = {
        init: function(db, collName, connCache) {
            // Insert an initial amount of documents into the collection, with a progressive _id and
            // the update counter set to zero.

            this.collName = `${kCollNamePrefix}${this.tid}`;
            let coll = db[this.collName];
            jsTestLog(`Initializing data: coll=${coll}`);

            for (let i = 0; i < kInitialCollSize; ++i) {
                this.collMirror[i] = {_id: i, updateCount: 0};
            }
            this.lastId = kInitialCollSize - 1;

            // Session with retryable writes is required to recover from a primary node step-down
            // event during bulk insert processing.
            this.session = db.getMongo().startSession({retryWrites: true});
            let sessionColl = this.session.getDatabase(db.getName()).getCollection(this.collName);

            assertCommandWorked(
                () => {
                    let bulkOp = sessionColl.initializeUnorderedBulkOp();
                    for (let i = 0; i < kInitialCollSize; ++i) {
                        bulkOp.insert(
                            {_id: i, updateCount: 0},
                        );
                    }
                    bulkOp.execute();
                },
                ErrorCodes.MovePrimaryInProgress,
                // TODO (SERVER-32113): Retryable writes may cause double inserts if performed on a
                // shard involved as the originator of a movePrimary operation.
                ErrorCodes.DuplicateKey);
        },
        insert: function(db, collName, connCache) {
            // Insert a document into the collection, with an _id greater than all those already
            // present (last + 1) and the update counter set to zero.

            let coll = db[this.collName];

            const newId = this.lastId += 1;
            jsTestLog(`Inserting document: coll=${coll} _id=${newId}`);

            this.collMirror[newId] = {_id: newId, updateCount: 0};

            assertCommandWorked(() => {
                coll.insertOne({_id: newId, updateCount: 0});
            }, ErrorCodes.MovePrimaryInProgress);
        },
        update: function(db, collName, connCache) {
            // Increment the update counter of a random document of the collection.

            let coll = db[this.collName];

            const randomId = this.getRandomDoc()._id;
            jsTestLog(`Updating document: coll=${coll} _id=${randomId}`);

            const newUpdateCount = this.collMirror[randomId].updateCount += 1;

            assertCommandWorked(() => {
                coll.updateOne({_id: randomId}, {$set: {updateCount: newUpdateCount}});
            }, ErrorCodes.MovePrimaryInProgress);
        },
        delete: function(db, collName, connCache) {
            // Remove a random document from the collection.

            let coll = db[this.collName];

            const randomId = this.getRandomDoc()._id;
            jsTestLog(`Deleting document: coll=${coll} _id=${randomId}`);

            delete this.collMirror[randomId];

            assertCommandWorked(() => {
                coll.deleteOne({_id: randomId});
            }, ErrorCodes.MovePrimaryInProgress);
        },
        movePrimary: function(db, collName, connCache) {
            // Move the primary shard of the database to a random shard (which could coincide with
            // the starting one).

            const shards = Object.keys(connCache.shards);
            const toShard = shards[Random.randInt(shards.length)];
            jsTestLog(`Running movePrimary: db=${db} to=${toShard}`);

            assertAlways.commandWorkedOrFailedWithCode(
                db.adminCommand({movePrimary: db.getName(), to: toShard}), [
                    // Caused by a concurrent movePrimary operation on the same database but a
                    // different destination shard.
                    ErrorCodes.ConflictingOperationInProgress,
                    // Due to a stepdown of the donor during the cloning phase, the movePrimary
                    // operation failed. It is not automatically recovered, but any orphaned data on
                    // the recipient has been deleted.
                    7120202,
                    // Due to a stepdown of the recipient during the cloning phase, the
                    // _shardsvrCloneCatalogData command is retried by the donor, finding orphaned
                    // documents. The movePrimary operation fails and is not automatically
                    // recovered, but orphaned data on the recipient has been deleted.
                    ErrorCodes.NamespaceExists
                ]);
        },
        verifyDocuments: function(db, collName, connCache) {
            // Verify the correctness of the collection data by checking that each document matches
            // its copy in memory.

            const coll = db[this.collName];
            jsTestLog(`Verifying data: coll=${coll}`);

            let docs = assertCommandWorked(
                () => {
                    return coll.find().batchSize(kBatchSizeForDocsLookup).toArray();
                },
                // Caused by a concurrent movePrimary operation.
                ErrorCodes.QueryPlanKilled);

            assertAlways.eq(Object.keys(this.collMirror).length,
                            docs.length,
                            `expectedData=${JSON.stringify(this.collMirror)}} actualData=${
                                JSON.stringify(docs)}`);

            docs.forEach(doc => {
                assertAlways.eq(this.collMirror[doc._id],
                                doc,
                                `expectedData=${JSON.stringify(this.collMirror)}} actualData=${
                                    JSON.stringify(docs)}`);
            });
        }
    };

    const standardTransition =
        {insert: 0.22, update: 0.22, delete: 0.22, movePrimary: 0.12, verifyDocuments: 0.22};

    const transitions = {
        init: standardTransition,
        insert: standardTransition,
        update: standardTransition,
        delete: standardTransition,
        movePrimary: standardTransition,
        verifyDocuments: standardTransition
    };

    return {
        threadCount: 8,
        iterations: 32,
        states: states,
        transitions: transitions,
        data: data,
        passConnectionCache: true
    };
})();