summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/txn_two_phase_commit_cmds.cpp
blob: a7ffc761188a1e4fcec620827059752e5c900653 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
 *    Copyright (C) 2018 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::kTransaction

#include "mongo/platform/basic.h"

#include "mongo/client/remote_command_targeter.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/txn_two_phase_commit_cmds_gen.h"
#include "mongo/db/operation_context_session_mongod.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/s/sharding_state.h"
#include "mongo/db/transaction_coordinator_service.h"
#include "mongo/db/transaction_participant.h"
#include "mongo/executor/task_executor.h"
#include "mongo/executor/task_executor_pool.h"
#include "mongo/s/grid.h"
#include "mongo/util/log.h"

namespace mongo {
namespace {

MONGO_FAIL_POINT_DEFINE(skipShardingPartsOfPrepareTransaction);

class PrepareTransactionCmd : public TypedCommand<PrepareTransactionCmd> {
public:
    class PrepareTimestamp {
    public:
        PrepareTimestamp(Timestamp timestamp) : _timestamp(std::move(timestamp)) {}
        void serialize(BSONObjBuilder* bob) const {
            bob->append("prepareTimestamp", _timestamp);
        }

    private:
        Timestamp _timestamp;
    };

    using Request = PrepareTransaction;
    using Response = PrepareTimestamp;

    class Invocation final : public InvocationBase {
    public:
        using InvocationBase::InvocationBase;

        Response typedRun(OperationContext* opCtx) {
            // In production, only config servers or initialized shard servers can participate in a
            // sharded transaction. However, many test suites test the replication and storage parts
            // of prepareTransaction against a standalone replica set, so allow skipping the check.
            if (!MONGO_FAIL_POINT(skipShardingPartsOfPrepareTransaction)) {
                if (serverGlobalParams.clusterRole != ClusterRole::ConfigServer) {
                    uassertStatusOK(ShardingState::get(opCtx)->canAcceptShardedCommands());
                }
            }

            auto txnParticipant = TransactionParticipant::get(opCtx);
            uassert(ErrorCodes::CommandFailed,
                    "prepareTransaction must be run within a transaction",
                    txnParticipant);

            LOG(3)
                << "Participant shard received prepareTransaction for transaction with txnNumber "
                << opCtx->getTxnNumber() << " on session "
                << opCtx->getLogicalSessionId()->toBSON();

            uassert(ErrorCodes::CommandNotSupported,
                    "'prepareTransaction' is only supported in feature compatibility version 4.2",
                    (serverGlobalParams.featureCompatibility.getVersion() ==
                     ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42));

            uassert(ErrorCodes::NoSuchTransaction,
                    "Transaction isn't in progress",
                    txnParticipant->inMultiDocumentTransaction());

            const auto& cmd = request();

            if (txnParticipant->transactionIsPrepared()) {
                auto& replClient = repl::ReplClientInfo::forClient(opCtx->getClient());
                auto prepareOpTime = txnParticipant->getPrepareOpTime();
                // Set the client optime to be prepareOpTime if it's not already later than
                // prepareOpTime. his ensures that we wait for writeConcern and that prepareOpTime
                // will be committed.
                if (prepareOpTime > replClient.getLastOp()) {
                    replClient.setLastOp(prepareOpTime);
                }

                invariant(opCtx->recoveryUnit()->getPrepareTimestamp() ==
                              prepareOpTime.getTimestamp(),
                          str::stream() << "recovery unit prepareTimestamp: "
                                        << opCtx->recoveryUnit()->getPrepareTimestamp().toString()
                                        << " participant prepareOpTime: "
                                        << prepareOpTime.toString());

                // A participant should re-send its vote if it re-received prepare.
                _sendVoteCommit(opCtx, prepareOpTime.getTimestamp(), cmd.getCoordinatorId());

                return PrepareTimestamp(prepareOpTime.getTimestamp());
            }

            // TODO (SERVER-36839): Pass coordinatorId into prepareTransaction() so that the
            // coordinatorId can be included in the write to config.transactions.
            const auto prepareTimestamp = txnParticipant->prepareTransaction(opCtx, {});
            _sendVoteCommit(opCtx, prepareTimestamp, cmd.getCoordinatorId());

            return PrepareTimestamp(prepareTimestamp);
        }

