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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <iostream>
#include <boost/iterator/counting_iterator.hpp>
#include "include/intarith.h"
#include "segmented_journal.h"
#include "crimson/common/config_proxy.h"
#include "crimson/os/seastore/logging.h"
SET_SUBSYS(seastore_journal);
/*
* format:
* - H<handle-addr> information
*
* levels:
* - INFO: major initiation, closing, rolling and replay operations
* - DEBUG: INFO details, major submit operations
* - TRACE: DEBUG details
*/
namespace crimson::os::seastore::journal {
SegmentedJournal::SegmentedJournal(
SegmentProvider &segment_provider,
JournalTrimmer &trimmer)
: segment_seq_allocator(
new SegmentSeqAllocator(segment_type_t::JOURNAL)),
journal_segment_allocator(&trimmer,
data_category_t::METADATA,
INLINE_GENERATION,
segment_provider,
*segment_seq_allocator),
record_submitter(crimson::common::get_conf<uint64_t>(
"seastore_journal_iodepth_limit"),
crimson::common::get_conf<uint64_t>(
"seastore_journal_batch_capacity"),
crimson::common::get_conf<Option::size_t>(
"seastore_journal_batch_flush_size"),
crimson::common::get_conf<double>(
"seastore_journal_batch_preferred_fullness"),
journal_segment_allocator),
sm_group(*segment_provider.get_segment_manager_group()),
trimmer{trimmer}
{
}
SegmentedJournal::open_for_mkfs_ret
SegmentedJournal::open_for_mkfs()
{
return record_submitter.open(true);
}
SegmentedJournal::open_for_mount_ret
SegmentedJournal::open_for_mount()
{
return record_submitter.open(false);
}
SegmentedJournal::close_ertr::future<> SegmentedJournal::close()
{
LOG_PREFIX(Journal::close);
INFO("closing, committed_to={}",
record_submitter.get_committed_to());
return record_submitter.close();
}
SegmentedJournal::prep_replay_segments_fut
SegmentedJournal::prep_replay_segments(
std::vector<std::pair<segment_id_t, segment_header_t>> segments)
{
LOG_PREFIX(Journal::prep_replay_segments);
if (segments.empty()) {
ERROR("no journal segments for replay");
return crimson::ct_error::input_output_error::make();
}
std::sort(
segments.begin(),
segments.end(),
[](const auto <, const auto &rt) {
return lt.second.segment_seq <
rt.second.segment_seq;
});
segment_seq_allocator->set_next_segment_seq(
segments.rbegin()->second.segment_seq + 1);
std::for_each(
segments.begin(),
segments.end(),
[FNAME](auto &seg)
{
if (seg.first != seg.second.physical_segment_id ||
seg.second.get_type() != segment_type_t::JOURNAL) {
ERROR("illegal journal segment for replay -- {}", seg.second);
ceph_abort();
}
});
auto last_segment_id = segments.rbegin()->first;
auto last_header = segments.rbegin()->second;
return scan_last_segment(last_segment_id, last_header
).safe_then([this, FNAME, segments=std::move(segments)] {
INFO("dirty_tail={}, alloc_tail={}",
trimmer.get_dirty_tail(),
trimmer.get_alloc_tail());
auto journal_tail = trimmer.get_journal_tail();
auto journal_tail_paddr = journal_tail.offset;
ceph_assert(journal_tail != JOURNAL_SEQ_NULL);
ceph_assert(journal_tail_paddr != P_ADDR_NULL);
auto from = std::find_if(
segments.begin(),
segments.end(),
[&journal_tail_paddr](const auto &seg) -> bool {
auto& seg_addr = journal_tail_paddr.as_seg_paddr();
return seg.first == seg_addr.get_segment_id();
});
if (from->second.segment_seq != journal_tail.segment_seq) {
ERROR("journal_tail {} does not match {}",
journal_tail, from->second);
ceph_abort();
}
auto num_segments = segments.end() - from;
INFO("{} segments to replay", num_segments);
auto ret = replay_segments_t(num_segments);
std::transform(
from, segments.end(), ret.begin(),
[this](const auto &p) {
auto ret = journal_seq_t{
p.second.segment_seq,
paddr_t::make_seg_paddr(
p.first,
sm_group.get_block_size())
};
return std::make_pair(ret, p.second);
});
ret[0].first.offset = journal_tail_paddr;
return prep_replay_segments_fut(
replay_ertr::ready_future_marker{},
std::move(ret));
});
}
SegmentedJournal::scan_last_segment_ertr::future<>
SegmentedJournal::scan_last_segment(
const segment_id_t &segment_id,
const segment_header_t &segment_header)
{
LOG_PREFIX(SegmentedJournal::scan_last_segment);
assert(segment_id == segment_header.physical_segment_id);
trimmer.update_journal_tails(
segment_header.dirty_tail, segment_header.alloc_tail);
auto seq = journal_seq_t{
segment_header.segment_seq,
paddr_t::make_seg_paddr(segment_id, 0)
};
INFO("scanning journal tail deltas -- {}", segment_header);
return seastar::do_with(
scan_valid_records_cursor(seq),
SegmentManagerGroup::found_record_handler_t(
[FNAME, this](
record_locator_t locator,
const record_group_header_t& record_group_header,
const bufferlist& mdbuf
) -> SegmentManagerGroup::scan_valid_records_ertr::future<>
{
DEBUG("decoding {} at {}", record_group_header, locator);
bool has_tail_delta = false;
auto maybe_headers = try_decode_record_headers(
record_group_header, mdbuf);
if (!maybe_headers) {
// This should be impossible, we did check the crc on the mdbuf
ERROR("unable to decode headers from {} at {}",
record_group_header, locator);
ceph_abort();
}
for (auto &record_header : *maybe_headers) {
ceph_assert(is_valid_transaction(record_header.type));
if (is_background_transaction(record_header.type)) {
has_tail_delta = true;
}
}
if (has_tail_delta) {
bool found_delta = false;
auto maybe_record_deltas_list = try_decode_deltas(
record_group_header, mdbuf, locator.record_block_base);
if (!maybe_record_deltas_list) {
ERROR("unable to decode deltas from {} at {}",
record_group_header, locator);
ceph_abort();
}
for (auto &record_deltas : *maybe_record_deltas_list) {
for (auto &[ctime, delta] : record_deltas.deltas) {
if (delta.type == extent_types_t::JOURNAL_TAIL) {
found_delta = true;
journal_tail_delta_t tail_delta;
decode(tail_delta, delta.bl);
auto start_seq = locator.write_result.start_seq;
DEBUG("got {}, at {}", tail_delta, start_seq);
ceph_assert(tail_delta.dirty_tail != JOURNAL_SEQ_NULL);
ceph_assert(tail_delta.alloc_tail != JOURNAL_SEQ_NULL);
trimmer.update_journal_tails(
tail_delta.dirty_tail, tail_delta.alloc_tail);
}
}
}
ceph_assert(found_delta);
}
return seastar::now();
}),
[this, nonce=segment_header.segment_nonce](auto &cursor, auto &handler)
{
return sm_group.scan_valid_records(
cursor,
nonce,
std::numeric_limits<std::size_t>::max(),
handler);
});
}
SegmentedJournal::replay_ertr::future<>
SegmentedJournal::replay_segment(
journal_seq_t seq,
segment_header_t header,
delta_handler_t &handler,
replay_stats_t &stats)
{
LOG_PREFIX(Journal::replay_segment);
INFO("starting at {} -- {}", seq, header);
return seastar::do_with(
scan_valid_records_cursor(seq),
SegmentManagerGroup::found_record_handler_t(
[&handler, this, &stats](
record_locator_t locator,
const record_group_header_t& header,
const bufferlist& mdbuf)
-> SegmentManagerGroup::scan_valid_records_ertr::future<>
{
LOG_PREFIX(Journal::replay_segment);
++stats.num_record_groups;
auto maybe_record_deltas_list = try_decode_deltas(
header, mdbuf, locator.record_block_base);
if (!maybe_record_deltas_list) {
// This should be impossible, we did check the crc on the mdbuf
ERROR("unable to decode deltas for record {} at {}",
header, locator);
return crimson::ct_error::input_output_error::make();
}
return seastar::do_with(
std::move(*maybe_record_deltas_list),
[write_result=locator.write_result,
this,
FNAME,
&handler,
&stats](auto& record_deltas_list)
{
return crimson::do_for_each(
record_deltas_list,
[write_result,
this,
FNAME,
&handler,
&stats](record_deltas_t& record_deltas)
{
++stats.num_records;
auto locator = record_locator_t{
record_deltas.record_block_base,
write_result
};
DEBUG("processing {} deltas at block_base {}",
record_deltas.deltas.size(),
locator);
return crimson::do_for_each(
record_deltas.deltas,
[locator,
this,
&handler,
&stats](auto &p)
{
auto& modify_time = p.first;
auto& delta = p.second;
return handler(
locator,
delta,
trimmer.get_dirty_tail(),
trimmer.get_alloc_tail(),
modify_time
).safe_then([&stats, delta_type=delta.type](auto ret) {
auto [is_applied, ext] = ret;
if (is_applied) {
// see Cache::replay_delta()
assert(delta_type != extent_types_t::JOURNAL_TAIL);
if (delta_type == extent_types_t::ALLOC_INFO) {
++stats.num_alloc_deltas;
} else {
++stats.num_dirty_deltas;
}
}
});
});
});
});
}),
[=, this](auto &cursor, auto &dhandler) {
return sm_group.scan_valid_records(
cursor,
header.segment_nonce,
std::numeric_limits<size_t>::max(),
dhandler
).handle_error(
replay_ertr::pass_further{},
crimson::ct_error::assert_all{
"shouldn't meet with any other error other replay_ertr"
}
);
}
);
}
SegmentedJournal::replay_ret SegmentedJournal::replay(
delta_handler_t &&delta_handler)
{
LOG_PREFIX(Journal::replay);
return sm_group.find_journal_segment_headers(
).safe_then([this, FNAME, delta_handler=std::move(delta_handler)]
(auto &&segment_headers) mutable -> replay_ret {
INFO("got {} segments", segment_headers.size());
return seastar::do_with(
std::move(delta_handler),
replay_segments_t(),
replay_stats_t(),
[this, segment_headers=std::move(segment_headers), FNAME]
(auto &handler, auto &segments, auto &stats) mutable -> replay_ret {
return prep_replay_segments(std::move(segment_headers)
).safe_then([this, &handler, &segments, &stats](auto replay_segs) mutable {
segments = std::move(replay_segs);
return crimson::do_for_each(segments,[this, &handler, &stats](auto i) mutable {
return replay_segment(i.first, i.second, handler, stats);
});
}).safe_then([&stats, FNAME] {
INFO("replay done, record_groups={}, records={}, "
"alloc_deltas={}, dirty_deltas={}",
stats.num_record_groups,
stats.num_records,
stats.num_alloc_deltas,
stats.num_dirty_deltas);
});
});
});
}
seastar::future<> SegmentedJournal::flush(OrderingHandle &handle)
{
LOG_PREFIX(SegmentedJournal::flush);
DEBUG("H{} flush ...", (void*)&handle);
assert(write_pipeline);
return handle.enter(write_pipeline->device_submission
).then([this, &handle] {
return handle.enter(write_pipeline->finalize);
}).then([FNAME, &handle] {
DEBUG("H{} flush done", (void*)&handle);
});
}
SegmentedJournal::submit_record_ret
SegmentedJournal::do_submit_record(
record_t &&record,
OrderingHandle &handle)
{
LOG_PREFIX(SegmentedJournal::do_submit_record);
if (!record_submitter.is_available()) {
DEBUG("H{} wait ...", (void*)&handle);
return record_submitter.wait_available(
).safe_then([this, record=std::move(record), &handle]() mutable {
return do_submit_record(std::move(record), handle);
});
}
auto action = record_submitter.check_action(record.size);
if (action == RecordSubmitter::action_t::ROLL) {
DEBUG("H{} roll, unavailable ...", (void*)&handle);
return record_submitter.roll_segment(
).safe_then([this, record=std::move(record), &handle]() mutable {
return do_submit_record(std::move(record), handle);
});
} else { // SUBMIT_FULL/NOT_FULL
DEBUG("H{} submit {} ...",
(void*)&handle,
action == RecordSubmitter::action_t::SUBMIT_FULL ?
"FULL" : "NOT_FULL");
auto submit_ret = record_submitter.submit(std::move(record));
// submit_ret.record_base_regardless_md is wrong for journaling
return handle.enter(write_pipeline->device_submission
).then([submit_fut=std::move(submit_ret.future)]() mutable {
return std::move(submit_fut);
}).safe_then([FNAME, this, &handle](record_locator_t result) {
return handle.enter(write_pipeline->finalize
).then([FNAME, this, result, &handle] {
DEBUG("H{} finish with {}", (void*)&handle, result);
auto new_committed_to = result.write_result.get_end_seq();
record_submitter.update_committed_to(new_committed_to);
return result;
});
});
}
}
SegmentedJournal::submit_record_ret
SegmentedJournal::submit_record(
record_t &&record,
OrderingHandle &handle)
{
LOG_PREFIX(SegmentedJournal::submit_record);
DEBUG("H{} {} start ...", (void*)&handle, record);
assert(write_pipeline);
auto expected_size = record_group_size_t(
record.size,
journal_segment_allocator.get_block_size()
).get_encoded_length();
auto max_record_length = journal_segment_allocator.get_max_write_length();
if (expected_size > max_record_length) {
ERROR("H{} {} exceeds max record size {}",
(void*)&handle, record, max_record_length);
return crimson::ct_error::erange::make();
}
return do_submit_record(std::move(record), handle);
}
}
|