summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_deleter/TrashWatcher.cc
blob: 552d77e0e9f7176fe6623ab31917074eea54bc20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd_mirror/image_deleter/TrashWatcher.h"
#include "include/rbd_types.h"
#include "cls/rbd/cls_rbd_client.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/Timer.h"
#include "librbd/ImageCtx.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "tools/rbd_mirror/Threads.h"
#include "tools/rbd_mirror/image_deleter/Types.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_deleter::TrashWatcher: " \
                           << this << " " << __func__ << ": "

using librbd::util::create_context_callback;
using librbd::util::create_rados_callback;

namespace rbd {
namespace mirror {
namespace image_deleter {

namespace {

const size_t MAX_RETURN = 1024;

} // anonymous namespace

template <typename I>
TrashWatcher<I>::TrashWatcher(librados::IoCtx &io_ctx, Threads<I> *threads,
                              TrashListener& trash_listener)
  : librbd::TrashWatcher<I>(io_ctx, threads->work_queue),
    m_io_ctx(io_ctx), m_threads(threads), m_trash_listener(trash_listener),
    m_lock(ceph::make_mutex(librbd::util::unique_lock_name(
      "rbd::mirror::image_deleter::TrashWatcher", this))) {
}

template <typename I>
void TrashWatcher<I>::init(Context *on_finish) {
  dout(5) << dendl;

  {
    std::lock_guard locker{m_lock};
    m_on_init_finish = on_finish;

    ceph_assert(!m_trash_list_in_progress);
    m_trash_list_in_progress = true;
  }

  create_trash();
}

template <typename I>
void TrashWatcher<I>::shut_down(Context *on_finish) {
  dout(5) << dendl;

  {
    std::scoped_lock locker{m_threads->timer_lock, m_lock};

    ceph_assert(!m_shutting_down);
    m_shutting_down = true;
    if (m_timer_ctx != nullptr) {
      m_threads->timer->cancel_event(m_timer_ctx);
      m_timer_ctx = nullptr;
    }
  }

  auto ctx = new LambdaContext([this, on_finish](int r) {
      unregister_watcher(on_finish);
    });
  m_async_op_tracker.wait_for_ops(ctx);
}

template <typename I>
void TrashWatcher<I>::handle_image_added(const std::string &image_id,
                                         const cls::rbd::TrashImageSpec& spec) {
  dout(10) << "image_id=" << image_id << dendl;

  std::lock_guard locker{m_lock};
  add_image(image_id, spec);
}

template <typename I>
void TrashWatcher<I>::handle_image_removed(const std::string &image_id) {
  // ignore removals -- the image deleter will ignore -ENOENTs
}

template <typename I>
void TrashWatcher<I>::handle_rewatch_complete(int r) {
  dout(5) << "r=" << r << dendl;

  if (r == -EBLOCKLISTED) {
    dout(0) << "detected client is blocklisted" << dendl;
    return;
  } else if (r == -ENOENT) {
    dout(5) << "trash directory deleted" << dendl;
  } else if (r < 0) {
    derr << "unexpected error re-registering trash directory watch: "
         << cpp_strerror(r) << dendl;
  }
  schedule_trash_list(30);
}

template <typename I>
void TrashWatcher<I>::create_trash() {
  dout(20) << dendl;
  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
  }

  librados::ObjectWriteOperation op;
  op.create(false);

  m_async_op_tracker.start_op();
  auto aio_comp = create_rados_callback<
    TrashWatcher<I>, &TrashWatcher<I>::handle_create_trash>(this);
  int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void TrashWatcher<I>::handle_create_trash(int r) {
  dout(20) << "r=" << r << dendl;
  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
  }

  Context* on_init_finish = nullptr;
  if (r == -EBLOCKLISTED || r == -ENOENT) {
    if (r == -EBLOCKLISTED) {
      dout(0) << "detected client is blocklisted" << dendl;
    } else {
      dout(0) << "detected pool no longer exists" << dendl;
    }

    std::lock_guard locker{m_lock};
    std::swap(on_init_finish, m_on_init_finish);
    m_trash_list_in_progress = false;
  } else if (r < 0 && r != -EEXIST) {
    derr << "failed to create trash object: " << cpp_strerror(r) << dendl;
    {
      std::lock_guard locker{m_lock};
      m_trash_list_in_progress = false;
    }

    schedule_trash_list(30);
  } else {
    register_watcher();
  }

  m_async_op_tracker.finish_op();
  if (on_init_finish != nullptr) {
    on_init_finish->complete(r);
  }
}

template <typename I>
void TrashWatcher<I>::register_watcher() {
  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
  }

  // if the watch registration is in-flight, let the watcher
  // handle the transition -- only (re-)register if it's not registered
  if (!this->is_unregistered()) {
    trash_list(true);
    return;
  }

  // first time registering or the watch failed
  dout(5) << dendl;
  m_async_op_tracker.start_op();

  Context *ctx = create_context_callback<
    TrashWatcher, &TrashWatcher<I>::handle_register_watcher>(this);
  this->register_watch(ctx);
}

template <typename I>
void TrashWatcher<I>::handle_register_watcher(int r) {
  dout(5) << "r=" << r << dendl;

  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
    if (r < 0) {
      m_trash_list_in_progress = false;
    }
  }

  Context *on_init_finish = nullptr;
  if (r >= 0) {
    trash_list(true);
  } else if (r == -EBLOCKLISTED) {
    dout(0) << "detected client is blocklisted" << dendl;

    std::lock_guard locker{m_lock};
    std::swap(on_init_finish, m_on_init_finish);
  } else {
    derr << "unexpected error registering trash directory watch: "
         << cpp_strerror(r) << dendl;
    schedule_trash_list(10);
  }

