From: J. Eric Ivancich Date: Mon, 25 Sep 2017 18:35:31 +0000 (-0400) Subject: rgw: consolidate code that implements hashing algorithms X-Git-Tag: v13.0.1~577^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e98cd6f2e043a7e5c9c334cf68da582ac1096ed2;p=ceph.git rgw: consolidate code that implements hashing algorithms Copies of the same hashing algorithm are scattered throughout the rgw codebase. Consolidate them. Signed-off-by: J. Eric Ivancich --- diff --git a/src/rgw/rgw_gc.cc b/src/rgw/rgw_gc.cc index e29af16ed176..bf64925abda5 100644 --- a/src/rgw/rgw_gc.cc +++ b/src/rgw/rgw_gc.cc @@ -43,7 +43,7 @@ void RGWGC::finalize() int RGWGC::tag_index(const string& tag) { - return rgw_shards_hash(tag, max_objs); + return rgw_shard_id(tag, max_objs); } void RGWGC::add_chain(ObjectWriteOperation& op, cls_rgw_obj_chain& chain, const string& tag) diff --git a/src/rgw/rgw_rados.cc b/src/rgw/rgw_rados.cc index b9181d47359a..eb4eafa576f7 100644 --- a/src/rgw/rgw_rados.cc +++ b/src/rgw/rgw_rados.cc @@ -5121,7 +5121,7 @@ next: int RGWRados::key_to_shard_id(const string& key, int max_shards) { - return rgw_shards_hash(key, max_shards); + return rgw_shard_id(key, max_shards); } void RGWRados::shard_name(const string& prefix, unsigned max_shards, const string& key, string& name, int *shard_id) @@ -5302,10 +5302,7 @@ int RGWRados::objexp_key_shard(const rgw_obj_index_key& key) { string obj_key = key.name + key.instance; int num_shards = cct->_conf->rgw_objexp_hints_num_shards; - uint32_t sid = ceph_str_hash_linux(obj_key.c_str(), obj_key.size()); - uint32_t sid2 = sid ^ ((sid & 0xFF) << 24); - sid = rgw_shards_mod(sid2, num_shards); - return sid; + return rgw_bucket_shard_index(obj_key, num_shards); } static string objexp_hint_get_keyext(const string& tenant_name, @@ -13384,9 +13381,7 @@ int RGWRados::get_target_shard_id(const RGWBucketInfo& bucket_info, const string *shard_id = -1; } } else { - uint32_t sid = ceph_str_hash_linux(obj_key.c_str(), obj_key.size()); - uint32_t sid2 = sid ^ ((sid & 0xFF) << 24); - sid = rgw_shards_mod(sid2, bucket_info.num_shards); + uint32_t sid = rgw_bucket_shard_index(obj_key, bucket_info.num_shards); if (shard_id) { *shard_id = (int)sid; } @@ -13424,9 +13419,7 @@ int RGWRados::get_bucket_index_object(const string& bucket_oid_base, const strin *shard_id = -1; } } else { - uint32_t sid = ceph_str_hash_linux(obj_key.c_str(), obj_key.size()); - uint32_t sid2 = sid ^ ((sid & 0xFF) << 24); - sid = rgw_shards_mod(sid2, num_shards); + uint32_t sid = rgw_bucket_shard_index(obj_key, num_shards); char buf[bucket_oid_base.size() + 32]; snprintf(buf, sizeof(buf), "%s.%d", bucket_oid_base.c_str(), sid); (*bucket_obj) = buf; diff --git a/src/rgw/rgw_rados.h b/src/rgw/rgw_rados.h index 220aa5e90a2f..09ae86c64dd6 100644 --- a/src/rgw/rgw_rados.h +++ b/src/rgw/rgw_rados.h @@ -57,6 +57,7 @@ class RGWReshardWait; #define RGW_SHARDS_PRIME_0 7877 #define RGW_SHARDS_PRIME_1 65521 +// only called by rgw_shard_id and rgw_bucket_shard_index static inline int rgw_shards_mod(unsigned hval, int max_shards) { if (max_shards <= RGW_SHARDS_PRIME_0) { @@ -65,9 +66,19 @@ static inline int rgw_shards_mod(unsigned hval, int max_shards) return hval % RGW_SHARDS_PRIME_1 % max_shards; } -static inline int rgw_shards_hash(const string& key, int max_shards) +// used for logging and tagging +static inline int rgw_shard_id(const string& key, int max_shards) { - return rgw_shards_mod(ceph_str_hash_linux(key.c_str(), key.size()), max_shards); + return rgw_shards_mod(ceph_str_hash_linux(key.c_str(), key.size()), + max_shards); +} + +// used for bucket indices +static inline uint32_t rgw_bucket_shard_index(const std::string& key, + int num_shards) { + uint32_t sid = ceph_str_hash_linux(key.c_str(), key.size()); + uint32_t sid2 = sid ^ ((sid & 0xFF) << 24); + return rgw_shards_mod(sid2, num_shards); } static inline int rgw_shards_max() diff --git a/src/rgw/rgw_reshard.cc b/src/rgw/rgw_reshard.cc index 0389ca33dce2..c4d04ae84456 100644 --- a/src/rgw/rgw_reshard.cc +++ b/src/rgw/rgw_reshard.cc @@ -588,9 +588,8 @@ void RGWReshard::get_bucket_logshard_oid(const string& tenant, const string& buc uint32_t sid = ceph_str_hash_linux(key.c_str(), key.size()); uint32_t sid2 = sid ^ ((sid & 0xFF) << 24); sid = sid2 % MAX_RESHARD_LOGSHARDS_PRIME % num_logshards; - int logshard = sid % num_logshards; - get_logshard_oid(logshard, oid); + get_logshard_oid(int(sid), oid); } int RGWReshard::add(cls_rgw_reshard_entry& entry)