summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/resharding/resharding_metrics.cpp
blob: a18067a00fe1d7335d5bc3ea3b160683055baa53 (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
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
687
688
689
690
691
692
693
694
/**
 *    Copyright (C) 2020-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::kResharding

#include <algorithm>
#include <memory>

#include "mongo/db/s/resharding/resharding_metrics.h"
#include "mongo/logv2/log.h"
#include "mongo/platform/compiler.h"
#include "mongo/util/with_alignment.h"

namespace mongo {

namespace {
constexpr auto kAnotherOperationInProgress = "Another operation is in progress";
constexpr auto kNoOperationInProgress = "No operation is in progress";

constexpr auto kTotalOps = "countReshardingOperations";
constexpr auto kSuccessfulOps = "countReshardingSuccessful";
constexpr auto kFailedOps = "countReshardingFailures";
constexpr auto kCanceledOps = "countReshardingCanceled";
constexpr auto kOpTimeElapsed = "totalOperationTimeElapsedSecs";
constexpr auto kOpTimeRemaining = "remainingOperationTimeEstimatedSecs";
constexpr auto kDocumentsToCopy = "approxDocumentsToCopy";
constexpr auto kDocumentsCopied = "documentsCopied";
constexpr auto kBytesToCopy = "approxBytesToCopy";
constexpr auto kBytesCopied = "bytesCopied";
constexpr auto kCopyTimeElapsed = "totalCopyTimeElapsedSecs";
constexpr auto kOplogsFetched = "oplogEntriesFetched";
constexpr auto kOplogsApplied = "oplogEntriesApplied";
constexpr auto kApplyTimeElapsed = "totalApplyTimeElapsedSecs";
constexpr auto kWritesDuringCritialSection = "countWritesDuringCriticalSection";
constexpr auto kCriticalSectionTimeElapsed = "totalCriticalSectionTimeElapsedSecs";
constexpr auto kCoordinatorState = "coordinatorState";
constexpr auto kDonorState = "donorState";
constexpr auto kRecipientState = "recipientState";
constexpr auto kOpStatus = "opStatus";
constexpr auto kLastOpEndingChunkImbalance = "lastOpEndingChunkImbalance";
constexpr auto kOpCounters = "opcounters";

using MetricsPtr = std::unique_ptr<ReshardingMetrics>;

const auto getMetrics = ServiceContext::declareDecoration<MetricsPtr>();

const auto reshardingMetricsRegisterer = ServiceContext::ConstructorActionRegisterer{
    "ReshardingMetrics",
    [](ServiceContext* ctx) { getMetrics(ctx) = std::make_unique<ReshardingMetrics>(ctx); }};

/**
 * Given a constant rate of time per unit of work:
 *    totalTime / totalWork == elapsedTime / elapsedWork
 * Solve for remaining time.
 *    remainingTime := totalTime - elapsedTime
 *                  == (totalWork * (elapsedTime / elapsedWork)) - elapsedTime
 *                  == elapsedTime * (totalWork / elapsedWork - 1)
 */
Milliseconds remainingTime(Milliseconds elapsedTime, double elapsedWork, double totalWork) {
    elapsedWork = std::min(elapsedWork, totalWork);
    double remainingMsec = 1.0 * elapsedTime.count() * (totalWork / elapsedWork - 1);
    return Milliseconds(Milliseconds::rep(remainingMsec));
}

static StringData serializeState(boost::optional<RecipientStateEnum> e) {
    return RecipientState_serializer(*e);
}

static StringData serializeState(boost::optional<DonorStateEnum> e) {
    return DonorState_serializer(*e);
}

static StringData serializeState(boost::optional<CoordinatorStateEnum> e) {
    return CoordinatorState_serializer(*e);
}

// Enables resharding to distinguish the ops it does to keep up with client writes from the
// client workload tracked in the globalOpCounters.
class ReshardingOpCounters {
public:
    void gotInsert() noexcept {
        _checkWrap(&ReshardingOpCounters::_insert);
    }
    void gotUpdate() noexcept {
        _checkWrap(&ReshardingOpCounters::_update);
    }
    void gotDelete() noexcept {
        _checkWrap(&ReshardingOpCounters::_delete);
    }

    BSONObj getObj() const noexcept {
        BSONObjBuilder b;
        b.append("insert", _insert.loadRelaxed());
        b.append("update", _update.loadRelaxed());
        b.append("delete", _delete.loadRelaxed());
        return b.obj();
    }

private:
    // Increment member `counter` by 1, resetting all counters if it was > 2^60.
    void _checkWrap(CacheAligned<AtomicWord<long long>> ReshardingOpCounters::*counter) {
        static constexpr auto maxCount = 1LL << 60;
        auto oldValue = (this->*counter).fetchAndAddRelaxed(1);
        if (oldValue > maxCount - 1) {
            LOGV2(5776000,
                  "ReshardingOpCounters exceeded maximum value, resetting all to 0",
                  "insert"_attr = _insert.loadRelaxed(),
                  "update"_attr = _update.loadRelaxed(),
                  "delete"_attr = _delete.loadRelaxed());
            _insert.store(0);
            _update.store(0);
            _delete.store(0);
        }
    }

    CacheAligned<AtomicWord<long long>> _insert;
    CacheAligned<AtomicWord<long long>> _update;
    CacheAligned<AtomicWord<long long>> _delete;
};

// Allows tracking elapsed time for the resharding operation and its sub operations (e.g.,
// applying oplog entries).
class TimeInterval {
public:
    void start(Date_t d) noexcept {
        invariant(!_start, "Already started");
        _start = d;
    }

    void end(Date_t d) noexcept {
        invariant(_start, "Not started");
        invariant(!_end, "Already stopped");
        _end = d;
    }

    // TODO Remove this function once all metrics classes can start from stepup.
    void forceEnd(Date_t d) noexcept {
        if (!_start)
            _start = d;
        end(d);
    }

    Milliseconds duration(Date_t now) const noexcept {
        return !_start ? Milliseconds(0) : ((!_end ? now : *_end) - *_start);
    }

private:
    boost::optional<Date_t> _start;
    boost::optional<Date_t> _end;
};

}  // namespace

