1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph distributed storage system
6 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
8 * Author: Loic Dachary <loic@dachary.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
18 #include "include/stringify.h"
19 #include "ErasureCodeExample.h"
20 #include "global/global_context.h"
21 #include "gtest/gtest.h"
23 TEST(ErasureCodeExample
, chunk_size
)
25 ErasureCodeExample example
;
26 EXPECT_EQ(3u, example
.get_chunk_count());
27 EXPECT_EQ(11u, example
.get_chunk_size(20));
30 TEST(ErasureCodeExample
, minimum_to_decode
)
32 ErasureCodeExample example
;
33 set
<int> available_chunks
;
34 set
<int> want_to_read
;
35 want_to_read
.insert(1);
38 EXPECT_EQ(-EIO
, example
.minimum_to_decode(want_to_read
,
42 available_chunks
.insert(0);
43 available_chunks
.insert(2);
46 EXPECT_EQ(0, example
.minimum_to_decode(want_to_read
,
49 EXPECT_EQ(available_chunks
, minimum
);
50 EXPECT_EQ(2u, minimum
.size());
51 EXPECT_EQ(1u, minimum
.count(0));
52 EXPECT_EQ(1u, minimum
.count(2));
56 available_chunks
.insert(1);
57 EXPECT_EQ(0, example
.minimum_to_decode(want_to_read
,
60 EXPECT_EQ(1u, minimum
.size());
61 EXPECT_EQ(1u, minimum
.count(1));
65 TEST(ErasureCodeExample
, minimum_to_decode_with_cost
)
67 ErasureCodeExample example
;
68 map
<int,int> available
;
69 set
<int> want_to_read
;
70 want_to_read
.insert(1);
73 EXPECT_EQ(-EIO
, example
.minimum_to_decode_with_cost(want_to_read
,
81 EXPECT_EQ(0, example
.minimum_to_decode_with_cost(want_to_read
,
84 EXPECT_EQ(2u, minimum
.size());
85 EXPECT_EQ(1u, minimum
.count(0));
86 EXPECT_EQ(1u, minimum
.count(2));
91 EXPECT_EQ(0, example
.minimum_to_decode_with_cost(want_to_read
,
94 EXPECT_EQ(1u, minimum
.size());
95 EXPECT_EQ(1u, minimum
.count(1));
100 EXPECT_EQ(0, example
.minimum_to_decode_with_cost(want_to_read
,
103 EXPECT_EQ(2u, minimum
.size());
104 EXPECT_EQ(1u, minimum
.count(0));
105 EXPECT_EQ(1u, minimum
.count(2));
109 TEST(ErasureCodeExample
, encode_decode
)
111 ErasureCodeExample example
;
115 set
<int> want_to_encode
;
116 for(unsigned int i
= 0; i
< example
.get_chunk_count(); i
++)
117 want_to_encode
.insert(i
);
118 map
<int, bufferlist
> encoded
;
119 EXPECT_EQ(0, example
.encode(want_to_encode
, in
, &encoded
));
120 EXPECT_EQ(example
.get_chunk_count(), encoded
.size());
121 EXPECT_EQ(example
.get_chunk_size(in
.length()), encoded
[0].length());
122 EXPECT_EQ('A', encoded
[0][0]);
123 EXPECT_EQ('B', encoded
[0][1]);
124 EXPECT_EQ('C', encoded
[0][2]);
125 EXPECT_EQ('D', encoded
[1][0]);
126 EXPECT_EQ('E', encoded
[1][1]);
127 EXPECT_EQ('A'^'D', encoded
[2][0]);
128 EXPECT_EQ('B'^'E', encoded
[2][1]);
129 EXPECT_EQ('C'^0, encoded
[2][2]);
131 // all chunks are available
133 int want_to_decode
[] = { 0, 1 };
134 map
<int, bufferlist
> decoded
;
135 EXPECT_EQ(0, example
.decode(set
<int>(want_to_decode
, want_to_decode
+2),
138 EXPECT_EQ(2u, decoded
.size());
139 EXPECT_EQ(3u, decoded
[0].length());
140 EXPECT_EQ('A', decoded
[0][0]);
141 EXPECT_EQ('B', decoded
[0][1]);
142 EXPECT_EQ('C', decoded
[0][2]);
143 EXPECT_EQ('D', decoded
[1][0]);
144 EXPECT_EQ('E', decoded
[1][1]);
147 // one chunk is missing
149 map
<int, bufferlist
> degraded
= encoded
;
151 EXPECT_EQ(2u, degraded
.size());
152 int want_to_decode
[] = { 0, 1 };
153 map
<int, bufferlist
> decoded
;
154 EXPECT_EQ(0, example
.decode(set
<int>(want_to_decode
, want_to_decode
+2),
157 EXPECT_EQ(2u, decoded
.size());
158 EXPECT_EQ(3u, decoded
[0].length());
159 EXPECT_EQ('A', decoded
[0][0]);
160 EXPECT_EQ('B', decoded
[0][1]);
161 EXPECT_EQ('C', decoded
[0][2]);
162 EXPECT_EQ('D', decoded
[1][0]);
163 EXPECT_EQ('E', decoded
[1][1]);
167 TEST(ErasureCodeExample
, decode
)
169 ErasureCodeExample example
;
171 #define LARGE_ENOUGH 2048
172 bufferptr
in_ptr(buffer::create_page_aligned(LARGE_ENOUGH
));
174 in_ptr
.set_length(0);
175 const char *payload
=
176 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
177 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
178 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
179 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
180 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
181 in_ptr
.append(payload
, strlen(payload
));
183 in
.push_front(in_ptr
);
184 int want_to_encode
[] = { 0, 1, 2 };
185 map
<int, bufferlist
> encoded
;
186 EXPECT_EQ(0, example
.encode(set
<int>(want_to_encode
, want_to_encode
+3),
189 EXPECT_EQ(3u, encoded
.size());
191 // successfull decode
193 EXPECT_EQ(0, example
.decode_concat(encoded
, &out
));
195 usable
.substr_of(out
, 0, in
.length());
196 EXPECT_TRUE(usable
== in
);
199 map
<int, bufferlist
> degraded
;
200 degraded
[0] = encoded
[0];
201 EXPECT_EQ(-ERANGE
, example
.decode_concat(degraded
, &out
));
204 TEST(ErasureCodeExample
, create_ruleset
)
206 CrushWrapper
*c
= new CrushWrapper
;
208 c
->set_type_name(2, "root");
209 c
->set_type_name(1, "host");
210 c
->set_type_name(0, "osd");
213 c
->add_bucket(0, CRUSH_BUCKET_STRAW
, CRUSH_HASH_RJENKINS1
,
214 5, 0, NULL
, NULL
, &rootno
);
215 c
->set_item_name(rootno
, "default");
217 map
<string
,string
> loc
;
218 loc
["root"] = "default";
223 for (int h
=0; h
<num_host
; ++h
) {
224 loc
["host"] = string("host-") + stringify(h
);
225 for (int o
=0; o
<num_osd
; ++o
, ++osd
) {
226 c
->insert_item(g_ceph_context
, osd
, 1.0, string("osd.") + stringify(osd
), loc
);
231 ErasureCodeExample example
;
232 EXPECT_EQ(0, example
.create_ruleset("myrule", *c
, &ss
));
237 * compile-command: "cd ../.. ;
239 * make unittest_erasure_code_example &&
240 * valgrind --leak-check=full --tool=memcheck \
241 * ./unittest_erasure_code_example --gtest_filter=*.* \
242 * --log-to-stderr=true --debug-osd=20