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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
/**
* 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::kSharding
#include "mongo/platform/basic.h"
#include "mongo/db/s/config/sharding_catalog_manager.h"
#include <tuple>
#include "mongo/db/auth/authorization_session_impl.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/error_labels.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/ops/write_ops.h"
#include "mongo/db/query/query_request_helper.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/s/balancer/type_migration.h"
#include "mongo/db/s/sharding_util.h"
#include "mongo/db/s/type_lockpings.h"
#include "mongo/db/s/type_locks.h"
#include "mongo/db/vector_clock.h"
#include "mongo/logv2/log.h"
#include "mongo/s/catalog/config_server_version.h"
#include "mongo/s/catalog/sharding_catalog_client.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/catalog/type_collection.h"
#include "mongo/s/catalog/type_config_version.h"
#include "mongo/s/catalog/type_shard.h"
#include "mongo/s/catalog/type_tags.h"
#include "mongo/s/client/shard_registry.h"
#include "mongo/s/database_version.h"
#include "mongo/s/grid.h"
#include "mongo/s/sharded_collections_ddl_parameters_gen.h"
#include "mongo/s/write_ops/batched_command_request.h"
#include "mongo/s/write_ops/batched_command_response.h"
#include "mongo/stdx/unordered_map.h"
#include "mongo/transport/service_entry_point.h"
#include "mongo/util/log_and_backoff.h"
namespace mongo {
namespace {
const WriteConcernOptions kNoWaitWriteConcern(1, WriteConcernOptions::SyncMode::UNSET, Seconds(0));
// This value is initialized only if the node is running as a config server
const auto getShardingCatalogManager =
ServiceContext::declareDecoration<boost::optional<ShardingCatalogManager>>();
OpMsg runCommandInLocalTxn(OperationContext* opCtx,
StringData db,
bool startTransaction,
TxnNumber txnNumber,
BSONObj cmdObj) {
BSONObjBuilder bob(std::move(cmdObj));
if (startTransaction) {
bob.append("startTransaction", true);
}
bob.append("autocommit", false);
bob.append(OperationSessionInfo::kTxnNumberFieldName, txnNumber);
BSONObjBuilder lsidBuilder(bob.subobjStart("lsid"));
opCtx->getLogicalSessionId()->serialize(&bob);
lsidBuilder.doneFast();
return OpMsg::parseOwned(
opCtx->getServiceContext()
->getServiceEntryPoint()
->handleRequest(opCtx,
OpMsgRequest::fromDBAndBody(db.toString(), bob.obj()).serialize())
.get()
.response);
}
void startTransactionWithNoopFind(OperationContext* opCtx,
const NamespaceString& nss,
TxnNumber txnNumber) {
FindCommand findCommand(nss);
findCommand.setBatchSize(0);
findCommand.setSingleBatch(true);
auto res =
runCommandInLocalTxn(
opCtx, nss.db(), true /*startTransaction*/, txnNumber, findCommand.toBSON(BSONObj()))
.body;
uassertStatusOK(getStatusFromCommandResult(res));
}
BSONObj commitOrAbortTransaction(OperationContext* opCtx,
TxnNumber txnNumber,
std::string cmdName) {
// Swap out the clients in order to get a fresh opCtx. Previous operations in this transaction
// that have been run on this opCtx would have set the timeout in the locker on the opCtx, but
// commit should not have a lock timeout.
auto newClient = getGlobalServiceContext()->makeClient("ShardingCatalogManager");
{
stdx::lock_guard<Client> lk(*newClient);
newClient->setSystemOperationKillableByStepdown(lk);
}
AlternativeClientRegion acr(newClient);
auto newOpCtx = cc().makeOperationContext();
newOpCtx->setAlwaysInterruptAtStepDownOrUp();
AuthorizationSession::get(newOpCtx.get()->getClient())
->grantInternalAuthorization(newOpCtx.get()->getClient());
newOpCtx.get()->setLogicalSessionId(opCtx->getLogicalSessionId().get());
newOpCtx.get()->setTxnNumber(txnNumber);
BSONObjBuilder bob;
bob.append(cmdName, true);
bob.append("autocommit", false);
bob.append(OperationSessionInfo::kTxnNumberFieldName, txnNumber);
bob.append(WriteConcernOptions::kWriteConcernField, WriteConcernOptions::Majority);
BSONObjBuilder lsidBuilder(bob.subobjStart("lsid"));
newOpCtx->getLogicalSessionId()->serialize(&bob);
lsidBuilder.doneFast();
const auto cmdObj = bob.obj();
const auto replyOpMsg =
OpMsg::parseOwned(newOpCtx->getServiceContext()
->getServiceEntryPoint()
->handleRequest(newOpCtx.get(),
OpMsgRequest::fromDBAndBody(
NamespaceString::kAdminDb.toString(), cmdObj)
.serialize())
.get()
.response);
return replyOpMsg.body;
}
// Runs commit for the transaction with 'txnNumber'.
auto commitTransaction(OperationContext* opCtx, TxnNumber txnNumber) {
auto response = commitOrAbortTransaction(opCtx, txnNumber, "commitTransaction");
return std::make_tuple(getStatusFromCommandResult(response),
getWriteConcernStatusFromCommandResult(response));
}
// Runs abort for the transaction with 'txnNumber'.
void abortTransaction(OperationContext* opCtx, TxnNumber txnNumber) {
auto response = commitOrAbortTransaction(opCtx, txnNumber, "abortTransaction");
// It is safe to ignore write concern errors in the presence of a NoSuchTransaction command
// error because the transaction being aborted was both generated by and run locally on this
// replica set primary. The NoSuchTransaction decision couldn't end up being rolled back.
auto status = getStatusFromCommandResult(response);
if (status.code() != ErrorCodes::NoSuchTransaction) {
uassertStatusOK(status);
uassertStatusOK(getWriteConcernStatusFromCommandResult(response));
}
}
// Updates documents in the config db using DBDirectClient
void updateConfigDocumentDBDirect(OperationContext* opCtx,
const mongo::NamespaceString& nss,
const BSONObj& query,
const BSONObj& update,
bool upsert,
bool multi) {
invariant(nss.db() == "config");
DBDirectClient client(opCtx);
write_ops::Update updateOp(nss, [&] {
write_ops::UpdateOpEntry u;
u.setQ(query);
u.setU(write_ops::UpdateModification::parseFromClassicUpdate(update));
u.setMulti(multi);
u.setUpsert(upsert);
return std::vector{u};
}());
updateOp.setWriteCommandBase([] {
write_ops::WriteCommandBase base;
base.setOrdered(false);
return base;
}());
auto commandResult =
client.runCommand(OpMsgRequest::fromDBAndBody(nss.db(), updateOp.toBSON({})));
uassertStatusOK([&] {
BatchedCommandResponse response;
std::string unusedErrmsg;
response.parseBSON(
commandResult->getCommandReply(),
&unusedErrmsg); // Return value intentionally ignored, because response.toStatus() will
// contain any errors in more detail
return response.toStatus();
}());
uassertStatusOK(getWriteConcernStatusFromCommandResult(commandResult->getCommandReply()));
}
Status createNsIndexesForConfigChunks(OperationContext* opCtx) {
const bool unique = true;
auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
Status result = configShard->createIndexOnConfig(
opCtx, ChunkType::ConfigNS, BSON(ChunkType::ns() << 1 << ChunkType::min() << 1), unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_min_1 index on config db");
}
result = configShard->createIndexOnConfig(
opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::ns() << 1 << ChunkType::shard() << 1 << ChunkType::min() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_shard_1_min_1 index on config db");
}
result =
configShard->createIndexOnConfig(opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::ns() << 1 << ChunkType::lastmod() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_lastmod_1 index on config db");
}
return Status::OK();
}
Status createUuidIndexesForConfigChunks(OperationContext* opCtx) {
const bool unique = true;
auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
Status result = configShard->createIndexOnConfig(
opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::collectionUUID() << 1 << ChunkType::min() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create uuid_1_min_1 index on config db");
}
result = configShard->createIndexOnConfig(
opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::collectionUUID() << 1 << ChunkType::shard() << 1 << ChunkType::min() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create uuid_1_shard_1_min_1 index on config db");
}
result = configShard->createIndexOnConfig(
opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::collectionUUID() << 1 << ChunkType::lastmod() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create uuid_1_lastmod_1 index on config db");
}
return Status::OK();
}
} // namespace
void ShardingCatalogManager::create(ServiceContext* serviceContext,
std::unique_ptr<executor::TaskExecutor> addShardExecutor) {
auto& shardingCatalogManager = getShardingCatalogManager(serviceContext);
invariant(!shardingCatalogManager);
shardingCatalogManager.emplace(serviceContext, std::move(addShardExecutor));
}
void ShardingCatalogManager::clearForTests(ServiceContext* serviceContext) {
auto& shardingCatalogManager = getShardingCatalogManager(serviceContext);
invariant(shardingCatalogManager);
shardingCatalogManager.reset();
}
ShardingCatalogManager* ShardingCatalogManager::get(ServiceContext* serviceContext) {
auto& shardingCatalogManager = getShardingCatalogManager(serviceContext);
invariant(shardingCatalogManager);
return shardingCatalogManager.get_ptr();
}
ShardingCatalogManager* ShardingCatalogManager::get(OperationContext* operationContext) {
return get(operationContext->getServiceContext());
}
ShardingCatalogManager::ShardingCatalogManager(
ServiceContext* serviceContext, std::unique_ptr<executor::TaskExecutor> addShardExecutor)
: _serviceContext(serviceContext),
_executorForAddShard(std::move(addShardExecutor)),
_kShardMembershipLock("shardMembershipLock"),
_kChunkOpLock("chunkOpLock"),
_kZoneOpLock("zoneOpLock") {
startup();
}
ShardingCatalogManager::~ShardingCatalogManager() {
shutDown();
}
void ShardingCatalogManager::startup() {
stdx::lock_guard<Latch> lk(_mutex);
if (_started) {
return;
}
_started = true;
_executorForAddShard->startup();
Grid::get(_serviceContext)
->setCustomConnectionPoolStatsFn(
[this](executor::ConnectionPoolStats* stats) { appendConnectionStats(stats); });
}
void ShardingCatalogManager::shutDown() {
Grid::get(_serviceContext)->setCustomConnectionPoolStatsFn(nullptr);
_executorForAddShard->shutdown();
_executorForAddShard->join();
}
Status ShardingCatalogManager::initializeConfigDatabaseIfNeeded(OperationContext* opCtx) {
{
stdx::lock_guard<Latch> lk(_mutex);
if (_configInitialized) {
return {ErrorCodes::AlreadyInitialized,
"Config database was previously loaded into memory"};
}
}
Status status = _initConfigIndexes(opCtx);
if (!status.isOK()) {
return status;
}
// Make sure to write config.version last since we detect rollbacks of config.version and
// will re-run initializeConfigDatabaseIfNeeded if that happens, but we don't detect rollback
// of the index builds.
status = _initConfigVersion(opCtx);
if (!status.isOK()) {
return status;
}
stdx::lock_guard<Latch> lk(_mutex);
_configInitialized = true;
return Status::OK();
}
void ShardingCatalogManager::discardCachedConfigDatabaseInitializationState() {
stdx::lock_guard<Latch> lk(_mutex);
_configInitialized = false;
}
Status ShardingCatalogManager::_initConfigVersion(OperationContext* opCtx) {
const auto catalogClient = Grid::get(opCtx)->catalogClient();
auto versionStatus =
catalogClient->getConfigVersion(opCtx, repl::ReadConcernLevel::kLocalReadConcern);
if (!versionStatus.isOK()) {
return versionStatus.getStatus();
}
const auto& versionInfo = versionStatus.getValue();
if (versionInfo.getMinCompatibleVersion() > CURRENT_CONFIG_VERSION) {
return {ErrorCodes::IncompatibleShardingConfigVersion,
str::stream() << "current version v" << CURRENT_CONFIG_VERSION
<< " is older than the cluster min compatible v"
<< versionInfo.getMinCompatibleVersion()};
}
if (versionInfo.getCurrentVersion() == UpgradeHistory_EmptyVersion) {
VersionType newVersion;
newVersion.setClusterId(OID::gen());
newVersion.setMinCompatibleVersion(MIN_COMPATIBLE_CONFIG_VERSION);
newVersion.setCurrentVersion(CURRENT_CONFIG_VERSION);
BSONObj versionObj(newVersion.toBSON());
auto insertStatus = catalogClient->insertConfigDocument(
opCtx, VersionType::ConfigNS, versionObj, kNoWaitWriteConcern);
return insertStatus;
}
if (versionInfo.getCurrentVersion() == UpgradeHistory_UnreportedVersion) {
return {ErrorCodes::IncompatibleShardingConfigVersion,
"Assuming config data is old since the version document cannot be found in the "
"config server and it contains databases besides 'local' and 'admin'. "
"Please upgrade if this is the case. Otherwise, make sure that the config "
"server is clean."};
}
if (versionInfo.getCurrentVersion() < CURRENT_CONFIG_VERSION) {
return {ErrorCodes::IncompatibleShardingConfigVersion,
str::stream() << "need to upgrade current cluster version to v"
<< CURRENT_CONFIG_VERSION << "; currently at v"
<< versionInfo.getCurrentVersion()};
}
return Status::OK();
}
Status ShardingCatalogManager::_initConfigIndexes(OperationContext* opCtx) {
const bool unique = true;
auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
if (feature_flags::gShardingFullDDLSupportTimestampedVersion.isEnabled(
serverGlobalParams.featureCompatibility)) {
const auto result = createUuidIndexesForConfigChunks(opCtx);
if (result != Status::OK()) {
return result;
}
} else {
const auto result = createNsIndexesForConfigChunks(opCtx);
if (result != Status::OK()) {
return result;
}
}
Status result = configShard->createIndexOnConfig(
opCtx,
MigrationType::ConfigNS,
BSON(MigrationType::ns() << 1 << MigrationType::min() << 1),
unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_min_1 index on config.migrations");
}
result = configShard->createIndexOnConfig(
opCtx, ShardType::ConfigNS, BSON(ShardType::host() << 1), unique);
if (!result.isOK()) {
return result.withContext("couldn't create host_1 index on config db");
}
result = configShard->createIndexOnConfig(
opCtx, LocksType::ConfigNS, BSON(LocksType::lockID() << 1), !unique);
if (!result.isOK()) {
return result.withContext("couldn't create lock id index on config db");
}
result =
configShard->createIndexOnConfig(opCtx,
LocksType::ConfigNS,
BSON(LocksType::state() << 1 << LocksType::process() << 1),
!unique);
if (!result.isOK()) {
return result.withContext("couldn't create state and process id index on config db");
}
result = configShard->createIndexOnConfig(
opCtx, LockpingsType::ConfigNS, BSON(LockpingsType::ping() << 1), !unique);
if (!result.isOK()) {
return result.withContext("couldn't create lockping ping time index on config db");
}
result = configShard->createIndexOnConfig(
opCtx, TagsType::ConfigNS, BSON(TagsType::ns() << 1 << TagsType::min() << 1), unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_min_1 index on config db");
}
result = configShard->createIndexOnConfig(
opCtx, TagsType::ConfigNS, BSON(TagsType::ns() << 1 << TagsType::tag() << 1), !unique);
if (!result.isOK()) {
return result.withContext("couldn't create ns_1_tag_1 index on config db");
}
return Status::OK();
}
Status ShardingCatalogManager::setFeatureCompatibilityVersionOnShards(OperationContext* opCtx,
const BSONObj& cmdObj) {
// No shards should be added until we have forwarded featureCompatibilityVersion to all shards.
Lock::SharedLock lk(opCtx->lockState(), _kShardMembershipLock);
// We do a direct read of the shards collection with local readConcern so no shards are missed,
// but don't go through the ShardRegistry to prevent it from caching data that may be rolled
// back.
const auto opTimeWithShards = uassertStatusOK(Grid::get(opCtx)->catalogClient()->getAllShards(
opCtx, repl::ReadConcernLevel::kLocalReadConcern));
for (const auto& shardType : opTimeWithShards.value) {
const auto shardStatus =
Grid::get(opCtx)->shardRegistry()->getShard(opCtx, shardType.getName());
if (!shardStatus.isOK()) {
continue;
}
const auto shard = shardStatus.getValue();
auto response = shard->runCommandWithFixedRetryAttempts(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
"admin",
cmdObj,
Shard::RetryPolicy::kIdempotent);
if (!response.isOK()) {
return response.getStatus();
}
if (!response.getValue().commandStatus.isOK()) {
return response.getValue().commandStatus;
}
if (!response.getValue().writeConcernStatus.isOK()) {
return response.getValue().writeConcernStatus;
}
}
return Status::OK();
}
void ShardingCatalogManager::_removePre49LegacyMetadata(OperationContext* opCtx) {
const auto catalogClient = Grid::get(opCtx)->catalogClient();
// Delete all documents which have {dropped: true} from config.collections
uassertStatusOK(
catalogClient->removeConfigDocuments(opCtx,
CollectionType::ConfigNS,
BSON("dropped" << true),
ShardingCatalogClient::kLocalWriteConcern));
// Clear the {dropped:true} and {distributionMode:sharded} fields from config.collections
updateConfigDocumentDBDirect(opCtx,
CollectionType::ConfigNS,
{} /* query */,
BSON("$unset" << BSON("dropped"
<< "")
<< "$unset"
<< BSON("distributionMode"
<< "")),
false /* upsert */,
true /* multi */);
}
void ShardingCatalogManager::upgradeMetadataFor49(OperationContext* opCtx) {
LOGV2(5276704, "Starting metadata upgrade to 4.9");
_removePre49LegacyMetadata(opCtx);
if (feature_flags::gShardingFullDDLSupportTimestampedVersion.isEnabledAndIgnoreFCV()) {
_createDBTimestampsFor49(opCtx);
_upgradeCollectionsAndChunksMetadataFor49(opCtx);
}
LOGV2(5276705, "Successfully upgraded metadata to 4.9");
}
void ShardingCatalogManager::downgradeMetadataToPre49(OperationContext* opCtx) {
LOGV2(5276706, "Starting metadata downgrade to pre 4.9");
if (feature_flags::gShardingFullDDLSupportTimestampedVersion.isEnabledAndIgnoreFCV()) {
_downgradeConfigDatabasesEntriesToPre49(opCtx);
_downgradeCollectionsAndChunksMetadataToPre49(opCtx);
}
LOGV2(5276707, "Successfully downgraded metadata to pre 4.9");
}
void ShardingCatalogManager::_createDBTimestampsFor49(OperationContext* opCtx) {
LOGV2(5258802, "Starting upgrade of config.databases");
auto const catalogCache = Grid::get(opCtx)->catalogCache();
auto const configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
const auto dbDocs =
uassertStatusOK(
configShard->exhaustiveFindOnConfig(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
repl::ReadConcernLevel::kLocalReadConcern,
DatabaseType::ConfigNS,
BSON(DatabaseType::version() + "." + DatabaseVersion::kTimestampFieldName
<< BSON("$exists" << false)),
BSONObj(),
boost::none))
.docs;
for (const auto& doc : dbDocs) {
const DatabaseType db = uassertStatusOK(DatabaseType::fromBSON(doc));
const auto name = db.getName();
auto now = VectorClock::get(opCtx)->getTime();
auto clusterTime = now.clusterTime().asTimestamp();
updateConfigDocumentDBDirect(
opCtx,
DatabaseType::ConfigNS,
BSON(DatabaseType::name << name),
BSON("$set" << BSON(DatabaseType::version() + "." + DatabaseVersion::kTimestampFieldName
<< clusterTime)),
false /* upsert */,
false /* multi*/);
}
// Wait until the last operation is majority-committed
WriteConcernResult ignoreResult;
const auto latestOpTime = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
uassertStatusOK(waitForWriteConcern(
opCtx, latestOpTime, ShardingCatalogClient::kMajorityWriteConcern, &ignoreResult));
// Forcing a refresh of each DB on each shard
const auto shardIds = Grid::get(opCtx)->shardRegistry()->getAllShardIds(opCtx);
const auto fixedExecutor = Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor();
for (const auto& doc : dbDocs) {
const DatabaseType db = uassertStatusOK(DatabaseType::fromBSON(doc));
const auto name = db.getName();
catalogCache->invalidateDatabaseEntry_LINEARIZABLE(name);
sharding_util::tellShardsToRefreshDatabase(opCtx, shardIds, name, fixedExecutor);
}
LOGV2(5258803, "Successfully upgraded config.databases");
}
void ShardingCatalogManager::_downgradeConfigDatabasesEntriesToPre49(OperationContext* opCtx) {
LOGV2(5258806, "Starting downgrade of config.databases");
updateConfigDocumentDBDirect(
opCtx,
DatabaseType::ConfigNS,
{} /* query */,
BSON("$unset" << BSON(DatabaseType::version() + "." + DatabaseVersion::kTimestampFieldName
<< "")),
false /* upsert */,
true /* multi */);
auto const catalogCache = Grid::get(opCtx)->catalogCache();
auto const configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
const auto dbDocs =
uassertStatusOK(
configShard->exhaustiveFindOnConfig(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
repl::ReadConcernLevel::kLocalReadConcern,
DatabaseType::ConfigNS,
BSON(DatabaseType::version() + "." + DatabaseVersion::kTimestampFieldName
<< BSON("$exists" << false)),
BSONObj(),
boost::none))
.docs;
// Wait until the last operation is majority-committed
WriteConcernResult ignoreResult;
const auto latestOpTime = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
uassertStatusOK(waitForWriteConcern(
opCtx, latestOpTime, ShardingCatalogClient::kMajorityWriteConcern, &ignoreResult));
// Forcing a refresh of each DB on each shard
const auto shardIds = Grid::get(opCtx)->shardRegistry()->getAllShardIds(opCtx);
const auto fixedExecutor = Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor();
for (const auto& doc : dbDocs) {
const DatabaseType db = uassertStatusOK(DatabaseType::fromBSON(doc));
const auto name = db.getName();
catalogCache->invalidateDatabaseEntry_LINEARIZABLE(name);
sharding_util::tellShardsToRefreshDatabase(opCtx, shardIds, name, fixedExecutor);
}
LOGV2(5258807, "Successfully downgraded config.databases");
}
void ShardingCatalogManager::_upgradeCollectionsAndChunksMetadataFor49(OperationContext* opCtx) {
LOGV2(5276700, "Starting upgrade of config.collections and config.chunks");
auto const catalogCache = Grid::get(opCtx)->catalogCache();
auto const configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
auto collectionDocs =
uassertStatusOK(
configShard->exhaustiveFindOnConfig(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
repl::ReadConcernLevel::kLocalReadConcern,
CollectionType::ConfigNS,
BSONObj(BSON(CollectionType::kTimestampFieldName << BSON("$exists" << false))),
BSONObj(),
boost::none))
.docs;
stdx::unordered_map<NamespaceString, Timestamp> timestampMap;
// Set timestamp and uuid for all chunks in all collections
for (const auto& doc : collectionDocs) {
const CollectionType coll(doc);
const auto uuid = coll.getUuid();
const auto nss = coll.getNss();
uassert(547900,
str::stream() << "The 'allowMigrations' field of the " << nss
<< " collection must be true",
coll.getAllowMigrations());
const auto now = VectorClock::get(opCtx)->getTime();
const auto newTimestamp = now.clusterTime().asTimestamp();
timestampMap.emplace(nss, newTimestamp);
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
updateConfigDocumentDBDirect(
opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::ns(nss.ns())) /* query */,
BSON("$set" << BSON(ChunkType::timestamp(newTimestamp)
<< ChunkType::collectionUUID() << uuid)) /* update */,
false /* upsert */,
true /* multi */);
}
// Create uuid_* indexes for config.chunks
uassertStatusOK(createUuidIndexesForConfigChunks(opCtx));
// Set timestamp for all collections in config.collections
for (const auto& doc : collectionDocs) {
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
const CollectionType coll(doc);
const auto nss = coll.getNss();
updateConfigDocumentDBDirect(opCtx,
CollectionType::ConfigNS,
BSON(CollectionType::kNssFieldName << nss.ns()) /* query */,
BSON("$set" << BSON(CollectionType::kTimestampFieldName
<< timestampMap.at(nss))) /* update */,
false /* upsert */,
false /* multi */);
}
// Wait until the last operation is majority-committed
WriteConcernResult ignoreResult;
const auto latestOpTime = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
uassertStatusOK(waitForWriteConcern(
opCtx, latestOpTime, ShardingCatalogClient::kMajorityWriteConcern, &ignoreResult));
// Forcing a refresh of each collection on each shard
const auto shardIds = Grid::get(opCtx)->shardRegistry()->getAllShardIds(opCtx);
const auto fixedExecutor = Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor();
for (const auto& doc : collectionDocs) {
const CollectionType coll(doc);
const auto nss = coll.getNss();
catalogCache->invalidateCollectionEntry_LINEARIZABLE(nss);
sharding_util::tellShardsToRefreshCollection(opCtx, shardIds, nss, fixedExecutor);
}
// Drop ns_* indexes of config.chunks
{
DBDirectClient client(opCtx);
const bool includeBuildUUIDs = false;
const int options = 0;
auto indexes = client.getIndexSpecs(ChunkType::ConfigNS, includeBuildUUIDs, options);
BSONArrayBuilder indexNamesToDrop;
for (const auto& index : indexes) {
const auto indexName = index.getStringField("name");
if (indexName == "ns_1_min_1"_sd || indexName == "ns_1_shard_1_min_1"_sd ||
indexName == "ns_1_lastmod_1"_sd) {
indexNamesToDrop.append(indexName);
}
}
BSONObj info;
if (!client.runCommand(ChunkType::ConfigNS.db().toString(),
BSON("dropIndexes" << ChunkType::ConfigNS.coll() << "index"
<< indexNamesToDrop.arr()),
info))
uassertStatusOK(getStatusFromCommandResult(info));
}
// Unset ns for all chunks on config.chunks
{
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
updateConfigDocumentDBDirect(opCtx,
ChunkType::ConfigNS,
{} /* query */,
BSON("$unset" << BSON(ChunkType::ns(""))) /* update */,
false /* upsert */,
true /* multi */);
}
LOGV2(5276701, "Successfully upgraded config.collections and config.chunks");
}
void ShardingCatalogManager::_downgradeCollectionsAndChunksMetadataToPre49(
OperationContext* opCtx) {
LOGV2(5276702, "Starting downgrade of config.collections and config.chunks");
auto const catalogCache = Grid::get(opCtx)->catalogCache();
auto const configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
auto collectionDocs =
uassertStatusOK(
configShard->exhaustiveFindOnConfig(
opCtx,
ReadPreferenceSetting{ReadPreference::PrimaryOnly},
repl::ReadConcernLevel::kLocalReadConcern,
CollectionType::ConfigNS,
BSONObj(BSON(CollectionType::kTimestampFieldName << BSON("$exists" << true))),
BSONObj(),
boost::none))
.docs;
// Set ns on all chunks
for (const auto& doc : collectionDocs) {
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
const CollectionType coll(doc);
const auto uuid = coll.getUuid();
const auto nss = coll.getNss();
uassert(547901,
str::stream() << "The 'allowMigrations' field of the " << nss
<< " collection must be true",
coll.getAllowMigrations());
updateConfigDocumentDBDirect(opCtx,
ChunkType::ConfigNS,
BSON(ChunkType::collectionUUID << uuid) /* query */,
BSON("$set" << BSON(ChunkType::ns(nss.ns()))) /* update */,
false /* upsert */,
true /* multi */);
}
// Create ns_* indexes for config.chunks
uassertStatusOK(createNsIndexesForConfigChunks(opCtx));
// Unset the timestamp field on all collections
{
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
updateConfigDocumentDBDirect(
opCtx,
CollectionType::ConfigNS,
{} /* query */,
BSON("$unset" << BSON(CollectionType::kTimestampFieldName << "")) /* update */,
false /* upsert */,
true /* multi */);
}
// Wait until the last operation is majority-committed
WriteConcernResult ignoreResult;
const auto latestOpTime = repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
uassertStatusOK(waitForWriteConcern(
opCtx, latestOpTime, ShardingCatalogClient::kMajorityWriteConcern, &ignoreResult));
// Forcing a refresh of each collection on each shard
const auto shardIds = Grid::get(opCtx)->shardRegistry()->getAllShardIds(opCtx);
const auto fixedExecutor = Grid::get(_serviceContext)->getExecutorPool()->getFixedExecutor();
for (const auto& doc : collectionDocs) {
const CollectionType coll(doc);
const auto nss = coll.getNss();
catalogCache->invalidateCollectionEntry_LINEARIZABLE(nss);
sharding_util::tellShardsToRefreshCollection(opCtx, shardIds, nss, fixedExecutor);
}
// Drop uuid_* indexes for config.chunks
{
DBDirectClient client(opCtx);
const bool includeBuildUUIDs = false;
const int options = 0;
auto indexes = client.getIndexSpecs(ChunkType::ConfigNS, includeBuildUUIDs, options);
BSONArrayBuilder indexNamesToDrop;
for (const auto& index : indexes) {
const auto indexName = index.getStringField("name");
if (indexName == "uuid_1_min_1"_sd || indexName == "uuid_1_shard_1_min_1"_sd ||
indexName == "uuid_1_lastmod_1"_sd) {
indexNamesToDrop.append(indexName);
}
}
BSONObj info;
if (!client.runCommand(ChunkType::ConfigNS.db().toString(),
BSON("dropIndexes" << ChunkType::ConfigNS.coll() << "index"
<< indexNamesToDrop.arr()),
info))
uassertStatusOK(getStatusFromCommandResult(info));
}
// Unset the timestamp and the uuid for all chunks on config.chunks
{
// Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and
// migrations.
Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock);
updateConfigDocumentDBDirect(
opCtx,
ChunkType::ConfigNS,
{} /* query */,
BSON("$unset" << BSON(ChunkType::timestamp.name()
<< "" << ChunkType::collectionUUID() << "")) /* update */,
false /* upsert */,
true /* multi */);
}
LOGV2(5276703, "Successfully downgraded config.collections and config.chunks");
}
Lock::ExclusiveLock ShardingCatalogManager::lockZoneMutex(OperationContext* opCtx) {
Lock::ExclusiveLock lk(opCtx->lockState(), _kZoneOpLock);
return lk;
}
StatusWith<bool> ShardingCatalogManager::_isShardRequiredByZoneStillInUse(
OperationContext* opCtx,
const ReadPreferenceSetting& readPref,
const std::string& shardName,
const std::string& zoneName) {
auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard();
auto findShardStatus =
configShard->exhaustiveFindOnConfig(opCtx,
readPref,
repl::ReadConcernLevel::kLocalReadConcern,
ShardType::ConfigNS,
BSON(ShardType::tags() << zoneName),
BSONObj(),
2);
if (!findShardStatus.isOK()) {
return findShardStatus.getStatus();
}
const auto shardDocs = findShardStatus.getValue().docs;
if (shardDocs.size() == 0) {
// The zone doesn't exists.
return false;
}
if (shardDocs.size() == 1) {
auto shardDocStatus = ShardType::fromBSON(shardDocs.front());
if (!shardDocStatus.isOK()) {
return shardDocStatus.getStatus();
}
auto shardDoc = shardDocStatus.getValue();
if (shardDoc.getName() != shardName) {
// The last shard that belongs to this zone is a different shard.
return false;
}
auto findChunkRangeStatus =
configShard->exhaustiveFindOnConfig(opCtx,
readPref,
repl::ReadConcernLevel::kLocalReadConcern,
TagsType::ConfigNS,
BSON(TagsType::tag() << zoneName),
BSONObj(),
1);
if (!findChunkRangeStatus.isOK()) {
return findChunkRangeStatus.getStatus();
}
return findChunkRangeStatus.getValue().docs.size() > 0;
}
return false;
}
BSONObj ShardingCatalogManager::writeToConfigDocumentInTxn(OperationContext* opCtx,
const NamespaceString& nss,
const BatchedCommandRequest& request,
TxnNumber txnNumber) {
invariant(nss.db() == NamespaceString::kConfigDb);
auto response = runCommandInLocalTxn(
opCtx, nss.db(), false /* startTransaction */, txnNumber, request.toBSON())
.body;
uassertStatusOK(getStatusFromWriteCommandReply(response));
return response;
}
void ShardingCatalogManager::insertConfigDocumentsInTxn(OperationContext* opCtx,
const NamespaceString& nss,
std::vector<BSONObj> docs,
TxnNumber txnNumber) {
invariant(nss.db() == NamespaceString::kConfigDb);
std::vector<BSONObj> workingBatch;
size_t workingBatchItemSize = 0;
int workingBatchDocSize = 0;
auto doBatchInsert = [&]() {
BatchedCommandRequest request([&] {
write_ops::Insert insertOp(nss);
insertOp.setDocuments(workingBatch);
return insertOp;
}());
writeToConfigDocumentInTxn(opCtx, nss, request, txnNumber);
};
while (!docs.empty()) {
BSONObj toAdd = docs.back();
docs.pop_back();
const int docSizePlusOverhead =
toAdd.objsize() + write_ops::kRetryableAndTxnBatchWriteBSONSizeOverhead;
// Check if pushing this object will exceed the batch size limit or the max object size
if ((workingBatchItemSize + 1 > write_ops::kMaxWriteBatchSize) ||
(workingBatchDocSize + docSizePlusOverhead > BSONObjMaxUserSize)) {
doBatchInsert();
workingBatch.clear();
workingBatchItemSize = 0;
workingBatchDocSize = 0;
}
workingBatch.push_back(toAdd);
++workingBatchItemSize;
workingBatchDocSize += docSizePlusOverhead;
}
if (!workingBatch.empty())
doBatchInsert();
}
void ShardingCatalogManager::withTransaction(
OperationContext* opCtx,
const NamespaceString& namespaceForInitialFind,
unique_function<void(OperationContext*, TxnNumber)> func) {
AlternativeSessionRegion asr(opCtx);
auto* const client = asr.opCtx()->getClient();
{
stdx::lock_guard<Client> lk(*client);
client->setSystemOperationKillableByStepdown(lk);
}
asr.opCtx()->setAlwaysInterruptAtStepDownOrUp();
AuthorizationSession::get(client)->grantInternalAuthorization(client);
TxnNumber txnNumber = 0;
auto guard = makeGuard([opCtx = asr.opCtx(), &txnNumber] {
try {
abortTransaction(opCtx, txnNumber);
} catch (DBException& e) {
LOGV2_WARNING(5192100,
"Failed to abort transaction in AlternativeSessionRegion",
"error"_attr = redact(e));
}
});
size_t attempt = 1;
while (true) {
// Some ErrorCategory::Interruption errors are also considered transient transaction errors.
// We don't attempt to enumerate them explicitly. Instead, we retry on all
// ErrorCategory::Interruption errors (e.g. LockTimeout) and detect whether asr.opCtx() was
// killed by explicitly checking if it has been interrupted.
asr.opCtx()->checkForInterrupt();
++txnNumber;
// We stop retrying on ErrorCategory::NotPrimaryError and ErrorCategory::ShutdownError
// exceptions because it is expected for another attempt on this same server to keep
// receiving that error.
try {
startTransactionWithNoopFind(asr.opCtx(), namespaceForInitialFind, txnNumber);
func(asr.opCtx(), txnNumber);
} catch (const ExceptionForCat<ErrorCategory::NotPrimaryError>&) {
throw;
} catch (const ExceptionForCat<ErrorCategory::ShutdownError>&) {
throw;
} catch (const DBException& ex) {
if (isTransientTransactionError(
ex.code(), false /* hasWriteConcernError */, false /* isCommitOrAbort */)) {
logAndBackoff(5108800,
::mongo::logv2::LogComponent::kSharding,
logv2::LogSeverity::Debug(1),
attempt++,
"Transient transaction error while running local replica set"
" transaction, retrying",
"reason"_attr = redact(ex.toStatus()));
continue;
}
throw;
}
auto [cmdStatus, wcStatus] = commitTransaction(asr.opCtx(), txnNumber);
if (!cmdStatus.isOK() && !cmdStatus.isA<ErrorCategory::NotPrimaryError>() &&
!cmdStatus.isA<ErrorCategory::ShutdownError>() &&
isTransientTransactionError(
cmdStatus.code(), !wcStatus.isOK(), true /* isCommitOrAbort */)) {
logAndBackoff(5108801,
::mongo::logv2::LogComponent::kSharding,
logv2::LogSeverity::Debug(1),
attempt++,
"Transient transaction error while committing local replica set"
" transaction, retrying",
"reason"_attr = redact(cmdStatus));
continue;
}
uassertStatusOK(cmdStatus);
// commitTransaction() specifies {writeConcern: {w: "majority"}} without a wtimeout, so it
// isn't expected to have a write concern error unless the primary is stepping down or
// shutting down or asr.opCtx() is killed. We throw because all of those cases are terminal
// for the caller running a local replica set transaction anyway.
uassertStatusOK(wcStatus);
guard.dismiss();
return;
}
}
} // namespace mongo
|