Debian Codegraph @

spm-cpp-include/grpcpp/ext/orca_service.h

raw
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef GRPCPP_EXT_ORCA_SERVICE_H
18 #define GRPCPP_EXT_ORCA_SERVICE_H
19
20 #include <map>
21 #include <string>
22
23 #include "absl/time/time.h"
24 #include "absl/types/optional.h"
25
26 #include <grpcpp/impl/codegen/sync.h>
27 #include <grpcpp/impl/service_type.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/support/server_callback.h>
30 #include <grpcpp/support/slice.h>
31
32 namespace grpc {
33 namespace experimental {
34
35 // RPC service implementation for supplying out-of-band backend
36 // utilization metrics to clients.
37 class OrcaService : public Service {
38 public:
39 struct Options {
40 // Minimum report interval. If a client requests an interval lower
41 // than this value, this value will be used instead.
42 absl::Duration min_report_duration = absl::Seconds(30);
43
44 Options() = default;
45 Options& set_min_report_duration(absl::Duration duration) {
46 min_report_duration = duration;
47 return *this;
48 }
49 };
50
51 explicit OrcaService(Options options);
52
53 // Sets or removes the CPU utilization value to be reported to clients.
54 void SetCpuUtilization(double cpu_utilization);
55 void DeleteCpuUtilization();
56
57 // Sets of removes the memory utilization value to be reported to clients.
58 void SetMemoryUtilization(double memory_utilization);
59 void DeleteMemoryUtilization();
60
61 // Sets or removed named utilization values to be reported to clients.
62 void SetNamedUtilization(std::string name, double utilization);
63 void DeleteNamedUtilization(const std::string& name);
64 void SetAllNamedUtilization(std::map<std::string, double> named_utilization);
65
66 private:
67 class Reactor;
68
69 Slice GetOrCreateSerializedResponse();
70
71 const absl::Duration min_report_duration_;
72
73 grpc::internal::Mutex mu_;
74 double cpu_utilization_ ABSL_GUARDED_BY(&mu_) = -1;
75 double memory_utilization_ ABSL_GUARDED_BY(&mu_) = -1;
76 std::map<std::string, double> named_utilization_ ABSL_GUARDED_BY(&mu_);
77 absl::optional<Slice> response_slice_ ABSL_GUARDED_BY(&mu_);
78 };
79
80 } // namespace experimental
81 } // namespace grpc
82
83 #endif // GRPCPP_EXT_ORCA_SERVICE_H