Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thrift-util.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_RPC_THRIFT_UTIL_H
17 #define IMPALA_RPC_THRIFT_UTIL_H
18 
19 #include <boost/shared_ptr.hpp>
20 #include <thrift/protocol/TBinaryProtocol.h>
21 #include <sstream>
22 #include <vector>
23 #include <thrift/TApplicationException.h>
24 #include <thrift/protocol/TDebugProtocol.h>
25 #include <thrift/transport/TBufferTransports.h>
26 
27 #include "common/status.h"
28 
29 namespace impala {
30 
31 class TColumnValue;
32 class TNetworkAddress;
33 class ThriftServer;
34 
40  public:
44  ThriftSerializer(bool compact, int initial_buffer_size = 1024);
45 
47  template <class T>
48  Status Serialize(T* obj, std::vector<uint8_t>* result) {
49  uint32_t len = 0;
50  uint8_t* buffer = NULL;
51  RETURN_IF_ERROR(Serialize<T>(obj, &len, &buffer));
52  result->resize(len);
53  memcpy(&((*result)[0]), buffer, len);
54  return Status::OK;
55  }
56 
60  template <class T>
61  Status Serialize(T* obj, uint32_t* len, uint8_t** buffer) {
62  try {
63  mem_buffer_->resetBuffer();
64  obj->write(protocol_.get());
65  } catch (std::exception& e) {
66  std::stringstream msg;
67  msg << "Couldn't serialize thrift object:\n" << e.what();
68  return Status(msg.str());
69  }
70  mem_buffer_->getBuffer(buffer, len);
71  return Status::OK;
72  }
73 
74  template <class T>
75  Status Serialize(T* obj, std::string* result) {
76  try {
77  mem_buffer_->resetBuffer();
78  obj->write(protocol_.get());
79  } catch (std::exception& e) {
80  std::stringstream msg;
81  msg << "Couldn't serialize thrift object:\n" << e.what();
82  return Status(msg.str());
83  }
84  *result = mem_buffer_->getBufferAsString();
85  return Status::OK;
86  }
87 
88  private:
89  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> mem_buffer_;
90  boost::shared_ptr<apache::thrift::protocol::TProtocol> protocol_;
91 };
92 
94  public:
95  ThriftDeserializer(bool compact);
96 
97  private:
98  boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> factory_;
99  boost::shared_ptr<apache::thrift::protocol::TProtocol> tproto_;
100 };
101 
103 boost::shared_ptr<apache::thrift::protocol::TProtocol>
105  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> mem, bool compact);
106 
110 template <class T>
111 Status DeserializeThriftMsg(const uint8_t* buf, uint32_t* len, bool compact,
112  T* deserialized_msg) {
116  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> tmem_transport(
117  new apache::thrift::transport::TMemoryBuffer(const_cast<uint8_t*>(buf), *len));
118  boost::shared_ptr<apache::thrift::protocol::TProtocol> tproto =
119  CreateDeserializeProtocol(tmem_transport, compact);
120  try {
121  deserialized_msg->read(tproto.get());
122  } catch (std::exception& e) {
123  std::stringstream msg;
124  msg << "couldn't deserialize thrift msg:\n" << e.what();
125  return Status(msg.str());
126  } catch (...) {
128  return Status("Unknown exception");
129  }
130  uint32_t bytes_left = tmem_transport->available_read();
131  *len = *len - bytes_left;
132  return Status::OK;
133 }
134 
136 void InitThriftLogging();
137 
140 Status WaitForLocalServer(const ThriftServer& server, int num_retries,
141  int retry_interval_ms);
142 
144 Status WaitForServer(const std::string& host, int port, int num_retries,
145  int retry_interval_ms);
146 
148 std::ostream& operator<<(std::ostream& out, const TColumnValue& colval);
149 
152 bool TNetworkAddressComparator(const TNetworkAddress& a, const TNetworkAddress& b);
153 
155 bool IsTimeoutTException(const apache::thrift::TException& e);
156 
157 }
158 
159 #endif
void InitThriftLogging()
Redirects all Thrift logging to VLOG(1)
Definition: thrift-util.cc:112
ThriftSerializer(bool compact, int initial_buffer_size=1024)
Definition: thrift-util.cc:66
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
Status Serialize(T *obj, std::string *result)
Definition: thrift-util.h:75
ThriftDeserializer(bool compact)
boost::shared_ptr< apache::thrift::protocol::TProtocol > protocol_
Definition: thrift-util.h:90
bool TNetworkAddressComparator(const TNetworkAddress &a, const TNetworkAddress &b)
Definition: thrift-util.cc:168
Status Serialize(T *obj, std::vector< uint8_t > *result)
Serializes obj into result. Result will contain a copy of the memory.
Definition: thrift-util.h:48
bool IsTimeoutTException(const TException &e)
Definition: thrift-util.cc:175
Status Serialize(T *obj, uint32_t *len, uint8_t **buffer)
Definition: thrift-util.h:61
Status WaitForServer(const string &host, int port, int num_retries, int retry_interval_ms)
Definition: thrift-util.cc:121
static const Status OK
Definition: status.h:87
Status WaitForLocalServer(const ThriftServer &server, int num_retries, int retry_interval_ms)
Definition: thrift-util.cc:116
boost::shared_ptr< apache::thrift::transport::TMemoryBuffer > mem_buffer_
Definition: thrift-util.h:89
Status DeserializeThriftMsg(JNIEnv *env, jbyteArray serialized_msg, T *deserialized_msg)
shared_ptr< TProtocol > CreateDeserializeProtocol(shared_ptr< TMemoryBuffer > mem, bool compact)
Definition: thrift-util.cc:77
boost::shared_ptr< apache::thrift::protocol::TProtocolFactory > factory_
Definition: thrift-util.h:98
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
boost::shared_ptr< apache::thrift::protocol::TProtocol > tproto_
Definition: thrift-util.h:99