]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/entity_name: cleanup entity_name::type
authorPatrick Donnelly <pdonnell@ibm.com>
Sat, 31 May 2025 23:52:33 +0000 (19:52 -0400)
committerPatrick Donnelly <pdonnell@ibm.com>
Mon, 5 Jan 2026 21:23:34 +0000 (16:23 -0500)
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 <pdonnell@ibm.com>
src/common/entity_name.cc
src/common/entity_name.h
src/include/msgr.h
src/msg/msg_types.h

index 996faa6ef42058ae454f00d9938dc480df937f1e..c6078e6388cb0e4e8fda3181e3ad94e8c090723f 100644 (file)
 #include "common/entity_name.h"
 #include "common/ceph_strings.h"
 #include "common/Formatter.h"
-#include "msg/msg_types.h"
 
 #include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
 
 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<entity_type_t>(_type), id_);
 }
 
-const std::array<EntityName::str_to_entity_type_t, 6> 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<std::pair<entity_type_t, std::string>> 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)
index e8182420c125ebf15415ffb885f07cf30c3fbb85..af377c99bc06822bde19fa6ac2e64d9c44da8102 100644 (file)
@@ -13,8 +13,7 @@
  *
  */
 
-#ifndef CEPH_COMMON_ENTITY_NAME_H
-#define CEPH_COMMON_ENTITY_NAME_H
+#pragma once
 
 #include <array>
 #include <cstdint>
 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_t, 6> 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
index c8ad48ad1afef1306a33662b92daa0e0245b3a0b..b8960c5f09e239fa087295f953d8f4442555d9fe 100644 (file)
@@ -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
index f823ad7e87c3d9821530e18aa405bb670b7fde35..10a8b1b9b2543a1ae3e0b5db553522d3d1c9e729 100644 (file)
@@ -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;