class ReshardingMetrics::OperationMetrics {
public:
    void appendCurrentOpMetrics(BSONObjBuilder*, Role, Date_t now) const;

    boost::optional<Milliseconds> remainingOperationTime(Date_t now) const;

    void gotInsert() noexcept;
    void gotUpdate() noexcept;
    void gotDelete() noexcept;

    TimeInterval runningOperation;
    ReshardingOperationStatusEnum opStatus = ReshardingOperationStatusEnum::kInactive;

    TimeInterval copyingDocuments;
    int64_t documentsToCopy = 0;
    int64_t documentsCopied = 0;
    int64_t bytesToCopy = 0;
    int64_t bytesCopied = 0;

    TimeInterval applyingOplogEntries;
    int64_t oplogEntriesFetched = 0;
    int64_t oplogEntriesApplied = 0;

    TimeInterval inCriticalSection;
    int64_t writesDuringCriticalSection = 0;

    int64_t chunkImbalanceCount = 0;

    // The ops done by resharding to keep up with the client writes.
    ReshardingOpCounters opCounters;

    boost::optional<DonorStateEnum> donorState;
    boost::optional<RecipientStateEnum> recipientState;
    boost::optional<CoordinatorStateEnum> coordinatorState;
};

void ReshardingMetrics::OperationMetrics::gotInsert() noexcept {
    opCounters.gotInsert();
}

void ReshardingMetrics::OperationMetrics::gotUpdate() noexcept {
    opCounters.gotUpdate();
}

void ReshardingMetrics::OperationMetrics::gotDelete() noexcept {
    opCounters.gotDelete();
}

boost::optional<Milliseconds> ReshardingMetrics::OperationMetrics::remainingOperationTime(
    Date_t now) const {
    if (recipientState > RecipientStateEnum::kCloning && oplogEntriesFetched == 0) {
        return Milliseconds(0);
    }

    if (oplogEntriesApplied > 0 && oplogEntriesFetched > 0) {
        // All fetched oplogEntries must be applied. Some of them already have been.
        return remainingTime(
            applyingOplogEntries.duration(now), oplogEntriesApplied, oplogEntriesFetched);
    }
    if (bytesCopied > 0 && bytesToCopy > 0) {
        // Until the time to apply batches of oplog entries is measured, we assume that applying all
        // of them will take as long as copying did.
        return remainingTime(copyingDocuments.duration(now), bytesCopied, 2 * bytesToCopy);
    }
    return {};
}

