forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memtx_tx.c
3066 lines (2828 loc) · 91.4 KB
/
memtx_tx.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 "memtx_tx.h"
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include "schema_def.h"
#include "small/mempool.h"
enum {
/**
* Virtual PSN that will be set to del_psn of rolled-back story.
* Must be less that any existing "real" PSN.
*/
MEMTX_TX_ROLLBACKED_PSN = 1,
};
static_assert((int)MEMTX_TX_ROLLBACKED_PSN < (int)TXN_MIN_PSN,
"There must be a range for TX manager's internal use");
/**
* Link that connects a memtx_story with older and newer stories of the same
* key in index.
*/
struct memtx_story_link {
/** Story that was happened after that story was ended. */
struct memtx_story *newer_story;
/** Story that was happened before that story was started. */
struct memtx_story *older_story;
/** List of interval items @sa gap_item. */
struct rlist nearby_gaps;
/**
* If the tuple of story is physically in index, here the pointer
* to that index is stored.
*/
struct index *in_index;
};
/**
* A part of a history of a value in space.
* It's a story about a tuple, from the point it was added to space to the
* point when it was deleted from a space.
* All stories are linked into a list of stories of the same key of each index.
*/
struct memtx_story {
/** The story is about this tuple. The tuple is referenced. */
struct tuple *tuple;
/**
* Statement that introduced this story. Is set to NULL when the
* statement's transaction becomes committed. Can also be NULL if we
* don't know who introduced that story, the tuple was added by a
* transaction that was completed and destroyed some time ago.
*/
struct txn_stmt *add_stmt;
/**
* Prepare sequence number of add_stmt's transaction. Is set when
* the transaction is prepared. Can be 0 if the transaction is
* in progress or we don't know who introduced that story.
*/
int64_t add_psn;
/**
* Statement that ended this story. Is set to NULL when the statement's
* transaction becomes committed. Can also be NULL if the tuple has not
* been deleted yet.
*/
struct txn_stmt *del_stmt;
/**
* Prepare sequence number of del_stmt's transaction. Is set when
* the transaction is prepared. Can be 0 if the transaction is
* in progress or if nobody has deleted the tuple.
*/
int64_t del_psn;
/**
* List of trackers - transactions that has read this tuple.
*/
struct rlist reader_list;
/**
* Link in tx_manager::all_stories
*/
struct rlist in_all_stories;
/**
* Link in space::memtx_tx_stories.
*/
struct rlist in_space_stories;
/**
* Number of indexes in this space - and the count of link[].
*/
uint32_t index_count;
/**
* Status of story, describes the reason why story cannot be deleted.
* It is initialized in memtx_story constructor and is changed only in
* memtx_tx_story_gc.
*/
enum memtx_tx_story_status status;
/**
* Flag is set when @a tuple is not placed in primary key and
* the story is the only reason why @a tuple cannot be deleted.
*/
bool tuple_is_retained;
/**
* Link with older and newer stories (and just tuples) for each
* index respectively.
*/
struct memtx_story_link link[];
};
static uint32_t
memtx_tx_story_key_hash(const struct tuple *a)
{
uintptr_t u = (uintptr_t)a;
if (sizeof(uintptr_t) <= sizeof(uint32_t))
return u;
else
return u ^ (u >> 32);
}
#define mh_name _history
#define mh_key_t struct tuple *
#define mh_node_t struct memtx_story *
#define mh_arg_t int
#define mh_hash(a, arg) (memtx_tx_story_key_hash((*(a))->tuple))
#define mh_hash_key(a, arg) (memtx_tx_story_key_hash(a))
#define mh_cmp(a, b, arg) ((*(a))->tuple != (*(b))->tuple)
#define mh_cmp_key(a, b, arg) ((a) != (*(b))->tuple)
#define MH_SOURCE
#include "salad/mhash.h"
/**
* Record that links transaction and a story that the transaction have read.
*/
struct tx_read_tracker {
/** The TX that read story. */
struct txn *reader;
/** The story that was read by reader. */
struct memtx_story *story;
/** Link in story->reader_list. */
struct rlist in_reader_list;
/** Link in reader->read_set. */
struct rlist in_read_set;
/** Bit field of indexes in which the data was read by reader. */
uint64_t index_mask;
};
/**
* An element that stores the fact that some transaction have read
* a full key and found nothing.
*/
struct point_hole_item {
/** A link of headless list of items with the same index and key. */
struct rlist ring;
/** Link in txn->point_holes_list. */
struct rlist in_point_holes_list;
/** Saved index->unique_id. */
uint32_t index_unique_id;
/** Precalculated hash for storing in hash table.. */
uint32_t hash;
/** Saved txn. */
struct txn *txn;
/** Saved key. Points to @a short_key or allocated in txn's region. */
const char *key;
/** Saved key len. */
size_t key_len;
/** Storage for short key. @key may point here. */
char short_key[16];
/** Flag that the hash tables stores pointer to this item. */
bool is_head;
};
/**
* An element that stores the fact that some transaction have read
* a full key and found nothing.
*/
struct gap_item {
/** A link in memtx_story_link::nearby_gaps OR index::nearby_gaps. */
struct rlist in_nearby_gaps;
/** Link in txn->gap_list. */
struct rlist in_gap_list;
/** The transaction that read it. */
struct txn *txn;
/** The key. Can be NULL. */
const char *key;
uint32_t key_len;
uint32_t part_count;
/** Search mode. */
enum iterator_type type;
/** Storage for short key. @key may point here. */
char short_key[16];
};
/**
* An element that stores the fact that some transaction have read
* a full index.
*/
struct full_scan_item {
/** A link in index::full_scans. */
struct rlist in_full_scans;
/** Link in txn->full_scan_list. */
struct rlist in_full_scan_list;
/** The transaction that read it. */
struct txn *txn;
};
/**
* Helper structure for searching for point_hole_item in the hash table,
* @sa point_hole_item_pool.
*/
struct point_hole_key {
struct index *index;
struct tuple *tuple;
};
/** Hash calculatore for the key. */
static uint32_t
point_hole_storage_key_hash(struct point_hole_key *key)
{
struct key_def *def = key->index->def->key_def;
return key->index->unique_id ^ def->tuple_hash(key->tuple, def);
}
/** point_hole_item comparator. */
static int
point_hole_storage_equal(const struct point_hole_item *obj1,
const struct point_hole_item *obj2)
{
/* Canonical msgpack is comparable by memcmp. */
if (obj1->index_unique_id != obj2->index_unique_id ||
obj1->key_len != obj2->key_len)
return 1;
return memcmp(obj1->key, obj2->key, obj1->key_len) != 0;
}
/** point_hole_item comparator with key. */
static int
point_hole_storage_key_equal(const struct point_hole_key *key,
const struct point_hole_item *object)
{
if (key->index->unique_id != object->index_unique_id)
return 1;
assert(key->index != NULL);
assert(key->tuple != NULL);
struct key_def *def = key->index->def->key_def;
hint_t oh = def->key_hint(object->key, def->part_count, def);
hint_t kh = def->tuple_hint(key->tuple, def);
return def->tuple_compare_with_key(key->tuple, kh, object->key,
def->part_count, oh, def);
}
/**
* Hash table definition for hole read storage.
* The key is constructed by unique index ID and search key.
* Actually it stores pointers to point_hole_item structures.
* If more than one point_hole_item is added to the hash table,
* it is simply added to the headless list in existing point_hole_item.
*/
#define mh_name _point_holes
#define mh_key_t struct point_hole_key *
#define mh_node_t struct point_hole_item *
#define mh_arg_t int
#define mh_hash(a, arg) ((*(a))->hash)
#define mh_hash_key(a, arg) ( point_hole_storage_key_hash(a) )
#define mh_cmp(a, b, arg) point_hole_storage_equal(*(a), *(b))
#define mh_cmp_key(a, b, arg) point_hole_storage_key_equal((a), *(b))
#define MH_SOURCE
#include "salad/mhash.h"
/**
* Collect an allocation to memtx_tx_stats.
*/
static inline void
memtx_tx_stats_collect(struct memtx_tx_stats *stats, size_t size)
{
stats->count++;
stats->total += size;
}
/**
* Discard an allocation collected by memtx_tx_stats.
*/
static inline void
memtx_tx_stats_discard(struct memtx_tx_stats *stats, size_t size)
{
assert(stats->count > 0);
assert(stats->total >= size);
stats->count--;
stats->total -= size;
}
/**
* Collect allocation statistics.
*/
static inline void
memtx_tx_track_allocation(struct txn *txn, size_t size,
enum memtx_tx_alloc_type alloc_type)
{
assert(alloc_type < MEMTX_TX_ALLOC_TYPE_MAX);
txn->memtx_tx_alloc_stats[alloc_type] += size;
}
/**
* Collect deallocation statistics.
*/
static inline void
memtx_tx_track_deallocation(struct txn *txn, size_t size,
enum memtx_tx_alloc_type alloc_type)
{
assert(alloc_type < MEMTX_TX_ALLOC_TYPE_MAX);
assert(txn->memtx_tx_alloc_stats[alloc_type] >= size);
txn->memtx_tx_alloc_stats[alloc_type] -= size;
}
/**
* A wrapper over mempool.
* Use it instead of mempool to track allocations!
*/
struct memtx_tx_mempool {
/**
* Wrapped mempool.
*/
struct mempool pool;
/**
* Each allocation is accounted with this type.
*/
enum memtx_tx_alloc_type alloc_type;
};
static inline void
memtx_tx_mempool_create(struct memtx_tx_mempool *mempool, uint32_t objsize,
enum memtx_tx_alloc_type alloc_type)
{
mempool_create(&mempool->pool, cord_slab_cache(), objsize);
mempool->alloc_type = alloc_type;
}
static inline void
memtx_tx_mempool_destroy(struct memtx_tx_mempool *mempool)
{
mempool_destroy(&mempool->pool);
mempool->alloc_type = MEMTX_TX_ALLOC_TYPE_MAX;
}
/**
* Allocate an object on given @a mempool and account allocated size in
* statistics of transaction @a txn.
*/
static void *
memtx_tx_xmempool_alloc(struct txn *txn, struct memtx_tx_mempool *mempool)
{
void *allocation = xmempool_alloc(&mempool->pool);
uint32_t size = mempool->pool.objsize;
memtx_tx_track_allocation(txn, size, mempool->alloc_type);
return allocation;
}
static void
memtx_tx_mempool_free(struct txn *txn, struct memtx_tx_mempool *mempool, void *ptr)
{
uint32_t size = mempool->pool.objsize;
memtx_tx_track_deallocation(txn, size, mempool->alloc_type);
mempool_free(&mempool->pool, ptr);
}
/**
* Choose memtx_tx_alloc_type for alloc_obj.
*/
static inline enum memtx_tx_alloc_type
memtx_tx_region_object_to_type(enum memtx_tx_alloc_object alloc_obj)
{
enum memtx_tx_alloc_type alloc_type = MEMTX_TX_ALLOC_TYPE_MAX;
switch (alloc_obj) {
case MEMTX_TX_OBJECT_CONFLICT_TRACKER:
case MEMTX_TX_OBJECT_READ_TRACKER:
alloc_type = MEMTX_TX_ALLOC_TRACKER;
break;
default:
unreachable();
};
assert(alloc_type < MEMTX_TX_ALLOC_TYPE_MAX);
return alloc_type;
}
/**
* Alloc object on region. Pass object as enum memtx_tx_alloc_object.
* Use this method to track txn's allocations!
*/
static inline void *
memtx_tx_xregion_alloc_object(struct txn *txn,
enum memtx_tx_alloc_object alloc_obj)
{
size_t size = 0;
void *alloc = NULL;
enum memtx_tx_alloc_type alloc_type =
memtx_tx_region_object_to_type(alloc_obj);
switch (alloc_obj) {
case MEMTX_TX_OBJECT_READ_TRACKER:
alloc = xregion_alloc_object(&txn->region,
struct tx_read_tracker);
size = sizeof(struct tx_read_tracker);
break;
default:
unreachable();
}
assert(alloc_type < MEMTX_TX_ALLOC_TYPE_MAX);
memtx_tx_track_allocation(txn, size, alloc_type);
return alloc;
}
/**
* Tx_region method for allocations of arbitrary size.
* You must pass allocation type explicitly to categorize an allocation.
* Use this method to track allocations!
*/
static inline void *
memtx_tx_xregion_alloc(struct txn *txn, size_t size,
enum memtx_tx_alloc_type alloc_type)
{
void *allocation = xregion_alloc(&txn->region, size);
if (allocation != NULL)
memtx_tx_track_allocation(txn, size, alloc_type);
return allocation;
}
/** String representation of enum memtx_tx_alloc_type. */
const char *memtx_tx_alloc_type_strs[MEMTX_TX_ALLOC_TYPE_MAX] = {
"trackers",
"conflicts",
};
/** String representation of enum memtx_tx_story_status. */
const char *memtx_tx_story_status_strs[MEMTX_TX_STORY_STATUS_MAX] = {
"used",
"read_view",
"tracking",
};
struct tx_manager
{
/**
* List of all transactions that are in a read view.
* New transactions are added to the tail of this list,
* so the list is ordered by rv_psn.
*/
struct rlist read_view_txs;
/**
* Mempools for tx_story objects with different index count.
* It's the only case when we use bare mempool in memtx_tx because
* we cannot account story allocation to any particular txn.
*/
struct mempool memtx_tx_story_pool[BOX_INDEX_MAX];
/** Hash table tuple -> memtx_story of that tuple. */
struct mh_history_t *history;
/** Mempool for point_hole_item objects. */
struct memtx_tx_mempool point_hole_item_pool;
/** Hash table that hold point selects with empty result. */
struct mh_point_holes_t *point_holes;
/** Mempool for gap_item objects. */
struct memtx_tx_mempool gap_item_mempoool;
/** Mempool for full_scan_item objects. */
struct memtx_tx_mempool full_scan_item_mempool;
/** List of all memtx_story objects. */
struct rlist all_stories;
struct memtx_tx_stats story_stats[MEMTX_TX_STORY_STATUS_MAX];
struct memtx_tx_stats retained_tuple_stats[MEMTX_TX_STORY_STATUS_MAX];
/** Iterator that sequentially traverses all memtx_story objects. */
struct rlist *traverse_all_stories;
/** The list containing all transactions. */
struct rlist all_txs;
/** Accumulated number of GC steps that should be done. */
size_t must_do_gc_steps;
};
enum {
/**
* Number of iterations that is allowed for TX manager to do for
* searching and deleting no more used memtx_tx_stories per creation of
* a new story.
*/
TX_MANAGER_GC_STEPS_SIZE = 2,
};
/** That's a definition, see declaration for description. */
bool memtx_tx_manager_use_mvcc_engine = false;
/** The one and only instance of tx_manager. */
static struct tx_manager txm;
void
memtx_tx_manager_init()
{
rlist_create(&txm.read_view_txs);
for (size_t i = 0; i < BOX_INDEX_MAX; i++) {
size_t item_size = sizeof(struct memtx_story) +
i * sizeof(struct memtx_story_link);
mempool_create(&txm.memtx_tx_story_pool[i],
cord_slab_cache(), item_size);
}
txm.history = mh_history_new();
memtx_tx_mempool_create(&txm.point_hole_item_pool,
sizeof(struct point_hole_item),
MEMTX_TX_ALLOC_TRACKER);
txm.point_holes = mh_point_holes_new();
memtx_tx_mempool_create(&txm.gap_item_mempoool,
sizeof(struct gap_item),
MEMTX_TX_ALLOC_TRACKER);
memtx_tx_mempool_create(&txm.full_scan_item_mempool,
sizeof(struct full_scan_item),
MEMTX_TX_ALLOC_TRACKER);
rlist_create(&txm.all_stories);
rlist_create(&txm.all_txs);
txm.traverse_all_stories = &txm.all_stories;
txm.must_do_gc_steps = 0;
memset(&txm.story_stats, 0, sizeof(txm.story_stats));
}
void
memtx_tx_manager_free()
{
for (size_t i = 0; i < BOX_INDEX_MAX; i++)
mempool_destroy(&txm.memtx_tx_story_pool[i]);
mh_history_delete(txm.history);
memtx_tx_mempool_destroy(&txm.point_hole_item_pool);
mh_point_holes_delete(txm.point_holes);
memtx_tx_mempool_destroy(&txm.gap_item_mempoool);
memtx_tx_mempool_destroy(&txm.full_scan_item_mempool);
}
void
memtx_tx_statistics_collect(struct memtx_tx_statistics *stats)
{
memset(stats, 0, sizeof(*stats));
for (size_t i = 0; i < MEMTX_TX_STORY_STATUS_MAX; ++i) {
stats->stories[i] = txm.story_stats[i];
stats->retained_tuples[i] = txm.retained_tuple_stats[i];
}
if (rlist_empty(&txm.all_txs)) {
return;
}
struct txn *txn;
size_t txn_count = 0;
rlist_foreach_entry(txn, &txm.all_txs, in_all_txs) {
txn_count++;
for (size_t i = 0; i < MEMTX_TX_ALLOC_TYPE_MAX; ++i) {
size_t txn_stat = txn->memtx_tx_alloc_stats[i];
stats->memtx_tx_total[i] += txn_stat;
if (txn_stat > stats->memtx_tx_max[i])
stats->memtx_tx_max[i] = txn_stat;
}
for (size_t i = 0; i < TX_ALLOC_TYPE_MAX; ++i) {
size_t txn_stat = txn->alloc_stats[i];
stats->tx_total[i] += txn_stat;
if (txn_stat > stats->tx_max[i])
stats->tx_max[i] = txn_stat;
}
}
stats->txn_count = txn_count;
}
void
memtx_tx_register_txn(struct txn *tx)
{
tx->memtx_tx_alloc_stats =
xregion_alloc_array(&tx->region,
typeof(*tx->memtx_tx_alloc_stats),
MEMTX_TX_ALLOC_TYPE_MAX);
memset(tx->memtx_tx_alloc_stats, 0,
sizeof(*tx->memtx_tx_alloc_stats) * MEMTX_TX_ALLOC_TYPE_MAX);
rlist_add_tail(&txm.all_txs, &tx->in_all_txs);
}
void
memtx_tx_acquire_ddl(struct txn *tx)
{
tx->is_schema_changed = true;
(void) txn_can_yield(tx, false);
}
void
memtx_tx_abort_all_for_ddl(struct txn *ddl_owner)
{
struct txn *to_be_aborted;
rlist_foreach_entry(to_be_aborted, &txm.all_txs, in_all_txs) {
if (to_be_aborted == ddl_owner)
continue;
if (to_be_aborted->status != TXN_INPROGRESS &&
to_be_aborted->status != TXN_IN_READ_VIEW)
continue;
to_be_aborted->status = TXN_ABORTED;
txn_set_flags(to_be_aborted, TXN_IS_CONFLICTED);
say_warn("Transaction committing DDL (id=%lld) has aborted "
"another TX (id=%lld)", (long long) ddl_owner->id,
(long long) to_be_aborted->id);
}
}
/**
* Fix position of @a txn in global read view list to preserve the list to
* be ordered by rv_psn. Can only move txn to the beginning of the list.
* The function must be called when a transaction A sends itself to a read view
* (perhaps a deeper read view in case when it's already in a read view) because
* it has to skip a statement of another B, prepared transaction.
* The transaction is always added to the tail of read view list, but in this
* case there's no guarantee that psn of B is the greatest psn of all prepared
* transactions, so we have to additionally and push A in the global read view
* list, jumping over read views with greater rv_psn.
*/
static void
memtx_tx_adjust_position_in_read_view_list(struct txn *txn)
{
if (txn->in_read_view_txs.prev == &txm.read_view_txs)
return; /* No transaction before */
struct txn *prev_txn = rlist_prev_entry(txn, in_read_view_txs);
if (prev_txn->rv_psn <= txn->rv_psn)
return; /* The order is already correct. */
/* Remove from list for a while. */
rlist_del(&txn->in_read_view_txs);
while (prev_txn->in_read_view_txs.prev != &txm.read_view_txs) {
struct txn *scan = rlist_prev_entry(prev_txn, in_read_view_txs);
if (scan->rv_psn <= txn->rv_psn)
break;
prev_txn = scan;
}
/* Insert before prev_txn. */
rlist_add_tail(&prev_txn->in_read_view_txs, &txn->in_read_view_txs);
}
/**
* Handle conflict when @a victim has read and @a breaker has written the same
* key, and @a breaker is prepared. The functions must be called in two cases:
* 1. @a breaker becomes prepared for every victim with non-empty intersection
* of victim read set / breaker write set.
* 2. @a victim has to read confirmed value and skips the value that prepared
* @a breaker wrote.
* If @a victim is read-only or hasn't made any changes, it should be sent
* to read view, in which is will not see @a breaker's changes. If @a victim
* is already in a read view - a read view that does not see every breaker
* changes is chosen.
* Otherwise @a victim must be marked as conflicted and aborted on occasion.
*
* NB: can trigger story garbage collection.
*/
static void
memtx_tx_handle_conflict(struct txn *breaker, struct txn *victim)
{
assert(breaker != victim);
assert(breaker->psn != 0);
assert(victim->psn == 0);
if (victim->status != TXN_INPROGRESS &&
victim->status != TXN_IN_READ_VIEW) {
/* Was conflicted by somebody else. */
return;
}
if (stailq_empty(&victim->stmts)) {
assert((victim->status == TXN_IN_READ_VIEW) ==
(victim->rv_psn != 0));
/* Send to read view, perhaps a deeper one. */
if (victim->status != TXN_IN_READ_VIEW) {
victim->status = TXN_IN_READ_VIEW;
victim->rv_psn = breaker->psn;
rlist_add_tail(&txm.read_view_txs,
&victim->in_read_view_txs);
} else if (victim->rv_psn > breaker->psn) {
/*
* Note that in every case for every key we may choose
* any read view psn between confirmed level and the
* oldest prepared transaction that changes that key.
* But we choose the latest level because it generally
* costs less, and if there are several breakers - we
* must sequentially decrease read view level.
*/
victim->rv_psn = breaker->psn;
assert(victim->rv_psn != 0);
}
memtx_tx_adjust_position_in_read_view_list(victim);
} else {
/* Mark as conflicted. */
if (victim->status == TXN_IN_READ_VIEW)
rlist_del(&victim->in_read_view_txs);
victim->status = TXN_ABORTED;
txn_set_flags(victim, TXN_IS_CONFLICTED);
}
}
/**
* Calculate size of story with its links.
*/
static inline size_t
memtx_story_size(struct memtx_story *story)
{
struct mempool *pool = &txm.memtx_tx_story_pool[story->index_count];
return pool->objsize;
}
/**
* Notify memory manager that a tuple referenced by @a story
* was replaced from primary key and that is why @a story
* is the only reason why the tuple cannot be deleted.
*/
static inline void
memtx_tx_story_track_retained_tuple(struct memtx_story *story)
{
assert(!story->tuple_is_retained);
assert(story->status < MEMTX_TX_STORY_STATUS_MAX);
story->tuple_is_retained = true;
struct memtx_tx_stats *stats = &txm.retained_tuple_stats[story->status];
size_t tuplesize = tuple_size(story->tuple);
memtx_tx_stats_collect(stats, tuplesize);
}
/**
* Notify memory manager that a tuple referenced by @a story
* was placed to primary key.
*/
static inline void
memtx_tx_story_untrack_retained_tuple(struct memtx_story *story)
{
assert(story->tuple_is_retained);
assert(story->status < MEMTX_TX_STORY_STATUS_MAX);
story->tuple_is_retained = false;
struct memtx_tx_stats *stats = &txm.retained_tuple_stats[story->status];
size_t tuplesize = tuple_size(story->tuple);
memtx_tx_stats_discard(stats, tuplesize);
}
/** Set status of story (see memtx_tx_story_status) */
static inline void
memtx_tx_story_set_status(struct memtx_story *story,
enum memtx_tx_story_status new_status)
{
assert(story->status < MEMTX_TX_STORY_STATUS_MAX);
enum memtx_tx_story_status old_status = story->status;
if (old_status == new_status)
return;
story->status = new_status;
struct memtx_tx_stats *old_story_stats = &txm.story_stats[old_status];
struct memtx_tx_stats *new_story_stats = &txm.story_stats[new_status];
size_t story_size = memtx_story_size(story);
memtx_tx_stats_discard(old_story_stats, story_size);
memtx_tx_stats_collect(new_story_stats, story_size);
if (story->tuple_is_retained) {
size_t tuplesize = tuple_size(story->tuple);
struct memtx_tx_stats *old =
&txm.retained_tuple_stats[old_status];
struct memtx_tx_stats *new =
&txm.retained_tuple_stats[new_status];
memtx_tx_stats_discard(old, tuplesize);
memtx_tx_stats_collect(new, tuplesize);
}
}
/**
* Use this method to ref tuple that belongs to @a story
* by primary index. Do not use bare tuple_ref!!!
*/
static inline void
memtx_tx_ref_to_primary(struct memtx_story *story)
{
assert(story != NULL);
tuple_ref(story->tuple);
if (story->tuple_is_retained)
memtx_tx_story_untrack_retained_tuple(story);
}
/**
* Use this method to unref tuple that belongs to @a story
* from primary index. Do not use bare tuple_unref!!!
*/
static inline void
memtx_tx_unref_from_primary(struct memtx_story *story)
{
assert(story != NULL);
tuple_unref(story->tuple);
if (!story->tuple_is_retained)
memtx_tx_story_track_retained_tuple(story);
}
/**
* Create a new story and link it with the @a tuple.
* There are two known scenarios of using this function:
* * The story is created for a clean tuple that is in space (and thus in
* space indexes) now. Such a story is a top of degenerate chains that
* consist of this story only.
* * The story is created for a new tuple that is to be inserted into space.
* Such a story will become the top of chains, and a special function
* memtx_tx_story_link_top must be called for that.
* In any case this story is expected to be a top of chains, so we set
* in_index members in story links to appropriate values.
*/
static struct memtx_story *
memtx_tx_story_new(struct space *space, struct tuple *tuple)
{
txm.must_do_gc_steps += TX_MANAGER_GC_STEPS_SIZE;
assert(!tuple_has_flag(tuple, TUPLE_IS_DIRTY));
uint32_t index_count = space->index_count;
assert(index_count < BOX_INDEX_MAX);
struct mempool *pool = &txm.memtx_tx_story_pool[index_count];
struct memtx_story *story = (struct memtx_story *)xmempool_alloc(pool);
story->tuple = tuple;
const struct memtx_story **put_story =
(const struct memtx_story **) &story;
struct memtx_story *replaced = NULL;
struct memtx_story **preplaced = &replaced;
mh_history_put(txm.history, put_story, &preplaced, 0);
assert(preplaced == NULL);
tuple_set_flag(tuple, TUPLE_IS_DIRTY);
tuple_ref(tuple);
story->status = MEMTX_TX_STORY_USED;
struct memtx_tx_stats *stats = &txm.story_stats[story->status];
memtx_tx_stats_collect(stats, pool->objsize);
story->tuple_is_retained = false;
story->index_count = index_count;
story->add_stmt = NULL;
story->add_psn = 0;
story->del_stmt = NULL;
story->del_psn = 0;
rlist_create(&story->reader_list);
rlist_add_tail(&txm.all_stories, &story->in_all_stories);
rlist_add(&space->memtx_stories, &story->in_space_stories);
for (uint32_t i = 0; i < index_count; i++) {
story->link[i].newer_story = story->link[i].older_story = NULL;
rlist_create(&story->link[i].nearby_gaps);
story->link[i].in_index = space->index[i];
}
return story;
}
static void
memtx_tx_story_delete(struct memtx_story *story)
{
memtx_tx_stats_discard(&txm.story_stats[story->status],
memtx_story_size(story));
if (story->tuple_is_retained)
memtx_tx_story_untrack_retained_tuple(story);
if (story->add_stmt != NULL) {
assert(story->add_stmt->add_story == story);
story->add_stmt->add_story = NULL;
story->add_stmt = NULL;
}
while (story->del_stmt != NULL) {
assert(story->del_stmt->del_story == story);
story->del_stmt->del_story = NULL;
struct txn_stmt *next = story->del_stmt->next_in_del_list;
story->del_stmt->next_in_del_list = NULL;
story->del_stmt = next;
}
if (txm.traverse_all_stories == &story->in_all_stories)
txm.traverse_all_stories = rlist_next(txm.traverse_all_stories);
rlist_del(&story->in_all_stories);
rlist_del(&story->in_space_stories);
mh_int_t pos = mh_history_find(txm.history, story->tuple, 0);
assert(pos != mh_end(txm.history));
mh_history_del(txm.history, pos, 0);
tuple_clear_flag(story->tuple, TUPLE_IS_DIRTY);
tuple_unref(story->tuple);
#ifndef NDEBUG
/* Expecting to delete fully unlinked story. */
for (uint32_t i = 0; i < story->index_count; i++) {
assert(story->link[i].newer_story == NULL);
assert(story->link[i].older_story == NULL);
}
#endif
struct mempool *pool = &txm.memtx_tx_story_pool[story->index_count];
mempool_free(pool, story);
}
/**
* Find a story of a @a tuple. The story expected to be present (assert).
*/
static struct memtx_story *
memtx_tx_story_get(struct tuple *tuple)
{
assert(tuple_has_flag(tuple, TUPLE_IS_DIRTY));
mh_int_t pos = mh_history_find(txm.history, tuple, 0);
assert(pos != mh_end(txm.history));
struct memtx_story *story = *mh_history_node(txm.history, pos);
if (story->add_stmt != NULL)
assert(story->add_psn == story->add_stmt->txn->psn);
if (story->del_stmt != NULL)
assert(story->del_psn == story->del_stmt->txn->psn);
return story;
}
/**
* Link that @a story was added by @a stmt.
*/
static void
memtx_tx_story_link_added_by(struct memtx_story *story,
struct txn_stmt *stmt)
{
assert(story->add_stmt == NULL);
assert(stmt->add_story == NULL);
story->add_stmt = stmt;
stmt->add_story = story;
}
/**
* Unlink that @a story from @a stmt which added it.
* Effectively undo memtx_tx_story_link_added_by.
*/
static void
memtx_tx_story_unlink_added_by(struct memtx_story *story,
struct txn_stmt *stmt)
{
assert(stmt->add_story == story);
assert(story->add_stmt == stmt);
stmt->add_story = NULL;
story->add_stmt = NULL;
}
/**
* Link that @a story was deleted by @a stmt.
*/
static void
memtx_tx_story_link_deleted_by(struct memtx_story *story,
struct txn_stmt *stmt)
{
assert(stmt->del_story == NULL);
assert(stmt->next_in_del_list == NULL);
stmt->del_story = story;
stmt->next_in_del_list = story->del_stmt;
story->del_stmt = stmt;
}
/**
* Unlink that @a story from @a stmt that deleted it.
* Effectively undo memtx_tx_story_link_deleted_by.
*/
static void
memtx_tx_story_unlink_deleted_by(struct memtx_story *story,
struct txn_stmt *stmt)
{
assert(stmt->del_story == story);
/* Find a place in list from which stmt must be deleted. */
struct txn_stmt **ptr = &story->del_stmt;
while (*ptr != stmt) {
ptr = &(*ptr)->next_in_del_list;
assert(ptr != NULL);
}
*ptr = stmt->next_in_del_list;
stmt->next_in_del_list = NULL;
stmt->del_story = NULL;
}
/**
* Link a @a story with @a old_story in @a index (in both directions).
* @a old_story is allowed to be NULL.
*/
static void
memtx_tx_story_link(struct memtx_story *story,
struct memtx_story *old_story,
uint32_t idx)
{
assert(idx < story->index_count);
struct memtx_story_link *link = &story->link[idx];
assert(link->older_story == NULL);
if (old_story == NULL)