forked from sfu-dis/corobase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corobase.cc
1632 lines (1454 loc) · 53.6 KB
/
corobase.cc
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
#include "dbcore/rcu.h"
#include "dbcore/sm-chkpt.h"
#include "dbcore/sm-cmd-log.h"
#include "dbcore/sm-rep.h"
#include "ermia.h"
#include "txn.h"
#include "masstree/masstree_scan.hh"
namespace ermia {
#ifdef ADV_COROUTINE
void ConcurrentMasstreeIndex::adv_coro_MultiGet(
transaction *t, std::vector<varstr *> &keys, std::vector<varstr *> &values,
std::vector<ermia::coro::task<bool>> &index_probe_tasks,
std::vector<ermia::coro::task<void>> &get_record_tasks) {
if (!t) {
ermia::epoch_num e = MM::epoch_enter();
ConcurrentMasstree::versioned_node_t sinfo;
std::vector<OID> oids(keys.size());
for (int i = 0; i < keys.size(); ++i) {
oids[i] = INVALID_OID;
index_probe_tasks[i] = masstree_.search(*keys[i], oids[i], e, &sinfo);
index_probe_tasks[i].start();
}
int finished = 0;
while (finished < keys.size()) {
for (auto &t : index_probe_tasks) {
if (t.valid()) {
if (t.done()) {
++finished;
t.destroy();
} else {
t.resume();
}
}
}
}
for (const OID & oid : oids) {
ALWAYS_ASSERT(oid != INVALID_OID);
}
MM::epoch_exit(0, e);
} else {
std::vector<rc_t> rcs(keys.size());
for (int i = 0; i < keys.size(); ++i) {
rcs[i]._val = RC_INVALID;
get_record_tasks[i] = GetRecord(t, rcs[i], *keys[i], *values[i]);
get_record_tasks[i].start();
}
int finished = 0;
while (finished < keys.size()) {
for (auto &task : get_record_tasks) {
if (task.valid()) {
if (task.done()) {
++finished;
task.destroy();
} else {
task.resume();
}
}
}
}
for (const rc_t & rc : rcs) {
ALWAYS_ASSERT(rc._val == RC_TRUE);
}
}
}
#else
void ConcurrentMasstreeIndex::amac_MultiGet(
transaction *t, std::vector<ConcurrentMasstree::AMACState> &requests,
std::vector<varstr *> &values) {
ConcurrentMasstree::versioned_node_t sinfo;
if (!t) {
auto e = MM::epoch_enter();
masstree_.search_amac(requests, e);
MM::epoch_exit(0, e);
} else {
t->ensure_active();
masstree_.search_amac(requests, t->xc->begin_epoch);
if (config::is_backup_srv()) {
for (uint32_t i = 0; i < requests.size(); ++i) {
auto &r = requests[i];
if (r.out_oid != INVALID_OID) {
// Key-OID mapping exists, now try to get the actual tuple to be sure
auto *tuple = oidmgr->BackupGetVersion(
table_descriptor->GetTupleArray(),
table_descriptor->GetPersistentAddressArray(), r.out_oid, t->xc);
if (tuple) {
t->DoTupleRead(tuple, values[i]);
} else if (config::phantom_prot) {
DoNodeRead(t, sinfo.first, sinfo.second);
}
}
}
} else if (!config::index_probe_only) {
if (config::amac_version_chain) {
// AMAC style version chain traversal
thread_local std::vector<OIDAMACState> version_requests;
version_requests.clear();
for (auto &s : requests) {
version_requests.emplace_back(s.out_oid);
}
oidmgr->oid_get_version_amac(table_descriptor->GetTupleArray(),
version_requests, t->xc);
uint32_t i = 0;
for (auto &vr : version_requests) {
if (vr.tuple) {
t->DoTupleRead(vr.tuple, values[i++]);
} else if (config::phantom_prot) {
DoNodeRead(t, sinfo.first, sinfo.second);
}
}
} else {
for (uint32_t i = 0; i < requests.size(); ++i) {
auto &r = requests[i];
if (r.out_oid != INVALID_OID) {
auto *tuple = oidmgr->oid_get_version(table_descriptor->GetTupleArray(),
r.out_oid, t->xc);
if (tuple) {
t->DoTupleRead(tuple, values[i]);
} else if (config::phantom_prot) {
DoNodeRead(t, sinfo.first, sinfo.second);
}
}
}
}
}
}
}
void ConcurrentMasstreeIndex::simple_coro_MultiGet(
transaction *t, std::vector<varstr *> &keys, std::vector<varstr *> &values,
std::vector<std::experimental::coroutine_handle<>> &handles) {
ermia::epoch_num e;
if (!t) {
e = MM::epoch_enter();
ConcurrentMasstree::threadinfo ti(e);
ConcurrentMasstree::versioned_node_t sinfo;
OID oid = INVALID_OID;
for (int i = 0; i < keys.size(); ++i) {
handles[i] = masstree_.search_coro(*keys[i], oid, ti, &sinfo).get_handle();
}
} else {
for (int i = 0; i < keys.size(); ++i) {
handles[i] = coro_GetRecord(t, *keys[i], *values[i]).get_handle();
}
}
int finished = 0;
while (finished < handles.size()) {
for (auto &h : handles) {
if (h) {
if (h.done()) {
++finished;
h.destroy();
h = nullptr;
} else {
h.resume();
}
}
}
}
if (!t)
MM::epoch_exit(0, e);
}
ermia::coro::generator<rc_t> ConcurrentMasstreeIndex::coro_GetRecordSV(transaction *t, const varstr &key,
varstr &value, OID *out_oid) {
OID oid = INVALID_OID;
rc_t rc = rc_t{RC_INVALID};
t->ensure_active();
// start: masstree search
ConcurrentMasstree::threadinfo ti(t->xc->begin_epoch);
ConcurrentMasstree::unlocked_tcursor_type lp(*masstree_.get_table(), key.data(), key.size());
// start: find_unlocked
int match;
key_indexed_position kx;
ConcurrentMasstree::node_base_type *root = const_cast<ConcurrentMasstree::node_base_type *>(lp.root_);
retry:
// start: reach_leaf
const ConcurrentMasstree::node_base_type* n[2];
ConcurrentMasstree::nodeversion_type v[2];
bool sense;
// Get a non-stale root.
// Detect staleness by checking whether n has ever split.
// The true root has never split.
sense = false;
n[sense] = root;
while (1) {
v[sense] = n[sense]->stable_annotated(ti.stable_fence());
if (!v[sense].has_split()) break;
n[sense] = n[sense]->unsplit_ancestor();
}
// Loop over internal nodes.
while (!v[sense].isleaf()) {
const ConcurrentMasstree::internode_type* in = static_cast<const ConcurrentMasstree::internode_type*>(n[sense]);
in->prefetch();
co_await std::experimental::suspend_always{};
int kp = ConcurrentMasstree::internode_type::bound_type::upper(lp.ka_, *in);
n[!sense] = in->child_[kp];
if (!n[!sense]) goto retry;
v[!sense] = n[!sense]->stable_annotated(ti.stable_fence());
if (likely(!in->has_changed(v[sense]))) {
sense = !sense;
continue;
}
ConcurrentMasstree::nodeversion_type oldv = v[sense];
v[sense] = in->stable_annotated(ti.stable_fence());
if (oldv.has_split(v[sense]) &&
in->stable_last_key_compare(lp.ka_, v[sense], ti) > 0) {
goto retry;
}
}
lp.v_ = v[sense];
lp.n_ = const_cast<ConcurrentMasstree::leaf_type *>(static_cast<const ConcurrentMasstree::leaf_type *>(n[sense]));
// end: reach_leaf
forward:
if (lp.v_.deleted()) goto retry;
//lp.n_->prefetch();
//co_await std::experimental::suspend_always{};
lp.perm_ = lp.n_->permutation();
kx = ConcurrentMasstree::leaf_type::bound_type::lower(lp.ka_, lp);
if (kx.p >= 0) {
lp.lv_ = lp.n_->lv_[kx.p];
lp.lv_.prefetch(lp.n_->keylenx_[kx.p]);
co_await std::experimental::suspend_always{};
match = lp.n_->ksuf_matches(kx.p, lp.ka_);
} else
match = 0;
if (lp.n_->has_changed(lp.v_)) {
lp.n_ = lp.n_->advance_to_key(lp.ka_, lp.v_, ti);
goto forward;
}
if (match < 0) {
lp.ka_.shift_by(-match);
root = lp.lv_.layer();
goto retry;
}
// end: find_unlocked
bool found = match;
dbtuple *tuple = nullptr;
if (found) {
oid = lp.value();
// end: masstree search
// start: oid_get_version
oid_array *oa = table_descriptor->GetTupleArray();
TXN::xid_context *visitor_xc = t->xc;
fat_ptr *entry = oa->get(oid);
start_over:
::prefetch((const char*)entry);
co_await std::experimental::suspend_always{};
fat_ptr ptr = volatile_read(*entry);
ASSERT(ptr.asi_type() == 0);
Object *prev_obj = nullptr;
while (ptr.offset()) {
Object *cur_obj = nullptr;
// Must read next_ before reading cur_obj->_clsn:
// the version we're currently reading (ie cur_obj) might be unlinked
// and thus recycled by the memory allocator at any time if it's not
// a committed version. If so, cur_obj->_next will be pointing to some
// other object in the allocator's free object pool - we'll probably
// end up at la-la land if we followed this _next pointer value...
// Here we employ some flavor of OCC to solve this problem:
// the aborting transaction that will unlink cur_obj will update
// cur_obj->_clsn to NULL_PTR, then deallocate(). Before reading
// cur_obj->_clsn, we (as the visitor), first dereference pp to get
// a stable value that "should" contain the right address of the next
// version. We then read cur_obj->_clsn to verify: if it's NULL_PTR
// that means we might have read a wrong _next value that's actually
// pointing to some irrelevant object in the allocator's memory pool,
// hence must start over from the beginning of the version chain.
fat_ptr tentative_next = NULL_PTR;
// If this is a backup server, then must see persistent_next to find out
// the **real** overwritten version.
ASSERT(ptr.asi_type() == 0);
cur_obj = (Object *)ptr.offset();
Object::PrefetchHeader(cur_obj);
co_await std::experimental::suspend_always{};
tentative_next = cur_obj->GetNextVolatile();
ASSERT(tentative_next.asi_type() == 0);
//bool retry = false;
//bool visible = oidmgr->TestVisibility(cur_obj, visitor_xc, retry);
// TestVisibility
{
fat_ptr clsn = cur_obj->GetClsn();
if (clsn == NULL_PTR) {
ASSERT(!config::is_backup_srv() || (config::command_log && config::replay_threads));
// dead tuple that was (or about to be) unlinked, start over
goto start_over;
}
uint16_t asi_type = clsn.asi_type();
ALWAYS_ASSERT(asi_type == fat_ptr::ASI_XID || asi_type == fat_ptr::ASI_LOG);
if (asi_type == fat_ptr::ASI_XID) { // in-flight
XID holder_xid = XID::from_ptr(clsn);
// Dirty data made by me is visible!
if (holder_xid == t->xc->owner) {
ASSERT(!cur_obj->GetNextVolatile().offset() ||
((Object *)cur_obj->GetNextVolatile().offset())
->GetClsn()
.asi_type() == fat_ptr::ASI_LOG);
ASSERT(!config::is_backup_srv() || (config::command_log && config::replay_threads));
goto handle_visible;
}
auto *holder = TXN::xid_get_context(holder_xid);
if (!holder) {
goto start_over;
}
auto state = volatile_read(holder->state);
auto owner = volatile_read(holder->owner);
// context still valid for this XID?
if (owner != holder_xid) {
goto start_over;
}
if (state == TXN::TXN_CMMTD) {
ASSERT(volatile_read(holder->end));
ASSERT(owner == holder_xid);
if (holder->end < t->xc->begin) {
goto handle_visible;
}
goto handle_invisible;
}
} else {
// Already committed, now do visibility test
ASSERT(cur_obj->GetPersistentAddress().asi_type() == fat_ptr::ASI_LOG ||
cur_obj->GetPersistentAddress().asi_type() == fat_ptr::ASI_CHK ||
cur_obj->GetPersistentAddress() == NULL_PTR); // Delete
uint64_t lsn_offset = LSN::from_ptr(clsn).offset();
if (lsn_offset <= t->xc->begin) {
goto handle_visible;
}
}
goto handle_invisible;
}
handle_visible:
if (out_oid) {
*out_oid = oid;
}
co_return t->DoTupleRead(cur_obj->GetPinnedTuple(), &value);
handle_invisible:
ptr = tentative_next;
prev_obj = cur_obj;
}
}
co_return {RC_FALSE};
}
ermia::coro::generator<rc_t> ConcurrentMasstreeIndex::coro_GetRecord(transaction *t, const varstr &key,
varstr &value, OID *out_oid) {
OID oid = INVALID_OID;
rc_t rc = rc_t{RC_INVALID};
t->ensure_active();
// start: masstree search
ConcurrentMasstree::threadinfo ti(t->xc->begin_epoch);
ConcurrentMasstree::unlocked_tcursor_type lp(*masstree_.get_table(), key.data(), key.size());
// start: find_unlocked
int match;
key_indexed_position kx;
ConcurrentMasstree::node_base_type *root = const_cast<ConcurrentMasstree::node_base_type *>(lp.root_);
retry:
// start: reach_leaf
const ConcurrentMasstree::node_base_type* n[2];
ConcurrentMasstree::nodeversion_type v[2];
bool sense;
// Get a non-stale root.
// Detect staleness by checking whether n has ever split.
// The true root has never split.
sense = false;
n[sense] = root;
while (1) {
v[sense] = n[sense]->stable_annotated(ti.stable_fence());
if (!v[sense].has_split()) break;
n[sense] = n[sense]->unsplit_ancestor();
}
// Loop over internal nodes.
while (!v[sense].isleaf()) {
const ConcurrentMasstree::internode_type* in = static_cast<const ConcurrentMasstree::internode_type*>(n[sense]);
in->prefetch();
co_await std::experimental::suspend_always{};
int kp = ConcurrentMasstree::internode_type::bound_type::upper(lp.ka_, *in);
n[!sense] = in->child_[kp];
if (!n[!sense]) goto retry;
//const ConcurrentMasstree::internode_type* in2 = static_cast<const ConcurrentMasstree::internode_type*>(n[!sense]);
//in2->prefetch();
//co_await std::experimental::suspend_always{};
v[!sense] = n[!sense]->stable_annotated(ti.stable_fence());
if (likely(!in->has_changed(v[sense]))) {
sense = !sense;
continue;
}
ConcurrentMasstree::nodeversion_type oldv = v[sense];
v[sense] = in->stable_annotated(ti.stable_fence());
if (oldv.has_split(v[sense]) &&
in->stable_last_key_compare(lp.ka_, v[sense], ti) > 0) {
goto retry;
}
}
lp.v_ = v[sense];
lp.n_ = const_cast<ConcurrentMasstree::leaf_type *>(static_cast<const ConcurrentMasstree::leaf_type *>(n[sense]));
// end: reach_leaf
forward:
if (lp.v_.deleted()) goto retry;
// XXX(tzwang): already working on this node, no need to prefetch+yield again?
//lp.n_->prefetch();
//co_await std::experimental::suspend_always{};
lp.perm_ = lp.n_->permutation();
kx = ConcurrentMasstree::leaf_type::bound_type::lower(lp.ka_, lp);
if (kx.p >= 0) {
lp.lv_ = lp.n_->lv_[kx.p];
lp.lv_.prefetch(lp.n_->keylenx_[kx.p]);
co_await std::experimental::suspend_always{};
match = lp.n_->ksuf_matches(kx.p, lp.ka_);
} else
match = 0;
if (lp.n_->has_changed(lp.v_)) {
lp.n_ = lp.n_->advance_to_key(lp.ka_, lp.v_, ti);
goto forward;
}
if (match < 0) {
lp.ka_.shift_by(-match);
root = lp.lv_.layer();
goto retry;
}
// end: find_unlocked
bool found = match;
dbtuple *tuple = nullptr;
if (found) {
oid = lp.value();
// end: masstree search
// start: oid_get_version
oid_array *oa = table_descriptor->GetTupleArray();
TXN::xid_context *visitor_xc = t->xc;
fat_ptr *entry = oa->get(oid);
start_over:
::prefetch((const char*)entry);
co_await std::experimental::suspend_always{};
fat_ptr ptr = volatile_read(*entry);
ASSERT(ptr.asi_type() == 0);
Object *prev_obj = nullptr;
while (ptr.offset()) {
Object *cur_obj = nullptr;
// Must read next_ before reading cur_obj->_clsn:
// the version we're currently reading (ie cur_obj) might be unlinked
// and thus recycled by the memory allocator at any time if it's not
// a committed version. If so, cur_obj->_next will be pointing to some
// other object in the allocator's free object pool - we'll probably
// end up at la-la land if we followed this _next pointer value...
// Here we employ some flavor of OCC to solve this problem:
// the aborting transaction that will unlink cur_obj will update
// cur_obj->_clsn to NULL_PTR, then deallocate(). Before reading
// cur_obj->_clsn, we (as the visitor), first dereference pp to get
// a stable value that "should" contain the right address of the next
// version. We then read cur_obj->_clsn to verify: if it's NULL_PTR
// that means we might have read a wrong _next value that's actually
// pointing to some irrelevant object in the allocator's memory pool,
// hence must start over from the beginning of the version chain.
fat_ptr tentative_next = NULL_PTR;
// If this is a backup server, then must see persistent_next to find out
// the **real** overwritten version.
ASSERT(ptr.asi_type() == 0);
cur_obj = (Object *)ptr.offset();
//Object::PrefetchHeader(cur_obj);
//co_await std::experimental::suspend_always{};
tentative_next = cur_obj->GetNextVolatile();
ASSERT(tentative_next.asi_type() == 0);
//bool retry = false;
//bool visible = oidmgr->TestVisibility(cur_obj, visitor_xc, retry);
{
fat_ptr clsn = cur_obj->GetClsn();
if (clsn == NULL_PTR) {
ASSERT(!config::is_backup_srv() || (config::command_log && config::replay_threads));
// dead tuple that was (or about to be) unlinked, start over
goto start_over;
}
uint16_t asi_type = clsn.asi_type();
ALWAYS_ASSERT(asi_type == fat_ptr::ASI_XID || asi_type == fat_ptr::ASI_LOG);
if (asi_type == fat_ptr::ASI_XID) { // in-flight
XID holder_xid = XID::from_ptr(clsn);
// Dirty data made by me is visible!
if (holder_xid == t->xc->owner) {
ASSERT(!cur_obj->GetNextVolatile().offset() ||
((Object *)cur_obj->GetNextVolatile().offset())
->GetClsn()
.asi_type() == fat_ptr::ASI_LOG);
ASSERT(!config::is_backup_srv() || (config::command_log && config::replay_threads));
goto handle_visible;
}
auto *holder = TXN::xid_get_context(holder_xid);
if (!holder) {
goto start_over;
}
auto state = volatile_read(holder->state);
auto owner = volatile_read(holder->owner);
// context still valid for this XID?
if (owner != holder_xid) {
goto start_over;
}
if (state == TXN::TXN_CMMTD) {
ASSERT(volatile_read(holder->end));
ASSERT(owner == holder_xid);
if (holder->end < t->xc->begin) {
goto handle_visible;
}
goto handle_invisible;
}
} else {
// Already committed, now do visibility test
ASSERT(cur_obj->GetPersistentAddress().asi_type() == fat_ptr::ASI_LOG ||
cur_obj->GetPersistentAddress().asi_type() == fat_ptr::ASI_CHK ||
cur_obj->GetPersistentAddress() == NULL_PTR); // Delete
uint64_t lsn_offset = LSN::from_ptr(clsn).offset();
if (lsn_offset <= t->xc->begin) {
goto handle_visible;
}
}
goto handle_invisible;
}
handle_visible:
if (out_oid) {
*out_oid = oid;
}
co_return t->DoTupleRead(cur_obj->GetPinnedTuple(), &value);
handle_invisible:
ptr = tentative_next;
prev_obj = cur_obj;
}
}
co_return {RC_FALSE};
}
ermia::coro::generator<rc_t> ConcurrentMasstreeIndex::coro_UpdateRecord(transaction *t, const varstr &key,
varstr &value) {
// For primary index only
ALWAYS_ASSERT(IsPrimary());
// Search for OID
OID oid = INVALID_OID;
rc_t rc = rc_t{RC_INVALID};
ConcurrentMasstree::versioned_node_t sinfo;
t->ensure_active();
// start: masstree search
ConcurrentMasstree::threadinfo ti(t->xc->begin_epoch);
ConcurrentMasstree::unlocked_tcursor_type lp(*masstree_.get_table(), key.data(), key.size());
// start: find_unlocked
int match;
key_indexed_position kx;
ConcurrentMasstree::node_base_type* root = const_cast<ConcurrentMasstree::node_base_type*>(lp.root_);
retry:
// start: reach_leaf
const ConcurrentMasstree::node_base_type* n[2];
ConcurrentMasstree::nodeversion_type v[2];
bool sense;
sense = false;
n[sense] = root;
while (1) {
v[sense] = n[sense]->stable_annotated(ti.stable_fence());
if (!v[sense].has_split()) break;
n[sense] = n[sense]->unsplit_ancestor();
}
// Loop over internal nodes.
while (!v[sense].isleaf()) {
const ConcurrentMasstree::internode_type* in = static_cast<const ConcurrentMasstree::internode_type*>(n[sense]);
in->prefetch();
co_await std::experimental::suspend_always{};
int kp = ConcurrentMasstree::internode_type::bound_type::upper(lp.ka_, *in);
n[!sense] = in->child_[kp];
if (!n[!sense]) goto retry;
v[!sense] = n[!sense]->stable_annotated(ti.stable_fence());
if (likely(!in->has_changed(v[sense]))) {
sense = !sense;
continue;
}
ConcurrentMasstree::nodeversion_type oldv = v[sense];
v[sense] = in->stable_annotated(ti.stable_fence());
if (oldv.has_split(v[sense]) &&
in->stable_last_key_compare(lp.ka_, v[sense], ti) > 0) {
goto retry;
}
}
lp.v_ = v[sense];
lp.n_ = const_cast<ConcurrentMasstree::leaf_type*>(static_cast<const ConcurrentMasstree::leaf_type*>(n[sense]));
// end: reach_leaf
forward:
if (lp.v_.deleted()) goto retry;
//lp.n_->prefetch();
//co_await std::experimental::suspend_always{};
lp.perm_ = lp.n_->permutation();
kx = ConcurrentMasstree::leaf_type::bound_type::lower(lp.ka_, lp);
if (kx.p >= 0) {
lp.lv_ = lp.n_->lv_[kx.p];
lp.lv_.prefetch(lp.n_->keylenx_[kx.p]);
co_await std::experimental::suspend_always{};
match = lp.n_->ksuf_matches(kx.p, lp.ka_);
} else
match = 0;
if (lp.n_->has_changed(lp.v_)) {
lp.n_ = lp.n_->advance_to_key(lp.ka_, lp.v_, ti);
goto forward;
}
if (match < 0) {
lp.ka_.shift_by(-match);
root = lp.lv_.layer();
goto retry;
}
// end: find_unlocked
if (match) {
oid = lp.value();
}
sinfo = ConcurrentMasstree::versioned_node_t(lp.node(), lp.full_version_value());
// end: masstree search
if (match) {
// By default we don't do coroutine prefetch-yield here for updates, assuming
// it's part of an RMW (most cases) which means the data is probably in cache
// anyway. This may not be true however for blind updates.
#ifndef CORO_UPDATE_VERSION_CHAIN
rc = t->Update(table_descriptor, oid, &key, &value);
#else
oid_array *tuple_array = table_descriptor->GetTupleArray();
FID tuple_fid = table_descriptor->GetTupleFid();
fat_ptr new_obj_ptr = NULL_PTR;
fat_ptr prev_obj_ptr = NULL_PTR;
Object *new_object = nullptr;
start_over:
auto *ptr = tuple_array->get(oid);
::prefetch((const char*)ptr);
co_await std::experimental::suspend_always{};
fat_ptr head = volatile_read(*ptr);
ASSERT(head.asi_type() == 0);
Object *old_desc = (Object *)head.offset();
ASSERT(old_desc);
ASSERT(head.size_code() != INVALID_SIZE_CODE);
Object::PrefetchHeader(old_desc);
co_await std::experimental::suspend_always{};
dbtuple *version = (dbtuple *)old_desc->GetPayload();
bool overwrite = false;
auto clsn = old_desc->GetClsn();
if (clsn == NULL_PTR) {
// stepping on an unlinked version?
goto start_over;
} else if (clsn.asi_type() == fat_ptr::ASI_XID) {
/* Grab the context for this XID. If we're too slow,
the context might be recycled for a different XID,
perhaps even *while* we are reading the
context. Copy everything we care about and then
(last) check the context's XID for a mismatch that
would indicate an inconsistent read. If this
occurs, just start over---the version we cared
about is guaranteed to have a LSN now.
*/
auto holder_xid = XID::from_ptr(clsn);
XID updater_xid = volatile_read(t->xid);
// in-place update case (multiple updates on the same record by same
// transaction)
if (holder_xid == updater_xid) {
overwrite = true;
goto install;
}
TXN::xid_context *holder = TXN::xid_get_context(holder_xid);
if (not holder) {
#ifndef NDEBUG
auto t = old_desc->GetClsn().asi_type();
ASSERT(t == fat_ptr::ASI_LOG or oid_get(oa, o) != head);
#endif
goto start_over;
}
ASSERT(holder);
auto state = volatile_read(holder->state);
auto owner = volatile_read(holder->owner);
holder = NULL; // use cached values instead!
// context still valid for this XID?
if (unlikely(owner != holder_xid)) {
goto start_over;
}
ASSERT(holder_xid != updater_xid);
if (state == TXN::TXN_CMMTD) {
// Allow installing a new version if the tx committed (might
// still hasn't finished post-commit). Note that the caller
// (ie do_tree_put) should look at the clsn field of the
// returned version (prev) to see if this is an overwrite
// (ie xids match) or not (xids don't match).
ASSERT(holder_xid != updater_xid);
goto install;
}
prev_obj_ptr = NULL_PTR;
goto check_prev;
}
// check dirty writes
else {
ASSERT(clsn.asi_type() == fat_ptr::ASI_LOG);
#ifndef RC
// First updater wins: if some concurrent tx committed first,
// I have to abort. Same as in Oracle. Otherwise it's an isolation
// failure: I can modify concurrent transaction's writes.
if (LSN::from_ptr(clsn).offset() >= t->xc->begin) {
prev_obj_ptr = NULL_PTR;
goto check_prev;
}
#endif
goto install;
}
install:
// remove uncommitted overwritten version
// (tx's repetitive updates, keep the latest one only)
// Note for this to be correct we shouldn't allow multiple txs
// working on the same tuple at the same time.
new_obj_ptr = Object::Create(&value, false, t->xc->begin_epoch);
ASSERT(new_obj_ptr.asi_type() == 0);
new_object = (Object *)new_obj_ptr.offset();
new_object->SetClsn(t->xc->owner.to_ptr());
if (overwrite) {
new_object->SetNextPersistent(old_desc->GetNextPersistent());
new_object->SetNextVolatile(old_desc->GetNextVolatile());
// I already claimed it, no need to use cas then
volatile_write(ptr->_ptr, new_obj_ptr._ptr);
__sync_synchronize();
prev_obj_ptr = head;
goto check_prev;
} else {
fat_ptr pa = old_desc->GetPersistentAddress();
while (pa == NULL_PTR) {
pa = old_desc->GetPersistentAddress();
}
new_object->SetNextPersistent(pa);
new_object->SetNextVolatile(head);
if (__sync_bool_compare_and_swap(&ptr->_ptr, head._ptr,
new_obj_ptr._ptr)) {
// Succeeded installing a new version, now only I can modify the
// chain, try recycle some objects
if (config::enable_gc) {
MM::gc_version_chain(ptr);
}
prev_obj_ptr = head;
goto check_prev;
} else {
MM::deallocate(new_obj_ptr);
}
}
prev_obj_ptr = NULL_PTR;
check_prev:
Object *prev_obj = (Object *)prev_obj_ptr.offset();
if (prev_obj) { // succeeded
Object::PrefetchHeader(prev_obj);
co_await std::experimental::suspend_always{};
dbtuple *tuple = ((Object *)new_obj_ptr.offset())->GetPinnedTuple();
ASSERT(tuple);
dbtuple *prev = prev_obj->GetPinnedTuple();
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
ASSERT(xc);
#ifdef SSI
// TODO
#endif
#ifdef SSN
// TODO
#endif
// read prev's clsn first, in case it's a committing XID, the clsn's state
// might change to ASI_LOG anytime
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
fat_ptr prev_clsn = prev->GetObject()->GetClsn();
fat_ptr prev_persistent_ptr = NULL_PTR;
if (prev_clsn.asi_type() == fat_ptr::ASI_XID and
XID::from_ptr(prev_clsn) == t->xid) {
// updating my own updates!
// prev's prev: previous *committed* version
ASSERT(((Object *)prev_obj_ptr.offset())->GetAllocateEpoch() ==
xc->begin_epoch);
prev_persistent_ptr = prev_obj->GetNextPersistent();
// FIXME(tzwang): 20190210: seems the deallocation here is too early,
// causing readers to not find any visible version. Fix this together with
// GC later.
//MM::deallocate(prev_obj_ptr);
} else { // prev is committed (or precommitted but in post-commit now) head
#if defined(SSI) || defined(SSN) || defined(MVOCC)
// TODO
#endif
t->add_to_write_set(tuple_array->get(oid));
prev_persistent_ptr = prev_obj->GetPersistentAddress();
}
ASSERT(not tuple->pvalue or tuple->pvalue->size() == tuple->size);
ASSERT(tuple->GetObject()->GetClsn().asi_type() == fat_ptr::ASI_XID);
ASSERT(oidmgr->oid_get_version(tuple_fid, oid, xc) == tuple);
ASSERT(log);
// FIXME(tzwang): mark deleted in all 2nd indexes as well?
// The varstr also encodes the pdest of the overwritten version.
// FIXME(tzwang): the pdest of the overwritten version doesn't belong to
// varstr. Embedding it in varstr makes it part of the payload and is
// helpful for digging out versions on backups. Not used by the primary.
value.ptr = prev_persistent_ptr;
ASSERT(is_delete || (value.ptr.offset() && value.ptr.asi_type() == fat_ptr::ASI_LOG));
// log the whole varstr so that recovery can figure out the real size
// of the tuple, instead of using the decoded (larger-than-real) size.
size_t data_size = value.size() + sizeof(varstr);
auto size_code = encode_size_aligned(data_size);
t->log->log_update(tuple_fid, oid, fat_ptr::make((void *)&value, size_code),
DEFAULT_ALIGNMENT_BITS,
tuple->GetObject()->GetPersistentAddressPtr());
if (config::log_key_for_update) {
auto key_size = align_up(key.size() + sizeof(varstr));
auto key_size_code = encode_size_aligned(key_size);
t->log->log_update_key(tuple_fid, oid,
fat_ptr::make((void *)&key, key_size_code),
DEFAULT_ALIGNMENT_BITS);
}
rc = rc_t{RC_TRUE};
} else { // somebody else acted faster than we did
rc = rc_t{RC_ABORT_SI_CONFLICT};
}
#endif // CORO_UPDATE_VERSION_CHAIN
} else {
rc = rc_t{RC_ABORT_INTERNAL};
}
co_return rc;
}
ermia::coro::generator<bool> ConcurrentMasstreeIndex::coro_InsertOID(transaction *t, const varstr &key, OID oid) {
ASSERT((char *)key.data() == (char *)&key + sizeof(varstr));
t->ensure_active();
// start: InsertIfAbsent
ConcurrentMasstree::insert_info_t ins_info;
// strat: insert_if_absent
ermia::ConcurrentMasstree::insert_info_t *insert_info = &ins_info;
// Recovery will give a null xc, use epoch 0 for the memory allocated
epoch_num e = 0;
if (t->xc)
e = t->xc->begin_epoch;
ConcurrentMasstree::threadinfo ti(e);
ConcurrentMasstree::tcursor_type lp(*masstree_.get_table(), key.data(), key.size());
// start: find_insert
// start: find_locked
ConcurrentMasstree::node_base_type* root = const_cast<ConcurrentMasstree::node_base_type*>(lp.root_);
ConcurrentMasstree::nodeversion_type version;
ConcurrentMasstree::permuter_type perm;
retry:
// start: reach_leaf
const ConcurrentMasstree::node_base_type* n[2];
ConcurrentMasstree::nodeversion_type v[2];
bool sense;
// Get a non-stale root.
// Detect staleness by checking whether n has ever split.
// The true root has never split.
sense = false;
n[sense] = root;
while (1) {
v[sense] = n[sense]->stable_annotated(ti.stable_fence());
if (!v[sense].has_split()) break;
n[sense] = n[sense]->unsplit_ancestor();
}
// Loop over internal nodes.
while (!v[sense].isleaf()) {
const ConcurrentMasstree::internode_type* in = static_cast<const ConcurrentMasstree::internode_type*>(n[sense]);
in->prefetch();
co_await std::experimental::suspend_always{};
int kp = ConcurrentMasstree::internode_type::bound_type::upper(lp.ka_, *in);
n[!sense] = in->child_[kp];
if (!n[!sense]) goto retry;
v[!sense] = n[!sense]->stable_annotated(ti.stable_fence());
if (likely(!in->has_changed(v[sense]))) {
sense = !sense;
continue;
}
ConcurrentMasstree::nodeversion_type oldv = v[sense];
v[sense] = in->stable_annotated(ti.stable_fence());
if (oldv.has_split(v[sense]) &&
in->stable_last_key_compare(lp.ka_, v[sense], ti) > 0) {
goto retry;
}
}
version = v[sense];
lp.n_ = const_cast<ConcurrentMasstree::leaf_type*>(static_cast<const ConcurrentMasstree::leaf_type*>(n[sense]));
// end: reach_leaf
forward:
if (version.deleted()) goto retry;
//lp.n_->prefetch();
//co_await std::experimental::suspend_always{};
perm = lp.n_->permutation();
fence();
lp.kx_ = ConcurrentMasstree::leaf_type::bound_type::lower(lp.ka_, *lp.n_);
if (lp.kx_.p >= 0) {
ConcurrentMasstree::leafvalue_type lv = lp.n_->lv_[lp.kx_.p];
lv.prefetch(lp.n_->keylenx_[lp.kx_.p]);
co_await std::experimental::suspend_always{};
lp.state_ = lp.n_->ksuf_matches(lp.kx_.p, lp.ka_);
if (lp.state_ < 0 && !lp.n_->has_changed(version) && !lv.layer()->has_split()) {
lp.ka_.shift_by(-lp.state_);
root = lv.layer();
goto retry;
}
} else
lp.state_ = 0;
lp.n_->lock(version, ti.lock_fence(tc_leaf_lock));
if (lp.n_->has_changed(version) || lp.n_->permutation() != perm) {
lp.n_->unlock();
lp.n_ = lp.n_->advance_to_key(lp.ka_, version, ti);
goto forward;
} else if (unlikely(lp.state_ < 0)) {
lp.ka_.shift_by(-lp.state_);
lp.n_->lv_[lp.kx_.p] = root = lp.n_->lv_[lp.kx_.p].layer()->unsplit_ancestor();
lp.n_->unlock();
goto retry;
} else if (unlikely(lp.n_->deleted_layer())) {
lp.ka_.unshift_all();
root = const_cast<ConcurrentMasstree::node_base_type*>(lp.root_);
goto retry;
}
// end: find_locked
lp.original_n_ = lp.n_;
lp.original_v_ = lp.n_->full_unlocked_version_value();
bool found = true;
// maybe we found it