void ReshardingMetrics::OperationMetrics::appendCurrentOpMetrics(BSONObjBuilder* bob,
                                                                 Role role,
                                                                 Date_t now) const {
    auto getElapsedTime = [&](const TimeInterval& interval) -> int64_t {
        return durationCount<Seconds>(interval.duration(now));
    };

    const auto remainingMsec = remainingOperationTime(now);

    bob->append(kOpTimeElapsed, getElapsedTime(runningOperation));

    switch (role) {
        case Role::kDonor:
            bob->append(kWritesDuringCritialSection, writesDuringCriticalSection);
            bob->append(kCriticalSectionTimeElapsed, getElapsedTime(inCriticalSection));
            bob->append(kDonorState, serializeState(donorState));
            bob->append(kOpStatus, ReshardingOperationStatus_serializer(opStatus));
            break;
        case Role::kRecipient:
            bob->append(kOpTimeRemaining,
                        !remainingMsec ? int64_t{-1} /** -1 is a specified integer null value */
                                       : durationCount<Seconds>(*remainingMsec));
            bob->append(kDocumentsToCopy, documentsToCopy);
            bob->append(kDocumentsCopied, documentsCopied);
            bob->append(kBytesToCopy, bytesToCopy);
            bob->append(kBytesCopied, bytesCopied);
            bob->append(kCopyTimeElapsed, getElapsedTime(copyingDocuments));

            bob->append(kOplogsFetched, oplogEntriesFetched);
            bob->append(kOplogsApplied, oplogEntriesApplied);
            bob->append(kApplyTimeElapsed, getElapsedTime(applyingOplogEntries));
            bob->append(kRecipientState, serializeState(recipientState));
            bob->append(kOpStatus, ReshardingOperationStatus_serializer(opStatus));
            break;
        case Role::kCoordinator:
            bob->append(kCoordinatorState, serializeState(coordinatorState));
            bob->append(kOpStatus, ReshardingOperationStatus_serializer(opStatus));
            break;
        default:
            MONGO_UNREACHABLE;
    }
}

ReshardingMetrics* ReshardingMetrics::get(ServiceContext* ctx) noexcept {
    return getMetrics(ctx).get();
}

void ReshardingMetrics::onStart(Role role, Date_t runningOperationStartTime) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    // TODO Re-add this invariant once all breaking test cases have been fixed.
    // invariant(!_currentOp.has_value(), kAnotherOperationInProgress);

    if (!_currentOp) {
        // Only incremement _started if this is the first time resharding metrics is being invoked
        // for this resharding operation, and we're not restoring the PrimaryOnlyService from disk.
        _started++;
    }

    // Create a new operation and record the time it started.
    _emplaceCurrentOpForRole(role, runningOperationStartTime);
}

void ReshardingMetrics::onCompletion(Role role,
                                     ReshardingOperationStatusEnum status,
                                     Date_t runningOperationEndTime) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    // TODO Re-add this invariant once all breaking test cases have been fixed. Add invariant that
    // role being completed is a role that is in progress.
    // invariant(_currentOp.has_value(), kNoOperationInProgress);

    if (_currentOp->donorState && _currentOp->recipientState) {
        switch (role) {
            case Role::kDonor:
                _currentOp->donorState = boost::none;
                break;
            case Role::kRecipient:
                _currentOp->recipientState = boost::none;
                break;
            default:
                MONGO_UNREACHABLE;
        }

        return;
    }

    switch (status) {
        case ReshardingOperationStatusEnum::kSuccess:
            _succeeded++;
            break;
        case ReshardingOperationStatusEnum::kFailure:
            _failed++;
            break;
        case ReshardingOperationStatusEnum::kCanceled:
            _canceled++;
            break;
        default:
            MONGO_UNREACHABLE;
    }

    _currentOp->runningOperation.end(runningOperationEndTime);

    // Reset current op metrics.
    _currentOp = nullptr;
}

