-
Notifications
You must be signed in to change notification settings - Fork 26
/
ermia.cc
329 lines (290 loc) · 9.96 KB
/
ermia.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
#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"
namespace ermia {
// Engine initialization, including creating the OID, log, and checkpoint
// managers and recovery if needed.
Engine::Engine() {
config::sanity_check();
if (!config::is_backup_srv()) {
if (!RCU::rcu_is_registered()) {
RCU::rcu_register();
}
RCU::rcu_enter();
ALWAYS_ASSERT(config::log_dir.size());
ALWAYS_ASSERT(not logmgr);
ALWAYS_ASSERT(not oidmgr);
sm_log::allocate_log_buffer();
logmgr = sm_log::new_log(config::recover_functor, nullptr);
sm_oid_mgr::create();
if (config::command_log) {
CommandLog::cmd_log = new CommandLog::CommandLogManager();
}
ALWAYS_ASSERT(logmgr);
ALWAYS_ASSERT(oidmgr);
LSN chkpt_lsn = logmgr->get_chkpt_start();
if (config::enable_chkpt) {
chkptmgr = new sm_chkpt_mgr(chkpt_lsn);
}
// The backup will want to recover in another thread
if (sm_log::need_recovery) {
logmgr->recover();
}
RCU::rcu_exit();
}
}
void Engine::CreateTable(uint16_t index_type, const char *name,
const char *primary_name) {
IndexDescriptor *index_desc = nullptr;
switch (index_type) {
case kIndexConcurrentMasstree:
index_desc =
(new ConcurrentMasstreeIndex(name, primary_name))->GetDescriptor();
break;
default:
LOG(FATAL) << "Wrong index type: " << index_type;
break;
}
if (!sm_log::need_recovery && !config::is_backup_srv()) {
ASSERT(ermia::logmgr);
auto create_file = [=](char *) {
ermia::RCU::rcu_enter();
DEFER(ermia::RCU::rcu_exit());
ermia::sm_tx_log *log = ermia::logmgr->new_tx_log();
index_desc->Initialize();
log->log_index(index_desc->GetTupleFid(), index_desc->GetKeyFid(),
index_desc->GetName());
log->commit(nullptr);
};
// Note: this will insert to the log and therefore affect min_flush_lsn,
// so must be done in an sm-thread.
ermia::thread::Thread *thread = ermia::thread::GetThread(true);
ALWAYS_ASSERT(thread);
thread->StartTask(create_file);
thread->Join();
ermia::thread::PutThread(thread);
}
}
rc_t ConcurrentMasstreeIndex::Scan(transaction *t, const varstr &start_key,
const varstr *end_key,
ScanCallback &callback, str_arena *arena) {
MARK_REFERENCED(arena);
SearchRangeCallback c(callback);
ASSERT(c.return_code._val == RC_FALSE);
t->ensure_active();
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);
varstr uppervk;
if (end_key) {
uppervk = *end_key;
}
masstree_.search_range_call(start_key, end_key ? &uppervk : nullptr, cb,
t->xc);
}
return c.return_code;
}
rc_t ConcurrentMasstreeIndex::ReverseScan(transaction *t,
const varstr &start_key,
const varstr *end_key,
ScanCallback &callback,
str_arena *arena) {
MARK_REFERENCED(arena);
SearchRangeCallback c(callback);
ASSERT(c.return_code._val == RC_FALSE);
t->ensure_active();
if (!unlikely(end_key && start_key <= *end_key)) {
XctSearchRangeCallback cb(t, &c);
varstr lowervk;
if (end_key) {
lowervk = *end_key;
}
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>();
}
void ConcurrentMasstreeIndex::Get(transaction *t, rc_t &rc, const varstr &key,
varstr &value, OID *out_oid) {
OID oid = 0;
rc = {RC_INVALID};
ConcurrentMasstree::versioned_node_t sinfo;
if (!t) {
auto e = MM::epoch_enter();
rc._val = masstree_.search(key, oid, e, &sinfo) ? RC_TRUE : RC_FALSE;
MM::epoch_exit(0, e);
} else {
t->ensure_active();
bool found = 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
if (config::is_backup_srv()) {
tuple = oidmgr->BackupGetVersion(
descriptor_->GetTupleArray(),
descriptor_->GetPersistentAddressArray(), oid, t->xc);
} else {
tuple =
oidmgr->oid_get_version(descriptor_->GetTupleArray(), oid, t->xc);
}
if (!tuple) {
found = false;
}
}
if (found) {
if (out_oid) {
*out_oid = oid;
}
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);
}
ASSERT(rc._val == RC_FALSE || rc._val == RC_TRUE);
}
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();
}
bool ConcurrentMasstreeIndex::InsertIfAbsent(transaction *t, const varstr &key,
OID oid) {
typename ConcurrentMasstree::insert_info_t ins_info;
bool inserted = 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;
}
rc_t OrderedIndex::TryInsert(transaction &t, const varstr *k, varstr *v,
bool upsert, OID *inserted_oid) {
if (t.TryInsertNewTuple(this, k, v, inserted_oid)) {
return rc_t{RC_TRUE};
} else if (!upsert) {
return rc_t{RC_ABORT_INTERNAL};
} else {
return rc_t{RC_FALSE};
}
}
rc_t ConcurrentMasstreeIndex::DoTreePut(transaction &t, const varstr *k,
varstr *v, bool expect_new, bool upsert,
OID *inserted_oid) {
ASSERT(k);
ASSERT((char *)k->data() == (char *)k + sizeof(varstr));
ASSERT(!expect_new || v);
t.ensure_active();
if (expect_new) {
rc_t rc = TryInsert(t, k, v, upsert, inserted_oid);
if (rc._val != RC_FALSE) {
return rc;
}
}
// do regular search
OID oid = 0;
rc_t rc = {RC_INVALID};
GetOID(*k, rc, t.xc, oid);
if (rc._val == RC_TRUE) {
return t.Update(descriptor_, oid, k, v);
} 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);
t->ensure_active();
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;
}
} // namespace ermia