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