Shared subchannel sketch

reviewable/pr4859/r1
Craig Tiller 9 years ago
parent edc2fffb34
commit 694cf8b0d2
  1. 6
      BUILD
  2. 2
      Makefile
  3. 1
      binding.gyp
  4. 2
      build.yaml
  5. 3
      gRPC.podspec
  6. 1
      src/core/client_config/subchannel.c
  7. 155
      src/core/client_config/subchannel_index.c
  8. 39
      src/core/client_config/subchannel_index.h
  9. 3
      src/core/surface/init.c
  10. 1
      src/python/grpcio/grpc_core_dependencies.py
  11. 2
      tools/doxygen/Doxyfile.core.internal
  12. 6
      tools/run_tests/sources_and_headers.json
  13. 3
      vsprojects/vcxproj/grpc/grpc.vcxproj
  14. 6
      vsprojects/vcxproj/grpc/grpc.vcxproj.filters
  15. 3
      vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj
  16. 6
      vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters

@ -178,6 +178,7 @@ cc_library(
"src/core/client_config/resolvers/sockaddr_resolver.h",
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm_metadata.h",
"src/core/compression/message_compress.h",
@ -316,6 +317,7 @@ cc_library(
"src/core/client_config/resolvers/sockaddr_resolver.c",
"src/core/client_config/subchannel.c",
"src/core/client_config/subchannel_factory.c",
"src/core/client_config/subchannel_index.c",
"src/core/client_config/uri_parser.c",
"src/core/compression/algorithm.c",
"src/core/compression/message_compress.c",
@ -469,6 +471,7 @@ cc_library(
"src/core/client_config/resolvers/sockaddr_resolver.h",
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm_metadata.h",
"src/core/compression/message_compress.h",
@ -587,6 +590,7 @@ cc_library(
"src/core/client_config/resolvers/sockaddr_resolver.c",
"src/core/client_config/subchannel.c",
"src/core/client_config/subchannel_factory.c",
"src/core/client_config/subchannel_index.c",
"src/core/client_config/uri_parser.c",
"src/core/compression/algorithm.c",
"src/core/compression/message_compress.c",
@ -1121,6 +1125,7 @@ objc_library(
"src/core/client_config/resolvers/sockaddr_resolver.c",
"src/core/client_config/subchannel.c",
"src/core/client_config/subchannel_factory.c",
"src/core/client_config/subchannel_index.c",
"src/core/client_config/uri_parser.c",
"src/core/compression/algorithm.c",
"src/core/compression/message_compress.c",
@ -1272,6 +1277,7 @@ objc_library(
"src/core/client_config/resolvers/sockaddr_resolver.h",
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm_metadata.h",
"src/core/compression/message_compress.h",

@ -2349,6 +2349,7 @@ LIBGRPC_SRC = \
src/core/client_config/resolvers/sockaddr_resolver.c \
src/core/client_config/subchannel.c \
src/core/client_config/subchannel_factory.c \
src/core/client_config/subchannel_index.c \
src/core/client_config/uri_parser.c \
src/core/compression/algorithm.c \
src/core/compression/message_compress.c \
@ -2651,6 +2652,7 @@ LIBGRPC_UNSECURE_SRC = \
src/core/client_config/resolvers/sockaddr_resolver.c \
src/core/client_config/subchannel.c \
src/core/client_config/subchannel_factory.c \
src/core/client_config/subchannel_index.c \
src/core/client_config/uri_parser.c \
src/core/compression/algorithm.c \
src/core/compression/message_compress.c \

@ -210,6 +210,7 @@
'src/core/client_config/resolvers/sockaddr_resolver.c',
'src/core/client_config/subchannel.c',
'src/core/client_config/subchannel_factory.c',
'src/core/client_config/subchannel_index.c',
'src/core/client_config/uri_parser.c',
'src/core/compression/algorithm.c',
'src/core/compression/message_compress.c',

@ -132,6 +132,7 @@ filegroups:
- src/core/client_config/resolvers/sockaddr_resolver.h
- src/core/client_config/subchannel.h
- src/core/client_config/subchannel_factory.h
- src/core/client_config/subchannel_index.h
- src/core/client_config/uri_parser.h
- src/core/compression/algorithm_metadata.h
- src/core/compression/message_compress.h
@ -247,6 +248,7 @@ filegroups:
- src/core/client_config/resolvers/sockaddr_resolver.c
- src/core/client_config/subchannel.c
- src/core/client_config/subchannel_factory.c
- src/core/client_config/subchannel_index.c
- src/core/client_config/uri_parser.c
- src/core/compression/algorithm.c
- src/core/compression/message_compress.c

@ -182,6 +182,7 @@ Pod::Spec.new do |s|
'src/core/client_config/resolvers/sockaddr_resolver.h',
'src/core/client_config/subchannel.h',
'src/core/client_config/subchannel_factory.h',
'src/core/client_config/subchannel_index.h',
'src/core/client_config/uri_parser.h',
'src/core/compression/algorithm_metadata.h',
'src/core/compression/message_compress.h',
@ -327,6 +328,7 @@ Pod::Spec.new do |s|
'src/core/client_config/resolvers/sockaddr_resolver.c',
'src/core/client_config/subchannel.c',
'src/core/client_config/subchannel_factory.c',
'src/core/client_config/subchannel_index.c',
'src/core/client_config/uri_parser.c',
'src/core/compression/algorithm.c',
'src/core/compression/message_compress.c',
@ -480,6 +482,7 @@ Pod::Spec.new do |s|
'src/core/client_config/resolvers/sockaddr_resolver.h',
'src/core/client_config/subchannel.h',
'src/core/client_config/subchannel_factory.h',
'src/core/client_config/subchannel_index.h',
'src/core/client_config/uri_parser.h',
'src/core/compression/algorithm_metadata.h',
'src/core/compression/message_compress.h',

@ -36,6 +36,7 @@
#include <string.h>
#include <grpc/support/alloc.h>
#include <grpc/support/avl.h>
#include "src/core/channel/channel_args.h"
#include "src/core/channel/client_channel.h"

@ -0,0 +1,155 @@
/*
*
* 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.
*
*/
/* a map of subchannel_key --> subchannel, used for detecting connections
to the same destination in order to share them */
static gpr_avl g_subchannel_index;
static gpr_mu g_mu;
struct subchannel_key {
size_t addr_len;
struct sockaddr *addr;
grpc_channel_args *normalized_args;
};
GPR_TLS_DECL(subchannel_index_exec_ctx);
static subchannel_key *subchannel_key_create(struct sockaddr *sockaddr, size_t addr_len, grpc_channel_args *args) {
subchannel_key *k = gpr_malloc(sizeof(*k));
k->addr_len = addr_len;
k->addr = gpr_malloc(addr_len);
memcpy(k->addr, addr, addr_len);
k->normalized_args = grpc_channel_args_normalize(args);
return k;
}
static subchannel_key *subchannel_key_copy(subchannel_key *k) {
subchannel_key *k = gpr_malloc(sizeof(*k));
k->addr_len = addr_len;
k->addr = gpr_malloc(addr_len);
memcpy(k->addr, addr, addr_len);
k->normalized_args = grpc_channel_args_copy(args);
return k;
}
static int subchannel_key_compare(subchannel_key *a, subchannel_key *b) {
int c = GPR_ICMP(a->addr_len, b->addr_len);
if (c != 0) return c;
c = memcmp(a->addr, b->addr, a->addr_len);
if (c != 0) return c;
return grpc_channel_args_compare(a->normalized_args, b->normalized_args);
}
static void subchannel_key_destroy(subchannel_key *k) {
gpr_free(k->addr);
grpc_channel_args_destroy(k->normalized_args);
gpr_free(k);
}
static void sck_avl_destroy(void *p) {
subchannel_key_destroy(p);
}
static void *sck_avl_copy(void *p) {
return subchannel_key_copy(p);
}
static void *sck_avl_compare(void *a, void *b) {
return subchannel_key_compare(a, b);
}
static void scv_avl_destroy(void *p) {
GRPC_SUBCHANNEL_UNREF(exec_ctx, p, "subchannel_index");
}
static void *scv_avl_copy(void *p) {
GRPC_SUBCHANNEL_REF(p, "subchannel_index");
return p;
}
static const gpr_avl_vtable subchannel_avl_vtable = {
.destroy_key = sck_avl_destroy,
.copy_key = sck_avl_copy,
.compare_keys = sck_avl_compare,
.destroy_value = scv_avl_destroy,
.copy_value = scv_avl_copy
};
grpc_subchannel *grpc_subchannel_index_find(
grpc_exec_ctx *ctx,
grpc_connector *connector,
grpc_subchannel_args *args) {
gpr_mu_lock(&g_mu);
gpr_avl index = gpr_avl_ref(g_subchannel_index);
gpr_mu_unlock(&g_mu);
subchannel_key *key = subchannel_key_create(connector, args);
grpc_subchannel *c = grpc_subchannel_ref(gpr_avl_get(index, key));
subchannel_key_destroy(key);
gpr_avl_unref(index);
return c;
}
grpc_subchannel *grpc_subchannel_index_register(
grpc_exec_ctx *ctx,
grpc_connector *connector,
grpc_subchannel_args *args,
grpc_subchannel *constructed) {
subchannel_key *key = subchannel_key_create(connector, args);
grpc_subchannel *c = NULL;
while (c == NULL) {
gpr_mu_lock(&g_mu);
gpr_avl index = gpr_avl_ref(g_subchannel_index);
gpr_mu_unlock(&g_mu);
c = gpr_avl_get(index, key);
if (c != NULL) {
GRPC_SUBCHANNEL_UNREF(constructed);
} else {
gpr_avl updated = gpr_avl_add(index, key, constructed);
gpr_mu_lock(&g_mu);
if (index.root == g_subchannel_index.root) {
GPR_SWAP(index, g_subchannel_index);
c = constructed;
}
gpr_mu_unlock(&g_mu);
}
gpr_avl_unref(index);
}
return c;
}

@ -0,0 +1,39 @@
/*
*
* 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_SUBCHANNEL_INDEX_H
#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_INDEX_H
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_INDEX_H */

@ -46,6 +46,7 @@
#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"
#include "src/core/client_config/subchannel.h"
#include "src/core/debug/trace.h"
#include "src/core/iomgr/executor.h"
#include "src/core/iomgr/iomgr.h"
@ -125,6 +126,7 @@ void grpc_init(void) {
}
gpr_timers_global_init();
grpc_cq_global_init();
grpc_subchannel_global_init();
for (i = 0; i < g_number_of_plugins; i++) {
if (g_all_of_the_plugins[i].init != NULL) {
g_all_of_the_plugins[i].init();
@ -143,6 +145,7 @@ void grpc_shutdown(void) {
grpc_executor_shutdown();
grpc_cq_global_shutdown();
grpc_iomgr_shutdown();
grpc_subchannel_global_shutdown();
census_shutdown();
gpr_timers_global_destroy();
grpc_tracer_shutdown();

@ -119,6 +119,7 @@ CORE_SOURCE_FILES = [
'src/core/client_config/resolvers/sockaddr_resolver.c',
'src/core/client_config/subchannel.c',
'src/core/client_config/subchannel_factory.c',
'src/core/client_config/subchannel_index.c',
'src/core/client_config/uri_parser.c',
'src/core/compression/algorithm.c',
'src/core/compression/message_compress.c',

@ -807,6 +807,7 @@ src/core/client_config/resolvers/dns_resolver.h \
src/core/client_config/resolvers/sockaddr_resolver.h \
src/core/client_config/subchannel.h \
src/core/client_config/subchannel_factory.h \
src/core/client_config/subchannel_index.h \
src/core/client_config/uri_parser.h \
src/core/compression/algorithm_metadata.h \
src/core/compression/message_compress.h \
@ -945,6 +946,7 @@ src/core/client_config/resolvers/dns_resolver.c \
src/core/client_config/resolvers/sockaddr_resolver.c \
src/core/client_config/subchannel.c \
src/core/client_config/subchannel_factory.c \
src/core/client_config/subchannel_index.c \
src/core/client_config/uri_parser.c \
src/core/compression/algorithm.c \
src/core/compression/message_compress.c \

@ -2885,6 +2885,7 @@
"src/core/client_config/resolvers/sockaddr_resolver.h",
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm_metadata.h",
"src/core/compression/message_compress.h",
@ -3057,6 +3058,8 @@
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.c",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.c",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.c",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm.c",
@ -3397,6 +3400,7 @@
"src/core/client_config/resolvers/sockaddr_resolver.h",
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm_metadata.h",
"src/core/compression/message_compress.h",
@ -3554,6 +3558,8 @@
"src/core/client_config/subchannel.h",
"src/core/client_config/subchannel_factory.c",
"src/core/client_config/subchannel_factory.h",
"src/core/client_config/subchannel_index.c",
"src/core/client_config/subchannel_index.h",
"src/core/client_config/uri_parser.c",
"src/core/client_config/uri_parser.h",
"src/core/compression/algorithm.c",

@ -314,6 +314,7 @@
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\resolvers\sockaddr_resolver.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\uri_parser.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\compression\algorithm_metadata.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\compression\message_compress.h" />
@ -502,6 +503,8 @@
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\uri_parser.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\compression\algorithm.c">

@ -145,6 +145,9 @@
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\uri_parser.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
@ -596,6 +599,9 @@
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.h">
<Filter>src\core\client_config</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.h">
<Filter>src\core\client_config</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\uri_parser.h">
<Filter>src\core\client_config</Filter>
</ClInclude>

@ -292,6 +292,7 @@
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\resolvers\sockaddr_resolver.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\uri_parser.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\compression\algorithm_metadata.h" />
<ClInclude Include="$(SolutionDir)\..\src\core\compression\message_compress.h" />
@ -440,6 +441,8 @@
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\uri_parser.c">
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\compression\algorithm.c">

@ -85,6 +85,9 @@
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
<ClCompile Include="$(SolutionDir)\..\src\core\client_config\uri_parser.c">
<Filter>src\core\client_config</Filter>
</ClCompile>
@ -491,6 +494,9 @@
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_factory.h">
<Filter>src\core\client_config</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\subchannel_index.h">
<Filter>src\core\client_config</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\src\core\client_config\uri_parser.h">
<Filter>src\core\client_config</Filter>
</ClInclude>

Loading…
Cancel
Save