1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/rados/librados.hpp"
#include "librbd/internal.h"
#include "librbd/Utils.h"
#include "librbd/api/Mirror.h"
#include "test/librbd/test_support.h"
#include "test/rbd_mirror/test_fixture.h"
#include "tools/rbd_mirror/LeaderWatcher.h"
#include "tools/rbd_mirror/Threads.h"
#include "common/Cond.h"
#include "test/librados/test_cxx.h"
#include "gtest/gtest.h"
using librbd::util::unique_lock_name;
using rbd::mirror::LeaderWatcher;
void register_test_leader_watcher() {
}
class TestLeaderWatcher : public ::rbd::mirror::TestFixture {
public:
class Listener : public rbd::mirror::leader_watcher::Listener {
public:
Listener()
: m_test_lock(ceph::make_mutex(
unique_lock_name("LeaderWatcher::m_test_lock", this))) {
}
void on_acquire(int r, Context *ctx) {
std::lock_guard locker{m_test_lock};
m_on_acquire_r = r;
m_on_acquire = ctx;
}
void on_release(int r, Context *ctx) {
std::lock_guard locker{m_test_lock};
m_on_release_r = r;
m_on_release = ctx;
}
int acquire_count() const {
std::lock_guard locker{m_test_lock};
return m_acquire_count;
}
int release_count() const {
std::lock_guard locker{m_test_lock};
return m_release_count;
}
void post_acquire_handler(Context *on_finish) override {
std::lock_guard locker{m_test_lock};
m_acquire_count++;
on_finish->complete(m_on_acquire_r);
m_on_acquire_r = 0;
if (m_on_acquire != nullptr) {
m_on_acquire->complete(0);
m_on_acquire = nullptr;
}
}
void pre_release_handler(Context *on_finish) override {
std::lock_guard locker{m_test_lock};
m_release_count++;
on_finish->complete(m_on_release_r);
m_on_release_r = 0;
if (m_on_release != nullptr) {
m_on_release->complete(0);
m_on_release = nullptr;
}
}
void update_leader_handler(const std::string &leader_instance_id) override {
}
void handle_instances_added(const InstanceIds& instance_ids) override {
}
void handle_instances_removed(const InstanceIds& instance_ids) override {
}
private:
mutable ceph::mutex m_test_lock;
int m_acquire_count = 0;
int m_release_count = 0;
int m_on_acquire_r = 0;
int m_on_release_r = 0;
Context *m_on_acquire = nullptr;
Context *m_on_release = nullptr;
};
struct Connection {
librados::Rados cluster;
librados::IoCtx io_ctx;
};
std::list<std::unique_ptr<Connection> > m_connections;
void SetUp() override {
TestFixture::SetUp();
EXPECT_EQ(0, librbd::api::Mirror<>::mode_set(m_local_io_ctx,
RBD_MIRROR_MODE_POOL));
if (is_librados_test_stub(*_rados)) {
// speed testing up a little
EXPECT_EQ(0, _rados->conf_set("rbd_mirror_leader_heartbeat_interval",
"1"));
}
}
librados::IoCtx &create_connection(bool no_heartbeats = false) {
m_connections.push_back(std::unique_ptr<Connection>(new Connection()));
Connection *c = m_connections.back().get();
EXPECT_EQ("", connect_cluster_pp(c->cluster));
if (no_heartbeats) {
EXPECT_EQ(0, c->cluster.conf_set("rbd_mirror_leader_heartbeat_interval",
"3600"));
} else if (is_librados_test_stub(*_rados)) {
EXPECT_EQ(0, c->cluster.conf_set("rbd_mirror_leader_heartbeat_interval",
"1"));
}
EXPECT_EQ(0, c->cluster.ioctx_create(_local_pool_name.c_str(), c->io_ctx));
return c->io_ctx;
}
};
TEST_F(TestLeaderWatcher, InitShutdown)
{
Listener listener;
LeaderWatcher<> leader_watcher(m_threads, m_local_io_ctx, &listener);
C_SaferCond on_init_acquire;
listener.on_acquire(0, &on_init_acquire);
ASSERT_EQ(0, leader_watcher.init());
ASSERT_EQ(0, on_init_acquire.wait());
ASSERT_TRUE(leader_watcher.is_leader());
leader_watcher.shut_down();
ASSERT_EQ(1, listener.acquire_count());
ASSERT_EQ(1, listener.release_count());
ASSERT_FALSE(leader_watcher.is_leader());
}
TEST_F(TestLeaderWatcher, Release)
{
Listener listener;
LeaderWatcher<> leader_watcher(m_threads, m_local_io_ctx, &listener);
C_SaferCond on_init_acquire;
listener.on_acquire(0, &on_init_acquire);
ASSERT_EQ(0, leader_watcher.init());
ASSERT_EQ(0, on_init_acquire.wait());
ASSERT_TRUE(leader_watcher.is_leader());
C_SaferCond on_release;
C_SaferCond on_acquire;
listener.on_release(0, &on_release);
listener.on_acquire(0, &on_acquire);
leader_watcher.release_leader();
ASSERT_EQ(0, on_release.wait());
ASSERT_FALSE(leader_watcher.is_leader());
// wait for lock re-acquired due to no another locker
ASSERT_EQ(0, on_acquire.wait());
ASSERT_TRUE(leader_watcher.is_leader());
C_SaferCond on_release2;
listener.on_release(0, &on_release2);
leader_watcher.release_leader();
ASSERT_EQ(0, on_release2.wait());
leader_watcher.shut_down();
ASSERT_EQ(2, listener.acquire_count());
ASSERT_EQ(2, listener.release_count());
}
TEST_F(TestLeaderWatcher, ListenerError)
{
Listener listener;
LeaderWatcher<> leader_watcher(m_threads, m_local_io_ctx, &listener);
// make listener return error on acquire
C_SaferCond on_init_acquire, on_init_release;
listener.on_acquire(-EINVAL, &on_init_acquire);
listener.on_release(0, &on_init_release);
ASSERT_EQ(0, leader_watcher.init());
ASSERT_EQ(0, on_init_acquire.wait());
ASSERT_EQ(0, on_init_release.wait());
ASSERT_FALSE(leader_watcher.is_leader());
// wait for lock re-acquired due to no another locker
C_SaferCond on_acquire;
listener.on_acquire(0, &on_acquire);
ASSERT_EQ(0, on_acquire.wait());
ASSERT_TRUE(leader_watcher.is_leader());
// make listener return error on release
C_SaferCond on_release;
listener.on_release(-EINVAL, &on_release);
leader_watcher.release_leader();
ASSERT_EQ(0, on_release.wait());
ASSERT_FALSE(leader_watcher.is_leader());
leader_watcher.shut_down();
ASSERT_EQ(2, listener.acquire_count());
ASSERT_EQ(2, listener.release_count());
ASSERT_FALSE(leader_watcher.is_leader());
}
TEST_F(TestLeaderWatcher, Two)
{
Listener listener1;
LeaderWatcher<> leader_watcher1(m_threads, create_connection(), &listener1);
C_SaferCond on_init_acquire;
listener1.on_acquire(0, &on_init_acquire);
ASSERT_EQ(0, leader_watcher1.init());
ASSERT_EQ(0, on_init_acquire.wait());
Listener listener2;
LeaderWatcher<> leader_watcher2(m_threads, create_connection(), &listener2);
ASSERT_EQ(0, leader_watcher2.init());
ASSERT_TRUE(leader_watcher1.is_leader());
ASSERT_FALSE(leader_watcher2.is_leader());
C_SaferCond on_release;
C_SaferCond on_acquire;
listener1.on_release(0, &on_release);
listener2.on_acquire(0, &on_acquire);
leader_watcher1.release_leader();
ASSERT_EQ(0, on_release.wait());
ASSERT_FALSE(leader_watcher1.is_leader());
// wait for lock acquired by another watcher
ASSERT_EQ(0, on_acquire.wait());
ASSERT_TRUE(leader_watcher2.is_leader());
leader_watcher1.shut_down();
leader_watcher2.shut_down();
ASSERT_EQ(1, listener1.acquire_count());
ASSERT_EQ(1, listener1.release_count());
ASSERT_EQ(1, listener2.acquire_count());
ASSERT_EQ(1, listener2.release_count());
}
TEST_F(TestLeaderWatcher, Break)
{
Listener listener1, listener2;
LeaderWatcher<> leader_watcher1(m_threads,
create_connection(true /* no heartbeats */),
&listener1);
LeaderWatcher<> leader_watcher2(m_threads, create_connection(), &listener2);
C_SaferCond on_init_acquire;
listener1.on_acquire(0, &on_init_acquire);
ASSERT_EQ(0, leader_watcher1.init());
ASSERT_EQ(0, on_init_acquire.wait());
C_SaferCond on_acquire;
listener2.on_acquire(0, &on_acquire);
ASSERT_EQ(0, leader_watcher2.init());
ASSERT_FALSE(leader_watcher2.is_leader());
// wait for lock broken due to no heartbeats and re-acquired
ASSERT_EQ(0, on_acquire.wait());
ASSERT_TRUE(leader_watcher2.is_leader());
leader_watcher1.shut_down();
leader_watcher2.shut_down();
}
TEST_F(TestLeaderWatcher, Stress)
{
const int WATCHERS_COUNT = 20;
std::list<LeaderWatcher<> *> leader_watchers;
Listener listener;
for (int i = 0; i < WATCHERS_COUNT; i++) {
auto leader_watcher =
new LeaderWatcher<>(m_threads, create_connection(), &listener);
leader_watchers.push_back(leader_watcher);
}
C_SaferCond on_init_acquire;
listener.on_acquire(0, &on_init_acquire);
for (auto &leader_watcher : leader_watchers) {
ASSERT_EQ(0, leader_watcher->init());
}
ASSERT_EQ(0, on_init_acquire.wait());
while (true) {
C_SaferCond on_acquire;
listener.on_acquire(0, &on_acquire);
std::unique_ptr<LeaderWatcher<> > leader_watcher;
for (auto it = leader_watchers.begin(); it != leader_watchers.end(); ) {
if ((*it)->is_leader()) {
ASSERT_FALSE(leader_watcher);
leader_watcher.reset(*it);
it = leader_watchers.erase(it);
} else {
it++;
}
}
ASSERT_TRUE(leader_watcher);
leader_watcher->shut_down();
if (leader_watchers.empty()) {
break;
}
ASSERT_EQ(0, on_acquire.wait());
}
}
|