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
|
/**
* Copyright (C) 2020-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/catalog/catalog_test_fixture.h"
#include "mongo/db/catalog/create_collection.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/timeseries/bucket_catalog.h"
#include "mongo/db/views/view_catalog.h"
#include "mongo/stdx/future.h"
#include "mongo/unittest/bson_test_util.h"
#include "mongo/unittest/death_test.h"
#include "mongo/util/concurrency/thread_pool.h"
namespace mongo {
namespace {
class BucketCatalogTest : public CatalogTestFixture {
protected:
class Task {
AtomicWord<bool> _running{false};
stdx::packaged_task<void()> _task;
stdx::future<void> _future;
stdx::thread _taskThread;
public:
Task(std::function<void()>&& fn);
~Task();
const stdx::future<void>& future();
};
void setUp() override;
std::pair<ServiceContext::UniqueClient, ServiceContext::UniqueOperationContext>
_makeOperationContext();
virtual BSONObj _makeTimeseriesOptionsForCreate() const;
TimeseriesOptions _getTimeseriesOptions(const NamespaceString& ns) const;
const CollatorInterface* _getCollator(const NamespaceString& ns) const;
void _commit(const std::shared_ptr<BucketCatalog::WriteBatch>& batch,
uint16_t numPreviouslyCommittedMeasurements,
size_t expectedBatchSize = 1);
void _insertOneAndCommit(const NamespaceString& ns,
uint16_t numPreviouslyCommittedMeasurements);
long long _getNumWaits(const NamespaceString& ns);
OperationContext* _opCtx;
BucketCatalog* _bucketCatalog;
StringData _timeField = "time";
StringData _metaField = "meta";
NamespaceString _ns1{"bucket_catalog_test_1", "t_1"};
NamespaceString _ns2{"bucket_catalog_test_1", "t_2"};
NamespaceString _ns3{"bucket_catalog_test_2", "t_1"};
};
class BucketCatalogWithoutMetadataTest : public BucketCatalogTest {
protected:
BSONObj _makeTimeseriesOptionsForCreate() const override;
};
BucketCatalogTest::Task::Task(std::function<void()>&& fn)
: _task{[this, fn = std::move(fn)]() {
_running.store(true);
fn();
}},
_future{_task.get_future()},
_taskThread{std::move(_task)} {
while (!_running.load()) {
stdx::this_thread::yield();
}
}
BucketCatalogTest::Task::~Task() {
_taskThread.join();
}
const stdx::future<void>& BucketCatalogTest::Task::future() {
return _future;
}
void BucketCatalogTest::setUp() {
CatalogTestFixture::setUp();
_opCtx = operationContext();
_bucketCatalog = &BucketCatalog::get(_opCtx);
for (const auto& ns : {_ns1, _ns2, _ns3}) {
ASSERT_OK(createCollection(
_opCtx,
ns.db().toString(),
BSON("create" << ns.coll() << "timeseries" << _makeTimeseriesOptionsForCreate())));
}
}
std::pair<ServiceContext::UniqueClient, ServiceContext::UniqueOperationContext>
BucketCatalogTest::_makeOperationContext() {
auto client = getServiceContext()->makeClient("BucketCatalogTest");
auto opCtx = client->makeOperationContext();
return {std::move(client), std::move(opCtx)};
}
BSONObj BucketCatalogTest::_makeTimeseriesOptionsForCreate() const {
return BSON("timeField" << _timeField << "metaField" << _metaField);
}
BSONObj BucketCatalogWithoutMetadataTest::_makeTimeseriesOptionsForCreate() const {
return BSON("timeField" << _timeField);
}
TimeseriesOptions BucketCatalogTest::_getTimeseriesOptions(const NamespaceString& ns) const {
AutoGetCollection autoColl(_opCtx, ns.makeTimeseriesBucketsNamespace(), MODE_IS);
return *autoColl->getTimeseriesOptions();
}
const CollatorInterface* BucketCatalogTest::_getCollator(const NamespaceString& ns) const {
AutoGetCollection autoColl(_opCtx, ns.makeTimeseriesBucketsNamespace(), MODE_IS);
return autoColl->getDefaultCollator();
}
void BucketCatalogTest::_commit(const std::shared_ptr<BucketCatalog::WriteBatch>& batch,
uint16_t numPreviouslyCommittedMeasurements,
size_t expectedBatchSize) {
ASSERT(batch->claimCommitRights());
_bucketCatalog->prepareCommit(batch);
ASSERT_EQ(batch->measurements().size(), expectedBatchSize);
ASSERT_EQ(batch->numPreviouslyCommittedMeasurements(), numPreviouslyCommittedMeasurements);
_bucketCatalog->finish(batch, {});
}
void BucketCatalogTest::_insertOneAndCommit(const NamespaceString& ns,
uint16_t numPreviouslyCommittedMeasurements) {
auto result = _bucketCatalog->insert(_opCtx,
ns,
_getCollator(ns),
_getTimeseriesOptions(ns),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto& batch = result.getValue();
_commit(batch, numPreviouslyCommittedMeasurements);
}
long long BucketCatalogTest::_getNumWaits(const NamespaceString& ns) {
BSONObjBuilder builder;
_bucketCatalog->appendExecutionStats(ns, &builder);
return builder.obj().getIntField("numWaits");
}
TEST_F(BucketCatalogTest, InsertIntoSameBucket) {
// The first insert should be able to take commit rights, but batch is still active
auto result1 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto batch1 = result1.getValue();
ASSERT(batch1->claimCommitRights());
ASSERT(batch1->active());
// A subsequent insert into the same bucket should land in the same batch, but not be able to
// claim commit rights
auto result2 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto batch2 = result2.getValue();
ASSERT_EQ(batch1, batch2);
ASSERT(!batch2->claimCommitRights());
// The batch hasn't actually been committed yet.
ASSERT(!batch1->finished());
_bucketCatalog->prepareCommit(batch1);
// Still not finished, but no longer active.
ASSERT(!batch1->finished());
ASSERT(!batch1->active());
// The batch should contain both documents since they belong in the same bucket and happened
// in the same commit epoch. Nothing else has been committed in this bucket yet.
ASSERT_EQ(batch1->measurements().size(), 2);
ASSERT_EQ(batch1->numPreviouslyCommittedMeasurements(), 0);
// Once the commit has occurred, the waiter should be notified.
_bucketCatalog->finish(batch1, {});
ASSERT(batch2->finished());
auto result3 = batch2->getResult();
ASSERT_OK(result3.getStatus());
}
TEST_F(BucketCatalogTest, GetMetadataReturnsEmptyDocOnMissingBucket) {
auto batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch->claimCommitRights());
auto bucket = batch->bucket();
_bucketCatalog->abort(batch);
ASSERT_BSONOBJ_EQ(BSONObj(), _bucketCatalog->getMetadata(bucket));
}
TEST_F(BucketCatalogTest, InsertIntoDifferentBuckets) {
auto result1 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField << "123"),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result2 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField << BSONObj()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result3 =
_bucketCatalog->insert(_opCtx,
_ns2,
_getCollator(_ns2),
_getTimeseriesOptions(_ns2),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
// Inserts should all be into three distinct buckets (and therefore batches).
ASSERT_NE(result1.getValue(), result2.getValue());
ASSERT_NE(result1.getValue(), result3.getValue());
ASSERT_NE(result2.getValue(), result3.getValue());
// Check metadata in buckets.
ASSERT_BSONOBJ_EQ(BSON(_metaField << "123"),
_bucketCatalog->getMetadata(result1.getValue()->bucket()));
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSONObj()),
_bucketCatalog->getMetadata(result2.getValue()->bucket()));
ASSERT(_bucketCatalog->getMetadata(result3.getValue()->bucket()).isEmpty());
// Committing one bucket should only return the one document in that bucket and should not
// affect the other bucket.
for (const auto& batch : {result1.getValue(), result2.getValue(), result3.getValue()}) {
_commit(batch, 0);
}
}
TEST_F(BucketCatalogTest, InsertIntoSameBucketArray) {
auto result1 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField << BSON_ARRAY(BSON("a" << 0 << "b" << 1))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result2 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField << BSON_ARRAY(BSON("b" << 1 << "a" << 0))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_EQ(result1.getValue(), result2.getValue());
// Check metadata in buckets.
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSON_ARRAY(BSON("a" << 0 << "b" << 1))),
_bucketCatalog->getMetadata(result1.getValue()->bucket()));
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSON_ARRAY(BSON("a" << 0 << "b" << 1))),
_bucketCatalog->getMetadata(result2.getValue()->bucket()));
}
TEST_F(BucketCatalogTest, InsertIntoSameBucketObjArray) {
auto result1 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField
<< BSONObj(BSON("c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1)
<< BSON("f" << 1 << "g" << 0))))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result2 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField
<< BSONObj(BSON("c" << BSON_ARRAY(BSON("b" << 1 << "a" << 0)
<< BSON("g" << 0 << "f" << 1))))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_EQ(result1.getValue(), result2.getValue());
// Check metadata in buckets.
ASSERT_BSONOBJ_EQ(
BSON(_metaField << BSONObj(BSON(
"c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1) << BSON("f" << 1 << "g" << 0))))),
_bucketCatalog->getMetadata(result1.getValue()->bucket()));
ASSERT_BSONOBJ_EQ(
BSON(_metaField << BSONObj(BSON(
"c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1) << BSON("f" << 1 << "g" << 0))))),
_bucketCatalog->getMetadata(result2.getValue()->bucket()));
}
TEST_F(BucketCatalogTest, InsertIntoSameBucketNestedArray) {
auto result1 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField
<< BSONObj(BSON("c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1)
<< BSON_ARRAY("123"
<< "456"))))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result2 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField
<< BSONObj(BSON("c" << BSON_ARRAY(BSON("b" << 1 << "a" << 0)
<< BSON_ARRAY("123"
<< "456"))))),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_EQ(result1.getValue(), result2.getValue());
// Check metadata in buckets.
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSONObj(BSON("c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1)
<< BSON_ARRAY("123"
<< "456"))))),
_bucketCatalog->getMetadata(result1.getValue()->bucket()));
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSONObj(BSON("c" << BSON_ARRAY(BSON("a" << 0 << "b" << 1)
<< BSON_ARRAY("123"
<< "456"))))),
_bucketCatalog->getMetadata(result2.getValue()->bucket()));
}
TEST_F(BucketCatalogTest, InsertNullAndMissingMetaFieldIntoDifferentBuckets) {
auto result1 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << _metaField << BSONNULL),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto result2 =
_bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
// Inserts should all be into three distinct buckets (and therefore batches).
ASSERT_NE(result1.getValue(), result2.getValue());
// Check metadata in buckets.
ASSERT_BSONOBJ_EQ(BSON(_metaField << BSONNULL),
_bucketCatalog->getMetadata(result1.getValue()->bucket()));
ASSERT(_bucketCatalog->getMetadata(result2.getValue()->bucket()).isEmpty());
// Committing one bucket should only return the one document in that bucket and should not
// affect the other bucket.
for (const auto& batch : {result1.getValue(), result2.getValue()}) {
_commit(batch, 0);
}
}
TEST_F(BucketCatalogTest, NumCommittedMeasurementsAccumulates) {
// The numCommittedMeasurements returned when committing should accumulate as more entries in
// the bucket are committed.
_insertOneAndCommit(_ns1, 0);
_insertOneAndCommit(_ns1, 1);
}
TEST_F(BucketCatalogTest, ClearNamespaceBuckets) {
_insertOneAndCommit(_ns1, 0);
_insertOneAndCommit(_ns2, 0);
_bucketCatalog->clear(_ns1);
_insertOneAndCommit(_ns1, 0);
_insertOneAndCommit(_ns2, 1);
}
TEST_F(BucketCatalogTest, ClearDatabaseBuckets) {
_insertOneAndCommit(_ns1, 0);
_insertOneAndCommit(_ns2, 0);
_insertOneAndCommit(_ns3, 0);
_bucketCatalog->clear(_ns1.db());
_insertOneAndCommit(_ns1, 0);
_insertOneAndCommit(_ns2, 0);
_insertOneAndCommit(_ns3, 1);
}
TEST_F(BucketCatalogTest, InsertBetweenPrepareAndFinish) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch1->claimCommitRights());
_bucketCatalog->prepareCommit(batch1);
ASSERT_EQ(batch1->measurements().size(), 1);
ASSERT_EQ(batch1->numPreviouslyCommittedMeasurements(), 0);
// Insert before finish so there's a second batch live at the same time.
auto batch2 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_NE(batch1, batch2);
_bucketCatalog->finish(batch1, {});
ASSERT(batch1->finished());
// Verify the second batch still commits one doc, and that the first batch only commited one.
_commit(batch2, 1);
}
DEATH_TEST_F(BucketCatalogTest, CannotCommitWithoutRights, "invariant") {
auto result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto& batch = result.getValue();
_bucketCatalog->prepareCommit(batch);
}
DEATH_TEST_F(BucketCatalogTest, CannotFinishUnpreparedBatch, "invariant") {
auto result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto& batch = result.getValue();
ASSERT(batch->claimCommitRights());
_bucketCatalog->finish(batch, {});
}
TEST_F(BucketCatalogWithoutMetadataTest, GetMetadataReturnsEmptyDoc) {
auto batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_BSONOBJ_EQ(BSONObj(), _bucketCatalog->getMetadata(batch->bucket()));
_commit(batch, 0);
}
TEST_F(BucketCatalogWithoutMetadataTest, CommitReturnsNewFields) {
// Creating a new bucket should return all fields from the initial measurement.
auto result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << "a" << 0),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_OK(result);
auto batch = result.getValue();
auto oldId = batch->bucket()->id();
_commit(batch, 0);
ASSERT_EQ(2U, batch->newFieldNamesToBeInserted().size()) << batch->toBSON();
ASSERT(batch->newFieldNamesToBeInserted().count(_timeField)) << batch->toBSON();
ASSERT(batch->newFieldNamesToBeInserted().count("a")) << batch->toBSON();
// Inserting a new measurement with the same fields should return an empty set of new fields.
result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << "a" << 1),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_OK(result);
batch = result.getValue();
_commit(batch, 1);
ASSERT_EQ(0U, batch->newFieldNamesToBeInserted().size()) << batch->toBSON();
// Insert a new measurement with the a new field.
result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << "a" << 2 << "b" << 2),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_OK(result);
batch = result.getValue();
_commit(batch, 2);
ASSERT_EQ(1U, batch->newFieldNamesToBeInserted().size()) << batch->toBSON();
ASSERT(batch->newFieldNamesToBeInserted().count("b")) << batch->toBSON();
// Fill up the bucket.
for (auto i = 3; i < gTimeseriesBucketMaxCount; ++i) {
result = _bucketCatalog->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << "a" << i),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
ASSERT_OK(result);
batch = result.getValue();
_commit(batch, i);
ASSERT_EQ(0U, batch->newFieldNamesToBeInserted().size()) << i << ":" << batch->toBSON();
}
// When a bucket overflows, committing to the new overflow bucket should return the fields of
// the first measurement as new fields.
auto result2 = _bucketCatalog->insert(
_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now() << "a" << gTimeseriesBucketMaxCount),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow);
auto& batch2 = result2.getValue();
ASSERT_NE(oldId, batch2->bucket()->id());
_commit(batch2, 0);
ASSERT_EQ(2U, batch2->newFieldNamesToBeInserted().size()) << batch2->toBSON();
ASSERT(batch2->newFieldNamesToBeInserted().count(_timeField)) << batch2->toBSON();
ASSERT(batch2->newFieldNamesToBeInserted().count("a")) << batch2->toBSON();
}
TEST_F(BucketCatalogTest, AbortBatchOnBucketWithPreparedCommit) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch1->claimCommitRights());
_bucketCatalog->prepareCommit(batch1);
ASSERT_EQ(batch1->measurements().size(), 1);
ASSERT_EQ(batch1->numPreviouslyCommittedMeasurements(), 0);
// Insert before finish so there's a second batch live at the same time.
auto batch2 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_NE(batch1, batch2);
ASSERT(batch2->claimCommitRights());
_bucketCatalog->abort(batch2);
ASSERT(batch2->finished());
ASSERT_EQ(batch2->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
_bucketCatalog->finish(batch1, {});
ASSERT(batch1->finished());
ASSERT_OK(batch1->getResult().getStatus());
}
TEST_F(BucketCatalogTest, ClearNamespaceWithConcurrentWrites) {
auto batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch->claimCommitRights());
_bucketCatalog->clear(_ns1);
bool prepared = _bucketCatalog->prepareCommit(batch);
ASSERT(!prepared);
ASSERT(batch->finished());
ASSERT_EQ(batch->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch->claimCommitRights());
_bucketCatalog->prepareCommit(batch);
ASSERT_EQ(batch->measurements().size(), 1);
ASSERT_EQ(batch->numPreviouslyCommittedMeasurements(), 0);
_bucketCatalog->clear(_ns1);
// Even though bucket has been cleared, finish should still report success. Basically, in this
// case we know that the write succeeded, so it must have happened before the namespace drop
// operation got the collection lock. So the write did actually happen, but is has since been
// removed, and that's fine for our purposes. The finish just records the result to the batch
// and updates some statistics.
_bucketCatalog->finish(batch, {});
ASSERT(batch->finished());
ASSERT_OK(batch->getResult().getStatus());
}
TEST_F(BucketCatalogTest, ClearBucketWithPreparedBatchThrowsConflict) {
auto batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch->claimCommitRights());
_bucketCatalog->prepareCommit(batch);
ASSERT_EQ(batch->measurements().size(), 1);
ASSERT_EQ(batch->numPreviouslyCommittedMeasurements(), 0);
ASSERT_THROWS(_bucketCatalog->clear(batch->bucket()->id()), WriteConflictException);
_bucketCatalog->abort(batch);
ASSERT(batch->finished());
ASSERT_EQ(batch->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
}
TEST_F(BucketCatalogTest, PrepareCommitOnClearedBatchWithAlreadyPreparedBatch) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch1->claimCommitRights());
_bucketCatalog->prepareCommit(batch1);
ASSERT_EQ(batch1->measurements().size(), 1);
ASSERT_EQ(batch1->numPreviouslyCommittedMeasurements(), 0);
// Insert before clear so there's a second batch live at the same time.
auto batch2 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_NE(batch1, batch2);
ASSERT_EQ(batch1->bucket(), batch2->bucket());
// Now clear the bucket. Since there's a prepared batch it should conflict.
ASSERT_THROWS(_bucketCatalog->clear(batch1->bucket()->id()), WriteConflictException);
// Now try to prepare the second batch. Ensure it aborts the batch.
ASSERT(batch2->claimCommitRights());
ASSERT(!_bucketCatalog->prepareCommit(batch2));
ASSERT(batch2->finished());
ASSERT_EQ(batch2->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
// Make sure we didn't clear the bucket state when we aborted the second batch.
ASSERT_THROWS(_bucketCatalog->clear(batch1->bucket()->id()), WriteConflictException);
// Make sure a subsequent insert, which opens a new bucket, doesn't corrupt the old bucket
// state and prevent us from finishing the first batch.
auto batch3 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_NE(batch1, batch3);
ASSERT_NE(batch2, batch3);
ASSERT_NE(batch1->bucket(), batch3->bucket());
// Clean up this batch
ASSERT(batch3->claimCommitRights());
_bucketCatalog->abort(batch3);
// Make sure we can finish the cleanly prepared batch.
_bucketCatalog->finish(batch1, {});
ASSERT(batch1->finished());
ASSERT_OK(batch1->getResult().getStatus());
}
TEST_F(BucketCatalogTest, PrepareCommitOnAlreadyAbortedBatch) {
auto batch = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT(batch->claimCommitRights());
_bucketCatalog->abort(batch);
ASSERT(batch->finished());
ASSERT_EQ(batch->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
bool prepared = _bucketCatalog->prepareCommit(batch);
ASSERT(!prepared);
ASSERT(batch->finished());
ASSERT_EQ(batch->getResult().getStatus(), ErrorCodes::TimeseriesBucketCleared);
}
TEST_F(BucketCatalogTest, CombiningWithInsertsFromOtherClients) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
auto batch2 = _bucketCatalog
->insert(_makeOperationContext().second.get(),
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
auto batch3 = _bucketCatalog
->insert(_makeOperationContext().second.get(),
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
auto batch4 = _bucketCatalog
->insert(_makeOperationContext().second.get(),
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kAllow)
.getValue();
ASSERT_NE(batch1, batch2);
ASSERT_NE(batch1, batch3);
ASSERT_NE(batch2, batch3);
ASSERT_EQ(batch3, batch4);
_commit(batch1, 0);
_commit(batch2, 1);
_commit(batch3, 2, 2);
}
TEST_F(BucketCatalogTest, CannotConcurrentlyCommitBatchesForSameBucket) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
auto batch2 = _bucketCatalog
->insert(_makeOperationContext().second.get(),
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
ASSERT(batch1->claimCommitRights());
ASSERT(batch2->claimCommitRights());
// Batch 2 will not be able to commit until batch 1 has finished.
_bucketCatalog->prepareCommit(batch1);
auto task = Task{[&]() { _bucketCatalog->prepareCommit(batch2); }};
// Add a little extra wait to make sure prepareCommit actually gets to the blocking point.
stdx::this_thread::sleep_for(stdx::chrono::milliseconds(10));
ASSERT(task.future().valid());
ASSERT(stdx::future_status::timeout == task.future().wait_for(stdx::chrono::microseconds(1)))
<< "prepareCommit finished before expected";
_bucketCatalog->finish(batch1, {});
task.future().wait();
_bucketCatalog->finish(batch2, {});
}
TEST_F(BucketCatalogTest, DuplicateNewFieldNamesAcrossConcurrentBatches) {
auto batch1 = _bucketCatalog
->insert(_opCtx,
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
auto batch2 = _bucketCatalog
->insert(_makeOperationContext().second.get(),
_ns1,
_getCollator(_ns1),
_getTimeseriesOptions(_ns1),
BSON(_timeField << Date_t::now()),
BucketCatalog::CombineWithInsertsFromOtherClients::kDisallow)
.getValue();
// Batch 2 is the first batch to commit the time field.
ASSERT(batch2->claimCommitRights());
_bucketCatalog->prepareCommit(batch2);
ASSERT_EQ(batch2->newFieldNamesToBeInserted().size(), 1);
ASSERT_EQ(batch2->newFieldNamesToBeInserted().begin()->first, _timeField);
_bucketCatalog->finish(batch2, {});
// Batch 1 was the first batch to insert the time field, but by commit time it was already
// committed by batch 2.
ASSERT(batch1->claimCommitRights());
_bucketCatalog->prepareCommit(batch1);
ASSERT(batch1->newFieldNamesToBeInserted().empty());
_bucketCatalog->finish(batch1, {});
}
} // namespace
} // namespace mongo
|