diff --git a/src/core/BUILD b/src/core/BUILD index 973cc9b1aa9..22304b723f4 100644 --- a/src/core/BUILD +++ b/src/core/BUILD @@ -2438,7 +2438,6 @@ grpc_cc_library( "posix_event_engine_timer_manager", "ref_counted_dns_resolver_interface", "useful", - "//:config_vars", "//:event_engine_base_hdrs", "//:gpr", "//:grpc_trace", @@ -2857,6 +2856,7 @@ grpc_cc_library( "resolved_address", "slice", "windows_iocp", + "//:config_vars", "//:debug_location", "//:event_engine_base_hdrs", "//:gpr", diff --git a/src/core/lib/event_engine/ares_resolver.cc b/src/core/lib/event_engine/ares_resolver.cc index 7fb665d744e..a02f11b3f3c 100644 --- a/src/core/lib/event_engine/ares_resolver.cc +++ b/src/core/lib/event_engine/ares_resolver.cc @@ -65,6 +65,7 @@ #include "src/core/lib/address_utils/parse_address.h" #include "src/core/lib/address_utils/sockaddr_utils.h" +#include "src/core/lib/config/config_vars.h" #include "src/core/lib/debug/trace.h" #include "src/core/lib/event_engine/grpc_polled_fd.h" #include "src/core/lib/event_engine/time_util.h" @@ -788,4 +789,50 @@ void (*event_engine_grpc_ares_test_only_inject_config)(ares_channel* channel) = bool g_event_engine_grpc_ares_test_only_force_tcp = false; +bool ShouldUseAresDnsResolver() { +#if defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) || \ + defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) + auto resolver_env = grpc_core::ConfigVars::Get().DnsResolver(); + return resolver_env.empty() || absl::EqualsIgnoreCase(resolver_env, "ares"); +#else // defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) || + // defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) + return false; +#endif // defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) || + // defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) +} + +absl::Status AresInit() { + if (ShouldUseAresDnsResolver()) { + address_sorting_init(); + // ares_library_init and ares_library_cleanup are currently no-op except + // under Windows. Calling them may cause race conditions when other parts of + // the binary calls these functions concurrently. +#ifdef GPR_WINDOWS + int status = ares_library_init(ARES_LIB_INIT_ALL); + if (status != ARES_SUCCESS) { + return GRPC_ERROR_CREATE( + absl::StrCat("ares_library_init failed: ", ares_strerror(status))); + } +#endif // GPR_WINDOWS + } + return absl::OkStatus(); +} +void AresShutdown() { + if (ShouldUseAresDnsResolver()) { + address_sorting_shutdown(); + // ares_library_init and ares_library_cleanup are currently no-op except + // under Windows. Calling them may cause race conditions when other parts of + // the binary calls these functions concurrently. +#ifdef GPR_WINDOWS + ares_library_cleanup(); +#endif // GPR_WINDOWS + } +} + +#else // GRPC_ARES == 1 + +bool ShouldUseAresDnsResolver() { return false; } +absl::Status AresInit() { return absl::OkStatus(); } +void AresShutdown() {} + #endif // GRPC_ARES == 1 diff --git a/src/core/lib/event_engine/ares_resolver.h b/src/core/lib/event_engine/ares_resolver.h index b0e1f3c06c3..0182d0bef4e 100644 --- a/src/core/lib/event_engine/ares_resolver.h +++ b/src/core/lib/event_engine/ares_resolver.h @@ -16,6 +16,8 @@ #include +#include "absl/status/status.h" + #include #include "src/core/lib/debug/trace.h" @@ -29,7 +31,6 @@ #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" -#include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" @@ -146,4 +147,9 @@ extern void (*event_engine_grpc_ares_test_only_inject_config)( extern bool g_event_engine_grpc_ares_test_only_force_tcp; #endif // GRPC_ARES == 1 + +bool ShouldUseAresDnsResolver(); +absl::Status AresInit(); +void AresShutdown(); + #endif // GRPC_SRC_CORE_LIB_EVENT_ENGINE_ARES_RESOLVER_H diff --git a/src/core/lib/event_engine/posix_engine/posix_engine.cc b/src/core/lib/event_engine/posix_engine/posix_engine.cc index 5f72d2b7b7e..10a583d51a1 100644 --- a/src/core/lib/event_engine/posix_engine/posix_engine.cc +++ b/src/core/lib/event_engine/posix_engine/posix_engine.cc @@ -38,7 +38,6 @@ #include #include -#include "src/core/lib/config/config_vars.h" #include "src/core/lib/debug/trace.h" #include "src/core/lib/event_engine/ares_resolver.h" #include "src/core/lib/event_engine/forkable.h" @@ -94,15 +93,6 @@ class TimerForkCallbackMethods { static void PostforkChild() { g_timer_fork_manager->PostforkChild(); } }; -bool ShouldUseAresDnsResolver() { -#if GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) - auto resolver_env = grpc_core::ConfigVars::Get().DnsResolver(); - return resolver_env.empty() || absl::EqualsIgnoreCase(resolver_env, "ares"); -#else // GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) - return false; -#endif // GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) -} - } // namespace #ifdef GRPC_POSIX_SOCKET_TCP diff --git a/src/core/lib/event_engine/windows/windows_engine.cc b/src/core/lib/event_engine/windows/windows_engine.cc index 28763382c22..3dd40177150 100644 --- a/src/core/lib/event_engine/windows/windows_engine.cc +++ b/src/core/lib/event_engine/windows/windows_engine.cc @@ -322,21 +322,24 @@ void WindowsEventEngine::WindowsDNSResolver::LookupTXT( absl::StatusOr> WindowsEventEngine::GetDNSResolver( EventEngine::DNSResolver::ResolverOptions const& options) { + if (ShouldUseAresDnsResolver()) { #if GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) - auto ares_resolver = AresResolver::CreateAresResolver( - options.dns_server, - std::make_unique(poller()), - shared_from_this()); - if (!ares_resolver.ok()) { - return ares_resolver.status(); + GRPC_TRACE_LOG(event_engine_dns, INFO) + << "WindowsEventEngine::" << this << " creating AresResolver"; + auto ares_resolver = AresResolver::CreateAresResolver( + options.dns_server, + std::make_unique(poller()), + shared_from_this()); + if (!ares_resolver.ok()) { + return ares_resolver.status(); + } + return std::make_unique( + std::move(*ares_resolver)); +#endif // GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) } - return std::make_unique( - std::move(*ares_resolver)); -#else // GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) GRPC_TRACE_LOG(event_engine_dns, INFO) << "WindowsEventEngine::" << this << " creating NativeWindowsDNSResolver"; return std::make_unique(shared_from_this()); -#endif // GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER) } bool WindowsEventEngine::IsWorkerThread() { grpc_core::Crash("unimplemented"); } diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc index fc8e75484d2..d0bf3e2ecdb 100644 --- a/src/core/lib/surface/init.cc +++ b/src/core/lib/surface/init.cc @@ -34,6 +34,7 @@ #include "src/core/lib/debug/trace.h" #include "src/core/lib/event_engine/posix_engine/timer_manager.h" #include "src/core/lib/experiments/config.h" +#include "src/core/lib/experiments/experiments.h" #include "src/core/lib/gprpp/fork.h" #include "src/core/lib/gprpp/sync.h" #include "src/core/lib/gprpp/thd.h" @@ -51,6 +52,10 @@ // Remnants of the old plugin system void grpc_resolver_dns_ares_init(void); void grpc_resolver_dns_ares_shutdown(void); +void grpc_resolver_dns_ares_reset_dns_resolver(void); + +extern absl::Status AresInit(); +extern void AresShutdown(); #define MAX_PLUGINS 128 @@ -109,7 +114,17 @@ void grpc_init(void) { g_shutting_down_cv->SignalAll(); } grpc_iomgr_init(); - grpc_resolver_dns_ares_init(); + if (grpc_core::IsEventEngineDnsEnabled()) { + auto status = AresInit(); + if (!status.ok()) { + VLOG(2) << "AresInit failed: " << status.message(); + } else { + // TODO(yijiem): remove this once we remove the iomgr dns system. + grpc_resolver_dns_ares_reset_dns_resolver(); + } + } else { + grpc_resolver_dns_ares_init(); + } grpc_iomgr_start(); } @@ -122,7 +137,11 @@ void grpc_shutdown_internal_locked(void) grpc_core::ExecCtx exec_ctx(0); grpc_iomgr_shutdown_background_closure(); grpc_timer_manager_set_threading(false); // shutdown timer_manager thread - grpc_resolver_dns_ares_shutdown(); + if (grpc_core::IsEventEngineDnsEnabled()) { + AresShutdown(); + } else { + grpc_resolver_dns_ares_shutdown(); + } grpc_iomgr_shutdown(); } g_shutting_down = false; diff --git a/src/core/resolver/dns/c_ares/dns_resolver_ares.cc b/src/core/resolver/dns/c_ares/dns_resolver_ares.cc index 3bb03ada824..87f4d7f3f24 100644 --- a/src/core/resolver/dns/c_ares/dns_resolver_ares.cc +++ b/src/core/resolver/dns/c_ares/dns_resolver_ares.cc @@ -743,6 +743,13 @@ void grpc_resolver_dns_ares_shutdown() { } } +void grpc_resolver_dns_ares_reset_dns_resolver() { + if (grpc_core::ShouldUseAresDnsResolver( + grpc_core::ConfigVars::Get().DnsResolver())) { + grpc_core::ResetDNSResolver(std::make_unique()); + } +} + #else // GRPC_ARES == 1 namespace grpc_core { @@ -756,4 +763,6 @@ void grpc_resolver_dns_ares_init() {} void grpc_resolver_dns_ares_shutdown() {} +void grpc_resolver_dns_ares_reset_dns_resolver() {} + #endif // GRPC_ARES == 1