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
|
/**
* 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::kTransaction
#include "mongo/platform/basic.h"
#include "mongo/db/s/transaction_coordinator_catalog.h"
#include "mongo/logv2/log.h"
#include "mongo/s/grid.h"
namespace mongo {
TransactionCoordinatorCatalog::TransactionCoordinatorCatalog() = default;
TransactionCoordinatorCatalog::~TransactionCoordinatorCatalog() {
join();
}
void TransactionCoordinatorCatalog::exitStepUp(Status status) {
if (status.isOK()) {
LOGV2(22438, "Incoming coordinateCommit requests are now enabled");
} else {
LOGV2_WARNING(22444,
"Coordinator recovery failed and coordinateCommit requests will not be "
"allowed: {error}",
"Coordinator recovery failed and coordinateCommit requests will not be "
"allowed",
"error"_attr = status);
}
stdx::lock_guard<Latch> lk(_mutex);
invariant(!_stepUpCompletionStatus);
_stepUpCompletionStatus = std::move(status);
_stepUpCompleteCV.notify_all();
}
void TransactionCoordinatorCatalog::onStepDown() {
stdx::unique_lock<Latch> ul(_mutex);
std::vector<std::shared_ptr<TransactionCoordinator>> coordinatorsToCancel;
for (auto&& [sessionId, coordinatorsForSession] : _coordinatorsBySession) {
for (auto&& [txnNumber, coordinatorsForTxnNumber] : coordinatorsForSession) {
for (auto&& [txnRetryCounter, coordinator] : coordinatorsForTxnNumber) {
coordinatorsToCancel.emplace_back(coordinator);
}
}
}
ul.unlock();
for (auto&& coordinator : coordinatorsToCancel) {
coordinator->cancelIfCommitNotYetStarted();
}
}
void TransactionCoordinatorCatalog::insert(OperationContext* opCtx,
const LogicalSessionId& lsid,
const TxnNumberAndRetryCounter& txnNumberAndRetryCounter,
std::shared_ptr<TransactionCoordinator> coordinator,
bool forStepUp) {
LOGV2_DEBUG(
22439,
3,
"{sessionId}:{txnNumberAndRetryCounter} Inserting coordinator into in-memory catalog",
"Inserting coordinator into in-memory catalog",
"sessionId"_attr = lsid.getId(),
"txnNumberAndRetryCounter"_attr = txnNumberAndRetryCounter);
auto txnNumber = txnNumberAndRetryCounter.getTxnNumber();
auto txnRetryCounter = *txnNumberAndRetryCounter.getTxnRetryCounter();
stdx::unique_lock<Latch> ul(_mutex);
if (!forStepUp) {
_waitForStepUpToComplete(ul, opCtx);
}
auto& coordinatorsBySession = _coordinatorsBySession[lsid];
// We should never try to insert a coordinator if one already exists for this session and txn
// number and transaction retry counter. Logic for avoiding this due to e.g. malformed commands
// should be handled external to the catalog.
if (coordinatorsBySession.find(txnNumber) != coordinatorsBySession.end()) {
auto coordinatorByTxnNumber = coordinatorsBySession[txnNumber];
invariant(
coordinatorByTxnNumber.find(txnRetryCounter) == coordinatorByTxnNumber.end(),
"Cannot insert a TransactionCoordinator into the TransactionCoordinatorCatalog with "
"the same session ID, transaction number and retry counter as a previous coordinator");
const auto latestCoordinatorOnSessionIter = coordinatorByTxnNumber.begin();
const auto latestTxnRetryCounter = latestCoordinatorOnSessionIter->first;
invariant(latestTxnRetryCounter < txnRetryCounter);
const auto& latestCoordinatorOnSession = latestCoordinatorOnSessionIter->second;
uassert(6032300,
"Cannot create a new transaction coordinator with the same session ID and "
"transaction number as a previous coordinator that has not reached a decision",
latestCoordinatorOnSession->getDecision().isReady());
auto swDecision = latestCoordinatorOnSession->getDecision().getNoThrow();
uassert(6032301,
"Cannot create a new transaction coordinator with the same session ID and "
"transaction number as a previous coordinator that has already reached a commit "
"decision",
!swDecision.isOK());
}
coordinatorsBySession[txnNumber][txnRetryCounter] = coordinator;
// Schedule callback to remove the coordinator from the catalog when all its activities have
// completed. This needs to be done outside of the mutex, in case the coordinator completed
// early because of stepdown. Otherwise the continuation could execute on the same thread and
// recursively acquire the mutex.
ul.unlock();
coordinator->onCompletion()
.thenRunOn(Grid::get(opCtx)->getExecutorPool()->getFixedExecutor())
.ignoreValue()
.getAsync([this, lsid, txnNumberAndRetryCounter](Status) {
_remove(lsid, txnNumberAndRetryCounter);
});
}
std::shared_ptr<TransactionCoordinator> TransactionCoordinatorCatalog::get(
OperationContext* opCtx,
const LogicalSessionId& lsid,
const TxnNumberAndRetryCounter& txnNumberAndRetryCounter) {
auto txnNumber = txnNumberAndRetryCounter.getTxnNumber();
auto txnRetryCounter = *txnNumberAndRetryCounter.getTxnRetryCounter();
stdx::unique_lock<Latch> ul(_mutex);
_waitForStepUpToComplete(ul, opCtx);
std::shared_ptr<TransactionCoordinator> coordinatorToReturn;
auto coordinatorsForSessionIter = _coordinatorsBySession.find(lsid);
if (coordinatorsForSessionIter != _coordinatorsBySession.end()) {
const auto& coordinatorsForSession = coordinatorsForSessionIter->second;
auto coordinatorForTxnNumberIter = coordinatorsForSession.find(txnNumber);
if (coordinatorForTxnNumberIter != coordinatorsForSession.end()) {
const auto& coordinatorsForTxnNumber = coordinatorForTxnNumberIter->second;
auto coordinatorForTxnRetryCounterIter = coordinatorsForTxnNumber.find(txnRetryCounter);
if (coordinatorForTxnRetryCounterIter != coordinatorsForTxnNumber.end()) {
coordinatorToReturn = coordinatorForTxnRetryCounterIter->second;
}
}
}
return coordinatorToReturn;
}
boost::optional<std::pair<TxnNumberAndRetryCounter, std::shared_ptr<TransactionCoordinator>>>
TransactionCoordinatorCatalog::getLatestOnSession(OperationContext* opCtx,
const LogicalSessionId& lsid) {
stdx::unique_lock<Latch> ul(_mutex);
_waitForStepUpToComplete(ul, opCtx);
const auto& coordinatorsForSessionIter = _coordinatorsBySession.find(lsid);
if (coordinatorsForSessionIter == _coordinatorsBySession.end()) {
return boost::none;
}
const auto& coordinatorsForSession = coordinatorsForSessionIter->second;
// We should never have empty map for a session because entries for sessions with no
// transactions are removed
invariant(!coordinatorsForSession.empty());
const auto& coordinatorsForLastTxnNumberIter = coordinatorsForSession.begin();
auto lastTxnNumber = coordinatorsForLastTxnNumberIter->first;
const auto& coordinatorsForLastTxnNumber = coordinatorsForLastTxnNumberIter->second;
// We should never have empty map for a txnNumber because entries for txnNumbers with no
// coordinators are removed.
invariant(!coordinatorsForLastTxnNumber.empty());
const auto& coordinatorsForLastTxnRetryCounterIter = coordinatorsForLastTxnNumber.begin();
auto lastTxnRetryCounter = coordinatorsForLastTxnRetryCounterIter->first;
return std::make_pair(TxnNumberAndRetryCounter{lastTxnNumber, lastTxnRetryCounter},
coordinatorsForLastTxnRetryCounterIter->second);
}
void TransactionCoordinatorCatalog::_remove(
const LogicalSessionId& lsid, const TxnNumberAndRetryCounter& txnNumberAndRetryCounter) {
LOGV2_DEBUG(
22440,
3,
"{sessionId}:{txnNumberAndRetryCounter} Removing coordinator from in-memory catalog",
"Removing coordinator from in-memory catalog",
"sessionId"_attr = lsid.getId(),
"txnNumberAndRetryCounter"_attr = txnNumberAndRetryCounter);
auto txnNumber = txnNumberAndRetryCounter.getTxnNumber();
auto txnRetryCounter = *txnNumberAndRetryCounter.getTxnRetryCounter();
stdx::lock_guard<Latch> lk(_mutex);
const auto& coordinatorsForSessionIter = _coordinatorsBySession.find(lsid);
if (coordinatorsForSessionIter != _coordinatorsBySession.end()) {
auto& coordinatorsForSession = coordinatorsForSessionIter->second;
const auto& coordinatorForTxnNumberIter = coordinatorsForSession.find(txnNumber);
if (coordinatorForTxnNumberIter != coordinatorsForSession.end()) {
auto& coordinatorsForTxnNumber = coordinatorForTxnNumberIter->second;
const auto& coordinatorForTxnRetryCounterIter =
coordinatorsForTxnNumber.find(txnRetryCounter);
if (coordinatorForTxnRetryCounterIter != coordinatorsForTxnNumber.end()) {
coordinatorsForTxnNumber.erase(coordinatorForTxnRetryCounterIter);
if (coordinatorsForTxnNumber.empty()) {
coordinatorsForSession.erase(coordinatorForTxnNumberIter);
if (coordinatorsForSession.empty()) {
_coordinatorsBySession.erase(coordinatorsForSessionIter);
}
}
}
}
}
if (_coordinatorsBySession.empty()) {
LOGV2_DEBUG(22441, 3, "Signaling last active coordinator removed");
_noActiveCoordinatorsCV.notify_all();
}
}
void TransactionCoordinatorCatalog::join() {
stdx::unique_lock<Latch> ul(_mutex);
while (!_noActiveCoordinatorsCV.wait_for(
ul, stdx::chrono::seconds{5}, [this] { return _coordinatorsBySession.empty(); })) {
LOGV2(22442,
"After 5 seconds of wait there are still {numSessionsLeft} sessions left "
"with active coordinators which have not yet completed",
"After 5 seconds of wait there are still sessions left with active coordinators "
"which have not yet completed",
"numSessionsLeft"_attr = _coordinatorsBySession.size());
LOGV2(22443,
"Active coordinators remaining: {activeCoordinators}",
"Active coordinators remaining",
"activeCoordinators"_attr = _toString(ul));
}
}
std::string TransactionCoordinatorCatalog::toString() const {
stdx::lock_guard<Latch> lk(_mutex);
return _toString(lk);
}
void TransactionCoordinatorCatalog::_waitForStepUpToComplete(stdx::unique_lock<Latch>& lk,
OperationContext* opCtx) {
invariant(lk.owns_lock());
opCtx->waitForConditionOrInterrupt(
_stepUpCompleteCV, lk, [this]() { return bool(_stepUpCompletionStatus); });
uassertStatusOK(*_stepUpCompletionStatus);
}
std::string TransactionCoordinatorCatalog::_toString(WithLock wl) const {
StringBuilder ss;
ss << "[";
for (const auto& coordinatorsForSession : _coordinatorsBySession) {
ss << "\n" << coordinatorsForSession.first.getId() << ": ";
for (const auto& coordinator : coordinatorsForSession.second) {
ss << coordinator.first << ",";
}
}
ss << "]";
return ss.str();
}
void TransactionCoordinatorCatalog::filter(FilterPredicate predicate, FilterVisitor visitor) {
stdx::lock_guard<Latch> lk(_mutex);
for (auto&& [sessionId, coordinatorsForSession] : _coordinatorsBySession) {
for (auto&& [txnNumber, coordinatorsForTxnNumber] : coordinatorsForSession) {
for (auto&& [txnRetryCounter, coordinator] : coordinatorsForTxnNumber) {
auto txnNumberAndRetryCounter =
TxnNumberAndRetryCounter(txnNumber, txnRetryCounter);
if (predicate(sessionId, txnNumberAndRetryCounter, coordinator)) {
visitor(sessionId, txnNumberAndRetryCounter, coordinator);
}
}
}
}
}
} // namespace mongo
|