1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "ClusterWatcher.h"
5 #include "common/debug.h"
6 #include "common/errno.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/internal.h"
9 #include "librbd/api/Mirror.h"
11 #define dout_context g_ceph_context
12 #define dout_subsys ceph_subsys_rbd_mirror
14 #define dout_prefix *_dout << "rbd::mirror::ClusterWatcher:" << this << " " \
21 using std::unique_ptr
;
24 using librados::Rados
;
25 using librados::IoCtx
;
30 ClusterWatcher::ClusterWatcher(RadosRef cluster
, Mutex
&lock
) :
36 const ClusterWatcher::PoolPeers
& ClusterWatcher::get_pool_peers() const
38 assert(m_lock
.is_locked());
42 void ClusterWatcher::refresh_pools()
44 dout(20) << "enter" << dendl
;
48 read_pool_peers(&pool_peers
, &pool_names
);
50 Mutex::Locker
l(m_lock
);
51 m_pool_peers
= pool_peers
;
52 // TODO: perhaps use a workqueue instead, once we get notifications
53 // about config changes for existing pools
56 void ClusterWatcher::read_pool_peers(PoolPeers
*pool_peers
,
57 PoolNames
*pool_names
)
59 list
<pair
<int64_t, string
> > pools
;
60 int r
= m_cluster
->pool_list2(pools
);
62 derr
<< "error listing pools: " << cpp_strerror(r
) << dendl
;
66 for (auto kv
: pools
) {
67 int64_t pool_id
= kv
.first
;
68 string pool_name
= kv
.second
;
70 r
= m_cluster
->pool_get_base_tier(pool_id
, &base_tier
);
72 dout(10) << "pool " << pool_name
<< " no longer exists" << dendl
;
75 derr
<< "Error retrieving base tier for pool " << pool_name
<< dendl
;
78 if (pool_id
!= base_tier
) {
79 // pool is a cache; skip it
84 r
= m_cluster
->ioctx_create2(pool_id
, ioctx
);
86 dout(10) << "pool " << pool_id
<< " no longer exists" << dendl
;
89 derr
<< "Error accessing pool " << pool_name
<< cpp_strerror(r
) << dendl
;
93 cls::rbd::MirrorMode mirror_mode_internal
;
94 r
= librbd::cls_client::mirror_mode_get(&ioctx
, &mirror_mode_internal
);
96 dout(10) << "access denied querying pool " << pool_name
<< dendl
;
99 derr
<< "could not tell whether mirroring was enabled for " << pool_name
100 << " : " << cpp_strerror(r
) << dendl
;
103 if (mirror_mode_internal
== cls::rbd::MIRROR_MODE_DISABLED
) {
104 dout(10) << "mirroring is disabled for pool " << pool_name
<< dendl
;
108 vector
<librbd::mirror_peer_t
> configs
;
109 r
= librbd::api::Mirror
<>::peer_list(ioctx
, &configs
);
111 derr
<< "error reading mirroring config for pool " << pool_name
112 << cpp_strerror(r
) << dendl
;
116 pool_peers
->insert({pool_id
, Peers
{configs
.begin(), configs
.end()}});
117 pool_names
->insert(pool_name
);
121 } // namespace mirror