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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
|
// @file oplog.cpp
/**
* Copyright (C) 2008-2014 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication
#include "mongo/platform/basic.h"
#include "mongo/db/repl/oplog.h"
#include <deque>
#include <set>
#include <vector>
#include "mongo/bson/util/bson_extract.h"
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/authorization_manager_global.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/background.h"
#include "mongo/db/catalog/capped_utils.h"
#include "mongo/db/catalog/coll_mod.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/collection_catalog_entry.h"
#include "mongo/db/catalog/create_collection.h"
#include "mongo/db/catalog/drop_collection.h"
#include "mongo/db/catalog/drop_database.h"
#include "mongo/db/catalog/drop_indexes.h"
#include "mongo/db/catalog/rename_collection.h"
#include "mongo/db/catalog/uuid_catalog.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/index/index_access_method.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/index_builder.h"
#include "mongo/db/keypattern.h"
#include "mongo/db/logical_clock.h"
#include "mongo/db/logical_time.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/op_observer.h"
#include "mongo/db/ops/delete.h"
#include "mongo/db/ops/update.h"
#include "mongo/db/ops/update_lifecycle_impl.h"
#include "mongo/db/repl/apply_ops.h"
#include "mongo/db/repl/bgsync.h"
#include "mongo/db/repl/oplogreader.h"
#include "mongo/db/repl/optime.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/replication_coordinator_global.h"
#include "mongo/db/repl/snapshot_thread.h"
#include "mongo/db/repl/sync_tail.h"
#include "mongo/db/server_options.h"
#include "mongo/db/server_parameters.h"
#include "mongo/db/service_context.h"
#include "mongo/db/session_catalog.h"
#include "mongo/db/stats/counters.h"
#include "mongo/db/storage/storage_engine.h"
#include "mongo/db/storage/storage_options.h"
#include "mongo/platform/random.h"
#include "mongo/scripting/engine.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/concurrency/idle_thread_block.h"
#include "mongo/util/elapsed_tracker.h"
#include "mongo/util/fail_point_service.h"
#include "mongo/util/file.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/stacktrace.h"
#include "mongo/util/startup_test.h"
namespace mongo {
using std::endl;
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::vector;
using IndexVersion = IndexDescriptor::IndexVersion;
namespace repl {
std::string masterSlaveOplogName = "local.oplog.$main";
MONGO_FP_DECLARE(disableSnapshotting);
namespace {
// cached copy...so don't rename, drop, etc.!!!
Collection* _localOplogCollection = nullptr;
PseudoRandom hashGenerator(std::unique_ptr<SecureRandom>(SecureRandom::create())->nextInt64());
// Synchronizes the section where a new Timestamp is generated and when it actually
// appears in the oplog.
stdx::mutex newOpMutex;
stdx::condition_variable newTimestampNotifier;
// Remembers that last timestamp generated for creating new oplog entries or last timestamp of
// oplog entry applied as a secondary. This should only be used for the snapshot thread. Must hold
// the newOpMutex when accessing this variable.
Timestamp lastSetTimestamp;
static std::string _oplogCollectionName;
// so we can fail the same way
void checkOplogInsert(Status result) {
massert(17322, str::stream() << "write to oplog failed: " << result.toString(), result.isOK());
}
struct OplogSlot {
OpTime opTime;
int64_t hash = 0;
};
/**
* Allocates an optime for a new entry in the oplog, and updates the replication coordinator to
* reflect that new optime. Returns the new optime and the correct value of the "h" field for
* the new oplog entry.
*/
void getNextOpTime(OperationContext* opCtx,
Collection* oplog,
ReplicationCoordinator* replCoord,
ReplicationCoordinator::Mode replicationMode,
unsigned count,
OplogSlot* slotsOut) {
synchronizeOnCappedInFlightResource(opCtx->lockState(), oplog->ns());
long long term = OpTime::kUninitializedTerm;
// Fetch term out of the newOpMutex.
if (replicationMode == ReplicationCoordinator::modeReplSet &&
replCoord->isV1ElectionProtocol()) {
// Current term. If we're not a replset of pv=1, it remains kOldProtocolVersionTerm.
term = replCoord->getTerm();
}
stdx::lock_guard<stdx::mutex> lk(newOpMutex);
auto ts = LogicalClock::get(opCtx)->reserveTicks(count).asTimestamp();
lastSetTimestamp = ts;
newTimestampNotifier.notify_all();
fassert(28560, oplog->getRecordStore()->oplogDiskLocRegister(opCtx, ts));
// Set hash if we're in replset mode, otherwise it remains 0 in master/slave.
const bool needHash = (replicationMode == ReplicationCoordinator::modeReplSet);
for (unsigned i = 0; i < count; i++) {
slotsOut[i].opTime = {Timestamp(ts.asULL() + i), term};
if (needHash) {
slotsOut[i].hash = hashGenerator.nextInt64();
}
}
}
/**
* This allows us to stream the oplog entry directly into data region
* main goal is to avoid copying the o portion
* which can be very large
* TODO: can have this build the entire doc
*/
class OplogDocWriter final : public DocWriter {
public:
OplogDocWriter(BSONObj frame, BSONObj oField)
: _frame(std::move(frame)), _oField(std::move(oField)) {}
void writeDocument(char* start) const {
char* buf = start;
memcpy(buf, _frame.objdata(), _frame.objsize() - 1); // don't copy final EOO
DataView(buf).write<LittleEndian<int>>(documentSize());
buf += (_frame.objsize() - 1);
buf[0] = (char)Object;
buf[1] = 'o';
buf[2] = 0;
memcpy(buf + 3, _oField.objdata(), _oField.objsize());
buf += 3 + _oField.objsize();
buf[0] = EOO;
verify(static_cast<size_t>((buf + 1) - start) == documentSize()); // DEV?
}
size_t documentSize() const {
return _frame.objsize() + _oField.objsize() + 1 /* type */ + 2 /* "o" */;
}
private:
BSONObj _frame;
BSONObj _oField;
};
} // namespace
void setOplogCollectionName() {
if (getGlobalReplicationCoordinator()->getReplicationMode() ==
ReplicationCoordinator::modeReplSet) {
_oplogCollectionName = NamespaceString::kRsOplogNamespace.ns();
} else {
_oplogCollectionName = masterSlaveOplogName;
}
}
void createIndexForApplyOps(OperationContext* opCtx,
const BSONObj& indexSpec,
const NamespaceString& indexNss,
IncrementOpsAppliedStatsFn incrementOpsAppliedStats) {
// Check if collection exists.
Database* db = dbHolder().get(opCtx, indexNss.ns());
auto indexCollection = db ? db->getCollection(opCtx, indexNss) : nullptr;
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to create index due to missing collection: " << indexNss.ns(),
indexCollection);
OpCounters* opCounters = opCtx->writesAreReplicated() ? &globalOpCounters : &replOpCounters;
opCounters->gotInsert();
bool relaxIndexConstraints =
ReplicationCoordinator::get(opCtx)->shouldRelaxIndexConstraints(opCtx, indexNss);
if (indexSpec["background"].trueValue()) {
Lock::TempRelease release(opCtx->lockState());
if (opCtx->lockState()->isLocked()) {
// If TempRelease fails, background index build will deadlock.
LOG(3) << "apply op: building background index " << indexSpec
<< " in the foreground because temp release failed";
IndexBuilder builder(indexSpec, relaxIndexConstraints);
Status status = builder.buildInForeground(opCtx, db);
uassertStatusOK(status);
} else {
IndexBuilder* builder = new IndexBuilder(indexSpec, relaxIndexConstraints);
// This spawns a new thread and returns immediately.
builder->go();
// Wait for thread to start and register itself
IndexBuilder::waitForBgIndexStarting();
}
opCtx->recoveryUnit()->abandonSnapshot();
} else {
IndexBuilder builder(indexSpec, relaxIndexConstraints);
Status status = builder.buildInForeground(opCtx, db);
uassertStatusOK(status);
}
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
}
namespace {
Collection* getLocalOplogCollection(OperationContext* opCtx,
const std::string& oplogCollectionName) {
if (_localOplogCollection)
return _localOplogCollection;
AutoGetCollection autoColl(opCtx, NamespaceString(oplogCollectionName), MODE_IX);
_localOplogCollection = autoColl.getCollection();
massert(13347,
"the oplog collection " + oplogCollectionName +
" missing. did you drop it? if so, restart the server",
_localOplogCollection);
return _localOplogCollection;
}
/**
* Attaches the session information of a write to an oplog entry if it exists.
*/
void appendSessionInfo(OperationContext* opCtx,
BSONObjBuilder* builder,
StmtId statementId,
const Timestamp& prevTs) {
auto txnNum = opCtx->getTxnNumber();
if (!txnNum) {
return;
}
auto logicalSessionId = opCtx->getLogicalSessionId();
invariant(logicalSessionId);
// Note: certain operations, like implicit collection creation will not have a stmtId.
if (statementId == kUninitializedStmtId) {
return;
}
OperationSessionInfo sessionInfo;
sessionInfo.setSessionId(*logicalSessionId);
sessionInfo.setTxnNumber(txnNum);
sessionInfo.serialize(builder);
builder->append(OplogEntryBase::kStatementIdFieldName, statementId);
auto session = OperationContextSession::get(opCtx);
invariant(session);
builder->append(OplogEntryBase::kPrevWriteTsInTransactionFieldName, prevTs);
}
OplogDocWriter _logOpWriter(OperationContext* opCtx,
const char* opstr,
const NamespaceString& nss,
OptionalCollectionUUID uuid,
const BSONObj& obj,
const BSONObj* o2,
bool fromMigrate,
OpTime optime,
long long hashNew,
Date_t wallTime,
StmtId statementId,
const Timestamp& prevTs,
const PreAndPostImageTimestamps& preAndPostTs) {
BSONObjBuilder b(256);
b.append("ts", optime.getTimestamp());
if (optime.getTerm() != -1)
b.append("t", optime.getTerm());
b.append("h", hashNew);
b.append("v", OplogEntry::kOplogVersion);
b.append("op", opstr);
b.append("ns", nss.ns());
if (uuid &&
repl::getGlobalReplicationCoordinator()->getReplicationMode() !=
repl::ReplicationCoordinator::modeMasterSlave)
uuid->appendToBuilder(&b, "ui");
if (fromMigrate)
b.appendBool("fromMigrate", true);
if (o2)
b.append("o2", *o2);
if (wallTime != Date_t{} &&
serverGlobalParams.featureCompatibility.version.load() !=
ServerGlobalParams::FeatureCompatibility::Version::k34) {
b.appendDate("wall", wallTime);
}
appendSessionInfo(opCtx, &b, statementId, prevTs);
if (!preAndPostTs.preImageTs.isNull()) {
b.append(OplogEntryBase::kPreImageTsFieldName, preAndPostTs.preImageTs);
}
if (!preAndPostTs.postImageTs.isNull()) {
b.append(OplogEntryBase::kPostImageTsFieldName, preAndPostTs.postImageTs);
}
return OplogDocWriter(OplogDocWriter(b.obj(), obj));
}
} // end anon namespace
/* we write to local.oplog.rs:
{ ts : ..., h: ..., v: ..., op: ..., etc }
ts: an OpTime timestamp
h: hash
v: version
op:
"i" insert
"u" update
"d" delete
"c" db cmd
"db" declares presence of a database (ns is set to the db name + '.')
"n" no op
bb param:
if not null, specifies a boolean to pass along to the other side as b: param.
used for "justOne" or "upsert" flags on 'd', 'u'
*/
void _logOpsInner(OperationContext* opCtx,
const NamespaceString& nss,
const DocWriter* const* writers,
size_t nWriters,
Collection* oplogCollection,
ReplicationCoordinator::Mode replicationMode,
OpTime finalOpTime) {
ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator();
if (nss.size() && replicationMode == ReplicationCoordinator::modeReplSet &&
!replCoord->canAcceptWritesFor(opCtx, nss)) {
severe() << "logOp() but can't accept write to collection " << nss.ns();
fassertFailed(17405);
}
// we jump through a bunch of hoops here to avoid copying the obj buffer twice --
// instead we do a single copy to the destination in the record store.
checkOplogInsert(oplogCollection->insertDocumentsForOplog(opCtx, writers, nWriters));
// Set replCoord last optime only after we're sure the WUOW didn't abort and roll back.
opCtx->recoveryUnit()->onCommit([opCtx, replCoord, finalOpTime] {
replCoord->setMyLastAppliedOpTimeForward(finalOpTime);
ReplClientInfo::forClient(opCtx->getClient()).setLastOp(finalOpTime);
});
}
OpTime logOp(OperationContext* opCtx,
const char* opstr,
const NamespaceString& nss,
OptionalCollectionUUID uuid,
const BSONObj& obj,
const BSONObj* o2,
bool fromMigrate,
StmtId statementId,
const PreAndPostImageTimestamps& preAndPostTs) {
auto replCoord = ReplicationCoordinator::get(opCtx);
if (replCoord->isOplogDisabledFor(opCtx, nss)) {
invariant(statementId == kUninitializedStmtId);
return {};
}
Collection* oplog = getLocalOplogCollection(opCtx, _oplogCollectionName);
Lock::DBLock lk(opCtx, "local", MODE_IX);
Lock::CollectionLock lock(opCtx->lockState(), _oplogCollectionName, MODE_IX);
auto replMode = replCoord->getReplicationMode();
OplogSlot slot;
getNextOpTime(opCtx, oplog, replCoord, replMode, 1, &slot);
Timestamp prevTs;
if (opCtx->getTxnNumber()) {
prevTs = OperationContextSession::get(opCtx)->getLastWriteOpTimeTs();
}
auto writer = _logOpWriter(opCtx,
opstr,
nss,
uuid,
obj,
o2,
fromMigrate,
slot.opTime,
slot.hash,
Date_t::now(),
statementId,
prevTs,
preAndPostTs);
const DocWriter* basePtr = &writer;
_logOpsInner(opCtx, nss, &basePtr, 1, oplog, replMode, slot.opTime);
return slot.opTime;
}
repl::OpTime logInsertOps(OperationContext* opCtx,
const NamespaceString& nss,
OptionalCollectionUUID uuid,
std::vector<InsertStatement>::const_iterator begin,
std::vector<InsertStatement>::const_iterator end,
bool fromMigrate) {
invariant(begin != end);
auto replCoord = ReplicationCoordinator::get(opCtx);
if (replCoord->isOplogDisabledFor(opCtx, nss)) {
invariant(begin->stmtId == kUninitializedStmtId);
return {};
}
const size_t count = end - begin;
std::vector<OplogDocWriter> writers;
writers.reserve(count);
Collection* oplog = getLocalOplogCollection(opCtx, _oplogCollectionName);
Lock::DBLock lk(opCtx, "local", MODE_IX);
Lock::CollectionLock lock(opCtx->lockState(), _oplogCollectionName, MODE_IX);
std::unique_ptr<OplogSlot[]> slots(new OplogSlot[count]);
auto replMode = replCoord->getReplicationMode();
getNextOpTime(opCtx, oplog, replCoord, replMode, count, slots.get());
auto wallTime = Date_t::now();
Timestamp prevTs;
if (opCtx->getTxnNumber()) {
prevTs = OperationContextSession::get(opCtx)->getLastWriteOpTimeTs();
}
for (size_t i = 0; i < count; i++) {
auto insertStatement = begin[i];
writers.emplace_back(_logOpWriter(opCtx,
"i",
nss,
uuid,
insertStatement.doc,
NULL,
fromMigrate,
slots[i].opTime,
slots[i].hash,
wallTime,
insertStatement.stmtId,
prevTs,
{}));
prevTs = slots[i].opTime.getTimestamp();
}
std::unique_ptr<DocWriter const* []> basePtrs(new DocWriter const*[count]);
for (size_t i = 0; i < count; i++) {
basePtrs[i] = &writers[i];
}
_logOpsInner(opCtx, nss, basePtrs.get(), count, oplog, replMode, slots[count - 1].opTime);
return slots[count - 1].opTime;
}
namespace {
long long getNewOplogSizeBytes(OperationContext* opCtx, const ReplSettings& replSettings) {
if (replSettings.getOplogSizeBytes() != 0) {
return replSettings.getOplogSizeBytes();
}
/* not specified. pick a default size */
ProcessInfo pi;
if (pi.getAddrSize() == 32) {
const auto sz = 50LL * 1024LL * 1024LL;
LOG(3) << "32bit system; choosing " << sz << " bytes oplog";
return sz;
}
// First choose a minimum size.
#if defined(__APPLE__)
// typically these are desktops (dev machines), so keep it smallish
const auto sz = 192 * 1024 * 1024;
LOG(3) << "Apple system; choosing " << sz << " bytes oplog";
return sz;
#else
long long lowerBound = 0;
double bytes = 0;
if (opCtx->getClient()->getServiceContext()->getGlobalStorageEngine()->isEphemeral()) {
// in memory: 50MB minimum size
lowerBound = 50LL * 1024 * 1024;
bytes = pi.getMemSizeMB() * 1024 * 1024;
LOG(3) << "Ephemeral storage system; lowerBound: " << lowerBound << " bytes, " << bytes
<< " bytes total memory";
} else {
// disk: 990MB minimum size
lowerBound = 990LL * 1024 * 1024;
bytes = File::freeSpace(storageGlobalParams.dbpath); //-1 if call not supported.
LOG(3) << "Disk storage system; lowerBound: " << lowerBound << " bytes, " << bytes
<< " bytes free space on device";
}
long long fivePct = static_cast<long long>(bytes * 0.05);
auto sz = std::max(fivePct, lowerBound);
// we use 5% of free [disk] space up to 50GB (1TB free)
const long long upperBound = 50LL * 1024 * 1024 * 1024;
sz = std::min(sz, upperBound);
return sz;
#endif
}
} // namespace
void createOplog(OperationContext* opCtx, const std::string& oplogCollectionName, bool isReplSet) {
Lock::GlobalWrite lk(opCtx);
const ReplSettings& replSettings = ReplicationCoordinator::get(opCtx)->getSettings();
OldClientContext ctx(opCtx, oplogCollectionName);
Collection* collection = ctx.db()->getCollection(opCtx, oplogCollectionName);
if (collection) {
if (replSettings.getOplogSizeBytes() != 0) {
const CollectionOptions oplogOpts =
collection->getCatalogEntry()->getCollectionOptions(opCtx);
int o = (int)(oplogOpts.cappedSize / (1024 * 1024));
int n = (int)(replSettings.getOplogSizeBytes() / (1024 * 1024));
if (n != o) {
stringstream ss;
ss << "cmdline oplogsize (" << n << ") different than existing (" << o
<< ") see: http://dochub.mongodb.org/core/increase-oplog";
log() << ss.str() << endl;
throw AssertionException(13257, ss.str());
}
}
if (!isReplSet)
initTimestampFromOplog(opCtx, oplogCollectionName);
return;
}
/* create an oplog collection, if it doesn't yet exist. */
const auto sz = getNewOplogSizeBytes(opCtx, replSettings);
log() << "******" << endl;
log() << "creating replication oplog of size: " << (int)(sz / (1024 * 1024)) << "MB..." << endl;
CollectionOptions options;
options.capped = true;
options.cappedSize = sz;
options.autoIndexId = CollectionOptions::NO;
writeConflictRetry(opCtx, "createCollection", oplogCollectionName, [&] {
WriteUnitOfWork uow(opCtx);
invariant(ctx.db()->createCollection(opCtx, oplogCollectionName, options));
if (!isReplSet)
getGlobalServiceContext()->getOpObserver()->onOpMessage(opCtx, BSONObj());
uow.commit();
});
/* sync here so we don't get any surprising lag later when we try to sync */
StorageEngine* storageEngine = getGlobalServiceContext()->getGlobalStorageEngine();
storageEngine->flushAllFiles(opCtx, true);
log() << "******" << endl;
}
void createOplog(OperationContext* opCtx) {
const auto isReplSet = ReplicationCoordinator::get(opCtx)->getReplicationMode() ==
ReplicationCoordinator::modeReplSet;
createOplog(opCtx, _oplogCollectionName, isReplSet);
}
// -------------------------------------
namespace {
NamespaceString parseNs(const string& ns, const BSONObj& cmdObj) {
BSONElement first = cmdObj.firstElement();
uassert(40073,
str::stream() << "collection name has invalid type " << typeName(first.type()),
first.canonicalType() == canonicalizeBSONType(mongo::String));
std::string coll = first.valuestr();
uassert(28635, "no collection name specified", !coll.empty());
return NamespaceString(NamespaceString(ns).db().toString(), coll);
}
std::pair<OptionalCollectionUUID, NamespaceString> parseCollModUUIDAndNss(OperationContext* opCtx,
const BSONElement& ui,
const char* ns,
BSONObj& cmd) {
if (ui.eoo()) {
return std::pair<OptionalCollectionUUID, NamespaceString>(boost::none, parseNs(ns, cmd));
}
CollectionUUID uuid = uassertStatusOK(UUID::parse(ui));
auto& catalog = UUIDCatalog::get(opCtx);
if (catalog.lookupCollectionByUUID(uuid)) {
return std::pair<OptionalCollectionUUID, NamespaceString>(uuid,
catalog.lookupNSSByUUID(uuid));
} else {
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply operation due to missing collection (" << uuid
<< "): "
<< redact(cmd.toString()),
cmd.nFields() == 1);
// If cmd is an empty collMod, i.e., nFields is 1, this is a UUID upgrade collMod.
return std::pair<OptionalCollectionUUID, NamespaceString>(uuid, parseNs(ns, cmd));
}
}
NamespaceString parseUUID(OperationContext* opCtx, const BSONElement& ui) {
auto statusWithUUID = UUID::parse(ui);
uassertStatusOK(statusWithUUID);
auto uuid = statusWithUUID.getValue();
auto& catalog = UUIDCatalog::get(opCtx);
auto nss = catalog.lookupNSSByUUID(uuid);
uassert(
ErrorCodes::NamespaceNotFound, "No namespace with UUID " + uuid.toString(), !nss.isEmpty());
return nss;
}
NamespaceString parseUUIDorNs(OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd) {
return ui.ok() ? parseUUID(opCtx, ui) : parseNs(ns, cmd);
}
using OpApplyFn = stdx::function<Status(OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime)>;
struct ApplyOpMetadata {
OpApplyFn applyFunc;
std::set<ErrorCodes::Error> acceptableErrors;
ApplyOpMetadata(OpApplyFn fun) {
applyFunc = fun;
}
ApplyOpMetadata(OpApplyFn fun, std::set<ErrorCodes::Error> theAcceptableErrors) {
applyFunc = fun;
acceptableErrors = theAcceptableErrors;
}
};
std::map<std::string, ApplyOpMetadata> opsMap = {
{"create",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
const NamespaceString nss(parseNs(ns, cmd));
if (auto idIndexElem = cmd["idIndex"]) {
// Remove "idIndex" field from command.
auto cmdWithoutIdIndex = cmd.removeField("idIndex");
return createCollectionForApplyOps(
opCtx, nss.db().toString(), ui, cmdWithoutIdIndex, idIndexElem.Obj());
}
// No _id index spec was provided, so we should build a v:1 _id index.
BSONObjBuilder idIndexSpecBuilder;
idIndexSpecBuilder.append(IndexDescriptor::kIndexVersionFieldName,
static_cast<int>(IndexVersion::kV1));
idIndexSpecBuilder.append(IndexDescriptor::kIndexNameFieldName, "_id_");
idIndexSpecBuilder.append(IndexDescriptor::kNamespaceFieldName, nss.ns());
idIndexSpecBuilder.append(IndexDescriptor::kKeyPatternFieldName, BSON("_id" << 1));
return createCollectionForApplyOps(
opCtx, nss.db().toString(), ui, cmd, idIndexSpecBuilder.done());
},
{ErrorCodes::NamespaceExists}}},
{"createIndexes",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
const NamespaceString nss(parseUUID(opCtx, ui));
BSONElement first = cmd.firstElement();
invariant(first.fieldNameStringData() == "createIndexes");
uassert(ErrorCodes::InvalidNamespace,
"createIndexes value must be a string",
first.type() == mongo::String);
BSONObj indexSpec = cmd.removeField("createIndexes");
// The UUID determines the collection to build the index on, so create new 'ns' field.
BSONObj nsObj = BSON("ns" << nss.ns());
indexSpec = indexSpec.addField(nsObj.firstElement());
createIndexForApplyOps(opCtx, indexSpec, nss, {});
return Status::OK();
},
{ErrorCodes::IndexAlreadyExists, ErrorCodes::NamespaceNotFound}}},
{"collMod",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
OptionalCollectionUUID uuid;
NamespaceString nss;
std::tie(uuid, nss) = parseCollModUUIDAndNss(opCtx, ui, ns, cmd);
return collModForUUIDUpgrade(opCtx, nss, cmd, uuid);
},
{ErrorCodes::IndexNotFound, ErrorCodes::NamespaceNotFound}}},
{"dropDatabase",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
return dropDatabase(opCtx, NamespaceString(ns).db().toString());
},
{ErrorCodes::NamespaceNotFound}}},
{"drop",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return dropCollection(opCtx,
parseUUIDorNs(opCtx, ns, ui, cmd),
resultWeDontCareAbout,
opTime,
DropCollectionSystemCollectionMode::kAllowSystemCollectionDrops);
},
{ErrorCodes::NamespaceNotFound}}},
// deleteIndex(es) is deprecated but still works as of April 10, 2015
{"deleteIndex",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return dropIndexes(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd), cmd, &resultWeDontCareAbout);
},
{ErrorCodes::NamespaceNotFound, ErrorCodes::IndexNotFound}}},
{"deleteIndexes",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return dropIndexes(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd), cmd, &resultWeDontCareAbout);
},
{ErrorCodes::NamespaceNotFound, ErrorCodes::IndexNotFound}}},
{"dropIndex",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return dropIndexes(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd), cmd, &resultWeDontCareAbout);
},
{ErrorCodes::NamespaceNotFound, ErrorCodes::IndexNotFound}}},
{"dropIndexes",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return dropIndexes(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd), cmd, &resultWeDontCareAbout);
},
{ErrorCodes::NamespaceNotFound, ErrorCodes::IndexNotFound}}},
{"renameCollection",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
return renameCollectionForApplyOps(opCtx, nsToDatabase(ns), ui, cmd);
},
{ErrorCodes::NamespaceNotFound, ErrorCodes::NamespaceExists}}},
{"applyOps",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
BSONObjBuilder resultWeDontCareAbout;
return applyOps(opCtx, nsToDatabase(ns), cmd, &resultWeDontCareAbout);
},
{ErrorCodes::UnknownError}}},
{"convertToCapped",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
return convertToCapped(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd), cmd["size"].number());
}}},
{"emptycapped",
{[](OperationContext* opCtx,
const char* ns,
const BSONElement& ui,
BSONObj& cmd,
const OpTime& opTime) -> Status {
return emptyCapped(opCtx, parseUUIDorNs(opCtx, ns, ui, cmd));
}}},
};
} // namespace
std::pair<BSONObj, NamespaceString> prepForApplyOpsIndexInsert(const BSONElement& fieldO,
const BSONObj& op,
const NamespaceString& requestNss) {
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Missing expected index spec in field 'o': " << op,
!fieldO.eoo());
uassert(ErrorCodes::TypeMismatch,
str::stream() << "Expected object for index spec in field 'o': " << op,
fieldO.isABSONObj());
BSONObj indexSpec = fieldO.embeddedObject();
std::string indexNs;
uassertStatusOK(bsonExtractStringField(indexSpec, "ns", &indexNs));
const NamespaceString indexNss(indexNs);
uassert(ErrorCodes::InvalidNamespace,
str::stream() << "Invalid namespace in index spec: " << op,
indexNss.isValid());
uassert(ErrorCodes::InvalidNamespace,
str::stream() << "Database name mismatch for database (" << requestNss.db()
<< ") while creating index: "
<< op,
requestNss.db() == indexNss.db());
if (!indexSpec["v"]) {
// If the "v" field isn't present in the index specification, then we assume it is a
// v=1 index from an older version of MongoDB. This is because
// (1) we haven't built v=0 indexes as the default for a long time, and
// (2) the index version has been included in the corresponding oplog entry since
// v=2 indexes were introduced.
BSONObjBuilder bob;
bob.append("v", static_cast<int>(IndexVersion::kV1));
bob.appendElements(indexSpec);
indexSpec = bob.obj();
}
return std::make_pair(indexSpec, indexNss);
}
// @return failure status if an update should have happened and the document DNE.
// See replset initial sync code.
Status applyOperation_inlock(OperationContext* opCtx,
Database* db,
const BSONObj& op,
bool inSteadyStateReplication,
IncrementOpsAppliedStatsFn incrementOpsAppliedStats) {
LOG(3) << "applying op: " << redact(op);
OpCounters* opCounters = opCtx->writesAreReplicated() ? &globalOpCounters : &replOpCounters;
std::array<StringData, 6> names = {"o", "ui", "ns", "op", "b", "o2"};
std::array<BSONElement, 6> fields;
op.getFields(names, &fields);
BSONElement& fieldO = fields[0];
BSONElement& fieldUI = fields[1];
BSONElement& fieldNs = fields[2];
BSONElement& fieldOp = fields[3];
BSONElement& fieldB = fields[4];
BSONElement& fieldO2 = fields[5];
BSONObj o;
if (fieldO.isABSONObj())
o = fieldO.embeddedObject();
// operation type -- see logOp() comments for types
const char* opType = fieldOp.valuestrsafe();
NamespaceString requestNss;
Collection* collection = nullptr;
if (fieldUI) {
UUIDCatalog& catalog = UUIDCatalog::get(opCtx);
auto uuid = uassertStatusOK(UUID::parse(fieldUI));
collection = catalog.lookupCollectionByUUID(uuid);
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply operation due to missing collection (" << uuid
<< "): "
<< redact(op.toString()),
collection);
requestNss = collection->ns();
dassert(opCtx->lockState()->isCollectionLockedForMode(
requestNss.ns(), supportsDocLocking() ? MODE_IX : MODE_X));
} else {
uassert(ErrorCodes::InvalidNamespace,
"'ns' must be of type String",
fieldNs.type() == BSONType::String);
const StringData ns = fieldNs.valueStringData();
requestNss = NamespaceString(ns);
if (nsIsFull(ns)) {
if (supportsDocLocking()) {
// WiredTiger, and others requires MODE_IX since the applier threads driving
// this allow writes to the same collection on any thread.
dassert(opCtx->lockState()->isCollectionLockedForMode(ns, MODE_IX));
} else {
// mmapV1 ensures that all operations to the same collection are executed from
// the same worker thread, so it takes an exclusive lock (MODE_X)
dassert(opCtx->lockState()->isCollectionLockedForMode(ns, MODE_X));
}
}
collection = db->getCollection(opCtx, requestNss);
}
BSONObj o2;
if (fieldO2.isABSONObj())
o2 = fieldO2.Obj();
bool valueB = fieldB.booleanSafe();
IndexCatalog* indexCatalog = collection == nullptr ? nullptr : collection->getIndexCatalog();
const bool haveWrappingWriteUnitOfWork = opCtx->lockState()->inAWriteUnitOfWork();
uassert(ErrorCodes::CommandNotSupportedOnView,
str::stream() << "applyOps not supported on view: " << requestNss.ns(),
collection || !db->getViewCatalog()->lookup(opCtx, requestNss.ns()));
if (*opType == 'i') {
if (requestNss.isSystemDotIndexes()) {
BSONObj indexSpec;
NamespaceString indexNss;
std::tie(indexSpec, indexNss) =
repl::prepForApplyOpsIndexInsert(fieldO, op, requestNss);
createIndexForApplyOps(opCtx, indexSpec, indexNss, incrementOpsAppliedStats);
return Status::OK();
}
uassert(ErrorCodes::NamespaceNotFound,
str::stream() << "Failed to apply insert due to missing collection: "
<< op.toString(),
collection);
if (fieldO.type() == Array) {
// Batched inserts.
std::vector<InsertStatement> insertObjs;
for (auto elem : fieldO.Obj()) {
// Note: we don't care about statement ids here since the secondaries don't create
// their own oplog entries.
insertObjs.emplace_back(elem.Obj());
}
uassert(ErrorCodes::OperationFailed,
str::stream() << "Failed to apply insert due to empty array element: "
<< op.toString(),
!insertObjs.empty());
WriteUnitOfWork wuow(opCtx);
OpDebug* const nullOpDebug = nullptr;
Status status = collection->insertDocuments(
opCtx, insertObjs.begin(), insertObjs.end(), nullOpDebug, true);
if (!status.isOK()) {
return status;
}
wuow.commit();
for (auto entry : insertObjs) {
opCounters->gotInsert();
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
}
} else {
// Single insert.
opCounters->gotInsert();
// No _id.
// This indicates an issue with the upstream server:
// The oplog entry is corrupted; or
// The version of the upstream server is obsolete.
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply insert due to missing _id: " << op.toString(),
o.hasField("_id"));
// 1. Try insert first, if we have no wrappingWriteUnitOfWork
// 2. If okay, commit
// 3. If not, do upsert (and commit)
// 4. If both !Ok, return status
// We cannot rely on a DuplicateKey error if we'repart of a larger transaction, because
// that would require the transaction to abort. So instead, use upsert in that case.
bool needToDoUpsert = haveWrappingWriteUnitOfWork;
if (!needToDoUpsert) {
WriteUnitOfWork wuow(opCtx);
OpDebug* const nullOpDebug = nullptr;
auto status =
collection->insertDocument(opCtx, InsertStatement(o), nullOpDebug, true);
if (status.isOK()) {
wuow.commit();
} else if (status == ErrorCodes::DuplicateKey) {
needToDoUpsert = true;
} else {
return status;
}
}
// Now see if we need to do an upsert.
if (needToDoUpsert) {
// Do update on DuplicateKey errors.
// This will only be on the _id field in replication,
// since we disable non-_id unique constraint violations.
BSONObjBuilder b;
b.append(o.getField("_id"));
UpdateRequest request(requestNss);
request.setQuery(b.done());
request.setUpdates(o);
request.setUpsert();
UpdateLifecycleImpl updateLifecycle(requestNss);
request.setLifecycle(&updateLifecycle);
UpdateResult res = update(opCtx, db, request);
if (res.numMatched == 0 && res.upserted.isEmpty()) {
error() << "No document was updated even though we got a DuplicateKey "
"error when inserting";
fassertFailedNoTrace(28750);
}
}
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
}
} else if (*opType == 'u') {
opCounters->gotUpdate();
BSONObj updateCriteria = o2;
const bool upsert = valueB || inSteadyStateReplication;
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply update due to missing _id: " << op.toString(),
updateCriteria.hasField("_id"));
UpdateRequest request(requestNss);
request.setQuery(updateCriteria);
request.setUpdates(o);
request.setUpsert(upsert);
UpdateLifecycleImpl updateLifecycle(requestNss);
request.setLifecycle(&updateLifecycle);
UpdateResult ur = update(opCtx, db, request);
if (ur.numMatched == 0 && ur.upserted.isEmpty()) {
if (ur.modifiers) {
if (updateCriteria.nFields() == 1) {
// was a simple { _id : ... } update criteria
string msg = str::stream() << "failed to apply update: " << redact(op);
error() << msg;
return Status(ErrorCodes::UpdateOperationFailed, msg);
}
// Need to check to see if it isn't present so we can exit early with a
// failure. Note that adds some overhead for this extra check in some cases,
// such as an updateCriteria
// of the form
// { _id:..., { x : {$size:...} }
// thus this is not ideal.
if (collection == NULL ||
(indexCatalog->haveIdIndex(opCtx) &&
Helpers::findById(opCtx, collection, updateCriteria).isNull()) ||
// capped collections won't have an _id index
(!indexCatalog->haveIdIndex(opCtx) &&
Helpers::findOne(opCtx, collection, updateCriteria, false).isNull())) {
string msg = str::stream() << "couldn't find doc: " << redact(op);
error() << msg;
return Status(ErrorCodes::UpdateOperationFailed, msg);
}
// Otherwise, it's present; zero objects were updated because of additional
// specifiers in the query for idempotence
} else {
// this could happen benignly on an oplog duplicate replay of an upsert
// (because we are idempotent),
// if an regular non-mod update fails the item is (presumably) missing.
if (!upsert) {
string msg = str::stream() << "update of non-mod failed: " << redact(op);
error() << msg;
return Status(ErrorCodes::UpdateOperationFailed, msg);
}
}
}
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
} else if (*opType == 'd') {
opCounters->gotDelete();
uassert(ErrorCodes::NoSuchKey,
str::stream() << "Failed to apply delete due to missing _id: " << op.toString(),
o.hasField("_id"));
if (opType[1] == 0) {
deleteObjects(opCtx, collection, requestNss, o, /*justOne*/ valueB);
} else
verify(opType[1] == 'b'); // "db" advertisement
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
} else if (*opType == 'n') {
// no op
if (incrementOpsAppliedStats) {
incrementOpsAppliedStats();
}
} else {
invariant(*opType != 'c'); // commands are processed in applyCommand_inlock()
throw AssertionException(
14825, str::stream() << "error in applyOperation : unknown opType " << *opType);
}
return Status::OK();
}
Status applyCommand_inlock(OperationContext* opCtx,
const BSONObj& op,
bool inSteadyStateReplication) {
std::array<StringData, 4> names = {"o", "ui", "ns", "op"};
std::array<BSONElement, 4> fields;
op.getFields(names, &fields);
BSONElement& fieldO = fields[0];
BSONElement& fieldUI = fields[1];
BSONElement& fieldNs = fields[2];
BSONElement& fieldOp = fields[3];
const char* opType = fieldOp.valuestrsafe();
invariant(*opType == 'c'); // only commands are processed here
if (fieldO.eoo()) {
return Status(ErrorCodes::NoSuchKey, "Missing expected field 'o'");
}
if (!fieldO.isABSONObj()) {
return Status(ErrorCodes::BadValue, "Expected object for field 'o'");
}
BSONObj o = fieldO.embeddedObject();
uassert(ErrorCodes::InvalidNamespace,
"'ns' must be of type String",
fieldNs.type() == BSONType::String);
const NamespaceString nss(fieldNs.valueStringData());
if (!nss.isValid()) {
return {ErrorCodes::InvalidNamespace, "invalid ns: " + std::string(nss.ns())};
}
{
Database* db = dbHolder().get(opCtx, nss.ns());
if (db && !db->getCollection(opCtx, nss) && db->getViewCatalog()->lookup(opCtx, nss.ns())) {
return {ErrorCodes::CommandNotSupportedOnView,
str::stream() << "applyOps not supported on view:" << nss.ns()};
}
}
// Applying renameCollection during initial sync might lead to data corruption, so we restart
// the initial sync.
if (!inSteadyStateReplication && o.firstElementFieldName() == std::string("renameCollection")) {
return Status(ErrorCodes::OplogOperationUnsupported,
str::stream() << "Applying renameCollection not supported in initial sync: "
<< redact(op));
}
// Applying commands in repl is done under Global W-lock, so it is safe to not
// perform the current DB checks after reacquiring the lock.
invariant(opCtx->lockState()->isW());
// Parse optime from oplog entry unless we are applying this command in standalone or on a
// primary (replicated writes enabled).
OpTime opTime;
if (!opCtx->writesAreReplicated()) {
auto opTimeResult = OpTime::parseFromOplogEntry(op);
if (opTimeResult.isOK()) {
opTime = opTimeResult.getValue();
}
}
bool done = false;
while (!done) {
auto op = opsMap.find(o.firstElementFieldName());
if (op == opsMap.end()) {
return Status(ErrorCodes::BadValue,
mongoutils::str::stream() << "Invalid key '" << o.firstElementFieldName()
<< "' found in field 'o'");
}
ApplyOpMetadata curOpToApply = op->second;
Status status = Status::OK();
try {
status = curOpToApply.applyFunc(opCtx, nss.ns().c_str(), fieldUI, o, opTime);
} catch (...) {
status = exceptionToStatus();
}
switch (status.code()) {
case ErrorCodes::WriteConflict: {
// Need to throw this up to a higher level where it will be caught and the
// operation retried.
throw WriteConflictException();
}
case ErrorCodes::BackgroundOperationInProgressForDatabase: {
Lock::TempRelease release(opCtx->lockState());
BackgroundOperation::awaitNoBgOpInProgForDb(nss.db());
opCtx->recoveryUnit()->abandonSnapshot();
opCtx->checkForInterrupt();
break;
}
case ErrorCodes::BackgroundOperationInProgressForNamespace: {
Lock::TempRelease release(opCtx->lockState());
Command* cmd = Command::findCommand(o.firstElement().fieldName());
invariant(cmd);
BackgroundOperation::awaitNoBgOpInProgForNs(cmd->parseNs(nss.db().toString(), o));
opCtx->recoveryUnit()->abandonSnapshot();
opCtx->checkForInterrupt();
break;
}
default:
if (_oplogCollectionName == masterSlaveOplogName) {
error() << "Failed command " << redact(o) << " on " << nss.db()
<< " with status " << status << " during oplog application";
} else if (curOpToApply.acceptableErrors.find(status.code()) ==
curOpToApply.acceptableErrors.end()) {
error() << "Failed command " << redact(o) << " on " << nss.db()
<< " with status " << status << " during oplog application";
return status;
}
// fallthrough
case ErrorCodes::OK:
done = true;
break;
}
}
// AuthorizationManager's logOp method registers a RecoveryUnit::Change
// and to do so we need to have begun a UnitOfWork
WriteUnitOfWork wuow(opCtx);
getGlobalAuthorizationManager()->logOp(opCtx, opType, nss, o, nullptr);
wuow.commit();
return Status::OK();
}
void setNewTimestamp(ServiceContext* service, const Timestamp& newTime) {
stdx::lock_guard<stdx::mutex> lk(newOpMutex);
LogicalClock::get(service)->setClusterTimeFromTrustedSource(LogicalTime(newTime));
lastSetTimestamp = newTime;
newTimestampNotifier.notify_all();
}
void initTimestampFromOplog(OperationContext* opCtx, const std::string& oplogNS) {
DBDirectClient c(opCtx);
BSONObj lastOp = c.findOne(oplogNS, Query().sort(reverseNaturalObj), NULL, QueryOption_SlaveOk);
if (!lastOp.isEmpty()) {
LOG(1) << "replSet setting last Timestamp";
const OpTime opTime = fassertStatusOK(28696, OpTime::parseFromOplogEntry(lastOp));
setNewTimestamp(opCtx->getServiceContext(), opTime.getTimestamp());
}
}
void oplogCheckCloseDatabase(OperationContext* opCtx, Database* db) {
invariant(opCtx->lockState()->isW());
_localOplogCollection = nullptr;
}
void signalOplogWaiters() {
if (_localOplogCollection) {
_localOplogCollection->notifyCappedWaitersIfNeeded();
}
}
MONGO_EXPORT_STARTUP_SERVER_PARAMETER(replSnapshotThreadThrottleMicros, int, 1000);
SnapshotThread::SnapshotThread(SnapshotManager* manager)
: _manager(manager), _thread([this] { run(); }) {}
bool SnapshotThread::shouldSleepMore(int numSleepsDone, size_t numUncommittedSnapshots) {
const double kThrottleRatio = 1 / 20.0;
const size_t kUncommittedSnapshotLimit = 1000;
const size_t kUncommittedSnapshotRestartPoint = kUncommittedSnapshotLimit / 2;
if (_inShutdown.load())
return false; // Exit the thread quickly without sleeping.
if (numSleepsDone == 0)
return true; // Always sleep at least once.
{
// Enforce a limit on the number of snapshots.
if (numUncommittedSnapshots >= kUncommittedSnapshotLimit)
_hitSnapshotLimit = true; // Don't create new snapshots.
if (numUncommittedSnapshots < kUncommittedSnapshotRestartPoint)
_hitSnapshotLimit = false; // Begin creating new snapshots again.
if (_hitSnapshotLimit)
return true;
}
// Spread out snapshots in time by sleeping as we collect more uncommitted snapshots.
const double numSleepsNeeded = numUncommittedSnapshots * kThrottleRatio;
return numSleepsNeeded > numSleepsDone;
}
void SnapshotThread::run() {
Client::initThread("SnapshotThread");
auto& client = cc();
auto service = client.getServiceContext();
auto replCoord = ReplicationCoordinator::get(service);
Timestamp lastTimestamp(Timestamp::max()); // hack to trigger snapshot from startup.
while (true) {
// This block logically belongs at the end of the loop, but having it at the top
// simplifies handling of the "continue" cases. It is harmless to do these before the
// first run of the loop.
for (int numSleepsDone = 0;
shouldSleepMore(numSleepsDone, replCoord->getNumUncommittedSnapshots());
numSleepsDone++) {
sleepmicros(replSnapshotThreadThrottleMicros);
_manager->cleanupUnneededSnapshots();
}
{
stdx::unique_lock<stdx::mutex> lock(newOpMutex);
while (true) {
if (_inShutdown.load())
return;
if (_forcedSnapshotPending.load() || lastTimestamp != lastSetTimestamp) {
_forcedSnapshotPending.store(false);
lastTimestamp = lastSetTimestamp;
break;
}
MONGO_IDLE_THREAD_BLOCK;
newTimestampNotifier.wait(lock);
}
}
while (MONGO_FAIL_POINT(disableSnapshotting)) {
sleepsecs(1);
if (_inShutdown.load()) {
return;
}
}
try {
auto opCtx = client.makeOperationContext();
Lock::GlobalLock globalLock(opCtx.get(), MODE_IS, UINT_MAX);
if (!replCoord->getMemberState().readable()) {
// If our MemberState isn't readable, we may not be in a consistent state so don't
// take snapshots. When we transition into a readable state from a non-readable
// state, a snapshot is forced to ensure we don't miss the latest write. This must
// be checked each time we acquire the global IS lock since that prevents the node
// from transitioning to a !readable() state from a readable() one in the cases
// where we shouldn't be creating a snapshot.
continue;
}
SnapshotName name(0); // assigned real value in block.
{
// Make sure there are no in-flight capped inserts while we create our snapshot.
// This lock cannot be aquired until all writes holding the resource commit/abort.
Lock::ResourceLock cappedInsertLockForOtherDb(
opCtx->lockState(), resourceCappedInFlightForOtherDb, MODE_X);
Lock::ResourceLock cappedInsertLockForLocalDb(
opCtx->lockState(), resourceCappedInFlightForLocalDb, MODE_X);
// Reserve the name immediately before we take our snapshot. This ensures that all
// names that compare lower must be from points in time visible to this named
// snapshot.
name = replCoord->reserveSnapshotName(nullptr);
// This establishes the view that we will name.
_manager->prepareForCreateSnapshot(opCtx.get()).transitional_ignore();
}
auto opTimeOfSnapshot = OpTime();
{
AutoGetCollectionForReadCommand oplog(opCtx.get(),
NamespaceString::kRsOplogNamespace);
invariant(oplog.getCollection());
// Read the latest op from the oplog.
auto cursor = oplog.getCollection()->getCursor(opCtx.get(), /*forward*/ false);
auto record = cursor->next();
if (!record)
continue; // oplog is completely empty.
const auto op = record->data.releaseToBson();
opTimeOfSnapshot = fassertStatusOK(28780, OpTime::parseFromOplogEntry(op));
invariant(!opTimeOfSnapshot.isNull());
}
replCoord->createSnapshot(opCtx.get(), opTimeOfSnapshot, name);
} catch (const WriteConflictException& wce) {
log() << "skipping storage snapshot pass due to write conflict";
continue;
}
}
}
void SnapshotThread::shutdown() {
invariant(_thread.joinable());
{
stdx::lock_guard<stdx::mutex> lock(newOpMutex);
invariant(!_inShutdown.load());
_inShutdown.store(true);
newTimestampNotifier.notify_all();
}
_thread.join();
}
void SnapshotThread::forceSnapshot() {
stdx::lock_guard<stdx::mutex> lock(newOpMutex);
_forcedSnapshotPending.store(true);
newTimestampNotifier.notify_all();
}
std::unique_ptr<SnapshotThread> SnapshotThread::start(ServiceContext* service) {
if (auto manager = service->getGlobalStorageEngine()->getSnapshotManager()) {
return std::unique_ptr<SnapshotThread>(new SnapshotThread(manager));
}
return {};
}
} // namespace repl
} // namespace mongo
|