forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn.c
1605 lines (1478 loc) · 40.2 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 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "txn.h"
#include "memtx_tx.h"
#include "txn_limbo.h"
#include "engine.h"
#include "tuple.h"
#include "journal.h"
#include <fiber.h>
#include "xrow.h"
#include "errinj.h"
#include "iproto_constants.h"
#include "box.h"
#include "session.h"
#include "wal_ext.h"
#include "rmean.h"
double too_long_threshold;
/** Last prepare-sequence-number that was assigned to prepared TX. */
int64_t txn_last_psn = 0;
enum txn_isolation_level txn_default_isolation = TXN_ISOLATION_BEST_EFFORT;
const char *txn_isolation_level_strs[txn_isolation_level_MAX] = {
"DEFAULT",
"READ_COMMITTED",
"READ_CONFIRMED",
"BEST_EFFORT",
"LINEARIZABLE",
};
const char *txn_isolation_level_aliases[txn_isolation_level_MAX] = {
"default",
"read-committed",
"read-confirmed",
"best-effort",
"linearizable"
};
/* Txn cache. */
static STAILQ(txn_cache);
static int
txn_on_stop(struct trigger *trigger, void *event);
static int
txn_on_yield(struct trigger *trigger, void *event);
/** String representation of enum tx_alloc_type. */
const char *tx_alloc_type_strs[TX_ALLOC_TYPE_MAX] = {
"statements",
"user",
"system",
};
/** Objects for tx_region_alloc_object method. */
enum tx_alloc_object {
/**
* Object of type struct txn_stmt.
*/
TX_OBJECT_STMT = 0,
/**
* Object of type struct xrow_header.
*/
TX_OBJECT_XROW_HEADER = 1,
/**
* Object of type struct trigger.
*/
TX_OBJECT_TRIGGER = 2,
/**
* Object of type struct ev_timer.
*/
TX_OBJECT_EV_TIMER = 3,
TX_OBJECT_MAX = 4,
};
/**
* Reset txn's alloc_stats.
*/
static inline void
txn_reset_stats(struct txn *txn)
{
memset(txn->alloc_stats, 0, sizeof(txn->alloc_stats));
}
/**
* Collect allocation statistics.
*/
static inline void
tx_track_allocation(struct txn *txn, size_t size, enum tx_alloc_type type)
{
assert(type < TX_ALLOC_TYPE_MAX);
assert(txn != NULL);
assert(txn->alloc_stats != NULL);
/* Check if txn region is not released. */
assert(txn->acquired_region_used == 0);
txn->alloc_stats[type] += size;
}
/**
* Choose tx_alloc_type for alloc_obj.
*/
static inline enum tx_alloc_type
tx_region_object_to_type(enum tx_alloc_object alloc_obj)
{
enum tx_alloc_type alloc_type = TX_ALLOC_TYPE_MAX;
switch (alloc_obj) {
case TX_OBJECT_STMT:
alloc_type = TX_ALLOC_STMT;
break;
case TX_OBJECT_XROW_HEADER:
case TX_OBJECT_TRIGGER:
case TX_OBJECT_EV_TIMER:
alloc_type = TX_ALLOC_SYSTEM;
break;
default:
unreachable();
};
assert(alloc_type < TX_ALLOC_TYPE_MAX);
return alloc_type;
}
/**
* Alloc object on region. Pass object as enum tx_alloc_object.
* Use this method to track txn's allocations!
*/
static inline void *
tx_region_alloc_object(struct txn *txn, enum tx_alloc_object alloc_obj,
size_t *size)
{
size_t alloc_size = 0;
void *alloc = NULL;
enum tx_alloc_type alloc_type = tx_region_object_to_type(alloc_obj);
switch (alloc_obj) {
case TX_OBJECT_STMT:
alloc = region_alloc_object(&txn->region,
struct txn_stmt, &alloc_size);
break;
case TX_OBJECT_TRIGGER:
alloc = region_alloc_object(&txn->region,
struct trigger, &alloc_size);
break;
case TX_OBJECT_XROW_HEADER:
alloc = region_alloc_object(&txn->region,
struct xrow_header, &alloc_size);
break;
case TX_OBJECT_EV_TIMER:
alloc = region_alloc_object(&txn->region,
struct ev_timer, &alloc_size);
break;
default:
unreachable();
}
assert(alloc_type < TX_ALLOC_TYPE_MAX);
*size = alloc_size;
if (alloc != NULL)
tx_track_allocation(txn, alloc_size, alloc_type);
return alloc;
}
/**
* Tx_region method for aligned allocations of arbitrary size.
* You must pass allocation type explicitly to categorize an allocation.
* Use this method to track txn's allocations!
*/
static inline void *
tx_region_aligned_alloc(struct txn *txn, size_t size, size_t alignment,
size_t alloc_type)
{
void *allocation = region_aligned_alloc(&txn->region, size, alignment);
if (allocation != NULL)
tx_track_allocation(txn, size, alloc_type);
return allocation;
}
struct region *
tx_region_acquire(struct txn *txn)
{
assert(txn != NULL);
assert(txn->acquired_region_used == 0);
struct region *txn_region = &txn->region;
txn->acquired_region_used = region_used(txn_region);
return txn_region;
}
void
tx_region_release(struct txn *txn, enum tx_alloc_type alloc_type)
{
assert(txn != NULL);
assert(alloc_type < TX_ALLOC_TYPE_MAX);
assert(txn->acquired_region_used != 0);
size_t taken_region_used = region_used(&txn->region);
assert(taken_region_used >= txn->acquired_region_used);
size_t new_alloc_size = taken_region_used - txn->acquired_region_used;
txn->acquired_region_used = 0;
if (new_alloc_size > 0)
tx_track_allocation(txn, new_alloc_size, alloc_type);
}
static inline void
txn_set_timeout(struct txn *txn, double timeout)
{
assert(timeout > 0);
txn->timeout = timeout;
}
static int
txn_add_redo(struct txn *txn, struct txn_stmt *stmt, struct request *request)
{
/* Create a redo log row. */
size_t size;
struct xrow_header *row;
row = tx_region_alloc_object(txn, TX_OBJECT_XROW_HEADER, &size);
if (row == NULL) {
diag_set(OutOfMemory, size, "tx_region_alloc_object", "row");
return -1;
}
if (request->header != NULL) {
*row = *request->header;
} else {
/* Initialize members explicitly to save time on memset() */
row->type = request->type;
row->replica_id = 0;
row->lsn = 0;
row->sync = 0;
row->tm = 0;
row->flags = 0;
}
/*
* Group ID should be set both for requests not having a
* header, and for the ones who have it. This is because
* even if a request has a header, the group id could be
* omitted in it, and is default - 0. Even if the space's
* real group id is different.
*/
struct space *space = stmt->space;
row->group_id = space != NULL ? space_group_id(space) : 0;
if (space != NULL && space->wal_ext != NULL)
space_wal_ext_process_request(space->wal_ext, stmt, request);
struct region *txn_region = tx_region_acquire(txn);
xrow_encode_dml(request, txn_region, row->body, &row->bodycnt);
tx_region_release(txn, TX_ALLOC_SYSTEM);
txn_region = NULL;
stmt->row = row;
return 0;
}
/** Initialize a new stmt object within txn. */
static struct txn_stmt *
txn_stmt_new(struct txn *txn)
{
size_t size;
struct txn_stmt *stmt;
stmt = tx_region_alloc_object(txn, TX_OBJECT_STMT, &size);
if (stmt == NULL) {
diag_set(OutOfMemory, size, "tx_region_alloc_object", "stmt");
return NULL;
}
/* Initialize members explicitly to save time on memset() */
stmt->txn = in_txn();
stmt->space = NULL;
stmt->old_tuple = NULL;
stmt->new_tuple = NULL;
stmt->rollback_info.old_tuple = NULL;
stmt->rollback_info.new_tuple = NULL;
stmt->add_story = NULL;
stmt->del_story = NULL;
stmt->next_in_del_list = NULL;
stmt->engine_savepoint = NULL;
stmt->row = NULL;
stmt->has_triggers = false;
stmt->does_require_old_tuple = false;
stmt->is_pure_insert = false;
return stmt;
}
static inline void
txn_stmt_destroy(struct txn_stmt *stmt)
{
assert(stmt->add_story == NULL && stmt->del_story == NULL);
if (stmt->old_tuple != NULL)
tuple_unref(stmt->old_tuple);
if (stmt->new_tuple != NULL)
tuple_unref(stmt->new_tuple);
if (stmt->rollback_info.old_tuple != NULL)
tuple_unref(stmt->rollback_info.old_tuple);
if (stmt->rollback_info.new_tuple != NULL)
tuple_unref(stmt->rollback_info.new_tuple);
}
void
txn_stmt_prepare_rollback_info(struct txn_stmt *stmt, struct tuple *old_tuple,
struct tuple *new_tuple)
{
stmt->rollback_info.old_tuple = old_tuple;
if (stmt->rollback_info.old_tuple != NULL)
tuple_ref(stmt->rollback_info.old_tuple);
stmt->rollback_info.new_tuple = new_tuple;
if (stmt->rollback_info.new_tuple != NULL)
tuple_ref(stmt->rollback_info.new_tuple);
}
/*
* Undo changes done by a statement and run the corresponding
* rollback triggers.
*
* Note, a trigger set by a particular statement must be run right
* after the statement is rolled back, because rollback triggers
* installed by DDL statements restore the schema cache, which is
* necessary to roll back previous statements. For example, to roll
* back a DML statement applied to a space whose index is dropped
* later in the same transaction, we must restore the dropped index
* first.
*/
static void
txn_rollback_one_stmt(struct txn *txn, struct txn_stmt *stmt)
{
if (txn->engine != NULL && stmt->space != NULL)
engine_rollback_statement(txn->engine, txn, stmt);
if (stmt->has_triggers && trigger_run(&stmt->on_rollback, txn) != 0) {
diag_log();
panic("statement rollback trigger failed");
}
}
/**
* Assign's a PSN to a RW transaction that wasn't yet prepared.
*/
static void
txn_assign_psn(struct txn *txn)
{
if (txn->psn == 0) {
if (txn->status == TXN_IN_READ_VIEW) {
assert(stailq_empty(&txn->stmts));
} else {
assert(txn->status == TXN_INPROGRESS ||
txn->status == TXN_ABORTED);
txn->psn = ++txn_last_psn;
}
} else {
assert(txn->status == TXN_PREPARED);
}
}
/*
* Begins the rollback to savepoint process by assigning a PSN to the
* transaction (rolled back statements require a PSN).
*/
static void
txn_rollback_to_svp_begin(struct txn *txn)
{
txn_assign_psn(txn);
}
/*
* Finishes the rollback to savepoint process by resetting the transaction's
* PSN (since this not a complete transaction rollback).
*/
static void
txn_rollback_to_svp_finish(struct txn *txn)
{
if (txn->status != TXN_PREPARED)
txn->psn = 0;
}
static void
txn_rollback_to_svp(struct txn *txn, struct stailq_entry *svp)
{
txn_rollback_to_svp_begin(txn);
struct txn_stmt *stmt;
struct stailq rollback;
stailq_cut_tail(&txn->stmts, svp, &rollback);
stailq_reverse(&rollback);
stailq_foreach_entry(stmt, &rollback, next) {
txn_rollback_one_stmt(txn, stmt);
if (stmt->row != NULL && stmt->row->replica_id == 0) {
assert(txn->n_new_rows > 0);
txn->n_new_rows--;
if (stmt->row->group_id == GROUP_LOCAL)
txn->n_local_rows--;
}
if (stmt->row != NULL && stmt->row->replica_id != 0) {
assert(txn->n_applier_rows > 0);
txn->n_applier_rows--;
}
txn_stmt_destroy(stmt);
stmt->space = NULL;
stmt->row = NULL;
}
txn_rollback_to_svp_finish(txn);
}
/*
* Return a txn from cache or create a new one if cache is empty.
*/
inline static struct txn *
txn_new(void)
{
if (!stailq_empty(&txn_cache))
return stailq_shift_entry(&txn_cache, struct txn, in_txn_cache);
/* Create a region. */
struct region region;
region_create(®ion, &cord()->slabc);
/* Place txn structure on the region. */
int size;
struct txn *txn = region_alloc_object(®ion, struct txn, &size);
if (txn == NULL) {
diag_set(OutOfMemory, size, "region_alloc_object", "txn");
return NULL;
}
assert(region_used(®ion) == sizeof(*txn));
txn_reset_stats(txn);
txn->region = region;
rlist_create(&txn->read_set);
rlist_create(&txn->point_holes_list);
rlist_create(&txn->gap_list);
rlist_create(&txn->full_scan_list);
rlist_create(&txn->conflict_list);
rlist_create(&txn->conflicted_by_list);
rlist_create(&txn->in_read_view_txs);
rlist_create(&txn->in_all_txs);
txn->space_on_replace_triggers_depth = 0;
txn->acquired_region_used = 0;
txn->limbo_entry = NULL;
return txn;
}
/*
* Free txn memory and return it to a cache.
*/
inline static void
txn_free(struct txn *txn)
{
assert(txn->limbo_entry == NULL);
if (txn->rollback_timer != NULL)
ev_timer_stop(loop(), txn->rollback_timer);
memtx_tx_clean_txn(txn);
struct txn_stmt *stmt;
stailq_foreach_entry(stmt, &txn->stmts, next)
txn_stmt_destroy(stmt);
/* Truncate region up to struct txn size. */
txn_reset_stats(txn);
region_truncate(&txn->region, sizeof(struct txn));
stailq_add(&txn_cache, &txn->in_txn_cache);
}
void
diag_set_txn_sign_detailed(const char *file, unsigned line, int64_t signature)
{
if (signature >= JOURNAL_ENTRY_ERR_MIN)
return diag_set_journal_res_detailed(file, line, signature);
switch(signature) {
case TXN_SIGNATURE_ROLLBACK:
diag_set_detailed(file, line, ClientError, ER_TXN_ROLLBACK);
return;
case TXN_SIGNATURE_QUORUM_TIMEOUT:
diag_set_detailed(file, line, ClientError,
ER_SYNC_QUORUM_TIMEOUT);
return;
case TXN_SIGNATURE_SYNC_ROLLBACK:
diag_set_detailed(file, line, ClientError, ER_SYNC_ROLLBACK);
return;
case TXN_SIGNATURE_ABORT:
if (diag_is_empty(diag_get()))
panic("Tried to get an absent transaction error");
return;
}
panic("Transaction signature %lld can't be converted to an error "
"at %s:%u", (long long)signature, file, line);
}
struct txn *
txn_begin(void)
{
static int64_t tsn = 0;
assert(! in_txn());
struct txn *txn = txn_new();
if (txn == NULL)
return NULL;
assert(rlist_empty(&txn->conflict_list));
assert(rlist_empty(&txn->conflicted_by_list));
/* Initialize members explicitly to save time on memset() */
stailq_create(&txn->stmts);
txn->n_new_rows = 0;
txn->n_local_rows = 0;
txn->n_applier_rows = 0;
txn->flags = 0;
txn->in_sub_stmt = 0;
txn->id = ++tsn;
txn->psn = 0;
txn->rv_psn = 0;
txn->status = TXN_INPROGRESS;
txn->isolation = txn_default_isolation;
txn->signature = TXN_SIGNATURE_UNKNOWN;
txn->engine = NULL;
txn->engine_tx = NULL;
txn->is_schema_changed = false;
rlist_create(&txn->savepoints);
txn->fiber = NULL;
txn->timeout = TIMEOUT_INFINITY;
txn->rollback_timer = NULL;
fiber_set_txn(fiber(), txn);
trigger_create(&txn->fiber_on_yield, txn_on_yield, NULL, NULL);
trigger_add(&fiber()->on_yield, &txn->fiber_on_yield);
trigger_create(&txn->fiber_on_stop, txn_on_stop, NULL, NULL);
trigger_add(&fiber()->on_stop, &txn->fiber_on_stop);
/*
* By default, all transactions may yield.
* It's a responsibility of an engine to disable yields
* if they are not supported.
*/
txn_set_flags(txn, TXN_CAN_YIELD);
int rc = memtx_tx_register_txn(txn);
if (rc == -1) {
txn_free(txn);
return NULL;
}
rmean_collect(rmean_box, IPROTO_BEGIN, 1);
return txn;
}
int
txn_begin_in_engine(struct engine *engine, struct txn *txn)
{
if (engine->flags & ENGINE_BYPASS_TX)
return 0;
if (txn->engine == NULL) {
txn->engine = engine;
return engine_begin(engine, txn);
} else if (txn->engine != engine) {
/**
* Only one engine can be used in
* a multi-statement transaction currently.
*/
diag_set(ClientError, ER_CROSS_ENGINE_TRANSACTION);
return -1;
}
return 0;
}
int
txn_begin_stmt(struct txn *txn, struct space *space, uint16_t type)
{
assert(txn == in_txn());
assert(txn != NULL);
if (txn->in_sub_stmt > TXN_SUB_STMT_MAX) {
diag_set(ClientError, ER_SUB_STMT_MAX);
return -1;
}
if (txn->status == TXN_IN_READ_VIEW) {
rlist_del(&txn->in_read_view_txs);
txn->status = TXN_ABORTED;
txn_set_flags(txn, TXN_IS_CONFLICTED);
}
if (txn_check_can_continue(txn) != 0)
return -1;
struct txn_stmt *stmt = txn_stmt_new(txn);
if (stmt == NULL)
return -1;
/* Set the savepoint for statement rollback. */
txn->sub_stmt_begin[txn->in_sub_stmt] = stailq_last(&txn->stmts);
txn->in_sub_stmt++;
stailq_add_tail_entry(&txn->stmts, stmt, next);
if (space == NULL)
return 0;
if (txn_check_space(txn, space) != 0)
goto fail;
struct engine *engine = space->engine;
if (txn_begin_in_engine(engine, txn) != 0)
goto fail;
stmt->space = space;
stmt->type = type;
if (engine_begin_statement(engine, txn) != 0)
goto fail;
return 0;
fail:
txn_rollback_stmt(txn);
return -1;
}
bool
txn_is_distributed(struct txn *txn)
{
assert(txn == in_txn());
/**
* Transaction has both new and applier rows, and some of
* the new rows need to be replicated back to the
* server of transaction origin.
*/
return (txn->n_new_rows > 0 && txn->n_applier_rows > 0 &&
txn->n_new_rows != txn->n_local_rows);
}
/**
* End a statement.
*/
int
txn_commit_stmt(struct txn *txn, struct request *request)
{
assert(txn->in_sub_stmt > 0);
/*
* Run on_replace triggers. For now, disallow mutation
* of tuples in the trigger.
*/
struct txn_stmt *stmt = txn_current_stmt(txn);
/*
* Create WAL record for the write requests in
* non-temporary spaces. stmt->space can be NULL for
* IRPOTO_NOP or IPROTO_RAFT_CONFIRM.
*/
if (stmt->space == NULL || !space_is_temporary(stmt->space)) {
if (txn_add_redo(txn, stmt, request) != 0)
goto fail;
assert(stmt->row != NULL);
if (stmt->row->replica_id == 0) {
++txn->n_new_rows;
if (stmt->row->group_id == GROUP_LOCAL)
++txn->n_local_rows;
} else {
++txn->n_applier_rows;
}
}
/*
* If there are triggers, and they are not disabled, and
* the statement found any rows, run triggers.
* XXX:
* - vinyl doesn't set old/new tuple, so triggers don't
* work for it
* - perhaps we should run triggers even for deletes which
* doesn't find any rows
*/
if (stmt->space != NULL && stmt->space->run_triggers &&
(stmt->old_tuple || stmt->new_tuple)) {
if (!rlist_empty(&stmt->space->before_replace)) {
/*
* Triggers see old_tuple and that tuple
* must remain the same
*/
stmt->does_require_old_tuple = true;
}
if (!rlist_empty(&stmt->space->on_replace)) {
/*
* Triggers see old_tuple and that tuple
* must remain the same
*/
stmt->does_require_old_tuple = true;
txn->space_on_replace_triggers_depth++;
int rc = trigger_run(&stmt->space->on_replace, txn);
txn->space_on_replace_triggers_depth--;
if (rc != 0)
goto fail;
assert(txn->in_sub_stmt > 0);
}
}
--txn->in_sub_stmt;
return 0;
fail:
txn_rollback_stmt(txn);
return -1;
}
/* A helper function to process on_wal_write triggers. */
static void
txn_run_wal_write_triggers(struct txn *txn)
{
/* Is zero during recovery. */
assert(txn->signature >= 0);
if (trigger_run(&txn->on_wal_write, txn) != 0) {
/*
* As transaction couldn't handle a trigger error so
* there is no option except panic.
*/
diag_log();
unreachable();
panic("wal_write trigger failed");
}
/* WAL write happens only once. */
trigger_destroy(&txn->on_wal_write);
}
/**
* If there is no fiber waiting for the transaction then the
* transaction could be safely freed. In the opposite case the
* owner fiber is in duty to free this transaction.
*/
static void
txn_free_or_wakeup(struct txn *txn)
{
if (txn->fiber == NULL)
txn_free(txn);
else {
txn_set_flags(txn, TXN_IS_DONE);
fiber_wakeup(txn->fiber);
}
}
void
txn_complete_fail(struct txn *txn)
{
assert(txn->psn != 0);
assert(!txn_has_flag(txn, TXN_IS_DONE));
assert(txn->signature < 0);
assert(txn->signature != TXN_SIGNATURE_UNKNOWN);
if (txn->limbo_entry != NULL) {
assert(txn_has_flag(txn, TXN_WAIT_SYNC));
txn_limbo_abort(&txn_limbo, txn->limbo_entry);
txn->limbo_entry = NULL;
}
txn->status = TXN_ABORTED;
struct txn_stmt *stmt;
stailq_reverse(&txn->stmts);
stailq_foreach_entry(stmt, &txn->stmts, next)
txn_rollback_one_stmt(txn, stmt);
if (txn->engine != NULL)
engine_rollback(txn->engine, txn);
if (txn_has_flag(txn, TXN_HAS_TRIGGERS)) {
assert(in_txn() != NULL);
if (trigger_run(&txn->on_rollback, txn) != 0) {
diag_log();
panic("transaction rollback trigger failed");
}
/* Can't rollback more than once. */
trigger_destroy(&txn->on_rollback);
/* Commit won't happen after rollback. */
trigger_destroy(&txn->on_commit);
}
txn_free_or_wakeup(txn);
rmean_collect(rmean_box, IPROTO_ROLLBACK, 1);
}
void
txn_complete_success(struct txn *txn)
{
assert(!txn_has_flag(txn, TXN_IS_DONE));
assert(!txn_has_flag(txn, TXN_WAIT_SYNC));
assert(txn->signature >= 0);
txn->status = TXN_COMMITTED;
if (txn->engine != NULL)
engine_commit(txn->engine, txn);
if (txn_has_flag(txn, TXN_HAS_TRIGGERS)) {
assert(in_txn() != NULL);
/*
* Commit triggers must be run in the same order they were added
* so that a trigger sees the changes done by previous triggers
* (this is vital for DDL).
*/
if (trigger_run_reverse(&txn->on_commit, txn) != 0) {
diag_log();
panic("transaction commit trigger failed");
}
/* Can't commit more than once. */
trigger_destroy(&txn->on_commit);
/* Rollback won't happen after commit. */
trigger_destroy(&txn->on_rollback);
}
txn_free_or_wakeup(txn);
rmean_collect(rmean_box, IPROTO_COMMIT, 1);
}
/** Callback invoked when the transaction's journal write is finished. */
static void
txn_on_journal_write(struct journal_entry *entry)
{
struct txn *txn = entry->complete_data;
assert(txn->signature == TXN_SIGNATURE_UNKNOWN);
txn->signature = entry->res;
/*
* Some commit/rollback triggers require for in_txn fiber
* variable to be set so restore it for the time triggers
* are in progress.
*/
assert(in_txn() == NULL);
fiber_set_txn(fiber(), txn);
/*
* Use session and credentials of the original fiber for
* commit/rollback triggers.
*/
struct session *orig_session = fiber_get_session(fiber());
struct session *session = (txn->fiber != NULL ?
fiber_get_session(txn->fiber) : NULL);
if (session != NULL)
fiber_set_session(fiber(), session);
struct credentials *orig_creds = fiber_get_user(fiber());
struct credentials *creds = (txn->fiber != NULL ?
fiber_get_user(txn->fiber) : NULL);
if (creds != NULL)
fiber_set_user(fiber(), creds);
if (txn->signature < 0) {
txn_complete_fail(txn);
goto finish;
}
double stop_tm = ev_monotonic_now(loop());
double delta = stop_tm - txn->start_tm;
if (delta > too_long_threshold) {
int n_rows = txn->n_new_rows + txn->n_applier_rows;
say_warn_ratelimited("too long WAL write: %d rows at LSN %lld: "
"%.3f sec", n_rows,
(long long)(txn->signature - n_rows + 1),
delta);
}
if (txn_has_flag(txn, TXN_HAS_TRIGGERS))
txn_run_wal_write_triggers(txn);
if (!txn_has_flag(txn, TXN_WAIT_SYNC)) {
txn_complete_success(txn);
} else {
int64_t lsn;
/*
* XXX: that is quite ugly. Need a more reliable way to get the
* synchro LSN.
*/
if (txn->n_applier_rows > 0)
lsn = entry->rows[txn->n_applier_rows - 1]->lsn;
else
lsn = entry->rows[entry->n_rows - 1]->lsn;
txn_limbo_assign_lsn(&txn_limbo, txn->limbo_entry, lsn);
if (txn->fiber != NULL)
fiber_wakeup(txn->fiber);
}
finish:
fiber_set_txn(fiber(), NULL);
fiber_set_user(fiber(), orig_creds);
fiber_set_session(fiber(), orig_session);
}
static struct journal_entry *
txn_journal_entry_new(struct txn *txn)
{
struct journal_entry *req;
struct txn_stmt *stmt;
assert(txn->n_new_rows + txn->n_applier_rows > 0);
/* Save space for an additional NOP row just in case. */
struct region *txn_region = tx_region_acquire(txn);
req = journal_entry_new(txn->n_new_rows + txn->n_applier_rows + 1,
txn_region, txn_on_journal_write, txn);
tx_region_release(txn, TX_ALLOC_SYSTEM);
txn_region = NULL;
if (req == NULL)
return NULL;
struct xrow_header **remote_row = req->rows;
struct xrow_header **local_row = req->rows + txn->n_applier_rows;
bool is_sync = false;
/*
* A transaction which consists of NOPs solely should pass through the
* limbo without waiting. Even when the limbo is not empty. This is
* because otherwise they might fail with the limbo being not owned by
* the NOPs owner. But it does not matter, because they just need to
* bump vclock. There is nothing to confirm or rollback in them.
*/
bool is_fully_nop = true;
stailq_foreach_entry(stmt, &txn->stmts, next) {
if (stmt->has_triggers) {
txn_init_triggers(txn);
rlist_splice(&txn->on_commit, &stmt->on_commit);
}
/* A read (e.g. select) request */
if (stmt->row == NULL)
continue;
if (stmt->row->type != IPROTO_NOP) {
is_fully_nop = false;
is_sync = is_sync || (stmt->space != NULL &&
space_is_sync(stmt->space));
}
if (stmt->row->replica_id == 0)
*local_row++ = stmt->row;
else
*remote_row++ = stmt->row;
req->approx_len += xrow_approx_len(stmt->row);
}
/*
* There is no a check for all-local rows, because a local
* space can't be synchronous. So if there is at least one
* synchronous space, the transaction is not local.
*/
if (!txn_has_flag(txn, TXN_FORCE_ASYNC) && !is_fully_nop) {
if (is_sync) {
txn_set_flags(txn, TXN_WAIT_SYNC | TXN_WAIT_ACK);
} else if (!txn_limbo_is_empty(&txn_limbo)) {
/*
* There some sync entries on the
* fly thus wait for their completion
* even if this particular transaction
* doesn't touch sync space (each sync txn
* should be considered as a barrier).
*/
txn_set_flags(txn, TXN_WAIT_SYNC);
}
}
assert(remote_row == req->rows + txn->n_applier_rows);
assert(local_row == remote_row + txn->n_new_rows);
/*
* Append a dummy NOP statement to preserve replication tx
* boundaries when the last tx row is a local one, and the
* transaction has at least one global row.
*/
if (txn->n_local_rows > 0 &&
(txn->n_local_rows != txn->n_new_rows || txn->n_applier_rows > 0) &&
(*(local_row - 1))->group_id == GROUP_LOCAL) {
size_t size;
*local_row =
tx_region_alloc_object(txn, TX_OBJECT_XROW_HEADER,
&size);
if (*local_row == NULL) {
diag_set(OutOfMemory, size, "tx_region_alloc_object",
"row");
return NULL;
}
memset(*local_row, 0, sizeof(**local_row));
(*local_row)->type = IPROTO_NOP;
(*local_row)->group_id = GROUP_DEFAULT;
} else {
--req->n_rows;
}
static const uint8_t flags_map[] = {
[TXN_WAIT_SYNC] = IPROTO_FLAG_WAIT_SYNC,
[TXN_WAIT_ACK] = IPROTO_FLAG_WAIT_ACK,
};
req->flags |= flags_map[txn->flags & TXN_WAIT_SYNC];
req->flags |= flags_map[txn->flags & TXN_WAIT_ACK];
return req;
}
/*
* Prepare a transaction using engines.
*/
static int
txn_prepare(struct txn *txn)
{
if (txn_check_can_continue(txn) != 0)
return -1;
txn_assign_psn(txn);
if (txn->rollback_timer != NULL) {
ev_timer_stop(loop(), txn->rollback_timer);
txn->rollback_timer = NULL;
}