]>
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 | #include "test/librbd/test_mock_fixture.h" | |
5 | #include "test/librbd/test_support.h" | |
6 | #include "test/librados_test_stub/MockTestMemIoCtxImpl.h" | |
7 | #include "common/bit_vector.hpp" | |
8 | #include "librbd/ImageState.h" | |
9 | #include "librbd/internal.h" | |
10 | #include "librbd/ObjectMap.h" | |
11 | #include "librbd/object_map/SnapshotRemoveRequest.h" | |
12 | #include "gmock/gmock.h" | |
13 | #include "gtest/gtest.h" | |
14 | ||
15 | namespace librbd { | |
16 | namespace object_map { | |
17 | ||
18 | using ::testing::_; | |
19 | using ::testing::DoDefault; | |
20 | using ::testing::Return; | |
21 | using ::testing::StrEq; | |
22 | ||
23 | class TestMockObjectMapSnapshotRemoveRequest : public TestMockFixture { | |
24 | public: | |
25 | void expect_load_map(librbd::ImageCtx *ictx, uint64_t snap_id, int r) { | |
26 | std::string snap_oid(ObjectMap<>::object_map_name(ictx->id, snap_id)); | |
27 | if (r < 0) { | |
28 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
29 | exec(snap_oid, _, StrEq("rbd"), StrEq("object_map_load"), _, _, _)) | |
30 | .WillOnce(Return(r)); | |
31 | } else { | |
32 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
33 | exec(snap_oid, _, StrEq("rbd"), StrEq("object_map_load"), _, _, _)) | |
34 | .WillOnce(DoDefault()); | |
35 | } | |
36 | } | |
37 | ||
38 | void expect_remove_snapshot(librbd::ImageCtx *ictx, int r) { | |
39 | std::string oid(ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP)); | |
40 | if (r < 0) { | |
41 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
42 | exec(oid, _, StrEq("lock"), StrEq("assert_locked"), _, _, _)) | |
43 | .WillOnce(Return(r)); | |
44 | } else { | |
45 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
46 | exec(oid, _, StrEq("lock"), StrEq("assert_locked"), _, _, _)) | |
47 | .WillOnce(DoDefault()); | |
48 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
49 | exec(oid, _, StrEq("rbd"), StrEq("object_map_snap_remove"), _, _, _)) | |
50 | .WillOnce(DoDefault()); | |
51 | } | |
52 | } | |
53 | ||
54 | void expect_remove_map(librbd::ImageCtx *ictx, uint64_t snap_id, int r) { | |
55 | std::string snap_oid(ObjectMap<>::object_map_name(ictx->id, snap_id)); | |
56 | if (r < 0) { | |
57 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), remove(snap_oid, _)) | |
58 | .WillOnce(Return(r)); | |
59 | } else { | |
60 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), remove(snap_oid, _)) | |
61 | .WillOnce(DoDefault()); | |
62 | } | |
63 | } | |
64 | ||
65 | void expect_invalidate(librbd::ImageCtx *ictx) { | |
66 | EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx), | |
67 | exec(ictx->header_oid, _, StrEq("rbd"), StrEq("set_flags"), _, _, _)) | |
68 | .WillOnce(DoDefault()); | |
69 | } | |
70 | }; | |
71 | ||
72 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, Success) { | |
73 | REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP); | |
74 | ||
75 | librbd::ImageCtx *ictx; | |
76 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
77 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
78 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
79 | ||
80 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
81 | if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) { | |
82 | expect_load_map(ictx, snap_id, 0); | |
83 | expect_remove_snapshot(ictx, 0); | |
84 | } | |
85 | expect_remove_map(ictx, snap_id, 0); | |
86 | ||
9f95a23c | 87 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
88 | ceph::BitVector<2> object_map; |
89 | C_SaferCond cond_ctx; | |
90 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 91 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 92 | { |
9f95a23c TL |
93 | std::shared_lock owner_locker{ictx->owner_lock}; |
94 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
95 | request->send(); |
96 | } | |
97 | ASSERT_EQ(0, cond_ctx.wait()); | |
98 | ||
99 | expect_unlock_exclusive_lock(*ictx); | |
100 | } | |
101 | ||
102 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, LoadMapMissing) { | |
103 | REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF); | |
104 | ||
105 | librbd::ImageCtx *ictx; | |
106 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
107 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
108 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
109 | ||
110 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
91327a77 AA |
111 | auto snap_it = ictx->snap_info.find(snap_id); |
112 | ASSERT_NE(ictx->snap_info.end(), snap_it); | |
113 | snap_it->second.flags |= RBD_FLAG_OBJECT_MAP_INVALID; | |
114 | ||
7c673cae FG |
115 | expect_load_map(ictx, snap_id, -ENOENT); |
116 | ||
9f95a23c | 117 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
118 | ceph::BitVector<2> object_map; |
119 | C_SaferCond cond_ctx; | |
120 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 121 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 122 | { |
9f95a23c TL |
123 | std::shared_lock owner_locker{ictx->owner_lock}; |
124 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
125 | request->send(); |
126 | } | |
127 | ASSERT_EQ(0, cond_ctx.wait()); | |
128 | ||
91327a77 AA |
129 | { |
130 | // shouldn't invalidate the HEAD revision when we fail to load | |
131 | // the already deleted snapshot | |
9f95a23c | 132 | std::shared_lock image_locker{ictx->image_lock}; |
91327a77 AA |
133 | uint64_t flags; |
134 | ASSERT_EQ(0, ictx->get_flags(CEPH_NOSNAP, &flags)); | |
135 | ASSERT_EQ(0U, flags & RBD_FLAG_OBJECT_MAP_INVALID); | |
136 | } | |
137 | ||
7c673cae FG |
138 | expect_unlock_exclusive_lock(*ictx); |
139 | } | |
140 | ||
141 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, LoadMapError) { | |
142 | REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF); | |
143 | ||
144 | librbd::ImageCtx *ictx; | |
145 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
146 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
147 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
148 | ||
149 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
150 | expect_load_map(ictx, snap_id, -EINVAL); | |
151 | expect_invalidate(ictx); | |
152 | expect_remove_map(ictx, snap_id, 0); | |
153 | ||
9f95a23c | 154 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
155 | ceph::BitVector<2> object_map; |
156 | C_SaferCond cond_ctx; | |
157 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 158 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 159 | { |
9f95a23c TL |
160 | std::shared_lock owner_locker{ictx->owner_lock}; |
161 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
162 | request->send(); |
163 | } | |
164 | ASSERT_EQ(0, cond_ctx.wait()); | |
165 | ||
166 | expect_unlock_exclusive_lock(*ictx); | |
167 | } | |
168 | ||
169 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, RemoveSnapshotMissing) { | |
170 | REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF); | |
171 | ||
172 | librbd::ImageCtx *ictx; | |
173 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
174 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
175 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
176 | ||
177 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
178 | expect_load_map(ictx, snap_id, 0); | |
179 | expect_remove_snapshot(ictx, -ENOENT); | |
180 | expect_remove_map(ictx, snap_id, 0); | |
181 | ||
9f95a23c | 182 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
183 | ceph::BitVector<2> object_map; |
184 | C_SaferCond cond_ctx; | |
185 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 186 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 187 | { |
9f95a23c TL |
188 | std::shared_lock owner_locker{ictx->owner_lock}; |
189 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
190 | request->send(); |
191 | } | |
192 | ASSERT_EQ(0, cond_ctx.wait()); | |
193 | ||
194 | expect_unlock_exclusive_lock(*ictx); | |
195 | } | |
196 | ||
197 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, RemoveSnapshotError) { | |
198 | REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF); | |
199 | ||
200 | librbd::ImageCtx *ictx; | |
201 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
202 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
203 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
204 | ||
205 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
206 | expect_load_map(ictx, snap_id, 0); | |
207 | expect_remove_snapshot(ictx, -EINVAL); | |
208 | expect_invalidate(ictx); | |
209 | expect_remove_map(ictx, snap_id, 0); | |
210 | ||
9f95a23c | 211 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
212 | ceph::BitVector<2> object_map; |
213 | C_SaferCond cond_ctx; | |
214 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 215 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 216 | { |
9f95a23c TL |
217 | std::shared_lock owner_locker{ictx->owner_lock}; |
218 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
219 | request->send(); |
220 | } | |
221 | ASSERT_EQ(0, cond_ctx.wait()); | |
222 | ||
223 | expect_unlock_exclusive_lock(*ictx); | |
224 | } | |
225 | ||
226 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, RemoveMapMissing) { | |
227 | REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP); | |
228 | ||
229 | librbd::ImageCtx *ictx; | |
230 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
231 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
232 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
233 | ||
234 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
235 | if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) { | |
236 | expect_load_map(ictx, snap_id, 0); | |
237 | expect_remove_snapshot(ictx, 0); | |
238 | } | |
239 | expect_remove_map(ictx, snap_id, -ENOENT); | |
240 | ||
9f95a23c | 241 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
242 | ceph::BitVector<2> object_map; |
243 | C_SaferCond cond_ctx; | |
244 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 245 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 246 | { |
9f95a23c TL |
247 | std::shared_lock owner_locker{ictx->owner_lock}; |
248 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
249 | request->send(); |
250 | } | |
251 | ASSERT_EQ(0, cond_ctx.wait()); | |
252 | ||
253 | expect_unlock_exclusive_lock(*ictx); | |
254 | } | |
255 | ||
256 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, RemoveMapError) { | |
257 | REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP); | |
258 | ||
259 | librbd::ImageCtx *ictx; | |
260 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
261 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); | |
262 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
263 | ||
264 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
265 | if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) { | |
266 | expect_load_map(ictx, snap_id, 0); | |
267 | expect_remove_snapshot(ictx, 0); | |
268 | } | |
269 | expect_remove_map(ictx, snap_id, -EINVAL); | |
270 | ||
9f95a23c | 271 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); |
7c673cae FG |
272 | ceph::BitVector<2> object_map; |
273 | C_SaferCond cond_ctx; | |
274 | AsyncRequest<> *request = new SnapshotRemoveRequest( | |
9f95a23c | 275 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx); |
7c673cae | 276 | { |
9f95a23c TL |
277 | std::shared_lock owner_locker{ictx->owner_lock}; |
278 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
279 | request->send(); |
280 | } | |
281 | ASSERT_EQ(-EINVAL, cond_ctx.wait()); | |
282 | ||
283 | expect_unlock_exclusive_lock(*ictx); | |
284 | } | |
285 | ||
286 | TEST_F(TestMockObjectMapSnapshotRemoveRequest, ScrubCleanObjects) { | |
287 | REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF); | |
288 | ||
289 | librbd::ImageCtx *ictx; | |
290 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
b32b8144 FG |
291 | librbd::NoOpProgressContext prog_ctx; |
292 | uint64_t size = 4294967296; // 4GB = 1024 * 4MB | |
293 | ASSERT_EQ(0, resize(ictx, size)); | |
9f95a23c TL |
294 | |
295 | // update image objectmap for snap inherit | |
296 | ceph::shared_mutex object_map_lock = ceph::make_shared_mutex("lock"); | |
b32b8144 FG |
297 | ceph::BitVector<2> object_map; |
298 | object_map.resize(1024); | |
299 | for (uint64_t i = 512; i < object_map.size(); ++i) { | |
300 | object_map[i] = i % 2 == 0 ? OBJECT_EXISTS : OBJECT_NONEXISTENT; | |
301 | } | |
9f95a23c | 302 | |
b32b8144 FG |
303 | C_SaferCond cond_ctx1; |
304 | { | |
9f95a23c TL |
305 | librbd::ObjectMap<> *om = new librbd::ObjectMap<>(*ictx, ictx->snap_id); |
306 | std::shared_lock owner_locker{ictx->owner_lock}; | |
307 | std::unique_lock image_locker{ictx->image_lock}; | |
308 | om->set_object_map(object_map); | |
309 | om->aio_save(&cond_ctx1); | |
310 | om->put(); | |
b32b8144 FG |
311 | } |
312 | ASSERT_EQ(0, cond_ctx1.wait()); | |
7c673cae FG |
313 | ASSERT_EQ(0, snap_create(*ictx, "snap1")); |
314 | ASSERT_EQ(0, ictx->state->refresh_if_required()); | |
315 | ||
b32b8144 | 316 | // simutate the image objectmap state after creating snap |
7c673cae FG |
317 | for (uint64_t i = 512; i < object_map.size(); ++i) { |
318 | object_map[i] = i % 2 == 0 ? OBJECT_EXISTS_CLEAN : OBJECT_NONEXISTENT; | |
319 | } | |
320 | ||
b32b8144 FG |
321 | C_SaferCond cond_ctx2; |
322 | uint64_t snap_id = ictx->snap_info.rbegin()->first; | |
7c673cae | 323 | AsyncRequest<> *request = new SnapshotRemoveRequest( |
9f95a23c | 324 | *ictx, &object_map_lock, &object_map, snap_id, &cond_ctx2); |
7c673cae | 325 | { |
9f95a23c TL |
326 | std::shared_lock owner_locker{ictx->owner_lock}; |
327 | std::unique_lock image_locker{ictx->image_lock}; | |
7c673cae FG |
328 | request->send(); |
329 | } | |
b32b8144 | 330 | ASSERT_EQ(0, cond_ctx2.wait()); |
7c673cae FG |
331 | |
332 | for (uint64_t i = 512; i < object_map.size(); ++i) { | |
333 | ASSERT_EQ(i % 2 == 0 ? OBJECT_EXISTS : OBJECT_NONEXISTENT, | |
334 | object_map[i]); | |
335 | } | |
336 | } | |
337 | ||
338 | } // namespace object_map | |
339 | } // namespace librbd |