blob: ba82675275048a921d186795d0ed86fe68a6d661 [file] [log] [blame]
Craig Tillercd29c582015-06-24 09:15:15 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "src/core/client_config/resolvers/dns_resolver.h"
35
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070036#include <string.h>
37
38#include <grpc/support/alloc.h>
39#include <grpc/support/string_util.h>
40
Craig Tillereb3b12e2015-06-26 14:42:49 -070041#include "src/core/client_config/lb_policies/pick_first.h"
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070042#include "src/core/iomgr/resolve_address.h"
43#include "src/core/support/string.h"
44
45typedef struct {
46 /** base class: must be first */
47 grpc_resolver base;
48 /** refcount */
49 gpr_refcount refs;
50 /** name to resolve */
51 char *name;
52 /** default port to use */
53 char *default_port;
54 /** subchannel factory */
55 grpc_subchannel_factory *subchannel_factory;
56 /** load balancing policy factory */
57 grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels,
58 size_t num_subchannels);
59
60 /** mutex guarding the rest of the state */
61 gpr_mu mu;
62 /** are we currently resolving? */
63 int resolving;
64 /** which version of resolved_config have we published? */
65 int published_version;
66 /** which version of resolved_config is current? */
67 int resolved_version;
68 /** pending next completion, or NULL */
69 grpc_iomgr_closure *next_completion;
70 /** target config address for next completion */
71 grpc_client_config **target_config;
72 /** current (fully resolved) config */
73 grpc_client_config *resolved_config;
74} dns_resolver;
75
76static void dns_destroy(dns_resolver *r);
77
78static void dns_start_resolving_locked(dns_resolver *r);
79static void dns_maybe_finish_next_locked(dns_resolver *r);
80
81static void dns_ref(grpc_resolver *r);
82static void dns_unref(grpc_resolver *r);
83static void dns_shutdown(grpc_resolver *r);
84static void dns_channel_saw_error(grpc_resolver *r,
85 struct sockaddr *failing_address,
86 int failing_address_len);
87static void dns_next(grpc_resolver *r, grpc_client_config **target_config,
88 grpc_iomgr_closure *on_complete);
89
90static const grpc_resolver_vtable dns_resolver_vtable = {
91 dns_ref, dns_unref, dns_shutdown, dns_channel_saw_error, dns_next};
92
93static void dns_ref(grpc_resolver *resolver) {
94 dns_resolver *r = (dns_resolver *)resolver;
95 gpr_ref(&r->refs);
96}
97
98static void dns_unref(grpc_resolver *resolver) {
99 dns_resolver *r = (dns_resolver *)resolver;
100 if (gpr_unref(&r->refs)) {
101 dns_destroy(r);
102 }
103}
104
105static void dns_shutdown(grpc_resolver *resolver) {
106 dns_resolver *r = (dns_resolver *)resolver;
107 gpr_mu_lock(&r->mu);
108 if (r->next_completion != NULL) {
109 *r->target_config = NULL;
110 /* TODO(ctiller): add delayed callback */
111 grpc_iomgr_add_callback(r->next_completion);
112 r->next_completion = NULL;
113 }
114 gpr_mu_unlock(&r->mu);
115}
116
117static void dns_channel_saw_error(grpc_resolver *resolver, struct sockaddr *sa,
118 int len) {
119 dns_resolver *r = (dns_resolver *)resolver;
120 gpr_mu_lock(&r->mu);
121 if (!r->resolving) {
122 dns_start_resolving_locked(r);
123 }
124 gpr_mu_unlock(&r->mu);
125}
126
127static void dns_next(grpc_resolver *resolver,
128 grpc_client_config **target_config,
129 grpc_iomgr_closure *on_complete) {
130 dns_resolver *r = (dns_resolver *)resolver;
131 gpr_mu_lock(&r->mu);
132 GPR_ASSERT(!r->next_completion);
133 r->next_completion = on_complete;
134 r->target_config = target_config;
135 if (r->resolved_version == 0 && !r->resolving) {
136 dns_start_resolving_locked(r);
137 } else {
138 dns_maybe_finish_next_locked(r);
139 }
140 gpr_mu_unlock(&r->mu);
141}
142
143static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) {
144 dns_resolver *r = arg;
145 grpc_client_config *config = NULL;
146 grpc_subchannel **subchannels;
147 grpc_subchannel_args args;
148 size_t i;
149 if (addresses) {
150 config = grpc_client_config_create();
151 subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs);
152 for (i = 0; i < addresses->naddrs; i++) {
153 memset(&args, 0, sizeof(args));
154 args.addr = (struct sockaddr *)(addresses->addrs[i].addr);
155 args.addr_len = addresses->addrs[i].len;
156 subchannels[i] = grpc_subchannel_factory_create_subchannel(
157 r->subchannel_factory, &args);
158 }
159 grpc_client_config_set_lb_policy(
160 config, r->lb_policy_factory(subchannels, addresses->naddrs));
161 }
162 gpr_mu_lock(&r->mu);
163 if (r->resolved_config) {
164 grpc_client_config_unref(r->resolved_config);
165 }
166 r->resolved_config = config;
167 r->resolved_version++;
168 dns_maybe_finish_next_locked(r);
169 gpr_mu_unlock(&r->mu);
170}
171
172static void dns_start_resolving_locked(dns_resolver *r) {
173 dns_ref(&r->base);
174 r->resolving = 1;
175 grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r);
176}
177
178static void dns_maybe_finish_next_locked(dns_resolver *r) {
179 if (r->next_completion != NULL &&
180 r->resolved_version != r->published_version) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700181 *r->target_config = r->resolved_config;
182 grpc_client_config_ref(r->resolved_config);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700183 grpc_iomgr_add_callback(r->next_completion);
184 r->next_completion = NULL;
185 r->published_version = r->resolved_version;
186 }
187}
188
189static void dns_destroy(dns_resolver *r) {
190 gpr_mu_destroy(&r->mu);
191 grpc_subchannel_factory_unref(r->subchannel_factory);
192 gpr_free(r->name);
193 gpr_free(r->default_port);
194 gpr_free(r);
195}
196
Craig Tillereb3b12e2015-06-26 14:42:49 -0700197static grpc_resolver *dns_create(
198 grpc_uri *uri, const char *default_port,
199 grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels,
200 size_t num_subchannels),
201 grpc_subchannel_factory *subchannel_factory) {
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700202 dns_resolver *r;
203 const char *path = uri->path;
204
205 if (0 != strcmp(uri->authority, "")) {
206 gpr_log(GPR_ERROR, "authority based uri's not supported");
207 return NULL;
208 }
209
210 if (path[0] == '/') ++path;
211
212 r = gpr_malloc(sizeof(dns_resolver));
213 memset(r, 0, sizeof(*r));
214 gpr_ref_init(&r->refs, 1);
215 gpr_mu_init(&r->mu);
216 r->base.vtable = &dns_resolver_vtable;
217 r->name = gpr_strdup(path);
218 r->default_port = gpr_strdup(default_port);
219 r->subchannel_factory = subchannel_factory;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700220 r->lb_policy_factory = lb_policy_factory;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700221 grpc_subchannel_factory_ref(subchannel_factory);
222 return &r->base;
223}
224
225/*
226 * FACTORY
227 */
228
Craig Tillereb3b12e2015-06-26 14:42:49 -0700229static void dns_factory_ref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700230
Craig Tillereb3b12e2015-06-26 14:42:49 -0700231static void dns_factory_unref(grpc_resolver_factory *factory) {}
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700232
233static grpc_resolver *dns_factory_create_resolver(
234 grpc_resolver_factory *factory, grpc_uri *uri,
235 grpc_subchannel_factory *subchannel_factory) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700236 return dns_create(uri, "https", grpc_create_pick_first_lb_policy,
237 subchannel_factory);
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700238}
239
240static const grpc_resolver_factory_vtable dns_factory_vtable = {
241 dns_factory_ref, dns_factory_unref, dns_factory_create_resolver};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700242static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable};
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700243
Craig Tillereb3b12e2015-06-26 14:42:49 -0700244grpc_resolver_factory *grpc_dns_resolver_factory_create() {
245 return &dns_resolver_factory;
Craig Tiller3bc8ebd2015-06-24 15:41:15 -0700246}