-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.cc
472 lines (408 loc) · 15.2 KB
/
engine.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
#include "dbcore/sm-thread.h"
#include "engine.h"
#include "txn.h"
namespace ermia {
thread_local dlog::tls_log tlog; // TODO(pcontext): at least context-local
std::mutex tlog_lock;
std::mutex receiver_fd_map_lock;
TATAS spinlock;
Spinlock myspinlock;
std::unordered_map<uint32_t, int> receiver_fd_map;
std::unordered_map<uint32_t, int> sender_idx_map;
dlog::tls_log *GetLog(uint32_t logid) {
// XXX(tzwang): this lock may become a problem; should be safe to not use it -
// the set of tlogs are stable before the system starts to run, i.e., only
// needed when creating logs
//std::lock_guard<std::mutex> guard(tlog_lock);
return dlog::tlogs[logid];
}
dlog::tls_log *GetLog() {
thread_local bool initialized = false; // TODO(pcontext): we need a dlog per context
if (!initialized) {
pcontext::lock();
std::lock_guard<std::mutex> guard(tlog_lock);
tlog.initialize(config::log_dir.c_str(),
dlog::tlogs.size(),
numa_node_of_cpu(sched_getcpu()),
config::log_buffer_mb,
config::log_segment_mb);
initialized = true;
dlog::tlogs.push_back(&tlog);
pcontext::unlock();
}
return &tlog;
}
// Engine initialization, including creating the OID, log, and checkpoint
// managers and recovery if needed.
Engine::Engine() {
config::sanity_check();
ALWAYS_ASSERT(config::log_dir.size());
ALWAYS_ASSERT(!oidmgr);
sm_oid_mgr::create();
ALWAYS_ASSERT(oidmgr);
ermia::dlog::initialize();
}
Engine::~Engine() {
ermia::dlog::uninitialize();
}
TableDescriptor *Engine::CreateTable(const char *name) {
auto *td = TableDescriptor::New(name);
if (true) { //!sm_log::need_recovery) {
// Note: this will insert to the log and therefore affect min_flush_lsn,
// so must be done in an sm-thread which must be created by the user
// application (not here in ERMIA library).
//ASSERT(ermia::logmgr);
// TODO(tzwang): perhaps make this transactional to allocate it from
// transaction string arena to avoid malloc-ing memory (~10k size).
//char *log_space = (char *)malloc(sizeof(sm_tx_log));
//ermia::sm_tx_log *log = ermia::logmgr->new_tx_log(log_space);
td->Initialize();
//log->log_table(td->GetTupleFid(), td->GetKeyFid(), td->GetName());
//log->commit(nullptr);
//free(log_space);
}
return td;
}
void Engine::LogIndexCreation(bool primary, FID table_fid, FID index_fid, const std::string &index_name) {
/*
if (!sm_log::need_recovery) {
// Note: this will insert to the log and therefore affect min_flush_lsn,
// so must be done in an sm-thread which must be created by the user
// application (not here in ERMIA library).
ASSERT(ermia::logmgr);
// TODO(tzwang): perhaps make this transactional to allocate it from
// transaction string arena to avoid malloc-ing memory (~10k size).
char *log_space = (char *)malloc(sizeof(sm_tx_log));
ermia::sm_tx_log *log = ermia::logmgr->new_tx_log(log_space);
log->log_index(table_fid, index_fid, index_name, primary);
log->commit(nullptr);
free(log_space);
}
*/
}
void Engine::CreateIndex(const char *table_name, const std::string &index_name, bool is_primary) {
auto *td = TableDescriptor::Get(table_name);
ALWAYS_ASSERT(td);
auto *index = new ConcurrentMasstreeIndex(table_name, is_primary);
if (is_primary) {
td->SetPrimaryIndex(index, index_name);
} else {
td->AddSecondaryIndex(index, index_name);
}
FID index_fid = index->GetIndexFid();
LogIndexCreation(is_primary, td->GetTupleFid(), index_fid, index_name);
}
PROMISE(rc_t) ConcurrentMasstreeIndex::Scan(transaction *t, const varstr &start_key,
const varstr *end_key, ScanCallback &callback) {
SearchRangeCallback c(callback);
ASSERT(c.return_code._val == RC_FALSE);
if (end_key) {
VERBOSE(std::cerr << "txn_btree(0x" << util::hexify(intptr_t(this))
<< ")::search_range_call [" << util::hexify(start_key)
<< ", " << util::hexify(*end_key) << ")" << std::endl);
} else {
VERBOSE(std::cerr << "txn_btree(0x" << util::hexify(intptr_t(this))
<< ")::search_range_call [" << util::hexify(start_key)
<< ", +inf)" << std::endl);
}
if (!unlikely(end_key && *end_key <= start_key)) {
XctSearchRangeCallback cb(t, &c);
AWAIT masstree_.search_range_call(start_key, end_key ? end_key : nullptr, cb, t->xc);
}
RETURN c.return_code;
}
PROMISE(rc_t) ConcurrentMasstreeIndex::ReverseScan(transaction *t,
const varstr &start_key,
const varstr *end_key,
ScanCallback &callback) {
SearchRangeCallback c(callback);
ASSERT(c.return_code._val == RC_FALSE);
if (!unlikely(end_key && start_key <= *end_key)) {
XctSearchRangeCallback cb(t, &c);
varstr lowervk;
if (end_key) {
lowervk = *end_key;
}
AWAIT masstree_.rsearch_range_call(start_key, end_key ? &lowervk : nullptr, cb,
t->xc);
}
RETURN c.return_code;
}
std::map<std::string, uint64_t> ConcurrentMasstreeIndex::Clear() {
PurgeTreeWalker w;
masstree_.tree_walk(w);
masstree_.clear();
return std::map<std::string, uint64_t>();
}
PROMISE(void) ConcurrentMasstreeIndex::GetRecord(transaction *t, rc_t &rc, const varstr &key,
varstr &value, OID *out_oid) {
OID oid = INVALID_OID;
rc = {RC_INVALID};
ConcurrentMasstree::versioned_node_t sinfo;
if (!t) {
auto e = MM::epoch_enter();
rc._val = AWAIT masstree_.search(key, oid, e, &sinfo) ? RC_TRUE : RC_FALSE;
MM::epoch_exit(0, e);
} else {
bool found = AWAIT masstree_.search(key, oid, t->xc->begin_epoch, &sinfo);
dbtuple *tuple = nullptr;
if (found) {
// Key-OID mapping exists, now try to get the actual tuple to be sure
tuple = AWAIT oidmgr->oid_get_version(table_descriptor->GetTupleArray(), oid, t->xc);
if (!tuple) {
found = false;
}
}
if (found) {
volatile_write(rc._val, t->DoTupleRead(tuple, &value)._val);
} else if (config::phantom_prot) {
volatile_write(rc._val, DoNodeRead(t, sinfo.first, sinfo.second)._val);
} else {
volatile_write(rc._val, RC_FALSE);
}
#ifndef SSN
ASSERT(rc._val == RC_FALSE || rc._val == RC_TRUE);
#endif
}
if (out_oid) {
*out_oid = oid;
}
}
void ConcurrentMasstreeIndex::PurgeTreeWalker::on_node_begin(
const typename ConcurrentMasstree::node_opaque_t *n) {
ASSERT(spec_values.empty());
spec_values = ConcurrentMasstree::ExtractValues(n);
}
void ConcurrentMasstreeIndex::PurgeTreeWalker::on_node_success() {
spec_values.clear();
}
void ConcurrentMasstreeIndex::PurgeTreeWalker::on_node_failure() {
spec_values.clear();
}
PROMISE(bool) ConcurrentMasstreeIndex::InsertIfAbsent(transaction *t, const varstr &key,
OID oid) {
typename ConcurrentMasstree::insert_info_t ins_info;
bool inserted = AWAIT masstree_.insert_if_absent(key, oid, t->xc, &ins_info);
if (!inserted) {
RETURN false;
}
if (config::phantom_prot && !t->masstree_absent_set.empty()) {
// Update node version number
ASSERT(ins_info.node);
auto it = t->masstree_absent_set.find(ins_info.node);
if (it != t->masstree_absent_set.end()) {
if (unlikely(it->second != ins_info.old_version)) {
// Important: caller should unlink the version, otherwise we risk
// leaving a dead version at chain head -> infinite loop or segfault...
RETURN false;
}
// otherwise, bump the version
it->second = ins_info.new_version;
}
}
RETURN true;
}
////////////////// Index interfaces /////////////////
PROMISE(bool) ConcurrentMasstreeIndex::InsertOID(transaction *t, const varstr &key, OID oid) {
bool inserted = AWAIT InsertIfAbsent(t, key, oid);
if (inserted) {
t->LogIndexInsert(this, oid, &key);
if (config::enable_chkpt) {
auto *key_array = GetTableDescriptor()->GetKeyArray();
volatile_write(key_array->get(oid)->_ptr, 0);
}
}
RETURN inserted;
}
PROMISE(rc_t) ConcurrentMasstreeIndex::InsertRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid) {
// For primary index only
ALWAYS_ASSERT(IsPrimary());
ASSERT((char *)key.data() == (char *)&key + sizeof(varstr));
// Insert to the table first
dbtuple *tuple = nullptr;
OID oid = t->Insert(table_descriptor, false, &value, &tuple);
// Done with table record, now set up index
ASSERT((char *)key.data() == (char *)&key + sizeof(varstr));
if (!AWAIT InsertOID(t, key, oid)) {
if (config::enable_chkpt) {
volatile_write(table_descriptor->GetKeyArray()->get(oid)->_ptr, 0);
}
RETURN rc_t{RC_ABORT_INTERNAL};
}
// Succeeded, now put the key there if we need it
if (config::enable_chkpt) {
// XXX(tzwang): only need to install this key if we need chkpt; not a
// realistic setting here to not generate it, the purpose of skipping
// this is solely for benchmarking CC.
varstr *new_key =
(varstr *)MM::allocate(sizeof(varstr) + key.size());
new (new_key) varstr((char *)new_key + sizeof(varstr), 0);
new_key->copy_from(&key);
auto *key_array = table_descriptor->GetKeyArray();
key_array->ensure_size(oid);
oidmgr->oid_put(key_array, oid,
fat_ptr::make((void *)new_key, INVALID_SIZE_CODE));
}
if (out_oid) {
*out_oid = oid;
}
RETURN rc_t{RC_TRUE};
}
PROMISE(rc_t) ConcurrentMasstreeIndex::InsertColdRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid) {
// For primary index only
ALWAYS_ASSERT(IsPrimary());
ASSERT((char *)key.data() == (char *)&key + sizeof(varstr));
// Insert to the table first
dbtuple *tuple = nullptr;
OID oid = t->Insert(table_descriptor, true, &value, &tuple);
// Done with table record, now set up index
ASSERT((char *)key.data() == (char *)&key + sizeof(varstr));
if (!AWAIT InsertOID(t, key, oid)) {
if (config::enable_chkpt) {
volatile_write(table_descriptor->GetKeyArray()->get(oid)->_ptr, 0);
}
RETURN rc_t{RC_ABORT_INTERNAL};
}
// Succeeded, now put the key there if we need it
if (config::enable_chkpt) {
// XXX(tzwang): only need to install this key if we need chkpt; not a
// realistic setting here to not generate it, the purpose of skipping
// this is solely for benchmarking CC.
varstr *new_key =
(varstr *)MM::allocate(sizeof(varstr) + key.size());
new (new_key) varstr((char *)new_key + sizeof(varstr), 0);
new_key->copy_from(&key);
auto *key_array = table_descriptor->GetKeyArray();
key_array->ensure_size(oid);
oidmgr->oid_put(key_array, oid,
fat_ptr::make((void *)new_key, INVALID_SIZE_CODE));
}
if (out_oid) {
*out_oid = oid;
}
RETURN rc_t{RC_TRUE};
}
PROMISE(rc_t) ConcurrentMasstreeIndex::UpdateRecord(transaction *t, const varstr &key, varstr &value) {
// For primary index only
ALWAYS_ASSERT(IsPrimary());
// Search for OID
OID oid = 0;
rc_t rc = {RC_INVALID};
AWAIT GetOID(key, rc, t->xc, oid);
if (rc._val == RC_TRUE) {
rc_t rc = AWAIT t->Update(table_descriptor, oid, &key, &value);
RETURN rc;
} else {
RETURN rc_t{RC_ABORT_INTERNAL};
}
}
PROMISE(rc_t) ConcurrentMasstreeIndex::RemoveRecord(transaction *t, const varstr &key) {
// For primary index only
ALWAYS_ASSERT(IsPrimary());
// Search for OID
OID oid = 0;
rc_t rc = {RC_INVALID};
AWAIT GetOID(key, rc, t->xc, oid);
if (rc._val == RC_TRUE) {
// Allocate an empty record version as the "new" version
varstr *null_val = t->string_allocator().next(0);
rc_t rc = AWAIT t->Update(table_descriptor, oid, &key, null_val);
RETURN rc;
} else {
RETURN rc_t{RC_ABORT_INTERNAL};
}
}
rc_t ConcurrentMasstreeIndex::DoNodeRead(
transaction *t, const ConcurrentMasstree::node_opaque_t *node,
uint64_t version) {
ALWAYS_ASSERT(config::phantom_prot);
ASSERT(node);
auto it = t->masstree_absent_set.find(node);
if (it == t->masstree_absent_set.end()) {
t->masstree_absent_set[node] = version;
} else if (it->second != version) {
return rc_t{RC_ABORT_PHANTOM};
}
return rc_t{RC_TRUE};
}
void ConcurrentMasstreeIndex::XctSearchRangeCallback::on_resp_node(
const typename ConcurrentMasstree::node_opaque_t *n, uint64_t version) {
VERBOSE(std::cerr << "on_resp_node(): <node=0x" << util::hexify(intptr_t(n))
<< ", version=" << version << ">" << std::endl);
VERBOSE(std::cerr << " " << ConcurrentMasstree::NodeStringify(n)
<< std::endl);
if (config::phantom_prot) {
#ifdef SSN
if (t->flags & transaction::TXN_FLAG_READ_ONLY) {
return;
}
#endif
rc_t rc = DoNodeRead(t, n, version);
if (rc.IsAbort()) {
caller_callback->return_code = rc;
}
}
}
bool ConcurrentMasstreeIndex::XctSearchRangeCallback::invoke(
const ConcurrentMasstree *btr_ptr,
const typename ConcurrentMasstree::string_type &k, dbtuple *v,
const typename ConcurrentMasstree::node_opaque_t *n, uint64_t version) {
MARK_REFERENCED(btr_ptr);
MARK_REFERENCED(n);
MARK_REFERENCED(version);
VERBOSE(std::cerr << "search range k: " << util::hexify(k) << " from <node=0x"
<< util::hexify(n) << ", version=" << version << ">"
<< std::endl
<< " " << *((dbtuple *)v) << std::endl);
varstr vv;
caller_callback->return_code = t->DoTupleRead(v, &vv);
if (caller_callback->return_code._val == RC_TRUE) {
return caller_callback->Invoke(k, vv);
} else if (caller_callback->return_code.IsAbort()) {
// don't continue the read if the tx should abort
// ^^^^^ note: see masstree_scan.hh, whose scan() calls
// visit_value(), which calls this function to determine
// if it should stop reading.
return false; // don't continue the read if the tx should abort
}
return true;
}
////////////////// End of index interfaces //////////
////////////////// Table interfaces /////////////////
rc_t Table::Insert(transaction &t, varstr *value, OID *out_oid) {
OID oid = t.Insert(td, false, value);
if (out_oid) {
*out_oid = oid;
}
return oid == INVALID_OID ? rc_t{RC_FALSE} : rc_t{RC_FALSE};
}
rc_t Table::Read(transaction &t, OID oid, varstr *out_value) {
auto *tuple = sync_wait_coro(oidmgr->oid_get_version(td->GetTupleArray(), oid, t.GetXIDContext()));
rc_t rc = {RC_INVALID};
if (tuple) {
// Record exists
volatile_write(rc._val, t.DoTupleRead(tuple, out_value)._val);
} else {
volatile_write(rc._val, RC_FALSE);
}
ASSERT(rc._val == RC_FALSE || rc._val == RC_TRUE);
return rc;
}
PROMISE(rc_t) Table::Update(transaction &t, OID oid, varstr &value) {
rc_t rc = AWAIT t.Update(td, oid, &value);
RETURN rc;
}
rc_t Table::SyncUpdate(transaction &t, OID oid, varstr &value) {
rc_t rc = t.SyncUpdate(td, oid, &value);
return rc;
}
PROMISE(rc_t) Table::Remove(transaction &t, OID oid) {
rc_t rc = AWAIT t.Update(td, oid, nullptr);
RETURN rc;
}
////////////////// End of Table interfaces //////////
OrderedIndex::OrderedIndex(std::string table_name, bool is_primary) : is_primary(is_primary) {
table_descriptor = TableDescriptor::Get(table_name);
self_fid = oidmgr->create_file(true);
}
} // namespace ermia