Skip to content

Commit 24fc31e

Browse files
authored
span for new bthread (#2519)
1 parent eac1901 commit 24fc31e

14 files changed

Lines changed: 616 additions & 25 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. 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,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
cmake_minimum_required(VERSION 2.8.10)
19+
project(rpcz_echo_c++ C CXX)
20+
21+
option(LINK_SO "Whether examples are linked dynamically" OFF)
22+
23+
execute_process(
24+
COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'"
25+
OUTPUT_VARIABLE OUTPUT_PATH
26+
)
27+
28+
set(CMAKE_PREFIX_PATH ${OUTPUT_PATH})
29+
30+
include(FindThreads)
31+
include(FindProtobuf)
32+
protobuf_generate_cpp(PROTO_SRC PROTO_HEADER echo.proto)
33+
# include PROTO_HEADER
34+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
35+
36+
# Search for libthrift* by best effort. If it is not found and brpc is
37+
# compiled with thrift protocol enabled, a link error would be reported.
38+
find_library(THRIFT_LIB NAMES thrift)
39+
if (NOT THRIFT_LIB)
40+
set(THRIFT_LIB "")
41+
endif()
42+
43+
find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
44+
if(LINK_SO)
45+
find_library(BRPC_LIB NAMES brpc)
46+
else()
47+
find_library(BRPC_LIB NAMES libbrpc.a brpc)
48+
endif()
49+
if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
50+
message(FATAL_ERROR "Fail to find brpc")
51+
endif()
52+
include_directories(${BRPC_INCLUDE_PATH})
53+
54+
find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
55+
find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
56+
if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
57+
message(FATAL_ERROR "Fail to find gflags")
58+
endif()
59+
include_directories(${GFLAGS_INCLUDE_PATH})
60+
61+
execute_process(
62+
COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
63+
OUTPUT_VARIABLE GFLAGS_NS
64+
)
65+
if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
66+
execute_process(
67+
COMMAND bash -c "grep \"#define GFLAGS_NAMESPACE [_A-Za-z0-9]\\+\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $3}' | tr -d '\n'"
68+
OUTPUT_VARIABLE GFLAGS_NS
69+
)
70+
endif()
71+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
72+
include(CheckFunctionExists)
73+
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
74+
if(NOT HAVE_CLOCK_GETTIME)
75+
set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
76+
endif()
77+
endif()
78+
79+
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
80+
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
81+
82+
if(CMAKE_VERSION VERSION_LESS "3.1.3")
83+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
84+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
85+
endif()
86+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
87+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
88+
endif()
89+
else()
90+
set(CMAKE_CXX_STANDARD 11)
91+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
92+
endif()
93+
94+
find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
95+
find_library(LEVELDB_LIB NAMES leveldb)
96+
if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
97+
message(FATAL_ERROR "Fail to find leveldb")
98+
endif()
99+
include_directories(${LEVELDB_INCLUDE_PATH})
100+
101+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
102+
set(OPENSSL_ROOT_DIR
103+
"/usr/local/opt/openssl" # Homebrew installed OpenSSL
104+
)
105+
endif()
106+
107+
find_package(OpenSSL)
108+
include_directories(${OPENSSL_INCLUDE_DIR})
109+
110+
set(DYNAMIC_LIB
111+
${CMAKE_THREAD_LIBS_INIT}
112+
${GFLAGS_LIBRARY}
113+
${PROTOBUF_LIBRARIES}
114+
${LEVELDB_LIB}
115+
${OPENSSL_CRYPTO_LIBRARY}
116+
${OPENSSL_SSL_LIBRARY}
117+
${THRIFT_LIB}
118+
dl
119+
)
120+
121+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
122+
set(DYNAMIC_LIB ${DYNAMIC_LIB}
123+
pthread
124+
"-framework CoreFoundation"
125+
"-framework CoreGraphics"
126+
"-framework CoreData"
127+
"-framework CoreText"
128+
"-framework Security"
129+
"-framework Foundation"
130+
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
131+
"-Wl,-U,_ProfilerStart"
132+
"-Wl,-U,_ProfilerStop"
133+
"-Wl,-U,__Z13GetStackTracePPvii")
134+
endif()
135+
136+
add_executable(rpcz_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
137+
add_executable(rpcz_echo_server server.cpp ${PROTO_SRC} ${PROTO_HEADER})
138+
139+
target_link_libraries(rpcz_echo_client ${BRPC_LIB} ${DYNAMIC_LIB})
140+
target_link_libraries(rpcz_echo_server ${BRPC_LIB} ${DYNAMIC_LIB})
141+

example/rpcz_echo_c++/client.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// A client sending requests to server every 1 second.
19+
20+
#include <gflags/gflags.h>
21+
#include <butil/logging.h>
22+
#include <butil/time.h>
23+
#include <brpc/channel.h>
24+
#include "echo.pb.h"
25+
26+
namespace brpc {
27+
DECLARE_bool(enable_rpcz);
28+
}
29+
DEFINE_string(attachment, "", "Carry this along with requests");
30+
DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in src/brpc/options.proto");
31+
DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
32+
DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
33+
DEFINE_string(load_balancer, "", "The algorithm for load balancing");
34+
DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
35+
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
36+
DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");
37+
38+
int main(int argc, char* argv[]) {
39+
// Parse gflags. We recommend you to use gflags as well.
40+
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
41+
42+
// brpc::FLAGS_enable_rpcz = true;
43+
// A Channel represents a communication line to a Server. Notice that
44+
// Channel is thread-safe and can be shared by all threads in your program.
45+
brpc::Channel channel;
46+
47+
// Initialize the channel, NULL means using default options.
48+
brpc::ChannelOptions options;
49+
options.protocol = FLAGS_protocol;
50+
options.connection_type = FLAGS_connection_type;
51+
options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
52+
options.max_retry = FLAGS_max_retry;
53+
if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
54+
LOG(ERROR) << "Fail to initialize channel";
55+
return -1;
56+
}
57+
58+
// Normally, you should not call a Channel directly, but instead construct
59+
// a stub Service wrapping it. stub can be shared by all threads as well.
60+
example::EchoService_Stub stub(&channel);
61+
62+
// Send a request and wait for the response every 1 second.
63+
int log_id = 0;
64+
while (!brpc::IsAskedToQuit()) {
65+
// We will receive response synchronously, safe to put variables
66+
// on stack.
67+
example::EchoRequest request;
68+
example::EchoResponse response;
69+
brpc::Controller cntl;
70+
71+
request.set_message("hello world");
72+
73+
cntl.set_log_id(log_id ++); // set by user
74+
// Set attachment which is wired to network directly instead of
75+
// being serialized into protobuf messages.
76+
cntl.request_attachment().append(FLAGS_attachment);
77+
78+
// Because `done'(last parameter) is NULL, this function waits until
79+
// the response comes back or error occurs(including timedout).
80+
stub.Echo(&cntl, &request, &response, NULL);
81+
if (!cntl.Failed()) {
82+
LOG(INFO) << "Received response from " << cntl.remote_side()
83+
<< " to " << cntl.local_side()
84+
<< ": " << response.message() << " (attached="
85+
<< cntl.response_attachment() << ")"
86+
<< " latency=" << cntl.latency_us() << "us";
87+
} else {
88+
LOG(WARNING) << cntl.ErrorText();
89+
}
90+
usleep(FLAGS_interval_ms * 1000L);
91+
}
92+
93+
LOG(INFO) << "EchoClient is going to quit";
94+
return 0;
95+
}
96+

example/rpcz_echo_c++/echo.proto

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
syntax="proto2";
19+
package example;
20+
21+
option cc_generic_services = true;
22+
23+
message EchoRequest {
24+
required string message = 1;
25+
};
26+
27+
message EchoResponse {
28+
required string message = 1;
29+
};
30+
31+
service EchoService {
32+
rpc Echo(EchoRequest) returns (EchoResponse);
33+
};

0 commit comments

Comments
 (0)