summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/set_shard_version_command.cpp
blob: 1fe15090e9b2a66234cbf5ca965b26a2bb4acb1b (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 *    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.
 */

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding

#include "mongo/platform/basic.h"

#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/lasterror.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/s/collection_sharding_runtime.h"
#include "mongo/db/s/migration_source_manager.h"
#include "mongo/db/s/shard_filtering_metadata_refresh.h"
#include "mongo/db/s/sharding_state.h"
#include "mongo/db/views/view_catalog.h"
#include "mongo/logv2/log.h"
#include "mongo/s/client/shard_registry.h"
#include "mongo/s/grid.h"
#include "mongo/s/request_types/set_shard_version_request.h"
#include "mongo/util/str.h"

namespace mongo {
namespace {

class SetShardVersion : public ErrmsgCommandDeprecated {
public:
    SetShardVersion() : ErrmsgCommandDeprecated("setShardVersion") {}

    std::string help() const override {
        return "internal";
    }

    bool adminOnly() const override {
        return true;
    }

    AllowedOnSecondary secondaryAllowed(ServiceContext*) const override {
        return AllowedOnSecondary::kAlways;
    }

    virtual bool supportsWriteConcern(const BSONObj& cmd) const override {
        return false;
    }

    void addRequiredPrivileges(const std::string& dbname,
                               const BSONObj& cmdObj,
                               std::vector<Privilege>* out) const override {
        ActionSet actions;
        actions.addAction(ActionType::internal);
        out->push_back(Privilege(ResourcePattern::forClusterResource(), actions));
    }

    bool errmsgRun(OperationContext* opCtx,
                   const std::string&,
                   const BSONObj& cmdObj,
                   std::string& errmsg,
                   BSONObjBuilder& result) {
        uassert(ErrorCodes::IllegalOperation,
                "can't issue setShardVersion from 'eval'",
                !opCtx->getClient()->isInDirectClient());

        auto const shardingState = ShardingState::get(opCtx);
        uassertStatusOK(shardingState->canAcceptShardedCommands());

        // Steps
        // 1. Set the `authoritative` and `forceRefresh` variables from the command object.
        //
        // 2. Validate all command parameters against the info in our ShardingState, and return an
        //    error if they do not match.
        //
        // 3. If the sent shardVersion is compatible with our shardVersion, return.
        //
        // 4. If the sent shardVersion indicates a drop, jump to step 6.
        //
        // 5. If the sent shardVersion is staler than ours, return a stale config error.
        //
        // 6. If the sent shardVersion is newer than ours (or indicates a drop), reload our metadata
        //    and compare the sent shardVersion with what we reloaded. If the sent shardVersion is
        //    staler than what we reloaded, return a stale config error, as in step 5.

        // Step 1

        Client* client = opCtx->getClient();
        LastError::get(client).disable();

        const bool authoritative = cmdObj.getBoolField("authoritative");
        // A flag that specifies whether the set shard version catalog refresh
        // is allowed to join an in-progress refresh triggered by an other
        // thread, or whether it's required to either a) trigger its own
        // refresh or b) wait for a refresh to be started after it has entered the
        // getCollectionRoutingInfoWithRefresh function
        const bool forceRefresh = cmdObj.getBoolField("forceRefresh");

        // Step 2

        // Validate shardName parameter.
        const auto shardName = cmdObj["shard"].str();
        const auto storedShardName = shardingState->shardId().toString();
        uassert(ErrorCodes::BadValue,
                str::stream() << "received shardName " << shardName
                              << " which differs from stored shardName " << storedShardName,
                storedShardName == shardName);

        // Validate config connection string parameter.
        const auto configdb = cmdObj["configdb"].String();
        uassert(ErrorCodes::BadValue,
                "Config server connection string cannot be empty",
                !configdb.empty());

        const auto givenConnStr = uassertStatusOK(ConnectionString::parse(configdb));
        uassert(ErrorCodes::InvalidOptions,
                str::stream() << "Given config server string " << givenConnStr.toString()
                              << " is not of type SET",
                givenConnStr.type() == ConnectionString::SET);

        const auto storedConnStr =
            Grid::get(opCtx)->shardRegistry()->getConfigServerConnectionString();
        uassert(ErrorCodes::IllegalOperation,
                str::stream() << "Given config server set name: " << givenConnStr.getSetName()
                              << " differs from known set name: " << storedConnStr.getSetName(),
                givenConnStr.getSetName() == storedConnStr.getSetName());

        // Validate namespace parameter.
        const NamespaceString nss(cmdObj["setShardVersion"].String());
        uassert(ErrorCodes::InvalidNamespace,
                str::stream() << "Invalid namespace " << nss.ns(),
                nss.isValid());

        // Validate chunk version parameter.
        const ChunkVersion requestedVersion = uassertStatusOK(
            ChunkVersion::parseLegacyWithField(cmdObj, SetShardVersionRequest::kVersion));

        // Step 3

        {
            boost::optional<AutoGetDb> autoDb;
            autoDb.emplace(opCtx, nss.db(), MODE_IS);

            // Slave nodes cannot support set shard version
            uassert(ErrorCodes::NotMaster,
                    str::stream() << "setShardVersion with collection version is only supported "
                                     "against primary nodes, but it was received for namespace "
                                  << nss.ns(),
                    repl::ReplicationCoordinator::get(opCtx)->canAcceptWritesForDatabase(opCtx,
                                                                                         nss.db()));

            boost::optional<Lock::CollectionLock> collLock;
            collLock.emplace(opCtx, nss, MODE_IS);

            // Views do not require a shard version check. We do not care about invalid system views
            // for this check, only to validate if a view already exists for this namespace.
            if (autoDb->getDb() &&
                !CollectionCatalog::get(opCtx).lookupCollectionByNamespace(opCtx, nss) &&
                ViewCatalog::get(autoDb->getDb())
                    ->lookupWithoutValidatingDurableViews(opCtx, nss.ns())) {
                return true;
            }

            auto* const csr = CollectionShardingRuntime::get(opCtx, nss);
            const ChunkVersion collectionShardVersion = [&] {
                auto optMetadata = csr->getCurrentMetadataIfKnown();
                return (optMetadata && optMetadata->isSharded()) ? optMetadata->getShardVersion()
                                                                 : ChunkVersion::UNSHARDED();
            }();

            if (requestedVersion.isWriteCompatibleWith(collectionShardVersion)) {
                return true;
            }

            // Step 4

            const bool isDropRequested =
                !requestedVersion.isSet() && collectionShardVersion.isSet();

            if (isDropRequested) {
                if (!authoritative) {
                    result.appendBool("need_authoritative", true);
                    result.append("ns", nss.ns());
                    collectionShardVersion.appendLegacyWithField(&result, "globalVersion");
                    errmsg = "dropping needs to be authoritative";
                    return false;
                }

                // Fall through to metadata reload below
            } else {
                // Not Dropping

                // Step 5

                // TODO: Refactor all of this
                if (requestedVersion < collectionShardVersion &&
                    requestedVersion.epoch() == collectionShardVersion.epoch()) {
                    auto critSecSignal = csr->getCriticalSectionSignal(
                        opCtx, ShardingMigrationCriticalSection::kWrite);
                    if (critSecSignal) {
                        collLock.reset();
                        autoDb.reset();
                        LOGV2(22056, "waiting till out of critical section");
                        critSecSignal->waitFor(opCtx, Seconds(10));
                    }

                    errmsg = str::stream() << "shard global version for collection is higher "
                                           << "than trying to set to '" << nss.ns() << "'";
                    result.append("ns", nss.ns());
                    requestedVersion.appendLegacyWithField(&result, "version");
                    collectionShardVersion.appendLegacyWithField(&result, "globalVersion");
                    result.appendBool("reloadConfig", true);
                    return false;
                }

                if (!collectionShardVersion.isSet() && !authoritative) {
                    // Needed b/c when the last chunk is moved off a shard, the version gets reset
                    // to zero, which should require a reload.
                    auto critSecSignal = csr->getCriticalSectionSignal(
                        opCtx, ShardingMigrationCriticalSection::kWrite);
                    if (critSecSignal) {
                        collLock.reset();
                        autoDb.reset();
                        LOGV2(22057, "waiting till out of critical section");
                        critSecSignal->waitFor(opCtx, Seconds(10));
                    }

                    // need authoritative for first look
                    result.append("ns", nss.ns());
                    result.appendBool("need_authoritative", true);
                    errmsg = str::stream() << "first time for collection '" << nss.ns() << "'";
                    return false;
                }

                // Fall through to metadata reload below
            }
        }

        // Step 6

        // Note: The forceRefresh flag controls whether we make sure to do our
        // own refresh or if we're okay with joining another thread
        const auto status = onShardVersionMismatchNoExcept(
            opCtx, nss, requestedVersion, forceRefresh /*forceRefreshFromThisThread*/);

        {
            // Avoid using AutoGetCollection() as it returns the InvalidViewDefinition error code
            // if an invalid view is in the 'system.views' collection.
            AutoGetDb autoDb(opCtx, nss.db(), MODE_IS);
            Lock::CollectionLock collLock(opCtx, nss, MODE_IS);

            const ChunkVersion currVersion = [&] {
                auto* const csr = CollectionShardingRuntime::get(opCtx, nss);
                auto optMetadata = csr->getCurrentMetadataIfKnown();
                return (optMetadata && optMetadata->isSharded()) ? optMetadata->getShardVersion()
                                                                 : ChunkVersion::UNSHARDED();
            }();

            if (!status.isOK()) {
                // The reload itself was interrupted or confused here
                LOGV2_WARNING(
                    22058,
                    "Could not refresh metadata for the namespace {namespace} with the requested "
                    "shard version {requestedShardVersion}; the current shard version is "
                    "{currentShardVersion}: {error}",
                    "Could not refresh metadata",
                    "namespace"_attr = nss.ns(),
                    "requestedShardVersion"_attr = requestedVersion,
                    "currentShardVersion"_attr = currVersion,
                    "error"_attr = redact(status));

                result.append("ns", nss.ns());
                result.append("code", status.code());
                requestedVersion.appendLegacyWithField(&result, "version");
                currVersion.appendLegacyWithField(&result, "globalVersion");
                result.appendBool("reloadConfig", true);

                return false;
            } else if (!requestedVersion.isWriteCompatibleWith(currVersion)) {
                // We reloaded a version that doesn't match the version mongos was trying to
                // set.
                static Occasionally sampler;
                if (sampler.tick()) {
                    LOGV2_WARNING(
                        22059,
                        "Requested shard version differs from the authoritative (current) shard "
                        "version for the namespace {namespace}; the requested version is "
                        "{requestedShardVersion}, but the current version is "
                        "{currentShardVersion}",
                        "Requested shard version differs from the authoritative (current) shard "
                        "version for this namespace",
                        "namespace"_attr = nss.ns(),
                        "requestedShardVersion"_attr = requestedVersion,
                        "currentShardVersion"_attr = currVersion);
                }

                // WARNING: the exact fields below are important for compatibility with mongos
                // version reload.

                result.append("ns", nss.ns());
                currVersion.appendLegacyWithField(&result, "globalVersion");

                // If this was a reset of a collection or the last chunk moved out, inform mongos to
                // do a full reload.
                if (currVersion.epoch() != requestedVersion.epoch() || !currVersion.isSet()) {
                    result.appendBool("reloadConfig", true);
                    // Zero-version also needed to trigger full mongos reload, sadly
                    // TODO: Make this saner, and less impactful (full reload on last chunk is bad)
                    ChunkVersion(0, 0, OID()).appendLegacyWithField(&result, "version");
                    // For debugging
                    requestedVersion.appendLegacyWithField(&result, "origVersion");
                } else {
                    requestedVersion.appendLegacyWithField(&result, "version");
                }

                return false;
            }
        }

        return true;
    }

} setShardVersionCmd;

}  // namespace
}  // namespace mongo