blob: deb4b9b1ef1380667d2912c2d21b71eec51feb9f [file] [log] [blame]
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -07004 * 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
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070034#include <string.h>
35
36#include <grpc/support/alloc.h>
37#include <grpc/support/string_util.h>
38
Hongwei Wanga3780a82015-07-17 15:27:18 -070039#include <grpc/grpc_zookeeper.h>
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070040#include <zookeeper/zookeeper.h>
41
Craig Tillerd4c98332016-03-31 13:45:47 -070042#include "src/core/ext/client_config/lb_policy_registry.h"
43#include "src/core/ext/client_config/resolver_registry.h"
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/iomgr/resolve_address.h"
45#include "src/core/lib/json/json.h"
46#include "src/core/lib/support/string.h"
47#include "src/core/lib/surface/api_trace.h"
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070048
Hongwei Wang35d5a0f2015-07-24 11:14:23 -070049/** Zookeeper session expiration time in milliseconds */
50#define GRPC_ZOOKEEPER_SESSION_TIMEOUT 15000
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070051
Craig Tillera82950e2015-09-22 12:33:20 -070052typedef struct {
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070053 /** base class: must be first */
54 grpc_resolver base;
55 /** refcount */
56 gpr_refcount refs;
57 /** name to resolve */
58 char *name;
59 /** subchannel factory */
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070060 grpc_client_channel_factory *client_channel_factory;
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070061 /** load balancing policy name */
62 char *lb_policy_name;
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070063
64 /** mutex guarding the rest of the state */
65 gpr_mu mu;
66 /** are we currently resolving? */
67 int resolving;
68 /** which version of resolved_config have we published? */
69 int published_version;
70 /** which version of resolved_config is current? */
71 int resolved_version;
72 /** pending next completion, or NULL */
Craig Tiller33825112015-09-18 07:44:19 -070073 grpc_closure *next_completion;
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070074 /** target config address for next completion */
75 grpc_client_config **target_config;
76 /** current (fully resolved) config */
77 grpc_client_config *resolved_config;
78
79 /** zookeeper handle */
80 zhandle_t *zookeeper_handle;
Hongwei Wangd65009d2015-07-14 17:01:49 -070081 /** zookeeper resolved addresses */
Hongwei Wangcd620752015-07-22 15:07:24 -070082 grpc_resolved_addresses *resolved_addrs;
Hongwei Wangc4cdbcd2015-07-16 11:36:39 -070083 /** total number of addresses to be resolved */
Hongwei Wang85fd2f72015-07-09 17:38:17 -070084 int resolved_total;
Hongwei Wang6e732ea2015-07-27 16:41:54 -070085 /** number of addresses resolved */
Hongwei Wang85fd2f72015-07-09 17:38:17 -070086 int resolved_num;
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070087} zookeeper_resolver;
88
Craig Tiller6ecd8ad2015-09-22 17:01:34 -070089static void zookeeper_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070090
Craig Tillera82950e2015-09-22 12:33:20 -070091static void zookeeper_start_resolving_locked(zookeeper_resolver *r);
Craig Tiller6ecd8ad2015-09-22 17:01:34 -070092static void zookeeper_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
93 zookeeper_resolver *r);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -070094
Craig Tiller6ecd8ad2015-09-22 17:01:34 -070095static void zookeeper_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
96static void zookeeper_channel_saw_error(grpc_exec_ctx *exec_ctx,
Craig Tillerdc036682015-12-02 08:43:37 -080097 grpc_resolver *r);
Craig Tiller6ecd8ad2015-09-22 17:01:34 -070098static void zookeeper_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
99 grpc_client_config **target_config,
Craig Tillera82950e2015-09-22 12:33:20 -0700100 grpc_closure *on_complete);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700101
Craig Tillera82950e2015-09-22 12:33:20 -0700102static const grpc_resolver_vtable zookeeper_resolver_vtable = {
103 zookeeper_destroy, zookeeper_shutdown, zookeeper_channel_saw_error,
104 zookeeper_next};
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700105
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700106static void zookeeper_shutdown(grpc_exec_ctx *exec_ctx,
107 grpc_resolver *resolver) {
Craig Tillera82950e2015-09-22 12:33:20 -0700108 zookeeper_resolver *r = (zookeeper_resolver *)resolver;
Craig Tiller33825112015-09-18 07:44:19 -0700109 grpc_closure *call = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700110 gpr_mu_lock(&r->mu);
111 if (r->next_completion != NULL) {
112 *r->target_config = NULL;
113 call = r->next_completion;
114 r->next_completion = NULL;
115 }
116 zookeeper_close(r->zookeeper_handle);
117 gpr_mu_unlock(&r->mu);
118 if (call != NULL) {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700119 call->cb(exec_ctx, call->cb_arg, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700120 }
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700121}
122
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700123static void zookeeper_channel_saw_error(grpc_exec_ctx *exec_ctx,
Craig Tillerdc036682015-12-02 08:43:37 -0800124 grpc_resolver *resolver) {
Craig Tillera82950e2015-09-22 12:33:20 -0700125 zookeeper_resolver *r = (zookeeper_resolver *)resolver;
126 gpr_mu_lock(&r->mu);
127 if (r->resolving == 0) {
128 zookeeper_start_resolving_locked(r);
129 }
130 gpr_mu_unlock(&r->mu);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700131}
132
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700133static void zookeeper_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
Craig Tillera82950e2015-09-22 12:33:20 -0700134 grpc_client_config **target_config,
135 grpc_closure *on_complete) {
136 zookeeper_resolver *r = (zookeeper_resolver *)resolver;
Craig Tillera82950e2015-09-22 12:33:20 -0700137 gpr_mu_lock(&r->mu);
138 GPR_ASSERT(r->next_completion == NULL);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700139 r->next_completion = on_complete;
140 r->target_config = target_config;
Craig Tillera82950e2015-09-22 12:33:20 -0700141 if (r->resolved_version == 0 && r->resolving == 0) {
142 zookeeper_start_resolving_locked(r);
143 } else {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700144 zookeeper_maybe_finish_next_locked(exec_ctx, r);
Craig Tillera82950e2015-09-22 12:33:20 -0700145 }
146 gpr_mu_unlock(&r->mu);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700147}
148
Craig Tillerd6c98df2015-08-18 09:33:44 -0700149/** Zookeeper global watcher for connection management
Hongwei Wang85ad6852015-08-13 16:13:10 -0700150 TODO: better connection management besides logs */
Craig Tillera82950e2015-09-22 12:33:20 -0700151static void zookeeper_global_watcher(zhandle_t *zookeeper_handle, int type,
152 int state, const char *path,
153 void *watcher_ctx) {
154 if (type == ZOO_SESSION_EVENT) {
155 if (state == ZOO_EXPIRED_SESSION_STATE) {
156 gpr_log(GPR_ERROR, "Zookeeper session expired");
157 } else if (state == ZOO_AUTH_FAILED_STATE) {
158 gpr_log(GPR_ERROR, "Zookeeper authentication failed");
Hongwei Wang6e732ea2015-07-27 16:41:54 -0700159 }
Craig Tillera82950e2015-09-22 12:33:20 -0700160 }
Hongwei Wang6e732ea2015-07-27 16:41:54 -0700161}
162
Hongwei Wang85ad6852015-08-13 16:13:10 -0700163/** Zookeeper watcher triggered by changes to watched nodes
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700164 Once triggered, it tries to resolve again to get updated addresses */
Craig Tillera82950e2015-09-22 12:33:20 -0700165static void zookeeper_watcher(zhandle_t *zookeeper_handle, int type, int state,
166 const char *path, void *watcher_ctx) {
167 if (watcher_ctx != NULL) {
168 zookeeper_resolver *r = (zookeeper_resolver *)watcher_ctx;
169 if (state == ZOO_CONNECTED_STATE) {
170 gpr_mu_lock(&r->mu);
171 if (r->resolving == 0) {
172 zookeeper_start_resolving_locked(r);
173 }
174 gpr_mu_unlock(&r->mu);
Hongwei Wang6e732ea2015-07-27 16:41:54 -0700175 }
Craig Tillera82950e2015-09-22 12:33:20 -0700176 }
Hongwei Wang6e732ea2015-07-27 16:41:54 -0700177}
178
Craig Tillerd6c98df2015-08-18 09:33:44 -0700179/** Callback function after getting all resolved addresses
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700180 Creates a subchannel for each address */
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700181static void zookeeper_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillera82950e2015-09-22 12:33:20 -0700182 grpc_resolved_addresses *addresses) {
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700183 zookeeper_resolver *r = arg;
184 grpc_client_config *config = NULL;
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700185 grpc_lb_policy *lb_policy;
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700186
Craig Tillera82950e2015-09-22 12:33:20 -0700187 if (addresses != NULL) {
188 grpc_lb_policy_args lb_policy_args;
189 config = grpc_client_config_create();
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700190 lb_policy_args.addresses = addresses;
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700191 lb_policy_args.client_channel_factory = r->client_channel_factory;
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700192 lb_policy =
193 grpc_lb_policy_create(exec_ctx, r->lb_policy_name, &lb_policy_args);
194
195 if (lb_policy != NULL) {
196 grpc_client_config_set_lb_policy(config, lb_policy);
197 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "construction");
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700198 }
Craig Tillera82950e2015-09-22 12:33:20 -0700199 grpc_resolved_addresses_destroy(addresses);
Craig Tillera82950e2015-09-22 12:33:20 -0700200 }
201 gpr_mu_lock(&r->mu);
202 GPR_ASSERT(r->resolving == 1);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700203 r->resolving = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700204 if (r->resolved_config != NULL) {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700205 grpc_client_config_unref(exec_ctx, r->resolved_config);
Craig Tillera82950e2015-09-22 12:33:20 -0700206 }
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700207 r->resolved_config = config;
208 r->resolved_version++;
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700209 zookeeper_maybe_finish_next_locked(exec_ctx, r);
Craig Tillera82950e2015-09-22 12:33:20 -0700210 gpr_mu_unlock(&r->mu);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700211
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700212 GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "zookeeper-resolving");
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700213}
214
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700215/** Callback function for each DNS resolved address */
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700216static void zookeeper_dns_resolved(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillera82950e2015-09-22 12:33:20 -0700217 grpc_resolved_addresses *addresses) {
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700218 size_t i;
219 zookeeper_resolver *r = arg;
Hongwei Wangb0453fb2015-08-10 17:04:36 -0700220 int resolve_done = 0;
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700221
Craig Tillera82950e2015-09-22 12:33:20 -0700222 gpr_mu_lock(&r->mu);
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700223 r->resolved_num++;
Craig Tillera82950e2015-09-22 12:33:20 -0700224 r->resolved_addrs->addrs =
225 gpr_realloc(r->resolved_addrs->addrs,
226 sizeof(grpc_resolved_address) *
227 (r->resolved_addrs->naddrs + addresses->naddrs));
228 for (i = 0; i < addresses->naddrs; i++) {
229 memcpy(r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].addr,
230 addresses->addrs[i].addr, addresses->addrs[i].len);
231 r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].len =
232 addresses->addrs[i].len;
233 }
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700234
235 r->resolved_addrs->naddrs += addresses->naddrs;
Craig Tillera82950e2015-09-22 12:33:20 -0700236 grpc_resolved_addresses_destroy(addresses);
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700237
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700238 /** Wait for all addresses to be resolved */
Hongwei Wangb0453fb2015-08-10 17:04:36 -0700239 resolve_done = (r->resolved_num == r->resolved_total);
Craig Tillera82950e2015-09-22 12:33:20 -0700240 gpr_mu_unlock(&r->mu);
241 if (resolve_done) {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700242 zookeeper_on_resolved(exec_ctx, r, r->resolved_addrs);
Craig Tillera82950e2015-09-22 12:33:20 -0700243 }
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700244}
245
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700246/** Parses JSON format address of a zookeeper node */
Craig Tillera82950e2015-09-22 12:33:20 -0700247static char *zookeeper_parse_address(const char *value, size_t value_len) {
Hongwei Wangbe447a82015-07-17 11:20:15 -0700248 grpc_json *json;
Hongwei Wangcd620752015-07-22 15:07:24 -0700249 grpc_json *cur;
Hongwei Wang85ad6852015-08-13 16:13:10 -0700250 const char *host;
251 const char *port;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700252 char *buffer;
Hongwei Wang85ad6852015-08-13 16:13:10 -0700253 char *address = NULL;
Hongwei Wangd65009d2015-07-14 17:01:49 -0700254
Craig Tillera82950e2015-09-22 12:33:20 -0700255 buffer = gpr_malloc(value_len);
256 memcpy(buffer, value, value_len);
257 json = grpc_json_parse_string_with_len(buffer, value_len);
258 if (json != NULL) {
259 host = NULL;
260 port = NULL;
261 for (cur = json->child; cur != NULL; cur = cur->next) {
262 if (!strcmp(cur->key, "host")) {
263 host = cur->value;
264 if (port != NULL) {
265 break;
266 }
267 } else if (!strcmp(cur->key, "port")) {
268 port = cur->value;
269 if (host != NULL) {
270 break;
271 }
272 }
Hongwei Wangd65009d2015-07-14 17:01:49 -0700273 }
Craig Tillera82950e2015-09-22 12:33:20 -0700274 if (host != NULL && port != NULL) {
275 gpr_asprintf(&address, "%s:%s", host, port);
276 }
277 grpc_json_destroy(json);
278 }
279 gpr_free(buffer);
Hongwei Wangc4cdbcd2015-07-16 11:36:39 -0700280
Hongwei Wangd65009d2015-07-14 17:01:49 -0700281 return address;
282}
283
Craig Tillera82950e2015-09-22 12:33:20 -0700284static void zookeeper_get_children_node_completion(int rc, const char *value,
285 int value_len,
286 const struct Stat *stat,
287 const void *arg) {
Hongwei Wang3e506592015-07-21 14:31:50 -0700288 char *address = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700289 zookeeper_resolver *r = (zookeeper_resolver *)arg;
Hongwei Wangb0453fb2015-08-10 17:04:36 -0700290 int resolve_done = 0;
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700291 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700292
Craig Tillera82950e2015-09-22 12:33:20 -0700293 if (rc != 0) {
294 gpr_log(GPR_ERROR, "Error in getting a child node of %s", r->name);
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700295 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700296 return;
297 }
Craig Tiller45724b32015-09-22 10:42:19 -0700298
Craig Tillera82950e2015-09-22 12:33:20 -0700299 address = zookeeper_parse_address(value, (size_t)value_len);
300 if (address != NULL) {
Craig Tiller45724b32015-09-22 10:42:19 -0700301 /** Further resolves address by DNS */
Craig Tiller24d687e2016-04-13 19:47:27 -0700302 grpc_resolve_address(&exec_ctx, address, NULL, zookeeper_dns_resolved, r);
Craig Tillera82950e2015-09-22 12:33:20 -0700303 gpr_free(address);
304 } else {
305 gpr_log(GPR_ERROR, "Error in resolving a child node of %s", r->name);
306 gpr_mu_lock(&r->mu);
307 r->resolved_total--;
308 resolve_done = (r->resolved_num == r->resolved_total);
309 gpr_mu_unlock(&r->mu);
310 if (resolve_done) {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700311 zookeeper_on_resolved(&exec_ctx, r, r->resolved_addrs);
Craig Tiller45724b32015-09-22 10:42:19 -0700312 }
Craig Tillera82950e2015-09-22 12:33:20 -0700313 }
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700314
315 grpc_exec_ctx_finish(&exec_ctx);
Hongwei Wang3e506592015-07-21 14:31:50 -0700316}
317
Craig Tillera82950e2015-09-22 12:33:20 -0700318static void zookeeper_get_children_completion(
319 int rc, const struct String_vector *children, const void *arg) {
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700320 char *path;
Hongwei Wang3e506592015-07-21 14:31:50 -0700321 int status;
Hongwei Wang3e506592015-07-21 14:31:50 -0700322 int i;
Craig Tillera82950e2015-09-22 12:33:20 -0700323 zookeeper_resolver *r = (zookeeper_resolver *)arg;
Hongwei Wang00de6982015-07-21 16:29:34 -0700324
Craig Tillera82950e2015-09-22 12:33:20 -0700325 if (rc != 0) {
326 gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
327 return;
328 }
Hongwei Wang3e506592015-07-21 14:31:50 -0700329
Craig Tillera82950e2015-09-22 12:33:20 -0700330 if (children->count == 0) {
331 gpr_log(GPR_ERROR, "Error in resolving zookeeper address %s", r->name);
332 return;
333 }
Hongwei Wang3e506592015-07-21 14:31:50 -0700334
Craig Tillera82950e2015-09-22 12:33:20 -0700335 r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
Hongwei Wang3e506592015-07-21 14:31:50 -0700336 r->resolved_addrs->addrs = NULL;
337 r->resolved_addrs->naddrs = 0;
338 r->resolved_total = children->count;
339
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700340 /** TODO: Replace expensive heap allocation with stack
341 if we can get maximum length of zookeeper path */
Craig Tillera82950e2015-09-22 12:33:20 -0700342 for (i = 0; i < children->count; i++) {
343 gpr_asprintf(&path, "%s/%s", r->name, children->data[i]);
344 status = zoo_awget(r->zookeeper_handle, path, zookeeper_watcher, r,
345 zookeeper_get_children_node_completion, r);
346 gpr_free(path);
347 if (status != 0) {
348 gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", path);
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700349 }
Craig Tillera82950e2015-09-22 12:33:20 -0700350 }
Hongwei Wang3e506592015-07-21 14:31:50 -0700351}
352
Craig Tillera82950e2015-09-22 12:33:20 -0700353static void zookeeper_get_node_completion(int rc, const char *value,
354 int value_len,
355 const struct Stat *stat,
356 const void *arg) {
Hongwei Wang3e506592015-07-21 14:31:50 -0700357 int status;
358 char *address = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700359 zookeeper_resolver *r = (zookeeper_resolver *)arg;
Hongwei Wang85fd2f72015-07-09 17:38:17 -0700360 r->resolved_addrs = NULL;
361 r->resolved_total = 0;
362 r->resolved_num = 0;
Hongwei Wangd65009d2015-07-14 17:01:49 -0700363
Craig Tillera82950e2015-09-22 12:33:20 -0700364 if (rc != 0) {
365 gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name);
366 return;
367 }
Hongwei Wang3e506592015-07-21 14:31:50 -0700368
Hongwei Wang62b90802015-07-28 18:37:40 -0700369 /** If zookeeper node of path r->name does not have address
370 (i.e. service node), get its children */
Craig Tillera82950e2015-09-22 12:33:20 -0700371 address = zookeeper_parse_address(value, (size_t)value_len);
372 if (address != NULL) {
373 r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
374 r->resolved_addrs->addrs = NULL;
375 r->resolved_addrs->naddrs = 0;
376 r->resolved_total = 1;
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700377 /** Further resolves address by DNS */
Craig Tillere06a81f2016-04-21 22:58:58 -0700378 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller24d687e2016-04-13 19:47:27 -0700379 grpc_resolve_address(&exec_ctx, address, NULL, zookeeper_dns_resolved, r);
Craig Tillera82950e2015-09-22 12:33:20 -0700380 gpr_free(address);
Craig Tillere06a81f2016-04-21 22:58:58 -0700381 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700382 return;
383 }
Craig Tiller45724b32015-09-22 10:42:19 -0700384
Craig Tillera82950e2015-09-22 12:33:20 -0700385 status = zoo_awget_children(r->zookeeper_handle, r->name, zookeeper_watcher,
386 r, zookeeper_get_children_completion, r);
387 if (status != 0) {
388 gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
389 }
Craig Tiller45724b32015-09-22 10:42:19 -0700390}
391
Craig Tillera82950e2015-09-22 12:33:20 -0700392static void zookeeper_resolve_address(zookeeper_resolver *r) {
Craig Tiller45724b32015-09-22 10:42:19 -0700393 int status;
Craig Tillera82950e2015-09-22 12:33:20 -0700394 status = zoo_awget(r->zookeeper_handle, r->name, zookeeper_watcher, r,
395 zookeeper_get_node_completion, r);
396 if (status != 0) {
397 gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name);
398 }
Craig Tiller45724b32015-09-22 10:42:19 -0700399}
400
Craig Tillera82950e2015-09-22 12:33:20 -0700401static void zookeeper_start_resolving_locked(zookeeper_resolver *r) {
402 GRPC_RESOLVER_REF(&r->base, "zookeeper-resolving");
403 GPR_ASSERT(r->resolving == 0);
Craig Tiller45724b32015-09-22 10:42:19 -0700404 r->resolving = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700405 zookeeper_resolve_address(r);
Craig Tiller45724b32015-09-22 10:42:19 -0700406}
407
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700408static void zookeeper_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
409 zookeeper_resolver *r) {
Craig Tillera82950e2015-09-22 12:33:20 -0700410 if (r->next_completion != NULL &&
411 r->resolved_version != r->published_version) {
412 *r->target_config = r->resolved_config;
413 if (r->resolved_config != NULL) {
414 grpc_client_config_ref(r->resolved_config);
Craig Tiller45724b32015-09-22 10:42:19 -0700415 }
Craig Tiller8045add2016-01-28 14:02:06 -0800416 grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700417 r->next_completion = NULL;
418 r->published_version = r->resolved_version;
419 }
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700420}
421
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700422static void zookeeper_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
Craig Tillera82950e2015-09-22 12:33:20 -0700423 zookeeper_resolver *r = (zookeeper_resolver *)gr;
424 gpr_mu_destroy(&r->mu);
425 if (r->resolved_config != NULL) {
Craig Tiller6ecd8ad2015-09-22 17:01:34 -0700426 grpc_client_config_unref(exec_ctx, r->resolved_config);
Craig Tillera82950e2015-09-22 12:33:20 -0700427 }
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -0700428 grpc_client_channel_factory_unref(exec_ctx, r->client_channel_factory);
Craig Tillera82950e2015-09-22 12:33:20 -0700429 gpr_free(r->name);
430 gpr_free(r->lb_policy_name);
431 gpr_free(r);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700432}
433
Craig Tillera82950e2015-09-22 12:33:20 -0700434static grpc_resolver *zookeeper_create(grpc_resolver_args *args,
435 const char *lb_policy_name) {
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700436 zookeeper_resolver *r;
Hongwei Wang85ad6852015-08-13 16:13:10 -0700437 size_t length;
Craig Tiller47a708e2015-09-15 16:16:06 -0700438 char *path = args->uri->path;
Hongwei Wang2ad07d72015-08-07 11:30:36 -0700439
Craig Tillera82950e2015-09-22 12:33:20 -0700440 if (0 == strcmp(args->uri->authority, "")) {
441 gpr_log(GPR_ERROR, "No authority specified in zookeeper uri");
442 return NULL;
443 }
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700444
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700445 /** Removes the trailing slash if exists */
Craig Tillera82950e2015-09-22 12:33:20 -0700446 length = strlen(path);
447 if (length > 1 && path[length - 1] == '/') {
448 path[length - 1] = 0;
449 }
Hongwei Wang85ad6852015-08-13 16:13:10 -0700450
Craig Tillera82950e2015-09-22 12:33:20 -0700451 r = gpr_malloc(sizeof(zookeeper_resolver));
452 memset(r, 0, sizeof(*r));
453 gpr_ref_init(&r->refs, 1);
454 gpr_mu_init(&r->mu);
455 grpc_resolver_init(&r->base, &zookeeper_resolver_vtable);
456 r->name = gpr_strdup(path);
Craig Tillerd6c98df2015-08-18 09:33:44 -0700457
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -0700458 r->client_channel_factory = args->client_channel_factory;
459 grpc_client_channel_factory_ref(r->client_channel_factory);
Craig Tiller47a708e2015-09-15 16:16:06 -0700460
Craig Tillera82950e2015-09-22 12:33:20 -0700461 r->lb_policy_name = gpr_strdup(lb_policy_name);
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700462
Hongwei Wangc3f48c82015-08-13 17:22:58 -0700463 /** Initializes zookeeper client */
Craig Tillera82950e2015-09-22 12:33:20 -0700464 zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
465 r->zookeeper_handle =
466 zookeeper_init(args->uri->authority, zookeeper_global_watcher,
467 GRPC_ZOOKEEPER_SESSION_TIMEOUT, 0, 0, 0);
468 if (r->zookeeper_handle == NULL) {
469 gpr_log(GPR_ERROR, "Unable to connect to zookeeper server");
470 return NULL;
471 }
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700472
473 return &r->base;
474}
475
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700476/*
477 * FACTORY
478 */
479
Craig Tillera82950e2015-09-22 12:33:20 -0700480static void zookeeper_factory_ref(grpc_resolver_factory *factory) {}
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700481
Craig Tillera82950e2015-09-22 12:33:20 -0700482static void zookeeper_factory_unref(grpc_resolver_factory *factory) {}
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700483
Craig Tillera82950e2015-09-22 12:33:20 -0700484static char *zookeeper_factory_get_default_hostname(
485 grpc_resolver_factory *factory, grpc_uri *uri) {
Craig Tillerbc85be12015-08-24 10:36:39 -0700486 return NULL;
487}
488
Craig Tillera82950e2015-09-22 12:33:20 -0700489static grpc_resolver *zookeeper_factory_create_resolver(
490 grpc_resolver_factory *factory, grpc_resolver_args *args) {
491 return zookeeper_create(args, "pick_first");
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700492}
493
494static const grpc_resolver_factory_vtable zookeeper_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700495 zookeeper_factory_ref, zookeeper_factory_unref,
496 zookeeper_factory_create_resolver, zookeeper_factory_get_default_hostname,
497 "zookeeper"};
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700498
Craig Tiller45724b32015-09-22 10:42:19 -0700499static grpc_resolver_factory zookeeper_resolver_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700500 &zookeeper_factory_vtable};
Craig Tiller45724b32015-09-22 10:42:19 -0700501
Craig Tiller3cba82e2016-04-01 12:46:04 -0700502static grpc_resolver_factory *zookeeper_resolver_factory_create() {
Hongwei Wangb1bbf6b2015-07-08 15:28:48 -0700503 return &zookeeper_resolver_factory;
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700504}
Craig Tiller3cba82e2016-04-01 12:46:04 -0700505
506static void zookeeper_plugin_init() {
507 grpc_register_resolver_type(zookeeper_resolver_factory_create());
508}
509
510void grpc_zookeeper_register() {
511 GRPC_API_TRACE("grpc_zookeeper_register(void)", 0, ());
512 grpc_register_plugin(zookeeper_plugin_init, NULL);
513}