-
Notifications
You must be signed in to change notification settings - Fork 1
/
txn.cc
610 lines (549 loc) · 22.1 KB
/
txn.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
#include "macros.h"
#include "txn.h"
#include "dbcore/serial.h"
#include "engine.h"
extern thread_local ermia::epoch_num coroutine_batch_end_epoch; // TODO(pcontext?): leave it as-is for now until we need to support coroutine
namespace ermia {
transaction::transaction(uint64_t flags, str_arena &sa, uint32_t coro_batch_idx)
: flags(flags), log(nullptr), log_size(0), sa(&sa), coro_batch_idx(coro_batch_idx) {
if (config::phantom_prot) {
masstree_absent_set.set_empty_key(NULL); // google dense map
masstree_absent_set.clear();
}
write_set.clear();
#if defined(SSN) || defined(SSI) || defined(MVOCC)
read_set.clear();
#endif
xid = TXN::xid_alloc();
xc = TXN::xid_get_context(xid);
xc->xct = this;
if (!(flags & TXN_FLAG_CSWITCH)) {
// "Normal" transactions
xc->begin_epoch = config::tls_alloc ? MM::epoch_enter() : 0;
}
#if defined(SSN) || defined(SSI)
// If there's a safesnap, then SSN treats the snapshot as a transaction
// that has read all the versions, which means every update transaction
// should have a initial pstamp of the safesnap.
//
// Readers under SSI using safesnap are free from SSI checks, but writers
// will have to see whether they have a ct3 that's before the safesnap lsn
// (ie the safesnap is T1, updater is T2). So for SSI updaters also needs
// to take a look at the safesnap lsn.
// Take a safe snapshot if read-only.
if (config::enable_safesnap && (flags & TXN_FLAG_READ_ONLY)) {
ASSERT(MM::safesnap_lsn);
xc->begin = volatile_read(MM::safesnap_lsn);
} else {
TXN::serial_register_tx(coro_batch_idx, xid);
log = logmgr->new_tx_log((char*)string_allocator().next(sizeof(sm_tx_log))->data());
// Must +1: a tx T can only update a tuple if its latest version was
// created before T's begin timestamp (i.e., version.clsn < T.begin,
// note the range is exclusive; see first updater wins rule in
// oid_put_update() in sm-oid.cpp). Otherwise we risk making no
// progress when retrying an aborted transaction: everyone is trying
// to update the same tuple with latest version stamped at cur_lsn()
// but no one can succeed (because version.clsn == cur_lsn == t.begin).
xc->begin = logmgr->cur_lsn().offset() + 1;
#ifdef SSN
xc->pstamp = volatile_read(MM::safesnap_lsn);
#elif defined(SSI)
xc->last_safesnap = volatile_read(MM::safesnap_lsn);
#endif
}
#elif defined(MVOCC)
log = logmgr->new_tx_log((char*)string_allocator().next(sizeof(sm_tx_log))->data());
xc->begin = logmgr->cur_lsn().offset() + 1;
#else
// Give a log regardless - with pipelined commit, read-only tx needs
// to go through the queue as well
log = GetLog();
xc->begin = dlog::current_csn.load(std::memory_order_relaxed);
#endif
is_disk = false;
is_in_memory_queue = true;
m_abort_if_cold = false;
m_is_forced_abort = false;
pos_in_queue = ~uint16_t{0};
cold_log_io_size = 0;
io_uring_user_data[0] = -1;
}
void transaction::uninitialize() {
// transaction shouldn't fall out of scope w/o resolution
// resolution means TXN_CMMTD, and TXN_ABRTD
ASSERT(state() != TXN::TXN_ACTIVE && state() != TXN::TXN_COMMITTING);
#if defined(SSN) || defined(SSI)
if (!config::enable_safesnap || (!(flags & TXN_FLAG_READ_ONLY))) {
TXN::serial_deregister_tx(coro_batch_idx, xid);
}
#endif
if (config::tls_alloc) {
if (flags & TXN_FLAG_CSWITCH) {
if (xc->end > coroutine_batch_end_epoch) {
coroutine_batch_end_epoch = xc->end;
}
} else if (config::enable_safesnap && (flags & TXN_FLAG_READ_ONLY)) {
MM::epoch_exit(0, xc->begin_epoch);
} else {
MM::epoch_exit(xc->end, xc->begin_epoch);
}
}
TXN::xid_free(xid); // must do this after epoch_exit, which uses xc.end
}
void transaction::Abort() {
// Mark the dirty tuple as invalid, for oid_get_version to
// move on more quickly.
volatile_write(xc->state, TXN::TXN_ABRTD);
#if defined(SSN) || defined(SSI)
// Go over the read set first, to deregister from the tuple
// asap so the updater won't wait for too long.
for (uint32_t i = 0; i < read_set.size(); ++i) {
auto &r = read_set[i];
ASSERT(r->GetObject()->GetClsn().asi_type() == fat_ptr::ASI_LOG);
// remove myself from reader list
serial_deregister_reader_tx(coro_batch_idx, &r->readers_bitmap);
}
#endif
for (uint32_t i = 0; i < write_set.size(); ++i) {
auto &w = write_set[i];
dbtuple *tuple = (dbtuple *)w.get_object()->GetPayload();
ASSERT(tuple);
#if defined(SSI) || defined(SSN) || defined(MVOCC)
ASSERT(XID::from_ptr(tuple->GetObject()->GetClsn()) == xid);
if (tuple->NextVolatile()) {
volatile_write(tuple->NextVolatile()->sstamp, NULL_PTR);
#ifdef SSN
tuple->NextVolatile()->welcome_read_mostly_tx();
#endif
}
#endif
Object *obj = w.get_object();
fat_ptr entry = *w.entry;
obj->SetCSN(NULL_PTR);
oidmgr->UnlinkTuple(w.entry);
ASSERT(obj->GetAllocateEpoch() == xc->begin_epoch);
MM::deallocate(entry);
}
}
rc_t transaction::commit() {
ALWAYS_ASSERT(state() == TXN::TXN_ACTIVE);
volatile_write(xc->state, TXN::TXN_COMMITTING);
rc_t ret;
#if defined(SSN) || defined(SSI)
// Safe snapshot optimization for read-only transactions:
// Use the begin ts as cstamp if it's a read-only transaction
// This is the same for both SSN and SSI.
if (config::enable_safesnap && (flags & TXN_FLAG_READ_ONLY)) {
ASSERT(!log);
ASSERT(write_set.size() == 0);
xc->end = xc->begin;
volatile_write(xc->state, TXN::TXN_CMMTD);
ret = {RC_TRUE};
} else {
ASSERT(log);
xc->end = log->pre_commit().offset();
if (xc->end == 0) {
ret = rc_t{RC_ABORT_INTERNAL};
}
#ifdef SSN
ret = parallel_ssn_commit();
#elif defined SSI
ret = parallel_ssi_commit();
#endif
}
#elif defined(MVOCC)
ret = mvocc_commit();
#else
ret = si_commit();
#endif
// Enqueue to pipelined commit queue, if enabled
if (ret._val == RC_TRUE) {
// Keep end CSN before xc is recycled by uninitialize()
auto end = xc->end;
uninitialize();
if (log && ermia::config::pcommit) {
end = !end || write_set.size() ? end : end - 1;
log->enqueue_committed_xct(end);
}
}
return ret;
}
#if !defined(SSI) && !defined(SSN) && !defined(MVOCC)
rc_t transaction::si_commit() {
if (!log && ((flags & TXN_FLAG_READ_ONLY) || write_set.size() == 0)) {
volatile_write(xc->state, TXN::TXN_CMMTD);
return rc_t{RC_TRUE};
}
if (config::phantom_prot && !MasstreeCheckPhantom()) {
return rc_t{RC_ABORT_PHANTOM};
}
ASSERT(log);
// Precommit: obtain a CSN
xc->end = write_set.size() ? dlog::current_csn.fetch_add(1) : xc->begin;
dlog::log_block *lb = nullptr;
dlog::tlog_lsn lb_lsn = dlog::INVALID_TLOG_LSN;
uint64_t segnum = -1;
// Generate a log block if not read-only
if (write_set.size()) {
lb = log->allocate_log_block(log_size, &lb_lsn, &segnum, xc->end);
}
// Normally, we'd generate each version's persitent address along the way or
// here first before toggling the CSN's "committed" bit. But we can actually
// do it first, and generate the log block as we scan the write set once,
// leveraging pipelined commit!
// Post-commit: install CSN to tuples (traverse write-tuple), generate log
// records, etc.
for (uint32_t i = 0; i < write_set.size(); ++i) {
auto &w = write_set[i];
Object *object = w.get_object();
dbtuple *tuple = (dbtuple *)object->GetPayload();
// Populate log block and obtain persistent address
uint32_t off = lb->payload_size;
if (w.is_insert) {
auto ret_off = dlog::log_insert(lb, w.fid, w.oid, (char *)tuple, w.size);
ALWAYS_ASSERT(ret_off == off);
} else {
auto ret_off = dlog::log_update(lb, w.fid, w.oid, (char *)tuple, w.size);
ALWAYS_ASSERT(ret_off == off);
}
ALWAYS_ASSERT(lb->payload_size <= lb->capacity);
// This aligned_size should match what was calculated during
// add_to_write_set, and the size_code calculated based on this aligned size
// will be part of the persistent address, which a read can directly use to
// load the log record from the log (i.e., knowing how many bytes to read to
// obtain the log record header + dbtuple header + record data).
auto aligned_size = align_up(w.size + sizeof(dlog::log_record));
auto size_code = encode_size_aligned(aligned_size);
// lb_lsn points to the start of the log block which has a header, followed
// by individual log records, so the log record's direct address would be
// lb_lsn + sizeof(log_block) + off
fat_ptr pdest = LSN::make(log->get_id(), lb_lsn + sizeof(dlog::log_block) + off, segnum, size_code).to_ptr();
if (w.is_cold) {
// Overwrite the entry to directly carry LSN
MM::deallocate(*w.entry);
volatile_write(w.entry->_ptr, pdest._ptr);
} else {
object->SetPersistentAddress(pdest);
ASSERT(object->GetPersistentAddress().asi_type() == fat_ptr::ASI_LOG);
// Set CSN
fat_ptr csn_ptr = object->GenerateCsnPtr(xc->end);
object->SetCSN(csn_ptr);
ASSERT(tuple->GetObject()->GetCSN().asi_type() == fat_ptr::ASI_CSN);
}
}
ALWAYS_ASSERT(!lb || lb->payload_size == lb->capacity);
// NOTE: make sure this happens after populating log block,
// otherwise readers will see inconsistent data!
// This is when (committed) tuple data are made visible to readers
volatile_write(xc->state, TXN::TXN_CMMTD);
return rc_t{RC_TRUE};
}
#endif
// returns true if btree versions have changed, ie there's phantom
bool transaction::MasstreeCheckPhantom() {
for (auto &r : masstree_absent_set) {
const uint64_t v = ConcurrentMasstree::ExtractVersionNumber(r.first);
if (unlikely(v != r.second)) return false;
}
return true;
}
rc_t transaction::SyncUpdate(TableDescriptor *td, OID oid, const varstr *k, varstr *v) {
oid_array *tuple_array = td->GetTupleArray();
FID tuple_fid = td->GetTupleFid();
// first *updater* wins
fat_ptr new_obj_ptr = NULL_PTR;
fat_ptr prev_obj_ptr =
oidmgr->UpdateTuple(tuple_array, oid, v, xc, &new_obj_ptr);
Object *prev_obj = (Object *)prev_obj_ptr.offset();
if (prev_obj) { // succeeded
dbtuple *tuple = ((Object *)new_obj_ptr.offset())->SyncGetPinnedTuple(this);
ASSERT(tuple);
dbtuple *prev = prev_obj->SyncGetPinnedTuple(this);
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
ASSERT(xc);
#ifdef SSI
ASSERT(prev->sstamp == NULL_PTR);
if (xc->ct3) {
// Check if we are the T2 with a committed T3 earlier than a safesnap
// (being T1)
if (xc->ct3 <= xc->last_safesnap) RETURN {RC_ABORT_SERIAL};
if (volatile_read(prev->xstamp) >= xc->ct3 or
not prev->readers_bitmap.is_empty(coro_batch_idx, true)) {
// Read-only optimization: safe if T1 is read-only (so far) and T1's
// begin ts
// is before ct3.
if (config::enable_ssi_read_only_opt) {
TXN::readers_bitmap_iterator readers_iter(&prev->readers_bitmap);
while (true) {
int32_t xid_idx = readers_iter.next(xc, true);
if (xid_idx == -1) break;
XID rxid = volatile_read(TXN::rlist.xids[xid_idx]);
ASSERT(rxid != xc->owner);
if (rxid == INVALID_XID) // reader is gone, check xstamp in the end
continue;
XID reader_owner = INVALID_XID;
uint64_t reader_begin = 0;
TXN::xid_context *reader_xc = NULL;
reader_xc = TXN::xid_get_context(rxid);
if (not reader_xc) // context change, consult xstamp later
continue;
// copy everything before doing anything
reader_begin = volatile_read(reader_xc->begin);
reader_owner = volatile_read(reader_xc->owner);
if (reader_owner != rxid) // consult xstamp later
continue;
// we're safe if the reader is read-only (so far) and started after
// ct3
if (reader_xc->xct->write_set.size() > 0 and
reader_begin <= xc->ct3) {
oidmgr->UpdateTuple(tuple_array, oid);
RETURN {RC_ABORT_SERIAL};
}
}
} else {
oidmgr->UnlinkTuple(tuple_array, oid);
RETURN {RC_ABORT_SERIAL};
}
}
}
#endif
#ifdef SSN
// update hi watermark
// Overwriting a version could trigger outbound anti-dep,
// i.e., I'll depend on some tx who has read the version that's
// being overwritten by me. So I'll need to see the version's
// access stamp to tell if the read happened.
ASSERT(prev->sstamp == NULL_PTR);
auto prev_xstamp = volatile_read(prev->xstamp);
if (xc->pstamp < prev_xstamp) xc->pstamp = prev_xstamp;
#ifdef EARLY_SSN_CHECK
if (not ssn_check_exclusion(xc)) {
// unlink the version here (note abort_impl won't be able to catch
// it because it's not yet in the write set)
oidmgr->UnlinkTuple(tuple_array, oid);
RETURN rc_t{RC_ABORT_SERIAL};
}
#endif
// copy access stamp to new tuple from overwritten version
// (no need to copy sucessor lsn (slsn))
volatile_write(tuple->xstamp, prev->xstamp);
#endif
// read prev's CSN first, in case it's a committing XID, the CSN's state
// might change to ASI_CSN anytime
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
fat_ptr prev_csn = prev->GetObject()->GetCSN();
fat_ptr prev_persistent_ptr = NULL_PTR;
if (prev_csn.asi_type() == fat_ptr::ASI_XID and
XID::from_ptr(prev_csn) == 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)
volatile_write(prev->sstamp, xc->owner.to_ptr());
ASSERT(prev->sstamp.asi_type() == fat_ptr::ASI_XID);
ASSERT(XID::from_ptr(prev->sstamp) == xc->owner);
ASSERT(tuple->NextVolatile() == prev);
#endif
add_to_write_set(tuple_array->get(oid), tuple_fid, oid, tuple->size, false, false);
prev_persistent_ptr = prev_obj->GetPersistentAddress();
}
ASSERT(tuple->GetObject()->GetCSN().asi_type() == fat_ptr::ASI_XID);
ASSERT(sync_wait_coro(oidmgr->oid_get_version(tuple_fid, oid, xc)) == tuple);
ASSERT(log);
// FIXME(tzwang): mark deleted in all 2nd indexes as well?
return rc_t{RC_TRUE};
} else { // somebody else acted faster than we did
return rc_t{RC_ABORT_SI_CONFLICT};
}
}
PROMISE(rc_t) transaction::Update(TableDescriptor *td, OID oid, const varstr *k, varstr *v) {
oid_array *tuple_array = td->GetTupleArray();
FID tuple_fid = td->GetTupleFid();
// first *updater* wins
fat_ptr new_obj_ptr = NULL_PTR;
fat_ptr prev_obj_ptr =
oidmgr->UpdateTuple(tuple_array, oid, v, xc, &new_obj_ptr);
Object *prev_obj = (Object *)prev_obj_ptr.offset();
if (prev_obj) { // succeeded
dbtuple *tuple = AWAIT ((Object *)new_obj_ptr.offset())->GetPinnedTuple(this);
ASSERT(tuple);
dbtuple *prev = AWAIT prev_obj->GetPinnedTuple(this);
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
ASSERT(xc);
#ifdef SSI
ASSERT(prev->sstamp == NULL_PTR);
if (xc->ct3) {
// Check if we are the T2 with a committed T3 earlier than a safesnap
// (being T1)
if (xc->ct3 <= xc->last_safesnap) RETURN {RC_ABORT_SERIAL};
if (volatile_read(prev->xstamp) >= xc->ct3 or
not prev->readers_bitmap.is_empty(coro_batch_idx, true)) {
// Read-only optimization: safe if T1 is read-only (so far) and T1's
// begin ts
// is before ct3.
if (config::enable_ssi_read_only_opt) {
TXN::readers_bitmap_iterator readers_iter(&prev->readers_bitmap);
while (true) {
int32_t xid_idx = readers_iter.next(xc, true);
if (xid_idx == -1) break;
XID rxid = volatile_read(TXN::rlist.xids[xid_idx]);
ASSERT(rxid != xc->owner);
if (rxid == INVALID_XID) // reader is gone, check xstamp in the end
continue;
XID reader_owner = INVALID_XID;
uint64_t reader_begin = 0;
TXN::xid_context *reader_xc = NULL;
reader_xc = TXN::xid_get_context(rxid);
if (not reader_xc) // context change, consult xstamp later
continue;
// copy everything before doing anything
reader_begin = volatile_read(reader_xc->begin);
reader_owner = volatile_read(reader_xc->owner);
if (reader_owner != rxid) // consult xstamp later
continue;
// we're safe if the reader is read-only (so far) and started after
// ct3
if (reader_xc->xct->write_set.size() > 0 and
reader_begin <= xc->ct3) {
oidmgr->UpdateTuple(tuple_array, oid);
RETURN {RC_ABORT_SERIAL};
}
}
} else {
oidmgr->UnlinkTuple(tuple_array, oid);
RETURN {RC_ABORT_SERIAL};
}
}
}
#endif
#ifdef SSN
// update hi watermark
// Overwriting a version could trigger outbound anti-dep,
// i.e., I'll depend on some tx who has read the version that's
// being overwritten by me. So I'll need to see the version's
// access stamp to tell if the read happened.
ASSERT(prev->sstamp == NULL_PTR);
auto prev_xstamp = volatile_read(prev->xstamp);
if (xc->pstamp < prev_xstamp) xc->pstamp = prev_xstamp;
#ifdef EARLY_SSN_CHECK
if (not ssn_check_exclusion(xc)) {
// unlink the version here (note abort_impl won't be able to catch
// it because it's not yet in the write set)
oidmgr->UnlinkTuple(tuple_array, oid);
RETURN rc_t{RC_ABORT_SERIAL};
}
#endif
// copy access stamp to new tuple from overwritten version
// (no need to copy sucessor lsn (slsn))
volatile_write(tuple->xstamp, prev->xstamp);
#endif
// read prev's CSN first, in case it's a committing XID, the CSN's state
// might change to ASI_CSN anytime
ASSERT((uint64_t)prev->GetObject() == prev_obj_ptr.offset());
fat_ptr prev_csn = prev->GetObject()->GetCSN();
fat_ptr prev_persistent_ptr = NULL_PTR;
if (prev_csn.asi_type() == fat_ptr::ASI_XID and
XID::from_ptr(prev_csn) == 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)
volatile_write(prev->sstamp, xc->owner.to_ptr());
ASSERT(prev->sstamp.asi_type() == fat_ptr::ASI_XID);
ASSERT(XID::from_ptr(prev->sstamp) == xc->owner);
ASSERT(tuple->NextVolatile() == prev);
#endif
add_to_write_set(tuple_array->get(oid), tuple_fid, oid, tuple->size, false, false);
prev_persistent_ptr = prev_obj->GetPersistentAddress();
}
ASSERT(tuple->GetObject()->GetCSN().asi_type() == fat_ptr::ASI_XID);
ASSERT(sync_wait_coro(oidmgr->oid_get_version(tuple_fid, oid, xc)) == tuple);
ASSERT(log);
// FIXME(tzwang): mark deleted in all 2nd indexes as well?
RETURN rc_t{RC_TRUE};
} else { // somebody else acted faster than we did
RETURN rc_t{RC_ABORT_SI_CONFLICT};
}
}
OID transaction::Insert(TableDescriptor *td, bool cold, varstr *value, dbtuple **out_tuple) {
auto *tuple_array = td->GetTupleArray();
FID tuple_fid = td->GetTupleFid();
fat_ptr new_head = Object::Create(value, xc->begin_epoch);
ASSERT(new_head.size_code() != INVALID_SIZE_CODE);
ASSERT(new_head.asi_type() == 0);
auto *tuple = (dbtuple *)((Object *)new_head.offset())->GetPayload();
ASSERT(decode_size_aligned(new_head.size_code()) >= tuple->size);
tuple->GetObject()->SetCSN(xid.to_ptr());
OID oid = oidmgr->alloc_oid(tuple_fid);
ALWAYS_ASSERT(oid != INVALID_OID);
oidmgr->oid_put_new(tuple_array, oid, new_head);
ASSERT(tuple->size == value->size());
add_to_write_set(tuple_array->get(oid), tuple_fid, oid, tuple->size, true, cold);
if (out_tuple) {
*out_tuple = tuple;
}
return oid;
}
void transaction::LogIndexInsert(OrderedIndex *index, OID oid, const varstr *key) {
/*
// Note: here we log the whole key varstr so that recovery can figure out the
// real key length with key->size(), otherwise it'll have to use the decoded
// (inaccurate) size (and so will build a different index).
auto record_size = align_up(sizeof(varstr) + key->size());
ASSERT((char *)key->data() == (char *)key + sizeof(varstr));
auto size_code = encode_size_aligned(record_size);
log->log_insert_index(index->GetIndexFid(), oid,
fat_ptr::make((void *)key, size_code),
DEFAULT_ALIGNMENT_BITS, NULL);
*/
}
rc_t transaction::DoTupleRead(dbtuple *tuple, varstr *out_v) {
ASSERT(tuple);
ASSERT(xc);
bool read_my_own =
(tuple->GetObject()->GetCSN().asi_type() == fat_ptr::ASI_XID);
ASSERT(!read_my_own || (read_my_own && XID::from_ptr(tuple->GetObject()->GetCSN()) == xc->owner));
ASSERT(!read_my_own || !(flags & TXN_FLAG_READ_ONLY));
#if defined(SSI) || defined(SSN) || defined(MVOCC)
if (!read_my_own) {
rc_t rc = {RC_INVALID};
if (flags & TXN_FLAG_READ_ONLY) {
#if defined(SSI) || defined(SSN)
if (config::enable_safesnap) {
return rc_t{RC_TRUE};
}
#endif
} else {
#ifdef SSN
rc = ssn_read(tuple);
#elif defined(SSI)
rc = ssi_read(tuple);
#else
rc = mvocc_read(tuple);
#endif
}
if (rc.IsAbort()) {
return rc;
}
} // otherwise it's my own update/insert, just read it
#endif
// do the actual tuple read
out_v->p = tuple->get_value_start();
out_v->l = tuple->size;
return tuple->size > 0 ? rc_t{RC_TRUE} : rc_t{RC_FALSE};
}
} // namespace ermia