Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 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 | |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/support/alloc.h> |
| 37 | #include <grpc/support/string_util.h> |
| 38 | |
Hongwei Wang | a3780a8 | 2015-07-17 15:27:18 -0700 | [diff] [blame] | 39 | #include <grpc/grpc_zookeeper.h> |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 40 | #include <zookeeper/zookeeper.h> |
| 41 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 42 | #include "src/core/ext/client_config/lb_policy_registry.h" |
| 43 | #include "src/core/ext/client_config/resolver_registry.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #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 Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 48 | |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 49 | /** Zookeeper session expiration time in milliseconds */ |
| 50 | #define GRPC_ZOOKEEPER_SESSION_TIMEOUT 15000 |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | typedef struct { |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 53 | /** 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 Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 60 | grpc_client_channel_factory *client_channel_factory; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 61 | /** load balancing policy name */ |
| 62 | char *lb_policy_name; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 63 | |
| 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 Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 73 | grpc_closure *next_completion; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 74 | /** 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 Wang | d65009d | 2015-07-14 17:01:49 -0700 | [diff] [blame] | 81 | /** zookeeper resolved addresses */ |
Hongwei Wang | cd62075 | 2015-07-22 15:07:24 -0700 | [diff] [blame] | 82 | grpc_resolved_addresses *resolved_addrs; |
Hongwei Wang | c4cdbcd | 2015-07-16 11:36:39 -0700 | [diff] [blame] | 83 | /** total number of addresses to be resolved */ |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 84 | int resolved_total; |
Hongwei Wang | 6e732ea | 2015-07-27 16:41:54 -0700 | [diff] [blame] | 85 | /** number of addresses resolved */ |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 86 | int resolved_num; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 87 | } zookeeper_resolver; |
| 88 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 89 | static void zookeeper_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 90 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | static void zookeeper_start_resolving_locked(zookeeper_resolver *r); |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 92 | static void zookeeper_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 93 | zookeeper_resolver *r); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 94 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 95 | static void zookeeper_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r); |
| 96 | static void zookeeper_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | dc03668 | 2015-12-02 08:43:37 -0800 | [diff] [blame] | 97 | grpc_resolver *r); |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 98 | static void zookeeper_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r, |
| 99 | grpc_client_config **target_config, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | grpc_closure *on_complete); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static const grpc_resolver_vtable zookeeper_resolver_vtable = { |
| 103 | zookeeper_destroy, zookeeper_shutdown, zookeeper_channel_saw_error, |
| 104 | zookeeper_next}; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 105 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 106 | static void zookeeper_shutdown(grpc_exec_ctx *exec_ctx, |
| 107 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | zookeeper_resolver *r = (zookeeper_resolver *)resolver; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 109 | grpc_closure *call = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | 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 Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 119 | call->cb(exec_ctx, call->cb_arg, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | } |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 123 | static void zookeeper_channel_saw_error(grpc_exec_ctx *exec_ctx, |
Craig Tiller | dc03668 | 2015-12-02 08:43:37 -0800 | [diff] [blame] | 124 | grpc_resolver *resolver) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | 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 Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 133 | static void zookeeper_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | grpc_client_config **target_config, |
| 135 | grpc_closure *on_complete) { |
| 136 | zookeeper_resolver *r = (zookeeper_resolver *)resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | gpr_mu_lock(&r->mu); |
| 138 | GPR_ASSERT(r->next_completion == NULL); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 139 | r->next_completion = on_complete; |
| 140 | r->target_config = target_config; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | if (r->resolved_version == 0 && r->resolving == 0) { |
| 142 | zookeeper_start_resolving_locked(r); |
| 143 | } else { |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 144 | zookeeper_maybe_finish_next_locked(exec_ctx, r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 145 | } |
| 146 | gpr_mu_unlock(&r->mu); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 149 | /** Zookeeper global watcher for connection management |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 150 | TODO: better connection management besides logs */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | static 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 Wang | 6e732ea | 2015-07-27 16:41:54 -0700 | [diff] [blame] | 159 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | } |
Hongwei Wang | 6e732ea | 2015-07-27 16:41:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 163 | /** Zookeeper watcher triggered by changes to watched nodes |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 164 | Once triggered, it tries to resolve again to get updated addresses */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | static 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 Wang | 6e732ea | 2015-07-27 16:41:54 -0700 | [diff] [blame] | 175 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | } |
Hongwei Wang | 6e732ea | 2015-07-27 16:41:54 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 179 | /** Callback function after getting all resolved addresses |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 180 | Creates a subchannel for each address */ |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 181 | static void zookeeper_on_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | grpc_resolved_addresses *addresses) { |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 183 | zookeeper_resolver *r = arg; |
| 184 | grpc_client_config *config = NULL; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 185 | grpc_lb_policy *lb_policy; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 186 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | if (addresses != NULL) { |
| 188 | grpc_lb_policy_args lb_policy_args; |
| 189 | config = grpc_client_config_create(); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 190 | lb_policy_args.addresses = addresses; |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 191 | lb_policy_args.client_channel_factory = r->client_channel_factory; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 192 | 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 Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 198 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | grpc_resolved_addresses_destroy(addresses); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | } |
| 201 | gpr_mu_lock(&r->mu); |
| 202 | GPR_ASSERT(r->resolving == 1); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 203 | r->resolving = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | if (r->resolved_config != NULL) { |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 205 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | } |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 207 | r->resolved_config = config; |
| 208 | r->resolved_version++; |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 209 | zookeeper_maybe_finish_next_locked(exec_ctx, r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 210 | gpr_mu_unlock(&r->mu); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 211 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 212 | GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "zookeeper-resolving"); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 215 | /** Callback function for each DNS resolved address */ |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 216 | static void zookeeper_dns_resolved(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | grpc_resolved_addresses *addresses) { |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 218 | size_t i; |
| 219 | zookeeper_resolver *r = arg; |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 220 | int resolve_done = 0; |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 221 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 222 | gpr_mu_lock(&r->mu); |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 223 | r->resolved_num++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 224 | 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 Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 234 | |
| 235 | r->resolved_addrs->naddrs += addresses->naddrs; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | grpc_resolved_addresses_destroy(addresses); |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 237 | |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 238 | /** Wait for all addresses to be resolved */ |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 239 | resolve_done = (r->resolved_num == r->resolved_total); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | gpr_mu_unlock(&r->mu); |
| 241 | if (resolve_done) { |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 242 | zookeeper_on_resolved(exec_ctx, r, r->resolved_addrs); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | } |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 246 | /** Parses JSON format address of a zookeeper node */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | static char *zookeeper_parse_address(const char *value, size_t value_len) { |
Hongwei Wang | be447a8 | 2015-07-17 11:20:15 -0700 | [diff] [blame] | 248 | grpc_json *json; |
Hongwei Wang | cd62075 | 2015-07-22 15:07:24 -0700 | [diff] [blame] | 249 | grpc_json *cur; |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 250 | const char *host; |
| 251 | const char *port; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 252 | char *buffer; |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 253 | char *address = NULL; |
Hongwei Wang | d65009d | 2015-07-14 17:01:49 -0700 | [diff] [blame] | 254 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | 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 Wang | d65009d | 2015-07-14 17:01:49 -0700 | [diff] [blame] | 273 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | 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 Wang | c4cdbcd | 2015-07-16 11:36:39 -0700 | [diff] [blame] | 280 | |
Hongwei Wang | d65009d | 2015-07-14 17:01:49 -0700 | [diff] [blame] | 281 | return address; |
| 282 | } |
| 283 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 284 | static 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 Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 288 | char *address = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | zookeeper_resolver *r = (zookeeper_resolver *)arg; |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 290 | int resolve_done = 0; |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 291 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 292 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | if (rc != 0) { |
| 294 | gpr_log(GPR_ERROR, "Error in getting a child node of %s", r->name); |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 295 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 298 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 299 | address = zookeeper_parse_address(value, (size_t)value_len); |
| 300 | if (address != NULL) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 301 | /** Further resolves address by DNS */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 302 | grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r); |
| 303 | 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 Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 311 | zookeeper_on_resolved(&exec_ctx, r, r->resolved_addrs); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 312 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | } |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 314 | |
| 315 | grpc_exec_ctx_finish(&exec_ctx); |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 318 | static void zookeeper_get_children_completion( |
| 319 | int rc, const struct String_vector *children, const void *arg) { |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 320 | char *path; |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 321 | int status; |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 322 | int i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | zookeeper_resolver *r = (zookeeper_resolver *)arg; |
Hongwei Wang | 00de698 | 2015-07-21 16:29:34 -0700 | [diff] [blame] | 324 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | if (rc != 0) { |
| 326 | gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name); |
| 327 | return; |
| 328 | } |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 329 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 330 | if (children->count == 0) { |
| 331 | gpr_log(GPR_ERROR, "Error in resolving zookeeper address %s", r->name); |
| 332 | return; |
| 333 | } |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 334 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 335 | r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 336 | r->resolved_addrs->addrs = NULL; |
| 337 | r->resolved_addrs->naddrs = 0; |
| 338 | r->resolved_total = children->count; |
| 339 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 340 | /** TODO: Replace expensive heap allocation with stack |
| 341 | if we can get maximum length of zookeeper path */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | 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 Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 349 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | } |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 353 | static void zookeeper_get_node_completion(int rc, const char *value, |
| 354 | int value_len, |
| 355 | const struct Stat *stat, |
| 356 | const void *arg) { |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 357 | int status; |
| 358 | char *address = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 359 | zookeeper_resolver *r = (zookeeper_resolver *)arg; |
Hongwei Wang | 85fd2f7 | 2015-07-09 17:38:17 -0700 | [diff] [blame] | 360 | r->resolved_addrs = NULL; |
| 361 | r->resolved_total = 0; |
| 362 | r->resolved_num = 0; |
Hongwei Wang | d65009d | 2015-07-14 17:01:49 -0700 | [diff] [blame] | 363 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 364 | if (rc != 0) { |
| 365 | gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name); |
| 366 | return; |
| 367 | } |
Hongwei Wang | 3e50659 | 2015-07-21 14:31:50 -0700 | [diff] [blame] | 368 | |
Hongwei Wang | 62b9080 | 2015-07-28 18:37:40 -0700 | [diff] [blame] | 369 | /** If zookeeper node of path r->name does not have address |
| 370 | (i.e. service node), get its children */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | 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 Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 377 | /** Further resolves address by DNS */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 378 | grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r); |
| 379 | gpr_free(address); |
| 380 | return; |
| 381 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 382 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | status = zoo_awget_children(r->zookeeper_handle, r->name, zookeeper_watcher, |
| 384 | r, zookeeper_get_children_completion, r); |
| 385 | if (status != 0) { |
| 386 | gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name); |
| 387 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 390 | static void zookeeper_resolve_address(zookeeper_resolver *r) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 391 | int status; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 392 | status = zoo_awget(r->zookeeper_handle, r->name, zookeeper_watcher, r, |
| 393 | zookeeper_get_node_completion, r); |
| 394 | if (status != 0) { |
| 395 | gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name); |
| 396 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 399 | static void zookeeper_start_resolving_locked(zookeeper_resolver *r) { |
| 400 | GRPC_RESOLVER_REF(&r->base, "zookeeper-resolving"); |
| 401 | GPR_ASSERT(r->resolving == 0); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 402 | r->resolving = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | zookeeper_resolve_address(r); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 406 | static void zookeeper_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx, |
| 407 | zookeeper_resolver *r) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | if (r->next_completion != NULL && |
| 409 | r->resolved_version != r->published_version) { |
| 410 | *r->target_config = r->resolved_config; |
| 411 | if (r->resolved_config != NULL) { |
| 412 | grpc_client_config_ref(r->resolved_config); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 413 | } |
Craig Tiller | 8045add | 2016-01-28 14:02:06 -0800 | [diff] [blame] | 414 | grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 415 | r->next_completion = NULL; |
| 416 | r->published_version = r->resolved_version; |
| 417 | } |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 420 | static void zookeeper_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 421 | zookeeper_resolver *r = (zookeeper_resolver *)gr; |
| 422 | gpr_mu_destroy(&r->mu); |
| 423 | if (r->resolved_config != NULL) { |
Craig Tiller | 6ecd8ad | 2015-09-22 17:01:34 -0700 | [diff] [blame] | 424 | grpc_client_config_unref(exec_ctx, r->resolved_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 425 | } |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 426 | grpc_client_channel_factory_unref(exec_ctx, r->client_channel_factory); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | gpr_free(r->name); |
| 428 | gpr_free(r->lb_policy_name); |
| 429 | gpr_free(r); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 432 | static grpc_resolver *zookeeper_create(grpc_resolver_args *args, |
| 433 | const char *lb_policy_name) { |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 434 | zookeeper_resolver *r; |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 435 | size_t length; |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 436 | char *path = args->uri->path; |
Hongwei Wang | 2ad07d7 | 2015-08-07 11:30:36 -0700 | [diff] [blame] | 437 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 438 | if (0 == strcmp(args->uri->authority, "")) { |
| 439 | gpr_log(GPR_ERROR, "No authority specified in zookeeper uri"); |
| 440 | return NULL; |
| 441 | } |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 442 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 443 | /** Removes the trailing slash if exists */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 444 | length = strlen(path); |
| 445 | if (length > 1 && path[length - 1] == '/') { |
| 446 | path[length - 1] = 0; |
| 447 | } |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 448 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 449 | r = gpr_malloc(sizeof(zookeeper_resolver)); |
| 450 | memset(r, 0, sizeof(*r)); |
| 451 | gpr_ref_init(&r->refs, 1); |
| 452 | gpr_mu_init(&r->mu); |
| 453 | grpc_resolver_init(&r->base, &zookeeper_resolver_vtable); |
| 454 | r->name = gpr_strdup(path); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 455 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 456 | r->client_channel_factory = args->client_channel_factory; |
| 457 | grpc_client_channel_factory_ref(r->client_channel_factory); |
Craig Tiller | 47a708e | 2015-09-15 16:16:06 -0700 | [diff] [blame] | 458 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 459 | r->lb_policy_name = gpr_strdup(lb_policy_name); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 460 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 461 | /** Initializes zookeeper client */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 462 | zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); |
| 463 | r->zookeeper_handle = |
| 464 | zookeeper_init(args->uri->authority, zookeeper_global_watcher, |
| 465 | GRPC_ZOOKEEPER_SESSION_TIMEOUT, 0, 0, 0); |
| 466 | if (r->zookeeper_handle == NULL) { |
| 467 | gpr_log(GPR_ERROR, "Unable to connect to zookeeper server"); |
| 468 | return NULL; |
| 469 | } |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 470 | |
| 471 | return &r->base; |
| 472 | } |
| 473 | |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 474 | /* |
| 475 | * FACTORY |
| 476 | */ |
| 477 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 478 | static void zookeeper_factory_ref(grpc_resolver_factory *factory) {} |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 479 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 480 | static void zookeeper_factory_unref(grpc_resolver_factory *factory) {} |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 481 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 482 | static char *zookeeper_factory_get_default_hostname( |
| 483 | grpc_resolver_factory *factory, grpc_uri *uri) { |
Craig Tiller | bc85be1 | 2015-08-24 10:36:39 -0700 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 487 | static grpc_resolver *zookeeper_factory_create_resolver( |
| 488 | grpc_resolver_factory *factory, grpc_resolver_args *args) { |
| 489 | return zookeeper_create(args, "pick_first"); |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static const grpc_resolver_factory_vtable zookeeper_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 493 | zookeeper_factory_ref, zookeeper_factory_unref, |
| 494 | zookeeper_factory_create_resolver, zookeeper_factory_get_default_hostname, |
| 495 | "zookeeper"}; |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 496 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 497 | static grpc_resolver_factory zookeeper_resolver_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 498 | &zookeeper_factory_vtable}; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 499 | |
Craig Tiller | 3cba82e | 2016-04-01 12:46:04 -0700 | [diff] [blame] | 500 | static grpc_resolver_factory *zookeeper_resolver_factory_create() { |
Hongwei Wang | b1bbf6b | 2015-07-08 15:28:48 -0700 | [diff] [blame] | 501 | return &zookeeper_resolver_factory; |
Hongwei Wang | 35d5a0f | 2015-07-24 11:14:23 -0700 | [diff] [blame] | 502 | } |
Craig Tiller | 3cba82e | 2016-04-01 12:46:04 -0700 | [diff] [blame] | 503 | |
| 504 | static void zookeeper_plugin_init() { |
| 505 | grpc_register_resolver_type(zookeeper_resolver_factory_create()); |
| 506 | } |
| 507 | |
| 508 | void grpc_zookeeper_register() { |
| 509 | GRPC_API_TRACE("grpc_zookeeper_register(void)", 0, ()); |
| 510 | grpc_register_plugin(zookeeper_plugin_init, NULL); |
| 511 | } |