From cb376a4c44e81dce628913b99ccc3b5b372a132d Mon Sep 17 00:00:00 2001 From: Shilpa Jagannath Date: Fri, 11 Sep 2020 11:58:55 +0530 Subject: [PATCH] rgw: use a helper function to handle repetitive num_shards check Signed-off-by: Shilpa Jagannath --- src/rgw/rgw_admin.cc | 12 ++++++++---- src/rgw/rgw_bucket.cc | 2 +- src/rgw/rgw_bucket_layout.h | 15 +++++++++++++++ src/rgw/rgw_rados.cc | 5 ++--- src/rgw/services/svc_bi_rados.cc | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 23378cd8340..177c8d2343a 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -2934,7 +2934,7 @@ int check_reshard_bucket_params(rgw::sal::RadosStore* store, return -EBUSY; } - int num_source_shards = ((*bucket)->get_info().layout.current_index.layout.normal.num_shards > 0 ? (*bucket)->get_info().layout.current_index.layout.normal.num_shards : 1); + int num_source_shards = rgw::current_num_shards((*bucket)->get_info().layout); if (num_shards <= num_source_shards && !yes_i_really_mean_it) { cerr << "num shards is less or equal to current shards count" << std::endl @@ -7246,7 +7246,7 @@ next: } const auto& index = bucket->get_info().layout.current_index; - int max_shards = index.layout.normal.num_shards; + const int max_shards = rgw::num_shards(index); formatter->open_array_section("entries"); @@ -7313,8 +7313,12 @@ next: } const auto& index = bucket->get_info().layout.current_index; - int max_shards = index.layout.normal.num_shards; + if (index.layout.type == rgw::BucketIndexType::Indexless) { + cerr << "ERROR: indexless bucket has no index to purge" << std::endl; + return EINVAL; + } + const int max_shards = rgw::num_shards(index); for (int i = 0; i < max_shards; i++) { RGWRados::BucketShard bs(static_cast(store)->getRados()); int shard_id = (bucket->get_info().layout.current_index.layout.normal.num_shards > 0 ? i : -1); @@ -7600,7 +7604,7 @@ next: return ret; } - int num_source_shards = (bucket->get_info().layout.current_index.layout.normal.num_shards > 0 ? bucket->get_info().layout.current_index.layout.normal.num_shards : 1); + int num_source_shards = rgw::current_num_shards(bucket->get_info().layout); RGWReshard reshard(static_cast(store), dpp()); cls_rgw_reshard_entry entry; diff --git a/src/rgw/rgw_bucket.cc b/src/rgw/rgw_bucket.cc index d8cab1734fd..e4b3dee8fc3 100644 --- a/src/rgw/rgw_bucket.cc +++ b/src/rgw/rgw_bucket.cc @@ -1322,7 +1322,7 @@ int RGWBucketAdminOp::set_quota(rgw::sal::Store* store, RGWBucketAdminOpState& o static int purge_bucket_instance(rgw::sal::Store* store, const RGWBucketInfo& bucket_info, const DoutPrefixProvider *dpp) { const auto& index = bucket_info.layout.current_index; - int max_shards = index.layout.normal.num_shards; + const int max_shards = num_shards(index); for (int i = 0; i < max_shards; i++) { RGWRados::BucketShard bs(static_cast(store)->getRados()); int ret = bs.init(bucket_info.bucket, i, index, nullptr, dpp); diff --git a/src/rgw/rgw_bucket_layout.h b/src/rgw/rgw_bucket_layout.h index 6ba752a2d16..b5f0bacb512 100644 --- a/src/rgw/rgw_bucket_layout.h +++ b/src/rgw/rgw_bucket_layout.h @@ -157,4 +157,19 @@ struct BucketLayout { void encode(const BucketLayout& l, bufferlist& bl, uint64_t f=0); void decode(BucketLayout& l, bufferlist::const_iterator& bl); + +inline uint32_t num_shards(const bucket_index_normal_layout& index) { + return index.num_shards; +} +inline uint32_t num_shards(const bucket_index_layout& index) { + ceph_assert(index.type == BucketIndexType::Normal); + return num_shards(index.normal); +} +inline uint32_t num_shards(const bucket_index_layout_generation& index) { + return num_shards(index.layout); +} +inline uint32_t current_num_shards(const BucketLayout& layout) { + return num_shards(layout.current_index); +} + } // namespace rgw diff --git a/src/rgw/rgw_rados.cc b/src/rgw/rgw_rados.cc index f66b790d9d7..0f88672df95 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -9382,8 +9382,7 @@ int RGWRados::check_bucket_shards(const RGWBucketInfo& bucket_info, } bool need_resharding = false; - uint32_t num_source_shards = - (bucket_info.layout.current_index.layout.normal.num_shards > 0 ? bucket_info.layout.current_index.layout.normal.num_shards : 1); + uint32_t num_source_shards = rgw::current_num_shards(bucket_info.layout); const uint32_t max_dynamic_shards = uint32_t(cct->_conf.get_val("rgw_max_dynamic_shards")); @@ -9421,7 +9420,7 @@ int RGWRados::add_bucket_to_reshard(const DoutPrefixProvider *dpp, const RGWBuck { RGWReshard reshard(this->store, dpp); - uint32_t num_source_shards = (bucket_info.layout.current_index.layout.normal.num_shards > 0 ? bucket_info.layout.current_index.layout.normal.num_shards : 1); + uint32_t num_source_shards = rgw::current_num_shards(bucket_info.layout); new_num_shards = std::min(new_num_shards, get_max_bucket_shards()); if (new_num_shards <= num_source_shards) { diff --git a/src/rgw/services/svc_bi_rados.cc b/src/rgw/services/svc_bi_rados.cc index 1b043d8f59b..a2f34382f5f 100644 --- a/src/rgw/services/svc_bi_rados.cc +++ b/src/rgw/services/svc_bi_rados.cc @@ -473,7 +473,7 @@ int RGWSI_BucketIndex_RADOS::handle_overwrite(const DoutPrefixProvider *dpp, bool old_sync_enabled = orig_info.datasync_flag_enabled(); if (old_sync_enabled != new_sync_enabled) { - int shards_num = info.layout.current_index.layout.normal.num_shards? info.layout.current_index.layout.normal.num_shards : 1; + int shards_num = rgw::current_num_shards(info.layout); int shard_id = info.layout.current_index.layout.normal.num_shards? 0 : -1; const auto& log_layout = info.layout.logs.back(); -- 2.39.5