1 /*
2 *
3 * Copyright 2023 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 #include <cstddef>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/strings/str_format.h"
26 #include "examples/protos/helloworld.grpc.pb.h"
27
28 #include <grpcpp/ext/call_metric_recorder.h>
29 #include <grpcpp/ext/orca_service.h>
30 #include <grpcpp/grpcpp.h>
31 #include <grpcpp/health_check_service_interface.h>
32 #include <grpcpp/support/status.h>
33
34 using grpc::CallbackServerContext;
35 using grpc::Server;
36 using grpc::ServerBuilder;
37 using grpc::ServerUnaryReactor;
38 using grpc::Status;
39 using helloworld::Greeter;
40 using helloworld::HelloReply;
41 using helloworld::HelloRequest;
42
43 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
44
45 // Logic and data behind the server's behavior.
46 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)47 ServerUnaryReactor* SayHello(CallbackServerContext* context,
48 const HelloRequest* request,
49 HelloReply* reply) override {
50 ServerUnaryReactor* reactor = context->DefaultReactor();
51 // Obtain the call metric recorder and use it to report the number of
52 // DB queries (custom cost metric) and CPU utilization.
53 auto recorder = context->ExperimentalGetCallMetricRecorder();
54 if (recorder == nullptr) {
55 reactor->Finish({grpc::StatusCode::INTERNAL,
56 "Unable to access metrics recorder. Make sure "
57 "EnableCallMetricRecording had been called."});
58 return reactor;
59 }
60 recorder->RecordRequestCostMetric("db_queries", 10);
61 recorder->RecordCpuUtilizationMetric(0.5);
62 std::string prefix("Hello ");
63 reply->set_message(prefix + request->name());
64 reactor->Finish(Status::OK);
65 return reactor;
66 }
67 };
68
RunServer(uint16_t port)69 void RunServer(uint16_t port) {
70 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
71 ServerBuilder builder;
72 GreeterServiceImpl service;
73 // Setup custom metrics recording. Note that this recorder may be use to send
74 // the out-of-band metrics to the client.
75 auto server_metric_recorder =
76 grpc::experimental::ServerMetricRecorder::Create();
77 grpc::experimental::OrcaService orca_service(
78 server_metric_recorder.get(),
79 grpc::experimental::OrcaService::Options().set_min_report_duration(
80 absl::Seconds(0.1)));
81 builder.RegisterService(&orca_service);
82 grpc::ServerBuilder::experimental_type(&builder).EnableCallMetricRecording(
83 server_metric_recorder.get());
84 // Resume setting up gRPC server as usual
85 grpc::EnableDefaultHealthCheckService(true);
86 // Listen on the given address without any authentication mechanism.
87 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
88 // Register "service" as the instance through which we'll communicate with
89 // clients. In this case it corresponds to an *synchronous* service.
90 builder.RegisterService(&service);
91 // Finally assemble the server.
92 std::unique_ptr<Server> server(builder.BuildAndStart());
93 std::cout << "Server listening on " << server_address << std::endl;
94 server->Wait();
95 }
96
main(int argc,char ** argv)97 int main(int argc, char** argv) {
98 absl::ParseCommandLine(argc, argv);
99 RunServer(absl::GetFlag(FLAGS_port));
100 return 0;
101 }
102