Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / image_sync / ObjectCopyRequest.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_OBJECT_COPY_REQUEST_H
5 #define RBD_MIRROR_IMAGE_SYNC_OBJECT_COPY_REQUEST_H
6
7 #include "include/int_types.h"
8 #include "include/rados/librados.hpp"
9 #include "common/snap_types.h"
10 #include "librbd/ImageCtx.h"
11 #include <list>
12 #include <map>
13 #include <string>
14 #include <vector>
15
16 class Context;
17 class RWLock;
18
19 namespace rbd {
20 namespace mirror {
21 namespace image_sync {
22
23 template <typename ImageCtxT = librbd::ImageCtx>
24 class ObjectCopyRequest {
25 public:
26   typedef std::vector<librados::snap_t> SnapIds;
27   typedef std::map<librados::snap_t, SnapIds> SnapMap;
28
29   static ObjectCopyRequest* create(ImageCtxT *local_image_ctx,
30                                    ImageCtxT *remote_image_ctx,
31                                    const SnapMap *snap_map,
32                                    uint64_t object_number, Context *on_finish) {
33     return new ObjectCopyRequest(local_image_ctx, remote_image_ctx, snap_map,
34                                  object_number, on_finish);
35   }
36
37   ObjectCopyRequest(ImageCtxT *local_image_ctx, ImageCtxT *remote_image_ctx,
38                     const SnapMap *snap_map, uint64_t object_number,
39                     Context *on_finish);
40
41   void send();
42
43   // testing support
44   inline librados::IoCtx &get_local_io_ctx() {
45     return m_local_io_ctx;
46   }
47   inline librados::IoCtx &get_remote_io_ctx() {
48     return m_remote_io_ctx;
49   }
50
51 private:
52   /**
53    * @verbatim
54    *
55    * <start>
56    *    |
57    *    v
58    * LIST_SNAPS < * * *
59    *    |             * (-ENOENT and snap set stale)
60    *    |   * * * * * *
61    *    |   *
62    *    v   *
63    * READ_OBJECT <--------\
64    *    |                 | (repeat for each snapshot)
65    *    v                 |
66    * WRITE_OBJECT --------/
67    *    |
68    *    |     /-----------\
69    *    |     |           | (repeat for each snapshot)
70    *    v     v           |
71    * UPDATE_OBJECT_MAP ---/ (skip if object
72    *    |                    map disabled)
73    *    |
74    *    v
75    * <finish>
76    *
77    * @endverbatim
78    */
79
80   enum SyncOpType {
81     SYNC_OP_TYPE_WRITE,
82     SYNC_OP_TYPE_TRUNC,
83     SYNC_OP_TYPE_REMOVE
84   };
85
86   typedef std::map<uint64_t, uint64_t> ExtentMap;
87
88   struct SyncOp {
89     SyncOp(SyncOpType type, uint64_t offset, uint64_t length)
90       : type(type), offset(offset), length(length) {
91     }
92
93     SyncOpType type;
94     uint64_t offset;
95     uint64_t length;
96
97     ExtentMap extent_map;
98     bufferlist out_bl;
99   };
100
101   typedef std::list<SyncOp> SyncOps;
102   typedef std::pair<librados::snap_t, librados::snap_t> WriteReadSnapIds;
103   typedef std::map<WriteReadSnapIds, SyncOps> SnapSyncOps;
104   typedef std::map<librados::snap_t, uint8_t> SnapObjectStates;
105   typedef std::map<librados::snap_t, uint64_t> SnapObjectSizes;
106
107   ImageCtxT *m_local_image_ctx;
108   ImageCtxT *m_remote_image_ctx;
109   const SnapMap *m_snap_map;
110   uint64_t m_object_number;
111   Context *m_on_finish;
112
113   decltype(m_local_image_ctx->data_ctx) m_local_io_ctx;
114   decltype(m_remote_image_ctx->data_ctx) m_remote_io_ctx;
115   std::string m_local_oid;
116   std::string m_remote_oid;
117
118   librados::snap_set_t m_snap_set;
119   int m_snap_ret;
120
121   bool m_retry_missing_read = false;
122   librados::snap_set_t m_retry_snap_set;
123
124   SnapSyncOps m_snap_sync_ops;
125   SnapObjectStates m_snap_object_states;
126   SnapObjectSizes m_snap_object_sizes;
127
128   void send_list_snaps();
129   void handle_list_snaps(int r);
130
131   void send_read_object();
132   void handle_read_object(int r);
133
134   void send_write_object();
135   void handle_write_object(int r);
136
137   void send_update_object_map();
138   void handle_update_object_map(int r);
139
140   Context *start_local_op(RWLock &owner_lock);
141
142   void compute_diffs();
143   void finish(int r);
144
145 };
146
147 } // namespace image_sync
148 } // namespace mirror
149 } // namespace rbd
150
151 extern template class rbd::mirror::image_sync::ObjectCopyRequest<librbd::ImageCtx>;
152
153 #endif // RBD_MIRROR_IMAGE_SYNC_OBJECT_COPY_REQUEST_H