1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_RBD_MIRROR_NAMESPACE_REPLAYER_H
5 #define CEPH_RBD_MIRROR_NAMESPACE_REPLAYER_H
7 #include "common/AsyncOpTracker.h"
8 #include "common/ceph_mutex.h"
9 #include "include/rados/librados.hpp"
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"
25 class AdminSocketHook
;
27 namespace journal
{ struct CacheManagerHandler
; }
29 namespace librbd
{ class ImageCtx
; }
35 template <typename
> class ServiceDaemon
;
36 template <typename
> class Throttler
;
37 template <typename
> struct Threads
;
40 * Controls mirroring for a single remote cluster.
42 template <typename ImageCtxT
= librbd::ImageCtx
>
43 class NamespaceReplayer
{
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
,
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;
81 bool is_blocklisted() const;
83 void init(Context
*on_finish
);
84 void shut_down(Context
*on_finish
);
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
);
92 void print_status(Formatter
*f
);
102 * <uninitialized> <------------------------------------\
103 * | (init) ^ (error) |
105 * INIT_LOCAL_STATUS_UPDATER * * * * * * * * > SHUT_DOWN_LOCAL_STATUS_UPDATER
108 * INIT_REMOTE_STATUS_UPDATER * * * * * * * > SHUT_DOWN_REMOTE_STATUS_UPDATER
111 * INIT_INSTANCE_REPLAYER * * * * * * * * * > SHUT_DOWN_INSTANCE_REPLAYER
114 * INIT_INSTANCE_WATCHER * * * * * * * * * * SHUT_DOWN_INSTANCE_WATCHER
117 * v STOP_INSTANCE_REPLAYER
120 * | /----------------------------------------------/
122 * <follower> <---------------------------\
125 * v (leader acquired) |
129 * INIT_LOCAL_POOL_WATCHER SHUT_DOWN_IMAGE_MAP
132 * INIT_REMOTE_POOL_WATCHER SHUT_DOWN_POOL_WATCHERS
135 * INIT_IMAGE_DELETER SHUT_DOWN_IMAGE_DELETER
138 * <leader> <-----------\ .
140 * . (image update) | .
141 * . . > NOTIFY_INSTANCE_WATCHER .
143 * . (leader lost / shut down) .
144 * . . . . . . . . . . . . . . . . . . .
149 struct PoolWatcherListener
: public pool_watcher::Listener
{
150 NamespaceReplayer
*namespace_replayer
;
153 PoolWatcherListener(NamespaceReplayer
*namespace_replayer
, bool local
)
154 : namespace_replayer(namespace_replayer
), local(local
) {
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
));
166 struct ImageMapListener
: public image_map::Listener
{
167 NamespaceReplayer
*namespace_replayer
;
169 ImageMapListener(NamespaceReplayer
*namespace_replayer
)
170 : namespace_replayer(namespace_replayer
) {
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
,
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
,
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
);
196 void handle_update(const std::string
&mirror_uuid
,
197 ImageIds
&&added_image_ids
,
198 ImageIds
&&removed_image_ids
);
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
);
207 void init_local_status_updater();
208 void handle_init_local_status_updater(int r
);
210 void init_remote_status_updater();
211 void handle_init_remote_status_updater(int r
);
213 void init_instance_replayer();
214 void handle_init_instance_replayer(int r
);
216 void init_instance_watcher();
217 void handle_init_instance_watcher(int r
);
219 void stop_instance_replayer();
220 void handle_stop_instance_replayer(int r
);
222 void shut_down_instance_watcher();
223 void handle_shut_down_instance_watcher(int r
);
225 void shut_down_instance_replayer();
226 void handle_shut_down_instance_replayer(int r
);
228 void shut_down_remote_status_updater();
229 void handle_shut_down_remote_status_updater(int r
);
231 void shut_down_local_status_updater();
232 void handle_shut_down_local_status_updater(int r
);
234 void init_image_map(Context
*on_finish
);
235 void handle_init_image_map(int r
, ImageMap
<ImageCtxT
> *image_map
,
238 void init_local_pool_watcher(Context
*on_finish
);
239 void handle_init_local_pool_watcher(int r
, Context
*on_finish
);
241 void init_remote_pool_watcher(Context
*on_finish
);
242 void handle_init_remote_pool_watcher(int r
, Context
*on_finish
);
244 void init_image_deleter(Context
* on_finish
);
245 void handle_init_image_deleter(int r
, Context
* on_finish
);
247 void shut_down_image_deleter(Context
* on_finish
);
248 void handle_shut_down_image_deleter(int r
, Context
* on_finish
);
250 void shut_down_pool_watchers(Context
*on_finish
);
251 void handle_shut_down_pool_watchers(int r
, Context
*on_finish
);
253 void shut_down_image_map(Context
*on_finish
);
254 void handle_shut_down_image_map(int r
, Context
*on_finish
);
256 void handle_acquire_image(const std::string
&global_image_id
,
257 const std::string
&instance_id
,
259 void handle_release_image(const std::string
&global_image_id
,
260 const std::string
&instance_id
,
262 void handle_remove_image(const std::string
&mirror_uuid
,
263 const std::string
&global_image_id
,
264 const std::string
&instance_id
,
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
;
280 mutable ceph::mutex m_lock
;
283 Context
*m_on_finish
= nullptr;
285 std::unique_ptr
<MirrorStatusUpdater
<ImageCtxT
>> m_local_status_updater
;
286 std::unique_ptr
<MirrorStatusUpdater
<ImageCtxT
>> m_remote_status_updater
;
288 PoolWatcherListener m_local_pool_watcher_listener
;
289 std::unique_ptr
<PoolWatcher
<ImageCtxT
>> m_local_pool_watcher
;
291 PoolWatcherListener m_remote_pool_watcher_listener
;
292 std::unique_ptr
<PoolWatcher
<ImageCtxT
>> m_remote_pool_watcher
;
294 std::unique_ptr
<InstanceReplayer
<ImageCtxT
>> m_instance_replayer
;
295 std::unique_ptr
<ImageDeleter
<ImageCtxT
>> m_image_deleter
;
297 ImageMapListener m_image_map_listener
;
298 std::unique_ptr
<ImageMap
<ImageCtxT
>> m_image_map
;
300 std::unique_ptr
<InstanceWatcher
<ImageCtxT
>> m_instance_watcher
;
303 } // namespace mirror
306 extern template class rbd::mirror::NamespaceReplayer
<librbd::ImageCtx
>;
308 #endif // CEPH_RBD_MIRROR_NAMESPACE_REPLAYER_H