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
|
/*
* include/framework/DynamicExtension.h
*
* Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu>
* Dong Xie <dongx@psu.edu>
*
* Distributed under the Modified BSD License.
*
*/
#pragma once
#include <atomic>
#include <cstdio>
#include <vector>
#include <set>
#include <shared_mutex>
#include <mutex>
#include "framework/interface/Scheduler.h"
#include "framework/scheduling/FIFOScheduler.h"
#include "framework/scheduling/SerialScheduler.h"
#include "framework/structure/MutableBuffer.h"
#include "framework/interface/Record.h"
#include "framework/structure/ExtensionStructure.h"
#include "framework/util/Configuration.h"
#include "framework/scheduling/Epoch.h"
namespace de {
template <RecordInterface R, ShardInterface S, QueryInterface Q, LayoutPolicy L=LayoutPolicy::TEIRING,
DeletePolicy D=DeletePolicy::TAGGING, SchedulerInterface SCHED=SerialScheduler>
class DynamicExtension {
typedef S Shard;
typedef MutableBuffer<R> Buffer;
typedef ExtensionStructure<R, S, Q, L> Structure;
typedef Epoch<R, S, Q, L> _Epoch;
typedef BufferView<R> BufView;
static constexpr size_t QUERY = 1;
static constexpr size_t RECONSTRUCTION = 2;
struct epoch_ptr {
_Epoch *epoch;
size_t refcnt;
};
public:
DynamicExtension(size_t buffer_lwm, size_t buffer_hwm, size_t scale_factor, size_t memory_budget=0,
size_t thread_cnt=16)
: m_scale_factor(scale_factor)
, m_max_delete_prop(1)
, m_sched(memory_budget, thread_cnt)
, m_buffer(new Buffer(buffer_lwm, buffer_hwm))
, m_core_cnt(thread_cnt)
, m_next_core(0)
, m_epoch_cnt(0)
{
auto vers = new Structure(buffer_hwm, m_scale_factor, m_max_delete_prop);
m_current_epoch.store({new _Epoch(0, vers, m_buffer, 0), 0});
m_previous_epoch.store({nullptr, 0});
m_next_epoch.store({nullptr, 0});
m_versions.insert(vers);
}
~DynamicExtension() {
/* let any in-flight epoch transition finish */
await_next_epoch();
/* shutdown the scheduler */
m_sched.shutdown();
/* delete all held resources */
delete m_next_epoch.load().epoch;
delete m_current_epoch.load().epoch;
delete m_previous_epoch.load().epoch;
delete m_buffer;
for (auto e : m_versions) {
delete e;
}
}
int insert(const R &rec) {
return internal_append(rec, false);
}
int erase(const R &rec) {
// FIXME: delete tagging will require a lot of extra work to get
// operating "correctly" in a concurrent environment.
/*
* Get a view on the buffer *first*. This will ensure a stronger
* ordering than simply accessing the buffer directly, but is
* not *strictly* necessary.
*/
if constexpr (D == DeletePolicy::TAGGING) {
auto view = m_buffer->get_buffer_view();
static_assert(std::same_as<SCHED, SerialScheduler>, "Tagging is only supported in single-threaded operation");
if (get_active_epoch()->get_structure()->tagged_delete(rec)) {
return 1;
}
/*
* the buffer will take the longest amount of time, and
* probably has the lowest probability of having the record,
* so we'll check it last.
*/
return view.delete_record(rec);
}
/*
* If tagging isn't used, then delete using a tombstone
*/
return internal_append(rec, true);
}
std::future<std::vector<R>> query(void *parms) {
return schedule_query(parms);
}
size_t get_record_count() {
auto epoch = get_active_epoch();
auto t = epoch->get_buffer().get_record_count() + epoch->get_structure()->get_record_count();
end_job(epoch);
return t;
}
size_t get_tombstone_count() {
auto epoch = get_active_epoch();
auto t = epoch->get_buffer().get_tombstone_count() + epoch->get_structure()->get_tombstone_count();
end_job(epoch);
return t;
}
size_t get_height() {
auto epoch = get_active_epoch();
auto t = epoch->get_structure()->get_height();
end_job(epoch);
return t;
}
size_t get_memory_usage() {
auto epoch = get_active_epoch();
auto t= epoch->get_buffer().get_memory_usage() + epoch->get_structure()->get_memory_usage();
end_job(epoch);
return t;
}
size_t get_aux_memory_usage() {
auto epoch = get_active_epoch();
auto t = epoch->get_buffer().get_aux_memory_usage() + epoch->get_structure()->get_aux_memory_usage();
end_job(epoch);
return t;
}
size_t get_buffer_capacity() {
return m_buffer->get_capacity();
}
Shard *create_static_structure(bool await_reconstruction_completion=false) {
if (await_reconstruction_completion) {
await_next_epoch();
}
auto epoch = get_active_epoch();
auto vers = epoch->get_structure();
std::vector<Shard *> shards;
if (vers->get_levels().size() > 0) {
for (int i=vers->get_levels().size() - 1; i>= 0; i--) {
if (vers->get_levels()[i] && vers->get_levels()[i]->get_record_count() > 0) {
shards.emplace_back(vers->get_levels()[i]->get_combined_shard());
}
}
}
/*
* construct a shard from the buffer view. We'll hold the view
* for as short a time as possible: once the records are exfiltrated
* from the buffer, there's no reason to retain a hold on the view's
* head pointer any longer
*/
{
auto bv = epoch->get_buffer();
if (bv.get_record_count() > 0) {
shards.emplace_back(new S(std::move(bv)));
}
}
Shard *flattened = new S(shards);
for (auto shard : shards) {
delete shard;
}
end_job(epoch);
return flattened;
}
/*
* If the current epoch is *not* the newest one, then wait for
* the newest one to become available. Otherwise, returns immediately.
*/
void await_next_epoch() {
while (m_next_epoch.load().epoch != nullptr) {
std::unique_lock<std::mutex> lk(m_epoch_cv_lk);
m_epoch_cv.wait(lk);
}
}
/*
* Mostly exposed for unit-testing purposes. Verifies that the current
* active version of the ExtensionStructure doesn't violate the maximum
* tombstone proportion invariant.
*/
bool validate_tombstone_proportion() {
auto epoch = get_active_epoch();
auto t = epoch->get_structure()->validate_tombstone_proportion();
end_job(epoch);
return t;
}
void print_scheduler_statistics() {
m_sched.print_statistics();
}
private:
SCHED m_sched;
Buffer *m_buffer;
std::mutex m_struct_lock;
std::set<Structure *> m_versions;
alignas(64) std::atomic<bool> m_reconstruction_scheduled;
std::atomic<epoch_ptr> m_next_epoch;
std::atomic<epoch_ptr> m_current_epoch;
std::atomic<epoch_ptr> m_previous_epoch;
std::condition_variable m_epoch_cv;
std::mutex m_epoch_cv_lk;
std::atomic<size_t> m_epoch_cnt;
size_t m_scale_factor;
double m_max_delete_prop;
std::atomic<int> m_next_core;
size_t m_core_cnt;
void enforce_delete_invariant(_Epoch *epoch) {
auto structure = epoch->get_structure();
auto compactions = structure->get_compaction_tasks();
while (compactions.size() > 0) {
/* schedule a compaction */
ReconstructionArgs<R, S, Q, L> *args = new ReconstructionArgs<R, S, Q, L>();
args->epoch = epoch;
args->merges = compactions;
args->extension = this;
args->compaction = true;
/* NOTE: args is deleted by the reconstruction job, so shouldn't be freed here */
auto wait = args->result.get_future();
m_sched.schedule_job(reconstruction, 0, args, RECONSTRUCTION);
/* wait for compaction completion */
wait.get();
/* get a new batch of compactions to perform, if needed */
compactions = structure->get_compaction_tasks();
}
}
_Epoch *get_active_epoch() {
epoch_ptr old, new_ptr;
do {
if (m_current_epoch.load().epoch == nullptr) {
old = m_previous_epoch;
new_ptr = {old.epoch, old.refcnt+1};
if (old.epoch != nullptr && m_previous_epoch.compare_exchange_strong(old, new_ptr)) {
break;
}
} else {
old = m_current_epoch;
new_ptr = {old.epoch, old.refcnt+1};
if (old.epoch != nullptr && m_current_epoch.compare_exchange_strong(old, new_ptr)) {
break;
}
}
} while (true);
return new_ptr.epoch;
}
void advance_epoch(size_t buffer_head) {
retire_epoch(m_previous_epoch.load().epoch);
epoch_ptr tmp = {nullptr, 0};
epoch_ptr cur;
do {
cur = m_current_epoch;
} while(!m_current_epoch.compare_exchange_strong(cur, tmp));
m_previous_epoch.store(cur);
// FIXME: this may currently block because there isn't any
// query preemption yet. At this point, we'd need to either
// 1) wait for all queries on the old_head to finish
// 2) kill all queries on the old_head
// 3) somehow migrate all queries on the old_head to the new
// version
while (!m_next_epoch.load().epoch->advance_buffer_head(buffer_head)) {
_mm_pause();
}
m_current_epoch.store(m_next_epoch);
m_next_epoch.store({nullptr, 0});
/* notify any blocking threads that the new epoch is available */
m_epoch_cv_lk.lock();
m_epoch_cv.notify_all();
m_epoch_cv_lk.unlock();
}
/*
* Creates a new epoch by copying the currently active one. The new epoch's
* structure will be a shallow copy of the old one's.
*/
_Epoch *create_new_epoch() {
/*
* This epoch access is _not_ protected under the assumption that
* only one reconstruction will be able to trigger at a time. If that condition
* is violated, it is possible that this code will clone a retired
* epoch.
*/
assert(m_next_epoch.load().epoch == nullptr);
auto current_epoch = get_active_epoch();
m_epoch_cnt.fetch_add(1);
m_next_epoch.store({current_epoch->clone(m_epoch_cnt.load()), 0});
end_job(current_epoch);
std::unique_lock<std::mutex> m_struct_lock;
m_versions.insert(m_next_epoch.load().epoch->get_structure());
m_struct_lock.release();
return m_next_epoch.load().epoch;
}
void retire_epoch(_Epoch *epoch) {
/*
* Epochs with currently active jobs cannot
* be retired. By the time retire_epoch is called,
* it is assumed that a new epoch is active, meaning
* that the epoch to be retired should no longer
* accumulate new active jobs. Eventually, this
* number will hit zero and the function will
* proceed.
*/
if (epoch == nullptr) {
return;
}
epoch_ptr old, new_ptr;
new_ptr = {nullptr, 0};
do {
old = m_previous_epoch.load();
if (old.epoch == epoch && old.refcnt == 0 &&
m_previous_epoch.compare_exchange_strong(old, new_ptr)) {
break;
}
usleep(1);
} while(true);
//fprintf(stderr, "Epoch %ld retired [%p]\n", epoch->get_epoch_number(), epoch);
delete epoch;
/*
* Following the epoch's destruction, any buffers
* or structures with no remaining references can
* be safely freed.
*/
std::unique_lock<std::mutex> lock(m_struct_lock);
for (auto itr = m_versions.begin(); itr != m_versions.end();) {
if ((*itr)->get_reference_count() == 0) {
auto tmp = *itr;
itr = m_versions.erase(itr);
delete tmp;
} else {
itr++;
}
}
}
static void reconstruction(void *arguments) {
auto args = (ReconstructionArgs<R, S, Q, L> *) arguments;
((DynamicExtension *) args->extension)->SetThreadAffinity();
Structure *vers = args->epoch->get_structure();
for (ssize_t i=0; i<args->merges.size(); i++) {
vers->reconstruction(args->merges[i].second, args->merges[i].first);
}
/*
* we'll grab the buffer AFTER doing the internal reconstruction, so we
* can flush as many records as possible in one go. The reconstruction
* was done so as to make room for the full buffer anyway, so there's
* no real benefit to doing this first.
*/
auto buffer_view = args->epoch->get_buffer();
size_t new_head = buffer_view.get_tail();
/*
* if performing a compaction, don't flush the buffer, as
* there is no guarantee that any necessary reconstructions
* will free sufficient space in L0 to support a flush
*/
if (!args->compaction) {
vers->flush_buffer(std::move(buffer_view));
}
args->result.set_value(true);
/*
* Compactions occur on an epoch _before_ it becomes active,
* and as a result the active epoch should _not_ be advanced as
* part of a compaction
*/
if (!args->compaction) {
((DynamicExtension *) args->extension)->advance_epoch(new_head);
}
((DynamicExtension *) args->extension)->m_reconstruction_scheduled.store(false);
delete args;
}
static void async_query(void *arguments) {
QueryArgs<R, S, Q, L> *args = (QueryArgs<R, S, Q, L> *) arguments;
auto epoch = ((DynamicExtension *) args->extension)->get_active_epoch();
auto ptr1 = ((DynamicExtension *) args->extension)->m_previous_epoch.load().epoch;
auto ptr2 = ((DynamicExtension *) args->extension)->m_current_epoch.load().epoch;
auto ptr3 = ((DynamicExtension *) args->extension)->m_next_epoch.load().epoch;
//fprintf(stderr, "(%ld, %p)\t%p\t%p\t%p\n", epoch->get_epoch_number(), epoch, ptr1, ptr2, ptr3);
auto buffer = epoch->get_buffer();
auto vers = epoch->get_structure();
void *parms = args->query_parms;
/* Get the buffer query states */
void *buffer_state = Q::get_buffer_query_state(std::move(buffer), parms);
/* Get the shard query states */
std::vector<std::pair<ShardID, Shard*>> shards;
std::vector<void *> states = vers->get_query_states(shards, parms);
Q::process_query_states(parms, states, buffer_state);
std::vector<std::vector<Wrapped<R>>> query_results(shards.size() + 1);
for (size_t i=0; i<query_results.size(); i++) {
std::vector<Wrapped<R>> local_results;
ShardID shid;
if (i == 0) { /* process the buffer first */
local_results = Q::buffer_query(buffer_state, parms);
shid = INVALID_SHID;
} else {
local_results = Q::query(shards[i - 1].second, states[i - 1], parms);
shid = shards[i - 1].first;
}
query_results[i] = std::move(filter_deletes(local_results, shid, vers));
if constexpr (Q::EARLY_ABORT) {
if (query_results[i].size() > 0) break;
}
}
auto result = Q::merge(query_results, parms);
args->result_set.set_value(std::move(result));
((DynamicExtension *) args->extension)->end_job(epoch);
Q::delete_buffer_query_state(buffer_state);
for (size_t i=0; i<states.size(); i++) {
Q::delete_query_state(states[i]);
}
delete args;
}
void schedule_reconstruction() {
auto epoch = create_new_epoch();
/*
* the reconstruction process calls end_job(),
* so we must start one before calling it
*/
ReconstructionArgs<R, S, Q, L> *args = new ReconstructionArgs<R, S, Q, L>();
args->epoch = epoch;
args->merges = epoch->get_structure()->get_reconstruction_tasks(m_buffer->get_high_watermark());
args->extension = this;
args->compaction = false;
/* NOTE: args is deleted by the reconstruction job, so shouldn't be freed here */
m_sched.schedule_job(reconstruction, 0, args, RECONSTRUCTION);
}
std::future<std::vector<R>> schedule_query(void *query_parms) {
QueryArgs<R, S, Q, L> *args = new QueryArgs<R, S, Q, L>();
args->extension = this;
args->query_parms = query_parms;
auto result = args->result_set.get_future();
m_sched.schedule_job(async_query, 0, args, QUERY);
return result;
}
int internal_append(const R &rec, bool ts) {
if (m_buffer->is_at_low_watermark()) {
auto old = false;
if (m_reconstruction_scheduled.compare_exchange_strong(old, true)) {
schedule_reconstruction();
}
}
/* this will fail if the HWM is reached and return 0 */
return m_buffer->append(rec, ts);
}
static std::vector<Wrapped<R>> filter_deletes(std::vector<Wrapped<R>> &records, ShardID shid, Structure *vers) {
if constexpr (!Q::SKIP_DELETE_FILTER) {
return records;
}
std::vector<Wrapped<R>> processed_records;
processed_records.reserve(records.size());
/*
* For delete tagging, we just need to check the delete bit
* on each record.
*/
if constexpr (D == DeletePolicy::TAGGING) {
for (auto &rec : records) {
if (rec.is_deleted()) {
continue;
}
processed_records.emplace_back(rec);
}
return processed_records;
}
/*
* For tombstone deletes, we need to search for the corresponding
* tombstone for each record.
*/
for (auto &rec : records) {
if (rec.is_tombstone()) {
continue;
}
// FIXME: need to figure out how best to re-enable the buffer tombstone
// check in the correct manner.
//if (buffview.check_tombstone(rec.rec)) {
//continue;
//}
if (shid != INVALID_SHID) {
for (size_t lvl=0; lvl<=shid.level_idx; lvl++) {
if (vers->get_levels()[lvl]->check_tombstone(0, rec.rec)) {
continue;
}
}
if (vers->get_levels()[shid.level_idx]->check_tombstone(shid.shard_idx + 1, rec.rec)) {
continue;
}
}
processed_records.emplace_back(rec);
}
return processed_records;
}
void SetThreadAffinity() {
int core = m_next_core.fetch_add(1) % m_core_cnt;
cpu_set_t mask;
CPU_ZERO(&mask);
switch (core % 2) {
case 0:
// 0 |-> 0
// 2 |-> 2
// 4 |-> 4
core = core;
break;
case 1:
// 1 |-> 28
// 3 |-> 30
// 5 |-> 32
core = (core - 1) + m_core_cnt;
break;
}
CPU_SET(core, &mask);
::sched_setaffinity(0, sizeof(mask), &mask);
}
void end_job(_Epoch *epoch) {
epoch_ptr old, new_ptr;
do {
if (m_previous_epoch.load().epoch == epoch) {
old = m_previous_epoch;
assert(old.refcnt > 0);
new_ptr = {old.epoch, old.refcnt - 1};
if (m_previous_epoch.compare_exchange_strong(old, new_ptr)) {
break;
}
} else {
old = m_current_epoch;
assert(old.refcnt > 0);
new_ptr = {old.epoch, old.refcnt - 1};
if (m_current_epoch.compare_exchange_strong(old, new_ptr)) {
break;
}
}
} while (true);
}
};
}
|