From 0498124f8ebead73d71fefdfc63e9b29d0da7a8f Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 1 Dec 2016 11:53:08 -0800 Subject: Add files --- .../grpc++/ext/health_check_service_interface.h | 52 ++++++++++++++++++++ .../health_check_service_server_builder_option.h | 56 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 include/grpc++/ext/health_check_service_interface.h create mode 100644 include/grpc++/ext/health_check_service_server_builder_option.h (limited to 'include') diff --git a/include/grpc++/ext/health_check_service_interface.h b/include/grpc++/ext/health_check_service_interface.h new file mode 100644 index 0000000000..b9f4ebb87f --- /dev/null +++ b/include/grpc++/ext/health_check_service_interface.h @@ -0,0 +1,52 @@ +/* + * + * Copyright 2016, 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 GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H +#define GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H + +#include + +namespace grpc { + +class HealthCheckServiceInterface { + public: + virtual ~HealthCheckServiceInterface() { } + virtual void SetServingStatus(const grpc::string& service_name, bool serving) = 0; + // Apply to all registered service names. + virtual void SetServingStatus(bool serving) = 0; +}; + + +} // namespace grpc + +#endif // GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h new file mode 100644 index 0000000000..9da1d18c98 --- /dev/null +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -0,0 +1,56 @@ +/* + * + * Copyright 2016, 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 GRPCXX_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H +#define GRPCXX_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H + +#include + +#include + +namespace grpc { + +class HealthCheckServiceInterface; + +class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { + public: + explicit HealthCheckServiceServerBuilderOption( + std::unique_ptr hc); + void UpdateArguments(ChannelArguments* args) override; + private: + std::unique_ptr hc_; +}; + +} // namespace grpc + +#endif // GRPCXX_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H -- cgit v1.2.3 From 64868839401b48a68afca187b28bc772043a2e1b Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 1 Dec 2016 12:15:50 -0800 Subject: Add more files --- BUILD | 3 ++ CMakeLists.txt | 3 ++ Makefile | 3 ++ build.yaml | 1 + .../health_check_service_server_builder_option.h | 8 ++- .../health_check_service_server_builder_option.cc | 59 ++++++++++++++++++++++ tools/doxygen/Doxyfile.c++.internal | 1 + tools/run_tests/generated/sources_and_headers.json | 1 + vsprojects/vcxproj/grpc++/grpc++.vcxproj | 2 + vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters | 3 ++ .../grpc++_unsecure/grpc++_unsecure.vcxproj | 2 + .../grpc++_unsecure.vcxproj.filters | 3 ++ 12 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/cpp/ext/health_check_service_server_builder_option.cc (limited to 'include') diff --git a/BUILD b/BUILD index 54eda959fc..fcd2742a8f 100644 --- a/BUILD +++ b/BUILD @@ -1396,6 +1396,7 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", + "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -1688,6 +1689,7 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", + "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -2038,6 +2040,7 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", + "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index 90cf1cdc77..e90e8426cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1085,6 +1085,7 @@ add_library(grpc++ src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc + src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc @@ -1245,6 +1246,7 @@ add_library(grpc++_cronet src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc + src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc @@ -1628,6 +1630,7 @@ add_library(grpc++_unsecure src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc + src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc diff --git a/Makefile b/Makefile index 85074b5b81..45d3cb2963 100644 --- a/Makefile +++ b/Makefile @@ -3779,6 +3779,7 @@ LIBGRPC++_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ + src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -3968,6 +3969,7 @@ LIBGRPC++_CRONET_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ + src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -4678,6 +4680,7 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ + src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ diff --git a/build.yaml b/build.yaml index 25c4dd7fbf..25dc9e0fcb 100644 --- a/build.yaml +++ b/build.yaml @@ -774,6 +774,7 @@ filegroups: - src/cpp/common/channel_filter.cc - src/cpp/common/completion_queue_cc.cc - src/cpp/common/core_codegen.cc + - src/cpp/common/health_check_service_server_builder_option.cc - src/cpp/common/resource_quota_cc.cc - src/cpp/common/rpc_method.cc - src/cpp/common/version_cc.cc diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h index 9da1d18c98..fb5263d742 100644 --- a/include/grpc++/ext/health_check_service_server_builder_option.h +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -36,21 +36,25 @@ #include +#include +#include #include namespace grpc { -class HealthCheckServiceInterface; - class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { public: explicit HealthCheckServiceServerBuilderOption( std::unique_ptr hc); + ~HealthCheckServiceServerBuilderOption() {} void UpdateArguments(ChannelArguments* args) override; + void UpdatePlugins(std::vector>* plugins) override; private: std::unique_ptr hc_; }; +void EnableDefaultHealthCheckService(bool enable); + } // namespace grpc #endif // GRPCXX_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H diff --git a/src/cpp/ext/health_check_service_server_builder_option.cc b/src/cpp/ext/health_check_service_server_builder_option.cc new file mode 100644 index 0000000000..dab5731e89 --- /dev/null +++ b/src/cpp/ext/health_check_service_server_builder_option.cc @@ -0,0 +1,59 @@ +/* + * + * Copyright 2016, 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 + +namespace grpc { +namespace { +bool g_grpc_default_health_check_service_enabled = false; +const char kDefaultHealthCheckServiceInterfaceArg[] = "grpc.default_health_check_service_interface"; +} // namesapce + +HealthCheckServiceServerBuilderOption::HealthCheckServiceServerBuilderOption( + std::unique_ptr hc) : hc_(std::move(hc)) { } + +HealthCheckServiceServerBuilderOption::UpdateArguments(ChannelArguments* args) override { + args->SetPointer(kDefaultHealthCheckServiceInterfaceArg, hc_.release()); +} + +void HealthCheckServiceServerBuilderOption::UpdatePlugins(std::vector>* plugins) override { + +} + +void EnableDefaultHealthCheckService(bool enable) { + g_grpc_default_health_check_service_enabled = enable; +} + +} // namespace grpc + + diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index f2e871e7bd..ac20f0fc02 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -877,6 +877,7 @@ src/cpp/common/channel_arguments.cc \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ +src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 4fd90ed24e..cb92999b26 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -7723,6 +7723,7 @@ "src/cpp/common/channel_filter.h", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", + "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj b/vsprojects/vcxproj/grpc++/grpc++.vcxproj index b8d4176f8a..468accbd23 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj @@ -398,6 +398,8 @@ + + diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters index b7eae651bd..d21b97b8df 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters @@ -58,6 +58,9 @@ src\cpp\common + + src\cpp\common + src\cpp\common diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj index 4b6d448f91..f0e3f3a23e 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -384,6 +384,8 @@ + + diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index deec6d7105..6076dbc693 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -43,6 +43,9 @@ src\cpp\common + + src\cpp\common + src\cpp\common -- cgit v1.2.3 From 8d668d8324c3aa24353b3774a8f6ce35edbf20b7 Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 1 Dec 2016 15:09:28 -0800 Subject: default service --- BUILD | 9 +++ CMakeLists.txt | 6 ++ Makefile | 6 ++ build.yaml | 3 + .../grpc++/ext/health_check_service_interface.h | 52 ----------------- .../health_check_service_server_builder_option.h | 5 +- include/grpc++/health_check_service_interface.h | 63 ++++++++++++++++++++ include/grpc++/server.h | 9 +++ .../health_check_service_server_builder_option.cc | 12 +--- src/cpp/server/default_health_check_service.cc | 67 ++++++++++++++++++++++ src/cpp/server/default_health_check_service.h | 59 +++++++++++++++++++ src/cpp/server/health_check_service.cc | 49 ++++++++++++++++ src/cpp/server/server_cc.cc | 33 ++++++++++- tools/doxygen/Doxyfile.c++.internal | 3 + tools/run_tests/generated/sources_and_headers.json | 4 ++ vsprojects/vcxproj/grpc++/grpc++.vcxproj | 5 ++ vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters | 9 +++ .../grpc++_unsecure/grpc++_unsecure.vcxproj | 5 ++ .../grpc++_unsecure.vcxproj.filters | 9 +++ 19 files changed, 341 insertions(+), 67 deletions(-) delete mode 100644 include/grpc++/ext/health_check_service_interface.h create mode 100644 include/grpc++/health_check_service_interface.h create mode 100644 src/cpp/server/default_health_check_service.cc create mode 100644 src/cpp/server/default_health_check_service.h create mode 100644 src/cpp/server/health_check_service.cc (limited to 'include') diff --git a/BUILD b/BUILD index fcd2742a8f..be1e23007b 100644 --- a/BUILD +++ b/BUILD @@ -1374,6 +1374,7 @@ cc_library( "src/cpp/server/secure_server_credentials.h", "src/cpp/client/create_channel_internal.h", "src/cpp/common/channel_filter.h", + "src/cpp/server/default_health_check_service.h", "src/cpp/server/dynamic_thread_pool.h", "src/cpp/server/thread_pool_interface.h", "src/cpp/thread_manager/thread_manager.h", @@ -1402,7 +1403,9 @@ cc_library( "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/create_default_thread_pool.cc", + "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", + "src/cpp/server/health_check_service.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", @@ -1525,6 +1528,7 @@ cc_library( srcs = [ "src/cpp/client/create_channel_internal.h", "src/cpp/common/channel_filter.h", + "src/cpp/server/default_health_check_service.h", "src/cpp/server/dynamic_thread_pool.h", "src/cpp/server/thread_pool_interface.h", "src/cpp/thread_manager/thread_manager.h", @@ -1695,7 +1699,9 @@ cc_library( "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/create_default_thread_pool.cc", + "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", + "src/cpp/server/health_check_service.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", @@ -2023,6 +2029,7 @@ cc_library( srcs = [ "src/cpp/client/create_channel_internal.h", "src/cpp/common/channel_filter.h", + "src/cpp/server/default_health_check_service.h", "src/cpp/server/dynamic_thread_pool.h", "src/cpp/server/thread_pool_interface.h", "src/cpp/thread_manager/thread_manager.h", @@ -2046,7 +2053,9 @@ cc_library( "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/create_default_thread_pool.cc", + "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", + "src/cpp/server/health_check_service.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index e90e8426cc..049f7fdaca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1091,7 +1091,9 @@ add_library(grpc++ src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc + src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc + src/cpp/server/health_check_service.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc @@ -1252,7 +1254,9 @@ add_library(grpc++_cronet src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc + src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc + src/cpp/server/health_check_service.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc @@ -1636,7 +1640,9 @@ add_library(grpc++_unsecure src/cpp/common/version_cc.cc src/cpp/server/async_generic_service.cc src/cpp/server/create_default_thread_pool.cc + src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc + src/cpp/server/health_check_service.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc diff --git a/Makefile b/Makefile index 45d3cb2963..63f5d2ad31 100644 --- a/Makefile +++ b/Makefile @@ -3785,7 +3785,9 @@ LIBGRPC++_SRC = \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/create_default_thread_pool.cc \ + src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ + src/cpp/server/health_check_service.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ @@ -3975,7 +3977,9 @@ LIBGRPC++_CRONET_SRC = \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/create_default_thread_pool.cc \ + src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ + src/cpp/server/health_check_service.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ @@ -4686,7 +4690,9 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/create_default_thread_pool.cc \ + src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ + src/cpp/server/health_check_service.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ diff --git a/build.yaml b/build.yaml index 25dc9e0fcb..925c73b729 100644 --- a/build.yaml +++ b/build.yaml @@ -759,6 +759,7 @@ filegroups: headers: - src/cpp/client/create_channel_internal.h - src/cpp/common/channel_filter.h + - src/cpp/server/default_health_check_service.h - src/cpp/server/dynamic_thread_pool.h - src/cpp/server/thread_pool_interface.h - src/cpp/thread_manager/thread_manager.h @@ -780,7 +781,9 @@ filegroups: - src/cpp/common/version_cc.cc - src/cpp/server/async_generic_service.cc - src/cpp/server/create_default_thread_pool.cc + - src/cpp/server/default_health_check_service.cc - src/cpp/server/dynamic_thread_pool.cc + - src/cpp/server/health_check_service.cc - src/cpp/server/server_builder.cc - src/cpp/server/server_cc.cc - src/cpp/server/server_context.cc diff --git a/include/grpc++/ext/health_check_service_interface.h b/include/grpc++/ext/health_check_service_interface.h deleted file mode 100644 index b9f4ebb87f..0000000000 --- a/include/grpc++/ext/health_check_service_interface.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * - * Copyright 2016, 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 GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H -#define GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H - -#include - -namespace grpc { - -class HealthCheckServiceInterface { - public: - virtual ~HealthCheckServiceInterface() { } - virtual void SetServingStatus(const grpc::string& service_name, bool serving) = 0; - // Apply to all registered service names. - virtual void SetServingStatus(bool serving) = 0; -}; - - -} // namespace grpc - -#endif // GRPCXX_EXT_HEALTH_CHECK_SERVICE_INTERFACE_H diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h index fb5263d742..e00c4c3dd5 100644 --- a/include/grpc++/ext/health_check_service_server_builder_option.h +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -36,7 +36,7 @@ #include -#include +#include #include #include @@ -44,6 +44,7 @@ namespace grpc { class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { public: + // Use nullptr to disable default service. explicit HealthCheckServiceServerBuilderOption( std::unique_ptr hc); ~HealthCheckServiceServerBuilderOption() {} @@ -53,8 +54,6 @@ class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { std::unique_ptr hc_; }; -void EnableDefaultHealthCheckService(bool enable); - } // namespace grpc #endif // GRPCXX_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H diff --git a/include/grpc++/health_check_service_interface.h b/include/grpc++/health_check_service_interface.h new file mode 100644 index 0000000000..70153d65cb --- /dev/null +++ b/include/grpc++/health_check_service_interface.h @@ -0,0 +1,63 @@ +/* + * + * Copyright 2016, 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 GRPCXX_HEALTH_CHECK_SERVICE_INTERFACE_H +#define GRPCXX_HEALTH_CHECK_SERVICE_INTERFACE_H + +#include + +namespace grpc { + +const char kDefaultHealthCheckServiceInterfaceArg[] = + "grpc.default_health_check_service_interface"; + +class HealthCheckServiceInterface { + public: + virtual ~HealthCheckServiceInterface() {} + virtual void SetServingStatus(const grpc::string& service_name, + bool serving) = 0; + // Apply to all registered service names. + virtual void SetServingStatus(bool serving) = 0; +}; + +bool DefaultHealthCheckServiceEnabled(); + +// Enable/disable the default health checking service. This applies to all C++ +// servers created afterwards. For each server, user can override the default +// with a HealthCheckServiceServerBuilderOption. +// NOT thread safe. +void EnableDefaultHealthCheckService(bool enable); + +} // namespace grpc + +#endif // GRPCXX_HEALTH_CHECK_SERVICE_INTERFACE_H diff --git a/include/grpc++/server.h b/include/grpc++/server.h index fba9952e6e..511dc35564 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -97,6 +97,11 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { // Returns a \em raw pointer to the underlying grpc_server instance. grpc_server* c_server(); + /// Returns the health check service. + HealthCheckServiceInterface* GetHealthCheckService() const { + return hc_.get(); + } + private: friend class AsyncGenericService; friend class ServerBuilder; @@ -214,6 +219,10 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { grpc_server* server_; std::unique_ptr server_initializer_; + + std::unique_ptr health_check_service_; + // User explicitly disabled health check service. + bool health_check_service_disabled_; }; } // namespace grpc diff --git a/src/cpp/ext/health_check_service_server_builder_option.cc b/src/cpp/ext/health_check_service_server_builder_option.cc index dab5731e89..4afc82dc14 100644 --- a/src/cpp/ext/health_check_service_server_builder_option.cc +++ b/src/cpp/ext/health_check_service_server_builder_option.cc @@ -34,26 +34,16 @@ #include namespace grpc { -namespace { -bool g_grpc_default_health_check_service_enabled = false; -const char kDefaultHealthCheckServiceInterfaceArg[] = "grpc.default_health_check_service_interface"; -} // namesapce HealthCheckServiceServerBuilderOption::HealthCheckServiceServerBuilderOption( std::unique_ptr hc) : hc_(std::move(hc)) { } HealthCheckServiceServerBuilderOption::UpdateArguments(ChannelArguments* args) override { - args->SetPointer(kDefaultHealthCheckServiceInterfaceArg, hc_.release()); + args->SetPointer(DefaultHealthCheckServiceInterfaceArg(), hc_.release()); } void HealthCheckServiceServerBuilderOption::UpdatePlugins(std::vector>* plugins) override { - -} - -void EnableDefaultHealthCheckService(bool enable) { - g_grpc_default_health_check_service_enabled = enable; } } // namespace grpc - diff --git a/src/cpp/server/default_health_check_service.cc b/src/cpp/server/default_health_check_service.cc new file mode 100644 index 0000000000..eec7e2cd58 --- /dev/null +++ b/src/cpp/server/default_health_check_service.cc @@ -0,0 +1,67 @@ +/* + * + * Copyright 2016, 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/cpp/server/default_health_check_service.h" + +namespace grpc { + +DefaultHealthCheckService::DefaultHealthCheckService() { + services_map_.insert("", true); +} + +void DefaultHealthCheckService::SetServingStatus( + const grpc::string& service_name, bool serving) override { + std::lock_guard lock(mu_); + services_map_[service_name] = serving; +} + +void SetServingStatus(bool serving) override { + std::lock_guard lock(mu_); + for (auto& iter = services_map_.begin(); iter != services_map_.end(); + ++iter) { + iter->second = serving; + } +} + +ServingStatus GetServingStatus(const grpc::string& service_name) const { + std::lock_guard lock(mu_); + const auto& iter = services_map_.find(service_name); + if (iter == services_map_.end()) { + return NOT_FOUND; + } + return iter->second ? SERVING : NOT_SERVING; +} + +} // namespace grpc + +#endif // GRPC_INTERNAL_CPP_SERVER_DEFAULT_HEALTH_CHECK_SERVICE_H diff --git a/src/cpp/server/default_health_check_service.h b/src/cpp/server/default_health_check_service.h new file mode 100644 index 0000000000..51afa6491e --- /dev/null +++ b/src/cpp/server/default_health_check_service.h @@ -0,0 +1,59 @@ +/* + * + * Copyright 2016, 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_CPP_SERVER_DEFAULT_HEALTH_CHECK_SERVICE_H +#define GRPC_INTERNAL_CPP_SERVER_DEFAULT_HEALTH_CHECK_SERVICE_H + +#include + +#include + +namespace grpc { + +class DefaultHealthCheckService : public HealthCheckServiceInterface { + public: + DefaultHealthCheckService(); + void SetServingStatus(const grpc::string& service_name, + bool serving) override; + void SetServingStatus(bool serving) override; + enum ServingStatus { NOT_FOUND, SERVING, NOT_SERVING }; + ServingStatus GetServingStatus(const grpc::string& service_name) const; + + private: + std::mutex mu_; + std::map services_map_; +}; + +} // namespace grpc + +#endif // GRPC_INTERNAL_CPP_SERVER_DEFAULT_HEALTH_CHECK_SERVICE_H diff --git a/src/cpp/server/health_check_service.cc b/src/cpp/server/health_check_service.cc new file mode 100644 index 0000000000..cca68c5549 --- /dev/null +++ b/src/cpp/server/health_check_service.cc @@ -0,0 +1,49 @@ +/* + * + * Copyright 2016, 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 + +namespace grpc { +namespace { +bool g_grpc_default_health_check_service_enabled = false; +} // namesapce + +bool DefaultHealthCheckServiceEnabled() { + return g_grpc_default_health_check_service_enabled; +} + +void EnableDefaultHealthCheckService(bool enable) { + g_grpc_default_health_check_service_enabled = enable; +} + +} // namespace grpc diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 817d85a81c..037da2b301 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -342,6 +343,7 @@ class Server::SyncRequestThreadManager : public ThreadManager { int cq_timeout_msec_; std::vector> sync_requests_; std::unique_ptr unknown_method_; + std::unique_ptr health_check_; std::shared_ptr global_callbacks_; }; @@ -358,7 +360,8 @@ Server::Server( shutdown_notified_(false), has_generic_service_(false), server_(nullptr), - server_initializer_(new ServerInitializer(this)) { + server_initializer_(new ServerInitializer(this)), + health_check_service_disabled_(false) { g_gli_initializer.summon(); gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); global_callbacks_ = g_callbacks; @@ -374,6 +377,18 @@ Server::Server( grpc_channel_args channel_args; args->SetChannelArgs(&channel_args); + for (size_t i = 0; i < channel_args.num_args; i++) { + if (0 == strcmp(channel_args.args[i].key, + kDefaultHealthCheckServiceInterfaceArg)) { + if (channel_args.args[i].value == nullptr) { + health_check_service_disabled_ = true; + } else { + health_check_service_.reset(channel_args.args[i].value); + } + break; + } + } + server_ = grpc_server_create(&channel_args, nullptr); } @@ -481,6 +496,22 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { started_ = true; grpc_server_start(server_); + // Only create default health check service when user did not provide an + // explicit one. + if (health_check_service_ == nullptr && !health_check_service_disabled_ && + EnableDefaultHealthCheckService()) { + health_check_service_.reset(CreateDefaultHealthCheckService()); + for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { + (*it)->AddHealthCheckSyncMethod(); + } + + for (size_t i = 0; i < num_cqs; i++) { + if (cqs[i]->IsFrequentlyPolled()) { + // new UnimplementedAsyncRequest(this, cqs[i]); + } + } + } + if (!has_generic_service_) { for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { (*it)->AddUnknownSyncMethod(); diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index ac20f0fc02..aa79aedeb9 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -855,6 +855,7 @@ src/cpp/common/secure_auth_context.h \ src/cpp/server/secure_server_credentials.h \ src/cpp/client/create_channel_internal.h \ src/cpp/common/channel_filter.h \ +src/cpp/server/default_health_check_service.h \ src/cpp/server/dynamic_thread_pool.h \ src/cpp/server/thread_pool_interface.h \ src/cpp/thread_manager/thread_manager.h \ @@ -883,7 +884,9 @@ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ src/cpp/server/async_generic_service.cc \ src/cpp/server/create_default_thread_pool.cc \ +src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ +src/cpp/server/health_check_service.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index cb92999b26..860a151f48 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -7658,6 +7658,7 @@ "include/grpc++/support/time.h", "src/cpp/client/create_channel_internal.h", "src/cpp/common/channel_filter.h", + "src/cpp/server/default_health_check_service.h", "src/cpp/server/dynamic_thread_pool.h", "src/cpp/server/thread_pool_interface.h", "src/cpp/thread_manager/thread_manager.h" @@ -7729,8 +7730,11 @@ "src/cpp/common/version_cc.cc", "src/cpp/server/async_generic_service.cc", "src/cpp/server/create_default_thread_pool.cc", + "src/cpp/server/default_health_check_service.cc", + "src/cpp/server/default_health_check_service.h", "src/cpp/server/dynamic_thread_pool.cc", "src/cpp/server/dynamic_thread_pool.h", + "src/cpp/server/health_check_service.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj b/vsprojects/vcxproj/grpc++/grpc++.vcxproj index 468accbd23..a57ed0e2e5 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj @@ -355,6 +355,7 @@ + @@ -410,8 +411,12 @@ + + + + diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters index d21b97b8df..e455636abe 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters @@ -76,9 +76,15 @@ src\cpp\server + + src\cpp\server + src\cpp\server + + src\cpp\server + src\cpp\server @@ -404,6 +410,9 @@ src\cpp\common + + src\cpp\server + src\cpp\server diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj index f0e3f3a23e..10c9be3339 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -351,6 +351,7 @@ + @@ -396,8 +397,12 @@ + + + + diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index 6076dbc693..0b65ff3fe8 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -61,9 +61,15 @@ src\cpp\server + + src\cpp\server + src\cpp\server + + src\cpp\server + src\cpp\server @@ -377,6 +383,9 @@ src\cpp\common + + src\cpp\server + src\cpp\server -- cgit v1.2.3 From c9d2954e556db3f102a858d97e339edb9db0388c Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 6 Dec 2016 14:20:17 -0800 Subject: more --- BUILD | 6 +-- CMakeLists.txt | 6 +-- Makefile | 6 +-- build.yaml | 2 +- include/grpc++/server.h | 8 +--- src/cpp/server/default_health_check_service.cc | 23 ++++++++++ src/cpp/server/default_health_check_service.h | 22 ++++++++-- .../health_check_service_server_builder_option.cc | 50 ++++++++++++++++++++++ src/cpp/server/server_cc.cc | 29 +++++-------- tools/doxygen/Doxyfile.c++.internal | 2 +- tools/run_tests/generated/sources_and_headers.json | 2 +- vsprojects/vcxproj/grpc++/grpc++.vcxproj | 4 +- vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters | 6 +-- .../grpc++_unsecure/grpc++_unsecure.vcxproj | 4 +- .../grpc++_unsecure.vcxproj.filters | 6 +-- 15 files changed, 127 insertions(+), 49 deletions(-) create mode 100644 src/cpp/server/health_check_service_server_builder_option.cc (limited to 'include') diff --git a/BUILD b/BUILD index be1e23007b..6e786607cc 100644 --- a/BUILD +++ b/BUILD @@ -1397,7 +1397,6 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", - "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -1406,6 +1405,7 @@ cc_library( "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", "src/cpp/server/health_check_service.cc", + "src/cpp/server/health_check_service_server_builder_option.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", @@ -1693,7 +1693,6 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", - "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -1702,6 +1701,7 @@ cc_library( "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", "src/cpp/server/health_check_service.cc", + "src/cpp/server/health_check_service_server_builder_option.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", @@ -2047,7 +2047,6 @@ cc_library( "src/cpp/common/channel_filter.cc", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", - "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -2056,6 +2055,7 @@ cc_library( "src/cpp/server/default_health_check_service.cc", "src/cpp/server/dynamic_thread_pool.cc", "src/cpp/server/health_check_service.cc", + "src/cpp/server/health_check_service_server_builder_option.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index 049f7fdaca..c32a4aaf96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1085,7 +1085,6 @@ add_library(grpc++ src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc - src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc @@ -1094,6 +1093,7 @@ add_library(grpc++ src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc src/cpp/server/health_check_service.cc + src/cpp/server/health_check_service_server_builder_option.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc @@ -1248,7 +1248,6 @@ add_library(grpc++_cronet src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc - src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc @@ -1257,6 +1256,7 @@ add_library(grpc++_cronet src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc src/cpp/server/health_check_service.cc + src/cpp/server/health_check_service_server_builder_option.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc @@ -1634,7 +1634,6 @@ add_library(grpc++_unsecure src/cpp/common/channel_filter.cc src/cpp/common/completion_queue_cc.cc src/cpp/common/core_codegen.cc - src/cpp/common/health_check_service_server_builder_option.cc src/cpp/common/resource_quota_cc.cc src/cpp/common/rpc_method.cc src/cpp/common/version_cc.cc @@ -1643,6 +1642,7 @@ add_library(grpc++_unsecure src/cpp/server/default_health_check_service.cc src/cpp/server/dynamic_thread_pool.cc src/cpp/server/health_check_service.cc + src/cpp/server/health_check_service_server_builder_option.cc src/cpp/server/server_builder.cc src/cpp/server/server_cc.cc src/cpp/server/server_context.cc diff --git a/Makefile b/Makefile index 63f5d2ad31..43ef3c4f06 100644 --- a/Makefile +++ b/Makefile @@ -3779,7 +3779,6 @@ LIBGRPC++_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ - src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -3788,6 +3787,7 @@ LIBGRPC++_SRC = \ src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ src/cpp/server/health_check_service.cc \ + src/cpp/server/health_check_service_server_builder_option.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ @@ -3971,7 +3971,6 @@ LIBGRPC++_CRONET_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ - src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -3980,6 +3979,7 @@ LIBGRPC++_CRONET_SRC = \ src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ src/cpp/server/health_check_service.cc \ + src/cpp/server/health_check_service_server_builder_option.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ @@ -4684,7 +4684,6 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ - src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -4693,6 +4692,7 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ src/cpp/server/health_check_service.cc \ + src/cpp/server/health_check_service_server_builder_option.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ diff --git a/build.yaml b/build.yaml index 925c73b729..f02142717d 100644 --- a/build.yaml +++ b/build.yaml @@ -775,7 +775,6 @@ filegroups: - src/cpp/common/channel_filter.cc - src/cpp/common/completion_queue_cc.cc - src/cpp/common/core_codegen.cc - - src/cpp/common/health_check_service_server_builder_option.cc - src/cpp/common/resource_quota_cc.cc - src/cpp/common/rpc_method.cc - src/cpp/common/version_cc.cc @@ -784,6 +783,7 @@ filegroups: - src/cpp/server/default_health_check_service.cc - src/cpp/server/dynamic_thread_pool.cc - src/cpp/server/health_check_service.cc + - src/cpp/server/health_check_service_server_builder_option.cc - src/cpp/server/server_builder.cc - src/cpp/server/server_cc.cc - src/cpp/server/server_context.cc diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 511dc35564..dc97a0ec4c 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -55,12 +55,10 @@ struct grpc_server; namespace grpc { -class GenericServerContext; class AsyncGenericService; -class ServerAsyncStreamingInterface; +class HealthCheckServiceInterface; class ServerContext; class ServerInitializer; -class ThreadPoolInterface; /// Models a gRPC server. /// @@ -99,7 +97,7 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { /// Returns the health check service. HealthCheckServiceInterface* GetHealthCheckService() const { - return hc_.get(); + return health_check_service_.get(); } private: @@ -221,8 +219,6 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { std::unique_ptr server_initializer_; std::unique_ptr health_check_service_; - // User explicitly disabled health check service. - bool health_check_service_disabled_; }; } // namespace grpc diff --git a/src/cpp/server/default_health_check_service.cc b/src/cpp/server/default_health_check_service.cc index eec7e2cd58..45f51a97b6 100644 --- a/src/cpp/server/default_health_check_service.cc +++ b/src/cpp/server/default_health_check_service.cc @@ -34,6 +34,29 @@ #include "src/cpp/server/default_health_check_service.h" namespace grpc { +namespace { + +const char kHealthCheckMethodName[] = "/grpc.health.v1.Health/Check"; + +} // namespace + +SyncHealthCheckServiceImpl::SyncHealthCheckServiceImpl( + DefaultHealthCheckService* service) + : service_(service) { + auto* handler = + new RpcMethodHandler( + std::mem_fn(&SyncHealthCheckServiceImpl::Check), this); + auto* method = new RpcServiceMethod(kHealthCheckMethodName, + RpcMethod::NORMAL_RPC, handler); + AddMethod(mehtod); +} + +Status SyncHealthCheckServiceImpl::Check(ServerContext* context, + const ByteBuffer* request, + ByteBuffer* response) { + // TODO nanopb + return Status::OK; +} DefaultHealthCheckService::DefaultHealthCheckService() { services_map_.insert("", true); diff --git a/src/cpp/server/default_health_check_service.h b/src/cpp/server/default_health_check_service.h index 51afa6491e..a2545c50e7 100644 --- a/src/cpp/server/default_health_check_service.h +++ b/src/cpp/server/default_health_check_service.h @@ -37,21 +37,37 @@ #include #include +#include namespace grpc { +class SyncHealthCheckServiceImpl : public Service { + public: + explicit SyncHealthCheckServiceImpl(DefaultHealthCheckService* service); + Status Check(ServerContext* context, const ByteBuffer* request, + ByteBuffer* response); + + private: + const DefaultHealthCheckService* service_; +}; + +// Default implementation of HealthCheckServiceInterface. Server will create and +// own it. class DefaultHealthCheckService : public HealthCheckServiceInterface { public: DefaultHealthCheckService(); - void SetServingStatus(const grpc::string& service_name, - bool serving) override; - void SetServingStatus(bool serving) override; + void SetServingStatus(const grpc::string& service_name, bool serving) final; + void SetServingStatus(bool serving) final; enum ServingStatus { NOT_FOUND, SERVING, NOT_SERVING }; ServingStatus GetServingStatus(const grpc::string& service_name) const; + SyncHealthCheckServiceImpl* GetSyncHealthCheckService() const { + return sync_service_.get(); + } private: std::mutex mu_; std::map services_map_; + std::unique_ptr sync_service_; }; } // namespace grpc diff --git a/src/cpp/server/health_check_service_server_builder_option.cc b/src/cpp/server/health_check_service_server_builder_option.cc new file mode 100644 index 0000000000..80004b77fa --- /dev/null +++ b/src/cpp/server/health_check_service_server_builder_option.cc @@ -0,0 +1,50 @@ +/* + * + * Copyright 2016, 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 + +namespace grpc { + +HealthCheckServiceServerBuilderOption::HealthCheckServiceServerBuilderOption( + std::unique_ptr hc) + : hc_(std::move(hc)) {} +// Hand over hc_ to the server. +void HealthCheckServiceServerBuilderOption::UpdateArguments( + ChannelArguments* args) override { + args->SetPointer(kDefaultHealthCheckServiceInterfaceArg, hc_.release()); +} + +void HealthCheckServiceServerBuilderOption::UpdatePlugins( + std::vector>* plugins) override {} + +} // namespace grpc diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 5d4da92f2b..03210883bf 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -370,8 +370,7 @@ Server::Server( shutdown_notified_(false), has_generic_service_(false), server_(nullptr), - server_initializer_(new ServerInitializer(this)), - health_check_service_disabled_(false) { + server_initializer_(new ServerInitializer(this)) { g_gli_initializer.summon(); gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); global_callbacks_ = g_callbacks; @@ -387,6 +386,7 @@ Server::Server( grpc_channel_args channel_args; args->SetChannelArgs(&channel_args); + bool health_check_service_disabled = false; for (size_t i = 0; i < channel_args.num_args; i++) { if (0 == strcmp(channel_args.args[i].key, kDefaultHealthCheckServiceInterfaceArg)) { @@ -398,6 +398,15 @@ Server::Server( break; } } + // Only create default health check service when user did not provide an + // explicit one. + if (health_check_service_ == nullptr && !health_check_service_disabled && + EnableDefaultHealthCheckService()) { + health_check_service_.reset(new DefaultHealthCheckService); + if (!sync_server_cqs->empty()) { // Has sync methods. + RegisterService(health_check_service_->GetSyncHealthCheckService()); + } + } server_ = grpc_server_create(&channel_args, nullptr); } @@ -506,22 +515,6 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { started_ = true; grpc_server_start(server_); - // Only create default health check service when user did not provide an - // explicit one. - if (health_check_service_ == nullptr && !health_check_service_disabled_ && - EnableDefaultHealthCheckService()) { - health_check_service_.reset(new DefaultHealthCheckService); - for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { - (*it)->AddHealthCheckSyncMethod(); - } - - for (size_t i = 0; i < num_cqs; i++) { - if (cqs[i]->IsFrequentlyPolled()) { - // new UnimplementedAsyncRequest(this, cqs[i]); - } - } - } - if (!has_generic_service_) { for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { (*it)->AddUnknownSyncMethod(); diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index aa79aedeb9..5e3f226603 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -878,7 +878,6 @@ src/cpp/common/channel_arguments.cc \ src/cpp/common/channel_filter.cc \ src/cpp/common/completion_queue_cc.cc \ src/cpp/common/core_codegen.cc \ -src/cpp/common/health_check_service_server_builder_option.cc \ src/cpp/common/resource_quota_cc.cc \ src/cpp/common/rpc_method.cc \ src/cpp/common/version_cc.cc \ @@ -887,6 +886,7 @@ src/cpp/server/create_default_thread_pool.cc \ src/cpp/server/default_health_check_service.cc \ src/cpp/server/dynamic_thread_pool.cc \ src/cpp/server/health_check_service.cc \ +src/cpp/server/health_check_service_server_builder_option.cc \ src/cpp/server/server_builder.cc \ src/cpp/server/server_cc.cc \ src/cpp/server/server_context.cc \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 860a151f48..84fc1ad3b1 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -7724,7 +7724,6 @@ "src/cpp/common/channel_filter.h", "src/cpp/common/completion_queue_cc.cc", "src/cpp/common/core_codegen.cc", - "src/cpp/common/health_check_service_server_builder_option.cc", "src/cpp/common/resource_quota_cc.cc", "src/cpp/common/rpc_method.cc", "src/cpp/common/version_cc.cc", @@ -7735,6 +7734,7 @@ "src/cpp/server/dynamic_thread_pool.cc", "src/cpp/server/dynamic_thread_pool.h", "src/cpp/server/health_check_service.cc", + "src/cpp/server/health_check_service_server_builder_option.cc", "src/cpp/server/server_builder.cc", "src/cpp/server/server_cc.cc", "src/cpp/server/server_context.cc", diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj b/vsprojects/vcxproj/grpc++/grpc++.vcxproj index a57ed0e2e5..4af3454e39 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj @@ -399,8 +399,6 @@ - - @@ -417,6 +415,8 @@ + + diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters index e455636abe..17ea98381a 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters @@ -58,9 +58,6 @@ src\cpp\common - - src\cpp\common - src\cpp\common @@ -85,6 +82,9 @@ src\cpp\server + + src\cpp\server + src\cpp\server diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj index 10c9be3339..060f2818e3 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -385,8 +385,6 @@ - - @@ -403,6 +401,8 @@ + + diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index 0b65ff3fe8..8aa4a77d40 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -43,9 +43,6 @@ src\cpp\common - - src\cpp\common - src\cpp\common @@ -70,6 +67,9 @@ src\cpp\server + + src\cpp\server + src\cpp\server -- cgit v1.2.3 From ad327642192363808c378b775ddcd34b86294443 Mon Sep 17 00:00:00 2001 From: yang-g Date: Mon, 12 Dec 2016 14:32:09 -0800 Subject: sync test pass --- Makefile | 48 +++++ build.yaml | 13 ++ include/grpc++/server.h | 1 + src/cpp/server/default_health_check_service.cc | 7 +- src/cpp/server/server_cc.cc | 29 +-- test/cpp/end2end/health_service_end2end_test.cc | 158 ++++++++++++++++ tools/run_tests/generated/sources_and_headers.json | 19 ++ tools/run_tests/generated/tests.json | 22 +++ .../health_service_end2end_test.vcxproj | 207 +++++++++++++++++++++ .../health_service_end2end_test.vcxproj.filters | 21 +++ 10 files changed, 509 insertions(+), 16 deletions(-) create mode 100644 test/cpp/end2end/health_service_end2end_test.cc create mode 100644 vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj create mode 100644 vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj.filters (limited to 'include') diff --git a/Makefile b/Makefile index e9d24906d5..6c7a0c1518 100644 --- a/Makefile +++ b/Makefile @@ -1085,6 +1085,7 @@ grpc_ruby_plugin: $(BINDIR)/$(CONFIG)/grpc_ruby_plugin grpc_tool_test: $(BINDIR)/$(CONFIG)/grpc_tool_test grpclb_api_test: $(BINDIR)/$(CONFIG)/grpclb_api_test grpclb_test: $(BINDIR)/$(CONFIG)/grpclb_test +health_service_end2end_test: $(BINDIR)/$(CONFIG)/health_service_end2end_test hybrid_end2end_test: $(BINDIR)/$(CONFIG)/hybrid_end2end_test interop_client: $(BINDIR)/$(CONFIG)/interop_client interop_server: $(BINDIR)/$(CONFIG)/interop_server @@ -1467,6 +1468,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/grpc_tool_test \ $(BINDIR)/$(CONFIG)/grpclb_api_test \ $(BINDIR)/$(CONFIG)/grpclb_test \ + $(BINDIR)/$(CONFIG)/health_service_end2end_test \ $(BINDIR)/$(CONFIG)/hybrid_end2end_test \ $(BINDIR)/$(CONFIG)/interop_client \ $(BINDIR)/$(CONFIG)/interop_server \ @@ -1560,6 +1562,7 @@ buildtests_cxx: privatelibs_cxx \ $(BINDIR)/$(CONFIG)/grpc_tool_test \ $(BINDIR)/$(CONFIG)/grpclb_api_test \ $(BINDIR)/$(CONFIG)/grpclb_test \ + $(BINDIR)/$(CONFIG)/health_service_end2end_test \ $(BINDIR)/$(CONFIG)/hybrid_end2end_test \ $(BINDIR)/$(CONFIG)/interop_client \ $(BINDIR)/$(CONFIG)/interop_server \ @@ -1883,6 +1886,8 @@ test_cxx: buildtests_cxx $(Q) $(BINDIR)/$(CONFIG)/grpclb_api_test || ( echo test grpclb_api_test failed ; exit 1 ) $(E) "[RUN] Testing grpclb_test" $(Q) $(BINDIR)/$(CONFIG)/grpclb_test || ( echo test grpclb_test failed ; exit 1 ) + $(E) "[RUN] Testing health_service_end2end_test" + $(Q) $(BINDIR)/$(CONFIG)/health_service_end2end_test || ( echo test health_service_end2end_test failed ; exit 1 ) $(E) "[RUN] Testing hybrid_end2end_test" $(Q) $(BINDIR)/$(CONFIG)/hybrid_end2end_test || ( echo test hybrid_end2end_test failed ; exit 1 ) $(E) "[RUN] Testing interop_test" @@ -12906,6 +12911,49 @@ endif $(OBJDIR)/$(CONFIG)/test/cpp/grpclb/grpclb_test.o: $(GENDIR)/src/proto/grpc/lb/v1/load_balancer.pb.cc $(GENDIR)/src/proto/grpc/lb/v1/load_balancer.grpc.pb.cc +HEALTH_SERVICE_END2END_TEST_SRC = \ + test/cpp/end2end/health_service_end2end_test.cc \ + +HEALTH_SERVICE_END2END_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(HEALTH_SERVICE_END2END_TEST_SRC)))) +ifeq ($(NO_SECURE),true) + +# You can't build secure targets if you don't have OpenSSL. + +$(BINDIR)/$(CONFIG)/health_service_end2end_test: openssl_dep_error + +else + + + + +ifeq ($(NO_PROTOBUF),true) + +# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+. + +$(BINDIR)/$(CONFIG)/health_service_end2end_test: protobuf_dep_error + +else + +$(BINDIR)/$(CONFIG)/health_service_end2end_test: $(PROTOBUF_DEP) $(HEALTH_SERVICE_END2END_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + $(E) "[LD] Linking $@" + $(Q) mkdir -p `dirname $@` + $(Q) $(LDXX) $(LDFLAGS) $(HEALTH_SERVICE_END2END_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/health_service_end2end_test + +endif + +endif + +$(OBJDIR)/$(CONFIG)/test/cpp/end2end/health_service_end2end_test.o: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a + +deps_health_service_end2end_test: $(HEALTH_SERVICE_END2END_TEST_OBJS:.o=.dep) + +ifneq ($(NO_SECURE),true) +ifneq ($(NO_DEPS),true) +-include $(HEALTH_SERVICE_END2END_TEST_OBJS:.o=.dep) +endif +endif + + HYBRID_END2END_TEST_SRC = \ test/cpp/end2end/hybrid_end2end_test.cc \ diff --git a/build.yaml b/build.yaml index 54635fe369..8be83187b1 100644 --- a/build.yaml +++ b/build.yaml @@ -3192,6 +3192,19 @@ targets: - grpc++ - grpc++_test_util - grpc_test_util +- name: health_service_end2end_test + gtest: true + build: test + language: c++ + src: + - test/cpp/end2end/health_service_end2end_test.cc + deps: + - grpc++_test_util + - grpc_test_util + - grpc++ + - grpc + - gpr_test_util + - gpr - name: hybrid_end2end_test gtest: true build: test diff --git a/include/grpc++/server.h b/include/grpc++/server.h index dc97a0ec4c..e3e9174c9c 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -219,6 +219,7 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { std::unique_ptr server_initializer_; std::unique_ptr health_check_service_; + bool health_check_service_disabled_; }; } // namespace grpc diff --git a/src/cpp/server/default_health_check_service.cc b/src/cpp/server/default_health_check_service.cc index 704243e831..ae10ec92c6 100644 --- a/src/cpp/server/default_health_check_service.cc +++ b/src/cpp/server/default_health_check_service.cc @@ -31,9 +31,11 @@ * */ +#include #include #include +#include #include "src/cpp/server/default_health_check_service.h" @@ -57,11 +59,12 @@ DefaultHealthCheckService::SyncHealthCheckServiceImpl:: Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( ServerContext* context, const ByteBuffer* request, ByteBuffer* response) { - // TODO nanopb + // TODO nanopb part return Status::OK; } -DefaultHealthCheckService::DefaultHealthCheckService() { +DefaultHealthCheckService::DefaultHealthCheckService() + : sync_service_(new SyncHealthCheckServiceImpl(this)) { services_map_.emplace("", true); } diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 4eaafeabb8..b8558cfc3d 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -360,7 +360,8 @@ Server::Server( shutdown_notified_(false), has_generic_service_(false), server_(nullptr), - server_initializer_(new ServerInitializer(this)) { + server_initializer_(new ServerInitializer(this)), + health_check_service_disabled_(false) { g_gli_initializer.summon(); gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); global_callbacks_ = g_callbacks; @@ -376,12 +377,11 @@ Server::Server( grpc_channel_args channel_args; args->SetChannelArgs(&channel_args); - bool health_check_service_disabled = false; for (size_t i = 0; i < channel_args.num_args; i++) { if (0 == strcmp(channel_args.args[i].key, kDefaultHealthCheckServiceInterfaceArg)) { if (channel_args.args[i].value.pointer.p == nullptr) { - health_check_service_disabled = true; + health_check_service_disabled_ = true; } else { health_check_service_.reset(static_cast( channel_args.args[i].value.pointer.p)); @@ -389,17 +389,6 @@ Server::Server( break; } } - // Only create default health check service when user did not provide an - // explicit one. - if (health_check_service_ == nullptr && !health_check_service_disabled && - DefaultHealthCheckServiceEnabled()) { - auto* default_hc_service = new DefaultHealthCheckService; - health_check_service_.reset(default_hc_service); - if (!sync_server_cqs->empty()) { // Has sync methods. - grpc::string host; - RegisterService(&host, default_hc_service->GetSyncHealthCheckService()); - } - } server_ = grpc_server_create(&channel_args, nullptr); } @@ -506,6 +495,18 @@ int Server::AddListeningPort(const grpc::string& addr, bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { GPR_ASSERT(!started_); started_ = true; + + // Only create default health check service when user did not provide an + // explicit one. + if (health_check_service_ == nullptr && !health_check_service_disabled_ && + DefaultHealthCheckServiceEnabled()) { + auto* default_hc_service = new DefaultHealthCheckService; + health_check_service_.reset(default_hc_service); + if (!sync_server_cqs_->empty()) { // Has sync methods. + RegisterService(nullptr, default_hc_service->GetSyncHealthCheckService()); + } + } + grpc_server_start(server_); if (!has_generic_service_) { diff --git a/test/cpp/end2end/health_service_end2end_test.cc b/test/cpp/end2end/health_service_end2end_test.cc new file mode 100644 index 0000000000..17e8c4b761 --- /dev/null +++ b/test/cpp/end2end/health_service_end2end_test.cc @@ -0,0 +1,158 @@ +/* + * + * Copyright 2016, 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 +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "src/proto/grpc/health/v1/health.grpc.pb.h" +#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" +#include "src/proto/grpc/testing/echo.grpc.pb.h" +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" +#include "test/cpp/end2end/test_service_impl.h" + +using grpc::health::v1::Health; +using grpc::health::v1::HealthCheckRequest; +using grpc::health::v1::HealthCheckResponse; + +namespace grpc { +namespace testing { +namespace { + +class HealthServiceEnd2endTest : public ::testing::Test { + protected: + HealthServiceEnd2endTest() {} + + void SetUpServer(grpc::Service* explicit_health_service) { + int port = grpc_pick_unused_port_or_die(); + server_address_ << "localhost:" << port; + + // Setup server + ServerBuilder builder; + builder.AddListeningPort(server_address_.str(), + grpc::InsecureServerCredentials()); + // Register a sync service. + builder.RegisterService(&echo_test_service_); + server_ = builder.BuildAndStart(); + } + + void TearDown() override { + if (server_) { + server_->Shutdown(); + } + } + + void ResetStubs() { + std::shared_ptr channel = + CreateChannel(server_address_.str(), InsecureChannelCredentials()); + stub_ = grpc::testing::EchoTestService::NewStub(channel); + hc_stub_ = grpc::health::v1::Health::NewStub(channel); + } + + void SendHealthCheckRpc( + const grpc::string& service_name, const Status& expected_status, + HealthCheckResponse::ServingStatus expected_serving_status) { + HealthCheckRequest request; + request.set_service(service_name); + HealthCheckResponse response; + ClientContext context; + Status s = hc_stub_->Check(&context, request, &response); + EXPECT_EQ(expected_status.error_code(), s.error_code()); + // EXPECT_EQ(expected_status.error_details(), s.error_details()); + if (s.ok()) { + EXPECT_EQ(expected_serving_status, response.status()); + } + } + + TestServiceImpl echo_test_service_; + std::unique_ptr stub_; + std::unique_ptr hc_stub_; + std::unique_ptr server_; + std::ostringstream server_address_; +}; + +TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) { + EnableDefaultHealthCheckService(false); + EXPECT_FALSE(DefaultHealthCheckServiceEnabled()); + SetUpServer(nullptr); + HealthCheckServiceInterface* default_service = + server_->GetHealthCheckService(); + EXPECT_TRUE(default_service == nullptr); +} + +TEST_F(HealthServiceEnd2endTest, DefaultHealthService) { + EnableDefaultHealthCheckService(true); + EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); + SetUpServer(nullptr); + HealthCheckServiceInterface* default_service = + server_->GetHealthCheckService(); + EXPECT_TRUE(default_service != nullptr); + const grpc::string kHealthyService("healthy_service"); + const grpc::string kUnhealthyService("unhealthy_service"); + default_service->SetServingStatus(kHealthyService, true); + default_service->SetServingStatus(kUnhealthyService, false); + + ResetStubs(); + + SendHealthCheckRpc("", Status::OK, HealthCheckResponse::SERVING); + SendHealthCheckRpc(kHealthyService, Status::OK, HealthCheckResponse::SERVING); + SendHealthCheckRpc(kUnhealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); + + default_service->SetServingStatus(false); + SendHealthCheckRpc("", Status::OK, HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kHealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kUnhealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); +} + +} // namespace +} // namespace testing +} // namespace grpc + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 02d03bd616..9366e3366b 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -2772,6 +2772,25 @@ "third_party": false, "type": "target" }, + { + "deps": [ + "gpr", + "gpr_test_util", + "grpc", + "grpc++", + "grpc++_test_util", + "grpc_test_util" + ], + "headers": [], + "is_filegroup": false, + "language": "c++", + "name": "health_service_end2end_test", + "src": [ + "test/cpp/end2end/health_service_end2end_test.cc" + ], + "third_party": false, + "type": "target" + }, { "deps": [ "gpr", diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index b76263b8b9..1cd04ea5dd 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -2805,6 +2805,28 @@ "windows" ] }, + { + "args": [], + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": true, + "language": "c++", + "name": "health_service_end2end_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ] + }, { "args": [], "ci_platforms": [ diff --git a/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj b/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj new file mode 100644 index 0000000000..28530d07b5 --- /dev/null +++ b/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj @@ -0,0 +1,207 @@ + + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {3EB2B3E9-8BC3-8DF7-82CB-38462FFE5919} + true + $(SolutionDir)IntDir\$(MSBuildProjectName)\ + + + + v100 + + + v110 + + + v120 + + + v140 + + + Application + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + + + + health_service_end2end_test + static + Debug + static + Debug + + + health_service_end2end_test + static + Release + static + Release + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + MultiThreadedDebug + true + None + false + + + Console + true + false + + + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + MultiThreadedDebug + true + None + false + + + Console + true + false + + + + + + NotUsing + Level3 + MaxSpeed + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + true + true + MultiThreaded + true + None + false + + + Console + true + false + true + true + + + + + + NotUsing + Level3 + MaxSpeed + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + true + true + MultiThreaded + true + None + false + + + Console + true + false + true + true + + + + + + + + + + {0BE77741-552A-929B-A497-4EF7ECE17A64} + + + {17BCAFC0-5FDC-4C94-AEB9-95F3E220614B} + + + {C187A093-A0FE-489D-A40A-6E33DE0F9FEB} + + + {29D16885-7228-4C31-81ED-5F9187C7F2A9} + + + {EAB0A629-17A9-44DB-B5FF-E91A721FE037} + + + {B23D3D1A-9438-4EDA-BEB6-9A0A03D17792} + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + + diff --git a/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj.filters b/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj.filters new file mode 100644 index 0000000000..cccf968286 --- /dev/null +++ b/vsprojects/vcxproj/test/health_service_end2end_test/health_service_end2end_test.vcxproj.filters @@ -0,0 +1,21 @@ + + + + + test\cpp\end2end + + + + + + {00d750b2-db02-2106-d9b7-1d3b2ca58604} + + + {02e29b2f-d68a-4474-8483-621ecfd7fa9d} + + + {b0de697a-d73a-23e1-c9af-fa0edf011d4d} + + + + -- cgit v1.2.3 From 9bef0740a4ad649b6eb7438fc9575dfc1e98250e Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 27 Dec 2016 15:31:57 -0800 Subject: Add test for explicit option --- .../health_check_service_server_builder_option.h | 2 +- .../server/health/default_health_check_service.cc | 7 +- test/cpp/end2end/health_service_end2end_test.cc | 186 +++++++++++++++++---- 3 files changed, 159 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h index e00c4c3dd5..6a8fd94317 100644 --- a/include/grpc++/ext/health_check_service_server_builder_option.h +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -47,7 +47,7 @@ class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { // Use nullptr to disable default service. explicit HealthCheckServiceServerBuilderOption( std::unique_ptr hc); - ~HealthCheckServiceServerBuilderOption() {} + ~HealthCheckServiceServerBuilderOption() override {} void UpdateArguments(ChannelArguments* args) override; void UpdatePlugins(std::vector>* plugins) override; private: diff --git a/src/cpp/server/health/default_health_check_service.cc b/src/cpp/server/health/default_health_check_service.cc index bc7d576741..cec7ecce70 100644 --- a/src/cpp/server/health/default_health_check_service.cc +++ b/src/cpp/server/health/default_health_check_service.cc @@ -35,6 +35,7 @@ #include #include +#include #include #include "src/cpp/server/health/default_health_check_service.h" @@ -65,18 +66,18 @@ Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( // Decode request. std::vector slices; request->Dump(&slices); - const uint8_t* request_bytes = nullptr; + uint8_t* request_bytes = nullptr; bool request_bytes_owned = false; size_t request_size = 0; grpc_health_v1_HealthCheckRequest request_struct; if (slices.empty()) { request_struct.has_service = false; } else if (slices.size() == 1) { - request_bytes = slices[0].begin(); + request_bytes = const_cast(slices[0].begin()); request_size = slices[0].size(); } else { request_bytes_owned = true; - request_bytes = gpr_malloc(request->Length()); + request_bytes = static_cast(gpr_malloc(request->Length())); uint8_t* copy_to = request_bytes; for (size_t i = 0; i < slices.size(); i++) { memcpy(copy_to, slices[i].begin(), slices[i].size()); diff --git a/test/cpp/end2end/health_service_end2end_test.cc b/test/cpp/end2end/health_service_end2end_test.cc index 02aa5cea3f..969aea1318 100644 --- a/test/cpp/end2end/health_service_end2end_test.cc +++ b/test/cpp/end2end/health_service_end2end_test.cc @@ -32,11 +32,14 @@ */ #include +#include #include +#include #include #include #include +#include #include #include #include @@ -59,20 +62,92 @@ namespace grpc { namespace testing { namespace { +// A sample sync implementation of the health checking service. This does the +// same thing as the default one. +class HealthCheckServiceImpl : public ::grpc::health::v1::Health::Service { + public: + Status Check(ServerContext* context, const HealthCheckRequest* request, + HealthCheckResponse* response) override { + std::lock_guard lock(mu_); + auto iter = status_map_.find(request->service()); + if (iter == status_map_.end()) { + return Status(StatusCode::NOT_FOUND, ""); + } + response->set_status(iter->second); + return Status::OK; + } + + void SetStatus(const grpc::string& service_name, + HealthCheckResponse::ServingStatus status) { + std::lock_guard lock(mu_); + status_map_[service_name] = status; + } + + void SetAll(HealthCheckResponse::ServingStatus status) { + std::lock_guard lock(mu_); + for (auto iter = status_map_.begin(); iter != status_map_.end(); ++iter) { + iter->second = status; + } + } + + private: + std::mutex mu_; + std::map status_map_; +}; + +// A custom implementation of the health checking service interface. This is +// used to test that it prevents the server from creating a default service and +// also serves as an example of how to override the default service. +class CustomHealthCheckService : public HealthCheckServiceInterface { + public: + explicit CustomHealthCheckService(HealthCheckServiceImpl* impl) + : impl_(impl) { + impl_->SetStatus("", HealthCheckResponse::SERVING); + } + void SetServingStatus(const grpc::string& service_name, + bool serving) override { + impl_->SetStatus(service_name, serving ? HealthCheckResponse::SERVING + : HealthCheckResponse::NOT_SERVING); + } + + void SetServingStatus(bool serving) override { + impl_->SetAll(serving ? HealthCheckResponse::SERVING + : HealthCheckResponse::NOT_SERVING); + } + + private: + HealthCheckServiceImpl* impl_; // not owned +}; + class HealthServiceEnd2endTest : public ::testing::Test { protected: HealthServiceEnd2endTest() {} - void SetUpServer(grpc::Service* explicit_health_service) { + void SetUpServer(bool register_sync_test_service, + bool explicit_health_service, + std::unique_ptr service) { int port = grpc_pick_unused_port_or_die(); server_address_ << "localhost:" << port; + bool register_sync_health_service_impl = + explicit_health_service && service != nullptr; + // Setup server ServerBuilder builder; + if (explicit_health_service) { + std::unique_ptr option( + new HealthCheckServiceServerBuilderOption(std::move(service))); + builder.SetOption(std::move(option)); + } builder.AddListeningPort(server_address_.str(), grpc::InsecureServerCredentials()); - // Register a sync service. - builder.RegisterService(&echo_test_service_); + if (register_sync_test_service) { + // Register a sync service. + builder.RegisterService(&echo_test_service_); + } + if (register_sync_health_service_impl) { + builder.RegisterService(&health_check_service_impl_); + } server_ = builder.BuildAndStart(); } @@ -88,6 +163,14 @@ class HealthServiceEnd2endTest : public ::testing::Test { hc_stub_ = grpc::health::v1::Health::NewStub(channel); } + // When the expected_status is NOT OK, we do not care about the response. + void SendHealthCheckRpc(const grpc::string& service_name, + const Status& expected_status) { + EXPECT_FALSE(expected_status.ok()); + SendHealthCheckRpc(service_name, expected_status, + HealthCheckResponse::UNKNOWN); + } + void SendHealthCheckRpc( const grpc::string& service_name, const Status& expected_status, HealthCheckResponse::ServingStatus expected_serving_status) { @@ -102,7 +185,37 @@ class HealthServiceEnd2endTest : public ::testing::Test { } } + void VerifyHealthCheckService() { + HealthCheckServiceInterface* service = server_->GetHealthCheckService(); + EXPECT_TRUE(service != nullptr); + const grpc::string kHealthyService("healthy_service"); + const grpc::string kUnhealthyService("unhealthy_service"); + const grpc::string kNotRegisteredService("not_registered"); + service->SetServingStatus(kHealthyService, true); + service->SetServingStatus(kUnhealthyService, false); + + ResetStubs(); + + SendHealthCheckRpc("", Status::OK, HealthCheckResponse::SERVING); + SendHealthCheckRpc(kHealthyService, Status::OK, + HealthCheckResponse::SERVING); + SendHealthCheckRpc(kUnhealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kNotRegisteredService, + Status(StatusCode::NOT_FOUND, "")); + + service->SetServingStatus(false); + SendHealthCheckRpc("", Status::OK, HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kHealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kUnhealthyService, Status::OK, + HealthCheckResponse::NOT_SERVING); + SendHealthCheckRpc(kNotRegisteredService, + Status(StatusCode::NOT_FOUND, "")); + } + TestServiceImpl echo_test_service_; + HealthCheckServiceImpl health_check_service_impl_; std::unique_ptr hc_stub_; std::unique_ptr server_; std::ostringstream server_address_; @@ -111,47 +224,56 @@ class HealthServiceEnd2endTest : public ::testing::Test { TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) { EnableDefaultHealthCheckService(false); EXPECT_FALSE(DefaultHealthCheckServiceEnabled()); - SetUpServer(nullptr); + SetUpServer(true, false, nullptr); HealthCheckServiceInterface* default_service = server_->GetHealthCheckService(); EXPECT_TRUE(default_service == nullptr); + + ResetStubs(); + + SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); } TEST_F(HealthServiceEnd2endTest, DefaultHealthService) { EnableDefaultHealthCheckService(true); EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); - SetUpServer(nullptr); - HealthCheckServiceInterface* default_service = - server_->GetHealthCheckService(); - EXPECT_TRUE(default_service != nullptr); - const grpc::string kHealthyService("healthy_service"); - const grpc::string kUnhealthyService("unhealthy_service"); - const grpc::string kNotRegisteredService("not_registered"); + SetUpServer(true, false, nullptr); + VerifyHealthCheckService(); + + // The default service has a size limit of the service name. const grpc::string kTooLongServiceName(201, 'x'); - default_service->SetServingStatus(kHealthyService, true); - default_service->SetServingStatus(kUnhealthyService, false); + SendHealthCheckRpc(kTooLongServiceName, + Status(StatusCode::INVALID_ARGUMENT, "")); +} + +// Provide an empty service to disable the default service. +TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) { + EnableDefaultHealthCheckService(true); + EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); + std::unique_ptr empty_service; + SetUpServer(true, true, std::move(empty_service)); + HealthCheckServiceInterface* service = server_->GetHealthCheckService(); + EXPECT_TRUE(service == nullptr); + + ResetStubs(); + + SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); +} + +// Provide an explicit override of health checking service interface. +TEST_F(HealthServiceEnd2endTest, ExplicitlyOverride) { + EnableDefaultHealthCheckService(true); + EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); + std::unique_ptr override_service( + new CustomHealthCheckService(&health_check_service_impl_)); + HealthCheckServiceInterface* underlying_service = override_service.get(); + SetUpServer(false, true, std::move(override_service)); + HealthCheckServiceInterface* service = server_->GetHealthCheckService(); + EXPECT_TRUE(service == underlying_service); ResetStubs(); - SendHealthCheckRpc("", Status::OK, HealthCheckResponse::SERVING); - SendHealthCheckRpc(kHealthyService, Status::OK, HealthCheckResponse::SERVING); - SendHealthCheckRpc(kUnhealthyService, Status::OK, - HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kNotRegisteredService, Status(StatusCode::NOT_FOUND, ""), - HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kTooLongServiceName, Status(StatusCode::INVALID_ARGUMENT, ""), - HealthCheckResponse::NOT_SERVING); - - default_service->SetServingStatus(false); - SendHealthCheckRpc("", Status::OK, HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kHealthyService, Status::OK, - HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kUnhealthyService, Status::OK, - HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kNotRegisteredService, Status(StatusCode::NOT_FOUND, ""), - HealthCheckResponse::NOT_SERVING); - SendHealthCheckRpc(kTooLongServiceName, Status(StatusCode::INVALID_ARGUMENT, ""), - HealthCheckResponse::NOT_SERVING); + VerifyHealthCheckService(); } } // namespace -- cgit v1.2.3 From f56c251af79577edfda3da795c8a26f31e4c7e01 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 27 Dec 2016 15:36:20 -0800 Subject: clang-format --- .../grpc++/ext/health_check_service_server_builder_option.h | 4 +++- src/cpp/ext/health_check_service_server_builder_option.cc | 11 ++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h index 6a8fd94317..2dd1e6dd32 100644 --- a/include/grpc++/ext/health_check_service_server_builder_option.h +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -49,7 +49,9 @@ class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { std::unique_ptr hc); ~HealthCheckServiceServerBuilderOption() override {} void UpdateArguments(ChannelArguments* args) override; - void UpdatePlugins(std::vector>* plugins) override; + void UpdatePlugins( + std::vector>* plugins) override; + private: std::unique_ptr hc_; }; diff --git a/src/cpp/ext/health_check_service_server_builder_option.cc b/src/cpp/ext/health_check_service_server_builder_option.cc index 4afc82dc14..0d523285e9 100644 --- a/src/cpp/ext/health_check_service_server_builder_option.cc +++ b/src/cpp/ext/health_check_service_server_builder_option.cc @@ -36,14 +36,15 @@ namespace grpc { HealthCheckServiceServerBuilderOption::HealthCheckServiceServerBuilderOption( - std::unique_ptr hc) : hc_(std::move(hc)) { } + std::unique_ptr hc) + : hc_(std::move(hc)) {} -HealthCheckServiceServerBuilderOption::UpdateArguments(ChannelArguments* args) override { +HealthCheckServiceServerBuilderOption::UpdateArguments( + ChannelArguments* args) override { args->SetPointer(DefaultHealthCheckServiceInterfaceArg(), hc_.release()); } -void HealthCheckServiceServerBuilderOption::UpdatePlugins(std::vector>* plugins) override { -} +void HealthCheckServiceServerBuilderOption::UpdatePlugins( + std::vector>* plugins) override {} } // namespace grpc - -- cgit v1.2.3 From 50993b7a4c083b8b202010aca1bb95cf9902cf74 Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 29 Dec 2016 10:00:27 -0800 Subject: async first take --- include/grpc++/impl/codegen/server_interface.h | 7 +- include/grpc++/server.h | 3 + .../server/health/default_health_check_service.cc | 54 +++++++++----- .../server/health/default_health_check_service.h | 16 +++++ src/cpp/server/server_cc.cc | 82 +++++++++++++++++++++- test/cpp/end2end/health_service_end2end_test.cc | 30 ++++++++ 6 files changed, 170 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/include/grpc++/impl/codegen/server_interface.h b/include/grpc++/impl/codegen/server_interface.h index 666b9ff66e..2acb174baf 100644 --- a/include/grpc++/impl/codegen/server_interface.h +++ b/include/grpc++/impl/codegen/server_interface.h @@ -159,7 +159,8 @@ class ServerInterface : public CallHook { public: RegisteredAsyncRequest(ServerInterface* server, ServerContext* context, ServerAsyncStreamingInterface* stream, - CompletionQueue* call_cq, void* tag); + CompletionQueue* call_cq, void* tag, + bool delete_on_finalize); // uses BaseAsyncRequest::FinalizeResult @@ -175,7 +176,7 @@ class ServerInterface : public CallHook { ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) - : RegisteredAsyncRequest(server, context, stream, call_cq, tag) { + : RegisteredAsyncRequest(server, context, stream, call_cq, tag, true) { IssueRequest(registered_method, nullptr, notification_cq); } @@ -191,7 +192,7 @@ class ServerInterface : public CallHook { CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag, Message* request) - : RegisteredAsyncRequest(server, context, stream, call_cq, tag), + : RegisteredAsyncRequest(server, context, stream, call_cq, tag, true), request_(request) { IssueRequest(registered_method, &payload_, notification_cq); } diff --git a/include/grpc++/server.h b/include/grpc++/server.h index e3e9174c9c..9f31d00ef0 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -119,6 +119,9 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { class UnimplementedAsyncRequest; class UnimplementedAsyncResponse; + class HealthCheckAsyncRequestContext; + class HealthCheckAsyncRequest; + /// Server constructors. To be used by \a ServerBuilder only. /// /// \param max_message_size Maximum message length that the channel can diff --git a/src/cpp/server/health/default_health_check_service.cc b/src/cpp/server/health/default_health_check_service.cc index cec7ecce70..26d8fd999f 100644 --- a/src/cpp/server/health/default_health_check_service.cc +++ b/src/cpp/server/health/default_health_check_service.cc @@ -48,21 +48,9 @@ namespace { const char kHealthCheckMethodName[] = "/grpc.health.v1.Health/Check"; -} // namespace - -DefaultHealthCheckService::SyncHealthCheckServiceImpl:: - SyncHealthCheckServiceImpl(DefaultHealthCheckService* service) - : service_(service) { - auto* handler = - new RpcMethodHandler( - std::mem_fn(&SyncHealthCheckServiceImpl::Check), this); - auto* method = new RpcServiceMethod(kHealthCheckMethodName, - RpcMethod::NORMAL_RPC, handler); - AddMethod(method); -} - -Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( - ServerContext* context, const ByteBuffer* request, ByteBuffer* response) { +Status CheckHealth(const DefaultHealthCheckService* service, + ServerContext* context, const ByteBuffer* request, + ByteBuffer* response) { // Decode request. std::vector slices; request->Dump(&slices); @@ -99,7 +87,7 @@ Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( // Check status from the associated default health checking service. DefaultHealthCheckService::ServingStatus serving_status = - service_->GetServingStatus( + service->GetServingStatus( request_struct.has_service ? request_struct.service : ""); if (serving_status == DefaultHealthCheckService::NOT_FOUND) { return Status(StatusCode::NOT_FOUND, ""); @@ -129,9 +117,41 @@ Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( response->Swap(&response_buffer); return Status::OK; } +} // namespace + +DefaultHealthCheckService::SyncHealthCheckServiceImpl:: + SyncHealthCheckServiceImpl(DefaultHealthCheckService* service) + : service_(service) { + auto* handler = + new RpcMethodHandler( + std::mem_fn(&SyncHealthCheckServiceImpl::Check), this); + auto* method = new RpcServiceMethod(kHealthCheckMethodName, + RpcMethod::NORMAL_RPC, handler); + AddMethod(method); +} + +Status DefaultHealthCheckService::SyncHealthCheckServiceImpl::Check( + ServerContext* context, const ByteBuffer* request, ByteBuffer* response) { + return CheckHealth(service_, context, request, response); +} + +DefaultHealthCheckService::AsyncHealthCheckServiceImpl:: + AsyncHealthCheckServiceImpl(DefaultHealthCheckService* service) + : service_(service) { + auto* method = new RpcServiceMethod(kHealthCheckMethodName, + RpcMethod::NORMAL_RPC, nullptr); + AddMethod(method); + method_ = method; +} + +Status DefaultHealthCheckService::AsyncHealthCheckServiceImpl::Check( + ServerContext* context, const ByteBuffer* request, ByteBuffer* response) { + return CheckHealth(service_, context, request, response); +} DefaultHealthCheckService::DefaultHealthCheckService() - : sync_service_(new SyncHealthCheckServiceImpl(this)) { + : sync_service_(new SyncHealthCheckServiceImpl(this)), + async_service_(new AsyncHealthCheckServiceImpl(this)) { services_map_.emplace("", true); } diff --git a/src/cpp/server/health/default_health_check_service.h b/src/cpp/server/health/default_health_check_service.h index 541c720aaa..411aac9713 100644 --- a/src/cpp/server/health/default_health_check_service.h +++ b/src/cpp/server/health/default_health_check_service.h @@ -56,6 +56,18 @@ class DefaultHealthCheckService : public HealthCheckServiceInterface { const DefaultHealthCheckService* service_; }; + class AsyncHealthCheckServiceImpl : public Service { + public: + explicit AsyncHealthCheckServiceImpl(DefaultHealthCheckService* service); + Status Check(ServerContext* context, const ByteBuffer* request, + ByteBuffer* response); + const RpcServiceMethod* method() const { return method_; } + + private: + const DefaultHealthCheckService* service_; + const RpcServiceMethod* method_; + }; + DefaultHealthCheckService(); void SetServingStatus(const grpc::string& service_name, bool serving) final; void SetServingStatus(bool serving) final; @@ -64,11 +76,15 @@ class DefaultHealthCheckService : public HealthCheckServiceInterface { SyncHealthCheckServiceImpl* GetSyncHealthCheckService() const { return sync_service_.get(); } + AsyncHealthCheckServiceImpl* GetAsyncHealthCheckService() const { + return async_service_.get(); + } private: mutable std::mutex mu_; std::map services_map_; std::unique_ptr sync_service_; + std::unique_ptr async_service_; }; } // namespace grpc diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index c50c076bdc..20641aeea8 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -118,6 +119,67 @@ class Server::UnimplementedAsyncResponse final UnimplementedAsyncRequest* const request_; }; +class Server::HealthCheckAsyncRequestContext { + protected: + HealthCheckAsyncRequestContext() : rpc_(&server_context_) {} + ServerContext server_context_; + ServerAsyncResponseWriter rpc_; +}; + +class Server::HealthCheckAsyncRequest final + : public HealthCheckAsyncRequestContext, + public RegisteredAsyncRequest { + public: + HealthCheckAsyncRequest( + DefaultHealthCheckService::AsyncHealthCheckServiceImpl* service, + Server* server, ServerCompletionQueue* cq) + : RegisteredAsyncRequest(server, &server_context_, &rpc_, cq, this, + false), + service_(service), + server_(server), + cq_(cq), + had_request_(false) { + IssueRequest(service->method()->server_tag(), &payload_, cq); + } + + bool FinalizeResult(void** tag, bool* status) override; + + private: + DefaultHealthCheckService::AsyncHealthCheckServiceImpl* service_; + Server* const server_; + ServerCompletionQueue* const cq_; + grpc_byte_buffer* payload_; + bool had_request_; + ByteBuffer request_; + ByteBuffer response_; +}; + +bool Server::HealthCheckAsyncRequest::FinalizeResult(void** tag, bool* status) { + if (!had_request_) { + had_request_ = true; + bool serialization_status = + *status && payload_ && + SerializationTraits::Deserialize( + payload_, &request_, server_->max_receive_message_size()) + .ok(); + RegisteredAsyncRequest::FinalizeResult(tag, status); + *status = serialization_status && *status; + if (*status) { + new HealthCheckAsyncRequest(service_, server_, cq_); + Status s = service_->Check(&server_context_, &request_, &response_); + rpc_.Finish(response_, s, this); + return false; + } else { + // TODO what to do here + delete this; + return false; + } + } else { + delete this; + return false; + } +} + class ShutdownTag : public CompletionQueueTag { public: bool FinalizeResult(void** tag, bool* status) { return false; } @@ -498,6 +560,8 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { // Only create default health check service when user did not provide an // explicit one. + DefaultHealthCheckService::AsyncHealthCheckServiceImpl* async_health_service = + nullptr; if (health_check_service_ == nullptr && !health_check_service_disabled_ && DefaultHealthCheckServiceEnabled()) { auto* default_hc_service = new DefaultHealthCheckService; @@ -505,6 +569,10 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { if (!sync_server_cqs_->empty()) { // Has sync methods. RegisterService(nullptr, default_hc_service->GetSyncHealthCheckService()); } + if (sync_server_cqs_->empty()) { // No sync methods. + async_health_service = default_hc_service->GetAsyncHealthCheckService(); + RegisterService(nullptr, async_health_service); + } } grpc_server_start(server_); @@ -521,6 +589,14 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { } } + if (async_health_service) { + for (size_t i = 0; i < num_cqs; i++) { + if (cqs[i]->IsFrequentlyPolled()) { + new HealthCheckAsyncRequest(async_health_service, this, cqs[i]); + } + } + } + for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { (*it)->Start(); } @@ -641,8 +717,10 @@ bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag, ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest( ServerInterface* server, ServerContext* context, - ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) - : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {} + ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag, + bool delete_on_finalize) + : BaseAsyncRequest(server, context, stream, call_cq, tag, + delete_on_finalize) {} void ServerInterface::RegisteredAsyncRequest::IssueRequest( void* registered_method, grpc_byte_buffer** payload, diff --git a/test/cpp/end2end/health_service_end2end_test.cc b/test/cpp/end2end/health_service_end2end_test.cc index 969aea1318..c41a75ec37 100644 --- a/test/cpp/end2end/health_service_end2end_test.cc +++ b/test/cpp/end2end/health_service_end2end_test.cc @@ -45,6 +45,7 @@ #include #include #include +#include #include #include "src/proto/grpc/health/v1/health.grpc.pb.h" @@ -148,12 +149,17 @@ class HealthServiceEnd2endTest : public ::testing::Test { if (register_sync_health_service_impl) { builder.RegisterService(&health_check_service_impl_); } + cq_ = builder.AddCompletionQueue(); server_ = builder.BuildAndStart(); } void TearDown() override { if (server_) { server_->Shutdown(); + cq_->Shutdown(); + if (cq_thread_.joinable()) { + cq_thread_.join(); + } } } @@ -219,6 +225,8 @@ class HealthServiceEnd2endTest : public ::testing::Test { std::unique_ptr hc_stub_; std::unique_ptr server_; std::ostringstream server_address_; + std::unique_ptr cq_; + std::thread cq_thread_; }; TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) { @@ -246,6 +254,28 @@ TEST_F(HealthServiceEnd2endTest, DefaultHealthService) { Status(StatusCode::INVALID_ARGUMENT, "")); } +void LoopCompletionQueue(ServerCompletionQueue* cq) { + void* tag; + bool ok; + while (cq->Next(&tag, &ok)) { + gpr_log(GPR_ERROR, "next %p %d", tag, ok); + } + gpr_log(GPR_ERROR, "returning from thread"); +} + +TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceAsync) { + EnableDefaultHealthCheckService(true); + EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); + SetUpServer(false, false, nullptr); + cq_thread_ = std::thread(LoopCompletionQueue, cq_.get()); + VerifyHealthCheckService(); + + // The default service has a size limit of the service name. + const grpc::string kTooLongServiceName(201, 'x'); + SendHealthCheckRpc(kTooLongServiceName, + Status(StatusCode::INVALID_ARGUMENT, "")); +} + // Provide an empty service to disable the default service. TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) { EnableDefaultHealthCheckService(true); -- cgit v1.2.3 From 8e708b12cb1c88f5cb6e3984d887c3c2b9bee54f Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 29 Dec 2016 11:44:36 -0800 Subject: WIP --- include/grpc++/server.h | 1 + src/cpp/server/server_cc.cc | 103 ++++++++++++++----- test/cpp/end2end/health_service_end2end_test.cc | 126 ++++++++++++------------ 3 files changed, 141 insertions(+), 89 deletions(-) (limited to 'include') diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 9f31d00ef0..3f205625ee 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -121,6 +121,7 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { class HealthCheckAsyncRequestContext; class HealthCheckAsyncRequest; + class HealthCheckAsyncResponse; /// Server constructors. To be used by \a ServerBuilder only. /// diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 20641aeea8..43f0947095 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -119,11 +119,24 @@ class Server::UnimplementedAsyncResponse final UnimplementedAsyncRequest* const request_; }; +class HealthCheckAsyncResponseWriter final + : public ServerAsyncStreamingInterface { + public: + HealthCheckAsyncResponseWriter() : call_(nullptr, nullptr, nullptr) {} + void SendInitialMetadata(void* tag) override { + abort(); // should not be called. + } + void BindCall(Call* call) override { call_ = *call; } + Call* call() { return &call_; } + + private: + Call call_; +}; + class Server::HealthCheckAsyncRequestContext { protected: - HealthCheckAsyncRequestContext() : rpc_(&server_context_) {} ServerContext server_context_; - ServerAsyncResponseWriter rpc_; + HealthCheckAsyncResponseWriter rpc_; }; class Server::HealthCheckAsyncRequest final @@ -137,49 +150,86 @@ class Server::HealthCheckAsyncRequest final false), service_(service), server_(server), - cq_(cq), - had_request_(false) { + cq_(cq) { IssueRequest(service->method()->server_tag(), &payload_, cq); } bool FinalizeResult(void** tag, bool* status) override; + Call* call() { return rpc_.call(); } + ByteBuffer* response() { return &response_; } + Status* status() { return &status_; } + ServerContext* server_context() { return &server_context_; } private: DefaultHealthCheckService::AsyncHealthCheckServiceImpl* service_; Server* const server_; ServerCompletionQueue* const cq_; grpc_byte_buffer* payload_; - bool had_request_; ByteBuffer request_; ByteBuffer response_; + Status status_; +}; + +typedef SneakyCallOpSet + HealthCheckAsyncResponseOp; +class Server::HealthCheckAsyncResponse final + : public HealthCheckAsyncResponseOp { + public: + HealthCheckAsyncResponse(HealthCheckAsyncRequest* request); + ~HealthCheckAsyncResponse() { delete request_; } + + bool FinalizeResult(void** tag, bool* status) override { + HealthCheckAsyncResponseOp::FinalizeResult(tag, status); + delete this; + return false; + } + + private: + HealthCheckAsyncRequest* const request_; }; bool Server::HealthCheckAsyncRequest::FinalizeResult(void** tag, bool* status) { - if (!had_request_) { - had_request_ = true; - bool serialization_status = - *status && payload_ && - SerializationTraits::Deserialize( - payload_, &request_, server_->max_receive_message_size()) - .ok(); - RegisteredAsyncRequest::FinalizeResult(tag, status); - *status = serialization_status && *status; - if (*status) { - new HealthCheckAsyncRequest(service_, server_, cq_); - Status s = service_->Check(&server_context_, &request_, &response_); - rpc_.Finish(response_, s, this); - return false; - } else { - // TODO what to do here - delete this; - return false; - } + bool serialization_status = + *status && payload_ && + SerializationTraits::Deserialize( + payload_, &request_, server_->max_receive_message_size()) + .ok(); + RegisteredAsyncRequest::FinalizeResult(tag, status); + *status = serialization_status && *status; + if (*status) { + new HealthCheckAsyncRequest(service_, server_, cq_); + status_ = service_->Check(&server_context_, &request_, &response_); + new HealthCheckAsyncResponse(this); + return false; } else { delete this; return false; } } +Server::HealthCheckAsyncResponse::HealthCheckAsyncResponse( + HealthCheckAsyncRequest* request) + : request_(request) { + ServerContext* context = request_->server_context(); + if (!context->sent_initial_metadata_) { + SendInitialMetadata(context->initial_metadata_, + context->initial_metadata_flags()); + if (context->compression_level_set()) { + set_compression_level(context->compression_level()); + } + context->sent_initial_metadata_ = true; + } + Status* status = request_->status(); + if (status->ok()) { + ServerSendStatus(context->trailing_metadata_, + SendMessage(*request_->response())); + } else { + ServerSendStatus(context->trailing_metadata_, *status); + } + request_->call()->PerformOps(this); +} + class ShutdownTag : public CompletionQueueTag { public: bool FinalizeResult(void** tag, bool* status) { return false; } @@ -567,9 +617,10 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { auto* default_hc_service = new DefaultHealthCheckService; health_check_service_.reset(default_hc_service); if (!sync_server_cqs_->empty()) { // Has sync methods. + gpr_log(GPR_ERROR, "register sync"); // XXX RegisterService(nullptr, default_hc_service->GetSyncHealthCheckService()); - } - if (sync_server_cqs_->empty()) { // No sync methods. + } else { + gpr_log(GPR_ERROR, "register async"); // XXX async_health_service = default_hc_service->GetAsyncHealthCheckService(); RegisterService(nullptr, async_health_service); } diff --git a/test/cpp/end2end/health_service_end2end_test.cc b/test/cpp/end2end/health_service_end2end_test.cc index c41a75ec37..9f2df90207 100644 --- a/test/cpp/end2end/health_service_end2end_test.cc +++ b/test/cpp/end2end/health_service_end2end_test.cc @@ -242,69 +242,69 @@ TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceDisabled) { SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); } -TEST_F(HealthServiceEnd2endTest, DefaultHealthService) { - EnableDefaultHealthCheckService(true); - EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); - SetUpServer(true, false, nullptr); - VerifyHealthCheckService(); - - // The default service has a size limit of the service name. - const grpc::string kTooLongServiceName(201, 'x'); - SendHealthCheckRpc(kTooLongServiceName, - Status(StatusCode::INVALID_ARGUMENT, "")); -} - -void LoopCompletionQueue(ServerCompletionQueue* cq) { - void* tag; - bool ok; - while (cq->Next(&tag, &ok)) { - gpr_log(GPR_ERROR, "next %p %d", tag, ok); - } - gpr_log(GPR_ERROR, "returning from thread"); -} - -TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceAsync) { - EnableDefaultHealthCheckService(true); - EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); - SetUpServer(false, false, nullptr); - cq_thread_ = std::thread(LoopCompletionQueue, cq_.get()); - VerifyHealthCheckService(); - - // The default service has a size limit of the service name. - const grpc::string kTooLongServiceName(201, 'x'); - SendHealthCheckRpc(kTooLongServiceName, - Status(StatusCode::INVALID_ARGUMENT, "")); -} - -// Provide an empty service to disable the default service. -TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) { - EnableDefaultHealthCheckService(true); - EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); - std::unique_ptr empty_service; - SetUpServer(true, true, std::move(empty_service)); - HealthCheckServiceInterface* service = server_->GetHealthCheckService(); - EXPECT_TRUE(service == nullptr); - - ResetStubs(); - - SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); -} - -// Provide an explicit override of health checking service interface. -TEST_F(HealthServiceEnd2endTest, ExplicitlyOverride) { - EnableDefaultHealthCheckService(true); - EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); - std::unique_ptr override_service( - new CustomHealthCheckService(&health_check_service_impl_)); - HealthCheckServiceInterface* underlying_service = override_service.get(); - SetUpServer(false, true, std::move(override_service)); - HealthCheckServiceInterface* service = server_->GetHealthCheckService(); - EXPECT_TRUE(service == underlying_service); - - ResetStubs(); - - VerifyHealthCheckService(); -} +// TEST_F(HealthServiceEnd2endTest, DefaultHealthService) { +// EnableDefaultHealthCheckService(true); +// EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); +// SetUpServer(true, false, nullptr); +// VerifyHealthCheckService(); +// +// // The default service has a size limit of the service name. +// const grpc::string kTooLongServiceName(201, 'x'); +// SendHealthCheckRpc(kTooLongServiceName, +// Status(StatusCode::INVALID_ARGUMENT, "")); +// } +// +// void LoopCompletionQueue(ServerCompletionQueue* cq) { +// void* tag; +// bool ok; +// while (cq->Next(&tag, &ok)) { +// abort(); // Nothing should come out of the cq. +// } +// gpr_log(GPR_ERROR, "returning from thread"); +// } +// +// TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceAsync) { +// EnableDefaultHealthCheckService(true); +// EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); +// SetUpServer(false, false, nullptr); +// cq_thread_ = std::thread(LoopCompletionQueue, cq_.get()); +// VerifyHealthCheckService(); +// +// // The default service has a size limit of the service name. +// const grpc::string kTooLongServiceName(201, 'x'); +// SendHealthCheckRpc(kTooLongServiceName, +// Status(StatusCode::INVALID_ARGUMENT, "")); +// } +// +// // Provide an empty service to disable the default service. +// TEST_F(HealthServiceEnd2endTest, ExplicitlyDisableViaOverride) { +// EnableDefaultHealthCheckService(true); +// EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); +// std::unique_ptr empty_service; +// SetUpServer(true, true, std::move(empty_service)); +// HealthCheckServiceInterface* service = server_->GetHealthCheckService(); +// EXPECT_TRUE(service == nullptr); +// +// ResetStubs(); +// +// SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); +// } +// +// // Provide an explicit override of health checking service interface. +// TEST_F(HealthServiceEnd2endTest, ExplicitlyOverride) { +// EnableDefaultHealthCheckService(true); +// EXPECT_TRUE(DefaultHealthCheckServiceEnabled()); +// std::unique_ptr override_service( +// new CustomHealthCheckService(&health_check_service_impl_)); +// HealthCheckServiceInterface* underlying_service = override_service.get(); +// SetUpServer(false, true, std::move(override_service)); +// HealthCheckServiceInterface* service = server_->GetHealthCheckService(); +// EXPECT_TRUE(service == underlying_service); +// +// ResetStubs(); +// +// VerifyHealthCheckService(); +// } } // namespace } // namespace testing -- cgit v1.2.3 From b90631d3f08aaea88345a2ce950bf16fbcb77ad0 Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 29 Dec 2016 14:08:13 -0800 Subject: more cleanup --- include/grpc++/health_check_service_interface.h | 13 +++- .../health_check_service_server_builder_option.cc | 2 +- src/cpp/server/server_cc.cc | 89 +++++++++++----------- 3 files changed, 56 insertions(+), 48 deletions(-) (limited to 'include') diff --git a/include/grpc++/health_check_service_interface.h b/include/grpc++/health_check_service_interface.h index 70153d65cb..0eed702683 100644 --- a/include/grpc++/health_check_service_interface.h +++ b/include/grpc++/health_check_service_interface.h @@ -38,26 +38,31 @@ namespace grpc { -const char kDefaultHealthCheckServiceInterfaceArg[] = - "grpc.default_health_check_service_interface"; +const char kHealthCheckServiceInterfaceArg[] = + "grpc.health_check_service_interface"; +// The gRPC server uses this interface to expose the health checking service +// without depending on protobuf. class HealthCheckServiceInterface { public: virtual ~HealthCheckServiceInterface() {} + + // Set or change the serving status of the given service_name. virtual void SetServingStatus(const grpc::string& service_name, bool serving) = 0; // Apply to all registered service names. virtual void SetServingStatus(bool serving) = 0; }; -bool DefaultHealthCheckServiceEnabled(); - // Enable/disable the default health checking service. This applies to all C++ // servers created afterwards. For each server, user can override the default // with a HealthCheckServiceServerBuilderOption. // NOT thread safe. void EnableDefaultHealthCheckService(bool enable); +// NOT thread safe. +bool DefaultHealthCheckServiceEnabled(); + } // namespace grpc #endif // GRPCXX_HEALTH_CHECK_SERVICE_INTERFACE_H diff --git a/src/cpp/server/health/health_check_service_server_builder_option.cc b/src/cpp/server/health/health_check_service_server_builder_option.cc index 6680bed2e2..24264204b3 100644 --- a/src/cpp/server/health/health_check_service_server_builder_option.cc +++ b/src/cpp/server/health/health_check_service_server_builder_option.cc @@ -41,7 +41,7 @@ HealthCheckServiceServerBuilderOption::HealthCheckServiceServerBuilderOption( // Hand over hc_ to the server. void HealthCheckServiceServerBuilderOption::UpdateArguments( ChannelArguments* args) { - args->SetPointer(kDefaultHealthCheckServiceInterfaceArg, hc_.release()); + args->SetPointer(kHealthCheckServiceInterfaceArg, hc_.release()); } void HealthCheckServiceServerBuilderOption::UpdatePlugins( diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index d9c972c64a..00aea485db 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -119,6 +119,9 @@ class Server::UnimplementedAsyncResponse final UnimplementedAsyncRequest* const request_; }; +// This is a dummy implementation of the interface so that +// HealthCheckAsyncRequest can get Call from RegisteredAsyncRequest. It does not +// do any reading or writing. class HealthCheckAsyncResponseWriter final : public ServerAsyncStreamingInterface { public: @@ -189,47 +192,6 @@ class Server::HealthCheckAsyncResponse final HealthCheckAsyncRequest* const request_; }; -bool Server::HealthCheckAsyncRequest::FinalizeResult(void** tag, bool* status) { - bool serialization_status = - *status && payload_ && - SerializationTraits::Deserialize( - payload_, &request_, server_->max_receive_message_size()) - .ok(); - RegisteredAsyncRequest::FinalizeResult(tag, status); - *status = serialization_status && *status; - if (*status) { - new HealthCheckAsyncRequest(service_, server_, cq_); - status_ = service_->Check(&server_context_, &request_, &response_); - new HealthCheckAsyncResponse(this); - return false; - } else { - delete this; - return false; - } -} - -Server::HealthCheckAsyncResponse::HealthCheckAsyncResponse( - HealthCheckAsyncRequest* request) - : request_(request) { - ServerContext* context = request_->server_context(); - if (!context->sent_initial_metadata_) { - SendInitialMetadata(context->initial_metadata_, - context->initial_metadata_flags()); - if (context->compression_level_set()) { - set_compression_level(context->compression_level()); - } - context->sent_initial_metadata_ = true; - } - Status* status = request_->status(); - if (status->ok()) { - ServerSendStatus(context->trailing_metadata_, - SendMessage(*request_->response())); - } else { - ServerSendStatus(context->trailing_metadata_, *status); - } - request_->call()->PerformOps(this); -} - class ShutdownTag : public CompletionQueueTag { public: bool FinalizeResult(void** tag, bool* status) { return false; } @@ -490,8 +452,8 @@ Server::Server( args->SetChannelArgs(&channel_args); for (size_t i = 0; i < channel_args.num_args; i++) { - if (0 == strcmp(channel_args.args[i].key, - kDefaultHealthCheckServiceInterfaceArg)) { + if (0 == + strcmp(channel_args.args[i].key, kHealthCheckServiceInterfaceArg)) { if (channel_args.args[i].value.pointer.p == nullptr) { health_check_service_disabled_ = true; } else { @@ -822,6 +784,47 @@ Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse( request_->stream()->call_.PerformOps(this); } +bool Server::HealthCheckAsyncRequest::FinalizeResult(void** tag, bool* status) { + bool serialization_status = + *status && payload_ && + SerializationTraits::Deserialize( + payload_, &request_, server_->max_receive_message_size()) + .ok(); + RegisteredAsyncRequest::FinalizeResult(tag, status); + *status = serialization_status && *status; + if (*status) { + new HealthCheckAsyncRequest(service_, server_, cq_); + status_ = service_->Check(&server_context_, &request_, &response_); + new HealthCheckAsyncResponse(this); + return false; + } else { + delete this; + return false; + } +} + +Server::HealthCheckAsyncResponse::HealthCheckAsyncResponse( + HealthCheckAsyncRequest* request) + : request_(request) { + ServerContext* context = request_->server_context(); + if (!context->sent_initial_metadata_) { + SendInitialMetadata(context->initial_metadata_, + context->initial_metadata_flags()); + if (context->compression_level_set()) { + set_compression_level(context->compression_level()); + } + context->sent_initial_metadata_ = true; + } + Status* status = request_->status(); + if (status->ok()) { + ServerSendStatus(context->trailing_metadata_, + SendMessage(*request_->response())); + } else { + ServerSendStatus(context->trailing_metadata_, *status); + } + request_->call()->PerformOps(this); +} + ServerInitializer* Server::initializer() { return server_initializer_.get(); } } // namespace grpc -- cgit v1.2.3 From 8bd6a0542b7d3ad08497e14fce5470cb50c7bc87 Mon Sep 17 00:00:00 2001 From: yang-g Date: Wed, 4 Jan 2017 13:10:30 -0800 Subject: remove copyright from generated files --- .../health_check_service_server_builder_option.h | 3 +- src/cpp/server/health/health.pb.c | 32 ---------------------- src/cpp/server/health/health.pb.h | 32 ---------------------- 3 files changed, 2 insertions(+), 65 deletions(-) (limited to 'include') diff --git a/include/grpc++/ext/health_check_service_server_builder_option.h b/include/grpc++/ext/health_check_service_server_builder_option.h index 2dd1e6dd32..4861daacd4 100644 --- a/include/grpc++/ext/health_check_service_server_builder_option.h +++ b/include/grpc++/ext/health_check_service_server_builder_option.h @@ -44,7 +44,8 @@ namespace grpc { class HealthCheckServiceServerBuilderOption : public ServerBuilderOption { public: - // Use nullptr to disable default service. + // The ownership of hc will be taken and transferred to the grpc server. + // To explicitly disable default service, pass in a nullptr. explicit HealthCheckServiceServerBuilderOption( std::unique_ptr hc); ~HealthCheckServiceServerBuilderOption() override {} diff --git a/src/cpp/server/health/health.pb.c b/src/cpp/server/health/health.pb.c index 1fca2c55c8..09bd98a3d9 100644 --- a/src/cpp/server/health/health.pb.c +++ b/src/cpp/server/health/health.pb.c @@ -1,35 +1,3 @@ -/* - * - * Copyright 2016, 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. - * - */ /* Automatically generated nanopb constant definitions */ /* Generated by nanopb-0.3.7-dev */ diff --git a/src/cpp/server/health/health.pb.h b/src/cpp/server/health/health.pb.h index dc790fb103..7051b3260a 100644 --- a/src/cpp/server/health/health.pb.h +++ b/src/cpp/server/health/health.pb.h @@ -1,35 +1,3 @@ -/* - * - * Copyright 2016, 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. - * - */ /* Automatically generated nanopb header */ /* Generated by nanopb-0.3.7-dev */ -- cgit v1.2.3 From 076bac017b200accb8071e569b471f118fc3ba32 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 7 Feb 2017 13:50:36 -0800 Subject: Remove async --- include/grpc++/impl/codegen/server_interface.h | 7 +- include/grpc++/server.h | 4 - .../server/health/default_health_check_service.cc | 17 ++- .../server/health/default_health_check_service.h | 11 +- src/cpp/server/server_cc.cc | 140 ++------------------- test/cpp/end2end/health_service_end2end_test.cc | 11 +- 6 files changed, 28 insertions(+), 162 deletions(-) (limited to 'include') diff --git a/include/grpc++/impl/codegen/server_interface.h b/include/grpc++/impl/codegen/server_interface.h index 785568f19a..bd1b36e883 100644 --- a/include/grpc++/impl/codegen/server_interface.h +++ b/include/grpc++/impl/codegen/server_interface.h @@ -158,8 +158,7 @@ class ServerInterface : public CallHook { public: RegisteredAsyncRequest(ServerInterface* server, ServerContext* context, ServerAsyncStreamingInterface* stream, - CompletionQueue* call_cq, void* tag, - bool delete_on_finalize); + CompletionQueue* call_cq, void* tag); // uses BaseAsyncRequest::FinalizeResult @@ -175,7 +174,7 @@ class ServerInterface : public CallHook { ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag) - : RegisteredAsyncRequest(server, context, stream, call_cq, tag, true) { + : RegisteredAsyncRequest(server, context, stream, call_cq, tag) { IssueRequest(registered_method, nullptr, notification_cq); } @@ -191,7 +190,7 @@ class ServerInterface : public CallHook { CompletionQueue* call_cq, ServerCompletionQueue* notification_cq, void* tag, Message* request) - : RegisteredAsyncRequest(server, context, stream, call_cq, tag, true), + : RegisteredAsyncRequest(server, context, stream, call_cq, tag), request_(request) { IssueRequest(registered_method, &payload_, notification_cq); } diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 3f205625ee..e3e9174c9c 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -119,10 +119,6 @@ class Server final : public ServerInterface, private GrpcLibraryCodegen { class UnimplementedAsyncRequest; class UnimplementedAsyncResponse; - class HealthCheckAsyncRequestContext; - class HealthCheckAsyncRequest; - class HealthCheckAsyncResponse; - /// Server constructors. To be used by \a ServerBuilder only. /// /// \param max_message_size Maximum message length that the channel can diff --git a/src/cpp/server/health/default_health_check_service.cc b/src/cpp/server/health/default_health_check_service.cc index 9743bd5775..46def70e8a 100644 --- a/src/cpp/server/health/default_health_check_service.cc +++ b/src/cpp/server/health/default_health_check_service.cc @@ -49,14 +49,11 @@ const char kHealthCheckMethodName[] = "/grpc.health.v1.Health/Check"; } // namespace DefaultHealthCheckService::HealthCheckServiceImpl::HealthCheckServiceImpl( - DefaultHealthCheckService* service, bool sync) - : service_(service), method_(nullptr), sync_(sync) { - MethodHandler* handler = nullptr; - if (sync_) { - handler = - new RpcMethodHandler( - std::mem_fn(&HealthCheckServiceImpl::Check), this); - } + DefaultHealthCheckService* service) + : service_(service), method_(nullptr) { + MethodHandler* handler = + new RpcMethodHandler( + std::mem_fn(&HealthCheckServiceImpl::Check), this); method_ = new RpcServiceMethod(kHealthCheckMethodName, RpcMethod::NORMAL_RPC, handler); AddMethod(method_); @@ -160,9 +157,9 @@ DefaultHealthCheckService::GetServingStatus( } DefaultHealthCheckService::HealthCheckServiceImpl* -DefaultHealthCheckService::GetHealthCheckService(bool sync) { +DefaultHealthCheckService::GetHealthCheckService() { GPR_ASSERT(impl_ == nullptr); - impl_.reset(new HealthCheckServiceImpl(this, sync)); + impl_.reset(new HealthCheckServiceImpl(this)); return impl_.get(); } diff --git a/src/cpp/server/health/default_health_check_service.h b/src/cpp/server/health/default_health_check_service.h index 1ecb0a2ba9..5c0e230342 100644 --- a/src/cpp/server/health/default_health_check_service.h +++ b/src/cpp/server/health/default_health_check_service.h @@ -49,21 +49,14 @@ class DefaultHealthCheckService final : public HealthCheckServiceInterface { // The service impl to register with the server. class HealthCheckServiceImpl : public Service { public: - HealthCheckServiceImpl(DefaultHealthCheckService* service, bool sync); + explicit HealthCheckServiceImpl(DefaultHealthCheckService* service); Status Check(ServerContext* context, const ByteBuffer* request, ByteBuffer* response); - bool sync() { return sync_; } - - // This is only useful for the async mode. It should be called after - // RegisterService returns. - void* server_tag() const { return method_->server_tag(); } - private: const DefaultHealthCheckService* const service_; RpcServiceMethod* method_; - const bool sync_; }; DefaultHealthCheckService(); @@ -72,7 +65,7 @@ class DefaultHealthCheckService final : public HealthCheckServiceInterface { void SetServingStatus(bool serving) override; enum ServingStatus { NOT_FOUND, SERVING, NOT_SERVING }; ServingStatus GetServingStatus(const grpc::string& service_name) const; - HealthCheckServiceImpl* GetHealthCheckService(bool sync); + HealthCheckServiceImpl* GetHealthCheckService(); private: mutable std::mutex mu_; diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 69e491dc96..c377297ec0 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -119,79 +119,6 @@ class Server::UnimplementedAsyncResponse final UnimplementedAsyncRequest* const request_; }; -// This is a dummy implementation of the interface so that -// HealthCheckAsyncRequest can get Call from RegisteredAsyncRequest. It does not -// do any reading or writing. -class HealthCheckAsyncResponseWriter final - : public ServerAsyncStreamingInterface { - public: - HealthCheckAsyncResponseWriter() : call_(nullptr, nullptr, nullptr) {} - void SendInitialMetadata(void* tag) override { - abort(); // should not be called. - } - void BindCall(Call* call) override { call_ = *call; } - Call* call() { return &call_; } - - private: - Call call_; -}; - -class Server::HealthCheckAsyncRequestContext { - protected: - ServerContext server_context_; - HealthCheckAsyncResponseWriter rpc_; -}; - -class Server::HealthCheckAsyncRequest final - : public HealthCheckAsyncRequestContext, - public RegisteredAsyncRequest { - public: - HealthCheckAsyncRequest( - DefaultHealthCheckService::HealthCheckServiceImpl* service, - Server* server, ServerCompletionQueue* cq) - : RegisteredAsyncRequest(server, &server_context_, &rpc_, cq, this, - false), - service_(service), - server_(server), - cq_(cq) { - IssueRequest(service->server_tag(), &payload_, cq); - } - - bool FinalizeResult(void** tag, bool* status) override; - Call* call() { return rpc_.call(); } - ByteBuffer* response() { return &response_; } - Status* status() { return &status_; } - ServerContext* server_context() { return &server_context_; } - - private: - DefaultHealthCheckService::HealthCheckServiceImpl* service_; - Server* const server_; - ServerCompletionQueue* const cq_; - grpc_byte_buffer* payload_; - ByteBuffer request_; - ByteBuffer response_; - Status status_; -}; - -typedef SneakyCallOpSet - HealthCheckAsyncResponseOp; -class Server::HealthCheckAsyncResponse final - : public HealthCheckAsyncResponseOp { - public: - HealthCheckAsyncResponse(HealthCheckAsyncRequest* request); - ~HealthCheckAsyncResponse() { delete request_; } - - bool FinalizeResult(void** tag, bool* status) override { - HealthCheckAsyncResponseOp::FinalizeResult(tag, status); - delete this; - return false; - } - - private: - HealthCheckAsyncRequest* const request_; -}; - class ShutdownTag : public CompletionQueueTag { public: bool FinalizeResult(void** tag, bool* status) { return false; } @@ -572,14 +499,16 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { // Only create default health check service when user did not provide an // explicit one. - DefaultHealthCheckService::HealthCheckServiceImpl* health_service = nullptr; if (health_check_service_ == nullptr && !health_check_service_disabled_ && DefaultHealthCheckServiceEnabled()) { - auto* default_hc_service = new DefaultHealthCheckService; - health_check_service_.reset(default_hc_service); - health_service = - default_hc_service->GetHealthCheckService(!sync_server_cqs_->empty()); - RegisterService(nullptr, health_service); + if (sync_server_cqs_->empty()) { + gpr_log(GPR_ERROR, + "Default health check service disabled at async-only server."); + } else { + auto* default_hc_service = new DefaultHealthCheckService; + health_check_service_.reset(default_hc_service); + RegisterService(nullptr, default_hc_service->GetHealthCheckService()); + } } grpc_server_start(server_); @@ -596,14 +525,6 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { } } - if (health_service && !health_service->sync()) { - for (size_t i = 0; i < num_cqs; i++) { - if (cqs[i]->IsFrequentlyPolled()) { - new HealthCheckAsyncRequest(health_service, this, cqs[i]); - } - } - } - for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { (*it)->Start(); } @@ -715,10 +636,8 @@ bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag, ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest( ServerInterface* server, ServerContext* context, - ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag, - bool delete_on_finalize) - : BaseAsyncRequest(server, context, stream, call_cq, tag, - delete_on_finalize) {} + ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) + : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {} void ServerInterface::RegisteredAsyncRequest::IssueRequest( void* registered_method, grpc_byte_buffer** payload, @@ -776,45 +695,6 @@ Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse( request_->stream()->call_.PerformOps(this); } -bool Server::HealthCheckAsyncRequest::FinalizeResult(void** tag, bool* status) { - bool serialization_status = - *status && payload_ && - SerializationTraits::Deserialize(payload_, &request_).ok(); - RegisteredAsyncRequest::FinalizeResult(tag, status); - *status = serialization_status && *status; - if (*status) { - new HealthCheckAsyncRequest(service_, server_, cq_); - status_ = service_->Check(&server_context_, &request_, &response_); - new HealthCheckAsyncResponse(this); - return false; - } else { - delete this; - return false; - } -} - -Server::HealthCheckAsyncResponse::HealthCheckAsyncResponse( - HealthCheckAsyncRequest* request) - : request_(request) { - ServerContext* context = request_->server_context(); - if (!context->sent_initial_metadata_) { - SendInitialMetadata(context->initial_metadata_, - context->initial_metadata_flags()); - if (context->compression_level_set()) { - set_compression_level(context->compression_level()); - } - context->sent_initial_metadata_ = true; - } - Status* status = request_->status(); - if (status->ok()) { - ServerSendStatus(context->trailing_metadata_, - SendMessage(*request_->response())); - } else { - ServerSendStatus(context->trailing_metadata_, *status); - } - request_->call()->PerformOps(this); -} - ServerInitializer* Server::initializer() { return server_initializer_.get(); } } // namespace grpc diff --git a/test/cpp/end2end/health_service_end2end_test.cc b/test/cpp/end2end/health_service_end2end_test.cc index 8a6a9886c9..3d51007857 100644 --- a/test/cpp/end2end/health_service_end2end_test.cc +++ b/test/cpp/end2end/health_service_end2end_test.cc @@ -273,12 +273,13 @@ TEST_F(HealthServiceEnd2endTest, DefaultHealthServiceAsyncOnly) { SetUpServer(false, true, false, nullptr); cq_thread_ = std::thread(LoopCompletionQueue, cq_.get()); - VerifyHealthCheckService(); + HealthCheckServiceInterface* default_service = + server_->GetHealthCheckService(); + EXPECT_TRUE(default_service == nullptr); - // The default service has a size limit of the service name. - const grpc::string kTooLongServiceName(201, 'x'); - SendHealthCheckRpc(kTooLongServiceName, - Status(StatusCode::INVALID_ARGUMENT, "")); + ResetStubs(); + + SendHealthCheckRpc("", Status(StatusCode::UNIMPLEMENTED, "")); } // Provide an empty service to disable the default service. -- cgit v1.2.3 From 6955c5e8d2c881169dfa4ffd80a38182d6e2e14e Mon Sep 17 00:00:00 2001 From: yang-g Date: Mon, 13 Feb 2017 15:49:27 -0800 Subject: Remove code from network_status_tracker.c Pull the hash function to useful.h --- include/grpc/support/useful.h | 3 + src/core/lib/iomgr/network_status_tracker.c | 87 ++----------------------- src/core/lib/iomgr/timer_generic.c | 10 +-- src/core/lib/support/cpu_posix.c | 9 +-- test/core/end2end/tests/network_status_change.c | 5 +- 5 files changed, 16 insertions(+), 98 deletions(-) (limited to 'include') diff --git a/include/grpc/support/useful.h b/include/grpc/support/useful.h index 003e096cf9..9d8314e4be 100644 --- a/include/grpc/support/useful.h +++ b/include/grpc/support/useful.h @@ -74,4 +74,7 @@ #define GPR_ICMP(a, b) ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0)) +#define GPR_HASH_POINTER(x, range) \ + ((((size_t)x) >> 4) ^ (((size_t)x) >> 9) ^ (((size_t)x) >> 14)) % (range) + #endif /* GRPC_SUPPORT_USEFUL_H */ diff --git a/src/core/lib/iomgr/network_status_tracker.c b/src/core/lib/iomgr/network_status_tracker.c index 1601a39002..4104bf927a 100644 --- a/src/core/lib/iomgr/network_status_tracker.c +++ b/src/core/lib/iomgr/network_status_tracker.c @@ -31,95 +31,18 @@ * */ -#include -#include #include "src/core/lib/iomgr/endpoint.h" -typedef struct endpoint_ll_node { - grpc_endpoint *ep; - struct endpoint_ll_node *next; -} endpoint_ll_node; - -static endpoint_ll_node *head = NULL; -static gpr_mu g_endpoint_mutex; - -void grpc_network_status_shutdown(void) { - if (head != NULL) { - gpr_log(GPR_ERROR, - "Memory leaked as not all network endpoints were shut down"); - } - gpr_mu_destroy(&g_endpoint_mutex); -} +void grpc_network_status_shutdown(void) {} void grpc_network_status_init(void) { - gpr_mu_init(&g_endpoint_mutex); // TODO(makarandd): Install callback with OS to monitor network status. } -void grpc_destroy_network_status_monitor() { - for (endpoint_ll_node *curr = head; curr != NULL;) { - endpoint_ll_node *next = curr->next; - gpr_free(curr); - curr = next; - } - gpr_mu_destroy(&g_endpoint_mutex); -} - -void grpc_network_status_register_endpoint(grpc_endpoint *ep) { - gpr_mu_lock(&g_endpoint_mutex); - if (head == NULL) { - head = (endpoint_ll_node *)gpr_malloc(sizeof(endpoint_ll_node)); - head->ep = ep; - head->next = NULL; - } else { - endpoint_ll_node *prev_head = head; - head = (endpoint_ll_node *)gpr_malloc(sizeof(endpoint_ll_node)); - head->ep = ep; - head->next = prev_head; - } - gpr_mu_unlock(&g_endpoint_mutex); -} +void grpc_destroy_network_status_monitor() {} -void grpc_network_status_unregister_endpoint(grpc_endpoint *ep) { - gpr_mu_lock(&g_endpoint_mutex); - GPR_ASSERT(head); - bool found = false; - endpoint_ll_node *prev = head; - // if we're unregistering the head, just move head to the next - if (ep == head->ep) { - head = head->next; - gpr_free(prev); - found = true; - } else { - for (endpoint_ll_node *curr = head->next; curr != NULL; curr = curr->next) { - if (ep == curr->ep) { - prev->next = curr->next; - gpr_free(curr); - found = true; - break; - } - prev = curr; - } - } - gpr_mu_unlock(&g_endpoint_mutex); - GPR_ASSERT(found); -} +void grpc_network_status_register_endpoint(grpc_endpoint *ep) { (void)ep; } -// Walk the linked-list from head and execute shutdown. It is possible that -// other threads might be in the process of shutdown as well, but that has -// no side effect since endpoint shutdown is idempotent. -void grpc_network_status_shutdown_all_endpoints() { - gpr_mu_lock(&g_endpoint_mutex); - if (head == NULL) { - gpr_mu_unlock(&g_endpoint_mutex); - return; - } - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; +void grpc_network_status_unregister_endpoint(grpc_endpoint *ep) { (void)ep; } - for (endpoint_ll_node *curr = head; curr != NULL; curr = curr->next) { - curr->ep->vtable->shutdown(&exec_ctx, curr->ep, - GRPC_ERROR_CREATE("Network unavailable")); - } - gpr_mu_unlock(&g_endpoint_mutex); - grpc_exec_ctx_finish(&exec_ctx); -} +void grpc_network_status_shutdown_all_endpoints() {} diff --git a/src/core/lib/iomgr/timer_generic.c b/src/core/lib/iomgr/timer_generic.c index 40c8351472..8a5617e7c1 100644 --- a/src/core/lib/iomgr/timer_generic.c +++ b/src/core/lib/iomgr/timer_generic.c @@ -121,12 +121,6 @@ void grpc_timer_list_shutdown(grpc_exec_ctx *exec_ctx) { g_initialized = false; } -/* This is a cheap, but good enough, pointer hash for sharding the tasks: */ -static size_t shard_idx(const grpc_timer *info) { - size_t x = (size_t)info; - return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) & (NUM_SHARDS - 1); -} - static double ts_to_dbl(gpr_timespec ts) { return (double)ts.tv_sec + 1e-9 * ts.tv_nsec; } @@ -181,7 +175,7 @@ void grpc_timer_init(grpc_exec_ctx *exec_ctx, grpc_timer *timer, gpr_timespec deadline, grpc_closure *closure, gpr_timespec now) { int is_first_timer = 0; - shard_type *shard = &g_shards[shard_idx(timer)]; + shard_type *shard = &g_shards[GPR_HASH_POINTER(timer, NUM_SHARDS)]; GPR_ASSERT(deadline.clock_type == g_clock_type); GPR_ASSERT(now.clock_type == g_clock_type); timer->closure = closure; @@ -247,7 +241,7 @@ void grpc_timer_cancel(grpc_exec_ctx *exec_ctx, grpc_timer *timer) { return; } - shard_type *shard = &g_shards[shard_idx(timer)]; + shard_type *shard = &g_shards[GPR_HASH_POINTER(timer, NUM_SHARDS)]; gpr_mu_lock(&shard->mu); if (!timer->triggered) { grpc_closure_sched(exec_ctx, timer->closure, GRPC_ERROR_CANCELLED); diff --git a/src/core/lib/support/cpu_posix.c b/src/core/lib/support/cpu_posix.c index 667bde7cad..245f12f06d 100644 --- a/src/core/lib/support/cpu_posix.c +++ b/src/core/lib/support/cpu_posix.c @@ -41,6 +41,7 @@ #include #include +#include static __thread char magic_thread_local; @@ -60,18 +61,12 @@ unsigned gpr_cpu_num_cores(void) { return (unsigned)ncpus; } -/* This is a cheap, but good enough, pointer hash for sharding things: */ -static size_t shard_ptr(const void *info) { - size_t x = (size_t)info; - return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) % gpr_cpu_num_cores(); -} - unsigned gpr_cpu_current_cpu(void) { /* NOTE: there's no way I know to return the actual cpu index portably... most code that's using this is using it to shard across work queues though, so here we use thread identity instead to achieve a similar though not identical effect */ - return (unsigned)shard_ptr(&magic_thread_local); + return (unsigned)GPR_HASH_POINTER(&magic_thread_local, gpr_cpu_num_cores()); } #endif /* GPR_CPU_POSIX */ diff --git a/test/core/end2end/tests/network_status_change.c b/test/core/end2end/tests/network_status_change.c index 9cef02b2b3..7540ce93a1 100644 --- a/test/core/end2end/tests/network_status_change.c +++ b/test/core/end2end/tests/network_status_change.c @@ -212,8 +212,11 @@ static void test_invoke_network_status_change(grpc_end2end_test_config config) { CQ_EXPECT_COMPLETION(cqv, tag(1), 1); cq_verify(cqv); + // TODO(makdharma) Update this when the shutdown_all_endpoints is implemented. // Expected behavior of a RPC when network is lost. - GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); + // GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); + GPR_ASSERT(status == GRPC_STATUS_OK); + GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); validate_host_override_string("foo.test.google.fr:1234", call_details.host, config); -- cgit v1.2.3 From f07ed45439110033c8460295a71e4a5b4c42a0a1 Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 16 Feb 2017 23:01:28 -0800 Subject: Restore the NextMessageSize for streamed calls --- include/grpc++/impl/codegen/call.h | 15 ++++++++++++++- include/grpc++/impl/codegen/sync_stream.h | 8 ++++---- src/cpp/server/server_cc.cc | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/grpc++/impl/codegen/call.h b/include/grpc++/impl/codegen/call.h index a17cdf9236..19a5ca2b2e 100644 --- a/include/grpc++/impl/codegen/call.h +++ b/include/grpc++/impl/codegen/call.h @@ -618,7 +618,17 @@ class Call final { public: /* call is owned by the caller */ Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq) - : call_hook_(call_hook), cq_(cq), call_(call) {} + : call_hook_(call_hook), + cq_(cq), + call_(call), + max_receive_message_size_(-1) {} + + Call(grpc_call* call, CallHook* call_hook, CompletionQueue* cq, + int max_receive_message_size) + : call_hook_(call_hook), + cq_(cq), + call_(call), + max_receive_message_size_(max_receive_message_size) {} void PerformOps(CallOpSetInterface* ops) { call_hook_->PerformOpsOnCall(ops, this); @@ -627,10 +637,13 @@ class Call final { grpc_call* call() const { return call_; } CompletionQueue* cq() const { return cq_; } + int max_receive_message_size() const { return max_receive_message_size_; } + private: CallHook* call_hook_; CompletionQueue* cq_; grpc_call* call_; + int max_receive_message_size_; }; } // namespace grpc diff --git a/include/grpc++/impl/codegen/sync_stream.h b/include/grpc++/impl/codegen/sync_stream.h index 1f7708bab9..4d9b074e95 100644 --- a/include/grpc++/impl/codegen/sync_stream.h +++ b/include/grpc++/impl/codegen/sync_stream.h @@ -160,7 +160,7 @@ class ClientReader final : public ClientReaderInterface { } bool NextMessageSize(uint32_t* sz) override { - *sz = INT_MAX; + *sz = call_.max_receive_message_size(); return true; } @@ -310,7 +310,7 @@ class ClientReaderWriter final : public ClientReaderWriterInterface { } bool NextMessageSize(uint32_t* sz) override { - *sz = INT_MAX; + *sz = call_.max_receive_message_size(); return true; } @@ -382,7 +382,7 @@ class ServerReader final : public ServerReaderInterface { } bool NextMessageSize(uint32_t* sz) override { - *sz = INT_MAX; + *sz = call_->max_receive_message_size(); return true; } @@ -474,7 +474,7 @@ class ServerReaderWriterBody final { } bool NextMessageSize(uint32_t* sz) { - *sz = INT_MAX; + *sz = call_->max_receive_message_size(); return true; } diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 29898a4209..5ed9f41d4f 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -186,7 +186,7 @@ class Server::SyncRequest final : public CompletionQueueTag { public: explicit CallData(Server* server, SyncRequest* mrd) : cq_(mrd->cq_), - call_(mrd->call_, server, &cq_), + call_(mrd->call_, server, &cq_, server->max_receive_message_size()), ctx_(mrd->deadline_, mrd->request_metadata_.metadata, mrd->request_metadata_.count), has_request_payload_(mrd->has_request_payload_), @@ -590,7 +590,7 @@ bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag, } context_->set_call(call_); context_->cq_ = call_cq_; - Call call(call_, server_, call_cq_); + Call call(call_, server_, call_cq_, server_->max_receive_message_size()); if (*status && call_) { context_->BeginCompletionOp(&call); } -- cgit v1.2.3 From f3dec9c995e5c2b85460107a9ad9d937facd9a49 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 21 Feb 2017 10:02:31 -0800 Subject: Add counters for rmw atomic operations to microbenchmarks --- Makefile | 2 +- build.yaml | 2 +- include/grpc/impl/codegen/atm_gcc_atomic.h | 30 ++++++++++++------ src/core/lib/support/sync_posix.c | 9 +++--- test/cpp/microbenchmarks/bm_closure.cc | 51 ++++++++++++++++++++++++++++++ test/cpp/microbenchmarks/bm_fullstack.cc | 20 ++++++++---- 6 files changed, 92 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/Makefile b/Makefile index 8f92816672..d47d967e47 100644 --- a/Makefile +++ b/Makefile @@ -217,7 +217,7 @@ CC_counters = $(DEFAULT_CC) CXX_counters = $(DEFAULT_CXX) LD_counters = $(DEFAULT_CC) LDXX_counters = $(DEFAULT_CXX) -CPPFLAGS_counters = -O2 -DGPR_MU_COUNTERS +CPPFLAGS_counters = -O2 -DGPR_LOW_LEVEL_COUNTERS DEFINES_counters = NDEBUG diff --git a/build.yaml b/build.yaml index 141526cb59..120c7a85b1 100644 --- a/build.yaml +++ b/build.yaml @@ -3919,7 +3919,7 @@ configs: CPPFLAGS: -O2 -DGRPC_BASIC_PROFILER -DGRPC_TIMERS_RDTSC DEFINES: NDEBUG counters: - CPPFLAGS: -O2 -DGPR_MU_COUNTERS + CPPFLAGS: -O2 -DGPR_LOW_LEVEL_COUNTERS DEFINES: NDEBUG dbg: CPPFLAGS: -O0 diff --git a/include/grpc/impl/codegen/atm_gcc_atomic.h b/include/grpc/impl/codegen/atm_gcc_atomic.h index 7d4ae98cf7..e5a623f723 100644 --- a/include/grpc/impl/codegen/atm_gcc_atomic.h +++ b/include/grpc/impl/codegen/atm_gcc_atomic.h @@ -40,6 +40,15 @@ typedef intptr_t gpr_atm; +#ifdef GPR_LOW_LEVEL_COUNTERS +extern gpr_atm gpr_counter_rmw; +#define GPR_ATM_INC_COUNTER(counter) \ + __atomic_fetch_add(&counter, 1, __ATOMIC_RELAXED) +#define GPR_ATM_INC_RMW_THEN(blah) (GPR_ATM_INC_COUNTER(gpr_counter_rmw), blah) +#else +#define GPR_ATM_INC_RMW_THEN(blah) blah +#endif + #define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST)) #define gpr_atm_acq_load(p) (__atomic_load_n((p), __ATOMIC_ACQUIRE)) @@ -50,25 +59,28 @@ typedef intptr_t gpr_atm; (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED)) #define gpr_atm_no_barrier_fetch_add(p, delta) \ - (__atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED)) + GPR_ATM_INC_RMW_THEN( \ + __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED)) #define gpr_atm_full_fetch_add(p, delta) \ - (__atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL)) + GPR_ATM_INC_RMW_THEN( \ + __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL)) static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_RELAXED, - __ATOMIC_RELAXED); + return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + p, &o, n, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)); } static __inline int gpr_atm_acq_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED); + return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + p, &o, n, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)); } static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_RELEASE, - __ATOMIC_RELAXED); + return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + p, &o, n, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED)); } -#define gpr_atm_full_xchg(p, n) __atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL) +#define gpr_atm_full_xchg(p, n) \ + GPR_ATM_INC_RMW_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL)) #endif /* GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H */ diff --git a/src/core/lib/support/sync_posix.c b/src/core/lib/support/sync_posix.c index de0f0484b5..3b7d780608 100644 --- a/src/core/lib/support/sync_posix.c +++ b/src/core/lib/support/sync_posix.c @@ -42,8 +42,9 @@ #include #include "src/core/lib/profiling/timers.h" -#ifdef GPR_MU_COUNTERS -gpr_atm grpc_mu_locks = 0; +#ifdef GPR_LOW_LEVEL_COUNTERS +gpr_atm gpr_mu_locks = 0; +gpr_atm gpr_counter_rmw = 0; #endif void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); } @@ -51,8 +52,8 @@ void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); } void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); } void gpr_mu_lock(gpr_mu* mu) { -#ifdef GPR_MU_COUNTERS - gpr_atm_no_barrier_fetch_add(&grpc_mu_locks, 1); +#ifdef GPR_LOW_LEVEL_COUNTERS + GPR_ATM_INC_COUNTER(gpr_mu_locks); #endif GPR_TIMER_BEGIN("gpr_mu_lock", 0); GPR_ASSERT(pthread_mutex_lock(mu) == 0); diff --git a/test/cpp/microbenchmarks/bm_closure.cc b/test/cpp/microbenchmarks/bm_closure.cc index 80d6610e13..16d05781bb 100644 --- a/test/cpp/microbenchmarks/bm_closure.cc +++ b/test/cpp/microbenchmarks/bm_closure.cc @@ -43,13 +43,46 @@ extern "C" { #include "third_party/benchmark/include/benchmark/benchmark.h" +#include + +#ifdef GPR_LOW_LEVEL_COUNTERS +extern "C" gpr_atm gpr_mu_locks; +#endif + static class InitializeStuff { public: InitializeStuff() { grpc_init(); } ~InitializeStuff() { grpc_shutdown(); } } initialize_stuff; +class TrackCounters { + public: + TrackCounters(benchmark::State& state) : state_(state) {} + + ~TrackCounters() { + std::ostringstream out; +#ifdef GPR_LOW_LEVEL_COUNTERS + out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) - + mu_locks_at_start_) / + (double)state_.iterations()) + << " atm_rmw/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_rmw) - + rmw_at_start_) / + (double)state_.iterations()); +#endif + state_.SetLabel(out.str()); + } + + private: + benchmark::State& state_; +#ifdef GPR_LOW_LEVEL_COUNTERS + const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); + const size_t rmw_at_start_ = gpr_atm_no_barrier_load(&gpr_counter_rmw); +#endif +}; + static void BM_NoOpExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); while (state.KeepRunning()) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_exec_ctx_finish(&exec_ctx); @@ -58,6 +91,7 @@ static void BM_NoOpExecCtx(benchmark::State& state) { BENCHMARK(BM_NoOpExecCtx); static void BM_WellFlushed(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; while (state.KeepRunning()) { grpc_exec_ctx_flush(&exec_ctx); @@ -69,6 +103,7 @@ BENCHMARK(BM_WellFlushed); static void DoNothing(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {} static void BM_ClosureInitAgainstExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_closure c; while (state.KeepRunning()) { benchmark::DoNotOptimize( @@ -78,6 +113,7 @@ static void BM_ClosureInitAgainstExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureInitAgainstExecCtx); static void BM_ClosureInitAgainstCombiner(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner = grpc_combiner_create(NULL); grpc_closure c; grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; @@ -91,6 +127,7 @@ static void BM_ClosureInitAgainstCombiner(benchmark::State& state) { BENCHMARK(BM_ClosureInitAgainstCombiner); static void BM_ClosureRunOnExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_closure c; grpc_closure_init(&c, DoNothing, NULL, grpc_schedule_on_exec_ctx); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; @@ -103,6 +140,7 @@ static void BM_ClosureRunOnExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureRunOnExecCtx); static void BM_ClosureCreateAndRun(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; while (state.KeepRunning()) { grpc_closure_run(&exec_ctx, grpc_closure_create(DoNothing, NULL, @@ -114,6 +152,7 @@ static void BM_ClosureCreateAndRun(benchmark::State& state) { BENCHMARK(BM_ClosureCreateAndRun); static void BM_ClosureInitAndRun(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_closure c; while (state.KeepRunning()) { @@ -126,6 +165,7 @@ static void BM_ClosureInitAndRun(benchmark::State& state) { BENCHMARK(BM_ClosureInitAndRun); static void BM_ClosureSchedOnExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_closure c; grpc_closure_init(&c, DoNothing, NULL, grpc_schedule_on_exec_ctx); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; @@ -138,6 +178,7 @@ static void BM_ClosureSchedOnExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureSchedOnExecCtx); static void BM_ClosureSched2OnExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_closure c1; grpc_closure c2; grpc_closure_init(&c1, DoNothing, NULL, grpc_schedule_on_exec_ctx); @@ -153,6 +194,7 @@ static void BM_ClosureSched2OnExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureSched2OnExecCtx); static void BM_ClosureSched3OnExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_closure c1; grpc_closure c2; grpc_closure c3; @@ -171,6 +213,7 @@ static void BM_ClosureSched3OnExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureSched3OnExecCtx); static void BM_AcquireMutex(benchmark::State& state) { + TrackCounters track_counters(state); // for comparison with the combiner stuff below gpr_mu mu; gpr_mu_init(&mu); @@ -185,6 +228,7 @@ static void BM_AcquireMutex(benchmark::State& state) { BENCHMARK(BM_AcquireMutex); static void BM_ClosureSchedOnCombiner(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner = grpc_combiner_create(NULL); grpc_closure c; grpc_closure_init(&c, DoNothing, NULL, @@ -200,6 +244,7 @@ static void BM_ClosureSchedOnCombiner(benchmark::State& state) { BENCHMARK(BM_ClosureSchedOnCombiner); static void BM_ClosureSched2OnCombiner(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner = grpc_combiner_create(NULL); grpc_closure c1; grpc_closure c2; @@ -219,6 +264,7 @@ static void BM_ClosureSched2OnCombiner(benchmark::State& state) { BENCHMARK(BM_ClosureSched2OnCombiner); static void BM_ClosureSched3OnCombiner(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner = grpc_combiner_create(NULL); grpc_closure c1; grpc_closure c2; @@ -242,6 +288,7 @@ static void BM_ClosureSched3OnCombiner(benchmark::State& state) { BENCHMARK(BM_ClosureSched3OnCombiner); static void BM_ClosureSched2OnTwoCombiners(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner1 = grpc_combiner_create(NULL); grpc_combiner* combiner2 = grpc_combiner_create(NULL); grpc_closure c1; @@ -263,6 +310,7 @@ static void BM_ClosureSched2OnTwoCombiners(benchmark::State& state) { BENCHMARK(BM_ClosureSched2OnTwoCombiners); static void BM_ClosureSched4OnTwoCombiners(benchmark::State& state) { + TrackCounters track_counters(state); grpc_combiner* combiner1 = grpc_combiner_create(NULL); grpc_combiner* combiner2 = grpc_combiner_create(NULL); grpc_closure c1; @@ -323,6 +371,7 @@ class Rescheduler { }; static void BM_ClosureReschedOnExecCtx(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; Rescheduler r(state, grpc_schedule_on_exec_ctx); r.ScheduleFirst(&exec_ctx); @@ -331,6 +380,7 @@ static void BM_ClosureReschedOnExecCtx(benchmark::State& state) { BENCHMARK(BM_ClosureReschedOnExecCtx); static void BM_ClosureReschedOnCombiner(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_combiner* combiner = grpc_combiner_create(NULL); Rescheduler r(state, grpc_combiner_scheduler(combiner, false)); @@ -342,6 +392,7 @@ static void BM_ClosureReschedOnCombiner(benchmark::State& state) { BENCHMARK(BM_ClosureReschedOnCombiner); static void BM_ClosureReschedOnCombinerFinally(benchmark::State& state) { + TrackCounters track_counters(state); grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_combiner* combiner = grpc_combiner_create(NULL); Rescheduler r(state, grpc_combiner_finally_scheduler(combiner, false)); diff --git a/test/cpp/microbenchmarks/bm_fullstack.cc b/test/cpp/microbenchmarks/bm_fullstack.cc index c63de0ce0a..5bb456ab46 100644 --- a/test/cpp/microbenchmarks/bm_fullstack.cc +++ b/test/cpp/microbenchmarks/bm_fullstack.cc @@ -99,8 +99,9 @@ static void ApplyCommonChannelArguments(ChannelArguments* c) { c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX); } -#ifdef GPR_MU_COUNTERS -extern "C" gpr_atm grpc_mu_locks; +#ifdef GPR_LOW_LEVEL_COUNTERS +extern "C" gpr_atm gpr_mu_locks; +extern "C" gpr_atm gpr_counter_rmw; #endif class BaseFixture { @@ -108,10 +109,14 @@ class BaseFixture { void Finish(benchmark::State& s) { std::ostringstream out; this->AddToLabel(out, s); -#ifdef GPR_MU_COUNTERS - out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&grpc_mu_locks) - +#ifdef GPR_LOW_LEVEL_COUNTERS + out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) - mu_locks_at_start_) / - (double)s.iterations()); + (double)s.iterations()) + << " atm_rmw/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_rmw) - + rmw_at_start_) / + (double)s.iterations()); #endif grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot(); out << " allocs/iter:" @@ -128,8 +133,9 @@ class BaseFixture { virtual void AddToLabel(std::ostream& out, benchmark::State& s) = 0; private: -#ifdef GPR_MU_COUNTERS - const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&grpc_mu_locks); +#ifdef GPR_LOW_LEVEL_COUNTERS + const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); + const size_t rmw_at_start_ = gpr_atm_no_barrier_load(&gpr_counter_rmw); #endif grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot(); }; -- cgit v1.2.3 From 7f4d30a0321a042d4f8512815c1029cbaad6fac8 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 21 Feb 2017 10:24:00 -0800 Subject: Separate CAS/ADD RMWs --- include/grpc/impl/codegen/atm_gcc_atomic.h | 23 ++++++++++++++--------- src/core/lib/support/sync_posix.c | 3 ++- test/cpp/microbenchmarks/bm_closure.cc | 15 +++++++++++---- test/cpp/microbenchmarks/bm_fullstack.cc | 18 +++++++++++++----- tools/profiling/microbenchmarks/bm2bq.py | 3 ++- 5 files changed, 42 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/grpc/impl/codegen/atm_gcc_atomic.h b/include/grpc/impl/codegen/atm_gcc_atomic.h index e5a623f723..4bd3b25741 100644 --- a/include/grpc/impl/codegen/atm_gcc_atomic.h +++ b/include/grpc/impl/codegen/atm_gcc_atomic.h @@ -41,12 +41,17 @@ typedef intptr_t gpr_atm; #ifdef GPR_LOW_LEVEL_COUNTERS -extern gpr_atm gpr_counter_rmw; +extern gpr_atm gpr_counter_atm_cas; +extern gpr_atm gpr_counter_atm_add; #define GPR_ATM_INC_COUNTER(counter) \ __atomic_fetch_add(&counter, 1, __ATOMIC_RELAXED) -#define GPR_ATM_INC_RMW_THEN(blah) (GPR_ATM_INC_COUNTER(gpr_counter_rmw), blah) +#define GPR_ATM_INC_CAS_THEN(blah) \ + (GPR_ATM_INC_COUNTER(gpr_counter_atm_cas), blah) +#define GPR_ATM_INC_ADD_THEN(blah) \ + (GPR_ATM_INC_COUNTER(gpr_counter_atm_add), blah) #else -#define GPR_ATM_INC_RMW_THEN(blah) blah +#define GPR_ATM_INC_CAS_THEN(blah) blah +#define GPR_ATM_INC_ADD_THEN(blah) blah #endif #define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST)) @@ -59,28 +64,28 @@ extern gpr_atm gpr_counter_rmw; (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED)) #define gpr_atm_no_barrier_fetch_add(p, delta) \ - GPR_ATM_INC_RMW_THEN( \ + GPR_ATM_INC_ADD_THEN( \ __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED)) #define gpr_atm_full_fetch_add(p, delta) \ - GPR_ATM_INC_RMW_THEN( \ + GPR_ATM_INC_ADD_THEN( \ __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL)) static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n( p, &o, n, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)); } static __inline int gpr_atm_acq_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n( p, &o, n, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)); } static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) { - return GPR_ATM_INC_RMW_THEN(__atomic_compare_exchange_n( + return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n( p, &o, n, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED)); } #define gpr_atm_full_xchg(p, n) \ - GPR_ATM_INC_RMW_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL)) + GPR_ATM_INC_CAS_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL)) #endif /* GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H */ diff --git a/src/core/lib/support/sync_posix.c b/src/core/lib/support/sync_posix.c index 3b7d780608..16e7d6e12a 100644 --- a/src/core/lib/support/sync_posix.c +++ b/src/core/lib/support/sync_posix.c @@ -44,7 +44,8 @@ #ifdef GPR_LOW_LEVEL_COUNTERS gpr_atm gpr_mu_locks = 0; -gpr_atm gpr_counter_rmw = 0; +gpr_atm gpr_counter_atm_cas = 0; +gpr_atm gpr_counter_atm_add = 0; #endif void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); } diff --git a/test/cpp/microbenchmarks/bm_closure.cc b/test/cpp/microbenchmarks/bm_closure.cc index 16d05781bb..03aede35b2 100644 --- a/test/cpp/microbenchmarks/bm_closure.cc +++ b/test/cpp/microbenchmarks/bm_closure.cc @@ -65,9 +65,13 @@ class TrackCounters { out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) - mu_locks_at_start_) / (double)state_.iterations()) - << " atm_rmw/iter:" - << ((double)(gpr_atm_no_barrier_load(&gpr_counter_rmw) - - rmw_at_start_) / + << " atm_cas/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) - + atm_cas_at_start_) / + (double)state_.iterations()) + << " atm_add/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) - + atm_add_at_start_) / (double)state_.iterations()); #endif state_.SetLabel(out.str()); @@ -77,7 +81,10 @@ class TrackCounters { benchmark::State& state_; #ifdef GPR_LOW_LEVEL_COUNTERS const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); - const size_t rmw_at_start_ = gpr_atm_no_barrier_load(&gpr_counter_rmw); + const size_t atm_cas_at_start_ = + gpr_atm_no_barrier_load(&gpr_counter_atm_cas); + const size_t atm_add_at_start_ = + gpr_atm_no_barrier_load(&gpr_counter_atm_add); #endif }; diff --git a/test/cpp/microbenchmarks/bm_fullstack.cc b/test/cpp/microbenchmarks/bm_fullstack.cc index 5bb456ab46..48e131f1be 100644 --- a/test/cpp/microbenchmarks/bm_fullstack.cc +++ b/test/cpp/microbenchmarks/bm_fullstack.cc @@ -101,7 +101,8 @@ static void ApplyCommonChannelArguments(ChannelArguments* c) { #ifdef GPR_LOW_LEVEL_COUNTERS extern "C" gpr_atm gpr_mu_locks; -extern "C" gpr_atm gpr_counter_rmw; +extern "C" gpr_atm gpr_counter_atm_cas; +extern "C" gpr_atm gpr_counter_atm_add; #endif class BaseFixture { @@ -113,9 +114,13 @@ class BaseFixture { out << " locks/iter:" << ((double)(gpr_atm_no_barrier_load(&gpr_mu_locks) - mu_locks_at_start_) / (double)s.iterations()) - << " atm_rmw/iter:" - << ((double)(gpr_atm_no_barrier_load(&gpr_counter_rmw) - - rmw_at_start_) / + << " atm_cas/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_cas) - + atm_cas_at_start_) / + (double)s.iterations()) + << " atm_add/iter:" + << ((double)(gpr_atm_no_barrier_load(&gpr_counter_atm_add) - + atm_add_at_start_) / (double)s.iterations()); #endif grpc_memory_counters counters_at_end = grpc_memory_counters_snapshot(); @@ -135,7 +140,10 @@ class BaseFixture { private: #ifdef GPR_LOW_LEVEL_COUNTERS const size_t mu_locks_at_start_ = gpr_atm_no_barrier_load(&gpr_mu_locks); - const size_t rmw_at_start_ = gpr_atm_no_barrier_load(&gpr_counter_rmw); + const size_t atm_cas_at_start_ = + gpr_atm_no_barrier_load(&gpr_counter_atm_cas); + const size_t atm_add_at_start_ = + gpr_atm_no_barrier_load(&gpr_counter_atm_add); #endif grpc_memory_counters counters_at_start_ = grpc_memory_counters_snapshot(); }; diff --git a/tools/profiling/microbenchmarks/bm2bq.py b/tools/profiling/microbenchmarks/bm2bq.py index 62a2f699c7..8ead4b4455 100755 --- a/tools/profiling/microbenchmarks/bm2bq.py +++ b/tools/profiling/microbenchmarks/bm2bq.py @@ -66,7 +66,8 @@ columns = [ ('cli_stream_stalls_per_iteration', 'float'), ('svr_transport_stalls_per_iteration', 'float'), ('svr_stream_stalls_per_iteration', 'float'), - ('atm_rmw_per_iteration', 'float') + ('atm_cas_per_iteration', 'float') + ('atm_add_per_iteration', 'float') ] if sys.argv[1] == '--schema': -- cgit v1.2.3