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
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h"
#include "mongo/base/checked_cast.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/repl/repl_settings.h"
#include "mongo/db/repl/replication_coordinator_mock.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/recovery_unit_test_harness.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_cursor_helpers.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_kv_engine.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_record_store.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_session_cache.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_util.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/temp_dir.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/clock_source_mock.h"
namespace mongo {
namespace {
class WiredTigerRecoveryUnitHarnessHelper final : public RecoveryUnitHarnessHelper {
public:
WiredTigerRecoveryUnitHarnessHelper()
: _dbpath("wt_test"),
_engine(kWiredTigerEngineName, // .canonicalName
_dbpath.path(), // .path
&_cs, // .cs
"", // .extraOpenOptions
1, // .cacheSizeMB
0, // .maxCacheOverflowFileSizeMB
false, // .durable
false, // .ephemeral
false, // .repair
false // .readOnly
) {
repl::ReplicationCoordinator::set(
getGlobalServiceContext(),
std::unique_ptr<repl::ReplicationCoordinator>(new repl::ReplicationCoordinatorMock(
getGlobalServiceContext(), repl::ReplSettings())));
_engine.notifyStartupComplete();
}
~WiredTigerRecoveryUnitHarnessHelper() {}
virtual std::unique_ptr<RecoveryUnit> newRecoveryUnit() final {
return std::unique_ptr<RecoveryUnit>(_engine.newRecoveryUnit());
}
virtual std::unique_ptr<RecordStore> createRecordStore(OperationContext* opCtx,
const std::string& ns) final {
std::string ident = ns;
NamespaceString nss(ns);
std::string uri = WiredTigerKVEngine::kTableUriPrefix + ns;
StatusWith<std::string> result =
WiredTigerRecordStore::generateCreateString(kWiredTigerEngineName,
nss,
ident,
CollectionOptions(),
"",
KeyFormat::Long,
WiredTigerUtil::useTableLogging(nss));
ASSERT_TRUE(result.isOK());
std::string config = result.getValue();
{
WriteUnitOfWork uow(opCtx);
WiredTigerRecoveryUnit* ru =
checked_cast<WiredTigerRecoveryUnit*>(opCtx->recoveryUnit());
WT_SESSION* s = ru->getSession()->getSession();
invariantWTOK(s->create(s, uri.c_str(), config.c_str()), s);
uow.commit();
}
WiredTigerRecordStore::Params params;
params.nss = nss;
params.ident = ident;
params.engineName = kWiredTigerEngineName;
params.isCapped = false;
params.keyFormat = KeyFormat::Long;
params.overwrite = true;
params.isEphemeral = false;
params.isLogged = WiredTigerUtil::useTableLogging(nss);
params.cappedCallback = nullptr;
params.sizeStorer = nullptr;
params.tracksSizeAdjustments = true;
params.isReadOnly = false;
params.forceUpdateWithFullDocument = false;
auto ret = std::make_unique<StandardWiredTigerRecordStore>(&_engine, opCtx, params);
ret->postConstructorInit(opCtx);
return std::move(ret);
}
WiredTigerKVEngine* getEngine() {
return &_engine;
}
private:
unittest::TempDir _dbpath;
ClockSourceMock _cs;
WiredTigerKVEngine _engine;
};
std::unique_ptr<RecoveryUnitHarnessHelper> makeWTRUHarnessHelper() {
return std::make_unique<WiredTigerRecoveryUnitHarnessHelper>();
}
MONGO_INITIALIZER(RegisterHarnessFactory)(InitializerContext* const) {
mongo::registerRecoveryUnitHarnessHelperFactory(makeWTRUHarnessHelper);
}
class WiredTigerRecoveryUnitTestFixture : public unittest::Test {
public:
typedef std::pair<ServiceContext::UniqueClient, ServiceContext::UniqueOperationContext>
ClientAndCtx;
ClientAndCtx makeClientAndOpCtx(RecoveryUnitHarnessHelper* harnessHelper,
const std::string& clientName) {
auto sc = harnessHelper->serviceContext();
auto client = sc->makeClient(clientName);
auto opCtx = client->makeOperationContext();
opCtx->setRecoveryUnit(harnessHelper->newRecoveryUnit(),
WriteUnitOfWork::RecoveryUnitState::kNotInUnitOfWork);
return std::make_pair(std::move(client), std::move(opCtx));
}
void getCursor(WiredTigerRecoveryUnit* ru, WT_CURSOR** cursor) {
WT_SESSION* wt_session = ru->getSession()->getSession();
invariantWTOK(wt_session->create(wt_session, wt_uri, wt_config), wt_session);
invariantWTOK(wt_session->open_cursor(wt_session, wt_uri, nullptr, nullptr, cursor),
wt_session);
}
void setUp() override {
harnessHelper = std::make_unique<WiredTigerRecoveryUnitHarnessHelper>();
clientAndCtx1 = makeClientAndOpCtx(harnessHelper.get(), "writer");
clientAndCtx2 = makeClientAndOpCtx(harnessHelper.get(), "reader");
ru1 = checked_cast<WiredTigerRecoveryUnit*>(clientAndCtx1.second->recoveryUnit());
ru2 = checked_cast<WiredTigerRecoveryUnit*>(clientAndCtx2.second->recoveryUnit());
snapshotManager = dynamic_cast<WiredTigerSnapshotManager*>(
harnessHelper->getEngine()->getSnapshotManager());
}
std::unique_ptr<WiredTigerRecoveryUnitHarnessHelper> harnessHelper;
ClientAndCtx clientAndCtx1, clientAndCtx2;
WiredTigerRecoveryUnit *ru1, *ru2;
WiredTigerSnapshotManager* snapshotManager;
private:
const char* wt_uri = "table:prepare_transaction";
const char* wt_config = "key_format=S,value_format=S";
};
TEST_F(WiredTigerRecoveryUnitTestFixture, SetReadSource) {
// Storage engine operations require at least Global IS.
Lock::GlobalLock lk(clientAndCtx1.second.get(), MODE_IS);
ru1->setTimestampReadSource(RecoveryUnit::ReadSource::kProvided, Timestamp(1, 1));
ASSERT_EQ(RecoveryUnit::ReadSource::kProvided, ru1->getTimestampReadSource());
ASSERT_EQ(Timestamp(1, 1), ru1->getPointInTimeReadTimestamp(clientAndCtx1.second.get()));
}
TEST_F(WiredTigerRecoveryUnitTestFixture, NoOverlapReadSource) {
OperationContext* opCtx1 = clientAndCtx1.second.get();
OperationContext* opCtx2 = clientAndCtx2.second.get();
// Hold the global locks throughout the test to avoid having the global lock destructor
// prematurely abandon snapshots.
Lock::GlobalLock globalLock1(opCtx1, MODE_IX);
Lock::GlobalLock globalLock2(opCtx2, MODE_IX);
std::unique_ptr<RecordStore> rs(harnessHelper->createRecordStore(opCtx1, "a.b"));
const std::string str = str::stream() << "test";
const Timestamp ts1{1, 1};
const Timestamp ts2{1, 2};
const Timestamp ts3{1, 2};
RecordId rid1;
{
WriteUnitOfWork wuow(opCtx1);
StatusWith<RecordId> res = rs->insertRecord(opCtx1, str.c_str(), str.size() + 1, ts1);
ASSERT_OK(res);
wuow.commit();
rid1 = res.getValue();
snapshotManager->setLastApplied(ts1);
}
// Read without a timestamp. The write should be visible.
ASSERT_EQ(opCtx1->recoveryUnit()->getTimestampReadSource(),
RecoveryUnit::ReadSource::kNoTimestamp);
RecordData unused;
ASSERT_TRUE(rs->findRecord(opCtx1, rid1, &unused));
// Read with kNoOverlap. The write should be visible.
opCtx1->recoveryUnit()->abandonSnapshot();
opCtx1->recoveryUnit()->setTimestampReadSource(RecoveryUnit::ReadSource::kNoOverlap);
ASSERT_TRUE(rs->findRecord(opCtx1, rid1, &unused));
RecordId rid2, rid3;
{
// Start, but do not commit a transaction with opCtx2. This sets a timestamp at ts2, which
// creates a hole. kNoOverlap, which is a function of all_durable, will only be able to read
// at the time immediately before.
WriteUnitOfWork wuow(opCtx2);
StatusWith<RecordId> res =
rs->insertRecord(opCtx2, str.c_str(), str.size() + 1, Timestamp());
ASSERT_OK(opCtx2->recoveryUnit()->setTimestamp(ts2));
ASSERT_OK(res);
rid2 = res.getValue();
// While holding open a transaction with opCtx2, perform an insert at ts3 with opCtx1. This
// creates a "hole".
{
WriteUnitOfWork wuow(opCtx1);
StatusWith<RecordId> res = rs->insertRecord(opCtx1, str.c_str(), str.size() + 1, ts3);
ASSERT_OK(res);
wuow.commit();
rid3 = res.getValue();
snapshotManager->setLastApplied(ts3);
}
// Read without a timestamp, and we should see the first and third records.
opCtx1->recoveryUnit()->abandonSnapshot();
opCtx1->recoveryUnit()->setTimestampReadSource(RecoveryUnit::ReadSource::kNoTimestamp);
ASSERT_TRUE(rs->findRecord(opCtx1, rid1, &unused));
ASSERT_FALSE(rs->findRecord(opCtx1, rid2, &unused));
ASSERT_TRUE(rs->findRecord(opCtx1, rid3, &unused));
// Now read at kNoOverlap. Since the transaction at ts2 has not committed, all_durable is
// held back to ts1. LastApplied has advanced to ts3, but because kNoOverlap is the minimum,
// we should only see one record.
opCtx1->recoveryUnit()->abandonSnapshot();
opCtx1->recoveryUnit()->setTimestampReadSource(RecoveryUnit::ReadSource::kNoOverlap);
ASSERT_TRUE(rs->findRecord(opCtx1, rid1, &unused));
ASSERT_FALSE(rs->findRecord(opCtx1, rid2, &unused));
ASSERT_FALSE(rs->findRecord(opCtx1, rid3, &unused));
wuow.commit();
}
// Now that the hole has been closed, kNoOverlap should see all 3 records.
opCtx1->recoveryUnit()->abandonSnapshot();
opCtx1->recoveryUnit()->setTimestampReadSource(RecoveryUnit::ReadSource::kNoOverlap);
ASSERT_TRUE(rs->findRecord(opCtx1, rid1, &unused));
ASSERT_TRUE(rs->findRecord(opCtx1, rid2, &unused));
ASSERT_TRUE(rs->findRecord(opCtx1, rid3, &unused));
}
TEST_F(WiredTigerRecoveryUnitTestFixture,
LocalReadOnADocumentBeingPreparedWithoutIgnoringPreparedTriggersPrepareConflict) {
// Prepare but don't commit a transaction
ru1->beginUnitOfWork(clientAndCtx1.second.get());
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(clientAndCtx1.second.get(), cursor), cursor->session);
ru1->setPrepareTimestamp({1, 1});
ru1->prepareUnitOfWork();
// The transaction read default enforces prepare conflicts and triggers a WT_PREPARE_CONFLICT.
ru2->beginUnitOfWork(clientAndCtx2.second.get());
getCursor(ru2, &cursor);
cursor->set_key(cursor, "key");
int ret = cursor->search(cursor);
ASSERT_EQ(WT_PREPARE_CONFLICT, ret);
ru1->abortUnitOfWork();
ru2->abortUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture,
LocalReadOnADocumentBeingPreparedDoesntTriggerPrepareConflict) {
// Prepare but don't commit a transaction
ru1->beginUnitOfWork(clientAndCtx1.second.get());
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(clientAndCtx1.second.get(), cursor), cursor->session);
ru1->setPrepareTimestamp({1, 1});
ru1->prepareUnitOfWork();
// A transaction that chooses to ignore prepare conflicts does not see the record instead of
// returning a prepare conflict.
ru2->beginUnitOfWork(clientAndCtx2.second.get());
ru2->setPrepareConflictBehavior(PrepareConflictBehavior::kIgnoreConflicts);
getCursor(ru2, &cursor);
cursor->set_key(cursor, "key");
int ret = cursor->search(cursor);
ASSERT_EQ(WT_NOTFOUND, ret);
ru1->abortUnitOfWork();
ru2->abortUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture, WriteAllowedWhileIgnorePrepareFalse) {
// Prepare but don't commit a transaction
ru1->beginUnitOfWork(clientAndCtx1.second.get());
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key1");
cursor->set_value(cursor, "value1");
invariantWTOK(wiredTigerCursorInsert(clientAndCtx1.second.get(), cursor), cursor->session);
ru1->setPrepareTimestamp({1, 1});
ru1->prepareUnitOfWork();
// A transaction that chooses to ignore prepare conflicts with kIgnoreConflictsAllowWrites does
// not see the record
ru2->beginUnitOfWork(clientAndCtx2.second.get());
ru2->setPrepareConflictBehavior(PrepareConflictBehavior::kIgnoreConflictsAllowWrites);
// The prepared write is not visible.
getCursor(ru2, &cursor);
cursor->set_key(cursor, "key1");
ASSERT_EQ(WT_NOTFOUND, cursor->search(cursor));
getCursor(ru2, &cursor);
cursor->set_key(cursor, "key2");
cursor->set_value(cursor, "value2");
// The write is allowed.
invariantWTOK(wiredTigerCursorInsert(clientAndCtx2.second.get(), cursor), cursor->session);
ru1->abortUnitOfWork();
ru2->abortUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture, WriteOnADocumentBeingPreparedTriggersWTRollback) {
// Prepare but don't commit a transaction
ru1->beginUnitOfWork(clientAndCtx1.second.get());
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(clientAndCtx1.second.get(), cursor), cursor->session);
ru1->setPrepareTimestamp({1, 1});
ru1->prepareUnitOfWork();
// Another transaction with write triggers WT_ROLLBACK
ru2->beginUnitOfWork(clientAndCtx2.second.get());
getCursor(ru2, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value2");
int ret = wiredTigerCursorInsert(clientAndCtx2.second.get(), cursor);
ASSERT_EQ(WT_ROLLBACK, ret);
ru1->abortUnitOfWork();
ru2->abortUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture,
ChangeIsPassedEmptyLastTimestampSetOnCommitWithNoTimestamp) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
wuow.commit();
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsPassedLastTimestampSetOnCommit) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts1));
ASSERT(!commitTs);
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts2));
ASSERT(!commitTs);
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts1));
ASSERT(!commitTs);
wuow.commit();
ASSERT_EQ(*commitTs, ts1);
}
ASSERT_EQ(*commitTs, ts1);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsNotPassedLastTimestampSetOnAbort) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts1));
ASSERT(!commitTs);
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsPassedCommitTimestamp) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
wuow.commit();
ASSERT_EQ(*commitTs, ts1);
}
ASSERT_EQ(*commitTs, ts1);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsNotPassedCommitTimestampIfCleared) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT(!commitTs);
opCtx->recoveryUnit()->clearCommitTimestamp();
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
wuow.commit();
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsPassedNewestCommitTimestamp) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
opCtx->recoveryUnit()->setCommitTimestamp(ts2);
ASSERT(!commitTs);
opCtx->recoveryUnit()->clearCommitTimestamp();
ASSERT(!commitTs);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
wuow.commit();
ASSERT_EQ(*commitTs, ts1);
}
ASSERT_EQ(*commitTs, ts1);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ChangeIsNotPassedCommitTimestampOnAbort) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitTimestampBeforeSetTimestampOnCommit) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
opCtx->recoveryUnit()->setCommitTimestamp(ts2);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
wuow.commit();
ASSERT_EQ(*commitTs, ts2);
}
ASSERT_EQ(*commitTs, ts2);
opCtx->recoveryUnit()->clearCommitTimestamp();
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts1));
ASSERT_EQ(*commitTs, ts2);
wuow.commit();
ASSERT_EQ(*commitTs, ts1);
}
ASSERT_EQ(*commitTs, ts1);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitTimestampAfterSetTimestampOnCommit) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts2));
ASSERT(!commitTs);
wuow.commit();
ASSERT_EQ(*commitTs, ts2);
}
ASSERT_EQ(*commitTs, ts2);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT_EQ(*commitTs, ts2);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT_EQ(*commitTs, ts2);
wuow.commit();
ASSERT_EQ(*commitTs, ts1);
}
ASSERT_EQ(*commitTs, ts1);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitTimestampBeforeSetTimestampOnAbort) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
opCtx->recoveryUnit()->setCommitTimestamp(ts2);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
}
ASSERT(!commitTs);
opCtx->recoveryUnit()->clearCommitTimestamp();
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts1));
ASSERT(!commitTs);
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitTimestampAfterSetTimestampOnAbort) {
boost::optional<Timestamp> commitTs = boost::none;
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
Timestamp ts2(6, 6);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
ASSERT_OK(opCtx->recoveryUnit()->setTimestamp(ts2));
ASSERT(!commitTs);
}
ASSERT(!commitTs);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
ASSERT(!commitTs);
{
WriteUnitOfWork wuow(opCtx);
opCtx->recoveryUnit()->onCommit(
[&](boost::optional<Timestamp> commitTime) { commitTs = commitTime; });
ASSERT(!commitTs);
}
ASSERT(!commitTs);
}
TEST_F(WiredTigerRecoveryUnitTestFixture, ReadOnceCursorsCached) {
auto opCtx = clientAndCtx1.second.get();
// Hold the global lock throughout the test to avoid having the global lock destructor
// prematurely abandon snapshots.
Lock::GlobalLock globalLock(opCtx, MODE_IX);
auto ru = WiredTigerRecoveryUnit::get(opCtx);
std::unique_ptr<RecordStore> rs(harnessHelper->createRecordStore(opCtx, "test.read_once"));
auto uri = dynamic_cast<WiredTigerRecordStore*>(rs.get())->getURI();
// Insert a record.
WriteUnitOfWork wuow(opCtx);
StatusWith<RecordId> s = rs->insertRecord(opCtx, "data", 4, Timestamp());
ASSERT_TRUE(s.isOK());
ASSERT_EQUALS(1, rs->numRecords(opCtx));
wuow.commit();
// Test 1: A normal read should create a new cursor and release it into the session cache.
// Close all cached cursors to establish a 'before' state.
ru->getSession()->closeAllCursors(uri);
int cachedCursorsBefore = ru->getSession()->cachedCursors();
RecordData rd;
ASSERT_TRUE(rs->findRecord(opCtx, s.getValue(), &rd));
// A cursor should have been checked out and released into the cache.
ASSERT_GT(ru->getSession()->cachedCursors(), cachedCursorsBefore);
// All opened cursors are returned.
ASSERT_EQ(0, ru->getSession()->cursorsOut());
ru->abandonSnapshot();
// Test 2: A read-once operation should create a new cursor because it has a different
// configuration. This will be released into the cache.
ru->setReadOnce(true);
// Close any cached cursors to establish a new 'before' state.
ru->getSession()->closeAllCursors(uri);
cachedCursorsBefore = ru->getSession()->cachedCursors();
// The subsequent read operation will create a new read_once cursor and release into the cache.
ASSERT_TRUE(rs->findRecord(opCtx, s.getValue(), &rd));
// A new cursor should have been released into the cache.
ASSERT_GT(ru->getSession()->cachedCursors(), cachedCursorsBefore);
// All opened cursors are closed.
ASSERT_EQ(0, ru->getSession()->cursorsOut());
ASSERT(ru->getReadOnce());
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CacheMixedOverwrite) {
auto opCtx = clientAndCtx1.second.get();
std::unique_ptr<RecordStore> rs(harnessHelper->createRecordStore(opCtx, "test.A"));
auto uri = dynamic_cast<WiredTigerRecordStore*>(rs.get())->getURI();
// Hold the global lock throughout the test to avoid having the global lock destructor
// prematurely abandon snapshots.
Lock::GlobalLock globalLock(opCtx, MODE_IX);
auto ru = WiredTigerRecoveryUnit::get(opCtx);
// Close all cached cursors to establish a 'before' state.
auto session = ru->getSession();
ru->getSession()->closeAllCursors(uri);
int cachedCursorsBefore = ru->getSession()->cachedCursors();
// Use a large, unused table ID for this test to ensure we don't collide with any other table
// ids.
int tableId = 999999999;
WT_CURSOR* cursor;
// Expect no cached cursors.
{
auto config = "";
cursor = session->getCachedCursor(tableId, config);
ASSERT_FALSE(cursor);
cursor = session->getNewCursor(uri, config);
ASSERT(cursor);
session->releaseCursor(tableId, cursor, config);
ASSERT_GT(session->cachedCursors(), cachedCursorsBefore);
}
cachedCursorsBefore = session->cachedCursors();
// Use a different overwrite setting, expect no cached cursors.
{
auto config = "overwrite=false";
cursor = session->getCachedCursor(tableId, config);
ASSERT_FALSE(cursor);
cursor = session->getNewCursor(uri, config);
ASSERT(cursor);
session->releaseCursor(tableId, cursor, config);
ASSERT_GT(session->cachedCursors(), cachedCursorsBefore);
}
cachedCursorsBefore = session->cachedCursors();
// Expect cursors to be cached.
{
auto config = "";
cursor = session->getCachedCursor(tableId, config);
ASSERT(cursor);
session->releaseCursor(tableId, cursor, config);
ASSERT_EQ(session->cachedCursors(), cachedCursorsBefore);
}
// Expect cursors to be cached.
{
auto config = "overwrite=false";
cursor = session->getCachedCursor(tableId, config);
ASSERT(cursor);
session->releaseCursor(tableId, cursor, config);
ASSERT_EQ(session->cachedCursors(), cachedCursorsBefore);
}
// Use yet another cursor config, and expect no cursors to be cached.
{
auto config = "overwrite=true";
cursor = session->getCachedCursor(tableId, config);
ASSERT_FALSE(cursor);
cursor = session->getNewCursor(uri, config);
ASSERT(cursor);
session->releaseCursor(tableId, cursor, config);
ASSERT_GT(session->cachedCursors(), cachedCursorsBefore);
}
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitWithDurableTimestamp) {
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(3, 3);
Timestamp ts2(5, 5);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
opCtx->recoveryUnit()->setDurableTimestamp(ts2);
auto durableTs = opCtx->recoveryUnit()->getDurableTimestamp();
ASSERT_EQ(ts2, durableTs);
{
WriteUnitOfWork wuow(opCtx);
wuow.commit();
}
}
TEST_F(WiredTigerRecoveryUnitTestFixture, CommitWithoutDurableTimestamp) {
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(5, 5);
opCtx->recoveryUnit()->setCommitTimestamp(ts1);
{
WriteUnitOfWork wuow(opCtx);
wuow.commit();
}
}
TEST_F(WiredTigerRecoveryUnitTestFixture, MultiTimestampConstraintsInternalState) {
Timestamp ts1(1, 1);
Timestamp ts2(2, 2);
OperationContext* opCtx = clientAndCtx1.second.get();
ru1->beginUnitOfWork(opCtx);
// Perform an non timestamped write.
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
// Perform a write at ts1.
cursor->set_key(cursor, "key2");
cursor->set_value(cursor, "value");
ASSERT_OK(ru1->setTimestamp(ts1));
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
// Setting the timestamp again to the same value should not fail.
ASSERT_OK(ru1->setTimestamp(ts1));
// Committing the unit of work should reset the internal state for the multi timestamp
// constraint checks.
ru1->commitUnitOfWork();
ru1->beginUnitOfWork(opCtx);
// Perform a write at ts2.
cursor->set_key(cursor, "key3");
cursor->set_value(cursor, "value");
ASSERT_OK(ru1->setTimestamp(ts2));
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
ru1->commitUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture, AbandonSnapshotAbortMode) {
ru1->setAbandonSnapshotMode(RecoveryUnit::AbandonSnapshotMode::kAbort);
OperationContext* opCtx = clientAndCtx1.second.get();
const char* const key = "key";
{
ru1->beginUnitOfWork(opCtx);
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, key);
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
ru1->commitUnitOfWork();
}
// Create a cursor. We will check that once positioned, the cursor is reset by a call to
// abandonSnapshot() on the associated RecoveryUnit.
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, key);
ASSERT_EQ(0, cursor->search(cursor));
ru1->abandonSnapshot();
// The WT transaction should have been aborted and the cursor reset.
// Advancing to the "next" record now that the cursor has been reset should give us the first
// record again.
ASSERT_EQ(0, cursor->next(cursor));
const char* returnedKey = nullptr;
ASSERT_EQ(0, cursor->get_key(cursor, &returnedKey));
ASSERT_EQ(0, strncmp(key, returnedKey, strlen(key)));
}
DEATH_TEST_REGEX_F(WiredTigerRecoveryUnitTestFixture,
MultiTimestampConstraints,
"Fatal assertion.*4877100") {
Timestamp ts1(1, 1);
Timestamp ts2(2, 2);
OperationContext* opCtx = clientAndCtx1.second.get();
ru1->beginUnitOfWork(opCtx);
auto writeTest = [&]() {
// Perform an non timestamped write.
WT_CURSOR* cursor;
getCursor(ru1, &cursor);
cursor->set_key(cursor, "key");
cursor->set_value(cursor, "value");
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
// Perform a write at ts1.
cursor->set_key(cursor, "key2");
cursor->set_value(cursor, "value");
ASSERT_OK(ru1->setTimestamp(ts1));
invariantWTOK(wiredTigerCursorInsert(opCtx, cursor), cursor->session);
// Setting the timestamp again to a different value should detect that we're trying to set
// multiple timestamps with the first write being non timestamped.
ASSERT_OK(ru1->setTimestamp(ts2));
ru1->commitUnitOfWork();
};
try {
writeTest();
} catch (WriteConflictException const&) {
// It's expected to get a WCE the first time we try this, due to the multi-timestamp
// constraint. We'll try again and it will fassert and print out extra debug info.
}
writeTest();
}
DEATH_TEST_F(WiredTigerRecoveryUnitTestFixture,
SetDurableTimestampTwice,
"Trying to reset durable timestamp when it was already set.") {
auto opCtx = clientAndCtx1.second.get();
Timestamp ts1(3, 3);
Timestamp ts2(5, 5);
opCtx->recoveryUnit()->setDurableTimestamp(ts1);
opCtx->recoveryUnit()->setDurableTimestamp(ts2);
}
DEATH_TEST_F(WiredTigerRecoveryUnitTestFixture,
RollbackHandlerAbortsOnTxnOpen,
"rollback handler reopened transaction") {
auto opCtx = clientAndCtx1.second.get();
auto ru = WiredTigerRecoveryUnit::get(opCtx);
ASSERT(ru->getSession());
{
WriteUnitOfWork wuow(opCtx);
ru->assertInActiveTxn();
ru->onRollback([ru] { ru->getSession(); });
}
}
DEATH_TEST_F(WiredTigerRecoveryUnitTestFixture,
MayNotChangeReadSourceWhilePinned,
"Cannot change ReadSource as it is pinned.") {
// Storage engine operations require at least Global IS.
Lock::GlobalLock lk(clientAndCtx1.second.get(), MODE_IS);
ru1->pinReadSource();
ru1->setTimestampReadSource(RecoveryUnit::ReadSource::kNoOverlap);
}
} // namespace
} // namespace mongo
|