]> git.proxmox.com Git - ceph.git/blame - ceph/src/tools/rbd_mirror/InstanceReplayer.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd_mirror / InstanceReplayer.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 RBD_MIRROR_INSTANCE_REPLAYER_H
5#define RBD_MIRROR_INSTANCE_REPLAYER_H
6
7#include <map>
8#include <sstream>
9
10#include "common/AsyncOpTracker.h"
11#include "common/Formatter.h"
12#include "common/Mutex.h"
13#include "types.h"
14
15namespace librbd { class ImageCtx; }
16
17namespace rbd {
18namespace mirror {
19
20class ImageDeleter;
21
22template <typename> class ImageReplayer;
23template <typename> struct Threads;
24
25template <typename ImageCtxT = librbd::ImageCtx>
26class InstanceReplayer {
27public:
28 static InstanceReplayer* create(
29 Threads<ImageCtxT> *threads, std::shared_ptr<ImageDeleter> image_deleter,
30 ImageSyncThrottlerRef<ImageCtxT> image_sync_throttler, RadosRef local_rados,
31 const std::string &local_mirror_uuid, int64_t local_pool_id) {
32 return new InstanceReplayer(threads, image_deleter, image_sync_throttler,
33 local_rados, local_mirror_uuid, local_pool_id);
34 }
35 void destroy() {
36 delete this;
37 }
38
39 InstanceReplayer(Threads<ImageCtxT> *threads,
40 std::shared_ptr<ImageDeleter> image_deleter,
41 ImageSyncThrottlerRef<ImageCtxT> image_sync_throttler,
42 RadosRef local_rados, const std::string &local_mirror_uuid,
43 int64_t local_pool_id);
44 ~InstanceReplayer();
45
46 int init();
47 void shut_down();
48
49 void init(Context *on_finish);
50 void shut_down(Context *on_finish);
51
52 void add_peer(std::string mirror_uuid, librados::IoCtx io_ctx);
53 void remove_peer(std::string mirror_uuid);
54
55 void acquire_image(const std::string &global_image_id,
56 const std::string &peer_mirror_uuid,
57 const std::string &peer_image_id,
58 Context *on_finish);
59 void release_image(const std::string &global_image_id,
60 const std::string &peer_mirror_uuid,
61 const std::string &peer_image_id,
62 bool schedule_delete, Context *on_finish);
63 void release_all(Context *on_finish);
64
65 void print_status(Formatter *f, stringstream *ss);
66 void start();
67 void stop();
68 void restart();
69 void flush();
70
71private:
72 /**
73 * @verbatim
74 *
75 * <uninitialized> <-------------------\
76 * | (init) | (repeat for each
77 * v STOP_IMAGE_REPLAYER ---\ image replayer)
78 * SCHEDULE_IMAGE_STATE_CHECK_TASK ^ ^ |
79 * | | | |
80 * v (shut_down) | \---------/
81 * <initialized> -----------------> WAIT_FOR_OPS
82 *
83 * @endverbatim
84 */
85
86 struct Peer {
87 std::string mirror_uuid;
88 librados::IoCtx io_ctx;
89
90 Peer() {
91 }
92
93 Peer(const std::string &mirror_uuid) : mirror_uuid(mirror_uuid) {
94 }
95
96 Peer(const std::string &mirror_uuid, librados::IoCtx &io_ctx)
97 : mirror_uuid(mirror_uuid), io_ctx(io_ctx) {
98 }
99
100 inline bool operator<(const Peer &rhs) const {
101 return mirror_uuid < rhs.mirror_uuid;
102 }
103 inline bool operator==(const Peer &rhs) const {
104 return mirror_uuid == rhs.mirror_uuid;
105 }
106 };
107
108 typedef std::set<Peer> Peers;
109
110 Threads<ImageCtxT> *m_threads;
111 std::shared_ptr<ImageDeleter> m_image_deleter;
112 ImageSyncThrottlerRef<ImageCtxT> m_image_sync_throttler;
113 RadosRef m_local_rados;
114 std::string m_local_mirror_uuid;
115 int64_t m_local_pool_id;
116
117 Mutex m_lock;
118 AsyncOpTracker m_async_op_tracker;
119 std::map<std::string, ImageReplayer<ImageCtxT> *> m_image_replayers;
120 Peers m_peers;
121 Context *m_image_state_check_task = nullptr;
122 Context *m_on_shut_down = nullptr;
123 bool m_manual_stop = false;
124
125 void wait_for_ops();
126 void handle_wait_for_ops(int r);
127
128 void start_image_replayer(ImageReplayer<ImageCtxT> *image_replayer);
129 void start_image_replayers();
130
131 void stop_image_replayer(ImageReplayer<ImageCtxT> *image_replayer,
132 Context *on_finish);
133
134 void stop_image_replayers();
135 void handle_stop_image_replayers(int r);
136
137 void schedule_image_state_check_task();
138 void cancel_image_state_check_task();
139};
140
141} // namespace mirror
142} // namespace rbd
143
144extern template class rbd::mirror::InstanceReplayer<librbd::ImageCtx>;
145
146#endif // RBD_MIRROR_INSTANCE_REPLAYER_H