]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/NamespaceReplayer.h
import ceph quincy 17.2.4
[ceph.git] / ceph / src / tools / rbd_mirror / NamespaceReplayer.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_NAMESPACE_REPLAYER_H
5 #define CEPH_RBD_MIRROR_NAMESPACE_REPLAYER_H
6
7 #include "common/AsyncOpTracker.h"
8 #include "common/ceph_mutex.h"
9 #include "include/rados/librados.hpp"
10
11 #include "tools/rbd_mirror/ImageDeleter.h"
12 #include "tools/rbd_mirror/ImageMap.h"
13 #include "tools/rbd_mirror/InstanceReplayer.h"
14 #include "tools/rbd_mirror/InstanceWatcher.h"
15 #include "tools/rbd_mirror/MirrorStatusUpdater.h"
16 #include "tools/rbd_mirror/PoolWatcher.h"
17 #include "tools/rbd_mirror/Types.h"
18 #include "tools/rbd_mirror/image_map/Types.h"
19 #include "tools/rbd_mirror/pool_watcher/Types.h"
20
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 class AdminSocketHook;
26
27 namespace journal { struct CacheManagerHandler; }
28
29 namespace librbd { class ImageCtx; }
30
31 namespace rbd {
32 namespace mirror {
33
34 struct PoolMetaCache;
35 template <typename> class ServiceDaemon;
36 template <typename> class Throttler;
37 template <typename> struct Threads;
38
39 /**
40 * Controls mirroring for a single remote cluster.
41 */
42 template <typename ImageCtxT = librbd::ImageCtx>
43 class NamespaceReplayer {
44 public:
45 static NamespaceReplayer *create(
46 const std::string &name,
47 librados::IoCtx &local_ioctx,
48 librados::IoCtx &remote_ioctx,
49 const std::string &local_mirror_uuid,
50 const std::string &local_mirror_peer_uuid,
51 const RemotePoolMeta& remote_pool_meta,
52 Threads<ImageCtxT> *threads,
53 Throttler<ImageCtxT> *image_sync_throttler,
54 Throttler<ImageCtxT> *image_deletion_throttler,
55 ServiceDaemon<ImageCtxT> *service_daemon,
56 journal::CacheManagerHandler *cache_manager_handler,
57 PoolMetaCache* pool_meta_cache) {
58 return new NamespaceReplayer(name, local_ioctx, remote_ioctx,
59 local_mirror_uuid, local_mirror_peer_uuid,
60 remote_pool_meta, threads,
61 image_sync_throttler, image_deletion_throttler,
62 service_daemon, cache_manager_handler,
63 pool_meta_cache);
64 }
65
66 NamespaceReplayer(const std::string &name,
67 librados::IoCtx &local_ioctx,
68 librados::IoCtx &remote_ioctx,
69 const std::string &local_mirror_uuid,
70 const std::string& local_mirror_peer_uuid,
71 const RemotePoolMeta& remote_pool_meta,
72 Threads<ImageCtxT> *threads,
73 Throttler<ImageCtxT> *image_sync_throttler,
74 Throttler<ImageCtxT> *image_deletion_throttler,
75 ServiceDaemon<ImageCtxT> *service_daemon,
76 journal::CacheManagerHandler *cache_manager_handler,
77 PoolMetaCache* pool_meta_cache);
78 NamespaceReplayer(const NamespaceReplayer&) = delete;
79 NamespaceReplayer& operator=(const NamespaceReplayer&) = delete;
80
81 bool is_blocklisted() const;
82
83 void init(Context *on_finish);
84 void shut_down(Context *on_finish);
85
86 void handle_acquire_leader(Context *on_finish);
87 void handle_release_leader(Context *on_finish);
88 void handle_update_leader(const std::string &leader_instance_id);
89 void handle_instances_added(const std::vector<std::string> &instance_ids);
90 void handle_instances_removed(const std::vector<std::string> &instance_ids);
91
92 void print_status(Formatter *f);
93 void start();
94 void stop();
95 void restart();
96 void flush();
97
98 private:
99 /**
100 * @verbatim
101 *
102 * <uninitialized> <------------------------------------\
103 * | (init) ^ (error) |
104 * v * |
105 * INIT_LOCAL_STATUS_UPDATER * * * * * * * * > SHUT_DOWN_LOCAL_STATUS_UPDATER
106 * | * (error) ^
107 * v * |
108 * INIT_REMOTE_STATUS_UPDATER * * * * * * * > SHUT_DOWN_REMOTE_STATUS_UPDATER
109 * | * (error) ^
110 * v * |
111 * INIT_INSTANCE_REPLAYER * * * * * * * * * > SHUT_DOWN_INSTANCE_REPLAYER
112 * | * ^
113 * v * |
114 * INIT_INSTANCE_WATCHER * * * * * * * * * * SHUT_DOWN_INSTANCE_WATCHER
115 * | (error) ^
116 * | |
117 * v STOP_INSTANCE_REPLAYER
118 * | ^
119 * | (shut down) |
120 * | /----------------------------------------------/
121 * v |
122 * <follower> <---------------------------\
123 * . |
124 * . |
125 * v (leader acquired) |
126 * INIT_IMAGE_MAP |
127 * | |
128 * v |
129 * INIT_LOCAL_POOL_WATCHER SHUT_DOWN_IMAGE_MAP
130 * | ^
131 * v |
132 * INIT_REMOTE_POOL_WATCHER SHUT_DOWN_POOL_WATCHERS
133 * | ^
134 * v |
135 * INIT_IMAGE_DELETER SHUT_DOWN_IMAGE_DELETER
136 * | ^
137 * v .
138 * <leader> <-----------\ .
139 * . | .
140 * . (image update) | .
141 * . . > NOTIFY_INSTANCE_WATCHER .
142 * . .
143 * . (leader lost / shut down) .
144 * . . . . . . . . . . . . . . . . . . .
145 *
146 * @endverbatim
147 */
148
149 struct PoolWatcherListener : public pool_watcher::Listener {
150 NamespaceReplayer *namespace_replayer;
151 bool local;
152
153 PoolWatcherListener(NamespaceReplayer *namespace_replayer, bool local)
154 : namespace_replayer(namespace_replayer), local(local) {
155 }
156
157 void handle_update(const std::string &mirror_uuid,
158 ImageIds &&added_image_ids,
159 ImageIds &&removed_image_ids) override {
160 namespace_replayer->handle_update((local ? "" : mirror_uuid),
161 std::move(added_image_ids),
162 std::move(removed_image_ids));
163 }
164 };
165
166 struct ImageMapListener : public image_map::Listener {
167 NamespaceReplayer *namespace_replayer;
168
169 ImageMapListener(NamespaceReplayer *namespace_replayer)
170 : namespace_replayer(namespace_replayer) {
171 }
172
173 void acquire_image(const std::string &global_image_id,
174 const std::string &instance_id,
175 Context* on_finish) override {
176 namespace_replayer->handle_acquire_image(global_image_id, instance_id,
177 on_finish);
178 }
179
180 void release_image(const std::string &global_image_id,
181 const std::string &instance_id,
182 Context* on_finish) override {
183 namespace_replayer->handle_release_image(global_image_id, instance_id,
184 on_finish);
185 }
186
187 void remove_image(const std::string &mirror_uuid,
188 const std::string &global_image_id,
189 const std::string &instance_id,
190 Context* on_finish) override {
191 namespace_replayer->handle_remove_image(mirror_uuid, global_image_id,
192 instance_id, on_finish);
193 }
194 };
195
196 void handle_update(const std::string &mirror_uuid,
197 ImageIds &&added_image_ids,
198 ImageIds &&removed_image_ids);
199
200 int init_rados(const std::string &cluster_name,
201 const std::string &client_name,
202 const std::string &mon_host,
203 const std::string &key,
204 const std::string &description, RadosRef *rados_ref,
205 bool strip_cluster_overrides);
206
207 void init_local_status_updater();
208 void handle_init_local_status_updater(int r);
209
210 void init_remote_status_updater();
211 void handle_init_remote_status_updater(int r);
212
213 void init_instance_replayer();
214 void handle_init_instance_replayer(int r);
215
216 void init_instance_watcher();
217 void handle_init_instance_watcher(int r);
218
219 void stop_instance_replayer();
220 void handle_stop_instance_replayer(int r);
221
222 void shut_down_instance_watcher();
223 void handle_shut_down_instance_watcher(int r);
224
225 void shut_down_instance_replayer();
226 void handle_shut_down_instance_replayer(int r);
227
228 void shut_down_remote_status_updater();
229 void handle_shut_down_remote_status_updater(int r);
230
231 void shut_down_local_status_updater();
232 void handle_shut_down_local_status_updater(int r);
233
234 void init_image_map(Context *on_finish);
235 void handle_init_image_map(int r, ImageMap<ImageCtxT> *image_map,
236 Context *on_finish);
237
238 void init_local_pool_watcher(Context *on_finish);
239 void handle_init_local_pool_watcher(int r, Context *on_finish);
240
241 void init_remote_pool_watcher(Context *on_finish);
242 void handle_init_remote_pool_watcher(int r, Context *on_finish);
243
244 void init_image_deleter(Context* on_finish);
245 void handle_init_image_deleter(int r, Context* on_finish);
246
247 void shut_down_image_deleter(Context* on_finish);
248 void handle_shut_down_image_deleter(int r, Context* on_finish);
249
250 void shut_down_pool_watchers(Context *on_finish);
251 void handle_shut_down_pool_watchers(int r, Context *on_finish);
252
253 void shut_down_image_map(Context *on_finish);
254 void handle_shut_down_image_map(int r, Context *on_finish);
255
256 void handle_acquire_image(const std::string &global_image_id,
257 const std::string &instance_id,
258 Context* on_finish);
259 void handle_release_image(const std::string &global_image_id,
260 const std::string &instance_id,
261 Context* on_finish);
262 void handle_remove_image(const std::string &mirror_uuid,
263 const std::string &global_image_id,
264 const std::string &instance_id,
265 Context* on_finish);
266
267 std::string m_namespace_name;
268 librados::IoCtx m_local_io_ctx;
269 librados::IoCtx m_remote_io_ctx;
270 std::string m_local_mirror_uuid;
271 std::string m_local_mirror_peer_uuid;
272 RemotePoolMeta m_remote_pool_meta;
273 Threads<ImageCtxT> *m_threads;
274 Throttler<ImageCtxT> *m_image_sync_throttler;
275 Throttler<ImageCtxT> *m_image_deletion_throttler;
276 ServiceDaemon<ImageCtxT> *m_service_daemon;
277 journal::CacheManagerHandler *m_cache_manager_handler;
278 PoolMetaCache* m_pool_meta_cache;
279
280 mutable ceph::mutex m_lock;
281
282 int m_ret_val = 0;
283 Context *m_on_finish = nullptr;
284
285 std::unique_ptr<MirrorStatusUpdater<ImageCtxT>> m_local_status_updater;
286 std::unique_ptr<MirrorStatusUpdater<ImageCtxT>> m_remote_status_updater;
287
288 PoolWatcherListener m_local_pool_watcher_listener;
289 std::unique_ptr<PoolWatcher<ImageCtxT>> m_local_pool_watcher;
290
291 PoolWatcherListener m_remote_pool_watcher_listener;
292 std::unique_ptr<PoolWatcher<ImageCtxT>> m_remote_pool_watcher;
293
294 std::unique_ptr<InstanceReplayer<ImageCtxT>> m_instance_replayer;
295 std::unique_ptr<ImageDeleter<ImageCtxT>> m_image_deleter;
296
297 ImageMapListener m_image_map_listener;
298 std::unique_ptr<ImageMap<ImageCtxT>> m_image_map;
299
300 std::unique_ptr<InstanceWatcher<ImageCtxT>> m_instance_watcher;
301 };
302
303 } // namespace mirror
304 } // namespace rbd
305
306 extern template class rbd::mirror::NamespaceReplayer<librbd::ImageCtx>;
307
308 #endif // CEPH_RBD_MIRROR_NAMESPACE_REPLAYER_H