Merge pull request #7594 from chedeti/grpc-thrift-v2

grpc thrift integration
pull/7670/merge
kpayson64 8 years ago committed by GitHub
commit b80d9c9626
  1. 3
      .gitmodules
  2. 2
      Makefile
  3. 8
      build.yaml
  4. 219
      include/grpc++/impl/codegen/thrift_serializer.h
  5. 85
      include/grpc++/impl/codegen/thrift_utils.h
  6. 1
      third_party/thrift
  7. 67
      tools/grift/Dockerfile
  8. 26
      tools/grift/README.md
  9. 2505
      tools/grift/grpc_plugins_generator.patch
  10. 1
      tools/run_tests/sanity/check_submodules.sh
  11. 20
      tools/run_tests/sources_and_headers.json
  12. 2
      vsprojects/vcxproj/grpc++_test_util/grpc++_test_util.vcxproj
  13. 6
      vsprojects/vcxproj/grpc++_test_util/grpc++_test_util.vcxproj.filters

3
.gitmodules vendored

@ -17,3 +17,6 @@
[submodule "third_party/nanopb"]
path = third_party/nanopb
url = https://github.com/nanopb/nanopb.git
[submodule "third_party/thrift"]
path = third_party/thrift
url = https://github.com/apache/thrift.git

@ -4060,6 +4060,8 @@ PUBLIC_HEADERS_CXX += \
include/grpc/impl/codegen/time.h \
include/grpc++/impl/codegen/proto_utils.h \
include/grpc++/impl/codegen/config_protobuf.h \
include/grpc++/impl/codegen/thrift_serializer.h \
include/grpc++/impl/codegen/thrift_utils.h \
LIBGRPC++_TEST_UTIL_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_TEST_UTIL_SRC))))

@ -789,6 +789,13 @@ filegroups:
- src/cpp/ext/reflection.pb.cc
uses:
- grpc++_codegen_proto
- name: thrift_util
language: c++
public_headers:
- include/grpc++/impl/codegen/thrift_serializer.h
- include/grpc++/impl/codegen/thrift_utils.h
uses:
- grpc++_codegen_base
libs:
- name: gpr
build: all
@ -1031,6 +1038,7 @@ libs:
- grpc++_codegen_base_src
- grpc++_codegen_proto
- grpc++_config_proto
- thrift_util
- name: grpc++_unsecure
build: all
language: c++

