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