  m_async_op_tracker.finish_op();
  if (on_init_finish != nullptr) {
    on_init_finish->complete(r);
  }
}

template <typename I>
void TrashWatcher<I>::unregister_watcher(Context* on_finish) {
  dout(5) << dendl;

  m_async_op_tracker.start_op();
  Context *ctx = new LambdaContext([this, on_finish](int r) {
      handle_unregister_watcher(r, on_finish);
    });
  this->unregister_watch(ctx);
}

template <typename I>
void TrashWatcher<I>::handle_unregister_watcher(int r, Context* on_finish) {
  dout(5) << "unregister_watcher: r=" << r << dendl;
  if (r < 0) {
    derr << "error unregistering watcher for trash directory: "
         << cpp_strerror(r) << dendl;
  }
  m_async_op_tracker.finish_op();
  on_finish->complete(0);
}

template <typename I>
void TrashWatcher<I>::trash_list(bool initial_request) {
  if (initial_request) {
    m_async_op_tracker.start_op();
    m_last_image_id = "";
  }

  dout(5) << "last_image_id=" << m_last_image_id << dendl;

  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
  }

  librados::ObjectReadOperation op;
  librbd::cls_client::trash_list_start(&op, m_last_image_id, MAX_RETURN);

  librados::AioCompletion *aio_comp = create_rados_callback<
    TrashWatcher<I>, &TrashWatcher<I>::handle_trash_list>(this);
  m_out_bl.clear();
  int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template <typename I>
void TrashWatcher<I>::handle_trash_list(int r) {
  dout(5) << "r=" << r << dendl;

  std::map<std::string, cls::rbd::TrashImageSpec> images;
  if (r >= 0) {
    auto bl_it = m_out_bl.cbegin();
    r = librbd::cls_client::trash_list_finish(&bl_it, &images);
  }

  Context *on_init_finish = nullptr;
  {
    std::lock_guard locker{m_lock};
    ceph_assert(m_trash_list_in_progress);
    if (r >= 0) {
      for (auto& image : images) {
        add_image(image.first, image.second);
      }
    } else if (r == -ENOENT) {
      r = 0;
    }

    if (r == -EBLOCKLISTED) {
      dout(0) << "detected client is blocklisted during trash refresh" << dendl;
      m_trash_list_in_progress = false;
      std::swap(on_init_finish, m_on_init_finish);
    } else if (r >= 0 && images.size() < MAX_RETURN) {
      m_trash_list_in_progress = false;
      std::swap(on_init_finish, m_on_init_finish);
    } else if (r < 0) {
      m_trash_list_in_progress = false;
    }
  }

  if (r >= 0 && images.size() == MAX_RETURN) {
    m_last_image_id = images.rbegin()->first;
    trash_list(false);
    return;
  } else if (r < 0 && r != -EBLOCKLISTED) {
    derr << "failed to retrieve trash directory: " << cpp_strerror(r) << dendl;
    schedule_trash_list(10);
  }

  m_async_op_tracker.finish_op();
  if (on_init_finish != nullptr) {
    on_init_finish->complete(r);
  }
}

template <typename I>
void TrashWatcher<I>::schedule_trash_list(double interval) {
  std::scoped_lock locker{m_threads->timer_lock, m_lock};
  if (m_shutting_down || m_trash_list_in_progress || m_timer_ctx != nullptr) {
    if (m_trash_list_in_progress && !m_deferred_trash_list) {
      dout(5) << "deferring refresh until in-flight refresh completes" << dendl;
      m_deferred_trash_list = true;
    }
    return;
  }

  dout(5) << dendl;
  m_timer_ctx = m_threads->timer->add_event_after(
    interval,
    new LambdaContext([this](int r) {
	process_trash_list();
      }));
}

template <typename I>
void TrashWatcher<I>::process_trash_list() {
  dout(5) << dendl;

  ceph_assert(ceph_mutex_is_locked(m_threads->timer_lock));
  ceph_assert(m_timer_ctx != nullptr);
  m_timer_ctx = nullptr;

  {
    std::lock_guard locker{m_lock};
    ceph_assert(!m_trash_list_in_progress);
    m_trash_list_in_progress = true;
  }

  // execute outside of the timer's lock
  m_async_op_tracker.start_op();
  Context *ctx = new LambdaContext([this](int r) {
      create_trash();
      m_async_op_tracker.finish_op();
    });
  m_threads->work_queue->queue(ctx, 0);
}

template <typename I>
void TrashWatcher<I>::add_image(const std::string& image_id,
                                const cls::rbd::TrashImageSpec& spec) {
  if (spec.source != cls::rbd::TRASH_IMAGE_SOURCE_MIRRORING) {
    return;
  }

  ceph_assert(ceph_mutex_is_locked(m_lock));
  auto& deferment_end_time = spec.deferment_end_time;
  dout(10) << "image_id=" << image_id << ", "
           << "deferment_end_time=" << deferment_end_time << dendl;

  m_async_op_tracker.start_op();
  auto ctx = new LambdaContext([this, image_id, deferment_end_time](int r) {
      m_trash_listener.handle_trash_image(image_id,
					  deferment_end_time.to_real_time());
      m_async_op_tracker.finish_op();
    });
  m_threads->work_queue->queue(ctx, 0);
}

} // namespace image_deleter;
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_deleter::TrashWatcher<librbd::ImageCtx>;