summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/resharding/resharding_data_replication.h
blob: 4b2b9f279980f3ca496f024a989f517d0c8b65ec (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 *    Copyright (C) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include <functional>
#include <memory>
#include <vector>

#include "mongo/bson/timestamp.h"
#include "mongo/db/cancelable_operation_context.h"
#include "mongo/s/chunk_manager.h"
#include "mongo/s/resharding/common_types_gen.h"
#include "mongo/s/shard_id.h"
#include "mongo/util/cancellation.h"
#include "mongo/util/functional.h"
#include "mongo/util/future.h"

namespace mongo {

class OperationContext;
class ReshardingOplogApplier;
class ReshardingCollectionCloner;
class ReshardingMetrics;
class ReshardingOplogFetcher;
class ReshardingTxnCloner;
class ServiceContext;
class ThreadPool;

namespace executor {

class TaskExecutor;

}  // namespace executor

/**
 * Manages the full sequence of data replication in resharding on the recipient.
 *
 *   - Cloning the collection being resharded.
 *   - Cloning the config.transactions records from before the resharding operation started.
 *   - Fetching any oplog entries affecting the collection being resharded.
 *   - Applying the fetched oplog entries.
 */
class ReshardingDataReplicationInterface {
public:
    virtual ~ReshardingDataReplicationInterface() = default;

    /**
     * Begins the data replication procedure and runs it to completion.
     *
     *   - Immediately starts cloning the collection being resharded.
     *   - Immediately starts fetching any oplog entries affecting the collection being resharded.
     *   - After minimumOperationDuration milliseconds, starts cloning the config.transactions
     *     records from before the resharding operation started.
     *   - After cloning both the collection being resharded and the config.transactions records
     *     from before the resharding operation started, and after startOplogApplication() has been
     *     called, starts applying the fetched oplog entries.
     *
     * This function returns a future that becomes ready when either
     *   (a) the recipient has applied the final resharding oplog entry from every donor shard, or
     *   (b) the recipient has encountered an operation-fatal error.
     *
     * The caller must take care to wait on the returned future or a future returned by
     * awaitStrictlyConsistent() to guarantee that all of the data replication components have
     * stopped running in both the success and failure cases.
     */
    virtual SemiFuture<void> runUntilStrictlyConsistent(
        std::shared_ptr<executor::TaskExecutor> executor,
        std::shared_ptr<executor::TaskExecutor> cleanupExecutor,
        CancellationToken cancelToken,
        CancelableOperationContextFactory opCtxFactory,
        Milliseconds minimumOperationDuration) = 0;

    /**
     * Releases the barrier to allow the fetched oplog entries to be applied.
     *
     * This method exists on ReshardingDataReplicationInterface to allow the RecipientStateMachine
     * to transition from kCloning to kApplying before the fetched oplog entries start to be
     * applied.
     *
     * This function is safe to be called multiple times in sequence but must not be called
     * concurrently by another thread.
     */
    virtual void startOplogApplication() = 0;

    /**
     * Returns a future that becomes ready when either
     *   (a) the recipient has finished cloning both the collection being resharded and the
     *       config.transactions records from before the resharding operation started, or
     *   (b) the recipient has encountered an operation-fatal error.
     */
    virtual SharedSemiFuture<void> awaitCloningDone() = 0;

    /**
     * Returns a future that becomes ready when either
     *   (a) the recipient with respect to each donor shard has applied through the timestamp it has
     *       finished cloning at (the fetchTimestamp), or
     *   (b) the recipient has encountered an operation-fatal error.
     */
    virtual SharedSemiFuture<void> awaitConsistentButStale() = 0;

    /**
     * Returns a future that becomes ready when either
     *   (a) the recipient has applied the final resharding oplog entry from every donor shard, or
     *   (b) the recipient has encountered an operation-fatal error.
     */
    virtual SharedSemiFuture<void> awaitStrictlyConsistent() = 0;

    virtual void shutdown() = 0;
};

class ReshardingDataReplication : public ReshardingDataReplicationInterface {
private:
    struct TrustedInitTag {};

public:
    static std::unique_ptr<ReshardingDataReplicationInterface> make(
        OperationContext* opCtx,
        ReshardingMetrics* metrics,
        CommonReshardingMetadata metadata,
        std::vector<ShardId> donorShardIds,
        Timestamp fetchTimestamp,
        bool cloningDone,
        ShardId myShardId,
        ChunkManager sourceChunkMgr,
        std::shared_ptr<executor::TaskExecutor> executor);

    // The TrustedInitTag being a private class makes this constructor effectively private. However,
    // it needs to technically be a public constructor for std::make_unique to be able to call it.
    // This C++ technique is known as the passkey idiom. ReshardingDataReplication::make() is the
    // entry point for constructing instances of ReshardingDataReplication.
    ReshardingDataReplication(std::unique_ptr<ReshardingCollectionCloner> collectionCloner,
                              std::vector<std::unique_ptr<ReshardingTxnCloner>> txnCloners,
                              std::vector<std::unique_ptr<ReshardingOplogApplier>> oplogAppliers,
                              std::vector<std::unique_ptr<ThreadPool>> _oplogApplierWorkers,
                              std::vector<std::unique_ptr<ReshardingOplogFetcher>> oplogFetchers,
                              std::shared_ptr<executor::TaskExecutor> oplogFetcherExecutor,
                              TrustedInitTag);

    SemiFuture<void> runUntilStrictlyConsistent(
        std::shared_ptr<executor::TaskExecutor> executor,
        std::shared_ptr<executor::TaskExecutor> cleanupExecutor,
        CancellationToken cancelToken,
        CancelableOperationContextFactory opCtxFactory,
        Milliseconds minimumOperationDuration) override;

    void startOplogApplication() override;

    SharedSemiFuture<void> awaitCloningDone() override;

    SharedSemiFuture<void> awaitConsistentButStale() override;

    SharedSemiFuture<void> awaitStrictlyConsistent() override;

    void shutdown() override;

private:
    static std::unique_ptr<ReshardingCollectionCloner> _makeCollectionCloner(
        ReshardingMetrics* metrics,
        const CommonReshardingMetadata& metadata,
        const ShardId& myShardId,
        Timestamp fetchTimestamp);

    static std::vector<std::unique_ptr<ReshardingTxnCloner>> _makeTxnCloners(
        const CommonReshardingMetadata& metadata,
        const std::vector<ShardId>& donorShardIds,
        Timestamp fetchTimestamp);

    static std::vector<std::unique_ptr<ReshardingOplogFetcher>> _makeOplogFetchers(
        OperationContext* opCtx,
        ReshardingMetrics* metrics,
        const CommonReshardingMetadata& metadata,
        const std::vector<ShardId>& donorShardIds,
        Timestamp fetchTimestamp,
        const ShardId& myShardId);

    static std::shared_ptr<executor::TaskExecutor> _makeOplogFetcherExecutor(size_t numDonors);

    static std::vector<std::unique_ptr<ThreadPool>> _makeOplogApplierWorkers(size_t numDonors);

    static std::vector<std::unique_ptr<ReshardingOplogApplier>> _makeOplogAppliers(
        OperationContext* opCtx,
        ReshardingMetrics* metrics,
        CommonReshardingMetadata metadata,
        const std::vector<ShardId>& donorShardIds,
        Timestamp fetchTimestamp,
        ChunkManager sourceChunkMgr,
        std::shared_ptr<executor::TaskExecutor> executor,
        const std::vector<NamespaceString>& stashCollections,
        const std::vector<std::unique_ptr<ReshardingOplogFetcher>>& oplogFetchers,
        const std::vector<std::unique_ptr<ThreadPool>>& oplogApplierWorkers);

    SharedSemiFuture<void> _runCollectionCloner(
        std::shared_ptr<executor::TaskExecutor> executor,
        std::shared_ptr<executor::TaskExecutor> cleanupExecutor,
        CancellationToken cancelToken,
        CancelableOperationContextFactory opCtxFactory);

    std::vector<SharedSemiFuture<void>> _runTxnCloners(
        std::shared_ptr<executor::TaskExecutor> executor,
        std::shared_ptr<executor::TaskExecutor> cleanupExecutor,
        CancellationToken cancelToken,
        Milliseconds minimumOperationDuration);

    std::vector<SharedSemiFuture<void>> _runOplogFetchers(
        std::shared_ptr<executor::TaskExecutor> executor, CancellationToken cancelToken);

    std::vector<SharedSemiFuture<void>> _runOplogAppliersUntilConsistentButStale(
        std::shared_ptr<executor::TaskExecutor> executor, CancellationToken cancelToken);

    std::vector<SharedSemiFuture<void>> _runOplogAppliersUntilStrictlyConsistent(
        std::shared_ptr<executor::TaskExecutor> executor, CancellationToken cancelToken);

    // _collectionCloner is left as nullptr when make() is called with cloningDone=true.
    const std::unique_ptr<ReshardingCollectionCloner> _collectionCloner;

    // _txnCloners is left as an empty vector when make() is called with cloningDone=true.
    const std::vector<std::unique_ptr<ReshardingTxnCloner>> _txnCloners;

    const std::vector<std::unique_ptr<ReshardingOplogApplier>> _oplogAppliers;
    const std::vector<std::unique_ptr<ThreadPool>> _oplogApplierWorkers;

    // The ReshardingOplogFetcher must be destructed before the corresponding ReshardingOplogApplier
    // to ensure the future returned by awaitInsert() is always eventually readied.
    const std::vector<std::unique_ptr<ReshardingOplogFetcher>> _oplogFetchers;
    const std::shared_ptr<executor::TaskExecutor> _oplogFetcherExecutor;

    // Promise fulfilled by startOplogApplication() to signal that oplog application can begin.
    SharedPromise<void> _startOplogApplication;

    SharedPromise<void> _cloningDone;
    SharedPromise<void> _consistentButStale;
    SharedPromise<void> _strictlyConsistent;
};

using ReshardingDataReplicationFactory = unique_function<decltype(ReshardingDataReplication::make)>;

}  // namespace mongo