]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/PoolWatcher.h
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / tools / rbd_mirror / PoolWatcher.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RBD_MIRROR_POOL_WATCHER_H
5 #define CEPH_RBD_MIRROR_POOL_WATCHER_H
6
7 #include <map>
8 #include <memory>
9 #include <set>
10 #include <string>
11
12 #include "common/AsyncOpTracker.h"
13 #include "common/ceph_context.h"
14 #include "common/ceph_mutex.h"
15 #include "include/rados/librados.hpp"
16 #include "tools/rbd_mirror/Types.h"
17 #include <boost/functional/hash.hpp>
18 #include <boost/optional.hpp>
19 #include "include/ceph_assert.h"
20 #include "tools/rbd_mirror/pool_watcher/Types.h"
21
22 namespace librbd { struct ImageCtx; }
23
24 namespace rbd {
25 namespace mirror {
26
27 template <typename> struct Threads;
28
29 /**
30 * Keeps track of images that have mirroring enabled within all
31 * pools.
32 */
33 template <typename ImageCtxT = librbd::ImageCtx>
34 class PoolWatcher {
35 public:
36 static PoolWatcher* create(Threads<ImageCtxT> *threads,
37 librados::IoCtx &io_ctx,
38 const std::string& mirror_uuid,
39 pool_watcher::Listener &listener) {
40 return new PoolWatcher(threads, io_ctx, mirror_uuid, listener);
41 }
42
43 PoolWatcher(Threads<ImageCtxT> *threads,
44 librados::IoCtx &io_ctx,
45 const std::string& mirror_uuid,
46 pool_watcher::Listener &listener);
47 ~PoolWatcher();
48 PoolWatcher(const PoolWatcher&) = delete;
49 PoolWatcher& operator=(const PoolWatcher&) = delete;
50
51 bool is_blocklisted() const;
52
53 void init(Context *on_finish = nullptr);
54 void shut_down(Context *on_finish);
55
56 inline uint64_t get_image_count() const {
57 std::lock_guard locker{m_lock};
58 return m_image_ids.size();
59 }
60
61 private:
62 /**
63 * @verbatim
64 *
65 * <start>
66 * |
67 * v
68 * INIT
69 * |
70 * v
71 * REGISTER_WATCHER
72 * |
73 * |/--------------------------------\
74 * | |
75 * v |
76 * REFRESH_IMAGES |
77 * | |
78 * |/----------------------------\ |
79 * | | |
80 * v | |
81 * NOTIFY_LISTENER | |
82 * | | |
83 * v | |
84 * IDLE ---\ | |
85 * | | | |
86 * | |\---> IMAGE_UPDATED | |
87 * | | | | |
88 * | | v | |
89 * | | GET_IMAGE_NAME --/ |
90 * | | |
91 * | \----> WATCH_ERROR ---------/
92 * v
93 * SHUT_DOWN
94 * |
95 * v
96 * UNREGISTER_WATCHER
97 * |
98 * v
99 * <finish>
100 *
101 * @endverbatim
102 */
103 class MirroringWatcher;
104
105 Threads<ImageCtxT> *m_threads;
106 librados::IoCtx m_io_ctx;
107 std::string m_mirror_uuid;
108 pool_watcher::Listener &m_listener;
109
110 ImageIds m_refresh_image_ids;
111 bufferlist m_out_bl;
112
113 mutable ceph::mutex m_lock;
114
115 Context *m_on_init_finish = nullptr;
116
117 ImageIds m_image_ids;
118
119 bool m_pending_updates = false;
120 bool m_notify_listener_in_progress = false;
121 ImageIds m_pending_image_ids;
122 ImageIds m_pending_added_image_ids;
123 ImageIds m_pending_removed_image_ids;
124
125 MirroringWatcher *m_mirroring_watcher;
126
127 Context *m_timer_ctx = nullptr;
128
129 AsyncOpTracker m_async_op_tracker;
130 bool m_blocklisted = false;
131 bool m_shutting_down = false;
132 bool m_image_ids_invalid = true;
133 bool m_refresh_in_progress = false;
134 bool m_deferred_refresh = false;
135
136 void register_watcher();
137 void handle_register_watcher(int r);
138 void unregister_watcher();
139
140 void refresh_images();
141 void handle_refresh_images(int r);
142
143 void schedule_refresh_images(double interval);
144 void process_refresh_images();
145
146 void handle_rewatch_complete(int r);
147 void handle_image_updated(const std::string &image_id,
148 const std::string &global_image_id,
149 bool enabled);
150
151 void schedule_listener();
152 void notify_listener();
153
154 };
155
156 } // namespace mirror
157 } // namespace rbd
158
159 extern template class rbd::mirror::PoolWatcher<librbd::ImageCtx>;
160
161 #endif // CEPH_RBD_MIRROR_POOL_WATCHER_H