| 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 <gmock/gmock.h> |
| 18 |
#include <gtest/gtest.h> |
| 19 |
|
| 20 |
#include "absl/strings/str_cat.h" |
| 21 |
#include "absl/types/optional.h" |
| 22 |
|
| 23 |
#include <grpc/grpc.h> |
| 24 |
#include <grpcpp/channel.h> |
| 25 |
#include <grpcpp/client_context.h> |
| 26 |
#include <grpcpp/create_channel.h> |
| 27 |
#include <grpcpp/ext/orca_service.h> |
| 28 |
#include <grpcpp/server.h> |
| 29 |
#include <grpcpp/server_builder.h> |
| 30 |
#include <grpcpp/server_context.h> |
| 31 |
|
| 32 |
#include "src/core/lib/gprpp/time.h" |
| 33 |
#include "src/proto/grpc/testing/xds/v3/orca_service.grpc.pb.h" |
| 34 |
#include "src/proto/grpc/testing/xds/v3/orca_service.pb.h" |
| 35 |
#include "test/core/util/port.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
|
| 38 |
using xds::data::orca::v3::OrcaLoadReport; |
| 39 |
using xds::service::orca::v3::OpenRcaService; |
| 40 |
using xds::service::orca::v3::OrcaLoadReportRequest; |
| 41 |
|
| 42 |
namespace grpc { |
| 43 |
namespace testing { |
| 44 |
namespace { |
| 45 |
|
| 46 |
using experimental::OrcaService; |
| 47 |
|
| 48 |
class OrcaServiceEnd2endTest : public ::testing::Test { |
| 49 |
protected: |
| 50 |
// A wrapper for the client stream that ensures that responses come |
| 51 |
// back at the requested interval. |
| 52 |
class Stream { |
| 53 |
public: |
| 54 |
Stream(OpenRcaService::Stub* stub, grpc_core::Duration requested_interval) |
| 55 |
: requested_interval_(requested_interval) { |
| 56 |
OrcaLoadReportRequest request; |
| 57 |
gpr_timespec timespec = requested_interval.as_timespec(); |
| 58 |
auto* interval_proto = request.mutable_report_interval(); |
| 59 |
interval_proto->set_seconds(timespec.tv_sec); |
| 60 |
interval_proto->set_nanos(timespec.tv_nsec); |
| 61 |
stream_ = stub->StreamCoreMetrics(&context_, request); |
| 62 |
} |
| 63 |
|
| 64 |
~Stream() { context_.TryCancel(); } |
| 65 |
|
| 66 |
OrcaLoadReport ReadResponse() { |
| 67 |
OrcaLoadReport response; |
| 68 |
EXPECT_TRUE(stream_->Read(&response)); |
| 69 |
auto now = grpc_core::Timestamp::FromTimespecRoundDown( |
| 70 |
gpr_now(GPR_CLOCK_MONOTONIC)); |
| 71 |
if (last_response_time_.has_value()) { |
| 72 |
// Allow a small fudge factor to avoid test flakiness. |
| 73 |
const grpc_core::Duration fudge_factor = |
| 74 |
grpc_core::Duration::Milliseconds(750) * |
| 75 |
grpc_test_slowdown_factor(); |
| 76 |
auto elapsed = now - *last_response_time_; |
| 77 |
EXPECT_GE(elapsed, requested_interval_ - fudge_factor) |
| 78 |
<< elapsed.ToString(); |
| 79 |
EXPECT_LE(elapsed, requested_interval_ + fudge_factor) |
| 80 |
<< elapsed.ToString(); |
| 81 |
} |
| 82 |
last_response_time_ = now; |
| 83 |
return response; |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
const grpc_core::Duration requested_interval_; |
| 88 |
ClientContext context_; |
| 89 |
std::unique_ptr<grpc::ClientReaderInterface<OrcaLoadReport>> stream_; |
| 90 |
absl::optional<grpc_core::Timestamp> last_response_time_; |
| 91 |
}; |
| 92 |
|
| 93 |
OrcaServiceEnd2endTest() |
| 94 |
: orca_service_(OrcaService::Options().set_min_report_duration( |
| 95 |
absl::ZeroDuration())) { |
| 96 |
std::string server_address = |
| 97 |
absl::StrCat("localhost:", grpc_pick_unused_port_or_die()); |
| 98 |
ServerBuilder builder; |
| 99 |
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
| 100 |
builder.RegisterService(&orca_service_); |
| 101 |
server_ = builder.BuildAndStart(); |
| 102 |
gpr_log(GPR_INFO, "server started on %s", server_address_.c_str()); |
| 103 |
auto channel = CreateChannel(server_address, InsecureChannelCredentials()); |
| 104 |
stub_ = OpenRcaService::NewStub(channel); |
| 105 |
} |
| 106 |
|
| 107 |
~OrcaServiceEnd2endTest() override { server_->Shutdown(); } |
| 108 |
|
| 109 |
std::string server_address_; |
| 110 |
OrcaService orca_service_; |
| 111 |
std::unique_ptr<Server> server_; |
| 112 |
std::unique_ptr<OpenRcaService::Stub> stub_; |
| 113 |
}; |
| 114 |
|
| 115 |
TEST_F(OrcaServiceEnd2endTest, Basic) { |
| 116 |
constexpr char kMetricName1[] = "foo"; |
| 117 |
constexpr char kMetricName2[] = "bar"; |
| 118 |
constexpr char kMetricName3[] = "baz"; |
| 119 |
constexpr char kMetricName4[] = "quux"; |
| 120 |
// Start stream1 with 5s interval and stream2 with 2.5s interval. |
| 121 |
// Throughout the test, we should get two responses on stream2 for |
| 122 |
// every one response on stream1. |
| 123 |
Stream stream1(stub_.get(), grpc_core::Duration::Milliseconds(5000)); |
| 124 |
Stream stream2(stub_.get(), grpc_core::Duration::Milliseconds(2500)); |
| 125 |
auto ReadResponses = [&](std::function<void(const OrcaLoadReport&)> checker) { |
| 126 |
gpr_log(GPR_INFO, "reading response from stream1"); |
| 127 |
OrcaLoadReport response = stream1.ReadResponse(); |
| 128 |
checker(response); |
| 129 |
gpr_log(GPR_INFO, "reading response from stream2"); |
| 130 |
response = stream2.ReadResponse(); |
| 131 |
checker(response); |
| 132 |
gpr_log(GPR_INFO, "reading response from stream2"); |
| 133 |
response = stream2.ReadResponse(); |
| 134 |
checker(response); |
| 135 |
}; |
| 136 |
// Initial response should not have any values populated. |
| 137 |
ReadResponses([](const OrcaLoadReport& response) { |
| 138 |
EXPECT_EQ(response.cpu_utilization(), 0); |
| 139 |
EXPECT_EQ(response.mem_utilization(), 0); |
| 140 |
EXPECT_THAT(response.utilization(), ::testing::UnorderedElementsAre()); |
| 141 |
}); |
| 142 |
// Now set CPU utilization on the server. |
| 143 |
orca_service_.SetCpuUtilization(0.5); |
| 144 |
ReadResponses([](const OrcaLoadReport& response) { |
| 145 |
EXPECT_EQ(response.cpu_utilization(), 0.5); |
| 146 |
EXPECT_EQ(response.mem_utilization(), 0); |
| 147 |
EXPECT_THAT(response.utilization(), ::testing::UnorderedElementsAre()); |
| 148 |
}); |
| 149 |
// Update CPU utilization and set memory utilization. |
| 150 |
orca_service_.SetCpuUtilization(0.8); |
| 151 |
orca_service_.SetMemoryUtilization(0.4); |
| 152 |
ReadResponses([](const OrcaLoadReport& response) { |
| 153 |
EXPECT_EQ(response.cpu_utilization(), 0.8); |
| 154 |
EXPECT_EQ(response.mem_utilization(), 0.4); |
| 155 |
EXPECT_THAT(response.utilization(), ::testing::UnorderedElementsAre()); |
| 156 |
}); |
| 157 |
// Unset CPU and memory utilization and set a named utilization. |
| 158 |
orca_service_.DeleteCpuUtilization(); |
| 159 |
orca_service_.DeleteMemoryUtilization(); |
| 160 |
orca_service_.SetNamedUtilization(kMetricName1, 0.3); |
| 161 |
ReadResponses([&](const OrcaLoadReport& response) { |
| 162 |
EXPECT_EQ(response.cpu_utilization(), 0); |
| 163 |
EXPECT_EQ(response.mem_utilization(), 0); |
| 164 |
EXPECT_THAT( |
| 165 |
response.utilization(), |
| 166 |
::testing::UnorderedElementsAre(::testing::Pair(kMetricName1, 0.3))); |
| 167 |
}); |
| 168 |
// Unset the previous named utilization and set two new ones. |
| 169 |
orca_service_.DeleteNamedUtilization(kMetricName1); |
| 170 |
orca_service_.SetNamedUtilization(kMetricName2, 0.2); |
| 171 |
orca_service_.SetNamedUtilization(kMetricName3, 0.1); |
| 172 |
ReadResponses([&](const OrcaLoadReport& response) { |
| 173 |
EXPECT_EQ(response.cpu_utilization(), 0); |
| 174 |
EXPECT_EQ(response.mem_utilization(), 0); |
| 175 |
EXPECT_THAT( |
| 176 |
response.utilization(), |
| 177 |
::testing::UnorderedElementsAre(::testing::Pair(kMetricName2, 0.2), |
| 178 |
::testing::Pair(kMetricName3, 0.1))); |
| 179 |
}); |
| 180 |
// Replace the entire named metric map at once. |
| 181 |
orca_service_.SetAllNamedUtilization( |
| 182 |
{{kMetricName2, 0.5}, {kMetricName4, 0.9}}); |
| 183 |
ReadResponses([&](const OrcaLoadReport& response) { |
| 184 |
EXPECT_EQ(response.cpu_utilization(), 0); |
| 185 |
EXPECT_EQ(response.mem_utilization(), 0); |
| 186 |
EXPECT_THAT( |
| 187 |
response.utilization(), |
| 188 |
::testing::UnorderedElementsAre(::testing::Pair(kMetricName2, 0.5), |
| 189 |
::testing::Pair(kMetricName4, 0.9))); |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
} // namespace |
| 194 |
} // namespace testing |
| 195 |
} // namespace grpc |
| 196 |
|
| 197 |
int main(int argc, char** argv) { |
| 198 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 199 |
::testing::InitGoogleTest(&argc, argv); |
| 200 |
return RUN_ALL_TESTS(); |
| 201 |
} |