|
| 1 | +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
| 2 | +// vim: ts=8 sw=2 smarttab ft=cpp |
| 3 | + |
| 4 | +#include "realm_watcher.h" |
| 5 | + |
| 6 | +#include "common/errno.h" |
| 7 | +#include "include/ceph_assert.h" |
| 8 | + |
| 9 | +#include "rgw_tools.h" |
| 10 | +#include "rgw_zone.h" |
| 11 | + |
| 12 | +#include "impl.h" |
| 13 | +#include "store.h" |
| 14 | + |
| 15 | +#define dout_subsys ceph_subsys_rgw |
| 16 | + |
| 17 | +#undef dout_prefix |
| 18 | +#define dout_prefix (*_dout << "rgw realm watcher: ") |
| 19 | + |
| 20 | + |
| 21 | +namespace rgw::rados { |
| 22 | + |
| 23 | +RadosRealmWatcher::RadosRealmWatcher(const DoutPrefixProvider* dpp, CephContext* cct, |
| 24 | + librados::Rados& rados, const RGWRealm& realm) |
| 25 | + : cct(cct) |
| 26 | +{ |
| 27 | + // no default realm, nothing to watch |
| 28 | + if (realm.get_id().empty()) { |
| 29 | + ldpp_dout(dpp, 4) << "No realm, disabling dynamic reconfiguration." << dendl; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // establish the watch on RGWRealm |
| 34 | + int r = watch_start(dpp, rados, realm); |
| 35 | + if (r < 0) { |
| 36 | + ldpp_dout(dpp, -1) << "Failed to establish a watch on RGWRealm, " |
| 37 | + "disabling dynamic reconfiguration." << dendl; |
| 38 | + return; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +RadosRealmWatcher::~RadosRealmWatcher() |
| 43 | +{ |
| 44 | + watch_stop(); |
| 45 | +} |
| 46 | + |
| 47 | +void RadosRealmWatcher::handle_notify(uint64_t notify_id, uint64_t cookie, |
| 48 | + uint64_t notifier_id, bufferlist& bl) |
| 49 | +{ |
| 50 | + if (cookie != watch_handle) |
| 51 | + return; |
| 52 | + |
| 53 | + // send an empty notify ack |
| 54 | + bufferlist reply; |
| 55 | + pool_ctx.notify_ack(watch_oid, notify_id, cookie, reply); |
| 56 | + |
| 57 | + try { |
| 58 | + auto p = bl.cbegin(); |
| 59 | + while (!p.end()) { |
| 60 | + RGWRealmNotify notify; |
| 61 | + decode(notify, p); |
| 62 | + auto watcher = watchers.find(notify); |
| 63 | + if (watcher == watchers.end()) { |
| 64 | + lderr(cct) << "Failed to find a watcher for notify type " |
| 65 | + << static_cast<int>(notify) << dendl; |
| 66 | + break; |
| 67 | + } |
| 68 | + watcher->second.handle_notify(notify, p); |
| 69 | + } |
| 70 | + } catch (const buffer::error &e) { |
| 71 | + lderr(cct) << "Failed to decode realm notifications." << dendl; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void RadosRealmWatcher::handle_error(uint64_t cookie, int err) |
| 76 | +{ |
| 77 | + lderr(cct) << "RadosRealmWatcher::handle_error oid=" << watch_oid << " err=" << err << dendl; |
| 78 | + if (cookie != watch_handle) |
| 79 | + return; |
| 80 | + |
| 81 | + watch_restart(); |
| 82 | +} |
| 83 | + |
| 84 | +int RadosRealmWatcher::watch_start(const DoutPrefixProvider* dpp, |
| 85 | + librados::Rados& rados, |
| 86 | + const RGWRealm& realm) |
| 87 | +{ |
| 88 | + // initialize a Rados client |
| 89 | + int r = rados.init_with_context(cct); |
| 90 | + if (r < 0) { |
| 91 | + ldpp_dout(dpp, -1) << "Rados client initialization failed with " |
| 92 | + << cpp_strerror(-r) << dendl; |
| 93 | + return r; |
| 94 | + } |
| 95 | + r = rados.connect(); |
| 96 | + if (r < 0) { |
| 97 | + ldpp_dout(dpp, -1) << "Rados client connection failed with " |
| 98 | + << cpp_strerror(-r) << dendl; |
| 99 | + return r; |
| 100 | + } |
| 101 | + |
| 102 | + // open an IoCtx for the realm's pool |
| 103 | + rgw_pool pool(realm.get_pool(cct)); |
| 104 | + r = rgw_init_ioctx(dpp, &rados, pool, pool_ctx); |
| 105 | + if (r < 0) { |
| 106 | + ldpp_dout(dpp, -1) << "Failed to open pool " << pool |
| 107 | + << " with " << cpp_strerror(-r) << dendl; |
| 108 | + rados.shutdown(); |
| 109 | + return r; |
| 110 | + } |
| 111 | + |
| 112 | + // register a watch on the realm's control object |
| 113 | + auto oid = realm.get_control_oid(); |
| 114 | + r = pool_ctx.watch2(oid, &watch_handle, this); |
| 115 | + if (r < 0) { |
| 116 | + ldpp_dout(dpp, -1) << "Failed to watch " << oid |
| 117 | + << " with " << cpp_strerror(-r) << dendl; |
| 118 | + pool_ctx.close(); |
| 119 | + rados.shutdown(); |
| 120 | + return r; |
| 121 | + } |
| 122 | + |
| 123 | + ldpp_dout(dpp, 10) << "Watching " << oid << dendl; |
| 124 | + std::swap(watch_oid, oid); |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +int RadosRealmWatcher::watch_restart() |
| 129 | +{ |
| 130 | + ceph_assert(!watch_oid.empty()); |
| 131 | + int r = pool_ctx.unwatch2(watch_handle); |
| 132 | + if (r < 0) { |
| 133 | + lderr(cct) << "Failed to unwatch on " << watch_oid |
| 134 | + << " with " << cpp_strerror(-r) << dendl; |
| 135 | + } |
| 136 | + r = pool_ctx.watch2(watch_oid, &watch_handle, this); |
| 137 | + if (r < 0) { |
| 138 | + lderr(cct) << "Failed to restart watch on " << watch_oid |
| 139 | + << " with " << cpp_strerror(-r) << dendl; |
| 140 | + pool_ctx.close(); |
| 141 | + watch_oid.clear(); |
| 142 | + } |
| 143 | + return r; |
| 144 | +} |
| 145 | + |
| 146 | +void RadosRealmWatcher::watch_stop() |
| 147 | +{ |
| 148 | + if (!watch_oid.empty()) { |
| 149 | + pool_ctx.unwatch2(watch_handle); |
| 150 | + pool_ctx.close(); |
| 151 | + watch_oid.clear(); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +auto RadosConfigStore::create_realm_watcher(const DoutPrefixProvider* dpp, |
| 156 | + optional_yield y, |
| 157 | + const RGWRealm& realm) |
| 158 | + -> std::unique_ptr<RGWRealmWatcher> |
| 159 | +{ |
| 160 | + return std::make_unique<RadosRealmWatcher>(dpp, dpp->get_cct(), impl->rados, realm); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace rgw::rados |
0 commit comments