From f26c1076518be47fb8d0d910a144956a8f68f5a0 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Thu, 9 Sep 2021 11:11:57 -0700 Subject: [PATCH] xDS server serving status: Use a struct to allow more fields to be added in the future (#27242) --- include/grpc/grpc.h | 10 ++++++++-- include/grpcpp/xds_server_builder.h | 13 +++++++++---- src/core/ext/xds/xds_server_config_fetcher.cc | 8 ++++---- test/cpp/end2end/xds_end2end_test.cc | 5 +++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index 3c7292c248a..ee30ae1abc6 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -410,12 +410,18 @@ GRPCAPI void grpc_server_register_completion_queue(grpc_server* server, grpc_completion_queue* cq, void* reserved); +// More members might be added in later, so users should take care to memset +// this to 0 before using it. +typedef struct { + grpc_status_code code; + const char* error_message; +} grpc_serving_status_update; + // There might be more methods added later, so users should take care to memset // this to 0 before using it. typedef struct { void (*on_serving_status_update)(void* user_data, const char* uri, - grpc_status_code code, - const char* error_message); + grpc_serving_status_update update); void* user_data; } grpc_server_xds_status_notifier; diff --git a/include/grpcpp/xds_server_builder.h b/include/grpcpp/xds_server_builder.h index 076c377f781..c25ad1afe85 100644 --- a/include/grpcpp/xds_server_builder.h +++ b/include/grpcpp/xds_server_builder.h @@ -28,6 +28,10 @@ namespace experimental { class XdsServerServingStatusNotifierInterface { public: + struct ServingStatusUpdate { + ::grpc::Status status; + }; + virtual ~XdsServerServingStatusNotifierInterface() = default; // \a uri contains the listening target associated with the notification. Note @@ -37,7 +41,8 @@ class XdsServerServingStatusNotifierInterface { // The API does not provide any guarantees around duplicate updates. // Status::OK signifies that the server is serving, while a non-OK status // signifies that the server is not serving. - virtual void OnServingStatusUpdate(std::string uri, grpc::Status status) = 0; + virtual void OnServingStatusUpdate(std::string uri, + ServingStatusUpdate update) = 0; }; class XdsServerBuilder : public ::grpc::ServerBuilder { @@ -62,13 +67,13 @@ class XdsServerBuilder : public ::grpc::ServerBuilder { } static void OnServingStatusUpdate(void* user_data, const char* uri, - grpc_status_code code, - const char* error_message) { + grpc_serving_status_update update) { if (user_data == nullptr) return; XdsServerServingStatusNotifierInterface* notifier = static_cast(user_data); notifier->OnServingStatusUpdate( - uri, grpc::Status(static_cast(code), error_message)); + uri, {grpc::Status(static_cast(update.code), + update.error_message)}); } XdsServerServingStatusNotifierInterface* notifier_ = nullptr; diff --git a/src/core/ext/xds/xds_server_config_fetcher.cc b/src/core/ext/xds/xds_server_config_fetcher.cc index 962731f9eeb..e9850f90b81 100644 --- a/src/core/ext/xds/xds_server_config_fetcher.cc +++ b/src/core/ext/xds/xds_server_config_fetcher.cc @@ -428,7 +428,7 @@ class XdsServerConfigFetcher : public grpc_server_config_fetcher { if (serving_status_notifier_.on_serving_status_update != nullptr) { serving_status_notifier_.on_serving_status_update( serving_status_notifier_.user_data, listening_address_.c_str(), - GRPC_STATUS_OK, ""); + {GRPC_STATUS_OK, ""}); } else { gpr_log(GPR_INFO, "xDS Listener resource obtained; will start serving on %s", @@ -459,7 +459,7 @@ class XdsServerConfigFetcher : public grpc_server_config_fetcher { if (serving_status_notifier_.on_serving_status_update != nullptr) { serving_status_notifier_.on_serving_status_update( serving_status_notifier_.user_data, listening_address_.c_str(), - GRPC_STATUS_UNAVAILABLE, grpc_error_std_string(error).c_str()); + {GRPC_STATUS_UNAVAILABLE, grpc_error_std_string(error).c_str()}); } else { gpr_log( GPR_ERROR, @@ -486,8 +486,8 @@ class XdsServerConfigFetcher : public grpc_server_config_fetcher { if (serving_status_notifier_.on_serving_status_update != nullptr) { serving_status_notifier_.on_serving_status_update( serving_status_notifier_.user_data, listening_address_.c_str(), - static_cast(status.raw_code()), - std::string(status.message()).c_str()); + {static_cast(status.raw_code()), + std::string(status.message()).c_str()}); } } diff --git a/test/cpp/end2end/xds_end2end_test.cc b/test/cpp/end2end/xds_end2end_test.cc index 7433e156e08..6fb6cda926d 100644 --- a/test/cpp/end2end/xds_end2end_test.cc +++ b/test/cpp/end2end/xds_end2end_test.cc @@ -2491,9 +2491,10 @@ class XdsEnd2endTest : public ::testing::TestWithParam { class XdsServingStatusNotifier : public grpc::experimental::XdsServerServingStatusNotifierInterface { public: - void OnServingStatusUpdate(std::string uri, grpc::Status status) override { + void OnServingStatusUpdate(std::string uri, + ServingStatusUpdate update) override { grpc_core::MutexLock lock(&mu_); - status_map[uri] = status; + status_map[uri] = update.status; cond_.Signal(); }