| 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 |
#include <stddef.h> |
| 18 |
|
| 19 |
#include <map> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include "absl/base/thread_annotations.h" |
| 25 |
#include "absl/time/time.h" |
| 26 |
#include "absl/types/optional.h" |
| 27 |
#include "google/protobuf/duration.upb.h" |
| 28 |
#include "upb/upb.h" |
| 29 |
#include "upb/upb.hpp" |
| 30 |
#include "xds/data/orca/v3/orca_load_report.upb.h" |
| 31 |
#include "xds/service/orca/v3/orca.upb.h" |
| 32 |
|
| 33 |
#include <grpc/event_engine/event_engine.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
#include <grpcpp/ext/orca_service.h> |
| 36 |
#include <grpcpp/impl/codegen/server_callback_handlers.h> |
| 37 |
#include <grpcpp/impl/codegen/sync.h> |
| 38 |
#include <grpcpp/impl/rpc_method.h> |
| 39 |
#include <grpcpp/impl/rpc_service_method.h> |
| 40 |
#include <grpcpp/server_context.h> |
| 41 |
#include <grpcpp/support/byte_buffer.h> |
| 42 |
#include <grpcpp/support/server_callback.h> |
| 43 |
#include <grpcpp/support/slice.h> |
| 44 |
#include <grpcpp/support/status.h> |
| 45 |
|
| 46 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 47 |
#include "src/core/lib/gprpp/debug_location.h" |
| 48 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 49 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 50 |
#include "src/core/lib/gprpp/time.h" |
| 51 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 52 |
|
| 53 |
namespace grpc { |
| 54 |
namespace experimental { |
| 55 |
|
| 56 |
using ::grpc_event_engine::experimental::EventEngine; |
| 57 |
|
| 58 |
// |
| 59 |
// OrcaService::Reactor |
| 60 |
// |
| 61 |
|
| 62 |
class OrcaService::Reactor : public ServerWriteReactor<ByteBuffer>, |
| 63 |
public grpc_core::RefCounted<Reactor> { |
| 64 |
public: |
| 65 |
explicit Reactor(OrcaService* service, const ByteBuffer* request_buffer) |
| 66 |
: RefCounted("OrcaService::Reactor"), |
| 67 |
service_(service), |
| 68 |
engine_(grpc_event_engine::experimental::GetDefaultEventEngine()) { |
| 69 |
// Get slice from request. |
| 70 |
Slice slice; |
| 71 |
GPR_ASSERT(request_buffer->DumpToSingleSlice(&slice).ok()); |
| 72 |
// Parse request proto. |
| 73 |
upb::Arena arena; |
| 74 |
xds_service_orca_v3_OrcaLoadReportRequest* request = |
| 75 |
xds_service_orca_v3_OrcaLoadReportRequest_parse( |
| 76 |
reinterpret_cast<const char*>(slice.begin()), slice.size(), |
| 77 |
arena.ptr()); |
| 78 |
if (request == nullptr) { |
| 79 |
Finish(Status(StatusCode::INTERNAL, "could not parse request proto")); |
| 80 |
return; |
| 81 |
} |
| 82 |
const auto* duration_proto = |
| 83 |
xds_service_orca_v3_OrcaLoadReportRequest_report_interval(request); |
| 84 |
if (duration_proto != nullptr) { |
| 85 |
report_interval_ = grpc_core::Duration::FromSecondsAndNanoseconds( |
| 86 |
google_protobuf_Duration_seconds(duration_proto), |
| 87 |
google_protobuf_Duration_nanos(duration_proto)); |
| 88 |
} |
| 89 |
auto min_interval = grpc_core::Duration::Milliseconds( |
| 90 |
service_->min_report_duration_ / absl::Milliseconds(1)); |
| 91 |
if (report_interval_ < min_interval) report_interval_ = min_interval; |
| 92 |
// Send initial response. |
| 93 |
SendResponse(); |
| 94 |
} |
| 95 |
|
| 96 |
void OnWriteDone(bool ok) override { |
| 97 |
if (!ok) { |
| 98 |
Finish(Status(StatusCode::UNKNOWN, "write failed")); |
| 99 |
return; |
| 100 |
} |
| 101 |
response_.Clear(); |
| 102 |
if (!MaybeScheduleTimer()) { |
| 103 |
Finish(Status(StatusCode::UNKNOWN, "call cancelled by client")); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void OnCancel() override { |
| 108 |
if (MaybeCancelTimer()) { |
| 109 |
Finish(Status(StatusCode::UNKNOWN, "call cancelled by client")); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void OnDone() override { |
| 114 |
// Free the initial ref from instantiation. |
| 115 |
Unref(DEBUG_LOCATION, "OnDone"); |
| 116 |
} |
| 117 |
|
| 118 |
private: |
| 119 |
void SendResponse() { |
| 120 |
Slice response_slice = service_->GetOrCreateSerializedResponse(); |
| 121 |
ByteBuffer response_buffer(&response_slice, 1); |
| 122 |
response_.Swap(&response_buffer); |
| 123 |
StartWrite(&response_); |
| 124 |
} |
| 125 |
|
| 126 |
bool MaybeScheduleTimer() { |
| 127 |
grpc::internal::MutexLock lock(&timer_mu_); |
| 128 |
if (cancelled_) return false; |
| 129 |
timer_handle_ = engine_->RunAfter( |
| 130 |
report_interval_, |
| 131 |
[self = Ref(DEBUG_LOCATION, "Orca Service")] { self->OnTimer(); }); |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
bool MaybeCancelTimer() { |
| 136 |
grpc::internal::MutexLock lock(&timer_mu_); |
| 137 |
cancelled_ = true; |
| 138 |
if (timer_handle_.has_value() && engine_->Cancel(*timer_handle_)) { |
| 139 |
timer_handle_.reset(); |
| 140 |
return true; |
| 141 |
} |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
void OnTimer() { |
| 146 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 147 |
grpc_core::ExecCtx exec_ctx; |
| 148 |
grpc::internal::MutexLock lock(&timer_mu_); |
| 149 |
timer_handle_.reset(); |
| 150 |
SendResponse(); |
| 151 |
} |
| 152 |
|
| 153 |
OrcaService* service_; |
| 154 |
|
| 155 |
grpc::internal::Mutex timer_mu_; |
| 156 |
absl::optional<EventEngine::TaskHandle> timer_handle_ |
| 157 |
ABSL_GUARDED_BY(&timer_mu_); |
| 158 |
bool cancelled_ ABSL_GUARDED_BY(&timer_mu_) = false; |
| 159 |
|
| 160 |
grpc_core::Duration report_interval_; |
| 161 |
ByteBuffer response_; |
| 162 |
std::shared_ptr<grpc_event_engine::experimental::EventEngine> engine_; |
| 163 |
}; |
| 164 |
|
| 165 |
// |
| 166 |
// OrcaService |
| 167 |
// |
| 168 |
|
| 169 |
OrcaService::OrcaService(OrcaService::Options options) |
| 170 |
: min_report_duration_(options.min_report_duration) { |
| 171 |
AddMethod(new internal::RpcServiceMethod( |
| 172 |
"/xds.service.orca.v3.OpenRcaService/StreamCoreMetrics", |
| 173 |
internal::RpcMethod::SERVER_STREAMING, /*handler=*/nullptr)); |
| 174 |
MarkMethodCallback( |
| 175 |
0, new internal::CallbackServerStreamingHandler<ByteBuffer, ByteBuffer>( |
| 176 |
[this](CallbackServerContext* /*ctx*/, const ByteBuffer* request) { |
| 177 |
return new Reactor(this, request); |
| 178 |
})); |
| 179 |
} |
| 180 |
|
| 181 |
void OrcaService::SetCpuUtilization(double cpu_utilization) { |
| 182 |
grpc::internal::MutexLock lock(&mu_); |
| 183 |
cpu_utilization_ = cpu_utilization; |
| 184 |
response_slice_.reset(); |
| 185 |
} |
| 186 |
|
| 187 |
void OrcaService::DeleteCpuUtilization() { |
| 188 |
grpc::internal::MutexLock lock(&mu_); |
| 189 |
cpu_utilization_ = -1; |
| 190 |
response_slice_.reset(); |
| 191 |
} |
| 192 |
|
| 193 |
void OrcaService::SetMemoryUtilization(double memory_utilization) { |
| 194 |
grpc::internal::MutexLock lock(&mu_); |
| 195 |
memory_utilization_ = memory_utilization; |
| 196 |
response_slice_.reset(); |
| 197 |
} |
| 198 |
|
| 199 |
void OrcaService::DeleteMemoryUtilization() { |
| 200 |
grpc::internal::MutexLock lock(&mu_); |
| 201 |
memory_utilization_ = -1; |
| 202 |
response_slice_.reset(); |
| 203 |
} |
| 204 |
|
| 205 |
void OrcaService::SetNamedUtilization(std::string name, double utilization) { |
| 206 |
grpc::internal::MutexLock lock(&mu_); |
| 207 |
named_utilization_[std::move(name)] = utilization; |
| 208 |
response_slice_.reset(); |
| 209 |
} |
| 210 |
|
| 211 |
void OrcaService::DeleteNamedUtilization(const std::string& name) { |
| 212 |
grpc::internal::MutexLock lock(&mu_); |
| 213 |
named_utilization_.erase(name); |
| 214 |
response_slice_.reset(); |
| 215 |
} |
| 216 |
|
| 217 |
void OrcaService::SetAllNamedUtilization( |
| 218 |
std::map<std::string, double> named_utilization) { |
| 219 |
grpc::internal::MutexLock lock(&mu_); |
| 220 |
named_utilization_ = std::move(named_utilization); |
| 221 |
response_slice_.reset(); |
| 222 |
} |
| 223 |
|
| 224 |
Slice OrcaService::GetOrCreateSerializedResponse() { |
| 225 |
grpc::internal::MutexLock lock(&mu_); |
| 226 |
if (!response_slice_.has_value()) { |
| 227 |
upb::Arena arena; |
| 228 |
xds_data_orca_v3_OrcaLoadReport* response = |
| 229 |
xds_data_orca_v3_OrcaLoadReport_new(arena.ptr()); |
| 230 |
if (cpu_utilization_ != -1) { |
| 231 |
xds_data_orca_v3_OrcaLoadReport_set_cpu_utilization(response, |
| 232 |
cpu_utilization_); |
| 233 |
} |
| 234 |
if (memory_utilization_ != -1) { |
| 235 |
xds_data_orca_v3_OrcaLoadReport_set_mem_utilization(response, |
| 236 |
memory_utilization_); |
| 237 |
} |
| 238 |
for (const auto& p : named_utilization_) { |
| 239 |
xds_data_orca_v3_OrcaLoadReport_utilization_set( |
| 240 |
response, |
| 241 |
upb_StringView_FromDataAndSize(p.first.data(), p.first.size()), |
| 242 |
p.second, arena.ptr()); |
| 243 |
} |
| 244 |
size_t buf_length; |
| 245 |
char* buf = xds_data_orca_v3_OrcaLoadReport_serialize(response, arena.ptr(), |
| 246 |
&buf_length); |
| 247 |
response_slice_.emplace(buf, buf_length); |
| 248 |
} |
| 249 |
return Slice(*response_slice_); |
| 250 |
} |
| 251 |
|
| 252 |
} // namespace experimental |
| 253 |
} // namespace grpc |