From eeba474f8ee21a2f8aa60c508053f9f17127b21a 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 (cherry picked from commit 835749e1791ecdb0ba1d4ae1686a4f9aed52c772) Conflicts: src/common/entity_name.cc: header changes src/common/entity_name.h: header changes --- src/common/entity_name.cc | 77 ++++++++++++++++++++++++++++++--------- src/common/entity_name.h | 44 ++++++++-------------- src/include/msgr.h | 2 + src/msg/msg_types.h | 2 - 4 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/common/entity_name.cc b/src/common/entity_name.cc index a9d6fb9c8b1..92c24443306 100644 --- a/src/common/entity_name.cc +++ b/src/common/entity_name.cc @@ -14,20 +14,45 @@ #include "common/entity_name.h" #include "common/ceph_strings.h" +#include "common/Formatter.h" #include +#include +#include +#include using std::string; +using namespace std::literals::string_view_literals; +void EntityName::encode(ceph::buffer::list& bl) const +{ + using ceph::encode; + { + uint32_t _type = type; + encode(_type, bl); + } + encode(id, bl); +} -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" }, -}}; +void EntityName::decode(ceph::buffer::list::const_iterator& bl) +{ + using ceph::decode; + std::string id_; + uint32_t _type; + decode(_type, bl); + decode(id_, bl); + set(static_cast(_type), id_); +} + +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); @@ -65,7 +90,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_; @@ -79,14 +104,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); } @@ -130,19 +155,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 53f8cd4d5d0..9084bc25659 100644 --- a/src/common/entity_name.h +++ b/src/common/entity_name.h @@ -12,8 +12,7 @@ * */ -#ifndef CEPH_COMMON_ENTITY_NAME_H -#define CEPH_COMMON_ENTITY_NAME_H +#pragma once #include @@ -21,41 +20,37 @@ #include "msg/msg_types.h" +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 { - void encode(ceph::buffer::list& bl) const { - using ceph::encode; - encode(type, bl); - encode(id, bl); - } - void decode(ceph::buffer::list::const_iterator& bl) { - using ceph::decode; - uint32_t type_; - std::string id_; - decode(type_, bl); - decode(id_, bl); - set(type_, id_); - } + 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; static void generate_test_instances(std::list& ls); 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; } @@ -67,7 +62,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); @@ -77,17 +73,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 d2d4e056477..580e75f7a49 100644 --- a/src/msg/msg_types.h +++ b/src/msg/msg_types.h @@ -45,8 +45,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.39.5