From a3780a81026b05b1121f764ab71955eec0092273 Mon Sep 17 00:00:00 2001 From: Hongwei Wang Date: Fri, 17 Jul 2015 15:27:18 -0700 Subject: [PATCH] Register zookeeper plugin in grpc --- BUILD | 1 + Makefile | 16 ++++++-- include/grpc/grpc.h | 6 +++ include/grpc/grpc_zookeeper.h | 9 +++++ .../resolvers/zookeeper_resolver.c | 24 ++++++++--- src/core/surface/init.c | 40 ++++++++++++++++++- templates/Makefile.template | 14 +++++-- tools/run_tests/sources_and_headers.json | 2 + 8 files changed, 98 insertions(+), 14 deletions(-) diff --git a/BUILD b/BUILD index a163d89d0ff..7c7a7052e98 100644 --- a/BUILD +++ b/BUILD @@ -630,6 +630,7 @@ cc_library( "src/core/client_config/resolvers/zookeeper_resolver.c", ], hdrs = [ + "include/grpc/grpc_zookeeper.h", ], includes = [ "include", diff --git a/Makefile b/Makefile index 1f4994611f4..307bf626018 100644 --- a/Makefile +++ b/Makefile @@ -1371,14 +1371,14 @@ $(LIBDIR)/$(CONFIG)/protobuf/libprotobuf.a: third_party/protobuf/configure static: static_c static_cxx -static_c: pc_c pc_c_unsecure $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a static_zookeeper_libs +static_c: pc_c pc_c_unsecure pc_gpr pc_c_zookeeper $(LIBDIR)/$(CONFIG)/libgpr.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.a static_zookeeper_libs static_cxx: pc_cxx pc_cxx_unsecure pc_gpr $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc++_unsecure.a shared: shared_c shared_cxx -shared_c: pc_c pc_c_unsecure pc_gpr $(LIBDIR)/$(CONFIG)/libgpr.$(SHARED_EXT) $(LIBDIR)/$(CONFIG)/libgrpc.$(SHARED_EXT) $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.$(SHARED_EXT) shared_zookeeper_libs +shared_c: pc_c pc_c_unsecure pc_gpr pc_c_zookeeper $(LIBDIR)/$(CONFIG)/libgpr.$(SHARED_EXT) $(LIBDIR)/$(CONFIG)/libgrpc.$(SHARED_EXT) $(LIBDIR)/$(CONFIG)/libgrpc_unsecure.$(SHARED_EXT) shared_zookeeper_libs shared_cxx: pc_cxx pc_cxx_unsecure $(LIBDIR)/$(CONFIG)/libgrpc++.$(SHARED_EXT) $(LIBDIR)/$(CONFIG)/libgrpc++_unsecure.$(SHARED_EXT) @@ -1407,6 +1407,12 @@ pc_c: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc.pc pc_c_unsecure: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_unsecure.pc +ifeq ($(HAS_ZOOKEEPER),true) +pc_c_zookeeper: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_zookeeper.pc +else +pc_c_zookeeper: +endif + pc_cxx: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc++.pc pc_cxx_unsecure: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc++_unsecure.pc @@ -3060,13 +3066,15 @@ else $(Q) $(INSTALL) $(BINDIR)/$(CONFIG)/grpc_ruby_plugin $(prefix)/bin/grpc_ruby_plugin endif -install-pkg-config_c: pc_gpr pc_c pc_c_unsecure +install-pkg-config_c: pc_gpr pc_c pc_c_unsecure pc_c_zookeeper $(E) "[INSTALL] Installing C pkg-config files" $(Q) $(INSTALL) -d $(prefix)/lib/pkgconfig $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/gpr.pc $(prefix)/lib/pkgconfig/gpr.pc $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc.pc $(prefix)/lib/pkgconfig/grpc.pc $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_unsecure.pc $(prefix)/lib/pkgconfig/grpc_unsecure.pc +ifeq ($(HAS_ZOOKEEPER),true) $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_zookeeper.pc $(prefix)/lib/pkgconfig/grpc_zookeeper.pc +endif install-pkg-config_cxx: pc_cxx pc_cxx_unsecure $(E) "[INSTALL] Installing C++ pkg-config files" @@ -3676,6 +3684,8 @@ endif LIBGRPC_ZOOKEEPER_SRC = \ src/core/client_config/resolvers/zookeeper_resolver.c \ +PUBLIC_HEADERS_C += \ + include/grpc/grpc_zookeeper.h \ LIBGRPC_ZOOKEEPER_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC_ZOOKEEPER_SRC)))) diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index 782923d599e..b5b25a68e2e 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -335,6 +335,12 @@ typedef struct grpc_op { } data; } grpc_op; +/** Registers a plugin to be initialized and deinitialized with the library. + + It is safe to pass NULL to either argument. The initialization and + deinitialization order isn't guaranteed. */ +void grpc_register_plugin(void (*init)(void), void (*deinit)(void)); + /** Initialize the grpc library. It is not safe to call any other grpc functions before calling this. diff --git a/include/grpc/grpc_zookeeper.h b/include/grpc/grpc_zookeeper.h index c711f69d2ff..02964c95291 100644 --- a/include/grpc/grpc_zookeeper.h +++ b/include/grpc/grpc_zookeeper.h @@ -34,6 +34,15 @@ #ifndef GRPC_GRPC_ZOOKEEPER_H #define GRPC_GRPC_ZOOKEEPER_H +#ifdef __cplusplus +extern "C" { +#endif +/* Register zookeeper name resolver in grpc */ +void grpc_zookeeper_register(); + +#ifdef __cplusplus +} +#endif #endif /* GRPC_GRPC_ZOOKEEPER_H */ \ No newline at end of file diff --git a/src/core/client_config/resolvers/zookeeper_resolver.c b/src/core/client_config/resolvers/zookeeper_resolver.c index 12d6de12438..e14d3416109 100644 --- a/src/core/client_config/resolvers/zookeeper_resolver.c +++ b/src/core/client_config/resolvers/zookeeper_resolver.c @@ -38,9 +38,11 @@ #include #include +#include #include #include "src/core/client_config/lb_policies/pick_first.h" +#include "src/core/client_config/resolver_registry.h" #include "src/core/iomgr/resolve_address.h" #include "src/core/support/string.h" #include "src/core/json/json.h" @@ -192,11 +194,12 @@ static void zookeeper_dns_resolved(void *arg, grpc_resolved_addresses *addresses r->resolved_addrs->naddrs += addresses->naddrs; grpc_resolved_addresses_destroy(addresses); + /* Wait for all addresses to be resolved */ if (r->resolved_num == r->resolved_total) zookeeper_on_resolved(r, r->resolved_addrs); } -/** Parse json format address of a zookeeper node */ +/* Parse json format address of a zookeeper node */ static char *zookeeper_parse_address(char *buffer, int buffer_len) { char *host; char *port; @@ -236,7 +239,6 @@ static char *zookeeper_parse_address(char *buffer, int buffer_len) { return address; } -/** Resolve address by zookeeper */ static void zookeeper_resolve_address(zookeeper_resolver *r) { struct String_vector children; int status; @@ -255,8 +257,8 @@ static void zookeeper_resolve_address(zookeeper_resolver *r) { memset(path, 0, buffer_len); memset(buffer, 0, buffer_len); - /** Get zookeeper node of given path r->name - If not containing address, get its children */ + /* Get zookeeper node of given path r->name + If not containing address(i.e. service node), get its children */ gpr_log(GPR_INFO, r->name); status = zoo_get(r->zookeeper_handle, r->name, GRPC_ZOOKEEPER_WATCH, buffer, &buffer_len, NULL); @@ -268,6 +270,7 @@ static void zookeeper_resolve_address(zookeeper_resolver *r) { r->resolved_addrs->addrs = NULL; r->resolved_addrs->naddrs = 0; r->resolved_total = 1; + /* Further resolve address by DNS */ grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r); gpr_free(address); return; @@ -297,6 +300,7 @@ static void zookeeper_resolve_address(zookeeper_resolver *r) { if (buffer_len > 0) { address = zookeeper_parse_address(buffer, buffer_len); if (address != NULL) { + /* Further resolve address by DNS */ grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r); } else { @@ -356,7 +360,7 @@ static void zookeeper_destroy(grpc_resolver *gr) { gpr_free(r); } -/** Zookeeper watcher function - handle updates to any watched nodes */ +/* Zookeeper watcher function - handle updates to any watched nodes */ static void zookeeper_watcher(zhandle_t *zookeeper_handle, int type, int state, const char* path, void* watcher_ctx) {} @@ -382,7 +386,7 @@ static grpc_resolver *zookeeper_create( r->lb_policy_factory = lb_policy_factory; grpc_subchannel_factory_ref(subchannel_factory); - /** Initialize zookeeper client */ + /* Initialize zookeeper client */ zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); r->zookeeper_handle = zookeeper_init(uri->authority, zookeeper_watcher, GRPC_ZOOKEEPER_TIMEOUT, 0, 0, 0); @@ -394,6 +398,14 @@ static grpc_resolver *zookeeper_create( return &r->base; } +static void zookeeper_plugin_init() { + grpc_register_resolver_type("zookeeper", grpc_zookeeper_resolver_factory_create()); +} + +void grpc_zookeeper_register() { + grpc_register_plugin(zookeeper_plugin_init, NULL); +} + /* * FACTORY */ diff --git a/src/core/surface/init.c b/src/core/surface/init.c index 2181408aa12..a31f0960aa4 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -33,8 +33,11 @@ #include +#include + #include #include +#include #include "src/core/channel/channel_stack.h" #include "src/core/client_config/resolver_registry.h" #include "src/core/client_config/resolvers/dns_resolver.h" @@ -50,8 +53,6 @@ #include "src/core/client_config/resolvers/unix_resolver_posix.h" #endif -#include "src/core/client_config/resolvers/zookeeper_resolver.h" - static gpr_once g_basic_init = GPR_ONCE_INIT; static gpr_mu g_init_mu; static int g_initializations; @@ -61,7 +62,31 @@ static void do_basic_init(void) { g_initializations = 0; } +typedef struct grpc_plugin { + void (*init)(); + void (*deinit)(); + struct grpc_plugin* next; +} grpc_plugin; + +static grpc_plugin* g_plugins_head = NULL; + +static grpc_plugin* new_plugin(void (*init)(void), void (*deinit)(void)) { + grpc_plugin* plugin = gpr_malloc(sizeof(*plugin)); + memset(plugin, 0, sizeof(*plugin)); + plugin->init = init; + plugin->deinit = deinit; + + return plugin; +} + +void grpc_register_plugin(void (*init)(void), void (*deinit)(void)) { + grpc_plugin* old_head = g_plugins_head; + g_plugins_head = new_plugin(init, deinit); + g_plugins_head->next = old_head; +} + void grpc_init(void) { + grpc_plugin* plugin; gpr_once_init(&g_basic_init, do_basic_init); gpr_mu_lock(&g_init_mu); @@ -83,11 +108,17 @@ void grpc_init(void) { gpr_log(GPR_ERROR, "Could not initialize census."); } grpc_timers_global_init(); + for (plugin = g_plugins_head; plugin; plugin = plugin->next) { + if (plugin->init) plugin->init(); + } } gpr_mu_unlock(&g_init_mu); } void grpc_shutdown(void) { + grpc_plugin* plugin; + grpc_plugin* next; + gpr_mu_lock(&g_init_mu); if (--g_initializations == 0) { grpc_iomgr_shutdown(); @@ -95,6 +126,11 @@ void grpc_shutdown(void) { grpc_timers_global_destroy(); grpc_tracer_shutdown(); grpc_resolver_registry_shutdown(); + for (plugin = g_plugins_head; plugin; plugin = next) { + if (plugin->deinit) plugin->deinit(); + next = plugin->next; + gpr_free(plugin); + } } gpr_mu_unlock(&g_init_mu); } diff --git a/templates/Makefile.template b/templates/Makefile.template index 7ff155dec59..961c5722d32 100644 --- a/templates/Makefile.template +++ b/templates/Makefile.template @@ -825,7 +825,7 @@ $(LIBDIR)/$(CONFIG)/protobuf/libprotobuf.a: third_party/protobuf/configure static: static_c static_cxx -static_c: pc_c pc_c_unsecure \ +static_c: pc_c pc_c_unsecure pc_gpr pc_c_zookeeper\ % for lib in libs: % if lib.build == 'all' and lib.language == 'c' and not lib.get('external_deps', None): $(LIBDIR)/$(CONFIG)/lib${lib.name}.a\ @@ -844,7 +844,7 @@ static_cxx: pc_cxx pc_cxx_unsecure pc_gpr\ shared: shared_c shared_cxx -shared_c: pc_c pc_c_unsecure pc_gpr\ +shared_c: pc_c pc_c_unsecure pc_gpr pc_c_zookeeper\ % for lib in libs: % if lib.build == 'all' and lib.language == 'c' and not lib.get('external_deps', None): $(LIBDIR)/$(CONFIG)/lib${lib.name}.$(SHARED_EXT)\ @@ -909,6 +909,12 @@ pc_c: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc.pc pc_c_unsecure: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_unsecure.pc +ifeq ($(HAS_ZOOKEEPER),true) +pc_c_zookeeper: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_zookeeper.pc +else +pc_c_zookeeper: +endif + pc_cxx: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc++.pc pc_cxx_unsecure: $(LIBDIR)/$(CONFIG)/pkgconfig/grpc++_unsecure.pc @@ -1330,13 +1336,15 @@ else % endfor endif -install-pkg-config_c: pc_gpr pc_c pc_c_unsecure +install-pkg-config_c: pc_gpr pc_c pc_c_unsecure pc_c_zookeeper $(E) "[INSTALL] Installing C pkg-config files" $(Q) $(INSTALL) -d $(prefix)/lib/pkgconfig $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/gpr.pc $(prefix)/lib/pkgconfig/gpr.pc $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc.pc $(prefix)/lib/pkgconfig/grpc.pc $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_unsecure.pc $(prefix)/lib/pkgconfig/grpc_unsecure.pc +ifeq ($(HAS_ZOOKEEPER),true) $(Q) $(INSTALL) $(LIBDIR)/$(CONFIG)/pkgconfig/grpc_zookeeper.pc $(prefix)/lib/pkgconfig/grpc_zookeeper.pc +endif install-pkg-config_cxx: pc_cxx pc_cxx_unsecure $(E) "[INSTALL] Installing C++ pkg-config files" diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index ce3a87bce9c..6db1d43604d 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -9442,11 +9442,13 @@ "grpc" ], "headers": [ + "include/grpc/grpc_zookeeper.h", "src/core/client_config/resolvers/zookeeper_resolver.h" ], "language": "c", "name": "grpc_zookeeper", "src": [ + "include/grpc/grpc_zookeeper.h", "src/core/client_config/resolvers/zookeeper_resolver.c", "src/core/client_config/resolvers/zookeeper_resolver.h" ]