Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / librbd / watcher / RewatchRequest.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 "librbd/watcher/RewatchRequest.h"
5 #include "common/RWLock.h"
6 #include "common/errno.h"
7 #include "librbd/Utils.h"
8
9 #define dout_subsys ceph_subsys_rbd
10 #undef dout_prefix
11 #define dout_prefix *_dout << "librbd::watcher::RewatchRequest: " \
12                            << this << " " << __func__ << " "
13
14 namespace librbd {
15
16 using util::create_context_callback;
17 using util::create_rados_callback;
18
19 namespace watcher {
20
21 using std::string;
22
23 RewatchRequest::RewatchRequest(librados::IoCtx& ioctx, const string& oid,
24                                RWLock &watch_lock,
25                                librados::WatchCtx2 *watch_ctx,
26                                uint64_t *watch_handle, Context *on_finish)
27   : m_ioctx(ioctx), m_oid(oid), m_watch_lock(watch_lock),
28     m_watch_ctx(watch_ctx), m_watch_handle(watch_handle),
29     m_on_finish(on_finish) {
30 }
31
32 void RewatchRequest::send() {
33   unwatch();
34 }
35
36 void RewatchRequest::unwatch() {
37   assert(m_watch_lock.is_wlocked());
38   assert(*m_watch_handle != 0);
39
40   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
41   ldout(cct, 10) << dendl;
42
43   librados::AioCompletion *aio_comp = create_rados_callback<
44                         RewatchRequest, &RewatchRequest::handle_unwatch>(this);
45   int r = m_ioctx.aio_unwatch(*m_watch_handle, aio_comp);
46   assert(r == 0);
47   aio_comp->release();
48
49   *m_watch_handle = 0;
50 }
51
52 void RewatchRequest::handle_unwatch(int r) {
53   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
54   ldout(cct, 10) << "r=" << r << dendl;
55
56   if (r == -EBLACKLISTED) {
57     lderr(cct) << "client blacklisted" << dendl;
58     finish(r);
59     return;
60   } else if (r < 0) {
61     lderr(cct) << "failed to unwatch: " << cpp_strerror(r) << dendl;
62   }
63   rewatch();
64 }
65
66 void RewatchRequest::rewatch() {
67   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
68   ldout(cct, 10) << dendl;
69
70   librados::AioCompletion *aio_comp = create_rados_callback<
71                         RewatchRequest, &RewatchRequest::handle_rewatch>(this);
72   int r = m_ioctx.aio_watch(m_oid, aio_comp, &m_rewatch_handle, m_watch_ctx);
73   assert(r == 0);
74   aio_comp->release();
75 }
76
77 void RewatchRequest::handle_rewatch(int r) {
78   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
79   ldout(cct, 10) << "r=" << r << dendl;
80
81   if (r == -EBLACKLISTED) {
82     lderr(cct) << "client blacklisted" << dendl;
83     finish(r);
84     return;
85   } else if (r == -ENOENT) {
86     ldout(cct, 5) << "object deleted" << dendl;
87     finish(r);
88     return;
89   } else if (r < 0) {
90     lderr(cct) << "failed to watch object: " << cpp_strerror(r)
91                << dendl;
92     rewatch();
93     return;
94   }
95
96   {
97     RWLock::WLocker watch_locker(m_watch_lock);
98     *m_watch_handle = m_rewatch_handle;
99   }
100
101   finish(0);
102 }
103
104 void RewatchRequest::finish(int r) {
105   CephContext *cct = reinterpret_cast<CephContext *>(m_ioctx.cct());
106   ldout(cct, 10) << "r=" << r << dendl;
107
108   m_on_finish->complete(r);
109   delete this;
110 }
111
112 } // namespace watcher
113 } // namespace librbd
114