From 690018e24d460a0b4b1bb7dda5c9156edd2f15b5 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Sat, 31 May 2025 19:52:33 -0400 Subject: [PATCH] common/entity_name: cleanup entity_name::type This should use the entity_type_t from the msg headers. The only awkwardness is that the encode/decode of the type needs to continue using a uint32_t. Signed-off-by: Patrick Donnelly --- src/common/entity_name.cc | 75 ++++++++++++++++++++++++++------------- src/common/entity_name.h | 29 ++++++++------- src/include/msgr.h | 2 ++ src/msg/msg_types.h | 2 -- 4 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/common/entity_name.cc b/src/common/entity_name.cc index 996faa6ef42..c6078e6388c 100644 --- a/src/common/entity_name.cc +++ b/src/common/entity_name.cc @@ -16,35 +16,44 @@ #include "common/entity_name.h" #include "common/ceph_strings.h" #include "common/Formatter.h" -#include "msg/msg_types.h" #include +#include +#include +#include using std::string; +using namespace std::literals::string_view_literals; -void EntityName::encode(ceph::buffer::list& bl) const { +void EntityName::encode(ceph::buffer::list& bl) const +{ using ceph::encode; - encode(type, bl); + { + uint32_t _type = type; + encode(_type, bl); + } encode(id, bl); } -void EntityName::decode(ceph::buffer::list::const_iterator& bl) { +void EntityName::decode(ceph::buffer::list::const_iterator& bl) +{ using ceph::decode; - uint32_t type_; std::string id_; - decode(type_, bl); + uint32_t _type; + decode(_type, bl); decode(id_, bl); - set(type_, id_); + set(static_cast(_type), id_); } -const std::array EntityName::STR_TO_ENTITY_TYPE = {{ - { CEPH_ENTITY_TYPE_AUTH, "auth" }, - { CEPH_ENTITY_TYPE_MON, "mon" }, - { CEPH_ENTITY_TYPE_OSD, "osd" }, - { CEPH_ENTITY_TYPE_MDS, "mds" }, - { CEPH_ENTITY_TYPE_MGR, "mgr" }, - { CEPH_ENTITY_TYPE_CLIENT, "client" }, -}}; +using namespace std::string_literals; +static const std::vector> STR_TO_ENTITY_TYPE = { + { CEPH_ENTITY_TYPE_AUTH, "auth"s }, + { CEPH_ENTITY_TYPE_MON, "mon"s }, + { CEPH_ENTITY_TYPE_OSD, "osd"s }, + { CEPH_ENTITY_TYPE_MDS, "mds"s }, + { CEPH_ENTITY_TYPE_MGR, "mgr"s }, + { CEPH_ENTITY_TYPE_CLIENT, "client"s }, +}; void EntityName::dump(ceph::Formatter *f) const { f->dump_int("type", type); @@ -84,7 +93,7 @@ bool EntityName::from_str(std::string_view s) { return true; } -void EntityName::set(uint32_t type_, std::string_view id_) { +void EntityName::set(entity_type_t type_, std::string_view id_) { type = type_; id = id_; @@ -98,14 +107,14 @@ void EntityName::set(uint32_t type_, std::string_view id_) { } int EntityName::set(std::string_view type_, std::string_view id_) { - uint32_t t = str_to_ceph_entity_type(type_); + auto t = str_to_ceph_entity_type(type_); if (t == CEPH_ENTITY_TYPE_ANY) return -EINVAL; set(t, id_); return 0; } -void EntityName::set_type(uint32_t type_) { +void EntityName::set_type(entity_type_t type_) { set(type_, id); } @@ -149,19 +158,35 @@ std::string EntityName::get_valid_types_as_str() { if (i > 0) { out << ", "; } - out << STR_TO_ENTITY_TYPE[i].str; + out << STR_TO_ENTITY_TYPE[i].first; } return out.str(); } -uint32_t EntityName::str_to_ceph_entity_type(std::string_view s) +entity_type_t EntityName::str_to_ceph_entity_type(std::string_view s) { - size_t i; - for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) { - if (s == STR_TO_ENTITY_TYPE[i].str) - return STR_TO_ENTITY_TYPE[i].type; + auto l = [s](const auto& p) { + return s == p.second; + }; + auto it = std::find_if(STR_TO_ENTITY_TYPE.begin(), STR_TO_ENTITY_TYPE.end(), std::move(l)); + if (it == STR_TO_ENTITY_TYPE.end()) { + return CEPH_ENTITY_TYPE_ANY; + } else { + return it->first; + } +} + +std::string_view EntityName::ceph_entity_type_to_str(entity_type_t type) +{ + auto l = [type](const auto& p) { + return type == p.first; + }; + auto it = std::find_if(STR_TO_ENTITY_TYPE.begin(), STR_TO_ENTITY_TYPE.end(), std::move(l)); + if (it == STR_TO_ENTITY_TYPE.end()) { + return "???"sv; + } else { + return it->second; } - return CEPH_ENTITY_TYPE_ANY; } bool operator<(const EntityName& a, const EntityName& b) diff --git a/src/common/entity_name.h b/src/common/entity_name.h index e8182420c12..af377c99bc0 100644 --- a/src/common/entity_name.h +++ b/src/common/entity_name.h @@ -13,8 +13,7 @@ * */ -#ifndef CEPH_COMMON_ENTITY_NAME_H -#define CEPH_COMMON_ENTITY_NAME_H +#pragma once #include #include @@ -28,13 +27,20 @@ class entity_name_t; namespace ceph { class Formatter; } +using namespace std::literals::string_view_literals; + + /* Represents a Ceph entity name. * * For example, mds.0 is the name of the first metadata server. * client */ + struct EntityName { + EntityName() = default; + explicit EntityName(entity_type_t t) : type(t), id("*"sv) {} + void encode(ceph::buffer::list& bl) const; void decode(ceph::buffer::list::const_iterator& bl); void dump(ceph::Formatter *f) const; @@ -42,16 +48,16 @@ struct EntityName const std::string& to_str() const; const char *to_cstr() const; bool from_str(std::string_view s); - void set(uint32_t type_, std::string_view id_); + void set(entity_type_t type_, std::string_view id_); int set(std::string_view type_, std::string_view id_); - void set_type(uint32_t type_); + void set_type(entity_type_t type_); int set_type(std::string_view type); void set_id(std::string_view id_); void set_name(entity_name_t n); const char* get_type_str() const; - uint32_t get_type() const { return type; } + entity_type_t get_type() const { return type; } bool is_osd() const { return get_type() == CEPH_ENTITY_TYPE_OSD; } bool is_mgr() const { return get_type() == CEPH_ENTITY_TYPE_MGR; } bool is_mds() const { return get_type() == CEPH_ENTITY_TYPE_MDS; } @@ -63,7 +69,8 @@ struct EntityName bool has_default_id() const; static std::string get_valid_types_as_str(); - static uint32_t str_to_ceph_entity_type(std::string_view); + static entity_type_t str_to_ceph_entity_type(std::string_view); + static std::string_view ceph_entity_type_to_str(entity_type_t type); friend bool operator<(const EntityName& a, const EntityName& b); friend std::ostream& operator<<(std::ostream& out, const EntityName& n); @@ -73,17 +80,9 @@ struct EntityName } private: - struct str_to_entity_type_t { - uint32_t type; - const char *str; - }; - static const std::array STR_TO_ENTITY_TYPE; - - uint32_t type = 0; + entity_type_t type = 0; std::string id; std::string type_id; }; WRITE_CLASS_ENCODER(EntityName) - -#endif diff --git a/src/include/msgr.h b/src/include/msgr.h index c8ad48ad1af..b8960c5f09e 100644 --- a/src/include/msgr.h +++ b/src/include/msgr.h @@ -86,6 +86,8 @@ struct ceph_entity_name { __le64 num; } __attribute__ ((packed)); +typedef uint8_t entity_type_t; + #define CEPH_ENTITY_TYPE_MON 0x01 #define CEPH_ENTITY_TYPE_MDS 0x02 #define CEPH_ENTITY_TYPE_OSD 0x04 diff --git a/src/msg/msg_types.h b/src/msg/msg_types.h index f823ad7e87c..10a8b1b9b25 100644 --- a/src/msg/msg_types.h +++ b/src/msg/msg_types.h @@ -47,8 +47,6 @@ namespace ceph { std::ostream& operator<<(std::ostream& out, const sockaddr_storage &ss); std::ostream& operator<<(std::ostream& out, const sockaddr *sa); -typedef uint8_t entity_type_t; - class entity_name_t { public: entity_type_t _type; -- 2.47.3