From 73ea202b48471379321c03c616c4db356716b115 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 05f4fd47ccd0b..2ce37e70d48ec 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -2728,7 +2728,7 @@ int check_reshard_bucket_params(rgw::sal::RGWRadosStore *store, return -EBUSY; } - int num_source_shards = (bucket_info.layout.current_index.layout.normal.num_shards > 0 ? bucket_info.layout.current_index.layout.normal.num_shards : 1); + int num_source_shards = rgw::current_num_shards(bucket_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 @@ -6591,7 +6591,7 @@ next: } const auto& index = bucket_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"); @@ -6658,8 +6658,12 @@ next: } const auto& index = bucket_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(store->getRados()); int ret = bs.init(bucket, i, index, nullptr /* no RGWBucketInfo */, dpp()); @@ -6961,7 +6965,7 @@ next: return ret; } - int num_source_shards = (bucket_info.layout.current_index.layout.normal.num_shards > 0 ? bucket_info.layout.current_index.layout.normal.num_shards : 1); + int num_source_shards = rgw::current_num_shards(bucket_info.layout); RGWReshard reshard(store, dpp()); cls_rgw_reshard_entry entry; diff --git a/src/rgw/rgw_bucket.cc b/src/rgw/rgw_bucket.cc index 7830e5105f24c..7097b509da79b 100644 --- a/src/rgw/rgw_bucket.cc +++ b/src/rgw/rgw_bucket.cc @@ -1625,7 +1625,7 @@ int RGWBucketAdminOp::set_quota(rgw::sal::RGWRadosStore *store, RGWBucketAdminOp static int purge_bucket_instance(rgw::sal::RGWRadosStore *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(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 6ba752a2d1686..b5f0bacb512bf 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 d3b830353185c..f66d2d6ca584e 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -9041,8 +9041,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")); @@ -9080,7 +9079,7 @@ int RGWRados::add_bucket_to_reshard(const RGWBucketInfo& bucket_info, uint32_t n { 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 22f9fd1ece6a9..eaba9c1cea2b8 100644 --- a/src/rgw/services/svc_bi_rados.cc +++ b/src/rgw/services/svc_bi_rados.cc @@ -459,7 +459,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