summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/transaction_coordinator_driver.h
blob: 90f70cc65b5d0df64c7f56420196fd9611ba4315 (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

/**
 *    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 <vector>

#include "mongo/db/logical_session_id.h"
#include "mongo/db/s/transaction_coordinator_futures_util.h"
#include "mongo/executor/task_executor.h"
#include "mongo/util/concurrency/thread_pool.h"
#include "mongo/util/future.h"

namespace mongo {

class TransactionCoordinatorDocument;

namespace txn {

/**
 * The decision made by the coordinator whether to commit or abort the transaction.
 */
enum class CommitDecision {
    kCommit,
    kAbort,
    kCanceled,
};

/**
 * An alias to indicate a vote from a participant. This just makes it clearer what's going on in
 * different stages of the commit process.
 */
using PrepareVote = CommitDecision;

/**
 * Represents a response to prepareTransaction from a single participant. The timestamp will only be
 * present if the participant votes to commit (indicated by the decision field).
 */
struct PrepareResponse {
    ShardId participantShardId;
    boost::optional<PrepareVote> vote;
    boost::optional<Timestamp> prepareTimestamp;
};

/**
 * Represents the aggregate of all prepare responses, including the decision that should be made and
 * the max of all prepare timestamps received in the case of a decision to commit.
 */
struct PrepareVoteConsensus {
    boost::optional<CommitDecision> decision;
    boost::optional<Timestamp> maxPrepareTimestamp;
};

}  // namespace txn

/**
 * Single instance of this class is owned by each TransactionCoordinator. It abstracts the
 * long-running blocking operations into a futurized interface and ensures that all outstanding
 * operations are joined at completion.
 *
 * Methods on this class must be called from a single thread at a time and other than cancellation,
 * no other operations are allowed until any returned futures are signalled.
 */
class TransactionCoordinatorDriver {
    MONGO_DISALLOW_COPYING(TransactionCoordinatorDriver);

public:
    TransactionCoordinatorDriver(ServiceContext* serviceContext);
    ~TransactionCoordinatorDriver();

    /**
     * Upserts a document of the form:
     *
     * {
     *    _id: {lsid: <lsid>, txnNumber: <txnNumber>}
     *    participants: ["shard0000", "shard0001"]
     * }
     *
     * into config.transaction_coordinators and waits for the upsert to be majority-committed.
     *
     * Throws if the upsert fails or waiting for writeConcern fails.
     *
     * If the upsert returns a DuplicateKey error, converts it to an anonymous error, because it
     * means a document for the (lsid, txnNumber) exists with a different participant list.
     */
    Future<void> persistParticipantList(const LogicalSessionId& lsid,
                                        TxnNumber txnNumber,
                                        std::vector<ShardId> participantList);

    /**
     * Sends prepare to all participants and returns a future that will be resolved when either:
     *    a) All participants have responded with a vote to commit, or
     *    b) At least one participant votes to abort.
     *
     * If all participants vote to commit, the result will contain the max prepare timestamp of all
     * prepare timestamps attached to the participants' responses. Otherwise the result will simply
     * contain the decision to abort.
     */
    Future<txn::PrepareVoteConsensus> sendPrepare(const std::vector<ShardId>& participantShards,
                                                  const LogicalSessionId& lsid,
                                                  TxnNumber txnNumber);

    /**
     * If 'commitTimestamp' is boost::none, updates the document in config.transaction_coordinators
     * for
     *
     * (lsid, txnNumber) to be:
     *
     * {
     *    _id: {lsid: <lsid>, txnNumber: <txnNumber>}
     *    participants: ["shard0000", "shard0001"]
     *    decision: "abort"
     * }
     *
     * else updates the document to be:
     *
     * {
     *    _id: {lsid: <lsid>, txnNumber: <txnNumber>}
     *    participants: ["shard0000", "shard0001"]
     *    decision: "commit"
     *    commitTimestamp: Timestamp(xxxxxxxx, x),
     * }
     *
     * and waits for the update to be majority-committed.
     *
     * Throws if the update fails or waiting for writeConcern fails.
     *
     * If the update succeeds but did not update any document, throws an anonymous error, because it
     * means either no document for (lsid, txnNumber) exists, or a document exists but has a
     * different participant list, different decision, or different commit Timestamp.
     */
    Future<void> persistDecision(const LogicalSessionId& lsid,
                                 TxnNumber txnNumber,
                                 std::vector<ShardId> participantList,
                                 const boost::optional<Timestamp>& commitTimestamp);

    /**
     * Sends commit to all shards and returns a future that will be resolved when all participants
     * have responded with success.
     */
    Future<void> sendCommit(const std::vector<ShardId>& participantShards,
                            const LogicalSessionId& lsid,
                            TxnNumber txnNumber,
                            Timestamp commitTimestamp);

    /**
     * Sends abort to all shards and returns a future that will be resolved when all participants
     * have responded with success.
     */
    Future<void> sendAbort(const std::vector<ShardId>& participantShards,
                           const LogicalSessionId& lsid,
                           TxnNumber txnNumber);

    /**
     * Deletes the document in config.transaction_coordinators for (lsid, txnNumber).
     *
     * Does *not* wait for the delete to be majority-committed.
     *
     * Throws if the update fails.
     *
     * If the update succeeds but did not update any document, throws an anonymous error, because it
     * means either no document for (lsid, txnNumber) exists, or a document exists but without a
     * decision.
     */
    Future<void> deleteCoordinatorDoc(const LogicalSessionId& lsid, TxnNumber txnNumber);

    /**
     * Non-blocking method, which interrupts any executing asynchronous tasks and prevents new ones
     * from getting scheduled. Any futures returned by the driver must still be waited on before the
     * object is disposed of.
     *
     * TODO (SERVER-38522): Implement using real cancellation through the AsyncWorkScheduler
     */
    void cancel();

    /**
     * Reads and returns all documents in config.transaction_coordinators.
     */
    static std::vector<TransactionCoordinatorDocument> readAllCoordinatorDocs(
        OperationContext* opCtx);

    //
    // These methods are used internally and are exposed for unit-testing purposes only
    //

    /**
     * Sends prepare to a given shard, retrying until a response is received from the participant or
     * until the coordinator is no longer in a preparing state due to having already received a vote
     * to abort from another participant.
     *
     * Returns a future containing the participant's response.
     */
    Future<txn::PrepareResponse> sendPrepareToShard(const ShardId& shardId,
                                                    const BSONObj& prepareCommandObj);

    /**
     * Sends a command corresponding to a commit decision (i.e. commitTransaction or
     * abortTransaction) to the given shard ID and retries on any retryable error until the command
     * succeeds or receives a response that may be interpreted as a vote to abort (e.g.
     * NoSuchTransaction).
     *
     * Used for sendCommit and sendAbort.
     */
    Future<void> sendDecisionToParticipantShard(const ShardId& shardId, const BSONObj& commandObj);

private:
    ServiceContext* _serviceContext;

    std::unique_ptr<txn::AsyncWorkScheduler> _scheduler;

    // TODO (SERVER-38522): Remove once AsyncWorkScheduler is used for cancellation
    AtomicWord<bool> _cancelled{false};
};

}  // namespace mongo