Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / ImageSync.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_SYNC_H
5 #define RBD_MIRROR_IMAGE_SYNC_H
6
7 #include "include/int_types.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/journal/TypeTraits.h"
10 #include "common/Mutex.h"
11 #include "tools/rbd_mirror/BaseRequest.h"
12 #include <map>
13 #include <vector>
14
15 class Context;
16 class ContextWQ;
17 class Mutex;
18 class SafeTimer;
19 namespace journal { class Journaler; }
20 namespace librbd { namespace journal { struct MirrorPeerClientMeta; } }
21
22 namespace rbd {
23 namespace mirror {
24
25 class ProgressContext;
26
27 template <typename> class InstanceWatcher;
28
29 namespace image_sync { template <typename> class ImageCopyRequest; }
30 namespace image_sync { template <typename> class SnapshotCopyRequest; }
31
32 template <typename ImageCtxT = librbd::ImageCtx>
33 class ImageSync : public BaseRequest {
34 public:
35   typedef librbd::journal::TypeTraits<ImageCtxT> TypeTraits;
36   typedef typename TypeTraits::Journaler Journaler;
37   typedef librbd::journal::MirrorPeerClientMeta MirrorPeerClientMeta;
38
39   static ImageSync* create(ImageCtxT *local_image_ctx,
40                            ImageCtxT *remote_image_ctx, SafeTimer *timer,
41                            Mutex *timer_lock, const std::string &mirror_uuid,
42                            Journaler *journaler,
43                            MirrorPeerClientMeta *client_meta,
44                            ContextWQ *work_queue,
45                            InstanceWatcher<ImageCtxT> *instance_watcher,
46                            Context *on_finish,
47                            ProgressContext *progress_ctx = nullptr) {
48     return new ImageSync(local_image_ctx, remote_image_ctx, timer, timer_lock,
49                          mirror_uuid, journaler, client_meta, work_queue,
50                          instance_watcher, on_finish, progress_ctx);
51   }
52
53   ImageSync(ImageCtxT *local_image_ctx, ImageCtxT *remote_image_ctx,
54             SafeTimer *timer, Mutex *timer_lock, const std::string &mirror_uuid,
55             Journaler *journaler, MirrorPeerClientMeta *client_meta,
56             ContextWQ *work_queue, InstanceWatcher<ImageCtxT> *instance_watcher,
57             Context *on_finish, ProgressContext *progress_ctx = nullptr);
58   ~ImageSync() override;
59
60   void send() override;
61   void cancel() override;
62
63 protected:
64   void finish(int r) override;
65
66 private:
67   /**
68    * @verbatim
69    *
70    * <start>
71    *    |
72    *    v
73    * NOTIFY_SYNC_REQUEST
74    *    |
75    *    v
76    * PRUNE_CATCH_UP_SYNC_POINT
77    *    |
78    *    v
79    * CREATE_SYNC_POINT (skip if already exists and
80    *    |               not disconnected)
81    *    v
82    * COPY_SNAPSHOTS
83    *    |
84    *    v
85    * COPY_IMAGE . . . . . . . . . . . . . .
86    *    |                                 .
87    *    v                                 .
88    * COPY_OBJECT_MAP (skip if object      .
89    *    |             map disabled)       .
90    *    v                                 .
91    * REFRESH_OBJECT_MAP (skip if object   .
92    *    |                map disabled)    .
93    *    v
94    * PRUNE_SYNC_POINTS                    . (image sync canceled)
95    *    |                                 .
96    *    v                                 .
97    * <finish> < . . . . . . . . . . . . . .
98    *
99    * @endverbatim
100    */
101
102   typedef std::vector<librados::snap_t> SnapIds;
103   typedef std::map<librados::snap_t, SnapIds> SnapMap;
104
105   ImageCtxT *m_local_image_ctx;
106   ImageCtxT *m_remote_image_ctx;
107   SafeTimer *m_timer;
108   Mutex *m_timer_lock;
109   std::string m_mirror_uuid;
110   Journaler *m_journaler;
111   MirrorPeerClientMeta *m_client_meta;
112   ContextWQ *m_work_queue;
113   InstanceWatcher<ImageCtxT> *m_instance_watcher;
114   ProgressContext *m_progress_ctx;
115
116   SnapMap m_snap_map;
117
118   Mutex m_lock;
119   bool m_canceled = false;
120
121   image_sync::SnapshotCopyRequest<ImageCtxT> *m_snapshot_copy_request = nullptr;
122   image_sync::ImageCopyRequest<ImageCtxT> *m_image_copy_request = nullptr;
123   decltype(ImageCtxT::object_map) m_object_map = nullptr;
124
125   void send_notify_sync_request();
126   void handle_notify_sync_request(int r);
127
128   void send_prune_catch_up_sync_point();
129   void handle_prune_catch_up_sync_point(int r);
130
131   void send_create_sync_point();
132   void handle_create_sync_point(int r);
133
134   void send_copy_snapshots();
135   void handle_copy_snapshots(int r);
136
137   void send_copy_image();
138   void handle_copy_image(int r);
139
140   void send_copy_object_map();
141   void handle_copy_object_map(int r);
142
143   void send_refresh_object_map();
144   void handle_refresh_object_map(int r);
145
146   void send_prune_sync_points();
147   void handle_prune_sync_points(int r);
148
149   void update_progress(const std::string &description);
150 };
151
152 } // namespace mirror
153 } // namespace rbd
154
155 extern template class rbd::mirror::ImageSync<librbd::ImageCtx>;
156
157 #endif // RBD_MIRROR_IMAGE_SYNC_H