-
Notifications
You must be signed in to change notification settings - Fork 382
/
txn.c
2756 lines (2422 loc) · 101 KB
/
txn.c
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) 2014-present MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __snapsort_partition --
* Custom quick sort partitioning for snapshots.
*/
static uint32_t
__snapsort_partition(uint64_t *array, uint32_t f, uint32_t l, uint64_t pivot)
{
uint32_t i, j;
i = f - 1;
j = l + 1;
for (;;) {
while (pivot < array[--j])
;
while (array[++i] < pivot)
;
if (i < j) {
uint64_t tmp = array[i];
array[i] = array[j];
array[j] = tmp;
} else
return (j);
}
}
/*
* __snapsort_impl --
* Custom quick sort implementation for snapshots.
*/
static void
__snapsort_impl(uint64_t *array, uint32_t f, uint32_t l)
{
while (f + 16 < l) {
uint64_t v1 = array[f], v2 = array[l], v3 = array[(f + l) / 2];
uint64_t median =
v1 < v2 ? (v3 < v1 ? v1 : WT_MIN(v2, v3)) : (v3 < v2 ? v2 : WT_MIN(v1, v3));
uint32_t m = __snapsort_partition(array, f, l, median);
__snapsort_impl(array, f, m);
f = m + 1;
}
}
/*
* __snapsort --
* Sort an array of transaction IDs.
*/
static void
__snapsort(uint64_t *array, uint32_t size)
{
__snapsort_impl(array, 0, size - 1);
WT_INSERTION_SORT(array, size, uint64_t, WT_TXNID_LT);
}
/*
* __txn_remove_from_global_table --
* Remove the transaction id from the global transaction table.
*/
static inline void
__txn_remove_from_global_table(WT_SESSION_IMPL *session)
{
#ifdef HAVE_DIAGNOSTIC
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
WT_TXN_SHARED *txn_shared;
txn = session->txn;
txn_global = &S2C(session)->txn_global;
txn_shared = WT_SESSION_TXN_SHARED(session);
WT_ASSERT(session, !WT_TXNID_LT(txn->id, txn_global->last_running));
WT_ASSERT(session, txn->id != WT_TXN_NONE && txn_shared->id != WT_TXN_NONE);
#else
WT_TXN_SHARED *txn_shared;
txn_shared = WT_SESSION_TXN_SHARED(session);
#endif
WT_PUBLISH(txn_shared->id, WT_TXN_NONE);
}
/*
* __txn_sort_snapshot --
* Sort a snapshot for faster searching and set the min/max bounds.
*/
static void
__txn_sort_snapshot(WT_SESSION_IMPL *session, uint32_t n, uint64_t snap_max)
{
WT_TXN *txn;
txn = session->txn;
if (n > 1)
__snapsort(txn->snapshot, n);
txn->snapshot_count = n;
txn->snap_max = snap_max;
txn->snap_min =
(n > 0 && WT_TXNID_LE(txn->snapshot[0], snap_max)) ? txn->snapshot[0] : snap_max;
F_SET(txn, WT_TXN_HAS_SNAPSHOT);
WT_ASSERT(session, n == 0 || txn->snap_min != WT_TXN_NONE);
}
/*
* __wt_txn_release_snapshot --
* Release the snapshot in the current transaction.
*/
void
__wt_txn_release_snapshot(WT_SESSION_IMPL *session)
{
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
WT_TXN_SHARED *txn_shared;
txn = session->txn;
txn_global = &S2C(session)->txn_global;
txn_shared = WT_SESSION_TXN_SHARED(session);
WT_ASSERT_OPTIONAL(session, WT_DIAGNOSTIC_TXN_VISIBILITY,
txn_shared->pinned_id == WT_TXN_NONE || session->txn->isolation == WT_ISO_READ_UNCOMMITTED ||
!__wt_txn_visible_all(session, txn_shared->pinned_id, WT_TS_NONE),
"A transactions pinned id cannot become globally visible before its snapshot is released");
txn_shared->metadata_pinned = txn_shared->pinned_id = WT_TXN_NONE;
F_CLR(txn, WT_TXN_HAS_SNAPSHOT);
/* Clear a checkpoint's pinned ID and timestamp. */
if (WT_SESSION_IS_CHECKPOINT(session)) {
txn_global->checkpoint_txn_shared.pinned_id = WT_TXN_NONE;
txn_global->checkpoint_timestamp = WT_TS_NONE;
}
}
/*
* __wt_txn_active --
* Check if a transaction is still active. If not, it is either committed, prepared, or rolled
* back. It is possible that we race with commit, prepare or rollback and a transaction is still
* active before the start of the call is eventually reported as resolved.
*/
bool
__wt_txn_active(WT_SESSION_IMPL *session, uint64_t txnid)
{
WT_CONNECTION_IMPL *conn;
WT_TXN_GLOBAL *txn_global;
WT_TXN_SHARED *s;
uint64_t oldest_id;
uint32_t i, session_cnt;
bool active;
conn = S2C(session);
txn_global = &conn->txn_global;
active = true;
/* We're going to scan the table: wait for the lock. */
__wt_readlock(session, &txn_global->rwlock);
oldest_id = txn_global->oldest_id;
if (WT_TXNID_LT(txnid, oldest_id)) {
active = false;
goto done;
}
/* Walk the array of concurrent transactions. */
WT_ORDERED_READ(session_cnt, conn->session_array.cnt);
WT_STAT_CONN_INCR(session, txn_walk_sessions);
for (i = 0, s = txn_global->txn_shared_list; i < session_cnt; i++, s++) {
WT_STAT_CONN_INCR(session, txn_sessions_walked);
/* If the transaction is in the list, it is uncommitted. */
if (s->id == txnid)
goto done;
}
active = false;
done:
__wt_readunlock(session, &txn_global->rwlock);
return (active);
}
/*
* __txn_get_snapshot_int --
* Allocate a snapshot, optionally update our shared txn ids.
*/
static void
__txn_get_snapshot_int(WT_SESSION_IMPL *session, bool publish)
{
WT_CONNECTION_IMPL *conn;
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
WT_TXN_SHARED *s, *txn_shared;
uint64_t commit_gen, current_id, id, prev_oldest_id, pinned_id;
uint32_t i, n, session_cnt;
conn = S2C(session);
txn = session->txn;
txn_global = &conn->txn_global;
txn_shared = WT_SESSION_TXN_SHARED(session);
n = 0;
/* Fast path if we already have the current snapshot. */
if ((commit_gen = __wt_session_gen(session, WT_GEN_COMMIT)) != 0) {
if (F_ISSET(txn, WT_TXN_HAS_SNAPSHOT) && commit_gen == __wt_gen(session, WT_GEN_COMMIT))
return;
__wt_session_gen_leave(session, WT_GEN_COMMIT);
}
__wt_session_gen_enter(session, WT_GEN_COMMIT);
/* We're going to scan the table: wait for the lock. */
__wt_readlock(session, &txn_global->rwlock);
current_id = pinned_id = txn_global->current;
prev_oldest_id = txn_global->oldest_id;
/*
* Include the checkpoint transaction, if one is running: we should ignore any uncommitted
* changes the checkpoint has written to the metadata. We don't have to keep the checkpoint's
* changes pinned so don't go including it in the published pinned ID.
*
* We can assume that if a function calls without intention to publish then it is the special
* case of checkpoint calling it twice. In which case do not include the checkpoint id.
*/
if ((id = txn_global->checkpoint_txn_shared.id) != WT_TXN_NONE) {
if (txn->id != id)
txn->snapshot[n++] = id;
if (publish)
txn_shared->metadata_pinned = id;
}
/* For pure read-only workloads, avoid scanning. */
if (prev_oldest_id == current_id) {
pinned_id = current_id;
/* Check that the oldest ID has not moved in the meantime. */
WT_ASSERT(session, prev_oldest_id == txn_global->oldest_id);
goto done;
}
/* Walk the array of concurrent transactions. */
WT_ORDERED_READ(session_cnt, conn->session_array.cnt);
WT_STAT_CONN_INCR(session, txn_walk_sessions);
for (i = 0, s = txn_global->txn_shared_list; i < session_cnt; i++, s++) {
WT_STAT_CONN_INCR(session, txn_sessions_walked);
/*
* Build our snapshot of any concurrent transaction IDs.
*
* Ignore:
* - Our own ID: we always read our own updates.
* - The ID if it is older than the oldest ID we saw. This
* can happen if we race with a thread that is allocating
* an ID -- the ID will not be used because the thread will
* keep spinning until it gets a valid one.
* - The ID if it is higher than the current ID we saw. This
* can happen if the transaction is already finished. In
* this case, we ignore this transaction because it would
* not be visible to the current snapshot.
*/
while (s != txn_shared && (id = s->id) != WT_TXN_NONE && WT_TXNID_LE(prev_oldest_id, id) &&
WT_TXNID_LT(id, current_id)) {
/*
* If the transaction is still allocating its ID, then we spin here until it gets its
* valid ID.
*/
WT_READ_BARRIER();
if (!s->is_allocating) {
/*
* There is still a chance that fetched ID is not valid after ID allocation, so we
* check again here. The read of transaction ID should be carefully ordered: we want
* to re-read ID from transaction state after this transaction completes ID
* allocation.
*/
WT_READ_BARRIER();
if (id == s->id) {
txn->snapshot[n++] = id;
if (WT_TXNID_LT(id, pinned_id))
pinned_id = id;
break;
}
}
WT_PAUSE();
}
}
/*
* If we got a new snapshot, update the published pinned ID for this session.
*/
WT_ASSERT(session, WT_TXNID_LE(prev_oldest_id, pinned_id));
WT_ASSERT(session, prev_oldest_id == txn_global->oldest_id);
done:
if (publish)
txn_shared->pinned_id = pinned_id;
__wt_readunlock(session, &txn_global->rwlock);
__txn_sort_snapshot(session, n, current_id);
}
/*
* __wt_txn_get_snapshot --
* Common case, allocate a snapshot and update our shared ids.
*/
void
__wt_txn_get_snapshot(WT_SESSION_IMPL *session)
{
__txn_get_snapshot_int(session, true);
}
/*
* __wt_txn_bump_snapshot --
* Uncommon case, allocate a snapshot but skip updating our shared ids.
*/
void
__wt_txn_bump_snapshot(WT_SESSION_IMPL *session)
{
__txn_get_snapshot_int(session, false);
}
/*
* __txn_oldest_scan --
* Sweep the running transactions to calculate the oldest ID required.
*/
static void
__txn_oldest_scan(WT_SESSION_IMPL *session, uint64_t *oldest_idp, uint64_t *last_runningp,
uint64_t *metadata_pinnedp, WT_SESSION_IMPL **oldest_sessionp)
{
WT_CONNECTION_IMPL *conn;
WT_SESSION_IMPL *oldest_session;
WT_TXN_GLOBAL *txn_global;
WT_TXN_SHARED *s;
uint64_t id, last_running, metadata_pinned, oldest_id, prev_oldest_id;
uint32_t i, session_cnt;
conn = S2C(session);
txn_global = &conn->txn_global;
oldest_session = NULL;
/* The oldest ID cannot change while we are holding the scan lock. */
prev_oldest_id = txn_global->oldest_id;
last_running = oldest_id = txn_global->current;
if ((metadata_pinned = txn_global->checkpoint_txn_shared.id) == WT_TXN_NONE)
metadata_pinned = oldest_id;
/* Walk the array of concurrent transactions. */
WT_ORDERED_READ(session_cnt, conn->session_array.cnt);
WT_STAT_CONN_INCR(session, txn_walk_sessions);
for (i = 0, s = txn_global->txn_shared_list; i < session_cnt; i++, s++) {
WT_STAT_CONN_INCR(session, txn_sessions_walked);
/* Update the last running transaction ID. */
while ((id = s->id) != WT_TXN_NONE && WT_TXNID_LE(prev_oldest_id, id) &&
WT_TXNID_LT(id, last_running)) {
/*
* If the transaction is still allocating its ID, then we spin here until it gets its
* valid ID.
*/
WT_READ_BARRIER();
if (!s->is_allocating) {
/*
* There is still a chance that fetched ID is not valid after ID allocation, so we
* check again here. The read of transaction ID should be carefully ordered: we want
* to re-read ID from transaction state after this transaction completes ID
* allocation.
*/
WT_READ_BARRIER();
if (id == s->id) {
last_running = id;
break;
}
}
WT_PAUSE();
}
/* Update the metadata pinned ID. */
if ((id = s->metadata_pinned) != WT_TXN_NONE && WT_TXNID_LT(id, metadata_pinned))
metadata_pinned = id;
/*
* !!!
* Note: Don't ignore pinned ID values older than the previous
* oldest ID. Read-uncommitted operations publish pinned ID
* values without acquiring the scan lock to protect the global
* table. See the comment in __wt_txn_cursor_op for more
* details.
*/
if ((id = s->pinned_id) != WT_TXN_NONE && WT_TXNID_LT(id, oldest_id)) {
oldest_id = id;
oldest_session = &WT_CONN_SESSIONS_GET(conn)[i];
}
}
if (WT_TXNID_LT(last_running, oldest_id))
oldest_id = last_running;
/* The metadata pinned ID can't move past the oldest ID. */
if (WT_TXNID_LT(oldest_id, metadata_pinned))
metadata_pinned = oldest_id;
*last_runningp = last_running;
*metadata_pinnedp = metadata_pinned;
*oldest_idp = oldest_id;
*oldest_sessionp = oldest_session;
}
/*
* __wt_txn_update_oldest --
* Sweep the running transactions to update the oldest ID required.
*/
int
__wt_txn_update_oldest(WT_SESSION_IMPL *session, uint32_t flags)
{
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
WT_SESSION_IMPL *oldest_session;
WT_TXN_GLOBAL *txn_global;
uint64_t current_id, last_running, metadata_pinned, oldest_id;
uint64_t prev_last_running, prev_metadata_pinned, prev_oldest_id;
bool strict, wait;
conn = S2C(session);
txn_global = &conn->txn_global;
strict = LF_ISSET(WT_TXN_OLDEST_STRICT);
wait = LF_ISSET(WT_TXN_OLDEST_WAIT);
current_id = last_running = metadata_pinned = txn_global->current;
prev_last_running = txn_global->last_running;
prev_metadata_pinned = txn_global->metadata_pinned;
prev_oldest_id = txn_global->oldest_id;
/* Try to move the pinned timestamp forward. */
if (strict)
__wt_txn_update_pinned_timestamp(session, false);
/*
* For pure read-only workloads, or if the update isn't forced and the oldest ID isn't too far
* behind, avoid scanning.
*/
if ((prev_oldest_id == current_id && prev_metadata_pinned == current_id) ||
(!strict && WT_TXNID_LT(current_id, prev_oldest_id + 100)))
return (0);
/* First do a read-only scan. */
if (wait)
__wt_readlock(session, &txn_global->rwlock);
else if ((ret = __wt_try_readlock(session, &txn_global->rwlock)) != 0)
return (ret == EBUSY ? 0 : ret);
__txn_oldest_scan(session, &oldest_id, &last_running, &metadata_pinned, &oldest_session);
__wt_readunlock(session, &txn_global->rwlock);
/*
* If the state hasn't changed (or hasn't moved far enough for non-forced updates), give up.
*/
if ((oldest_id == prev_oldest_id ||
(!strict && WT_TXNID_LT(oldest_id, prev_oldest_id + 100))) &&
((last_running == prev_last_running) ||
(!strict && WT_TXNID_LT(last_running, prev_last_running + 100))) &&
metadata_pinned == prev_metadata_pinned)
return (0);
/* It looks like an update is necessary, wait for exclusive access. */
if (wait)
__wt_writelock(session, &txn_global->rwlock);
else if ((ret = __wt_try_writelock(session, &txn_global->rwlock)) != 0)
return (ret == EBUSY ? 0 : ret);
/*
* If the oldest ID has been updated while we waited, don't bother scanning.
*/
if (WT_TXNID_LE(oldest_id, txn_global->oldest_id) &&
WT_TXNID_LE(last_running, txn_global->last_running) &&
WT_TXNID_LE(metadata_pinned, txn_global->metadata_pinned))
goto done;
/*
* Re-scan now that we have exclusive access. This is necessary because threads get transaction
* snapshots with read locks, and we have to be sure that there isn't a thread that has got a
* snapshot locally but not yet published its snap_min.
*/
__txn_oldest_scan(session, &oldest_id, &last_running, &metadata_pinned, &oldest_session);
/* Update the public IDs. */
if (WT_TXNID_LT(txn_global->metadata_pinned, metadata_pinned))
txn_global->metadata_pinned = metadata_pinned;
if (WT_TXNID_LT(txn_global->oldest_id, oldest_id))
txn_global->oldest_id = oldest_id;
if (WT_TXNID_LT(txn_global->last_running, last_running)) {
txn_global->last_running = last_running;
/* Output a verbose message about long-running transactions,
* but only when some progress is being made. */
if (WT_VERBOSE_ISSET(session, WT_VERB_TRANSACTION) &&
current_id - oldest_id > (10 * WT_THOUSAND) && oldest_session != NULL) {
__wt_verbose(session, WT_VERB_TRANSACTION,
"old snapshot %" PRIu64 " pinned in session %" PRIu32 " [%s] with snap_min %" PRIu64,
oldest_id, oldest_session->id, oldest_session->lastop, oldest_session->txn->snap_min);
}
}
done:
__wt_writeunlock(session, &txn_global->rwlock);
return (ret);
}
/*
* __txn_config_operation_timeout --
* Configure a transactions operation timeout duration.
*/
static int
__txn_config_operation_timeout(WT_SESSION_IMPL *session, const char *cfg[], bool start_timer)
{
WT_CONFIG_ITEM cval;
WT_TXN *txn;
txn = session->txn;
if (cfg == NULL)
return (0);
/* Retrieve the maximum operation time. */
WT_RET(__wt_config_gets_def(session, cfg, "operation_timeout_ms", 0, &cval));
/*
* The default configuration value is 0, we can't tell if they're setting it back to 0 or, if
* the default was automatically passed in.
*/
if (cval.val != 0) {
txn->operation_timeout_us = (uint64_t)(cval.val * WT_THOUSAND);
/*
* The op timer will generally be started on entry to the API call however when we configure
* it internally we need to start it separately.
*/
if (start_timer)
__wt_op_timer_start(session);
}
return (0);
}
/*
* __wt_txn_config --
* Configure a transaction.
*/
int
__wt_txn_config(WT_SESSION_IMPL *session, const char *cfg[])
{
WT_CONFIG_ITEM cval;
WT_DECL_RET;
WT_TXN *txn;
wt_timestamp_t read_ts;
txn = session->txn;
if (cfg == NULL)
return (0);
WT_ERR(__wt_config_gets_def(session, cfg, "isolation", 0, &cval));
if (cval.len != 0)
txn->isolation = WT_STRING_MATCH("snapshot", cval.str, cval.len) ? WT_ISO_SNAPSHOT :
WT_STRING_MATCH("read-committed", cval.str, cval.len) ? WT_ISO_READ_COMMITTED :
WT_ISO_READ_UNCOMMITTED;
WT_ERR(__txn_config_operation_timeout(session, cfg, false));
/*
* The default sync setting is inherited from the connection, but can be overridden by an
* explicit "sync" setting for this transaction.
*
* We want to distinguish between inheriting implicitly and explicitly.
*/
F_CLR(txn, WT_TXN_SYNC_SET);
WT_ERR(__wt_config_gets_def(session, cfg, "sync", (int)UINT_MAX, &cval));
if (cval.val == 0 || cval.val == 1)
/*
* This is an explicit setting of sync. Set the flag so that we know not to overwrite it in
* commit_transaction.
*/
F_SET(txn, WT_TXN_SYNC_SET);
/*
* If sync is turned off explicitly, clear the transaction's sync field.
*/
if (cval.val == 0)
txn->txn_logsync = 0;
/* Check if prepared updates should be ignored during reads. */
WT_ERR(__wt_config_gets_def(session, cfg, "ignore_prepare", 0, &cval));
if (cval.len > 0 && WT_STRING_MATCH("force", cval.str, cval.len))
F_SET(txn, WT_TXN_IGNORE_PREPARE);
else if (cval.val)
F_SET(txn, WT_TXN_IGNORE_PREPARE | WT_TXN_READONLY);
/* Check if commits without a timestamp are allowed. */
WT_ERR(__wt_config_gets_def(session, cfg, "no_timestamp", 0, &cval));
if (cval.val)
F_SET(txn, WT_TXN_TS_NOT_SET);
/*
* Check if the prepare timestamp and the commit timestamp of a prepared transaction need to be
* rounded up.
*/
WT_ERR(__wt_config_gets_def(session, cfg, "roundup_timestamps.prepared", 0, &cval));
if (cval.val)
F_SET(txn, WT_TXN_TS_ROUND_PREPARED);
/* Check if read timestamp needs to be rounded up. */
WT_ERR(__wt_config_gets_def(session, cfg, "roundup_timestamps.read", 0, &cval));
if (cval.val)
F_SET(txn, WT_TXN_TS_ROUND_READ);
WT_ERR(__wt_config_gets_def(session, cfg, "read_timestamp", 0, &cval));
if (cval.len != 0) {
WT_ERR(__wt_txn_parse_timestamp(session, "read", &read_ts, &cval));
WT_ERR(__wt_txn_set_read_timestamp(session, read_ts));
}
err:
if (ret != 0)
/*
* In the event that we error during configuration we should clear the flags on the
* transaction so they are not set in a subsequent call to transaction begin.
*/
txn->flags = 0;
return (ret);
}
/*
* __wt_txn_reconfigure --
* WT_SESSION::reconfigure for transactions.
*/
int
__wt_txn_reconfigure(WT_SESSION_IMPL *session, const char *config)
{
WT_CONFIG_ITEM cval;
WT_DECL_RET;
WT_TXN *txn;
txn = session->txn;
ret = __wt_config_getones(session, config, "isolation", &cval);
if (ret == 0 && cval.len != 0) {
session->isolation = txn->isolation = WT_STRING_MATCH("snapshot", cval.str, cval.len) ?
WT_ISO_SNAPSHOT :
WT_STRING_MATCH("read-uncommitted", cval.str, cval.len) ? WT_ISO_READ_UNCOMMITTED :
WT_ISO_READ_COMMITTED;
}
WT_RET_NOTFOUND_OK(ret);
return (0);
}
/*
* __wt_txn_release --
* Release the resources associated with the current transaction.
*/
void
__wt_txn_release(WT_SESSION_IMPL *session)
{
WT_TXN *txn;
WT_TXN_GLOBAL *txn_global;
txn = session->txn;
txn_global = &S2C(session)->txn_global;
WT_ASSERT(session, txn->mod_count == 0);
/* Clear the transaction's ID from the global table. */
if (WT_SESSION_IS_CHECKPOINT(session)) {
WT_ASSERT(session, WT_SESSION_TXN_SHARED(session)->id == WT_TXN_NONE);
txn->id = txn_global->checkpoint_txn_shared.id = WT_TXN_NONE;
/*
* Be extra careful to cleanup everything for checkpoints: once the global checkpoint ID is
* cleared, we can no longer tell if this session is doing a checkpoint.
*/
txn_global->checkpoint_id = 0;
} else if (F_ISSET(txn, WT_TXN_HAS_ID)) {
/*
* If transaction is prepared, this would have been done in prepare.
*/
if (!F_ISSET(txn, WT_TXN_PREPARE))
__txn_remove_from_global_table(session);
else
WT_ASSERT(session, WT_SESSION_TXN_SHARED(session)->id == WT_TXN_NONE);
txn->id = WT_TXN_NONE;
}
__wt_txn_clear_durable_timestamp(session);
/* Free the scratch buffer allocated for logging. */
__wt_logrec_free(session, &txn->logrec);
/* Discard any memory from the session's stash that we can. */
WT_ASSERT(session, __wt_session_gen(session, WT_GEN_SPLIT) == 0);
__wt_stash_discard(session);
/*
* Reset the transaction state to not running and release the snapshot.
*/
__wt_txn_release_snapshot(session);
/* Clear the read timestamp. */
__wt_txn_clear_read_timestamp(session);
txn->isolation = session->isolation;
txn->rollback_reason = NULL;
/*
* Ensure the transaction flags are cleared on exit
*
* Purposely do NOT clear the commit and durable timestamps on release. Other readers may still
* find these transactions in the durable queue and will need to see those timestamps.
*/
txn->flags = 0;
txn->prepare_timestamp = WT_TS_NONE;
/* Clear operation timer. */
txn->operation_timeout_us = 0;
}
/*
* __txn_prepare_rollback_restore_hs_update --
* Restore the history store update to the update chain before roll back prepared update evicted
* to disk
*/
static int
__txn_prepare_rollback_restore_hs_update(
WT_SESSION_IMPL *session, WT_CURSOR *hs_cursor, WT_PAGE *page, WT_UPDATE *upd_chain)
{
WT_DECL_ITEM(hs_value);
WT_DECL_RET;
WT_TIME_WINDOW *hs_tw;
WT_UPDATE *tombstone, *upd;
wt_timestamp_t durable_ts, hs_stop_durable_ts;
size_t size, total_size;
uint64_t type_full;
char ts_string[2][WT_TS_INT_STRING_SIZE];
WT_ASSERT(session, upd_chain != NULL);
hs_tw = NULL;
size = total_size = 0;
tombstone = upd = NULL;
WT_ERR(__wt_scr_alloc(session, 0, &hs_value));
/* Get current value. */
WT_ERR(hs_cursor->get_value(hs_cursor, &hs_stop_durable_ts, &durable_ts, &type_full, hs_value));
/* The value older than the prepared update in the history store must be a full value. */
WT_ASSERT(session, (uint8_t)type_full == WT_UPDATE_STANDARD);
/* Use time window in cell to initialize the update. */
__wt_hs_upd_time_window(hs_cursor, &hs_tw);
WT_ERR(__wt_upd_alloc(session, hs_value, WT_UPDATE_STANDARD, &upd, &size));
upd->txnid = hs_tw->start_txn;
upd->durable_ts = hs_tw->durable_start_ts;
upd->start_ts = hs_tw->start_ts;
/*
* Set the flag to indicate that this update has been restored from history store for the
* rollback of a prepared transaction.
*/
F_SET(upd, WT_UPDATE_RESTORED_FROM_HS | WT_UPDATE_TO_DELETE_FROM_HS);
total_size += size;
__wt_verbose_debug2(session, WT_VERB_TRANSACTION,
"update restored from history store (txnid: %" PRIu64 ", start_ts: %s, durable_ts: %s",
upd->txnid, __wt_timestamp_to_string(upd->start_ts, ts_string[0]),
__wt_timestamp_to_string(upd->durable_ts, ts_string[1]));
/* If the history store record has a valid stop time point, append it. */
if (hs_stop_durable_ts != WT_TS_MAX) {
WT_ASSERT(session, hs_tw->stop_ts != WT_TS_MAX);
WT_ERR(__wt_upd_alloc(session, NULL, WT_UPDATE_TOMBSTONE, &tombstone, &size));
tombstone->durable_ts = hs_tw->durable_stop_ts;
tombstone->start_ts = hs_tw->stop_ts;
tombstone->txnid = hs_tw->stop_txn;
tombstone->next = upd;
/*
* Set the flag to indicate that this update has been restored from history store for the
* rollback of a prepared transaction.
*/
F_SET(tombstone, WT_UPDATE_RESTORED_FROM_HS | WT_UPDATE_TO_DELETE_FROM_HS);
total_size += size;
__wt_verbose_debug2(session, WT_VERB_TRANSACTION,
"tombstone restored from history store (txnid: %" PRIu64 ", start_ts: %s, durable_ts: %s",
tombstone->txnid, __wt_timestamp_to_string(tombstone->start_ts, ts_string[0]),
__wt_timestamp_to_string(tombstone->durable_ts, ts_string[1]));
upd = tombstone;
}
/* Walk to the end of the chain and we can only have prepared updates on the update chain. */
for (;; upd_chain = upd_chain->next) {
WT_ASSERT(session,
upd_chain->txnid != WT_TXN_ABORTED && upd_chain->prepare_state == WT_PREPARE_INPROGRESS);
if (upd_chain->next == NULL)
break;
}
/* Append the update to the end of the chain. */
WT_PUBLISH(upd_chain->next, upd);
__wt_cache_page_inmem_incr(session, page, total_size);
if (0) {
err:
WT_ASSERT(session, tombstone == NULL || upd == tombstone);
__wt_free_update_list(session, &upd);
}
__wt_scr_free(session, &hs_value);
return (ret);
}
/*
* __txn_timestamp_usage_check --
* Check if a commit will violate timestamp rules.
*/
static inline int
__txn_timestamp_usage_check(WT_SESSION_IMPL *session, WT_TXN_OP *op, WT_UPDATE *upd)
{
WT_BTREE *btree;
WT_TXN *txn;
wt_timestamp_t op_ts, prev_op_durable_ts;
uint16_t flags;
char ts_string[2][WT_TS_INT_STRING_SIZE];
const char *name;
bool no_ts_ok, txn_has_ts;
btree = op->btree;
txn = session->txn;
flags = btree->dhandle->ts_flags;
name = btree->dhandle->name;
txn_has_ts = F_ISSET(txn, WT_TXN_HAS_TS_COMMIT | WT_TXN_HAS_TS_DURABLE);
/* Timestamps are ignored on logged files. */
if (F_ISSET(btree, WT_BTREE_LOGGED))
return (0);
/*
* Do not check for timestamp usage in recovery. We don't expect recovery to be using timestamps
* when applying commits, and it is possible that timestamps may be out-of-order in log replay.
*/
if (F_ISSET(S2C(session), WT_CONN_RECOVERING))
return (0);
op_ts = upd->start_ts != WT_TS_NONE ? upd->start_ts : txn->commit_timestamp;
/* Check for disallowed timestamps. */
if (LF_ISSET(WT_DHANDLE_TS_NEVER)) {
if (!txn_has_ts)
return (0);
__wt_err(session, EINVAL,
"%s: " WT_TS_VERBOSE_PREFIX "timestamp %s set when disallowed by table configuration",
name, __wt_timestamp_to_string(op_ts, ts_string[0]));
#ifdef HAVE_DIAGNOSTIC
__wt_abort(session);
#endif
return (EINVAL);
}
prev_op_durable_ts = upd->prev_durable_ts;
/*
* Ordered consistency requires all updates use timestamps, once they are first used, but this
* test can be turned off on a per-transaction basis.
*/
no_ts_ok = F_ISSET(txn, WT_TXN_TS_NOT_SET);
if (!txn_has_ts && prev_op_durable_ts != WT_TS_NONE && !no_ts_ok) {
__wt_err(session, EINVAL,
"%s: " WT_TS_VERBOSE_PREFIX
"no timestamp provided for an update to a table configured to always use timestamps "
"once they are first used",
name);
#ifdef HAVE_DIAGNOSTIC
__wt_abort(session);
#endif
return (EINVAL);
}
/* Ordered consistency requires all updates be in timestamp order. */
if (txn_has_ts && prev_op_durable_ts > op_ts) {
__wt_err(session, EINVAL,
"%s: " WT_TS_VERBOSE_PREFIX
"updating a value with a timestamp %s before the previous update %s",
name, __wt_timestamp_to_string(op_ts, ts_string[0]),
__wt_timestamp_to_string(prev_op_durable_ts, ts_string[1]));
#ifdef HAVE_DIAGNOSTIC
__wt_abort(session);
#endif
return (EINVAL);
}
return (0);
}
/*
* __txn_fixup_hs_update --
* Fix the history store update with the max stop time point if we commit the prepared update.
*/
static int
__txn_fixup_hs_update(WT_SESSION_IMPL *session, WT_CURSOR *hs_cursor)
{
WT_DECL_ITEM(hs_value);
WT_DECL_RET;
WT_TIME_WINDOW *hs_tw, tw;
WT_TXN *txn;
wt_timestamp_t hs_durable_ts, hs_stop_durable_ts;
uint64_t type_full;
bool txn_error, txn_prepare_ignore_api_check;
hs_tw = NULL;
txn = session->txn;
__wt_hs_upd_time_window(hs_cursor, &hs_tw);
/*
* If the history update already has a stop time point there is no work to do. This happens if a
* deleted key is reinserted by a prepared update.
*/
if (WT_TIME_WINDOW_HAS_STOP(hs_tw))
return (0);
WT_RET(__wt_scr_alloc(session, 0, &hs_value));
/*
* Transaction error is cleared temporarily as cursor functions are not allowed after an error
* or a prepared transaction.
*/
txn_error = F_ISSET(txn, WT_TXN_ERROR);
F_CLR(txn, WT_TXN_ERROR);
/*
* The API layer will immediately return an error if the WT_TXN_PREPARE flag is set before
* attempting cursor operations. However, we can't clear the WT_TXN_PREPARE flag because a
* function in the eviction flow may attempt to forcibly rollback the transaction if it is not
* marked as a prepared transaction. The flag WT_TXN_PREPARE_IGNORE_API_CHECK is set so that
* cursor operations can proceed without having to clear the WT_TXN_PREPARE flag.
*/
txn_prepare_ignore_api_check = F_ISSET(txn, WT_TXN_PREPARE_IGNORE_API_CHECK);
F_SET(txn, WT_TXN_PREPARE_IGNORE_API_CHECK);
/* Get current value. */
WT_ERR(
hs_cursor->get_value(hs_cursor, &hs_stop_durable_ts, &hs_durable_ts, &type_full, hs_value));
/* The old stop timestamp must be max. */
WT_ASSERT(session, hs_stop_durable_ts == WT_TS_MAX);
/* The value older than the prepared update in the history store must be a full value. */
WT_ASSERT(session, (uint8_t)type_full == WT_UPDATE_STANDARD);
/*
* Set the stop time point to be the committing transaction's time point and copy the start time
* point from the current history store update.
*/
tw.stop_ts = txn->commit_timestamp;
tw.durable_stop_ts = txn->durable_timestamp;
tw.stop_txn = txn->id;
WT_TIME_WINDOW_COPY_START(&tw, hs_tw);
/*
* We need to update the stop durable timestamp stored in the history store value.
*
* Pack the value using cursor api.
*/
hs_cursor->set_value(hs_cursor, &tw, tw.durable_stop_ts, tw.durable_start_ts,
(uint64_t)WT_UPDATE_STANDARD, hs_value);
WT_ERR(hs_cursor->update(hs_cursor));
err:
if (!txn_prepare_ignore_api_check)
F_CLR(txn, WT_TXN_PREPARE_IGNORE_API_CHECK);
if (txn_error)
F_SET(txn, WT_TXN_ERROR);
__wt_scr_free(session, &hs_value);
return (ret);
}
/*
* __txn_search_prepared_op --
* Search for an operation's prepared update.
*/
static int
__txn_search_prepared_op(
WT_SESSION_IMPL *session, WT_TXN_OP *op, WT_CURSOR **cursorp, WT_UPDATE **updp)
{
WT_CURSOR *cursor;
WT_DECL_RET;
WT_TXN *txn;
uint32_t txn_flags;
const char *open_cursor_cfg[] = {WT_CONFIG_BASE(session, WT_SESSION_open_cursor), NULL};
*updp = NULL;
txn = session->txn;
cursor = *cursorp;