/Users/deen/code/yugabyte-db/src/yb/common/ybc_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | |
13 | | // C wrappers around some YB utilities. Suitable for inclusion into C codebases such as our modified |
14 | | // version of PostgreSQL. |
15 | | |
16 | | #ifndef YB_COMMON_YBC_UTIL_H |
17 | | #define YB_COMMON_YBC_UTIL_H |
18 | | |
19 | | #include <stddef.h> |
20 | | #include <stdint.h> |
21 | | |
22 | | #ifdef __cplusplus |
23 | | extern "C" { |
24 | | |
25 | | struct varlena; |
26 | | |
27 | | #endif |
28 | | |
29 | | /* |
30 | | * Guc variable to log the protobuf string for every outgoing (DocDB) read/write request. |
31 | | * See the "YB Debug utils" section in pg_yb_utils.h (as well as guc.c) for more information. |
32 | | */ |
33 | | extern bool yb_debug_log_docdb_requests; |
34 | | |
35 | | /* |
36 | | * Toggles whether formatting functions exporting system catalog information |
37 | | * include DocDB metadata (such as tablet split information). |
38 | | */ |
39 | | extern bool yb_format_funcs_include_yb_metadata; |
40 | | |
41 | | /* |
42 | | * Guc variable to enable the use of regular transactions for operating on system catalog tables |
43 | | * in case a DDL transaction has not been started. |
44 | | */ |
45 | | extern bool yb_non_ddl_txn_for_sys_tables_allowed; |
46 | | |
47 | | /* |
48 | | * Toggles whether to force use of global transaction status table. |
49 | | */ |
50 | | extern bool yb_force_global_transaction; |
51 | | |
52 | | /* |
53 | | * Guc variable to suppress non-Postgres logs from appearing in Postgres log file. |
54 | | */ |
55 | | extern bool suppress_nonpg_logs; |
56 | | |
57 | | /* |
58 | | * Guc variable to enable binary restore from a binary backup of YSQL tables. When doing binary |
59 | | * restore, we copy the docdb SST files of those tables from the source database and reuse them |
60 | | * for a newly created target database to restore those tables. |
61 | | */ |
62 | | extern bool yb_binary_restore; |
63 | | |
64 | | typedef struct YBCStatusStruct* YBCStatus; |
65 | | |
66 | | extern YBCStatus YBCStatusOK; |
67 | | bool YBCStatusIsOK(YBCStatus s); |
68 | | bool YBCStatusIsNotFound(YBCStatus s); |
69 | | bool YBCStatusIsDuplicateKey(YBCStatus s); |
70 | | uint32_t YBCStatusPgsqlError(YBCStatus s); |
71 | | uint16_t YBCStatusTransactionError(YBCStatus s); |
72 | | void YBCFreeStatus(YBCStatus s); |
73 | | |
74 | | size_t YBCStatusMessageLen(YBCStatus s); |
75 | | const char* YBCStatusMessageBegin(YBCStatus s); |
76 | | const char* YBCStatusCodeAsCString(YBCStatus s); |
77 | | char* DupYBStatusMessage(YBCStatus status, bool message_only); |
78 | | |
79 | | bool YBCIsRestartReadError(uint16_t txn_errcode); |
80 | | |
81 | | bool YBCIsTxnConflictError(uint16_t txn_errcode); |
82 | | bool YBCIsTxnSkipLockingError(uint16_t txn_errcode); |
83 | | uint16_t YBCGetTxnConflictErrorCode(); |
84 | | |
85 | | void YBCResolveHostname(); |
86 | | |
87 | | #define CHECKED_YBCSTATUS __attribute__ ((warn_unused_result)) YBCStatus |
88 | | |
89 | | typedef void* (*YBCPAllocFn)(size_t size); |
90 | | |
91 | | typedef struct varlena* (*YBCCStringToTextWithLenFn)(const char* c, int size); |
92 | | |
93 | | // Global initialization of the YugaByte subsystem. |
94 | | CHECKED_YBCSTATUS YBCInit( |
95 | | const char* argv0, |
96 | | YBCPAllocFn palloc_fn, |
97 | | YBCCStringToTextWithLenFn cstring_to_text_with_len_fn); |
98 | | |
99 | | CHECKED_YBCSTATUS YBCInitGFlags(const char* argv0); |
100 | | |
101 | | // From glog's log_severity.h: |
102 | | // const int GLOG_INFO = 0, GLOG_WARNING = 1, GLOG_ERROR = 2, GLOG_FATAL = 3; |
103 | | |
104 | | // Logging macros with printf-like formatting capabilities. |
105 | | #define YBC_LOG_INFO(...) \ |
106 | 4.90k | YBCLogImpl(/* severity */ 0, __FILE__, __LINE__, /* stack_trace */ false, __VA_ARGS__) |
107 | | #define YBC_LOG_WARNING(...) \ |
108 | 177 | YBCLogImpl(/* severity */ 1, __FILE__, __LINE__, /* stack_trace */ false, __VA_ARGS__) |
109 | | #define YBC_LOG_ERROR(...) \ |
110 | 0 | YBCLogImpl(/* severity */ 2, __FILE__, __LINE__, /* stack_trace */ false, __VA_ARGS__) |
111 | | #define YBC_LOG_FATAL(...) \ |
112 | | YBCLogImpl(/* severity */ 3, __FILE__, __LINE__, /* stack_trace */ false, __VA_ARGS__) |
113 | | |
114 | | // Versions of these warnings that do nothing in debug mode. The fatal version logs a warning |
115 | | // in release mode but does not crash. |
116 | | #ifndef NDEBUG |
117 | | // Logging macros with printf-like formatting capabilities. |
118 | | #define YBC_DEBUG_LOG_INFO(...) YBC_LOG_INFO(__VA_ARGS__) |
119 | | #define YBC_DEBUG_LOG_WARNING(...) YBC_LOG_WARNING(__VA_ARGS__) |
120 | | #define YBC_DEBUG_LOG_ERROR(...) YBC_LOG_ERROR(__VA_ARGS__) |
121 | | #define YBC_DEBUG_LOG_FATAL(...) YBC_LOG_FATAL(__VA_ARGS__) |
122 | | #else |
123 | | #define YBC_DEBUG_LOG_INFO(...) |
124 | | #define YBC_DEBUG_LOG_WARNING(...) |
125 | | #define YBC_DEBUG_LOG_ERROR(...) |
126 | | #define YBC_DEBUG_LOG_FATAL(...) YBC_LOG_ERROR(__VA_ARGS__) |
127 | | #endif |
128 | | |
129 | | // The following functions log the given message formatted similarly to printf followed by a stack |
130 | | // trace. |
131 | | |
132 | | #define YBC_LOG_INFO_STACK_TRACE(...) \ |
133 | | YBCLogImpl(/* severity */ 0, __FILE__, __LINE__, /* stack_trace */ true, __VA_ARGS__) |
134 | | #define YBC_LOG_WARNING_STACK_TRACE(...) \ |
135 | | YBCLogImpl(/* severity */ 1, __FILE__, __LINE__, /* stack_trace */ true, __VA_ARGS__) |
136 | | #define YBC_LOG_ERROR_STACK_TRACE(...) \ |
137 | 0 | YBCLogImpl(/* severity */ 2, __FILE__, __LINE__, /* stack_trace */ true, __VA_ARGS__) |
138 | | |
139 | | // 5 is the index of the format string, 6 is the index of the first printf argument to check. |
140 | | void YBCLogImpl(int severity, |
141 | | const char* file_name, |
142 | | int line_number, |
143 | | bool stack_trace, |
144 | | const char* format, |
145 | | ...) __attribute__((format(printf, 5, 6))); |
146 | | |
147 | | // Returns a string representation of the given block of binary data. The memory for the resulting |
148 | | // string is allocated using palloc. |
149 | | const char* YBCFormatBytesAsStr(const char* data, size_t size); |
150 | | |
151 | | const char* YBCGetStackTrace(); |
152 | | |
153 | | // Initializes global state needed for thread management, including CDS library initialization. |
154 | | void YBCInitThreading(); |
155 | | |
156 | | double YBCEvalHashValueSelectivity(int32_t hash_low, int32_t hash_high); |
157 | | |
158 | | #ifdef __cplusplus |
159 | | } // extern "C" |
160 | | #endif |
161 | | |
162 | | #endif // YB_COMMON_YBC_UTIL_H |