Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
status.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_COMMON_STATUS_H
17 #define IMPALA_COMMON_STATUS_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include <boost/lexical_cast.hpp>
23 
24 #include "common/logging.h"
25 #include "common/compiler-util.h"
26 #include "gen-cpp/Status_types.h" // for TStatus
27 #include "gen-cpp/ErrorCodes_types.h" // for TErrorCode
28 #include "gen-cpp/TCLIService_types.h" // for HS2 TStatus
29 #include "util/error-util.h" // for ErrorMessage
30 
31 #define STATUS_API_VERSION 2
32 
33 namespace impala {
34 
39 //
44 //
48 //
52 //
55 //
62 //
66 //
74 //
77 //
81 class Status {
82  public:
83  typedef strings::internal::SubstituteArg ArgType;
84 
85  Status(): msg_(NULL) {}
86 
87  static const Status OK;
88  static const Status CANCELLED;
89  static const Status MEM_LIMIT_EXCEEDED;
90  static const Status DEPRECATED_RPC;
91 
93  Status(const Status& status)
94  : msg_(status.msg_ != NULL ? new ErrorMsg(*status.msg_) : NULL) { }
95 
98  Status(TErrorCode::type code);
99 
103  Status(TErrorCode::type error, const ArgType& arg0);
104  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1);
105  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
106  const ArgType& arg2);
107  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
108  const ArgType& arg2, const ArgType& arg3);
109  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
110  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4);
111  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
112  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4,
113  const ArgType& arg5);
114  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
115  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4,
116  const ArgType& arg5, const ArgType& arg6);
117  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
118  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4,
119  const ArgType& arg5, const ArgType& arg6, const ArgType& arg7);
120  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
121  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4,
122  const ArgType& arg5, const ArgType& arg6, const ArgType& arg7,
123  const ArgType& arg8);
124  Status(TErrorCode::type error, const ArgType& arg0, const ArgType& arg1,
125  const ArgType& arg2, const ArgType& arg3, const ArgType& arg4,
126  const ArgType& arg5, const ArgType& arg6, const ArgType& arg7,
127  const ArgType& arg8, const ArgType& arg9);
128 
131  Status(const ErrorMsg& e);
132 
136  Status(const std::string& error_msg);
137 
139  static Status Expected(const std::string& error_msg);
140 
142  if (msg_ != NULL) delete msg_;
143  }
144 
146  Status& operator=(const Status& status) {
147  delete msg_;
148  if (LIKELY(status.msg_ == NULL)) {
149  msg_ = NULL;
150  } else {
151  msg_ = new ErrorMsg(*status.msg_);
152  }
153  return *this;
154  }
155 
158  Status(const TStatus& status);
159 
162  Status& operator=(const TStatus& status);
163 
166  Status(const apache::hive::service::cli::thrift::TStatus& hs2_status);
167 
170  Status& operator=(const apache::hive::service::cli::thrift::TStatus& hs2_status);
171 
172  bool ok() const { return msg_ == NULL; }
173 
174  bool IsCancelled() const {
175  return msg_ != NULL && msg_->error() == TErrorCode::CANCELLED;
176  }
177 
178  bool IsMemLimitExceeded() const {
179  return msg_ != NULL
180  && msg_->error() == TErrorCode::MEM_LIMIT_EXCEEDED;
181  }
182 
183  bool IsRecoverableError() const {
184  return msg_ != NULL
185  && msg_->error() == TErrorCode::RECOVERABLE_ERROR;
186  }
187 
189  const ErrorMsg& msg() const {
190  DCHECK_NOTNULL(msg_);
191  return *msg_;
192  }
193 
197  void SetErrorMsg(const ErrorMsg& m) {
198  DCHECK_NOTNULL(msg_);
199  delete msg_;
200  msg_ = new ErrorMsg(m);
201  }
202 
204  void AddDetail(const std::string& msg);
205 
209  void MergeStatus(const Status& status);
210 
213  template <typename T> void SetTStatus(T* status_container) const {
214  ToThrift(&status_container->status);
215  status_container->__isset.status = true;
216  }
217 
219  void ToThrift(TStatus* status) const;
220 
224  const std::string GetDetail() const;
225 
226  TErrorCode::type code() const {
227  return msg_ == NULL ? TErrorCode::OK : msg_->error();
228  }
229 
230  private:
231 
234  Status(const std::string& error_msg, bool silent);
235 
239 };
240 
242 #define RETURN_IF_ERROR(stmt) \
243  do { \
244  Status __status__ = (stmt); \
245  if (UNLIKELY(!__status__.ok())) return __status__; \
246  } while (false)
247 
248 #define EXIT_IF_ERROR(stmt) \
249  do { \
250  Status __status__ = (stmt); \
251  if (UNLIKELY(!__status__.ok())) { \
252  EXIT_WITH_ERROR(__status__.GetDetail()); \
253  } \
254  } while (false)
255 
256 #define EXIT_WITH_ERROR(msg) \
257  do { \
258  LOG(ERROR) << msg; \
259  exit(1); \
260  } while (false)
261 
262 }
263 
264 #endif
const std::string GetDetail() const
Definition: status.cc:184
static const Status DEPRECATED_RPC
Definition: status.h:90
strings::internal::SubstituteArg ArgType
Definition: status.h:83
Status(const Status &status)
copy c'tor makes copy of error detail so Status can be returned by value
Definition: status.h:93
void MergeStatus(const Status &status)
Definition: status.cc:172
TErrorCode::type error() const
Definition: error-util.h:105
void AddDetail(const std::string &msg)
Add a detail string. Calling this method is only defined on a non-OK message.
Definition: status.cc:166
Status & operator=(const Status &status)
same as copy c'tor
Definition: status.h:146
ErrorMsg * msg_
Definition: status.h:238
TErrorCode::type code() const
Definition: status.h:226
bool IsCancelled() const
Definition: status.h:174
void ToThrift(TStatus *status) const
Convert into TStatus.
Definition: status.cc:188
void SetTStatus(T *status_container) const
Definition: status.h:213
static const Status CANCELLED
Definition: status.h:88
static Status Expected(const std::string &error_msg)
Create a status instance that represents an expected error and will not be logged.
Definition: status.cc:162
static const Status MEM_LIMIT_EXCEEDED
Definition: status.h:89
void SetErrorMsg(const ErrorMsg &m)
Definition: status.h:197
static const Status OK
Definition: status.h:87
#define LIKELY(expr)
Definition: compiler-util.h:32
const ErrorMsg & msg() const
Returns the error message associated with a non-successful status.
Definition: status.h:189
bool ok() const
Definition: status.h:172
bool IsMemLimitExceeded() const
Definition: status.h:178
bool IsRecoverableError() const
Definition: status.h:183