void ReshardingMetrics::onStepUp(Role role) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    _emplaceCurrentOpForRole(role, boost::none);

    // TODO SERVER-53914 Implement coordinator metrics rehydration.

    // TODO SERVER-57094 Resume the runningOperation duration from a timestamp stored on disk
    // instead of starting from the current time.
}

void ReshardingMetrics::onStepUp(DonorStateEnum state, ReshardingDonorMetrics donorMetrics) {
    stdx::lock_guard<Latch> lk(_mutex);
    auto operationRuntime = donorMetrics.getOperationRuntime();
    _emplaceCurrentOpForRole(
        Role::kDonor, operationRuntime.has_value() ? operationRuntime->getStart() : boost::none);
    _currentOp->donorState = state;

    if (auto criticalSectionTimeInterval = donorMetrics.getCriticalSection();
        criticalSectionTimeInterval.has_value() &&
        criticalSectionTimeInterval->getStart().has_value()) {
        _currentOp->inCriticalSection.start(criticalSectionTimeInterval->getStart().get());

        if (auto stopTime = criticalSectionTimeInterval->getStop(); stopTime.has_value())
            _currentOp->inCriticalSection.forceEnd(stopTime.get());
    }
}

void ReshardingMetrics::onStepDown(Role role) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    if (_currentOp && _currentOp->donorState && _currentOp->recipientState) {
        switch (role) {
            case Role::kDonor:
                _currentOp->donorState = boost::none;
                break;
            case Role::kRecipient:
                _currentOp->recipientState = boost::none;
                break;
            default:
                MONGO_UNREACHABLE;
        }
    } else {
        _currentOp = nullptr;
    }
}

void ReshardingMetrics::_emplaceCurrentOpForRole(
    Role role, boost::optional<Date_t> runningOperationStartTime) noexcept {
    // Invariants in this function ensure that the only multi-role state allowed is a combination
    // of donor and recipient.
    if (!_currentOp) {
        _currentOp = std::make_unique<OperationMetrics>();
        _currentOp->runningOperation.start(runningOperationStartTime
                                               ? *runningOperationStartTime
                                               : _svcCtx->getFastClockSource()->now());
        _currentOp->opStatus = ReshardingOperationStatusEnum::kRunning;
    } else {
        invariant(role != Role::kCoordinator, kAnotherOperationInProgress);
        invariant(!_currentOp->coordinatorState, kAnotherOperationInProgress);
    }

    switch (role) {
        case Role::kCoordinator:
            _currentOp->coordinatorState.emplace(CoordinatorStateEnum::kUnused);
            break;
        case Role::kDonor:
            invariant(!_currentOp->donorState, kAnotherOperationInProgress);
            _currentOp->donorState.emplace(DonorStateEnum::kUnused);
            break;
        case Role::kRecipient:
            invariant(!_currentOp->recipientState, kAnotherOperationInProgress);
            _currentOp->recipientState.emplace(RecipientStateEnum::kUnused);
            break;
        default:
            MONGO_UNREACHABLE
    }
}

void ReshardingMetrics::setDonorState(DonorStateEnum state) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    invariant(_currentOp, kNoOperationInProgress);

    const auto oldState = std::exchange(_currentOp->donorState, state);
    invariant(oldState != state);
}

void ReshardingMetrics::setRecipientState(RecipientStateEnum state) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    invariant(_currentOp, kNoOperationInProgress);

    const auto oldState = std::exchange(_currentOp->recipientState, state);
    invariant(oldState != state);
}

void ReshardingMetrics::setCoordinatorState(CoordinatorStateEnum state) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    invariant(_currentOp, kNoOperationInProgress);
    _currentOp->coordinatorState = state;
}

