From 6494347dd20be5cc31edd08edb7d0bb5ba6fdac1 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Thu, 3 Dec 2015 19:44:24 -0800 Subject: [PATCH] rgw: realm pull also pulls period Signed-off-by: Yehuda Sadeh --- src/rgw/rgw_admin.cc | 92 +++++++++++++++++++++++++++++--------------- src/rgw/rgw_rados.cc | 47 +++++++++++++++++----- 2 files changed, 97 insertions(+), 42 deletions(-) diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 63ebd03d55ee8..9dc26084378a9 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -1489,6 +1489,52 @@ static int init_bucket_for_sync(const string& bucket_name, string& bucket_id) 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 ¶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 args; @@ -3146,6 +3192,16 @@ int main(int argc, char **argv) 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() << ": " @@ -3199,40 +3255,12 @@ int main(int argc, char **argv) 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 ¶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); diff --git a/src/rgw/rgw_rados.cc b/src/rgw/rgw_rados.cc index 5cd3e8c21e1ae..a8e364d2c0966 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -672,16 +672,25 @@ int RGWRealm::create(bool exclusive) 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) { @@ -1037,6 +1046,12 @@ int RGWPeriod::create(bool exclusive) 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()); @@ -1044,7 +1059,19 @@ int RGWPeriod::store_info(bool exclusive) 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) -- 2.39.5