Expose channel stack type to builder (#29088)

* split builder

* expose channel stack type to builder

* Automated change: Fix sanity tests

* Update channel_stack_builder_impl.h

* Update channel_init.h

Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/29118/head
Craig Tiller 3 years ago committed by GitHub
parent 28f1453ad5
commit 0ea1eeb4e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      BUILD
  2. 2
      CMakeLists.txt
  3. 2
      Makefile
  4. 4
      build_autogenerated.yaml
  5. 1
      config.m4
  6. 1
      config.w32
  7. 2
      gRPC-C++.podspec
  8. 3
      gRPC-Core.podspec
  9. 2
      grpc.gemspec
  10. 2
      grpc.gyp
  11. 2
      package.xml
  12. 6
      src/core/ext/filters/client_channel/subchannel.cc
  13. 65
      src/core/lib/channel/channel_stack_builder.cc
  14. 33
      src/core/lib/channel/channel_stack_builder.h
  15. 102
      src/core/lib/channel/channel_stack_builder_impl.cc
  16. 48
      src/core/lib/channel/channel_stack_builder_impl.h
  17. 1
      src/core/lib/channel/connected_channel.h
  18. 7
      src/core/lib/surface/channel.cc
  19. 5
      src/core/lib/surface/channel_init.cc
  20. 8
      src/core/lib/surface/channel_init.h
  21. 1
      src/python/grpcio/grpc_core_dependencies.py
  22. 4
      test/core/channel/channel_stack_builder_test.cc
  23. 7
      test/core/channel/minimal_stack_is_minimal_test.cc
  24. 1
      test/core/end2end/tests/filter_causes_close.cc
  25. 1
      test/core/end2end/tests/filter_context.cc
  26. 1
      test/core/end2end/tests/filter_init_fails.cc
  27. 1
      test/core/end2end/tests/filter_latency.cc
  28. 11
      test/core/xds/xds_channel_stack_modifier_test.cc
  29. 3
      test/cpp/microbenchmarks/bm_call_create.cc
  30. 2
      tools/doxygen/Doxyfile.c++.internal
  31. 2
      tools/doxygen/Doxyfile.core.internal

26
BUILD

@ -1875,7 +1875,7 @@ grpc_cc_library(
"src/core/lib/address_utils/parse_address.cc", "src/core/lib/address_utils/parse_address.cc",
"src/core/lib/backoff/backoff.cc", "src/core/lib/backoff/backoff.cc",
"src/core/lib/channel/channel_stack.cc", "src/core/lib/channel/channel_stack.cc",
"src/core/lib/channel/channel_stack_builder.cc", "src/core/lib/channel/channel_stack_builder_impl.cc",
"src/core/lib/channel/channel_trace.cc", "src/core/lib/channel/channel_trace.cc",
"src/core/lib/channel/channelz.cc", "src/core/lib/channel/channelz.cc",
"src/core/lib/channel/channelz_registry.cc", "src/core/lib/channel/channelz_registry.cc",
@ -2017,7 +2017,7 @@ grpc_cc_library(
"src/core/lib/channel/call_tracer.h", "src/core/lib/channel/call_tracer.h",
"src/core/lib/channel/channel_stack.h", "src/core/lib/channel/channel_stack.h",
"src/core/lib/channel/promise_based_filter.h", "src/core/lib/channel/promise_based_filter.h",
"src/core/lib/channel/channel_stack_builder.h", "src/core/lib/channel/channel_stack_builder_impl.h",
"src/core/lib/channel/channel_trace.h", "src/core/lib/channel/channel_trace.h",
"src/core/lib/channel/channelz.h", "src/core/lib/channel/channelz.h",
"src/core/lib/channel/channelz_registry.h", "src/core/lib/channel/channelz_registry.h",
@ -2132,6 +2132,7 @@ grpc_cc_library(
"src/core/lib/iomgr/combiner.h", "src/core/lib/iomgr/combiner.h",
"src/core/lib/iomgr/iomgr_internal.h", "src/core/lib/iomgr/iomgr_internal.h",
"src/core/lib/channel/channel_args.h", "src/core/lib/channel/channel_args.h",
"src/core/lib/channel/channel_stack_builder.h",
] + ] +
# TODO(hork): delete the iomgr glue code when EventEngine is fully # TODO(hork): delete the iomgr glue code when EventEngine is fully
# integrated, or when it becomes obvious the glue code is unnecessary. # integrated, or when it becomes obvious the glue code is unnecessary.
@ -2164,6 +2165,7 @@ grpc_cc_library(
"avl", "avl",
"bitset", "bitset",
"channel_args", "channel_args",
"channel_stack_builder",
"channel_stack_type", "channel_stack_type",
"chunked_vector", "chunked_vector",
"closure", "closure",
@ -2224,11 +2226,31 @@ grpc_cc_library(
], ],
language = "c++", language = "c++",
deps = [ deps = [
"channel_stack_builder",
"channel_stack_type", "channel_stack_type",
"gpr_base", "gpr_base",
], ],
) )
grpc_cc_library(
name = "channel_stack_builder",
srcs = [
"src/core/lib/channel/channel_stack_builder.cc",
],
hdrs = [
"src/core/lib/channel/channel_stack_builder.h",
],
language = "c++",
visibility = ["@grpc:alt_grpc_base_legacy"],
deps = [
"channel_args",
"channel_stack_type",
"closure",
"error",
"gpr_base",
],
)
grpc_cc_library( grpc_cc_library(
name = "grpc_common", name = "grpc_common",
defines = select({ defines = select({

2
CMakeLists.txt generated

@ -1838,6 +1838,7 @@ add_library(grpc
src/core/lib/channel/channel_args_preconditioning.cc src/core/lib/channel/channel_args_preconditioning.cc
src/core/lib/channel/channel_stack.cc src/core/lib/channel/channel_stack.cc
src/core/lib/channel/channel_stack_builder.cc src/core/lib/channel/channel_stack_builder.cc
src/core/lib/channel/channel_stack_builder_impl.cc
src/core/lib/channel/channel_trace.cc src/core/lib/channel/channel_trace.cc
src/core/lib/channel/channelz.cc src/core/lib/channel/channelz.cc
src/core/lib/channel/channelz_registry.cc src/core/lib/channel/channelz_registry.cc
@ -2484,6 +2485,7 @@ add_library(grpc_unsecure
src/core/lib/channel/channel_args_preconditioning.cc src/core/lib/channel/channel_args_preconditioning.cc
src/core/lib/channel/channel_stack.cc src/core/lib/channel/channel_stack.cc
src/core/lib/channel/channel_stack_builder.cc src/core/lib/channel/channel_stack_builder.cc
src/core/lib/channel/channel_stack_builder_impl.cc
src/core/lib/channel/channel_trace.cc src/core/lib/channel/channel_trace.cc
src/core/lib/channel/channelz.cc src/core/lib/channel/channelz.cc
src/core/lib/channel/channelz_registry.cc src/core/lib/channel/channelz_registry.cc

2
Makefile generated

@ -1425,6 +1425,7 @@ LIBGRPC_SRC = \
src/core/lib/channel/channel_args_preconditioning.cc \ src/core/lib/channel/channel_args_preconditioning.cc \
src/core/lib/channel/channel_stack.cc \ src/core/lib/channel/channel_stack.cc \
src/core/lib/channel/channel_stack_builder.cc \ src/core/lib/channel/channel_stack_builder.cc \
src/core/lib/channel/channel_stack_builder_impl.cc \
src/core/lib/channel/channel_trace.cc \ src/core/lib/channel/channel_trace.cc \
src/core/lib/channel/channelz.cc \ src/core/lib/channel/channelz.cc \
src/core/lib/channel/channelz_registry.cc \ src/core/lib/channel/channelz_registry.cc \
@ -1920,6 +1921,7 @@ LIBGRPC_UNSECURE_SRC = \
src/core/lib/channel/channel_args_preconditioning.cc \ src/core/lib/channel/channel_args_preconditioning.cc \
src/core/lib/channel/channel_stack.cc \ src/core/lib/channel/channel_stack.cc \
src/core/lib/channel/channel_stack_builder.cc \ src/core/lib/channel/channel_stack_builder.cc \
src/core/lib/channel/channel_stack_builder_impl.cc \
src/core/lib/channel/channel_trace.cc \ src/core/lib/channel/channel_trace.cc \
src/core/lib/channel/channelz.cc \ src/core/lib/channel/channelz.cc \
src/core/lib/channel/channelz_registry.cc \ src/core/lib/channel/channelz_registry.cc \

@ -702,6 +702,7 @@ libs:
- src/core/lib/channel/channel_args_preconditioning.h - src/core/lib/channel/channel_args_preconditioning.h
- src/core/lib/channel/channel_stack.h - src/core/lib/channel/channel_stack.h
- src/core/lib/channel/channel_stack_builder.h - src/core/lib/channel/channel_stack_builder.h
- src/core/lib/channel/channel_stack_builder_impl.h
- src/core/lib/channel/channel_trace.h - src/core/lib/channel/channel_trace.h
- src/core/lib/channel/channelz.h - src/core/lib/channel/channelz.h
- src/core/lib/channel/channelz_registry.h - src/core/lib/channel/channelz_registry.h
@ -1367,6 +1368,7 @@ libs:
- src/core/lib/channel/channel_args_preconditioning.cc - src/core/lib/channel/channel_args_preconditioning.cc
- src/core/lib/channel/channel_stack.cc - src/core/lib/channel/channel_stack.cc
- src/core/lib/channel/channel_stack_builder.cc - src/core/lib/channel/channel_stack_builder.cc
- src/core/lib/channel/channel_stack_builder_impl.cc
- src/core/lib/channel/channel_trace.cc - src/core/lib/channel/channel_trace.cc
- src/core/lib/channel/channelz.cc - src/core/lib/channel/channelz.cc
- src/core/lib/channel/channelz_registry.cc - src/core/lib/channel/channelz_registry.cc
@ -1879,6 +1881,7 @@ libs:
- src/core/lib/channel/channel_args_preconditioning.h - src/core/lib/channel/channel_args_preconditioning.h
- src/core/lib/channel/channel_stack.h - src/core/lib/channel/channel_stack.h
- src/core/lib/channel/channel_stack_builder.h - src/core/lib/channel/channel_stack_builder.h
- src/core/lib/channel/channel_stack_builder_impl.h
- src/core/lib/channel/channel_trace.h - src/core/lib/channel/channel_trace.h
- src/core/lib/channel/channelz.h - src/core/lib/channel/channelz.h
- src/core/lib/channel/channelz_registry.h - src/core/lib/channel/channelz_registry.h
@ -2198,6 +2201,7 @@ libs:
- src/core/lib/channel/channel_args_preconditioning.cc - src/core/lib/channel/channel_args_preconditioning.cc
- src/core/lib/channel/channel_stack.cc - src/core/lib/channel/channel_stack.cc
- src/core/lib/channel/channel_stack_builder.cc - src/core/lib/channel/channel_stack_builder.cc
- src/core/lib/channel/channel_stack_builder_impl.cc
- src/core/lib/channel/channel_trace.cc - src/core/lib/channel/channel_trace.cc
- src/core/lib/channel/channelz.cc - src/core/lib/channel/channelz.cc
- src/core/lib/channel/channelz_registry.cc - src/core/lib/channel/channelz_registry.cc

1
config.m4 generated

@ -443,6 +443,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/channel/channel_args_preconditioning.cc \ src/core/lib/channel/channel_args_preconditioning.cc \
src/core/lib/channel/channel_stack.cc \ src/core/lib/channel/channel_stack.cc \
src/core/lib/channel/channel_stack_builder.cc \ src/core/lib/channel/channel_stack_builder.cc \
src/core/lib/channel/channel_stack_builder_impl.cc \
src/core/lib/channel/channel_trace.cc \ src/core/lib/channel/channel_trace.cc \
src/core/lib/channel/channelz.cc \ src/core/lib/channel/channelz.cc \
src/core/lib/channel/channelz_registry.cc \ src/core/lib/channel/channelz_registry.cc \

1
config.w32 generated

@ -409,6 +409,7 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\channel\\channel_args_preconditioning.cc " + "src\\core\\lib\\channel\\channel_args_preconditioning.cc " +
"src\\core\\lib\\channel\\channel_stack.cc " + "src\\core\\lib\\channel\\channel_stack.cc " +
"src\\core\\lib\\channel\\channel_stack_builder.cc " + "src\\core\\lib\\channel\\channel_stack_builder.cc " +
"src\\core\\lib\\channel\\channel_stack_builder_impl.cc " +
"src\\core\\lib\\channel\\channel_trace.cc " + "src\\core\\lib\\channel\\channel_trace.cc " +
"src\\core\\lib\\channel\\channelz.cc " + "src\\core\\lib\\channel\\channelz.cc " +
"src\\core\\lib\\channel\\channelz_registry.cc " + "src\\core\\lib\\channel\\channelz_registry.cc " +

2
gRPC-C++.podspec generated

@ -642,6 +642,7 @@ Pod::Spec.new do |s|
'src/core/lib/channel/channel_args_preconditioning.h', 'src/core/lib/channel/channel_args_preconditioning.h',
'src/core/lib/channel/channel_stack.h', 'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h', 'src/core/lib/channel/channel_stack_builder.h',
'src/core/lib/channel/channel_stack_builder_impl.h',
'src/core/lib/channel/channel_trace.h', 'src/core/lib/channel/channel_trace.h',
'src/core/lib/channel/channelz.h', 'src/core/lib/channel/channelz.h',
'src/core/lib/channel/channelz_registry.h', 'src/core/lib/channel/channelz_registry.h',
@ -1446,6 +1447,7 @@ Pod::Spec.new do |s|
'src/core/lib/channel/channel_args_preconditioning.h', 'src/core/lib/channel/channel_args_preconditioning.h',
'src/core/lib/channel/channel_stack.h', 'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h', 'src/core/lib/channel/channel_stack_builder.h',
'src/core/lib/channel/channel_stack_builder_impl.h',
'src/core/lib/channel/channel_trace.h', 'src/core/lib/channel/channel_trace.h',
'src/core/lib/channel/channelz.h', 'src/core/lib/channel/channelz.h',
'src/core/lib/channel/channelz_registry.h', 'src/core/lib/channel/channelz_registry.h',

3
gRPC-Core.podspec generated

@ -984,6 +984,8 @@ Pod::Spec.new do |s|
'src/core/lib/channel/channel_stack.h', 'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.cc', 'src/core/lib/channel/channel_stack_builder.cc',
'src/core/lib/channel/channel_stack_builder.h', 'src/core/lib/channel/channel_stack_builder.h',
'src/core/lib/channel/channel_stack_builder_impl.cc',
'src/core/lib/channel/channel_stack_builder_impl.h',
'src/core/lib/channel/channel_trace.cc', 'src/core/lib/channel/channel_trace.cc',
'src/core/lib/channel/channel_trace.h', 'src/core/lib/channel/channel_trace.h',
'src/core/lib/channel/channelz.cc', 'src/core/lib/channel/channelz.cc',
@ -2045,6 +2047,7 @@ Pod::Spec.new do |s|
'src/core/lib/channel/channel_args_preconditioning.h', 'src/core/lib/channel/channel_args_preconditioning.h',
'src/core/lib/channel/channel_stack.h', 'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h', 'src/core/lib/channel/channel_stack_builder.h',
'src/core/lib/channel/channel_stack_builder_impl.h',
'src/core/lib/channel/channel_trace.h', 'src/core/lib/channel/channel_trace.h',
'src/core/lib/channel/channelz.h', 'src/core/lib/channel/channelz.h',
'src/core/lib/channel/channelz_registry.h', 'src/core/lib/channel/channelz_registry.h',

2
grpc.gemspec generated

@ -903,6 +903,8 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/channel/channel_stack.h ) s.files += %w( src/core/lib/channel/channel_stack.h )
s.files += %w( src/core/lib/channel/channel_stack_builder.cc ) s.files += %w( src/core/lib/channel/channel_stack_builder.cc )
s.files += %w( src/core/lib/channel/channel_stack_builder.h ) s.files += %w( src/core/lib/channel/channel_stack_builder.h )
s.files += %w( src/core/lib/channel/channel_stack_builder_impl.cc )
s.files += %w( src/core/lib/channel/channel_stack_builder_impl.h )
s.files += %w( src/core/lib/channel/channel_trace.cc ) s.files += %w( src/core/lib/channel/channel_trace.cc )
s.files += %w( src/core/lib/channel/channel_trace.h ) s.files += %w( src/core/lib/channel/channel_trace.h )
s.files += %w( src/core/lib/channel/channelz.cc ) s.files += %w( src/core/lib/channel/channelz.cc )

2
grpc.gyp generated

@ -773,6 +773,7 @@
'src/core/lib/channel/channel_args_preconditioning.cc', 'src/core/lib/channel/channel_args_preconditioning.cc',
'src/core/lib/channel/channel_stack.cc', 'src/core/lib/channel/channel_stack.cc',
'src/core/lib/channel/channel_stack_builder.cc', 'src/core/lib/channel/channel_stack_builder.cc',
'src/core/lib/channel/channel_stack_builder_impl.cc',
'src/core/lib/channel/channel_trace.cc', 'src/core/lib/channel/channel_trace.cc',
'src/core/lib/channel/channelz.cc', 'src/core/lib/channel/channelz.cc',
'src/core/lib/channel/channelz_registry.cc', 'src/core/lib/channel/channelz_registry.cc',
@ -1239,6 +1240,7 @@
'src/core/lib/channel/channel_args_preconditioning.cc', 'src/core/lib/channel/channel_args_preconditioning.cc',
'src/core/lib/channel/channel_stack.cc', 'src/core/lib/channel/channel_stack.cc',
'src/core/lib/channel/channel_stack_builder.cc', 'src/core/lib/channel/channel_stack_builder.cc',
'src/core/lib/channel/channel_stack_builder_impl.cc',
'src/core/lib/channel/channel_trace.cc', 'src/core/lib/channel/channel_trace.cc',
'src/core/lib/channel/channelz.cc', 'src/core/lib/channel/channelz.cc',
'src/core/lib/channel/channelz_registry.cc', 'src/core/lib/channel/channelz_registry.cc',

2
package.xml generated

@ -883,6 +883,8 @@
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack.h" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channel_stack.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder.cc" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder.h" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder_impl.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack_builder_impl.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_trace.cc" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channel_trace.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_trace.h" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channel_trace.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channelz.cc" role="src" /> <file baseinstalldir="/" name="src/core/lib/channel/channelz.cc" role="src" />

@ -38,6 +38,7 @@
#include "src/core/lib/address_utils/sockaddr_utils.h" #include "src/core/lib/address_utils/sockaddr_utils.h"
#include "src/core/lib/backoff/backoff.h" #include "src/core/lib/backoff/backoff.h"
#include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/channel/connected_channel.h" #include "src/core/lib/channel/connected_channel.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/debug/stats.h" #include "src/core/lib/debug/stats.h"
@ -966,11 +967,10 @@ void ConnectionDestroy(void* arg, grpc_error_handle /*error*/) {
bool Subchannel::PublishTransportLocked() { bool Subchannel::PublishTransportLocked() {
// Construct channel stack. // Construct channel stack.
ChannelStackBuilder builder("subchannel"); ChannelStackBuilderImpl builder("subchannel", GRPC_CLIENT_SUBCHANNEL);
builder.SetChannelArgs(connecting_result_.channel_args) builder.SetChannelArgs(connecting_result_.channel_args)
.SetTransport(connecting_result_.transport); .SetTransport(connecting_result_.transport);
if (!CoreConfiguration::Get().channel_init().CreateStack( if (!CoreConfiguration::Get().channel_init().CreateStack(&builder)) {
&builder, GRPC_CLIENT_SUBCHANNEL)) {
return false; return false;
} }
grpc_channel_stack* stk; grpc_channel_stack* stk;

@ -61,69 +61,4 @@ void ChannelStackBuilder::AppendFilter(const grpc_channel_filter* filter,
stack_.push_back({filter, std::move(post_init)}); stack_.push_back({filter, std::move(post_init)});
} }
grpc_error_handle ChannelStackBuilder::Build(size_t prefix_bytes,
int initial_refs,
grpc_iomgr_cb_func destroy,
void* destroy_arg, void** result) {
// create an array of filters
std::vector<const grpc_channel_filter*> filters;
filters.reserve(stack_.size());
for (const auto& elem : stack_) {
filters.push_back(elem.filter);
}
// calculate the size of the channel stack
size_t channel_stack_size =
grpc_channel_stack_size(filters.data(), filters.size());
// allocate memory, with prefix_bytes followed by channel_stack_size
*result = gpr_zalloc(prefix_bytes + channel_stack_size);
// fetch a pointer to the channel stack
grpc_channel_stack* channel_stack = reinterpret_cast<grpc_channel_stack*>(
static_cast<char*>(*result) + prefix_bytes);
const grpc_channel_args* final_args;
if (transport_ != nullptr) {
static const grpc_arg_pointer_vtable vtable = {
// copy
[](void* p) { return p; },
// destroy
[](void*) {},
// cmp
[](void* a, void* b) { return QsortCompare(a, b); },
};
grpc_arg arg = grpc_channel_arg_pointer_create(
const_cast<char*>(GRPC_ARG_TRANSPORT), transport_, &vtable);
final_args = grpc_channel_args_copy_and_add(args_, &arg, 1);
} else {
final_args = args_;
}
// and initialize it
grpc_error_handle error = grpc_channel_stack_init(
initial_refs, destroy, destroy_arg == nullptr ? *result : destroy_arg,
filters.data(), filters.size(), final_args, name_, channel_stack);
if (final_args != args_) {
grpc_channel_args_destroy(final_args);
}
if (error != GRPC_ERROR_NONE) {
grpc_channel_stack_destroy(channel_stack);
gpr_free(*result);
*result = nullptr;
return error;
}
// run post-initialization functions
for (size_t i = 0; i < filters.size(); i++) {
if (stack_[i].post_init != nullptr) {
stack_[i].post_init(channel_stack,
grpc_channel_stack_element(channel_stack, i));
}
}
return GRPC_ERROR_NONE;
}
} // namespace grpc_core } // namespace grpc_core

@ -19,8 +19,20 @@
#include <stdbool.h> #include <stdbool.h>
#include <functional>
#include <vector>
#include "absl/strings/string_view.h"
#include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack.h" #include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/surface/channel_stack_type.h"
typedef struct grpc_channel_stack grpc_channel_stack;
typedef struct grpc_channel_element grpc_channel_element;
typedef struct grpc_channel_filter grpc_channel_filter;
typedef struct grpc_transport grpc_transport;
namespace grpc_core { namespace grpc_core {
@ -43,9 +55,10 @@ class ChannelStackBuilder {
}; };
// Initialize with a name. // Initialize with a name.
explicit ChannelStackBuilder(const char* name) : name_(name) {} ChannelStackBuilder(const char* name, grpc_channel_stack_type type)
: name_(name), type_(type) {}
~ChannelStackBuilder(); const char* name() const { return name_; }
// Set the target string. // Set the target string.
ChannelStackBuilder& SetTarget(const char* target); ChannelStackBuilder& SetTarget(const char* target);
@ -72,6 +85,9 @@ class ChannelStackBuilder {
// Mutable vector of proposed stack entries. // Mutable vector of proposed stack entries.
std::vector<StackEntry>* mutable_stack() { return &stack_; } std::vector<StackEntry>* mutable_stack() { return &stack_; }
// The type of channel stack being built.
grpc_channel_stack_type channel_stack_type() const { return type_; }
// Helper to add a filter to the front of the stack. // Helper to add a filter to the front of the stack.
void PrependFilter(const grpc_channel_filter* filter, PostInitFunc post_init); void PrependFilter(const grpc_channel_filter* filter, PostInitFunc post_init);
@ -83,15 +99,20 @@ class ChannelStackBuilder {
// prefix_bytes are allocated before the channel stack, // prefix_bytes are allocated before the channel stack,
// initial_refs, destroy, destroy_arg are as per grpc_channel_stack_init // initial_refs, destroy, destroy_arg are as per grpc_channel_stack_init
// On failure, *result is nullptr. // On failure, *result is nullptr.
grpc_error_handle Build(size_t prefix_bytes, int initial_refs, virtual grpc_error_handle Build(size_t prefix_bytes, int initial_refs,
grpc_iomgr_cb_func destroy, void* destroy_arg, grpc_iomgr_cb_func destroy, void* destroy_arg,
void** result); void** result) = 0;
protected:
~ChannelStackBuilder();
private: private:
static std::string unknown_target() { return "unknown"; } static std::string unknown_target() { return "unknown"; }
// The name of the stack // The name of the stack
const char* const name_; const char* const name_;
// The type of stack being built
const grpc_channel_stack_type type_;
// The target // The target
std::string target_{unknown_target()}; std::string target_{unknown_target()};
// The transport // The transport

@ -0,0 +1,102 @@
/*
*
* Copyright 2016 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <grpc/support/port_platform.h>
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include <string.h>
#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/gprpp/memory.h"
namespace grpc_core {
grpc_error_handle ChannelStackBuilderImpl::Build(size_t prefix_bytes,
int initial_refs,
grpc_iomgr_cb_func destroy,
void* destroy_arg,
void** result) {
auto* stack = mutable_stack();
// create an array of filters
std::vector<const grpc_channel_filter*> filters;
filters.reserve(stack->size());
for (const auto& elem : *stack) {
filters.push_back(elem.filter);
}
// calculate the size of the channel stack
size_t channel_stack_size =
grpc_channel_stack_size(filters.data(), filters.size());
// allocate memory, with prefix_bytes followed by channel_stack_size
*result = gpr_zalloc(prefix_bytes + channel_stack_size);
// fetch a pointer to the channel stack
grpc_channel_stack* channel_stack = reinterpret_cast<grpc_channel_stack*>(
static_cast<char*>(*result) + prefix_bytes);
const grpc_channel_args* final_args;
if (transport() != nullptr) {
static const grpc_arg_pointer_vtable vtable = {
// copy
[](void* p) { return p; },
// destroy
[](void*) {},
// cmp
[](void* a, void* b) { return QsortCompare(a, b); },
};
grpc_arg arg = grpc_channel_arg_pointer_create(
const_cast<char*>(GRPC_ARG_TRANSPORT), transport(), &vtable);
final_args = grpc_channel_args_copy_and_add(channel_args(), &arg, 1);
} else {
final_args = channel_args();
}
// and initialize it
grpc_error_handle error = grpc_channel_stack_init(
initial_refs, destroy, destroy_arg == nullptr ? *result : destroy_arg,
filters.data(), filters.size(), final_args, name(), channel_stack);
if (final_args != channel_args()) {
grpc_channel_args_destroy(final_args);
}
if (error != GRPC_ERROR_NONE) {
grpc_channel_stack_destroy(channel_stack);
gpr_free(*result);
*result = nullptr;
return error;
}
// run post-initialization functions
for (size_t i = 0; i < filters.size(); i++) {
if ((*stack)[i].post_init != nullptr) {
(*stack)[i].post_init(channel_stack,
grpc_channel_stack_element(channel_stack, i));
}
}
return GRPC_ERROR_NONE;
}
} // namespace grpc_core

@ -0,0 +1,48 @@
// Copyright 2016 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_IMPL_H
#define GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_IMPL_H
#include <grpc/support/port_platform.h>
#include <stdbool.h>
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h"
namespace grpc_core {
// Build a channel stack.
// Allows interested parties to add filters to the stack, and to query an
// in-progress build.
// Carries some useful context for the channel stack, such as a target string
// and a transport.
class ChannelStackBuilderImpl final : public ChannelStackBuilder {
public:
using ChannelStackBuilder::ChannelStackBuilder;
// Build the channel stack.
// After success, *result holds the new channel stack,
// prefix_bytes are allocated before the channel stack,
// initial_refs, destroy, destroy_arg are as per grpc_channel_stack_init
// On failure, *result is nullptr.
grpc_error_handle Build(size_t prefix_bytes, int initial_refs,
grpc_iomgr_cb_func destroy, void* destroy_arg,
void** result) override;
};
} // namespace grpc_core
#endif // GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_IMPL_H

@ -21,6 +21,7 @@
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder.h"
extern const grpc_channel_filter grpc_connected_filter; extern const grpc_channel_filter grpc_connected_filter;

@ -31,6 +31,7 @@
#include <grpc/support/string_util.h> #include <grpc/support/string_util.h>
#include "src/core/lib/channel/channel_args.h" #include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/channel/channel_trace.h" #include "src/core/lib/channel/channel_trace.h"
#include "src/core/lib/channel/channelz.h" #include "src/core/lib/channel/channelz.h"
#include "src/core/lib/channel/channelz_registry.h" #include "src/core/lib/channel/channelz_registry.h"
@ -241,8 +242,8 @@ grpc_channel* grpc_channel_create_internal(
// grpc_shutdown() when the channel is actually destroyed, thus // grpc_shutdown() when the channel is actually destroyed, thus
// ensuring that shutdown is deferred until that point. // ensuring that shutdown is deferred until that point.
grpc_init(); grpc_init();
grpc_core::ChannelStackBuilder builder( grpc_core::ChannelStackBuilderImpl builder(
grpc_channel_stack_type_string(channel_stack_type)); grpc_channel_stack_type_string(channel_stack_type), channel_stack_type);
const grpc_core::UniquePtr<char> default_authority = const grpc_core::UniquePtr<char> default_authority =
get_default_authority(input_args); get_default_authority(input_args);
grpc_channel_args* args = grpc_channel_args* args =
@ -258,7 +259,7 @@ grpc_channel* grpc_channel_create_internal(
optional_transport); optional_transport);
grpc_channel_args_destroy(args); grpc_channel_args_destroy(args);
if (!grpc_core::CoreConfiguration::Get().channel_init().CreateStack( if (!grpc_core::CoreConfiguration::Get().channel_init().CreateStack(
&builder, channel_stack_type)) { &builder)) {
grpc_shutdown(); // Since we won't call destroy_channel(). grpc_shutdown(); // Since we won't call destroy_channel().
return nullptr; return nullptr;
} }

@ -45,9 +45,8 @@ ChannelInit ChannelInit::Builder::Build() {
return result; return result;
} }
bool ChannelInit::CreateStack(ChannelStackBuilder* builder, bool ChannelInit::CreateStack(ChannelStackBuilder* builder) const {
grpc_channel_stack_type type) const { for (const auto& stage : slots_[builder->channel_stack_type()]) {
for (const auto& stage : slots_[type]) {
if (!stage(builder)) return false; if (!stage(builder)) return false;
} }
return true; return true;

@ -24,7 +24,7 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
#include "src/core/lib/surface/channel_stack_type.h" #include "src/core/lib/channel/channel_stack_builder.h"
#define GRPC_CHANNEL_INIT_BUILTIN_PRIORITY 10000 #define GRPC_CHANNEL_INIT_BUILTIN_PRIORITY 10000
@ -35,8 +35,6 @@
namespace grpc_core { namespace grpc_core {
class ChannelStackBuilder;
class ChannelInit { class ChannelInit {
public: public:
/// One stage of mutation: call functions against \a builder to influence the /// One stage of mutation: call functions against \a builder to influence the
@ -72,10 +70,8 @@ class ChannelInit {
}; };
/// Construct a channel stack of some sort: see channel_stack.h for details /// Construct a channel stack of some sort: see channel_stack.h for details
/// \a type is the type of channel stack to create
/// \a builder is the channel stack builder to build into. /// \a builder is the channel stack builder to build into.
bool CreateStack(ChannelStackBuilder* builder, bool CreateStack(ChannelStackBuilder* builder) const;
grpc_channel_stack_type type) const;
private: private:
std::vector<Stage> slots_[GRPC_NUM_CHANNEL_STACK_TYPES]; std::vector<Stage> slots_[GRPC_NUM_CHANNEL_STACK_TYPES];

@ -418,6 +418,7 @@ CORE_SOURCE_FILES = [
'src/core/lib/channel/channel_args_preconditioning.cc', 'src/core/lib/channel/channel_args_preconditioning.cc',
'src/core/lib/channel/channel_stack.cc', 'src/core/lib/channel/channel_stack.cc',
'src/core/lib/channel/channel_stack_builder.cc', 'src/core/lib/channel/channel_stack_builder.cc',
'src/core/lib/channel/channel_stack_builder_impl.cc',
'src/core/lib/channel/channel_trace.cc', 'src/core/lib/channel/channel_trace.cc',
'src/core/lib/channel/channelz.cc', 'src/core/lib/channel/channelz.cc',
'src/core/lib/channel/channelz_registry.cc', 'src/core/lib/channel/channelz_registry.cc',

@ -28,6 +28,8 @@
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/string_util.h> #include <grpc/support/string_util.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/slice/slice_internal.h" #include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"
@ -123,7 +125,7 @@ bool AddOriginalFilter(ChannelStackBuilder* builder) {
} }
TEST(ChannelStackBuilder, UnknownTarget) { TEST(ChannelStackBuilder, UnknownTarget) {
ChannelStackBuilder builder("alpha-beta-gamma"); ChannelStackBuilderImpl builder("alpha-beta-gamma", GRPC_CLIENT_CHANNEL);
EXPECT_EQ(builder.target(), "unknown"); EXPECT_EQ(builder.target(), "unknown");
} }

@ -39,7 +39,7 @@
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/string_util.h> #include <grpc/support/string_util.h>
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/string.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"
@ -124,7 +124,8 @@ static int check_stack(const char* file, int line, const char* transport_name,
grpc_channel_args* init_args, grpc_channel_args* init_args,
unsigned channel_stack_type, ...) { unsigned channel_stack_type, ...) {
// create phony channel stack // create phony channel stack
grpc_core::ChannelStackBuilder builder("test"); grpc_core::ChannelStackBuilderImpl builder(
"test", static_cast<grpc_channel_stack_type>(channel_stack_type));
grpc_transport_vtable fake_transport_vtable; grpc_transport_vtable fake_transport_vtable;
memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable)); memset(&fake_transport_vtable, 0, sizeof(grpc_transport_vtable));
fake_transport_vtable.name = transport_name; fake_transport_vtable.name = transport_name;
@ -137,7 +138,7 @@ static int check_stack(const char* file, int line, const char* transport_name,
{ {
grpc_core::ExecCtx exec_ctx; grpc_core::ExecCtx exec_ctx;
GPR_ASSERT(grpc_core::CoreConfiguration::Get().channel_init().CreateStack( GPR_ASSERT(grpc_core::CoreConfiguration::Get().channel_init().CreateStack(
&builder, (grpc_channel_stack_type)channel_stack_type)); &builder));
} }
// build up our expectation list // build up our expectation list

@ -25,6 +25,7 @@
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"

@ -26,6 +26,7 @@
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"

@ -26,6 +26,7 @@
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"

@ -26,6 +26,7 @@
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder.h" #include "src/core/lib/channel/channel_stack_builder.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"

@ -22,6 +22,7 @@
#include <grpcpp/opencensus.h> #include <grpcpp/opencensus.h>
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/surface/channel_init.h" #include "src/core/lib/surface/channel_init.h"
#include "src/core/lib/transport/transport_impl.h" #include "src/core/lib/transport/transport_impl.h"
@ -79,7 +80,7 @@ TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertion) {
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); grpc_arg arg = channel_stack_modifier->MakeChannelArg();
// Create a phony ChannelStackBuilder object // Create a phony ChannelStackBuilder object
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
ChannelStackBuilder builder("test"); ChannelStackBuilderImpl builder("test", GRPC_SERVER_CHANNEL);
builder.SetChannelArgs(args); builder.SetChannelArgs(args);
grpc_channel_args_destroy(args); grpc_channel_args_destroy(args);
grpc_transport_vtable fake_transport_vtable; grpc_transport_vtable fake_transport_vtable;
@ -89,8 +90,7 @@ TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertion) {
builder.SetTransport(&fake_transport); builder.SetTransport(&fake_transport);
// Construct channel stack and verify that the test filters were successfully // Construct channel stack and verify that the test filters were successfully
// added // added
ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack( ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack(&builder));
&builder, GRPC_SERVER_CHANNEL));
std::vector<std::string> filters; std::vector<std::string> filters;
for (const auto& entry : *builder.mutable_stack()) { for (const auto& entry : *builder.mutable_stack()) {
filters.push_back(entry.filter->name); filters.push_back(entry.filter->name);
@ -118,7 +118,7 @@ TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertionAfterCensus) {
grpc_arg arg = channel_stack_modifier->MakeChannelArg(); grpc_arg arg = channel_stack_modifier->MakeChannelArg();
// Create a phony ChannelStackBuilder object // Create a phony ChannelStackBuilder object
grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1); grpc_channel_args* args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);
ChannelStackBuilder builder("test"); ChannelStackBuilderImpl builder("test", GRPC_SERVER_CHANNEL);
builder.SetChannelArgs(args); builder.SetChannelArgs(args);
grpc_channel_args_destroy(args); grpc_channel_args_destroy(args);
grpc_transport_vtable fake_transport_vtable; grpc_transport_vtable fake_transport_vtable;
@ -128,8 +128,7 @@ TEST(XdsChannelStackModifierTest, XdsHttpFiltersInsertionAfterCensus) {
builder.SetTransport(&fake_transport); builder.SetTransport(&fake_transport);
// Construct channel stack and verify that the test filters were successfully // Construct channel stack and verify that the test filters were successfully
// added after the census filter // added after the census filter
ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack( ASSERT_TRUE(CoreConfiguration::Get().channel_init().CreateStack(&builder));
&builder, GRPC_SERVER_CHANNEL));
std::vector<std::string> filters; std::vector<std::string> filters;
for (const auto& entry : *builder.mutable_stack()) { for (const auto& entry : *builder.mutable_stack()) {
filters.push_back(entry.filter->name); filters.push_back(entry.filter->name);

@ -39,6 +39,7 @@
#include "src/core/ext/filters/http/server/http_server_filter.h" #include "src/core/ext/filters/http/server/http_server_filter.h"
#include "src/core/ext/filters/message_size/message_size_filter.h" #include "src/core/ext/filters/message_size/message_size_filter.h"
#include "src/core/lib/channel/channel_stack.h" #include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/channel/channel_stack_builder_impl.h"
#include "src/core/lib/channel/connected_channel.h" #include "src/core/lib/channel/connected_channel.h"
#include "src/core/lib/config/core_configuration.h" #include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/iomgr/call_combiner.h" #include "src/core/lib/iomgr/call_combiner.h"
@ -723,7 +724,7 @@ class IsolatedCallFixture : public TrackCounters {
const grpc_channel_args* args = grpc_core::CoreConfiguration::Get() const grpc_channel_args* args = grpc_core::CoreConfiguration::Get()
.channel_args_preconditioning() .channel_args_preconditioning()
.PreconditionChannelArgs(nullptr); .PreconditionChannelArgs(nullptr);
grpc_core::ChannelStackBuilder builder("phony"); grpc_core::ChannelStackBuilderImpl builder("phony", GRPC_CLIENT_CHANNEL);
builder.SetTarget("phony_target"); builder.SetTarget("phony_target");
builder.SetChannelArgs(args); builder.SetChannelArgs(args);
builder.AppendFilter(&isolated_call_filter::isolated_call_filter, nullptr); builder.AppendFilter(&isolated_call_filter::isolated_call_filter, nullptr);

@ -1882,6 +1882,8 @@ src/core/lib/channel/channel_stack.cc \
src/core/lib/channel/channel_stack.h \ src/core/lib/channel/channel_stack.h \
src/core/lib/channel/channel_stack_builder.cc \ src/core/lib/channel/channel_stack_builder.cc \
src/core/lib/channel/channel_stack_builder.h \ src/core/lib/channel/channel_stack_builder.h \
src/core/lib/channel/channel_stack_builder_impl.cc \
src/core/lib/channel/channel_stack_builder_impl.h \
src/core/lib/channel/channel_trace.cc \ src/core/lib/channel/channel_trace.cc \
src/core/lib/channel/channel_trace.h \ src/core/lib/channel/channel_trace.h \
src/core/lib/channel/channelz.cc \ src/core/lib/channel/channelz.cc \

@ -1674,6 +1674,8 @@ src/core/lib/channel/channel_stack.cc \
src/core/lib/channel/channel_stack.h \ src/core/lib/channel/channel_stack.h \
src/core/lib/channel/channel_stack_builder.cc \ src/core/lib/channel/channel_stack_builder.cc \
src/core/lib/channel/channel_stack_builder.h \ src/core/lib/channel/channel_stack_builder.h \
src/core/lib/channel/channel_stack_builder_impl.cc \
src/core/lib/channel/channel_stack_builder_impl.h \
src/core/lib/channel/channel_trace.cc \ src/core/lib/channel/channel_trace.cc \
src/core/lib/channel/channel_trace.h \ src/core/lib/channel/channel_trace.h \
src/core/lib/channel/channelz.cc \ src/core/lib/channel/channelz.cc \

Loading…
Cancel
Save