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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/**
* 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.h"
#include "mongo/db/repl/wait_for_majority_service.h"
#include "mongo/db/s/transaction_coordinator_metrics_observer.h"
#include "mongo/db/server_options.h"
#include "mongo/db/vector_clock_mutable.h"
#include "mongo/logv2/log.h"
#include "mongo/s/grid.h"
#include "mongo/util/cancellation.h"
#include "mongo/util/fail_point.h"
namespace mongo {
namespace {
using CommitDecision = txn::CommitDecision;
using CoordinatorCommitDecision = txn::CoordinatorCommitDecision;
using PrepareVoteConsensus = txn::PrepareVoteConsensus;
using TransactionCoordinatorDocument = txn::TransactionCoordinatorDocument;
MONGO_FAIL_POINT_DEFINE(hangBeforeWaitingForParticipantListWriteConcern);
MONGO_FAIL_POINT_DEFINE(hangBeforeWaitingForDecisionWriteConcern);
ExecutorFuture<void> waitForMajorityWithHangFailpoint(ServiceContext* service,
FailPoint& failpoint,
const std::string& failPointName,
repl::OpTime opTime) {
auto executor = Grid::get(service)->getExecutorPool()->getFixedExecutor();
auto waitForWC = [service, executor](repl::OpTime opTime) {
return WaitForMajorityService::get(service)
.waitUntilMajority(opTime, CancellationToken::uncancelable())
.thenRunOn(executor);
};
if (auto sfp = failpoint.scoped(); MONGO_unlikely(sfp.isActive())) {
const BSONObj& data = sfp.getData();
LOGV2(22445, "Hit {failPointName} failpoint", "failPointName"_attr = failPointName);
// Run the hang failpoint asynchronously on a different thread to avoid self deadlocks.
return ExecutorFuture<void>(executor).then(
[service, &failpoint, failPointName, data, waitForWC, opTime] {
if (!data["useUninterruptibleSleep"].eoo()) {
failpoint.pauseWhileSet();
} else {
ThreadClient tc(failPointName, service);
auto opCtx = tc->makeOperationContext();
failpoint.pauseWhileSet(opCtx.get());
}
return waitForWC(std::move(opTime));
});
}
return waitForWC(std::move(opTime));
}
} // namespace
TransactionCoordinator::TransactionCoordinator(
OperationContext* operationContext,
const LogicalSessionId& lsid,
const TxnNumberAndRetryCounter& txnNumberAndRetryCounter,
std::unique_ptr<txn::AsyncWorkScheduler> scheduler,
Date_t deadline)
: _serviceContext(operationContext->getServiceContext()),
_lsid(lsid),
_txnNumberAndRetryCounter(txnNumberAndRetryCounter),
_scheduler(std::move(scheduler)),
_sendPrepareScheduler(_scheduler->makeChildScheduler()),
_transactionCoordinatorMetricsObserver(
std::make_unique<TransactionCoordinatorMetricsObserver>()),
_deadline(deadline) {
invariant(_txnNumberAndRetryCounter.getTxnRetryCounter());
auto apiParams = APIParameters::get(operationContext);
auto kickOffCommitPF = makePromiseFuture<void>();
_kickOffCommitPromise = std::move(kickOffCommitPF.promise);
// Task, which will fire when the transaction's total deadline has been reached. If the 2PC
// sequence has not yet started, it will be abandoned altogether.
auto deadlineFuture =
_scheduler
->scheduleWorkAt(deadline,
[this](OperationContext*) {
LOGV2_DEBUG(5047000,
1,
"TransactionCoordinator deadline reached",
"sessionId"_attr = _lsid.getId(),
"txnNumberAndRetryCounter"_attr =
_txnNumberAndRetryCounter);
cancelIfCommitNotYetStarted();
// See the comments for sendPrepare about the purpose of this
// cancellation code
_sendPrepareScheduler->shutdown(
{ErrorCodes::TransactionCoordinatorReachedAbortDecision,
"Transaction exceeded deadline"});
})
.tapError([this](Status s) {
if (_reserveKickOffCommitPromise()) {
_kickOffCommitPromise.setError(std::move(s));
}
});
// TODO: The duration will be meaningless after failover.
_updateAssociatedClient(operationContext->getClient());
_transactionCoordinatorMetricsObserver->onCreate(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
// Two-phase commit phases chain. Once this chain executes, the 2PC sequence has completed
// either with success or error and the scheduled deadline task above has been joined.
std::move(kickOffCommitPF.future)
.then([this] {
return VectorClockMutable::get(_serviceContext)->waitForDurableTopologyTime();
})
.thenRunOn(Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor())
.then([this] {
// Persist the participants, unless they have been made durable already (which would
// only be the case if this coordinator was created as part of step-up recovery).
// Input: _participants
// _participantsDurable (optional)
// Output: _participantsDurable = true
{
stdx::lock_guard<Latch> lg(_mutex);
invariant(_participants);
_step = Step::kWritingParticipantList;
_transactionCoordinatorMetricsObserver->onStartWritingParticipantList(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
if (_participantsDurable)
return Future<repl::OpTime>::makeReady(repl::OpTime());
}
return txn::persistParticipantsList(
*_sendPrepareScheduler, _lsid, _txnNumberAndRetryCounter, *_participants);
})
.then([this](repl::OpTime opTime) {
return waitForMajorityWithHangFailpoint(
_serviceContext,
hangBeforeWaitingForParticipantListWriteConcern,
"hangBeforeWaitingForParticipantListWriteConcern",
std::move(opTime));
})
.thenRunOn(Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor())
.then([this, apiParams] {
{
stdx::lock_guard<Latch> lg(_mutex);
_participantsDurable = true;
}
// Send prepare to the participants, unless this has already been done (which would only
// be the case if this coordinator was created as part of step-up recovery and the
// recovery document contained a decision).
// Input: _participants, _participantsDurable
// _decision (optional)
// Output: _decision is set
{
stdx::lock_guard<Latch> lg(_mutex);
invariant(_participantsDurable);
_step = Step::kWaitingForVotes;
_transactionCoordinatorMetricsObserver->onStartWaitingForVotes(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
if (_decision)
return Future<void>::makeReady();
}
return txn::sendPrepare(_serviceContext,
*_sendPrepareScheduler,
_lsid,
_txnNumberAndRetryCounter,
apiParams,
*_participants)
.then([this](PrepareVoteConsensus consensus) mutable {
{
stdx::lock_guard<Latch> lg(_mutex);
_decision = consensus.decision();
}
if (_decision->getDecision() == CommitDecision::kCommit) {
LOGV2_DEBUG(
22446,
3,
"{sessionId}:{_txnNumberAndRetryCounter} Advancing cluster time to "
"the commit timestamp {commitTimestamp}",
"Advancing cluster time to the commit timestamp",
"sessionId"_attr = _lsid.getId(),
"txnNumberAndRetryCounter"_attr = _txnNumberAndRetryCounter,
"commitTimestamp"_attr = *_decision->getCommitTimestamp());
VectorClockMutable::get(_serviceContext)
->tickClusterTimeTo(LogicalTime(*_decision->getCommitTimestamp()));
}
});
})
.onError<ErrorCodes::TransactionCoordinatorReachedAbortDecision>(
[this, lsid, txnNumberAndRetryCounter](const Status& status) {
// Timeout happened, propagate the decision to abort the transaction to replicas
// and convert the internal error code to the public one.
LOGV2(5047001,
"Transaction coordinator made abort decision",
"sessionId"_attr = lsid.getId(),
"txnNumberAndRetryCounter"_attr = txnNumberAndRetryCounter,
"status"_attr = redact(status));
stdx::lock_guard<Latch> lg(_mutex);
_decision = txn::PrepareVote::kAbort;
_decision->setAbortStatus(Status(ErrorCodes::NoSuchTransaction, status.reason()));
})
.then([this] {
// Persist the commit decision, unless this has already been done (which would only be
// the case if this coordinator was created as part of step-up recovery and the recovery
// document contained a decision).
// Input: _decision
// _decisionDurable (optional)
// Output: _decisionDurable = true
{
stdx::lock_guard<Latch> lg(_mutex);
invariant(_decision);
_step = Step::kWritingDecision;
_transactionCoordinatorMetricsObserver->onStartWritingDecision(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
if (_decisionDurable)
return Future<repl::OpTime>::makeReady(repl::OpTime());
}
return txn::persistDecision(
*_scheduler, _lsid, _txnNumberAndRetryCounter, *_participants, *_decision);
})
.then([this](repl::OpTime opTime) {
switch (_decision->getDecision()) {
case CommitDecision::kCommit: {
_decisionPromise.emplaceValue(CommitDecision::kCommit);
break;
}
case CommitDecision::kAbort: {
_decisionPromise.setError(*_decision->getAbortStatus());
break;
}
default:
MONGO_UNREACHABLE;
};
return waitForMajorityWithHangFailpoint(_serviceContext,
hangBeforeWaitingForDecisionWriteConcern,
"hangBeforeWaitingForDecisionWriteConcern",
std::move(opTime));
})
.then([this, apiParams] {
{
stdx::lock_guard<Latch> lg(_mutex);
_decisionDurable = true;
}
// Send the commit/abort decision to the participants.
// Input: _decisionDurable
// Output: (none)
{
stdx::lock_guard<Latch> lg(_mutex);
invariant(_decisionDurable);
_step = Step::kWaitingForDecisionAcks;
_transactionCoordinatorMetricsObserver->onStartWaitingForDecisionAcks(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
}
switch (_decision->getDecision()) {
case CommitDecision::kCommit: {
return txn::sendCommit(_serviceContext,
*_scheduler,
_lsid,
_txnNumberAndRetryCounter,
apiParams,
*_participants,
*_decision->getCommitTimestamp());
}
case CommitDecision::kAbort: {
return txn::sendAbort(_serviceContext,
*_scheduler,
_lsid,
_txnNumberAndRetryCounter,
apiParams,
*_participants);
}
default:
MONGO_UNREACHABLE;
};
})
.then([this] {
// Do a best-effort attempt (i.e., writeConcern w:1) to delete the coordinator's durable
// state.
{
stdx::lock_guard<Latch> lg(_mutex);
_step = Step::kDeletingCoordinatorDoc;
_transactionCoordinatorMetricsObserver->onStartDeletingCoordinatorDoc(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
_serviceContext->getTickSource(),
_serviceContext->getPreciseClockSource()->now());
}
return txn::deleteCoordinatorDoc(*_scheduler, _lsid, _txnNumberAndRetryCounter)
.then([this] {
invariant(!_coordinatorDocRemovalPromise.getFuture().isReady());
_coordinatorDocRemovalPromise.emplaceValue();
});
})
.getAsync([this, deadlineFuture = std::move(deadlineFuture)](Status s) mutable {
// Interrupt this coordinator's scheduler hierarchy and join the deadline task's future
// in order to guarantee that there are no more threads running within the coordinator.
_scheduler->shutdown(
{ErrorCodes::TransactionCoordinatorDeadlineTaskCanceled, "Coordinator completed"});
return std::move(deadlineFuture).getAsync([this, s = std::move(s)](Status) {
// Notify all the listeners which are interested in the coordinator's lifecycle.
// After this call, the coordinator object could potentially get destroyed by its
// lifetime controller, so there shouldn't be any accesses to `this` after this
// call.
_done(s);
});
});
}
TransactionCoordinator::~TransactionCoordinator() {
invariant(_completionPromise.getFuture().isReady());
}
void TransactionCoordinator::runCommit(OperationContext* opCtx, std::vector<ShardId> participants) {
if (!_reserveKickOffCommitPromise())
return;
invariant(opCtx != nullptr && opCtx->getClient() != nullptr);
_updateAssociatedClient(opCtx->getClient());
_participants = std::move(participants);
_kickOffCommitPromise.emplaceValue();
}
void TransactionCoordinator::continueCommit(const TransactionCoordinatorDocument& doc) {
if (!_reserveKickOffCommitPromise())
return;
_transactionCoordinatorMetricsObserver->onRecoveryFromFailover();
_participants = std::move(doc.getParticipants());
if (doc.getDecision()) {
_participantsDurable = true;
_decision = std::move(doc.getDecision());
}
_kickOffCommitPromise.emplaceValue();
}
SharedSemiFuture<CommitDecision> TransactionCoordinator::getDecision() const {
return _decisionPromise.getFuture();
}
SharedSemiFuture<txn::CommitDecision> TransactionCoordinator::onCompletion() {
return _completionPromise.getFuture();
}
void TransactionCoordinator::cancelIfCommitNotYetStarted() {
if (!_reserveKickOffCommitPromise())
return;
_kickOffCommitPromise.setError({ErrorCodes::TransactionCoordinatorCanceled,
"Transaction exceeded deadline or newer transaction started"});
}
bool TransactionCoordinator::_reserveKickOffCommitPromise() {
stdx::lock_guard<Latch> lg(_mutex);
if (_kickOffCommitPromiseSet)
return false;
_kickOffCommitPromiseSet = true;
return true;
}
void TransactionCoordinator::_done(Status status) {
// TransactionCoordinatorSteppingDown indicates the *sending* node (that is, *this* node) is
// stepping down. Active coordinator tasks are interrupted with this code instead of
// InterruptedDueToReplStateChange, because InterruptedDueToReplStateChange indicates the
// *receiving* node was stepping down.
if (status == ErrorCodes::TransactionCoordinatorSteppingDown)
status = Status(ErrorCodes::InterruptedDueToReplStateChange,
str::stream() << "Coordinator " << _lsid.getId() << ':'
<< _txnNumberAndRetryCounter.toBSON()
<< " stopped due to: " << status.reason());
LOGV2_DEBUG(22447,
3,
"{sessionId}:{_txnNumberAndRetryCounter} Two-phase commit completed with {status}",
"Two-phase commit completed",
"sessionId"_attr = _lsid.getId(),
"txnNumberAndRetryCounter"_attr = _txnNumberAndRetryCounter,
"status"_attr = redact(status));
stdx::unique_lock<Latch> ul(_mutex);
const auto tickSource = _serviceContext->getTickSource();
_transactionCoordinatorMetricsObserver->onEnd(
ServerTransactionCoordinatorsMetrics::get(_serviceContext),
tickSource,
_serviceContext->getPreciseClockSource()->now(),
_step,
_decisionDurable ? _decision : boost::none);
if (status.isOK() &&
(shouldLog(logv2::LogComponent::kTransaction, logv2::LogSeverity::Debug(1)) ||
_transactionCoordinatorMetricsObserver->getSingleTransactionCoordinatorStats()
.getTwoPhaseCommitDuration(tickSource, tickSource->getTicks()) >
Milliseconds(serverGlobalParams.slowMS))) {
_logSlowTwoPhaseCommit(*_decision);
}
ul.unlock();
if (!_decisionPromise.getFuture().isReady()) {
_decisionPromise.setError(status);
}
if (!_coordinatorDocRemovalPromise.getFuture().isReady()) {
_coordinatorDocRemovalPromise.setError(status);
}
if (!status.isOK()) {
_completionPromise.setError(status);
} else {
// If the status is OK, the decisionPromise must be set.
_completionPromise.setFrom(_decisionPromise.getFuture().getNoThrow());
}
}
void TransactionCoordinator::_logSlowTwoPhaseCommit(
const txn::CoordinatorCommitDecision& decision) {
logv2::DynamicAttributes attrs;
BSONObjBuilder parametersBuilder;
BSONObjBuilder lsidBuilder(parametersBuilder.subobjStart("lsid"));
_lsid.serialize(&lsidBuilder);
lsidBuilder.doneFast();
parametersBuilder.append("txnNumber", _txnNumberAndRetryCounter.getTxnNumber());
parametersBuilder.append("txnRetryCounter", *_txnNumberAndRetryCounter.getTxnRetryCounter());
attrs.add("parameters", parametersBuilder.obj());
std::string decisionTemp;
switch (decision.getDecision()) {
case txn::CommitDecision::kCommit:
attrs.add("terminationCause", "committed");
attrs.add("commitTimestamp", decision.getCommitTimestamp()->toBSON());
break;
case txn::CommitDecision::kAbort:
attrs.add("terminationCause", "aborted");
attrs.add("terminationDetails", *decision.getAbortStatus());
break;
default:
MONGO_UNREACHABLE;
};
attrs.add("numParticipants", _participants->size());
auto tickSource = _serviceContext->getTickSource();
auto curTick = tickSource->getTicks();
const auto& singleTransactionCoordinatorStats =
_transactionCoordinatorMetricsObserver->getSingleTransactionCoordinatorStats();
BSONObjBuilder stepDurations;
stepDurations.append("writingParticipantListMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWritingParticipantListDuration(
tickSource, curTick)));
stepDurations.append(
"waitingForVotesMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWaitingForVotesDuration(tickSource, curTick)));
stepDurations.append(
"writingDecisionMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWritingDecisionDuration(tickSource, curTick)));
stepDurations.append("waitingForDecisionAcksMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWaitingForDecisionAcksDuration(
tickSource, curTick)));
stepDurations.append("deletingCoordinatorDocMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getDeletingCoordinatorDocDuration(
tickSource, curTick)));
attrs.add("stepDurations", stepDurations.obj());
// Total duration of the commit coordination. Logged at the end of the line for consistency
// with slow command logging. Note that this is reported in milliseconds while the step
// durations are reported in microseconds.
attrs.add(
"duration",
duration_cast<Milliseconds>(
singleTransactionCoordinatorStats.getTwoPhaseCommitDuration(tickSource, curTick)));
LOGV2(51804, "two-phase commit", attrs);
}
std::string TransactionCoordinator::_twoPhaseCommitInfoForLog(
const txn::CoordinatorCommitDecision& decision) const {
StringBuilder s;
s << "two-phase commit";
BSONObjBuilder parametersBuilder;
BSONObjBuilder lsidBuilder(parametersBuilder.subobjStart("lsid"));
_lsid.serialize(&lsidBuilder);
lsidBuilder.doneFast();
parametersBuilder.append("txnNumber", _txnNumberAndRetryCounter.getTxnNumber());
parametersBuilder.append("txnRetryCounter", *_txnNumberAndRetryCounter.getTxnRetryCounter());
s << " parameters:" << parametersBuilder.obj().toString();
switch (decision.getDecision()) {
case txn::CommitDecision::kCommit:
s << ", terminationCause:committed";
s << ", commitTimestamp: " << decision.getCommitTimestamp()->toString();
break;
case txn::CommitDecision::kAbort:
s << ", terminationCause:aborted";
s << ", terminationDetails: " << *decision.getAbortStatus();
break;
default:
MONGO_UNREACHABLE;
};
s << ", numParticipants:" << _participants->size();
auto tickSource = _serviceContext->getTickSource();
auto curTick = tickSource->getTicks();
const auto& singleTransactionCoordinatorStats =
_transactionCoordinatorMetricsObserver->getSingleTransactionCoordinatorStats();
BSONObjBuilder stepDurations;
stepDurations.append("writingParticipantListMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWritingParticipantListDuration(
tickSource, curTick)));
stepDurations.append(
"waitingForVotesMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWaitingForVotesDuration(tickSource, curTick)));
stepDurations.append(
"writingDecisionMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWritingDecisionDuration(tickSource, curTick)));
stepDurations.append("waitingForDecisionAcksMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getWaitingForDecisionAcksDuration(
tickSource, curTick)));
stepDurations.append("deletingCoordinatorDocMicros",
durationCount<Microseconds>(
singleTransactionCoordinatorStats.getDeletingCoordinatorDocDuration(
tickSource, curTick)));
s << ", stepDurations:" << stepDurations.obj();
// Total duration of the commit coordination. Logged at the end of the line for consistency with
// slow command logging.
// Note that this is reported in milliseconds while the step durations are reported in
// microseconds.
s << " "
<< duration_cast<Milliseconds>(
singleTransactionCoordinatorStats.getTwoPhaseCommitDuration(tickSource, curTick));
return s.str();
}
TransactionCoordinator::Step TransactionCoordinator::getStep() const {
stdx::lock_guard<Latch> lk(_mutex);
return _step;
}
void TransactionCoordinator::reportState(BSONObjBuilder& parent) const {
BSONObjBuilder doc;
TickSource* tickSource = _serviceContext->getTickSource();
TickSource::Tick currentTick = tickSource->getTicks();
stdx::lock_guard<Latch> lk(_mutex);
BSONObjBuilder lsidBuilder(doc.subobjStart("lsid"));
_lsid.serialize(&lsidBuilder);
lsidBuilder.doneFast();
doc.append("txnNumber", _txnNumberAndRetryCounter.getTxnNumber());
doc.append("txnRetryCounter", *_txnNumberAndRetryCounter.getTxnRetryCounter());
if (_participants) {
doc.append("numParticipants", static_cast<long long>(_participants->size()));
}
doc.append("state", toString(_step));
const auto& singleStats =
_transactionCoordinatorMetricsObserver->getSingleTransactionCoordinatorStats();
singleStats.reportMetrics(doc, tickSource, currentTick);
singleStats.reportLastClient(parent);
if (_decision)
doc.append("decision", _decision->toBSON());
doc.append("deadline", _deadline);
parent.append("desc", "transaction coordinator");
parent.append("twoPhaseCommitCoordinator", doc.obj());
}
std::string TransactionCoordinator::toString(Step step) const {
switch (step) {
case Step::kInactive:
return "inactive";
case Step::kWritingParticipantList:
return "writingParticipantList";
case Step::kWaitingForVotes:
return "waitingForVotes";
case Step::kWritingDecision:
return "writingDecision";
case Step::kWaitingForDecisionAcks:
return "waitingForDecisionAck";
case Step::kDeletingCoordinatorDoc:
return "deletingCoordinatorDoc";
default:
MONGO_UNREACHABLE;
}
}
void TransactionCoordinator::_updateAssociatedClient(Client* client) {
stdx::lock_guard<Latch> lk(_mutex);
_transactionCoordinatorMetricsObserver->updateLastClientInfo(client);
}
} // namespace mongo
|