]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph distributed storage system | |
5 | * | |
6 | * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com> | |
7 | * Copyright (C) 2014 Red Hat <contact@redhat.com> | |
8 | * | |
9 | * Author: Loic Dachary <loic@dachary.org> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include <errno.h> | |
19 | #include <stdlib.h> | |
20 | ||
21 | #include "crush/CrushWrapper.h" | |
22 | #include "include/stringify.h" | |
23 | #include "erasure-code/jerasure/ErasureCodeJerasure.h" | |
24 | #include "global/global_context.h" | |
25 | #include "common/config.h" | |
26 | #include "gtest/gtest.h" | |
27 | ||
20effc67 | 28 | using namespace std; |
7c673cae FG |
29 | |
30 | template <typename T> | |
31 | class ErasureCodeTest : public ::testing::Test { | |
32 | public: | |
33 | }; | |
34 | ||
35 | typedef ::testing::Types< | |
36 | ErasureCodeJerasureReedSolomonVandermonde, | |
37 | ErasureCodeJerasureReedSolomonRAID6, | |
38 | ErasureCodeJerasureCauchyOrig, | |
39 | ErasureCodeJerasureCauchyGood, | |
40 | ErasureCodeJerasureLiberation, | |
41 | ErasureCodeJerasureBlaumRoth, | |
42 | ErasureCodeJerasureLiber8tion | |
43 | > JerasureTypes; | |
9f95a23c | 44 | TYPED_TEST_SUITE(ErasureCodeTest, JerasureTypes); |
7c673cae FG |
45 | |
46 | TYPED_TEST(ErasureCodeTest, sanity_check_k) | |
47 | { | |
48 | TypeParam jerasure; | |
49 | ErasureCodeProfile profile; | |
50 | profile["k"] = "1"; | |
51 | profile["m"] = "1"; | |
52 | profile["packetsize"] = "8"; | |
53 | ostringstream errors; | |
54 | EXPECT_EQ(-EINVAL, jerasure.init(profile, &errors)); | |
55 | EXPECT_NE(std::string::npos, errors.str().find("must be >= 2")); | |
56 | } | |
57 | ||
58 | TYPED_TEST(ErasureCodeTest, encode_decode) | |
59 | { | |
60 | const char *per_chunk_alignments[] = { "false", "true" }; | |
61 | for (int per_chunk_alignment = 0 ; | |
62 | per_chunk_alignment < 2; | |
63 | per_chunk_alignment++) { | |
64 | TypeParam jerasure; | |
65 | ErasureCodeProfile profile; | |
66 | profile["k"] = "2"; | |
67 | profile["m"] = "2"; | |
68 | profile["packetsize"] = "8"; | |
69 | profile["jerasure-per-chunk-alignment"] = | |
70 | per_chunk_alignments[per_chunk_alignment]; | |
71 | jerasure.init(profile, &cerr); | |
72 | ||
73 | #define LARGE_ENOUGH 2048 | |
74 | bufferptr in_ptr(buffer::create_page_aligned(LARGE_ENOUGH)); | |
75 | in_ptr.zero(); | |
76 | in_ptr.set_length(0); | |
77 | const char *payload = | |
78 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
79 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
80 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
81 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
82 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
83 | in_ptr.append(payload, strlen(payload)); | |
84 | bufferlist in; | |
11fdf7f2 | 85 | in.push_back(in_ptr); |
7c673cae FG |
86 | int want_to_encode[] = { 0, 1, 2, 3 }; |
87 | map<int, bufferlist> encoded; | |
88 | EXPECT_EQ(0, jerasure.encode(set<int>(want_to_encode, want_to_encode+4), | |
89 | in, | |
90 | &encoded)); | |
91 | EXPECT_EQ(4u, encoded.size()); | |
92 | unsigned length = encoded[0].length(); | |
93 | EXPECT_EQ(0, memcmp(encoded[0].c_str(), in.c_str(), length)); | |
94 | EXPECT_EQ(0, memcmp(encoded[1].c_str(), in.c_str() + length, | |
95 | in.length() - length)); | |
96 | ||
97 | ||
98 | // all chunks are available | |
99 | { | |
100 | int want_to_decode[] = { 0, 1 }; | |
101 | map<int, bufferlist> decoded; | |
11fdf7f2 TL |
102 | EXPECT_EQ(0, jerasure._decode(set<int>(want_to_decode, want_to_decode+2), |
103 | encoded, | |
104 | &decoded)); | |
7c673cae FG |
105 | EXPECT_EQ(2u, decoded.size()); |
106 | EXPECT_EQ(length, decoded[0].length()); | |
107 | EXPECT_EQ(0, memcmp(decoded[0].c_str(), in.c_str(), length)); | |
108 | EXPECT_EQ(0, memcmp(decoded[1].c_str(), in.c_str() + length, | |
109 | in.length() - length)); | |
110 | } | |
111 | ||
112 | // two chunks are missing | |
113 | { | |
114 | map<int, bufferlist> degraded = encoded; | |
115 | degraded.erase(0); | |
116 | degraded.erase(1); | |
117 | EXPECT_EQ(2u, degraded.size()); | |
118 | int want_to_decode[] = { 0, 1 }; | |
119 | map<int, bufferlist> decoded; | |
11fdf7f2 TL |
120 | EXPECT_EQ(0, jerasure._decode(set<int>(want_to_decode, want_to_decode+2), |
121 | degraded, | |
122 | &decoded)); | |
7c673cae FG |
123 | // always decode all, regardless of want_to_decode |
124 | EXPECT_EQ(4u, decoded.size()); | |
125 | EXPECT_EQ(length, decoded[0].length()); | |
126 | EXPECT_EQ(0, memcmp(decoded[0].c_str(), in.c_str(), length)); | |
127 | EXPECT_EQ(0, memcmp(decoded[1].c_str(), in.c_str() + length, | |
128 | in.length() - length)); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | TYPED_TEST(ErasureCodeTest, minimum_to_decode) | |
134 | { | |
135 | TypeParam jerasure; | |
136 | ErasureCodeProfile profile; | |
137 | profile["k"] = "2"; | |
138 | profile["m"] = "2"; | |
139 | profile["w"] = "7"; | |
140 | profile["packetsize"] = "8"; | |
141 | jerasure.init(profile, &cerr); | |
142 | ||
143 | // | |
144 | // If trying to read nothing, the minimum is empty. | |
145 | // | |
146 | { | |
147 | set<int> want_to_read; | |
148 | set<int> available_chunks; | |
149 | set<int> minimum; | |
150 | ||
11fdf7f2 TL |
151 | EXPECT_EQ(0, jerasure._minimum_to_decode(want_to_read, |
152 | available_chunks, | |
153 | &minimum)); | |
7c673cae FG |
154 | EXPECT_TRUE(minimum.empty()); |
155 | } | |
156 | // | |
157 | // There is no way to read a chunk if none are available. | |
158 | // | |
159 | { | |
160 | set<int> want_to_read; | |
161 | set<int> available_chunks; | |
162 | set<int> minimum; | |
163 | ||
164 | want_to_read.insert(0); | |
165 | ||
11fdf7f2 TL |
166 | EXPECT_EQ(-EIO, jerasure._minimum_to_decode(want_to_read, |
167 | available_chunks, | |
168 | &minimum)); | |
7c673cae FG |
169 | } |
170 | // | |
171 | // Reading a subset of the available chunks is always possible. | |
172 | // | |
173 | { | |
174 | set<int> want_to_read; | |
175 | set<int> available_chunks; | |
176 | set<int> minimum; | |
177 | ||
178 | want_to_read.insert(0); | |
179 | available_chunks.insert(0); | |
180 | ||
11fdf7f2 TL |
181 | EXPECT_EQ(0, jerasure._minimum_to_decode(want_to_read, |
182 | available_chunks, | |
183 | &minimum)); | |
7c673cae FG |
184 | EXPECT_EQ(want_to_read, minimum); |
185 | } | |
186 | // | |
187 | // There is no way to read a missing chunk if there is less than k | |
188 | // chunks available. | |
189 | // | |
190 | { | |
191 | set<int> want_to_read; | |
192 | set<int> available_chunks; | |
193 | set<int> minimum; | |
194 | ||
195 | want_to_read.insert(0); | |
196 | want_to_read.insert(1); | |
197 | available_chunks.insert(0); | |
198 | ||
11fdf7f2 TL |
199 | EXPECT_EQ(-EIO, jerasure._minimum_to_decode(want_to_read, |
200 | available_chunks, | |
201 | &minimum)); | |
7c673cae FG |
202 | } |
203 | // | |
204 | // When chunks are not available, the minimum can be made of any | |
205 | // chunks. For instance, to read 1 and 3 below the minimum could be | |
206 | // 2 and 3 which may seem better because it contains one of the | |
207 | // chunks to be read. But it won't be more efficient than retrieving | |
208 | // 0 and 2 instead because, in both cases, the decode function will | |
209 | // need to run the same recovery operation and use the same amount | |
210 | // of CPU and memory. | |
211 | // | |
212 | { | |
213 | set<int> want_to_read; | |
214 | set<int> available_chunks; | |
215 | set<int> minimum; | |
216 | ||
217 | want_to_read.insert(1); | |
218 | want_to_read.insert(3); | |
219 | available_chunks.insert(0); | |
220 | available_chunks.insert(2); | |
221 | available_chunks.insert(3); | |
222 | ||
11fdf7f2 TL |
223 | EXPECT_EQ(0, jerasure._minimum_to_decode(want_to_read, |
224 | available_chunks, | |
225 | &minimum)); | |
7c673cae FG |
226 | EXPECT_EQ(2u, minimum.size()); |
227 | EXPECT_EQ(0u, minimum.count(3)); | |
228 | } | |
229 | } | |
230 | ||
231 | TEST(ErasureCodeTest, encode) | |
232 | { | |
233 | ErasureCodeJerasureReedSolomonVandermonde jerasure; | |
234 | ErasureCodeProfile profile; | |
235 | profile["k"] = "2"; | |
236 | profile["m"] = "2"; | |
237 | profile["w"] = "8"; | |
238 | jerasure.init(profile, &cerr); | |
239 | ||
240 | unsigned aligned_object_size = jerasure.get_alignment() * 2; | |
241 | { | |
242 | // | |
243 | // When the input bufferlist needs to be padded because | |
244 | // it is not properly aligned, it is padded with zeros. | |
245 | // | |
246 | bufferlist in; | |
247 | map<int,bufferlist> encoded; | |
248 | int want_to_encode[] = { 0, 1, 2, 3 }; | |
249 | int trail_length = 1; | |
250 | in.append(string(aligned_object_size + trail_length, 'X')); | |
251 | EXPECT_EQ(0, jerasure.encode(set<int>(want_to_encode, want_to_encode+4), | |
252 | in, | |
253 | &encoded)); | |
254 | EXPECT_EQ(4u, encoded.size()); | |
255 | char *last_chunk = encoded[1].c_str(); | |
256 | int length =encoded[1].length(); | |
257 | EXPECT_EQ('X', last_chunk[0]); | |
258 | EXPECT_EQ('\0', last_chunk[length - trail_length]); | |
259 | } | |
260 | ||
261 | { | |
262 | // | |
263 | // When only the first chunk is required, the encoded map only | |
264 | // contains the first chunk. Although the jerasure encode | |
265 | // internally allocated a buffer because of padding requirements | |
266 | // and also computes the coding chunks, they are released before | |
267 | // the return of the method, as shown when running the tests thru | |
268 | // valgrind (there is no leak). | |
269 | // | |
270 | bufferlist in; | |
271 | map<int,bufferlist> encoded; | |
272 | set<int> want_to_encode; | |
273 | want_to_encode.insert(0); | |
274 | int trail_length = 1; | |
275 | in.append(string(aligned_object_size + trail_length, 'X')); | |
276 | EXPECT_EQ(0, jerasure.encode(want_to_encode, in, &encoded)); | |
277 | EXPECT_EQ(1u, encoded.size()); | |
278 | } | |
279 | } | |
280 | ||
224ce89b | 281 | TEST(ErasureCodeTest, create_rule) |
7c673cae | 282 | { |
11fdf7f2 | 283 | std::unique_ptr<CrushWrapper> c = std::make_unique<CrushWrapper>(); |
7c673cae FG |
284 | c->create(); |
285 | int root_type = 2; | |
286 | c->set_type_name(root_type, "root"); | |
287 | int host_type = 1; | |
288 | c->set_type_name(host_type, "host"); | |
289 | int osd_type = 0; | |
290 | c->set_type_name(osd_type, "osd"); | |
291 | ||
292 | int rootno; | |
293 | c->add_bucket(0, CRUSH_BUCKET_STRAW, CRUSH_HASH_RJENKINS1, | |
294 | root_type, 0, NULL, NULL, &rootno); | |
295 | c->set_item_name(rootno, "default"); | |
296 | ||
297 | map<string,string> loc; | |
298 | loc["root"] = "default"; | |
299 | ||
300 | int num_host = 4; | |
301 | int num_osd = 5; | |
302 | int osd = 0; | |
303 | for (int h=0; h<num_host; ++h) { | |
304 | loc["host"] = string("host-") + stringify(h); | |
305 | for (int o=0; o<num_osd; ++o, ++osd) { | |
306 | c->insert_item(g_ceph_context, osd, 1.0, string("osd.") + stringify(osd), loc); | |
307 | } | |
308 | } | |
309 | ||
310 | c->finalize(); | |
311 | ||
312 | { | |
313 | stringstream ss; | |
314 | ErasureCodeJerasureReedSolomonVandermonde jerasure; | |
315 | ErasureCodeProfile profile; | |
316 | profile["k"] = "2"; | |
317 | profile["m"] = "2"; | |
318 | profile["w"] = "8"; | |
319 | jerasure.init(profile, &cerr); | |
20effc67 TL |
320 | int rule = jerasure.create_rule("myrule", *c, &ss); |
321 | EXPECT_EQ(0, rule); | |
224ce89b | 322 | EXPECT_EQ(-EEXIST, jerasure.create_rule("myrule", *c, &ss)); |
7c673cae | 323 | // |
20effc67 | 324 | // the minimum that is expected from the created rule is to |
7c673cae FG |
325 | // successfully map get_chunk_count() devices from the crushmap, |
326 | // at least once. | |
327 | // | |
328 | vector<__u32> weight(c->get_max_devices(), 0x10000); | |
329 | vector<int> out; | |
330 | int x = 0; | |
20effc67 | 331 | c->do_rule(rule, x, out, jerasure.get_chunk_count(), weight, 0); |
7c673cae FG |
332 | ASSERT_EQ(out.size(), jerasure.get_chunk_count()); |
333 | for (unsigned i=0; i<out.size(); ++i) | |
334 | ASSERT_NE(CRUSH_ITEM_NONE, out[i]); | |
335 | } | |
336 | { | |
337 | stringstream ss; | |
338 | ErasureCodeJerasureReedSolomonVandermonde jerasure; | |
339 | ErasureCodeProfile profile; | |
340 | profile["k"] = "2"; | |
341 | profile["m"] = "2"; | |
342 | profile["w"] = "8"; | |
224ce89b | 343 | profile["crush-root"] = "BAD"; |
7c673cae | 344 | jerasure.init(profile, &cerr); |
224ce89b | 345 | EXPECT_EQ(-ENOENT, jerasure.create_rule("otherrule", *c, &ss)); |
7c673cae FG |
346 | EXPECT_EQ("root item BAD does not exist", ss.str()); |
347 | } | |
348 | { | |
349 | stringstream ss; | |
350 | ErasureCodeJerasureReedSolomonVandermonde jerasure; | |
351 | ErasureCodeProfile profile; | |
352 | profile["k"] = "2"; | |
353 | profile["m"] = "2"; | |
354 | profile["w"] = "8"; | |
224ce89b | 355 | profile["crush-failure-domain"] = "WORSE"; |
7c673cae | 356 | jerasure.init(profile, &cerr); |
224ce89b | 357 | EXPECT_EQ(-EINVAL, jerasure.create_rule("otherrule", *c, &ss)); |
7c673cae FG |
358 | EXPECT_EQ("unknown type WORSE", ss.str()); |
359 | } | |
360 | } | |
361 | ||
362 | /* | |
363 | * Local Variables: | |
364 | * compile-command: "cd ../.. ; | |
365 | * make -j4 unittest_erasure_code_jerasure && | |
366 | * valgrind --tool=memcheck \ | |
367 | * ./unittest_erasure_code_jerasure \ | |
368 | * --gtest_filter=*.* --log-to-stderr=true --debug-osd=20" | |
369 | * End: | |
370 | */ |