From 93bb51ba9bcfbcd56291119ab37abf88934ee807 Mon Sep 17 00:00:00 2001 From: Elliott Clark Date: Tue, 12 Apr 2016 12:03:52 -0700 Subject: [PATCH] HBASE-15620 Add on Call serialization Summary: Add on delimited serialization so that request headers and request payloads can be serialized. Test Plan: Add a unit test. Differential Revision: https://reviews.facebook.net/D56757 --- hbase-native-client/core/BUCK | 9 ++ hbase-native-client/core/client-dispatcher.h | 2 +- .../core/client-serialize-handler.cc | 58 +-------- .../core/client-serialize-handler.h | 17 +-- hbase-native-client/core/client-serializer-test.cc | 26 ++++ hbase-native-client/core/client-serializer.cc | 136 +++++++++++++++++++++ hbase-native-client/core/client-serializer.h | 54 ++++++++ hbase-native-client/core/client.h | 2 +- hbase-native-client/core/connection-factory.h | 2 +- hbase-native-client/core/get-request.h | 2 +- hbase-native-client/core/get-result.h | 2 +- hbase-native-client/core/location-cache.cc | 3 +- hbase-native-client/core/location-cache.h | 2 +- hbase-native-client/core/pipeline.h | 2 +- hbase-native-client/core/request.h | 2 +- hbase-native-client/core/response.h | 2 +- hbase-native-client/core/service.h | 2 +- hbase-native-client/core/table-name.h | 2 +- 18 files changed, 251 insertions(+), 74 deletions(-) create mode 100644 hbase-native-client/core/client-serializer-test.cc create mode 100644 hbase-native-client/core/client-serializer.cc create mode 100644 hbase-native-client/core/client-serializer.h diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK index 2b00d66..099f750 100644 --- a/hbase-native-client/core/BUCK +++ b/hbase-native-client/core/BUCK @@ -20,6 +20,7 @@ cxx_library(name="core", "admin.h", "client-dispatcher.h", "client-serialize-handler.h", + "client-serializer.h", "client.h", "connection-factory.h", "connection.h", @@ -43,6 +44,7 @@ cxx_library(name="core", "admin.cc", "client-dispatcher.cc", "client-serialize-handler.cc", + "client-serializer.cc", "client.cc", "connection-factory.cc", "connection.cc", @@ -76,6 +78,13 @@ cxx_test(name="simple-test", ":core", ], run_test_separately=True, ) +cxx_test(name="client-ser-test", + srcs=[ + "client-serializer-test.cc", + ], + deps=[ + ":core", + ],) cxx_test(name="location-cache-test", srcs=[ "native-client-test-env.cc", diff --git a/hbase-native-client/core/client-dispatcher.h b/hbase-native-client/core/client-dispatcher.h index 4b9d35a..68e1d7f 100644 --- a/hbase-native-client/core/client-dispatcher.h +++ b/hbase-native-client/core/client-dispatcher.h @@ -39,4 +39,4 @@ private: std::unordered_map> requests_; uint32_t current_call_id_ = 1; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/client-serialize-handler.cc b/hbase-native-client/core/client-serialize-handler.cc index cad1308..09fa198 100644 --- a/hbase-native-client/core/client-serialize-handler.cc +++ b/hbase-native-client/core/client-serialize-handler.cc @@ -18,6 +18,8 @@ */ #include "core/client-serialize-handler.h" +#include "core/request.h" +#include "core/response.h" #include @@ -25,11 +27,6 @@ using namespace hbase; using namespace folly; using namespace wangle; -static const std::string PREAMBLE = "HBas"; -static const std::string INTERFACE = "ClientService"; -static const uint8_t RPC_VERSION = 0; -static const uint8_t AUTH_TYPE = 80; - // TODO(eclark): Make this actually do ANYTHING. void ClientSerializeHandler::read(Context *ctx, std::unique_ptr msg) { Response received; @@ -47,58 +44,13 @@ Future ClientSerializeHandler::write(Context *ctx, Request r) { write_header(ctx); } - // Send out the actual request and not just a test string. - std::string out{"test"}; - return ctx->fireWrite(prepend_length(IOBuf::copyBuffer(out))); + return ctx->fireWrite(serializer_.request(r)); } Future ClientSerializeHandler::write_preamble(Context *ctx) { - auto magic = IOBuf::copyBuffer(PREAMBLE); - auto buf = IOBuf::create(2); - buf->append(2); - folly::io::RWPrivateCursor c(buf.get()); - - // Version - c.write(RPC_VERSION); - // Standard security aka Please don't lie to me. - c.write(AUTH_TYPE); - magic->appendChain(std::move(buf)); - return ctx->fireWrite(std::move(magic)); + return ctx->fireWrite(serializer_.preamble()); } Future ClientSerializeHandler::write_header(Context *ctx) { - pb::ConnectionHeader h; - - // TODO(eclark): Make this not a total lie. - h.mutable_user_info()->set_effective_user("elliott"); - // The service name that we want to talk to. - // - // Right now we're completely ignoring the service interface. - // That may or may not be the correct thing to do. - // It worked for a while with the java client; until it - // didn't. - h.set_service_name(INTERFACE); - // TODO(eclark): Make this 1 copy. - auto msg = IOBuf::copyBuffer(h.SerializeAsString()); - return ctx->fireWrite(prepend_length(std::move(msg))); -} - -// Our own simple version of LengthFieldPrepender -std::unique_ptr -ClientSerializeHandler::prepend_length(std::unique_ptr msg) { - // Java ints are 4 long. So create a buffer that large - auto len_buf = IOBuf::create(4); - // Then make those bytes visible. - len_buf->append(4); - - io::RWPrivateCursor c(len_buf.get()); - // Get the size of the data to be pushed out the network. - auto size = msg->computeChainDataLength(); - - // Write the length to this IOBuf. - c.writeBE(static_cast(size)); - - // Then attach the origional to the back of len_buf - len_buf->appendChain(std::move(msg)); - return len_buf; + return ctx->fireWrite(serializer_.header("elliott")); } diff --git a/hbase-native-client/core/client-serialize-handler.h b/hbase-native-client/core/client-serialize-handler.h index 961a03b..19c1ad3 100644 --- a/hbase-native-client/core/client-serialize-handler.h +++ b/hbase-native-client/core/client-serialize-handler.h @@ -20,10 +20,13 @@ #include -#include "if/HBase.pb.h" -#include "if/RPC.pb.h" -#include "core/request.h" -#include "core/response.h" +#include "core/client-serializer.h" + +// Forward decs. +namespace hbase { +class Request; +class Response; +} namespace hbase { class ClientSerializeHandler @@ -36,9 +39,7 @@ public: private: folly::Future write_preamble(Context *ctx); folly::Future write_header(Context *ctx); - // Our own simple version of LengthFieldPrepender - std::unique_ptr - prepend_length(std::unique_ptr msg); bool need_send_header_ = true; + ClientSerializer serializer_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/client-serializer-test.cc b/hbase-native-client/core/client-serializer-test.cc new file mode 100644 index 0000000..145057d --- /dev/null +++ b/hbase-native-client/core/client-serializer-test.cc @@ -0,0 +1,26 @@ +#include + +#include + +#include "core/client-serializer.h" + +using namespace hbase; + +TEST(ClientSerializerTest, PreambleIncludesHBas) { + ClientSerializer ser; + auto buf = ser.preamble(); + const char* p = reinterpret_cast(buf->data()); + // Take the first for chars and make sure they are the + // magic string + EXPECT_EQ("HBas", std::string(p, 4)); + + EXPECT_EQ(6, buf->computeChainDataLength()); +} + + +TEST(ClientSerializerTest, PreambleIncludesVersion) { + ClientSerializer ser; + auto buf = ser.preamble(); + EXPECT_EQ(0, static_cast(buf->data())[4]); + EXPECT_EQ(80, static_cast(buf->data())[5]); +} diff --git a/hbase-native-client/core/client-serializer.cc b/hbase-native-client/core/client-serializer.cc new file mode 100644 index 0000000..29396be --- /dev/null +++ b/hbase-native-client/core/client-serializer.cc @@ -0,0 +1,136 @@ +/* + * 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 "core/client-serializer.h" + +#include +#include +#include + +#include "if/HBase.pb.h" +#include "if/RPC.pb.h" +#include "core/request.h" +#include "core/response.h" + +using namespace hbase; + +using folly::IOBuf; +using folly::io::RWPrivateCursor; +using std::unique_ptr; +using std::string; +using google::protobuf::Message; +using google::protobuf::io::CodedOutputStream; +using google::protobuf::io::ArrayOutputStream; +using google::protobuf::io::ZeroCopyOutputStream; + +static const std::string PREAMBLE = "HBas"; +static const std::string INTERFACE = "ClientService"; +static const uint8_t RPC_VERSION = 0; +static const uint8_t DEFAULT_AUTH_TYPE = 80; + +ClientSerializer::ClientSerializer() : auth_type_(DEFAULT_AUTH_TYPE) {} + +unique_ptr ClientSerializer::preamble() { + auto magic = IOBuf::copyBuffer(PREAMBLE, 0, 2); + magic->append(2); + RWPrivateCursor c(magic.get()); + c.skip(4); + // Version + c.write(RPC_VERSION); + // Standard security aka Please don't lie to me. + c.write(auth_type_); + return magic; +} + +unique_ptr ClientSerializer::header(string user) { + pb::ConnectionHeader h; + + // TODO(eclark): Make this not a total lie. + h.mutable_user_info()->set_effective_user(user); + // The service name that we want to talk to. + // + // Right now we're completely ignoring the service interface. + // That may or may not be the correct thing to do. + // It worked for a while with the java client; until it + // didn't. + h.set_service_name(INTERFACE); + return prepend_length(serialize_message(h)); +} + +unique_ptr ClientSerializer::request(Request &request) { + // Send out the actual request and not just a test string. + pb::RequestHeader rq; + rq.set_call_id(request.call_id()); + rq.set_method_name("get"); + rq.set_request_param(false); + + auto ser_header = serialize_delimited(rq); + + return prepend_length(std::move(ser_header)); +} + +unique_ptr ClientSerializer::prepend_length(unique_ptr msg) { + // Java ints are 4 long. So create a buffer that large + auto len_buf = IOBuf::create(4); + // Then make those bytes visible. + len_buf->append(4); + + RWPrivateCursor c(len_buf.get()); + // Get the size of the data to be pushed out the network. + auto size = msg->computeChainDataLength(); + + // Write the length to this IOBuf. + c.writeBE(static_cast(size)); + + // Then attach the origional to the back of len_buf + len_buf->appendChain(std::move(msg)); + return len_buf; +} + +unique_ptr ClientSerializer::serialize_delimited(Message &msg) { + // Get the buffer size needed for just the message. + int msg_size = msg.ByteSize(); + int buf_size = CodedOutputStream::VarintSize32(msg_size) + msg_size; + + // Create a buffer big enough to hold the varint and the object. + auto buf = IOBuf::create(buf_size); + buf->append(buf_size); + + // Create the array output stream. + ArrayOutputStream aos{buf->writableData(), static_cast(buf->length())}; + // Wrap the ArrayOuputStream in the coded output stream to allow writing + // Varint32 + CodedOutputStream cos{&aos}; + + // Write out the size. + cos.WriteVarint32(msg_size); + + // Now write the rest out. + // We're using the protobuf output streams here to keep track + // of where in the output array we are rather than IOBuf. + msg.SerializeWithCachedSizesToArray( + cos.GetDirectBufferForNBytesAndAdvance(msg_size)); + + // Return the buffer. + return buf; +} +// TODO(eclark): Make this 1 copy. +unique_ptr ClientSerializer::serialize_message(Message &msg) { + auto buf = IOBuf::copyBuffer(msg.SerializeAsString()); + return buf; +} diff --git a/hbase-native-client/core/client-serializer.h b/hbase-native-client/core/client-serializer.h new file mode 100644 index 0000000..86dd7e7 --- /dev/null +++ b/hbase-native-client/core/client-serializer.h @@ -0,0 +1,54 @@ +/* + * 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 +#include + +// Forward +namespace google { +namespace protobuf { +class Message; +} +} +namespace hbase { +class Request; +} + +namespace hbase { +class ClientSerializer { +public: + ClientSerializer(); + std::unique_ptr preamble(); + std::unique_ptr header(std::string user); + std::unique_ptr request(Request &request); + +private: + std::unique_ptr + serialize_message(google::protobuf::Message &msg); + + std::unique_ptr + serialize_delimited(google::protobuf::Message &msg); + + std::unique_ptr + prepend_length(std::unique_ptr msg); + + uint8_t auth_type_; +}; +} // namespace hbase diff --git a/hbase-native-client/core/client.h b/hbase-native-client/core/client.h index 818bc6b..c2dc226 100644 --- a/hbase-native-client/core/client.h +++ b/hbase-native-client/core/client.h @@ -39,4 +39,4 @@ private: LocationCache location_cache; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/connection-factory.h b/hbase-native-client/core/connection-factory.h index 6f450c2..a27019b 100644 --- a/hbase-native-client/core/connection-factory.h +++ b/hbase-native-client/core/connection-factory.h @@ -38,4 +38,4 @@ public: private: wangle::ClientBootstrap bootstrap_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/get-request.h b/hbase-native-client/core/get-request.h index c9113ad..bb755c5 100644 --- a/hbase-native-client/core/get-request.h +++ b/hbase-native-client/core/get-request.h @@ -32,4 +32,4 @@ private: TableName table_name_; std::string key_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/get-result.h b/hbase-native-client/core/get-result.h index e021316..a49ad98 100644 --- a/hbase-native-client/core/get-result.h +++ b/hbase-native-client/core/get-result.h @@ -29,4 +29,4 @@ public: private: std::string key_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/location-cache.cc b/hbase-native-client/core/location-cache.cc index 34e3236..52e86e3 100644 --- a/hbase-native-client/core/location-cache.cc +++ b/hbase-native-client/core/location-cache.cc @@ -70,8 +70,7 @@ ServerName LocationCache::ReadMetaLocation() { // This needs to be int rather than size_t as that's what ZK expects. int len = sizeof(contents); // TODO(elliott): handle disconnects/reconntion as needed. - int zk_result = - zoo_get(this->zk_, META_LOCATION, 0, contents, &len, nullptr); + int zk_result = zoo_get(this->zk_, META_LOCATION, 0, contents, &len, nullptr); if (zk_result != ZOK || len < 9) { LOG(ERROR) << "Error getting meta location."; throw runtime_error("Error getting meta location"); diff --git a/hbase-native-client/core/location-cache.h b/hbase-native-client/core/location-cache.h index efcfde5..28a1ee1 100644 --- a/hbase-native-client/core/location-cache.h +++ b/hbase-native-client/core/location-cache.h @@ -51,4 +51,4 @@ private: zhandle_t *zk_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/pipeline.h b/hbase-native-client/core/pipeline.h index d199d08..fc2c0ac 100644 --- a/hbase-native-client/core/pipeline.h +++ b/hbase-native-client/core/pipeline.h @@ -31,4 +31,4 @@ public: SerializePipeline::Ptr newPipeline(std::shared_ptr sock) override; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/request.h b/hbase-native-client/core/request.h index 39083ed..a2e6521 100644 --- a/hbase-native-client/core/request.h +++ b/hbase-native-client/core/request.h @@ -30,4 +30,4 @@ public: private: uint32_t call_id_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/response.h b/hbase-native-client/core/response.h index 34a284d..61024af 100644 --- a/hbase-native-client/core/response.h +++ b/hbase-native-client/core/response.h @@ -31,4 +31,4 @@ public: private: uint32_t call_id_; }; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/service.h b/hbase-native-client/core/service.h index 880e65f..28bf7bf 100644 --- a/hbase-native-client/core/service.h +++ b/hbase-native-client/core/service.h @@ -23,4 +23,4 @@ namespace hbase { using HBaseService = wangle::Service; -} // namespace hbase +} // namespace hbase diff --git a/hbase-native-client/core/table-name.h b/hbase-native-client/core/table-name.h index 796115b..37c3461 100644 --- a/hbase-native-client/core/table-name.h +++ b/hbase-native-client/core/table-name.h @@ -29,4 +29,4 @@ public: explicit TableName(std::string tableName); explicit TableName(std::string namespaceName, std::string tableName); }; -} // namespace hbase +} // namespace hbase -- 2.8.0-rc2