orca_service.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef GRPCPP_EXT_ORCA_SERVICE_H
  17. #define GRPCPP_EXT_ORCA_SERVICE_H
  18. #include <map>
  19. #include <util/generic/string.h>
  20. #include <util/string/cast.h>
  21. #include "y_absl/time/time.h"
  22. #include "y_absl/types/optional.h"
  23. #include <grpcpp/ext/server_metric_recorder.h>
  24. #include <grpcpp/impl/service_type.h>
  25. #include <grpcpp/impl/sync.h>
  26. #include <grpcpp/server_builder.h>
  27. #include <grpcpp/support/server_callback.h>
  28. #include <grpcpp/support/slice.h>
  29. namespace grpc {
  30. namespace experimental {
  31. // RPC service implementation for supplying out-of-band backend
  32. // utilization metrics to clients.
  33. class OrcaService : public Service {
  34. public:
  35. struct Options {
  36. // Minimum report interval. If a client requests an interval lower
  37. // than this value, this value will be used instead.
  38. y_absl::Duration min_report_duration = y_absl::Seconds(30);
  39. Options() = default;
  40. Options& set_min_report_duration(y_absl::Duration duration) {
  41. min_report_duration = duration;
  42. return *this;
  43. }
  44. };
  45. // ServerMetricRecorder is required.
  46. OrcaService(ServerMetricRecorder* const server_metric_recorder,
  47. Options options);
  48. private:
  49. class Reactor;
  50. Slice GetOrCreateSerializedResponse();
  51. const ServerMetricRecorder* const server_metric_recorder_;
  52. const y_absl::Duration min_report_duration_;
  53. grpc::internal::Mutex mu_;
  54. // Contains the last serialized metrics from server_metric_recorder_.
  55. y_absl::optional<Slice> response_slice_ Y_ABSL_GUARDED_BY(mu_);
  56. // The update sequence number of metrics serialized in response_slice_.
  57. y_absl::optional<uint64_t> response_slice_seq_ Y_ABSL_GUARDED_BY(mu_);
  58. };
  59. } // namespace experimental
  60. } // namespace grpc
  61. #endif // GRPCPP_EXT_ORCA_SERVICE_H