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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
#include <iostream>
#include <boost/intrusive/list.hpp>
#include "crimson/common/log.h"
#include "crimson/os/seastore/logging.h"
#include "crimson/os/seastore/ordering_handle.h"
#include "crimson/os/seastore/seastore_types.h"
#include "crimson/os/seastore/cached_extent.h"
#include "crimson/os/seastore/root_block.h"
namespace crimson::os::seastore {
class SeaStore;
class Transaction;
struct io_stat_t {
uint64_t num = 0;
uint64_t bytes = 0;
bool is_clear() const {
return (num == 0 && bytes == 0);
}
void increment(uint64_t _bytes) {
++num;
bytes += _bytes;
}
void increment_stat(const io_stat_t& stat) {
num += stat.num;
bytes += stat.bytes;
}
};
inline std::ostream& operator<<(std::ostream& out, const io_stat_t& stat) {
return out << stat.num << "(" << stat.bytes << "B)";
}
/**
* Transaction
*
* Representation of in-progress mutation. Used exclusively through Cache methods.
*/
class Transaction {
public:
using Ref = std::unique_ptr<Transaction>;
using on_destruct_func_t = std::function<void(Transaction&)>;
enum class get_extent_ret {
PRESENT,
ABSENT,
RETIRED
};
get_extent_ret get_extent(paddr_t addr, CachedExtentRef *out) {
LOG_PREFIX(Transaction::get_extent);
if (retired_set.count(addr)) {
return get_extent_ret::RETIRED;
} else if (auto iter = write_set.find_offset(addr);
iter != write_set.end()) {
if (out)
*out = CachedExtentRef(&*iter);
SUBTRACET(seastore_tm, "Found offset {} in write_set: {}", *this, addr, *iter);
return get_extent_ret::PRESENT;
} else if (
auto iter = read_set.find(addr);
iter != read_set.end()) {
// placeholder in read-set should be in the retired-set
// at the same time.
assert(iter->ref->get_type() != extent_types_t::RETIRED_PLACEHOLDER);
if (out)
*out = iter->ref;
SUBTRACET(seastore_tm, "Found offset {} in read_set: {}", *this, addr, *(iter->ref));
return get_extent_ret::PRESENT;
} else {
return get_extent_ret::ABSENT;
}
}
void add_to_retired_set(CachedExtentRef ref) {
ceph_assert(!is_weak());
if (ref->is_initial_pending()) {
ref->state = CachedExtent::extent_state_t::INVALID;
write_set.erase(*ref);
} else if (ref->is_mutation_pending()) {
ref->state = CachedExtent::extent_state_t::INVALID;
write_set.erase(*ref);
assert(ref->prior_instance);
retired_set.insert(ref->prior_instance);
assert(read_set.count(ref->prior_instance->get_paddr()));
ref->prior_instance.reset();
} else {
// && retired_set.count(ref->get_paddr()) == 0
// If it's already in the set, insert here will be a noop,
// which is what we want.
retired_set.insert(ref);
}
}
void add_to_read_set(CachedExtentRef ref) {
if (is_weak()) return;
auto [iter, inserted] = read_set.emplace(this, ref);
ceph_assert(inserted);
}
void add_fresh_extent(
CachedExtentRef ref,
bool delayed = false) {
LOG_PREFIX(Transaction::add_fresh_extent);
ceph_assert(!is_weak());
if (delayed) {
assert(ref->is_logical());
ref->set_paddr(delayed_temp_paddr(delayed_temp_offset));
delayed_temp_offset += ref->get_length();
delayed_alloc_list.emplace_back(ref->cast<LogicalCachedExtent>());
} else {
ref->set_paddr(make_record_relative_paddr(offset));
offset += ref->get_length();
inline_block_list.push_back(ref);
}
fresh_block_stats.increment(ref->get_length());
SUBTRACET(seastore_tm, "adding {} to write_set", *this, *ref);
write_set.insert(*ref);
}
void mark_delayed_extent_inline(LogicalCachedExtentRef& ref) {
LOG_PREFIX(Transaction::mark_delayed_extent_inline);
SUBTRACET(seastore_tm, "removing {} from write_set", *this, *ref);
write_set.erase(*ref);
ref->set_paddr(make_record_relative_paddr(offset));
offset += ref->get_length();
inline_block_list.push_back(ref);
SUBTRACET(seastore_tm, "adding {} to write_set", *this, *ref);
write_set.insert(*ref);
}
void mark_delayed_extent_ool(LogicalCachedExtentRef& ref, paddr_t final_addr) {
LOG_PREFIX(Transaction::mark_delayed_extent_ool);
SUBTRACET(seastore_tm, "removing {} from write_set", *this, *ref);
write_set.erase(*ref);
ref->set_paddr(final_addr);
assert(!ref->get_paddr().is_null());
assert(!ref->is_inline());
ool_block_list.push_back(ref);
SUBTRACET(seastore_tm, "adding {} to write_set", *this, *ref);
write_set.insert(*ref);
}
void add_mutated_extent(CachedExtentRef ref) {
LOG_PREFIX(Transaction::add_mutated_extent);
ceph_assert(!is_weak());
assert(read_set.count(ref->prior_instance->get_paddr()));
mutated_block_list.push_back(ref);
SUBTRACET(seastore_tm, "adding {} to write_set", *this, *ref);
write_set.insert(*ref);
}
void replace_placeholder(CachedExtent& placeholder, CachedExtent& extent) {
ceph_assert(!is_weak());
assert(placeholder.get_type() == extent_types_t::RETIRED_PLACEHOLDER);
assert(extent.get_type() != extent_types_t::RETIRED_PLACEHOLDER);
assert(extent.get_type() != extent_types_t::ROOT);
assert(extent.get_paddr() == placeholder.get_paddr());
{
auto where = read_set.find(placeholder.get_paddr());
assert(where != read_set.end());
assert(where->ref.get() == &placeholder);
where = read_set.erase(where);
read_set.emplace_hint(where, this, &extent);
}
{
auto where = retired_set.find(&placeholder);
assert(where != retired_set.end());
assert(where->get() == &placeholder);
where = retired_set.erase(where);
retired_set.emplace_hint(where, &extent);
}
}
void mark_segment_to_release(segment_id_t segment) {
assert(to_release == NULL_SEG_ID);
to_release = segment;
}
segment_id_t get_segment_to_release() const {
return to_release;
}
auto& get_delayed_alloc_list() {
return delayed_alloc_list;
}
const auto &get_mutated_block_list() {
return mutated_block_list;
}
const auto &get_retired_set() {
return retired_set;
}
template <typename F>
auto for_each_fresh_block(F &&f) const {
std::for_each(ool_block_list.begin(), ool_block_list.end(), f);
std::for_each(inline_block_list.begin(), inline_block_list.end(), f);
}
const io_stat_t& get_fresh_block_stats() const {
return fresh_block_stats;
}
size_t get_allocation_size() const {
size_t ret = 0;
for_each_fresh_block([&ret](auto &e) { ret += e->get_length(); });
return ret;
}
enum class src_t : uint8_t {
MUTATE = 0,
READ, // including weak and non-weak read transactions
CLEANER_TRIM,
CLEANER_RECLAIM,
MAX
};
static constexpr auto SRC_MAX = static_cast<std::size_t>(src_t::MAX);
src_t get_src() const {
return src;
}
bool is_weak() const {
return weak;
}
void test_set_conflict() {
conflicted = true;
}
bool is_conflicted() const {
return conflicted;
}
auto &get_handle() {
return handle;
}
Transaction(
OrderingHandle &&handle,
bool weak,
src_t src,
journal_seq_t initiated_after,
on_destruct_func_t&& f
) : weak(weak),
handle(std::move(handle)),
on_destruct(std::move(f)),
src(src)
{}
void invalidate_clear_write_set() {
for (auto &&i: write_set) {
i.state = CachedExtent::extent_state_t::INVALID;
}
write_set.clear();
}
~Transaction() {
on_destruct(*this);
invalidate_clear_write_set();
}
friend class crimson::os::seastore::SeaStore;
friend class TransactionConflictCondition;
void reset_preserve_handle(journal_seq_t initiated_after) {
root.reset();
offset = 0;
delayed_temp_offset = 0;
read_set.clear();
invalidate_clear_write_set();
mutated_block_list.clear();
fresh_block_stats = {};
num_delayed_invalid_extents = 0;
delayed_alloc_list.clear();
inline_block_list.clear();
ool_block_list.clear();
retired_set.clear();
onode_tree_stats = {};
lba_tree_stats = {};
ool_write_stats = {};
to_release = NULL_SEG_ID;
conflicted = false;
if (!has_reset) {
has_reset = true;
}
}
bool did_reset() const {
return has_reset;
}
struct tree_stats_t {
uint64_t depth = 0;
uint64_t num_inserts = 0;
uint64_t num_erases = 0;
bool is_clear() const {
return (depth == 0 &&
num_inserts == 0 &&
num_erases == 0);
}
};
tree_stats_t& get_onode_tree_stats() {
return onode_tree_stats;
}
tree_stats_t& get_lba_tree_stats() {
return lba_tree_stats;
}
void add_rbm_alloc_info_blocks(rbm_alloc_delta_t &d) {
rbm_alloc_info_blocks.push_back(d);
}
void clear_rbm_alloc_info_blocks() {
if (!rbm_alloc_info_blocks.empty()) {
rbm_alloc_info_blocks.clear();
}
}
const auto &get_rbm_alloc_info_blocks() {
return rbm_alloc_info_blocks;
}
struct ool_write_stats_t {
io_stat_t extents;
uint64_t header_raw_bytes = 0;
uint64_t header_bytes = 0;
uint64_t data_bytes = 0;
uint64_t num_records = 0;
bool is_clear() const {
return (extents.is_clear() &&
header_raw_bytes == 0 &&
header_bytes == 0 &&
data_bytes == 0 &&
num_records == 0);
}
};
ool_write_stats_t& get_ool_write_stats() {
return ool_write_stats;
}
void increment_delayed_invalid_extents() {
++num_delayed_invalid_extents;
}
private:
friend class Cache;
friend Ref make_test_transaction();
/**
* If set, *this may not be used to perform writes and will not provide
* consistentency allowing operations using to avoid maintaining a read_set.
*/
const bool weak;
RootBlockRef root; ///< ref to root if read or written by transaction
seastore_off_t offset = 0; ///< relative offset of next block
seastore_off_t delayed_temp_offset = 0;
/**
* read_set
*
* Holds a reference (with a refcount) to every extent read via *this.
* Submitting a transaction mutating any contained extent/addr will
* invalidate *this.
*/
read_set_t<Transaction> read_set; ///< set of extents read by paddr
/**
* write_set
*
* Contains a reference (without a refcount) to every extent mutated
* as part of *this. No contained extent may be referenced outside
* of *this. Every contained extent will be in one of inline_block_list,
* ool_block_list, mutated_block_list, or delayed_alloc_list.
*/
ExtentIndex write_set;
/**
* lists of fresh blocks, holds refcounts, subset of write_set
*/
io_stat_t fresh_block_stats;
uint64_t num_delayed_invalid_extents = 0;
/// blocks that will be committed with journal record inline
std::list<CachedExtentRef> inline_block_list;
/// blocks that will be committed with out-of-line record
std::list<CachedExtentRef> ool_block_list;
/// blocks with delayed allocation, may become inline or ool above
std::list<LogicalCachedExtentRef> delayed_alloc_list;
/// list of mutated blocks, holds refcounts, subset of write_set
std::list<CachedExtentRef> mutated_block_list;
/**
* retire_set
*
* Set of extents retired by *this.
*/
pextent_set_t retired_set;
/// stats to collect when commit or invalidate
tree_stats_t onode_tree_stats;
tree_stats_t lba_tree_stats;
ool_write_stats_t ool_write_stats;
///< if != NULL_SEG_ID, release this segment after completion
segment_id_t to_release = NULL_SEG_ID;
bool conflicted = false;
bool has_reset = false;
OrderingHandle handle;
on_destruct_func_t on_destruct;
const src_t src;
std::vector<rbm_alloc_delta_t> rbm_alloc_info_blocks;
};
using TransactionRef = Transaction::Ref;
inline std::ostream& operator<<(std::ostream& os,
const Transaction::src_t& src) {
switch (src) {
case Transaction::src_t::MUTATE:
return os << "MUTATE";
case Transaction::src_t::READ:
return os << "READ";
case Transaction::src_t::CLEANER_TRIM:
return os << "CLEANER_TRIM";
case Transaction::src_t::CLEANER_RECLAIM:
return os << "CLEANER_RECLAIM";
default:
ceph_abort("impossible");
}
}
/// Should only be used with dummy staged-fltree node extent manager
inline TransactionRef make_test_transaction() {
return std::make_unique<Transaction>(
get_dummy_ordering_handle(),
false,
Transaction::src_t::MUTATE,
journal_seq_t{},
[](Transaction&) {}
);
}
struct TransactionConflictCondition {
class transaction_conflict final : public std::exception {
public:
const char* what() const noexcept final {
return "transaction conflict detected";
}
};
public:
TransactionConflictCondition(Transaction &t) : t(t) {}
template <typename Fut>
std::pair<bool, std::optional<Fut>> may_interrupt() {
if (t.conflicted) {
return {
true,
seastar::futurize<Fut>::make_exception_future(
transaction_conflict())};
} else {
return {false, std::optional<Fut>()};
}
}
template <typename T>
static constexpr bool is_interruption_v =
std::is_same_v<T, transaction_conflict>;
static bool is_interruption(std::exception_ptr& eptr) {
return *eptr.__cxa_exception_type() == typeid(transaction_conflict);
}
private:
Transaction &t;
};
using trans_intr = crimson::interruptible::interruptor<
TransactionConflictCondition
>;
template <typename E>
using trans_iertr =
crimson::interruptible::interruptible_errorator<
TransactionConflictCondition,
E
>;
template <typename F, typename... Args>
auto with_trans_intr(Transaction &t, F &&f, Args&&... args) {
return trans_intr::with_interruption_to_error<crimson::ct_error::eagain>(
std::move(f),
TransactionConflictCondition(t),
t,
std::forward<Args>(args)...);
}
template <typename T>
using with_trans_ertr = typename T::base_ertr::template extend<crimson::ct_error::eagain>;
}
|