]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/PoolReplayer.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / tools / rbd_mirror / PoolReplayer.h
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#ifndef CEPH_RBD_MIRROR_POOL_REPLAYER_H
5#define CEPH_RBD_MIRROR_POOL_REPLAYER_H
6
7#include "common/AsyncOpTracker.h"
8#include "common/Cond.h"
9#include "common/Mutex.h"
10#include "common/WorkQueue.h"
11#include "include/rados/librados.hpp"
12
13#include "ClusterWatcher.h"
14#include "LeaderWatcher.h"
15#include "PoolWatcher.h"
16#include "ImageDeleter.h"
11fdf7f2
TL
17#include "tools/rbd_mirror/Types.h"
18#include "tools/rbd_mirror/image_map/Types.h"
19#include "tools/rbd_mirror/leader_watcher/Types.h"
20#include "tools/rbd_mirror/pool_watcher/Types.h"
c07f9fc5 21#include "tools/rbd_mirror/service_daemon/Types.h"
7c673cae
FG
22
23#include <set>
24#include <map>
25#include <memory>
26#include <atomic>
27#include <string>
11fdf7f2 28#include <vector>
7c673cae
FG
29
30class AdminSocketHook;
31
32namespace librbd { class ImageCtx; }
33
34namespace rbd {
35namespace mirror {
36
11fdf7f2 37template <typename> class ImageMap;
7c673cae
FG
38template <typename> class InstanceReplayer;
39template <typename> class InstanceWatcher;
c07f9fc5
FG
40template <typename> class ServiceDaemon;
41template <typename> struct Threads;
7c673cae
FG
42
43/**
44 * Controls mirroring for a single remote cluster.
45 */
11fdf7f2 46template <typename ImageCtxT = librbd::ImageCtx>
7c673cae
FG
47class PoolReplayer {
48public:
11fdf7f2
TL
49 PoolReplayer(Threads<ImageCtxT> *threads,
50 ServiceDaemon<ImageCtxT>* service_daemon,
51 int64_t local_pool_id, const PeerSpec &peer,
7c673cae
FG
52 const std::vector<const char*> &args);
53 ~PoolReplayer();
54 PoolReplayer(const PoolReplayer&) = delete;
55 PoolReplayer& operator=(const PoolReplayer&) = delete;
56
57 bool is_blacklisted() const;
58 bool is_leader() const;
c07f9fc5
FG
59 bool is_running() const;
60
61 void init();
62 void shut_down();
7c673cae 63
7c673cae
FG
64 void run();
65
66 void print_status(Formatter *f, stringstream *ss);
67 void start();
68 void stop(bool manual);
69 void restart();
70 void flush();
71 void release_leader();
72
73private:
11fdf7f2
TL
74 /**
75 * @verbatim
76 *
77 * <start>
78 * |
79 * v
80 * INIT
81 * |
82 * v
83 * <follower> <-------------------------\
84 * . |
85 * . |
86 * v (leader acquired) |
87 * INIT_IMAGE_MAP SHUT_DOWN_IMAGE_MAP
88 * | ^
89 * v |
90 * INIT_LOCAL_POOL_WATCHER WAIT_FOR_NOTIFICATIONS
91 * | ^
92 * v |
93 * INIT_REMOTE_POOL_WATCHER SHUT_DOWN_POOL_WATCHERS
94 * | ^
95 * v |
96 * INIT_IMAGE_DELETER SHUT_DOWN_IMAGE_DELETER
97 * | ^
98 * v .
99 * <leader> <-----------\ .
100 * . | .
101 * . (image update) | .
102 * . . > NOTIFY_INSTANCE_WATCHER .
103 * . .
104 * . (leader lost / shut down) .
105 * . . . . . . . . . . . . . . . . . .
106 *
107 * @endverbatim
108 */
109
110 typedef std::vector<std::string> InstanceIds;
111
112 struct PoolWatcherListener : public pool_watcher::Listener {
7c673cae
FG
113 PoolReplayer *pool_replayer;
114 bool local;
115
116 PoolWatcherListener(PoolReplayer *pool_replayer, bool local)
117 : pool_replayer(pool_replayer), local(local) {
118 }
119
120 void handle_update(const std::string &mirror_uuid,
121 ImageIds &&added_image_ids,
122 ImageIds &&removed_image_ids) override {
123 pool_replayer->handle_update((local ? "" : mirror_uuid),
124 std::move(added_image_ids),
125 std::move(removed_image_ids));
126 }
127 };
128
11fdf7f2
TL
129 struct ImageMapListener : public image_map::Listener {
130 PoolReplayer *pool_replayer;
131
132 ImageMapListener(PoolReplayer *pool_replayer)
133 : pool_replayer(pool_replayer) {
134 }
135
136 void acquire_image(const std::string &global_image_id,
137 const std::string &instance_id,
138 Context* on_finish) override {
139 pool_replayer->handle_acquire_image(global_image_id, instance_id,
140 on_finish);
141 }
142
143 void release_image(const std::string &global_image_id,
144 const std::string &instance_id,
145 Context* on_finish) override {
146 pool_replayer->handle_release_image(global_image_id, instance_id,
147 on_finish);
148 }
149
150 void remove_image(const std::string &mirror_uuid,
151 const std::string &global_image_id,
152 const std::string &instance_id,
153 Context* on_finish) override {
154 pool_replayer->handle_remove_image(mirror_uuid, global_image_id,
155 instance_id, on_finish);
156 }
157 };
158
7c673cae
FG
159 void handle_update(const std::string &mirror_uuid,
160 ImageIds &&added_image_ids,
161 ImageIds &&removed_image_ids);
162
163 int init_rados(const std::string &cluster_name,
164 const std::string &client_name,
11fdf7f2
TL
165 const std::string &mon_host,
166 const std::string &key,
3efd9988
FG
167 const std::string &description, RadosRef *rados_ref,
168 bool strip_cluster_overrides);
7c673cae
FG
169
170 void handle_post_acquire_leader(Context *on_finish);
171 void handle_pre_release_leader(Context *on_finish);
172
11fdf7f2
TL
173 void init_image_map(Context *on_finish);
174 void handle_init_image_map(int r, Context *on_finish);
175
7c673cae
FG
176 void init_local_pool_watcher(Context *on_finish);
177 void handle_init_local_pool_watcher(int r, Context *on_finish);
178
179 void init_remote_pool_watcher(Context *on_finish);
11fdf7f2
TL
180 void handle_init_remote_pool_watcher(int r, Context *on_finish);
181
182 void init_image_deleter(Context* on_finish);
183 void handle_init_image_deleter(int r, Context* on_finish);
184
185 void shut_down_image_deleter(Context* on_finish);
186 void handle_shut_down_image_deleter(int r, Context* on_finish);
7c673cae
FG
187
188 void shut_down_pool_watchers(Context *on_finish);
189 void handle_shut_down_pool_watchers(int r, Context *on_finish);
190
191 void wait_for_update_ops(Context *on_finish);
192 void handle_wait_for_update_ops(int r, Context *on_finish);
193
11fdf7f2
TL
194 void shut_down_image_map(Context *on_finish);
195 void handle_shut_down_image_map(int r, Context *on_finish);
196
31f18b77
FG
197 void handle_update_leader(const std::string &leader_instance_id);
198
11fdf7f2
TL
199 void handle_acquire_image(const std::string &global_image_id,
200 const std::string &instance_id,
201 Context* on_finish);
202 void handle_release_image(const std::string &global_image_id,
203 const std::string &instance_id,
204 Context* on_finish);
205 void handle_remove_image(const std::string &mirror_uuid,
206 const std::string &global_image_id,
207 const std::string &instance_id,
208 Context* on_finish);
209
210 void handle_instances_added(const InstanceIds &instance_ids);
211 void handle_instances_removed(const InstanceIds &instance_ids);
212
213 Threads<ImageCtxT> *m_threads;
214 ServiceDaemon<ImageCtxT>* m_service_daemon;
c07f9fc5 215 int64_t m_local_pool_id = -1;
11fdf7f2 216 PeerSpec m_peer;
c07f9fc5
FG
217 std::vector<const char*> m_args;
218
7c673cae
FG
219 mutable Mutex m_lock;
220 Cond m_cond;
221 std::atomic<bool> m_stopping = { false };
222 bool m_manual_stop = false;
223 bool m_blacklisted = false;
224
7c673cae
FG
225 RadosRef m_local_rados;
226 RadosRef m_remote_rados;
227
228 librados::IoCtx m_local_io_ctx;
229 librados::IoCtx m_remote_io_ctx;
230
7c673cae 231 PoolWatcherListener m_local_pool_watcher_listener;
11fdf7f2 232 std::unique_ptr<PoolWatcher<ImageCtxT>> m_local_pool_watcher;
7c673cae
FG
233
234 PoolWatcherListener m_remote_pool_watcher_listener;
11fdf7f2
TL
235 std::unique_ptr<PoolWatcher<ImageCtxT>> m_remote_pool_watcher;
236
237 std::unique_ptr<InstanceReplayer<ImageCtxT>> m_instance_replayer;
238 std::unique_ptr<ImageDeleter<ImageCtxT>> m_image_deleter;
7c673cae 239
11fdf7f2
TL
240 ImageMapListener m_image_map_listener;
241 std::unique_ptr<ImageMap<ImageCtxT>> m_image_map;
7c673cae
FG
242
243 std::string m_asok_hook_name;
c07f9fc5 244 AdminSocketHook *m_asok_hook = nullptr;
7c673cae 245
c07f9fc5
FG
246 service_daemon::CalloutId m_callout_id = service_daemon::CALLOUT_ID_NONE;
247
7c673cae
FG
248 class PoolReplayerThread : public Thread {
249 PoolReplayer *m_pool_replayer;
250 public:
251 PoolReplayerThread(PoolReplayer *pool_replayer)
252 : m_pool_replayer(pool_replayer) {
253 }
254 void *entry() override {
255 m_pool_replayer->run();
256 return 0;
257 }
258 } m_pool_replayer_thread;
259
11fdf7f2 260 class LeaderListener : public leader_watcher::Listener {
7c673cae
FG
261 public:
262 LeaderListener(PoolReplayer *pool_replayer)
263 : m_pool_replayer(pool_replayer) {
264 }
265
266 protected:
267 void post_acquire_handler(Context *on_finish) override {
268 m_pool_replayer->handle_post_acquire_leader(on_finish);
269 }
270
271 void pre_release_handler(Context *on_finish) override {
272 m_pool_replayer->handle_pre_release_leader(on_finish);
273 }
274
31f18b77
FG
275 void update_leader_handler(
276 const std::string &leader_instance_id) override {
277 m_pool_replayer->handle_update_leader(leader_instance_id);
278 }
279
11fdf7f2
TL
280 void handle_instances_added(const InstanceIds& instance_ids) override {
281 m_pool_replayer->handle_instances_added(instance_ids);
282 }
283
284 void handle_instances_removed(const InstanceIds& instance_ids) override {
285 m_pool_replayer->handle_instances_removed(instance_ids);
286 }
287
7c673cae
FG
288 private:
289 PoolReplayer *m_pool_replayer;
290 } m_leader_listener;
291
11fdf7f2
TL
292 std::unique_ptr<LeaderWatcher<ImageCtxT>> m_leader_watcher;
293 std::unique_ptr<InstanceWatcher<ImageCtxT>> m_instance_watcher;
7c673cae
FG
294 AsyncOpTracker m_update_op_tracker;
295};
296
297} // namespace mirror
298} // namespace rbd
299
11fdf7f2
TL
300extern template class rbd::mirror::PoolReplayer<librbd::ImageCtx>;
301
7c673cae 302#endif // CEPH_RBD_MIRROR_POOL_REPLAYER_H