[EventEngine] Respect the `dns_resolver` config var in windows engine (#36901)

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #36901

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36901 from yijiem:ares-windows-fix 7c16a8a741
PiperOrigin-RevId: 645182157
pull/36946/head
Yijie Ma 10 months ago committed by Copybara-Service
parent f33083bfd5
commit b51d61987b
  1. 2
      src/core/BUILD
  2. 47
      src/core/lib/event_engine/ares_resolver.cc
  3. 8
      src/core/lib/event_engine/ares_resolver.h
  4. 10
      src/core/lib/event_engine/posix_engine/posix_engine.cc
  5. 23
      src/core/lib/event_engine/windows/windows_engine.cc
  6. 23
      src/core/lib/surface/init.cc
  7. 9
      src/core/resolver/dns/c_ares/dns_resolver_ares.cc

@ -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",

@ -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

@ -16,6 +16,8 @@
#include <utility>
#include "absl/status/status.h"
#include <grpc/support/port_platform.h>
#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

@ -38,7 +38,6 @@
#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#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

@ -322,21 +322,24 @@ void WindowsEventEngine::WindowsDNSResolver::LookupTXT(
absl::StatusOr<std::unique_ptr<EventEngine::DNSResolver>>
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<GrpcPolledFdFactoryWindows>(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<GrpcPolledFdFactoryWindows>(poller()),
shared_from_this());
if (!ares_resolver.ok()) {
return ares_resolver.status();
}
return std::make_unique<WindowsEventEngine::WindowsDNSResolver>(
std::move(*ares_resolver));
#endif // GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER)
}
return std::make_unique<WindowsEventEngine::WindowsDNSResolver>(
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<NativeWindowsDNSResolver>(shared_from_this());
#endif // GRPC_ARES == 1 && defined(GRPC_WINDOWS_SOCKET_ARES_EV_DRIVER)
}
bool WindowsEventEngine::IsWorkerThread() { grpc_core::Crash("unimplemented"); }

@ -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;

@ -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<grpc_core::AresDNSResolver>());
}
}
#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

Loading…
Cancel
Save