forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_log.cc
More file actions
322 lines (289 loc) · 9.71 KB
/
Copy pathcommit_log.cc
File metadata and controls
322 lines (289 loc) · 9.71 KB
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
#include "commit_log.hh"
#include "store/checked_file_impl.hh"
#include "store/util/coding.hh"
#include "store/util/crc32c.hh"
#include "store/log_format.hh"
#include "store/priority_manager.hh"
#include "core/align.hh"
#include "core/gate.hh"
namespace store {
class flush_buffer final {
lw_shared_ptr<temporary_buffer<char>> _data;
data_output _output;
size_t _offset;
size_t _written;
uint32_t _touched_counter;
uint32_t _ref;
gate _gate;
public:
flush_buffer()
: _data (nullptr)
, _output(nullptr, size_t(0))
, _offset(0)
, _written(0)
, _touched_counter(0)
{
}
flush_buffer(char* data, size_t size)
: _data(make_lw_shared<temporary_buffer<char>>(data, size, make_free_deleter(data)))
, _output(data, size)
, _offset(0)
, _written(0)
, _touched_counter(0)
{
}
inline void enter() { _gate.enter(); }
inline void leave() { _gate.leave(); }
inline future<> close() { return _gate.close(); }
inline void skip(size_t n) {
_offset += n;
}
inline size_t write(lw_shared_ptr<mutation> m) {
size_t serialized = m->encode_to(_output);
_offset += serialized;
return serialized;
}
inline char* get_current() {
return _data->get_write() + _offset;
}
inline size_t available_size() const {
return _data->size() - _offset;
}
inline bool available() const {
return !!_data;
}
inline size_t payload_size() const {
return _offset;
}
inline bool flushed_all() const {
return _written >= _offset;
}
inline void update_flushed_size(size_t flushed) {
_written += flushed;
}
inline char* data() {
return _data->get_write() + _written;
}
inline size_t size() const {
return _offset - _written;
}
inline void reset() {
_offset = 0;
_written = 0;
_touched_counter = 0;
_gate = {};
}
inline uint32_t touch() {
++_touched_counter;
return _touched_counter;
}
};
static void init_type_crc(uint32_t* type_crc) {
for (int i = 0; i <= MAX_RECORD_TYPE; i++) {
char t = static_cast<char>(i);
type_crc[i] = crc32c::value(&t, 1);
}
}
class commit_log::impl final {
sstring _file_name;
lw_shared_ptr<file> _file;
static const size_t IO_EXTENT_ALLOCATION_SIZE = 32 * 1024 * 1024;
static const size_t FLUSH_BUFFER_SIZE = 1024 * 1024 * 1024;
static const size_t SEGEMENT_SIZE = 32 * 1024;
static const size_t OUTPUT_BUFFER_ALIGNMENT = 4096;
static const uint32_t MAX_FLUSH_BUFFER_SIZE = 32;
static const size_t FLUSH_SIZE_THRESHOLD = FLUSH_BUFFER_SIZE * 80 / 100;
static const uint32_t FLUSH_TOUCH_COUNTER_THRESHOLD = 10;
std::queue<lw_shared_ptr<flush_buffer>> _released_buffers;
std::queue<lw_shared_ptr<flush_buffer>> _pending_buffers;
lw_shared_ptr<flush_buffer> _current_buffer;
size_t _file_offset;
bool _shutdown { false };
using clock_type = lowres_clock;
// periodically flush commitlog buffer to disk
timer<clock_type> _timer;
gate _gate;
using timeout_exception_factory = default_timeout_exception_factory;
basic_semaphore<timeout_exception_factory> _released_semaphore;
basic_semaphore<timeout_exception_factory> _pending_semaphore;
uint32_t type_crc_[MAX_RECORD_TYPE + 1];
public:
explicit impl(sstring fn);
~impl() {}
future<> append(lw_shared_ptr<mutation> entry);
future<> close();
private:
future<> initialize();
future<> make_room_for_apending_mutation(size_t size);
future<> do_flush_one_buffer(lw_shared_ptr<flush_buffer> fb);
lw_shared_ptr<flush_buffer> make_flush_buffer();
void on_timer();
};
commit_log::impl::impl(sstring fn)
: _file_name (std::move(fn))
, _file(nullptr)
, _current_buffer(nullptr)
, _file_offset(0)
, _shutdown(false)
, _released_semaphore(MAX_FLUSH_BUFFER_SIZE)
, _pending_semaphore(0)
{
init_type_crc(type_crc_);
}
future<> commit_log::impl::do_flush_one_buffer(lw_shared_ptr<flush_buffer> fb)
{
// flush buffer to file
assert(_file);
return repeat([this, fb] () mutable {
auto data = fb->data();
auto size = align_up<size_t>(fb->size(), OUTPUT_BUFFER_ALIGNMENT);
std::fill(data + fb->size(), data + size, 0);
auto&& priority_class = get_local_commitlog_priority();
return _file->dma_write(_file_offset, data, size, priority_class).then([this, fb] (auto s) {
fb->update_flushed_size(s);
s = align_down<size_t>(s, OUTPUT_BUFFER_ALIGNMENT);
_file_offset += s;
if (fb->flushed_all()) {
return make_ready_future<stop_iteration>(stop_iteration::yes);
}
return make_ready_future<stop_iteration>(stop_iteration::no);
});
}).then ([this] {
return _file->flush().then([] {
return make_ready_future<>();
});
});
}
void commit_log::impl::on_timer()
{
if (_current_buffer) {
_current_buffer->touch();
if ( _current_buffer->payload_size() > FLUSH_SIZE_THRESHOLD || _current_buffer->touch() > FLUSH_TOUCH_COUNTER_THRESHOLD) {
_current_buffer->close().then([this] {
_pending_buffers.emplace(_current_buffer);
_pending_semaphore.signal();
_current_buffer = nullptr;
});
}
}
if (!_shutdown) {
_timer.arm(std::chrono::milliseconds(8000));
}
}
future<> commit_log::impl::initialize()
{
repeat([this] {
return _pending_semaphore.wait().then([this] {
auto b = _pending_buffers.front();
_pending_buffers.pop();
return do_flush_one_buffer(b).then([this, released_buffer = b] {
released_buffer->reset();
_released_buffers.emplace(released_buffer);
_released_semaphore.signal();
auto should_stop = _pending_buffers.empty() && _shutdown;
return make_ready_future<stop_iteration>(should_stop ? stop_iteration::yes : stop_iteration::no);
});
});
});
_timer.set_callback(std::bind(&commit_log::impl::on_timer, this));
_timer.arm(std::chrono::milliseconds(8000));
file_open_options opt;
opt.extent_allocation_size_hint = IO_EXTENT_ALLOCATION_SIZE;
return open_checked_file_dma(commit_error_handler, _file_name, open_flags::wo | open_flags::create, opt).then([this](file f) {
// xfs doesn't like files extended betond eof, so enlarge the file
_file = make_lw_shared<file>(std::move(f));
return _released_semaphore.wait().then([this] {
_current_buffer = this->make_flush_buffer();
return make_ready_future<>();
});
});
}
lw_shared_ptr<flush_buffer> commit_log::impl::make_flush_buffer()
{
auto b = ::memalign(OUTPUT_BUFFER_ALIGNMENT, FLUSH_BUFFER_SIZE);
// OUTPUT_BUFFER_ALIGNMENT, FLUSH_BUFFER_SIZE);
if (!b) {
throw std::bad_alloc();
}
auto s = FLUSH_BUFFER_SIZE;
return make_lw_shared<flush_buffer>(reinterpret_cast<char *>(b), s);
}
future<> commit_log::impl::make_room_for_apending_mutation(size_t size)
{
if (!_current_buffer || _current_buffer->available_size() < size) {
if (_current_buffer) {
_current_buffer->close();
_pending_buffers.emplace(_current_buffer);
_pending_semaphore.signal();
}
return _released_semaphore.wait().then([this] {
if (!_released_buffers.empty()) {
_current_buffer = _released_buffers.front();
_released_buffers.pop();
}
else {
_current_buffer = make_flush_buffer();
}
return make_ready_future<>();
});
}
return make_ready_future<>();
}
future<> commit_log::impl::append(lw_shared_ptr<mutation> m)
{
if (!_file) {
return initialize().then([this, m = std::move(m)] {
return this->append(m);
});
}
_gate.enter();
auto estimated_size = m->estimate_serialized_size() + HEADER_SIZE;
return make_room_for_apending_mutation(estimated_size).then([this, m, estimated_size] {
_current_buffer->enter();
// Format the header
char* header = _current_buffer->get_current();
// Fill the header later, because we need to compute the CRC32 of mutation.
_current_buffer->skip(HEADER_SIZE);
auto record = _current_buffer->get_current();
auto serialized_size = _current_buffer->write(m);
uint32_t crc = crc32c::extend(type_crc_[record_type::full], record, serialized_size);
crc = crc32c::mask(crc);
// [0-3] 4bytes for crc
encode_fixed32(header, crc);
header[4] = static_cast<char>(serialized_size & 0xff);
header[5] = static_cast<char>(serialized_size >> 8);
header[6] = static_cast<char>(record_type::full);
}).then([this] {
_current_buffer->leave();
_gate.leave();
return make_ready_future<>();
});
}
future<> commit_log::impl::close()
{
return _gate.close().then([this] {
// flush all pending buffers to disk;
});
}
commit_log::commit_log(std::unique_ptr<impl> i) : _impl(std::move(i))
{
}
commit_log::~commit_log()
{
}
future<> commit_log::append(lw_shared_ptr<mutation> m)
{
return _impl->append(m);
}
future<> commit_log::close()
{
return _impl->close();
}
lw_shared_ptr<commit_log> make_commit_log()
{
using clk = std::chrono::system_clock;
auto timestap = clk::now().time_since_epoch().count();
sstring filename = "commitlog-" + to_sstring(engine().cpu_id()) + "-" + to_sstring(timestap) + ".log";
return make_lw_shared<commit_log>(std::make_unique<commit_log::impl>(std::move(filename)));
}
}