diff options
-rw-r--r-- | BUILD | 12 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | build.yaml | 2 | ||||
-rw-r--r-- | gRPC.podspec | 6 | ||||
-rw-r--r-- | src/core/client_config/lb_policies/pick_first.c | 21 | ||||
-rw-r--r-- | src/core/client_config/lb_policies/pick_first.h | 9 | ||||
-rw-r--r-- | src/core/client_config/lb_policy_factory.c | 49 | ||||
-rw-r--r-- | src/core/client_config/lb_policy_factory.h | 70 | ||||
-rw-r--r-- | src/core/client_config/lb_policy_registry.c | 89 | ||||
-rw-r--r-- | src/core/client_config/lb_policy_registry.h | 55 | ||||
-rw-r--r-- | src/core/client_config/resolvers/dns_resolver.c | 19 | ||||
-rw-r--r-- | src/core/client_config/resolvers/sockaddr_resolver.c | 19 | ||||
-rw-r--r-- | src/core/client_config/resolvers/zookeeper_resolver.c | 20 | ||||
-rw-r--r-- | src/core/surface/init.c | 4 | ||||
-rw-r--r-- | tools/doxygen/Doxyfile.core.internal | 4 | ||||
-rw-r--r-- | tools/run_tests/sources_and_headers.json | 12 | ||||
-rw-r--r-- | vsprojects/vcxproj/grpc/grpc.vcxproj | 6 | ||||
-rw-r--r-- | vsprojects/vcxproj/grpc/grpc.vcxproj.filters | 12 | ||||
-rw-r--r-- | vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj | 6 | ||||
-rw-r--r-- | vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters | 12 |
20 files changed, 393 insertions, 38 deletions
@@ -158,6 +158,8 @@ cc_library( "src/core/client_config/connector.h", "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", @@ -283,6 +285,8 @@ cc_library( "src/core/client_config/connector.c", "src/core/client_config/lb_policies/pick_first.c", "src/core/client_config/lb_policy.c", + "src/core/client_config/lb_policy_factory.c", + "src/core/client_config/lb_policy_registry.c", "src/core/client_config/resolver.c", "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", @@ -428,6 +432,8 @@ cc_library( "src/core/client_config/connector.h", "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", @@ -533,6 +539,8 @@ cc_library( "src/core/client_config/connector.c", "src/core/client_config/lb_policies/pick_first.c", "src/core/client_config/lb_policy.c", + "src/core/client_config/lb_policy_factory.c", + "src/core/client_config/lb_policy_registry.c", "src/core/client_config/resolver.c", "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", @@ -1047,6 +1055,8 @@ objc_library( "src/core/client_config/connector.c", "src/core/client_config/lb_policies/pick_first.c", "src/core/client_config/lb_policy.c", + "src/core/client_config/lb_policy_factory.c", + "src/core/client_config/lb_policy_registry.c", "src/core/client_config/resolver.c", "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", @@ -1189,6 +1199,8 @@ objc_library( "src/core/client_config/connector.h", "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", @@ -4045,6 +4045,8 @@ LIBGRPC_SRC = \ src/core/client_config/connector.c \ src/core/client_config/lb_policies/pick_first.c \ src/core/client_config/lb_policy.c \ + src/core/client_config/lb_policy_factory.c \ + src/core/client_config/lb_policy_registry.c \ src/core/client_config/resolver.c \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ @@ -4320,6 +4322,8 @@ LIBGRPC_UNSECURE_SRC = \ src/core/client_config/connector.c \ src/core/client_config/lb_policies/pick_first.c \ src/core/client_config/lb_policy.c \ + src/core/client_config/lb_policy_factory.c \ + src/core/client_config/lb_policy_registry.c \ src/core/client_config/resolver.c \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ diff --git a/build.yaml b/build.yaml index 31a1e328a8..c01807878f 100644 --- a/build.yaml +++ b/build.yaml @@ -46,6 +46,7 @@ filegroups: src/core/channel/context.h, src/core/channel/http_client_filter.h, src/core/channel/http_server_filter.h, src/core/channel/noop_filter.h, src/core/client_config/client_config.h, src/core/client_config/connector.h, src/core/client_config/lb_policies/pick_first.h, src/core/client_config/lb_policy.h, + src/core/client_config/lb_policy_factory.h, src/core/client_config/lb_policy_registry.h, src/core/client_config/resolver.h, src/core/client_config/resolver_factory.h, src/core/client_config/resolver_registry.h, src/core/client_config/resolvers/dns_resolver.h, src/core/client_config/resolvers/sockaddr_resolver.h, src/core/client_config/subchannel.h, @@ -85,6 +86,7 @@ filegroups: src/core/channel/connected_channel.c, src/core/channel/http_client_filter.c, src/core/channel/http_server_filter.c, src/core/channel/noop_filter.c, src/core/client_config/client_config.c, src/core/client_config/connector.c, src/core/client_config/lb_policies/pick_first.c, src/core/client_config/lb_policy.c, + src/core/client_config/lb_policy_factory.c, src/core/client_config/lb_policy_registry.c, src/core/client_config/resolver.c, src/core/client_config/resolver_factory.c, src/core/client_config/resolver_registry.c, src/core/client_config/resolvers/dns_resolver.c, src/core/client_config/resolvers/sockaddr_resolver.c, src/core/client_config/subchannel.c, diff --git a/gRPC.podspec b/gRPC.podspec index d0e1e2d848..aaa6abb15c 100644 --- a/gRPC.podspec +++ b/gRPC.podspec @@ -160,6 +160,8 @@ Pod::Spec.new do |s| 'src/core/client_config/connector.h', 'src/core/client_config/lb_policies/pick_first.h', 'src/core/client_config/lb_policy.h', + 'src/core/client_config/lb_policy_factory.h', + 'src/core/client_config/lb_policy_registry.h', 'src/core/client_config/resolver.h', 'src/core/client_config/resolver_factory.h', 'src/core/client_config/resolver_registry.h', @@ -292,6 +294,8 @@ Pod::Spec.new do |s| 'src/core/client_config/connector.c', 'src/core/client_config/lb_policies/pick_first.c', 'src/core/client_config/lb_policy.c', + 'src/core/client_config/lb_policy_factory.c', + 'src/core/client_config/lb_policy_registry.c', 'src/core/client_config/resolver.c', 'src/core/client_config/resolver_factory.c', 'src/core/client_config/resolver_registry.c', @@ -434,6 +438,8 @@ Pod::Spec.new do |s| 'src/core/client_config/connector.h', 'src/core/client_config/lb_policies/pick_first.h', 'src/core/client_config/lb_policy.h', + 'src/core/client_config/lb_policy_factory.h', + 'src/core/client_config/lb_policy_registry.h', 'src/core/client_config/resolver.h', 'src/core/client_config/resolver_factory.h', 'src/core/client_config/resolver_registry.h', diff --git a/src/core/client_config/lb_policies/pick_first.c b/src/core/client_config/lb_policies/pick_first.c index 5ae2e0ea52..1c5d058d97 100644 --- a/src/core/client_config/lb_policies/pick_first.c +++ b/src/core/client_config/lb_policies/pick_first.c @@ -31,6 +31,7 @@ * */ +#include "src/core/client_config/lb_policy_factory.h" #include "src/core/client_config/lb_policies/pick_first.h" #include <string.h> @@ -314,8 +315,13 @@ static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { pf_check_connectivity, pf_notify_on_state_change}; -grpc_lb_policy *grpc_create_pick_first_lb_policy(grpc_subchannel **subchannels, - size_t num_subchannels) { +static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} + +static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} + +static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory, + grpc_subchannel **subchannels, + size_t num_subchannels) { pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); GPR_ASSERT(num_subchannels); memset(p, 0, sizeof(*p)); @@ -330,3 +336,14 @@ grpc_lb_policy *grpc_create_pick_first_lb_policy(grpc_subchannel **subchannels, gpr_mu_init(&p->mu); return &p->base; } + +static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { + pick_first_factory_ref, pick_first_factory_unref, create_pick_first, + "pick_first"}; + +static grpc_lb_policy_factory pick_first_lb_policy_factory = { + &pick_first_factory_vtable}; + +grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() { + return &pick_first_lb_policy_factory; +} diff --git a/src/core/client_config/lb_policies/pick_first.h b/src/core/client_config/lb_policies/pick_first.h index 31394985e5..3ca53ad42a 100644 --- a/src/core/client_config/lb_policies/pick_first.h +++ b/src/core/client_config/lb_policies/pick_first.h @@ -34,11 +34,10 @@ #ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_PICK_FIRST_H #define GRPC_INTERNAL_CORE_CLIENT_CONFIG_PICK_FIRST_H -#include "src/core/client_config/lb_policy.h" +#include "src/core/client_config/lb_policy_factory.h" -/** Returns a load balancing policy instance that picks up the first subchannel - * from \a subchannels to succesfully connect */ -grpc_lb_policy *grpc_create_pick_first_lb_policy(grpc_subchannel **subchannels, - size_t num_subchannels); +/** Returns a load balancing factory for the pick first policy, which picks up + * the first subchannel from \a subchannels to succesfully connect */ +grpc_lb_policy_factory *grpc_pick_first_lb_factory_create(); #endif diff --git a/src/core/client_config/lb_policy_factory.c b/src/core/client_config/lb_policy_factory.c new file mode 100644 index 0000000000..533f973cea --- /dev/null +++ b/src/core/client_config/lb_policy_factory.c @@ -0,0 +1,49 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "src/core/client_config/lb_policy_factory.h" + +void grpc_lb_policy_factory_ref(grpc_lb_policy_factory *factory) { + factory->vtable->ref(factory); +} +void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory) { + factory->vtable->unref(factory); +} + +grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy( + grpc_lb_policy_factory *factory, grpc_subchannel **subchannels, + size_t num_subchannels) { + if (factory == NULL) return NULL; + return factory->vtable->create_lb_policy(factory, subchannels, + num_subchannels); +} diff --git a/src/core/client_config/lb_policy_factory.h b/src/core/client_config/lb_policy_factory.h new file mode 100644 index 0000000000..ba057648f7 --- /dev/null +++ b/src/core/client_config/lb_policy_factory.h @@ -0,0 +1,70 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_FACTORY_H +#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_FACTORY_H + +#include "src/core/client_config/lb_policy.h" +#include "src/core/client_config/subchannel.h" + +typedef struct grpc_lb_policy_factory grpc_lb_policy_factory; +typedef struct grpc_lb_policy_factory_vtable grpc_lb_policy_factory_vtable; + +/** grpc_lb_policy provides grpc_client_config objects to grpc_channel + objects */ +struct grpc_lb_policy_factory { + const grpc_lb_policy_factory_vtable *vtable; +}; + +struct grpc_lb_policy_factory_vtable { + void (*ref)(grpc_lb_policy_factory *factory); + void (*unref)(grpc_lb_policy_factory *factory); + + /** Implementation of grpc_lb_policy_factory_create_lb_policy */ + grpc_lb_policy *(*create_lb_policy)(grpc_lb_policy_factory *factory, + grpc_subchannel **subchannels, + size_t num_subchannels); + + /** Name for the LB policy this factory implements */ + const char *name; +}; + +void grpc_lb_policy_factory_ref(grpc_lb_policy_factory *factory); +void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory); + +/** Create a lb_policy instance. */ +grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy( + grpc_lb_policy_factory *factory, grpc_subchannel **subchannels, + size_t num_subchannels); + +#endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_FACTORY_H */ diff --git a/src/core/client_config/lb_policy_registry.c b/src/core/client_config/lb_policy_registry.c new file mode 100644 index 0000000000..0a3ebbe4b3 --- /dev/null +++ b/src/core/client_config/lb_policy_registry.c @@ -0,0 +1,89 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "src/core/client_config/lb_policy_registry.h" + +#include <string.h> + +#define MAX_POLICIES 10 + +static grpc_lb_policy_factory *g_all_of_the_lb_policies[MAX_POLICIES]; +static int g_number_of_lb_policies = 0; + +static grpc_lb_policy_factory *g_default_lb_policy_factory; + +void grpc_lb_policy_registry_init(grpc_lb_policy_factory *default_factory) { + g_number_of_lb_policies = 0; + g_default_lb_policy_factory = default_factory; +} + +void grpc_lb_policy_registry_shutdown(void) { + int i; + for (i = 0; i < g_number_of_lb_policies; i++) { + grpc_lb_policy_factory_unref(g_all_of_the_lb_policies[i]); + } +} + +void grpc_register_lb_policy(grpc_lb_policy_factory *factory) { + int i; + for (i = 0; i < g_number_of_lb_policies; i++) { + GPR_ASSERT(0 != strcmp(factory->vtable->name, + g_all_of_the_lb_policies[i]->vtable->name)); + } + GPR_ASSERT(g_number_of_lb_policies != MAX_POLICIES); + grpc_lb_policy_factory_ref(factory); + g_all_of_the_lb_policies[g_number_of_lb_policies++] = factory; +} + +static grpc_lb_policy_factory *lookup_factory(const char* name) { + int i; + + if (name == NULL) return NULL; + + for (i = 0; i < g_number_of_lb_policies; i++) { + if (0 == strcmp(name, g_all_of_the_lb_policies[i]->vtable->name)) { + return g_all_of_the_lb_policies[i]; + } + } + + return NULL; +} + +grpc_lb_policy *grpc_lb_policy_create(const char *name, + grpc_subchannel **subchannels, + size_t num_subchannels) { + grpc_lb_policy_factory *factory = lookup_factory(name); + grpc_lb_policy *lb_policy = grpc_lb_policy_factory_create_lb_policy( + factory, subchannels, num_subchannels); + return lb_policy; +} diff --git a/src/core/client_config/lb_policy_registry.h b/src/core/client_config/lb_policy_registry.h new file mode 100644 index 0000000000..75114d9f02 --- /dev/null +++ b/src/core/client_config/lb_policy_registry.h @@ -0,0 +1,55 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_REGISTRY_H +#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_REGISTRY_H + +#include "src/core/client_config/lb_policy_factory.h" + +/** Initialize the registry and set \a default_factory as the factory to be + * returned when no name is provided in a lookup */ +void grpc_lb_policy_registry_init(grpc_lb_policy_factory *default_factory); +void grpc_lb_policy_registry_shutdown(void); + +/** Register a LB policy factory. */ +void grpc_register_lb_policy(grpc_lb_policy_factory *factory); + +/** Create a \a grpc_lb_policy instance. + * + * If \a name is NULL, the default factory from \a grpc_lb_policy_registry_init + * will be returned. */ +grpc_lb_policy *grpc_lb_policy_create(const char *name, + grpc_subchannel **subchannels, + size_t num_subchannels); + +#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_REGISTRY_H */ diff --git a/src/core/client_config/resolvers/dns_resolver.c b/src/core/client_config/resolvers/dns_resolver.c index 84643c464a..830f3f5286 100644 --- a/src/core/client_config/resolvers/dns_resolver.c +++ b/src/core/client_config/resolvers/dns_resolver.c @@ -39,7 +39,7 @@ #include <grpc/support/host_port.h> #include <grpc/support/string_util.h> -#include "src/core/client_config/lb_policies/pick_first.h" +#include "src/core/client_config/lb_policy_registry.h" #include "src/core/client_config/subchannel_factory_decorators/add_channel_arg.h" #include "src/core/iomgr/resolve_address.h" #include "src/core/support/string.h" @@ -55,9 +55,8 @@ typedef struct { char *default_port; /** subchannel factory */ grpc_subchannel_factory *subchannel_factory; - /** load balancing policy factory */ - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels); + /** load balancing policy name */ + char *lb_policy_name; /** mutex guarding the rest of the state */ gpr_mu mu; @@ -144,7 +143,8 @@ static void dns_on_resolved(void *arg, grpc_resolved_addresses *addresses) { subchannels[i] = grpc_subchannel_factory_create_subchannel( r->subchannel_factory, &args); } - lb_policy = r->lb_policy_factory(subchannels, addresses->naddrs); + lb_policy = grpc_lb_policy_create(r->lb_policy_name, subchannels, + addresses->naddrs); grpc_client_config_set_lb_policy(config, lb_policy); GRPC_LB_POLICY_UNREF(lb_policy, "construction"); grpc_resolved_addresses_destroy(addresses); @@ -193,13 +193,13 @@ static void dns_destroy(grpc_resolver *gr) { grpc_subchannel_factory_unref(r->subchannel_factory); gpr_free(r->name); gpr_free(r->default_port); + gpr_free(r->lb_policy_name); gpr_free(r); } static grpc_resolver *dns_create( grpc_uri *uri, const char *default_port, - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels), + const char* lb_policy_name, grpc_subchannel_factory *subchannel_factory) { dns_resolver *r; const char *path = uri->path; @@ -220,7 +220,7 @@ static grpc_resolver *dns_create( r->default_port = gpr_strdup(default_port); r->subchannel_factory = subchannel_factory; grpc_subchannel_factory_ref(subchannel_factory); - r->lb_policy_factory = lb_policy_factory; + r->lb_policy_name = gpr_strdup(lb_policy_name); return &r->base; } @@ -235,8 +235,7 @@ static void dns_factory_unref(grpc_resolver_factory *factory) {} static grpc_resolver *dns_factory_create_resolver( grpc_resolver_factory *factory, grpc_uri *uri, grpc_subchannel_factory *subchannel_factory) { - return dns_create(uri, "https", grpc_create_pick_first_lb_policy, - subchannel_factory); + return dns_create(uri, "https", "pick_first", subchannel_factory); } char *dns_factory_get_default_host_name(grpc_resolver_factory *factory, diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c index 0d8540a566..1bff1d7abf 100644 --- a/src/core/client_config/resolvers/sockaddr_resolver.c +++ b/src/core/client_config/resolvers/sockaddr_resolver.c @@ -45,7 +45,7 @@ #include <grpc/support/host_port.h> #include <grpc/support/string_util.h> -#include "src/core/client_config/lb_policies/pick_first.h" +#include "src/core/client_config/lb_policy_registry.h" #include "src/core/iomgr/resolve_address.h" #include "src/core/support/string.h" @@ -56,9 +56,8 @@ typedef struct { gpr_refcount refs; /** subchannel factory */ grpc_subchannel_factory *subchannel_factory; - /** load balancing policy factory */ - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels); + /** load balancing policy name */ + char *lb_policy_name; /** the addresses that we've 'resolved' */ struct sockaddr_storage *addrs; @@ -136,7 +135,8 @@ static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r) { subchannels[i] = grpc_subchannel_factory_create_subchannel( r->subchannel_factory, &args); } - lb_policy = r->lb_policy_factory(subchannels, r->num_addrs); + lb_policy = + grpc_lb_policy_create(r->lb_policy_name, subchannels, r->num_addrs); gpr_free(subchannels); grpc_client_config_set_lb_policy(cfg, lb_policy); GRPC_LB_POLICY_UNREF(lb_policy, "unix"); @@ -153,6 +153,7 @@ static void sockaddr_destroy(grpc_resolver *gr) { grpc_subchannel_factory_unref(r->subchannel_factory); gpr_free(r->addrs); gpr_free(r->addrs_len); + gpr_free(r->lb_policy_name); gpr_free(r); } @@ -271,9 +272,7 @@ done: static void do_nothing(void *ignored) {} static grpc_resolver *sockaddr_create( - grpc_uri *uri, - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels), + grpc_uri *uri, const char *lb_policy_name, grpc_subchannel_factory *subchannel_factory, int parse(grpc_uri *uri, struct sockaddr_storage *dst, int *len)) { size_t i; @@ -320,7 +319,7 @@ static grpc_resolver *sockaddr_create( gpr_mu_init(&r->mu); grpc_resolver_init(&r->base, &sockaddr_resolver_vtable); r->subchannel_factory = subchannel_factory; - r->lb_policy_factory = lb_policy_factory; + r->lb_policy_name = gpr_strdup(lb_policy_name); grpc_subchannel_factory_ref(subchannel_factory); return &r->base; @@ -338,7 +337,7 @@ static void sockaddr_factory_unref(grpc_resolver_factory *factory) {} static grpc_resolver *name##_factory_create_resolver( \ grpc_resolver_factory *factory, grpc_uri *uri, \ grpc_subchannel_factory *subchannel_factory) { \ - return sockaddr_create(uri, grpc_create_pick_first_lb_policy, \ + return sockaddr_create(uri, "pick_first", \ subchannel_factory, parse_##name); \ } \ static const grpc_resolver_factory_vtable name##_factory_vtable = { \ diff --git a/src/core/client_config/resolvers/zookeeper_resolver.c b/src/core/client_config/resolvers/zookeeper_resolver.c index da399f9954..8b89100245 100644 --- a/src/core/client_config/resolvers/zookeeper_resolver.c +++ b/src/core/client_config/resolvers/zookeeper_resolver.c @@ -41,7 +41,7 @@ #include <grpc/grpc_zookeeper.h> #include <zookeeper/zookeeper.h> -#include "src/core/client_config/lb_policies/pick_first.h" +#include "src/core/client_config/lb_policy_registry.h" #include "src/core/client_config/resolver_registry.h" #include "src/core/iomgr/resolve_address.h" #include "src/core/support/string.h" @@ -59,9 +59,8 @@ typedef struct { char *name; /** subchannel factory */ grpc_subchannel_factory *subchannel_factory; - /** load balancing policy factory */ - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels); + /** load balancing policy name */ + char *lb_policy_name; /** mutex guarding the rest of the state */ gpr_mu mu; @@ -192,7 +191,8 @@ static void zookeeper_on_resolved(void *arg, subchannels[i] = grpc_subchannel_factory_create_subchannel( r->subchannel_factory, &args); } - lb_policy = r->lb_policy_factory(subchannels, addresses->naddrs); + lb_policy = + grpc_lb_policy_create(r->lb_policy_name, subchannels, addresses->naddrs); grpc_client_config_set_lb_policy(config, lb_policy); GRPC_LB_POLICY_UNREF(lb_policy, "construction"); grpc_resolved_addresses_destroy(addresses); @@ -420,13 +420,12 @@ static void zookeeper_destroy(grpc_resolver *gr) { } grpc_subchannel_factory_unref(r->subchannel_factory); gpr_free(r->name); + gpr_free(r->lb_policy_name); gpr_free(r); } static grpc_resolver *zookeeper_create( - grpc_uri *uri, - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels), + grpc_uri *uri, const char *lb_policy_name, grpc_subchannel_factory *subchannel_factory) { zookeeper_resolver *r; size_t length; @@ -451,7 +450,7 @@ static grpc_resolver *zookeeper_create( r->name = gpr_strdup(path); r->subchannel_factory = subchannel_factory; - r->lb_policy_factory = lb_policy_factory; + r->lb_policy_name = gpr_strdup(lb_policy_name); grpc_subchannel_factory_ref(subchannel_factory); /** Initializes zookeeper client */ @@ -490,8 +489,7 @@ static char *zookeeper_factory_get_default_hostname( static grpc_resolver *zookeeper_factory_create_resolver( grpc_resolver_factory *factory, grpc_uri *uri, grpc_subchannel_factory *subchannel_factory) { - return zookeeper_create(uri, grpc_create_pick_first_lb_policy, - subchannel_factory); + return zookeeper_create(uri, "pick_first", subchannel_factory); } static const grpc_resolver_factory_vtable zookeeper_factory_vtable = { diff --git a/src/core/surface/init.c b/src/core/surface/init.c index 0d48cd42d7..03bd026a42 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -40,6 +40,8 @@ #include <grpc/support/alloc.h> #include <grpc/support/time.h> #include "src/core/channel/channel_stack.h" +#include "src/core/client_config/lb_policy_registry.h" +#include "src/core/client_config/lb_policies/pick_first.h" #include "src/core/client_config/resolver_registry.h" #include "src/core/client_config/resolvers/dns_resolver.h" #include "src/core/client_config/resolvers/sockaddr_resolver.h" @@ -85,6 +87,8 @@ void grpc_init(void) { gpr_mu_lock(&g_init_mu); if (++g_initializations == 1) { gpr_time_init(); + grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create()); + grpc_register_lb_policy(grpc_pick_first_lb_factory_create()); grpc_resolver_registry_init("dns:///"); grpc_register_resolver_type(grpc_dns_resolver_factory_create()); grpc_register_resolver_type(grpc_ipv4_resolver_factory_create()); diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index fdc32c7776..acfb38e629 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -794,6 +794,8 @@ src/core/client_config/client_config.h \ src/core/client_config/connector.h \ src/core/client_config/lb_policies/pick_first.h \ src/core/client_config/lb_policy.h \ +src/core/client_config/lb_policy_factory.h \ +src/core/client_config/lb_policy_registry.h \ src/core/client_config/resolver.h \ src/core/client_config/resolver_factory.h \ src/core/client_config/resolver_registry.h \ @@ -919,6 +921,8 @@ src/core/client_config/client_config.c \ src/core/client_config/connector.c \ src/core/client_config/lb_policies/pick_first.c \ src/core/client_config/lb_policy.c \ +src/core/client_config/lb_policy_factory.c \ +src/core/client_config/lb_policy_registry.c \ src/core/client_config/resolver.c \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 3a94b9ee6b..2d2108f1c2 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -12246,6 +12246,8 @@ "src/core/client_config/connector.h", "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", @@ -12392,6 +12394,10 @@ "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.c", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.c", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.c", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.c", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.c", @@ -12726,6 +12732,8 @@ "src/core/client_config/connector.h", "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", @@ -12858,6 +12866,10 @@ "src/core/client_config/lb_policies/pick_first.h", "src/core/client_config/lb_policy.c", "src/core/client_config/lb_policy.h", + "src/core/client_config/lb_policy_factory.c", + "src/core/client_config/lb_policy_factory.h", + "src/core/client_config/lb_policy_registry.c", + "src/core/client_config/lb_policy_registry.h", "src/core/client_config/resolver.c", "src/core/client_config/resolver.h", "src/core/client_config/resolver_factory.c", diff --git a/vsprojects/vcxproj/grpc/grpc.vcxproj b/vsprojects/vcxproj/grpc/grpc.vcxproj index 852da5d95f..2eda9e32e6 100644 --- a/vsprojects/vcxproj/grpc/grpc.vcxproj +++ b/vsprojects/vcxproj/grpc/grpc.vcxproj @@ -256,6 +256,8 @@ <ClInclude Include="..\..\..\src\core\client_config\connector.h" /> <ClInclude Include="..\..\..\src\core\client_config\lb_policies\pick_first.h" /> <ClInclude Include="..\..\..\src\core\client_config\lb_policy.h" /> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_factory.h" /> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_registry.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver_factory.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver_registry.h" /> @@ -418,6 +420,10 @@ </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\lb_policy.c"> </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_factory.c"> + </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_registry.c"> + </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver.c"> </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver_factory.c"> diff --git a/vsprojects/vcxproj/grpc/grpc.vcxproj.filters b/vsprojects/vcxproj/grpc/grpc.vcxproj.filters index b334602e02..7026da89ab 100644 --- a/vsprojects/vcxproj/grpc/grpc.vcxproj.filters +++ b/vsprojects/vcxproj/grpc/grpc.vcxproj.filters @@ -106,6 +106,12 @@ <ClCompile Include="..\..\..\src\core\client_config\lb_policy.c"> <Filter>src\core\client_config</Filter> </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_factory.c"> + <Filter>src\core\client_config</Filter> + </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_registry.c"> + <Filter>src\core\client_config</Filter> + </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver.c"> <Filter>src\core\client_config</Filter> </ClCompile> @@ -530,6 +536,12 @@ <ClInclude Include="..\..\..\src\core\client_config\lb_policy.h"> <Filter>src\core\client_config</Filter> </ClInclude> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_factory.h"> + <Filter>src\core\client_config</Filter> + </ClInclude> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_registry.h"> + <Filter>src\core\client_config</Filter> + </ClInclude> <ClInclude Include="..\..\..\src\core\client_config\resolver.h"> <Filter>src\core\client_config</Filter> </ClInclude> diff --git a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj index f069aa5225..b48fa462e6 100644 --- a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj +++ b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj @@ -235,6 +235,8 @@ <ClInclude Include="..\..\..\src\core\client_config\connector.h" /> <ClInclude Include="..\..\..\src\core\client_config\lb_policies\pick_first.h" /> <ClInclude Include="..\..\..\src\core\client_config\lb_policy.h" /> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_factory.h" /> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_registry.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver_factory.h" /> <ClInclude Include="..\..\..\src\core\client_config\resolver_registry.h" /> @@ -357,6 +359,10 @@ </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\lb_policy.c"> </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_factory.c"> + </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_registry.c"> + </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver.c"> </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver_factory.c"> diff --git a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters index 35f8599cb9..8e3ee19b92 100644 --- a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters +++ b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters @@ -46,6 +46,12 @@ <ClCompile Include="..\..\..\src\core\client_config\lb_policy.c"> <Filter>src\core\client_config</Filter> </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_factory.c"> + <Filter>src\core\client_config</Filter> + </ClCompile> + <ClCompile Include="..\..\..\src\core\client_config\lb_policy_registry.c"> + <Filter>src\core\client_config</Filter> + </ClCompile> <ClCompile Include="..\..\..\src\core\client_config\resolver.c"> <Filter>src\core\client_config</Filter> </ClCompile> @@ -428,6 +434,12 @@ <ClInclude Include="..\..\..\src\core\client_config\lb_policy.h"> <Filter>src\core\client_config</Filter> </ClInclude> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_factory.h"> + <Filter>src\core\client_config</Filter> + </ClInclude> + <ClInclude Include="..\..\..\src\core\client_config\lb_policy_registry.h"> + <Filter>src\core\client_config</Filter> + </ClInclude> <ClInclude Include="..\..\..\src\core\client_config\resolver.h"> <Filter>src\core\client_config</Filter> </ClInclude> |