Commit 888a763a authored by gejun's avatar gejun

replaced baidu::rpc:: with brpc::

parent c9627fc4
......@@ -163,7 +163,7 @@ class Variable {
$ cat bvar.echo_server.data
rpc_server_8002_builtin_service_count : 20
rpc_server_8002_connection_count : 1
rpc_server_8002_nshead_service_adaptor : baidu::rpc::policy::NovaServiceAdaptor
rpc_server_8002_nshead_service_adaptor : brpc::policy::NovaServiceAdaptor
rpc_server_8002_service_count : 1
rpc_server_8002_start_time : 2015/07/24-21:08:03
rpc_server_8002_uptime_ms : 14740954
......
This diff is collapsed.
......@@ -17,7 +17,7 @@ ParallelChannel (“pchan”)同时访问其包含的sub channel,并合并它
示例代码见[example/parallel_echo_c++](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/parallel_echo_c++/)
任何baidu::rpc::ChannelBase的子类都可以加入ParallelChannel,包括ParallelChannel和其他组合Channel。用户可以设置ParallelChannelOptions.fail_limit来控制访问的最大失败次数(r31803前是ParallelChannel::set_fail_limit),当失败的访问达到这个数目时,RPC call会立刻结束而不等待超时。
任何brpc::ChannelBase的子类都可以加入ParallelChannel,包括ParallelChannel和其他组合Channel。用户可以设置ParallelChannelOptions.fail_limit来控制访问的最大失败次数(r31803前是ParallelChannel::set_fail_limit),当失败的访问达到这个数目时,RPC call会立刻结束而不等待超时。
当baidu-rpc >= 1.0.155.31351时,一个sub channel可多次加入同一个ParallelChannel。当你需要对同一个服务发起多次异步访问并等待它们完成的话,这很有用。
......@@ -30,13 +30,13 @@ ParallelChannel的内部结构大致如下:
可通过如下接口把sub channel插入ParallelChannel:
```c++
int AddChannel(baidu::rpc::ChannelBase* sub_channel,
int AddChannel(brpc::ChannelBase* sub_channel,
ChannelOwnership ownership,
CallMapper* call_mapper,
ResponseMerger* response_merger);
```
当ownership为baidu::rpc::OWNS_CHANNEL时,sub_channel会在ParallelChannel析构时被删除。当baidu-rpc >= 1.0.155.31351时,由于一个sub channel可能会多次加入一个ParallelChannel,只要其中一个指明了ownership为baidu::rpc::OWNS_CHANNEL,那个sub channel就会在ParallelChannel析构时被删除(一次)。
当ownership为brpc::OWNS_CHANNEL时,sub_channel会在ParallelChannel析构时被删除。当baidu-rpc >= 1.0.155.31351时,由于一个sub channel可能会多次加入一个ParallelChannel,只要其中一个指明了ownership为brpc::OWNS_CHANNEL,那个sub channel就会在ParallelChannel析构时被删除(一次)。
访问ParallelChannel时调用AddChannel是线程**不安全**的。
......@@ -88,7 +88,7 @@ method/request/response:ParallelChannel.CallMethod()的参数。
const google::protobuf::MethodDescriptor* method,
const google::protobuf::Message* request,
google::protobuf::Message* response) {
FooRequest* copied_req = baidu::rpc::Clone<FooRequest>(request);
FooRequest* copied_req = brpc::Clone<FooRequest>(request);
copied_req->set_xxx(...);
// 拷贝并修改request,最后的flag告诉pchan在RPC结束后删除Request和Response。
return SubCall(method, copied_req, response->New(), DELETE_REQUEST | DELETE_RESPONSE);
......@@ -157,7 +157,7 @@ const Controller* sub(int index) const;
示例代码见[example/selective_echo_c++](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/selective_echo_c++/)
任何baidu::rpc::ChannelBase的子类都可加入SelectiveChannel,包括SelectiveChannel和其他组合Channel。
任何brpc::ChannelBase的子类都可加入SelectiveChannel,包括SelectiveChannel和其他组合Channel。
SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访问某个sub channel失败时(可能本身包含了重试),它会重试另外一个sub channel。
......@@ -168,10 +168,10 @@ SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访
SelectiveChannel的初始化和普通Channel基本一样,但Init不需要指定名字服务,因为SelectiveChannel面向sub channel并通过AddChannel动态添加,而普通Channel面向的server才记录在名字服务中。
```c++
#include <baidu/rpc/selective_channel.h>
#include brpc/selective_channel.h>
...
baidu::rpc::SelectiveChannel schan;
baidu::rpc::ChannelOptions schan_options;
brpc::SelectiveChannel schan;
brpc::ChannelOptions schan_options;
schan_options.timeout_ms = ...;
schan_options.backup_request_ms = ...;
schan_options.max_retry = ...;
......@@ -194,7 +194,7 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
- 和ParallelChannel不同,SelectiveChannel的AddChannel可在任意时刻调用,即使该SelectiveChannel正在被访问(下一次访问时生效)
- SelectiveChannel总是own sub channel,这和ParallelChannel可选择ownership是不同的。
- 如果AddChannel第二个参数不为空,会填入一个类型为baidu::rpc::SelectiveChannel::ChannelHandle的值,这个handle可作为RemoveAndDestroyChannel的参数来动态删除一个channel。
- 如果AddChannel第二个参数不为空,会填入一个类型为brpc::SelectiveChannel::ChannelHandle的值,这个handle可作为RemoveAndDestroyChannel的参数来动态删除一个channel。
- SelectiveChannel会用自身的超时覆盖sub channel初始化时指定的超时。比如某个sub channel的超时为100ms,SelectiveChannel的超时为500ms,实际访问时的超时是500ms,而不是100ms。
访问SelectiveChannel的方式和普通Channel是一样的。
......@@ -211,8 +211,8 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
SelectiveChannel的创建和普通Channel类似,但不需要名字服务,而是通过AddChannel方法插入sub channel。下面的代码创建了一个SelectiveChannel,并插入三个访问不同bns的普通Channel。
```c++
baidu::rpc::SelectiveChannel channel;
baidu::rpc::ChannelOptions schan_options;
brpc::SelectiveChannel channel;
brpc::ChannelOptions schan_options;
schan_options.timeout_ms = FLAGS_timeout_ms;
schan_options.backup_request_ms = FLAGS_backup_ms;
schan_options.max_retry = FLAGS_max_retry;
......@@ -222,7 +222,7 @@ if (channel.Init("c_murmurhash", &schan_options) != 0) {
}
for (int i = 0; i < 3; ++i) {
baidu::rpc::Channel* sub_channel = new baidu::rpc::Channel;
brpc::Channel* sub_channel = new brpc::Channel;
if (sub_channel->Init(bns_node_name[i], "rr", NULL) != 0) {
LOG(ERROR) << "Fail to init sub channel " << i;
return -1;
......@@ -251,11 +251,11 @@ ParititonChannel只能处理一种分库方法,当用户需要多种分库方
首先定制PartitionParser。这个例子中tag的形式是N/M,N代表分库的index,M是分库的个数。比如0/3代表一共3个分库,这是第一个。
```c++
#include <baidu/rpc/partition_channel.h>
#include brpc/partition_channel.h>
...
class MyPartitionParser : public baidu::rpc::PartitionParser {
class MyPartitionParser : public brpc::PartitionParser {
public:
bool ParseFromTag(const std::string& tag, baidu::rpc::Partition* out) {
bool ParseFromTag(const std::string& tag, brpc::Partition* out) {
// "N/M" : #N partition of M partitions.
size_t pos = tag.find_first_of('/');
if (pos == std::string::npos) {
......@@ -281,11 +281,11 @@ public:
然后初始化PartitionChannel。
```c++
#include <baidu/rpc/partition_channel.h>
#include brpc/partition_channel.h>
...
baidu::rpc::PartitionChannel channel;
brpc::PartitionChannel channel;
baidu::rpc::PartitionChannelOptions options;
brpc::PartitionChannelOptions options;
options.protocol = ...; // PartitionChannelOptions继承了ChannelOptions,后者有的前者也有
options.timeout_ms = ...; // 同上
options.fail_limit = 1; // PartitionChannel自己的选项,意思同ParalellChannel中的fail_limit。这里为1的意思是只要有1个分库访问失败,这次RPC就失败了。
......@@ -320,8 +320,8 @@ TRACE: 09-06 10:40:42: * 0 server.cpp:192] S[0]=0 S[1]=0 S[2]=0 [total=0]
```c++
...
baidu::rpc::DynamicPartitionChannel channel;
baidu::rpc::PartitionChannelOptions options;
brpc::DynamicPartitionChannel channel;
brpc::PartitionChannelOptions options;
options.succeed_without_server = true; // 表示允许server_list在DynamicPartitionChannel.Init启动时为空,否则Init会失败。
options.fail_limit = 1; // 任何访问分库失败都认为RPC失败。调大这个数值可以使访问更宽松,比如等于2的话表示至少两个分库失败才算失败。
if (channel.Init(new MyPartitionParser(), "file://server_list", "rr", &options) != 0) {
......
......@@ -23,7 +23,7 @@ channel_short_socket_count: 0
上述信息分为三段:
- 第一段是server接受(accept)的连接。
- 第二段是server与下游的单连接(使用baidu::rpc::Channel建立),fd为-1的是虚拟连接,对应第三段中所有相同RemoteSide的连接。
- 第二段是server与下游的单连接(使用brpc::Channel建立),fd为-1的是虚拟连接,对应第三段中所有相同RemoteSide的连接。
- 第三段是server与下游的短连接或连接池(pooled connections),这些连接从属于第二段中的相同RemoteSide的虚拟连接。
表格标题的含义:
......
......@@ -51,20 +51,20 @@ Wrote profile to /home/gejun/pprof/echo_server.1419501210.0.0.0.0
Removing funlockfile from all stack traces.
Total: 2946 samples
1161 39.4% 39.4% 1161 39.4% syscall
248 8.4% 47.8% 248 8.4% baidu::bthread::TaskControl::steal_task
248 8.4% 47.8% 248 8.4% bthread::TaskControl::steal_task
227 7.7% 55.5% 227 7.7% writev
87 3.0% 58.5% 88 3.0% ::cpp_alloc
74 2.5% 61.0% 74 2.5% __read_nocancel
46 1.6% 62.6% 48 1.6% tc_delete
42 1.4% 64.0% 42 1.4% baidu::rpc::Socket::Address
42 1.4% 64.0% 42 1.4% brpc::Socket::Address
41 1.4% 65.4% 41 1.4% epoll_wait
35 1.2% 66.6% 35 1.2% memcpy
33 1.1% 67.7% 33 1.1% __pthread_getspecific
33 1.1% 68.8% 33 1.1% baidu::rpc::Socket::Write
33 1.1% 68.8% 33 1.1% brpc::Socket::Write
33 1.1% 69.9% 33 1.1% epoll_ctl
28 1.0% 70.9% 42 1.4% baidu::rpc::policy::ProcessRpcRequest
27 0.9% 71.8% 27 0.9% baidu::IOBuf::_push_back_ref
27 0.9% 72.7% 27 0.9% baidu::bthread::TaskGroup::ending_sched
28 1.0% 70.9% 42 1.4% brpc::policy::ProcessRpcRequest
27 0.9% 71.8% 27 0.9% base::IOBuf::_push_back_ref
27 0.9% 72.7% 27 0.9% bthread::TaskGroup::ending_sched
```
省略–text进入交互模式,如下图所示:
......@@ -80,14 +80,14 @@ Welcome to pprof! For help, type 'help'.
(pprof) top
Total: 2954 samples
1099 37.2% 37.2% 1099 37.2% syscall
253 8.6% 45.8% 253 8.6% baidu::bthread::TaskControl::steal_task
253 8.6% 45.8% 253 8.6% bthread::TaskControl::steal_task
240 8.1% 53.9% 240 8.1% writev
90 3.0% 56.9% 90 3.0% ::cpp_alloc
67 2.3% 59.2% 67 2.3% __read_nocancel
47 1.6% 60.8% 47 1.6% baidu::IOBuf::_push_back_ref
42 1.4% 62.2% 56 1.9% baidu::rpc::policy::ProcessRpcRequest
47 1.6% 60.8% 47 1.6% base::IOBuf::_push_back_ref
42 1.4% 62.2% 56 1.9% brpc::policy::ProcessRpcRequest
41 1.4% 63.6% 41 1.4% epoll_wait
38 1.3% 64.9% 38 1.3% epoll_ctl
37 1.3% 66.1% 37 1.3% memcpy
35 1.2% 67.3% 35 1.2% baidu::rpc::Socket::Address
35 1.2% 67.3% 35 1.2% brpc::Socket::Address
```
......@@ -12,14 +12,14 @@
你必须手动加入dummy server。你得先查看[Getting Started](getting_started.md)如何下载和编译baidu-rpc,然后在程序入口处加入如下代码片段:
```c++
#include <baidu/rpc/server.h>
#include brpc/server.h>
...
int main() {
...
baidu::rpc::Server dummy_server;
baidu::rpc::ServerOptions dummy_server_options;
brpc::Server dummy_server;
brpc::ServerOptions dummy_server_options;
dummy_server_options.num_threads = 0; // 不要改变寄主程序的线程数。
if (dummy_server.Start(8888/*port*/, &dummy_server_options) != 0) {
LOG(FATAL) << "Fail to start dummy server";
......@@ -32,13 +32,13 @@ int main() {
r31803之后加入dummy server更容易了,只要一行:
```c++
#include <baidu/rpc/server.h>
#include brpc/server.h>
...
int main() {
...
baidu::rpc::StartDummyServerAt(8888/*port*/);
brpc::StartDummyServerAt(8888/*port*/);
...
}
```
baidu-rpc使用[baidu::rpc::Controller](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/controller.h)设置一次RPC的参数和获取一次RPC的结果,ErrorCode()和ErrorText()是Controller的两个方法,分别是该次RPC的错误码和错误描述,只在RPC结束后才能访问,否则结果未定义。ErrorText()由Controller的基类google::protobuf::RpcController定义,ErrorCode()则是baidu::rpc::Controller定义的。Controller还有个Failed()方法告知该次RPC是否失败,这三者的关系是:
baidu-rpc使用[brpc::Controller](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/controller.h)设置一次RPC的参数和获取一次RPC的结果,ErrorCode()和ErrorText()是Controller的两个方法,分别是该次RPC的错误码和错误描述,只在RPC结束后才能访问,否则结果未定义。ErrorText()由Controller的基类google::protobuf::RpcController定义,ErrorCode()则是brpc::Controller定义的。Controller还有个Failed()方法告知该次RPC是否失败,这三者的关系是:
- 当Failed()为true时,ErrorCode()一定不为0,ErrorText()是非空的错误描述
- 当Failed()为false时,ErrorCode()一定为0,ErrorText()是未定义的(目前在baidu-rpc中会为空,但你最好不要依赖这个事实)
......
......@@ -117,7 +117,7 @@ r31658之后支持可视化地修改,在浏览器上访问时将看到(R)下
```c++
DEFINE_bool(hex_log_id, false, "Show log_id in hexadecimal");
BAIDU_RPC_VALIDATE_GFLAG(hex_log_id, baidu::rpc::PassValidate/*always true*/);
BAIDU_RPC_VALIDATE_GFLAG(hex_log_id, brpc::PassValidate/*always true*/);
```
这个flag是单纯的开关,修改后不需要更新其他数据(没有处理逻辑),代码中前面看到true后面看到false也不会产生什么后果(不需要线程同步),所以我们让其默认可重载。
......@@ -126,7 +126,7 @@ BAIDU_RPC_VALIDATE_GFLAG(hex_log_id, baidu::rpc::PassValidate/*always true*/);
```c++
DEFINE_int32(health_check_interval, 3, "seconds between consecutive health-checkings");
BAIDU_RPC_VALIDATE_GFLAG(health_check_interval, baidu::rpc::PositiveInteger);
BAIDU_RPC_VALIDATE_GFLAG(health_check_interval, brpc::PositiveInteger);
```
以上操作都可以在命令行中进行:
......
......@@ -97,4 +97,4 @@ baidu-rpc detects valgrind automatically (and registers stacks of bthread). Olde
# Track instances
We provide a program to help you to track and monitor all baidu-rpc instances. Just run [trackme_server](tools/trackme_server/trackme_server.cpp) somewhere and launch need-to-be-tracked instances with -trackme_server=<SERVER>. The trackme_server will receive pings from instance periodically and print logs when it does. You can aggregate instance addresses from the log and call builtin services of the instances for further information.
\ No newline at end of file
We provide a program to help you to track and monitor all baidu-rpc instances. Just run [trackme_server](tools/trackme_server/trackme_server.cpp) somewhere and launch need-to-be-tracked instances with -trackme_server=<SERVER>. The trackme_server will receive pings from instance periodically and print logs when it does. You can aggregate instance addresses from the log and call builtin services of the instances for further information.
......@@ -74,13 +74,13 @@ Total: 38.9 MB
0.0 0.0% 100.0% 0.5 1.3% __do_global_ctors_aux
0.0 0.0% 100.0% 1.6 4.2% _end
0.0 0.0% 100.0% 0.5 1.3% _init
0.0 0.0% 100.0% 0.6 1.5% baidu::rpc::CloseIdleConnections
0.0 0.0% 100.0% 1.1 2.9% baidu::rpc::GlobalUpdate
0.0 0.0% 100.0% 0.6 1.5% baidu::rpc::PProfService::heap
0.0 0.0% 100.0% 1.9 4.9% baidu::rpc::Socket::Create
0.0 0.0% 100.0% 2.9 7.4% baidu::rpc::Socket::Write
0.0 0.0% 100.0% 3.8 9.7% baidu::rpc::Span::CreateServerSpan
0.0 0.0% 100.0% 1.4 3.5% baidu::rpc::SpanQueue::Push
0.0 0.0% 100.0% 0.6 1.5% brpc::CloseIdleConnections
0.0 0.0% 100.0% 1.1 2.9% brpc::GlobalUpdate
0.0 0.0% 100.0% 0.6 1.5% brpc::PProfService::heap
0.0 0.0% 100.0% 1.9 4.9% brpc::Socket::Create
0.0 0.0% 100.0% 2.9 7.4% brpc::Socket::Write
0.0 0.0% 100.0% 3.8 9.7% brpc::Span::CreateServerSpan
0.0 0.0% 100.0% 1.4 3.5% brpc::SpanQueue::Push
0.0 0.0% 100.0% 1.9 4.8% base::ObjectPool
0.0 0.0% 100.0% 0.8 2.0% base::ResourcePool
0.0 0.0% 100.0% 1.0 2.6% base::iobuf::tls_block
......
......@@ -2,13 +2,13 @@ http client的例子见[example/http_c++](http://icode.baidu.com/repo/baidu/open
# 创建Channel
baidu::rpc::Channel可访问HTTP服务,ChannelOptions.protocol须指定为PROTOCOL_HTTP。
brpc::Channel可访问HTTP服务,ChannelOptions.protocol须指定为PROTOCOL_HTTP。
设定为HTTP协议后,`Channel::Init`的第一个参数可为任意合法的URL。注意:允许任意URL是为了省去用户取出host和port的麻烦,`Channel::Init`只用其中的host及port,其他部分都会丢弃。
```c++
baidu::rpc::ChannelOptions options;
options.protocol = baidu::rpc::PROTOCOL_HTTP;
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_HTTP;
if (channel.Init("www.baidu.com" /*any url*/, &options) != 0) {
LOG(ERROR) << "Fail to initialize channel";
return -1;
......@@ -20,7 +20,7 @@ http channel也支持bns地址。
# GET
```c++
baidu::rpc::Controller cntl;
brpc::Controller cntl;
cntl.http_request().uri() = "www.baidu.com/index.html"; // 设置为待访问的URL
channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
```
......@@ -34,9 +34,9 @@ HTTP和protobuf无关,所以除了Controller和done,CallMethod的其他参
默认的HTTP Method为GET,如果需要做POST,则需要设置。待POST的数据应置入request_attachment(),它([base::IOBuf](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/base/iobuf.h))可以直接append std::string或char*
```c++
baidu::rpc::Controller cntl;
brpc::Controller cntl;
cntl.http_request().uri() = "..."; // 设置为待访问的URL
cntl.http_request().set_method(baidu::rpc::HTTP_METHOD_POST);
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.request_attachment().append("{\"message\":\"hello world!\"}");
channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
```
......@@ -44,9 +44,9 @@ channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
需要大量打印过程的body建议使用base::IOBufBuilder,它的用法和std::ostringstream是一样的。对于有大量对象要打印的场景,IOBufBuilder会简化代码,并且效率也更高。
```c++
baidu::rpc::Controller cntl;
brpc::Controller cntl;
cntl.http_request().uri() = "..."; // 设置为待访问的URL
cntl.http_request().set_method(baidu::rpc::HTTP_METHOD_POST);
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
base::IOBufBuilder os;
os << "A lot of printing" << printable_objects << ...;
os.move_to(cntl.request_attachment());
......@@ -108,7 +108,7 @@ cntl->http_request().uri().SetQuery("Foo", "value");
```
设置HTTP方法
```c++
cntl->http_request().set_method(baidu::rpc::HTTP_METHOD_POST);
cntl->http_request().set_method(brpc::HTTP_METHOD_POST);
```
设置url
```c++
......@@ -149,19 +149,19 @@ Notes on http header:
# 压缩request body
在r33877后,调用Controller::set_request_compress_type(baidu::rpc::COMPRESS_TYPE_GZIP)可将http body用gzip压缩,并设置"Content-Encoding"为"gzip"。
在r33877后,调用Controller::set_request_compress_type(brpc::COMPRESS_TYPE_GZIP)可将http body用gzip压缩,并设置"Content-Encoding"为"gzip"。
# 解压response body
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压response body,用户可以自己做,方法如下:
```c++
#include <baidu/rpc/policy/gzip_compress.h>
#include brpc/policy/gzip_compress.h>
...
const std::string* encoding = cntl->http_response().GetHeader("Content-Encoding");
if (encoding != NULL && *encoding == "gzip") {
base::IOBuf uncompressed;
if (!baidu::rpc::policy::GzipDecompress(cntl->response_attachment(), &uncompressed)) {
if (!brpc::policy::GzipDecompress(cntl->response_attachment(), &uncompressed)) {
LOG(ERROR) << "Fail to un-gzip response body";
return;
}
......@@ -181,7 +181,7 @@ r33796后baidu-rpc client支持在读取完body前就结束RPC,让用户在RPC
1. 首先实现ProgressiveReader,接口如下:
```c++
#include <baidu/rpc/progressive_reader.h>
#include brpc/progressive_reader.h>
...
class ProgressiveReader {
public:
......
......@@ -29,8 +29,8 @@ public:
                      const HttpRequest* /*request*/,
                      HttpResponse* /*response*/,
                      google::protobuf::Closure* done) {
        baidu::rpc::ClosureGuard done_guard(done);
        baidu::rpc::Controller* cntl = static_cast<baidu::rpc::Controller*>(cntl_base);
        brpc::ClosureGuard done_guard(done);
        brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
 
        // 这里返回纯文本。
        cntl->http_response().set_content_type("text/plain");
......@@ -38,7 +38,7 @@ public:
        // 把请求的query-string和body打印出来,作为回复内容。
        base::IOBufBuilder os;
        os << "queries:";
        for (baidu::rpc::URI::QueryIterator it = cntl->http_request().uri().QueryBegin();
        for (brpc::URI::QueryIterator it = cntl->http_request().uri().QueryBegin();
                it != cntl->http_request().uri().QueryEnd(); ++it) {
            os << ' ' << it->first << '=' << it->second;
        }
......@@ -87,8 +87,8 @@ public:
const HttpRequest* /*request*/,
HttpResponse* /*response*/,
google::protobuf::Closure* done) {
baidu::rpc::ClosureGuard done_guard(done);
baidu::rpc::Controller* cntl = static_cast<baidu::rpc::Controller*>(cntl_base);
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
cntl->response_attachment().append("Getting file: ");
cntl->response_attachment().append(cntl->http_request().unresolved_path());
}
......@@ -135,7 +135,7 @@ service QueueService {
```c++
// r33521前星号只能出现在最后
if (server.AddService(&queue_svc,
                      baidu::rpc::SERVER_DOESNT_OWN_SERVICE,
                      brpc::SERVER_DOESNT_OWN_SERVICE,
                      "/v1/queue/start   => start,"
                      "/v1/queue/stop    => stop,"
                      "/v1/queue/stats/* => get_stats") != 0) {
......@@ -145,7 +145,7 @@ if (server.AddService(&queue_svc,
 
// r33521后星号可出现在中间
if (server.AddService(&queue_svc,
                      baidu::rpc::SERVER_DOESNT_OWN_SERVICE,
                      brpc::SERVER_DOESNT_OWN_SERVICE,
                      "/v1/*/start   => start,"
                      "/v1/*/stop    => stop,"
                      "*.data        => download_data") != 0) {
......@@ -230,19 +230,19 @@ status code是http response特有的字段,标记http请求的完成情况。
```c++
// Get Status Code
if (cntl->http_response().status_code() == baidu::rpc::HTTP_STATUS_NOT_FOUND) {
if (cntl->http_response().status_code() == brpc::HTTP_STATUS_NOT_FOUND) {
    LOG(FATAL) << "FAILED: " << controller.http_response().reason_phrase();
}
...
// Set Status code
cntl->http_response().set_status_code(baidu::rpc::HTTP_STATUS_INTERNAL_SERVER_ERROR);
cntl->http_response().set_status_code(baidu::rpc::HTTP_STATUS_INTERNAL_SERVER_ERROR, "My explanation of the error...");
cntl->http_response().set_status_code(brpc::HTTP_STATUS_INTERNAL_SERVER_ERROR);
cntl->http_response().set_status_code(brpc::HTTP_STATUS_INTERNAL_SERVER_ERROR, "My explanation of the error...");
```
以下代码在302错误时重定向:
```c++
cntl->http_response().set_status_code(baidu::rpc::HTTP_STATUS_FOUND);
cntl->http_response().set_status_code(brpc::HTTP_STATUS_FOUND);
cntl->http_response().SetHeader("Location", "http://bj.bs.bae.baidu.com/family/image001(4979).jpg");
```
......@@ -270,19 +270,19 @@ cntl->http_request().uri().SetQuery("time", "2015/1/2");
http服务常对http body进行压缩,对于文本网页可以有效减少传输时间,加快页面的展现速度。
在r33093后,调用Controller::set_response_compress_type(baidu::rpc::COMPRESS_TYPE_GZIP)将尝试用gzip压缩http body,并设置"Content-Encoding"为"gzip"。“尝试”指的是如果请求中没有设置Accept-encoding或不包含gzip,压缩不会进行。比如curl不加--compressed时是不支持压缩的,这时server端总是会返回不压缩的结果。
在r33093后,调用Controller::set_response_compress_type(brpc::COMPRESS_TYPE_GZIP)将尝试用gzip压缩http body,并设置"Content-Encoding"为"gzip"。“尝试”指的是如果请求中没有设置Accept-encoding或不包含gzip,压缩不会进行。比如curl不加--compressed时是不支持压缩的,这时server端总是会返回不压缩的结果。
# 解压request body
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压request body,用户可以自己做,方法如下:
```c++
#include <baidu/rpc/policy/gzip_compress.h>
#include brpc/policy/gzip_compress.h>
...
const std::string* encoding = cntl->http_request().GetHeader("Content-Encoding");
if (encoding != NULL && *encoding == "gzip") {
    base::IOBuf uncompressed;
    if (!baidu::rpc::policy::GzipDecompress(cntl->request_attachment(), &uncompressed)) {
    if (!brpc::policy::GzipDecompress(cntl->request_attachment(), &uncompressed)) {
        LOG(ERROR) << "Fail to un-gzip request body";
        return;
    }
......@@ -351,13 +351,13 @@ r33796前baidu-rpc server不适合发送超大或无限长的body。r33796后bai
\1. 调用Controller::CreateProgressiveAttachment()创建可持续发送的body。
`boost::intrusive_ptr<baidu::rpc::ProgressiveAttachment> pa(cntl->CreateProgressiveAttachment());`
`boost::intrusive_ptr<brpc::ProgressiveAttachment> pa(cntl->CreateProgressiveAttachment());`
返回的ProgressiveAttachment对象需要用boost::intrusive_ptr<>管理,定义在<baidu/rpc/progressive_attachment.h>中。
返回的ProgressiveAttachment对象需要用boost::intrusive_ptr<>管理,定义在brpc/progressive_attachment.h>中。
\2. 调用ProgressiveAttachment::Write()发送数据。如果写入发生在server回调结束前,发送的数据将会被缓存直到回调结束发送了header部分后才会开始发送数据。如果写入发生在server回调结束后,发送的数据将立刻以chunked mode写出。
\3. 发送完毕后确保所有的boost::intrusive_ptr<baidu::rpc::ProgressiveAttachment>都析构了。
\3. 发送完毕后确保所有的boost::intrusive_ptr<brpc::ProgressiveAttachment>都析构了。
# 持续接收
......
......@@ -16,11 +16,11 @@
创建一个访问memcached的Channel:
```c++
#include <baidu/rpc/memcache.h>
#include <baidu/rpc/channel.h>
#include brpc/memcache.h>
#include brpc/channel.h>
ChannelOptions options;
options.protocol = baidu::rpc::PROTOCOL_MEMCACHE;
options.protocol = brpc::PROTOCOL_MEMCACHE;
if (channel.Init("0.0.0.0:11211", &options) != 0) { // 11211是memcached的默认端口
LOG(FATAL) << "Fail to init channel to memcached";
return -1;
......@@ -34,9 +34,9 @@ if (channel.Init("0.0.0.0:11211", &options) != 0) { // 11211是memcached的默
```c++
// 写入key="hello" value="world" flags=0xdeadbeef,10秒失效,无视cas。
baidu::rpc::MemcacheRequest request;
baidu::rpc::MemcacheResponse response;
baidu::rpc::Controller cntl;
brpc::MemcacheRequest request;
brpc::MemcacheResponse response;
brpc::Controller cntl;
if (!request.Set("hello", "world", 0xdeadbeef/*flags*/, 10/*expiring seconds*/, 0/*ignore cas*/)) {
LOG(FATAL) << "Fail to SET request";
return -1;
......
......@@ -85,8 +85,8 @@ public:
const EchoRequest* request,
EchoResponse* response,
google::protobuf::Closure* done) {
baidu::rpc::ClosureGuard done_guard(done);
baidu::rpc::Controller* cntl = static_cast<baidu::rpc::Controller*>(cntl_base);
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
// 填充response。
response->set_message(request->message());
......@@ -99,15 +99,15 @@ public:
const MultiRequests* request,
MultiResponses* response,
google::protobuf::Closure* done) {
baidu::rpc::ClosureGuard done_guard(done);
baidu::rpc::Controller* cntl = static_cast<baidu::rpc::Controller*>(cntl_base);
brpc::ClosureGuard done_guard(done);
brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
// 填充response。response是我们定义的包含所有idl response的消息。
response->mutable_res1()->set_message(request->req1().message());
response->mutable_res2()->set_message(request->req2().message());
// 告诉RPC有多个request和response。
cntl->set_idl_names(baidu::rpc::idl_multi_req_multi_res);
cntl->set_idl_names(brpc::idl_multi_req_multi_res);
// 对应idl方法的返回值。
cntl->set_idl_result(17);
......@@ -118,10 +118,10 @@ public:
## 设置ServerOptions.nshead_service
```c++
#include <baidu/rpc/ubrpc2pb_protocol.h>
#include brpc/ubrpc2pb_protocol.h>
...
baidu::rpc::ServerOptions option;
option.nshead_service = new baidu::rpc::policy::UbrpcCompackAdaptor; // mcpack2用UbrpcMcpack2Adaptor
brpc::ServerOptions option;
option.nshead_service = new brpc::policy::UbrpcCompackAdaptor; // mcpack2用UbrpcMcpack2Adaptor
```
例子见[example/echo_c++_ubrpc_compack](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/echo_c++_ubrpc_compack/)
......@@ -147,15 +147,15 @@ public:
virtual ~NsheadService();
// 实现这个方法来处理nshead请求。注意这个方法可能在调用时controller->Failed()已经为true了。
// 原因可能是Server.Stop()被调用正在退出(错误码是baidu::rpc::ELOGOFF)
// 或触发了ServerOptions.max_concurrency(错误码是baidu::rpc::ELIMIT)
// 原因可能是Server.Stop()被调用正在退出(错误码是brpc::ELOGOFF)
// 或触发了ServerOptions.max_concurrency(错误码是brpc::ELIMIT)
// 在这种情况下,这个方法应该通过返回一个代表错误的response让客户端知道这些错误。
// Parameters:
// server The server receiving the request.
// controller Contexts of the request.
// request The nshead request received.
// response The nshead response that you should fill in.
// done You must call done->Run() to end the processing, baidu::rpc::ClosureGuard is preferred.
// done You must call done->Run() to end the processing, brpc::ClosureGuard is preferred.
virtual void ProcessNsheadRequest(const Server& server,
Controller* controller,
const NsheadMessage& request,
......
......@@ -16,12 +16,12 @@ r32037后加上[-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose;r
创建一个访问redis的Channel:
```c++
#include <baidu/rpc/redis.h>
#include <baidu/rpc/channel.h>
#include brpc/redis.h>
#include brpc/channel.h>
baidu::rpc::ChannelOptions options;
options.protocol = baidu::rpc::PROTOCOL_REDIS;
baidu::rpc::Channel redis_channel;
brpc::ChannelOptions options;
options.protocol = brpc::PROTOCOL_REDIS;
brpc::Channel redis_channel;
if (redis_channel.Init("0.0.0.0:6379", &options) != 0) { // 6379是redis-server的默认端口
LOG(ERROR) << "Fail to init channel to redis-server";
return -1;
......@@ -36,9 +36,9 @@ std::string my_key = "my_key_1";
int my_number = 1;
...
// 执行"SET <my_key> <my_number>"
baidu::rpc::RedisRequest set_request;
baidu::rpc::RedisResponse response;
baidu::rpc::Controller cntl;
brpc::RedisRequest set_request;
brpc::RedisResponse response;
brpc::Controller cntl;
set_request.AddCommand("SET %s %d", my_key.c_str(), my_number);
redis_channel.CallMethod(NULL, &cntl, &set_request, &response, NULL/*done*/);
if (cntl.Failed()) {
......@@ -56,7 +56,7 @@ LOG(INFO) << response.reply(0).c_str() // OK
...
// 执行"INCR <my_key>"
baidu::rpc::RedisRequest incr_request;
brpc::RedisRequest incr_request;
incr_request.AddCommand("INCR %s", my_key.c_str());
response.Clear();
cntl.Reset();
......@@ -78,9 +78,9 @@ LOG(INFO) << response.reply(0).integer() // 2
批量执行incr或decr
```c++
baidu::rpc::RedisRequest request;
baidu::rpc::RedisResponse response;
baidu::rpc::Controller cntl;
brpc::RedisRequest request;
brpc::RedisResponse response;
brpc::Controller cntl;
request.AddCommand("INCR counter1");
request.AddCommand("DECR counter1");
request.AddCommand("INCRBY counter1 10");
......@@ -93,7 +93,7 @@ if (cntl.Failed()) {
CHECK_EQ(4, response.reply_size());
for (int i = 0; i < 4; ++i) {
CHECK(response.reply(i).is_integer());
CHECK_EQ(baidu::rpc::REDIS_REPLY_INTEGER, response.reply(i).type());
CHECK_EQ(brpc::REDIS_REPLY_INTEGER, response.reply(i).type());
}
CHECK_EQ(1, response.reply(0).integer());
CHECK_EQ(0, response.reply(1).integer());
......
......@@ -56,12 +56,12 @@ serialized request (body_size - meta_size bytes, including attachment)
baidu-rpc提供了[SampleIterator](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/rpc_dump.h)从一个采样目录下的所有文件中依次读取所有的被采样请求,用户可根据需求把serialized request反序列化为protobuf请求,做一些二次开发。
```
#include <baidu/rpc/rpc_dump.h>
#include brpc/rpc_dump.h>
...
baidu::rpc::SampleIterator it("./rpc_data/rpc_dump/echo_server");
brpc::SampleIterator it("./rpc_data/rpc_dump/echo_server");
for (SampleRequest* req = it->Next(); req != NULL; req = it->Next()) {
...
// req->meta的类型是baidu::rpc::RpcDumpMeta,定义在protocol/baidu/rpc/rpc_dump.proto
// req->meta的类型是brpc::RpcDumpMeta,定义在protocol/baidu/rpc/rpc_dump.proto
// req->request的类型是base::IOBuf,对应格式说明中的"serialized request"
// 使用结束后必须delete req。
}
......
This diff is collapsed.
......@@ -18,13 +18,13 @@
- **processing**: 正在处理的请求个数。如果持续不为0(特别是在压力归0后),应考虑程序是否有bug。
用户可通过让对应Service实现[baidu::rpc::Describable](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/describable.h)自定义在/status页面上的描述.
用户可通过让对应Service实现[brpc::Describable](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/describable.h)自定义在/status页面上的描述.
```c++
class MyService : public XXXService, public baidu::rpc::Describable {
class MyService : public XXXService, public brpc::Describable {
public:
...
void DescribeStatus(std::ostream& os, const baidu::rpc::DescribeOptions& options) const {
void DescribeStatus(std::ostream& os, const brpc::DescribeOptions& options) const {
os << "my_status: blahblah";
}
};
......
This diff is collapsed.
[public/bvar](../src/bvar/)是多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值,它利用了thread local存储避免了cache bouncing,相比UbMonitor几乎不会给程序增加性能开销,也快于竞争频繁的原子操作。baidu-rpc集成了bvar,[/vars](http://brpc.baidu.com:8765/vars)可查看所有曝光的bvar,[/vars/VARNAME](http://brpc.baidu.com:8765/vars/rpc_socket_count)可查阅某个bvar,增加计数器的方法请查看[bvar](http://wiki.baidu.com/display/RPC/bvar)。baidu-rpc大量使用了bvar提供统计数值,当你需要在多线程环境中计数并展现时,应该第一时间想到bvar。但bvar不能代替所有的计数器,它的本质是把写时的竞争转移到了读:读得合并所有写过的线程中的数据,而不可避免地变慢了。当你读写都很频繁并得基于数值做一些逻辑判断时,你不应该用bvar。
[public/bvar](http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/tree/src/bvar/)是多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值,它利用了thread local存储避免了cache bouncing,相比UbMonitor几乎不会给程序增加性能开销,也快于竞争频繁的原子操作。baidu-rpc集成了bvar,[/vars](http://brpc.baidu.com:8765/vars)可查看所有曝光的bvar,[/vars/VARNAME](http://brpc.baidu.com:8765/vars/rpc_socket_count)可查阅某个bvar,增加计数器的方法请查看[bvar](http://wiki.baidu.com/display/RPC/bvar)。baidu-rpc大量使用了bvar提供统计数值,当你需要在多线程环境中计数并展现时,应该第一时间想到bvar。但bvar不能代替所有的计数器,它的本质是把写时的竞争转移到了读:读得合并所有写过的线程中的数据,而不可避免地变慢了。当你读写都很频繁并得基于数值做一些逻辑判断时,你不应该用bvar。
## 查询方法
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment