You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pegasus.apache.org by la...@apache.org on 2023/12/27 12:06:37 UTC
(incubator-pegasus) branch master updated: feat(fqdn): json decode & encode for rpc_host_port (#1811)
This is an automated email from the ASF dual-hosted git repository.
laiyingchun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git
The following commit(s) were added to refs/heads/master by this push:
new 4e66ebe3f feat(fqdn): json decode & encode for rpc_host_port (#1811)
4e66ebe3f is described below
commit 4e66ebe3f2d9e895b77b77d6b3b5fcfda54818db
Author: liguohao <48...@users.noreply.github.com>
AuthorDate: Wed Dec 27 20:06:31 2023 +0800
feat(fqdn): json decode & encode for rpc_host_port (#1811)
Issue: https://github.com/apache/incubator-pegasus/issues/1810
Implement json_decode & json_encode for class `rpc_host_port`.
---
src/common/json_helper.h | 15 +++++++++++++++
src/runtime/rpc/rpc_host_port.cpp | 30 ++++++++++++++++++++++++++++--
src/runtime/rpc/rpc_host_port.h | 2 ++
src/runtime/test/host_port_test.cpp | 13 +++++++++++++
src/utils/string_conv.h | 5 +++++
src/utils/test/json_helper_test.cpp | 11 +++++++++++
6 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/src/common/json_helper.h b/src/common/json_helper.h
index 3c5bed8ea..39c1ad1be 100644
--- a/src/common/json_helper.h
+++ b/src/common/json_helper.h
@@ -424,6 +424,21 @@ inline bool json_decode(const dsn::json::JsonObject &in, dsn::rpc_address &addre
return address.from_string_ipv4(rpc_address_string.c_str());
}
+// json serialization for rpc host_port, we use the string representation of a host_port
+inline void json_encode(JsonWriter &out, const dsn::host_port &hp)
+{
+ json_encode(out, hp.to_string());
+}
+inline bool json_decode(const dsn::json::JsonObject &in, dsn::host_port &hp)
+{
+ std::string host_port_string;
+ dverify(json_decode(in, host_port_string));
+ if (host_port_string == "invalid host_port") {
+ return true;
+ }
+ return hp.from_string(host_port_string);
+}
+
inline void json_encode(JsonWriter &out, const dsn::partition_configuration &config);
inline bool json_decode(const JsonObject &in, dsn::partition_configuration &config);
inline void json_encode(JsonWriter &out, const dsn::app_info &info);
diff --git a/src/runtime/rpc/rpc_host_port.cpp b/src/runtime/rpc/rpc_host_port.cpp
index 13c6462b7..fa4d36c9c 100644
--- a/src/runtime/rpc/rpc_host_port.cpp
+++ b/src/runtime/rpc/rpc_host_port.cpp
@@ -30,7 +30,9 @@
#include "runtime/rpc/group_host_port.h"
#include "runtime/rpc/rpc_host_port.h"
#include "utils/error_code.h"
+#include "utils/ports.h"
#include "utils/safe_strerror_posix.h"
+#include "utils/string_conv.h"
#include "utils/utils.h"
namespace dsn {
@@ -57,7 +59,6 @@ error_s GetAddrInfo(const std::string &hostname, const addrinfo &hints, AddrInfo
if (info != nullptr) {
info->swap(result);
}
-
return error_s::ok();
}
}
@@ -73,7 +74,7 @@ host_port::host_port(rpc_address addr)
switch (addr.type()) {
case HOST_TYPE_IPV4: {
CHECK(utils::hostname_from_ip(htonl(addr.ip()), &_host),
- "invalid address {}",
+ "invalid host_port {}",
addr.ipv4_str());
_port = addr.port();
} break;
@@ -86,6 +87,31 @@ host_port::host_port(rpc_address addr)
_type = addr.type();
}
+bool host_port::from_string(const std::string &s)
+{
+ const auto pos = s.find_last_of(':');
+ if (dsn_unlikely(pos == std::string::npos)) {
+ return false;
+ }
+ _host = s.substr(0, pos);
+ std::string port = s.substr(pos + 1);
+
+ if (dsn_unlikely(!dsn::buf2uint16(port, _port))) {
+ return false;
+ }
+
+ struct addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ if (dsn_unlikely(!GetAddrInfo(_host, hints, nullptr))) {
+ return false;
+ }
+
+ _type = HOST_TYPE_IPV4;
+ return true;
+}
+
void host_port::reset()
{
switch (type()) {
diff --git a/src/runtime/rpc/rpc_host_port.h b/src/runtime/rpc/rpc_host_port.h
index be825b22c..763c8ac91 100644
--- a/src/runtime/rpc/rpc_host_port.h
+++ b/src/runtime/rpc/rpc_host_port.h
@@ -82,6 +82,8 @@ public:
// Resolve host_port to rpc_addresses.
// Trere may be multiple rpc_addresses for one host_port.
error_s resolve_addresses(std::vector<rpc_address> &addresses) const;
+ // This function is used for validating the format of string like "localhost:8888".
+ bool from_string(const std::string &s);
// for serialization in thrift format
uint32_t read(::apache::thrift::protocol::TProtocol *iprot);
diff --git a/src/runtime/test/host_port_test.cpp b/src/runtime/test/host_port_test.cpp
index 728bef653..c2e5445cf 100644
--- a/src/runtime/test/host_port_test.cpp
+++ b/src/runtime/test/host_port_test.cpp
@@ -87,6 +87,19 @@ TEST(host_port_test, operators)
ASSERT_NE(hp, hp2);
ASSERT_FALSE(hp.is_invalid());
ASSERT_TRUE(hp2.is_invalid());
+
+ std::string hp_str = "localhost:8080";
+ host_port hp3;
+ ASSERT_TRUE(hp3.is_invalid());
+ ASSERT_TRUE(hp3.from_string(hp_str));
+ ASSERT_EQ(hp, hp3);
+ ASSERT_FALSE(hp3.is_invalid());
+
+ host_port hp4;
+ ASSERT_TRUE(hp4.is_invalid());
+ std::string hp_str2 = "pegasus:8080";
+ ASSERT_FALSE(hp4.from_string(hp_str2));
+ ASSERT_TRUE(hp4.is_invalid());
}
TEST(host_port_test, rpc_group_host_port)
diff --git a/src/utils/string_conv.h b/src/utils/string_conv.h
index 1418e0925..e03df7473 100644
--- a/src/utils/string_conv.h
+++ b/src/utils/string_conv.h
@@ -124,6 +124,11 @@ inline bool buf2uint64(absl::string_view buf, uint64_t &result)
return internal::buf2unsigned(buf, result);
}
+inline bool buf2uint16(absl::string_view buf, uint16_t &result)
+{
+ return internal::buf2unsigned(buf, result);
+}
+
inline bool buf2bool(absl::string_view buf, bool &result, bool ignore_case = true)
{
std::string data(buf.data(), buf.length());
diff --git a/src/utils/test/json_helper_test.cpp b/src/utils/test/json_helper_test.cpp
index 4006b10bf..978f9e3da 100644
--- a/src/utils/test/json_helper_test.cpp
+++ b/src/utils/test/json_helper_test.cpp
@@ -36,6 +36,7 @@
#include "common/json_helper.h"
#include "gtest/gtest.h"
+#include "runtime/rpc/rpc_host_port.h"
#include "utils/blob.h"
namespace dsn {
@@ -505,4 +506,14 @@ TEST(json_helper, upgrade_downgrade)
ASSERT_EQ(n2.c, o.c);
}
+TEST(json_helper, host_port_encode_decode)
+{
+ dsn::host_port hp("localhost", 8888);
+ dsn::blob bb = dsn::json::json_forwarder<decltype(hp)>::encode(hp);
+ dsn::host_port hp2;
+ dsn::json::json_forwarder<dsn::host_port>::decode(bb, hp2);
+
+ ASSERT_EQ(hp, hp2);
+}
+
} // namespace dsn
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pegasus.apache.org
For additional commands, e-mail: commits-help@pegasus.apache.org