summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/MirrorStatusUpdater.h
blob: 783b818fc56e84d2ee52f33d478b1371aaa2f1cf (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_RBD_MIRROR_MIRROR_STATUS_UPDATER_H
#define CEPH_RBD_MIRROR_MIRROR_STATUS_UPDATER_H

#include "include/rados/librados.hpp"
#include "common/ceph_mutex.h"
#include "cls/rbd/cls_rbd_types.h"
#include <list>
#include <map>
#include <set>
#include <string>

struct Context;
namespace librbd { class ImageCtx; }

namespace rbd {
namespace mirror {

template <typename> struct MirrorStatusWatcher;
template <typename> struct Threads;

template <typename ImageCtxT = librbd::ImageCtx>
class MirrorStatusUpdater {
public:

  static MirrorStatusUpdater* create(librados::IoCtx& io_ctx,
                                     Threads<ImageCtxT> *threads,
                                     const std::string& local_mirror_uuid) {
    return new MirrorStatusUpdater(io_ctx, threads, local_mirror_uuid);
  }

  MirrorStatusUpdater(librados::IoCtx& io_ctx, Threads<ImageCtxT> *threads,
                      const std::string& local_mirror_uuid);
  ~MirrorStatusUpdater();

  void init(Context* on_finish);
  void shut_down(Context* on_finish);

  bool exists(const std::string& global_image_id);
  void set_mirror_image_status(
      const std::string& global_image_id,
      const cls::rbd::MirrorImageSiteStatus& mirror_image_site_status,
      bool immediate_update);
  void remove_mirror_image_status(const std::string& global_image_id,
                                  bool immediate_update, Context* on_finish);
  void remove_refresh_mirror_image_status(const std::string& global_image_id,
                                          Context* on_finish);

private:
  /**
   * @verbatim
   *
   * <uninitialized> <----------------------\
   *    | (init)                 ^ (error)  |
   *    v                        *          |
   * INIT_STATUS_WATCHER * * * * *          |
   *    |                                   |
   *    |                           SHUT_DOWN_STATUS_WATCHER
   *    |                                   ^
   *    |                                   |
   *    |           (shutdown)              |
   * <initialized> -------------------------/
   *
   * @endverbatim
   */
  typedef std::list<Context*> Contexts;
  typedef std::set<std::string> GlobalImageIds;
  typedef std::map<std::string, cls::rbd::MirrorImageSiteStatus>
      GlobalImageStatus;

  librados::IoCtx m_io_ctx;
  Threads<ImageCtxT>* m_threads;
  std::string m_local_mirror_uuid;

  Context* m_timer_task = nullptr;

  ceph::mutex m_lock;

  bool m_initialized = false;

  MirrorStatusWatcher<ImageCtxT>* m_mirror_status_watcher = nullptr;

  GlobalImageIds m_update_global_image_ids;
  GlobalImageStatus m_global_image_status;

  bool m_update_in_progress = false;
  bool m_update_in_flight = false;
  bool m_update_requested = false;
  Contexts m_update_on_finish_ctxs;
  GlobalImageIds m_updating_global_image_ids;

  bool try_remove_mirror_image_status(const std::string& global_image_id,
                                      bool queue_update, bool immediate_update,
                                      Context* on_finish);

  void init_mirror_status_watcher(Context* on_finish);
  void handle_init_mirror_status_watcher(int r, Context* on_finish);

  void shut_down_mirror_status_watcher(Context* on_finish);
  void handle_shut_down_mirror_status_watcher(int r, Context* on_finish);
  void finalize_shutdown(int r, Context* on_finish);

  void schedule_timer_task();
  void handle_timer_task(int r);

  void queue_update_task(std::unique_lock<ceph::mutex>&& locker);
  void update_task(int r);
  void handle_update_task(int r);

};

} // namespace mirror
} // namespace rbd

extern template class rbd::mirror::MirrorStatusUpdater<librbd::ImageCtx>;

#endif // CEPH_RBD_MIRROR_MIRROR_STATUS_UPDATER_H