From 449ef32852d4b649f1d7e3791bc53868362f85a8 Mon Sep 17 00:00:00 2001 From: Guohao Li Date: Wed, 7 Feb 2024 18:09:53 +0800 Subject: [PATCH 1/2] refactor: make class 'dns_resolver' implement singleton --- src/runtime/rpc/dns_resolver.cpp | 2 ++ src/runtime/rpc/dns_resolver.h | 10 +++++++--- src/runtime/test/host_port_test.cpp | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/runtime/rpc/dns_resolver.cpp b/src/runtime/rpc/dns_resolver.cpp index 06bf4aca17..f7dac6dbb1 100644 --- a/src/runtime/rpc/dns_resolver.cpp +++ b/src/runtime/rpc/dns_resolver.cpp @@ -141,4 +141,6 @@ rpc_address dns_resolver::resolve_address(const host_port &hp) } } +dns_resolver::~dns_resolver() { _dns_cache.clear(); } + } // namespace dsn diff --git a/src/runtime/rpc/dns_resolver.h b/src/runtime/rpc/dns_resolver.h index aadb3287ff..fff1de8c73 100644 --- a/src/runtime/rpc/dns_resolver.h +++ b/src/runtime/rpc/dns_resolver.h @@ -26,6 +26,7 @@ #include "runtime/rpc/rpc_host_port.h" #include "utils/errors.h" #include "utils/metrics.h" +#include "utils/singleton.h" #include "utils/synchronize.h" namespace dsn { @@ -37,15 +38,18 @@ namespace dsn { // effect. // TODO(yingchun): Now the cache is unlimited, the cache size may be huge. Implement an expiration // mechanism to limit the cache size and make it possible to update the resolve result. -class dns_resolver +class dns_resolver : public utils::singleton { public: - explicit dns_resolver(); - // Resolve this host_port to an unique rpc_address. rpc_address resolve_address(const host_port &hp); private: + dns_resolver(); + ~dns_resolver(); + + friend class utils::singleton; + bool get_cached_addresses(const host_port &hp, std::vector &addresses); error_s resolve_addresses(const host_port &hp, std::vector &addresses); diff --git a/src/runtime/test/host_port_test.cpp b/src/runtime/test/host_port_test.cpp index 2f7dd24a76..8e99b0ee6c 100644 --- a/src/runtime/test/host_port_test.cpp +++ b/src/runtime/test/host_port_test.cpp @@ -230,10 +230,9 @@ TEST(host_port_test, transfer_rpc_address) TEST(host_port_test, dns_resolver) { - dns_resolver resolver; { host_port hp("localhost", 8080); - auto addr = resolver.resolve_address(hp); + auto addr = dns_resolver::instance().resolve_address(hp); ASSERT_TRUE(rpc_address("127.0.0.1", 8080) == addr || rpc_address("127.0.1.1", 8080) == addr); } @@ -248,7 +247,7 @@ TEST(host_port_test, dns_resolver) host_port hp2("localhost", 8081); g_hp->set_leader(hp2); - auto addr_grp = resolver.resolve_address(hp_grp); + auto addr_grp = dns_resolver::instance().resolve_address(hp_grp); auto g_addr = addr_grp.group_address(); ASSERT_EQ(g_addr->is_update_leader_automatically(), g_hp->is_update_leader_automatically()); From f818d6408d67eb53679599767152b18e5c7a3ce8 Mon Sep 17 00:00:00 2001 From: Guohao Li Date: Thu, 8 Feb 2024 15:27:39 +0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Dan Wang --- src/runtime/rpc/dns_resolver.cpp | 2 -- src/runtime/rpc/dns_resolver.h | 2 +- src/runtime/rpc/group_address.h | 4 ++-- src/runtime/test/host_port_test.cpp | 6 +++--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/runtime/rpc/dns_resolver.cpp b/src/runtime/rpc/dns_resolver.cpp index f7dac6dbb1..06bf4aca17 100644 --- a/src/runtime/rpc/dns_resolver.cpp +++ b/src/runtime/rpc/dns_resolver.cpp @@ -141,6 +141,4 @@ rpc_address dns_resolver::resolve_address(const host_port &hp) } } -dns_resolver::~dns_resolver() { _dns_cache.clear(); } - } // namespace dsn diff --git a/src/runtime/rpc/dns_resolver.h b/src/runtime/rpc/dns_resolver.h index fff1de8c73..b4a1f03950 100644 --- a/src/runtime/rpc/dns_resolver.h +++ b/src/runtime/rpc/dns_resolver.h @@ -46,7 +46,7 @@ class dns_resolver : public utils::singleton private: dns_resolver(); - ~dns_resolver(); + ~dns_resolver() = default; friend class utils::singleton; diff --git a/src/runtime/rpc/group_address.h b/src/runtime/rpc/group_address.h index 08689b6fc7..fdd97a798a 100644 --- a/src/runtime/rpc/group_address.h +++ b/src/runtime/rpc/group_address.h @@ -55,7 +55,7 @@ class rpc_group_address : public ref_counter void set_leader(rpc_address addr); bool remove(rpc_address addr) WARN_UNUSED_RESULT; bool contains(rpc_address addr) const WARN_UNUSED_RESULT; - int count(); + int count() const; const std::vector &members() const { return _members; } rpc_address random_member() const @@ -194,7 +194,7 @@ inline bool rpc_group_address::contains(rpc_address addr) const return _members.end() != std::find(_members.begin(), _members.end(), addr); } -inline int rpc_group_address::count() +inline int rpc_group_address::count() const { alr_t l(_lock); return _members.size(); diff --git a/src/runtime/test/host_port_test.cpp b/src/runtime/test/host_port_test.cpp index 8e99b0ee6c..0ab4f1f2e9 100644 --- a/src/runtime/test/host_port_test.cpp +++ b/src/runtime/test/host_port_test.cpp @@ -232,7 +232,7 @@ TEST(host_port_test, dns_resolver) { { host_port hp("localhost", 8080); - auto addr = dns_resolver::instance().resolve_address(hp); + const auto &addr = dns_resolver::instance().resolve_address(hp); ASSERT_TRUE(rpc_address("127.0.0.1", 8080) == addr || rpc_address("127.0.1.1", 8080) == addr); } @@ -247,8 +247,8 @@ TEST(host_port_test, dns_resolver) host_port hp2("localhost", 8081); g_hp->set_leader(hp2); - auto addr_grp = dns_resolver::instance().resolve_address(hp_grp); - auto g_addr = addr_grp.group_address(); + const auto &addr_grp = dns_resolver::instance().resolve_address(hp_grp); + const auto *const g_addr = addr_grp.group_address(); ASSERT_EQ(g_addr->is_update_leader_automatically(), g_hp->is_update_leader_automatically()); ASSERT_STREQ(g_addr->name(), g_hp->name());