return 0;
}
+static int do_period_pull(const string& remote, const string& url, const string& access_key, const string& secret_key,
+ const string& realm_id, const string& realm_name, const string& period_id, const string& period_epoch,
+ RGWPeriod *period)
+{
+ RGWEnv env;
+ req_info info(g_ceph_context, &env);
+ info.method = "GET";
+ info.request_uri = "/admin/realm/period";
+
+ map<string, string> ¶ms = info.args.get_params();
+ if (!realm_id.empty())
+ params["realm_id"] = realm_id;
+ if (!realm_name.empty())
+ params["realm_name"] = realm_name;
+ if (!period_id.empty())
+ params["period_id"] = period_id;
+ if (!period_epoch.empty())
+ params["epoch"] = period_epoch;
+
+ bufferlist bl;
+ JSONParser p;
+ int ret = send_to_remote_or_url(remote, url, access_key, secret_key,
+ info, bl, p);
+ if (ret < 0) {
+ cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
+ return ret;
+ }
+ ret = period->init(g_ceph_context, store, false);
+ if (ret < 0) {
+ cerr << "faile to init period " << cpp_strerror(-ret) << std::endl;
+ return ret;
+ }
+ try {
+ decode_json_obj(*period, &p);
+ } catch (JSONDecoder::err& e) {
+ cout << "failed to decode JSON input: " << e.message << std::endl;
+ return -EINVAL;
+ }
+ ret = period->store_info(false);
+ if (ret < 0) {
+ cerr << "Error storing period " << period->get_id() << ": " << cpp_strerror(ret) << std::endl;
+ }
+
+ return 0;
+}
+
int main(int argc, char **argv)
{
vector<const char*> args;
cout << "failed to decode JSON response: " << e.message << std::endl;
return -EINVAL;
}
+ RGWPeriod period;
+ if (!realm.get_current_period().empty()) {
+ ret = do_period_pull(remote, url, access_key, secret_key,
+ realm_id, realm_name, period_id, period_epoch,
+ &period);
+ if (ret < 0) {
+ cerr << "could not fetch period " << realm.get_current_period() << std::endl;
+ return -ret;
+ }
+ }
ret = realm.create(false);
if (ret < 0) {
cerr << "Error storing realm " << realm.get_id() << ": "
return 0;
case OPT_PERIOD_PULL:
{
- RGWEnv env;
- req_info info(g_ceph_context, &env);
- info.method = "GET";
- info.request_uri = "/admin/realm/period";
-
- map<string, string> ¶ms = info.args.get_params();
- if (!period_id.empty())
- params["period_id"] = period_id;
- if (!period_epoch.empty())
- params["epoch"] = period_epoch;
-
- bufferlist bl;
- JSONParser p;
- int ret = send_to_remote_or_url(remote, url, access_key, secret_key,
- info, bl, p);
- if (ret < 0) {
- cerr << "request failed: " << cpp_strerror(-ret) << std::endl;
- return ret;
- }
RGWPeriod period;
- ret = period.init(g_ceph_context, store, false);
- if (ret < 0) {
- cerr << "faile to init period " << cpp_strerror(-ret) << std::endl;
- return ret;
- }
- try {
- decode_json_obj(period, &p);
- } catch (JSONDecoder::err& e) {
- cout << "failed to decode JSON input: " << e.message << std::endl;
- return -EINVAL;
- }
- ret = period.store_info(false);
+ int ret = do_period_pull(remote, url, access_key, secret_key,
+ realm_id, realm_name, period_id, period_epoch,
+ &period);
if (ret < 0) {
- cerr << "Error storing period " << period.get_id() << ": " << cpp_strerror(ret) << std::endl;
+ cerr << "period pull failed: " << cpp_strerror(-ret) << std::endl;
}
encode_json("period", period, formatter);
if (ret < 0) {
return ret;
}
- /* create new period for the realm */
RGWPeriod period;
- ret = period.init(cct, store, id, name, false);
- if (ret < 0 ) {
- return ret;
- }
- ret = period.create(true);
- if (ret < 0) {
- ldout(cct, 0) << "ERROR creating new period for realm " << name << ": " << cpp_strerror(-ret) << dendl;
- return ret;
+ if (current_period.empty()) {
+ /* create new period for the realm */
+ ret = period.init(cct, store, id, name, false);
+ if (ret < 0 ) {
+ return ret;
+ }
+ ret = period.create(true);
+ if (ret < 0) {
+ ldout(cct, 0) << "ERROR: creating new period for realm " << name << ": " << cpp_strerror(-ret) << dendl;
+ return ret;
+ }
+ } else {
+ period = RGWPeriod(current_period, 0);
+ int ret = period.init(cct, store, id, name);
+ if (ret < 0) {
+ lderr(cct) << "ERROR: failed to init period " << current_period << dendl;
+ return ret;
+ }
}
ret = set_current_period(period.get_id());
if (ret < 0) {
int RGWPeriod::store_info(bool exclusive)
{
+ epoch_t latest_epoch = FIRST_EPOCH;
+ int ret = get_latest_epoch(latest_epoch);
+ if (ret < 0 && ret != -ENOENT) {
+ ldout(cct, 0) << "ERROR: RGWPeriod::get_latest_epoch() returned " << cpp_strerror(-ret) << dendl;
+ return ret;
+ }
string pool_name = get_pool_name(cct);
rgw_bucket pool(pool_name.c_str());
string oid = get_period_oid();
bufferlist bl;
::encode(*this, bl);
- return rgw_put_system_obj(store, pool, oid, bl.c_str(), bl.length(), exclusive, NULL, 0, NULL);
+ ret = rgw_put_system_obj(store, pool, oid, bl.c_str(), bl.length(), exclusive, NULL, 0, NULL);
+ if (ret < 0) {
+ ldout(cct, 0) << "ERROR: rgw_put_system_obj(" << pool << ":" << oid << "): " << cpp_strerror(-ret) << dendl;
+ return ret;
+ }
+ if (latest_epoch < epoch) {
+ ret = set_latest_epoch(epoch);
+ if (ret < 0) {
+ ldout(cct, 0) << "ERROR: RGWPeriod::set_latest_epoch() returned " << cpp_strerror(-ret) << dendl;
+ return ret;
+ }
+ }
+ return 0;
}
const string& RGWPeriod::get_pool_name(CephContext *cct)