    private:
        void _sendVoteCommit(OperationContext* opCtx,
                             Timestamp prepareTimestamp,
                             ShardId coordinatorId) {
            // In a production cluster, a participant should always send its vote to the coordinator
            // as part of prepareTransaction. However, many test suites test the replication and
            // storage parts of prepareTransaction against a standalone replica set, so allow
            // skipping sending a vote.
            if (MONGO_FAIL_POINT(skipShardingPartsOfPrepareTransaction)) {
                return;
            }

            VoteCommitTransaction voteCommit;
            voteCommit.setDbName("admin");
            voteCommit.setShardId(ShardingState::get(opCtx)->shardId());
            voteCommit.setPrepareTimestamp(prepareTimestamp);
            BSONObj voteCommitObj = voteCommit.toBSON(
                BSON("lsid" << opCtx->getLogicalSessionId()->toBSON() << "txnNumber"
                            << *opCtx->getTxnNumber()
                            << "autocommit"
                            << false));
            _sendVote(opCtx, voteCommitObj, coordinatorId);
        }

        void _sendVoteAbort(OperationContext* opCtx, ShardId coordinatorId) {
            // In a production cluster, a participant should always send its vote to the coordinator
            // as part of prepareTransaction. However, many test suites test the replication and
            // storage parts of prepareTransaction against a standalone replica set, so allow
            // skipping sending a vote.
            if (MONGO_FAIL_POINT(skipShardingPartsOfPrepareTransaction)) {
                return;
            }

            VoteAbortTransaction voteAbort;
            voteAbort.setDbName("admin");
            voteAbort.setShardId(ShardingState::get(opCtx)->shardId());
            BSONObj voteAbortObj = voteAbort.toBSON(
                BSON("lsid" << opCtx->getLogicalSessionId()->toBSON() << "txnNumber"
                            << *opCtx->getTxnNumber()
                            << "autocommit"
                            << false));
            _sendVote(opCtx, voteAbortObj, coordinatorId);
        }

        void _sendVote(OperationContext* opCtx, const BSONObj& voteObj, ShardId coordinatorId) {
            try {
                // TODO (SERVER-37328): Participant should wait for writeConcern before sending its
                // vote.

                LOG(3) << "Participant shard sending " << voteObj << " to " << coordinatorId;

                const auto coordinatorPrimaryHost = [&] {
                    auto coordinatorShard = uassertStatusOK(
                        Grid::get(opCtx)->shardRegistry()->getShard(opCtx, coordinatorId));
                    return uassertStatusOK(coordinatorShard->getTargeter()->findHostNoWait(
                        ReadPreferenceSetting{ReadPreference::PrimaryOnly}));
                }();

                const executor::RemoteCommandRequest request(
                    coordinatorPrimaryHost,
                    NamespaceString::kAdminDb.toString(),
                    voteObj,
                    ReadPreferenceSetting{ReadPreference::PrimaryOnly}.toContainingBSON(),
                    opCtx,
                    executor::RemoteCommandRequest::kNoTimeout);

                auto noOp = [](const executor::TaskExecutor::RemoteCommandCallbackArgs&) {};
                uassertStatusOK(
                    Grid::get(opCtx)->getExecutorPool()->getFixedExecutor()->scheduleRemoteCommand(
                        request, noOp));
            } catch (const DBException& ex) {
                LOG(3) << "Participant shard failed to send " << voteObj << " to " << coordinatorId
                       << causedBy(ex.toStatus());
            }
        }

        bool supportsWriteConcern() const override {
            return true;
        }

        NamespaceString ns() const override {
            return NamespaceString(request().getDbName(), "");
        }

        void doCheckAuthorization(OperationContext* opCtx) const override {}
    };

    virtual bool adminOnly() const {
        return true;
    }

    std::string help() const override {
        return "Prepares a transaction on this shard; sent by a router or re-sent by the "
               "transaction commit coordinator for a cross-shard transaction";
    }

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

class VoteCommitTransactionCmd : public TypedCommand<VoteCommitTransactionCmd> {
public:
    using Request = VoteCommitTransaction;
    class Invocation final : public InvocationBase {
    public:
        using InvocationBase::InvocationBase;

