From 5e714e31e60d77a667fcd59381921247c8723370 Mon Sep 17 00:00:00 2001 From: Xiaobing Zhou Date: Fri, 7 Jul 2017 14:47:27 -0700 Subject: [PATCH] HBASE-18338. [C++] Implement RpcTestServer --- hbase-native-client/connection/BUCK | 5 ++ hbase-native-client/connection/rpc-test-server.cc | 39 +++++++++++ hbase-native-client/connection/rpc-test-server.h | 82 +++++++++++++++++++++++ hbase-native-client/connection/rpc-test.cc | 42 ++++++++++++ hbase-native-client/if/test.proto | 43 ++++++++++++ hbase-native-client/if/test_rpc_service.proto | 35 ++++++++++ 6 files changed, 246 insertions(+) create mode 100644 hbase-native-client/connection/rpc-test-server.cc create mode 100644 hbase-native-client/connection/rpc-test-server.h create mode 100644 hbase-native-client/connection/rpc-test.cc create mode 100644 hbase-native-client/if/test.proto create mode 100644 hbase-native-client/if/test_rpc_service.proto diff --git a/hbase-native-client/connection/BUCK b/hbase-native-client/connection/BUCK index c3119eb..5da0911 100644 --- a/hbase-native-client/connection/BUCK +++ b/hbase-native-client/connection/BUCK @@ -68,3 +68,8 @@ cxx_test( deps=[ ":connection", ],) +cxx_test( + name="rpc-test", + srcs=["rpc-test.cc",], + deps=[":connection",], + run_test_separately=True,) \ No newline at end of file diff --git a/hbase-native-client/connection/rpc-test-server.cc b/hbase-native-client/connection/rpc-test-server.cc new file mode 100644 index 0000000..9c8491c --- /dev/null +++ b/hbase-native-client/connection/rpc-test-server.cc @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include "connection/rpc-test-server.h" + +namespace hbase { + +RpcTestServerHandler::RpcTestServerHandler(const string& client_addr) : + client_addr_(client_addr) { +} + +void RpcTestServerHandler::read( + Context *ctx, + std::unique_ptr msg) { + //TODO: +} + +folly::Future RpcTestServerHandler::write( + Context *ctx, + std::unique_ptr resp) { + //TODO: +} +} // namespace hbase + diff --git a/hbase-native-client/connection/rpc-test-server.h b/hbase-native-client/connection/rpc-test-server.h new file mode 100644 index 0000000..5cbbae4 --- /dev/null +++ b/hbase-native-client/connection/rpc-test-server.h @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#pragma once + +#include "wangle/bootstrap/ServerBootstrap.h" +#include + +#include "connection/pipeline.h" +#include "connection/request.h" +#include "connection/response.h" + + +namespace hbase { + +typedef ClientBootstrap TestClient; +typedef ServerBootstrap RpcTestServer; + +class RpcTestServerHandler + : public wangle::Handler, std::unique_ptr, + std::unique_ptr, std::unique_ptr> { +public: + RpcTestServerHandler(const string& client_addr); + /** + * Get bytes from the wire. + * This should be the full message as the length field decoder should be + * in the pipeline before this. + */ + void read(Context *ctx, std::unique_ptr buf) override; + + /** + * Write the data down the wire. + */ + folly::Future write(Context *ctx, std::unique_ptr resp) + override; + +private: + std::string client_addr_; // for logging +}; + +class RpcTestClientPipelineFactory: public ClientHandler { +public: + RpcTestClientPipelineFactory() {} + RpcTestClientPipelineFactory(std::string user_name, + std::shared_ptr codec, const std::string &server) : + ClientHandler(user_name, codec, server) { + } +}; + +class RpcTestServerPipelineFactory : public PipelineFactory { + public: + SerializePipeline::Ptr newPipeline( + std::shared_ptr sock) override { + folly::SocketAddress addr; // for logging + sock->getPeerAddress(&addr); + + auto pipeline = SerializePipeline::create(); + pipeline->addBack(wangle::AsyncSocketHandler{sock}); + pipeline->addBack(wangle::EventBaseHandler{}); + pipeline->addBack(wangle::LengthFieldBasedFrameDecoder{}); + pipeline->addBack(RpcTestServerHandler{addr.describe()}); + pipeline->finalize(); + return pipeline; + } +}; +} // namespace hbase diff --git a/hbase-native-client/connection/rpc-test.cc b/hbase-native-client/connection/rpc-test.cc new file mode 100644 index 0000000..718ed2c --- /dev/null +++ b/hbase-native-client/connection/rpc-test.cc @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "wangle/bootstrap/ClientBootstrap.h" +#include "wangle/channel/Handler.h" + +#include +#include +#include +#include +#include + +#include "connection/rpc-test-server.h" + +using namespace wangle; +using namespace folly; + +namespace hbase { + +TEST(RPC, ServerWithPipeline) { + RpcTestServer server; + server.childPipeline(std::make_shared()); + server.bind(0); + server.stop(); +} +} // namespace hbase diff --git a/hbase-native-client/if/test.proto b/hbase-native-client/if/test.proto new file mode 100644 index 0000000..647fafe --- /dev/null +++ b/hbase-native-client/if/test.proto @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +option java_package = "org.apache.hadoop.hbase.shaded.ipc.protobuf.generated"; +option java_outer_classname = "TestProtos"; +option java_generate_equals_and_hash = true; + +message EmptyRequestProto { +} + +message EmptyResponseProto { +} + +message EchoRequestProto { + required string message = 1; +} + +message EchoResponseProto { + required string message = 1; +} + +message PauseRequestProto { + required uint32 ms = 1; +} + +message AddrResponseProto { + required string addr = 1; +} diff --git a/hbase-native-client/if/test_rpc_service.proto b/hbase-native-client/if/test_rpc_service.proto new file mode 100644 index 0000000..51472db --- /dev/null +++ b/hbase-native-client/if/test_rpc_service.proto @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +option java_package = "org.apache.hadoop.hbase.shaded.ipc.protobuf.generated"; +option java_outer_classname = "TestRpcServiceProtos"; +option java_generic_services = true; +option java_generate_equals_and_hash = true; + +import "test.proto"; + + +/** + * A protobuf service for use in tests + */ +service TestProtobufRpcProto { + rpc ping(EmptyRequestProto) returns (EmptyResponseProto); + rpc echo(EchoRequestProto) returns (EchoResponseProto); + rpc error(EmptyRequestProto) returns (EmptyResponseProto); + rpc pause(PauseRequestProto) returns (EmptyResponseProto); + rpc addr(EmptyRequestProto) returns (AddrResponseProto); +} -- 2.10.1 (Apple Git-78)