]>
git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/Watcher.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_LIBRBD_WATCHER_H
5 #define CEPH_LIBRBD_WATCHER_H
7 #include "common/Mutex.h"
8 #include "common/RWLock.h"
9 #include "include/rados/librados.hpp"
10 #include "librbd/watcher/Notifier.h"
11 #include "librbd/watcher/Types.h"
19 namespace watcher
{ struct NotifyResponse
; }
23 struct C_NotifyAck
: public Context
{
30 C_NotifyAck(Watcher
*watcher
, uint64_t notify_id
, uint64_t handle
);
31 void finish(int r
) override
;
34 Watcher(librados::IoCtx
& ioctx
, ContextWQ
*work_queue
,
35 const std::string
& oid
);
38 void register_watch(Context
*on_finish
);
39 void unregister_watch(Context
*on_finish
);
40 void flush(Context
*on_finish
);
42 std::string
get_oid() const;
43 void set_oid(const string
& oid
);
45 uint64_t get_watch_handle() const {
46 RWLock::RLocker
watch_locker(m_watch_lock
);
47 return m_watch_handle
;
50 bool is_registered() const {
51 RWLock::RLocker
locker(m_watch_lock
);
52 return m_watch_state
== WATCH_STATE_REGISTERED
;
54 bool is_unregistered() const {
55 RWLock::RLocker
locker(m_watch_lock
);
56 return m_watch_state
== WATCH_STATE_UNREGISTERED
;
61 WATCH_STATE_UNREGISTERED
,
62 WATCH_STATE_REGISTERING
,
63 WATCH_STATE_REGISTERED
,
65 WATCH_STATE_REWATCHING
68 librados::IoCtx
& m_ioctx
;
69 ContextWQ
*m_work_queue
;
72 mutable RWLock m_watch_lock
;
73 uint64_t m_watch_handle
;
74 watcher::Notifier m_notifier
;
75 WatchState m_watch_state
;
77 void send_notify(bufferlist
&payload
,
78 watcher::NotifyResponse
*response
= nullptr,
79 Context
*on_finish
= nullptr);
81 virtual void handle_notify(uint64_t notify_id
, uint64_t handle
,
82 uint64_t notifier_id
, bufferlist
&bl
) = 0;
84 virtual void handle_error(uint64_t cookie
, int err
);
86 void acknowledge_notify(uint64_t notify_id
, uint64_t handle
,
89 virtual void handle_rewatch_complete(int r
) { }
105 * REGISTERED * * * * * * * > ERROR
112 * | \---------------------/
114 * | (unregister_watch)
125 struct WatchCtx
: public librados::WatchCtx2
{
128 WatchCtx(Watcher
&parent
) : watcher(parent
) {}
130 void handle_notify(uint64_t notify_id
,
132 uint64_t notifier_id
,
133 bufferlist
& bl
) override
;
134 void handle_error(uint64_t handle
, int err
) override
;
137 struct C_RegisterWatch
: public Context
{
141 C_RegisterWatch(Watcher
*watcher
, Context
*on_finish
)
142 : watcher(watcher
), on_finish(on_finish
) {
144 void finish(int r
) override
{
145 watcher
->handle_register_watch(r
, on_finish
);
149 WatchCtx m_watch_ctx
;
150 Context
*m_unregister_watch_ctx
= nullptr;
152 void handle_register_watch(int r
, Context
*on_finish
);
155 void handle_rewatch(int r
);
159 } // namespace librbd
161 #endif // CEPH_LIBRBD_WATCHER_H