Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / image_replayer / BootstrapRequest.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_IMAGE_REPLAYER_BOOTSTRAP_REQUEST_H
5 #define RBD_MIRROR_IMAGE_REPLAYER_BOOTSTRAP_REQUEST_H
6
7 #include "include/int_types.h"
8 #include "include/rados/librados.hpp"
9 #include "common/Mutex.h"
10 #include "cls/journal/cls_journal_types.h"
11 #include "librbd/journal/TypeTraits.h"
12 #include "tools/rbd_mirror/BaseRequest.h"
13 #include "tools/rbd_mirror/types.h"
14 #include <list>
15 #include <string>
16
17 class Context;
18 class ContextWQ;
19 class Mutex;
20 class SafeTimer;
21 namespace journal { class Journaler; }
22 namespace librbd { class ImageCtx; }
23 namespace librbd { namespace journal { struct MirrorPeerClientMeta; } }
24
25 namespace rbd {
26 namespace mirror {
27
28 class ProgressContext;
29
30 template <typename> class ImageSync;
31 template <typename> class InstanceWatcher;
32
33 namespace image_replayer {
34
35 template <typename ImageCtxT = librbd::ImageCtx>
36 class BootstrapRequest : public BaseRequest {
37 public:
38   typedef librbd::journal::TypeTraits<ImageCtxT> TypeTraits;
39   typedef typename TypeTraits::Journaler Journaler;
40   typedef librbd::journal::MirrorPeerClientMeta MirrorPeerClientMeta;
41   typedef rbd::mirror::ProgressContext ProgressContext;
42
43   static BootstrapRequest* create(
44         librados::IoCtx &local_io_ctx,
45         librados::IoCtx &remote_io_ctx,
46         InstanceWatcher<ImageCtxT> *instance_watcher,
47         ImageCtxT **local_image_ctx,
48         const std::string &local_image_id,
49         const std::string &remote_image_id,
50         const std::string &global_image_id,
51         ContextWQ *work_queue, SafeTimer *timer,
52         Mutex *timer_lock,
53         const std::string &local_mirror_uuid,
54         const std::string &remote_mirror_uuid,
55         Journaler *journaler,
56         MirrorPeerClientMeta *client_meta,
57         Context *on_finish,
58         bool *do_resync,
59         ProgressContext *progress_ctx = nullptr) {
60     return new BootstrapRequest(local_io_ctx, remote_io_ctx,
61                                 instance_watcher, local_image_ctx,
62                                 local_image_id, remote_image_id,
63                                 global_image_id, work_queue, timer, timer_lock,
64                                 local_mirror_uuid, remote_mirror_uuid,
65                                 journaler, client_meta, on_finish, do_resync,
66                                 progress_ctx);
67   }
68
69   BootstrapRequest(librados::IoCtx &local_io_ctx,
70                    librados::IoCtx &remote_io_ctx,
71                    InstanceWatcher<ImageCtxT> *instance_watcher,
72                    ImageCtxT **local_image_ctx,
73                    const std::string &local_image_id,
74                    const std::string &remote_image_id,
75                    const std::string &global_image_id, ContextWQ *work_queue,
76                    SafeTimer *timer, Mutex *timer_lock,
77                    const std::string &local_mirror_uuid,
78                    const std::string &remote_mirror_uuid, Journaler *journaler,
79                    MirrorPeerClientMeta *client_meta, Context *on_finish,
80                    bool *do_resync, ProgressContext *progress_ctx = nullptr);
81   ~BootstrapRequest() override;
82
83   bool is_syncing() const;
84
85   void send() override;
86   void cancel() override;
87
88 private:
89   /**
90    * @verbatim
91    *
92    * <start>
93    *    |
94    *    v
95    * GET_REMOTE_TAG_CLASS * * * * * * * * * * * * * * * * * *
96    *    |                                                   * (error)
97    *    v                                                   *
98    * OPEN_REMOTE_IMAGE  * * * * * * * * * * * * * * * * * * *
99    *    |                                                   *
100    *    v                                                   *
101    * GET_CLIENT * * * * * * * * * * * * * * * * * * * * *   *
102    *    |                                               *   *
103    *    |/----------------------------------------------*---*---\
104    *    v (skip if not needed)                          *   *   |
105    * REGISTER_CLIENT  * * * * * * * * * * * * * * * * * *   *   |
106    *    |                                               *   *   |
107    *    v                                               *   *   |
108    * IS_PRIMARY * * * * * * * * * * * * * * * * * * * * *   *   |
109    *    |                                               *   *   |
110    *    | (remote image primary, no local image id)     *   *   |
111    *    \----> UPDATE_CLIENT_IMAGE  * * * * * * * * * * *   *   |
112    *    |         |                                     *   *   |
113    *    |         v                                     *   *   |
114    *    \----> CREATE_LOCAL_IMAGE * * * * * * * * * * * *   *   |
115    *    |         |                                     *   *   |
116    *    |         v                                     *   *   |
117    *    | (remote image primary)                        *   *   |
118    *    \----> OPEN_LOCAL_IMAGE * * * * * * * * * * * * *   *   |
119    *    |         |   .                                 *   *   |
120    *    |         |   . (image doesn't exist)           *   *   |
121    *    |         |   . . > UNREGISTER_CLIENT * * * * * *   *   |
122    *    |         |             |                       *   *   |
123    *    |         |             \-----------------------*---*---/
124    *    |         |                                     *   *
125    *    |         v (skip if not needed)                *   *
126    *    |      GET_REMOTE_TAGS  * * * * * * *           *   *
127    *    |         |                         *           *   *
128    *    |         v (skip if not needed)    v           *   *
129    *    |      IMAGE_SYNC * * * > CLOSE_LOCAL_IMAGE     *   *
130    *    |         |                         |           *   *
131    *    |         \-----------------\ /-----/           *   *
132    *    |                            |                  *   *
133    *    |                            |                  *   *
134    *    | (skip if not needed)       |                  *   *
135    *    \----> UPDATE_CLIENT_STATE  *|* * * * * * * * * *   *
136    *                |                |                  *   *
137    *    /-----------/----------------/                  *   *
138    *    |                                               *   *
139    *    v                                               *   *
140    * CLOSE_REMOTE_IMAGE < * * * * * * * * * * * * * * * *   *
141    *    |                                                   *
142    *    v                                                   *
143    * <finish> < * * * * * * * * * * * * * * * * * * * * * * *
144    *
145    * @endverbatim
146    */
147   typedef std::list<cls::journal::Tag> Tags;
148
149   librados::IoCtx &m_local_io_ctx;
150   librados::IoCtx &m_remote_io_ctx;
151   InstanceWatcher<ImageCtxT> *m_instance_watcher;
152   ImageCtxT **m_local_image_ctx;
153   std::string m_local_image_id;
154   std::string m_remote_image_id;
155   std::string m_global_image_id;
156   ContextWQ *m_work_queue;
157   SafeTimer *m_timer;
158   Mutex *m_timer_lock;
159   std::string m_local_mirror_uuid;
160   std::string m_remote_mirror_uuid;
161   Journaler *m_journaler;
162   MirrorPeerClientMeta *m_client_meta;
163   ProgressContext *m_progress_ctx;
164   bool *m_do_resync;
165
166   mutable Mutex m_lock;
167   bool m_canceled = false;
168
169   Tags m_remote_tags;
170   cls::journal::Client m_client;
171   uint64_t m_remote_tag_class = 0;
172   ImageCtxT *m_remote_image_ctx = nullptr;
173   bool m_primary = false;
174   int m_ret_val = 0;
175   ImageSync<ImageCtxT> *m_image_sync = nullptr;
176
177   bufferlist m_out_bl;
178
179   void get_remote_tag_class();
180   void handle_get_remote_tag_class(int r);
181
182   void get_client();
183   void handle_get_client(int r);
184
185   void register_client();
186   void handle_register_client(int r);
187
188   void open_remote_image();
189   void handle_open_remote_image(int r);
190
191   void is_primary();
192   void handle_is_primary(int r);
193
194   void update_client_state();
195   void handle_update_client_state(int r);
196
197   void open_local_image();
198   void handle_open_local_image(int r);
199
200   void unregister_client();
201   void handle_unregister_client(int r);
202
203   void create_local_image();
204   void handle_create_local_image(int r);
205
206   void update_client_image();
207   void handle_update_client_image(int r);
208
209   void get_remote_tags();
210   void handle_get_remote_tags(int r);
211
212   void image_sync();
213   void handle_image_sync(int r);
214
215   void close_local_image();
216   void handle_close_local_image(int r);
217
218   void close_remote_image();
219   void handle_close_remote_image(int r);
220
221   bool decode_client_meta();
222
223   void update_progress(const std::string &description);
224 };
225
226 } // namespace image_replayer
227 } // namespace mirror
228 } // namespace rbd
229
230 extern template class rbd::mirror::image_replayer::BootstrapRequest<librbd::ImageCtx>;
231
232 #endif // RBD_MIRROR_IMAGE_REPLAYER_BOOTSTRAP_REQUEST_H