Files
grpc/test/cpp/end2end/rls_server.cc
Tanvi Jagtap 4dc1097d04 [grpc][Gpr_To_Absl_Logging] Migrating from gpr to absl logging - gpr_log (#36677)
[grpc][Gpr_To_Absl_Logging] Migrating from gpr to absl logging - gpr_log
In this CL we are migrating from gRPCs own gpr logging mechanism to absl logging mechanism. The intention is to deprecate gpr_log in the future.

We have the following mapping

1. gpr_log(GPR_INFO,...) -> LOG(INFO)
2. gpr_log(GPR_ERROR,...) -> LOG(ERROR)
3. gpr_log(GPR_DEBUG,...) -> VLOG(2)

Reviewers need to check :

1. If the above mapping is correct.
2. The content of the log is as before.
gpr_log format strings did not use string_view or std::string . absl LOG accepts these. So there will be some elimination of string_view and std::string related conversions. This is expected.

Closes #36677

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36677 from tanvi-jagtap:regex_test_end2end dfb803ebffe4ddc68698a91ee6e84be4bd49d4cd
PiperOrigin-RevId: 635807701
2024-05-21 08:03:45 -07:00

105 lines
3.5 KiB
C++

//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "test/cpp/end2end/rls_server.h"
#include <gmock/gmock.h>
#include "absl/log/log.h"
#include "src/proto/grpc/lookup/v1/rls.grpc.pb.h"
#include "src/proto/grpc/lookup/v1/rls.pb.h"
#include "test/core/test_util/test_config.h"
using ::grpc::lookup::v1::RouteLookupRequest;
using ::grpc::lookup::v1::RouteLookupResponse;
namespace grpc {
namespace testing {
::grpc::Status RlsServiceImpl::RouteLookup(grpc::ServerContext* context,
const RouteLookupRequest* request,
RouteLookupResponse* response) {
LOG(INFO) << "RLS: Received request: " << request->DebugString();
if (context_proc_ != nullptr) {
context_proc_(context);
}
IncreaseRequestCount();
EXPECT_EQ(request->target_type(), "grpc");
// See if we have a configured response for this request.
ResponseData res;
{
grpc::internal::MutexLock lock(&mu_);
auto it = responses_.find(*request);
if (it == responses_.end()) {
LOG(INFO) << "RLS: no matching request, returning INTERNAL";
unmatched_requests_.push_back(*request);
return Status(StatusCode::INTERNAL, "no response entry");
}
res = it->second;
}
// Configured response found, so use it.
if (res.response_delay > grpc_core::Duration::Zero()) {
gpr_sleep_until(
grpc_timeout_milliseconds_to_deadline(res.response_delay.millis()));
}
IncreaseResponseCount();
*response = res.response;
LOG(INFO) << "RLS: returning configured response: "
<< response->DebugString();
return Status::OK;
}
void RlsServiceImpl::SetResponse(RouteLookupRequest request,
RouteLookupResponse response,
grpc_core::Duration response_delay) {
grpc::internal::MutexLock lock(&mu_);
responses_[std::move(request)] = {std::move(response), response_delay};
}
void RlsServiceImpl::RemoveResponse(const RouteLookupRequest& request) {
grpc::internal::MutexLock lock(&mu_);
responses_.erase(request);
}
std::vector<RouteLookupRequest> RlsServiceImpl::GetUnmatchedRequests() {
grpc::internal::MutexLock lock(&mu_);
return std::move(unmatched_requests_);
}
grpc::lookup::v1::RouteLookupRequest BuildRlsRequest(
std::map<std::string, std::string> key,
grpc::lookup::v1::RouteLookupRequest::Reason reason,
const char* stale_header_data) {
grpc::lookup::v1::RouteLookupRequest request;
request.set_target_type("grpc");
request.mutable_key_map()->insert(key.begin(), key.end());
request.set_reason(reason);
request.set_stale_header_data(stale_header_data);
return request;
}
grpc::lookup::v1::RouteLookupResponse BuildRlsResponse(
std::vector<std::string> targets, const char* header_data) {
grpc::lookup::v1::RouteLookupResponse response;
response.mutable_targets()->Add(targets.begin(), targets.end());
response.set_header_data(header_data);
return response;
}
} // namespace testing
} // namespace grpc