Address review comments

reviewable/pr7771/r1
Yuchen Zeng 8 years ago
parent 4eedcdedf5
commit 38f34fbe54
  1. 116
      src/core/ext/resolver/dns/c_ares/dns_resolver_ares.c
  2. 2
      src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h

@ -55,16 +55,12 @@
typedef struct {
/** base class: must be first */
grpc_resolver base;
/** refcount */
gpr_refcount refs;
/** target name */
char *target_name;
/** name to resolve (usually the same as target_name) */
char *name_to_resolve;
/** default port to use */
char *default_port;
/** load balancing policy name */
char *lb_policy_name;
/** mutex guarding the rest of the state */
gpr_mu mu;
@ -92,24 +88,27 @@ typedef struct {
grpc_polling_entity *pollent;
} dns_resolver;
static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
static void dns_start_resolving_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r);
static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r);
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r);
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r);
static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
grpc_polling_entity *pollent,
grpc_resolver_result **target_result,
grpc_closure *on_complete);
static void dns_ares_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
static void dns_ares_channel_saw_error(grpc_exec_ctx *exec_ctx,
grpc_resolver *r);
static void dns_ares_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
grpc_polling_entity *pollent,
grpc_resolver_result **target_result,
grpc_closure *on_complete);
static const grpc_resolver_vtable dns_resolver_vtable = {
dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next};
dns_ares_destroy, dns_ares_shutdown, dns_ares_channel_saw_error,
dns_ares_next};
static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) {
static void dns_ares_shutdown(grpc_exec_ctx *exec_ctx,
grpc_resolver *resolver) {
dns_resolver *r = (dns_resolver *)resolver;
gpr_mu_lock(&r->mu);
if (r->have_retry_timer) {
@ -124,35 +123,33 @@ static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) {
gpr_mu_unlock(&r->mu);
}
static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx,
grpc_resolver *resolver) {
static void dns_ares_channel_saw_error(grpc_exec_ctx *exec_ctx,
grpc_resolver *resolver) {
dns_resolver *r = (dns_resolver *)resolver;
gpr_mu_lock(&r->mu);
if (!r->resolving) {
gpr_backoff_reset(&r->backoff_state);
dns_start_resolving_locked(exec_ctx, r);
dns_ares_start_resolving_locked(exec_ctx, r);
}
gpr_mu_unlock(&r->mu);
}
static void dns_on_retry_timer(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
static void dns_ares_on_retry_timer(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
dns_resolver *r = arg;
gpr_mu_lock(&r->mu);
r->have_retry_timer = false;
if (error == GRPC_ERROR_NONE) {
if (!r->resolving) {
dns_start_resolving_locked(exec_ctx, r);
dns_ares_start_resolving_locked(exec_ctx, r);
}
}
gpr_mu_unlock(&r->mu);
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "retry-timer");
}
static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
static void dns_ares_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
dns_resolver *r = arg;
grpc_resolver_result *result = NULL;
gpr_mu_lock(&r->mu);
@ -169,8 +166,7 @@ static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
}
grpc_resolved_addresses_destroy(r->addresses);
result = grpc_resolver_result_create(r->target_name, addresses,
r->lb_policy_name, NULL);
;
NULL /* lb_policy_name */, NULL);
if (r->pollent) {
grpc_polling_entity_del_from_pollset_set(exec_ctx, r->pollent,
r->base.pollset_set);
@ -192,23 +188,23 @@ static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
} else {
gpr_log(GPR_DEBUG, "retrying immediately");
}
grpc_timer_init(exec_ctx, &r->retry_timer, next_try, dns_on_retry_timer, r,
now);
grpc_timer_init(exec_ctx, &r->retry_timer, next_try,
dns_ares_on_retry_timer, r, now);
}
if (r->resolved_result) {
grpc_resolver_result_unref(exec_ctx, r->resolved_result);
}
r->resolved_result = result;
r->resolved_version++;
dns_maybe_finish_next_locked(exec_ctx, r);
dns_ares_maybe_finish_next_locked(exec_ctx, r);
gpr_mu_unlock(&r->mu);
GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving");
}
static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
grpc_polling_entity *pollent,
grpc_resolver_result **target_result,
grpc_closure *on_complete) {
static void dns_ares_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
grpc_polling_entity *pollent,
grpc_resolver_result **target_result,
grpc_closure *on_complete) {
dns_resolver *r = (dns_resolver *)resolver;
gpr_mu_lock(&r->mu);
GPR_ASSERT(!r->next_completion);
@ -226,30 +222,30 @@ static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
grpc_polling_entity_add_to_pollset_set(exec_ctx, pollent,
r->base.pollset_set);
} else {
gpr_log(GPR_DEBUG, "dns_next is called without giving a pollent");
gpr_log(GPR_DEBUG, "dns_ares_next is called without giving a pollent");
}
grpc_resolve_address_ares(
exec_ctx, r->name_to_resolve, r->default_port, r->base.pollset_set,
grpc_closure_create(dns_on_resolved, r), &r->addresses);
grpc_closure_create(dns_ares_on_resolved, r), &r->addresses);
} else {
dns_maybe_finish_next_locked(exec_ctx, r);
dns_ares_maybe_finish_next_locked(exec_ctx, r);
}
gpr_mu_unlock(&r->mu);
}
static void dns_start_resolving_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r) {
static void dns_ares_start_resolving_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r) {
GRPC_RESOLVER_REF(&r->base, "dns-resolving");
GPR_ASSERT(!r->resolving);
r->resolving = true;
r->addresses = NULL;
grpc_resolve_address_ares(
exec_ctx, r->name_to_resolve, r->default_port, r->base.pollset_set,
grpc_closure_create(dns_on_resolved, r), &r->addresses);
grpc_closure_create(dns_ares_on_resolved, r), &r->addresses);
}
static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r) {
static void dns_ares_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
dns_resolver *r) {
if (r->next_completion != NULL &&
r->resolved_version != r->published_version) {
*r->target_result = r->resolved_result;
@ -262,7 +258,7 @@ static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
}
}
static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
static void dns_ares_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
dns_resolver *r = (dns_resolver *)gr;
gpr_mu_destroy(&r->mu);
grpc_ares_cleanup();
@ -272,13 +268,11 @@ static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
gpr_free(r->target_name);
gpr_free(r->name_to_resolve);
gpr_free(r->default_port);
gpr_free(r->lb_policy_name);
gpr_free(r);
}
static grpc_resolver *dns_create(grpc_resolver_args *args,
const char *default_port,
const char *lb_policy_name) {
static grpc_resolver *dns_ares_create(grpc_resolver_args *args,
const char *default_port) {
dns_resolver *r;
grpc_error *error = GRPC_ERROR_NONE;
char *proxy_name;
@ -303,7 +297,6 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
// Create resolver.
r = gpr_malloc(sizeof(dns_resolver));
memset(r, 0, sizeof(*r));
gpr_ref_init(&r->refs, 1);
gpr_mu_init(&r->mu);
grpc_resolver_init(&r->base, &dns_resolver_vtable);
r->target_name = gpr_strdup(path);
@ -311,7 +304,6 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
r->default_port = gpr_strdup(default_port);
gpr_backoff_init(&r->backoff_state, BACKOFF_MULTIPLIER, BACKOFF_JITTER,
BACKOFF_MIN_SECONDS * 1000, BACKOFF_MAX_SECONDS * 1000);
r->lb_policy_name = gpr_strdup(lb_policy_name);
return &r->base;
}
@ -319,33 +311,33 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
* FACTORY
*/
static void dns_factory_ref(grpc_resolver_factory *factory) {}
static void dns_ares_factory_ref(grpc_resolver_factory *factory) {}
static void dns_factory_unref(grpc_resolver_factory *factory) {}
static void dns_ares_factory_unref(grpc_resolver_factory *factory) {}
static grpc_resolver *dns_factory_create_resolver(
grpc_resolver_factory *factory, grpc_resolver_args *args) {
return dns_create(args, "https", "pick_first");
return dns_ares_create(args, "https");
}
static char *dns_factory_get_default_host_name(grpc_resolver_factory *factory,
grpc_uri *uri) {
static char *dns_ares_factory_get_default_host_name(
grpc_resolver_factory *factory, grpc_uri *uri) {
const char *path = uri->path;
if (path[0] == '/') ++path;
return gpr_strdup(path);
}
static const grpc_resolver_factory_vtable dns_factory_vtable = {
dns_factory_ref, dns_factory_unref, dns_factory_create_resolver,
dns_factory_get_default_host_name, "dns"};
static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable};
static const grpc_resolver_factory_vtable dns_ares_factory_vtable = {
dns_ares_factory_ref, dns_ares_factory_unref, dns_factory_create_resolver,
dns_ares_factory_get_default_host_name, "dns"};
static grpc_resolver_factory dns_resolver_factory = {&dns_ares_factory_vtable};
static grpc_resolver_factory *dns_resolver_factory_create() {
static grpc_resolver_factory *dns_ares_resolver_factory_create() {
return &dns_resolver_factory;
}
void grpc_resolver_dns_ares_init(void) {
grpc_register_resolver_type(dns_resolver_factory_create());
grpc_register_resolver_type(dns_ares_resolver_factory_create());
}
void grpc_resolver_dns_ares_shutdown(void) {}

@ -44,7 +44,7 @@
typedef struct grpc_ares_ev_driver grpc_ares_ev_driver;
/* Strat \a ev_driver. It will keep working until all IO on its ares_channel is
/* Start \a ev_driver. It will keep working until all IO on its ares_channel is
done, or grpc_ares_ev_driver_destroy() is called. It may notify the callbacks
bound to its ares_channel when necessary. */
void grpc_ares_ev_driver_start(grpc_exec_ctx *exec_ctx,

Loading…
Cancel
Save