@ -0,0 +1,219 @@
/*
*
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_CODEGEN_THRIFT_SERIALIZER_H
#define GRPCXX_IMPL_CODEGEN_THRIFT_SERIALIZER_H
#include <grpc/impl/codegen/byte_buffer.h>
#include <grpc/impl/codegen/byte_buffer_reader.h>
#include <grpc/impl/codegen/slice.h>
#include <grpc/impl/codegen/slice_buffer.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TCompactProtocol.h>
#include <thrift/protocol/TProtocolException.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TTransportUtils.h>
#include <boost/make_shared.hpp>
#include <memory>
#include <stdexcept>
#include <string>
namespace apache {
namespace thrift {
namespace util {
using apache::thrift::protocol::TBinaryProtocolT;
using apache::thrift::protocol::TCompactProtocolT;
using apache::thrift::protocol::TMessageType;
using apache::thrift::protocol::TNetworkBigEndian;
using apache::thrift::transport::TMemoryBuffer;
using apache::thrift::transport::TBufferBase;
using apache::thrift::transport::TTransport;
template <typename Dummy, typename Protocol>
class ThriftSerializer {
public:
ThriftSerializer()
: prepared_(false),
last_deserialized_(false),
serialize_version_(false) {}
virtual ~ThriftSerializer() {}
// Serialize the passed type into the internal buffer
// and returns a pointer to internal buffer and its size
template <typename T>
void Serialize(const T& fields, const uint8_t** serialized_buffer,
size_t* serialized_len) {
// prepare or reset buffer
if (!prepared_ || last_deserialized_) {
prepare();
} else {
buffer_->resetBuffer();
}
last_deserialized_ = false;
// if required serialize protocol version
if (serialize_version_) {
protocol_->writeMessageBegin("", TMessageType(0), 0);
}
// serialize fields into buffer
fields.write(protocol_.get());
// write the end of message
if (serialize_version_) {
protocol_->writeMessageEnd();
}
uint8_t* byte_buffer;
uint32_t byte_buffer_size;
buffer_->getBuffer(&byte_buffer, &byte_buffer_size);
*serialized_buffer = byte_buffer;
*serialized_len = byte_buffer_size;
}
// Serialize the passed type into the byte buffer
template <typename T>
void Serialize(const T& fields, grpc_byte_buffer** bp) {
const uint8_t* byte_buffer;
size_t byte_buffer_size;
Serialize(fields, &byte_buffer, &byte_buffer_size);
gpr_slice slice = gpr_slice_from_copied_buffer(
reinterpret_cast<const char*>(byte_buffer), byte_buffer_size);
*bp = grpc_raw_byte_buffer_create(&slice, 1);
gpr_slice_unref(slice);
}
// Deserialize the passed char array into the passed type, returns the number
// of bytes that have been consumed from the passed string.
template <typename T>
uint32_t Deserialize(uint8_t* serialized_buffer, size_t length, T* fields) {
// prepare buffer if necessary
if (!prepared_) {
prepare();
}
last_deserialized_ = true;
// reset buffer transport
buffer_->resetBuffer(serialized_buffer, length);
// read the protocol version if necessary
if (serialize_version_) {
std::string name = "";
TMessageType mt = static_cast<TMessageType>(0);
int32_t seq_id = 0;
protocol_->readMessageBegin(name, mt, seq_id);
}
// deserialize buffer into fields
uint32_t len = fields->read(protocol_.get());
// read the end of message
if (serialize_version_) {
protocol_->readMessageEnd();
}
return len;
}
// Deserialize the passed byte buffer to passed type, returns the number
// of bytes consumed from byte buffer
template <typename T>
uint32_t Deserialize(grpc_byte_buffer* buffer, T* msg) {
grpc_byte_buffer_reader reader;
grpc_byte_buffer_reader_init(&reader, buffer);
gpr_slice slice = grpc_byte_buffer_reader_readall(&reader);
uint32_t len =
Deserialize(GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice), msg);
gpr_slice_unref(slice);
grpc_byte_buffer_reader_destroy(&reader);
return len;
}
// set serialization version flag
void SetSerializeVersion(bool value) { serialize_version_ = value; }
// Set the container size limit to deserialize
// This function should be called after buffer_ is initialized
void SetContainerSizeLimit(int32_t container_limit) {
if (!prepared_) {
prepare();
}
protocol_->setContainerSizeLimit(container_limit);
}
// Set the string size limit to deserialize
// This function should be called after buffer_ is initialized
void SetStringSizeLimit(int32_t string_limit) {
if (!prepared_) {
prepare();
}
protocol_->setStringSizeLimit(string_limit);
}
private:
bool prepared_;
bool last_deserialized_;
boost::shared_ptr<TMemoryBuffer> buffer_;
std::shared_ptr<Protocol> protocol_;
bool serialize_version_;
void prepare() {
buffer_ = boost::make_shared<TMemoryBuffer>();
// create a protocol for the memory buffer transport
protocol_ = std::make_shared<Protocol>(buffer_);
prepared_ = true;
}
}; // ThriftSerializer
typedef ThriftSerializer<void, TBinaryProtocolT<TBufferBase, TNetworkBigEndian>>
ThriftSerializerBinary;
typedef ThriftSerializer<void, TCompactProtocolT<TBufferBase>>
ThriftSerializerCompact;
} // namespace util
} // namespace thrift
} // namespace apache
#endif

@ -0,0 +1,85 @@
/*
*
* Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H
#define GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H
#include <grpc++/impl/codegen/config.h>
#include <grpc++/impl/codegen/core_codegen_interface.h>
#include <grpc++/impl/codegen/serialization_traits.h>
#include <grpc++/impl/codegen/status.h>
#include <grpc++/impl/codegen/status_code_enum.h>
#include <grpc++/impl/codegen/thrift_serializer.h>
#include <grpc/impl/codegen/byte_buffer.h>
#include <grpc/impl/codegen/byte_buffer_reader.h>
#include <grpc/impl/codegen/slice.h>
#include <grpc/impl/codegen/slice_buffer.h>
#include <cstdint>
#include <cstdlib>
namespace grpc {
using apache::thrift::util::ThriftSerializerCompact;
template <class T>
class SerializationTraits<T, typename std::enable_if<std::is_base_of<
apache::thrift::TBase, T>::value>::type> {
public:
static Status Serialize(const T& msg, grpc_byte_buffer** bp,
bool* own_buffer) {
*own_buffer = true;
ThriftSerializerCompact serializer;
serializer.Serialize(msg, bp);
return Status(StatusCode::OK, "ok");
}
static Status Deserialize(grpc_byte_buffer* buffer, T* msg,
int max_message_size) {
if (!buffer) {
return Status(StatusCode::INTERNAL, "No payload");
}
ThriftSerializerCompact deserializer;
deserializer.Deserialize(buffer, msg);
grpc_byte_buffer_destroy(buffer);
return Status(StatusCode::OK, "ok");
}
};
} // namespace grpc
#endif // GRPCXX_IMPL_CODEGEN_THRIFT_UTILS_H

@ -0,0 +1 @@
Subproject commit bcad91771b7f0bff28a1cac1981d7ef2b9bcef3c

@ -0,0 +1,67 @@
# Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
FROM ubuntu:14.04
RUN apt-get update && \
apt-get install -y \
git build-essential \
pkg-config flex \
bison \
libkrb5-dev \
libsasl2-dev \
libnuma-dev \
pkg-config \
libssl-dev \
autoconf libtool \
cmake \
libiberty-dev \
g++ unzip \
curl make automake libtool libboost-dev
# Configure git
RUN git config --global user.name "Jenkins" && \
git config --global user.email "jenkins@grpc"
# Clone gRPC
RUN git clone https://github.com/grpc/grpc
# Update Submodules
RUN cd grpc && git submodule update --init
# Install protobuf
RUN cd grpc/third_party/protobuf && ./autogen.sh && ./configure && \
make -j && make check -j && make install && ldconfig
# Install gRPC
RUN cd grpc && make -j && make install
# Install thrift
RUN cd grpc/third_party/thrift && git am --signoff < ../../tools/grift/grpc_plugins_generator.patch && \
./bootstrap.sh && ./configure && make -j && make install

@ -0,0 +1,26 @@
Copyright 2016 Google Inc.
#Documentation
grift is integration of [Apache Thrift](https://github.com/apache/thrift.git) Serializer with gRPC.
This integration allows you to use grpc to send thrift messages in C++ and java.
grift uses Compact Protocol to serialize thrift messages.
##generating grpc plugins for thrift services
###CPP
```sh
$ thrift --gen cpp <thrift-file>
```
###JAVA
```sh
$ thrift --gen java <thrift-file>
```
#Installation
Before Installing thrift make sure to apply this [patch](grpc_plugins_generator.patch) to third_party/thrift.
Go to third_party/thrift and follow the [INSTALLATION](https://github.com/apache/thrift.git) instructions to install thrift with commit id bcad91771b7f0bff28a1cac1981d7ef2b9bcef3c.

File diff suppressed because it is too large Load Diff

@ -47,6 +47,7 @@ cat << EOF | awk '{ print $1 }' | sort > $want_submodules
f8ac463766281625ad710900479130c7fcb4d63b third_party/nanopb (nanopb-0.3.4-29-gf8ac463)
e8ae137c96444ea313485ed1118c5e43b2099cf1 third_party/protobuf (v3.0.0-beta-4-74-ge8ae137)
50893291621658f355bc5b4d450a8d06a563053d third_party/zlib (v1.2.8)
bcad91771b7f0bff28a1cac1981d7ef2b9bcef3c third_party/thrift
EOF
diff -u $submodules $want_submodules

@ -4468,7 +4468,8 @@
"grpc++_codegen_base_src",
"grpc++_codegen_proto",
"grpc++_config_proto",
"grpc_test_util"
"grpc_test_util",
"thrift_util"
],
"headers": [
"src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h",
@ -6870,5 +6871,22 @@
],
"third_party": false,
"type": "filegroup"
},
{
"deps": [
"grpc++_codegen_base"
],
"headers": [
"include/grpc++/impl/codegen/thrift_serializer.h",
"include/grpc++/impl/codegen/thrift_utils.h"
],
"language": "c++",
"name": "thrift_util",
"src": [
"include/grpc++/impl/codegen/thrift_serializer.h",
"include/grpc++/impl/codegen/thrift_utils.h"
],
"third_party": false,
"type": "filegroup"
}
]

@ -200,6 +200,8 @@
<ClInclude Include="$(SolutionDir)\..\include\grpc\impl\codegen\time.h" />
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\proto_utils.h" />
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\config_protobuf.h" />
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\thrift_serializer.h" />
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\thrift_utils.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(SolutionDir)\..\test\cpp\end2end\test_service_impl.h" />

@ -192,6 +192,12 @@
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\config_protobuf.h">
<Filter>include\grpc++\impl\codegen</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\thrift_serializer.h">
<Filter>include\grpc++\impl\codegen</Filter>
</ClInclude>
<ClInclude Include="$(SolutionDir)\..\include\grpc++\impl\codegen\thrift_utils.h">
<Filter>include\grpc++\impl\codegen</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(SolutionDir)\..\test\cpp\end2end\test_service_impl.h">

Loading…
Cancel
Save