Skip to content

Commit 8d87814

Browse files
authored
Wrap absl::string_view as std::string to support protobuf v30+ (#3187)
* Wrap absl::string_view as std::string to support protobuf v30+ Closes #3181 * remove unnecessary specialization for abs:string_view * keep path for consistency
1 parent d688e75 commit 8d87814

20 files changed

+184
-122
lines changed

src/brpc/amf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ void WriteAMFObject(const google::protobuf::Message& message,
10001000
continue;
10011001
}
10021002
}
1003-
const std::string& name = field->name();
1003+
const auto& name = field->name();
10041004
if (name.size() >= 65536u) {
10051005
LOG(ERROR) << "name is too long!";
10061006
return stream->set_bad();

src/brpc/builtin/protobufs_service.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

1818

1919
#include <google/protobuf/descriptor.h> // ServiceDescriptor
20+
21+
#include "brpc/builtin/protobufs_service.h"
22+
2023
#include "brpc/controller.h" // Controller
2124
#include "brpc/server.h" // Server
2225
#include "brpc/closure_guard.h" // ClosureGuard
2326
#include "brpc/details/method_status.h"// MethodStatus
24-
#include "brpc/builtin/protobufs_service.h"
2527
#include "brpc/builtin/common.h"
2628

29+
#include "butil/strings/string_util.h"
2730

2831
namespace brpc {
2932

@@ -42,7 +45,7 @@ int ProtobufsService::Init() {
4245
}
4346
const google::protobuf::ServiceDescriptor* d =
4447
iter->second.service->GetDescriptor();
45-
_map[d->full_name()] = d->DebugString();
48+
_map[butil::EnsureString(d->full_name())] = d->DebugString();
4649
const int method_count = d->method_count();
4750
for (int j = 0; j < method_count; ++j) {
4851
const google::protobuf::MethodDescriptor* md = d->method(j);
@@ -53,13 +56,13 @@ int ProtobufsService::Init() {
5356
while (!stack.empty()) {
5457
const google::protobuf::Descriptor* d = stack.back();
5558
stack.pop_back();
56-
_map[d->full_name()] = d->DebugString();
59+
_map[butil::EnsureString(d->full_name())] = d->DebugString();
5760
for (int i = 0; i < d->field_count(); ++i) {
5861
const google::protobuf::FieldDescriptor* f = d->field(i);
5962
if (f->type() == google::protobuf::FieldDescriptor::TYPE_MESSAGE ||
6063
f->type() == google::protobuf::FieldDescriptor::TYPE_GROUP) {
6164
const google::protobuf::Descriptor* sub_d = f->message_type();
62-
if (sub_d != d && _map.find(sub_d->full_name()) == _map.end()) {
65+
if (sub_d != d && _map.find(butil::EnsureString(sub_d->full_name())) == _map.end()) {
6366
stack.push_back(sub_d);
6467
}
6568
}

src/brpc/channel.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,17 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method,
492492

493493
if (cntl->_sender == NULL && IsTraceable(Span::tls_parent())) {
494494
const int64_t start_send_us = butil::cpuwide_time_us();
495-
const std::string* method_name = NULL;
495+
std::string method_name;
496496
if (_get_method_name) {
497-
method_name = &_get_method_name(method, cntl);
497+
method_name = butil::EnsureString(_get_method_name(method, cntl));
498498
} else if (method) {
499-
method_name = &method->full_name();
499+
method_name = butil::EnsureString(method->full_name());
500500
} else {
501501
const static std::string NULL_METHOD_STR = "null-method";
502-
method_name = &NULL_METHOD_STR;
502+
method_name = NULL_METHOD_STR;
503503
}
504504
Span* span = Span::CreateClientSpan(
505-
*method_name, start_send_real_us - start_send_us);
505+
method_name, start_send_real_us - start_send_us);
506506
span->set_log_id(cntl->log_id());
507507
span->set_base_cid(correlation_id);
508508
span->set_protocol(_options.protocol);

src/brpc/nshead_pb_service_adaptor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@
1919
#include <google/protobuf/descriptor.h> // MethodDescriptor
2020
#include <google/protobuf/message.h> // Message
2121

22-
#include "butil/time.h"
23-
#include "butil/iobuf.h" // butil::IOBuf
22+
#include "brpc/nshead_pb_service_adaptor.h"
2423

2524
#include "brpc/controller.h" // Controller
2625
#include "brpc/socket.h" // Socket
2726
#include "brpc/server.h" // Server
2827
#include "brpc/span.h"
2928
#include "brpc/details/server_private_accessor.h"
3029
#include "brpc/details/controller_private_accessor.h"
31-
#include "brpc/nshead_pb_service_adaptor.h"
3230
#include "brpc/policy/most_common_message.h"
3331

32+
#include "butil/iobuf.h" // butil::IOBuf
33+
#include "butil/strings/string_util.h"
34+
#include "butil/time.h"
35+
3436

3537
namespace brpc {
3638

@@ -126,7 +128,7 @@ void NsheadPbServiceAdaptor::ProcessNsheadRequest(
126128
google::protobuf::Service* svc = sp->service;
127129
const google::protobuf::MethodDescriptor* method = sp->method;
128130
ControllerPrivateAccessor(controller).set_method(method);
129-
done->SetMethodName(method->full_name());
131+
done->SetMethodName(butil::EnsureString(method->full_name()));
130132
pbdone->pbreq.reset(svc->GetRequestPrototype(method).New());
131133
pbdone->pbres.reset(svc->GetResponsePrototype(method).New());
132134

src/brpc/policy/baidu_rpc_protocol.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
2222
#include <google/protobuf/io/coded_stream.h>
2323
#include <google/protobuf/text_format.h>
24-
#include "butil/logging.h" // LOG()
24+
2525
#include "butil/iobuf.h" // butil::IOBuf
26-
#include "butil/raw_pack.h" // RawPacker RawUnpacker
26+
#include "butil/logging.h" // LOG()
2727
#include "butil/memory/scope_guard.h"
28+
#include "butil/raw_pack.h" // RawPacker RawUnpacker
29+
#include "butil/strings/string_util.h"
30+
2831
#include "json2pb/json_to_pb.h"
2932
#include "json2pb/pb_to_json.h"
3033
#include "brpc/controller.h" // Controller
@@ -233,7 +236,7 @@ static bool SerializeResponse(const google::protobuf::Message& res,
233236
cntl.SetFailed(ERESPONSE,
234237
"Fail to serialize response=%s, "
235238
"ContentType=%s, CompressType=%s, ChecksumType=%s",
236-
res.GetDescriptor()->full_name().c_str(),
239+
butil::EnsureString(res.GetDescriptor()->full_name()).c_str(),
237240
ContentTypeToCStr(content_type),
238241
CompressTypeToCStr(compress_type),
239242
ChecksumTypeToCStr(checksum_type));
@@ -775,7 +778,7 @@ void ProcessRpcRequest(InputMessageBase* msg_base) {
775778
cntl->SetFailed(
776779
ELIMIT,
777780
"Rejected by %s's ConcurrencyLimiter, concurrency=%d",
778-
mp->method->full_name().c_str(), rejected_cc);
781+
butil::EnsureString(mp->method->full_name()).c_str(), rejected_cc);
779782
break;
780783
}
781784
}
@@ -784,7 +787,7 @@ void ProcessRpcRequest(InputMessageBase* msg_base) {
784787
accessor.set_method(method);
785788

786789
if (span) {
787-
span->ResetServerSpanName(method->full_name());
790+
span->ResetServerSpanName(butil::EnsureString(method->full_name()));
788791
}
789792

790793
if (!server->AcceptRequest(cntl.get())) {
@@ -812,7 +815,7 @@ void ProcessRpcRequest(InputMessageBase* msg_base) {
812815
EREQUEST,
813816
"Fail to parse request=%s, ContentType=%s, "
814817
"CompressType=%s, ChecksumType=%s, request_size=%d",
815-
messages->Request()->GetDescriptor()->full_name().c_str(),
818+
butil::EnsureString(messages->Request()->GetDescriptor()->full_name()).c_str(),
816819
ContentTypeToCStr(content_type),
817820
CompressTypeToCStr(compress_type),
818821
ChecksumTypeToCStr(checksum_type), req_size);
@@ -996,7 +999,7 @@ void ProcessRpcResponse(InputMessageBase* msg_base) {
996999
EREQUEST,
9971000
"Fail to parse response=%s, ContentType=%s, "
9981001
"CompressType=%s, ChecksumType=%s, request_size=%d",
999-
cntl->response()->GetDescriptor()->full_name().c_str(),
1002+
butil::EnsureString(cntl->response()->GetDescriptor()->full_name()).c_str(),
10001003
ContentTypeToCStr(content_type),
10011004
CompressTypeToCStr(compress_type),
10021005
ChecksumTypeToCStr(checksum_type), res_size);
@@ -1033,7 +1036,7 @@ void SerializeRpcRequest(butil::IOBuf* request_buf, Controller* cntl,
10331036
EREQUEST,
10341037
"Fail to compress request=%s, "
10351038
"ContentType=%s, CompressType=%s, ChecksumType=%s",
1036-
request->GetDescriptor()->full_name().c_str(),
1039+
butil::EnsureString(request->GetDescriptor()->full_name()).c_str(),
10371040
ContentTypeToCStr(content_type), CompressTypeToCStr(compress_type),
10381041
ChecksumTypeToCStr(checksum_type));
10391042
}

src/brpc/policy/http_rpc_protocol.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
#include <google/protobuf/text_format.h>
2121
#include <gflags/gflags.h>
2222
#include <string>
23+
2324
#include "brpc/policy/http_rpc_protocol.h"
24-
#include "butil/unique_ptr.h" // std::unique_ptr
25-
#include "butil/string_splitter.h" // StringMultiSplitter
25+
2626
#include "butil/string_printf.h"
27-
#include "butil/time.h"
27+
#include "butil/string_splitter.h" // StringMultiSplitter
28+
#include "butil/strings/string_util.h"
2829
#include "butil/sys_byteorder.h"
30+
#include "butil/time.h"
31+
#include "butil/unique_ptr.h" // std::unique_ptr
32+
2933
#include "json2pb/pb_to_json.h" // ProtoMessageToJson
3034
#include "json2pb/json_to_pb.h" // JsonToProtoMessage
3135
#include "brpc/compress.h"
@@ -284,7 +288,7 @@ static bool JsonToProtoMessage(const butil::IOBuf& body,
284288
bool ok = json2pb::JsonToProtoMessage(&wrapper, message, options, &error);
285289
if (!ok) {
286290
cntl->SetFailed(error_code, "Fail to parse http json body as %s: %s",
287-
message->GetDescriptor()->full_name().c_str(),
291+
butil::EnsureString(message->GetDescriptor()->full_name()).c_str(),
288292
error.c_str());
289293
}
290294
return ok;
@@ -305,7 +309,7 @@ static bool ProtoMessageToJson(const google::protobuf::Message& message,
305309
bool ok = json2pb::ProtoMessageToJson(message, wrapper, options, &error);
306310
if (!ok) {
307311
cntl->SetFailed(error_code, "Fail to convert %s to json: %s",
308-
message.GetDescriptor()->full_name().c_str(),
312+
butil::EnsureString(message.GetDescriptor()->full_name()).c_str(),
309313
error.c_str());
310314
}
311315
return ok;
@@ -321,7 +325,7 @@ static bool ProtoJsonToProtoMessage(const butil::IOBuf& body,
321325
bool ok = json2pb::ProtoJsonToProtoMessage(&wrapper, message, options, &error);
322326
if (!ok) {
323327
cntl->SetFailed(error_code, "Fail to parse http proto-json body as %s: %s",
324-
message->GetDescriptor()->full_name().c_str(),
328+
butil::EnsureString(message->GetDescriptor()->full_name()).c_str(),
325329
error.c_str());
326330
}
327331
return ok;
@@ -337,7 +341,7 @@ static bool ProtoMessageToProtoJson(const google::protobuf::Message& message,
337341
bool ok = json2pb::ProtoMessageToProtoJson(message, wrapper, options, &error);
338342
if (!ok) {
339343
cntl->SetFailed(error_code, "Fail to convert %s to proto-json: %s",
340-
message.GetDescriptor()->full_name().c_str(), error.c_str());
344+
butil::EnsureString(message.GetDescriptor()->full_name()).c_str(), error.c_str());
341345
}
342346
return ok;
343347
}
@@ -527,13 +531,13 @@ void ProcessHttpResponse(InputMessageBase* msg) {
527531
if (content_type == HTTP_CONTENT_PROTO) {
528532
if (!ParsePbFromIOBuf(cntl->response(), res_body)) {
529533
cntl->SetFailed(ERESPONSE, "Fail to parse content as %s",
530-
cntl->response()->GetDescriptor()->full_name().c_str());
534+
butil::EnsureString(cntl->response()->GetDescriptor()->full_name()).c_str());
531535
break;
532536
}
533537
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
534538
if (!ParsePbTextFromIOBuf(cntl->response(), res_body)) {
535539
cntl->SetFailed(ERESPONSE, "Fail to parse proto-text content as %s",
536-
cntl->response()->GetDescriptor()->full_name().c_str());
540+
butil::EnsureString(cntl->response()->GetDescriptor()->full_name()).c_str());
537541
break;
538542
}
539543
} else if (content_type == HTTP_CONTENT_JSON) {
@@ -612,13 +616,13 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
612616
if (!pbreq->SerializeToZeroCopyStream(&wrapper)) {
613617
cntl->request_attachment().clear();
614618
return cntl->SetFailed(EREQUEST, "Fail to serialize %s",
615-
pbreq->GetTypeName().c_str());
619+
butil::EnsureString(pbreq->GetTypeName()).c_str());
616620
}
617621
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
618622
if (!google::protobuf::TextFormat::Print(*pbreq, &wrapper)) {
619623
cntl->request_attachment().clear();
620624
return cntl->SetFailed(EREQUEST, "Fail to print %s as proto-text",
621-
pbreq->GetTypeName().c_str());
625+
butil::EnsureString(pbreq->GetTypeName()).c_str());
622626
}
623627
} else if (content_type == HTTP_CONTENT_PROTO_JSON) {
624628
if (!ProtoMessageToProtoJson(*pbreq, &wrapper, cntl, EREQUEST)) {
@@ -880,11 +884,13 @@ HttpResponseSender::~HttpResponseSender() {
880884
butil::IOBufAsZeroCopyOutputStream wrapper(&cntl->response_attachment());
881885
if (content_type == HTTP_CONTENT_PROTO) {
882886
if (!res->SerializeToZeroCopyStream(&wrapper)) {
883-
cntl->SetFailed(ERESPONSE, "Fail to serialize %s", res->GetTypeName().c_str());
887+
cntl->SetFailed(ERESPONSE, "Fail to serialize %s",
888+
butil::EnsureString(res->GetTypeName()).c_str());
884889
}
885890
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
886891
if (!google::protobuf::TextFormat::Print(*res, &wrapper)) {
887-
cntl->SetFailed(ERESPONSE, "Fail to print %s as proto-text", res->GetTypeName().c_str());
892+
cntl->SetFailed(ERESPONSE, "Fail to print %s as proto-text",
893+
butil::EnsureString(res->GetTypeName()).c_str());
888894
}
889895
} else if (content_type == HTTP_CONTENT_PROTO_JSON) {
890896
ProtoMessageToProtoJson(*res, &wrapper, cntl, ERESPONSE);
@@ -1535,7 +1541,7 @@ void ProcessHttpRequest(InputMessageBase *msg) {
15351541
cntl->request_attachment().swap(req_body);
15361542
google::protobuf::Closure* done = new HttpResponseSenderAsDone(&resp_sender);
15371543
if (span) {
1538-
span->ResetServerSpanName(md->full_name());
1544+
span->ResetServerSpanName(butil::EnsureString(md->full_name()));
15391545
span->set_start_callback_us(butil::cpuwide_time_us());
15401546
span->AsParent();
15411547
}
@@ -1565,18 +1571,19 @@ void ProcessHttpRequest(InputMessageBase *msg) {
15651571
// Switch to service-specific error.
15661572
non_service_error.release();
15671573
MethodStatus* method_status = mp->status;
1574+
const std::string method_full_name = butil::EnsureString(mp->method->full_name());
15681575
resp_sender.set_method_status(method_status);
15691576
if (method_status) {
15701577
int rejected_cc = 0;
15711578
if (!method_status->OnRequested(&rejected_cc)) {
15721579
cntl->SetFailed(ELIMIT, "Rejected by %s's ConcurrencyLimiter, concurrency=%d",
1573-
mp->method->full_name().c_str(), rejected_cc);
1580+
method_full_name.c_str(), rejected_cc);
15741581
return;
15751582
}
15761583
}
15771584

15781585
if (span) {
1579-
span->ResetServerSpanName(mp->method->full_name());
1586+
span->ResetServerSpanName(method_full_name);
15801587
}
15811588
// NOTE: accesses to builtin services are not counted as part of
15821589
// concurrency, therefore are not limited by ServerOptions.max_concurrency.
@@ -1616,6 +1623,8 @@ void ProcessHttpRequest(InputMessageBase *msg) {
16161623
google::protobuf::Message* req = messages->Request();
16171624
google::protobuf::Message* res = messages->Response();
16181625

1626+
const std::string request_full_name = butil::EnsureString(req->GetDescriptor()->full_name());
1627+
16191628
if (__builtin_expect(!req || !res, 0)) {
16201629
PLOG(FATAL) << "Fail to new req or res";
16211630
cntl->SetFailed("Fail to new req or res");
@@ -1632,7 +1641,7 @@ void ProcessHttpRequest(InputMessageBase *msg) {
16321641
if (!req->IsInitialized()) {
16331642
cntl->SetFailed(EREQUEST, "%s needs to be created from a"
16341643
" non-empty json, it has required fields.",
1635-
req->GetDescriptor()->full_name().c_str());
1644+
request_full_name.c_str());
16361645
return;
16371646
} // else all fields of the request are optional.
16381647
} else {
@@ -1677,13 +1686,13 @@ void ProcessHttpRequest(InputMessageBase *msg) {
16771686
if (content_type == HTTP_CONTENT_PROTO) {
16781687
if (!ParsePbFromIOBuf(req, req_body)) {
16791688
cntl->SetFailed(EREQUEST, "Fail to parse http body as %s",
1680-
req->GetDescriptor()->full_name().c_str());
1689+
request_full_name.c_str());
16811690
return;
16821691
}
16831692
} else if (content_type == HTTP_CONTENT_PROTO_TEXT) {
16841693
if (!ParsePbTextFromIOBuf(req, req_body)) {
16851694
cntl->SetFailed(EREQUEST, "Fail to parse http proto-text body as %s",
1686-
req->GetDescriptor()->full_name().c_str());
1695+
request_full_name.c_str());
16871696
return;
16881697
}
16891698
} else if (content_type == HTTP_CONTENT_PROTO_JSON) {

src/brpc/policy/hulu_pbrpc_protocol.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
#include <google/protobuf/message.h> // Message
2121
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
2222
#include <google/protobuf/io/coded_stream.h>
23+
24+
#include "butil/strings/string_util.h"
2325
#include "butil/time.h"
26+
2427
#include "brpc/controller.h" // Controller
2528
#include "brpc/socket.h" // Socket
2629
#include "brpc/server.h" // Server
@@ -469,25 +472,26 @@ void ProcessHuluRequest(InputMessageBase* msg_base) {
469472
// Switch to service-specific error.
470473
non_service_error.release();
471474
method_status = sp->status;
475+
const google::protobuf::MethodDescriptor* method = sp->method;
476+
const std::string method_full_name = butil::EnsureString(method->full_name());
472477
if (method_status) {
473478
int rejected_cc = 0;
474479
if (!method_status->OnRequested(&rejected_cc)) {
475480
cntl->SetFailed(ELIMIT, "Rejected by %s's ConcurrencyLimiter, concurrency=%d",
476-
sp->method->full_name().c_str(), rejected_cc);
481+
method_full_name.c_str(), rejected_cc);
477482
break;
478483
}
479484
}
480485

481486
google::protobuf::Service* svc = sp->service;
482-
const google::protobuf::MethodDescriptor* method = sp->method;
483487
accessor.set_method(method);
484488

485489
if (!server->AcceptRequest(cntl.get())) {
486490
break;
487491
}
488492

489493
if (span) {
490-
span->ResetServerSpanName(method->full_name());
494+
span->ResetServerSpanName(method_full_name);
491495
}
492496
const int reqsize = msg->payload.length();
493497
butil::IOBuf req_buf;

0 commit comments

Comments
 (0)