template <typename T>
static bool checkState(T state, std::initializer_list<T> validStates) {
    invariant(validStates.size());
    if (std::find(validStates.begin(), validStates.end(), state) != validStates.end())
        return true;

    std::stringstream ss;
    StringData sep = "";
    for (auto state : validStates) {
        ss << sep << serializeState(state);
        sep = ", "_sd;
    }

    LOGV2_FATAL_CONTINUE(5553300,
                         "Invalid resharding state",
                         "state"_attr = serializeState(state),
                         "valid"_attr = ss.str());
    return false;
}

void ReshardingMetrics::setDocumentsToCopy(int64_t documents, int64_t bytes) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    invariant(_currentOp, kNoOperationInProgress);
    invariant(_currentOp->recipientState == RecipientStateEnum::kCreatingCollection);

    setDocumentsToCopyForCurrentOp(documents, bytes);
}

void ReshardingMetrics::setDocumentsToCopyForCurrentOp(int64_t documents, int64_t bytes) noexcept {
    invariant(_currentOp, kNoOperationInProgress);

    _currentOp->documentsToCopy = documents;
    _currentOp->bytesToCopy = bytes;
}

void ReshardingMetrics::setLastReshardChunkImbalanceCount(int64_t newCount) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);

    invariant(_currentOp, kNoOperationInProgress);
    invariant(_currentOp->coordinatorState);

    _cumulativeOp->chunkImbalanceCount = newCount;
}

void ReshardingMetrics::onDocumentsCopied(int64_t documents, int64_t bytes) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    if (!_currentOp)
        return;

    invariant(checkState(*_currentOp->recipientState,
                         {RecipientStateEnum::kCloning, RecipientStateEnum::kError}));

    onDocumentsCopiedForCurrentOp(documents, bytes);
    _cumulativeOp->documentsCopied += documents;
    _cumulativeOp->bytesCopied += bytes;
}

void ReshardingMetrics::onDocumentsCopiedForCurrentOp(int64_t documents, int64_t bytes) noexcept {
    invariant(_currentOp, kNoOperationInProgress);

    _currentOp->documentsCopied += documents;
    _currentOp->bytesCopied += bytes;
}

void ReshardingMetrics::gotInsert() noexcept {
    _cumulativeOp->gotInsert();
}

void ReshardingMetrics::gotUpdate() noexcept {
    _cumulativeOp->gotUpdate();
}

void ReshardingMetrics::gotDelete() noexcept {
    _cumulativeOp->gotDelete();
}

void ReshardingMetrics::startCopyingDocuments(Date_t start) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->copyingDocuments.start(start);
}

void ReshardingMetrics::endCopyingDocuments(Date_t end) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->copyingDocuments.forceEnd(end);
}

void ReshardingMetrics::startApplyingOplogEntries(Date_t start) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->applyingOplogEntries.start(start);
}

void ReshardingMetrics::endApplyingOplogEntries(Date_t end) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->applyingOplogEntries.forceEnd(end);
}

void ReshardingMetrics::enterCriticalSection(Date_t start) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->inCriticalSection.start(start);
}

void ReshardingMetrics::leaveCriticalSection(Date_t end) {
    stdx::lock_guard<Latch> lk(_mutex);
    _currentOp->inCriticalSection.forceEnd(end);
}

void ReshardingMetrics::onOplogEntriesFetched(int64_t entries) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    if (!_currentOp)
        return;

    invariant(checkState(
        *_currentOp->recipientState,
        {RecipientStateEnum::kCloning, RecipientStateEnum::kApplying, RecipientStateEnum::kError}));

    onOplogEntriesFetchedForCurrentOp(entries);
    _cumulativeOp->oplogEntriesFetched += entries;
}

void ReshardingMetrics::onOplogEntriesFetchedForCurrentOp(int64_t entries) noexcept {
    invariant(_currentOp, kNoOperationInProgress);

    _currentOp->oplogEntriesFetched += entries;
}

void ReshardingMetrics::onOplogEntriesApplied(int64_t entries) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    if (!_currentOp)
        return;

    invariant(checkState(*_currentOp->recipientState,
                         {RecipientStateEnum::kApplying, RecipientStateEnum::kError}));

    onOplogEntriesAppliedForCurrentOp(entries);
    _cumulativeOp->oplogEntriesApplied += entries;
}

