Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / InstanceReplayer.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 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
15 namespace librbd { class ImageCtx; }
16
17 namespace rbd {
18 namespace mirror {
19
20 template <typename> class ImageDeleter;
21 template <typename> class ImageReplayer;
22 template <typename> class InstanceWatcher;
23 template <typename> class ServiceDaemon;
24 template <typename> struct Threads;
25
26 template <typename ImageCtxT = librbd::ImageCtx>
27 class InstanceReplayer {
28 public:
29   static InstanceReplayer* create(
30       Threads<ImageCtxT> *threads,
31       ServiceDaemon<ImageCtxT>* service_daemon,
32       ImageDeleter<ImageCtxT>* image_deleter,
33       RadosRef local_rados, const std::string &local_mirror_uuid,
34       int64_t local_pool_id) {
35     return new InstanceReplayer(threads, service_daemon, image_deleter,
36                                 local_rados, local_mirror_uuid, local_pool_id);
37   }
38   void destroy() {
39     delete this;
40   }
41
42   InstanceReplayer(Threads<ImageCtxT> *threads,
43                    ServiceDaemon<ImageCtxT>* service_daemon,
44                    ImageDeleter<ImageCtxT>* image_deleter,
45                    RadosRef local_rados, const std::string &local_mirror_uuid,
46                    int64_t local_pool_id);
47   ~InstanceReplayer();
48
49   int init();
50   void shut_down();
51
52   void init(Context *on_finish);
53   void shut_down(Context *on_finish);
54
55   void add_peer(std::string peer_uuid, librados::IoCtx io_ctx);
56
57   void acquire_image(InstanceWatcher<ImageCtxT> *instance_watcher,
58                      const std::string &global_image_id, Context *on_finish);
59   void release_image(const std::string &global_image_id, Context *on_finish);
60   void remove_peer_image(const std::string &global_image_id,
61                          const std::string &peer_mirror_uuid,
62                          Context *on_finish);
63
64   void release_all(Context *on_finish);
65
66   void print_status(Formatter *f, stringstream *ss);
67   void start();
68   void stop();
69   void restart();
70   void flush();
71
72 private:
73   /**
74    * @verbatim
75    *
76    * <uninitialized> <-------------------\
77    *    | (init)                         |                    (repeat for each
78    *    v                             STOP_IMAGE_REPLAYER ---\ image replayer)
79    * SCHEDULE_IMAGE_STATE_CHECK_TASK     ^         ^         |
80    *    |                                |         |         |
81    *    v          (shut_down)           |         \---------/
82    * <initialized> -----------------> WAIT_FOR_OPS
83    *
84    * @endverbatim
85    */
86
87   Threads<ImageCtxT> *m_threads;
88   ServiceDaemon<ImageCtxT>* m_service_daemon;
89   ImageDeleter<ImageCtxT>* m_image_deleter;
90   RadosRef m_local_rados;
91   std::string m_local_mirror_uuid;
92   int64_t m_local_pool_id;
93
94   Mutex m_lock;
95   AsyncOpTracker m_async_op_tracker;
96   std::map<std::string, ImageReplayer<ImageCtxT> *> m_image_replayers;
97   Peers m_peers;
98   Context *m_image_state_check_task = nullptr;
99   Context *m_on_shut_down = nullptr;
100   bool m_manual_stop = false;
101
102   void wait_for_ops();
103   void handle_wait_for_ops(int r);
104
105   void start_image_replayer(ImageReplayer<ImageCtxT> *image_replayer);
106   void queue_start_image_replayers();
107   void start_image_replayers(int r);
108
109   void stop_image_replayer(ImageReplayer<ImageCtxT> *image_replayer,
110                            Context *on_finish);
111
112   void stop_image_replayers();
113   void handle_stop_image_replayers(int r);
114
115   void schedule_image_state_check_task();
116   void cancel_image_state_check_task();
117 };
118
119 } // namespace mirror
120 } // namespace rbd
121
122 extern template class rbd::mirror::InstanceReplayer<librbd::ImageCtx>;
123
124 #endif // RBD_MIRROR_INSTANCE_REPLAYER_H