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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
/**
* 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::kReplication
#include "mongo/platform/basic.h"
#include "mongo/base/checked_cast.h"
#include "mongo/client/dbclient_connection.h"
#include "mongo/client/replica_set_monitor.h"
#include "mongo/client/replica_set_monitor_manager.h"
#include "mongo/db/client.h"
#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/cloner_utils.h"
#include "mongo/db/repl/data_replicator_external_state.h"
#include "mongo/db/repl/oplog_applier.h"
#include "mongo/db/repl/oplog_buffer_collection.h"
#include "mongo/db/repl/oplog_entry.h"
#include "mongo/db/repl/read_concern_args.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/repl_server_parameters_gen.h"
#include "mongo/db/repl/tenant_migration_recipient_entry_helpers.h"
#include "mongo/db/repl/tenant_migration_recipient_service.h"
#include "mongo/db/repl/tenant_migration_state_machine_gen.h"
#include "mongo/db/repl/wait_for_majority_service.h"
#include "mongo/db/session_txn_record_gen.h"
#include "mongo/db/write_concern_options.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/get_status_from_command_result.h"
namespace mongo {
namespace repl {
namespace {
constexpr StringData kOplogBufferPrefix = "repl.migration.oplog_"_sd;
} // namespace
// A convenient place to set test-specific parameters.
MONGO_FAIL_POINT_DEFINE(pauseBeforeRunTenantMigrationRecipientInstance);
// Fails before waiting for the state doc to be majority replicated.
MONGO_FAIL_POINT_DEFINE(failWhilePersistingTenantMigrationRecipientInstanceStateDoc);
MONGO_FAIL_POINT_DEFINE(fpAfterPersistingTenantMigrationRecipientInstanceStateDoc);
MONGO_FAIL_POINT_DEFINE(fpAfterConnectingTenantMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(fpAfterRetrievingStartOpTimesMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(fpAfterStartingOplogFetcherMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(setTenantMigrationRecipientInstanceHostTimeout);
MONGO_FAIL_POINT_DEFINE(pauseAfterRetrievingLastTxnMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(fpAfterCollectionClonerDone);
MONGO_FAIL_POINT_DEFINE(fpAfterStartingOplogApplierMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(fpAfterDataConsistentMigrationRecipientInstance);
MONGO_FAIL_POINT_DEFINE(hangBeforeTaskCompletion);
namespace {
// We never restart just the oplog fetcher. If a failure occurs, we restart the whole state machine
// and recover from there. So the restart decision is always "no".
class OplogFetcherRestartDecisionTenantMigration
: public OplogFetcher::OplogFetcherRestartDecision {
public:
~OplogFetcherRestartDecisionTenantMigration(){};
bool shouldContinue(OplogFetcher* fetcher, Status status) final {
return false;
}
void fetchSuccessful(OplogFetcher* fetcher) final {}
};
// The oplog fetcher requires some of the methods in DataReplicatorExternalState to operate.
class DataReplicatorExternalStateTenantMigration : public DataReplicatorExternalState {
public:
// The oplog fetcher is passed its executor directly and does not use the one from the
// DataReplicatorExternalState.
executor::TaskExecutor* getTaskExecutor() const final {
MONGO_UNREACHABLE;
}
std::shared_ptr<executor::TaskExecutor> getSharedTaskExecutor() const final {
MONGO_UNREACHABLE;
}
// The oplog fetcher uses the current term and opTime to inform the sync source of term changes.
// As the term on the donor and the term on the recipient have nothing to do with each other,
// we do not want to do that.
OpTimeWithTerm getCurrentTermAndLastCommittedOpTime() final {
return {OpTime::kUninitializedTerm, OpTime()};
}
// Tenant migration does not require the metadata from the oplog query.
void processMetadata(const rpc::ReplSetMetadata& replMetadata,
rpc::OplogQueryMetadata oqMetadata) final {}
// Tenant migration does not change sync source depending on metadata.
ChangeSyncSourceAction shouldStopFetching(const HostAndPort& source,
const rpc::ReplSetMetadata& replMetadata,
const rpc::OplogQueryMetadata& oqMetadata,
const OpTime& previousOpTimeFetched,
const OpTime& lastOpTimeFetched) final {
return ChangeSyncSourceAction::kContinueSyncing;
}
// The oplog fetcher should never call the rest of the methods.
std::unique_ptr<OplogBuffer> makeInitialSyncOplogBuffer(OperationContext* opCtx) const final {
MONGO_UNREACHABLE;
}
std::unique_ptr<OplogApplier> makeOplogApplier(
OplogBuffer* oplogBuffer,
OplogApplier::Observer* observer,
ReplicationConsistencyMarkers* consistencyMarkers,
StorageInterface* storageInterface,
const OplogApplier::Options& options,
ThreadPool* writerPool) final {
MONGO_UNREACHABLE;
};
virtual StatusWith<ReplSetConfig> getCurrentConfig() const final {
MONGO_UNREACHABLE;
}
};
} // namespace
TenantMigrationRecipientService::TenantMigrationRecipientService(ServiceContext* serviceContext)
: PrimaryOnlyService(serviceContext) {}
StringData TenantMigrationRecipientService::getServiceName() const {
return kTenantMigrationRecipientServiceName;
}
NamespaceString TenantMigrationRecipientService::getStateDocumentsNS() const {
return NamespaceString::kTenantMigrationRecipientsNamespace;
}
ThreadPool::Limits TenantMigrationRecipientService::getThreadPoolLimits() const {
// TODO SERVER-50669: This will be replaced by a tunable server parameter.
return ThreadPool::Limits();
}
std::shared_ptr<PrimaryOnlyService::Instance> TenantMigrationRecipientService::constructInstance(
BSONObj initialStateDoc) const {
return std::make_shared<TenantMigrationRecipientService::Instance>(this, initialStateDoc);
}
TenantMigrationRecipientService::Instance::Instance(
const TenantMigrationRecipientService* recipientService, BSONObj stateDoc)
: PrimaryOnlyService::TypedInstance<Instance>(),
_recipientService(recipientService),
_stateDoc(TenantMigrationRecipientDocument::parse(IDLParserErrorContext("recipientStateDoc"),
stateDoc)),
_tenantId(_stateDoc.getTenantId().toString()),
_migrationUuid(_stateDoc.getId()),
_donorConnectionString(_stateDoc.getDonorConnectionString().toString()),
_readPreference(_stateDoc.getReadPreference()) {}
Status TenantMigrationRecipientService::Instance::checkIfOptionsConflict(
const TenantMigrationRecipientDocument& requestedStateDoc) const {
invariant(requestedStateDoc.getId() == _migrationUuid);
if (requestedStateDoc.getTenantId() == _tenantId &&
requestedStateDoc.getDonorConnectionString() == _donorConnectionString &&
requestedStateDoc.getReadPreference().equals(_readPreference)) {
return Status::OK();
}
return Status(ErrorCodes::ConflictingOperationInProgress,
str::stream() << "Requested options for tenant migration doesn't match"
<< " the active migration options, migrationId: " << _migrationUuid
<< ", tenantId: " << _tenantId
<< ", connectionString: " << _donorConnectionString
<< ", readPreference: " << _readPreference.toString()
<< ", requested options:" << requestedStateDoc.toBSON());
}
OpTime TenantMigrationRecipientService::Instance::waitUntilMigrationReachesConsistentState(
OperationContext* opCtx) const {
return _dataConsistentPromise.getFuture().get(opCtx);
}
OpTime TenantMigrationRecipientService::Instance::waitUntilTimestampIsMajorityCommitted(
OperationContext* opCtx, const Timestamp& donorTs) const {
// This gives assurance that _tenantOplogApplier pointer won't be empty.
_dataSyncStartedPromise.getFuture().get(opCtx);
auto getWaitOpTimeFuture = [&]() {
stdx::lock_guard lk(_mutex);
// Sanity checks.
invariant(_tenantOplogApplier);
auto state = _stateDoc.getState();
uassert(ErrorCodes::IllegalOperation,
str::stream()
<< "Failed to wait for the donor timestamp to be majority committed due to"
"conflicting tenant migration state, migration uuid: "
<< getMigrationUUID() << " , current state: "
<< TenantMigrationRecipientState_serializer(state) << " , expected state: "
<< TenantMigrationRecipientState_serializer(
TenantMigrationRecipientStateEnum::kConsistent)
<< ".",
state == TenantMigrationRecipientStateEnum::kConsistent);
return _tenantOplogApplier->getNotificationForOpTime(
OpTime(donorTs, OpTime::kUninitializedTerm));
};
auto donorRecipientOpTimePair = getWaitOpTimeFuture().get(opCtx);
// Wait for the read recipient optime to be majority committed.
WaitForMajorityService::get(opCtx->getServiceContext())
.waitUntilMajority(donorRecipientOpTimePair.recipientOpTime)
.get(opCtx);
return donorRecipientOpTimePair.donorOpTime;
}
std::unique_ptr<DBClientConnection> TenantMigrationRecipientService::Instance::_connectAndAuth(
const HostAndPort& serverAddress, StringData applicationName, BSONObj authParams) {
std::string errMsg;
auto clientBase = ConnectionString(serverAddress).connect(applicationName, errMsg);
if (!clientBase) {
LOGV2_ERROR(4880400,
"Failed to connect to migration donor",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"serverAddress"_attr = serverAddress,
"applicationName"_attr = applicationName,
"error"_attr = errMsg);
uasserted(ErrorCodes::HostNotFound, errMsg);
}
// ConnectionString::connect() always returns a DBClientConnection in a unique_ptr of
// DBClientBase type.
std::unique_ptr<DBClientConnection> client(
checked_cast<DBClientConnection*>(clientBase.release()));
if (!authParams.isEmpty()) {
client->auth(authParams);
} else {
// Tenant migration in production should always require auth.
uassert(4880405, "No auth data provided to tenant migration", getTestCommandsEnabled());
}
return client;
}
SemiFuture<TenantMigrationRecipientService::Instance::ConnectionPair>
TenantMigrationRecipientService::Instance::_createAndConnectClients() {
LOGV2_DEBUG(4880401,
1,
"Recipient migration service connecting clients",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"connectionString"_attr = _donorConnectionString,
"readPreference"_attr = _readPreference,
"authParams"_attr = redact(_authParams));
auto connectionStringWithStatus = ConnectionString::parse(_donorConnectionString);
if (!connectionStringWithStatus.isOK()) {
LOGV2_ERROR(4880403,
"Failed to parse connection string",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"connectionString"_attr = _donorConnectionString,
"error"_attr = connectionStringWithStatus.getStatus());
return SemiFuture<ConnectionPair>::makeReady(connectionStringWithStatus.getStatus());
}
auto connectionString = std::move(connectionStringWithStatus.getValue());
const auto& servers = connectionString.getServers();
stdx::lock_guard lk(_mutex);
_donorReplicaSetMonitor = ReplicaSetMonitor::createIfNeeded(
connectionString.getSetName(), std::set<HostAndPort>(servers.begin(), servers.end()));
Milliseconds findHostTimeout = ReplicaSetMonitorInterface::kDefaultFindHostTimeout;
setTenantMigrationRecipientInstanceHostTimeout.execute([&](const BSONObj& data) {
findHostTimeout = Milliseconds(data["findHostTimeoutMillis"].safeNumberLong());
});
return _donorReplicaSetMonitor->getHostOrRefresh(_readPreference, findHostTimeout)
.thenRunOn(**_scopedExecutor)
.then([this](const HostAndPort& serverAddress) {
auto applicationName =
"TenantMigrationRecipient_" + getTenantId() + "_" + getMigrationUUID().toString();
auto client = _connectAndAuth(serverAddress, applicationName, _authParams);
applicationName += "_fetcher";
auto oplogFetcherClient = _connectAndAuth(serverAddress, applicationName, _authParams);
return ConnectionPair(std::move(client), std::move(oplogFetcherClient));
})
.onError([this](const Status& status) -> SemiFuture<ConnectionPair> {
LOGV2_ERROR(4880404,
"Connecting to donor failed",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"error"_attr = status);
// Make sure we don't end up with a partially initialized set of connections.
stdx::lock_guard lk(_mutex);
_client = nullptr;
_oplogFetcherClient = nullptr;
return status;
})
.semi();
}
SemiFuture<void> TenantMigrationRecipientService::Instance::_initializeStateDoc(WithLock) {
// If the instance state is not 'kUninitialized', then the instance is restarted by step
// up. So, skip persisting the state doc. And, PrimaryOnlyService::onStepUp() waits for
// majority commit of the primary no-op oplog entry written by the node in the newer
// term before scheduling the Instance::run(). So, it's also safe to assume that
// instance's state document written in an older term on disk won't get rolled back for
// step up case.
if (_stateDoc.getState() != TenantMigrationRecipientStateEnum::kUninitialized) {
return SemiFuture<void>::makeReady();
}
auto uniqueOpCtx = cc().makeOperationContext();
auto opCtx = uniqueOpCtx.get();
LOGV2_DEBUG(5081400,
2,
"Recipient migration service initializing state document",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"connectionString"_attr = _donorConnectionString,
"readPreference"_attr = _readPreference);
// Persist the state doc before starting the data sync.
_stateDoc.setState(TenantMigrationRecipientStateEnum::kStarted);
{
Lock::ExclusiveLock stateDocInsertLock(
opCtx, opCtx->lockState(), _recipientService->_stateDocInsertMutex);
uassertStatusOK(tenantMigrationRecipientEntryHelpers::insertStateDoc(opCtx, _stateDoc));
}
if (MONGO_unlikely(failWhilePersistingTenantMigrationRecipientInstanceStateDoc.shouldFail())) {
LOGV2(4878500, "Persisting state doc failed due to fail point enabled.");
uassert(ErrorCodes::NotWritablePrimary,
"Persisting state doc failed - "
"'failWhilePersistingTenantMigrationRecipientInstanceStateDoc' fail point active",
false);
}
// Wait for the state doc to be majority replicated to make sure that the state doc doesn't
// rollback.
auto insertOpTime = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
return WaitForMajorityService::get(opCtx->getServiceContext())
.waitUntilMajority(insertOpTime)
.semi();
}
void TenantMigrationRecipientService::Instance::_getStartOpTimesFromDonor(WithLock) {
// Get the last oplog entry at the read concern majority optime in the remote oplog. It
// does not matter which tenant it is for.
auto oplogOpTimeFields =
BSON(OplogEntry::kTimestampFieldName << 1 << OplogEntry::kTermFieldName << 1);
auto lastOplogEntry1Bson =
_client->findOne(NamespaceString::kRsOplogNamespace.ns(),
Query().sort("$natural", -1),
&oplogOpTimeFields,
QueryOption_SlaveOk,
ReadConcernArgs(ReadConcernLevel::kMajorityReadConcern).toBSONInner());
uassert(4880601, "Found no entries in the remote oplog", !lastOplogEntry1Bson.isEmpty());
LOGV2_DEBUG(4880600,
2,
"Found last oplog entry at read concern majority optime on remote node",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = _stateDoc.getTenantId(),
"lastOplogEntry"_attr = lastOplogEntry1Bson);
auto lastOplogEntry1OpTime = uassertStatusOK(OpTime::parseFromOplogEntry(lastOplogEntry1Bson));
// Get the optime of the earliest transaction that was open at the read concern majority optime
// As with the last oplog entry, it does not matter that this may be for a different tenant; an
// optime that is too early does not result in incorrect behavior.
const auto preparedState = DurableTxnState_serializer(DurableTxnStateEnum::kPrepared);
const auto inProgressState = DurableTxnState_serializer(DurableTxnStateEnum::kInProgress);
auto transactionTableOpTimeFields = BSON(SessionTxnRecord::kStartOpTimeFieldName << 1);
auto earliestOpenTransactionBson = _client->findOne(
NamespaceString::kSessionTransactionsTableNamespace.ns(),
QUERY("state" << BSON("$in" << BSON_ARRAY(preparedState << inProgressState)))
.sort(SessionTxnRecord::kStartOpTimeFieldName.toString(), 1),
&transactionTableOpTimeFields,
QueryOption_SlaveOk,
ReadConcernArgs(ReadConcernLevel::kMajorityReadConcern).toBSONInner());
LOGV2_DEBUG(4880602,
2,
"Transaction table entry for earliest transaction that was open at the read "
"concern majority optime on remote node (may be empty)",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = _stateDoc.getTenantId(),
"earliestOpenTransaction"_attr = earliestOpenTransactionBson);
pauseAfterRetrievingLastTxnMigrationRecipientInstance.pauseWhileSet();
// We need to fetch the last oplog entry both before and after getting the transaction
// table entry, as otherwise there is a potential race where we may try to apply
// a commit for which we have not fetched a previous transaction oplog entry.
auto lastOplogEntry2Bson =
_client->findOne(NamespaceString::kRsOplogNamespace.ns(),
Query().sort("$natural", -1),
&oplogOpTimeFields,
QueryOption_SlaveOk,
ReadConcernArgs(ReadConcernLevel::kMajorityReadConcern).toBSONInner());
uassert(4880603, "Found no entries in the remote oplog", !lastOplogEntry2Bson.isEmpty());
LOGV2_DEBUG(4880604,
2,
"Found last oplog entry at the read concern majority optime (after reading txn "
"table) on remote node",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = _stateDoc.getTenantId(),
"lastOplogEntry"_attr = lastOplogEntry2Bson);
auto lastOplogEntry2OpTime = uassertStatusOK(OpTime::parseFromOplogEntry(lastOplogEntry2Bson));
_stateDoc.setStartApplyingOpTime(lastOplogEntry2OpTime);
OpTime startFetchingOpTime = lastOplogEntry1OpTime;
if (!earliestOpenTransactionBson.isEmpty()) {
auto startOpTimeField =
earliestOpenTransactionBson[SessionTxnRecord::kStartOpTimeFieldName];
if (startOpTimeField.isABSONObj()) {
startFetchingOpTime = OpTime::parse(startOpTimeField.Obj());
}
}
_stateDoc.setStartFetchingOpTime(startFetchingOpTime);
}
void TenantMigrationRecipientService::Instance::_startOplogFetcher() {
auto opCtx = cc().makeOperationContext();
OplogBufferCollection::Options options;
options.peekCacheSize = static_cast<size_t>(tenantMigrationOplogBufferPeekCacheSize);
options.dropCollectionAtStartup = false;
options.dropCollectionAtShutdown = false;
options.useTemporaryCollection = false;
NamespaceString oplogBufferNs(NamespaceString::kConfigDb,
kOplogBufferPrefix + getMigrationUUID().toString());
stdx::lock_guard lk(_mutex);
invariant(_stateDoc.getStartFetchingOpTime());
_donorOplogBuffer = std::make_unique<OplogBufferCollection>(
StorageInterface::get(opCtx.get()), oplogBufferNs, options);
_donorOplogBuffer->startup(opCtx.get());
_dataReplicatorExternalState = std::make_unique<DataReplicatorExternalStateTenantMigration>();
_donorOplogFetcher = (*_createOplogFetcherFn)(
(**_scopedExecutor).get(),
*_stateDoc.getStartFetchingOpTime(),
_oplogFetcherClient->getServerHostAndPort(),
// The config is only used for setting the awaitData timeout; the defaults are fine.
ReplSetConfig::parse(BSON("_id"
<< "dummy"
<< "version" << 1 << "members" << BSONObj())),
std::make_unique<OplogFetcherRestartDecisionTenantMigration>(),
// We do not need to check the rollback ID.
ReplicationProcess::kUninitializedRollbackId,
false /* requireFresherSyncSource */,
_dataReplicatorExternalState.get(),
[this](OplogFetcher::Documents::const_iterator first,
OplogFetcher::Documents::const_iterator last,
const OplogFetcher::DocumentsInfo& info) {
return _enqueueDocuments(first, last, info);
},
[this](const Status& s, int rbid) { _oplogFetcherCallback(s); },
tenantMigrationOplogFetcherBatchSize,
OplogFetcher::StartingPoint::kEnqueueFirstDoc,
_getOplogFetcherFilter(),
ReadConcernArgs(repl::ReadConcernLevel::kMajorityReadConcern),
"TenantOplogFetcher_" + getTenantId() + "_" + getMigrationUUID().toString());
_donorOplogFetcher->setConnection(std::move(_oplogFetcherClient));
uassertStatusOK(_donorOplogFetcher->startup());
}
Status TenantMigrationRecipientService::Instance::_enqueueDocuments(
OplogFetcher::Documents::const_iterator begin,
OplogFetcher::Documents::const_iterator end,
const OplogFetcher::DocumentsInfo& info) {
invariant(_donorOplogBuffer);
if (info.toApplyDocumentCount == 0)
return Status::OK();
auto opCtx = cc().makeOperationContext();
// Wait for enough space.
_donorOplogBuffer->waitForSpace(opCtx.get(), info.toApplyDocumentBytes);
// Buffer docs for later application.
_donorOplogBuffer->push(opCtx.get(), begin, end);
return Status::OK();
}
void TenantMigrationRecipientService::Instance::_oplogFetcherCallback(Status oplogFetcherStatus) {
// The oplog fetcher is normally canceled when migration is done; any other error
// indicates failure.
if (oplogFetcherStatus.isOK()) {
// Oplog fetcher status of "OK" means the stopReplProducer failpoint is set. Migration
// cannot continue in this state so force a failure.
LOGV2_ERROR(
4881205,
"Recipient migration service oplog fetcher stopped due to stopReplProducer failpoint",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID());
interrupt({ErrorCodes::Error(4881206),
"Recipient migration service oplog fetcher stopped due to stopReplProducer "
"failpoint"});
} else if (oplogFetcherStatus.code() != ErrorCodes::CallbackCanceled) {
LOGV2_ERROR(4881204,
"Recipient migration service oplog fetcher failed",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"error"_attr = oplogFetcherStatus);
interrupt(oplogFetcherStatus);
}
}
namespace {
constexpr std::int32_t stopFailPointErrorCode = 4880402;
} // namespace
void TenantMigrationRecipientService::Instance::_stopOrHangOnFailPoint(FailPoint* fp) {
fp->executeIf(
[&](const BSONObj& data) {
LOGV2(4881103,
"Tenant migration recipient instance: failpoint enabled",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"name"_attr = fp->getName(),
"args"_attr = data);
if (data["action"].str() == "hang") {
fp->pauseWhileSet();
} else {
uasserted(stopFailPointErrorCode,
"Skipping remaining processing due to fail point");
}
},
[&](const BSONObj& data) {
auto action = data["action"].str();
return (action == "hang" || action == "stop");
});
}
bool TenantMigrationRecipientService::Instance::_isCloneCompletedMarkerSet(WithLock) const {
return _stateDoc.getCloneFinishedOpTime().has_value();
}
Future<void> TenantMigrationRecipientService::Instance::_startTenantAllDatabaseCloner(WithLock lk) {
// If the state is data consistent, do not start the cloner.
if (_isCloneCompletedMarkerSet(lk)) {
return {Future<void>::makeReady()};
}
auto opCtx = cc().makeOperationContext();
_tenantAllDatabaseCloner =
std::make_unique<TenantAllDatabaseCloner>(_sharedData.get(),
_client->getServerHostAndPort(),
_client.get(),
repl::StorageInterface::get(opCtx.get()),
_writerPool.get(),
_tenantId);
LOGV2_DEBUG(4881100,
2,
"Starting TenantAllDatabaseCloner",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = getTenantId());
auto [startClonerFuture, startCloner] =
_tenantAllDatabaseCloner->runOnExecutorEvent((**_scopedExecutor).get());
// runOnExecutorEvent ensures the future is not ready unless an error has occurred.
if (startClonerFuture.isReady()) {
auto status = startClonerFuture.getNoThrow();
uassertStatusOK(status);
MONGO_UNREACHABLE;
}
// Signal the cloner to start.
(*_scopedExecutor)->signalEvent(startCloner);
return std::move(startClonerFuture);
}
SemiFuture<void> TenantMigrationRecipientService::Instance::_onCloneSuccess() {
stdx::lock_guard lk(_mutex);
// PrimaryOnlyService::onStepUp() before starting instance makes sure that the state doc
// is majority committed, so we can also skip waiting for it to be majority replicated.
if (_isCloneCompletedMarkerSet(lk)) {
return SemiFuture<void>::makeReady();
}
auto opCtx = cc().makeOperationContext();
{
stdx::lock_guard<TenantMigrationSharedData> sharedDatalk(*_sharedData);
_stateDoc.setDataConsistentStopOpTime(_sharedData->getLastVisibleOpTime(sharedDatalk));
}
_stateDoc.setCloneFinishedOpTime(
repl::ReplicationCoordinator::get(opCtx.get())->getMyLastAppliedOpTime());
uassertStatusOK(tenantMigrationRecipientEntryHelpers::updateStateDoc(opCtx.get(), _stateDoc));
return WaitForMajorityService::get(opCtx->getServiceContext())
.waitUntilMajority(repl::ReplClientInfo::forClient(cc()).getLastOp())
.semi();
}
SemiFuture<void> TenantMigrationRecipientService::Instance::_getDataConsistentFuture() {
stdx::lock_guard lk(_mutex);
// PrimaryOnlyService::onStepUp() before starting instance makes sure that the state doc
// is majority committed, so we can also skip waiting for it to be majority replicated.
if (_stateDoc.getState() == TenantMigrationRecipientStateEnum::kConsistent) {
return SemiFuture<void>::makeReady();
}
return _tenantOplogApplier
->getNotificationForOpTime(_stateDoc.getDataConsistentStopOpTime().get())
.thenRunOn(**_scopedExecutor)
.then([this](TenantOplogApplier::OpTimePair donorRecipientOpTime) {
auto opCtx = cc().makeOperationContext();
stdx::lock_guard lk(_mutex);
// Persist the state that tenant migration instance has reached
// consistent state.
_stateDoc.setState(TenantMigrationRecipientStateEnum::kConsistent);
uassertStatusOK(
tenantMigrationRecipientEntryHelpers::updateStateDoc(opCtx.get(), _stateDoc));
return WaitForMajorityService::get(opCtx->getServiceContext())
.waitUntilMajority(repl::ReplClientInfo::forClient(cc()).getLastOp());
})
.semi();
}
void TenantMigrationRecipientService::Instance::_shutdownComponents(WithLock lk) {
if (_writerPool) {
_writerPool->shutdown();
}
if (_client) {
// interrupts running tenant cloner.
_client->shutdownAndDisallowReconnect();
}
if (_oplogFetcherClient) {
// interrupts running tenant oplog fetcher.
_oplogFetcherClient->shutdownAndDisallowReconnect();
}
if (_tenantOplogApplier) {
_tenantOplogApplier->shutdown();
}
if (_donorOplogBuffer) {
auto opCtx = cc().makeOperationContext();
_donorOplogBuffer->shutdown(opCtx.get());
}
}
void TenantMigrationRecipientService::Instance::interrupt(Status status) {
invariant(!status.isOK());
stdx::lock_guard lk(_mutex);
if (_taskState.isInterrupted() || _taskState.isDone()) {
// nothing to do.
return;
}
_shutdownComponents(lk);
// If the task is running, then setting promise result will be taken care by the main task
// continuation chain.
if (_taskState.isNotStarted()) {
_dataSyncStartedPromise.setError(status);
_dataConsistentPromise.setError(status);
_completionPromise.setError(status);
}
_taskState.setState(TaskState::kInterrupted, status);
}
void TenantMigrationRecipientService::Instance::_cleanupOnTaskCompletion(Status status) {
stdx::lock_guard lk(_mutex);
_shutdownComponents(lk);
if (_donorOplogFetcher) {
_donorOplogFetcher->shutdown();
_donorOplogFetcher->join();
}
if (_writerPool) {
_writerPool->join();
}
if (status.isOK()) {
// All intermediary promise should have been fulfilled already.
invariant(_dataSyncStartedPromise.getFuture().isReady() &&
_dataConsistentPromise.getFuture().isReady());
_completionPromise.emplaceValue();
}
auto setPromiseErrorifNotReady = [&](auto& promise) {
if (promise.getFuture().isReady()) {
return;
}
if (status.code() == stopFailPointErrorCode) {
promise.emplaceValue();
} else {
promise.setError(status);
}
};
setPromiseErrorifNotReady(_dataSyncStartedPromise);
setPromiseErrorifNotReady(_dataConsistentPromise);
setPromiseErrorifNotReady(_completionPromise);
_taskState.setState(TaskState::kDone);
}
BSONObj TenantMigrationRecipientService::Instance::_getOplogFetcherFilter() const {
// Either the namespace belongs to the tenant, or it's an applyOps in the admin namespace
// and the first operation belongs to the tenant. A transaction with mixed tenant/non-tenant
// operations should not be possible and will fail in the TenantOplogApplier.
//
// Commit of prepared transactions is not handled here; we'd need to handle them in the applier
// by allowing all commits through here and ignoring those not corresponding to active
// transactions.
BSONObj namespaceRegex = ClonerUtils::makeTenantDatabaseRegex(getTenantId());
return BSON("$or" << BSON_ARRAY(BSON("ns" << namespaceRegex)
<< BSON("ns"
<< "admin.$cmd"
<< "o.applyOps.0.ns" << namespaceRegex)));
}
void TenantMigrationRecipientService::Instance::run(
std::shared_ptr<executor::ScopedTaskExecutor> executor) noexcept {
_scopedExecutor = executor;
pauseBeforeRunTenantMigrationRecipientInstance.pauseWhileSet();
LOGV2(4879607,
"Starting tenant migration recipient instance: ",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = getTenantId(),
"connectionString"_attr = _donorConnectionString,
"readPreference"_attr = _readPreference);
ExecutorFuture(**executor)
.then([this] {
stdx::lock_guard lk(_mutex);
// Instance task can be started only once for the current term on a primary.
invariant(!_taskState.isDone());
// If the task state is interrupted, then don't start the task.
if (_taskState.isInterrupted()) {
uassertStatusOK(_taskState.getInterruptStatus());
}
_taskState.setState(TaskState::kRunning);
return _initializeStateDoc(lk);
})
.then([this] {
_stopOrHangOnFailPoint(&fpAfterPersistingTenantMigrationRecipientInstanceStateDoc);
return _createAndConnectClients();
})
.then([this](ConnectionPair ConnectionPair) {
stdx::lock_guard lk(_mutex);
if (_taskState.isInterrupted()) {
uassertStatusOK(_taskState.getInterruptStatus());
}
// interrupt() called after this code block will interrupt the cloner, oplog applier and
// fetcher.
_client = std::move(ConnectionPair.first);
_oplogFetcherClient = std::move(ConnectionPair.second);
// Create the writer pool and shared data.
_writerPool = makeTenantMigrationWriterPool();
_sharedData = std::make_unique<TenantMigrationSharedData>(
getGlobalServiceContext()->getFastClockSource());
})
.then([this] {
_stopOrHangOnFailPoint(&fpAfterConnectingTenantMigrationRecipientInstance);
stdx::lock_guard lk(_mutex);
// The instance is marked as garbage collect if the migration is either
// committed or aborted on donor side. So, don't start the recipient task if the
// instance state doc is marked for garbage collect.
uassert(ErrorCodes::IllegalOperation,
str::stream() << "Can't start the data sync as the state doc is already marked "
"for garbage collect for migration uuid: "
<< getMigrationUUID(),
!_stateDoc.getExpireAt());
_getStartOpTimesFromDonor(lk);
auto opCtx = cc().makeOperationContext();
uassertStatusOK(
tenantMigrationRecipientEntryHelpers::updateStateDoc(opCtx.get(), _stateDoc));
return WaitForMajorityService::get(opCtx->getServiceContext())
.waitUntilMajority(repl::ReplClientInfo::forClient(cc()).getLastOp());
})
.then([this] {
_stopOrHangOnFailPoint(&fpAfterRetrievingStartOpTimesMigrationRecipientInstance);
_startOplogFetcher();
})
.then([this] {
_stopOrHangOnFailPoint(&fpAfterStartingOplogFetcherMigrationRecipientInstance);
stdx::lock_guard lk(_mutex);
// Create the oplog applier but do not start it yet.
invariant(_stateDoc.getStartApplyingOpTime());
LOGV2_DEBUG(4881202,
1,
"Recipient migration service creating oplog applier",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"startApplyingOpTime"_attr = *_stateDoc.getStartApplyingOpTime());
_tenantOplogApplier =
std::make_unique<TenantOplogApplier>(_migrationUuid,
_tenantId,
*_stateDoc.getStartApplyingOpTime(),
_donorOplogBuffer.get(),
**_scopedExecutor,
_writerPool.get());
// Start the cloner.
auto clonerFuture = _startTenantAllDatabaseCloner(lk);
// Signal that the data sync has started successfully.
_dataSyncStartedPromise.emplaceValue();
return clonerFuture;
})
.then([this] { return _onCloneSuccess(); })
.then([this] {
_stopOrHangOnFailPoint(&fpAfterCollectionClonerDone);
LOGV2_DEBUG(4881200,
1,
"Recipient migration service starting oplog applier",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID());
uassertStatusOK(_tenantOplogApplier->startup());
_stopOrHangOnFailPoint(&fpAfterStartingOplogApplierMigrationRecipientInstance);
return _getDataConsistentFuture();
})
.then([this] {
stdx::lock_guard lk(_mutex);
LOGV2_DEBUG(4881101,
1,
"Tenant migration recipient instance is in consistent state",
"migrationId"_attr = getMigrationUUID(),
"tenantId"_attr = getTenantId(),
"donorConsistentOpTime"_attr = _stateDoc.getDataConsistentStopOpTime());
_dataConsistentPromise.emplaceValue(_stateDoc.getDataConsistentStopOpTime().get());
})
.then([this] {
_stopOrHangOnFailPoint(&fpAfterDataConsistentMigrationRecipientInstance);
// wait for oplog applier to complete/stop.
// The oplog applier does not exit normally; it must be shut down externally,
// e.g. by recipientForgetMigration.
return _tenantOplogApplier->getNotificationForOpTime(OpTime::max());
})
.getAsync([this](StatusOrStatusWith<TenantOplogApplier::OpTimePair> applierStatus) {
// We don't need the final optime from the oplog applier.
Status status = applierStatus.getStatus();
{
// If we were interrupted during oplog application, replace oplog application
// status with error state.
stdx::lock_guard lk(_mutex);
if ((status.isOK() || ErrorCodes::isCancelationError(status)) &&
_taskState.isInterrupted()) {
// We get an "OK" result when the stopReplProducer failpoint is set. This also
// cancels the migration. We will have already logged this in
// _oplogFetcherCallback()
if (!status.isOK()) {
LOGV2(4881207,
"Migration completed with both error and interrupt",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"completionStatus"_attr = status,
"interruptStatus"_attr = _taskState.getInterruptStatus());
}
status = _taskState.getInterruptStatus();
}
}
LOGV2(4878501,
"Tenant migration recipient instance: Data sync completed.",
"tenantId"_attr = getTenantId(),
"migrationId"_attr = getMigrationUUID(),
"error"_attr = status);
if (MONGO_unlikely(hangBeforeTaskCompletion.shouldFail())) {
LOGV2(4881102,
"Tenant migration recipient instance: hangBeforeTaskCompletion failpoint "
"enabled");
hangBeforeTaskCompletion.pauseWhileSet();
}
_cleanupOnTaskCompletion(status);
});
}
const UUID& TenantMigrationRecipientService::Instance::getMigrationUUID() const {
return _migrationUuid;
}
const std::string& TenantMigrationRecipientService::Instance::getTenantId() const {
return _tenantId;
}
} // namespace repl
} // namespace mongo
|