Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / image_sync / SyncPointPruneRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "SyncPointPruneRequest.h"
5 #include "common/errno.h"
6 #include "journal/Journaler.h"
7 #include "librbd/ImageCtx.h"
8 #include "librbd/ImageState.h"
9 #include "librbd/Operations.h"
10 #include "librbd/Utils.h"
11 #include <set>
12
13 #define dout_context g_ceph_context
14 #define dout_subsys ceph_subsys_rbd_mirror
15 #undef dout_prefix
16 #define dout_prefix *_dout << "rbd::mirror::image_sync::SyncPointPruneRequest: " \
17                            << this << " " << __func__
18 namespace rbd {
19 namespace mirror {
20 namespace image_sync {
21
22 using librbd::util::create_context_callback;
23
24 template <typename I>
25 SyncPointPruneRequest<I>::SyncPointPruneRequest(I *remote_image_ctx,
26                                                 bool sync_complete,
27                                                 Journaler *journaler,
28                                                 MirrorPeerClientMeta *client_meta,
29                                                 Context *on_finish)
30   : m_remote_image_ctx(remote_image_ctx), m_sync_complete(sync_complete),
31     m_journaler(journaler), m_client_meta(client_meta), m_on_finish(on_finish),
32     m_client_meta_copy(*client_meta) {
33 }
34
35 template <typename I>
36 void SyncPointPruneRequest<I>::send() {
37   if (m_client_meta->sync_points.empty()) {
38     send_remove_snap();
39     return;
40   }
41
42   if (m_sync_complete) {
43     // if sync is complete, we can remove the master sync point
44     auto it = m_client_meta_copy.sync_points.begin();
45     MirrorPeerSyncPoint &sync_point = *it;
46
47     ++it;
48     if (it == m_client_meta_copy.sync_points.end() ||
49         it->from_snap_name != sync_point.snap_name) {
50       m_snap_names.push_back(sync_point.snap_name);
51     }
52
53     if (!sync_point.from_snap_name.empty()) {
54       m_snap_names.push_back(sync_point.from_snap_name);
55     }
56   } else {
57     // if we have more than one sync point or invalid sync points,
58     // trim them off
59     RWLock::RLocker snap_locker(m_remote_image_ctx->snap_lock);
60     std::set<std::string> snap_names;
61     for (auto it = m_client_meta_copy.sync_points.rbegin();
62          it != m_client_meta_copy.sync_points.rend(); ++it) {
63       MirrorPeerSyncPoint &sync_point = *it;
64       if (&sync_point == &m_client_meta_copy.sync_points.front()) {
65         if (m_remote_image_ctx->get_snap_id(
66               cls::rbd::UserSnapshotNamespace(), sync_point.snap_name) ==
67               CEPH_NOSNAP) {
68           derr << ": failed to locate sync point snapshot: "
69                << sync_point.snap_name << dendl;
70         } else if (!sync_point.from_snap_name.empty()) {
71           derr << ": unexpected from_snap_name in primary sync point: "
72                << sync_point.from_snap_name << dendl;
73         } else {
74           // first sync point is OK -- keep it
75           break;
76         }
77         m_invalid_master_sync_point = true;
78       }
79
80       if (snap_names.count(sync_point.snap_name) == 0) {
81         snap_names.insert(sync_point.snap_name);
82         m_snap_names.push_back(sync_point.snap_name);
83       }
84
85       MirrorPeerSyncPoint &front_sync_point =
86         m_client_meta_copy.sync_points.front();
87       if (!sync_point.from_snap_name.empty() &&
88           snap_names.count(sync_point.from_snap_name) == 0 &&
89           sync_point.from_snap_name != front_sync_point.snap_name) {
90         snap_names.insert(sync_point.from_snap_name);
91         m_snap_names.push_back(sync_point.from_snap_name);
92       }
93     }
94   }
95
96   send_remove_snap();
97 }
98
99 template <typename I>
100 void SyncPointPruneRequest<I>::send_remove_snap() {
101   if (m_snap_names.empty()) {
102     send_refresh_image();
103     return;
104   }
105
106   const std::string &snap_name = m_snap_names.front();
107
108   dout(20) << ": snap_name=" << snap_name << dendl;
109
110   Context *ctx = create_context_callback<
111     SyncPointPruneRequest<I>, &SyncPointPruneRequest<I>::handle_remove_snap>(
112       this);
113   m_remote_image_ctx->operations->snap_remove(cls::rbd::UserSnapshotNamespace(),
114                                               snap_name.c_str(),
115                                               ctx);
116 }
117
118 template <typename I>
119 void SyncPointPruneRequest<I>::handle_remove_snap(int r) {
120   dout(20) << ": r=" << r << dendl;
121
122   assert(!m_snap_names.empty());
123   std::string snap_name = m_snap_names.front();
124   m_snap_names.pop_front();
125
126   if (r == -ENOENT) {
127     r = 0;
128   }
129   if (r < 0) {
130     derr << ": failed to remove snapshot '" << snap_name << "': "
131          << cpp_strerror(r) << dendl;
132     finish(r);
133     return;
134   }
135
136   send_remove_snap();
137 }
138
139 template <typename I>
140 void SyncPointPruneRequest<I>::send_refresh_image() {
141   dout(20) << dendl;
142
143   Context *ctx = create_context_callback<
144     SyncPointPruneRequest<I>, &SyncPointPruneRequest<I>::handle_refresh_image>(
145       this);
146   m_remote_image_ctx->state->refresh(ctx);
147 }
148
149 template <typename I>
150 void SyncPointPruneRequest<I>::handle_refresh_image(int r) {
151   dout(20) << ": r=" << r << dendl;
152
153   if (r < 0) {
154     derr << ": remote image refresh failed: " << cpp_strerror(r) << dendl;
155     finish(r);
156     return;
157   }
158
159   send_update_client();
160 }
161
162 template <typename I>
163 void SyncPointPruneRequest<I>::send_update_client() {
164   dout(20) << dendl;
165
166   if (m_sync_complete) {
167     m_client_meta_copy.sync_points.pop_front();
168     if (m_client_meta_copy.sync_points.empty()) {
169       m_client_meta_copy.state = librbd::journal::MIRROR_PEER_STATE_REPLAYING;
170     }
171   } else {
172     while (m_client_meta_copy.sync_points.size() > 1) {
173       m_client_meta_copy.sync_points.pop_back();
174     }
175     if (m_invalid_master_sync_point) {
176       // all subsequent sync points would have been pruned
177       m_client_meta_copy.sync_points.clear();
178     }
179   }
180
181   bufferlist client_data_bl;
182   librbd::journal::ClientData client_data(m_client_meta_copy);
183   ::encode(client_data, client_data_bl);
184
185   Context *ctx = create_context_callback<
186     SyncPointPruneRequest<I>, &SyncPointPruneRequest<I>::handle_update_client>(
187       this);
188   m_journaler->update_client(client_data_bl, ctx);
189 }
190
191 template <typename I>
192 void SyncPointPruneRequest<I>::handle_update_client(int r) {
193   dout(20) << ": r=" << r << dendl;
194
195   if (r < 0) {
196     derr << ": failed to update client data: " << cpp_strerror(r)
197          << dendl;
198     finish(r);
199     return;
200   }
201
202   // update provided meta structure to reflect reality
203   *m_client_meta = m_client_meta_copy;
204   finish(0);
205 }
206
207 template <typename I>
208 void SyncPointPruneRequest<I>::finish(int r) {
209   dout(20) << ": r=" << r << dendl;
210
211   m_on_finish->complete(r);
212   delete this;
213 }
214
215 } // namespace image_sync
216 } // namespace mirror
217 } // namespace rbd
218
219 template class rbd::mirror::image_sync::SyncPointPruneRequest<librbd::ImageCtx>;