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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "common/ceph_context.h"
#include "common/ceph_json.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/WorkQueue.h"
#include "cls/cephfs/cls_cephfs_client.h"
#include "include/stringify.h"
#include "aio_utils.h"
#include "InstanceWatcher.h"
#include "Types.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_cephfs_mirror
#undef dout_prefix
#define dout_prefix *_dout << "cephfs::mirror::InstanceWatcher " << __func__
using namespace std;
namespace cephfs {
namespace mirror {
namespace {
std::string instance_oid(const std::string &instance_id) {
return CEPHFS_MIRROR_OBJECT + "." + instance_id;
}
} // anonymous namespace
InstanceWatcher::InstanceWatcher(librados::IoCtx &ioctx,
Listener &listener, ContextWQ *work_queue)
: Watcher(ioctx, instance_oid(stringify(ioctx.get_instance_id())), work_queue),
m_ioctx(ioctx),
m_listener(listener),
m_work_queue(work_queue),
m_lock(ceph::make_mutex("cephfs::mirror::instance_watcher")) {
}
InstanceWatcher::~InstanceWatcher() {
}
void InstanceWatcher::init(Context *on_finish) {
dout(20) << dendl;
{
std::scoped_lock locker(m_lock);
ceph_assert(m_on_init_finish == nullptr);
m_on_init_finish = new LambdaContext([this, on_finish](int r) {
on_finish->complete(r);
if (m_on_shutdown_finish != nullptr) {
m_on_shutdown_finish->complete(0);
}
});
}
create_instance();
}
void InstanceWatcher::shutdown(Context *on_finish) {
dout(20) << dendl;
{
std::scoped_lock locker(m_lock);
ceph_assert(m_on_shutdown_finish == nullptr);
if (m_on_init_finish != nullptr) {
dout(10) << ": delaying shutdown -- init in progress" << dendl;
m_on_shutdown_finish = new LambdaContext([this, on_finish](int r) {
m_on_shutdown_finish = nullptr;
shutdown(on_finish);
});
return;
}
m_on_shutdown_finish = on_finish;
}
unregister_watcher();
}
void InstanceWatcher::handle_notify(uint64_t notify_id, uint64_t handle,
uint64_t notifier_id, bufferlist& bl) {
dout(20) << dendl;
std::string dir_path;
std::string mode;
try {
JSONDecoder jd(bl);
JSONDecoder::decode_json("dir_path", dir_path, &jd.parser, true);
JSONDecoder::decode_json("mode", mode, &jd.parser, true);
} catch (const JSONDecoder::err &e) {
derr << ": failed to decode notify json: " << e.what() << dendl;
}
dout(20) << ": notifier_id=" << notifier_id << ", dir_path=" << dir_path
<< ", mode=" << mode << dendl;
if (mode == "acquire") {
m_listener.acquire_directory(dir_path);
} else if (mode == "release") {
m_listener.release_directory(dir_path);
} else {
derr << ": unknown mode" << dendl;
}
bufferlist outbl;
acknowledge_notify(notify_id, handle, outbl);
}
void InstanceWatcher::handle_rewatch_complete(int r) {
dout(5) << ": r=" << r << dendl;
if (r == -EBLOCKLISTED) {
dout(0) << ": client blocklisted" <<dendl;
std::scoped_lock locker(m_lock);
m_blocklisted = true;
m_blocklisted_ts = ceph_clock_now();
} else if (r == -ENOENT) {
derr << ": mirroring object deleted" << dendl;
m_failed = true;
m_failed_ts = ceph_clock_now();
} else if (r < 0) {
derr << ": rewatch error: " << cpp_strerror(r) << dendl;
m_failed = true;
m_failed_ts = ceph_clock_now();
}
}
void InstanceWatcher::create_instance() {
dout(20) << dendl;
std::scoped_lock locker(m_lock);
librados::ObjectWriteOperation op;
op.create(false);
librados::AioCompletion *aio_comp =
librados::Rados::aio_create_completion(
this, &rados_callback<InstanceWatcher, &InstanceWatcher::handle_create_instance>);
int r = m_ioctx.aio_operate(m_oid, aio_comp, &op);
ceph_assert(r == 0);
aio_comp->release();
}
void InstanceWatcher::handle_create_instance(int r) {
dout(20) << ": r=" << r << dendl;
Context *on_init_finish = nullptr;
{
std::scoped_lock locker(m_lock);
if (r < 0) {
std::swap(on_init_finish, m_on_init_finish);
}
}
if (on_init_finish != nullptr) {
on_init_finish->complete(r);
return;
}
register_watcher();
}
void InstanceWatcher::register_watcher() {
dout(20) << dendl;
std::scoped_lock locker(m_lock);
Context *on_finish = new C_CallbackAdapter<
InstanceWatcher, &InstanceWatcher::handle_register_watcher>(this);
register_watch(on_finish);
}
void InstanceWatcher::handle_register_watcher(int r) {
dout(20) << ": r=" << r << dendl;
Context *on_init_finish = nullptr;
{
std::scoped_lock locker(m_lock);
if (r == 0) {
std::swap(on_init_finish, m_on_init_finish);
}
}
if (on_init_finish != nullptr) {
on_init_finish->complete(r);
return;
}
remove_instance();
}
void InstanceWatcher::unregister_watcher() {
dout(20) << dendl;
std::scoped_lock locker(m_lock);
Context *on_finish = new C_CallbackAdapter<
InstanceWatcher, &InstanceWatcher::handle_unregister_watcher>(this);
unregister_watch(new C_AsyncCallback<ContextWQ>(m_work_queue, on_finish));
}
void InstanceWatcher::handle_unregister_watcher(int r) {
dout(20) << ": r=" << r << dendl;
Context *on_shutdown_finish = nullptr;
{
std::scoped_lock locker(m_lock);
if (r < 0) {
std::swap(on_shutdown_finish, m_on_shutdown_finish);
}
}
if (on_shutdown_finish != nullptr) {
on_shutdown_finish->complete(r);
return;
}
remove_instance();
}
void InstanceWatcher::remove_instance() {
dout(20) << dendl;
std::scoped_lock locker(m_lock);
librados::ObjectWriteOperation op;
op.remove();
librados::AioCompletion *aio_comp =
librados::Rados::aio_create_completion(
this, &rados_callback<InstanceWatcher, &InstanceWatcher::handle_remove_instance>);
int r = m_ioctx.aio_operate(m_oid, aio_comp, &op);
ceph_assert(r == 0);
aio_comp->release();
}
void InstanceWatcher::handle_remove_instance(int r) {
dout(20) << ": r=" << r << dendl;
Context *on_init_finish = nullptr;
Context *on_shutdown_finish = nullptr;
{
std::scoped_lock locker(m_lock);
std::swap(on_init_finish, m_on_init_finish);
std::swap(on_shutdown_finish, m_on_shutdown_finish);
}
if (on_init_finish != nullptr) {
on_init_finish->complete(r);
}
if (on_shutdown_finish != nullptr) {
on_shutdown_finish->complete(r);
}
}
} // namespace mirror
} // namespace cephfs
|