summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/collection_sharding_state.h
blob: 5e0e2a9c8db3a1a5747f4bf94960268264223025 (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

/**
 *    Copyright (C) 2018-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 "mongo/base/disallow_copying.h"
#include "mongo/db/logical_time.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/s/collection_sharding_runtime_lock.h"
#include "mongo/db/s/scoped_collection_metadata.h"
#include "mongo/db/s/sharding_migration_critical_section.h"

namespace mongo {

/**
 * Each collection on a mongod instance is dynamically assigned two pieces of information for the
 * duration of its lifetime:
 *  CollectionShardingState - this is a passive data-only state, which represents what is the
 * shard's knowledge of its the shard version and the set of chunks that it owns.
 *  CollectionShardingRuntime (missing from the embedded mongod) - this is the heavyweight machinery
 * which implements the sharding protocol functions and is what controls the data-only state.
 *
 * The CollectionShardingStateFactory class below is used in order to allow for the collection
 * runtime to be instantiated separately from the sharding state.
 *
 * Synchronization rule: In order to obtain an instance of this object, the caller must have some
 * lock on the respective collection.
 */
class CollectionShardingState {
    MONGO_DISALLOW_COPYING(CollectionShardingState);

public:
    virtual ~CollectionShardingState() = default;

    /**
     * Obtains the sharding state for the specified collection. If it does not exist, it will be
     * created and will remain in memory until the collection is dropped.
     *
     * Must be called with some lock held on the specific collection being looked up and the
     * returned pointer must not be stored.
     */
    static CollectionShardingState* get(OperationContext* opCtx, const NamespaceString& nss);

    /**
     * Reports all collections which have filtering information associated.
     */
    static void report(OperationContext* opCtx, BSONObjBuilder* builder);

    /**
     * Returns the chunk filtering metadata that the current operation should be using for that
     * collection or otherwise throws if it has not been loaded yet. If the operation does not
     * require a specific shard version, returns an UNSHARDED metadata. The returned object is safe
     * to access outside of collection lock.
     *
     * If the operation context contains an 'atClusterTime' property, the returned filtering
     * metadata will be tied to a specific point in time. Otherwise it will reference the latest
     * time available.
     */
    ScopedCollectionMetadata getMetadataForOperation(OperationContext* opCtx);
    ScopedCollectionMetadata getCurrentMetadata();

    /**
     * Returns boost::none if the filtering metadata for the collection is not known yet. Otherwise
     * returns the most recently refreshed from the config server metadata or shard version.
     *
     * These methods do not check for the shard version that the operation requires and should only
     * be used for cases such as checking whether a particular config server update has taken
     * effect.
     */
    boost::optional<ScopedCollectionMetadata> getCurrentMetadataIfKnown();
    boost::optional<ChunkVersion> getCurrentShardVersionIfKnown();

    /**
     * Checks whether the shard version in the operation context is compatible with the shard
     * version of the collection and if not, throws StaleConfigException populated with the received
     * and wanted versions.
     */
    void checkShardVersionOrThrow(OperationContext* opCtx);

    /**
     * Methods to control the collection's critical section. Methods listed below must be called
     * with both the collection lock and CollectionShardingRuntimeLock held in exclusive mode.
     *
     * In these methods, the CollectionShardingRuntimeLock ensures concurrent access to the
     * critical section.
     */
    void enterCriticalSectionCatchUpPhase(OperationContext* opCtx, CollectionShardingRuntimeLock&);
    void enterCriticalSectionCommitPhase(OperationContext* opCtx, CollectionShardingRuntimeLock&);


    /**
     * Method to control the collection's critical secion. Method listed below must be called with
     * the collection lock in IX mode and the CollectionShardingRuntimeLock in exclusive mode.
     *
     * In this method, the CollectionShardingRuntimeLock ensures concurrent access to the
     * critical section.
     */
    void exitCriticalSection(OperationContext* opCtx, CollectionShardingRuntimeLock&);

    /**
     * If the collection is currently in a critical section, returns the critical section signal to
     * be waited on. Otherwise, returns nullptr.
     */
    auto getCriticalSectionSignal(ShardingMigrationCriticalSection::Operation op) const {
        return _critSec.getSignal(op);
    }

protected:
    CollectionShardingState(NamespaceString nss);

private:
    friend CollectionShardingRuntimeLock;

    // Object-wide ResourceMutex to protect changes to the CollectionShardingRuntime or objects
    // held within. Use only the CollectionShardingRuntimeLock to lock this mutex.
    Lock::ResourceMutex _stateChangeMutex;

    // Namespace this state belongs to.
    const NamespaceString _nss;

    // Tracks the migration critical section state for this collection.
    ShardingMigrationCriticalSection _critSec;

    /**
     * Obtains the current metadata for the collection or boost::none if the metadata is not yet
     * known
     */
    virtual boost::optional<ScopedCollectionMetadata> _getMetadata(
        const boost::optional<mongo::LogicalTime>& atClusterTime) = 0;
};

/**
 * Singleton factory to instantiate CollectionShardingState objects specific to the type of instance
 * which is running.
 */
class CollectionShardingStateFactory {
    MONGO_DISALLOW_COPYING(CollectionShardingStateFactory);

public:
    static void set(ServiceContext* service,
                    std::unique_ptr<CollectionShardingStateFactory> factory);
    static void clear(ServiceContext* service);

    virtual ~CollectionShardingStateFactory() = default;

    /**
     * Called by the CollectionShardingState::get method once per newly cached namespace. It is
     * invoked under a mutex and must not acquire any locks or do blocking work.
     *
     * Implementations must be thread-safe when called from multiple threads.
     */
    virtual std::unique_ptr<CollectionShardingState> make(const NamespaceString& nss) = 0;

protected:
    CollectionShardingStateFactory(ServiceContext* serviceContext)
        : _serviceContext(serviceContext) {}

    // The service context which owns this factory
    ServiceContext* const _serviceContext;
};

}  // namespace mongo