forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn_limbo.c
1261 lines (1178 loc) · 34.1 KB
/
txn_limbo.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-2020, 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 "txn_limbo.h"
#include "replication.h"
#include "iproto_constants.h"
#include "journal.h"
#include "box.h"
#include "raft.h"
#include "tt_static.h"
struct txn_limbo txn_limbo;
static inline void
txn_limbo_create(struct txn_limbo *limbo)
{
rlist_create(&limbo->queue);
limbo->len = 0;
limbo->owner_id = REPLICA_ID_NIL;
fiber_cond_create(&limbo->wait_cond);
vclock_create(&limbo->vclock);
vclock_create(&limbo->promote_term_map);
limbo->promote_greatest_term = 0;
latch_create(&limbo->promote_latch);
limbo->confirmed_lsn = 0;
limbo->rollback_count = 0;
limbo->is_in_rollback = false;
limbo->svp_confirmed_lsn = -1;
limbo->frozen_reasons = 0;
limbo->is_frozen_until_promotion = true;
limbo->do_validate = false;
}
static inline bool
txn_limbo_is_frozen(const struct txn_limbo *limbo)
{
return limbo->frozen_reasons != 0;
}
bool
txn_limbo_is_ro(struct txn_limbo *limbo)
{
return limbo->owner_id != REPLICA_ID_NIL &&
(limbo->owner_id != instance_id || txn_limbo_is_frozen(limbo));
}
void
txn_limbo_complete(struct txn *txn, bool is_success)
{
/*
* Some rollback/commit triggers require the in_txn fiber
* variable to be set.
*/
assert(in_txn() == NULL);
fiber_set_txn(fiber(), txn);
if (is_success)
txn_complete_success(txn);
else
txn_complete_fail(txn);
fiber_set_txn(fiber(), NULL);
}
struct txn_limbo_entry *
txn_limbo_last_synchro_entry(struct txn_limbo *limbo)
{
struct txn_limbo_entry *entry;
rlist_foreach_entry_reverse(entry, &limbo->queue, in_queue) {
if (txn_has_flag(entry->txn, TXN_WAIT_ACK))
return entry;
}
return NULL;
}
struct txn_limbo_entry *
txn_limbo_append(struct txn_limbo *limbo, uint32_t id, struct txn *txn)
{
assert(txn_has_flag(txn, TXN_WAIT_SYNC));
assert(limbo == &txn_limbo);
/*
* Transactions should be added to the limbo before WAL write. Limbo
* needs that to be able rollback transactions, whose WAL write is in
* progress.
*/
assert(txn->signature == TXN_SIGNATURE_UNKNOWN);
assert(txn->status == TXN_PREPARED);
if (limbo->is_in_rollback) {
/*
* Cascading rollback. It is impossible to commit the
* transaction, because if there is an existing rollback in
* progress, it should rollback this one too for the sake of
* 'reversed rollback order' rule. On the other hand the
* rollback can't be postponed until after WAL write as well -
* it should be done right now. See in the limbo comments why.
*/
diag_set(ClientError, ER_SYNC_ROLLBACK);
return NULL;
}
if (id == 0)
id = instance_id;
if (limbo->owner_id == REPLICA_ID_NIL) {
diag_set(ClientError, ER_SYNC_QUEUE_UNCLAIMED);
return NULL;
} else if (limbo->owner_id != id && !txn_is_fully_local(txn)) {
if (txn_limbo_is_empty(limbo)) {
diag_set(ClientError, ER_SYNC_QUEUE_FOREIGN,
limbo->owner_id);
} else {
diag_set(ClientError, ER_UNCOMMITTED_FOREIGN_SYNC_TXNS,
limbo->owner_id);
}
return NULL;
}
size_t size;
struct txn_limbo_entry *e = region_alloc_object(&txn->region,
typeof(*e), &size);
if (e == NULL) {
diag_set(OutOfMemory, size, "region_alloc_object", "e");
return NULL;
}
e->txn = txn;
e->lsn = -1;
e->ack_count = 0;
e->is_commit = false;
e->is_rollback = false;
rlist_add_tail_entry(&limbo->queue, e, in_queue);
limbo->len++;
return e;
}
static inline void
txn_limbo_remove(struct txn_limbo *limbo, struct txn_limbo_entry *entry)
{
assert(!rlist_empty(&entry->in_queue));
assert(txn_limbo_first_entry(limbo) == entry);
rlist_del_entry(entry, in_queue);
limbo->len--;
}
static inline void
txn_limbo_pop(struct txn_limbo *limbo, struct txn_limbo_entry *entry)
{
assert(!rlist_empty(&entry->in_queue));
assert(txn_limbo_last_entry(limbo) == entry);
assert(entry->is_rollback);
rlist_del_entry(entry, in_queue);
limbo->len--;
++limbo->rollback_count;
}
void
txn_limbo_abort(struct txn_limbo *limbo, struct txn_limbo_entry *entry)
{
entry->is_rollback = true;
/*
* The simple rule about rollback/commit order applies
* here as well: commit always in the order of WAL write,
* rollback in the reversed order. Rolled back transaction
* is always the last.
*/
txn_limbo_pop(limbo, entry);
}
void
txn_limbo_assign_remote_lsn(struct txn_limbo *limbo,
struct txn_limbo_entry *entry, int64_t lsn)
{
assert(limbo->owner_id != REPLICA_ID_NIL);
assert(limbo->owner_id != instance_id);
assert(entry->lsn == -1);
assert(lsn > 0);
(void) limbo;
/*
* Same as with local LSN assign, it is given after a WAL write. But for
* remotely received transactions it doesn't matter so far. They don't
* needs ACKs. They wait for explicit confirmations. That will be a
* problem when need acks for anything else and when local txns will
* become optionally non-blocking.
*/
entry->lsn = lsn;
}
void
txn_limbo_assign_local_lsn(struct txn_limbo *limbo,
struct txn_limbo_entry *entry, int64_t lsn)
{
assert(limbo->owner_id != REPLICA_ID_NIL);
assert(limbo->owner_id == instance_id);
assert(entry->lsn == -1);
assert(lsn > 0);
entry->lsn = lsn;
/*
* The entry just got its LSN after a WAL write. It could
* happen that this LSN was already ACKed by some
* replicas. Update the ACK counter to take them into
* account.
*/
struct vclock_iterator iter;
vclock_iterator_init(&iter, &limbo->vclock);
int ack_count = 0;
vclock_foreach(&iter, vc)
ack_count += vc.lsn >= lsn;
assert(ack_count >= entry->ack_count);
entry->ack_count = ack_count;
}
void
txn_limbo_assign_lsn(struct txn_limbo *limbo, struct txn_limbo_entry *entry,
int64_t lsn)
{
if (limbo->owner_id == instance_id)
txn_limbo_assign_local_lsn(limbo, entry, lsn);
else
txn_limbo_assign_remote_lsn(limbo, entry, lsn);
}
static void
txn_limbo_write_rollback(struct txn_limbo *limbo, int64_t lsn);
int
txn_limbo_wait_complete(struct txn_limbo *limbo, struct txn_limbo_entry *entry)
{
assert(entry->lsn > 0 || !txn_has_flag(entry->txn, TXN_WAIT_ACK));
if (txn_limbo_entry_is_complete(entry))
goto complete;
assert(!txn_has_flag(entry->txn, TXN_IS_DONE));
assert(txn_has_flag(entry->txn, TXN_WAIT_SYNC));
double start_time = fiber_clock();
while (true) {
double deadline = start_time + replication_synchro_timeout;
double timeout = deadline - fiber_clock();
int rc = fiber_cond_wait_timeout(&limbo->wait_cond, timeout);
if (txn_limbo_entry_is_complete(entry))
goto complete;
if (txn_limbo_is_frozen(limbo))
goto wait;
if (rc != 0)
break;
}
assert(!txn_limbo_is_empty(limbo));
if (txn_limbo_first_entry(limbo) != entry) {
/*
* If this is not a first entry in the limbo, it
* is definitely not a first timed out entry. And
* since it managed to time out too, it means
* there is currently another fiber writing
* rollback, or waiting for confirmation WAL
* write. Wait when it will finish and wake us up.
*/
goto wait;
}
/* First in the queue is always a synchronous transaction. */
assert(entry->lsn > 0);
if (entry->lsn <= limbo->confirmed_lsn) {
/*
* Yes, the wait timed out, but there is an on-going CONFIRM WAL
* write in another fiber covering this LSN. Can't rollback it
* already. All what can be done is waiting. The CONFIRM writer
* will wakeup all the confirmed txns when WAL write will be
* finished.
*/
goto wait;
}
txn_limbo_write_rollback(limbo, entry->lsn);
struct txn_limbo_entry *e, *tmp;
rlist_foreach_entry_safe_reverse(e, &limbo->queue,
in_queue, tmp) {
e->txn->signature = TXN_SIGNATURE_QUORUM_TIMEOUT;
e->txn->limbo_entry = NULL;
txn_limbo_abort(limbo, e);
txn_clear_flags(e->txn, TXN_WAIT_SYNC | TXN_WAIT_ACK);
txn_limbo_complete(e->txn, false);
if (e == entry)
break;
fiber_wakeup(e->txn->fiber);
}
diag_set(ClientError, ER_SYNC_QUORUM_TIMEOUT);
return -1;
wait:
do {
fiber_yield();
} while (!txn_limbo_entry_is_complete(entry));
complete:
assert(txn_limbo_entry_is_complete(entry));
/*
* Entry is *always* removed from the limbo by the same fiber, which
* installed the commit/rollback flag.
*/
assert(rlist_empty(&entry->in_queue));
assert(txn_has_flag(entry->txn, TXN_IS_DONE));
/*
* The first tx to be rolled back already performed all
* the necessary cleanups for us.
*/
if (entry->is_rollback) {
diag_set(ClientError, ER_SYNC_ROLLBACK);
return -1;
}
return 0;
}
void
txn_limbo_checkpoint(const struct txn_limbo *limbo,
struct synchro_request *req)
{
req->type = IPROTO_RAFT_PROMOTE;
req->replica_id = limbo->owner_id;
req->lsn = limbo->confirmed_lsn;
req->term = limbo->promote_greatest_term;
}
/** Write a request to WAL. */
static void
synchro_request_write(const struct synchro_request *req)
{
/*
* This is a synchronous commit so we can
* allocate everything on a stack.
*/
char body[XROW_SYNCHRO_BODY_LEN_MAX];
struct xrow_header row;
char buf[sizeof(struct journal_entry) +
sizeof(struct xrow_header *)];
struct journal_entry *entry = (struct journal_entry *)buf;
entry->rows[0] = &row;
xrow_encode_synchro(&row, body, req);
journal_entry_create(entry, 1, xrow_approx_len(&row),
journal_entry_fiber_wakeup_cb, fiber());
if (journal_write(entry) != 0)
goto fail;
if (entry->res < 0) {
diag_set_journal_res(entry->res);
goto fail;
}
return;
fail:
diag_log();
/*
* XXX: the stub is supposed to be removed once it is defined what to do
* when a synchro request WAL write fails. One of the possible
* solutions: log the error, keep the limbo queue as is and probably put
* in rollback mode. Then provide a hook to call manually when WAL
* problems are fixed. Or retry automatically with some period.
*/
panic("Could not write a synchro request to WAL: lsn = %lld, "
"type = %s\n", (long long)req->lsn, iproto_type_name(req->type));
}
/** Create a request for a specific limbo and write it to WAL. */
static void
txn_limbo_write_synchro(struct txn_limbo *limbo, uint16_t type, int64_t lsn,
uint64_t term)
{
assert(lsn >= 0);
struct synchro_request req = {
.type = type,
.replica_id = limbo->owner_id,
.lsn = lsn,
.term = term,
};
synchro_request_write(&req);
}
/**
* Write a confirmation entry to WAL. After it's written all the
* transactions waiting for confirmation may be finished.
*/
static void
txn_limbo_write_confirm(struct txn_limbo *limbo, int64_t lsn)
{
assert(lsn > limbo->confirmed_lsn);
assert(!limbo->is_in_rollback);
limbo->confirmed_lsn = lsn;
txn_limbo_write_synchro(limbo, IPROTO_RAFT_CONFIRM, lsn, 0);
}
/** Confirm all the entries <= @a lsn. */
static void
txn_limbo_read_confirm(struct txn_limbo *limbo, int64_t lsn)
{
assert(limbo->owner_id != REPLICA_ID_NIL || txn_limbo_is_empty(limbo));
assert(limbo == &txn_limbo);
struct txn_limbo_entry *e, *tmp;
rlist_foreach_entry_safe(e, &limbo->queue, in_queue, tmp) {
/*
* Check if it is an async transaction last in the queue. When
* it is last, it does not depend on a not finished sync
* transaction anymore and can be confirmed right away.
*/
if (txn_has_flag(e->txn, TXN_WAIT_ACK)) {
/* Sync transaction not covered by the confirmation. */
if (e->lsn > lsn)
break;
/*
* Sync transaction not yet received an LSN. Happens
* only to local master transactions whose WAL write is
* in progress.
*/
if (e->lsn == -1)
break;
} else if (e->txn->signature == TXN_SIGNATURE_UNKNOWN) {
/*
* A transaction might be covered by the CONFIRM even if
* it is not written to WAL yet when it is an async
* transaction. It could be created just when the
* CONFIRM was being written to WAL.
*/
assert(e->txn->status == TXN_PREPARED);
/*
* Let it complete normally as a plain transaction. It
* is important to remove the limbo entry, because the
* async transaction might be committed in a
* non-blocking way and won't ever wait explicitly for
* its completion. Therefore, won't be able to remove
* the limbo entry on its own. This happens for txns
* created in the applier.
*/
txn_clear_flags(e->txn, TXN_WAIT_SYNC);
txn_limbo_remove(limbo, e);
/*
* The limbo entry now should not be used by the owner
* transaction since it just became a plain one. Nullify
* the txn to get a crash on any usage attempt instead
* of potential undefined behaviour.
*/
e->txn->limbo_entry = NULL;
e->txn = NULL;
continue;
}
e->is_commit = true;
e->txn->limbo_entry = NULL;
txn_limbo_remove(limbo, e);
txn_clear_flags(e->txn, TXN_WAIT_SYNC | TXN_WAIT_ACK);
/*
* Should be written to WAL by now. Confirm is always written
* after the affected transactions.
*/
assert(e->txn->signature >= 0);
txn_limbo_complete(e->txn, true);
}
/*
* Track CONFIRM lsn on replica in order to detect split-brain by
* comparing existing confirm_lsn with the one arriving from a remote
* instance.
*/
if (limbo->confirmed_lsn < lsn)
limbo->confirmed_lsn = lsn;
}
/**
* Write a rollback message to WAL. After it's written all the
* transactions following the current one and waiting for
* confirmation must be rolled back.
*/
static void
txn_limbo_write_rollback(struct txn_limbo *limbo, int64_t lsn)
{
assert(lsn > limbo->confirmed_lsn);
assert(!limbo->is_in_rollback);
limbo->is_in_rollback = true;
txn_limbo_write_synchro(limbo, IPROTO_RAFT_ROLLBACK, lsn, 0);
limbo->is_in_rollback = false;
}
/** Rollback all the entries >= @a lsn. */
static void
txn_limbo_read_rollback(struct txn_limbo *limbo, int64_t lsn)
{
assert(limbo->owner_id != REPLICA_ID_NIL || txn_limbo_is_empty(limbo));
assert(limbo == &txn_limbo);
struct txn_limbo_entry *e, *tmp;
struct txn_limbo_entry *last_rollback = NULL;
rlist_foreach_entry_reverse(e, &limbo->queue, in_queue) {
if (!txn_has_flag(e->txn, TXN_WAIT_ACK))
continue;
if (e->lsn < lsn)
break;
last_rollback = e;
}
if (last_rollback == NULL)
return;
rlist_foreach_entry_safe_reverse(e, &limbo->queue, in_queue, tmp) {
txn_limbo_abort(limbo, e);
txn_clear_flags(e->txn, TXN_WAIT_ACK);
/*
* Should be written to WAL by now. Rollback is always written
* after the affected transactions.
*/
assert(e->txn->signature >= 0);
e->txn->signature = TXN_SIGNATURE_SYNC_ROLLBACK;
e->txn->limbo_entry = NULL;
txn_limbo_complete(e->txn, false);
if (e == last_rollback)
break;
}
}
int
txn_limbo_write_promote(struct txn_limbo *limbo, int64_t lsn, uint64_t term)
{
assert(latch_is_locked(&limbo->promote_latch));
/*
* We make sure that promote is only written once everything this
* instance has may be confirmed.
*/
struct txn_limbo_entry *e = txn_limbo_last_synchro_entry(limbo);
assert(e == NULL || e->lsn <= lsn);
(void) e;
struct synchro_request req = {
.type = IPROTO_RAFT_PROMOTE,
.replica_id = limbo->owner_id,
.origin_id = instance_id,
.lsn = lsn,
.term = term,
};
if (txn_limbo_req_prepare(limbo, &req) < 0)
return -1;
synchro_request_write(&req);
txn_limbo_req_commit(limbo, &req);
return 0;
}
/**
* Process a PROMOTE request, i.e. confirm all entries <= @a req.lsn and
* rollback all entries > @a req.lsn.
*/
static void
txn_limbo_read_promote(struct txn_limbo *limbo, uint32_t replica_id,
uint32_t prev_id, int64_t lsn)
{
txn_limbo_read_confirm(limbo, lsn);
txn_limbo_read_rollback(limbo, lsn + 1);
assert(txn_limbo_is_empty(limbo));
limbo->owner_id = replica_id;
box_update_ro_summary();
/*
* Only nullify confirmed_lsn when the new value is unknown. I.e. when
* prev_id != replica_id.
*/
if (replica_id != prev_id)
limbo->confirmed_lsn = 0;
}
int
txn_limbo_write_demote(struct txn_limbo *limbo, int64_t lsn, uint64_t term)
{
assert(latch_is_locked(&limbo->promote_latch));
struct txn_limbo_entry *e = txn_limbo_last_synchro_entry(limbo);
assert(e == NULL || e->lsn <= lsn);
(void)e;
struct synchro_request req = {
.type = IPROTO_RAFT_DEMOTE,
.replica_id = limbo->owner_id,
.origin_id = instance_id,
.lsn = lsn,
.term = term,
};
if (txn_limbo_req_prepare(limbo, &req) < 0)
return -1;
synchro_request_write(&req);
txn_limbo_req_commit(limbo, &req);
return 0;
}
/**
* Process a DEMOTE request, which's like PROMOTE, but clears the limbo
* ownership.
* @sa txn_limbo_read_promote.
*/
static void
txn_limbo_read_demote(struct txn_limbo *limbo, uint32_t prev_id, int64_t lsn)
{
return txn_limbo_read_promote(limbo, REPLICA_ID_NIL, prev_id, lsn);
}
void
txn_limbo_ack(struct txn_limbo *limbo, uint32_t replica_id, int64_t lsn)
{
if (rlist_empty(&limbo->queue))
return;
if (txn_limbo_is_frozen(limbo))
return;
assert(!txn_limbo_is_ro(limbo));
/*
* If limbo is currently writing a rollback, it means that the whole
* queue will be rolled back. Because rollback is written only for
* timeout. Timeout always happens first for the oldest entry, i.e.
* first entry in the queue. The rollback will clear all the newer
* entries. So in total the whole queue is dead already. Would be
* strange to write CONFIRM for rolled back LSNs. Even though
* probably it wouldn't break anything. Would be just 2 conflicting
* decisions for the same LSNs.
*/
if (limbo->is_in_rollback)
return;
assert(limbo->owner_id != REPLICA_ID_NIL);
int64_t prev_lsn = vclock_get(&limbo->vclock, replica_id);
/*
* One of the reasons why can happen - the remote instance is not
* read-only and wrote something under its own insance_id. For qsync
* that most likely means that the remote instance decided to take over
* the limbo ownership, and the current node is going to become a
* replica very soon.
*/
if (lsn == prev_lsn)
return;
vclock_follow(&limbo->vclock, replica_id, lsn);
struct txn_limbo_entry *e;
int64_t confirm_lsn = -1;
rlist_foreach_entry(e, &limbo->queue, in_queue) {
assert(e->ack_count <= VCLOCK_MAX);
if (e->lsn > lsn)
break;
/*
* Sync transactions need to collect acks. Async
* transactions are automatically committed right
* after all the previous sync transactions are.
*/
if (!txn_has_flag(e->txn, TXN_WAIT_ACK)) {
continue;
} else if (e->lsn <= prev_lsn) {
continue;
} else if (++e->ack_count < replication_synchro_quorum) {
continue;
} else {
confirm_lsn = e->lsn;
}
}
if (confirm_lsn == -1 || confirm_lsn <= limbo->confirmed_lsn)
return;
txn_limbo_write_confirm(limbo, confirm_lsn);
txn_limbo_read_confirm(limbo, confirm_lsn);
}
/**
* Waitpoint stores information about the progress of confirmation.
* In the case of multimaster support, it will store a bitset
* or array instead of the boolean.
*/
struct confirm_waitpoint {
/** Fiber that is waiting for the end of confirmation. */
struct fiber *caller;
/**
* Result flag.
*/
bool is_confirm;
bool is_rollback;
};
static int
txn_commit_cb(struct trigger *trigger, void *event)
{
(void)event;
struct confirm_waitpoint *cwp =
(struct confirm_waitpoint *)trigger->data;
cwp->is_confirm = true;
fiber_wakeup(cwp->caller);
return 0;
}
static int
txn_rollback_cb(struct trigger *trigger, void *event)
{
(void)event;
struct confirm_waitpoint *cwp =
(struct confirm_waitpoint *)trigger->data;
cwp->is_rollback = true;
fiber_wakeup(cwp->caller);
return 0;
}
int
txn_limbo_wait_last_txn(struct txn_limbo *limbo, bool *is_rollback,
double timeout)
{
assert(!txn_limbo_is_empty(limbo));
/* initialization of a waitpoint. */
struct confirm_waitpoint cwp;
cwp.caller = fiber();
cwp.is_confirm = false;
cwp.is_rollback = false;
/* Set triggers for the last limbo transaction. */
struct trigger on_complete;
trigger_create(&on_complete, txn_commit_cb, &cwp, NULL);
struct trigger on_rollback;
trigger_create(&on_rollback, txn_rollback_cb, &cwp, NULL);
struct txn_limbo_entry *tle = txn_limbo_last_entry(limbo);
txn_on_commit(tle->txn, &on_complete);
txn_on_rollback(tle->txn, &on_rollback);
double deadline = fiber_clock() + timeout;
int rc;
while (true) {
if (timeout < 0) {
rc = -1;
break;
}
rc = fiber_cond_wait_timeout(&limbo->wait_cond, timeout);
if (cwp.is_confirm || cwp.is_rollback) {
*is_rollback = cwp.is_rollback;
rc = 0;
break;
}
if (rc != 0)
break;
timeout = deadline - fiber_clock();
}
trigger_clear(&on_complete);
trigger_clear(&on_rollback);
return rc;
}
int
txn_limbo_wait_confirm(struct txn_limbo *limbo)
{
if (txn_limbo_is_empty(limbo))
return 0;
bool is_rollback;
if (txn_limbo_wait_last_txn(limbo, &is_rollback,
replication_synchro_timeout) != 0) {
diag_set(ClientError, ER_SYNC_QUORUM_TIMEOUT);
return -1;
}
if (is_rollback) {
/* The transaction has been rolled back. */
diag_set(ClientError, ER_SYNC_ROLLBACK);
return -1;
}
return 0;
}
int
txn_limbo_wait_empty(struct txn_limbo *limbo, double timeout)
{
if (txn_limbo_is_empty(limbo))
return 0;
bool is_rollback;
double deadline = fiber_clock() + timeout;
/*
* Retry in the loop. More transactions might be added while waiting for
* the last one.
*/
do {
if (txn_limbo_wait_last_txn(limbo, &is_rollback,
timeout) != 0) {
diag_set(ClientError, ER_TIMEOUT);
return -1;
}
timeout = deadline - fiber_clock();
} while (!txn_limbo_is_empty(limbo));
return 0;
}
static int
txn_write_cb(struct trigger *trigger, void *event)
{
(void)event;
struct fiber *fiber = (struct fiber *)trigger->data;
fiber_wakeup(fiber);
return 0;
}
/**
* Wait until all the limbo entries receive an lsn.
*/
static int
txn_limbo_wait_persisted(struct txn_limbo *limbo)
{
if (txn_limbo_is_empty(limbo))
return 0;
struct txn_limbo_entry *e = txn_limbo_last_entry(limbo);
while (e != NULL && e->lsn <= 0) {
struct trigger on_wal_write;
trigger_create(&on_wal_write, txn_write_cb, fiber(), NULL);
txn_on_wal_write(e->txn, &on_wal_write);
fiber_yield();
trigger_clear(&on_wal_write);
if (fiber_is_cancelled()) {
diag_set(FiberIsCancelled);
return -1;
}
e = txn_limbo_last_entry(limbo);
}
return 0;
}
/**
* Fill the reject reason with request data.
* The function is not reenterable, use with care.
*/
static const char *
reject_str(const struct synchro_request *req)
{
const char *type_name = iproto_type_name(req->type);
return tt_sprintf("RAFT: rejecting %s (%d) request from origin_id %u "
"replica_id %u term %llu",
type_name ? type_name : "UNKNOWN", req->type,
req->origin_id, req->replica_id,
(long long)req->term);
}
/**
* Common filter for any incoming packet.
*/
static int
txn_limbo_filter_generic(struct txn_limbo *limbo,
const struct synchro_request *req)
{
assert(latch_is_locked(&limbo->promote_latch));
if (!limbo->do_validate)
return 0;
/*
* Zero replica_id is allowed for PROMOTE packets only.
*/
if (req->replica_id == REPLICA_ID_NIL) {
if (req->type != IPROTO_RAFT_PROMOTE) {
say_error("%s. Zero replica_id detected",
reject_str(req));
diag_set(ClientError, ER_UNSUPPORTED, "Replication",
"synchronous requests with zero replica_id");
return -1;
}
}
if (req->replica_id != limbo->owner_id) {
/*
* Incoming packets should esteem limbo owner,
* if it doesn't match it means the sender
* missed limbo owner migrations and is out of date.
*/
say_error("%s. Limbo owner mismatch, owner_id %u",
reject_str(req), limbo->owner_id);
diag_set(ClientError, ER_SPLIT_BRAIN,
"got a request from a foreign synchro queue owner");
return -1;
}
return 0;
}
/**
* A common filter for all synchro requests, checking that request operates
* over a valid lsn range.
*/
static int
txn_limbo_filter_queue_boundaries(struct txn_limbo *limbo,
const struct synchro_request *req)
{
int64_t lsn = req->lsn;
/*
* Easy case - processed LSN matches the new one which comes inside
* request, everything is consistent. This is allowed only for
* PROMOTE/DEMOTE.
*/
if (limbo->confirmed_lsn == lsn) {
if (iproto_type_is_promote_request(req->type)) {
return 0;
} else {
say_error("%s. Duplicate request with confirmed lsn "
"%lld = request lsn %lld", reject_str(req),
(long long)limbo->confirmed_lsn,
(long long)lsn);
diag_set(ClientError, ER_UNSUPPORTED, "Replication",
"Duplicate CONFIRM/ROLLBACK request");
return -1;
}
}
/*
* Explicit split brain situation. Request comes in with an old LSN
* which we've already processed.
*/
if (limbo->confirmed_lsn > lsn) {
say_error("%s. confirmed lsn %lld > request lsn %lld",
reject_str(req), (long long)limbo->confirmed_lsn,
(long long)lsn);
diag_set(ClientError, ER_SPLIT_BRAIN,
"got a request with lsn from an already "
"processed range");
return -1;
}
/*
* The last case requires a few subcases.
*/
assert(limbo->confirmed_lsn < lsn);
if (txn_limbo_is_empty(limbo)) {
/*
* Transactions are rolled back already,
* since the limbo is empty.
*/
say_error("%s. confirmed lsn %lld < request lsn %lld "
"and empty limbo", reject_str(req),
(long long)limbo->confirmed_lsn,
(long long)lsn);
diag_set(ClientError, ER_SPLIT_BRAIN,
"got a request mentioning future lsn");
return -1;
} else {
/*
* Some entries are present in the limbo, we need to make sure
* that request lsn lays inside limbo [first; last] range.
* So that the request has some queued data to process,
* otherwise it means the request comes from split brained node.
*/
int64_t first_lsn = txn_limbo_first_entry(limbo)->lsn;
int64_t last_lsn = txn_limbo_last_synchro_entry(limbo)->lsn;
if (lsn < first_lsn || last_lsn < lsn) {
say_error("%s. request lsn %lld out of range "
"[%lld; %lld]", reject_str(req),
(long long)lsn,
(long long)first_lsn,
(long long)last_lsn);
diag_set(ClientError, ER_SPLIT_BRAIN,
"got a request lsn out of queue range");
return -1;
}
}
return 0;
}
/**
* Filter CONFIRM and ROLLBACK packets.
*/
static int
txn_limbo_filter_confirm_rollback(struct txn_limbo *limbo,
const struct synchro_request *req)
{
assert(latch_is_locked(&limbo->promote_latch));
assert(limbo->do_validate);
assert(req->type == IPROTO_RAFT_CONFIRM ||
req->type == IPROTO_RAFT_ROLLBACK);
/*
* Zero LSN is allowed for PROMOTE and DEMOTE requests only.
*/
if (req->lsn == 0) {
say_error("%s. Zero lsn detected", reject_str(req));
diag_set(ClientError, ER_UNSUPPORTED, "Replication",
"zero LSN for CONFIRM/ROLLBACK");
return -1;
}
return txn_limbo_filter_queue_boundaries(limbo, req);
}
/** A filter PROMOTE and DEMOTE packets. */
static int
txn_limbo_filter_promote_demote(struct txn_limbo *limbo,
const struct synchro_request *req)
{
assert(latch_is_locked(&limbo->promote_latch));
assert(limbo->do_validate);
assert(iproto_type_is_promote_request(req->type));