| 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 | |
| 26 | typedef 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 | |
| 36 | class set_var; |
| 37 | class THD; |
| 38 | |
| 39 | enum wsrep_consistency_check_mode { |
| 40 | NO_CONSISTENCY_CHECK, |
| 41 | CONSISTENCY_CHECK_DECLARED, |
| 42 | CONSISTENCY_CHECK_RUNNING, |
| 43 | }; |
| 44 | |
| 45 | struct 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 |
| 58 | extern wsrep_t* wsrep; |
| 59 | |
| 60 | // MySQL wsrep options |
| 61 | extern const char* wsrep_provider; |
| 62 | extern const char* wsrep_provider_options; |
| 63 | extern const char* wsrep_cluster_name; |
| 64 | extern const char* wsrep_cluster_address; |
| 65 | extern const char* wsrep_node_name; |
| 66 | extern const char* wsrep_node_address; |
| 67 | extern const char* wsrep_node_incoming_address; |
| 68 | extern const char* wsrep_data_home_dir; |
| 69 | extern const char* wsrep_dbug_option; |
| 70 | extern long wsrep_slave_threads; |
| 71 | extern int wsrep_slave_count_change; |
| 72 | extern my_bool wsrep_convert_LOCK_to_trx; |
| 73 | extern ulong wsrep_retry_autocommit; |
| 74 | extern my_bool wsrep_auto_increment_control; |
| 75 | extern my_bool wsrep_incremental_data_collection; |
| 76 | extern const char* wsrep_start_position; |
| 77 | extern ulong wsrep_max_ws_size; |
| 78 | extern ulong wsrep_max_ws_rows; |
| 79 | extern const char* wsrep_notify_cmd; |
| 80 | extern long wsrep_max_protocol_version; |
| 81 | extern ulong wsrep_forced_binlog_format; |
| 82 | extern my_bool wsrep_desync; |
| 83 | extern ulong wsrep_reject_queries; |
| 84 | extern my_bool wsrep_replicate_myisam; |
| 85 | extern ulong wsrep_mysql_replication_bundle; |
| 86 | extern my_bool wsrep_restart_slave; |
| 87 | extern my_bool wsrep_restart_slave_activated; |
| 88 | extern my_bool wsrep_slave_FK_checks; |
| 89 | extern my_bool wsrep_slave_UK_checks; |
| 90 | extern ulong wsrep_running_threads; |
| 91 | extern bool wsrep_new_cluster; |
| 92 | extern bool wsrep_gtid_mode; |
| 93 | extern uint32 wsrep_gtid_domain_id; |
| 94 | |
| 95 | enum 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 | |
| 101 | enum enum_wsrep_OSU_method { |
| 102 | WSREP_OSU_TOI, |
| 103 | WSREP_OSU_RSU, |
| 104 | WSREP_OSU_NONE, |
| 105 | }; |
| 106 | |
| 107 | enum 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 |
| 118 | extern my_bool wsrep_connected; |
| 119 | extern my_bool wsrep_ready; |
| 120 | extern const char* wsrep_cluster_state_uuid; |
| 121 | extern long long wsrep_cluster_conf_id; |
| 122 | extern const char* wsrep_cluster_status; |
| 123 | extern long wsrep_cluster_size; |
| 124 | extern long wsrep_local_index; |
| 125 | extern long long wsrep_local_bf_aborts; |
| 126 | extern const char* wsrep_provider_name; |
| 127 | extern const char* wsrep_provider_version; |
| 128 | extern const char* wsrep_provider_vendor; |
| 129 | |
| 130 | int wsrep_show_status(THD *thd, SHOW_VAR *var, char *buff, |
| 131 | enum enum_var_type scope); |
| 132 | int wsrep_init(); |
| 133 | void wsrep_deinit(bool free_options); |
| 134 | |
| 135 | /* Initialize wsrep thread LOCKs and CONDs */ |
| 136 | void wsrep_thr_init(); |
| 137 | /* Destroy wsrep thread LOCKs and CONDs */ |
| 138 | void wsrep_thr_deinit(); |
| 139 | |
| 140 | void wsrep_recover(); |
| 141 | bool 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 */ |
| 145 | void wsrep_init_startup(bool before); |
| 146 | |
| 147 | // Other wsrep global variables |
| 148 | extern my_bool wsrep_inited; // whether wsrep is initialized ? |
| 149 | |
| 150 | |
| 151 | extern "C" void wsrep_thd_set_exec_mode(THD *thd, enum wsrep_exec_mode mode); |
| 152 | extern "C" void wsrep_thd_set_query_state( |
| 153 | THD *thd, enum wsrep_query_state state); |
| 154 | |
| 155 | extern "C" void wsrep_thd_set_trx_to_replay(THD *thd, uint64 trx_id); |
| 156 | |
| 157 | extern "C" uint32 wsrep_thd_wsrep_rand(THD *thd); |
| 158 | extern "C" time_t wsrep_thd_query_start(THD *thd); |
| 159 | extern "C" query_id_t wsrep_thd_query_id(THD *thd); |
| 160 | extern "C" query_id_t wsrep_thd_wsrep_last_query_id(THD *thd); |
| 161 | extern "C" void wsrep_thd_set_wsrep_last_query_id(THD *thd, query_id_t id); |
| 162 | |
| 163 | extern void wsrep_close_client_connections(my_bool wait_to_end); |
| 164 | extern int wsrep_wait_committing_connections_close(int wait_time); |
| 165 | extern void wsrep_close_applier(THD *thd); |
| 166 | extern void wsrep_wait_appliers_close(THD *thd); |
| 167 | extern void wsrep_close_applier_threads(int count); |
| 168 | extern void wsrep_kill_mysql(THD *thd); |
| 169 | |
| 170 | /* new defines */ |
| 171 | extern void wsrep_stop_replication(THD *thd); |
| 172 | extern bool wsrep_start_replication(); |
| 173 | extern bool wsrep_must_sync_wait(THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ); |
| 174 | extern bool wsrep_sync_wait(THD* thd, uint mask = WSREP_SYNC_WAIT_BEFORE_READ); |
| 175 | extern int wsrep_check_opts(); |
| 176 | extern void wsrep_prepend_PATH (const char* path); |
| 177 | |
| 178 | /* Other global variables */ |
| 179 | extern 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" |
| 203 | void 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 | |
| 231 | extern void wsrep_ready_wait(); |
| 232 | |
| 233 | class Ha_trx_info; |
| 234 | struct THD_TRANS; |
| 235 | void wsrep_register_hton(THD* thd, bool all); |
| 236 | void wsrep_brute_force_killer(THD *thd); |
| 237 | int 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 */ |
| 240 | typedef struct wsrep_aborting_thd { |
| 241 | struct wsrep_aborting_thd *next; |
| 242 | THD *aborting_thd; |
| 243 | } *wsrep_aborting_thd_t; |
| 244 | |
| 245 | extern mysql_mutex_t LOCK_wsrep_ready; |
| 246 | extern mysql_cond_t COND_wsrep_ready; |
| 247 | extern mysql_mutex_t LOCK_wsrep_sst; |
| 248 | extern mysql_cond_t COND_wsrep_sst; |
| 249 | extern mysql_mutex_t LOCK_wsrep_sst_init; |
| 250 | extern mysql_cond_t COND_wsrep_sst_init; |
| 251 | extern mysql_mutex_t LOCK_wsrep_rollback; |
| 252 | extern mysql_cond_t COND_wsrep_rollback; |
| 253 | extern int wsrep_replaying; |
| 254 | extern mysql_mutex_t LOCK_wsrep_replaying; |
| 255 | extern mysql_cond_t COND_wsrep_replaying; |
| 256 | extern mysql_mutex_t LOCK_wsrep_slave_threads; |
| 257 | extern mysql_mutex_t LOCK_wsrep_desync; |
| 258 | extern mysql_mutex_t LOCK_wsrep_config_state; |
| 259 | extern wsrep_aborting_thd_t wsrep_aborting_thd; |
| 260 | extern my_bool wsrep_emulate_bin_log; |
| 261 | extern int wsrep_to_isolation; |
| 262 | #ifdef GTID_SUPPORT |
| 263 | extern rpl_sidno wsrep_sidno; |
| 264 | #endif /* GTID_SUPPORT */ |
| 265 | extern my_bool wsrep_preordered_opt; |
| 266 | extern handlerton *wsrep_hton; |
| 267 | |
| 268 | #ifdef HAVE_PSI_INTERFACE |
| 269 | extern PSI_mutex_key key_LOCK_wsrep_ready; |
| 270 | extern PSI_mutex_key key_COND_wsrep_ready; |
| 271 | extern PSI_mutex_key key_LOCK_wsrep_sst; |
| 272 | extern PSI_cond_key key_COND_wsrep_sst; |
| 273 | extern PSI_mutex_key key_LOCK_wsrep_sst_init; |
| 274 | extern PSI_cond_key key_COND_wsrep_sst_init; |
| 275 | extern PSI_mutex_key key_LOCK_wsrep_sst_thread; |
| 276 | extern PSI_cond_key key_COND_wsrep_sst_thread; |
| 277 | extern PSI_mutex_key key_LOCK_wsrep_rollback; |
| 278 | extern PSI_cond_key key_COND_wsrep_rollback; |
| 279 | extern PSI_mutex_key key_LOCK_wsrep_replaying; |
| 280 | extern PSI_cond_key key_COND_wsrep_replaying; |
| 281 | extern PSI_mutex_key key_LOCK_wsrep_slave_threads; |
| 282 | extern PSI_mutex_key key_LOCK_wsrep_desync; |
| 283 | |
| 284 | extern PSI_file_key key_file_wsrep_gra_log; |
| 285 | #endif /* HAVE_PSI_INTERFACE */ |
| 286 | struct TABLE_LIST; |
| 287 | int wsrep_to_isolation_begin(THD *thd, const char *db_, const char *table_, |
| 288 | const TABLE_LIST* table_list); |
| 289 | void wsrep_to_isolation_end(THD *thd); |
| 290 | void wsrep_cleanup_transaction(THD *thd); |
| 291 | int wsrep_to_buf_helper( |
| 292 | THD* thd, const char *query, uint query_len, uchar** buf, size_t* buf_len); |
| 293 | int wsrep_create_event_query(THD *thd, uchar** buf, size_t* buf_len); |
| 294 | |
| 295 | extern bool |
| 296 | wsrep_grant_mdl_exception(MDL_context *requestor_ctx, |
| 297 | MDL_ticket *ticket, |
| 298 | const MDL_key *key); |
| 299 | IO_CACHE * get_trans_log(THD * thd); |
| 300 | bool wsrep_trans_cache_is_empty(THD *thd); |
| 301 | void thd_binlog_flush_pending_rows_event(THD *thd, bool stmt_end); |
| 302 | void thd_binlog_rollback_stmt(THD * thd); |
| 303 | void thd_binlog_trx_reset(THD * thd); |
| 304 | |
| 305 | typedef void (*wsrep_thd_processor_fun)(THD *); |
| 306 | pthread_handler_t start_wsrep_THD(void *arg); |
| 307 | int wsrep_wait_committing_connections_close(int wait_time); |
| 308 | void wsrep_close_client_connections(my_bool wait_to_end); |
| 309 | void wsrep_close_applier(THD *thd); |
| 310 | void wsrep_close_applier_threads(int count); |
| 311 | void wsrep_wait_appliers_close(THD *thd); |
| 312 | void wsrep_kill_mysql(THD *thd); |
| 313 | void wsrep_close_threads(THD *thd); |
| 314 | void wsrep_copy_query(THD *thd); |
| 315 | bool wsrep_is_show_query(enum enum_sql_command command); |
| 316 | void wsrep_replay_transaction(THD *thd); |
| 317 | bool wsrep_create_like_table(THD* thd, TABLE_LIST* table, |
| 318 | TABLE_LIST* src_table, |
| 319 | HA_CREATE_INFO *create_info); |
| 320 | bool wsrep_node_is_donor(); |
| 321 | bool 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 | |