xref: /aosp_15_r20/external/grpc-grpc/include/grpcpp/ext/orca_service.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/ext/server_metric_recorder.h>
27 #include <grpcpp/impl/service_type.h>
28 #include <grpcpp/impl/sync.h>
29 #include <grpcpp/server_builder.h>
30 #include <grpcpp/support/server_callback.h>
31 #include <grpcpp/support/slice.h>
32 
33 namespace grpc {
34 namespace experimental {
35 
36 // RPC service implementation for supplying out-of-band backend
37 // utilization metrics to clients.
38 class OrcaService : public Service {
39  public:
40   struct Options {
41     // Minimum report interval.  If a client requests an interval lower
42     // than this value, this value will be used instead.
43     absl::Duration min_report_duration = absl::Seconds(30);
44 
45     Options() = default;
set_min_report_durationOptions46     Options& set_min_report_duration(absl::Duration duration) {
47       min_report_duration = duration;
48       return *this;
49     }
50   };
51 
52   // ServerMetricRecorder is required.
53   OrcaService(ServerMetricRecorder* const server_metric_recorder,
54               Options options);
55 
56  private:
57   class Reactor;
58 
59   Slice GetOrCreateSerializedResponse();
60 
61   const ServerMetricRecorder* const server_metric_recorder_;
62   const absl::Duration min_report_duration_;
63   grpc::internal::Mutex mu_;
64   // Contains the last serialized metrics from server_metric_recorder_.
65   absl::optional<Slice> response_slice_ ABSL_GUARDED_BY(mu_);
66   // The update sequence number of metrics serialized in response_slice_.
67   absl::optional<uint64_t> response_slice_seq_ ABSL_GUARDED_BY(mu_);
68 };
69 
70 }  // namespace experimental
71 }  // namespace grpc
72 
73 #endif  // GRPCPP_EXT_ORCA_SERVICE_H
74