summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/MirrorStatusUpdater.cc
blob: 257cb1df27cf7a9e559d5f1192aa5f6d6de9cb2b (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
385
386
387
388
389
390
391
392
393
394
395
396
397
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "tools/rbd_mirror/MirrorStatusUpdater.h"
#include "include/Context.h"
#include "include/stringify.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/MirrorStatusWatcher.h"
#include "tools/rbd_mirror/Threads.h"

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

namespace rbd {
namespace mirror {

static const double UPDATE_INTERVAL_SECONDS = 30;
static const uint32_t MAX_UPDATES_PER_OP = 100;

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

template <typename I>
MirrorStatusUpdater<I>::MirrorStatusUpdater(
    librados::IoCtx& io_ctx, Threads<I> *threads,
    const std::string& local_mirror_uuid)
  : m_io_ctx(io_ctx), m_threads(threads),
    m_local_mirror_uuid(local_mirror_uuid),
    m_lock(ceph::make_mutex("rbd::mirror::MirrorStatusUpdater " +
                              stringify(m_io_ctx.get_id()))) {
  dout(10) << "local_mirror_uuid=" << local_mirror_uuid << ", "
           << "pool_id=" << m_io_ctx.get_id() << dendl;
}

template <typename I>
MirrorStatusUpdater<I>::~MirrorStatusUpdater() {
  ceph_assert(!m_initialized);
  delete m_mirror_status_watcher;
}

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

  ceph_assert(!m_initialized);
  m_initialized = true;

  {
    std::lock_guard timer_locker{m_threads->timer_lock};
    schedule_timer_task();
  }

  init_mirror_status_watcher(on_finish);
}

template <typename I>
void MirrorStatusUpdater<I>::init_mirror_status_watcher(Context* on_finish) {
  dout(10) << dendl;

  auto ctx = new LambdaContext([this, on_finish](int r) {
      handle_init_mirror_status_watcher(r, on_finish);
    });
  m_mirror_status_watcher = MirrorStatusWatcher<I>::create(
    m_io_ctx, m_threads->work_queue);
  m_mirror_status_watcher->init(ctx);
}

template <typename I>
void MirrorStatusUpdater<I>::handle_init_mirror_status_watcher(
    int r, Context* on_finish) {
  dout(10) << "r=" << r << dendl;

  if (r < 0) {
    derr << "failed to init mirror status watcher: " << cpp_strerror(r)
         << dendl;

    delete m_mirror_status_watcher;
    m_mirror_status_watcher = nullptr;

    on_finish = new LambdaContext([r, on_finish](int) {
        on_finish->complete(r);
      });
    shut_down(on_finish);
    return;
  }

  m_threads->work_queue->queue(on_finish, 0);
}

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

  {
    std::lock_guard timer_locker{m_threads->timer_lock};
    ceph_assert(m_timer_task != nullptr);
    m_threads->timer->cancel_event(m_timer_task);
  }

  {
    std::unique_lock locker(m_lock);
    ceph_assert(m_initialized);
    m_initialized = false;
  }

  shut_down_mirror_status_watcher(on_finish);
}

template <typename I>
void MirrorStatusUpdater<I>::shut_down_mirror_status_watcher(
    Context* on_finish) {
  if (m_mirror_status_watcher == nullptr) {
    finalize_shutdown(0, on_finish);
    return;
  }

  dout(10) << dendl;

  auto ctx = new LambdaContext([this, on_finish](int r) {
      handle_shut_down_mirror_status_watcher(r, on_finish);
    });
  m_mirror_status_watcher->shut_down(ctx);
}

template <typename I>
void MirrorStatusUpdater<I>::handle_shut_down_mirror_status_watcher(
    int r, Context* on_finish) {
  dout(10) << "r=" << r << dendl;

  if (r < 0) {
    derr << "failed to shut down mirror status watcher: " << cpp_strerror(r)
         << dendl;
  }

  finalize_shutdown(r, on_finish);
}

template <typename I>
void MirrorStatusUpdater<I>::finalize_shutdown(int r, Context* on_finish) {
  dout(10) << dendl;

  {
    std::unique_lock locker(m_lock);
    if (m_update_in_progress) {
      if (r < 0) {
        on_finish = new LambdaContext([r, on_finish](int) {
            on_finish->complete(r);
          });
      }

      m_update_on_finish_ctxs.push_back(on_finish);
      return;
    }
  }

  m_threads->work_queue->queue(on_finish, r);
}

template <typename I>
bool MirrorStatusUpdater<I>::exists(const std::string& global_image_id) {
  dout(15) << "global_image_id=" << global_image_id << dendl;

  std::unique_lock locker(m_lock);
  return (m_global_image_status.count(global_image_id) > 0);
}

template <typename I>
void MirrorStatusUpdater<I>::set_mirror_image_status(
    const std::string& global_image_id,
    const cls::rbd::MirrorImageSiteStatus& mirror_image_site_status,
    bool immediate_update) {
  dout(15) << "global_image_id=" << global_image_id << ", "
           << "mirror_image_site_status=" << mirror_image_site_status << dendl;

  std::unique_lock locker(m_lock);

  m_global_image_status[global_image_id] = mirror_image_site_status;
  if (immediate_update) {
    m_update_global_image_ids.insert(global_image_id);
    queue_update_task(std::move(locker));
  }
}

template <typename I>
void MirrorStatusUpdater<I>::remove_refresh_mirror_image_status(
    const std::string& global_image_id,
    Context* on_finish) {
  if (try_remove_mirror_image_status(global_image_id, false, false,
                                     on_finish)) {
    m_threads->work_queue->queue(on_finish, 0);
  }
}

template <typename I>
void MirrorStatusUpdater<I>::remove_mirror_image_status(
    const std::string& global_image_id, bool immediate_update,
    Context* on_finish) {
  if (try_remove_mirror_image_status(global_image_id, true, immediate_update,
                                     on_finish)) {
    m_threads->work_queue->queue(on_finish, 0);
  }
}

template <typename I>
bool MirrorStatusUpdater<I>::try_remove_mirror_image_status(
    const std::string& global_image_id, bool queue_update,
    bool immediate_update, Context* on_finish) {
  dout(15) << "global_image_id=" << global_image_id << ", "
           << "queue_update=" << queue_update << ", "
           << "immediate_update=" << immediate_update << dendl;

  std::unique_lock locker(m_lock);
  if ((m_update_in_flight &&
       m_updating_global_image_ids.count(global_image_id) > 0) ||
      ((m_update_in_progress || m_update_requested) &&
       m_update_global_image_ids.count(global_image_id) > 0)) {
    // if update is scheduled/in-progress, wait for it to complete
    on_finish = new LambdaContext(
      [this, global_image_id, queue_update, immediate_update,
             on_finish](int r) {
        if (try_remove_mirror_image_status(global_image_id, queue_update,
                                           immediate_update, on_finish)) {
          on_finish->complete(0);
        }
      });
    m_update_on_finish_ctxs.push_back(on_finish);
    return false;
  }

  m_global_image_status.erase(global_image_id);
  if (queue_update) {
    m_update_global_image_ids.insert(global_image_id);
    if (immediate_update) {
      queue_update_task(std::move(locker));
    }
  }

  return true;
}

template <typename I>
void MirrorStatusUpdater<I>::schedule_timer_task() {
  dout(10) << dendl;

  ceph_assert(ceph_mutex_is_locked(m_threads->timer_lock));
  ceph_assert(m_timer_task == nullptr);
  m_timer_task = create_context_callback<
    MirrorStatusUpdater<I>,
    &MirrorStatusUpdater<I>::handle_timer_task>(this);
  m_threads->timer->add_event_after(UPDATE_INTERVAL_SECONDS, m_timer_task);
}

template <typename I>
void MirrorStatusUpdater<I>::handle_timer_task(int r) {
  dout(10) << dendl;

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

  std::unique_lock locker(m_lock);
  for (auto& pair : m_global_image_status) {
    m_update_global_image_ids.insert(pair.first);
  }

  queue_update_task(std::move(locker));
}

template <typename I>
void MirrorStatusUpdater<I>::queue_update_task(
  std::unique_lock<ceph::mutex>&& locker) {
  if (!m_initialized) {
    return;
  }

  if (m_update_in_progress) {
    if (m_update_in_flight) {
      dout(10) << "deferring update due to in-flight ops" << dendl;
      m_update_requested = true;
    }
    return;
  }

  m_update_in_progress = true;
  ceph_assert(!m_update_in_flight);
  ceph_assert(!m_update_requested);
  locker.unlock();

  dout(10) << dendl;
  auto ctx = create_context_callback<
    MirrorStatusUpdater<I>,
    &MirrorStatusUpdater<I>::update_task>(this);
  m_threads->work_queue->queue(ctx);
}

template <typename I>
void MirrorStatusUpdater<I>::update_task(int r) {
  dout(10) << dendl;

  std::unique_lock locker(m_lock);
  ceph_assert(m_update_in_progress);
  ceph_assert(!m_update_in_flight);
  m_update_in_flight = true;

  std::swap(m_updating_global_image_ids, m_update_global_image_ids);
  auto updating_global_image_ids = m_updating_global_image_ids;
  auto global_image_status = m_global_image_status;
  locker.unlock();

  Context* ctx = create_context_callback<
    MirrorStatusUpdater<I>,
    &MirrorStatusUpdater<I>::handle_update_task>(this);
  if (updating_global_image_ids.empty()) {
    ctx->complete(0);
    return;
  }

  auto gather = new C_Gather(g_ceph_context, ctx);

  auto it = updating_global_image_ids.begin();
  while (it != updating_global_image_ids.end()) {
    librados::ObjectWriteOperation op;
    uint32_t op_count = 0;

    while (it != updating_global_image_ids.end() &&
           op_count < MAX_UPDATES_PER_OP) {
      auto& global_image_id = *it;
      ++it;

      auto status_it = global_image_status.find(global_image_id);
      if (status_it == global_image_status.end()) {
        librbd::cls_client::mirror_image_status_remove(&op, global_image_id);
        ++op_count;
        continue;
      }

      status_it->second.mirror_uuid = m_local_mirror_uuid;
      librbd::cls_client::mirror_image_status_set(&op, global_image_id,
                                                  status_it->second);
      ++op_count;
    }

    auto aio_comp = create_rados_callback(gather->new_sub());
    int r = m_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op);
    ceph_assert(r == 0);
    aio_comp->release();
  }

  gather->activate();
}

template <typename I>
void MirrorStatusUpdater<I>::handle_update_task(int r) {
  dout(10) << dendl;
  if (r < 0) {
    derr << "failed to update mirror image statuses: " << cpp_strerror(r)
         << dendl;
  }

  std::unique_lock locker(m_lock);

  Contexts on_finish_ctxs;
  std::swap(on_finish_ctxs, m_update_on_finish_ctxs);

  ceph_assert(m_update_in_progress);
  m_update_in_progress = false;

  ceph_assert(m_update_in_flight);
  m_update_in_flight = false;

  m_updating_global_image_ids.clear();

  if (m_update_requested) {
    m_update_requested = false;
    queue_update_task(std::move(locker));
  } else {
    locker.unlock();
  }

  for (auto on_finish : on_finish_ctxs) {
    on_finish->complete(0);
  }
}

} // namespace mirror
} // namespace rbd

template class rbd::mirror::MirrorStatusUpdater<librbd::ImageCtx>;