xds_interop_server.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2020 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <sstream>
20 
21 #include "absl/flags/flag.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/synchronization/mutex.h"
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
31 #include <grpcpp/server.h>
32 #include <grpcpp/server_builder.h>
33 #include <grpcpp/server_context.h>
35 
39 #include "src/proto/grpc/testing/empty.pb.h"
40 #include "src/proto/grpc/testing/messages.pb.h"
41 #include "src/proto/grpc/testing/test.grpc.pb.h"
45 
46 ABSL_FLAG(int32_t, port, 8080, "Server port for service.");
47 ABSL_FLAG(int32_t, maintenance_port, 8081,
48  "Server port for maintenance if --security is \"secure\".");
49 ABSL_FLAG(std::string, server_id, "cpp_server",
50  "Server ID to include in responses.");
51 ABSL_FLAG(bool, secure_mode, false,
52  "If true, XdsServerCredentials are used, InsecureServerCredentials "
53  "otherwise");
54 
55 using grpc::Server;
58 using grpc::Status;
60 using grpc::testing::Empty;
64 using grpc::testing::TestService;
65 using grpc::testing::XdsUpdateHealthService;
66 
67 class TestServiceImpl : public TestService::Service {
68  public:
69  explicit TestServiceImpl(const std::string& hostname) : hostname_(hostname) {}
70 
72  SimpleResponse* response) override {
73  response->set_server_id(absl::GetFlag(FLAGS_server_id));
74  response->set_hostname(hostname_);
75  context->AddInitialMetadata("hostname", hostname_);
76  return Status::OK;
77  }
78 
79  Status EmptyCall(ServerContext* context, const Empty* /*request*/,
80  Empty* /*response*/) override {
81  context->AddInitialMetadata("hostname", hostname_);
82  return Status::OK;
83  }
84 
85  private:
87 };
88 
89 class XdsUpdateHealthServiceImpl : public XdsUpdateHealthService::Service {
90  public:
94 
95  Status SetServing(ServerContext* /* context */, const Empty* /* request */,
96  Empty* /* response */) override {
98  grpc::health::v1::HealthCheckResponse::SERVING);
99  return Status::OK;
100  }
101 
102  Status SetNotServing(ServerContext* /* context */, const Empty* /* request */,
103  Empty* /* response */) override {
105  grpc::health::v1::HealthCheckResponse::NOT_SERVING);
106  return Status::OK;
107  }
108 
109  private:
111 };
112 
113 void RunServer(bool secure_mode, const int port, const int maintenance_port,
114  const std::string& hostname) {
115  std::unique_ptr<Server> xds_enabled_server;
116  std::unique_ptr<Server> server;
117  TestServiceImpl service(hostname);
119  health_check_service.SetStatus(
120  "", grpc::health::v1::HealthCheckResponse::SERVING);
121  health_check_service.SetStatus(
122  "grpc.testing.TestService",
123  grpc::health::v1::HealthCheckResponse::SERVING);
124  health_check_service.SetStatus(
125  "grpc.testing.XdsUpdateHealthService",
126  grpc::health::v1::HealthCheckResponse::SERVING);
127  XdsUpdateHealthServiceImpl update_health_service(&health_check_service);
128 
131  if (secure_mode) {
132  grpc::XdsServerBuilder xds_builder;
133  xds_builder.RegisterService(&service);
134  xds_builder.AddListeningPort(
135  absl::StrCat("0.0.0.0:", port),
137  xds_enabled_server = xds_builder.BuildAndStart();
138  gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
139  builder.RegisterService(&health_check_service);
140  builder.RegisterService(&update_health_service);
142  builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
144  server = builder.BuildAndStart();
145  gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
146  maintenance_port);
147  } else {
148  builder.RegisterService(&service);
149  builder.RegisterService(&health_check_service);
150  builder.RegisterService(&update_health_service);
152  builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
154  server = builder.BuildAndStart();
155  gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port);
156  }
157 
158  server->Wait();
159 }
160 
161 int main(int argc, char** argv) {
162  grpc::testing::TestEnvironment env(&argc, argv);
163  grpc::testing::InitTest(&argc, &argv, true);
164  char* hostname = grpc_gethostname();
165  if (hostname == nullptr) {
166  std::cout << "Failed to get hostname, terminating" << std::endl;
167  return 1;
168  }
169  int port = absl::GetFlag(FLAGS_port);
170  if (port == 0) {
171  std::cout << "Invalid port, terminating" << std::endl;
172  return 1;
173  }
174  int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
175  if (maintenance_port == 0) {
176  std::cout << "Invalid maintenance port, terminating" << std::endl;
177  return 1;
178  }
180  RunServer(absl::GetFlag(FLAGS_secure_mode), port, maintenance_port, hostname);
181 
182  return 0;
183 }
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
xds_server_builder.h
messages_pb2.SimpleRequest
SimpleRequest
Definition: messages_pb2.py:597
XdsUpdateHealthServiceImpl::SetNotServing
Status SetNotServing(ServerContext *, const Empty *, Empty *) override
Definition: xds_interop_server.cc:102
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
TestServiceImpl::EmptyCall
Status EmptyCall(ServerContext *context, const Empty *, Empty *) override
Definition: xds_interop_server.cc:79
TestServiceImpl::TestServiceImpl
TestServiceImpl(const std::string &hostname)
Definition: xds_interop_server.cc:69
XdsUpdateHealthServiceImpl
Definition: xds_interop_server.cc:89
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
log.h
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Definition: server_builder.cc:275
grpc::EnableDefaultHealthCheckService
void EnableDefaultHealthCheckService(bool enable)
Definition: health_check_service.cc:30
proto_server_reflection_plugin.h
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
XdsUpdateHealthServiceImpl::XdsUpdateHealthServiceImpl
XdsUpdateHealthServiceImpl(HealthCheckServiceImpl *health_check_service)
Definition: xds_interop_server.cc:91
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
health_check_service_interface.h
OK
@ OK
Definition: cronet_status.h:43
grpc::XdsServerCredentials
std::shared_ptr< ServerCredentials > XdsServerCredentials(const std::shared_ptr< ServerCredentials > &fallback_credentials)
Builds Xds ServerCredentials given fallback credentials.
Definition: xds_server_credentials.cc:30
time.h
TestServiceImpl::UnaryCall
Status UnaryCall(ServerContext *context, const SimpleRequest *, SimpleResponse *response) override
Definition: xds_interop_server.cc:71
grpc::ServerBuilder::RegisterService
ServerBuilder & RegisterService(grpc::Service *service)
Definition: server_builder.cc:101
grpc::XdsServerBuilder
Definition: xds_server_builder.h:47
gethostname.h
health_check_service
bool health_check_service
Definition: async_end2end_test.cc:236
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
grpc::ServerBuilder::AddListeningPort
ServerBuilder & AddListeningPort(const std::string &addr_uri, std::shared_ptr< grpc::ServerCredentials > creds, int *selected_port=nullptr)
Definition: server_builder.cc:222
XdsUpdateHealthServiceImpl::SetServing
Status SetServing(ServerContext *, const Empty *, Empty *) override
Definition: xds_interop_server.cc:95
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
grpc_gethostname
char * grpc_gethostname()
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc::testing::HealthCheckServiceImpl::SetAll
void SetAll(health::v1::HealthCheckResponse::ServingStatus status)
Definition: test_health_check_service_impl.cc:76
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
admin_services.h
Empty
Definition: abseil-cpp/absl/container/internal/compressed_tuple_test.cc:33
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
server
Definition: examples/python/async_streaming/server.py:1
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
TestServiceImpl::hostname_
std::string hostname_
Definition: xds_interop_server.cc:86
grpc::AddAdminServices
void AddAdminServices(grpc::ServerBuilder *builder)
Definition: admin_services.cc:41
test_config.h
transport.h
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::testing::HealthCheckServiceImpl
Definition: test_health_check_service_impl.h:34
server.h
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
messages_pb2.SimpleResponse
SimpleResponse
Definition: messages_pb2.py:604
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
TestServiceImpl
Definition: interop_server.cc:139
main
int main(int argc, char **argv)
Definition: xds_interop_server.cc:161
grpc::reflection::InitProtoReflectionServerBuilderPlugin
void InitProtoReflectionServerBuilderPlugin()
Definition: proto_server_reflection_plugin.cc:66
test_health_check_service_impl.h
server_builder.h
XdsUpdateHealthServiceImpl::health_check_service_
HealthCheckServiceImpl *const health_check_service_
Definition: xds_interop_server.cc:110
ABSL_FLAG
ABSL_FLAG(int32_t, port, 8080, "Server port for service.")
RunServer
void RunServer(bool secure_mode, const int port, const int maintenance_port, const std::string &hostname)
Definition: xds_interop_server.cc:113


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:59