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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include <wsrep.h>
17
18#ifndef WSREP_MYSQLD_H
19#define WSREP_MYSQLD_H
20
21#include <mysql/plugin.h>
22#include <mysql/service_wsrep.h>
23
24#ifdef WITH_WSREP
25
26typedef struct st_mysql_show_var SHOW_VAR;
27#include <sql_priv.h>
28//#include "rpl_gtid.h"
29#include "../wsrep/wsrep_api.h"
30#include "mdl.h"
31#include "mysqld.h"
32#include "sql_table.h"
33
34#define WSREP_UNDEFINED_TRX_ID ULONGLONG_MAX
35
36class set_var;
37class THD;
38
39enum wsrep_consistency_check_mode {
40 NO_CONSISTENCY_CHECK,
41 CONSISTENCY_CHECK_DECLARED,
42 CONSISTENCY_CHECK_RUNNING,
43};
44
45struct wsrep_thd_shadow {
46 ulonglong options;
47 uint server_status;
48 enum wsrep_exec_mode wsrep_exec_mode;
49 Vio *vio;
50 ulong tx_isolation;
51 const char *db;
52 size_t db_length;
53 my_hrtime_t user_time;
54 longlong row_count_func;
55};
56
57// Global wsrep parameters
58extern wsrep_t* wsrep;
59
60// MySQL wsrep options
61extern const char* wsrep_provider;
62extern const char* wsrep_provider_options;
63extern const char* wsrep_cluster_name;
64extern const char* wsrep_cluster_address;
65extern const char* wsrep_node_name;
66extern const char* wsrep_node_address;
67extern const char* wsrep_node_incoming_address;
68extern const char* wsrep_data_home_dir;
69extern const char* wsrep_dbug_option;
70extern long wsrep_slave_threads;
71extern int wsrep_slave_count_change;
72extern my_bool wsrep_convert_LOCK_to_trx;
73extern ulong wsrep_retry_autocommit;
74extern my_bool wsrep_auto_increment_control;
75extern my_bool wsrep_incremental_data_collection;
76extern const char* wsrep_start_position;
77extern ulong wsrep_max_ws_size;
78extern ulong wsrep_max_ws_rows;
79extern const char* wsrep_notify_cmd;
80extern long wsrep_max_protocol_version;
81extern ulong wsrep_forced_binlog_format;
82extern my_bool wsrep_desync;
83extern ulong wsrep_reject_queries;
84extern my_bool wsrep_replicate_myisam;
85extern ulong wsrep_mysql_replication_bundle;
86extern my_bool wsrep_restart_slave;
87extern my_bool wsrep_restart_slave_activated;
88extern my_bool wsrep_slave_FK_checks;
89extern my_bool wsrep_slave_UK_checks;
90extern ulong wsrep_running_threads;
91extern bool wsrep_new_cluster;
92extern bool wsrep_gtid_mode;
93extern uint32 wsrep_gtid_domain_id;
94
95enum enum_wsrep_reject_types {
96 WSREP_REJECT_NONE, /* nothing rejected */
97 WSREP_REJECT_ALL, /* reject all queries, with UNKNOWN_COMMAND error */
98 WSREP_REJECT_ALL_KILL /* kill existing connections and reject all queries*/
99};
100
101enum enum_wsrep_OSU_method {
102 WSREP_OSU_TOI,
103 WSREP_OSU_RSU,
104 WSREP_OSU_NONE,
105};
106
107enum enum_wsrep_sync_wait {
108 WSREP_SYNC_WAIT_NONE = 0x0,
109 // select, begin
110 WSREP_SYNC_WAIT_BEFORE_READ = 0x1,
111 WSREP_SYNC_WAIT_BEFORE_UPDATE_DELETE = 0x2,
112 WSREP_SYNC_WAIT_BEFORE_INSERT_REPLACE = 0x4,
113 WSREP_SYNC_WAIT_BEFORE_SHOW = 0x8,
114 WSREP_SYNC_WAIT_MAX = 0xF
115};
116
117// MySQL status variables
118extern my_bool wsrep_connected;
119extern my_bool wsrep_ready;
120extern const char* wsrep_cluster_state_uuid;
121extern long long wsrep_cluster_conf_id;
122extern const char* wsrep_cluster_status;
123extern long wsrep_cluster_size;
124extern long wsrep_local_index;
125extern long long wsrep_local_bf_aborts;
126extern const char* wsrep_provider_name;
127extern const char* wsrep_provider_version;
128extern const char* wsrep_provider_vendor;
129
130int wsrep_show_status(THD *thd, SHOW_VAR *var, char *buff,
131 enum enum_var_type scope);
132int wsrep_init();
133void wsrep_deinit(bool free_options);
134
135/* Initialize wsrep thread LOCKs and CONDs */
136void wsrep_thr_init();
137/* Destroy wsrep thread LOCKs and CONDs */
138void wsrep_thr_deinit();
139
140void wsrep_recover();
141bool wsrep_before_SE(); // initialize wsrep before storage
142 // engines (true) or after (false)
143/* wsrep initialization sequence at startup
144 * @param before wsrep_before_SE() value */
145void wsrep_init_startup(bool before);
146
147// Other wsrep global variables
148extern my_bool wsrep_inited; // whether wsrep is initialized ?
149
150
151extern "C" void wsrep_thd_set_exec_mode(THD *thd, enum wsrep_exec_mode mode);
152extern "C" void wsrep_thd_set_query_state(
153 THD *thd, enum wsrep_query_state state);
154
155extern "C" void wsrep_thd_set_trx_to_replay(THD *thd, uint64 trx_id);
156
157extern "C" uint32 wsrep_thd_wsrep_rand(THD *thd);
158extern "C" time_t wsrep_thd_query_start(THD *thd);
159extern "C" query_id_t wsrep_thd_query_id(THD *thd);
160extern "C" query_id_t wsrep_thd_wsrep_last_query_id(THD *thd);
161extern "C" void wsrep_thd_set_wsrep_last_query_id(THD *thd, query_id_t id);
162
163extern void wsrep_close_client_connections(my_bool wait_to_end);
164extern int wsrep_wait_committing_connections_close(int wait_time);
165extern void wsrep_close_applier(THD *thd);
166extern void wsrep_wait_appliers_close(THD *thd);
167extern void wsrep_close_applier_threads(int count);
168extern void wsrep_kill_mysql(THD *thd);
169
170/* new defines */
171extern void wsrep_stop_replication(THD *thd);
172extern bool wsrep_start_replication();
173extern bool wsrep_must_sync_wait(THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ);
174extern bool wsrep_sync_wait(THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ);
175extern int wsrep_check_opts();
176extern void wsrep_prepend_PATH (const char* path);
177
178/* Other global variables */
179extern wsrep_seqno_t wsrep_locked_seqno;
180
181#define WSREP_ON \
182 (global_system_variables.wsrep_on)
183
184#define WSREP_ON_NEW \
185 ((global_system_variables.wsrep_on) && \
186 wsrep_provider && \
187 strcmp(wsrep_provider, WSREP_NONE))
188
189#define WSREP(thd) \
190 (WSREP_ON && thd->variables.wsrep_on)
191
192#define WSREP_CLIENT(thd) \
193 (WSREP(thd) && thd->wsrep_client_thread)
194
195#define WSREP_EMULATE_BINLOG(thd) \
196 (WSREP(thd) && wsrep_emulate_bin_log)
197
198#define WSREP_FORMAT(my_format) \
199 ((wsrep_forced_binlog_format != BINLOG_FORMAT_UNSPEC) \
200 ? wsrep_forced_binlog_format : (ulong)(my_format))
201
202// prefix all messages with "WSREP"
203void wsrep_log(void (*fun)(const char *, ...), const char *format, ...);
204#define WSREP_LOG(fun, ...) wsrep_log(fun, ## __VA_ARGS__)
205#define WSREP_LOG_CONFLICT_THD(thd, role) \
206 WSREP_LOG(sql_print_information, \
207 "%s: \n " \
208 " THD: %lu, mode: %s, state: %s, conflict: %s, seqno: %lld\n " \
209 " SQL: %s", \
210 role, thd_get_thread_id(thd), wsrep_thd_exec_mode_str(thd), \
211 wsrep_thd_query_state_str(thd), \
212 wsrep_thd_conflict_state_str(thd), (long long)wsrep_thd_trx_seqno(thd), \
213 wsrep_thd_query(thd) \
214 );
215
216#define WSREP_LOG_CONFLICT(bf_thd, victim_thd, bf_abort) \
217 if (wsrep_debug || wsrep_log_conflicts) \
218 { \
219 WSREP_LOG(sql_print_information, "cluster conflict due to %s for threads:",\
220 (bf_abort) ? "high priority abort" : "certification failure" \
221 ); \
222 if (bf_thd != NULL) WSREP_LOG_CONFLICT_THD(bf_thd, "Winning thread"); \
223 if (victim_thd) WSREP_LOG_CONFLICT_THD(victim_thd, "Victim thread"); \
224 }
225
226#define WSREP_PROVIDER_EXISTS \
227 (wsrep_provider && strncasecmp(wsrep_provider, WSREP_NONE, FN_REFLEN))
228
229#define WSREP_QUERY(thd) (thd->query())
230
231extern void wsrep_ready_wait();
232
233class Ha_trx_info;
234struct THD_TRANS;
235void wsrep_register_hton(THD* thd, bool all);
236void wsrep_brute_force_killer(THD *thd);
237int wsrep_hire_brute_force_killer(THD *thd, uint64_t trx_id);
238
239/* this is visible for client build so that innodb plugin gets this */
240typedef struct wsrep_aborting_thd {
241 struct wsrep_aborting_thd *next;
242 THD *aborting_thd;
243} *wsrep_aborting_thd_t;
244
245extern mysql_mutex_t LOCK_wsrep_ready;
246extern mysql_cond_t COND_wsrep_ready;
247extern mysql_mutex_t LOCK_wsrep_sst;
248extern mysql_cond_t COND_wsrep_sst;
249extern mysql_mutex_t LOCK_wsrep_sst_init;
250extern mysql_cond_t COND_wsrep_sst_init;
251extern mysql_mutex_t LOCK_wsrep_rollback;
252extern mysql_cond_t COND_wsrep_rollback;
253extern int wsrep_replaying;
254extern mysql_mutex_t LOCK_wsrep_replaying;
255extern mysql_cond_t COND_wsrep_replaying;
256extern mysql_mutex_t LOCK_wsrep_slave_threads;
257extern mysql_mutex_t LOCK_wsrep_desync;
258extern mysql_mutex_t LOCK_wsrep_config_state;
259extern wsrep_aborting_thd_t wsrep_aborting_thd;
260extern my_bool wsrep_emulate_bin_log;
261extern int wsrep_to_isolation;
262#ifdef GTID_SUPPORT
263extern rpl_sidno wsrep_sidno;
264#endif /* GTID_SUPPORT */
265extern my_bool wsrep_preordered_opt;
266extern handlerton *wsrep_hton;
267
268#ifdef HAVE_PSI_INTERFACE
269extern PSI_mutex_key key_LOCK_wsrep_ready;
270extern PSI_mutex_key key_COND_wsrep_ready;
271extern PSI_mutex_key key_LOCK_wsrep_sst;
272extern PSI_cond_key key_COND_wsrep_sst;
273extern PSI_mutex_key key_LOCK_wsrep_sst_init;
274extern PSI_cond_key key_COND_wsrep_sst_init;
275extern PSI_mutex_key key_LOCK_wsrep_sst_thread;
276extern PSI_cond_key key_COND_wsrep_sst_thread;
277extern PSI_mutex_key key_LOCK_wsrep_rollback;
278extern PSI_cond_key key_COND_wsrep_rollback;
279extern PSI_mutex_key key_LOCK_wsrep_replaying;
280extern PSI_cond_key key_COND_wsrep_replaying;
281extern PSI_mutex_key key_LOCK_wsrep_slave_threads;
282extern PSI_mutex_key key_LOCK_wsrep_desync;
283
284extern PSI_file_key key_file_wsrep_gra_log;
285#endif /* HAVE_PSI_INTERFACE */
286struct TABLE_LIST;
287int wsrep_to_isolation_begin(THD *thd, const char *db_, const char *table_,
288 const TABLE_LIST* table_list);
289void wsrep_to_isolation_end(THD *thd);
290void wsrep_cleanup_transaction(THD *thd);
291int wsrep_to_buf_helper(
292 THD* thd, const char *query, uint query_len, uchar** buf, size_t* buf_len);
293int wsrep_create_event_query(THD *thd, uchar** buf, size_t* buf_len);
294
295extern bool
296wsrep_grant_mdl_exception(MDL_context *requestor_ctx,
297 MDL_ticket *ticket,
298 const MDL_key *key);
299IO_CACHE * get_trans_log(THD * thd);
300bool wsrep_trans_cache_is_empty(THD *thd);
301void thd_binlog_flush_pending_rows_event(THD *thd, bool stmt_end);
302void thd_binlog_rollback_stmt(THD * thd);
303void thd_binlog_trx_reset(THD * thd);
304
305typedef void (*wsrep_thd_processor_fun)(THD *);
306pthread_handler_t start_wsrep_THD(void *arg);
307int wsrep_wait_committing_connections_close(int wait_time);
308void wsrep_close_client_connections(my_bool wait_to_end);
309void wsrep_close_applier(THD *thd);
310void wsrep_close_applier_threads(int count);
311void wsrep_wait_appliers_close(THD *thd);
312void wsrep_kill_mysql(THD *thd);
313void wsrep_close_threads(THD *thd);
314void wsrep_copy_query(THD *thd);
315bool wsrep_is_show_query(enum enum_sql_command command);
316void wsrep_replay_transaction(THD *thd);
317bool wsrep_create_like_table(THD* thd, TABLE_LIST* table,
318 TABLE_LIST* src_table,
319 HA_CREATE_INFO *create_info);
320bool wsrep_node_is_donor();
321bool wsrep_node_is_synced();
322
323#define WSREP_BINLOG_FORMAT(my_format) \
324 ((wsrep_forced_binlog_format != BINLOG_FORMAT_UNSPEC) ? \
325 wsrep_forced_binlog_format : my_format)
326
327#else /* WITH_WSREP */
328
329#define WSREP(T) (0)
330#define WSREP_ON (0)
331#define WSREP_EMULATE_BINLOG(thd) (0)
332#define WSREP_CLIENT(thd) (0)
333#define WSREP_FORMAT(my_format) ((ulong)my_format)
334#define WSREP_PROVIDER_EXISTS (0)
335#define wsrep_emulate_bin_log (0)
336#define wsrep_to_isolation (0)
337#define wsrep_init() (1)
338#define wsrep_prepend_PATH(X)
339#define wsrep_before_SE() (0)
340#define wsrep_init_startup(X)
341#define wsrep_must_sync_wait(...) (0)
342#define wsrep_sync_wait(...) (0)
343#define wsrep_to_isolation_begin(...) (0)
344#define wsrep_register_hton(...) do { } while(0)
345#define wsrep_check_opts() (0)
346#define wsrep_stop_replication(X) do { } while(0)
347#define wsrep_inited (0)
348#define wsrep_deinit(X) do { } while(0)
349#define wsrep_recover() do { } while(0)
350#define wsrep_slave_threads (1)
351#define wsrep_replicate_myisam (0)
352#define wsrep_thr_init() do {} while(0)
353#define wsrep_thr_deinit() do {} while(0)
354#define wsrep_running_threads (0)
355#define WSREP_BINLOG_FORMAT(my_format) my_format
356
357#endif /* WITH_WSREP */
358#endif /* WSREP_MYSQLD_H */
359