1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
19 #include "erasure-code/ErasureCodeInterface.h"
20 #include "include/buffer_fwd.h"
21 #include "include/ceph_assert.h"
22 #include "include/encoding.h"
23 #include "common/Formatter.h"
28 const uint64_t stripe_width
;
29 const uint64_t chunk_size
;
31 stripe_info_t(uint64_t stripe_size
, uint64_t stripe_width
)
32 : stripe_width(stripe_width
),
33 chunk_size(stripe_width
/ stripe_size
) {
34 ceph_assert(stripe_width
% stripe_size
== 0);
36 bool logical_offset_is_stripe_aligned(uint64_t logical
) const {
37 return (logical
% stripe_width
) == 0;
39 uint64_t get_stripe_width() const {
42 uint64_t get_chunk_size() const {
45 uint64_t logical_to_prev_chunk_offset(uint64_t offset
) const {
46 return (offset
/ stripe_width
) * chunk_size
;
48 uint64_t logical_to_next_chunk_offset(uint64_t offset
) const {
49 return ((offset
+ stripe_width
- 1)/ stripe_width
) * chunk_size
;
51 uint64_t logical_to_prev_stripe_offset(uint64_t offset
) const {
52 return offset
- (offset
% stripe_width
);
54 uint64_t logical_to_next_stripe_offset(uint64_t offset
) const {
55 return ((offset
% stripe_width
) ?
56 (offset
- (offset
% stripe_width
) + stripe_width
) :
59 uint64_t aligned_logical_offset_to_chunk_offset(uint64_t offset
) const {
60 ceph_assert(offset
% stripe_width
== 0);
61 return (offset
/ stripe_width
) * chunk_size
;
63 uint64_t aligned_chunk_offset_to_logical_offset(uint64_t offset
) const {
64 ceph_assert(offset
% chunk_size
== 0);
65 return (offset
/ chunk_size
) * stripe_width
;
67 std::pair
<uint64_t, uint64_t> aligned_offset_len_to_chunk(
68 std::pair
<uint64_t, uint64_t> in
) const {
69 return std::make_pair(
70 aligned_logical_offset_to_chunk_offset(in
.first
),
71 aligned_logical_offset_to_chunk_offset(in
.second
));
73 std::pair
<uint64_t, uint64_t> offset_len_to_stripe_bounds(
74 std::pair
<uint64_t, uint64_t> in
) const {
75 uint64_t off
= logical_to_prev_stripe_offset(in
.first
);
76 uint64_t len
= logical_to_next_stripe_offset(
77 (in
.first
- off
) + in
.second
);
78 return std::make_pair(off
, len
);
83 const stripe_info_t
&sinfo
,
84 ceph::ErasureCodeInterfaceRef
&ec_impl
,
85 std::map
<int, ceph::buffer::list
> &to_decode
,
86 ceph::buffer::list
*out
);
89 const stripe_info_t
&sinfo
,
90 ceph::ErasureCodeInterfaceRef
&ec_impl
,
91 std::map
<int, ceph::buffer::list
> &to_decode
,
92 std::map
<int, ceph::buffer::list
*> &out
);
95 const stripe_info_t
&sinfo
,
96 ceph::ErasureCodeInterfaceRef
&ec_impl
,
97 ceph::buffer::list
&in
,
98 const std::set
<int> &want
,
99 std::map
<int, ceph::buffer::list
> *out
);
102 uint64_t total_chunk_size
= 0;
103 std::vector
<uint32_t> cumulative_shard_hashes
;
105 // purely ephemeral, represents the size once all in-flight ops commit
106 uint64_t projected_total_chunk_size
= 0;
109 explicit HashInfo(unsigned num_chunks
) :
110 cumulative_shard_hashes(num_chunks
, -1) {}
111 void append(uint64_t old_size
, std::map
<int, ceph::buffer::list
> &to_append
);
113 total_chunk_size
= 0;
114 cumulative_shard_hashes
= std::vector
<uint32_t>(
115 cumulative_shard_hashes
.size(),
118 void encode(ceph::buffer::list
&bl
) const;
119 void decode(ceph::buffer::list::const_iterator
&bl
);
120 void dump(ceph::Formatter
*f
) const;
121 static void generate_test_instances(std::list
<HashInfo
*>& o
);
122 uint32_t get_chunk_hash(int shard
) const {
123 ceph_assert((unsigned)shard
< cumulative_shard_hashes
.size());
124 return cumulative_shard_hashes
[shard
];
126 uint64_t get_total_chunk_size() const {
127 return total_chunk_size
;
129 uint64_t get_projected_total_chunk_size() const {
130 return projected_total_chunk_size
;
132 uint64_t get_total_logical_size(const stripe_info_t
&sinfo
) const {
133 return get_total_chunk_size() *
134 (sinfo
.get_stripe_width()/sinfo
.get_chunk_size());
136 uint64_t get_projected_total_logical_size(const stripe_info_t
&sinfo
) const {
137 return get_projected_total_chunk_size() *
138 (sinfo
.get_stripe_width()/sinfo
.get_chunk_size());
140 void set_projected_total_logical_size(
141 const stripe_info_t
&sinfo
,
142 uint64_t logical_size
) {
143 ceph_assert(sinfo
.logical_offset_is_stripe_aligned(logical_size
));
144 projected_total_chunk_size
= sinfo
.aligned_logical_offset_to_chunk_offset(
147 void set_total_chunk_size_clear_hash(uint64_t new_chunk_size
) {
148 cumulative_shard_hashes
.clear();
149 total_chunk_size
= new_chunk_size
;
151 bool has_chunk_hash() const {
152 return !cumulative_shard_hashes
.empty();
154 void update_to(const HashInfo
&rhs
) {
155 auto ptcs
= projected_total_chunk_size
;
157 projected_total_chunk_size
= ptcs
;
159 friend std::ostream
& operator<<(std::ostream
& out
, const HashInfo
& hi
);
162 typedef std::shared_ptr
<HashInfo
> HashInfoRef
;
164 bool is_hinfo_key_string(const std::string
&key
);
165 const std::string
&get_hinfo_key();
167 WRITE_CLASS_ENCODER(ECUtil::HashInfo
)