Skip to content

Commit

Permalink
Support proxy and generic call of baidu protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed May 1, 2024
1 parent bbd88a8 commit 544cb06
Show file tree
Hide file tree
Showing 13 changed files with 666 additions and 93 deletions.
52 changes: 52 additions & 0 deletions src/brpc/baidu_master_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 "brpc/baidu_master_service.h"
#include "butil/logging.h"

namespace brpc {

BaiduMasterService::BaiduMasterService()
:_status(new(std::nothrow) MethodStatus) {
LOG_IF(FATAL, NULL == _status) << "Fail to new MethodStatus";
}

BaiduMasterService::~BaiduMasterService() {
delete _status;
_status = NULL;
}

void BaiduMasterService::Describe(std::ostream &os,
const DescribeOptions&) const {
os << butil::class_name_str(*this);
}

void BaiduMasterService::Expose(const butil::StringPiece& prefix) {
if (NULL == _status) {
return;
}
std::string s;
const std::string& cached_name = butil::class_name_str(*this);
s.reserve(prefix.size() + 1 + cached_name.size());
s.append(prefix.data(), prefix.size());
s.push_back('_');
s.append(cached_name);
_status->Expose(s);
}

}
99 changes: 99 additions & 0 deletions src/brpc/baidu_master_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.


#ifndef BRPC_BAIDU_MASTER_SERVICE_H
#define BRPC_BAIDU_MASTER_SERVICE_H

#include <google/protobuf/service.h>
#include <google/protobuf/stubs/callback.h>
#include "brpc/details/method_status.h"
#include "brpc/proto_base.pb.h"
#include "brpc/controller.h"
#include "brpc/serialized_request.h"
#include "brpc/serialized_response.h"

namespace brpc {

namespace policy {
void ProcessRpcRequest(InputMessageBase* msg_base);
}

class BaiduMasterService : public ::google::protobuf::Service
, public Describable {
public:
BaiduMasterService();
~BaiduMasterService() override;

DISALLOW_COPY_AND_ASSIGN(BaiduMasterService);

AdaptiveMaxConcurrency& MaxConcurrency() {
return _max_concurrency;
}

virtual void ProcessRpcRequest(Controller* cntl,
const SerializedRequest* request,
SerializedResponse* response,
::google::protobuf::Closure* done) = 0;


// Put descriptions into the stream.
void Describe(std::ostream &os, const DescribeOptions&) const override;

// implements Service ----------------------------------------------

void CallMethod(const ::google::protobuf::MethodDescriptor*,
::google::protobuf::RpcController* controller,
const ::google::protobuf::Message* request,
::google::protobuf::Message* response,
::google::protobuf::Closure* done) override {
ProcessRpcRequest((Controller*)controller,
(const SerializedRequest*)request,
(SerializedResponse*)response, done);
}

const ::google::protobuf::ServiceDescriptor* GetDescriptor() override {
return BaiduMasterServiceBase::descriptor();
}

const ::google::protobuf::Message& GetRequestPrototype(
const ::google::protobuf::MethodDescriptor*) const override {
return _default_request;
}

const ::google::protobuf::Message& GetResponsePrototype(
const ::google::protobuf::MethodDescriptor*) const override {
return _default_response;
}

private:
friend void policy::ProcessRpcRequest(InputMessageBase* msg_base);
friend class StatusService;
friend class Server;

void Expose(const butil::StringPiece& prefix);

SerializedRequest _default_request;
SerializedResponse _default_response;

MethodStatus* _status;
AdaptiveMaxConcurrency _max_concurrency;
};

}

#endif //BRPC_BAIDU_MASTER_SERVICE_H
11 changes: 11 additions & 0 deletions src/brpc/builtin/status_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ void StatusService::default_method(::google::protobuf::RpcController* cntl_base,
}
}
}
const BaiduMasterService* baidu_master_service = server->options().baidu_master_service;
if (baidu_master_service && baidu_master_service->_status) {
DescribeOptions options;
options.verbose = false;
options.use_html = use_html;
os << (use_html ? "<h3>" : "[");
baidu_master_service->Describe(os, options);
os << (use_html ? "</h3>\n" : "]\n");
baidu_master_service->_status->Describe(os, desc_options);
os << '\n';
}
const NsheadService* nshead_svc = server->options().nshead_service;
if (nshead_svc && nshead_svc->_status) {
DescribeOptions options;
Expand Down
2 changes: 2 additions & 0 deletions src/brpc/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "brpc/details/load_balancer_with_naming.h"
#include "brpc/controller.h"
#include "brpc/channel.h"
#include "brpc/serialized_request.h"
#include "brpc/serialized_response.h"
#include "brpc/details/usercode_backup_pool.h" // TooManyUserCode
#include "brpc/rdma/rdma_helper.h"
#include "brpc/policy/esp_authenticator.h"
Expand Down
2 changes: 1 addition & 1 deletion src/brpc/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "brpc/progressive_reader.h" // ProgressiveReader
#include "brpc/grpc.h"
#include "brpc/kvmap.h"
#include "brpc/rpc_dump.h"

// EAUTH is defined in MAC
#ifndef EAUTH
Expand All @@ -68,7 +69,6 @@ class SharedLoadBalancer;
class ExcludedServers;
class RPCSender;
class StreamSettings;
class SampledRequest;
class MongoContext;
class RetryPolicy;
class InputMessageBase;
Expand Down
Loading

0 comments on commit 544cb06

Please sign in to comment.