void ReshardingMetrics::onOplogEntriesAppliedForCurrentOp(int64_t entries) noexcept {
    invariant(_currentOp, kNoOperationInProgress);

    _currentOp->oplogEntriesApplied += entries;
}

void ReshardingMetrics::onWriteDuringCriticalSection(int64_t writes) noexcept {
    stdx::lock_guard<Latch> lk(_mutex);
    if (!_currentOp)
        return;

    invariant(checkState(*_currentOp->donorState,
                         {DonorStateEnum::kPreparingToBlockWrites,
                          DonorStateEnum::kBlockingWrites,
                          DonorStateEnum::kError}));

    onWriteDuringCriticalSectionForCurrentOp(writes);
    _cumulativeOp->writesDuringCriticalSection += writes;
}

void ReshardingMetrics::onWriteDuringCriticalSectionForCurrentOp(int64_t writes) noexcept {
    invariant(_currentOp, kNoOperationInProgress);

    _currentOp->writesDuringCriticalSection += writes;
}

void ReshardingMetrics::serializeCurrentOpMetrics(BSONObjBuilder* bob, Role role) const {
    stdx::lock_guard<Latch> lk(_mutex);
    if (_currentOp)
        _currentOp->appendCurrentOpMetrics(bob, role, _now());
}

BSONObj ReshardingMetrics::reportForCurrentOp(const ReporterOptions& options) const noexcept {
    auto roleName = [](Role role) {
        switch (role) {
            case Role::kDonor:
                return "Donor"_sd;
            case Role::kRecipient:
                return "Recipient"_sd;
            case Role::kCoordinator:
                return "Coordinator"_sd;
        }
        MONGO_UNREACHABLE;
    };

    BSONObjBuilder bob;
    bob.append("type", "op");
    bob.append(
        "desc",
        fmt::format("Resharding{}Service {}", roleName(options.role), options.id.toString()));
    bob.append("op", "command");
    bob.append("ns", options.nss.toString());

    {
        BSONObjBuilder originating{bob.subobjStart("originatingCommand")};
        originating.append("reshardCollection", options.nss.toString());
        originating.append("key", options.shardKey);
        originating.append("unique", options.unique);
        BSONObjBuilder{originating.subobjStart("collation")}.append("locale", "simple");
    }

    serializeCurrentOpMetrics(&bob, options.role);

    return bob.obj();
}

boost::optional<Milliseconds> ReshardingMetrics::getOperationElapsedTime() const {
    stdx::lock_guard<Latch> lk(_mutex);
    if (!_currentOp)
        return boost::none;
    return _currentOp->runningOperation.duration(_now());
}

boost::optional<Milliseconds> ReshardingMetrics::getOperationRemainingTime() const {
    stdx::lock_guard<Latch> lk(_mutex);
    if (_currentOp)
        return _currentOp->remainingOperationTime(_now());
    return boost::none;
}

void ReshardingMetrics::serializeCumulativeOpMetrics(BSONObjBuilder* bob) const {
    stdx::lock_guard<Latch> lk(_mutex);

    bob->append(kTotalOps, _started);
    bob->append(kSuccessfulOps, _succeeded);
    bob->append(kFailedOps, _failed);
    bob->append(kCanceledOps, _canceled);

    const auto& ops = *_cumulativeOp;
    bob->append(kDocumentsCopied, ops.documentsCopied);
    bob->append(kBytesCopied, ops.bytesCopied);
    bob->append(kOplogsApplied, ops.oplogEntriesApplied);
    bob->append(kWritesDuringCritialSection, ops.writesDuringCriticalSection);
    bob->append(kOplogsFetched, ops.oplogEntriesFetched);
    bob->append(kLastOpEndingChunkImbalance, ops.chunkImbalanceCount);
    bob->append(kOpCounters, ops.opCounters.getObj());
}

Date_t ReshardingMetrics::_now() const {
    return _svcCtx->getFastClockSource()->now();
}

ReshardingMetrics::ReshardingMetrics(ServiceContext* svcCtx)
    : _svcCtx{svcCtx}, _cumulativeOp{std::make_unique<OperationMetrics>()} {}

ReshardingMetrics::~ReshardingMetrics() = default;

}  // namespace mongo