1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
6 #include "common/errno.h"
9 #include "rgw_realm_watcher.h"
10 #include "rgw_meta_sync_status.h"
11 #include "rgw_sal_config.h"
12 #include "rgw_string.h"
15 #include "services/svc_zone.h"
16 #include "services/svc_sys_obj.h"
18 #include "common/ceph_json.h"
19 #include "common/Formatter.h"
21 #define dout_context g_ceph_context
22 #define dout_subsys ceph_subsys_rgw
24 namespace rgw_zone_defaults
{
26 std::string realm_info_oid_prefix
= "realms.";
27 std::string realm_names_oid_prefix
= "realms_names.";
28 std::string default_realm_info_oid
= "default.realm";
29 std::string RGW_DEFAULT_REALM_ROOT_POOL
= "rgw.root";
34 using namespace rgw_zone_defaults
;
36 RGWRealm::~RGWRealm() {}
38 RGWRemoteMetaLog::~RGWRemoteMetaLog()
43 string
RGWRealm::get_predefined_id(CephContext
*cct
) const {
44 return cct
->_conf
.get_val
<string
>("rgw_realm_id");
47 const string
& RGWRealm::get_predefined_name(CephContext
*cct
) const {
48 return cct
->_conf
->rgw_realm
;
51 int RGWRealm::create(const DoutPrefixProvider
*dpp
, optional_yield y
, bool exclusive
)
53 int ret
= RGWSystemMetaObj::create(dpp
, y
, exclusive
);
55 ldpp_dout(dpp
, 0) << "ERROR creating new realm object " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
58 // create the control object for watch/notify
59 ret
= create_control(dpp
, exclusive
, y
);
61 ldpp_dout(dpp
, 0) << "ERROR creating control for new realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
65 if (current_period
.empty()) {
66 /* create new period for the realm */
67 ret
= period
.init(dpp
, cct
, sysobj_svc
, id
, y
, name
, false);
71 ret
= period
.create(dpp
, y
, true);
73 ldpp_dout(dpp
, 0) << "ERROR: creating new period for realm " << name
<< ": " << cpp_strerror(-ret
) << dendl
;
77 period
= RGWPeriod(current_period
, 0);
78 int ret
= period
.init(dpp
, cct
, sysobj_svc
, id
, y
, name
);
80 ldpp_dout(dpp
, 0) << "ERROR: failed to init period " << current_period
<< dendl
;
84 ret
= set_current_period(dpp
, period
, y
);
86 ldpp_dout(dpp
, 0) << "ERROR: failed set current period " << current_period
<< dendl
;
89 // try to set as default. may race with another create, so pass exclusive=true
90 // so we don't override an existing default
91 ret
= set_as_default(dpp
, y
, true);
92 if (ret
< 0 && ret
!= -EEXIST
) {
93 ldpp_dout(dpp
, 0) << "WARNING: failed to set realm as default realm, ret=" << ret
<< dendl
;
99 int RGWRealm::delete_obj(const DoutPrefixProvider
*dpp
, optional_yield y
)
101 int ret
= RGWSystemMetaObj::delete_obj(dpp
, y
);
105 return delete_control(dpp
, y
);
108 int RGWRealm::create_control(const DoutPrefixProvider
*dpp
, bool exclusive
, optional_yield y
)
110 auto pool
= rgw_pool
{get_pool(cct
)};
111 auto oid
= get_control_oid();
113 auto sysobj
= sysobj_svc
->get_obj(rgw_raw_obj
{pool
, oid
});
115 .set_exclusive(exclusive
)
119 int RGWRealm::delete_control(const DoutPrefixProvider
*dpp
, optional_yield y
)
121 auto pool
= rgw_pool
{get_pool(cct
)};
122 auto obj
= rgw_raw_obj
{pool
, get_control_oid()};
123 auto sysobj
= sysobj_svc
->get_obj(obj
);
124 return sysobj
.wop().remove(dpp
, y
);
127 rgw_pool
RGWRealm::get_pool(CephContext
*cct
) const
129 if (cct
->_conf
->rgw_realm_root_pool
.empty()) {
130 return rgw_pool(RGW_DEFAULT_REALM_ROOT_POOL
);
132 return rgw_pool(cct
->_conf
->rgw_realm_root_pool
);
135 const string
RGWRealm::get_default_oid(bool old_format
) const
137 if (cct
->_conf
->rgw_default_realm_info_oid
.empty()) {
138 return default_realm_info_oid
;
140 return cct
->_conf
->rgw_default_realm_info_oid
;
143 const string
& RGWRealm::get_names_oid_prefix() const
145 return realm_names_oid_prefix
;
148 const string
& RGWRealm::get_info_oid_prefix(bool old_format
) const
150 return realm_info_oid_prefix
;
153 int RGWRealm::set_current_period(const DoutPrefixProvider
*dpp
, RGWPeriod
& period
, optional_yield y
)
155 // update realm epoch to match the period's
156 if (epoch
> period
.get_realm_epoch()) {
157 ldpp_dout(dpp
, 0) << "ERROR: set_current_period with old realm epoch "
158 << period
.get_realm_epoch() << ", current epoch=" << epoch
<< dendl
;
161 if (epoch
== period
.get_realm_epoch() && current_period
!= period
.get_id()) {
162 ldpp_dout(dpp
, 0) << "ERROR: set_current_period with same realm epoch "
163 << period
.get_realm_epoch() << ", but different period id "
164 << period
.get_id() << " != " << current_period
<< dendl
;
168 epoch
= period
.get_realm_epoch();
169 current_period
= period
.get_id();
171 int ret
= update(dpp
, y
);
173 ldpp_dout(dpp
, 0) << "ERROR: period update: " << cpp_strerror(-ret
) << dendl
;
177 ret
= period
.reflect(dpp
, y
);
179 ldpp_dout(dpp
, 0) << "ERROR: period.reflect(): " << cpp_strerror(-ret
) << dendl
;
186 string
RGWRealm::get_control_oid() const
188 return get_info_oid_prefix() + id
+ ".control";
191 int RGWRealm::notify_zone(const DoutPrefixProvider
*dpp
, bufferlist
& bl
, optional_yield y
)
193 rgw_pool pool
{get_pool(cct
)};
194 auto sysobj
= sysobj_svc
->get_obj(rgw_raw_obj
{pool
, get_control_oid()});
195 int ret
= sysobj
.wn().notify(dpp
, bl
, 0, nullptr, y
);
202 int RGWRealm::notify_new_period(const DoutPrefixProvider
*dpp
, const RGWPeriod
& period
, optional_yield y
)
206 // push the period to dependent zonegroups/zones
207 encode(RGWRealmNotify::ZonesNeedPeriod
, bl
);
209 // reload the gateway with the new period
210 encode(RGWRealmNotify::Reload
, bl
);
212 return notify_zone(dpp
, bl
, y
);
216 int RGWRealm::find_zone(const DoutPrefixProvider
*dpp
,
217 const rgw_zone_id
& zid
,
219 RGWZoneGroup
*pzonegroup
,
221 optional_yield y
) const
223 auto& found
= *pfound
;
230 RGWPeriod
period(period_id
, epoch
);
231 int r
= period
.init(dpp
, cct
, sysobj_svc
, get_id(), y
, get_name());
233 ldpp_dout(dpp
, 0) << "WARNING: period init failed: " << cpp_strerror(-r
) << " ... skipping" << dendl
;
237 found
= period
.find_zone(dpp
, zid
, pzonegroup
, y
);
244 void RGWRealm::generate_test_instances(list
<RGWRealm
*> &o
)
246 RGWRealm
*z
= new RGWRealm
;
248 o
.push_back(new RGWRealm
);
251 void RGWRealm::dump(Formatter
*f
) const
253 RGWSystemMetaObj::dump(f
);
254 encode_json("current_period", current_period
, f
);
255 encode_json("epoch", epoch
, f
);
259 void RGWRealm::decode_json(JSONObj
*obj
)
261 RGWSystemMetaObj::decode_json(obj
);
262 JSONDecoder::decode_json("current_period", current_period
, obj
);
263 JSONDecoder::decode_json("epoch", epoch
, obj
);