summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/metadata_manager.h
blob: 2311433aa4941027cf90a8f2af72d21553302f17 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 *    Copyright (C) 2016 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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 "mongo/base/disallow_copying.h"
#include "mongo/bson/simple_bsonobj_comparator.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/range_arithmetic.h"
#include "mongo/db/s/collection_metadata.h"
#include "mongo/db/s/collection_range_deleter.h"
#include "mongo/db/service_context.h"
#include "mongo/executor/task_executor.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/util/concurrency/notification.h"

#include "mongo/stdx/memory.h"

#include <list>

namespace mongo {

class ScopedCollectionMetadata;

class MetadataManager {
    MONGO_DISALLOW_COPYING(MetadataManager);

public:
    MetadataManager(ServiceContext*, NamespaceString nss, executor::TaskExecutor* rangeDeleter);
    ~MetadataManager();

    /**
     * An ActiveMetadata must be set before this function can be called.
     *
     * Increments the usage counter of the active metadata and returns an RAII object, which
     * contains the currently active metadata.  When the usageCounter goes to zero, the RAII
     * object going out of scope will call _removeMetadata.
     */
    ScopedCollectionMetadata getActiveMetadata();

    /**
     * Returns the number of CollectionMetadata objects being maintained on behalf of running
     * queries.  The actual number may vary after it returns, so this is really only useful for unit
     * tests.
     */
    size_t numberOfMetadataSnapshots();

    /**
     * Uses the contents of the specified metadata as a way to purge any pending chunks.
     */
    void refreshActiveMetadata(std::unique_ptr<CollectionMetadata> newMetadata);

    void toBSONPending(BSONArrayBuilder& bb) const;

    /**
     * Appends information on all the chunk ranges in rangesToClean to builder.
     */
    void append(BSONObjBuilder* builder);

    /**
     * Returns a map to the set of chunks being migrated in.
     */
    RangeMap const& getReceiveMap() const {
        return _receivingChunks;
    }

    /**
     * If no running queries can depend on documents in the range, schedules any such documents for
     * immediate cleanup. Otherwise, returns false.
     */
    bool beginReceive(ChunkRange const& range);

    /**
     * Removes the range from the pending list, and schedules any documents in the range for
     * immediate cleanup.  Assumes no active queries can see any local documents in the range.
     */
    void forgetReceive(const ChunkRange& range);

    /**
     * Initiates cleanup of the orphaned documents as if a chunk has been migrated out. If any
     * documents in the range might still be in use by running queries, queues cleanup to begin
     * after they have all terminated.  Otherwise, schedules documents for immediate cleanup.
     *
     * Must be called with the collection locked for writing.  To monitor completion, use
     * trackCleanup or CollectionShardingState::waitForClean.
     */
    void cleanUpRange(ChunkRange const& range);

    /**
     * Returns true if the specified key is in a range being received.
     */
    bool keyIsPending(const BSONObj& key) const;

    /**
     * Returns the number of ranges scheduled to be cleaned, exclusive of such ranges that might
     * still be in use by running queries.  Outside of test drivers, the actual number may vary
     * after it returns, so this is really only useful for unit tests.
     */
    size_t numberOfRangesToClean();

    /**
     * Returns the number of ranges scheduled to be cleaned once all queries that could depend on
     * them have terminated. The actual number may vary after it returns, so this is really only
     * useful for unit tests.
     */
    size_t numberOfRangesToCleanStillInUse();

    using CleanupNotification = CollectionRangeDeleter::DeleteNotification;
    /**
     * Reports whether the argument range is still scheduled for deletion. If not, returns nullptr.
     * Otherwise, returns a notification n such that n->get(opCtx) will wake when deletion of a
     * range (possibly the one of interest) is completed.
     */
    CleanupNotification trackCleanup(ChunkRange const& orphans);

    /**
     * Called for the range deletion executor, actually deletes a scheduled range of documents
     * immediately.  Returns true if it should be scheduled to be called again.
     */
    bool cleanUpNextRange(OperationContext* opCtx,
                          Collection* collection,
                          BSONObj const& keyPattern,
                          int maxToDelete);

private:
    struct CollectionMetadataTracker {
        /**
         * Creates a new CollectionMetadataTracker with the usageCounter initialized to zero.
         */
        CollectionMetadataTracker(std::unique_ptr<CollectionMetadata> m);

    private:
        std::unique_ptr<CollectionMetadata> metadata;
        uint32_t usageCounter{0};
        boost::optional<ChunkRange> orphans{boost::none};

        friend class ScopedCollectionMetadata;  // for access to usageCounter:
        friend class MetadataManager;
    };

