From b66d975658a79290f70e005d419c54a838711032 Mon Sep 17 00:00:00 2001 From: zd-double Date: Tue, 19 Jul 2016 21:01:53 +0800 Subject: [PATCH 1/6] adapt proto3 --- Makefile | 14 +- sample/compress_sample/Makefile | 14 +- sample/compress_sample/client.cc | 7 +- .../{ => proto2}/echo_service.proto | 4 +- .../compress_sample/proto3/echo_service.proto | 22 +++ sample/compress_sample/server.cc | 7 +- sample/echo/Makefile | 14 +- sample/echo/client_async.cc | 7 +- sample/echo/client_sync.cc | 7 +- .../proto2}/echo_service.proto | 2 + sample/echo/proto3/echo_service.proto | 18 +++ sample/echo/server.cc | 7 +- sample/mock_sample/Makefile | 14 +- sample/mock_sample/mock_test_sample.cc | 7 +- .../{ => proto2}/echo_service.proto | 2 + sample/mock_sample/proto3/echo_service.proto | 17 +++ sample/multi_server_sample/Makefile | 14 +- sample/multi_server_sample/client.cc | 7 +- .../proto2/echo_service.proto | 18 +++ .../proto3/echo_service.proto | 18 +++ sample/multi_server_sample/server.cc | 7 +- sample/timeout_sample/Makefile | 14 +- sample/timeout_sample/client.cc | 7 +- .../{ => proto2}/sleep_service.proto | 4 +- .../timeout_sample/proto3/sleep_service.proto | 27 ++++ sample/timeout_sample/server.cc | 7 +- src/sofa/pbrpc/binary_rpc_request.cc | 17 +++ src/sofa/pbrpc/binary_rpc_request.h | 7 +- src/sofa/pbrpc/block_wrappers.h | 4 +- src/sofa/pbrpc/builtin_service_impl.cc | 31 ++++ src/sofa/pbrpc/builtin_service_impl.h | 7 +- src/sofa/pbrpc/compressed_stream.h | 6 +- src/sofa/pbrpc/dynamic_rpc_channel_impl.h | 8 +- src/sofa/pbrpc/http-agent/http_agent.cc | 8 +- .../pbrpc/http-agent/sofa_pbrpc_client.cc | 8 +- src/sofa/pbrpc/pbjson.cc | 4 +- .../pbrpc/{ => proto2}/builtin_service.proto | 4 +- src/sofa/pbrpc/{ => proto2}/rpc_meta.proto | 4 +- src/sofa/pbrpc/{ => proto2}/rpc_option.proto | 2 + src/sofa/pbrpc/proto3/builtin_service.proto | 138 ++++++++++++++++++ src/sofa/pbrpc/proto3/rpc_meta.proto | 58 ++++++++ src/sofa/pbrpc/proto3/rpc_option.proto | 34 +++++ src/sofa/pbrpc/rpc_client_stream.h | 18 ++- src/sofa/pbrpc/rpc_controller.h | 7 +- src/sofa/pbrpc/rpc_controller_impl.h | 14 +- src/sofa/pbrpc/rpc_server_stream.h | 7 +- src/sofa/pbrpc/service_pool.h | 8 +- test/kill_test/Makefile | 14 +- test/kill_test/client_parallel.cc | 7 +- test/kill_test/echo_server.cc | 7 +- .../kill_test/{ => proto2}/echo_service.proto | 2 + test/kill_test/proto3/echo_service.proto | 17 +++ test/perf_test/Makefile | 14 +- test/perf_test/client_multi_server.cc | 7 +- test/perf_test/client_parallel.cc | 7 +- test/perf_test/client_serial.cc | 7 +- test/perf_test/echo_server.cc | 7 +- .../perf_test/{ => proto2}/echo_service.proto | 4 +- test/perf_test/proto3/echo_service.proto | 22 +++ 59 files changed, 740 insertions(+), 55 deletions(-) rename sample/compress_sample/{ => proto2}/echo_service.proto (86%) create mode 100644 sample/compress_sample/proto3/echo_service.proto rename sample/{multi_server_sample => echo/proto2}/echo_service.proto (93%) create mode 100644 sample/echo/proto3/echo_service.proto rename sample/mock_sample/{ => proto2}/echo_service.proto (92%) create mode 100644 sample/mock_sample/proto3/echo_service.proto create mode 100644 sample/multi_server_sample/proto2/echo_service.proto create mode 100644 sample/multi_server_sample/proto3/echo_service.proto rename sample/timeout_sample/{ => proto2}/sleep_service.proto (89%) create mode 100644 sample/timeout_sample/proto3/sleep_service.proto rename src/sofa/pbrpc/{ => proto2}/builtin_service.proto (98%) rename src/sofa/pbrpc/{ => proto2}/rpc_meta.proto (95%) rename src/sofa/pbrpc/{ => proto2}/rpc_option.proto (98%) create mode 100644 src/sofa/pbrpc/proto3/builtin_service.proto create mode 100644 src/sofa/pbrpc/proto3/rpc_meta.proto create mode 100644 src/sofa/pbrpc/proto3/rpc_option.proto rename test/kill_test/{ => proto2}/echo_service.proto (92%) create mode 100644 test/kill_test/proto3/echo_service.proto rename test/perf_test/{ => proto2}/echo_service.proto (86%) create mode 100644 test/perf_test/proto3/echo_service.proto diff --git a/Makefile b/Makefile index 543e5bc..ae4e102 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ OPT ?= -O2 # (A) Production use (optimized mode) # SOFA_PBRPC_ENABLE_DETAILED_LOGGING : print current-time and thread-id in logging header # SOFA_PBRPC_ENABLE_FUNCTION_TRACE : print trace log when enter and leave function # SOFA_PBRPC_USE_SPINLOCK : use SpinLock as FastLock +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language # CXXFLAGS ?= -DSOFA_PBRPC_ENABLE_DETAILED_LOGGING #----------------------------------------------- @@ -32,10 +33,12 @@ CXXFLAGS ?= -DSOFA_PBRPC_ENABLE_DETAILED_LOGGING include depends.mk +PROTO_VERSION=proto2 + LIB=libsofa-pbrpc.a LIB_SRC=$(wildcard src/sofa/pbrpc/*.cc) LIB_OBJ=$(patsubst %.cc,%.o,$(LIB_SRC)) -PROTO=$(wildcard src/sofa/pbrpc/*.proto) +PROTO=$(wildcard src/sofa/pbrpc/$(PROTO_VERSION)/*.proto) PROTO_SRC=$(patsubst %.proto,%.pb.cc,$(PROTO)) PROTO_HEADER=$(patsubst %.proto,%.pb.h,$(PROTO)) PROTO_OBJ=$(patsubst %.cc,%.o,$(PROTO_SRC)) @@ -53,8 +56,7 @@ PUB_INC=src/sofa/pbrpc/pbrpc.h src/sofa/pbrpc/closure_helper.h src/sofa/pbrpc/cl src/sofa/pbrpc/locks.h src/sofa/pbrpc/mutex_lock.h src/sofa/pbrpc/spin_lock.h \ src/sofa/pbrpc/fast_lock.h src/sofa/pbrpc/rw_lock.h src/sofa/pbrpc/scoped_locker.h \ src/sofa/pbrpc/condition_variable.h src/sofa/pbrpc/wait_event.h src/sofa/pbrpc/http.h \ - src/sofa/pbrpc/buffer.h src/sofa/pbrpc/buf_handle.h src/sofa/pbrpc/profiling_linker.h \ - $(PROTO) $(PROTO_HEADER) + src/sofa/pbrpc/buffer.h src/sofa/pbrpc/buf_handle.h src/sofa/pbrpc/profiling_linker.h #----------------------------------------------- ifeq ($(OS),Windows_NT) @@ -73,6 +75,9 @@ else endif endif +ifeq ($(PROTO_VERSION), proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif #----------------------------------------------- CXX=g++ @@ -124,6 +129,9 @@ install: $(LIB) $(BIN) cp src/sofa/pbrpc/smart_ptr/*.hpp $(PREFIX)/include/sofa/pbrpc/smart_ptr mkdir -p $(PREFIX)/include/sofa/pbrpc/smart_ptr/detail cp src/sofa/pbrpc/smart_ptr/detail/*.hpp $(PREFIX)/include/sofa/pbrpc/smart_ptr/detail + mkdir -p $(PREFIX)/include/sofa/pbrpc/$(PROTO_VERSION) + cp src/sofa/pbrpc/$(PROTO_VERSION)/*.proto $(PREFIX)/include/sofa/pbrpc/$(PROTO_VERSION) + cp src/sofa/pbrpc/$(PROTO_VERSION)/*.pb.h $(PREFIX)/include/sofa/pbrpc/$(PROTO_VERSION) mkdir -p $(PREFIX)/lib cp $(LIB) $(PREFIX)/lib/ mkdir -p $(PREFIX)/bin diff --git a/sample/compress_sample/Makefile b/sample/compress_sample/Makefile index 8a9c939..81480be 100644 --- a/sample/compress_sample/Makefile +++ b/sample/compress_sample/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#---------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/compress_sample/client.cc b/sample/compress_sample/client.cc index 6f97a38..d0a4928 100644 --- a/sample/compress_sample/client.cc +++ b/sample/compress_sample/client.cc @@ -5,7 +5,12 @@ // Author: qinzuoyan01@baidu.com (Qin Zuoyan) #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif int main(int /*argc*/, char** /*argv*/) { diff --git a/sample/compress_sample/echo_service.proto b/sample/compress_sample/proto2/echo_service.proto similarity index 86% rename from sample/compress_sample/echo_service.proto rename to sample/compress_sample/proto2/echo_service.proto index 7886d13..9f17a72 100644 --- a/sample/compress_sample/echo_service.proto +++ b/sample/compress_sample/proto2/echo_service.proto @@ -1,4 +1,6 @@ -import "sofa/pbrpc/rpc_option.proto"; +syntax = "proto2"; + +import "sofa/pbrpc/proto2/rpc_option.proto"; package sofa.pbrpc.test; diff --git a/sample/compress_sample/proto3/echo_service.proto b/sample/compress_sample/proto3/echo_service.proto new file mode 100644 index 0000000..3f6109c --- /dev/null +++ b/sample/compress_sample/proto3/echo_service.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +import "sofa/pbrpc/proto3/rpc_option.proto"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse) { + option (sofa.pbrpc.request_compress_type) = CompressTypeSnappy; + option (sofa.pbrpc.response_compress_type) = CompressTypeSnappy; + } +} diff --git a/sample/compress_sample/server.cc b/sample/compress_sample/server.cc index c48bd18..7b81d84 100644 --- a/sample/compress_sample/server.cc +++ b/sample/compress_sample/server.cc @@ -7,7 +7,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif class EchoServerImpl : public sofa::pbrpc::test::EchoServer { diff --git a/sample/echo/Makefile b/sample/echo/Makefile index ee15715..e7bef9b 100644 --- a/sample/echo/Makefile +++ b/sample/echo/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#---------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/echo/client_async.cc b/sample/echo/client_async.cc index 3055c09..3e93f8b 100644 --- a/sample/echo/client_async.cc +++ b/sample/echo/client_async.cc @@ -6,7 +6,12 @@ #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif void EchoCallback(sofa::pbrpc::RpcController* cntl, sofa::pbrpc::test::EchoRequest* request, diff --git a/sample/echo/client_sync.cc b/sample/echo/client_sync.cc index 7b26ff5..91e4708 100644 --- a/sample/echo/client_sync.cc +++ b/sample/echo/client_sync.cc @@ -5,7 +5,12 @@ // Author: qinzuoyan01@baidu.com (Qin Zuoyan) #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif // Using global RpcClient object can help share resources such as threads and buffers. sofa::pbrpc::RpcClient g_rpc_client; diff --git a/sample/multi_server_sample/echo_service.proto b/sample/echo/proto2/echo_service.proto similarity index 93% rename from sample/multi_server_sample/echo_service.proto rename to sample/echo/proto2/echo_service.proto index 637ac32..1aca3c7 100644 --- a/sample/multi_server_sample/echo_service.proto +++ b/sample/echo/proto2/echo_service.proto @@ -1,3 +1,5 @@ +syntax = "proto2"; + package sofa.pbrpc.test; option cc_generic_services = true; diff --git a/sample/echo/proto3/echo_service.proto b/sample/echo/proto3/echo_service.proto new file mode 100644 index 0000000..286f903 --- /dev/null +++ b/sample/echo/proto3/echo_service.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; +option java_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse); +} diff --git a/sample/echo/server.cc b/sample/echo/server.cc index c38015e..9034483 100644 --- a/sample/echo/server.cc +++ b/sample/echo/server.cc @@ -7,7 +7,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif bool WebServlet(const sofa::pbrpc::HTTPRequest& request, sofa::pbrpc::HTTPResponse& response) { diff --git a/sample/mock_sample/Makefile b/sample/mock_sample/Makefile index 38ec00f..8a532a9 100644 --- a/sample/mock_sample/Makefile +++ b/sample/mock_sample/Makefile @@ -28,6 +28,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk include depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include -I$(GTEST_DIR)/include @@ -45,7 +47,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#----------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -66,7 +76,7 @@ check_depends: @if [ ! -f "$(GTEST_DIR)/src/gtest-all.cc" ]; then echo "ERROR: need gtest"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* libgtest.a + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* libgtest.a rebuild: clean all diff --git a/sample/mock_sample/mock_test_sample.cc b/sample/mock_sample/mock_test_sample.cc index f0f26f3..3aedd2f 100644 --- a/sample/mock_sample/mock_test_sample.cc +++ b/sample/mock_sample/mock_test_sample.cc @@ -8,7 +8,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif using namespace ::sofa::pbrpc; using namespace ::sofa::pbrpc::test; diff --git a/sample/mock_sample/echo_service.proto b/sample/mock_sample/proto2/echo_service.proto similarity index 92% rename from sample/mock_sample/echo_service.proto rename to sample/mock_sample/proto2/echo_service.proto index 9117a9d..7d107f4 100644 --- a/sample/mock_sample/echo_service.proto +++ b/sample/mock_sample/proto2/echo_service.proto @@ -1,3 +1,5 @@ +syntax = "proto2"; + package sofa.pbrpc.test; option cc_generic_services = true; diff --git a/sample/mock_sample/proto3/echo_service.proto b/sample/mock_sample/proto3/echo_service.proto new file mode 100644 index 0000000..a31a480 --- /dev/null +++ b/sample/mock_sample/proto3/echo_service.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse); +} diff --git a/sample/multi_server_sample/Makefile b/sample/multi_server_sample/Makefile index 8a9c939..81480be 100644 --- a/sample/multi_server_sample/Makefile +++ b/sample/multi_server_sample/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#---------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/multi_server_sample/client.cc b/sample/multi_server_sample/client.cc index 00f601b..8499bb9 100644 --- a/sample/multi_server_sample/client.cc +++ b/sample/multi_server_sample/client.cc @@ -12,7 +12,12 @@ #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif class ReloadableAddressProvider : public sofa::pbrpc::RpcChannel::AddressProvider { diff --git a/sample/multi_server_sample/proto2/echo_service.proto b/sample/multi_server_sample/proto2/echo_service.proto new file mode 100644 index 0000000..1aca3c7 --- /dev/null +++ b/sample/multi_server_sample/proto2/echo_service.proto @@ -0,0 +1,18 @@ +syntax = "proto2"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; +option java_generic_services = true; + +message EchoRequest { + required string message = 1; +} + +message EchoResponse { + required string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse); +} diff --git a/sample/multi_server_sample/proto3/echo_service.proto b/sample/multi_server_sample/proto3/echo_service.proto new file mode 100644 index 0000000..286f903 --- /dev/null +++ b/sample/multi_server_sample/proto3/echo_service.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; +option java_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse); +} diff --git a/sample/multi_server_sample/server.cc b/sample/multi_server_sample/server.cc index d502f6a..59d38ca 100644 --- a/sample/multi_server_sample/server.cc +++ b/sample/multi_server_sample/server.cc @@ -8,7 +8,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif class EchoServerImpl : public sofa::pbrpc::test::EchoServer { diff --git a/sample/timeout_sample/Makefile b/sample/timeout_sample/Makefile index c935028..bc31d9e 100644 --- a/sample/timeout_sample/Makefile +++ b/sample/timeout_sample/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=sleep_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#---------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/sleep_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/timeout_sample/client.cc b/sample/timeout_sample/client.cc index 5bf6069..d2cacf2 100644 --- a/sample/timeout_sample/client.cc +++ b/sample/timeout_sample/client.cc @@ -5,7 +5,12 @@ // Author: qinzuoyan01@baidu.com (Qin Zuoyan) #include -#include "sleep_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/sleep_service.pb.h" +#else +#include "proto2/sleep_service.pb.h" +#endif int main(int /*argc*/, char** /*argv*/) { diff --git a/sample/timeout_sample/sleep_service.proto b/sample/timeout_sample/proto2/sleep_service.proto similarity index 89% rename from sample/timeout_sample/sleep_service.proto rename to sample/timeout_sample/proto2/sleep_service.proto index 488226c..851cbc0 100644 --- a/sample/timeout_sample/sleep_service.proto +++ b/sample/timeout_sample/proto2/sleep_service.proto @@ -1,4 +1,6 @@ -import "sofa/pbrpc/rpc_option.proto"; +syntax = "proto2"; + +import "sofa/pbrpc/proto2/rpc_option.proto"; package sofa.pbrpc.test; diff --git a/sample/timeout_sample/proto3/sleep_service.proto b/sample/timeout_sample/proto3/sleep_service.proto new file mode 100644 index 0000000..7ea8175 --- /dev/null +++ b/sample/timeout_sample/proto3/sleep_service.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +import "sofa/pbrpc/proto3/rpc_option.proto"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; + +message SleepRequest { + int32 sleep_time = 1; // in seconds +} + +message SleepResponse { + string message = 1; +} + +service SleepServer { + // The service timeout is 2 seconds. + option (sofa.pbrpc.service_timeout) = 2000; + + rpc SleepWithServiceTimeout(SleepRequest) returns(SleepResponse); + + // The method timeout is 4 seconds. + rpc SleepWithMethodTimeout(SleepRequest) returns(SleepResponse) { + option (sofa.pbrpc.method_timeout) = 4000; + } +} diff --git a/sample/timeout_sample/server.cc b/sample/timeout_sample/server.cc index aa36516..bab10c2 100644 --- a/sample/timeout_sample/server.cc +++ b/sample/timeout_sample/server.cc @@ -8,7 +8,12 @@ #include #include #include -#include "sleep_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/sleep_service.pb.h" +#else +#include "proto2/sleep_service.pb.h" +#endif class SleepServerImpl : public sofa::pbrpc::test::SleepServer { diff --git a/src/sofa/pbrpc/binary_rpc_request.cc b/src/sofa/pbrpc/binary_rpc_request.cc index 83a6ea9..55b9f6d 100644 --- a/src/sofa/pbrpc/binary_rpc_request.cc +++ b/src/sofa/pbrpc/binary_rpc_request.cc @@ -78,8 +78,12 @@ void BinaryRpcRequest::ProcessRequest( const google::protobuf::MethodDescriptor* method_desc = method_board->Descriptor(); google::protobuf::Message* request = service->GetRequestPrototype(method_desc).New(); +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + CompressType compress_type = _req_meta.compress_type(); +#else CompressType compress_type = _req_meta.has_compress_type() ? _req_meta.compress_type(): CompressTypeNone; +#endif bool parse_request_return = false; if (compress_type == CompressTypeNone) { @@ -115,13 +119,26 @@ void BinaryRpcRequest::ProcessRequest( cntl->SetLocalEndpoint(_local_endpoint); cntl->SetRemoteEndpoint(_remote_endpoint); cntl->SetRpcServerStream(stream); +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + if (_req_meta.server_timeout() > 0) + { + cntl->SetServerTimeout(_req_meta.server_timeout()); + } +#else if (_req_meta.has_server_timeout() && _req_meta.server_timeout() > 0) { cntl->SetServerTimeout(_req_meta.server_timeout()); } +#endif + cntl->SetRequestReceivedTime(_received_time); + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + cntl->SetResponseCompressType(_req_meta.expected_response_compress_type()); +#else cntl->SetResponseCompressType(_req_meta.has_expected_response_compress_type() ? _req_meta.expected_response_compress_type() : CompressTypeNone); +#endif CallMethod(method_board, controller, request, response); } diff --git a/src/sofa/pbrpc/binary_rpc_request.h b/src/sofa/pbrpc/binary_rpc_request.h index 6237694..cba0ac6 100644 --- a/src/sofa/pbrpc/binary_rpc_request.h +++ b/src/sofa/pbrpc/binary_rpc_request.h @@ -9,7 +9,12 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif namespace sofa { namespace pbrpc { diff --git a/src/sofa/pbrpc/block_wrappers.h b/src/sofa/pbrpc/block_wrappers.h index 4850b2c..553e60c 100644 --- a/src/sofa/pbrpc/block_wrappers.h +++ b/src/sofa/pbrpc/block_wrappers.h @@ -32,7 +32,7 @@ class BlockCompressionInputStream : public AbstractCompressedInputStream { bool Next(const void** data, int* size); void BackUp(int count); bool Skip(int count); - int64_t ByteCount() const { return _byte_count; } + int64 ByteCount() const { return _byte_count; } bool ExpectAtEnd() { return true; } protected: @@ -62,7 +62,7 @@ class BlockCompressionOutputStream : public AbstractCompressedOutputStream { bool Next(void** data, int* size); void BackUp(int count); - int64_t ByteCount() const { return _byte_count; }; + int64 ByteCount() const { return _byte_count; }; bool Flush(); bool Close() { return Flush(); } diff --git a/src/sofa/pbrpc/builtin_service_impl.cc b/src/sofa/pbrpc/builtin_service_impl.cc index a2fad04..27fc96f 100644 --- a/src/sofa/pbrpc/builtin_service_impl.cc +++ b/src/sofa/pbrpc/builtin_service_impl.cc @@ -83,6 +83,17 @@ void BuiltinServiceImpl::UpdateOptions(::google::protobuf::RpcController* contro return; } const ::sofa::pbrpc::builtin::ServerOptions& request_options = request->options(); +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + if (request_options.max_connection_count() == 0 + && request_options.keep_alive_time() == 0 + && request_options.max_pending_buffer_size() == 0 + && request_options.max_throughput_in() == 0 + && request_options.max_throughput_out() == 0) { + controller->SetFailed("no option need to update"); + done->Run(); + return; + } +#else if (!request_options.has_max_connection_count() && !request_options.has_keep_alive_time() && !request_options.has_max_pending_buffer_size() @@ -92,6 +103,7 @@ void BuiltinServiceImpl::UpdateOptions(::google::protobuf::RpcController* contro done->Run(); return; } +#endif RpcServerImplPtr server = _rpc_server.lock(); if (!server) { controller->SetFailed("server not exist"); @@ -99,6 +111,23 @@ void BuiltinServiceImpl::UpdateOptions(::google::protobuf::RpcController* contro return; } RpcServerOptions options = server->GetOptions(); +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + if (request_options.max_connection_count() != 0) { + options.max_connection_count = request_options.max_connection_count(); + } + if (request_options.keep_alive_time() != 0) { + options.keep_alive_time = request_options.keep_alive_time(); + } + if (request_options.max_pending_buffer_size() != 0) { + options.max_pending_buffer_size = request_options.max_pending_buffer_size(); + } + if (request_options.max_throughput_in() != 0) { + options.max_throughput_in = request_options.max_throughput_in(); + } + if (request_options.max_throughput_out() != 0) { + options.max_throughput_out = request_options.max_throughput_out(); + } +#else if (request_options.has_max_connection_count()) { options.max_connection_count = request_options.max_connection_count(); } @@ -114,6 +143,8 @@ void BuiltinServiceImpl::UpdateOptions(::google::protobuf::RpcController* contro if (request_options.has_max_throughput_out()) { options.max_throughput_out = request_options.max_throughput_out(); } +#endif + #if defined( LOG ) LOG(INFO) << "UpdateOptions(): update by builtin service"; #else diff --git a/src/sofa/pbrpc/builtin_service_impl.h b/src/sofa/pbrpc/builtin_service_impl.h index cb6ee2a..6434d24 100644 --- a/src/sofa/pbrpc/builtin_service_impl.h +++ b/src/sofa/pbrpc/builtin_service_impl.h @@ -8,7 +8,12 @@ #define _SOFA_PBRPC_BUILTIN_SERVICE_IMPL_H_ #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif namespace sofa { namespace pbrpc { diff --git a/src/sofa/pbrpc/compressed_stream.h b/src/sofa/pbrpc/compressed_stream.h index 64942b8..d2be091 100644 --- a/src/sofa/pbrpc/compressed_stream.h +++ b/src/sofa/pbrpc/compressed_stream.h @@ -10,7 +10,11 @@ #include -#include +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif #define HAVE_SNAPPY 1 diff --git a/src/sofa/pbrpc/dynamic_rpc_channel_impl.h b/src/sofa/pbrpc/dynamic_rpc_channel_impl.h index cdda6cf..081a5f1 100644 --- a/src/sofa/pbrpc/dynamic_rpc_channel_impl.h +++ b/src/sofa/pbrpc/dynamic_rpc_channel_impl.h @@ -17,7 +17,13 @@ #include #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif + #include namespace sofa { diff --git a/src/sofa/pbrpc/http-agent/http_agent.cc b/src/sofa/pbrpc/http-agent/http_agent.cc index 2809425..3b95573 100644 --- a/src/sofa/pbrpc/http-agent/http_agent.cc +++ b/src/sofa/pbrpc/http-agent/http_agent.cc @@ -8,7 +8,13 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif + #include #include #include diff --git a/src/sofa/pbrpc/http-agent/sofa_pbrpc_client.cc b/src/sofa/pbrpc/http-agent/sofa_pbrpc_client.cc index 323b93d..5dd09e1 100644 --- a/src/sofa/pbrpc/http-agent/sofa_pbrpc_client.cc +++ b/src/sofa/pbrpc/http-agent/sofa_pbrpc_client.cc @@ -8,7 +8,13 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif + #include static std::string g_server_address; diff --git a/src/sofa/pbrpc/pbjson.cc b/src/sofa/pbrpc/pbjson.cc index 341a907..cb2ac79 100644 --- a/src/sofa/pbrpc/pbjson.cc +++ b/src/sofa/pbrpc/pbjson.cc @@ -107,7 +107,7 @@ static rapidjson::Value* field2json(const Message *msg, const FieldDescriptor *f } else { - json = new rapidjson::Value(ref->GetInt64(*msg, field)); + json = new rapidjson::Value(static_cast(ref->GetInt64(*msg, field))); } break; case FieldDescriptor::CPPTYPE_UINT64: @@ -122,7 +122,7 @@ static rapidjson::Value* field2json(const Message *msg, const FieldDescriptor *f } else { - json = new rapidjson::Value(ref->GetUInt64(*msg, field)); + json = new rapidjson::Value(static_cast(ref->GetUInt64(*msg, field))); } break; case FieldDescriptor::CPPTYPE_INT32: diff --git a/src/sofa/pbrpc/builtin_service.proto b/src/sofa/pbrpc/proto2/builtin_service.proto similarity index 98% rename from src/sofa/pbrpc/builtin_service.proto rename to src/sofa/pbrpc/proto2/builtin_service.proto index 48e651c..9b50610 100644 --- a/src/sofa/pbrpc/builtin_service.proto +++ b/src/sofa/pbrpc/proto2/builtin_service.proto @@ -4,8 +4,10 @@ // // Author: qinzuoyan01@baidu.com (Qin Zuoyan) +syntax = "proto2"; + import "google/protobuf/descriptor.proto"; -import "sofa/pbrpc/rpc_option.proto"; +import "sofa/pbrpc/proto2/rpc_option.proto"; package sofa.pbrpc.builtin; diff --git a/src/sofa/pbrpc/rpc_meta.proto b/src/sofa/pbrpc/proto2/rpc_meta.proto similarity index 95% rename from src/sofa/pbrpc/rpc_meta.proto rename to src/sofa/pbrpc/proto2/rpc_meta.proto index c8ad235..bb25e9f 100644 --- a/src/sofa/pbrpc/rpc_meta.proto +++ b/src/sofa/pbrpc/proto2/rpc_meta.proto @@ -4,7 +4,9 @@ // // Author: qinzuoyan01@baidu.com (Qin Zuoyan) -import "sofa/pbrpc/rpc_option.proto"; +syntax = "proto2"; + +import "sofa/pbrpc/proto2/rpc_option.proto"; package sofa.pbrpc; diff --git a/src/sofa/pbrpc/rpc_option.proto b/src/sofa/pbrpc/proto2/rpc_option.proto similarity index 98% rename from src/sofa/pbrpc/rpc_option.proto rename to src/sofa/pbrpc/proto2/rpc_option.proto index 2472d92..d2e80d9 100644 --- a/src/sofa/pbrpc/rpc_option.proto +++ b/src/sofa/pbrpc/proto2/rpc_option.proto @@ -4,6 +4,8 @@ // // Author: qinzuoyan01@baidu.com (Qin Zuoyan) +syntax = "proto2"; + import "google/protobuf/descriptor.proto"; package sofa.pbrpc; diff --git a/src/sofa/pbrpc/proto3/builtin_service.proto b/src/sofa/pbrpc/proto3/builtin_service.proto new file mode 100644 index 0000000..4d7884e --- /dev/null +++ b/src/sofa/pbrpc/proto3/builtin_service.proto @@ -0,0 +1,138 @@ +// Copyright (c) 2014 Baidu.com, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Author: qinzuoyan01@baidu.com (Qin Zuoyan) + +syntax = "proto3"; + +import "google/protobuf/descriptor.proto"; +import "sofa/pbrpc/proto3/rpc_option.proto"; + +package sofa.pbrpc.builtin; + +option cc_generic_services = true; +option java_generic_services = true; +option py_generic_services = true; + +message HealthRequest { +} + +message HealthResponse { + string health = 1; + string version = 2; + string start_time = 3; +} + +message ServerOptions { + int64 work_thread_num = 1; + int64 keep_alive_time = 2; + int64 max_pending_buffer_size = 3; + int64 max_throughput_in = 4; + int64 max_throughput_out = 5; + bool disable_builtin_services = 6; + bool disable_list_service = 7; + int64 max_connection_count = 8; +} + +message ServerOptionsRequest { +} + +message ServerOptionsResponse { + ServerOptions options = 1; +} + +// Update server options. Only the following options can be update: +// - keep_alive_time +// - max_pending_buffer_size +// - max_throughput_in +// - max_throughput_out +// If not set, then use old value. +message UpdateOptionsRequest { + ServerOptions options = 1; +} + +// If succeed, returns new values. +message UpdateOptionsResponse { + ServerOptions options = 1; +} + +message ServerStatusRequest { +} + +message ServerStatusResponse { + bool is_listening = 1; + + // count of client connections. + int64 connection_count = 2; + + // count of registered services. + int64 service_count = 3; + + // total count of pending messages. + int64 pending_message_count = 4; + + // total size of pending buffers occupied by pending messages. + int64 pending_buffer_size = 5; + + // total size of pending data in pending messages, may by less than pending_buffer_size. + int64 pending_data_size = 6; +} + +message ListServiceRequest { +} + +message ListServiceResponse { + // full name of all registered services. + repeated string services = 1; + + // descriptor of all dependency proto files. + repeated google.protobuf.FileDescriptorProto files = 2; +} + +message MethodStat { + string method_name = 1; + int64 succeed_count = 2; + float succeed_avg_time_us = 3; + int64 succeed_max_time_us = 4; + int64 failed_count = 5; + float failed_avg_time_us = 6; + int64 failed_max_time_us = 7; + int64 slot_id = 8; +} + +message ServiceStat { + string service_name = 1; + int64 period_seconds = 2; + int64 succeed_count = 3; + int64 failed_count = 4; + repeated MethodStat method_stats = 5; +} + +message StatRequest { + string service_name = 1; + int64 period_seconds = 2; // expect period in seconds +} + +message StatResponse { + repeated ServiceStat service_stats = 1; + string server_start_time = 2; +} + +service BuiltinService { + option (sofa.pbrpc.service_timeout) = 3000; + + rpc Health(HealthRequest) returns (HealthResponse); + + rpc ServerOptions(ServerOptionsRequest) returns (ServerOptionsResponse); + + rpc UpdateOptions(UpdateOptionsRequest) returns (UpdateOptionsResponse); + + rpc ServerStatus(ServerStatusRequest) returns (ServerStatusResponse); + + rpc ListService(ListServiceRequest) returns (ListServiceResponse) { + option (sofa.pbrpc.response_compress_type) = CompressTypeGzip; + } + + rpc Stat(StatRequest) returns (StatResponse); +} diff --git a/src/sofa/pbrpc/proto3/rpc_meta.proto b/src/sofa/pbrpc/proto3/rpc_meta.proto new file mode 100644 index 0000000..f6f36d5 --- /dev/null +++ b/src/sofa/pbrpc/proto3/rpc_meta.proto @@ -0,0 +1,58 @@ +// Copyright (c) 2014 Baidu.com, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Author: qinzuoyan01@baidu.com (Qin Zuoyan) + +syntax = "proto3"; + +import "sofa/pbrpc/proto3/rpc_option.proto"; + +package sofa.pbrpc; + +message RpcMeta { + + ///////////////////////////////////////////////////// + // The following fields are used both for request and response. + + // Message type. + enum Type { + REQUEST = 0; + RESPONSE = 1; + }; + Type type = 1; + + // Message sequence id. + uint64 sequence_id = 2; + + ///////////////////////////////////////////////////// + // The following fields are used only for request. + + // Method full name. + // For example: "test.HelloService.GreetMethod" + string method = 100; + + // Server timeout in milli-seconds. + int64 server_timeout = 101; + + ///////////////////////////////////////////////////// + // The following fields are used only for response. + + // Set as true if the call is failed. + bool failed = 200; + + // The error code if the call is failed. + int32 error_code = 201; + + // The error reason if the call is failed. + string reason = 202; + + ///////////////////////////////////////////////////// + // Compression related fields. + + // Set the request/response compress type. + CompressType compress_type = 300; + + // Set the response compress type of user expected. + CompressType expected_response_compress_type = 301; +} diff --git a/src/sofa/pbrpc/proto3/rpc_option.proto b/src/sofa/pbrpc/proto3/rpc_option.proto new file mode 100644 index 0000000..3524746 --- /dev/null +++ b/src/sofa/pbrpc/proto3/rpc_option.proto @@ -0,0 +1,34 @@ +// Copyright (c) 2014 Baidu.com, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Author: qinzuoyan01@baidu.com (Qin Zuoyan) + +syntax = "proto3"; + +import "google/protobuf/descriptor.proto"; + +package sofa.pbrpc; + +extend google.protobuf.ServiceOptions { + // Timeout in milli-seconds at service level. Default is 10 seconds. + int64 service_timeout = 20000; +} + +enum CompressType { + CompressTypeNone = 0; + CompressTypeGzip = 1; + CompressTypeZlib = 2; + CompressTypeSnappy = 3; + CompressTypeLZ4 = 4; +} + +extend google.protobuf.MethodOptions { + // Timeout in milli-seconds at method level. There is no default value because + // method without method_timeout set will use service_timeout. + int64 method_timeout = 20000; + + // Compress type. Default is no compression. + CompressType request_compress_type = 20001; + CompressType response_compress_type = 20002; +} diff --git a/src/sofa/pbrpc/rpc_client_stream.h b/src/sofa/pbrpc/rpc_client_stream.h index 541fb39..a5e0b29 100644 --- a/src/sofa/pbrpc/rpc_client_stream.h +++ b/src/sofa/pbrpc/rpc_client_stream.h @@ -9,7 +9,12 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif namespace sofa { namespace pbrpc { @@ -235,6 +240,12 @@ class RpcClientStream : public RpcMessageStream return; } +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + if (meta.failed()) + { + cntl->Done(meta.error_code(), meta.reason()); + } +#else if (!meta.has_failed()) { #if defined( LOG ) @@ -278,12 +289,17 @@ class RpcClientStream : public RpcMessageStream cntl->Done(meta.error_code(), meta.reason()); } } +#endif else // !meta.failed() { SCHECK_EQ(data_size, message->TotalCount() - message->ByteCount()); cntl->SetResponseBuffer(message); +#if defined ( SOFA_PBRPC_USE_PROTO3 ) + cntl->SetResponseCompressType(meta.compress_type()); +#else cntl->SetResponseCompressType(meta.has_compress_type() ? meta.compress_type() : CompressTypeNone); +#endif cntl->Done(RPC_SUCCESS, "succeed"); } } diff --git a/src/sofa/pbrpc/rpc_controller.h b/src/sofa/pbrpc/rpc_controller.h index 2ec3c14..7974e31 100644 --- a/src/sofa/pbrpc/rpc_controller.h +++ b/src/sofa/pbrpc/rpc_controller.h @@ -10,7 +10,12 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif namespace sofa { namespace pbrpc { diff --git a/src/sofa/pbrpc/rpc_controller_impl.h b/src/sofa/pbrpc/rpc_controller_impl.h index 79dabb2..e4e62b6 100644 --- a/src/sofa/pbrpc/rpc_controller_impl.h +++ b/src/sofa/pbrpc/rpc_controller_impl.h @@ -17,7 +17,13 @@ #include #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif + #include namespace sofa { @@ -239,8 +245,10 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_thisoptions().GetExtension(method_timeout) : method->service()->options().GetExtension(service_timeout); if (timeout_in_ms <= 0) { - // Just a protection, it shouldn't happen. - timeout_in_ms = 1; + // In protobuf v3 default option is not support. + // For numeric types, the default value is zero. + // set timeout_in_ms to 10 seconds when service_timeout equal to zero. + timeout_in_ms = 10000; } _auto_options.timeout = timeout_in_ms; } diff --git a/src/sofa/pbrpc/rpc_server_stream.h b/src/sofa/pbrpc/rpc_server_stream.h index e2292e3..67ad578 100644 --- a/src/sofa/pbrpc/rpc_server_stream.h +++ b/src/sofa/pbrpc/rpc_server_stream.h @@ -9,7 +9,12 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif namespace sofa { namespace pbrpc { diff --git a/src/sofa/pbrpc/service_pool.h b/src/sofa/pbrpc/service_pool.h index e0d2a66..3966573 100644 --- a/src/sofa/pbrpc/service_pool.h +++ b/src/sofa/pbrpc/service_pool.h @@ -15,7 +15,13 @@ #include #include -#include + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#else +#include +#endif + #include #include #include diff --git a/test/kill_test/Makefile b/test/kill_test/Makefile index 351d93d..f16fbf9 100644 --- a/test/kill_test/Makefile +++ b/test/kill_test/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(BOOST_HEADER_DIR) -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#----------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/test/kill_test/client_parallel.cc b/test/kill_test/client_parallel.cc index 4a8d117..1841b86 100644 --- a/test/kill_test/client_parallel.cc +++ b/test/kill_test/client_parallel.cc @@ -9,7 +9,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif static volatile bool s_is_running = true; static volatile bool s_should_wait = false; diff --git a/test/kill_test/echo_server.cc b/test/kill_test/echo_server.cc index 257ae84..a49357c 100644 --- a/test/kill_test/echo_server.cc +++ b/test/kill_test/echo_server.cc @@ -9,7 +9,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif static sofa::pbrpc::AtomicCounter s_succeed_count(0); diff --git a/test/kill_test/echo_service.proto b/test/kill_test/proto2/echo_service.proto similarity index 92% rename from test/kill_test/echo_service.proto rename to test/kill_test/proto2/echo_service.proto index 9117a9d..7d107f4 100644 --- a/test/kill_test/echo_service.proto +++ b/test/kill_test/proto2/echo_service.proto @@ -1,3 +1,5 @@ +syntax = "proto2"; + package sofa.pbrpc.test; option cc_generic_services = true; diff --git a/test/kill_test/proto3/echo_service.proto b/test/kill_test/proto3/echo_service.proto new file mode 100644 index 0000000..a31a480 --- /dev/null +++ b/test/kill_test/proto3/echo_service.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse); +} diff --git a/test/perf_test/Makefile b/test/perf_test/Makefile index 76b180c..dfa23ce 100644 --- a/test/perf_test/Makefile +++ b/test/perf_test/Makefile @@ -27,6 +27,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk +PROTO_VERSION=proto2 + CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(BOOST_HEADER_DIR) -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include @@ -44,7 +46,15 @@ else endif endif -PROTO_SRC=echo_service.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#----------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/echo_service.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include @@ -64,7 +74,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) *.o *.pb.* + @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/test/perf_test/client_multi_server.cc b/test/perf_test/client_multi_server.cc index 2b664d9..25a4910 100644 --- a/test/perf_test/client_multi_server.cc +++ b/test/perf_test/client_multi_server.cc @@ -10,7 +10,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif volatile bool g_quit = false; diff --git a/test/perf_test/client_parallel.cc b/test/perf_test/client_parallel.cc index 277a3ee..7c244c9 100644 --- a/test/perf_test/client_parallel.cc +++ b/test/perf_test/client_parallel.cc @@ -9,7 +9,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif static volatile bool s_is_running = true; static volatile bool s_should_wait = false; diff --git a/test/perf_test/client_serial.cc b/test/perf_test/client_serial.cc index 72f90dd..b8bc94e 100644 --- a/test/perf_test/client_serial.cc +++ b/test/perf_test/client_serial.cc @@ -10,7 +10,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif volatile bool g_quit = false; diff --git a/test/perf_test/echo_server.cc b/test/perf_test/echo_server.cc index 54853cf..962598d 100644 --- a/test/perf_test/echo_server.cc +++ b/test/perf_test/echo_server.cc @@ -9,7 +9,12 @@ #include #include #include -#include "echo_service.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include "proto3/echo_service.pb.h" +#else +#include "proto2/echo_service.pb.h" +#endif static sofa::pbrpc::AtomicCounter s_succeed_count(0); diff --git a/test/perf_test/echo_service.proto b/test/perf_test/proto2/echo_service.proto similarity index 86% rename from test/perf_test/echo_service.proto rename to test/perf_test/proto2/echo_service.proto index 70b3643..308cde7 100644 --- a/test/perf_test/echo_service.proto +++ b/test/perf_test/proto2/echo_service.proto @@ -1,4 +1,6 @@ -import "sofa/pbrpc/rpc_option.proto"; +syntax = "proto2"; + +import "sofa/pbrpc/proto2/rpc_option.proto"; package sofa.pbrpc.test; diff --git a/test/perf_test/proto3/echo_service.proto b/test/perf_test/proto3/echo_service.proto new file mode 100644 index 0000000..5bd9e27 --- /dev/null +++ b/test/perf_test/proto3/echo_service.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +import "sofa/pbrpc/proto3/rpc_option.proto"; + +package sofa.pbrpc.test; + +option cc_generic_services = true; + +message EchoRequest { + string message = 1; +} + +message EchoResponse { + string message = 1; +} + +service EchoServer { + rpc Echo(EchoRequest) returns(EchoResponse) { + option (sofa.pbrpc.request_compress_type) = CompressTypeNone; + option (sofa.pbrpc.response_compress_type) = CompressTypeNone; + } +} From ea78fc7a5ad803bc1d4dc4638e8b8004441b9ac5 Mon Sep 17 00:00:00 2001 From: zd-double Date: Wed, 20 Jul 2016 15:20:11 +0800 Subject: [PATCH 2/6] set PROTO_VERSION in depends.mk --- Makefile | 5 ++--- depends.mk | 10 ++++++++++ sample/compress_sample/Makefile | 2 -- sample/echo/Makefile | 2 -- sample/echo/echo_service.proto | 16 ---------------- sample/mock_sample/Makefile | 2 -- sample/multi_server_sample/Makefile | 2 -- sample/timeout_sample/Makefile | 2 -- src/sofa/pbrpc/binary_rpc_request.cc | 4 ++-- src/sofa/pbrpc/profiling.h | 2 +- test/kill_test/Makefile | 2 -- test/perf_test/Makefile | 2 -- 12 files changed, 15 insertions(+), 36 deletions(-) delete mode 100644 sample/echo/echo_service.proto diff --git a/Makefile b/Makefile index ae4e102..3b9f1be 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,8 @@ OPT ?= -O2 # (A) Production use (optimized mode) # SOFA_PBRPC_ENABLE_DETAILED_LOGGING : print current-time and thread-id in logging header # SOFA_PBRPC_ENABLE_FUNCTION_TRACE : print trace log when enter and leave function # SOFA_PBRPC_USE_SPINLOCK : use SpinLock as FastLock -# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +# SOFA_PBRPC_PROFILING : use Profiling +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the Protocol Buffers language # CXXFLAGS ?= -DSOFA_PBRPC_ENABLE_DETAILED_LOGGING #----------------------------------------------- @@ -33,8 +34,6 @@ CXXFLAGS ?= -DSOFA_PBRPC_ENABLE_DETAILED_LOGGING include depends.mk -PROTO_VERSION=proto2 - LIB=libsofa-pbrpc.a LIB_SRC=$(wildcard src/sofa/pbrpc/*.cc) LIB_OBJ=$(patsubst %.cc,%.o,$(LIB_SRC)) diff --git a/depends.mk b/depends.mk index 91eb1b9..768d41c 100644 --- a/depends.mk +++ b/depends.mk @@ -9,6 +9,16 @@ ## ################################################################ +################################################################ +## Version of Protocol Buffers language. +## +## Support Protocol Buffers version: +## proto2 +## proto3 +## +PROTO_VERSION=proto2 +################################################################ + ################################################################ ## Boost header directory. ## diff --git a/sample/compress_sample/Makefile b/sample/compress_sample/Makefile index 81480be..14a67c0 100644 --- a/sample/compress_sample/Makefile +++ b/sample/compress_sample/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include diff --git a/sample/echo/Makefile b/sample/echo/Makefile index e7bef9b..d2dbb24 100644 --- a/sample/echo/Makefile +++ b/sample/echo/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include diff --git a/sample/echo/echo_service.proto b/sample/echo/echo_service.proto deleted file mode 100644 index 637ac32..0000000 --- a/sample/echo/echo_service.proto +++ /dev/null @@ -1,16 +0,0 @@ -package sofa.pbrpc.test; - -option cc_generic_services = true; -option java_generic_services = true; - -message EchoRequest { - required string message = 1; -} - -message EchoResponse { - required string message = 1; -} - -service EchoServer { - rpc Echo(EchoRequest) returns(EchoResponse); -} diff --git a/sample/mock_sample/Makefile b/sample/mock_sample/Makefile index 8a532a9..8f6d662 100644 --- a/sample/mock_sample/Makefile +++ b/sample/mock_sample/Makefile @@ -28,8 +28,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk include depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include -I$(GTEST_DIR)/include diff --git a/sample/multi_server_sample/Makefile b/sample/multi_server_sample/Makefile index 81480be..14a67c0 100644 --- a/sample/multi_server_sample/Makefile +++ b/sample/multi_server_sample/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include diff --git a/sample/timeout_sample/Makefile b/sample/timeout_sample/Makefile index bc31d9e..2899fc1 100644 --- a/sample/timeout_sample/Makefile +++ b/sample/timeout_sample/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include diff --git a/src/sofa/pbrpc/binary_rpc_request.cc b/src/sofa/pbrpc/binary_rpc_request.cc index 55b9f6d..a16ea53 100644 --- a/src/sofa/pbrpc/binary_rpc_request.cc +++ b/src/sofa/pbrpc/binary_rpc_request.cc @@ -79,7 +79,7 @@ void BinaryRpcRequest::ProcessRequest( google::protobuf::Message* request = service->GetRequestPrototype(method_desc).New(); #if defined ( SOFA_PBRPC_USE_PROTO3 ) - CompressType compress_type = _req_meta.compress_type(); + CompressType compress_type = _req_meta.compress_type(); #else CompressType compress_type = _req_meta.has_compress_type() ? _req_meta.compress_type(): CompressTypeNone; @@ -134,7 +134,7 @@ void BinaryRpcRequest::ProcessRequest( cntl->SetRequestReceivedTime(_received_time); #if defined ( SOFA_PBRPC_USE_PROTO3 ) - cntl->SetResponseCompressType(_req_meta.expected_response_compress_type()); + cntl->SetResponseCompressType(_req_meta.expected_response_compress_type()); #else cntl->SetResponseCompressType(_req_meta.has_expected_response_compress_type() ? _req_meta.expected_response_compress_type() : CompressTypeNone); diff --git a/src/sofa/pbrpc/profiling.h b/src/sofa/pbrpc/profiling.h index 7bc41a4..54cabcb 100644 --- a/src/sofa/pbrpc/profiling.h +++ b/src/sofa/pbrpc/profiling.h @@ -90,4 +90,4 @@ class Profiling } // namespace pbrpc } // namespace sofa -#endif // _SOFA_PBRPC_WEB_SERVICE_H_ +#endif // _SOFA_PBRPC_PROFILING_H_ diff --git a/test/kill_test/Makefile b/test/kill_test/Makefile index f16fbf9..fbba89e 100644 --- a/test/kill_test/Makefile +++ b/test/kill_test/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(BOOST_HEADER_DIR) -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include diff --git a/test/perf_test/Makefile b/test/perf_test/Makefile index dfa23ce..78dd350 100644 --- a/test/perf_test/Makefile +++ b/test/perf_test/Makefile @@ -27,8 +27,6 @@ OPT ?= -O2 # (A) Production use (optimized mode) include ../../depends.mk -PROTO_VERSION=proto2 - CXX=g++ INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(BOOST_HEADER_DIR) -I$(PROTOBUF_DIR)/include \ -I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include From 73f86d4f0a9e4307f5e5cc283e1e3800d13366c7 Mon Sep 17 00:00:00 2001 From: zd-double Date: Wed, 20 Jul 2016 15:52:08 +0800 Subject: [PATCH 3/6] update sample Makefile --- sample/compress_sample/Makefile | 2 +- sample/echo/Makefile | 2 +- sample/mock_sample/Makefile | 2 +- sample/multi_server_sample/Makefile | 2 +- sample/timeout_sample/Makefile | 2 +- test/kill_test/Makefile | 2 +- test/perf_test/Makefile | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sample/compress_sample/Makefile b/sample/compress_sample/Makefile index 14a67c0..9c007de 100644 --- a/sample/compress_sample/Makefile +++ b/sample/compress_sample/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/echo/Makefile b/sample/echo/Makefile index d2dbb24..3b9a195 100644 --- a/sample/echo/Makefile +++ b/sample/echo/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/mock_sample/Makefile b/sample/mock_sample/Makefile index 8f6d662..8d3f710 100644 --- a/sample/mock_sample/Makefile +++ b/sample/mock_sample/Makefile @@ -74,7 +74,7 @@ check_depends: @if [ ! -f "$(GTEST_DIR)/src/gtest-all.cc" ]; then echo "ERROR: need gtest"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* libgtest.a + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* libgtest.a rebuild: clean all diff --git a/sample/multi_server_sample/Makefile b/sample/multi_server_sample/Makefile index 14a67c0..9c007de 100644 --- a/sample/multi_server_sample/Makefile +++ b/sample/multi_server_sample/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/sample/timeout_sample/Makefile b/sample/timeout_sample/Makefile index 2899fc1..e81452b 100644 --- a/sample/timeout_sample/Makefile +++ b/sample/timeout_sample/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/test/kill_test/Makefile b/test/kill_test/Makefile index fbba89e..f3013f6 100644 --- a/test/kill_test/Makefile +++ b/test/kill_test/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all diff --git a/test/perf_test/Makefile b/test/perf_test/Makefile index 78dd350..b94b1ff 100644 --- a/test/perf_test/Makefile +++ b/test/perf_test/Makefile @@ -72,7 +72,7 @@ check_depends: @if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi clean: - @rm -f $(BIN) $(PROTO_OBJ) *.o $(PROTO_VERSION)/*.pb.* + @rm -f $(BIN) *.o $(PROTO_VERSION)/*.pb.* rebuild: clean all From 3a539569018079b3006015ff1919beb3ceec910a Mon Sep 17 00:00:00 2001 From: zd-double Date: Thu, 21 Jul 2016 00:38:02 +0800 Subject: [PATCH 4/6] update unit-test --- unit-test/Makefile | 14 +++++++++++--- unit-test/proto2/.test_data.proto.un~ | Bin 0 -> 2726 bytes unit-test/{ => proto2}/test_data.proto | 2 ++ unit-test/proto3/test_data.proto | 11 +++++++++++ unit-test/test_buffer.cc | 10 ++++++++-- 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 unit-test/proto2/.test_data.proto.un~ rename unit-test/{ => proto2}/test_data.proto (90%) create mode 100644 unit-test/proto3/test_data.proto diff --git a/unit-test/Makefile b/unit-test/Makefile index 85ae6e9..e0a06c7 100644 --- a/unit-test/Makefile +++ b/unit-test/Makefile @@ -34,7 +34,15 @@ else endif endif -PROTO_SRC=test_data.proto +#----------------------------------------------- +# Customized macro switch: +# SOFA_PBRPC_USE_PROTO3 : use proto3 version of the protocol buffers language +ifeq ($(PROTO_VERSION),proto3) + CXXFLAGS += -DSOFA_PBRPC_USE_PROTO3 +endif +#----------------------------------------------- + +PROTO_SRC=$(PROTO_VERSION)/test_data.proto PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC)) PROTO_OPTIONS=--proto_path=. --proto_path=../src --proto_path=$(PROTOBUF_DIR)/include @@ -66,7 +74,7 @@ check_depends: @if [ ! -f "../libsofa-pbrpc.a" ]; then echo "ERROR: should build sofa-pbrpc first"; exit 1; fi clean: - @rm -f $(TESTS) *.o *.pb.* libgtest.a + @rm -f $(TESTS) *.o $(PROTO_VERSION)/*.pb.* libgtest.a rebuild: clean all @@ -82,7 +90,7 @@ test_epoll_support: test_epoll_support.o libgtest.a test_tran_buf_pool: test_tran_buf_pool.o libgtest.a $(CXX) $^ -o $@ $(LIBRARY) $(LDFLAGS) -test_buffer: test_data.pb.o test_buffer.o libgtest.a +test_buffer: $(PROTO_VERSION)/test_data.pb.o test_buffer.o libgtest.a $(CXX) $^ -o $@ $(LIBRARY) $(LDFLAGS) test_closure: test_closure.o libgtest.a diff --git a/unit-test/proto2/.test_data.proto.un~ b/unit-test/proto2/.test_data.proto.un~ new file mode 100644 index 0000000000000000000000000000000000000000..7e5632f25b6a5ba17b28def4551519a9b2ad53c4 GIT binary patch literal 2726 zcmWH`%$*;a=aT=FfvH;H=j5vg-n^Xmg;Ca-vo`C)mAG?T*Gj8jGiJD2bhF5dfq{V= zh(Ul6LV;;EC>un>Fhh91<9!y8DrScG3s412P#Q#mq+o_H{09OA4@uQ1lS&~F-tSNd zjt&NfuM$u*S%5Sr5QCyYq98FjJ25>~p*TM+QLiAWs32LdB(=E2+O!cIeUO-hY6McC zs2q(xM0n7c1&qED^ymXc0Vq3yqX8OCATdz1!T>Xn0g~qe35@14P$$S zSLT%@Rw&piC@Dc>3=~7GNNxhf9w^2HL1LpZ1_}jQ0$_~Qfn$sbQ7$59EI~w!6%^%{ zz=(Uk3IH_mY<2(u literal 0 HcmV?d00001 diff --git a/unit-test/test_data.proto b/unit-test/proto2/test_data.proto similarity index 90% rename from unit-test/test_data.proto rename to unit-test/proto2/test_data.proto index 1fc7305..1d822fe 100644 --- a/unit-test/test_data.proto +++ b/unit-test/proto2/test_data.proto @@ -1,3 +1,5 @@ +syntax = "proto2"; + package sofa.pbrpc.test; message TestData { diff --git a/unit-test/proto3/test_data.proto b/unit-test/proto3/test_data.proto new file mode 100644 index 0000000..7e4a374 --- /dev/null +++ b/unit-test/proto3/test_data.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package sofa.pbrpc.test; + +message TestData { + int32 v1 = 1; + int64 v2 = 2; + bool v3 = 3; + bytes v4 = 4; + string v5 = 5; +} diff --git a/unit-test/test_buffer.cc b/unit-test/test_buffer.cc index 9ff9ed7..2134719 100644 --- a/unit-test/test_buffer.cc +++ b/unit-test/test_buffer.cc @@ -16,8 +16,14 @@ #include // ATTENTION: include source file for different block size #include #include -#include -#include "test_data.pb.h" + +#if defined ( SOFA_PBRPC_USE_PROTO3 ) +#include +#include "proto3/test_data.pb.h" +#else +#include +#include "proto2/test_data.pb.h" +#endif using namespace sofa::pbrpc; From 4c6ad61052bc3ace282d44a518c2caefb5ec7dfd Mon Sep 17 00:00:00 2001 From: zd-double Date: Mon, 1 Aug 2016 10:39:16 +0800 Subject: [PATCH 5/6] initialize service_time in RpcControllerImpl construct function --- src/sofa/pbrpc/rpc_controller_impl.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/sofa/pbrpc/rpc_controller_impl.h b/src/sofa/pbrpc/rpc_controller_impl.h index e4e62b6..5c04aae 100644 --- a/src/sofa/pbrpc/rpc_controller_impl.h +++ b/src/sofa/pbrpc/rpc_controller_impl.h @@ -51,7 +51,13 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_this( + google::protobuf::ServiceOptions::default_instance()); + service_options.SetExtension(service_timeout, 10000); + } virtual ~RpcControllerImpl() {} @@ -245,10 +251,8 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_thisoptions().GetExtension(method_timeout) : method->service()->options().GetExtension(service_timeout); if (timeout_in_ms <= 0) { - // In protobuf v3 default option is not support. - // For numeric types, the default value is zero. - // set timeout_in_ms to 10 seconds when service_timeout equal to zero. - timeout_in_ms = 10000; + // Just a protection, it shouldn't happen. + timeout_in_ms = 1; } _auto_options.timeout = timeout_in_ms; } From 4a24999f7ad0b5803bda7d60bd9354883ca0ec05 Mon Sep 17 00:00:00 2001 From: zd-double Date: Mon, 1 Aug 2016 20:52:02 +0800 Subject: [PATCH 6/6] remove default value in builtin proto --- src/sofa/pbrpc/common_internal.h | 2 ++ src/sofa/pbrpc/proto2/builtin_service.proto | 4 +-- src/sofa/pbrpc/proto2/rpc_option.proto | 13 ++++----- src/sofa/pbrpc/proto3/rpc_option.proto | 7 ++--- src/sofa/pbrpc/rpc_controller_impl.h | 32 ++++++++++----------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/sofa/pbrpc/common_internal.h b/src/sofa/pbrpc/common_internal.h index 91f88c2..a4dc2fb 100644 --- a/src/sofa/pbrpc/common_internal.h +++ b/src/sofa/pbrpc/common_internal.h @@ -21,6 +21,8 @@ namespace sofa { namespace pbrpc { +static const int64 DEFAULT_SERVICE_TIMEOUT = 10000; + class RpcControllerImpl; typedef sofa::pbrpc::shared_ptr RpcControllerImplPtr; typedef sofa::pbrpc::weak_ptr RpcControllerImplWPtr; diff --git a/src/sofa/pbrpc/proto2/builtin_service.proto b/src/sofa/pbrpc/proto2/builtin_service.proto index 9b50610..3963803 100644 --- a/src/sofa/pbrpc/proto2/builtin_service.proto +++ b/src/sofa/pbrpc/proto2/builtin_service.proto @@ -110,8 +110,8 @@ message ServiceStat { } message StatRequest { - optional string service_name = 1 [default = "all"]; - optional int64 period_seconds = 2 [default = 60]; // expect period in seconds + optional string service_name = 1; + optional int64 period_seconds = 2; // expect period in seconds } message StatResponse { diff --git a/src/sofa/pbrpc/proto2/rpc_option.proto b/src/sofa/pbrpc/proto2/rpc_option.proto index d2e80d9..ee281d0 100644 --- a/src/sofa/pbrpc/proto2/rpc_option.proto +++ b/src/sofa/pbrpc/proto2/rpc_option.proto @@ -11,8 +11,8 @@ import "google/protobuf/descriptor.proto"; package sofa.pbrpc; extend google.protobuf.ServiceOptions { - // Timeout in milli-seconds at service level. Default is 10 seconds. - optional int64 service_timeout = 20000 [default = 10000]; + // Timeout in milli-seconds at service level. + optional int64 service_timeout = 20000; } enum CompressType { @@ -24,11 +24,10 @@ enum CompressType { } extend google.protobuf.MethodOptions { - // Timeout in milli-seconds at method level. There is no default value because - // method without method_timeout set will use service_timeout. + // Timeout in milli-seconds at method level. optional int64 method_timeout = 20000; - // Compress type. Default is no compression. - optional CompressType request_compress_type = 20001 [default = CompressTypeNone]; - optional CompressType response_compress_type = 20002 [default = CompressTypeNone]; + // Compress type. + optional CompressType request_compress_type = 20001; + optional CompressType response_compress_type = 20002; } diff --git a/src/sofa/pbrpc/proto3/rpc_option.proto b/src/sofa/pbrpc/proto3/rpc_option.proto index 3524746..881b500 100644 --- a/src/sofa/pbrpc/proto3/rpc_option.proto +++ b/src/sofa/pbrpc/proto3/rpc_option.proto @@ -11,7 +11,7 @@ import "google/protobuf/descriptor.proto"; package sofa.pbrpc; extend google.protobuf.ServiceOptions { - // Timeout in milli-seconds at service level. Default is 10 seconds. + // Timeout in milli-seconds at service level. int64 service_timeout = 20000; } @@ -24,11 +24,10 @@ enum CompressType { } extend google.protobuf.MethodOptions { - // Timeout in milli-seconds at method level. There is no default value because - // method without method_timeout set will use service_timeout. + // Timeout in milli-seconds at method level. int64 method_timeout = 20000; - // Compress type. Default is no compression. + // Compress type. CompressType request_compress_type = 20001; CompressType response_compress_type = 20002; } diff --git a/src/sofa/pbrpc/rpc_controller_impl.h b/src/sofa/pbrpc/rpc_controller_impl.h index 5c04aae..0d8b749 100644 --- a/src/sofa/pbrpc/rpc_controller_impl.h +++ b/src/sofa/pbrpc/rpc_controller_impl.h @@ -51,13 +51,7 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_this( - google::protobuf::ServiceOptions::default_instance()); - service_options.SetExtension(service_timeout, 10000); - } + {} virtual ~RpcControllerImpl() {} @@ -247,22 +241,28 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_thisfull_name(); if (_user_options.timeout <= 0) { - int64 timeout_in_ms = method->options().HasExtension(method_timeout) ? - method->options().GetExtension(method_timeout) : - method->service()->options().GetExtension(service_timeout); - if (timeout_in_ms <= 0) { - // Just a protection, it shouldn't happen. - timeout_in_ms = 1; + if (method->options().HasExtension(method_timeout)) + { + _auto_options.timeout = method->options().GetExtension(method_timeout); + } + else if (method->service()->options().HasExtension(service_timeout)) + { + _auto_options.timeout = method->service()->options().GetExtension(service_timeout); + } + else + { + _auto_options.timeout = DEFAULT_SERVICE_TIMEOUT; } - _auto_options.timeout = timeout_in_ms; } - if (_user_options.request_compress_type == CompressTypeAuto) { + if (_user_options.request_compress_type == CompressTypeAuto) + { _auto_options.request_compress_type = method->options().HasExtension(request_compress_type) ? method->options().GetExtension(request_compress_type) : CompressTypeNone; } - if (_user_options.response_compress_type == CompressTypeAuto) { + if (_user_options.response_compress_type == CompressTypeAuto) + { _auto_options.response_compress_type = method->options().HasExtension(response_compress_type) ? method->options().GetExtension(response_compress_type) :