Skip to content

Commit 2c3acbb

Browse files
authored
Revert "Add SRV and TXT record lookup methods to the iomgr API (grpc#30078)" (grpc#30176)
This reverts commit c835402.
1 parent 27509c3 commit 2c3acbb

23 files changed

+397
-937
lines changed

BUILD

-2
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,6 @@ grpc_cc_library(
27672767
"cpp_impl_of",
27682768
"debug_location",
27692769
"default_event_engine_factory",
2770-
"default_event_engine_factory_hdrs",
27712770
"dual_ref_counted",
27722771
"error",
27732772
"event_engine_base",
@@ -4732,7 +4731,6 @@ grpc_cc_library(
47324731
"absl/status:statusor",
47334732
"absl/strings",
47344733
"absl/strings:str_format",
4735-
"absl/time",
47364734
"address_sorting",
47374735
"cares",
47384736
],

src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc

+108-372
Large diffs are not rendered by default.

src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc

+90-165
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include <algorithm>
2222
#include <vector>
2323

24-
#include "absl/strings/string_view.h"
25-
2624
#include "src/core/lib/iomgr/sockaddr.h"
2725

2826
// IWYU pragma: no_include <arpa/nameser.h>
@@ -600,10 +598,12 @@ static void grpc_ares_request_unref_locked(grpc_ares_request* r)
600598

601599
void grpc_ares_complete_request_locked(grpc_ares_request* r)
602600
ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
603-
// Invoke on_done callback and destroy the request
601+
/* Invoke on_done callback and destroy the
602+
request */
604603
r->ev_driver = nullptr;
605-
if (r->addresses_out != nullptr && *r->addresses_out != nullptr) {
606-
grpc_cares_wrapper_address_sorting_sort(r, r->addresses_out->get());
604+
ServerAddressList* addresses = r->addresses_out->get();
605+
if (addresses != nullptr) {
606+
grpc_cares_wrapper_address_sorting_sort(r, addresses);
607607
GRPC_ERROR_UNREF(r->error);
608608
r->error = GRPC_ERROR_NONE;
609609
// TODO(apolcyn): allow c-ares to return a service config
@@ -816,7 +816,6 @@ static void on_txt_done_locked(void* arg, int status, int /*timeouts*/,
816816
}
817817
// Clean up.
818818
ares_free_data(reply);
819-
grpc_ares_request_unref_locked(r);
820819
return;
821820
fail:
822821
std::string error_msg =
@@ -828,22 +827,46 @@ static void on_txt_done_locked(void* arg, int status, int /*timeouts*/,
828827
r->error = grpc_error_add_child(error, r->error);
829828
}
830829

831-
grpc_error_handle set_request_dns_server(grpc_ares_request* r,
832-
absl::string_view dns_server)
833-
ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
834-
if (!dns_server.empty()) {
835-
GRPC_CARES_TRACE_LOG("request:%p Using DNS server %s", r,
836-
dns_server.data());
830+
void grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked(
831+
grpc_ares_request* r, const char* dns_server, const char* name,
832+
const char* default_port, grpc_pollset_set* interested_parties,
833+
int query_timeout_ms) ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
834+
grpc_error_handle error = GRPC_ERROR_NONE;
835+
grpc_ares_hostbyname_request* hr = nullptr;
836+
/* parse name, splitting it into host and port parts */
837+
std::string host;
838+
std::string port;
839+
grpc_core::SplitHostPort(name, &host, &port);
840+
if (host.empty()) {
841+
error = grpc_error_set_str(
842+
GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
843+
GRPC_ERROR_STR_TARGET_ADDRESS, name);
844+
goto error_cleanup;
845+
} else if (port.empty()) {
846+
if (default_port == nullptr || strlen(default_port) == 0) {
847+
error = grpc_error_set_str(
848+
GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
849+
GRPC_ERROR_STR_TARGET_ADDRESS, name);
850+
goto error_cleanup;
851+
}
852+
port = default_port;
853+
}
854+
error = grpc_ares_ev_driver_create_locked(&r->ev_driver, interested_parties,
855+
query_timeout_ms, r);
856+
if (!GRPC_ERROR_IS_NONE(error)) goto error_cleanup;
857+
// If dns_server is specified, use it.
858+
if (dns_server != nullptr && dns_server[0] != '\0') {
859+
GRPC_CARES_TRACE_LOG("request:%p Using DNS server %s", r, dns_server);
837860
grpc_resolved_address addr;
838-
if (grpc_parse_ipv4_hostport(dns_server, &addr, /*log_errors=*/false)) {
861+
if (grpc_parse_ipv4_hostport(dns_server, &addr, false /* log_errors */)) {
839862
r->dns_server_addr.family = AF_INET;
840863
struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(addr.addr);
841864
memcpy(&r->dns_server_addr.addr.addr4, &in->sin_addr,
842865
sizeof(struct in_addr));
843866
r->dns_server_addr.tcp_port = grpc_sockaddr_get_port(&addr);
844867
r->dns_server_addr.udp_port = grpc_sockaddr_get_port(&addr);
845868
} else if (grpc_parse_ipv6_hostport(dns_server, &addr,
846-
/*log_errors=*/false)) {
869+
false /* log_errors */)) {
847870
r->dns_server_addr.family = AF_INET6;
848871
struct sockaddr_in6* in6 =
849872
reinterpret_cast<struct sockaddr_in6*>(addr.addr);
@@ -852,49 +875,51 @@ grpc_error_handle set_request_dns_server(grpc_ares_request* r,
852875
r->dns_server_addr.tcp_port = grpc_sockaddr_get_port(&addr);
853876
r->dns_server_addr.udp_port = grpc_sockaddr_get_port(&addr);
854877
} else {
855-
return GRPC_ERROR_CREATE_FROM_CPP_STRING(
856-
absl::StrCat("cannot parse authority ", dns_server));
878+
error = grpc_error_set_str(
879+
GRPC_ERROR_CREATE_FROM_STATIC_STRING("cannot parse authority"),
880+
GRPC_ERROR_STR_TARGET_ADDRESS, name);
881+
goto error_cleanup;
857882
}
858883
int status =
859884
ares_set_servers_ports(r->ev_driver->channel, &r->dns_server_addr);
860885
if (status != ARES_SUCCESS) {
861-
return GRPC_ERROR_CREATE_FROM_CPP_STRING(absl::StrCat(
886+
error = GRPC_ERROR_CREATE_FROM_CPP_STRING(absl::StrCat(
862887
"C-ares status is not ARES_SUCCESS: ", ares_strerror(status)));
888+
goto error_cleanup;
863889
}
864890
}
865-
return GRPC_ERROR_NONE;
866-
}
867-
868-
// Common logic for all lookup methods.
869-
// If an error occurs, callers must run the client callback.
870-
grpc_error_handle grpc_dns_lookup_ares_continued(
871-
grpc_ares_request* r, const char* dns_server, const char* name,
872-
const char* default_port, grpc_pollset_set* interested_parties,
873-
int query_timeout_ms, std::string* host, std::string* port, bool check_port)
874-
ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
875-
grpc_error_handle error = GRPC_ERROR_NONE;
876-
/* parse name, splitting it into host and port parts */
877-
grpc_core::SplitHostPort(name, host, port);
878-
if (host->empty()) {
879-
error = grpc_error_set_str(
880-
GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
881-
GRPC_ERROR_STR_TARGET_ADDRESS, name);
882-
return error;
883-
} else if (check_port && port->empty()) {
884-
if (default_port == nullptr || strlen(default_port) == 0) {
885-
error = grpc_error_set_str(
886-
GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
887-
GRPC_ERROR_STR_TARGET_ADDRESS, name);
888-
return error;
889-
}
890-
*port = default_port;
891+
r->pending_queries = 1;
892+
if (grpc_ares_query_ipv6()) {
893+
hr = create_hostbyname_request_locked(r, host.c_str(),
894+
grpc_strhtons(port.c_str()),
895+
/*is_balancer=*/false, "AAAA");
896+
ares_gethostbyname(r->ev_driver->channel, hr->host, AF_INET6,
897+
on_hostbyname_done_locked, hr);
891898
}
892-
error = grpc_ares_ev_driver_create_locked(&r->ev_driver, interested_parties,
893-
query_timeout_ms, r);
894-
if (!GRPC_ERROR_IS_NONE(error)) return error;
895-
// If dns_server is specified, use it.
896-
error = set_request_dns_server(r, dns_server);
897-
return error;
899+
hr = create_hostbyname_request_locked(r, host.c_str(),
900+
grpc_strhtons(port.c_str()),
901+
/*is_balancer=*/false, "A");
902+
ares_gethostbyname(r->ev_driver->channel, hr->host, AF_INET,
903+
on_hostbyname_done_locked, hr);
904+
if (r->balancer_addresses_out != nullptr) {
905+
/* Query the SRV record */
906+
std::string service_name = absl::StrCat("_grpclb._tcp.", host);
907+
GrpcAresQuery* srv_query = new GrpcAresQuery(r, service_name);
908+
ares_query(r->ev_driver->channel, service_name.c_str(), ns_c_in, ns_t_srv,
909+
on_srv_query_done_locked, srv_query);
910+
}
911+
if (r->service_config_json_out != nullptr) {
912+
std::string config_name = absl::StrCat("_grpc_config.", host);
913+
GrpcAresQuery* txt_query = new GrpcAresQuery(r, config_name);
914+
ares_search(r->ev_driver->channel, config_name.c_str(), ns_c_in, ns_t_txt,
915+
on_txt_done_locked, txt_query);
916+
}
917+
grpc_ares_ev_driver_start_locked(r->ev_driver);
918+
grpc_ares_request_unref_locked(r);
919+
return;
920+
921+
error_cleanup:
922+
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
898923
}
899924

900925
static bool inner_resolve_as_ip_literal_locked(
@@ -1026,18 +1051,21 @@ static bool grpc_ares_maybe_resolve_localhost_manually_locked(
10261051
}
10271052
#endif /* GRPC_ARES_RESOLVE_LOCALHOST_MANUALLY */
10281053

1029-
static grpc_ares_request* grpc_dns_lookup_hostname_ares_impl(
1054+
static grpc_ares_request* grpc_dns_lookup_ares_impl(
10301055
const char* dns_server, const char* name, const char* default_port,
10311056
grpc_pollset_set* interested_parties, grpc_closure* on_done,
10321057
std::unique_ptr<grpc_core::ServerAddressList>* addrs,
1033-
int query_timeout_ms) {
1058+
std::unique_ptr<grpc_core::ServerAddressList>* balancer_addrs,
1059+
char** service_config_json, int query_timeout_ms) {
10341060
grpc_ares_request* r = new grpc_ares_request();
10351061
grpc_core::MutexLock lock(&r->mu);
10361062
r->ev_driver = nullptr;
10371063
r->on_done = on_done;
10381064
r->addresses_out = addrs;
1065+
r->balancer_addresses_out = balancer_addrs;
1066+
r->service_config_json_out = service_config_json;
10391067
GRPC_CARES_TRACE_LOG(
1040-
"request:%p c-ares grpc_dns_lookup_hostname_ares_impl name=%s, "
1068+
"request:%p c-ares grpc_dns_lookup_ares_impl name=%s, "
10411069
"default_port=%s",
10421070
r, name, default_port);
10431071
// Early out if the target is an ipv4 or ipv6 literal.
@@ -1051,129 +1079,26 @@ static grpc_ares_request* grpc_dns_lookup_hostname_ares_impl(
10511079
grpc_ares_complete_request_locked(r);
10521080
return r;
10531081
}
1054-
// Look up name using c-ares lib.
1055-
std::string host;
1056-
std::string port;
1057-
grpc_error_handle error = grpc_dns_lookup_ares_continued(
1058-
r, dns_server, name, default_port, interested_parties, query_timeout_ms,
1059-
&host, &port, true);
1060-
if (!GRPC_ERROR_IS_NONE(error)) {
1061-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
1062-
return r;
1063-
}
1064-
r->pending_queries = 1;
1065-
grpc_ares_hostbyname_request* hr = nullptr;
1066-
if (grpc_ares_query_ipv6()) {
1067-
hr = create_hostbyname_request_locked(r, host.c_str(),
1068-
grpc_strhtons(port.c_str()),
1069-
/*is_balancer=*/false, "AAAA");
1070-
ares_gethostbyname(r->ev_driver->channel, hr->host, AF_INET6,
1071-
on_hostbyname_done_locked, hr);
1072-
}
1073-
hr = create_hostbyname_request_locked(r, host.c_str(),
1074-
grpc_strhtons(port.c_str()),
1075-
/*is_balancer=*/false, "A");
1076-
ares_gethostbyname(r->ev_driver->channel, hr->host, AF_INET,
1077-
on_hostbyname_done_locked, hr);
1078-
grpc_ares_ev_driver_start_locked(r->ev_driver);
1079-
grpc_ares_request_unref_locked(r);
1080-
return r;
1081-
}
1082-
1083-
grpc_ares_request* grpc_dns_lookup_srv_ares_impl(
1084-
const char* dns_server, const char* name,
1085-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1086-
std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
1087-
int query_timeout_ms) {
1088-
grpc_ares_request* r = new grpc_ares_request();
1089-
grpc_core::MutexLock lock(&r->mu);
1090-
r->ev_driver = nullptr;
1091-
r->on_done = on_done;
1092-
r->balancer_addresses_out = balancer_addresses;
1093-
GRPC_CARES_TRACE_LOG(
1094-
"request:%p c-ares grpc_dns_lookup_srv_ares_impl name=%s", r, name);
1095-
grpc_error_handle error = GRPC_ERROR_NONE;
1096-
// Don't query for SRV records if the target is "localhost"
1097-
if (target_matches_localhost(name)) {
1098-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
1099-
return r;
1100-
}
1101-
// Look up name using c-ares lib.
1102-
std::string host;
1103-
std::string port;
1104-
error = grpc_dns_lookup_ares_continued(r, dns_server, name, nullptr,
1105-
interested_parties, query_timeout_ms,
1106-
&host, &port, false);
1107-
if (!GRPC_ERROR_IS_NONE(error)) {
1108-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
1109-
return r;
1110-
}
1111-
r->pending_queries = 1;
1112-
/* Query the SRV record */
1113-
std::string service_name = absl::StrCat("_grpclb._tcp.", host);
1114-
GrpcAresQuery* srv_query = new GrpcAresQuery(r, service_name);
1115-
ares_query(r->ev_driver->channel, service_name.c_str(), ns_c_in, ns_t_srv,
1116-
on_srv_query_done_locked, srv_query);
1117-
grpc_ares_ev_driver_start_locked(r->ev_driver);
1118-
grpc_ares_request_unref_locked(r);
1119-
return r;
1120-
}
1121-
1122-
grpc_ares_request* grpc_dns_lookup_txt_ares_impl(
1123-
const char* dns_server, const char* name,
1124-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1125-
char** service_config_json, int query_timeout_ms) {
1126-
grpc_ares_request* r = new grpc_ares_request();
1127-
grpc_core::MutexLock lock(&r->mu);
1128-
r->ev_driver = nullptr;
1129-
r->on_done = on_done;
1130-
r->service_config_json_out = service_config_json;
1131-
GRPC_CARES_TRACE_LOG(
1132-
"request:%p c-ares grpc_dns_lookup_txt_ares_impl name=%s", r, name);
1133-
grpc_error_handle error = GRPC_ERROR_NONE;
1134-
// Don't query for TXT records if the target is "localhost"
1082+
// Don't query for SRV and TXT records if the target is "localhost", so
1083+
// as to cut down on lookups over the network, especially in tests:
1084+
// https://github.com/grpc/proposal/pull/79
11351085
if (target_matches_localhost(name)) {
1136-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
1137-
return r;
1086+
r->balancer_addresses_out = nullptr;
1087+
r->service_config_json_out = nullptr;
11381088
}
11391089
// Look up name using c-ares lib.
1140-
std::string host;
1141-
std::string port;
1142-
error = grpc_dns_lookup_ares_continued(r, dns_server, name, nullptr,
1143-
interested_parties, query_timeout_ms,
1144-
&host, &port, false);
1145-
if (!GRPC_ERROR_IS_NONE(error)) {
1146-
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, error);
1147-
return r;
1148-
}
1149-
r->pending_queries = 1;
1150-
/* Query the TXT record */
1151-
std::string config_name = absl::StrCat("_grpc_config.", host);
1152-
GrpcAresQuery* txt_query = new GrpcAresQuery(r, config_name);
1153-
ares_search(r->ev_driver->channel, config_name.c_str(), ns_c_in, ns_t_txt,
1154-
on_txt_done_locked, txt_query);
1155-
grpc_ares_ev_driver_start_locked(r->ev_driver);
1156-
grpc_ares_request_unref_locked(r);
1090+
grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked(
1091+
r, dns_server, name, default_port, interested_parties, query_timeout_ms);
11571092
return r;
11581093
}
11591094

1160-
grpc_ares_request* (*grpc_dns_lookup_hostname_ares)(
1095+
grpc_ares_request* (*grpc_dns_lookup_ares)(
11611096
const char* dns_server, const char* name, const char* default_port,
11621097
grpc_pollset_set* interested_parties, grpc_closure* on_done,
11631098
std::unique_ptr<grpc_core::ServerAddressList>* addrs,
1164-
int query_timeout_ms) = grpc_dns_lookup_hostname_ares_impl;
1165-
1166-
grpc_ares_request* (*grpc_dns_lookup_srv_ares)(
1167-
const char* dns_server, const char* name,
1168-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1169-
std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
1170-
int query_timeout_ms) = grpc_dns_lookup_srv_ares_impl;
1171-
1172-
grpc_ares_request* (*grpc_dns_lookup_txt_ares)(
1173-
const char* dns_server, const char* name,
1174-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1099+
std::unique_ptr<grpc_core::ServerAddressList>* balancer_addrs,
11751100
char** service_config_json,
1176-
int query_timeout_ms) = grpc_dns_lookup_txt_ares_impl;
1101+
int query_timeout_ms) = grpc_dns_lookup_ares_impl;
11771102

11781103
static void grpc_cancel_ares_request_impl(grpc_ares_request* r) {
11791104
GPR_ASSERT(r != nullptr);

src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h

+7-20
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,23 @@ struct grpc_ares_request {
7878
grpc_error_handle error ABSL_GUARDED_BY(mu) = GRPC_ERROR_NONE;
7979
};
8080

81-
/* Asynchronously resolve \a name (A/AAAA records only).
82-
It uses \a default_port if a port isn't designated in \a name, otherwise it
83-
uses the port in \a name. grpc_ares_init() must be called at least once before
84-
this function. The returned grpc_ares_request object is owned by the caller
85-
and it is safe to free after on_done is called back.
81+
/* Asynchronously resolve \a name. It will try to resolve grpclb SRV records in
82+
addition to the normal address records. For normal address records, it uses
83+
\a default_port if a port isn't designated in \a name, otherwise it uses the
84+
port in \a name. grpc_ares_init() must be called at least once before this
85+
function. The returned grpc_ares_request object is owned by the caller and it
86+
is safe to free after on_done is called back.
8687
8788
Note on synchronization: \a as on_done might be called from another thread
8889
~immediately, access to the grpc_ares_request* return value must be
8990
synchronized by the caller. TODO(apolcyn): we should remove this requirement
9091
by changing this API to use two phase initialization - one API to create
9192
the grpc_ares_request* and another to start the async work. */
92-
extern grpc_ares_request* (*grpc_dns_lookup_hostname_ares)(
93+
extern grpc_ares_request* (*grpc_dns_lookup_ares)(
9394
const char* dns_server, const char* name, const char* default_port,
9495
grpc_pollset_set* interested_parties, grpc_closure* on_done,
9596
std::unique_ptr<grpc_core::ServerAddressList>* addresses,
96-
int query_timeout_ms);
97-
98-
// Asynchronously resolve a SRV record.
99-
// See \a grpc_dns_lookup_hostname_ares for usage details and caveats.
100-
extern grpc_ares_request* (*grpc_dns_lookup_srv_ares)(
101-
const char* dns_server, const char* name,
102-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
10397
std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
104-
int query_timeout_ms);
105-
106-
// Asynchronously resolve a TXT record.
107-
// See \a grpc_dns_lookup_hostname_ares for usage details and caveats.
108-
extern grpc_ares_request* (*grpc_dns_lookup_txt_ares)(
109-
const char* dns_server, const char* name,
110-
grpc_pollset_set* interested_parties, grpc_closure* on_done,
11198
char** service_config_json, int query_timeout_ms);
11299

113100
/* Cancel the pending grpc_ares_request \a request */

0 commit comments

Comments
 (0)