Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / Instances.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_INSTANCES_H
5 #define CEPH_RBD_MIRROR_INSTANCES_H
6
7 #include <map>
8 #include <vector>
9
10 #include "include/buffer.h"
11 #include "common/AsyncOpTracker.h"
12 #include "common/Mutex.h"
13 #include "librbd/Watcher.h"
14
15 namespace librados { class IoCtx; }
16 namespace librbd { class ImageCtx; }
17
18 namespace rbd {
19 namespace mirror {
20
21 template <typename> struct Threads;
22
23 template <typename ImageCtxT = librbd::ImageCtx>
24 class Instances {
25 public:
26   static Instances *create(Threads<ImageCtxT> *threads,
27                            librados::IoCtx &ioctx) {
28     return new Instances(threads, ioctx);
29   }
30   void destroy() {
31     delete this;
32   }
33
34   Instances(Threads<ImageCtxT> *threads, librados::IoCtx &ioctx);
35   virtual ~Instances();
36
37   void init(Context *on_finish);
38   void shut_down(Context *on_finish);
39
40   void notify(const std::string &instance_id);
41   void list(std::vector<std::string> *instance_ids);
42
43 private:
44   /**
45    * @verbatim
46    *
47    * <uninitialized> <---------------------\
48    *    | (init)           ^               |
49    *    v          (error) *               |
50    * GET_INSTANCES * * * * *            WAIT_FOR_OPS
51    *    |                                  ^
52    *    v          (shut_down)             |
53    * <initialized> ------------------------/
54    *      .
55    *      . (remove_instance)
56    *      v
57    *   REMOVE_INSTANCE
58    *
59    * @endverbatim
60    */
61
62   struct Instance {
63     std::string id;
64     Context *timer_task = nullptr;
65
66     Instance(const std::string &instance_id) : id(instance_id) {
67     }
68   };
69
70   struct C_Notify : Context {
71     Instances *instances;
72     std::string instance_id;
73
74     C_Notify(Instances *instances, const std::string &instance_id)
75       : instances(instances), instance_id(instance_id) {
76       instances->m_async_op_tracker.start_op();
77     }
78
79     void finish(int r) override {
80       instances->handle_notify(instance_id);
81       instances->m_async_op_tracker.finish_op();
82     }
83   };
84
85   Threads<ImageCtxT> *m_threads;
86   librados::IoCtx &m_ioctx;
87   CephContext *m_cct;
88
89   Mutex m_lock;
90   std::vector<std::string> m_instance_ids;
91   std::map<std::string, Instance> m_instances;
92   Context *m_on_finish = nullptr;
93   AsyncOpTracker m_async_op_tracker;
94
95   void handle_notify(const std::string &instance_id);
96
97   void get_instances();
98   void handle_get_instances(int r);
99
100   void wait_for_ops();
101   void handle_wait_for_ops(int r);
102
103   void remove_instance(Instance &instance);
104   void handle_remove_instance(int r);
105
106   void cancel_remove_task(Instance &instance);
107   void schedule_remove_task(Instance &instance);
108 };
109
110 } // namespace mirror
111 } // namespace rbd
112
113 #endif // CEPH_RBD_MIRROR_INSTANCES_H