        void typedRun(OperationContext* opCtx) {
            // Only config servers or initialized shard servers can act as transaction coordinators.
            if (serverGlobalParams.clusterRole != ClusterRole::ConfigServer) {
                uassertStatusOK(ShardingState::get(opCtx)->canAcceptShardedCommands());
            }

            uassert(
                ErrorCodes::CommandNotSupported,
                "'voteCommitTransaction' is only supported in feature compatibility version 4.2",
                (serverGlobalParams.featureCompatibility.getVersion() ==
                 ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42));

            const auto& cmd = request();

            LOG(3) << "Coordinator shard received voteCommit from " << cmd.getShardId()
                   << " with prepare timestamp " << cmd.getPrepareTimestamp() << " for transaction "
                   << opCtx->getTxnNumber() << " on session "
                   << opCtx->getLogicalSessionId()->toBSON();

            TransactionCoordinatorService::get(opCtx)->voteCommit(
                opCtx,
                opCtx->getLogicalSessionId().get(),
                opCtx->getTxnNumber().get(),
                cmd.getShardId(),
                cmd.getPrepareTimestamp());
        }

    private:
        bool supportsWriteConcern() const override {
            return false;
        }

        NamespaceString ns() const override {
            return NamespaceString(request().getDbName(), "");
        }

        void doCheckAuthorization(OperationContext* opCtx) const override {}
    };

    virtual bool adminOnly() const {
        return true;
    }

    std::string help() const override {
        return "Votes to commit a transaction; sent by a transaction participant to the "
               "transaction commit coordinator for a cross-shard transaction";
    }

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

class VoteAbortTransactionCmd : public TypedCommand<VoteAbortTransactionCmd> {
public:
    using Request = VoteAbortTransaction;
    class Invocation final : public InvocationBase {
    public:
        using InvocationBase::InvocationBase;

        void typedRun(OperationContext* opCtx) {
            // Only config servers or initialized shard servers can act as transaction coordinators.
            if (serverGlobalParams.clusterRole != ClusterRole::ConfigServer) {
                uassertStatusOK(ShardingState::get(opCtx)->canAcceptShardedCommands());
            }

            uassert(ErrorCodes::CommandNotSupported,
                    "'voteAbortTransaction' is only supported in feature compatibility version 4.2",
                    (serverGlobalParams.featureCompatibility.getVersion() ==
                     ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42));

            const auto& cmd = request();

            LOG(3) << "Coordinator shard received voteAbort from " << cmd.getShardId()
                   << " for transaction " << opCtx->getTxnNumber() << " on session "
                   << opCtx->getLogicalSessionId()->toBSON();

            TransactionCoordinatorService::get(opCtx)->voteAbort(opCtx,
                                                                 opCtx->getLogicalSessionId().get(),
                                                                 opCtx->getTxnNumber().get(),
                                                                 cmd.getShardId());
        }

    private:
        bool supportsWriteConcern() const override {
            return false;
        }

        NamespaceString ns() const override {
            return NamespaceString(request().getDbName(), "");
        }

        void doCheckAuthorization(OperationContext* opCtx) const override {}
    };

    virtual bool adminOnly() const {
        return true;
    }

    std::string help() const override {
        return "Votes to abort a transaction; sent by a transaction participant to the transaction "
               "commit coordinator for a cross-shard transaction";
    }

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

// TODO (SERVER-37440): Make coordinateCommit idempotent.
class CoordinateCommitTransactionCmd : public TypedCommand<CoordinateCommitTransactionCmd> {
public:
    using Request = CoordinateCommitTransaction;
    class Invocation final : public InvocationBase {
    public:
        using InvocationBase::InvocationBase;

