| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include <thread> |
| 22 |
|
| 23 |
#include <gmock/gmock.h> |
| 24 |
#include <gtest/gtest.h> |
| 25 |
|
| 26 |
#include <grpc++/grpc++.h> |
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
#include <grpcpp/ext/server_load_reporting.h> |
| 31 |
#include <grpcpp/server_builder.h> |
| 32 |
|
| 33 |
#include "src/proto/grpc/lb/v1/load_reporter.grpc.pb.h" |
| 34 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 35 |
#include "test/core/util/port.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
|
| 38 |
namespace grpc { |
| 39 |
namespace testing { |
| 40 |
namespace { |
| 41 |
|
| 42 |
constexpr double kMetricValue = 3.1415; |
| 43 |
constexpr char kMetricName[] = "METRIC_PI"; |
| 44 |
|
| 45 |
// Different messages result in different response statuses. For simplicity in |
| 46 |
// computing request bytes, the message sizes should be the same. |
| 47 |
const char kOkMessage[] = "hello"; |
| 48 |
const char kServerErrorMessage[] = "sverr"; |
| 49 |
const char kClientErrorMessage[] = "clerr"; |
| 50 |
|
| 51 |
class EchoTestServiceImpl : public EchoTestService::Service { |
| 52 |
public: |
| 53 |
~EchoTestServiceImpl() override {} |
| 54 |
|
| 55 |
Status Echo(ServerContext* context, const EchoRequest* request, |
| 56 |
EchoResponse* response) override { |
| 57 |
if (request->message() == kServerErrorMessage) { |
| 58 |
return Status(StatusCode::UNKNOWN, "Server error requested"); |
| 59 |
} |
| 60 |
if (request->message() == kClientErrorMessage) { |
| 61 |
return Status(StatusCode::FAILED_PRECONDITION, "Client error requested"); |
| 62 |
} |
| 63 |
response->set_message(request->message()); |
| 64 |
grpc::load_reporter::experimental::AddLoadReportingCost( |
| 65 |
context, kMetricName, kMetricValue); |
| 66 |
return Status::OK; |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
class ServerLoadReportingEnd2endTest : public ::testing::Test { |
| 71 |
protected: |
| 72 |
void SetUp() override { |
| 73 |
server_address_ = |
| 74 |
"localhost:" + std::to_string(grpc_pick_unused_port_or_die()); |
| 75 |
server_ = |
| 76 |
ServerBuilder() |
| 77 |
.AddListeningPort(server_address_, InsecureServerCredentials()) |
| 78 |
.RegisterService(&echo_service_) |
| 79 |
.SetOption(std::unique_ptr<grpc::ServerBuilderOption>( |
| 80 |
new grpc::load_reporter::experimental:: |
| 81 |
LoadReportingServiceServerBuilderOption())) |
| 82 |
.BuildAndStart(); |
| 83 |
server_thread_ = |
| 84 |
std::thread(&ServerLoadReportingEnd2endTest::RunServerLoop, this); |
| 85 |
} |
| 86 |
|
| 87 |
void RunServerLoop() { server_->Wait(); } |
| 88 |
|
| 89 |
void TearDown() override { |
| 90 |
server_->Shutdown(); |
| 91 |
server_thread_.join(); |
| 92 |
} |
| 93 |
|
| 94 |
void ClientMakeEchoCalls(const std::string& lb_id, const std::string& lb_tag, |
| 95 |
const std::string& message, size_t num_requests) { |
| 96 |
auto stub = EchoTestService::NewStub( |
| 97 |
grpc::CreateChannel(server_address_, InsecureChannelCredentials())); |
| 98 |
std::string lb_token = lb_id + lb_tag; |
| 99 |
for (size_t i = 0; i < num_requests; ++i) { |
| 100 |
ClientContext ctx; |
| 101 |
if (!lb_token.empty()) ctx.AddMetadata(GRPC_LB_TOKEN_MD_KEY, lb_token); |
| 102 |
EchoRequest request; |
| 103 |
EchoResponse response; |
| 104 |
request.set_message(message); |
| 105 |
Status status = stub->Echo(&ctx, request, &response); |
| 106 |
if (message == kOkMessage) { |
| 107 |
ASSERT_EQ(status.error_code(), StatusCode::OK); |
| 108 |
ASSERT_EQ(request.message(), response.message()); |
| 109 |
} else if (message == kServerErrorMessage) { |
| 110 |
ASSERT_EQ(status.error_code(), StatusCode::UNKNOWN); |
| 111 |
} else if (message == kClientErrorMessage) { |
| 112 |
ASSERT_EQ(status.error_code(), StatusCode::FAILED_PRECONDITION); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
std::string server_address_; |
| 118 |
std::unique_ptr<Server> server_; |
| 119 |
std::thread server_thread_; |
| 120 |
EchoTestServiceImpl echo_service_; |
| 121 |
}; |
| 122 |
|
| 123 |
TEST_F(ServerLoadReportingEnd2endTest, NoCall) {} |
| 124 |
|
| 125 |
TEST_F(ServerLoadReportingEnd2endTest, BasicReport) { |
| 126 |
auto channel = |
| 127 |
grpc::CreateChannel(server_address_, InsecureChannelCredentials()); |
| 128 |
auto stub = grpc::lb::v1::LoadReporter::NewStub(channel); |
| 129 |
ClientContext ctx; |
| 130 |
auto stream = stub->ReportLoad(&ctx); |
| 131 |
grpc::lb::v1::LoadReportRequest request; |
| 132 |
request.mutable_initial_request()->set_load_balanced_hostname( |
| 133 |
server_address_); |
| 134 |
request.mutable_initial_request()->set_load_key("LOAD_KEY"); |
| 135 |
request.mutable_initial_request() |
| 136 |
->mutable_load_report_interval() |
| 137 |
->set_seconds(5); |
| 138 |
stream->Write(request); |
| 139 |
gpr_log(GPR_INFO, "Initial request sent."); |
| 140 |
grpc::lb::v1::LoadReportResponse response; |
| 141 |
stream->Read(&response); |
| 142 |
const std::string& lb_id = response.initial_response().load_balancer_id(); |
| 143 |
gpr_log(GPR_INFO, "Initial response received (lb_id: %s).", lb_id.c_str()); |
| 144 |
ClientMakeEchoCalls(lb_id, "LB_TAG", kOkMessage, 1); |
| 145 |
|
| 146 |
unsigned load_count = 0; |
| 147 |
bool got_in_progress = false; |
| 148 |
bool got_orphaned = false; |
| 149 |
bool got_calls = false; |
| 150 |
while (load_count < 3) { |
| 151 |
stream->Read(&response); |
| 152 |
for (const auto& load : response.load()) { |
| 153 |
load_count++; |
| 154 |
if (load.in_progress_report_case()) { |
| 155 |
// The special load record that reports the number of in-progress |
| 156 |
// calls. |
| 157 |
EXPECT_EQ(load.num_calls_in_progress(), 1); |
| 158 |
EXPECT_FALSE(got_in_progress); |
| 159 |
got_in_progress = true; |
| 160 |
} else if (load.orphaned_load_case()) { |
| 161 |
// The call from the balancer doesn't have any valid LB token. |
| 162 |
EXPECT_EQ(load.orphaned_load_case(), load.kLoadKeyUnknown); |
| 163 |
EXPECT_EQ(load.num_calls_started(), 1); |
| 164 |
EXPECT_EQ(load.num_calls_finished_without_error(), 0); |
| 165 |
EXPECT_EQ(load.num_calls_finished_with_error(), 0); |
| 166 |
EXPECT_FALSE(got_orphaned); |
| 167 |
got_orphaned = true; |
| 168 |
} else { |
| 169 |
// This corresponds to the calls from the client. |
| 170 |
EXPECT_EQ(load.num_calls_started(), 1); |
| 171 |
EXPECT_EQ(load.num_calls_finished_without_error(), 1); |
| 172 |
EXPECT_EQ(load.num_calls_finished_with_error(), 0); |
| 173 |
EXPECT_GE(load.total_bytes_received(), sizeof(kOkMessage)); |
| 174 |
EXPECT_GE(load.total_bytes_sent(), sizeof(kOkMessage)); |
| 175 |
EXPECT_EQ(load.metric_data().size(), 1); |
| 176 |
EXPECT_EQ(load.metric_data().Get(0).metric_name(), kMetricName); |
| 177 |
EXPECT_EQ(load.metric_data().Get(0).num_calls_finished_with_metric(), |
| 178 |
1); |
| 179 |
EXPECT_EQ(load.metric_data().Get(0).total_metric_value(), kMetricValue); |
| 180 |
EXPECT_FALSE(got_calls); |
| 181 |
got_calls = true; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
EXPECT_EQ(load_count, 3); |
| 186 |
EXPECT_TRUE(got_in_progress); |
| 187 |
EXPECT_TRUE(got_orphaned); |
| 188 |
EXPECT_TRUE(got_calls); |
| 189 |
stream->WritesDone(); |
| 190 |
EXPECT_EQ(stream->Finish().error_code(), StatusCode::CANCELLED); |
| 191 |
} |
| 192 |
|
| 193 |
// TODO(juanlishen): Add more tests. |
| 194 |
|
| 195 |
} // namespace |
| 196 |
} // namespace testing |
| 197 |
} // namespace grpc |
| 198 |
|
| 199 |
int main(int argc, char** argv) { |
| 200 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 201 |
::testing::InitGoogleTest(&argc, argv); |
| 202 |
return RUN_ALL_TESTS(); |
| 203 |
} |