]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/ImageReplayer.cc
d/control: depend on python3-yaml for ceph-mgr
[ceph.git] / ceph / src / tools / rbd_mirror / ImageReplayer.cc
CommitLineData
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 "include/compat.h"
5#include "common/Formatter.h"
11fdf7f2 6#include "common/admin_socket.h"
7c673cae
FG
7#include "common/debug.h"
8#include "common/errno.h"
9#include "include/stringify.h"
10#include "cls/rbd/cls_rbd_client.h"
11#include "common/Timer.h"
12#include "common/WorkQueue.h"
13#include "global/global_context.h"
14#include "journal/Journaler.h"
7c673cae
FG
15#include "librbd/ExclusiveLock.h"
16#include "librbd/ImageCtx.h"
17#include "librbd/ImageState.h"
18#include "librbd/Journal.h"
19#include "librbd/Operations.h"
20#include "librbd/Utils.h"
d2e6a577 21#include "ImageDeleter.h"
7c673cae 22#include "ImageReplayer.h"
9f95a23c 23#include "MirrorStatusUpdater.h"
7c673cae
FG
24#include "Threads.h"
25#include "tools/rbd_mirror/image_replayer/BootstrapRequest.h"
9f95a23c
TL
26#include "tools/rbd_mirror/image_replayer/ReplayerListener.h"
27#include "tools/rbd_mirror/image_replayer/StateBuilder.h"
28#include "tools/rbd_mirror/image_replayer/Utils.h"
29#include "tools/rbd_mirror/image_replayer/journal/Replayer.h"
30#include "tools/rbd_mirror/image_replayer/journal/StateBuilder.h"
31#include <map>
7c673cae
FG
32
33#define dout_context g_ceph_context
34#define dout_subsys ceph_subsys_rbd_mirror
35#undef dout_prefix
36#define dout_prefix *_dout << "rbd::mirror::" << *this << " " \
37 << __func__ << ": "
38
11fdf7f2
TL
39extern PerfCounters *g_perf_counters;
40
7c673cae
FG
41namespace rbd {
42namespace mirror {
43
44using librbd::util::create_context_callback;
7c673cae
FG
45
46template <typename I>
47std::ostream &operator<<(std::ostream &os,
48 const typename ImageReplayer<I>::State &state);
49
50namespace {
51
3efd9988 52template <typename I>
7c673cae
FG
53class ImageReplayerAdminSocketCommand {
54public:
3efd9988
FG
55 ImageReplayerAdminSocketCommand(const std::string &desc,
56 ImageReplayer<I> *replayer)
57 : desc(desc), replayer(replayer) {
58 }
7c673cae 59 virtual ~ImageReplayerAdminSocketCommand() {}
9f95a23c 60 virtual int call(Formatter *f) = 0;
3efd9988
FG
61
62 std::string desc;
63 ImageReplayer<I> *replayer;
64 bool registered = false;
7c673cae
FG
65};
66
67template <typename I>
3efd9988 68class StatusCommand : public ImageReplayerAdminSocketCommand<I> {
7c673cae 69public:
3efd9988
FG
70 explicit StatusCommand(const std::string &desc, ImageReplayer<I> *replayer)
71 : ImageReplayerAdminSocketCommand<I>(desc, replayer) {
72 }
7c673cae 73
9f95a23c
TL
74 int call(Formatter *f) override {
75 this->replayer->print_status(f);
76 return 0;
7c673cae 77 }
7c673cae
FG
78};
79
80template <typename I>
3efd9988 81class StartCommand : public ImageReplayerAdminSocketCommand<I> {
7c673cae 82public:
3efd9988
FG
83 explicit StartCommand(const std::string &desc, ImageReplayer<I> *replayer)
84 : ImageReplayerAdminSocketCommand<I>(desc, replayer) {
85 }
7c673cae 86
9f95a23c 87 int call(Formatter *f) override {
3efd9988 88 this->replayer->start(nullptr, true);
9f95a23c 89 return 0;
7c673cae 90 }
7c673cae
FG
91};
92
93template <typename I>
3efd9988 94class StopCommand : public ImageReplayerAdminSocketCommand<I> {
7c673cae 95public:
3efd9988
FG
96 explicit StopCommand(const std::string &desc, ImageReplayer<I> *replayer)
97 : ImageReplayerAdminSocketCommand<I>(desc, replayer) {
98 }
7c673cae 99
9f95a23c 100 int call(Formatter *f) override {
3efd9988 101 this->replayer->stop(nullptr, true);
9f95a23c 102 return 0;
7c673cae 103 }
7c673cae
FG
104};
105
106template <typename I>
3efd9988 107class RestartCommand : public ImageReplayerAdminSocketCommand<I> {
7c673cae 108public:
3efd9988
FG
109 explicit RestartCommand(const std::string &desc, ImageReplayer<I> *replayer)
110 : ImageReplayerAdminSocketCommand<I>(desc, replayer) {
111 }
7c673cae 112
9f95a23c 113 int call(Formatter *f) override {
3efd9988 114 this->replayer->restart();
9f95a23c 115 return 0;
7c673cae 116 }
7c673cae
FG
117};
118
119template <typename I>
3efd9988 120class FlushCommand : public ImageReplayerAdminSocketCommand<I> {
7c673cae 121public:
3efd9988
FG
122 explicit FlushCommand(const std::string &desc, ImageReplayer<I> *replayer)
123 : ImageReplayerAdminSocketCommand<I>(desc, replayer) {
124 }
7c673cae 125
9f95a23c 126 int call(Formatter *f) override {
28e407b8 127 this->replayer->flush();
9f95a23c 128 return 0;
7c673cae 129 }
7c673cae
FG
130};
131
132template <typename I>
133class ImageReplayerAdminSocketHook : public AdminSocketHook {
134public:
135 ImageReplayerAdminSocketHook(CephContext *cct, const std::string &name,
31f18b77 136 ImageReplayer<I> *replayer)
3efd9988
FG
137 : admin_socket(cct->get_admin_socket()),
138 commands{{"rbd mirror flush " + name,
139 new FlushCommand<I>("flush rbd mirror " + name, replayer)},
140 {"rbd mirror restart " + name,
141 new RestartCommand<I>("restart rbd mirror " + name, replayer)},
142 {"rbd mirror start " + name,
143 new StartCommand<I>("start rbd mirror " + name, replayer)},
144 {"rbd mirror status " + name,
145 new StatusCommand<I>("get status for rbd mirror " + name, replayer)},
146 {"rbd mirror stop " + name,
147 new StopCommand<I>("stop rbd mirror " + name, replayer)}} {
d2e6a577
FG
148 }
149
150 int register_commands() {
3efd9988 151 for (auto &it : commands) {
9f95a23c 152 int r = admin_socket->register_command(it.first, this,
3efd9988
FG
153 it.second->desc);
154 if (r < 0) {
155 return r;
156 }
157 it.second->registered = true;
7c673cae 158 }
d2e6a577 159 return 0;
7c673cae
FG
160 }
161
162 ~ImageReplayerAdminSocketHook() override {
9f95a23c 163 admin_socket->unregister_commands(this);
3efd9988 164 for (auto &it : commands) {
3efd9988 165 delete it.second;
7c673cae 166 }
31f18b77 167 commands.clear();
7c673cae
FG
168 }
169
9f95a23c
TL
170 int call(std::string_view command, const cmdmap_t& cmdmap,
171 Formatter *f,
172 std::ostream& errss,
173 bufferlist& out) override {
3efd9988 174 auto i = commands.find(command);
11fdf7f2 175 ceph_assert(i != commands.end());
9f95a23c 176 return i->second->call(f);
7c673cae
FG
177 }
178
179private:
11fdf7f2
TL
180 typedef std::map<std::string, ImageReplayerAdminSocketCommand<I>*,
181 std::less<>> Commands;
7c673cae
FG
182
183 AdminSocket *admin_socket;
184 Commands commands;
185};
186
7c673cae
FG
187} // anonymous namespace
188
189template <typename I>
190void ImageReplayer<I>::BootstrapProgressContext::update_progress(
191 const std::string &description, bool flush)
192{
193 const std::string desc = "bootstrapping, " + description;
194 replayer->set_state_description(0, desc);
195 if (flush) {
196 replayer->update_mirror_image_status(false, boost::none);
197 }
198}
199
200template <typename I>
9f95a23c
TL
201struct ImageReplayer<I>::ReplayerListener
202 : public image_replayer::ReplayerListener {
203 ImageReplayer<I>* image_replayer;
204
205 ReplayerListener(ImageReplayer<I>* image_replayer)
206 : image_replayer(image_replayer) {
207 }
208
209 void handle_notification() override {
210 image_replayer->handle_replayer_notification();
211 }
212};
7c673cae
FG
213
214template <typename I>
9f95a23c
TL
215ImageReplayer<I>::ImageReplayer(
216 librados::IoCtx &local_io_ctx, const std::string &local_mirror_uuid,
217 const std::string &global_image_id, Threads<I> *threads,
218 InstanceWatcher<I> *instance_watcher,
219 MirrorStatusUpdater<I>* local_status_updater,
220 journal::CacheManagerHandler *cache_manager_handler,
221 PoolMetaCache* pool_meta_cache) :
222 m_local_io_ctx(local_io_ctx), m_local_mirror_uuid(local_mirror_uuid),
223 m_global_image_id(global_image_id), m_threads(threads),
31f18b77 224 m_instance_watcher(instance_watcher),
9f95a23c
TL
225 m_local_status_updater(local_status_updater),
226 m_cache_manager_handler(cache_manager_handler),
227 m_pool_meta_cache(pool_meta_cache),
228 m_local_image_name(global_image_id),
229 m_lock(ceph::make_mutex("rbd::mirror::ImageReplayer " +
230 stringify(local_io_ctx.get_id()) + " " + global_image_id)),
7c673cae 231 m_progress_cxt(this),
9f95a23c 232 m_replayer_listener(new ReplayerListener(this))
7c673cae
FG
233{
234 // Register asok commands using a temporary "remote_pool_name/global_image_id"
235 // name. When the image name becomes known on start the asok commands will be
236 // re-registered using "remote_pool_name/remote_image_name" name.
237
9f95a23c
TL
238 m_image_spec = image_replayer::util::compute_image_spec(
239 local_io_ctx, global_image_id);
d2e6a577 240 register_admin_socket_hook();
7c673cae
FG
241}
242
243template <typename I>
244ImageReplayer<I>::~ImageReplayer()
245{
d2e6a577 246 unregister_admin_socket_hook();
9f95a23c 247 ceph_assert(m_state_builder == nullptr);
11fdf7f2
TL
248 ceph_assert(m_on_start_finish == nullptr);
249 ceph_assert(m_on_stop_finish == nullptr);
250 ceph_assert(m_bootstrap_request == nullptr);
1911f103 251 ceph_assert(m_update_status_task == nullptr);
9f95a23c 252 delete m_replayer_listener;
7c673cae
FG
253}
254
c07f9fc5
FG
255template <typename I>
256image_replayer::HealthState ImageReplayer<I>::get_health_state() const {
9f95a23c 257 std::lock_guard locker{m_lock};
c07f9fc5
FG
258
259 if (!m_mirror_image_status_state) {
260 return image_replayer::HEALTH_STATE_OK;
261 } else if (*m_mirror_image_status_state ==
262 cls::rbd::MIRROR_IMAGE_STATUS_STATE_SYNCING ||
263 *m_mirror_image_status_state ==
264 cls::rbd::MIRROR_IMAGE_STATUS_STATE_UNKNOWN) {
265 return image_replayer::HEALTH_STATE_WARNING;
266 }
267 return image_replayer::HEALTH_STATE_ERROR;
268}
269
7c673cae 270template <typename I>
9f95a23c
TL
271void ImageReplayer<I>::add_peer(const Peer<I>& peer) {
272 dout(10) << "peer=" << peer << dendl;
273
274 std::lock_guard locker{m_lock};
275 auto it = m_peers.find(peer);
d2e6a577 276 if (it == m_peers.end()) {
9f95a23c 277 m_peers.insert(peer);
7c673cae
FG
278 }
279}
280
7c673cae
FG
281template <typename I>
282void ImageReplayer<I>::set_state_description(int r, const std::string &desc) {
9f95a23c 283 dout(10) << "r=" << r << ", desc=" << desc << dendl;
7c673cae 284
9f95a23c 285 std::lock_guard l{m_lock};
7c673cae
FG
286 m_last_r = r;
287 m_state_desc = desc;
288}
289
290template <typename I>
291void ImageReplayer<I>::start(Context *on_finish, bool manual)
292{
11fdf7f2 293 dout(10) << "on_finish=" << on_finish << dendl;
7c673cae
FG
294
295 int r = 0;
296 {
9f95a23c 297 std::lock_guard locker{m_lock};
7c673cae
FG
298 if (!is_stopped_()) {
299 derr << "already running" << dendl;
300 r = -EINVAL;
301 } else if (m_manual_stop && !manual) {
302 dout(5) << "stopped manually, ignoring start without manual flag"
303 << dendl;
304 r = -EPERM;
305 } else {
306 m_state = STATE_STARTING;
307 m_last_r = 0;
308 m_state_desc.clear();
309 m_manual_stop = false;
d2e6a577 310 m_delete_requested = false;
7c673cae
FG
311
312 if (on_finish != nullptr) {
11fdf7f2 313 ceph_assert(m_on_start_finish == nullptr);
7c673cae
FG
314 m_on_start_finish = on_finish;
315 }
11fdf7f2 316 ceph_assert(m_on_stop_finish == nullptr);
7c673cae
FG
317 }
318 }
319
320 if (r < 0) {
321 if (on_finish) {
322 on_finish->complete(r);
323 }
324 return;
325 }
326
9f95a23c 327 bootstrap();
7c673cae
FG
328}
329
330template <typename I>
9f95a23c 331void ImageReplayer<I>::bootstrap() {
11fdf7f2 332 dout(10) << dendl;
7c673cae 333
9f95a23c 334 std::unique_lock locker{m_lock};
b32b8144 335 if (m_peers.empty()) {
9f95a23c
TL
336 locker.unlock();
337
338 dout(5) << "no peer clusters" << dendl;
339 on_start_fail(-ENOENT, "no peer clusters");
b32b8144
FG
340 return;
341 }
7c673cae 342
d2e6a577 343 // TODO need to support multiple remote images
11fdf7f2 344 ceph_assert(!m_peers.empty());
9f95a23c 345 m_remote_image_peer = *m_peers.begin();
7c673cae 346
9f95a23c 347 if (on_start_interrupted(m_lock)) {
b32b8144
FG
348 return;
349 }
7c673cae 350
9f95a23c
TL
351 ceph_assert(m_state_builder == nullptr);
352 auto ctx = create_context_callback<
11fdf7f2 353 ImageReplayer, &ImageReplayer<I>::handle_bootstrap>(this);
9f95a23c
TL
354 auto request = image_replayer::BootstrapRequest<I>::create(
355 m_threads, m_local_io_ctx, m_remote_image_peer.io_ctx, m_instance_watcher,
356 m_global_image_id, m_local_mirror_uuid,
357 m_remote_image_peer.remote_pool_meta, m_cache_manager_handler,
358 m_pool_meta_cache, &m_progress_cxt, &m_state_builder, &m_resync_requested,
359 ctx);
7c673cae 360
9f95a23c
TL
361 request->get();
362 m_bootstrap_request = request;
363 locker.unlock();
7c673cae 364
9f95a23c 365 update_mirror_image_status(false, boost::none);
7c673cae
FG
366 request->send();
367}
368
369template <typename I>
370void ImageReplayer<I>::handle_bootstrap(int r) {
11fdf7f2 371 dout(10) << "r=" << r << dendl;
7c673cae 372 {
9f95a23c 373 std::lock_guard locker{m_lock};
7c673cae
FG
374 m_bootstrap_request->put();
375 m_bootstrap_request = nullptr;
7c673cae
FG
376 }
377
11fdf7f2
TL
378 if (on_start_interrupted()) {
379 return;
9f95a23c
TL
380 } else if (r == -ENOMSG) {
381 dout(5) << "local image is primary" << dendl;
382 on_start_fail(0, "local image is primary");
383 return;
11fdf7f2 384 } else if (r == -EREMOTEIO) {
c07f9fc5
FG
385 dout(5) << "remote image is non-primary" << dendl;
386 on_start_fail(-EREMOTEIO, "remote image is non-primary");
7c673cae
FG
387 return;
388 } else if (r == -EEXIST) {
7c673cae
FG
389 on_start_fail(r, "split-brain detected");
390 return;
9f95a23c
TL
391 } else if (r == -ENOLINK) {
392 m_delete_requested = true;
393 on_start_fail(0, "remote image no longer exists");
394 return;
7c673cae
FG
395 } else if (r < 0) {
396 on_start_fail(r, "error bootstrapping replay");
397 return;
d2e6a577
FG
398 } else if (m_resync_requested) {
399 on_start_fail(0, "resync requested");
400 return;
7c673cae
FG
401 }
402
9f95a23c 403 start_replay();
7c673cae
FG
404}
405
406template <typename I>
9f95a23c 407void ImageReplayer<I>::start_replay() {
11fdf7f2 408 dout(10) << dendl;
7c673cae 409
9f95a23c
TL
410 std::unique_lock locker{m_lock};
411 ceph_assert(m_replayer == nullptr);
412 m_replayer = m_state_builder->create_replayer(m_threads, m_instance_watcher,
413 m_local_mirror_uuid,
414 m_pool_meta_cache,
415 m_replayer_listener);
416
417 auto ctx = create_context_callback<
418 ImageReplayer<I>, &ImageReplayer<I>::handle_start_replay>(this);
419 m_replayer->init(ctx);
7c673cae
FG
420}
421
422template <typename I>
9f95a23c 423void ImageReplayer<I>::handle_start_replay(int r) {
11fdf7f2 424 dout(10) << "r=" << r << dendl;
7c673cae 425
11fdf7f2
TL
426 if (on_start_interrupted()) {
427 return;
428 } else if (r < 0) {
9f95a23c
TL
429 std::string error_description = m_replayer->get_error_description();
430 if (r == -ENOTCONN && m_replayer->is_resync_requested()) {
431 std::unique_lock locker{m_lock};
d2e6a577 432 m_resync_requested = true;
7c673cae 433 }
7c673cae 434
9f95a23c
TL
435 // shut down not required if init failed
436 m_replayer->destroy();
437 m_replayer = nullptr;
7c673cae 438
9f95a23c
TL
439 derr << "error starting replay: " << cpp_strerror(r) << dendl;
440 on_start_fail(r, error_description);
7c673cae
FG
441 return;
442 }
443
9f95a23c 444 Context *on_finish = nullptr;
7c673cae 445 {
9f95a23c 446 std::unique_lock locker{m_lock};
11fdf7f2 447 ceph_assert(m_state == STATE_STARTING);
7c673cae
FG
448 m_state = STATE_REPLAYING;
449 std::swap(m_on_start_finish, on_finish);
1911f103
TL
450
451 std::unique_lock timer_locker{m_threads->timer_lock};
452 schedule_update_mirror_image_replay_status();
7c673cae
FG
453 }
454
7c673cae 455 update_mirror_image_status(true, boost::none);
7c673cae 456 if (on_replay_interrupted()) {
9f95a23c
TL
457 if (on_finish != nullptr) {
458 on_finish->complete(r);
459 }
7c673cae
FG
460 return;
461 }
462
11fdf7f2 463 dout(10) << "start succeeded" << dendl;
d2e6a577 464 if (on_finish != nullptr) {
11fdf7f2 465 dout(10) << "on finish complete, r=" << r << dendl;
d2e6a577
FG
466 on_finish->complete(r);
467 }
7c673cae
FG
468}
469
470template <typename I>
471void ImageReplayer<I>::on_start_fail(int r, const std::string &desc)
472{
9f95a23c
TL
473 dout(10) << "r=" << r << ", desc=" << desc << dendl;
474 Context *ctx = new LambdaContext([this, r, desc](int _r) {
7c673cae 475 {
9f95a23c 476 std::lock_guard locker{m_lock};
11fdf7f2 477 ceph_assert(m_state == STATE_STARTING);
7c673cae 478 m_state = STATE_STOPPING;
d2e6a577 479 if (r < 0 && r != -ECANCELED && r != -EREMOTEIO && r != -ENOENT) {
7c673cae
FG
480 derr << "start failed: " << cpp_strerror(r) << dendl;
481 } else {
11fdf7f2 482 dout(10) << "start canceled" << dendl;
7c673cae
FG
483 }
484 }
485
486 set_state_description(r, desc);
9f95a23c 487 update_mirror_image_status(false, boost::none);
7c673cae
FG
488 shut_down(r);
489 });
490 m_threads->work_queue->queue(ctx, 0);
491}
492
493template <typename I>
11fdf7f2 494bool ImageReplayer<I>::on_start_interrupted() {
9f95a23c 495 std::lock_guard locker{m_lock};
11fdf7f2
TL
496 return on_start_interrupted(m_lock);
497}
498
499template <typename I>
9f95a23c
TL
500bool ImageReplayer<I>::on_start_interrupted(ceph::mutex& lock) {
501 ceph_assert(ceph_mutex_is_locked(m_lock));
11fdf7f2
TL
502 ceph_assert(m_state == STATE_STARTING);
503 if (!m_stop_requested) {
7c673cae
FG
504 return false;
505 }
506
11fdf7f2 507 on_start_fail(-ECANCELED, "");
7c673cae
FG
508 return true;
509}
510
511template <typename I>
512void ImageReplayer<I>::stop(Context *on_finish, bool manual, int r,
513 const std::string& desc)
514{
11fdf7f2 515 dout(10) << "on_finish=" << on_finish << ", manual=" << manual
7c673cae
FG
516 << ", desc=" << desc << dendl;
517
518 image_replayer::BootstrapRequest<I> *bootstrap_request = nullptr;
519 bool shut_down_replay = false;
520 bool running = true;
7c673cae 521 {
9f95a23c 522 std::lock_guard locker{m_lock};
7c673cae
FG
523
524 if (!is_running_()) {
525 running = false;
526 } else {
527 if (!is_stopped_()) {
528 if (m_state == STATE_STARTING) {
11fdf7f2
TL
529 dout(10) << "canceling start" << dendl;
530 if (m_bootstrap_request != nullptr) {
7c673cae
FG
531 bootstrap_request = m_bootstrap_request;
532 bootstrap_request->get();
533 }
534 } else {
11fdf7f2 535 dout(10) << "interrupting replay" << dendl;
7c673cae
FG
536 shut_down_replay = true;
537 }
538
11fdf7f2 539 ceph_assert(m_on_stop_finish == nullptr);
7c673cae
FG
540 std::swap(m_on_stop_finish, on_finish);
541 m_stop_requested = true;
542 m_manual_stop = manual;
7c673cae
FG
543 }
544 }
545 }
546
547 // avoid holding lock since bootstrap request will update status
548 if (bootstrap_request != nullptr) {
11fdf7f2 549 dout(10) << "canceling bootstrap" << dendl;
7c673cae
FG
550 bootstrap_request->cancel();
551 bootstrap_request->put();
552 }
553
7c673cae
FG
554 if (!running) {
555 dout(20) << "not running" << dendl;
556 if (on_finish) {
557 on_finish->complete(-EINVAL);
558 }
559 return;
560 }
561
562 if (shut_down_replay) {
563 on_stop_journal_replay(r, desc);
564 } else if (on_finish != nullptr) {
565 on_finish->complete(0);
566 }
567}
568
569template <typename I>
570void ImageReplayer<I>::on_stop_journal_replay(int r, const std::string &desc)
571{
11fdf7f2 572 dout(10) << dendl;
7c673cae
FG
573
574 {
9f95a23c 575 std::lock_guard locker{m_lock};
7c673cae
FG
576 if (m_state != STATE_REPLAYING) {
577 // might be invoked multiple times while stopping
578 return;
579 }
9f95a23c 580
7c673cae
FG
581 m_stop_requested = true;
582 m_state = STATE_STOPPING;
583 }
584
1911f103 585 cancel_update_mirror_image_replay_status();
7c673cae 586 set_state_description(r, desc);
a8e16298 587 update_mirror_image_status(true, boost::none);
7c673cae
FG
588 shut_down(0);
589}
590
7c673cae
FG
591template <typename I>
592void ImageReplayer<I>::restart(Context *on_finish)
593{
9f95a23c 594 auto ctx = new LambdaContext(
7c673cae
FG
595 [this, on_finish](int r) {
596 if (r < 0) {
597 // Try start anyway.
598 }
599 start(on_finish, true);
600 });
601 stop(ctx);
602}
603
604template <typename I>
81eedcae 605void ImageReplayer<I>::flush()
7c673cae 606{
81eedcae 607 C_SaferCond ctx;
7c673cae 608
9f95a23c
TL
609 {
610 std::unique_lock locker{m_lock};
611 if (m_state != STATE_REPLAYING) {
612 return;
613 }
7c673cae 614
9f95a23c
TL
615 dout(10) << dendl;
616 ceph_assert(m_replayer != nullptr);
617 m_replayer->flush(&ctx);
81eedcae
TL
618 }
619
9f95a23c
TL
620 int r = ctx.wait();
621 if (r >= 0) {
622 update_mirror_image_status(false, boost::none);
7c673cae 623 }
7c673cae
FG
624}
625
626template <typename I>
627bool ImageReplayer<I>::on_replay_interrupted()
628{
629 bool shut_down;
630 {
9f95a23c 631 std::lock_guard locker{m_lock};
7c673cae
FG
632 shut_down = m_stop_requested;
633 }
634
635 if (shut_down) {
636 on_stop_journal_replay();
637 }
638 return shut_down;
639}
640
641template <typename I>
9f95a23c 642void ImageReplayer<I>::print_status(Formatter *f)
7c673cae 643{
11fdf7f2 644 dout(10) << dendl;
7c673cae 645
9f95a23c 646 std::lock_guard l{m_lock};
7c673cae 647
9f95a23c
TL
648 f->open_object_section("image_replayer");
649 f->dump_string("name", m_image_spec);
650 f->dump_string("state", to_string(m_state));
651 f->close_section();
7c673cae
FG
652}
653
1911f103
TL
654template <typename I>
655void ImageReplayer<I>::schedule_update_mirror_image_replay_status() {
656 ceph_assert(ceph_mutex_is_locked_by_me(m_lock));
657 ceph_assert(ceph_mutex_is_locked_by_me(m_threads->timer_lock));
658 if (m_state != STATE_REPLAYING) {
659 return;
660 }
661
662 dout(10) << dendl;
663
664 // periodically update the replaying status even if nothing changes
665 // so that we can adjust our performance stats
666 ceph_assert(m_update_status_task == nullptr);
667 m_update_status_task = create_context_callback<
668 ImageReplayer<I>,
669 &ImageReplayer<I>::handle_update_mirror_image_replay_status>(this);
670 m_threads->timer->add_event_after(10, m_update_status_task);
671}
672
673template <typename I>
674void ImageReplayer<I>::handle_update_mirror_image_replay_status(int r) {
675 dout(10) << dendl;
676
677 auto ctx = new LambdaContext([this](int) {
678 update_mirror_image_status(false, boost::none);
679
680 {
681 std::unique_lock locker{m_lock};
682 std::unique_lock timer_locker{m_threads->timer_lock};
683 ceph_assert(m_update_status_task != nullptr);
684 m_update_status_task = nullptr;
685
686 schedule_update_mirror_image_replay_status();
687 }
688
689 m_in_flight_op_tracker.finish_op();
690 });
691
692 m_in_flight_op_tracker.start_op();
693 m_threads->work_queue->queue(ctx, 0);
694}
695
696template <typename I>
697void ImageReplayer<I>::cancel_update_mirror_image_replay_status() {
698 std::unique_lock timer_locker{m_threads->timer_lock};
699 if (m_update_status_task != nullptr) {
700 dout(10) << dendl;
701
702 if (m_threads->timer->cancel_event(m_update_status_task)) {
703 m_update_status_task = nullptr;
704 }
705 }
706}
707
7c673cae 708template <typename I>
9f95a23c
TL
709void ImageReplayer<I>::update_mirror_image_status(
710 bool force, const OptionalState &opt_state) {
711 dout(15) << "force=" << force << ", "
712 << "state=" << opt_state << dendl;
7c673cae
FG
713
714 {
9f95a23c
TL
715 std::lock_guard locker{m_lock};
716 if (!force && !is_stopped_() && !is_running_()) {
717 dout(15) << "shut down in-progress: ignoring update" << dendl;
28e407b8 718 return;
28e407b8
AA
719 }
720 }
721
9f95a23c
TL
722 m_in_flight_op_tracker.start_op();
723 auto ctx = new LambdaContext(
724 [this, force, opt_state](int r) {
725 set_mirror_image_status_update(force, opt_state);
11fdf7f2
TL
726 });
727 m_threads->work_queue->queue(ctx, 0);
7c673cae
FG
728}
729
730template <typename I>
9f95a23c
TL
731void ImageReplayer<I>::set_mirror_image_status_update(
732 bool force, const OptionalState &opt_state) {
733 dout(15) << "force=" << force << ", "
734 << "state=" << opt_state << dendl;
7c673cae 735
28e407b8
AA
736 reregister_admin_socket_hook();
737
7c673cae
FG
738 State state;
739 std::string state_desc;
740 int last_r;
7c673cae 741 bool stopping_replay;
c07f9fc5 742
9f95a23c
TL
743 auto mirror_image_status_state = boost::make_optional(
744 false, cls::rbd::MIRROR_IMAGE_STATUS_STATE_UNKNOWN);
c07f9fc5 745 image_replayer::BootstrapRequest<I>* bootstrap_request = nullptr;
7c673cae 746 {
9f95a23c 747 std::lock_guard locker{m_lock};
7c673cae
FG
748 state = m_state;
749 state_desc = m_state_desc;
c07f9fc5 750 mirror_image_status_state = m_mirror_image_status_state;
7c673cae 751 last_r = m_last_r;
9f95a23c 752 stopping_replay = (m_replayer != nullptr);
c07f9fc5
FG
753
754 if (m_bootstrap_request != nullptr) {
755 bootstrap_request = m_bootstrap_request;
756 bootstrap_request->get();
757 }
758 }
759
760 bool syncing = false;
761 if (bootstrap_request != nullptr) {
762 syncing = bootstrap_request->is_syncing();
763 bootstrap_request->put();
764 bootstrap_request = nullptr;
7c673cae
FG
765 }
766
767 if (opt_state) {
768 state = *opt_state;
769 }
770
9f95a23c 771 cls::rbd::MirrorImageSiteStatus status;
7c673cae
FG
772 status.up = true;
773 switch (state) {
774 case STATE_STARTING:
c07f9fc5 775 if (syncing) {
7c673cae
FG
776 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_SYNCING;
777 status.description = state_desc.empty() ? "syncing" : state_desc;
c07f9fc5 778 mirror_image_status_state = status.state;
7c673cae
FG
779 } else {
780 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_STARTING_REPLAY;
781 status.description = "starting replay";
782 }
783 break;
784 case STATE_REPLAYING:
7c673cae
FG
785 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_REPLAYING;
786 {
9f95a23c
TL
787 std::string desc;
788 auto on_req_finish = new LambdaContext(
789 [this, force](int r) {
11fdf7f2 790 dout(15) << "replay status ready: r=" << r << dendl;
7c673cae 791 if (r >= 0) {
9f95a23c 792 set_mirror_image_status_update(force, boost::none);
7c673cae 793 } else if (r == -EAGAIN) {
9f95a23c 794 m_in_flight_op_tracker.finish_op();
7c673cae
FG
795 }
796 });
797
9f95a23c
TL
798 ceph_assert(m_replayer != nullptr);
799 if (!m_replayer->get_replay_status(&desc, on_req_finish)) {
11fdf7f2 800 dout(15) << "waiting for replay status" << dendl;
7c673cae
FG
801 return;
802 }
9f95a23c 803
7c673cae 804 status.description = "replaying, " + desc;
11fdf7f2
TL
805 mirror_image_status_state = boost::make_optional(
806 false, cls::rbd::MIRROR_IMAGE_STATUS_STATE_UNKNOWN);
7c673cae
FG
807 }
808 break;
809 case STATE_STOPPING:
810 if (stopping_replay) {
811 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_STOPPING_REPLAY;
a8e16298 812 status.description = state_desc.empty() ? "stopping replay" : state_desc;
7c673cae
FG
813 break;
814 }
815 // FALLTHROUGH
816 case STATE_STOPPED:
c07f9fc5
FG
817 if (last_r == -EREMOTEIO) {
818 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_UNKNOWN;
819 status.description = state_desc;
820 mirror_image_status_state = status.state;
9f95a23c 821 } else if (last_r < 0 && last_r != -ECANCELED) {
7c673cae
FG
822 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_ERROR;
823 status.description = state_desc;
c07f9fc5 824 mirror_image_status_state = status.state;
7c673cae
FG
825 } else {
826 status.state = cls::rbd::MIRROR_IMAGE_STATUS_STATE_STOPPED;
827 status.description = state_desc.empty() ? "stopped" : state_desc;
c07f9fc5 828 mirror_image_status_state = boost::none;
7c673cae
FG
829 }
830 break;
831 default:
11fdf7f2 832 ceph_assert(!"invalid state");
7c673cae
FG
833 }
834
c07f9fc5 835 {
9f95a23c 836 std::lock_guard locker{m_lock};
c07f9fc5
FG
837 m_mirror_image_status_state = mirror_image_status_state;
838 }
839
840 // prevent the status from ping-ponging when failed replays are restarted
841 if (mirror_image_status_state &&
842 *mirror_image_status_state == cls::rbd::MIRROR_IMAGE_STATUS_STATE_ERROR) {
843 status.state = *mirror_image_status_state;
844 }
845
11fdf7f2 846 dout(15) << "status=" << status << dendl;
9f95a23c
TL
847 m_local_status_updater->set_mirror_image_status(m_global_image_id, status,
848 force);
849 if (m_remote_image_peer.mirror_status_updater != nullptr) {
850 m_remote_image_peer.mirror_status_updater->set_mirror_image_status(
851 m_global_image_id, status, force);
7c673cae
FG
852 }
853
9f95a23c 854 m_in_flight_op_tracker.finish_op();
7c673cae
FG
855}
856
857template <typename I>
858void ImageReplayer<I>::shut_down(int r) {
11fdf7f2 859 dout(10) << "r=" << r << dendl;
3efd9988 860
3efd9988 861 {
9f95a23c 862 std::lock_guard locker{m_lock};
11fdf7f2 863 ceph_assert(m_state == STATE_STOPPING);
7c673cae
FG
864 }
865
9f95a23c
TL
866 if (!m_in_flight_op_tracker.empty()) {
867 dout(15) << "waiting for in-flight operations to complete" << dendl;
868 m_in_flight_op_tracker.wait_for_ops(new LambdaContext([this, r](int) {
869 shut_down(r);
870 }));
871 return;
872 }
31f18b77 873
7c673cae 874 // chain the shut down sequence (reverse order)
9f95a23c 875 Context *ctx = new LambdaContext(
7c673cae 876 [this, r](int _r) {
9f95a23c 877 update_mirror_image_status(true, STATE_STOPPED);
7c673cae
FG
878 handle_shut_down(r);
879 });
31f18b77 880
9f95a23c
TL
881 // destruct the state builder
882 if (m_state_builder != nullptr) {
883 ctx = new LambdaContext([this, ctx](int r) {
884 m_state_builder->close(ctx);
31f18b77
FG
885 });
886 }
887
9f95a23c
TL
888 // close the replayer
889 if (m_replayer != nullptr) {
890 ctx = new LambdaContext([this, ctx](int r) {
891 m_replayer->destroy();
892 m_replayer = nullptr;
893 ctx->complete(0);
894 });
895 ctx = new LambdaContext([this, ctx](int r) {
896 m_replayer->shut_down(ctx);
31f18b77 897 });
7c673cae 898 }
31f18b77 899
7c673cae
FG
900 m_threads->work_queue->queue(ctx, 0);
901}
902
903template <typename I>
904void ImageReplayer<I>::handle_shut_down(int r) {
11fdf7f2
TL
905 bool resync_requested = false;
906 bool delete_requested = false;
3efd9988 907 bool unregister_asok_hook = false;
7c673cae 908 {
9f95a23c 909 std::lock_guard locker{m_lock};
7c673cae 910
9f95a23c
TL
911 if (m_delete_requested && m_state_builder != nullptr &&
912 !m_state_builder->local_image_id.empty()) {
913 ceph_assert(m_state_builder->remote_image_id.empty());
d2e6a577 914 dout(0) << "remote image no longer exists: scheduling deletion" << dendl;
11fdf7f2
TL
915 unregister_asok_hook = true;
916 std::swap(delete_requested, m_delete_requested);
d2e6a577 917 }
d2e6a577 918
11fdf7f2 919 std::swap(resync_requested, m_resync_requested);
9f95a23c
TL
920 if (!delete_requested && !resync_requested && m_last_r == -ENOENT &&
921 ((m_state_builder == nullptr) ||
922 (m_state_builder->local_image_id.empty() &&
923 m_state_builder->remote_image_id.empty()))) {
d2e6a577 924 dout(0) << "mirror image no longer exists" << dendl;
3efd9988 925 unregister_asok_hook = true;
d2e6a577 926 m_finished = true;
7c673cae
FG
927 }
928 }
929
3efd9988
FG
930 if (unregister_asok_hook) {
931 unregister_admin_socket_hook();
932 }
933
11fdf7f2
TL
934 if (delete_requested || resync_requested) {
935 dout(5) << "moving image to trash" << dendl;
9f95a23c 936 auto ctx = new LambdaContext([this, r](int) {
11fdf7f2
TL
937 handle_shut_down(r);
938 });
9f95a23c 939 ImageDeleter<I>::trash_move(m_local_io_ctx, m_global_image_id,
11fdf7f2
TL
940 resync_requested, m_threads->work_queue, ctx);
941 return;
942 }
7c673cae 943
9f95a23c
TL
944 if (!m_in_flight_op_tracker.empty()) {
945 dout(15) << "waiting for in-flight operations to complete" << dendl;
946 m_in_flight_op_tracker.wait_for_ops(new LambdaContext([this, r](int) {
947 handle_shut_down(r);
948 }));
949 return;
950 }
7c673cae 951
9f95a23c
TL
952 if (m_local_status_updater->exists(m_global_image_id)) {
953 dout(15) << "removing local mirror image status" << dendl;
954 auto ctx = new LambdaContext([this, r](int) {
955 handle_shut_down(r);
956 });
957 m_local_status_updater->remove_mirror_image_status(m_global_image_id, ctx);
958 return;
959 }
960
961 if (m_remote_image_peer.mirror_status_updater != nullptr &&
962 m_remote_image_peer.mirror_status_updater->exists(m_global_image_id)) {
963 dout(15) << "removing remote mirror image status" << dendl;
964 auto ctx = new LambdaContext([this, r](int) {
965 handle_shut_down(r);
966 });
967 m_remote_image_peer.mirror_status_updater->remove_mirror_image_status(
968 m_global_image_id, ctx);
969 return;
970 }
971
972 if (m_state_builder != nullptr) {
973 m_state_builder->destroy();
974 m_state_builder = nullptr;
975 }
976
977 dout(10) << "stop complete" << dendl;
7c673cae
FG
978 Context *on_start = nullptr;
979 Context *on_stop = nullptr;
980 {
9f95a23c 981 std::lock_guard locker{m_lock};
7c673cae
FG
982 std::swap(on_start, m_on_start_finish);
983 std::swap(on_stop, m_on_stop_finish);
984 m_stop_requested = false;
11fdf7f2 985 ceph_assert(m_state == STATE_STOPPING);
7c673cae
FG
986 m_state = STATE_STOPPED;
987 }
988
989 if (on_start != nullptr) {
11fdf7f2 990 dout(10) << "on start finish complete, r=" << r << dendl;
7c673cae
FG
991 on_start->complete(r);
992 r = 0;
993 }
994 if (on_stop != nullptr) {
11fdf7f2 995 dout(10) << "on stop finish complete, r=" << r << dendl;
7c673cae
FG
996 on_stop->complete(r);
997 }
998}
999
1000template <typename I>
9f95a23c
TL
1001void ImageReplayer<I>::handle_replayer_notification() {
1002 dout(10) << dendl;
1003
1004 std::unique_lock locker{m_lock};
1005 if (m_state != STATE_REPLAYING) {
1006 // might be attempting to shut down
1007 return;
1008 }
7c673cae 1009
7c673cae 1010 {
9f95a23c
TL
1011 // detect a rename of the local image
1012 ceph_assert(m_state_builder != nullptr &&
1013 m_state_builder->local_image_ctx != nullptr);
1014 std::shared_lock image_locker{m_state_builder->local_image_ctx->image_lock};
1015 if (m_local_image_name != m_state_builder->local_image_ctx->name) {
1016 // will re-register with new name after next status update
1017 dout(10) << "image renamed" << dendl;
1018 m_local_image_name = m_state_builder->local_image_ctx->name;
7c673cae 1019 }
9f95a23c 1020 }
7c673cae 1021
9f95a23c
TL
1022 // replayer cannot be shut down while notification is in-flight
1023 ceph_assert(m_replayer != nullptr);
1024 locker.unlock();
1025
1026 if (m_replayer->is_resync_requested()) {
1027 dout(10) << "resync requested" << dendl;
1028 m_resync_requested = true;
1029 on_stop_journal_replay(0, "resync requested");
1030 return;
7c673cae
FG
1031 }
1032
9f95a23c
TL
1033 if (!m_replayer->is_replaying()) {
1034 auto error_code = m_replayer->get_error_code();
1035 auto error_description = m_replayer->get_error_description();
1036 dout(10) << "replay interrupted: "
1037 << "r=" << error_code << ", "
1038 << "error=" << error_description << dendl;
1039 on_stop_journal_replay(error_code, error_description);
1040 return;
7c673cae 1041 }
9f95a23c
TL
1042
1043 update_mirror_image_status(false, {});
7c673cae
FG
1044}
1045
1046template <typename I>
1047std::string ImageReplayer<I>::to_string(const State state) {
1048 switch (state) {
1049 case ImageReplayer<I>::STATE_STARTING:
1050 return "Starting";
1051 case ImageReplayer<I>::STATE_REPLAYING:
1052 return "Replaying";
7c673cae
FG
1053 case ImageReplayer<I>::STATE_STOPPING:
1054 return "Stopping";
1055 case ImageReplayer<I>::STATE_STOPPED:
1056 return "Stopped";
1057 default:
1058 break;
1059 }
1060 return "Unknown(" + stringify(state) + ")";
1061}
1062
d2e6a577
FG
1063template <typename I>
1064void ImageReplayer<I>::register_admin_socket_hook() {
3efd9988
FG
1065 ImageReplayerAdminSocketHook<I> *asok_hook;
1066 {
9f95a23c 1067 std::lock_guard locker{m_lock};
3efd9988
FG
1068 if (m_asok_hook != nullptr) {
1069 return;
1070 }
7c673cae 1071
9f95a23c
TL
1072 dout(15) << "registered asok hook: " << m_image_spec << dendl;
1073 asok_hook = new ImageReplayerAdminSocketHook<I>(
1074 g_ceph_context, m_image_spec, this);
3efd9988
FG
1075 int r = asok_hook->register_commands();
1076 if (r == 0) {
1077 m_asok_hook = asok_hook;
1078 return;
1079 }
d2e6a577 1080 derr << "error registering admin socket commands" << dendl;
d2e6a577 1081 }
3efd9988 1082 delete asok_hook;
d2e6a577
FG
1083}
1084
1085template <typename I>
1086void ImageReplayer<I>::unregister_admin_socket_hook() {
28e407b8 1087 dout(15) << dendl;
d2e6a577 1088
3efd9988
FG
1089 AdminSocketHook *asok_hook = nullptr;
1090 {
9f95a23c 1091 std::lock_guard locker{m_lock};
3efd9988
FG
1092 std::swap(asok_hook, m_asok_hook);
1093 }
1094 delete asok_hook;
1095}
1096
1097template <typename I>
28e407b8 1098void ImageReplayer<I>::reregister_admin_socket_hook() {
9f95a23c
TL
1099 std::unique_lock locker{m_lock};
1100 if (m_state == STATE_STARTING && m_bootstrap_request != nullptr) {
1101 m_local_image_name = m_bootstrap_request->get_local_image_name();
3efd9988 1102 }
9f95a23c
TL
1103
1104 auto image_spec = image_replayer::util::compute_image_spec(
1105 m_local_io_ctx, m_local_image_name);
1106 if (m_asok_hook != nullptr && m_image_spec == image_spec) {
1107 return;
1108 }
1109
1110 dout(15) << "old_image_spec=" << m_image_spec << ", "
1111 << "new_image_spec=" << image_spec << dendl;
1112 m_image_spec = image_spec;
1113
1114 if (m_state == STATE_STOPPING || m_state == STATE_STOPPED) {
1115 // no need to re-register if stopping
1116 return;
1117 }
1118 locker.unlock();
1119
3efd9988
FG
1120 unregister_admin_socket_hook();
1121 register_admin_socket_hook();
7c673cae
FG
1122}
1123
1124template <typename I>
1125std::ostream &operator<<(std::ostream &os, const ImageReplayer<I> &replayer)
1126{
1127 os << "ImageReplayer: " << &replayer << " [" << replayer.get_local_pool_id()
1128 << "/" << replayer.get_global_image_id() << "]";
1129 return os;
1130}
1131
1132} // namespace mirror
1133} // namespace rbd
1134
1135template class rbd::mirror::ImageReplayer<librbd::ImageCtx>;