        void typedRun(OperationContext* opCtx) {
            // Only config servers or initialized shard servers can act as transaction coordinators.
            if (serverGlobalParams.clusterRole != ClusterRole::ConfigServer) {
                uassertStatusOK(ShardingState::get(opCtx)->canAcceptShardedCommands());
            }

            uassert(ErrorCodes::CommandNotSupported,
                    "'coordinateCommitTransaction' is only supported in feature compatibility "
                    "version 4.2",
                    (serverGlobalParams.featureCompatibility.getVersion() ==
                     ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42));

            const auto& cmd = request();

            // Convert the participant list array into a set, and assert that all participants in
            // the list are unique.
            // TODO (PM-564): Propagate the 'readOnly' flag down into the TransactionCoordinator.
            std::set<ShardId> participantList;
            StringBuilder ss;
            ss << "[";
            for (const auto& participant : cmd.getParticipants()) {
                const auto shardId = participant.getShardId();
                uassert(ErrorCodes::InvalidOptions,
                        str::stream() << "participant list contained duplicate shardId " << shardId,
                        std::find(participantList.begin(), participantList.end(), shardId) ==
                            participantList.end());
                participantList.insert(shardId);
                ss << shardId << " ";
            }
            ss << "]";
            LOG(3) << "Coordinator shard received participant list with shards " << ss.str()
                   << " for transaction " << opCtx->getTxnNumber() << " on session "
                   << opCtx->getLogicalSessionId()->toBSON();

            auto commitDecisionFuture = TransactionCoordinatorService::get(opCtx)->coordinateCommit(
                opCtx,
                opCtx->getLogicalSessionId().get(),
                opCtx->getTxnNumber().get(),
                participantList);

            // If the commit decision is already available before we prepare locally, it means the
            // transaction has completed and we should skip preparing locally.
            //
            // TODO (SERVER-37440): Reconsider when coordinateCommit is made idempotent.
            if (!commitDecisionFuture.isReady()) {
                // Execute the 'prepare' logic on the local participant (the router does not send a
                // separate 'prepare' message to the coordinator shard).
                _callPrepareOnLocalParticipant(opCtx);
            }

            // Block waiting for the commit decision.
            auto commitDecision = commitDecisionFuture.get(opCtx);

            // If the decision was abort, propagate NoSuchTransaction exception back to mongos.
            uassert(ErrorCodes::NoSuchTransaction,
                    "Transaction was aborted",
                    commitDecision != TransactionCoordinatorService::CommitDecision::kAbort);
        }

    private:
        void _callPrepareOnLocalParticipant(OperationContext* opCtx) {
            auto localParticipantPrepareTimestamp = [&]() -> Timestamp {
                OperationContextSessionMongod checkOutSession(
                    opCtx, true, false, boost::none, false);

                auto txnParticipant = TransactionParticipant::get(opCtx);

                txnParticipant->unstashTransactionResources(opCtx, "prepareTransaction");
                ScopeGuard guard = MakeGuard([&txnParticipant, opCtx]() {
                    txnParticipant->abortActiveUnpreparedOrStashPreparedTransaction(opCtx);
                });

                auto prepareTimestamp = txnParticipant->prepareTransaction(opCtx, {});

                txnParticipant->stashTransactionResources(opCtx);
                guard.Dismiss();
                return prepareTimestamp;
            }();

            LOG(3) << "Participant shard delivering voteCommit with prepareTimestamp "
                   << localParticipantPrepareTimestamp << " to local coordinator for transaction "
                   << opCtx->getTxnNumber() << " on session "
                   << opCtx->getLogicalSessionId()->toBSON();

            // Deliver the local participant's vote to the coordinator.
            TransactionCoordinatorService::get(opCtx)->voteCommit(
                opCtx,
                opCtx->getLogicalSessionId().get(),
                opCtx->getTxnNumber().get(),
                ShardingState::get(opCtx)->shardId(),
                localParticipantPrepareTimestamp);
        }

        bool supportsWriteConcern() const override {
            return true;
        }

        NamespaceString ns() const override {
            return NamespaceString(request().getDbName(), "");
        }

        void doCheckAuthorization(OperationContext* opCtx) const override {}
    };

    bool adminOnly() const override {
        return true;
    }

    std::string help() const override {
        return "Coordinates the commit for a transaction. Only called by mongos.";
    }

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

}  // namespace
}  // namespace mongo