• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2022 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 <grpcpp/grpcpp.h>
20 #include <grpcpp/security/server_credentials.h>
21 #include <grpcpp/support/server_callback.h>
22 #include <grpcpp/support/status.h>
23 #include <grpcpp/xds_server_builder.h>
24 #include <signal.h>
25 #include <unistd.h>
26 
27 #include <memory>
28 #include <string>
29 
30 #include "absl/flags/flag.h"
31 #include "absl/flags/parse.h"
32 #include "absl/log/check.h"
33 #include "absl/log/log.h"
34 #include "src/cpp/ext/chaotic_good.h"
35 #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h"
36 #include "src/proto/grpc/testing/messages.pb.h"
37 #include "test/core/memory_usage/memstats.h"
38 #include "test/core/test_util/test_config.h"
39 
40 ABSL_FLAG(std::string, bind, "", "Bind host:port");
41 ABSL_FLAG(bool, secure, false, "Use SSL Credentials");
42 ABSL_FLAG(bool, use_xds, false, "Use xDS");
43 ABSL_FLAG(bool, chaotic_good, false, "Use chaotic good");
44 
45 class ServerCallbackImpl final
46     : public grpc::testing::BenchmarkService::CallbackService {
47  public:
ServerCallbackImpl(long before_server_memory)48   explicit ServerCallbackImpl(long before_server_memory)
49       : before_server_create(before_server_memory) {}
50 
UnaryCall(grpc::CallbackServerContext * context,const grpc::testing::SimpleRequest *,grpc::testing::SimpleResponse *)51   grpc::ServerUnaryReactor* UnaryCall(
52       grpc::CallbackServerContext* context,
53       const grpc::testing::SimpleRequest* /* request */,
54       grpc::testing::SimpleResponse* /* response */) override {
55     auto* reactor = context->DefaultReactor();
56     reactor->Finish(grpc::Status::OK);
57     return reactor;
58   }
GetBeforeSnapshot(grpc::CallbackServerContext * context,const grpc::testing::SimpleRequest *,grpc::testing::MemorySize * response)59   grpc::ServerUnaryReactor* GetBeforeSnapshot(
60       grpc::CallbackServerContext* context,
61       const grpc::testing::SimpleRequest* /* request */,
62       grpc::testing::MemorySize* response) override {
63     LOG(INFO) << "BeforeSnapshot RPC CALL RECEIVED";
64     response->set_rss(before_server_create);
65     auto* reactor = context->DefaultReactor();
66     reactor->Finish(grpc::Status::OK);
67     return reactor;
68   }
69 
70  private:
71   long before_server_create;
72 };
73 
74 // We have some sort of deadlock, so let's not exit gracefully for now.
75 // TODO(chennancy) Add graceful shutdown
sigint_handler(int)76 static void sigint_handler(int /*x*/) { _exit(0); }
77 
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79   absl::ParseCommandLine(argc, argv);
80   char* fake_argv[1];
81   CHECK_GE(argc, 1);
82   fake_argv[0] = argv[0];
83   grpc::testing::TestEnvironment env(&argc, argv);
84   grpc_init();
85   signal(SIGINT, sigint_handler);
86   std::string server_address = absl::GetFlag(FLAGS_bind);
87   if (server_address.empty()) {
88     LOG(ERROR) << "Server: No port entered";
89     return 1;
90   }
91   LOG(INFO) << "Server port: " << server_address;
92 
93   // Get initial process memory usage before creating server
94   long before_server_create = GetMemUsage();
95   ServerCallbackImpl callback_server(before_server_create);
96 
97   grpc::XdsServerBuilder xds_builder;
98   grpc::ServerBuilder normal_builder;
99   grpc::ServerBuilder* builder =
100       absl::GetFlag(FLAGS_use_xds) ? &xds_builder : &normal_builder;
101 
102   // Set the authentication mechanism.
103   std::shared_ptr<grpc::ServerCredentials> creds =
104       grpc::InsecureServerCredentials();
105   if (absl::GetFlag(FLAGS_chaotic_good)) {
106     creds = grpc::ChaoticGoodInsecureServerCredentials();
107   } else if (absl::GetFlag(FLAGS_secure)) {
108     LOG(INFO) << "Supposed to be secure, is not yet";
109     // TODO (chennancy) Add in secure credentials
110   }
111   builder->AddListeningPort(server_address, creds);
112   builder->RegisterService(&callback_server);
113 
114   // Set up the server to start accepting requests.
115   std::shared_ptr<grpc::Server> server(builder->BuildAndStart());
116   LOG(INFO) << "Server listening on " << server_address;
117 
118   // Keep the program running until the server shuts down.
119   server->Wait();
120   return 0;
121 }
122