    /**
     * Atomically decrements scoped.tracker->usageCount under our own mutex. At zero, retires any
     * metadata that has fallen out of use, and pushes any orphan ranges found in them to the list
     * of ranges actively being cleaned up.  Calling with a null scoped.tracker is a no-op.
     */
    void _decrementTrackerUsage(ScopedCollectionMetadata const& scoped);

    /**
     * Pushes current set of chunks, if any, to _metadataInUse, replaces it with newMetadata.
     */
    void _setActiveMetadata_inlock(std::unique_ptr<CollectionMetadata> newMetadata);

    /**
     * Returns true if the specified range overlaps any chunk that might be currently in use by a
     * running query.
     *
     * must be called locked.
     */

    bool _overlapsInUseChunk(ChunkRange const& range);

    /**
     * Returns true if any range (possibly) still in use, but scheduled for cleanup, overlaps
     * the argument range.
     *
     * Must be called locked.
     */
    bool _overlapsInUseCleanups(ChunkRange const& range);

    /**
     * Deletes ranges, in background, until done, normally using a task executor attached to the
     * ShardingState.
     *
     * Each time it completes cleaning up a range, it wakes up clients waiting on completion of
     * that range, which may then verify their range has no more deletions scheduled, and proceed.
     */
    static void _scheduleCleanup(executor::TaskExecutor*, NamespaceString nss);

    /**
     * Adds the range to the list of ranges scheduled for immediate deletion, and schedules a
     * a background task to perform the work.
     *
     * Must be called locked.
     */
    void _pushRangeToClean(ChunkRange const& range);

    /**
     * Adds a range from the receiving map, so getNextOrphanRange will skip ranges migrating in.
     */
    void _addToReceiving(ChunkRange const& range);

    /**
     * Removes a range from the receiving map after a migration failure. range.minKey() must
     * exactly match an element of _receivingChunks.
     */
    void _removeFromReceiving(ChunkRange const& range);

    /**
     * Wakes up any clients waiting on a range to leave _metadataInUse
     *
     * Must be called locked.
     */
    void _notifyInUse();

    // data members

    const NamespaceString _nss;

    // ServiceContext from which to obtain instances of global support objects.
    ServiceContext* _serviceContext;

    // Mutex to protect the state below
    stdx::mutex _managerLock;

    // The currently active collection metadata
    std::unique_ptr<CollectionMetadataTracker> _activeMetadataTracker;

    // Previously active collection metadata instances still in use by active server operations or
    // cursors
    std::list<std::unique_ptr<CollectionMetadataTracker>> _metadataInUse;

    // Chunk ranges which are currently assumed to be transferred to the shard. Indexed by the min
    // key of the range.
    RangeMap _receivingChunks;

    // Clients can sleep on copies of _notification while waiting for their orphan ranges to fall
    // out of use.
    std::shared_ptr<Notification<Status>> _notification;

    // The background task that deletes documents from orphaned chunk ranges.
    executor::TaskExecutor* _executor;

    // Ranges being deleted, or scheduled to be deleted, by a background task
    CollectionRangeDeleter _rangesToClean;

    friend class ScopedCollectionMetadata;  // Its op=() and dtor call _decrementTrackerUsage().
};

class ScopedCollectionMetadata {
    MONGO_DISALLOW_COPYING(ScopedCollectionMetadata);

public:
    /**
     * Creates an empty ScopedCollectionMetadata. Using the default constructor means that no
     * metadata is available.
     */
    ScopedCollectionMetadata();

    // Must be called with owning MetadataManager unlocked
    ~ScopedCollectionMetadata();

    // must be called with owning MetadataManager unlocked
    ScopedCollectionMetadata(ScopedCollectionMetadata&& other);
    ScopedCollectionMetadata& operator=(ScopedCollectionMetadata&& other);

    /**
     * Dereferencing the ScopedCollectionMetadata dereferences the internal CollectionMetadata.
     */
    CollectionMetadata* operator->() const;
    CollectionMetadata* getMetadata() const;

    /**
     * True if the ScopedCollectionMetadata stores a metadata (is not empty)
     */
    operator bool() const;

private:
    /**
     * Increments the refcount in the specified tracker.
     *
     * Must be called with specified *manager locked.
     */
    ScopedCollectionMetadata(MetadataManager* manager,
                             MetadataManager::CollectionMetadataTracker* tracker);

    MetadataManager* _manager{nullptr};
    MetadataManager::CollectionMetadataTracker* _tracker{nullptr};

    friend ScopedCollectionMetadata MetadataManager::getActiveMetadata();  // uses our private ctor
    friend void MetadataManager::_decrementTrackerUsage(ScopedCollectionMetadata const&);
};

}  // namespace mongo