-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxn.cc
286 lines (243 loc) · 8.08 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
#include "txn.h"
#include "index.h"
#include "util/objects.h"
#include "literals.h"
#include "gc.h"
#include "commit_buffer.h"
#include "coro_sched.h"
namespace felis {
BaseTxn::BrkType BaseTxn::g_brk;
int BaseTxn::g_cur_numa_node = 0;
void BaseTxn::InitBrk(long nr_epochs)
{
auto nr_numa_nodes = (NodeConfiguration::g_nr_threads - 1) / mem::kNrCorePerNode + 1;
auto lmt = 100_M * nr_epochs / nr_numa_nodes;
for (auto n = 0; n < nr_numa_nodes; n++) {
auto numa_node = n;
g_brk[n] = mem::Brk::New(mem::AllocMemory(mem::Txn, lmt, numa_node), lmt);
}
}
// #define READ_OWN_WRITE
void BaseTxn::BaseTxnRow::AppendNewVersion(int ondemand_split_weight)
{
if (!EpochClient::g_enable_granola && !EpochClient::g_enable_pwv) {
auto commit_buffer = EpochClient::g_workload_client->commit_buffer;
auto is_dup = false;
#ifdef READ_OWN_WRITE
if (!VHandleSyncService::g_lock_elision)
is_dup = commit_buffer->AddRef(go::Scheduler::CurrentThreadPoolId() - 1, vhandle, sid);
#endif
if (!is_dup) {
vhandle->AppendNewVersion(sid, epoch_nr, ondemand_split_weight);
} else {
// This should be rare. Let's warn the user.
logger->warn("Duplicate write detected in sid {} on row {}", sid, (void *) vhandle);
}
} else {
if (vhandle->nr_versions() == 0) {
vhandle->AppendNewVersion(sid, epoch_nr);
vhandle->WriteExactVersion(0, nullptr, epoch_nr);
}
}
}
VarStr *BaseTxn::BaseTxnRow::ReadVarStr()
{
auto commit_buffer = EpochClient::g_workload_client->commit_buffer;
CommitBuffer::Entry *ent = nullptr;
#ifdef READ_OWN_WRITE
ent = commit_buffer->LookupDuplicate(vhandle, sid);
#endif
if (ent && ent->u.value != (VarStr *) kPendingValue)
return ent->u.value;
else
return vhandle->ReadWithVersion(sid);
}
bool BaseTxn::BaseTxnRow::WriteVarStr(VarStr *obj)
{
if (!EpochClient::g_enable_granola && !EpochClient::g_enable_pwv) {
auto commit_buffer = EpochClient::g_workload_client->commit_buffer;
CommitBuffer::Entry *ent = nullptr;
#ifdef READ_OWN_WRITE
ent = commit_buffer->LookupDuplicate(vhandle, sid);
#endif
if (ent) {
ent->u.value = obj;
if (ent->wcnt.fetch_sub(1) - 1 > 0)
return true;
}
return vhandle->WriteWithVersion(sid, obj, epoch_nr);
} else {
auto p = vhandle->ReadExactVersion(0);
size_t nr_bytes = 0;
util::Instance<GC>().FreeIfGarbage(vhandle, p, obj);
return vhandle->WriteExactVersion(0, obj, epoch_nr);
}
}
int64_t BaseTxn::UpdateForKeyAffinity(int node, VHandle *row)
{
if (Options::kOnDemandSplitting) {
auto &conf = util::Instance<NodeConfiguration>();
if (row->contention_affinity() == -1 || (node != 0 && conf.node_id() != node))
goto nosplit;
auto client = EpochClient::g_workload_client;
auto commit_buffer = client->commit_buffer;
if (commit_buffer->LookupDuplicate(row, sid))
goto nosplit;
auto &mgr = client->get_contention_locality_manager();
auto aff = mgr.GetScheduleCore(row->contention_affinity());
return aff;
}
nosplit:
return -1;
}
BaseTxn::BaseTxnIndexOpContext::BaseTxnIndexOpContext(
BaseTxnHandle handle, EpochObject state,
uint16_t keys_bitmap, VarStr **keys,
uint16_t slices_bitmap, int16_t *slices,
uint16_t rels_bitmap, int16_t *rels)
: handle(handle), state(state), keys_bitmap(keys_bitmap),
slices_bitmap(slices_bitmap), rels_bitmap(rels_bitmap)
{
ForEachWithBitmap(
keys_bitmap,
[this, keys](int to, int from) {
key_len[to] = keys[from]->length();
key_data[to] = keys[from]->data();
});
ForEachWithBitmap(
slices_bitmap,
[this, slices](int to, int from) {
slice_ids[to] = slices[from];
});
ForEachWithBitmap(
rels_bitmap,
[this, rels](int to, int from) {
relation_ids[to] = rels[from];
});
}
size_t BaseTxn::BaseTxnIndexOpContext::EncodeSize() const
{
size_t sum = 0;
int nr_keys = __builtin_popcount(keys_bitmap);
for (auto i = 0; i < nr_keys; i++) {
sum += 2 + key_len[i];
}
sum += __builtin_popcount(slices_bitmap) * sizeof(uint16_t);
sum += __builtin_popcount(rels_bitmap) * sizeof(int16_t);
return kHeaderSize + sum;
}
uint8_t *BaseTxn::BaseTxnIndexOpContext::EncodeTo(uint8_t *buf) const
{
memcpy(buf, this, kHeaderSize);
uint8_t *p = buf + kHeaderSize;
int nr_keys = __builtin_popcount(keys_bitmap);
int nr_slices = __builtin_popcount(slices_bitmap);
int nr_rels = __builtin_popcount(rels_bitmap);
for (auto i = 0; i < nr_keys; i++) {
memcpy(p, &key_len[i], 2);
memcpy(p + 2, key_data[i], key_len[i]);
p += 2 + key_len[i];
}
memcpy(p, slice_ids, nr_slices * sizeof(int16_t));
p += nr_slices * sizeof(int16_t);
memcpy(p, relation_ids, nr_rels * sizeof(int16_t));
p += nr_rels * sizeof(int16_t);
return p;
}
const uint8_t *BaseTxn::BaseTxnIndexOpContext::DecodeFrom(const uint8_t *buf)
{
memcpy(this, buf, kHeaderSize);
const uint8_t *p = buf + kHeaderSize;
int nr_keys = __builtin_popcount(keys_bitmap);
int nr_slices = __builtin_popcount(slices_bitmap);
int nr_rels = __builtin_popcount(rels_bitmap);
for (auto i = 0; i < nr_keys; i++) {
memcpy(&key_len[i], p, 2);
key_data[i] = (uint8_t *) p + 2;
p += 2 + key_len[i];
}
memcpy(slice_ids, p, nr_slices * sizeof(int16_t));
p += nr_slices * sizeof(int16_t);
memcpy(relation_ids, p, nr_rels * sizeof(int16_t));
p += nr_rels * sizeof(int16_t);
return p;
}
BaseTxn::LookupRowResult BaseTxn::BaseTxnIndexOpLookup(const BaseTxnIndexOpContext &ctx, int idx)
{
auto tbl = util::Instance<TableManager>().GetTable(ctx.relation_ids[idx]);
BaseTxn::LookupRowResult result;
result.fill(nullptr);
if (ctx.slice_ids[idx] >= 0 || ctx.slice_ids[idx] == kReadOnlySliceId) {
VarStrView key(ctx.key_len[idx], ctx.key_data[idx]);
auto handle = tbl->Search(key);
result[0] = handle;
} else if (ctx.slice_ids[idx] == -1) {
VarStrView range_start(ctx.key_len[idx], ctx.key_data[idx]);
VarStrView range_end(ctx.key_len[idx + 1], ctx.key_data[idx + 1]);
// auto &table = mgr[ctx.relation_ids[idx]];
int i = 0;
// We need the scoped data for the iterators. Since we don't store the
// iterators, we can allocate on the stack.
auto r = go::Scheduler::Current()->current_routine();
auto *olddata = r->userdata();
if (olddata == nullptr) {
void *buf = alloca(512);
r->set_userdata(mem::Brk::New(buf, 512));
}
for (auto it = tbl->IndexSearchIterator(range_start, range_end);
it->IsValid(); it->Next(), i++) {
result[i] = it->row();
}
r->set_userdata(olddata);
}
return result;
}
VHandle *BaseTxn::BaseTxnIndexOpInsert(const BaseTxnIndexOpContext &ctx, int idx)
{
auto tbl = util::Instance<TableManager>().GetTable(ctx.relation_ids[idx]);
abort_if(ctx.keys_bitmap != ctx.slices_bitmap,
"InsertOp should have same number of keys and values. bitmap {} != {}",
ctx.keys_bitmap, ctx.slices_bitmap);
VarStrView key(ctx.key_len[idx], ctx.key_data[idx]);
bool created = false;
VHandle *result = tbl->SearchOrCreate(key, &created);
if (created) {
VarStr *kstr = VarStr::New(ctx.key_len[idx]);
memcpy((void *) kstr->data(), ctx.key_data[idx], ctx.key_len[idx]);
util::Instance<felis::SliceManager>().OnNewRow(
ctx.slice_ids[idx], ctx.relation_ids[idx], kstr, result);
}
return result;
}
void BaseFutureValue::Signal()
{
ready = true;
}
//TODO: Consider unifying spin code
void BaseFutureValue::Wait()
{
long wait_cnt = 0;
//int preempt_times = 0;
int core_id = go::Scheduler::CurrentThreadPoolId() - 1;
auto &transport = util::Impl<PromiseRoutineTransportService>();
while (!ready) {
wait_cnt++;
if ((wait_cnt & 0x3F) == 0) {
transport.PeriodicIO(core_id);
}
//preempt_times++;
auto routine = go::Scheduler::Current()->current_routine();
bool preempted;
if (Options::kUseCoroutineScheduler) {
preempted = coro_sched->WaitForFutureValue(this);
} else {
preempted = ((BasePieceCollection::ExecutionRoutine *) routine)->Preempt(0, 0);
}
if (preempted) {
continue;
}
_mm_pause();
}
ready = false;
}
}