-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
timeseries_write_util.cpp
1144 lines (1022 loc) · 50.1 KB
/
timeseries_write_util.cpp
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
/**
* Copyright (C) 2023-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.
*/
#include "mongo/db/timeseries/timeseries_write_util.h"
#include <cstdint>
#include <string>
#include <type_traits>
#include <utility>
#include <absl/container/flat_hash_map.h>
#include <absl/meta/type_traits.h>
#include <boost/container/small_vector.hpp>
#include <boost/container/vector.hpp>
#include <boost/cstdint.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/none.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional.hpp>
#include "mongo/base/error_codes.h"
#include "mongo/base/status.h"
#include "mongo/bson/bsonelement.h"
#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/client/dbclient_cursor.h"
#include "mongo/db/catalog/collection_catalog.h"
#include "mongo/db/catalog/collection_operation_source.h"
#include "mongo/db/catalog/collection_write_path.h"
#include "mongo/db/catalog/document_validation.h"
#include "mongo/db/concurrency/exception_util.h"
#include "mongo/db/curop.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/feature_flag.h"
#include "mongo/db/ops/delete_request_gen.h"
#include "mongo/db/ops/update_request.h"
#include "mongo/db/ops/write_ops_exec_util.h"
#include "mongo/db/pipeline/aggregate_command_gen.h"
#include "mongo/db/query/collation/collator_interface.h"
#include "mongo/db/record_id_helpers.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl/tenant_migration_decoration.h"
#include "mongo/db/server_options.h"
#include "mongo/db/storage/snapshot.h"
#include "mongo/db/storage/storage_parameters_gen.h"
#include "mongo/db/storage/write_unit_of_work.h"
#include "mongo/db/timeseries/bucket_catalog/bucket_catalog.h"
#include "mongo/db/timeseries/bucket_catalog/bucket_catalog_helpers.h"
#include "mongo/db/timeseries/bucket_catalog/bucket_catalog_internal.h"
#include "mongo/db/timeseries/bucket_catalog/bucket_identifiers.h"
#include "mongo/db/timeseries/bucket_catalog/bucket_metadata.h"
#include "mongo/db/timeseries/bucket_catalog/execution_stats.h"
#include "mongo/db/timeseries/bucket_catalog/flat_bson.h"
#include "mongo/db/timeseries/bucket_catalog/reopening.h"
#include "mongo/db/timeseries/bucket_compression.h"
#include "mongo/db/timeseries/bucket_compression_failure.h"
#include "mongo/db/timeseries/timeseries_constants.h"
#include "mongo/db/timeseries/timeseries_index_schema_conversion_functions.h"
#include "mongo/db/timeseries/timeseries_options.h"
#include "mongo/db/timeseries/timeseries_update_delete_util.h"
#include "mongo/db/update/document_diff_applier.h"
#include "mongo/db/update/document_diff_serialization.h"
#include "mongo/db/update/update_oplog_entry_serialization.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/decimal_counter.h"
#include "mongo/util/decorable.h"
#include "mongo/util/scopeguard.h"
#include "mongo/util/str.h"
#include "mongo/util/string_map.h"
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage
namespace mongo::timeseries {
namespace {
// Builds the data field of a bucket document. Computes the min and max fields if necessary.
boost::optional<bucket_catalog::MinMax> processTimeseriesMeasurements(
const std::vector<BSONObj>& measurements,
const BSONObj& metadata,
StringDataMap<BSONObjBuilder>& dataBuilders,
const boost::optional<TimeseriesOptions>& options = boost::none,
const boost::optional<const StringDataComparator*>& comparator = boost::none) {
bucket_catalog::MinMax minmax;
bool computeMinmax = options && comparator;
auto metadataElem = metadata.firstElement();
boost::optional<StringData> metaFieldName;
if (metadataElem) {
metaFieldName = metadataElem.fieldNameStringData();
}
DecimalCounter<uint32_t> count;
for (const auto& doc : measurements) {
if (computeMinmax) {
minmax.update(doc, metaFieldName, *comparator);
}
for (const auto& elem : doc) {
auto key = elem.fieldNameStringData();
if (key == metaFieldName) {
continue;
}
dataBuilders[key].appendAs(elem, count);
}
++count;
}
// Rounds the minimum timestamp and updates the min time field.
if (computeMinmax) {
auto minTime = roundTimestampToGranularity(
minmax.min().getField(options->getTimeField()).Date(), *options);
auto controlDoc =
bucket_catalog::buildControlMinTimestampDoc(options->getTimeField(), minTime);
minmax.update(controlDoc, /*metaField=*/boost::none, *comparator);
return minmax;
}
return boost::none;
}
// Builds a complete and new bucket document.
BucketDocument makeNewDocument(const OID& bucketId,
const BSONObj& metadata,
const BSONObj& min,
const BSONObj& max,
StringDataMap<BSONObjBuilder>& dataBuilders,
StringData timeField,
const NamespaceString& nss) {
auto metadataElem = metadata.firstElement();
BSONObjBuilder builder;
builder.append("_id", bucketId);
{
BSONObjBuilder bucketControlBuilder(builder.subobjStart("control"));
bucketControlBuilder.append(kBucketControlVersionFieldName,
kTimeseriesControlUncompressedVersion);
bucketControlBuilder.append(kBucketControlMinFieldName, min);
bucketControlBuilder.append(kBucketControlMaxFieldName, max);
}
if (metadataElem) {
builder.appendAs(metadataElem, kBucketMetaFieldName);
}
{
BSONObjBuilder bucketDataBuilder(builder.subobjStart(kBucketDataFieldName));
for (auto& dataBuilder : dataBuilders) {
bucketDataBuilder.append(dataBuilder.first, dataBuilder.second.obj());
}
}
BucketDocument bucketDoc{builder.obj()};
if (!feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot())) {
return bucketDoc;
}
const bool validateCompression = gValidateTimeseriesCompression.load();
auto compressed = timeseries::compressBucket(
bucketDoc.uncompressedBucket, timeField, nss, validateCompression);
uassert(BucketCompressionFailure{bucketId},
"Failed to compress time-series bucket",
compressed.compressedBucket);
bucketDoc.compressedBucket = std::move(*compressed.compressedBucket);
return bucketDoc;
}
// Makes a write command request base and sets the statement Ids if provided a non-empty vector.
write_ops::WriteCommandRequestBase makeTimeseriesWriteOpBase(std::vector<StmtId>&& stmtIds) {
write_ops::WriteCommandRequestBase base;
// The schema validation configured in the bucket collection is intended for direct
// operations by end users and is not applicable here.
base.setBypassDocumentValidation(true);
if (!stmtIds.empty()) {
base.setStmtIds(std::move(stmtIds));
}
return base;
}
// Generates a delta update oplog entry using the before and after compressed bucket documents.
write_ops::UpdateOpEntry makeTimeseriesCompressedDiffEntry(
OperationContext* opCtx,
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const BSONObj& before,
const BSONObj& after,
const BSONObj& metadata) {
BSONObjBuilder updateBuilder;
{
// Control builder.
BSONObjBuilder controlBuilder(updateBuilder.subobjStart(
str::stream() << doc_diff::kSubDiffSectionFieldPrefix << kBucketControlFieldName));
BSONObj countObj =
BSON(kBucketControlCountFieldName << after.getObjectField(kBucketControlFieldName)
.getField(kBucketControlCountFieldName)
.Int());
controlBuilder.append(doc_diff::kUpdateSectionFieldName, countObj);
if (!batch->min.isEmpty() || !batch->max.isEmpty()) {
if (!batch->min.isEmpty()) {
controlBuilder.append(str::stream() << doc_diff::kSubDiffSectionFieldPrefix
<< kBucketControlMinFieldName,
batch->min);
}
if (!batch->max.isEmpty()) {
controlBuilder.append(str::stream() << doc_diff::kSubDiffSectionFieldPrefix
<< kBucketControlMaxFieldName,
batch->max);
}
}
}
{
// Data builder.
const BSONObj& beforeData = before.getObjectField(kBucketDataFieldName);
const BSONObj& afterData = after.getObjectField(kBucketDataFieldName);
BSONObjBuilder dataBuilder(updateBuilder.subobjStart("sdata"));
BSONObjBuilder newDataFieldsBuilder;
BSONObjBuilder updatedDataFieldsBuilder;
auto beforeIt = beforeData.begin();
auto afterIt = afterData.begin();
while (beforeIt != beforeData.end()) {
invariant(afterIt != afterData.end());
invariant(beforeIt->fieldNameStringData() == afterIt->fieldNameStringData());
if (beforeIt->binaryEqual(*afterIt)) {
// Contents are the same, nothing to diff.
beforeIt++;
afterIt++;
continue;
}
// Generate the binary diff.
int beforeLen = 0;
const char* beforeData = beforeIt->binData(beforeLen);
int afterLen = 0;
const char* afterData = afterIt->binData(afterLen);
int offset = 0;
while (beforeData[offset] == afterData[offset]) {
if (offset == beforeLen - 1 || offset == afterLen - 1) {
break;
}
offset++;
}
BSONObj binaryObj = BSON("o" << offset << "d"
<< BSONBinData(afterData + offset,
afterLen - offset,
BinDataType::BinDataGeneral));
updatedDataFieldsBuilder.append(beforeIt->fieldNameStringData(), binaryObj);
beforeIt++;
afterIt++;
}
// Finish consuming the after iterator, which should only contain new fields at this point
// as we've finished consuming the before iterator.
while (afterIt != afterData.end()) {
// Newly inserted fields are added as DocDiff inserts using the BSONColumn format.
invariant(batch->newFieldNamesToBeInserted.count(afterIt->fieldNameStringData()) == 1);
newDataFieldsBuilder.append(*afterIt);
afterIt++;
}
auto newDataFields = newDataFieldsBuilder.obj();
if (!newDataFields.isEmpty()) {
dataBuilder.append(doc_diff::kInsertSectionFieldName, newDataFields);
}
auto updatedDataFields = updatedDataFieldsBuilder.obj();
if (!updatedDataFields.isEmpty()) {
dataBuilder.append(doc_diff::kBinarySectionFieldName, updatedDataFields);
}
}
write_ops::UpdateModification::DiffOptions options;
options.mustCheckExistenceForInsertOperations =
static_cast<bool>(repl::tenantMigrationInfo(opCtx));
write_ops::UpdateModification u(
updateBuilder.obj(), write_ops::UpdateModification::DeltaTag{}, options);
auto oid = batch->bucketHandle.bucketId.oid;
write_ops::UpdateOpEntry update(BSON("_id" << oid), std::move(u));
invariant(!update.getMulti(), oid.toString());
invariant(!update.getUpsert(), oid.toString());
return update;
}
// Builds the delta update oplog entry from a time-series insert write batch.
write_ops::UpdateOpEntry makeTimeseriesUpdateOpEntry(
OperationContext* opCtx,
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const BSONObj& metadata) {
BSONObjBuilder updateBuilder;
{
if (!batch->min.isEmpty() || !batch->max.isEmpty()) {
BSONObjBuilder controlBuilder(updateBuilder.subobjStart(
str::stream() << doc_diff::kSubDiffSectionFieldPrefix << "control"));
if (!batch->min.isEmpty()) {
controlBuilder.append(
str::stream() << doc_diff::kSubDiffSectionFieldPrefix << "min", batch->min);
}
if (!batch->max.isEmpty()) {
controlBuilder.append(
str::stream() << doc_diff::kSubDiffSectionFieldPrefix << "max", batch->max);
}
}
}
{ // doc_diff::kSubDiffSectionFieldPrefix + <field name> => {<index_0>: ..., <index_1>:}
StringDataMap<BSONObjBuilder> dataFieldBuilders;
auto metadataElem = metadata.firstElement();
DecimalCounter<uint32_t> count(batch->numPreviouslyCommittedMeasurements);
for (const auto& doc : batch->measurements) {
for (const auto& elem : doc) {
auto key = elem.fieldNameStringData();
if (metadataElem && key == metadataElem.fieldNameStringData()) {
continue;
}
auto& builder = dataFieldBuilders[key];
builder.appendAs(elem, count);
}
++count;
}
// doc_diff::kSubDiffSectionFieldPrefix + <field name>
BSONObjBuilder dataBuilder(updateBuilder.subobjStart("sdata"));
BSONObjBuilder newDataFieldsBuilder;
for (auto& pair : dataFieldBuilders) {
// Existing 'data' fields with measurements require different treatment from fields
// not observed before (missing from control.min and control.max).
if (batch->newFieldNamesToBeInserted.count(pair.first)) {
newDataFieldsBuilder.append(pair.first, pair.second.obj());
}
}
auto newDataFields = newDataFieldsBuilder.obj();
if (!newDataFields.isEmpty()) {
dataBuilder.append(doc_diff::kInsertSectionFieldName, newDataFields);
}
for (auto& pair : dataFieldBuilders) {
// Existing 'data' fields with measurements require different treatment from fields
// not observed before (missing from control.min and control.max).
if (!batch->newFieldNamesToBeInserted.count(pair.first)) {
dataBuilder.append(doc_diff::kSubDiffSectionFieldPrefix + pair.first.toString(),
BSON(doc_diff::kInsertSectionFieldName << pair.second.obj()));
}
}
}
write_ops::UpdateModification::DiffOptions options;
options.mustCheckExistenceForInsertOperations =
static_cast<bool>(repl::tenantMigrationInfo(opCtx));
write_ops::UpdateModification u(
updateBuilder.obj(), write_ops::UpdateModification::DeltaTag{}, options);
auto oid = batch->bucketHandle.bucketId.oid;
write_ops::UpdateOpEntry update(BSON("_id" << oid), std::move(u));
invariant(!update.getMulti(), oid.toString());
invariant(!update.getUpsert(), oid.toString());
return update;
}
// Performs the storage write of an update to a time-series bucket document.
void updateTimeseriesDocument(OperationContext* opCtx,
const CollectionPtr& coll,
const write_ops::UpdateCommandRequest& op,
OpDebug* opDebug,
bool fromMigrate,
StmtId stmtId) {
invariant(op.getUpdates().size() == 1);
auto& update = op.getUpdates().front();
invariant(coll->isClustered());
auto recordId = record_id_helpers::keyForOID(update.getQ()["_id"].OID());
auto original = coll->docFor(opCtx, recordId);
CollectionUpdateArgs args{original.value()};
args.criteria = update.getQ();
args.stmtIds = {stmtId};
if (fromMigrate) {
args.source = OperationSource::kFromMigrate;
}
BSONObj updated;
BSONObj diffFromUpdate;
const BSONObj* diffOnIndexes =
collection_internal::kUpdateAllIndexes; // Assume all indexes are affected.
if (update.getU().type() == write_ops::UpdateModification::Type::kDelta) {
diffFromUpdate = update.getU().getDiff();
updated = doc_diff::applyDiff(
original.value(), diffFromUpdate, static_cast<bool>(repl::tenantMigrationInfo(opCtx)));
diffOnIndexes = &diffFromUpdate;
args.update = update_oplog_entry::makeDeltaOplogEntry(diffFromUpdate);
} else if (update.getU().type() == write_ops::UpdateModification::Type::kTransform) {
const auto& transform = update.getU().getTransform();
auto transformed = transform(original.value());
tassert(7667900,
"Could not apply transformation to time series bucket document",
transformed.has_value());
updated = std::move(transformed.value());
args.update = update_oplog_entry::makeReplacementOplogEntry(updated);
} else if (update.getU().type() == write_ops::UpdateModification::Type::kReplacement) {
updated = update.getU().getUpdateReplacement();
args.update = update_oplog_entry::makeReplacementOplogEntry(updated);
} else {
invariant(false, "Unexpected update type");
}
collection_internal::updateDocument(opCtx,
coll,
recordId,
original,
updated,
diffOnIndexes,
nullptr /*indexesAffected*/,
opDebug,
&args);
}
std::shared_ptr<bucket_catalog::WriteBatch>& extractFromSelf(
std::shared_ptr<bucket_catalog::WriteBatch>& batch) {
return batch;
}
BSONObj getSuitableBucketForReopening(OperationContext* opCtx,
const Collection* bucketsColl,
const TimeseriesOptions& options,
bucket_catalog::ReopeningContext& reopeningContext) {
return visit(
OverloadedVisitor{
[](const std::monostate&) { return BSONObj{}; },
[&](const OID& bucketId) {
reopeningContext.fetchedBucket = true;
return DBDirectClient{opCtx}.findOne(bucketsColl->ns(), BSON("_id" << bucketId));
},
[&](const std::vector<BSONObj>& pipeline) {
// Ensure we have a index on meta and time for the time-series collection before
// performing the query. Without the index we will perform a full collection scan
// which could cause us to take a performance hit.
if (auto index = getIndexSupportingReopeningQuery(
opCtx, bucketsColl->getIndexCatalog(), options)) {
// Resort to Query-Based reopening approach.
reopeningContext.queriedBucket = true;
DBDirectClient client{opCtx};
// Run an aggregation to find a suitable bucket to reopen.
AggregateCommandRequest aggRequest(bucketsColl->ns(), pipeline);
aggRequest.setHint(index);
// TODO SERVER-86094: remove after fixing perf regression.
query_settings::QuerySettings querySettings;
querySettings.setQueryFramework(QueryFrameworkControlEnum::kForceClassicEngine);
aggRequest.setQuerySettings(querySettings);
auto swCursor = DBClientCursor::fromAggregationRequest(
&client, aggRequest, false /* secondaryOk */, false /* useExhaust */);
if (swCursor.isOK() && swCursor.getValue()->more()) {
return swCursor.getValue()->next();
}
}
return BSONObj{};
},
},
reopeningContext.candidate);
}
StatusWith<bucket_catalog::InsertResult> attemptInsertIntoBucketWithReopening(
OperationContext* opCtx,
bucket_catalog::BucketCatalog& bucketCatalog,
const NamespaceString& viewNs,
const Collection* bucketsColl,
const TimeseriesOptions& options,
const BSONObj& measurementDoc,
bucket_catalog::CombineWithInsertsFromOtherClients combine) {
auto swResult = bucket_catalog::tryInsert(opCtx,
bucketCatalog,
viewNs,
bucketsColl->getDefaultCollator(),
options,
measurementDoc,
combine);
if (!swResult.isOK()) {
return swResult;
}
return visit(
OverloadedVisitor{
[&](const bucket_catalog::SuccessfulInsertion&)
-> StatusWith<bucket_catalog::InsertResult> { return std::move(swResult); },
[&](bucket_catalog::ReopeningContext& reopeningContext) {
auto suitableBucket =
getSuitableBucketForReopening(opCtx, bucketsColl, options, reopeningContext);
if (!suitableBucket.isEmpty()) {
reopeningContext.bucketToReopen = bucket_catalog::BucketToReopen{
suitableBucket, [&](OperationContext* opCtx, const BSONObj& bucketDoc) {
return bucketsColl->checkValidation(opCtx, bucketDoc);
}};
}
return bucket_catalog::insertWithReopeningContext(opCtx,
bucketCatalog,
viewNs,
bucketsColl->getDefaultCollator(),
options,
measurementDoc,
combine,
reopeningContext);
},
[](bucket_catalog::InsertWaiter& waiter) -> StatusWith<bucket_catalog::InsertResult> {
// Need to wait for another operation to finish, then retry. This could be another
// reopening request or a previously prepared write batch for the same series
// (metaField value). The easiest way to retry here is to return a write conflict.
bucket_catalog::waitToInsert(&waiter);
return Status{ErrorCodes::WriteConflict, "waited to retry"};
},
},
swResult.getValue());
}
} // namespace
write_ops::UpdateCommandRequest buildSingleUpdateOp(const write_ops::UpdateCommandRequest& wholeOp,
size_t opIndex) {
write_ops::UpdateCommandRequest singleUpdateOp(wholeOp.getNamespace(),
{wholeOp.getUpdates()[opIndex]});
auto commandBase = singleUpdateOp.getWriteCommandRequestBase();
commandBase.setOrdered(wholeOp.getOrdered());
commandBase.setBypassDocumentValidation(wholeOp.getBypassDocumentValidation());
return singleUpdateOp;
}
void assertTimeseriesBucketsCollection(const Collection* bucketsColl) {
uassert(
8555700,
"Catalog changed during operation, could not find time series buckets collection for write",
bucketsColl);
uassert(8555701,
"Catalog changed during operation, missing time-series options",
bucketsColl->getTimeseriesOptions());
}
BucketDocument makeNewDocumentForWrite(std::shared_ptr<bucket_catalog::WriteBatch> batch,
const BSONObj& metadata) {
StringDataMap<BSONObjBuilder> dataBuilders;
processTimeseriesMeasurements(
{batch->measurements.begin(), batch->measurements.end()}, metadata, dataBuilders);
return makeNewDocument(batch->bucketHandle.bucketId.oid,
metadata,
batch->min,
batch->max,
dataBuilders,
batch->timeField,
batch->bucketHandle.bucketId.ns);
}
BucketDocument makeNewDocumentForWrite(
const NamespaceString& nss,
const OID& bucketId,
const std::vector<BSONObj>& measurements,
const BSONObj& metadata,
const TimeseriesOptions& options,
const boost::optional<const StringDataComparator*>& comparator) {
StringDataMap<BSONObjBuilder> dataBuilders;
auto minmax =
processTimeseriesMeasurements(measurements, metadata, dataBuilders, options, comparator);
invariant(minmax);
return makeNewDocument(bucketId,
metadata,
minmax->min(),
minmax->max(),
dataBuilders,
options.getTimeField(),
nss);
}
BSONObj makeBucketDocument(const std::vector<BSONObj>& measurements,
const NamespaceString& nss,
const TimeseriesOptions& options,
const StringDataComparator* comparator) {
std::vector<write_ops::InsertCommandRequest> insertOps;
auto res = uassertStatusOK(bucket_catalog::internal::extractBucketingParameters(
nss, comparator, options, measurements[0]));
auto time = res.second;
auto [oid, _] = bucket_catalog::internal::generateBucketOID(time, options);
BucketDocument bucketDoc = makeNewDocumentForWrite(
nss, oid, measurements, res.first.metadata.toBSON(), options, comparator);
invariant(bucketDoc.compressedBucket ||
!feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot()));
if (bucketDoc.compressedBucket) {
return *bucketDoc.compressedBucket;
}
return bucketDoc.uncompressedBucket;
}
std::variant<write_ops::UpdateCommandRequest, write_ops::DeleteCommandRequest> makeModificationOp(
const OID& bucketId, const CollectionPtr& coll, const std::vector<BSONObj>& measurements) {
// A bucket will be fully deleted if no measurements are passed in.
if (measurements.empty()) {
write_ops::DeleteOpEntry deleteEntry(BSON("_id" << bucketId), false);
write_ops::DeleteCommandRequest op(coll->ns(), {deleteEntry});
return op;
}
auto timeseriesOptions = coll->getTimeseriesOptions();
invariant(timeseriesOptions);
auto metaFieldName = timeseriesOptions->getMetaField();
auto metadata = [&] {
if (!metaFieldName) { // Collection has no metadata field.
return BSONObj();
}
// Look for the metadata field on this bucket and return it if present.
auto metaField = measurements[0].getField(*metaFieldName);
return metaField ? metaField.wrap() : BSONObj();
}();
BucketDocument bucketDoc = makeNewDocumentForWrite(coll->ns(),
bucketId,
measurements,
metadata,
*timeseriesOptions,
coll->getDefaultCollator());
BSONObj bucketToReplace = bucketDoc.uncompressedBucket;
invariant(bucketDoc.compressedBucket ||
!feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot()));
if (bucketDoc.compressedBucket) {
bucketToReplace = *bucketDoc.compressedBucket;
}
write_ops::UpdateModification u(bucketToReplace);
write_ops::UpdateOpEntry updateEntry(BSON("_id" << bucketId), std::move(u));
write_ops::UpdateCommandRequest op(coll->ns(), {updateEntry});
return op;
}
write_ops::UpdateOpEntry makeTimeseriesTransformationOpEntry(
OperationContext* opCtx,
const OID& bucketId,
write_ops::UpdateModification::TransformFunc transformationFunc) {
write_ops::UpdateModification u(std::move(transformationFunc));
write_ops::UpdateOpEntry update(BSON("_id" << bucketId), std::move(u));
invariant(!update.getMulti(), bucketId.toString());
invariant(!update.getUpsert(), bucketId.toString());
return update;
}
void getOpTimeAndElectionId(OperationContext* opCtx,
boost::optional<repl::OpTime>* opTime,
boost::optional<OID>* electionId) {
auto* replCoord = repl::ReplicationCoordinator::get(opCtx->getServiceContext());
const auto isReplSet = replCoord->getSettings().isReplSet();
*opTime = isReplSet
? boost::make_optional(repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp())
: boost::none;
*electionId = isReplSet ? boost::make_optional(replCoord->getElectionId()) : boost::none;
}
write_ops::InsertCommandRequest makeTimeseriesInsertOp(
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const NamespaceString& bucketsNs,
const BSONObj& metadata,
std::vector<StmtId>&& stmtIds) {
BucketDocument bucketDoc = makeNewDocumentForWrite(batch, metadata);
BSONObj bucketToInsert = bucketDoc.uncompressedBucket;
if (feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot())) {
invariant(bucketDoc.compressedBucket);
batch->uncompressed = bucketDoc.uncompressedBucket.getOwned();
}
if (bucketDoc.compressedBucket) {
batch->compressed = bucketDoc.compressedBucket->getOwned();
bucketToInsert = *bucketDoc.compressedBucket;
}
write_ops::InsertCommandRequest op{bucketsNs, {bucketToInsert}};
op.setWriteCommandRequestBase(makeTimeseriesWriteOpBase(std::move(stmtIds)));
return op;
}
write_ops::UpdateCommandRequest makeTimeseriesUpdateOp(
OperationContext* opCtx,
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const NamespaceString& bucketsNs,
const BSONObj& metadata,
std::vector<StmtId>&& stmtIds) {
if (!feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot())) {
write_ops::UpdateCommandRequest op(bucketsNs,
{makeTimeseriesUpdateOpEntry(opCtx, batch, metadata)});
op.setWriteCommandRequestBase(makeTimeseriesWriteOpBase(std::move(stmtIds)));
return op;
}
auto updateMod = makeTimeseriesUpdateOpEntry(opCtx, batch, metadata).getU();
auto updated = doc_diff::applyDiff(batch->uncompressed,
updateMod.getDiff(),
updateMod.mustCheckExistenceForInsertOperations());
// Hold the uncompressed bucket document that's currently on-disk prior to this write batch
// running.
auto before = std::move(batch->uncompressed);
auto compressionResult = timeseries::compressBucket(
updated, batch->timeField, bucketsNs, gValidateTimeseriesCompression.load());
uassert(BucketCompressionFailure{batch->bucketHandle.bucketId.oid},
"Failed to compress time-series bucket",
compressionResult.compressedBucket);
batch->uncompressed = updated;
batch->compressed = *compressionResult.compressedBucket;
auto after = compressionResult.compressedBucket ? *compressionResult.compressedBucket : updated;
auto bucketTransformationFunc = [before = std::move(before), after = std::move(after)](
const BSONObj& bucketDoc) -> boost::optional<BSONObj> {
// Make sure the document hasn't changed since we read it into the BucketCatalog.
// This should not happen, but since we can double-check it here, we can guard
// against the missed update that would result from simply replacing with 'after'.
if (!timeseries::decompressBucket(bucketDoc).value_or(bucketDoc).binaryEqual(before)) {
throwWriteConflictException("Bucket document changed between initial read and update");
}
return after;
};
auto updates = makeTimeseriesTransformationOpEntry(
opCtx,
/*bucketId=*/batch->bucketHandle.bucketId.oid,
/*transformationFunc=*/std::move(bucketTransformationFunc));
write_ops::UpdateCommandRequest op(bucketsNs, {updates});
op.setWriteCommandRequestBase(makeTimeseriesWriteOpBase(std::move(stmtIds)));
return op;
}
write_ops::UpdateCommandRequest makeTimeseriesDecompressAndUpdateOp(
OperationContext* opCtx,
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const NamespaceString& bucketsNs,
const BSONObj& metadata,
std::vector<StmtId>&& stmtIds) {
invariant(feature_flags::gTimeseriesAlwaysUseCompressedBuckets.isEnabled(
serverGlobalParams.featureCompatibility.acquireFCVSnapshot()));
// Generate the diff and apply it against the previously decompressed bucket document.
auto updateMod = makeTimeseriesUpdateOpEntry(opCtx, batch, metadata).getU();
auto diff = updateMod.getDiff();
auto updated = doc_diff::applyDiff(
batch->uncompressed, diff, updateMod.mustCheckExistenceForInsertOperations());
// Holds the compressed bucket document that's currently on-disk prior to this write batch
// running.
auto before = std::move(*batch->compressed);
CompressionResult compressionResult = timeseries::compressBucket(
updated, batch->timeField, bucketsNs, gValidateTimeseriesCompression.load());
uassert(BucketCompressionFailure{batch->bucketHandle.bucketId.oid},
"Failed to compress time-series bucket",
compressionResult.compressedBucket);
batch->uncompressed = updated;
batch->compressed = *compressionResult.compressedBucket;
// Generates a delta update request using the before and after compressed bucket documents.
auto after = *compressionResult.compressedBucket;
const auto updateEntry =
makeTimeseriesCompressedDiffEntry(opCtx, batch, before, after, metadata);
write_ops::UpdateCommandRequest op(bucketsNs, {updateEntry});
op.setWriteCommandRequestBase(makeTimeseriesWriteOpBase(std::move(stmtIds)));
return op;
}
StatusWith<bucket_catalog::InsertResult> attemptInsertIntoBucket(
OperationContext* opCtx,
bucket_catalog::BucketCatalog& bucketCatalog,
const NamespaceString& viewNs,
const Collection* bucketsColl,
TimeseriesOptions& timeSeriesOptions,
const BSONObj& measurementDoc,
BucketReopeningPermittance reopening,
bucket_catalog::CombineWithInsertsFromOtherClients combine) {
switch (reopening) {
case BucketReopeningPermittance::kAllowed:
while (true) {
auto result = attemptInsertIntoBucketWithReopening(opCtx,
bucketCatalog,
viewNs,
bucketsColl,
timeSeriesOptions,
measurementDoc,
combine);
if (!result.isOK() && result.getStatus().code() == ErrorCodes::WriteConflict) {
// If there is an era offset (between the bucket we want to reopen and the
// catalog's current era), we could hit a WriteConflict error indicating we will
// need to refetch a bucket document as it is potentially stale.
continue;
}
return result;
}
case BucketReopeningPermittance::kDisallowed:
return bucket_catalog::insert(opCtx,
bucketCatalog,
viewNs,
bucketsColl->getDefaultCollator(),
timeSeriesOptions,
measurementDoc,
combine);
}
MONGO_UNREACHABLE;
}
void makeWriteRequest(OperationContext* opCtx,
std::shared_ptr<bucket_catalog::WriteBatch> batch,
const BSONObj& metadata,
TimeseriesStmtIds& stmtIds,
const NamespaceString& bucketsNs,
std::vector<write_ops::InsertCommandRequest>* insertOps,
std::vector<write_ops::UpdateCommandRequest>* updateOps) {
if (batch->numPreviouslyCommittedMeasurements == 0) {
insertOps->push_back(makeTimeseriesInsertOp(
batch, bucketsNs, metadata, std::move(stmtIds[batch->bucketHandle.bucketId.oid])));
return;
}
if (batch->compressed) {
updateOps->push_back(makeTimeseriesDecompressAndUpdateOp(
opCtx,
batch,
bucketsNs,
metadata,
std::move(stmtIds[batch->bucketHandle.bucketId.oid])));
} else {
updateOps->push_back(
makeTimeseriesUpdateOp(opCtx,
batch,
bucketsNs,
metadata,
std::move(stmtIds[batch->bucketHandle.bucketId.oid])));
}
}
TimeseriesBatches insertIntoBucketCatalogForUpdate(OperationContext* opCtx,
bucket_catalog::BucketCatalog& bucketCatalog,
const CollectionPtr& bucketsColl,
const std::vector<BSONObj>& measurements,
const NamespaceString& bucketsNs,
TimeseriesOptions& timeSeriesOptions) {
TimeseriesBatches batches;
auto viewNs = bucketsNs.getTimeseriesViewNamespace();
for (const auto& measurement : measurements) {
auto result = uassertStatusOK(
attemptInsertIntoBucket(opCtx,
bucketCatalog,
viewNs,
bucketsColl.get(),
timeSeriesOptions,
measurement,
BucketReopeningPermittance::kDisallowed,
bucket_catalog::CombineWithInsertsFromOtherClients::kDisallow));
auto* insertResult = get_if<bucket_catalog::SuccessfulInsertion>(&result);
invariant(insertResult);
batches.emplace_back(std::move(insertResult->batch));
}
return batches;
}
void performAtomicWrites(
OperationContext* opCtx,
const CollectionPtr& coll,
const RecordId& recordId,
const boost::optional<std::variant<write_ops::UpdateCommandRequest,
write_ops::DeleteCommandRequest>>& modificationOp,
const std::vector<write_ops::InsertCommandRequest>& insertOps,
const std::vector<write_ops::UpdateCommandRequest>& updateOps,
bool fromMigrate,
StmtId stmtId) {
tassert(
7655102, "must specify at least one type of write", modificationOp || !insertOps.empty());
NamespaceString ns = coll->ns();
DisableDocumentValidation disableDocumentValidation{opCtx};
write_ops_exec::LastOpFixer lastOpFixer{opCtx};
lastOpFixer.startingOp(ns);
auto curOp = CurOp::get(opCtx);
curOp->raiseDbProfileLevel(CollectionCatalog::get(opCtx)->getDatabaseProfileLevel(ns.dbName()));
write_ops_exec::assertCanWrite_inlock(opCtx, ns);
// Groups all operations in one or several chained oplog entries to ensure the writes are
// replicated atomically.
auto groupOplogEntries =
!opCtx->getTxnNumber() && (!insertOps.empty() || !updateOps.empty()) && modificationOp;
WriteUnitOfWork wuow{opCtx, groupOplogEntries};
if (modificationOp) {
visit(
OverloadedVisitor{[&](const write_ops::UpdateCommandRequest& updateOp) {
updateTimeseriesDocument(
opCtx, coll, updateOp, &curOp->debug(), fromMigrate, stmtId);
},
[&](const write_ops::DeleteCommandRequest& deleteOp) {
invariant(deleteOp.getDeletes().size() == 1);
auto deleteId = record_id_helpers::keyForOID(
deleteOp.getDeletes().front().getQ()["_id"].OID());
invariant(recordId == deleteId);
collection_internal::deleteDocument(
opCtx, coll, stmtId, recordId, &curOp->debug(), fromMigrate);
}},
*modificationOp);
}
if (!insertOps.empty()) {
std::vector<InsertStatement> insertStatements;
for (auto& op : insertOps) {
invariant(op.getDocuments().size() == 1);
if (modificationOp) {
insertStatements.emplace_back(op.getDocuments().front());
} else {
// Appends the stmtId for upsert.
insertStatements.emplace_back(stmtId, op.getDocuments().front());
}
}
uassertStatusOK(collection_internal::insertDocuments(
opCtx, coll, insertStatements.begin(), insertStatements.end(), &curOp->debug()));
}
for (auto& updateOp : updateOps) {
updateTimeseriesDocument(opCtx, coll, updateOp, &curOp->debug(), fromMigrate, stmtId);
}
wuow.commit();
lastOpFixer.finishedOpSuccessfully();
}
void commitTimeseriesBucketsAtomically(
OperationContext* opCtx,
bucket_catalog::BucketCatalog& sideBucketCatalog,
const CollectionPtr& coll,
const RecordId& recordId,
const boost::optional<std::variant<write_ops::UpdateCommandRequest,
write_ops::DeleteCommandRequest>>& modificationOp,
TimeseriesBatches* batches,
const NamespaceString& bucketsNs,
bool fromMigrate,
StmtId stmtId,
std::set<OID>* bucketIds) {
auto batchesToCommit = determineBatchesToCommit(*batches, extractFromSelf);
if (batchesToCommit.empty()) {
return;
}
Status abortStatus = Status::OK();
ScopeGuard batchGuard{[&] {
for (auto batch : batchesToCommit) {
if (batch.get()) {
abort(sideBucketCatalog, batch, abortStatus);
}
}
}};