1 /* Copyright 2008-2015 Codership Oy <http://www.codership.com>
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15 
16 #ifndef WSREP_MYSQLD_H
17 #define WSREP_MYSQLD_H
18 
19 #include "mysqld.h"
20 #include "wsrep_api.h"
21 typedef struct st_mysql_show_var SHOW_VAR;
22 #include "rpl_gtid.h"
23 
24 #define WSREP_UNDEFINED_TRX_ID ULLONG_MAX
25 
26 class set_var;
27 class THD;
28 
29 enum wsrep_exec_mode {
30     LOCAL_STATE,
31     REPL_RECV,
32     TOTAL_ORDER,
33     LOCAL_COMMIT
34 };
35 
36 enum wsrep_query_state {
37     QUERY_IDLE,
38     QUERY_EXEC,
39     QUERY_COMMITTING,
40     QUERY_EXITING,
41     QUERY_ROLLINGBACK,
42 };
43 
44 enum wsrep_conflict_state {
45     NO_CONFLICT,
46     MUST_ABORT,
47     ABORTING,
48     ABORTED,
49     MUST_REPLAY,
50     REPLAYING,
51     REPLAYED,
52     RETRY_AUTOCOMMIT,
53     CERT_FAILURE,
54 };
55 
56 enum wsrep_consistency_check_mode {
57     NO_CONSISTENCY_CHECK,
58     CONSISTENCY_CHECK_DECLARED,
59     CONSISTENCY_CHECK_RUNNING,
60 };
61 
62 enum enum_wsrep_certification_rules {
63     WSREP_CERTIFICATION_RULES_STRICT,
64     WSREP_CERTIFICATION_RULES_OPTIMIZED
65 };
66 
67 // Global wsrep parameters
68 extern wsrep_t*    wsrep;
69 
70 // MySQL wsrep options
71 extern const char* wsrep_provider;
72 extern const char* wsrep_provider_options;
73 extern const char* wsrep_cluster_name;
74 extern const char* wsrep_cluster_address;
75 extern const char* wsrep_node_name;
76 extern const char* wsrep_node_address;
77 extern const char* wsrep_node_incoming_address;
78 extern const char* wsrep_data_home_dir;
79 extern const char* wsrep_dbug_option;
80 extern long        wsrep_slave_threads;
81 extern int         wsrep_slave_count_change;
82 extern MYSQL_PLUGIN_IMPORT my_bool wsrep_debug;
83 extern my_bool     wsrep_convert_LOCK_to_trx;
84 extern ulong       wsrep_retry_autocommit;
85 extern my_bool     wsrep_auto_increment_control;
86 extern my_bool     wsrep_drupal_282555_workaround;
87 extern my_bool     wsrep_incremental_data_collection;
88 extern const char* wsrep_start_position;
89 extern ulong       wsrep_max_ws_size;
90 extern ulong       wsrep_max_ws_rows;
91 extern const char* wsrep_notify_cmd;
92 extern my_bool     wsrep_certify_nonPK;
93 extern ulong       wsrep_certification_rules;
94 extern long        wsrep_max_protocol_version;
95 extern long        wsrep_protocol_version;
96 extern ulong       wsrep_forced_binlog_format;
97 extern my_bool     wsrep_desync;
98 extern ulong       wsrep_reject_queries;
99 extern my_bool     wsrep_recovery;
100 extern my_bool     wsrep_replicate_myisam;
101 extern my_bool     wsrep_log_conflicts;
102 extern ulong       wsrep_mysql_replication_bundle;
103 extern my_bool     wsrep_load_data_splitting;
104 extern my_bool     wsrep_restart_slave;
105 extern my_bool     wsrep_restart_slave_activated;
106 extern my_bool     wsrep_slave_FK_checks;
107 extern my_bool     wsrep_slave_UK_checks;
108 extern ulonglong   wsrep_mode;
109 extern ulong       wsrep_running_threads;
110 
111 enum enum_wsrep_reject_types {
112   WSREP_REJECT_NONE,    /* nothing rejected */
113   WSREP_REJECT_ALL,     /* reject all queries, with UNKNOWN_COMMAND error */
114   WSREP_REJECT_ALL_KILL /* kill existing connections and reject all queries*/
115 };
116 
117 enum enum_wsrep_OSU_method {
118     WSREP_OSU_TOI,
119     WSREP_OSU_RSU,
120     WSREP_OSU_NONE,
121 };
122 
123 enum enum_wsrep_sync_wait {
124     WSREP_SYNC_WAIT_NONE = 0x0,
125     // select, begin
126     WSREP_SYNC_WAIT_BEFORE_READ = 0x1,
127     WSREP_SYNC_WAIT_BEFORE_UPDATE_DELETE = 0x2,
128     WSREP_SYNC_WAIT_BEFORE_INSERT_REPLACE = 0x4,
129     WSREP_SYNC_WAIT_BEFORE_SHOW = 0x8,
130     WSREP_SYNC_WAIT_MAX = 0xF
131 };
132 
133 enum enum_wsrep_mode {
134     WSREP_MODE_IGNORE_NATIVE_REPLICATION_FILTER_RULES = 0,
135     WSREP_MODE_IGNORE_CASCADING_FK_DELETE_MISSING_ROW_ERROR = 1,
136 };
137 
138 // MySQL status variables
139 extern my_bool     wsrep_connected;
140 extern const char* wsrep_cluster_state_uuid;
141 extern long long   wsrep_cluster_conf_id;
142 extern const char* wsrep_cluster_status;
143 extern long        wsrep_cluster_size;
144 extern long        wsrep_local_index;
145 extern long long   wsrep_local_bf_aborts;
146 extern const char* wsrep_provider_name;
147 extern const char* wsrep_provider_version;
148 extern const char* wsrep_provider_vendor;
149 
150 int  wsrep_show_status(THD *thd, SHOW_VAR *var, char *buff);
151 int  wsrep_show_ready(THD *thd, SHOW_VAR *var, char *buff);
152 void wsrep_free_status(THD *thd);
153 
154 /* Filters out --wsrep-new-cluster oprtion from argv[]
155  * should be called in the very beginning of main() */
156 void wsrep_filter_new_cluster (int* argc, char* argv[]);
157 
158 int  wsrep_init();
159 void wsrep_deinit();
160 void wsrep_recover();
161 bool wsrep_before_SE(); // initialize wsrep before storage
162                         // engines (true) or after (false)
163 /* wsrep initialization sequence at startup
164  * @param before wsrep_before_SE() value */
165 void wsrep_init_startup(bool before);
166 
167 
168 extern "C" enum wsrep_exec_mode wsrep_thd_exec_mode(THD *thd);
169 extern "C" enum wsrep_conflict_state wsrep_thd_conflict_state(THD *thd);
170 extern "C" enum wsrep_query_state wsrep_thd_query_state(THD *thd);
171 extern "C" const char * wsrep_thd_exec_mode_str(THD *thd);
172 extern "C" const char * wsrep_thd_conflict_state_str(THD *thd);
173 extern "C" const char * wsrep_thd_query_state_str(THD *thd);
174 extern "C" wsrep_ws_handle_t* wsrep_thd_ws_handle(THD *thd);
175 
176 extern "C" void wsrep_thd_set_exec_mode(THD *thd, enum wsrep_exec_mode mode);
177 extern "C" void wsrep_thd_set_query_state(
178         THD *thd, enum wsrep_query_state state);
179 extern "C" void wsrep_thd_set_conflict_state(
180         THD *thd, bool lock, enum wsrep_conflict_state state);
181 
182 extern "C" void wsrep_thd_set_trx_to_replay(THD *thd, uint64 trx_id);
183 
184 extern "C" void wsrep_thd_LOCK(THD *thd);
185 extern "C" void wsrep_thd_UNLOCK(THD *thd);
186 extern "C" uint32 wsrep_thd_wsrep_rand(THD *thd);
187 extern "C" time_t wsrep_thd_query_start(THD *thd);
188 extern "C" my_thread_id wsrep_thd_thread_id(THD *thd);
189 extern "C" int64_t wsrep_thd_trx_seqno(THD *thd);
190 extern "C" query_id_t wsrep_thd_query_id(THD *thd);
191 extern "C" wsrep_trx_id_t wsrep_thd_next_trx_id(THD *thd);
192 extern "C" wsrep_trx_id_t wsrep_thd_trx_id(THD *thd);
193 extern "C" const char * wsrep_thd_query(THD *thd);
194 extern "C" query_id_t wsrep_thd_wsrep_last_query_id(THD *thd);
195 extern "C" void wsrep_thd_set_wsrep_last_query_id(THD *thd, query_id_t id);
196 extern "C" void wsrep_thd_awake(THD *thd, my_bool signal);
197 extern "C" int wsrep_thd_retry_counter(THD *thd);
198 extern "C" void wsrep_thd_set_next_trx_id(THD *thd);
199 
200 extern "C" void wsrep_thd_auto_increment_variables(THD*,
201                                                    unsigned long long *offset,
202                                                    unsigned long long *increment);
203 
204 extern void wsrep_close_client_connections(my_bool wait_to_end);
205 extern int  wsrep_wait_committing_connections_close(int wait_time);
206 extern void wsrep_close_applier(THD *thd);
207 extern void wsrep_wait_appliers_close(THD *thd);
208 extern void wsrep_close_applier_threads();
209 extern void wsrep_kill_mysql(THD *thd);
210 
211 /* new defines */
212 extern void wsrep_stop_replication(THD *thd);
213 extern bool wsrep_start_replication();
214 extern bool wsrep_must_sync_wait (THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ);
215 extern bool wsrep_sync_wait (THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ);
216 extern bool wsrep_check_mode(uint mask);
217 extern int  wsrep_check_opts (int argc, char* const* argv);
218 extern void wsrep_prepend_PATH (const char* path);
219 /* some inline functions are defined in wsrep_mysqld_inl.h */
220 
221 /* Other global variables */
222 extern wsrep_seqno_t wsrep_locked_seqno;
223 
224 #define WSREP_ON                         \
225   ((global_system_variables.wsrep_on) && \
226    wsrep_provider                     && \
227    strcmp(wsrep_provider, WSREP_NONE))
228 
229 /* use xxxxxx_NNULL macros when thd pointer is guaranteed to be non-null to
230  * avoid compiler warnings (GCC 6 and later) */
231 #define WSREP_NNULL(thd) \
232   (WSREP_ON && wsrep && thd->variables.wsrep_on)
233 
234 #define WSREP(thd) \
235   (thd && WSREP_NNULL(thd))
236 
237 #define WSREP_CLIENT(thd) \
238   (WSREP(thd) && thd->wsrep_client_thread)
239 
240 #define WSREP_EMULATE_BINLOG_NNULL(thd) \
241   (WSREP_NNULL(thd) && wsrep_emulate_bin_log)
242 
243 #define WSREP_EMULATE_BINLOG(thd) \
244   (WSREP(thd) && wsrep_emulate_bin_log)
245 
246 
247 /* A wrapper function for MySQL log functions. The call will prefix
248    the log message with WSREP and forwards the result buffer to
249    MySQL log function specified in fun. */
250 void WSREP_LOG(void (*fun)(const char* fmt, ...), const char* fmt, ...);
251 
252 #define WSREP_DEBUG(...)                                                \
253     if (wsrep_debug)     WSREP_LOG(sql_print_information, ##__VA_ARGS__)
254 #define WSREP_INFO(...)  WSREP_LOG(sql_print_information, ##__VA_ARGS__)
255 #define WSREP_WARN(...)  WSREP_LOG(sql_print_warning,     ##__VA_ARGS__)
256 #define WSREP_ERROR(...) WSREP_LOG(sql_print_error,       ##__VA_ARGS__)
257 
258 #define WSREP_LOG_CONFLICT_THD(thd, role)                                      \
259     WSREP_LOG(sql_print_information, 	                                       \
260       "%s: \n "       	                                                       \
261       "  THD: %u, mode: %s, state: %s, conflict: %s, seqno: %lld\n "           \
262       "  SQL: %s",							       \
263       role, wsrep_thd_thread_id(thd), wsrep_thd_exec_mode_str(thd),            \
264       wsrep_thd_query_state_str(thd),                                          \
265       wsrep_thd_conflict_state_str(thd), (long long)wsrep_thd_trx_seqno(thd),  \
266       wsrep_thd_query(thd)                                                     \
267     );
268 
269 #define WSREP_LOG_CONFLICT(bf_thd, victim_thd, bf_abort)		       \
270   if (wsrep_debug || wsrep_log_conflicts)				       \
271   {                                                                            \
272     WSREP_LOG(sql_print_information, "cluster conflict due to %s for threads:",\
273       (bf_abort) ? "high priority abort" : "certification failure"             \
274     );                                                                         \
275     if (bf_thd)     WSREP_LOG_CONFLICT_THD(bf_thd, "Winning thread");          \
276     if (victim_thd) WSREP_LOG_CONFLICT_THD(victim_thd, "Victim thread");       \
277   }
278 
279 #define WSREP_QUERY(thd)                                        \
280   ((!opt_general_log_raw) && thd->rewritten_query().length()    \
281    ? thd->rewritten_query().ptr() : thd->query().str)
282 
283 extern my_bool wsrep_ready_get();
284 extern void wsrep_ready_wait();
285 
286 enum wsrep_trx_status {
287     WSREP_TRX_OK,
288     WSREP_TRX_CERT_FAIL,      /* certification failure, must abort */
289     WSREP_TRX_SIZE_EXCEEDED,  /* trx size exceeded */
290     WSREP_TRX_ERROR,          /* native mysql error */
291 };
292 
293 extern enum wsrep_trx_status
294 wsrep_run_wsrep_commit(THD *thd, handlerton *hton, bool all);
295 class Ha_trx_info;
296 struct THD_TRANS;
297 void wsrep_register_hton(THD* thd, bool all);
298 void wsrep_post_commit(THD* thd, bool all);
299 void wsrep_brute_force_killer(THD *thd);
300 int  wsrep_hire_brute_force_killer(THD *thd, uint64_t trx_id);
301 
302 extern "C" bool wsrep_consistency_check(void *thd_ptr);
303 
304 /* this is visible for client build so that innodb plugin gets this */
305 typedef struct wsrep_aborting_thd {
306   struct wsrep_aborting_thd *next;
307   THD *aborting_thd;
308 } *wsrep_aborting_thd_t;
309 
310 extern mysql_mutex_t LOCK_wsrep_ready;
311 extern mysql_cond_t  COND_wsrep_ready;
312 extern mysql_mutex_t LOCK_wsrep_sst;
313 extern mysql_cond_t  COND_wsrep_sst;
314 extern mysql_mutex_t LOCK_wsrep_sst_init;
315 extern mysql_cond_t  COND_wsrep_sst_init;
316 extern mysql_mutex_t LOCK_wsrep_rollback;
317 extern mysql_cond_t  COND_wsrep_rollback;
318 extern int wsrep_replaying;
319 extern mysql_mutex_t LOCK_wsrep_replaying;
320 extern mysql_cond_t  COND_wsrep_replaying;
321 extern mysql_mutex_t LOCK_wsrep_slave_threads;
322 extern mysql_mutex_t LOCK_wsrep_desync;
323 extern wsrep_aborting_thd_t wsrep_aborting_thd;
324 extern my_bool       wsrep_emulate_bin_log;
325 extern int           wsrep_to_isolation;
326 extern rpl_sidno     wsrep_sidno;
327 extern my_bool       wsrep_preordered_opt;
328 #ifdef HAVE_PSI_INTERFACE
329 extern PSI_mutex_key key_LOCK_wsrep_ready;
330 extern PSI_mutex_key key_COND_wsrep_ready;
331 extern PSI_mutex_key key_LOCK_wsrep_sst;
332 extern PSI_cond_key  key_COND_wsrep_sst;
333 extern PSI_mutex_key key_LOCK_wsrep_sst_init;
334 extern PSI_cond_key  key_COND_wsrep_sst_init;
335 extern PSI_mutex_key key_LOCK_wsrep_sst_thread;
336 extern PSI_cond_key  key_COND_wsrep_sst_thread;
337 extern PSI_mutex_key key_LOCK_wsrep_rollback;
338 extern PSI_cond_key  key_COND_wsrep_rollback;
339 extern PSI_mutex_key key_LOCK_wsrep_replaying;
340 extern PSI_cond_key  key_COND_wsrep_replaying;
341 extern PSI_mutex_key key_LOCK_wsrep_slave_threads;
342 extern PSI_mutex_key key_LOCK_wsrep_desync;
343 #endif /* HAVE_PSI_INTERFACE */
344 struct TABLE_LIST;
345 class Alter_info;
346 int wsrep_to_isolation_begin(THD *thd, const char *db_, const char *table_,
347                              const TABLE_LIST* table_list,
348                              Alter_info* alter_info = NULL);
349 void wsrep_to_isolation_end(THD *thd);
350 void wsrep_cleanup_transaction(THD *thd);
351 int wsrep_to_buf_helper(
352   THD* thd, const char *query, uint query_len, uchar** buf, size_t* buf_len);
353 int wsrep_create_sp(THD *thd, uchar** buf, size_t* buf_len);
354 int wsrep_create_trigger_query(THD *thd, uchar** buf, size_t* buf_len);
355 int wsrep_create_event_query(THD *thd, uchar** buf, size_t* buf_len);
356 int wsrep_alter_event_query(THD *thd, uchar** buf, size_t* buf_len);
357 
358 bool wsrep_stmt_rollback_is_safe(THD* thd);
359 
360 void wsrep_init_sidno(const wsrep_uuid_t&);
361 bool wsrep_node_is_donor();
362 bool wsrep_node_is_synced();
363 bool wsrep_replicate_GTID(THD* thd);
364 
365 typedef struct wsrep_key_arr
366 {
367     wsrep_key_t* keys;
368     size_t       keys_len;
369 } wsrep_key_arr_t;
370 bool wsrep_prepare_keys_for_isolation(THD*              thd,
371                                       const char*       db,
372                                       const char*       table,
373                                       const TABLE_LIST* table_list,
374                                       wsrep_key_arr_t*  ka);
375 void wsrep_keys_free(wsrep_key_arr_t* key_arr);
376 #endif /* WSREP_MYSQLD_H */
377