• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/event_engine/event_engine.h>
21 #include <grpcpp/ext/server_metric_recorder.h>
22 #include <grpcpp/impl/service_type.h>
23 #include <grpcpp/impl/sync.h>
24 #include <grpcpp/server_builder.h>
25 #include <grpcpp/support/server_callback.h>
26 #include <grpcpp/support/slice.h>
27 #include <grpcpp/support/status.h>
28 
29 #include <cstdint>
30 
31 #include "absl/base/thread_annotations.h"
32 #include "absl/time/time.h"
33 #include "absl/types/optional.h"
34 
35 namespace grpc {
36 
37 namespace testing {
38 class OrcaServiceTest;
39 }  // namespace testing
40 
41 namespace experimental {
42 
43 // RPC service implementation for supplying out-of-band backend
44 // utilization metrics to clients.
45 class OrcaService : public Service {
46  public:
47   struct Options {
48     // Minimum report interval.  If a client requests an interval lower
49     // than this value, this value will be used instead.
50     absl::Duration min_report_duration = absl::Seconds(30);
51 
52     Options() = default;
set_min_report_durationOptions53     Options& set_min_report_duration(absl::Duration duration) {
54       min_report_duration = duration;
55       return *this;
56     }
57   };
58 
59   // ServerMetricRecorder is required.
60   OrcaService(ServerMetricRecorder* const server_metric_recorder,
61               Options options);
62 
63  private:
64   class ReactorHook {
65    public:
66     virtual ~ReactorHook() = default;
67     virtual void OnFinish(grpc::Status status) = 0;
68     virtual void OnStartWrite(const ByteBuffer* response) = 0;
69   };
70 
71   class Reactor;
72   friend class testing::OrcaServiceTest;
73 
74   Slice GetOrCreateSerializedResponse();
75 
76   const ServerMetricRecorder* const server_metric_recorder_;
77   const absl::Duration min_report_duration_;
78   grpc::internal::Mutex mu_;
79   // Contains the last serialized metrics from server_metric_recorder_.
80   absl::optional<Slice> response_slice_ ABSL_GUARDED_BY(mu_);
81   // The update sequence number of metrics serialized in response_slice_.
82   absl::optional<uint64_t> response_slice_seq_ ABSL_GUARDED_BY(mu_);
83 };
84 
85 }  // namespace experimental
86 }  // namespace grpc
87 
88 #endif  // GRPCPP_EXT_ORCA_SERVICE_H
89