]>
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 - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2016 Red Hat | |
7 | * | |
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. | |
12 | * | |
13 | */ | |
14 | ||
15 | ||
16 | #include <gtest/gtest.h> | |
17 | #include "osd/ExtentCache.h" | |
18 | #include <iostream> | |
19 | ||
20effc67 TL |
20 | using namespace std; |
21 | ||
7c673cae FG |
22 | extent_map imap_from_vector(vector<pair<uint64_t, uint64_t> > &&in) |
23 | { | |
24 | extent_map out; | |
25 | for (auto &&tup: in) { | |
26 | bufferlist bl; | |
27 | bl.append_zero(tup.second); | |
28 | out.insert(tup.first, bl.length(), bl); | |
29 | } | |
30 | return out; | |
31 | } | |
32 | ||
33 | extent_map imap_from_iset(const extent_set &set) | |
34 | { | |
35 | extent_map out; | |
36 | for (auto &&iter: set) { | |
37 | bufferlist bl; | |
38 | bl.append_zero(iter.second); | |
39 | out.insert(iter.first, iter.second, bl); | |
40 | } | |
41 | return out; | |
42 | } | |
43 | ||
44 | extent_set iset_from_vector(vector<pair<uint64_t, uint64_t> > &&in) | |
45 | { | |
46 | extent_set out; | |
47 | for (auto &&tup: in) { | |
48 | out.insert(tup.first, tup.second); | |
49 | } | |
50 | return out; | |
51 | } | |
52 | ||
53 | TEST(extentcache, simple_write) | |
54 | { | |
55 | hobject_t oid; | |
56 | ||
57 | ExtentCache c; | |
58 | ExtentCache::write_pin pin; | |
59 | c.open_write_pin(pin); | |
60 | ||
61 | auto to_read = iset_from_vector( | |
62 | {{0, 2}, {8, 2}, {20, 2}}); | |
63 | auto to_write = iset_from_vector( | |
64 | {{0, 10}, {20, 4}}); | |
65 | auto must_read = c.reserve_extents_for_rmw( | |
66 | oid, pin, to_write, to_read); | |
67 | ASSERT_EQ( | |
68 | must_read, | |
69 | to_read); | |
70 | ||
71 | c.print(std::cerr); | |
72 | ||
73 | auto got = imap_from_iset(must_read); | |
74 | auto pending_read = to_read; | |
75 | pending_read.subtract(must_read); | |
76 | ||
77 | auto pending = c.get_remaining_extents_for_rmw( | |
78 | oid, | |
79 | pin, | |
80 | pending_read); | |
81 | ASSERT_TRUE(pending.empty()); | |
82 | ||
83 | auto write_map = imap_from_iset(to_write); | |
84 | c.present_rmw_update( | |
85 | oid, | |
86 | pin, | |
87 | write_map); | |
88 | ||
89 | c.release_write_pin(pin); | |
90 | } | |
91 | ||
92 | TEST(extentcache, write_write_overlap) | |
93 | { | |
94 | hobject_t oid; | |
95 | ||
96 | ExtentCache c; | |
97 | ExtentCache::write_pin pin; | |
98 | c.open_write_pin(pin); | |
99 | ||
100 | // start write 1 | |
101 | auto to_read = iset_from_vector( | |
102 | {{0, 2}, {8, 2}, {20, 2}}); | |
103 | auto to_write = iset_from_vector( | |
104 | {{0, 10}, {20, 4}}); | |
105 | auto must_read = c.reserve_extents_for_rmw( | |
106 | oid, pin, to_write, to_read); | |
107 | ASSERT_EQ( | |
108 | must_read, | |
109 | to_read); | |
110 | ||
111 | c.print(std::cerr); | |
112 | ||
113 | // start write 2 | |
114 | ExtentCache::write_pin pin2; | |
115 | c.open_write_pin(pin2); | |
116 | auto to_read2 = iset_from_vector( | |
117 | {{2, 4}, {10, 4}, {18, 4}}); | |
118 | auto to_write2 = iset_from_vector( | |
119 | {{2, 12}, {18, 12}}); | |
120 | auto must_read2 = c.reserve_extents_for_rmw( | |
121 | oid, pin2, to_write2, to_read2); | |
122 | ASSERT_EQ( | |
123 | must_read2, | |
124 | iset_from_vector({{10, 4}, {18, 2}})); | |
125 | ||
126 | c.print(std::cerr); | |
127 | ||
128 | // complete read for write 1 and start commit | |
129 | auto got = imap_from_iset(must_read); | |
130 | auto pending_read = to_read; | |
131 | pending_read.subtract(must_read); | |
132 | auto pending = c.get_remaining_extents_for_rmw( | |
133 | oid, | |
134 | pin, | |
135 | pending_read); | |
136 | ASSERT_TRUE(pending.empty()); | |
137 | ||
138 | auto write_map = imap_from_iset(to_write); | |
139 | c.present_rmw_update( | |
140 | oid, | |
141 | pin, | |
142 | write_map); | |
143 | ||
144 | c.print(std::cerr); | |
145 | ||
146 | // complete read for write 2 and start commit | |
147 | auto pending_read2 = to_read2; | |
148 | pending_read2.subtract(must_read2); | |
149 | auto pending2 = c.get_remaining_extents_for_rmw( | |
150 | oid, | |
151 | pin2, | |
152 | pending_read2); | |
153 | ASSERT_EQ( | |
154 | pending2, | |
155 | imap_from_iset(pending_read2)); | |
156 | ||
157 | auto write_map2 = imap_from_iset(to_write2); | |
158 | c.present_rmw_update( | |
159 | oid, | |
160 | pin2, | |
161 | write_map2); | |
162 | ||
163 | c.print(std::cerr); | |
164 | ||
165 | c.release_write_pin(pin); | |
166 | ||
167 | c.print(std::cerr); | |
168 | ||
169 | c.release_write_pin(pin2); | |
170 | } | |
171 | ||
172 | TEST(extentcache, write_write_overlap2) | |
173 | { | |
174 | hobject_t oid; | |
175 | ||
176 | ExtentCache c; | |
177 | ExtentCache::write_pin pin; | |
178 | c.open_write_pin(pin); | |
179 | ||
180 | // start write 1 | |
181 | auto to_read = extent_set(); | |
182 | auto to_write = iset_from_vector( | |
183 | {{659456, 4096}}); | |
184 | auto must_read = c.reserve_extents_for_rmw( | |
185 | oid, pin, to_write, to_read); | |
186 | ASSERT_EQ( | |
187 | must_read, | |
188 | to_read); | |
189 | ||
190 | c.print(std::cerr); | |
191 | ||
192 | // start write 2 | |
193 | ExtentCache::write_pin pin2; | |
194 | c.open_write_pin(pin2); | |
195 | auto to_read2 = extent_set(); | |
196 | auto to_write2 = iset_from_vector( | |
197 | {{663552, 4096}}); | |
198 | auto must_read2 = c.reserve_extents_for_rmw( | |
199 | oid, pin2, to_write2, to_read2); | |
200 | ASSERT_EQ( | |
201 | must_read2, | |
202 | to_read2); | |
203 | ||
204 | ||
205 | // start write 3 | |
206 | ExtentCache::write_pin pin3; | |
207 | c.open_write_pin(pin3); | |
208 | auto to_read3 = iset_from_vector({{659456, 8192}}); | |
209 | auto to_write3 = iset_from_vector({{659456, 8192}}); | |
210 | auto must_read3 = c.reserve_extents_for_rmw( | |
211 | oid, pin3, to_write3, to_read3); | |
212 | ASSERT_EQ( | |
213 | must_read3, | |
214 | extent_set()); | |
215 | ||
216 | c.print(std::cerr); | |
217 | ||
218 | // complete read for write 1 and start commit | |
219 | auto got = imap_from_iset(must_read); | |
220 | auto pending_read = to_read; | |
221 | pending_read.subtract(must_read); | |
222 | auto pending = c.get_remaining_extents_for_rmw( | |
223 | oid, | |
224 | pin, | |
225 | pending_read); | |
226 | ASSERT_TRUE(pending.empty()); | |
227 | ||
228 | auto write_map = imap_from_iset(to_write); | |
229 | c.present_rmw_update( | |
230 | oid, | |
231 | pin, | |
232 | write_map); | |
233 | ||
234 | c.print(std::cerr); | |
235 | ||
236 | // complete read for write 2 and start commit | |
237 | auto pending_read2 = to_read2; | |
238 | pending_read2.subtract(must_read2); | |
239 | auto pending2 = c.get_remaining_extents_for_rmw( | |
240 | oid, | |
241 | pin2, | |
242 | pending_read2); | |
243 | ASSERT_EQ( | |
244 | pending2, | |
245 | imap_from_iset(pending_read2)); | |
246 | ||
247 | auto write_map2 = imap_from_iset(to_write2); | |
248 | c.present_rmw_update( | |
249 | oid, | |
250 | pin2, | |
251 | write_map2); | |
252 | ||
253 | // complete read for write 2 and start commit | |
254 | auto pending_read3 = to_read3; | |
255 | pending_read3.subtract(must_read3); | |
256 | auto pending3 = c.get_remaining_extents_for_rmw( | |
257 | oid, | |
258 | pin3, | |
259 | pending_read3); | |
260 | ASSERT_EQ( | |
261 | pending3, | |
262 | imap_from_iset(pending_read3)); | |
263 | ||
264 | auto write_map3 = imap_from_iset(to_write3); | |
265 | c.present_rmw_update( | |
266 | oid, | |
267 | pin3, | |
268 | write_map3); | |
269 | ||
270 | ||
271 | c.print(std::cerr); | |
272 | ||
273 | c.release_write_pin(pin); | |
274 | ||
275 | c.print(std::cerr); | |
276 | ||
277 | c.release_write_pin(pin2); | |
278 | ||
279 | c.print(std::cerr); | |
280 | ||
281 | c.release_write_pin(pin3); | |
282 | } |