]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/rbd_mirror/test_mock_Throttler.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / test / rbd_mirror / test_mock_Throttler.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 SUSE LINUX GmbH
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 #include "test/rbd_mirror/test_mock_fixture.h"
16 #include "test/librbd/mock/MockImageCtx.h"
17
18 namespace librbd {
19
20 namespace {
21
22 struct MockTestImageCtx : public librbd::MockImageCtx {
23 MockTestImageCtx(librbd::ImageCtx &image_ctx)
24 : librbd::MockImageCtx(image_ctx) {
25 }
26 };
27
28 } // anonymous namespace
29
30 } // namespace librbd
31
32 // template definitions
33 #include "tools/rbd_mirror/Throttler.cc"
34
35 namespace rbd {
36 namespace mirror {
37
38 class TestMockThrottler : public TestMockFixture {
39 public:
40 typedef Throttler<librbd::MockTestImageCtx> MockThrottler;
41
42 };
43
44 TEST_F(TestMockThrottler, Single_Sync) {
45 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
46 C_SaferCond on_start;
47 throttler.start_op("ns", "id", &on_start);
48 ASSERT_EQ(0, on_start.wait());
49 throttler.finish_op("ns", "id");
50 }
51
52 TEST_F(TestMockThrottler, Multiple_Syncs) {
53 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
54 throttler.set_max_concurrent_ops(2);
55
56 C_SaferCond on_start1;
57 throttler.start_op("ns", "id1", &on_start1);
58 C_SaferCond on_start2;
59 throttler.start_op("ns", "id2", &on_start2);
60 C_SaferCond on_start3;
61 throttler.start_op("ns", "id3", &on_start3);
62 C_SaferCond on_start4;
63 throttler.start_op("ns", "id4", &on_start4);
64
65 ASSERT_EQ(0, on_start2.wait());
66 throttler.finish_op("ns", "id2");
67 ASSERT_EQ(0, on_start3.wait());
68 throttler.finish_op("ns", "id3");
69 ASSERT_EQ(0, on_start1.wait());
70 throttler.finish_op("ns", "id1");
71 ASSERT_EQ(0, on_start4.wait());
72 throttler.finish_op("ns", "id4");
73 }
74
75 TEST_F(TestMockThrottler, Cancel_Running_Sync) {
76 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
77 C_SaferCond on_start;
78 throttler.start_op("ns", "id", &on_start);
79 ASSERT_EQ(0, on_start.wait());
80 ASSERT_FALSE(throttler.cancel_op("ns", "id"));
81 throttler.finish_op("ns", "id");
82 }
83
84 TEST_F(TestMockThrottler, Cancel_Waiting_Sync) {
85 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
86 throttler.set_max_concurrent_ops(1);
87
88 C_SaferCond on_start1;
89 throttler.start_op("ns", "id1", &on_start1);
90 C_SaferCond on_start2;
91 throttler.start_op("ns", "id2", &on_start2);
92
93 ASSERT_EQ(0, on_start1.wait());
94 ASSERT_TRUE(throttler.cancel_op("ns", "id2"));
95 ASSERT_EQ(-ECANCELED, on_start2.wait());
96 throttler.finish_op("ns", "id1");
97 }
98
99 TEST_F(TestMockThrottler, Cancel_Running_Sync_Start_Waiting) {
100 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
101 throttler.set_max_concurrent_ops(1);
102
103 C_SaferCond on_start1;
104 throttler.start_op("ns", "id1", &on_start1);
105 C_SaferCond on_start2;
106 throttler.start_op("ns", "id2", &on_start2);
107
108 ASSERT_EQ(0, on_start1.wait());
109 ASSERT_FALSE(throttler.cancel_op("ns", "id1"));
110 throttler.finish_op("ns", "id1");
111 ASSERT_EQ(0, on_start2.wait());
112 throttler.finish_op("ns", "id2");
113 }
114
115 TEST_F(TestMockThrottler, Duplicate) {
116 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
117 throttler.set_max_concurrent_ops(1);
118
119 C_SaferCond on_start1;
120 throttler.start_op("ns", "id1", &on_start1);
121 ASSERT_EQ(0, on_start1.wait());
122
123 C_SaferCond on_start2;
124 throttler.start_op("ns", "id1", &on_start2);
125 ASSERT_EQ(0, on_start2.wait());
126
127 C_SaferCond on_start3;
128 throttler.start_op("ns", "id2", &on_start3);
129 C_SaferCond on_start4;
130 throttler.start_op("ns", "id2", &on_start4);
131 ASSERT_EQ(-ENOENT, on_start3.wait());
132
133 throttler.finish_op("ns", "id1");
134 ASSERT_EQ(0, on_start4.wait());
135 throttler.finish_op("ns", "id2");
136 }
137
138 TEST_F(TestMockThrottler, Duplicate2) {
139 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
140 throttler.set_max_concurrent_ops(2);
141
142 C_SaferCond on_start1;
143 throttler.start_op("ns", "id1", &on_start1);
144 ASSERT_EQ(0, on_start1.wait());
145 C_SaferCond on_start2;
146 throttler.start_op("ns", "id2", &on_start2);
147 ASSERT_EQ(0, on_start2.wait());
148
149 C_SaferCond on_start3;
150 throttler.start_op("ns", "id3", &on_start3);
151 C_SaferCond on_start4;
152 throttler.start_op("ns", "id3", &on_start4); // dup
153 ASSERT_EQ(-ENOENT, on_start3.wait());
154
155 C_SaferCond on_start5;
156 throttler.start_op("ns", "id4", &on_start5);
157
158 throttler.finish_op("ns", "id1");
159 ASSERT_EQ(0, on_start4.wait());
160
161 throttler.finish_op("ns", "id2");
162 ASSERT_EQ(0, on_start5.wait());
163
164 C_SaferCond on_start6;
165 throttler.start_op("ns", "id5", &on_start6);
166
167 throttler.finish_op("ns", "id3");
168 ASSERT_EQ(0, on_start6.wait());
169
170 throttler.finish_op("ns", "id4");
171 throttler.finish_op("ns", "id5");
172 }
173
174 TEST_F(TestMockThrottler, Increase_Max_Concurrent_Syncs) {
175 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
176 throttler.set_max_concurrent_ops(2);
177
178 C_SaferCond on_start1;
179 throttler.start_op("ns", "id1", &on_start1);
180 C_SaferCond on_start2;
181 throttler.start_op("ns", "id2", &on_start2);
182 C_SaferCond on_start3;
183 throttler.start_op("ns", "id3", &on_start3);
184 C_SaferCond on_start4;
185 throttler.start_op("ns", "id4", &on_start4);
186 C_SaferCond on_start5;
187 throttler.start_op("ns", "id5", &on_start5);
188
189 ASSERT_EQ(0, on_start1.wait());
190 ASSERT_EQ(0, on_start2.wait());
191
192 throttler.set_max_concurrent_ops(4);
193
194 ASSERT_EQ(0, on_start3.wait());
195 ASSERT_EQ(0, on_start4.wait());
196
197 throttler.finish_op("ns", "id4");
198 ASSERT_EQ(0, on_start5.wait());
199
200 throttler.finish_op("ns", "id1");
201 throttler.finish_op("ns", "id2");
202 throttler.finish_op("ns", "id3");
203 throttler.finish_op("ns", "id5");
204 }
205
206 TEST_F(TestMockThrottler, Decrease_Max_Concurrent_Syncs) {
207 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
208 throttler.set_max_concurrent_ops(4);
209
210 C_SaferCond on_start1;
211 throttler.start_op("ns", "id1", &on_start1);
212 C_SaferCond on_start2;
213 throttler.start_op("ns", "id2", &on_start2);
214 C_SaferCond on_start3;
215 throttler.start_op("ns", "id3", &on_start3);
216 C_SaferCond on_start4;
217 throttler.start_op("ns", "id4", &on_start4);
218 C_SaferCond on_start5;
219 throttler.start_op("ns", "id5", &on_start5);
220
221 ASSERT_EQ(0, on_start1.wait());
222 ASSERT_EQ(0, on_start2.wait());
223 ASSERT_EQ(0, on_start3.wait());
224 ASSERT_EQ(0, on_start4.wait());
225
226 throttler.set_max_concurrent_ops(2);
227
228 throttler.finish_op("ns", "id1");
229 throttler.finish_op("ns", "id2");
230 throttler.finish_op("ns", "id3");
231
232 ASSERT_EQ(0, on_start5.wait());
233
234 throttler.finish_op("ns", "id4");
235 throttler.finish_op("ns", "id5");
236 }
237
238 TEST_F(TestMockThrottler, Drain) {
239 MockThrottler throttler(g_ceph_context, "rbd_mirror_concurrent_image_syncs");
240 throttler.set_max_concurrent_ops(1);
241
242 C_SaferCond on_start1;
243 throttler.start_op("ns", "id1", &on_start1);
244 C_SaferCond on_start2;
245 throttler.start_op("ns", "id2", &on_start2);
246
247 ASSERT_EQ(0, on_start1.wait());
248 throttler.drain("ns", -ESTALE);
249 ASSERT_EQ(-ESTALE, on_start2.wait());
250 }
251
252 } // namespace mirror
253 } // namespace rbd