/Users/deen/code/yugabyte-db/src/yb/util/service_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | | // |
5 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | // Utilities to be used for RPC services. |
20 | | |
21 | | #ifndef YB_UTIL_SERVICE_UTIL_H_ |
22 | | #define YB_UTIL_SERVICE_UTIL_H_ |
23 | | |
24 | | #include "yb/rpc/rpc_context.h" |
25 | | |
26 | | #include "yb/util/status_fwd.h" |
27 | | #include "yb/util/status.h" |
28 | | |
29 | | // Utility macro to setup error response and return if status is not OK. |
30 | 1.75k | #define RPC_STATUS_RETURN_ERROR(s, error, code, context) do { \ |
31 | 1.75k | auto s_tmp = (s); \ |
32 | 1.75k | if (PREDICT_FALSE(!s_tmp.ok())) { \ |
33 | 0 | SetupErrorAndRespond(error, s_tmp, code, &(context)); \ |
34 | 1 | return; \ |
35 | 1 | } \ |
36 | 1.75k | } while (false) |
37 | | |
38 | | // Utility macros to perform the appropriate check. If the check fails, |
39 | | // returns the specified (error) Status, with the given message. |
40 | | #define RPC_CHECK_OP_AND_RETURN_ERROR(var1, op, var2, s, error, code, context) \ |
41 | 2.55k | do { \ |
42 | 948 | auto v1_tmp = (var1); \ |
43 | 2.23k | auto v2_tmp = (var2); \ |
44 | 2.23k | if (PREDICT_FALSE(!((v1_tmp)op(v2_tmp)))) { \ |
45 | 0 | RPC_STATUS_RETURN_ERROR(s, error, code, context); \ |
46 | 0 | } \ |
47 | 2.23k | } while (false) |
48 | | |
49 | | #define RPC_CHECK_AND_RETURN_ERROR(expr, s, error, code, context) \ |
50 | 2.23k | RPC_CHECK_OP_AND_RETURN_ERROR(expr, ==, true, s, error, code, context) |
51 | | #define RPC_CHECK_EQ_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
52 | | RPC_CHECK_OP_AND_RETURN_ERROR(var1, ==, var2, s, error, code, context) |
53 | | #define RPC_CHECK_NE_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
54 | 0 | RPC_CHECK_OP_AND_RETURN_ERROR(var1, !=, var2, s, error, code, context) |
55 | | #define RPC_CHECK_GT_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
56 | | RPC_CHECK_OP_AND_RETURN_ERROR(var1, >, var2, s, error, code, context) |
57 | | #define RPC_CHECK_GE_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
58 | | RPC_CHECK_OP_AND_RETURN_ERROR(var1, >=, var2, s, error, code, context) |
59 | | #define RPC_CHECK_LT_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
60 | | RPC_CHECK_OP_AND_RETURN_ERROR(var1, <, var2, s, error, code, context) |
61 | | #define RPC_CHECK_LE_AND_RETURN_ERROR(var1, var2, s, error, code, context) \ |
62 | | RPC_CHECK_OP_AND_RETURN_ERROR(var1, <=, var2, s, error, code, context) |
63 | | |
64 | | namespace yb { |
65 | | |
66 | | template <class ErrType, typename ErrEnum> |
67 | | void SetupErrorAndRespond(ErrType* error, |
68 | | const Status& s, |
69 | | ErrEnum code, |
70 | 2 | rpc::RpcContext* context) { |
71 | | // Generic "service unavailable" errors will cause the client to retry later. |
72 | 2 | if (code == ErrType::UNKNOWN_ERROR && s.IsServiceUnavailable()) { |
73 | 0 | context->RespondRpcFailure(rpc::ErrorStatusPB::ERROR_SERVER_TOO_BUSY, s); |
74 | 0 | return; |
75 | 0 | } |
76 | | |
77 | 2 | StatusToPB(s, error->mutable_status()); |
78 | 2 | error->set_code(code); |
79 | 2 | context->RespondSuccess(); |
80 | 2 | } _ZN2yb20SetupErrorAndRespondINS_3cdc10CDCErrorPBENS1_15CDCErrorPB_CodeEEEvPT_RKNS_6StatusET0_PNS_3rpc10RpcContextE Line | Count | Source | 70 | 2 | rpc::RpcContext* context) { | 71 | | // Generic "service unavailable" errors will cause the client to retry later. | 72 | 2 | if (code == ErrType::UNKNOWN_ERROR && s.IsServiceUnavailable()) { | 73 | 0 | context->RespondRpcFailure(rpc::ErrorStatusPB::ERROR_SERVER_TOO_BUSY, s); | 74 | 0 | return; | 75 | 0 | } | 76 | | | 77 | 2 | StatusToPB(s, error->mutable_status()); | 78 | 2 | error->set_code(code); | 79 | 2 | context->RespondSuccess(); | 80 | 2 | } |
Unexecuted instantiation: _ZN2yb20SetupErrorAndRespondINS_6master13MasterErrorPBENS1_18MasterErrorPB_CodeEEEvPT_RKNS_6StatusET0_PNS_3rpc10RpcContextE |
81 | | |
82 | | } // namespace yb |
83 | | |
84 | | |
85 | | #endif // YB_UTIL_SERVICE_UTIL_H_ |