1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of TokuDB
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     TokuDB is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     TokuDB is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with TokuDB.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ======= */
23 
24 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
25 
26 #ifndef _HATOKU_DEFINES_H
27 #define _HATOKU_DEFINES_H
28 
29 #define MYSQL_SERVER 1
30 #include <my_global.h>
31 #include "mysql_version.h"
32 #include "sql_table.h"
33 #include "handler.h"
34 #include "table.h"
35 #include "log.h"
36 #include "sql_class.h"
37 #include "sql_show.h"
38 #include "item_cmpfunc.h"
39 //#include <binlog.h>
40 #include "debug_sync.h"
41 
42 #undef PACKAGE
43 #undef VERSION
44 #undef HAVE_DTRACE
45 #undef _DTRACE_VERSION
46 
47 /* We define DTRACE after mysql_priv.h in case it disabled dtrace in the main server */
48 #ifdef HAVE_DTRACE
49 #define _DTRACE_VERSION 1
50 #else
51 #endif
52 
53 #include <mysql/plugin.h>
54 
55 #include <ctype.h>
56 #include <stdint.h>
57 #if !defined(__STDC_FORMAT_MACROS)
58 #define __STDC_FORMAT_MACROS
59 #endif  // !defined(__STDC_FORMAT_MACROS)
60 #include <inttypes.h>
61 #if defined(_WIN32)
62 #include "misc.h"
63 #endif
64 
65 #include <string>
66 #include <unordered_map>
67 
68 #include "db.h"
69 #include "toku_os.h"
70 #include "toku_time.h"
71 #include "partitioned_counter.h"
72 
73 #ifdef USE_PRAGMA_INTERFACE
74 #pragma interface               /* gcc class implementation */
75 #endif
76 
77 // TOKU_INCLUDE_WRITE_FRM_DATA, TOKU_PARTITION_WRITE_FRM_DATA, and
78 // TOKU_INCLUDE_DISCOVER_FRM all work together as two opposing sides
79 // of the same functionality. The 'WRITE' includes functionality to
80 // write a copy of every tables .frm data into the tables status dictionary on
81 // CREATE or ALTER. When WRITE is in, the .frm data is also verified whenever a
82 // table is opened.
83 //
84 // The 'DISCOVER' then implements the MySQL table discovery API which reads
85 // this same data and returns it back to MySQL.
86 // In most cases, they should all be in or out without mixing. There may be
87 // extreme cases though where one side (WRITE) is supported but perhaps
88 // 'DISCOVERY' may not be, thus the need for individual indicators.
89 
90 #if 100000 <= MYSQL_VERSION_ID
91 // mariadb 10.0
92 #define TOKU_USE_DB_TYPE_TOKUDB 1
93 #define TOKU_INCLUDE_ALTER_56 1
94 #define TOKU_INCLUDE_ROW_TYPE_COMPRESSION 0
95 #define TOKU_INCLUDE_XA 1
96 #define TOKU_INCLUDE_WRITE_FRM_DATA 1
97 #define TOKU_PARTITION_WRITE_FRM_DATA 0
98 #define TOKU_INCLUDE_DISCOVER_FRM 1
99 #if defined(MARIADB_BASE_VERSION)
100 #define TOKU_INCLUDE_EXTENDED_KEYS 1
101 #endif
102 #define TOKU_INCLUDE_OPTION_STRUCTS 1
103 #define TOKU_OPTIMIZE_WITH_RECREATE 1
104 #define TOKU_CLUSTERING_IS_COVERING 1
105 
106 #elif 50700 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50799
107 // mysql 5.7 with no patches
108 #if TOKUDB_NOPATCH_CONFIG
109 #define TOKU_USE_DB_TYPE_UNKNOWN 1
110 #define TOKU_INCLUDE_ALTER_56 1
111 #define TOKU_INCLUDE_ROW_TYPE_COMPRESSION 0
112 #define TOKU_INCLUDE_WRITE_FRM_DATA 1
113 #define TOKU_PARTITION_WRITE_FRM_DATA 0
114 #define TOKU_INCLUDE_DISCOVER_FRM 1
115 #define TOKU_INCLUDE_RFR 1
116 #else
117 #error
118 #endif
119 
120 #elif 50613 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50699
121 // mysql 5.6 with no patches
122 #if TOKUDB_NOPATCH_CONFIG
123 #define TOKU_USE_DB_TYPE_UNKNOWN 1
124 #define TOKU_INCLUDE_ALTER_56 1
125 #define TOKU_INCLUDE_ROW_TYPE_COMPRESSION 0
126 #define TOKU_INCLUDE_XA 0
127 #define TOKU_INCLUDE_WRITE_FRM_DATA 1
128 #define TOKU_PARTITION_WRITE_FRM_DATA 0
129 #define TOKU_INCLUDE_DISCOVER_FRM 1
130 #else
131 // mysql 5.6 with tokutek patches
132 #define TOKU_USE_DB_TYPE_TOKUDB 1           // has DB_TYPE_TOKUDB patch
133 #define TOKU_INCLUDE_ALTER_56 1
134 #define TOKU_INCLUDE_ROW_TYPE_COMPRESSION 1 // has tokudb row format compression patch
135 #define TOKU_INCLUDE_XA 1                   // has patch that fixes TC_LOG_MMAP code
136 #define TOKU_INCLUDE_WRITE_FRM_DATA 1
137 #define TOKU_PARTITION_WRITE_FRM_DATA 0
138 #define TOKU_INCLUDE_DISCOVER_FRM 1
139 #define TOKU_INCLUDE_UPSERT 1               // has tokudb upsert patch
140 #if defined(HTON_SUPPORTS_EXTENDED_KEYS)
141 #define TOKU_INCLUDE_EXTENDED_KEYS 1
142 #endif
143 #endif
144 #define TOKU_OPTIMIZE_WITH_RECREATE 1
145 #define TOKU_INCLUDE_RFR 1
146 
147 #elif 50500 <= MYSQL_VERSION_ID && MYSQL_VERSION_ID <= 50599
148 #define TOKU_USE_DB_TYPE_TOKUDB 1
149 #define TOKU_INCLUDE_ALTER_56 0         /* MariaDB 5.5 */
150 #define TOKU_INCLUDE_ALTER_55 0         /* MariaDB 5.5 */
151 #define TOKU_INCLUDE_ROW_TYPE_COMPRESSION 0 /* MariaDB 5.5 */
152 #define TOKU_INCLUDE_XA 1
153 #define TOKU_PARTITION_WRITE_FRM_DATA 0 /* MariaDB 5.5 */
154 #define TOKU_INCLUDE_WRITE_FRM_DATA 0   /* MariaDB 5.5 */
155 #define TOKU_INCLUDE_DISCOVER_FRM 1
156 #define TOKU_INCLUDE_UPSERT 0           /* MariaDB 5.5 */
157 #if defined(MARIADB_BASE_VERSION)
158 #define TOKU_INCLUDE_EXTENDED_KEYS 1
159 #define TOKU_INCLUDE_OPTION_STRUCTS 1
160 #define TOKU_CLUSTERING_IS_COVERING 1
161 #define TOKU_INCLUDE_LOCK_TIMEOUT_QUERY_STRING 1
162 #else
163 #define TOKU_INCLUDE_LOCK_TIMEOUT_QUERY_STRING 1
164 #endif
165 #define TOKU_INCLUDE_HANDLERTON_HANDLE_FATAL_SIGNAL 0
166 
167 #else
168 #error
169 
170 #endif
171 
172 #if defined(TOKU_INCLUDE_DISCOVER_FRM) && TOKU_INCLUDE_DISCOVER_FRM
173 #include "discover.h"
174 #endif  // defined(TOKU_INCLUDE_DISCOVER_FRM) && TOKU_INCLUDE_DISCOVER_FRM
175 
176 
177 #ifdef MARIADB_BASE_VERSION
178 // In MariaDB 5.3, thread progress reporting was introduced.
179 // Only include that functionality if we're using maria 5.3 +
180 #define HA_TOKUDB_HAS_THD_PROGRESS 1
181 
182 // MariaDB supports thdvar memalloc correctly
183 #define TOKU_THDVAR_MEMALLOC_BUG 0
184 #else
185 // MySQL does not support thdvar memalloc correctly
186 // see http://bugs.mysql.com/bug.php?id=71759
187 #define TOKU_THDVAR_MEMALLOC_BUG 1
188 #endif
189 
190 #if !defined(HA_CLUSTERING)
191 #define HA_CLUSTERING 0
192 #endif
193 
194 #if !defined(HA_CLUSTERED_INDEX)
195 #define HA_CLUSTERED_INDEX 0
196 #endif
197 
198 #if !defined(HA_CAN_WRITE_DURING_OPTIMIZE)
199 #define HA_CAN_WRITE_DURING_OPTIMIZE 0
200 #endif
201 
202 #if !defined(HA_ONLINE_ANALYZE)
203 #define HA_ONLINE_ANALYZE 0
204 #endif
205 
206 #if !defined(MY_ATTRIBUTE)
207 #define MY_ATTRIBUTE(A) __attribute__(A)
208 #endif
209 
210 #if !defined(HA_OPTION_CREATE_FROM_ENGINE)
211 #define HA_OPTION_CREATE_FROM_ENGINE 0
212 #endif
213 
214 // In older (< 5.5) versions of MySQL and MariaDB, it is necessary to
215 // use a read/write lock on the key_file array in a table share,
216 // because table locks do not protect the race of some thread closing
217 // a table and another calling ha_tokudb::info()
218 //
219 // In version 5.5 and, a higher layer "metadata lock" was introduced
220 // to synchronize threads that open, close, call info(), etc on tables.
221 // In these versions, we don't need the key_file lock
222 #if MYSQL_VERSION_ID < 50500
223 #define HA_TOKUDB_NEEDS_KEY_FILE_LOCK
224 #endif
225 
226 //
227 // returns maximum length of dictionary name, such as key-NAME
228 // NAME_CHAR_LEN is max length of the key name, and have upper bound of 10 for key-
229 //
230 #define MAX_DICT_NAME_LEN NAME_CHAR_LEN + 10
231 
232 // QQQ how to tune these?
233 #define HA_TOKUDB_RANGE_COUNT   100
234 /* extra rows for estimate_rows_upper_bound() */
235 #define HA_TOKUDB_EXTRA_ROWS    100
236 
237 /* Bits for share->status */
238 #define STATUS_PRIMARY_KEY_INIT 0x1
239 
240 #if defined(TOKUDB_VERSION_MAJOR) && defined(TOKUDB_VERSION_MINOR)
241 #define TOKUDB_PLUGIN_VERSION ((TOKUDB_VERSION_MAJOR << 8) + TOKUDB_VERSION_MINOR)
242 #else
243 #define TOKUDB_PLUGIN_VERSION 0
244 #endif
245 
246 // Branch prediction macros.
247 // If supported by the compiler, will hint in instruction caching for likely
248 // branching. Should only be used where there is a very good idea of the correct
249 // branch heuristics as determined by profiling. Mostly copied from InnoDB.
250 // Use:
251 //   "if (TOKUDB_LIKELY(x))" where the chances of "x" evaluating true are higher
252 //   "if (TOKUDB_UNLIKELY(x))" where the chances of "x" evaluating false are higher
253 #if defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
254 
255 // Tell the compiler that 'expr' probably evaluates to 'constant'.
256 #define TOKUDB_EXPECT(expr,constant) __builtin_expect(expr, constant)
257 
258 #else
259 
260 #error "No TokuDB branch prediction operations in use!"
261 #define TOKUDB_EXPECT(expr,constant) (expr)
262 
263 #endif // defined(__GNUC__) && (__GNUC__ > 2) && ! defined(__INTEL_COMPILER)
264 
265 // Tell the compiler that cond is likely to hold
266 #define TOKUDB_LIKELY(cond) TOKUDB_EXPECT(cond, 1)
267 
268 // Tell the compiler that cond is unlikely to hold
269 #define TOKUDB_UNLIKELY(cond) TOKUDB_EXPECT(cond, 0)
270 
271 // Tell the compiler that the function/argument is unused
272 #define TOKUDB_UNUSED(_uu) _uu __attribute__((unused))
273 // mysql 5.6.15 removed the test macro, so we define our own
274 #define tokudb_test(e) ((e) ? 1 : 0)
275 
tokudb_thd_get_proc_info(const THD * thd)276 inline const char* tokudb_thd_get_proc_info(const THD* thd) {
277     return thd->proc_info;
278 }
tokudb_thd_set_proc_info(THD * thd,const char * proc_info)279 inline void tokudb_thd_set_proc_info(THD* thd, const char* proc_info) {
280     thd_proc_info(thd, proc_info);
281 }
282 
283 // uint3korr reads 4 bytes and valgrind reports an error, so we use this function instead
tokudb_uint3korr(const uchar * a)284 inline uint tokudb_uint3korr(const uchar *a) {
285     uchar b[4] = {};
286     memcpy(b, a, 3);
287     return uint3korr(b);
288 }
289 
290 typedef unsigned int pfs_key_t;
291 
292 #if defined(SAFE_MUTEX) || defined(HAVE_PSI_MUTEX_INTERFACE)
293 #define mutex_t_lock(M) (M).lock(__FILE__, __LINE__)
294 #else  // SAFE_MUTEX || HAVE_PSI_MUTEX_INTERFACE
295 #define mutex_t_lock(M) (M).lock()
296 #endif  // SAFE_MUTEX || HAVE_PSI_MUTEX_INTERFACE
297 
298 #if defined(SAFE_MUTEX)
299 #define mutex_t_unlock(M) (M).unlock(__FILE__, __LINE__)
300 #else  // SAFE_MUTEX
301 #define mutex_t_unlock(M) (M).unlock()
302 #endif  // SAFE_MUTEX
303 
304 #if defined(HAVE_PSI_RWLOCK_INTERFACE)
305 #define rwlock_t_lock_read(M) (M).lock_read(__FILE__, __LINE__)
306 #define rwlock_t_lock_write(M) (M).lock_write(__FILE__, __LINE__)
307 #else  // HAVE_PSI_RWLOCK_INTERFACE
308 #define rwlock_t_lock_read(M) (M).lock_read()
309 #define rwlock_t_lock_write(M) (M).lock_write()
310 #endif  // HAVE_PSI_RWLOCK_INTERFACE
311 
312 #endif  // _HATOKU_DEFINES_H
313