summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/move_chunk_command.cpp
blob: decb11713ca4c3ad06c4f6e527a363b666706423 (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
/**
 *    Copyright (C) 2015 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.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::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_manager.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/commands.h"
#include "mongo/db/range_deleter_service.h"
#include "mongo/db/s/chunk_move_write_concern_options.h"
#include "mongo/db/s/collection_metadata.h"
#include "mongo/db/s/migration_source_manager.h"
#include "mongo/db/s/move_timing_helper.h"
#include "mongo/db/s/sharding_state.h"
#include "mongo/s/catalog/dist_lock_manager.h"
#include "mongo/s/client/shard_registry.h"
#include "mongo/s/grid.h"
#include "mongo/s/migration_secondary_throttle_options.h"
#include "mongo/s/move_chunk_request.h"
#include "mongo/util/fail_point_service.h"
#include "mongo/util/log.h"

namespace mongo {

using std::string;

namespace {

/**
 * RAII object, which will register an active migration with the global sharding state so that no
 * subsequent migrations may start until the previous one has completed.
 */
class ScopedSetInMigration {
    MONGO_DISALLOW_COPYING(ScopedSetInMigration);

public:
    /**
     * Registers a new migration with the global sharding state and acquires distributed lock on the
     * collection so other shards cannot do migrations. If a migration is already active it will
     * throw a user assertion with a ConflictingOperationInProgress code. Otherwise it may throw any
     * other error due to distributed lock acquisition failure.
     */
    ScopedSetInMigration(OperationContext* txn, MoveChunkRequest args)
        : _scopedRegisterMigration(txn, args.getNss()),
          _collectionDistLock(_acquireDistLock(txn, args)) {}

    ~ScopedSetInMigration() = default;

    /**
     * Blocking call, which polls the state of the distributed lock and ensures that it is still
     * held.
     */
    Status checkDistLock() {
        return _collectionDistLock.checkStatus();
    }

private:
    /**
     * Acquires a distributed lock for the specified colleciton or throws if lock cannot be
     * acquired.
     */
    DistLockManager::ScopedDistLock _acquireDistLock(OperationContext* txn,
                                                     const MoveChunkRequest& args) {
        const std::string whyMessage(
            str::stream() << "migrating chunk [" << args.getMinKey() << ", " << args.getMaxKey()
                          << ") in "
                          << args.getNss().ns());
        auto distLockStatus =
            grid.catalogManager(txn)->distLock(txn, args.getNss().ns(), whyMessage);
        if (!distLockStatus.isOK()) {
            const std::string msg = str::stream()
                << "Could not acquire collection lock for " << args.getNss().ns()
                << " to migrate chunk [" << args.getMinKey() << "," << args.getMaxKey()
                << ") due to " << distLockStatus.getStatus().toString();
            warning() << msg;
            uasserted(distLockStatus.getStatus().code(), msg);
        }

        return std::move(distLockStatus.getValue());
    }

    // The scoped migration registration
    ShardingState::ScopedRegisterMigration _scopedRegisterMigration;

    // Handle for the the distributed lock, which protects other migrations from happening on the
    // same collection
    DistLockManager::ScopedDistLock _collectionDistLock;
};

/**
 * If the specified status is not OK logs a warning and throws a DBException corresponding to the
 * specified status.
 */
void uassertStatusOKWithWarning(const Status& status) {
    if (!status.isOK()) {
        warning() << "Chunk move failed" << causedBy(status);
        uassertStatusOK(status);
    }
}

// Tests can pause and resume moveChunk's progress at each step by enabling/disabling each failpoint
MONGO_FP_DECLARE(moveChunkHangAtStep1);
MONGO_FP_DECLARE(moveChunkHangAtStep2);
MONGO_FP_DECLARE(moveChunkHangAtStep3);
MONGO_FP_DECLARE(moveChunkHangAtStep4);
MONGO_FP_DECLARE(moveChunkHangAtStep5);
MONGO_FP_DECLARE(moveChunkHangAtStep6);

class MoveChunkCommand : public Command {
public:
    MoveChunkCommand() : Command("moveChunk") {}

    void help(std::stringstream& help) const override {
        help << "should not be calling this directly";
    }

    bool slaveOk() const override {
        return false;
    }

    bool adminOnly() const override {
        return true;
    }

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

    Status checkAuthForCommand(ClientBasic* client,
                               const std::string& dbname,
                               const BSONObj& cmdObj) override {
        if (!AuthorizationSession::get(client)->isAuthorizedForActionsOnResource(
                ResourcePattern::forClusterResource(), ActionType::internal)) {
            return Status(ErrorCodes::Unauthorized, "Unauthorized");
        }
        return Status::OK();
    }

    std::string parseNs(const std::string& dbname, const BSONObj& cmdObj) const override {
        return parseNsFullyQualified(dbname, cmdObj);
    }

    bool run(OperationContext* txn,
             const string& dbname,
             BSONObj& cmdObj,
             int options,
             std::string& errmsg,
             BSONObjBuilder& result) override {
        const NamespaceString nss = NamespaceString(parseNs(dbname, cmdObj));
        uassert(ErrorCodes::InvalidNamespace, "need to specify a valid namespace", nss.isValid());

        MoveChunkRequest moveChunkRequest =
            uassertStatusOK(MoveChunkRequest::createFromCommand(nss, cmdObj));

        ShardingState* const shardingState = ShardingState::get(txn);

        if (!shardingState->enabled()) {
            shardingState->initializeFromConfigConnString(
                txn, moveChunkRequest.getConfigServerCS().toString());
        }

        shardingState->setShardName(moveChunkRequest.getFromShardId());

        const auto writeConcernForRangeDeleter =
            uassertStatusOK(ChunkMoveWriteConcernOptions::getEffectiveWriteConcern(
                txn, moveChunkRequest.getSecondaryThrottle()));

        // Make sure we're as up-to-date as possible with shard information. This catches the case
        // where we might have changed a shard's host by removing/adding a shard with the same name.
        grid.shardRegistry()->reload(txn);

        MoveTimingHelper moveTimingHelper(txn,
                                          "from",
                                          nss.ns(),
                                          moveChunkRequest.getMinKey(),
                                          moveChunkRequest.getMaxKey(),
                                          6,  // Total number of steps
                                          &errmsg,
                                          moveChunkRequest.getToShardId(),
                                          moveChunkRequest.getFromShardId());

        moveTimingHelper.done(1);
        MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep1);

        ScopedSetInMigration scopedInMigration(txn, moveChunkRequest);

        BSONObj shardKeyPattern;

        {
            MigrationSourceManager migrationSourceManager(txn, moveChunkRequest);

            shardKeyPattern =
                migrationSourceManager.getCommittedMetadata()->getKeyPattern().getOwned();

            moveTimingHelper.done(2);
            MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep2);

            Status startCloneStatus = migrationSourceManager.startClone(txn);
            if (startCloneStatus == ErrorCodes::ChunkTooBig) {
                // TODO: This is for compatibility with pre-3.2 balancer, which does not recognize
                // the ChunkTooBig error code and instead uses the "chunkTooBig" field in the
                // response. Remove after 3.4 is released.
                errmsg = startCloneStatus.reason();
                result.appendBool("chunkTooBig", true);
                return false;
            }

            uassertStatusOKWithWarning(startCloneStatus);
            moveTimingHelper.done(3);
            MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep3);

            uassertStatusOKWithWarning(migrationSourceManager.awaitToCatchUp(txn));
            moveTimingHelper.done(4);
            MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep4);

            // Ensure distributed lock is still held
            Status checkDistLockStatus = scopedInMigration.checkDistLock();
            if (!checkDistLockStatus.isOK()) {
                const string msg = str::stream() << "not entering migrate critical section due to "
                                                 << checkDistLockStatus.toString();
                warning() << msg;
                migrationSourceManager.cleanupOnError(txn);

                uasserted(checkDistLockStatus.code(), msg);
            }

            uassertStatusOKWithWarning(migrationSourceManager.enterCriticalSection(txn));
            uassertStatusOKWithWarning(migrationSourceManager.commitDonateChunk(txn));
            moveTimingHelper.done(5);
            MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep5);
        }

        // Schedule the range deleter
        RangeDeleterOptions deleterOptions(KeyRange(moveChunkRequest.getNss().ns(),
                                                    moveChunkRequest.getMinKey().getOwned(),
                                                    moveChunkRequest.getMaxKey().getOwned(),
                                                    shardKeyPattern));
        deleterOptions.writeConcern = writeConcernForRangeDeleter;
        deleterOptions.waitForOpenCursors = true;
        deleterOptions.fromMigrate = true;
        deleterOptions.onlyRemoveOrphanedDocs = true;
        deleterOptions.removeSaverReason = "post-cleanup";

        if (moveChunkRequest.getWaitForDelete()) {
            log() << "doing delete inline for cleanup of chunk data";

            string errMsg;

            // This is an immediate delete, and as a consequence, there could be more
            // deletes happening simultaneously than there are deleter worker threads.
            if (!getDeleter()->deleteNow(txn, deleterOptions, &errMsg)) {
                log() << "Error occured while performing cleanup: " << errMsg;
            }
        } else {
            log() << "forking for cleanup of chunk data";

            string errMsg;
            if (!getDeleter()->queueDelete(txn,
                                           deleterOptions,
                                           NULL,  // Don't want to be notified
                                           &errMsg)) {
                log() << "could not queue migration cleanup: " << errMsg;
            }
        }

        moveTimingHelper.done(6);
        MONGO_FAIL_POINT_PAUSE_WHILE_SET(moveChunkHangAtStep6);

        return true;
    }

} moveChunkCmd;

}  // namespace
}  // namespace mongo