1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 SUSE LINUX GmbH
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.
15 #include "test/rbd_mirror/test_mock_fixture.h"
16 #include "test/librbd/mock/MockImageCtx.h"
22 struct MockTestImageCtx
: public librbd::MockImageCtx
{
23 MockTestImageCtx(librbd::ImageCtx
&image_ctx
)
24 : librbd::MockImageCtx(image_ctx
) {
28 } // anonymous namespace
32 // template definitions
33 #include "tools/rbd_mirror/Throttler.cc"
38 class TestMockThrottler
: public TestMockFixture
{
40 typedef Throttler
<librbd::MockTestImageCtx
> MockThrottler
;
44 TEST_F(TestMockThrottler
, Single_Sync
) {
45 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
47 throttler
.start_op("ns", "id", &on_start
);
48 ASSERT_EQ(0, on_start
.wait());
49 throttler
.finish_op("ns", "id");
52 TEST_F(TestMockThrottler
, Multiple_Syncs
) {
53 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
54 throttler
.set_max_concurrent_ops(2);
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
);
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");
75 TEST_F(TestMockThrottler
, Cancel_Running_Sync
) {
76 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
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");
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);
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
);
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");
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);
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
);
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");
115 TEST_F(TestMockThrottler
, Duplicate
) {
116 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
117 throttler
.set_max_concurrent_ops(1);
119 C_SaferCond on_start1
;
120 throttler
.start_op("ns", "id1", &on_start1
);
121 ASSERT_EQ(0, on_start1
.wait());
123 C_SaferCond on_start2
;
124 throttler
.start_op("ns", "id1", &on_start2
);
125 ASSERT_EQ(0, on_start2
.wait());
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());
133 throttler
.finish_op("ns", "id1");
134 ASSERT_EQ(0, on_start4
.wait());
135 throttler
.finish_op("ns", "id2");
138 TEST_F(TestMockThrottler
, Duplicate2
) {
139 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
140 throttler
.set_max_concurrent_ops(2);
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());
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());
155 C_SaferCond on_start5
;
156 throttler
.start_op("ns", "id4", &on_start5
);
158 throttler
.finish_op("ns", "id1");
159 ASSERT_EQ(0, on_start4
.wait());
161 throttler
.finish_op("ns", "id2");
162 ASSERT_EQ(0, on_start5
.wait());
164 C_SaferCond on_start6
;
165 throttler
.start_op("ns", "id5", &on_start6
);
167 throttler
.finish_op("ns", "id3");
168 ASSERT_EQ(0, on_start6
.wait());
170 throttler
.finish_op("ns", "id4");
171 throttler
.finish_op("ns", "id5");
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);
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
);
189 ASSERT_EQ(0, on_start1
.wait());
190 ASSERT_EQ(0, on_start2
.wait());
192 throttler
.set_max_concurrent_ops(4);
194 ASSERT_EQ(0, on_start3
.wait());
195 ASSERT_EQ(0, on_start4
.wait());
197 throttler
.finish_op("ns", "id4");
198 ASSERT_EQ(0, on_start5
.wait());
200 throttler
.finish_op("ns", "id1");
201 throttler
.finish_op("ns", "id2");
202 throttler
.finish_op("ns", "id3");
203 throttler
.finish_op("ns", "id5");
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);
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
);
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());
226 throttler
.set_max_concurrent_ops(2);
228 throttler
.finish_op("ns", "id1");
229 throttler
.finish_op("ns", "id2");
230 throttler
.finish_op("ns", "id3");
232 ASSERT_EQ(0, on_start5
.wait());
234 throttler
.finish_op("ns", "id4");
235 throttler
.finish_op("ns", "id5");
238 TEST_F(TestMockThrottler
, Drain
) {
239 MockThrottler
throttler(g_ceph_context
, "rbd_mirror_concurrent_image_syncs");
240 throttler
.set_max_concurrent_ops(1);
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
);
247 ASSERT_EQ(0, on_start1
.wait());
248 throttler
.drain("ns", -ESTALE
);
249 ASSERT_EQ(-ESTALE
, on_start2
.wait());
252 } // namespace mirror