1 | /* Copyright (C) 2008-2017 Kentoku Shiba |
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 | #define MYSQL_SERVER 1 |
17 | #include <my_global.h> |
18 | #include "mysql_version.h" |
19 | #include "spd_environ.h" |
20 | #if MYSQL_VERSION_ID < 50500 |
21 | #include "mysql_priv.h" |
22 | #include <mysql/plugin.h> |
23 | #else |
24 | #include "sql_priv.h" |
25 | #include "probes_mysql.h" |
26 | #include "sql_class.h" |
27 | #include "sql_partition.h" |
28 | #include "records.h" |
29 | #endif |
30 | #include "spd_err.h" |
31 | #include "spd_param.h" |
32 | #include "spd_db_include.h" |
33 | #include "spd_include.h" |
34 | #include "spd_sys_table.h" |
35 | #include "ha_spider.h" |
36 | #include "spd_trx.h" |
37 | #include "spd_db_conn.h" |
38 | #include "spd_table.h" |
39 | #include "spd_conn.h" |
40 | #include "spd_ping_table.h" |
41 | #include "spd_malloc.h" |
42 | |
43 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
44 | #else |
45 | #ifdef XID_CACHE_IS_SPLITTED |
46 | extern uint *spd_db_att_xid_cache_split_num; |
47 | #endif |
48 | extern pthread_mutex_t *spd_db_att_LOCK_xid_cache; |
49 | extern HASH *spd_db_att_xid_cache; |
50 | #endif |
51 | extern struct charset_info_st *spd_charset_utf8_bin; |
52 | |
53 | extern handlerton *spider_hton_ptr; |
54 | extern SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE]; |
55 | pthread_mutex_t spider_thread_id_mutex; |
56 | ulonglong spider_thread_id = 1; |
57 | |
58 | #ifdef HAVE_PSI_INTERFACE |
59 | extern PSI_mutex_key spd_key_mutex_udf_table; |
60 | #endif |
61 | |
62 | extern HASH spider_allocated_thds; |
63 | extern uint spider_allocated_thds_id; |
64 | extern const char *spider_allocated_thds_func_name; |
65 | extern const char *spider_allocated_thds_file_name; |
66 | extern ulong spider_allocated_thds_line_no; |
67 | extern pthread_mutex_t spider_allocated_thds_mutex; |
68 | |
69 | // for spider_alter_tables |
70 | uchar *spider_alter_tbl_get_key( |
71 | SPIDER_ALTER_TABLE *alter_table, |
72 | size_t *length, |
73 | my_bool not_used __attribute__ ((unused)) |
74 | ) { |
75 | DBUG_ENTER("spider_alter_tbl_get_key" ); |
76 | *length = alter_table->table_name_length; |
77 | DBUG_PRINT("info" ,("spider table_name_length=%zu" , *length)); |
78 | DBUG_PRINT("info" ,("spider table_name=%s" , alter_table->table_name)); |
79 | DBUG_RETURN((uchar*) alter_table->table_name); |
80 | } |
81 | |
82 | // for SPIDER_TRX_HA |
83 | uchar *spider_trx_ha_get_key( |
84 | SPIDER_TRX_HA *trx_ha, |
85 | size_t *length, |
86 | my_bool not_used __attribute__ ((unused)) |
87 | ) { |
88 | DBUG_ENTER("spider_trx_ha_get_key" ); |
89 | *length = trx_ha->table_name_length; |
90 | DBUG_PRINT("info" ,("spider table_name_length=%zu" , *length)); |
91 | DBUG_PRINT("info" ,("spider table_name=%s" , trx_ha->table_name)); |
92 | DBUG_RETURN((uchar*) trx_ha->table_name); |
93 | } |
94 | |
95 | int spider_free_trx_conn( |
96 | SPIDER_TRX *trx, |
97 | bool trx_free |
98 | ) { |
99 | int roop_count; |
100 | SPIDER_CONN *conn; |
101 | DBUG_ENTER("spider_free_trx_conn" ); |
102 | roop_count = 0; |
103 | if ( |
104 | trx_free || |
105 | spider_param_conn_recycle_mode(trx->thd) != 2 |
106 | ) { |
107 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
108 | roop_count))) |
109 | { |
110 | spider_conn_clear_queue_at_commit(conn); |
111 | if (conn->table_lock) |
112 | { |
113 | DBUG_ASSERT(!trx_free); |
114 | roop_count++; |
115 | } else |
116 | spider_free_conn_from_trx(trx, conn, FALSE, trx_free, &roop_count); |
117 | } |
118 | trx->trx_conn_adjustment++; |
119 | } else { |
120 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
121 | roop_count))) |
122 | { |
123 | spider_conn_clear_queue_at_commit(conn); |
124 | if (conn->table_lock) |
125 | { |
126 | DBUG_ASSERT(!trx_free); |
127 | } else |
128 | conn->error_mode = 1; |
129 | roop_count++; |
130 | } |
131 | } |
132 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
133 | roop_count = 0; |
134 | if ( |
135 | trx_free || |
136 | spider_param_hs_r_conn_recycle_mode(trx->thd) != 2 |
137 | ) { |
138 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_hs_r_conn_hash, |
139 | roop_count))) |
140 | { |
141 | if (conn->table_lock) |
142 | { |
143 | DBUG_ASSERT(!trx_free); |
144 | roop_count++; |
145 | } else |
146 | spider_free_conn_from_trx(trx, conn, FALSE, trx_free, &roop_count); |
147 | } |
148 | trx->trx_hs_r_conn_adjustment++; |
149 | } else { |
150 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_hs_r_conn_hash, |
151 | roop_count))) |
152 | { |
153 | if (conn->table_lock) |
154 | { |
155 | DBUG_ASSERT(!trx_free); |
156 | } else |
157 | conn->error_mode = 1; |
158 | roop_count++; |
159 | } |
160 | } |
161 | roop_count = 0; |
162 | if ( |
163 | trx_free || |
164 | spider_param_hs_w_conn_recycle_mode(trx->thd) != 2 |
165 | ) { |
166 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_hs_w_conn_hash, |
167 | roop_count))) |
168 | { |
169 | if (conn->table_lock) |
170 | { |
171 | DBUG_ASSERT(!trx_free); |
172 | roop_count++; |
173 | } else |
174 | spider_free_conn_from_trx(trx, conn, FALSE, trx_free, &roop_count); |
175 | } |
176 | trx->trx_hs_w_conn_adjustment++; |
177 | } else { |
178 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_hs_w_conn_hash, |
179 | roop_count))) |
180 | { |
181 | if (conn->table_lock) |
182 | { |
183 | DBUG_ASSERT(!trx_free); |
184 | } else |
185 | conn->error_mode = 1; |
186 | roop_count++; |
187 | } |
188 | } |
189 | #endif |
190 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
191 | if (trx_free) |
192 | { |
193 | while ((conn = (SPIDER_CONN*) my_hash_element( |
194 | &trx->trx_direct_hs_r_conn_hash, 0))) |
195 | { |
196 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
197 | my_hash_delete_with_hash_value(&trx->trx_direct_hs_r_conn_hash, |
198 | conn->conn_key_hash_value, (uchar*) conn); |
199 | #else |
200 | my_hash_delete(&trx->trx_direct_hs_r_conn_hash, (uchar*) conn); |
201 | #endif |
202 | spider_free_conn(conn); |
203 | } |
204 | while ((conn = (SPIDER_CONN*) my_hash_element( |
205 | &trx->trx_direct_hs_w_conn_hash, 0))) |
206 | { |
207 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
208 | my_hash_delete_with_hash_value(&trx->trx_direct_hs_w_conn_hash, |
209 | conn->conn_key_hash_value, (uchar*) conn); |
210 | #else |
211 | my_hash_delete(&trx->trx_direct_hs_w_conn_hash, (uchar*) conn); |
212 | #endif |
213 | spider_free_conn(conn); |
214 | } |
215 | } |
216 | #endif |
217 | DBUG_RETURN(0); |
218 | } |
219 | |
220 | int spider_free_trx_another_conn( |
221 | SPIDER_TRX *trx, |
222 | bool lock |
223 | ) { |
224 | int error_num, tmp_error_num; |
225 | int roop_count = 0; |
226 | SPIDER_CONN *conn; |
227 | DBUG_ENTER("spider_free_trx_another_conn" ); |
228 | trx->tmp_spider->conns = &conn; |
229 | error_num = 0; |
230 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_another_conn_hash, |
231 | roop_count))) |
232 | { |
233 | if (lock && (tmp_error_num = spider_db_unlock_tables(trx->tmp_spider, 0))) |
234 | error_num = tmp_error_num; |
235 | spider_free_conn_from_trx(trx, conn, TRUE, TRUE, &roop_count); |
236 | } |
237 | DBUG_RETURN(error_num); |
238 | } |
239 | |
240 | int spider_trx_another_lock_tables( |
241 | SPIDER_TRX *trx |
242 | ) { |
243 | int error_num; |
244 | int roop_count = 0, need_mon = 0; |
245 | THD *thd = trx->thd; |
246 | SPIDER_CONN *conn; |
247 | ha_spider tmp_spider; |
248 | SPIDER_SHARE tmp_share; |
249 | char sql_buf[MAX_FIELD_WIDTH]; |
250 | spider_string sql_str(sql_buf, sizeof(sql_buf), system_charset_info); |
251 | DBUG_ENTER("spider_trx_another_lock_tables" ); |
252 | SPIDER_BACKUP_DASTATUS; |
253 | sql_str.init_calc_mem(188); |
254 | sql_str.length(0); |
255 | memset((void*)&tmp_spider, 0, sizeof(ha_spider)); |
256 | memset(&tmp_share, 0, sizeof(SPIDER_SHARE)); |
257 | tmp_spider.share = &tmp_share; |
258 | tmp_spider.trx = trx; |
259 | tmp_share.access_charset = system_charset_info; |
260 | /* |
261 | if ((error_num = spider_db_append_set_names(&tmp_share))) |
262 | DBUG_RETURN(error_num); |
263 | */ |
264 | tmp_spider.conns = &conn; |
265 | tmp_spider.result_list.sqls = &sql_str; |
266 | tmp_spider.need_mons = &need_mon; |
267 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_another_conn_hash, |
268 | roop_count))) |
269 | { |
270 | if ((error_num = spider_db_lock_tables(&tmp_spider, 0))) |
271 | { |
272 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
273 | if (error_num) |
274 | { |
275 | /* |
276 | spider_db_free_set_names(&tmp_share); |
277 | */ |
278 | DBUG_RETURN(error_num); |
279 | } |
280 | } |
281 | roop_count++; |
282 | } |
283 | /* |
284 | spider_db_free_set_names(&tmp_share); |
285 | */ |
286 | DBUG_RETURN(0); |
287 | } |
288 | |
289 | int spider_trx_another_flush_tables( |
290 | SPIDER_TRX *trx |
291 | ) { |
292 | int error_num; |
293 | int roop_count = 0, need_mon = 0; |
294 | THD *thd = trx->thd; |
295 | SPIDER_CONN *conn; |
296 | ha_spider tmp_spider; |
297 | SPIDER_SHARE tmp_share; |
298 | long tmp_link_statuses = SPIDER_LINK_STATUS_OK; |
299 | DBUG_ENTER("spider_trx_another_flush_tables" ); |
300 | SPIDER_BACKUP_DASTATUS; |
301 | memset((void*)&tmp_spider, 0, sizeof(ha_spider)); |
302 | tmp_share.link_count = 1; |
303 | tmp_share.all_link_count = 1; |
304 | tmp_share.link_statuses = &tmp_link_statuses; |
305 | tmp_share.link_statuses_length = 1; |
306 | tmp_spider.share = &tmp_share; |
307 | tmp_spider.conns = &conn; |
308 | tmp_spider.need_mons = &need_mon; |
309 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_another_conn_hash, |
310 | roop_count))) |
311 | { |
312 | if ((error_num = spider_db_flush_tables(&tmp_spider, FALSE))) |
313 | { |
314 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
315 | if (error_num) |
316 | DBUG_RETURN(error_num); |
317 | } |
318 | roop_count++; |
319 | } |
320 | DBUG_RETURN(0); |
321 | } |
322 | |
323 | int spider_trx_all_flush_tables( |
324 | SPIDER_TRX *trx |
325 | ) { |
326 | int error_num; |
327 | int roop_count = 0, need_mon = 0; |
328 | THD *thd = trx->thd; |
329 | SPIDER_CONN *conn; |
330 | ha_spider tmp_spider; |
331 | SPIDER_SHARE tmp_share; |
332 | long tmp_link_statuses = SPIDER_LINK_STATUS_OK; |
333 | DBUG_ENTER("spider_trx_all_flush_tables" ); |
334 | SPIDER_BACKUP_DASTATUS; |
335 | memset((void*)&tmp_spider, 0, sizeof(ha_spider)); |
336 | tmp_share.link_count = 1; |
337 | tmp_share.all_link_count = 1; |
338 | tmp_share.link_statuses = &tmp_link_statuses; |
339 | tmp_share.link_statuses_length = 1; |
340 | tmp_spider.share = &tmp_share; |
341 | tmp_spider.conns = &conn; |
342 | tmp_spider.need_mons = &need_mon; |
343 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
344 | roop_count))) |
345 | { |
346 | if ((error_num = spider_db_flush_tables(&tmp_spider, TRUE))) |
347 | { |
348 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
349 | if (error_num) |
350 | DBUG_RETURN(error_num); |
351 | } |
352 | roop_count++; |
353 | } |
354 | DBUG_RETURN(0); |
355 | } |
356 | |
357 | int spider_trx_all_unlock_tables( |
358 | SPIDER_TRX *trx |
359 | ) { |
360 | int error_num; |
361 | int roop_count = 0; |
362 | THD *thd = trx->thd; |
363 | SPIDER_CONN *conn; |
364 | DBUG_ENTER("spider_trx_all_unlock_tables" ); |
365 | SPIDER_BACKUP_DASTATUS; |
366 | trx->tmp_spider->conns = &conn; |
367 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
368 | roop_count))) |
369 | { |
370 | if ((error_num = spider_db_unlock_tables(trx->tmp_spider, 0))) |
371 | { |
372 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
373 | if (error_num) |
374 | DBUG_RETURN(error_num); |
375 | } |
376 | roop_count++; |
377 | } |
378 | DBUG_RETURN(0); |
379 | } |
380 | |
381 | int spider_trx_all_start_trx( |
382 | SPIDER_TRX *trx |
383 | ) { |
384 | int error_num, need_mon = 0; |
385 | int roop_count = 0; |
386 | THD *thd = trx->thd; |
387 | SPIDER_CONN *conn; |
388 | ha_spider tmp_spider; |
389 | DBUG_ENTER("spider_trx_all_start_trx" ); |
390 | SPIDER_BACKUP_DASTATUS; |
391 | memset((void*)&tmp_spider, 0, sizeof(ha_spider)); |
392 | tmp_spider.trx = trx; |
393 | tmp_spider.need_mons = &need_mon; |
394 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
395 | roop_count))) |
396 | { |
397 | if ( |
398 | (spider_param_sync_trx_isolation(trx->thd) && |
399 | (error_num = spider_check_and_set_trx_isolation(conn, &need_mon))) || |
400 | (error_num = spider_internal_start_trx(&tmp_spider, conn, 0)) |
401 | ) { |
402 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
403 | if (error_num) |
404 | DBUG_RETURN(error_num); |
405 | } |
406 | roop_count++; |
407 | } |
408 | DBUG_RETURN(0); |
409 | } |
410 | |
411 | int spider_trx_all_flush_logs( |
412 | SPIDER_TRX *trx |
413 | ) { |
414 | int error_num; |
415 | int roop_count = 0, need_mon = 0; |
416 | THD *thd = trx->thd; |
417 | SPIDER_CONN *conn; |
418 | ha_spider tmp_spider; |
419 | SPIDER_SHARE tmp_share; |
420 | long tmp_link_statuses = SPIDER_LINK_STATUS_OK; |
421 | uint conn_link_idx = 0; |
422 | long net_read_timeout = 600; |
423 | long net_write_timeout = 600; |
424 | DBUG_ENTER("spider_trx_all_flush_logs" ); |
425 | SPIDER_BACKUP_DASTATUS; |
426 | memset((void*)&tmp_spider, 0, sizeof(ha_spider)); |
427 | tmp_share.link_count = 1; |
428 | tmp_share.all_link_count = 1; |
429 | tmp_share.link_statuses = &tmp_link_statuses; |
430 | tmp_share.link_statuses_length = 1; |
431 | tmp_share.net_read_timeouts = &net_read_timeout; |
432 | tmp_share.net_read_timeouts_length = 1; |
433 | tmp_share.net_write_timeouts = &net_write_timeout; |
434 | tmp_share.net_write_timeouts_length = 1; |
435 | tmp_spider.share = &tmp_share; |
436 | tmp_spider.conns = &conn; |
437 | tmp_spider.need_mons = &need_mon; |
438 | tmp_spider.conn_link_idx = &conn_link_idx; |
439 | tmp_spider.trx = trx; |
440 | while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash, |
441 | roop_count))) |
442 | { |
443 | if ((error_num = spider_db_flush_logs(&tmp_spider))) |
444 | { |
445 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
446 | if (error_num) |
447 | DBUG_RETURN(error_num); |
448 | } |
449 | roop_count++; |
450 | } |
451 | DBUG_RETURN(0); |
452 | } |
453 | |
454 | void spider_free_trx_alter_table_alloc( |
455 | SPIDER_TRX *trx, |
456 | SPIDER_ALTER_TABLE *alter_table |
457 | ) { |
458 | DBUG_ENTER("spider_free_trx_alter_table_alloc" ); |
459 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
460 | my_hash_delete_with_hash_value(&trx->trx_alter_table_hash, |
461 | alter_table->table_name_hash_value, (uchar*) alter_table); |
462 | #else |
463 | my_hash_delete(&trx->trx_alter_table_hash, (uchar*) alter_table); |
464 | #endif |
465 | if (alter_table->tmp_char) |
466 | spider_free(trx, alter_table->tmp_char, MYF(0)); |
467 | spider_free(trx, alter_table, MYF(0)); |
468 | DBUG_VOID_RETURN; |
469 | } |
470 | |
471 | int spider_free_trx_alter_table( |
472 | SPIDER_TRX *trx |
473 | ) { |
474 | SPIDER_ALTER_TABLE *alter_table; |
475 | DBUG_ENTER("spider_free_trx_alter_table" ); |
476 | while ((alter_table = |
477 | (SPIDER_ALTER_TABLE*) my_hash_element(&trx->trx_alter_table_hash, 0))) |
478 | { |
479 | spider_free_trx_alter_table_alloc(trx, alter_table); |
480 | } |
481 | DBUG_RETURN(0); |
482 | } |
483 | |
484 | int spider_create_trx_alter_table( |
485 | SPIDER_TRX *trx, |
486 | SPIDER_SHARE *share, |
487 | bool now_create |
488 | ) { |
489 | int error_num, roop_count; |
490 | SPIDER_ALTER_TABLE *alter_table, *share_alter; |
491 | char *tmp_name; |
492 | char **tmp_server_names; |
493 | char **tmp_tgt_table_names; |
494 | char **tmp_tgt_dbs; |
495 | char **tmp_tgt_hosts; |
496 | char **tmp_tgt_usernames; |
497 | char **tmp_tgt_passwords; |
498 | char **tmp_tgt_sockets; |
499 | char **tmp_tgt_wrappers; |
500 | char **tmp_tgt_ssl_cas; |
501 | char **tmp_tgt_ssl_capaths; |
502 | char **tmp_tgt_ssl_certs; |
503 | char **tmp_tgt_ssl_ciphers; |
504 | char **tmp_tgt_ssl_keys; |
505 | char **tmp_tgt_default_files; |
506 | char **tmp_tgt_default_groups; |
507 | char **tmp_static_link_ids; |
508 | uint *tmp_server_names_lengths; |
509 | uint *tmp_tgt_table_names_lengths; |
510 | uint *tmp_tgt_dbs_lengths; |
511 | uint *tmp_tgt_hosts_lengths; |
512 | uint *tmp_tgt_usernames_lengths; |
513 | uint *tmp_tgt_passwords_lengths; |
514 | uint *tmp_tgt_sockets_lengths; |
515 | uint *tmp_tgt_wrappers_lengths; |
516 | uint *tmp_tgt_ssl_cas_lengths; |
517 | uint *tmp_tgt_ssl_capaths_lengths; |
518 | uint *tmp_tgt_ssl_certs_lengths; |
519 | uint *tmp_tgt_ssl_ciphers_lengths; |
520 | uint *tmp_tgt_ssl_keys_lengths; |
521 | uint *tmp_tgt_default_files_lengths; |
522 | uint *tmp_tgt_default_groups_lengths; |
523 | uint *tmp_static_link_ids_lengths; |
524 | long *tmp_tgt_ports; |
525 | long *tmp_tgt_ssl_vscs; |
526 | long *tmp_monitoring_binlog_pos_at_failing; |
527 | long *tmp_link_statuses; |
528 | char *tmp_server_names_char; |
529 | char *tmp_tgt_table_names_char; |
530 | char *tmp_tgt_dbs_char; |
531 | char *tmp_tgt_hosts_char; |
532 | char *tmp_tgt_usernames_char; |
533 | char *tmp_tgt_passwords_char; |
534 | char *tmp_tgt_sockets_char; |
535 | char *tmp_tgt_wrappers_char; |
536 | char *tmp_tgt_ssl_cas_char; |
537 | char *tmp_tgt_ssl_capaths_char; |
538 | char *tmp_tgt_ssl_certs_char; |
539 | char *tmp_tgt_ssl_ciphers_char; |
540 | char *tmp_tgt_ssl_keys_char; |
541 | char *tmp_tgt_default_files_char; |
542 | char *tmp_tgt_default_groups_char; |
543 | char *tmp_static_link_ids_char; |
544 | uint old_elements; |
545 | |
546 | DBUG_ENTER("spider_create_trx_alter_table" ); |
547 | share_alter = &share->alter_table; |
548 | |
549 | if (!(alter_table = (SPIDER_ALTER_TABLE *) |
550 | spider_bulk_malloc(spider_current_trx, 55, MYF(MY_WME | MY_ZEROFILL), |
551 | &alter_table, sizeof(*alter_table), |
552 | &tmp_name, sizeof(char) * (share->table_name_length + 1), |
553 | |
554 | &tmp_server_names, sizeof(char *) * share->all_link_count, |
555 | &tmp_tgt_table_names, sizeof(char *) * share->all_link_count, |
556 | &tmp_tgt_dbs, sizeof(char *) * share->all_link_count, |
557 | &tmp_tgt_hosts, sizeof(char *) * share->all_link_count, |
558 | &tmp_tgt_usernames, sizeof(char *) * share->all_link_count, |
559 | &tmp_tgt_passwords, sizeof(char *) * share->all_link_count, |
560 | &tmp_tgt_sockets, sizeof(char *) * share->all_link_count, |
561 | &tmp_tgt_wrappers, sizeof(char *) * share->all_link_count, |
562 | &tmp_tgt_ssl_cas, sizeof(char *) * share->all_link_count, |
563 | &tmp_tgt_ssl_capaths, sizeof(char *) * share->all_link_count, |
564 | &tmp_tgt_ssl_certs, sizeof(char *) * share->all_link_count, |
565 | &tmp_tgt_ssl_ciphers, sizeof(char *) * share->all_link_count, |
566 | &tmp_tgt_ssl_keys, sizeof(char *) * share->all_link_count, |
567 | &tmp_tgt_default_files, sizeof(char *) * share->all_link_count, |
568 | &tmp_tgt_default_groups, sizeof(char *) * share->all_link_count, |
569 | &tmp_static_link_ids, sizeof(char *) * share->all_link_count, |
570 | |
571 | &tmp_server_names_lengths, sizeof(uint) * share->all_link_count, |
572 | &tmp_tgt_table_names_lengths, sizeof(uint) * share->all_link_count, |
573 | &tmp_tgt_dbs_lengths, sizeof(uint) * share->all_link_count, |
574 | &tmp_tgt_hosts_lengths, sizeof(uint) * share->all_link_count, |
575 | &tmp_tgt_usernames_lengths, sizeof(uint) * share->all_link_count, |
576 | &tmp_tgt_passwords_lengths, sizeof(uint) * share->all_link_count, |
577 | &tmp_tgt_sockets_lengths, sizeof(uint) * share->all_link_count, |
578 | &tmp_tgt_wrappers_lengths, sizeof(uint) * share->all_link_count, |
579 | &tmp_tgt_ssl_cas_lengths, sizeof(uint) * share->all_link_count, |
580 | &tmp_tgt_ssl_capaths_lengths, sizeof(uint) * share->all_link_count, |
581 | &tmp_tgt_ssl_certs_lengths, sizeof(uint) * share->all_link_count, |
582 | &tmp_tgt_ssl_ciphers_lengths, sizeof(uint) * share->all_link_count, |
583 | &tmp_tgt_ssl_keys_lengths, sizeof(uint) * share->all_link_count, |
584 | &tmp_tgt_default_files_lengths, sizeof(uint) * share->all_link_count, |
585 | &tmp_tgt_default_groups_lengths, sizeof(uint) * share->all_link_count, |
586 | &tmp_static_link_ids_lengths, sizeof(uint) * share->all_link_count, |
587 | |
588 | &tmp_tgt_ports, sizeof(long) * share->all_link_count, |
589 | &tmp_tgt_ssl_vscs, sizeof(long) * share->all_link_count, |
590 | &tmp_monitoring_binlog_pos_at_failing, |
591 | sizeof(long) * share->all_link_count, |
592 | &tmp_link_statuses, sizeof(long) * share->all_link_count, |
593 | |
594 | &tmp_server_names_char, sizeof(char) * |
595 | (share_alter->tmp_server_names_charlen + 1), |
596 | &tmp_tgt_table_names_char, sizeof(char) * |
597 | (share_alter->tmp_tgt_table_names_charlen + 1), |
598 | &tmp_tgt_dbs_char, sizeof(char) * |
599 | (share_alter->tmp_tgt_dbs_charlen + 1), |
600 | &tmp_tgt_hosts_char, sizeof(char) * |
601 | (share_alter->tmp_tgt_hosts_charlen + 1), |
602 | &tmp_tgt_usernames_char, sizeof(char) * |
603 | (share_alter->tmp_tgt_usernames_charlen + 1), |
604 | &tmp_tgt_passwords_char, sizeof(char) * |
605 | (share_alter->tmp_tgt_passwords_charlen + 1), |
606 | &tmp_tgt_sockets_char, sizeof(char) * |
607 | (share_alter->tmp_tgt_sockets_charlen + 1), |
608 | &tmp_tgt_wrappers_char, sizeof(char) * |
609 | (share_alter->tmp_tgt_wrappers_charlen + 1), |
610 | &tmp_tgt_ssl_cas_char, sizeof(char) * |
611 | (share_alter->tmp_tgt_ssl_cas_charlen + 1), |
612 | &tmp_tgt_ssl_capaths_char, sizeof(char) * |
613 | (share_alter->tmp_tgt_ssl_capaths_charlen + 1), |
614 | &tmp_tgt_ssl_certs_char, sizeof(char) * |
615 | (share_alter->tmp_tgt_ssl_certs_charlen + 1), |
616 | &tmp_tgt_ssl_ciphers_char, sizeof(char) * |
617 | (share_alter->tmp_tgt_ssl_ciphers_charlen + 1), |
618 | &tmp_tgt_ssl_keys_char, sizeof(char) * |
619 | (share_alter->tmp_tgt_ssl_keys_charlen + 1), |
620 | &tmp_tgt_default_files_char, sizeof(char) * |
621 | (share_alter->tmp_tgt_default_files_charlen + 1), |
622 | &tmp_tgt_default_groups_char, sizeof(char) * |
623 | (share_alter->tmp_tgt_default_groups_charlen + 1), |
624 | &tmp_static_link_ids_char, sizeof(char) * |
625 | (share_alter->tmp_static_link_ids_charlen + 1), |
626 | NullS)) |
627 | ) { |
628 | error_num = HA_ERR_OUT_OF_MEM; |
629 | goto error_alloc_alter_table; |
630 | } |
631 | alter_table->now_create = now_create; |
632 | alter_table->table_name = tmp_name; |
633 | memcpy(alter_table->table_name, share->table_name, share->table_name_length); |
634 | alter_table->table_name_length = share->table_name_length; |
635 | #ifdef SPIDER_HAS_HASH_VALUE_TYPE |
636 | DBUG_PRINT("info" ,("spider table_name_hash_value=%u" , |
637 | share->table_name_hash_value)); |
638 | alter_table->table_name_hash_value = share->table_name_hash_value; |
639 | #endif |
640 | alter_table->tmp_priority = share->priority; |
641 | alter_table->link_count = share->link_count; |
642 | alter_table->all_link_count = share->all_link_count; |
643 | |
644 | alter_table->tmp_server_names = tmp_server_names; |
645 | alter_table->tmp_tgt_table_names = tmp_tgt_table_names; |
646 | alter_table->tmp_tgt_dbs = tmp_tgt_dbs; |
647 | alter_table->tmp_tgt_hosts = tmp_tgt_hosts; |
648 | alter_table->tmp_tgt_usernames = tmp_tgt_usernames; |
649 | alter_table->tmp_tgt_passwords = tmp_tgt_passwords; |
650 | alter_table->tmp_tgt_sockets = tmp_tgt_sockets; |
651 | alter_table->tmp_tgt_wrappers = tmp_tgt_wrappers; |
652 | alter_table->tmp_tgt_ssl_cas = tmp_tgt_ssl_cas; |
653 | alter_table->tmp_tgt_ssl_capaths = tmp_tgt_ssl_capaths; |
654 | alter_table->tmp_tgt_ssl_certs = tmp_tgt_ssl_certs; |
655 | alter_table->tmp_tgt_ssl_ciphers = tmp_tgt_ssl_ciphers; |
656 | alter_table->tmp_tgt_ssl_keys = tmp_tgt_ssl_keys; |
657 | alter_table->tmp_tgt_default_files = tmp_tgt_default_files; |
658 | alter_table->tmp_tgt_default_groups = tmp_tgt_default_groups; |
659 | alter_table->tmp_static_link_ids = tmp_static_link_ids; |
660 | |
661 | alter_table->tmp_tgt_ports = tmp_tgt_ports; |
662 | alter_table->tmp_tgt_ssl_vscs = tmp_tgt_ssl_vscs; |
663 | alter_table->tmp_monitoring_binlog_pos_at_failing = |
664 | tmp_monitoring_binlog_pos_at_failing; |
665 | alter_table->tmp_link_statuses = tmp_link_statuses; |
666 | |
667 | alter_table->tmp_server_names_lengths = tmp_server_names_lengths; |
668 | alter_table->tmp_tgt_table_names_lengths = tmp_tgt_table_names_lengths; |
669 | alter_table->tmp_tgt_dbs_lengths = tmp_tgt_dbs_lengths; |
670 | alter_table->tmp_tgt_hosts_lengths = tmp_tgt_hosts_lengths; |
671 | alter_table->tmp_tgt_usernames_lengths = tmp_tgt_usernames_lengths; |
672 | alter_table->tmp_tgt_passwords_lengths = tmp_tgt_passwords_lengths; |
673 | alter_table->tmp_tgt_sockets_lengths = tmp_tgt_sockets_lengths; |
674 | alter_table->tmp_tgt_wrappers_lengths = tmp_tgt_wrappers_lengths; |
675 | alter_table->tmp_tgt_ssl_cas_lengths = tmp_tgt_ssl_cas_lengths; |
676 | alter_table->tmp_tgt_ssl_capaths_lengths = tmp_tgt_ssl_capaths_lengths; |
677 | alter_table->tmp_tgt_ssl_certs_lengths = tmp_tgt_ssl_certs_lengths; |
678 | alter_table->tmp_tgt_ssl_ciphers_lengths = tmp_tgt_ssl_ciphers_lengths; |
679 | alter_table->tmp_tgt_ssl_keys_lengths = tmp_tgt_ssl_keys_lengths; |
680 | alter_table->tmp_tgt_default_files_lengths = tmp_tgt_default_files_lengths; |
681 | alter_table->tmp_tgt_default_groups_lengths = tmp_tgt_default_groups_lengths; |
682 | alter_table->tmp_static_link_ids_lengths = tmp_static_link_ids_lengths; |
683 | |
684 | for(roop_count = 0; roop_count < (int) share->all_link_count; roop_count++) |
685 | { |
686 | tmp_server_names[roop_count] = tmp_server_names_char; |
687 | memcpy(tmp_server_names_char, |
688 | share_alter->tmp_server_names[roop_count], |
689 | sizeof(char) * share_alter->tmp_server_names_lengths[roop_count]); |
690 | tmp_server_names_char += |
691 | share_alter->tmp_server_names_lengths[roop_count] + 1; |
692 | |
693 | tmp_tgt_table_names[roop_count] = tmp_tgt_table_names_char; |
694 | memcpy(tmp_tgt_table_names_char, |
695 | share_alter->tmp_tgt_table_names[roop_count], |
696 | sizeof(char) * share_alter->tmp_tgt_table_names_lengths[roop_count]); |
697 | tmp_tgt_table_names_char += |
698 | share_alter->tmp_tgt_table_names_lengths[roop_count] + 1; |
699 | |
700 | tmp_tgt_dbs[roop_count] = tmp_tgt_dbs_char; |
701 | memcpy(tmp_tgt_dbs_char, share_alter->tmp_tgt_dbs[roop_count], |
702 | sizeof(char) * share_alter->tmp_tgt_dbs_lengths[roop_count]); |
703 | tmp_tgt_dbs_char += |
704 | share_alter->tmp_tgt_dbs_lengths[roop_count] + 1; |
705 | |
706 | tmp_tgt_hosts[roop_count] = tmp_tgt_hosts_char; |
707 | memcpy(tmp_tgt_hosts_char, share_alter->tmp_tgt_hosts[roop_count], |
708 | sizeof(char) * share_alter->tmp_tgt_hosts_lengths[roop_count]); |
709 | tmp_tgt_hosts_char += |
710 | share_alter->tmp_tgt_hosts_lengths[roop_count] + 1; |
711 | |
712 | tmp_tgt_usernames[roop_count] = tmp_tgt_usernames_char; |
713 | memcpy(tmp_tgt_usernames_char, share_alter->tmp_tgt_usernames[roop_count], |
714 | sizeof(char) * share_alter->tmp_tgt_usernames_lengths[roop_count]); |
715 | tmp_tgt_usernames_char += |
716 | share_alter->tmp_tgt_usernames_lengths[roop_count] + 1; |
717 | |
718 | tmp_tgt_passwords[roop_count] = tmp_tgt_passwords_char; |
719 | memcpy(tmp_tgt_passwords_char, share_alter->tmp_tgt_passwords[roop_count], |
720 | sizeof(char) * share_alter->tmp_tgt_passwords_lengths[roop_count]); |
721 | tmp_tgt_passwords_char += |
722 | share_alter->tmp_tgt_passwords_lengths[roop_count] + 1; |
723 | |
724 | tmp_tgt_sockets[roop_count] = tmp_tgt_sockets_char; |
725 | memcpy(tmp_tgt_sockets_char, share_alter->tmp_tgt_sockets[roop_count], |
726 | sizeof(char) * share_alter->tmp_tgt_sockets_lengths[roop_count]); |
727 | tmp_tgt_sockets_char += |
728 | share_alter->tmp_tgt_sockets_lengths[roop_count] + 1; |
729 | |
730 | tmp_tgt_wrappers[roop_count] = tmp_tgt_wrappers_char; |
731 | memcpy(tmp_tgt_wrappers_char, share_alter->tmp_tgt_wrappers[roop_count], |
732 | sizeof(char) * share_alter->tmp_tgt_wrappers_lengths[roop_count]); |
733 | tmp_tgt_wrappers_char += |
734 | share_alter->tmp_tgt_wrappers_lengths[roop_count] + 1; |
735 | |
736 | tmp_tgt_ssl_cas[roop_count] = tmp_tgt_ssl_cas_char; |
737 | memcpy(tmp_tgt_ssl_cas_char, share_alter->tmp_tgt_ssl_cas[roop_count], |
738 | sizeof(char) * share_alter->tmp_tgt_ssl_cas_lengths[roop_count]); |
739 | tmp_tgt_ssl_cas_char += |
740 | share_alter->tmp_tgt_ssl_cas_lengths[roop_count] + 1; |
741 | |
742 | tmp_tgt_ssl_capaths[roop_count] = tmp_tgt_ssl_capaths_char; |
743 | memcpy(tmp_tgt_ssl_capaths_char, |
744 | share_alter->tmp_tgt_ssl_capaths[roop_count], |
745 | sizeof(char) * share_alter->tmp_tgt_ssl_capaths_lengths[roop_count]); |
746 | tmp_tgt_ssl_capaths_char += |
747 | share_alter->tmp_tgt_ssl_capaths_lengths[roop_count] + 1; |
748 | |
749 | tmp_tgt_ssl_certs[roop_count] = tmp_tgt_ssl_certs_char; |
750 | memcpy(tmp_tgt_ssl_certs_char, share_alter->tmp_tgt_ssl_certs[roop_count], |
751 | sizeof(char) * share_alter->tmp_tgt_ssl_certs_lengths[roop_count]); |
752 | tmp_tgt_ssl_certs_char += |
753 | share_alter->tmp_tgt_ssl_certs_lengths[roop_count] + 1; |
754 | |
755 | tmp_tgt_ssl_ciphers[roop_count] = tmp_tgt_ssl_ciphers_char; |
756 | memcpy(tmp_tgt_ssl_ciphers_char, |
757 | share_alter->tmp_tgt_ssl_ciphers[roop_count], |
758 | sizeof(char) * share_alter->tmp_tgt_ssl_ciphers_lengths[roop_count]); |
759 | tmp_tgt_ssl_ciphers_char += |
760 | share_alter->tmp_tgt_ssl_ciphers_lengths[roop_count] + 1; |
761 | |
762 | tmp_tgt_ssl_keys[roop_count] = tmp_tgt_ssl_keys_char; |
763 | memcpy(tmp_tgt_ssl_keys_char, share_alter->tmp_tgt_ssl_keys[roop_count], |
764 | sizeof(char) * share_alter->tmp_tgt_ssl_keys_lengths[roop_count]); |
765 | tmp_tgt_ssl_keys_char += |
766 | share_alter->tmp_tgt_ssl_keys_lengths[roop_count] + 1; |
767 | |
768 | tmp_tgt_default_files[roop_count] = tmp_tgt_default_files_char; |
769 | memcpy(tmp_tgt_default_files_char, |
770 | share_alter->tmp_tgt_default_files[roop_count], |
771 | sizeof(char) * share_alter->tmp_tgt_default_files_lengths[roop_count]); |
772 | tmp_tgt_default_files_char += |
773 | share_alter->tmp_tgt_default_files_lengths[roop_count] + 1; |
774 | |
775 | tmp_tgt_default_groups[roop_count] = tmp_tgt_default_groups_char; |
776 | memcpy(tmp_tgt_default_groups_char, |
777 | share_alter->tmp_tgt_default_groups[roop_count], |
778 | sizeof(char) * share_alter->tmp_tgt_default_groups_lengths[roop_count]); |
779 | tmp_tgt_default_groups_char += |
780 | share_alter->tmp_tgt_default_groups_lengths[roop_count] + 1; |
781 | |
782 | if (share_alter->tmp_static_link_ids[roop_count]) |
783 | { |
784 | tmp_static_link_ids[roop_count] = tmp_static_link_ids_char; |
785 | memcpy(tmp_static_link_ids_char, |
786 | share_alter->tmp_static_link_ids[roop_count], |
787 | sizeof(char) * share_alter->tmp_static_link_ids_lengths[roop_count]); |
788 | tmp_static_link_ids_char += |
789 | share_alter->tmp_static_link_ids_lengths[roop_count] + 1; |
790 | } |
791 | } |
792 | |
793 | memcpy(tmp_tgt_ports, share_alter->tmp_tgt_ports, |
794 | sizeof(long) * share->all_link_count); |
795 | memcpy(tmp_tgt_ssl_vscs, share_alter->tmp_tgt_ssl_vscs, |
796 | sizeof(long) * share->all_link_count); |
797 | memcpy(tmp_monitoring_binlog_pos_at_failing, |
798 | share_alter->tmp_monitoring_binlog_pos_at_failing, |
799 | sizeof(long) * share->all_link_count); |
800 | memcpy(tmp_link_statuses, share_alter->tmp_link_statuses, |
801 | sizeof(long) * share->all_link_count); |
802 | |
803 | memcpy(tmp_server_names_lengths, share_alter->tmp_server_names_lengths, |
804 | sizeof(uint) * share->all_link_count); |
805 | memcpy(tmp_tgt_table_names_lengths, share_alter->tmp_tgt_table_names_lengths, |
806 | sizeof(uint) * share->all_link_count); |
807 | memcpy(tmp_tgt_dbs_lengths, share_alter->tmp_tgt_dbs_lengths, |
808 | sizeof(uint) * share->all_link_count); |
809 | memcpy(tmp_tgt_hosts_lengths, share_alter->tmp_tgt_hosts_lengths, |
810 | sizeof(uint) * share->all_link_count); |
811 | memcpy(tmp_tgt_usernames_lengths, share_alter->tmp_tgt_usernames_lengths, |
812 | sizeof(uint) * share->all_link_count); |
813 | memcpy(tmp_tgt_passwords_lengths, share_alter->tmp_tgt_passwords_lengths, |
814 | sizeof(uint) * share->all_link_count); |
815 | memcpy(tmp_tgt_sockets_lengths, share_alter->tmp_tgt_sockets_lengths, |
816 | sizeof(uint) * share->all_link_count); |
817 | memcpy(tmp_tgt_wrappers_lengths, share_alter->tmp_tgt_wrappers_lengths, |
818 | sizeof(uint) * share->all_link_count); |
819 | memcpy(tmp_tgt_ssl_cas_lengths, share_alter->tmp_tgt_ssl_cas_lengths, |
820 | sizeof(uint) * share->all_link_count); |
821 | memcpy(tmp_tgt_ssl_capaths_lengths, share_alter->tmp_tgt_ssl_capaths_lengths, |
822 | sizeof(uint) * share->all_link_count); |
823 | memcpy(tmp_tgt_ssl_certs_lengths, share_alter->tmp_tgt_ssl_certs_lengths, |
824 | sizeof(uint) * share->all_link_count); |
825 | memcpy(tmp_tgt_ssl_ciphers_lengths, share_alter->tmp_tgt_ssl_ciphers_lengths, |
826 | sizeof(uint) * share->all_link_count); |
827 | memcpy(tmp_tgt_ssl_keys_lengths, share_alter->tmp_tgt_ssl_keys_lengths, |
828 | sizeof(uint) * share->all_link_count); |
829 | memcpy(tmp_tgt_default_files_lengths, |
830 | share_alter->tmp_tgt_default_files_lengths, |
831 | sizeof(uint) * share->all_link_count); |
832 | memcpy(tmp_tgt_default_groups_lengths, |
833 | share_alter->tmp_tgt_default_groups_lengths, |
834 | sizeof(uint) * share->all_link_count); |
835 | memcpy(tmp_static_link_ids_lengths, |
836 | share_alter->tmp_static_link_ids_lengths, |
837 | sizeof(uint) * share->all_link_count); |
838 | |
839 | alter_table->tmp_server_names_length = |
840 | share_alter->tmp_server_names_length; |
841 | alter_table->tmp_tgt_table_names_length = |
842 | share_alter->tmp_tgt_table_names_length; |
843 | alter_table->tmp_tgt_dbs_length = |
844 | share_alter->tmp_tgt_dbs_length; |
845 | alter_table->tmp_tgt_hosts_length = |
846 | share_alter->tmp_tgt_hosts_length; |
847 | alter_table->tmp_tgt_usernames_length = |
848 | share_alter->tmp_tgt_usernames_length; |
849 | alter_table->tmp_tgt_passwords_length = |
850 | share_alter->tmp_tgt_passwords_length; |
851 | alter_table->tmp_tgt_sockets_length = |
852 | share_alter->tmp_tgt_sockets_length; |
853 | alter_table->tmp_tgt_wrappers_length = |
854 | share_alter->tmp_tgt_wrappers_length; |
855 | alter_table->tmp_tgt_ssl_cas_length = |
856 | share_alter->tmp_tgt_ssl_cas_length; |
857 | alter_table->tmp_tgt_ssl_capaths_length = |
858 | share_alter->tmp_tgt_ssl_capaths_length; |
859 | alter_table->tmp_tgt_ssl_certs_length = |
860 | share_alter->tmp_tgt_ssl_certs_length; |
861 | alter_table->tmp_tgt_ssl_ciphers_length = |
862 | share_alter->tmp_tgt_ssl_ciphers_length; |
863 | alter_table->tmp_tgt_ssl_keys_length = |
864 | share_alter->tmp_tgt_ssl_keys_length; |
865 | alter_table->tmp_tgt_default_files_length = |
866 | share_alter->tmp_tgt_default_files_length; |
867 | alter_table->tmp_tgt_default_groups_length = |
868 | share_alter->tmp_tgt_default_groups_length; |
869 | alter_table->tmp_static_link_ids_length = |
870 | share_alter->tmp_static_link_ids_length; |
871 | alter_table->tmp_tgt_ports_length = |
872 | share_alter->tmp_tgt_ports_length; |
873 | alter_table->tmp_tgt_ssl_vscs_length = |
874 | share_alter->tmp_tgt_ssl_vscs_length; |
875 | alter_table->tmp_monitoring_binlog_pos_at_failing_length = |
876 | share_alter->tmp_monitoring_binlog_pos_at_failing_length; |
877 | alter_table->tmp_link_statuses_length = |
878 | share_alter->tmp_link_statuses_length; |
879 | |
880 | old_elements = trx->trx_alter_table_hash.array.max_element; |
881 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
882 | if (my_hash_insert_with_hash_value(&trx->trx_alter_table_hash, |
883 | alter_table->table_name_hash_value, (uchar*) alter_table)) |
884 | #else |
885 | if (my_hash_insert(&trx->trx_alter_table_hash, (uchar*) alter_table)) |
886 | #endif |
887 | { |
888 | error_num = HA_ERR_OUT_OF_MEM; |
889 | goto error; |
890 | } |
891 | if (trx->trx_alter_table_hash.array.max_element > old_elements) |
892 | { |
893 | spider_alloc_calc_mem(spider_current_trx, |
894 | trx->trx_alter_table_hash, |
895 | (trx->trx_alter_table_hash.array.max_element - old_elements) * |
896 | trx->trx_alter_table_hash.array.size_of_element); |
897 | } |
898 | DBUG_RETURN(0); |
899 | |
900 | error: |
901 | spider_free(trx, alter_table, MYF(0)); |
902 | error_alloc_alter_table: |
903 | DBUG_RETURN(error_num); |
904 | } |
905 | |
906 | bool spider_cmp_trx_alter_table( |
907 | SPIDER_ALTER_TABLE *cmp1, |
908 | SPIDER_ALTER_TABLE *cmp2 |
909 | ) { |
910 | int roop_count; |
911 | DBUG_ENTER("spider_cmp_trx_alter_table" ); |
912 | if ( |
913 | cmp1->tmp_priority != cmp2->tmp_priority || |
914 | cmp1->link_count != cmp2->link_count || |
915 | cmp1->all_link_count != cmp2->all_link_count |
916 | ) |
917 | DBUG_RETURN(TRUE); |
918 | |
919 | for (roop_count = 0; roop_count < (int) cmp1->all_link_count; roop_count++) |
920 | { |
921 | if ( |
922 | ( |
923 | cmp1->tmp_server_names[roop_count] != |
924 | cmp2->tmp_server_names[roop_count] && |
925 | ( |
926 | !cmp1->tmp_server_names[roop_count] || |
927 | !cmp2->tmp_server_names[roop_count] || |
928 | strcmp(cmp1->tmp_server_names[roop_count], |
929 | cmp2->tmp_server_names[roop_count]) |
930 | ) |
931 | ) || |
932 | ( |
933 | cmp1->tmp_tgt_table_names[roop_count] != |
934 | cmp2->tmp_tgt_table_names[roop_count] && |
935 | ( |
936 | !cmp1->tmp_tgt_table_names[roop_count] || |
937 | !cmp2->tmp_tgt_table_names[roop_count] || |
938 | strcmp(cmp1->tmp_tgt_table_names[roop_count], |
939 | cmp2->tmp_tgt_table_names[roop_count]) |
940 | ) |
941 | ) || |
942 | ( |
943 | cmp1->tmp_tgt_dbs[roop_count] != |
944 | cmp2->tmp_tgt_dbs[roop_count] && |
945 | ( |
946 | !cmp1->tmp_tgt_dbs[roop_count] || |
947 | !cmp2->tmp_tgt_dbs[roop_count] || |
948 | strcmp(cmp1->tmp_tgt_dbs[roop_count], |
949 | cmp2->tmp_tgt_dbs[roop_count]) |
950 | ) |
951 | ) || |
952 | ( |
953 | cmp1->tmp_tgt_hosts[roop_count] != |
954 | cmp2->tmp_tgt_hosts[roop_count] && |
955 | ( |
956 | !cmp1->tmp_tgt_hosts[roop_count] || |
957 | !cmp2->tmp_tgt_hosts[roop_count] || |
958 | strcmp(cmp1->tmp_tgt_hosts[roop_count], |
959 | cmp2->tmp_tgt_hosts[roop_count]) |
960 | ) |
961 | ) || |
962 | ( |
963 | cmp1->tmp_tgt_usernames[roop_count] != |
964 | cmp2->tmp_tgt_usernames[roop_count] && |
965 | ( |
966 | !cmp1->tmp_tgt_usernames[roop_count] || |
967 | !cmp2->tmp_tgt_usernames[roop_count] || |
968 | strcmp(cmp1->tmp_tgt_usernames[roop_count], |
969 | cmp2->tmp_tgt_usernames[roop_count]) |
970 | ) |
971 | ) || |
972 | ( |
973 | cmp1->tmp_tgt_passwords[roop_count] != |
974 | cmp2->tmp_tgt_passwords[roop_count] && |
975 | ( |
976 | !cmp1->tmp_tgt_passwords[roop_count] || |
977 | !cmp2->tmp_tgt_passwords[roop_count] || |
978 | strcmp(cmp1->tmp_tgt_passwords[roop_count], |
979 | cmp2->tmp_tgt_passwords[roop_count]) |
980 | ) |
981 | ) || |
982 | ( |
983 | cmp1->tmp_tgt_sockets[roop_count] != |
984 | cmp2->tmp_tgt_sockets[roop_count] && |
985 | ( |
986 | !cmp1->tmp_tgt_sockets[roop_count] || |
987 | !cmp2->tmp_tgt_sockets[roop_count] || |
988 | strcmp(cmp1->tmp_tgt_sockets[roop_count], |
989 | cmp2->tmp_tgt_sockets[roop_count]) |
990 | ) |
991 | ) || |
992 | ( |
993 | cmp1->tmp_tgt_wrappers[roop_count] != |
994 | cmp2->tmp_tgt_wrappers[roop_count] && |
995 | ( |
996 | !cmp1->tmp_tgt_wrappers[roop_count] || |
997 | !cmp2->tmp_tgt_wrappers[roop_count] || |
998 | strcmp(cmp1->tmp_tgt_wrappers[roop_count], |
999 | cmp2->tmp_tgt_wrappers[roop_count]) |
1000 | ) |
1001 | ) || |
1002 | ( |
1003 | cmp1->tmp_tgt_ssl_cas[roop_count] != |
1004 | cmp2->tmp_tgt_ssl_cas[roop_count] && |
1005 | ( |
1006 | !cmp1->tmp_tgt_ssl_cas[roop_count] || |
1007 | !cmp2->tmp_tgt_ssl_cas[roop_count] || |
1008 | strcmp(cmp1->tmp_tgt_ssl_cas[roop_count], |
1009 | cmp2->tmp_tgt_ssl_cas[roop_count]) |
1010 | ) |
1011 | ) || |
1012 | ( |
1013 | cmp1->tmp_tgt_ssl_capaths[roop_count] != |
1014 | cmp2->tmp_tgt_ssl_capaths[roop_count] && |
1015 | ( |
1016 | !cmp1->tmp_tgt_ssl_capaths[roop_count] || |
1017 | !cmp2->tmp_tgt_ssl_capaths[roop_count] || |
1018 | strcmp(cmp1->tmp_tgt_ssl_capaths[roop_count], |
1019 | cmp2->tmp_tgt_ssl_capaths[roop_count]) |
1020 | ) |
1021 | ) || |
1022 | ( |
1023 | cmp1->tmp_tgt_ssl_certs[roop_count] != |
1024 | cmp2->tmp_tgt_ssl_certs[roop_count] && |
1025 | ( |
1026 | !cmp1->tmp_tgt_ssl_certs[roop_count] || |
1027 | !cmp2->tmp_tgt_ssl_certs[roop_count] || |
1028 | strcmp(cmp1->tmp_tgt_ssl_certs[roop_count], |
1029 | cmp2->tmp_tgt_ssl_certs[roop_count]) |
1030 | ) |
1031 | ) || |
1032 | ( |
1033 | cmp1->tmp_tgt_ssl_ciphers[roop_count] != |
1034 | cmp2->tmp_tgt_ssl_ciphers[roop_count] && |
1035 | ( |
1036 | !cmp1->tmp_tgt_ssl_ciphers[roop_count] || |
1037 | !cmp2->tmp_tgt_ssl_ciphers[roop_count] || |
1038 | strcmp(cmp1->tmp_tgt_ssl_ciphers[roop_count], |
1039 | cmp2->tmp_tgt_ssl_ciphers[roop_count]) |
1040 | ) |
1041 | ) || |
1042 | ( |
1043 | cmp1->tmp_tgt_ssl_keys[roop_count] != |
1044 | cmp2->tmp_tgt_ssl_keys[roop_count] && |
1045 | ( |
1046 | !cmp1->tmp_tgt_ssl_keys[roop_count] || |
1047 | !cmp2->tmp_tgt_ssl_keys[roop_count] || |
1048 | strcmp(cmp1->tmp_tgt_ssl_keys[roop_count], |
1049 | cmp2->tmp_tgt_ssl_keys[roop_count]) |
1050 | ) |
1051 | ) || |
1052 | ( |
1053 | cmp1->tmp_tgt_default_files[roop_count] != |
1054 | cmp2->tmp_tgt_default_files[roop_count] && |
1055 | ( |
1056 | !cmp1->tmp_tgt_default_files[roop_count] || |
1057 | !cmp2->tmp_tgt_default_files[roop_count] || |
1058 | strcmp(cmp1->tmp_tgt_default_files[roop_count], |
1059 | cmp2->tmp_tgt_default_files[roop_count]) |
1060 | ) |
1061 | ) || |
1062 | ( |
1063 | cmp1->tmp_tgt_default_groups[roop_count] != |
1064 | cmp2->tmp_tgt_default_groups[roop_count] && |
1065 | ( |
1066 | !cmp1->tmp_tgt_default_groups[roop_count] || |
1067 | !cmp2->tmp_tgt_default_groups[roop_count] || |
1068 | strcmp(cmp1->tmp_tgt_default_groups[roop_count], |
1069 | cmp2->tmp_tgt_default_groups[roop_count]) |
1070 | ) |
1071 | ) || |
1072 | ( |
1073 | cmp1->tmp_static_link_ids[roop_count] != |
1074 | cmp2->tmp_static_link_ids[roop_count] && |
1075 | ( |
1076 | !cmp1->tmp_static_link_ids[roop_count] || |
1077 | !cmp2->tmp_static_link_ids[roop_count] || |
1078 | strcmp(cmp1->tmp_static_link_ids[roop_count], |
1079 | cmp2->tmp_static_link_ids[roop_count]) |
1080 | ) |
1081 | ) || |
1082 | cmp1->tmp_tgt_ports[roop_count] != cmp2->tmp_tgt_ports[roop_count] || |
1083 | cmp1->tmp_tgt_ssl_vscs[roop_count] != |
1084 | cmp2->tmp_tgt_ssl_vscs[roop_count] || |
1085 | cmp1->tmp_monitoring_binlog_pos_at_failing[roop_count] != |
1086 | cmp2->tmp_monitoring_binlog_pos_at_failing[roop_count] || |
1087 | cmp1->tmp_link_statuses[roop_count] != |
1088 | cmp2->tmp_link_statuses[roop_count] |
1089 | ) |
1090 | DBUG_RETURN(TRUE); |
1091 | } |
1092 | DBUG_RETURN(FALSE); |
1093 | } |
1094 | |
1095 | int spider_free_trx_alloc( |
1096 | SPIDER_TRX *trx |
1097 | ) { |
1098 | int roop_count; |
1099 | DBUG_ENTER("spider_free_trx_alloc" ); |
1100 | if (trx->tmp_spider) |
1101 | { |
1102 | for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; ++roop_count) |
1103 | { |
1104 | if (trx->tmp_spider->dbton_handler[roop_count]) |
1105 | { |
1106 | delete trx->tmp_spider->dbton_handler[roop_count]; |
1107 | trx->tmp_spider->dbton_handler[roop_count] = NULL; |
1108 | } |
1109 | } |
1110 | if (trx->tmp_spider->result_list.sqls) |
1111 | { |
1112 | delete [] trx->tmp_spider->result_list.sqls; |
1113 | trx->tmp_spider->result_list.sqls = NULL; |
1114 | } |
1115 | delete trx->tmp_spider; |
1116 | trx->tmp_spider = NULL; |
1117 | } |
1118 | if (trx->tmp_share) |
1119 | { |
1120 | for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; ++roop_count) |
1121 | { |
1122 | if (trx->tmp_share->dbton_share[roop_count]) |
1123 | { |
1124 | delete trx->tmp_share->dbton_share[roop_count]; |
1125 | trx->tmp_share->dbton_share[roop_count] = NULL; |
1126 | } |
1127 | } |
1128 | spider_free_tmp_share_alloc(trx->tmp_share); |
1129 | } |
1130 | spider_db_udf_free_set_names(trx); |
1131 | for (roop_count = spider_param_udf_table_lock_mutex_count() - 1; |
1132 | roop_count >= 0; roop_count--) |
1133 | pthread_mutex_destroy(&trx->udf_table_mutexes[roop_count]); |
1134 | spider_free_trx_ha(trx); |
1135 | spider_free_trx_conn(trx, TRUE); |
1136 | spider_free_trx_alter_table(trx); |
1137 | spider_free_mem_calc(spider_current_trx, |
1138 | trx->trx_conn_hash_id, |
1139 | trx->trx_conn_hash.array.max_element * |
1140 | trx->trx_conn_hash.array.size_of_element); |
1141 | my_hash_free(&trx->trx_conn_hash); |
1142 | spider_free_mem_calc(spider_current_trx, |
1143 | trx->trx_another_conn_hash_id, |
1144 | trx->trx_another_conn_hash.array.max_element * |
1145 | trx->trx_another_conn_hash.array.size_of_element); |
1146 | my_hash_free(&trx->trx_another_conn_hash); |
1147 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1148 | spider_free_mem_calc(spider_current_trx, |
1149 | trx->trx_direct_hs_r_conn_hash_id, |
1150 | trx->trx_direct_hs_r_conn_hash.array.max_element * |
1151 | trx->trx_direct_hs_r_conn_hash.array.size_of_element); |
1152 | my_hash_free(&trx->trx_direct_hs_r_conn_hash); |
1153 | spider_free_mem_calc(spider_current_trx, |
1154 | trx->trx_direct_hs_w_conn_hash_id, |
1155 | trx->trx_direct_hs_w_conn_hash.array.max_element * |
1156 | trx->trx_direct_hs_w_conn_hash.array.size_of_element); |
1157 | my_hash_free(&trx->trx_direct_hs_w_conn_hash); |
1158 | #endif |
1159 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1160 | spider_free_mem_calc(spider_current_trx, |
1161 | trx->trx_hs_r_conn_hash_id, |
1162 | trx->trx_hs_r_conn_hash.array.max_element * |
1163 | trx->trx_hs_r_conn_hash.array.size_of_element); |
1164 | my_hash_free(&trx->trx_hs_r_conn_hash); |
1165 | spider_free_mem_calc(spider_current_trx, |
1166 | trx->trx_hs_w_conn_hash_id, |
1167 | trx->trx_hs_w_conn_hash.array.max_element * |
1168 | trx->trx_hs_w_conn_hash.array.size_of_element); |
1169 | my_hash_free(&trx->trx_hs_w_conn_hash); |
1170 | #endif |
1171 | spider_free_mem_calc(spider_current_trx, |
1172 | trx->trx_ha_hash_id, |
1173 | trx->trx_ha_hash.array.max_element * |
1174 | trx->trx_ha_hash.array.size_of_element); |
1175 | my_hash_free(&trx->trx_ha_hash); |
1176 | spider_free_mem_calc(spider_current_trx, |
1177 | trx->trx_alter_table_hash_id, |
1178 | trx->trx_alter_table_hash.array.max_element * |
1179 | trx->trx_alter_table_hash.array.size_of_element); |
1180 | my_hash_free(&trx->trx_alter_table_hash); |
1181 | free_root(&trx->mem_root, MYF(0)); |
1182 | DBUG_RETURN(0); |
1183 | } |
1184 | |
1185 | SPIDER_TRX *spider_get_trx( |
1186 | THD *thd, |
1187 | bool regist_allocated_thds, |
1188 | int *error_num |
1189 | ) { |
1190 | int roop_count = 0, roop_count2; |
1191 | SPIDER_TRX *trx; |
1192 | SPIDER_SHARE *tmp_share; |
1193 | pthread_mutex_t *udf_table_mutexes; |
1194 | DBUG_ENTER("spider_get_trx" ); |
1195 | |
1196 | if ( |
1197 | !thd || |
1198 | !(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)) |
1199 | ) { |
1200 | DBUG_PRINT("info" ,("spider create new trx" )); |
1201 | if (!(trx = (SPIDER_TRX *) |
1202 | spider_bulk_malloc(NULL, 56, MYF(MY_WME | MY_ZEROFILL), |
1203 | &trx, sizeof(*trx), |
1204 | &tmp_share, sizeof(SPIDER_SHARE), |
1205 | &udf_table_mutexes, sizeof(pthread_mutex_t) * |
1206 | spider_param_udf_table_lock_mutex_count(), |
1207 | NullS)) |
1208 | ) |
1209 | goto error_alloc_trx; |
1210 | |
1211 | SPD_INIT_ALLOC_ROOT(&trx->mem_root, 4096, 0, MYF(MY_WME)); |
1212 | trx->tmp_share = tmp_share; |
1213 | trx->udf_table_mutexes = udf_table_mutexes; |
1214 | |
1215 | for (roop_count = 0; |
1216 | roop_count < (int) spider_param_udf_table_lock_mutex_count(); |
1217 | roop_count++) |
1218 | { |
1219 | #if MYSQL_VERSION_ID < 50500 |
1220 | if (pthread_mutex_init(&trx->udf_table_mutexes[roop_count], |
1221 | MY_MUTEX_INIT_FAST)) |
1222 | #else |
1223 | if (mysql_mutex_init(spd_key_mutex_udf_table, |
1224 | &trx->udf_table_mutexes[roop_count], MY_MUTEX_INIT_FAST)) |
1225 | #endif |
1226 | goto error_init_udf_table_mutex; |
1227 | } |
1228 | |
1229 | if ( |
1230 | my_hash_init(&trx->trx_conn_hash, spd_charset_utf8_bin, 32, 0, 0, |
1231 | (my_hash_get_key) spider_conn_get_key, 0, 0) |
1232 | ) |
1233 | goto error_init_hash; |
1234 | spider_alloc_calc_mem_init(trx->trx_conn_hash, 151); |
1235 | spider_alloc_calc_mem( |
1236 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1237 | trx->trx_conn_hash, |
1238 | trx->trx_conn_hash.array.max_element * |
1239 | trx->trx_conn_hash.array.size_of_element); |
1240 | |
1241 | if ( |
1242 | my_hash_init(&trx->trx_another_conn_hash, spd_charset_utf8_bin, 32, 0, 0, |
1243 | (my_hash_get_key) spider_conn_get_key, 0, 0) |
1244 | ) |
1245 | goto error_init_another_hash; |
1246 | spider_alloc_calc_mem_init(trx->trx_another_conn_hash, 152); |
1247 | spider_alloc_calc_mem( |
1248 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1249 | trx->trx_another_conn_hash, |
1250 | trx->trx_another_conn_hash.array.max_element * |
1251 | trx->trx_another_conn_hash.array.size_of_element); |
1252 | |
1253 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1254 | if ( |
1255 | my_hash_init(&trx->trx_hs_r_conn_hash, spd_charset_utf8_bin, 32, 0, 0, |
1256 | (my_hash_get_key) spider_conn_get_key, 0, 0) |
1257 | ) |
1258 | goto error_hs_r_init_hash; |
1259 | spider_alloc_calc_mem_init(trx->trx_hs_r_conn_hash, 153); |
1260 | spider_alloc_calc_mem( |
1261 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1262 | trx->trx_hs_r_conn_hash, |
1263 | trx->trx_hs_r_conn_hash.array.max_element * |
1264 | trx->trx_hs_r_conn_hash.array.size_of_element); |
1265 | |
1266 | if ( |
1267 | my_hash_init(&trx->trx_hs_w_conn_hash, spd_charset_utf8_bin, 32, 0, 0, |
1268 | (my_hash_get_key) spider_conn_get_key, 0, 0) |
1269 | ) |
1270 | goto error_hs_w_init_hash; |
1271 | spider_alloc_calc_mem_init(trx->trx_hs_w_conn_hash, 154); |
1272 | spider_alloc_calc_mem( |
1273 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1274 | trx->trx_hs_w_conn_hash, |
1275 | trx->trx_hs_w_conn_hash.array.max_element * |
1276 | trx->trx_hs_w_conn_hash.array.size_of_element); |
1277 | #endif |
1278 | |
1279 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1280 | if ( |
1281 | my_hash_init(&trx->trx_direct_hs_r_conn_hash, spd_charset_utf8_bin, 32, |
1282 | 0, 0, (my_hash_get_key) spider_conn_get_key, 0, 0) |
1283 | ) |
1284 | goto error_direct_hs_r_init_hash; |
1285 | spider_alloc_calc_mem_init(trx->trx_direct_hs_r_conn_hash, 155); |
1286 | spider_alloc_calc_mem( |
1287 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1288 | trx->trx_direct_hs_r_conn_hash, |
1289 | trx->trx_direct_hs_r_conn_hash.array.max_element * |
1290 | trx->trx_direct_hs_r_conn_hash.array.size_of_element); |
1291 | |
1292 | if ( |
1293 | my_hash_init(&trx->trx_direct_hs_w_conn_hash, spd_charset_utf8_bin, 32, |
1294 | 0, 0, (my_hash_get_key) spider_conn_get_key, 0, 0) |
1295 | ) |
1296 | goto error_direct_hs_w_init_hash; |
1297 | spider_alloc_calc_mem_init(trx->trx_direct_hs_w_conn_hash, 156); |
1298 | spider_alloc_calc_mem( |
1299 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1300 | trx->trx_direct_hs_w_conn_hash, |
1301 | trx->trx_direct_hs_w_conn_hash.array.max_element * |
1302 | trx->trx_direct_hs_w_conn_hash.array.size_of_element); |
1303 | #endif |
1304 | |
1305 | if ( |
1306 | my_hash_init(&trx->trx_alter_table_hash, spd_charset_utf8_bin, 32, 0, 0, |
1307 | (my_hash_get_key) spider_alter_tbl_get_key, 0, 0) |
1308 | ) |
1309 | goto error_init_alter_hash; |
1310 | spider_alloc_calc_mem_init(trx->trx_alter_table_hash, 157); |
1311 | spider_alloc_calc_mem( |
1312 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1313 | trx->trx_alter_table_hash, |
1314 | trx->trx_alter_table_hash.array.max_element * |
1315 | trx->trx_alter_table_hash.array.size_of_element); |
1316 | |
1317 | if ( |
1318 | my_hash_init(&trx->trx_ha_hash, spd_charset_utf8_bin, 32, 0, 0, |
1319 | (my_hash_get_key) spider_trx_ha_get_key, 0, 0) |
1320 | ) |
1321 | goto error_init_trx_ha_hash; |
1322 | spider_alloc_calc_mem_init(trx->trx_ha_hash, 158); |
1323 | spider_alloc_calc_mem( |
1324 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1325 | trx->trx_ha_hash, |
1326 | trx->trx_ha_hash.array.max_element * |
1327 | trx->trx_ha_hash.array.size_of_element); |
1328 | |
1329 | trx->thd = (THD*) thd; |
1330 | #ifdef SPIDER_HAS_HASH_VALUE_TYPE |
1331 | if (thd) |
1332 | trx->thd_hash_value = my_calc_hash(&spider_allocated_thds, |
1333 | (uchar*) thd, sizeof(THD *)); |
1334 | else |
1335 | trx->thd_hash_value = 0; |
1336 | #endif |
1337 | pthread_mutex_lock(&spider_thread_id_mutex); |
1338 | trx->spider_thread_id = spider_thread_id; |
1339 | ++spider_thread_id; |
1340 | pthread_mutex_unlock(&spider_thread_id_mutex); |
1341 | trx->trx_conn_adjustment = 1; |
1342 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1343 | trx->trx_hs_r_conn_adjustment = 1; |
1344 | trx->trx_hs_w_conn_adjustment = 1; |
1345 | #endif |
1346 | |
1347 | if (thd) |
1348 | { |
1349 | spider_set_tmp_share_pointer(trx->tmp_share, trx->tmp_connect_info, |
1350 | trx->tmp_connect_info_length, trx->tmp_long, trx->tmp_longlong); |
1351 | if ( |
1352 | spider_set_connect_info_default( |
1353 | trx->tmp_share, |
1354 | #ifdef WITH_PARTITION_STORAGE_ENGINE |
1355 | NULL, |
1356 | NULL, |
1357 | #endif |
1358 | NULL |
1359 | ) || |
1360 | spider_set_connect_info_default_db_table( |
1361 | trx->tmp_share, |
1362 | "" , 0, |
1363 | "" , 0 |
1364 | ) || |
1365 | spider_create_conn_keys(trx->tmp_share) |
1366 | ) { |
1367 | goto error_set_connect_info_default; |
1368 | } |
1369 | |
1370 | if (!(trx->tmp_spider = new (&trx->mem_root) ha_spider())) |
1371 | { |
1372 | goto error_alloc_spider; |
1373 | } |
1374 | trx->tmp_spider->need_mons = &trx->tmp_need_mon; |
1375 | trx->tmp_spider->share = trx->tmp_share; |
1376 | trx->tmp_spider->trx = trx; |
1377 | trx->tmp_spider->dbton_handler = trx->tmp_dbton_handler; |
1378 | if (!(trx->tmp_spider->result_list.sqls = |
1379 | new spider_string[trx->tmp_share->link_count])) |
1380 | { |
1381 | goto error_init_result_list_sql; |
1382 | } |
1383 | for (roop_count2 = 0; roop_count2 < (int) trx->tmp_share->link_count; |
1384 | ++roop_count2) |
1385 | { |
1386 | trx->tmp_spider->result_list.sqls[roop_count2].init_calc_mem(121); |
1387 | trx->tmp_spider->result_list.sqls[roop_count2].set_charset( |
1388 | trx->tmp_share->access_charset); |
1389 | } |
1390 | |
1391 | for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; ++roop_count2) |
1392 | { |
1393 | if (!spider_dbton[roop_count2].init) |
1394 | continue; |
1395 | |
1396 | if (!(trx->tmp_share->dbton_share[roop_count2] = |
1397 | spider_dbton[roop_count2].create_db_share(trx->tmp_share))) |
1398 | { |
1399 | goto error_create_db_share; |
1400 | } |
1401 | if (trx->tmp_share->dbton_share[roop_count2]->init()) |
1402 | { |
1403 | delete trx->tmp_share->dbton_share[roop_count2]; |
1404 | trx->tmp_share->dbton_share[roop_count2] = NULL; |
1405 | goto error_create_db_share; |
1406 | } |
1407 | |
1408 | if (!(trx->tmp_spider->dbton_handler[roop_count2] = |
1409 | spider_dbton[roop_count2].create_db_handler(trx->tmp_spider, |
1410 | trx->tmp_share->dbton_share[roop_count2]))) |
1411 | { |
1412 | goto error_create_db_share; |
1413 | } |
1414 | if (trx->tmp_spider->dbton_handler[roop_count2]->init()) |
1415 | { |
1416 | delete trx->tmp_spider->dbton_handler[roop_count2]; |
1417 | trx->tmp_spider->dbton_handler[roop_count2] = NULL; |
1418 | goto error_create_db_share; |
1419 | } |
1420 | } |
1421 | |
1422 | if (regist_allocated_thds) |
1423 | { |
1424 | pthread_mutex_lock(&spider_allocated_thds_mutex); |
1425 | uint old_elements = spider_allocated_thds.array.max_element; |
1426 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
1427 | if (my_hash_insert_with_hash_value(&spider_allocated_thds, |
1428 | trx->thd_hash_value, (uchar*) thd)) |
1429 | #else |
1430 | if (my_hash_insert(&spider_allocated_thds, (uchar*) thd)) |
1431 | #endif |
1432 | { |
1433 | pthread_mutex_unlock(&spider_allocated_thds_mutex); |
1434 | goto error_allocated_thds_insert; |
1435 | } |
1436 | if (spider_allocated_thds.array.max_element > old_elements) |
1437 | { |
1438 | spider_alloc_calc_mem(trx, |
1439 | spider_allocated_thds, |
1440 | (spider_allocated_thds.array.max_element - old_elements) * |
1441 | spider_allocated_thds.array.size_of_element); |
1442 | } |
1443 | pthread_mutex_unlock(&spider_allocated_thds_mutex); |
1444 | trx->registed_allocated_thds = TRUE; |
1445 | } |
1446 | thd_set_ha_data(thd, spider_hton_ptr, trx); |
1447 | } |
1448 | } |
1449 | |
1450 | DBUG_PRINT("info" ,("spider trx=%p" , trx)); |
1451 | DBUG_RETURN(trx); |
1452 | |
1453 | error_allocated_thds_insert: |
1454 | error_alloc_spider: |
1455 | error_create_db_share: |
1456 | if (thd) |
1457 | { |
1458 | delete [] trx->tmp_spider->result_list.sqls; |
1459 | trx->tmp_spider->result_list.sqls = NULL; |
1460 | } |
1461 | error_init_result_list_sql: |
1462 | if (thd) |
1463 | { |
1464 | delete trx->tmp_spider; |
1465 | trx->tmp_spider = NULL; |
1466 | for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; ++roop_count2) |
1467 | { |
1468 | if (trx->tmp_spider->dbton_handler[roop_count2]) |
1469 | { |
1470 | delete trx->tmp_spider->dbton_handler[roop_count2]; |
1471 | trx->tmp_spider->dbton_handler[roop_count2] = NULL; |
1472 | } |
1473 | if (trx->tmp_share->dbton_share[roop_count2]) |
1474 | { |
1475 | delete trx->tmp_share->dbton_share[roop_count2]; |
1476 | trx->tmp_share->dbton_share[roop_count2] = NULL; |
1477 | } |
1478 | } |
1479 | } |
1480 | error_set_connect_info_default: |
1481 | if (thd) |
1482 | { |
1483 | spider_free_tmp_share_alloc(trx->tmp_share); |
1484 | } |
1485 | spider_free_mem_calc(trx, |
1486 | trx->trx_ha_hash_id, |
1487 | trx->trx_ha_hash.array.max_element * |
1488 | trx->trx_ha_hash.array.size_of_element); |
1489 | my_hash_free(&trx->trx_ha_hash); |
1490 | error_init_trx_ha_hash: |
1491 | spider_free_mem_calc( |
1492 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1493 | trx->trx_alter_table_hash_id, |
1494 | trx->trx_alter_table_hash.array.max_element * |
1495 | trx->trx_alter_table_hash.array.size_of_element); |
1496 | my_hash_free(&trx->trx_alter_table_hash); |
1497 | error_init_alter_hash: |
1498 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1499 | spider_free_mem_calc( |
1500 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1501 | trx->trx_direct_hs_w_conn_hash_id, |
1502 | trx->trx_direct_hs_w_conn_hash.array.max_element * |
1503 | trx->trx_direct_hs_w_conn_hash.array.size_of_element); |
1504 | my_hash_free(&trx->trx_direct_hs_w_conn_hash); |
1505 | error_direct_hs_w_init_hash: |
1506 | spider_free_mem_calc( |
1507 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1508 | trx->trx_direct_hs_r_conn_hash_id, |
1509 | trx->trx_direct_hs_r_conn_hash.array.max_element * |
1510 | trx->trx_direct_hs_r_conn_hash.array.size_of_element); |
1511 | my_hash_free(&trx->trx_direct_hs_r_conn_hash); |
1512 | error_direct_hs_r_init_hash: |
1513 | #endif |
1514 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
1515 | spider_free_mem_calc( |
1516 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1517 | trx->trx_hs_w_conn_hash_id, |
1518 | trx->trx_hs_w_conn_hash.array.max_element * |
1519 | trx->trx_hs_w_conn_hash.array.size_of_element); |
1520 | my_hash_free(&trx->trx_hs_w_conn_hash); |
1521 | error_hs_w_init_hash: |
1522 | spider_free_mem_calc( |
1523 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1524 | trx->trx_hs_r_conn_hash_id, |
1525 | trx->trx_hs_r_conn_hash.array.max_element * |
1526 | trx->trx_hs_r_conn_hash.array.size_of_element); |
1527 | my_hash_free(&trx->trx_hs_r_conn_hash); |
1528 | error_hs_r_init_hash: |
1529 | #endif |
1530 | spider_free_mem_calc( |
1531 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1532 | trx->trx_another_conn_hash_id, |
1533 | trx->trx_another_conn_hash.array.max_element * |
1534 | trx->trx_another_conn_hash.array.size_of_element); |
1535 | my_hash_free(&trx->trx_another_conn_hash); |
1536 | error_init_another_hash: |
1537 | spider_free_mem_calc( |
1538 | thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL, |
1539 | trx->trx_conn_hash_id, |
1540 | trx->trx_conn_hash.array.max_element * |
1541 | trx->trx_conn_hash.array.size_of_element); |
1542 | my_hash_free(&trx->trx_conn_hash); |
1543 | error_init_hash: |
1544 | if (roop_count > 0) |
1545 | { |
1546 | for (roop_count--; roop_count >= 0; roop_count--) |
1547 | pthread_mutex_destroy(&trx->udf_table_mutexes[roop_count]); |
1548 | } |
1549 | error_init_udf_table_mutex: |
1550 | free_root(&trx->mem_root, MYF(0)); |
1551 | spider_free(NULL, trx, MYF(0)); |
1552 | error_alloc_trx: |
1553 | *error_num = HA_ERR_OUT_OF_MEM; |
1554 | DBUG_RETURN(NULL); |
1555 | } |
1556 | |
1557 | int spider_free_trx( |
1558 | SPIDER_TRX *trx, |
1559 | bool need_lock |
1560 | ) { |
1561 | DBUG_ENTER("spider_free_trx" ); |
1562 | if (trx->thd) |
1563 | { |
1564 | if (trx->registed_allocated_thds) |
1565 | { |
1566 | if (need_lock) |
1567 | pthread_mutex_lock(&spider_allocated_thds_mutex); |
1568 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
1569 | my_hash_delete_with_hash_value(&spider_allocated_thds, |
1570 | trx->thd_hash_value, (uchar*) trx->thd); |
1571 | #else |
1572 | my_hash_delete(&spider_allocated_thds, (uchar*) trx->thd); |
1573 | #endif |
1574 | if (need_lock) |
1575 | pthread_mutex_unlock(&spider_allocated_thds_mutex); |
1576 | } |
1577 | thd_set_ha_data(trx->thd, spider_hton_ptr, NULL); |
1578 | } |
1579 | spider_free_trx_alloc(trx); |
1580 | spider_merge_mem_calc(trx, TRUE); |
1581 | spider_free(NULL, trx, MYF(0)); |
1582 | DBUG_RETURN(0); |
1583 | } |
1584 | |
1585 | int spider_check_and_set_trx_isolation( |
1586 | SPIDER_CONN *conn, |
1587 | int *need_mon |
1588 | ) { |
1589 | int trx_isolation; |
1590 | DBUG_ENTER("spider_check_and_set_trx_isolation" ); |
1591 | |
1592 | trx_isolation = thd_tx_isolation(conn->thd); |
1593 | DBUG_PRINT("info" ,("spider local trx_isolation=%d" , trx_isolation)); |
1594 | /* |
1595 | DBUG_PRINT("info",("spider conn->trx_isolation=%d", conn->trx_isolation)); |
1596 | if (conn->trx_isolation != trx_isolation) |
1597 | { |
1598 | */ |
1599 | spider_conn_queue_trx_isolation(conn, trx_isolation); |
1600 | /* |
1601 | conn->trx_isolation = trx_isolation; |
1602 | } |
1603 | */ |
1604 | DBUG_RETURN(0); |
1605 | } |
1606 | |
1607 | int spider_check_and_set_autocommit( |
1608 | THD *thd, |
1609 | SPIDER_CONN *conn, |
1610 | int *need_mon |
1611 | ) { |
1612 | bool autocommit; |
1613 | DBUG_ENTER("spider_check_and_set_autocommit" ); |
1614 | |
1615 | autocommit = !thd_test_options(thd, OPTION_NOT_AUTOCOMMIT); |
1616 | if (autocommit) |
1617 | { |
1618 | spider_conn_queue_autocommit(conn, TRUE); |
1619 | } else { |
1620 | spider_conn_queue_autocommit(conn, FALSE); |
1621 | } |
1622 | /* |
1623 | if (autocommit && conn->autocommit != 1) |
1624 | { |
1625 | spider_conn_queue_autocommit(conn, TRUE); |
1626 | conn->autocommit = 1; |
1627 | } else if (!autocommit && conn->autocommit != 0) |
1628 | { |
1629 | spider_conn_queue_autocommit(conn, FALSE); |
1630 | conn->autocommit = 0; |
1631 | } |
1632 | */ |
1633 | DBUG_RETURN(0); |
1634 | } |
1635 | |
1636 | int spider_check_and_set_sql_log_off( |
1637 | THD *thd, |
1638 | SPIDER_CONN *conn, |
1639 | int *need_mon |
1640 | ) { |
1641 | int internal_sql_log_off; |
1642 | DBUG_ENTER("spider_check_and_set_sql_log_off" ); |
1643 | |
1644 | internal_sql_log_off = spider_param_internal_sql_log_off(thd); |
1645 | if (internal_sql_log_off != -1) |
1646 | { |
1647 | if (internal_sql_log_off) |
1648 | { |
1649 | spider_conn_queue_sql_log_off(conn, TRUE); |
1650 | } |
1651 | else |
1652 | { |
1653 | spider_conn_queue_sql_log_off(conn, FALSE); |
1654 | } |
1655 | } |
1656 | /* |
1657 | if (internal_sql_log_off && conn->sql_log_off != 1) |
1658 | { |
1659 | spider_conn_queue_sql_log_off(conn, TRUE); |
1660 | conn->sql_log_off = 1; |
1661 | } else if (!internal_sql_log_off && conn->sql_log_off != 0) |
1662 | { |
1663 | spider_conn_queue_sql_log_off(conn, FALSE); |
1664 | conn->sql_log_off = 0; |
1665 | } |
1666 | */ |
1667 | DBUG_RETURN(0); |
1668 | } |
1669 | |
1670 | int spider_check_and_set_time_zone( |
1671 | THD *thd, |
1672 | SPIDER_CONN *conn, |
1673 | int *need_mon |
1674 | ) { |
1675 | Time_zone *time_zone; |
1676 | DBUG_ENTER("spider_check_and_set_time_zone" ); |
1677 | |
1678 | time_zone = thd->variables.time_zone; |
1679 | DBUG_PRINT("info" ,("spider local time_zone=%p" , time_zone)); |
1680 | /* |
1681 | DBUG_PRINT("info",("spider conn->time_zone=%p", conn->time_zone)); |
1682 | if (time_zone != conn->time_zone) |
1683 | { |
1684 | */ |
1685 | spider_conn_queue_time_zone(conn, time_zone); |
1686 | /* |
1687 | conn->time_zone = time_zone; |
1688 | } |
1689 | */ |
1690 | DBUG_RETURN(0); |
1691 | } |
1692 | |
1693 | int spider_xa_lock( |
1694 | XID_STATE *xid_state |
1695 | ) { |
1696 | THD *thd = current_thd; |
1697 | int error_num; |
1698 | const char *old_proc_info; |
1699 | DBUG_ENTER("spider_xa_lock" ); |
1700 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
1701 | #else |
1702 | #ifdef SPIDER_HAS_HASH_VALUE_TYPE |
1703 | my_hash_value_type hash_value = my_calc_hash(spd_db_att_xid_cache, |
1704 | (uchar*) xid_state->xid.key(), xid_state->xid.key_length()); |
1705 | #ifdef XID_CACHE_IS_SPLITTED |
1706 | uint idx = hash_value % *spd_db_att_xid_cache_split_num; |
1707 | #endif |
1708 | #endif |
1709 | #endif |
1710 | old_proc_info = thd_proc_info(thd, "Locking xid by Spider" ); |
1711 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
1712 | if (xid_cache_insert(thd, xid_state)) |
1713 | { |
1714 | error_num = (spider_stmt_da_sql_errno(thd) == ER_XAER_DUPID ? |
1715 | ER_SPIDER_XA_LOCKED_NUM : HA_ERR_OUT_OF_MEM); |
1716 | goto error; |
1717 | } |
1718 | #else |
1719 | #ifdef XID_CACHE_IS_SPLITTED |
1720 | pthread_mutex_lock(&spd_db_att_LOCK_xid_cache[idx]); |
1721 | #else |
1722 | pthread_mutex_lock(spd_db_att_LOCK_xid_cache); |
1723 | #endif |
1724 | #ifdef SPIDER_HAS_HASH_VALUE_TYPE |
1725 | #ifdef XID_CACHE_IS_SPLITTED |
1726 | if (my_hash_search_using_hash_value(&spd_db_att_xid_cache[idx], hash_value, |
1727 | xid_state->xid.key(), xid_state->xid.key_length())) |
1728 | #else |
1729 | if (my_hash_search_using_hash_value(spd_db_att_xid_cache, hash_value, |
1730 | xid_state->xid.key(), xid_state->xid.key_length())) |
1731 | #endif |
1732 | #else |
1733 | if (my_hash_search(spd_db_att_xid_cache, |
1734 | xid_state->xid.key(), xid_state->xid.key_length())) |
1735 | #endif |
1736 | { |
1737 | error_num = ER_SPIDER_XA_LOCKED_NUM; |
1738 | goto error; |
1739 | } |
1740 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
1741 | #ifdef XID_CACHE_IS_SPLITTED |
1742 | if (my_hash_insert_with_hash_value(&spd_db_att_xid_cache[idx], hash_value, |
1743 | (uchar*)xid_state)) |
1744 | #else |
1745 | if (my_hash_insert_with_hash_value(spd_db_att_xid_cache, hash_value, |
1746 | (uchar*)xid_state)) |
1747 | #endif |
1748 | #else |
1749 | if (my_hash_insert(spd_db_att_xid_cache, (uchar*)xid_state)) |
1750 | #endif |
1751 | { |
1752 | error_num = HA_ERR_OUT_OF_MEM; |
1753 | goto error; |
1754 | } |
1755 | #ifdef XID_CACHE_IS_SPLITTED |
1756 | pthread_mutex_unlock(&spd_db_att_LOCK_xid_cache[idx]); |
1757 | #else |
1758 | pthread_mutex_unlock(spd_db_att_LOCK_xid_cache); |
1759 | #endif |
1760 | #endif |
1761 | thd_proc_info(thd, old_proc_info); |
1762 | DBUG_RETURN(0); |
1763 | |
1764 | error: |
1765 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
1766 | #else |
1767 | #ifdef XID_CACHE_IS_SPLITTED |
1768 | pthread_mutex_unlock(&spd_db_att_LOCK_xid_cache[idx]); |
1769 | #else |
1770 | pthread_mutex_unlock(spd_db_att_LOCK_xid_cache); |
1771 | #endif |
1772 | #endif |
1773 | thd_proc_info(thd, old_proc_info); |
1774 | DBUG_RETURN(error_num); |
1775 | } |
1776 | |
1777 | int spider_xa_unlock( |
1778 | XID_STATE *xid_state |
1779 | ) { |
1780 | THD *thd = current_thd; |
1781 | const char *old_proc_info; |
1782 | DBUG_ENTER("spider_xa_unlock" ); |
1783 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
1784 | #else |
1785 | #if defined(SPIDER_HAS_HASH_VALUE_TYPE) && defined(HASH_UPDATE_WITH_HASH_VALUE) |
1786 | my_hash_value_type hash_value = my_calc_hash(spd_db_att_xid_cache, |
1787 | (uchar*) xid_state->xid.key(), xid_state->xid.key_length()); |
1788 | #ifdef XID_CACHE_IS_SPLITTED |
1789 | uint idx = hash_value % *spd_db_att_xid_cache_split_num; |
1790 | #endif |
1791 | #endif |
1792 | #endif |
1793 | old_proc_info = thd_proc_info(thd, "Unlocking xid by Spider" ); |
1794 | #ifdef SPIDER_XID_USES_xid_cache_iterate |
1795 | xid_cache_delete(thd, xid_state); |
1796 | #else |
1797 | #ifdef XID_CACHE_IS_SPLITTED |
1798 | pthread_mutex_lock(&spd_db_att_LOCK_xid_cache[idx]); |
1799 | #else |
1800 | pthread_mutex_lock(spd_db_att_LOCK_xid_cache); |
1801 | #endif |
1802 | #if defined(SPIDER_HAS_HASH_VALUE_TYPE) && defined(HASH_UPDATE_WITH_HASH_VALUE) |
1803 | #ifdef XID_CACHE_IS_SPLITTED |
1804 | my_hash_delete_with_hash_value(&spd_db_att_xid_cache[idx], |
1805 | hash_value, (uchar *)xid_state); |
1806 | #else |
1807 | my_hash_delete_with_hash_value(spd_db_att_xid_cache, |
1808 | hash_value, (uchar *)xid_state); |
1809 | #endif |
1810 | #else |
1811 | my_hash_delete(spd_db_att_xid_cache, (uchar *)xid_state); |
1812 | #endif |
1813 | #ifdef XID_CACHE_IS_SPLITTED |
1814 | pthread_mutex_unlock(&spd_db_att_LOCK_xid_cache[idx]); |
1815 | #else |
1816 | pthread_mutex_unlock(spd_db_att_LOCK_xid_cache); |
1817 | #endif |
1818 | #endif |
1819 | thd_proc_info(thd, old_proc_info); |
1820 | DBUG_RETURN(0); |
1821 | } |
1822 | |
1823 | int spider_start_internal_consistent_snapshot( |
1824 | SPIDER_TRX *trx, |
1825 | SPIDER_CONN *conn, |
1826 | int *need_mon |
1827 | ) { |
1828 | DBUG_ENTER("spider_start_internal_consistent_snapshot" ); |
1829 | if (trx->trx_consistent_snapshot) |
1830 | DBUG_RETURN(spider_db_consistent_snapshot(conn, need_mon)); |
1831 | DBUG_RETURN(0); |
1832 | } |
1833 | |
1834 | int spider_internal_start_trx( |
1835 | ha_spider *spider, |
1836 | SPIDER_CONN *conn, |
1837 | int link_idx |
1838 | ) { |
1839 | int error_num; |
1840 | SPIDER_TRX *trx = spider->trx; |
1841 | THD *thd = trx->thd; |
1842 | bool sync_autocommit = spider_param_sync_autocommit(thd); |
1843 | bool sync_time_zone = spider_param_sync_time_zone(thd); |
1844 | double ping_interval_at_trx_start = |
1845 | spider_param_ping_interval_at_trx_start(thd); |
1846 | bool xa_lock = FALSE; |
1847 | time_t tmp_time = (time_t) time((time_t*) 0); |
1848 | DBUG_ENTER("spider_internal_start_trx" ); |
1849 | |
1850 | if ( |
1851 | conn->server_lost || |
1852 | difftime(tmp_time, conn->ping_time) >= ping_interval_at_trx_start |
1853 | ) { |
1854 | spider_conn_queue_ping(spider, conn, link_idx); |
1855 | } |
1856 | conn->disable_reconnect = TRUE; |
1857 | if (!trx->trx_start) |
1858 | { |
1859 | if (!trx->trx_consistent_snapshot) |
1860 | { |
1861 | trx->use_consistent_snapshot = |
1862 | spider_param_use_consistent_snapshot(thd); |
1863 | trx->internal_xa = spider_param_internal_xa(thd); |
1864 | trx->internal_xa_snapshot = spider_param_internal_xa_snapshot(thd); |
1865 | } |
1866 | } |
1867 | if ( |
1868 | (error_num = spider_check_and_set_sql_log_off(thd, conn, |
1869 | &spider->need_mons[link_idx])) || |
1870 | (sync_time_zone && |
1871 | (error_num = spider_check_and_set_time_zone(thd, conn, |
1872 | &spider->need_mons[link_idx]))) || |
1873 | (sync_autocommit && |
1874 | (error_num = spider_check_and_set_autocommit(thd, conn, |
1875 | &spider->need_mons[link_idx]))) |
1876 | ) |
1877 | goto error; |
1878 | if (trx->trx_consistent_snapshot) |
1879 | { |
1880 | if (trx->internal_xa && trx->internal_xa_snapshot < 2) |
1881 | { |
1882 | error_num = ER_SPIDER_CANT_USE_BOTH_INNER_XA_AND_SNAPSHOT_NUM; |
1883 | my_message(error_num, ER_SPIDER_CANT_USE_BOTH_INNER_XA_AND_SNAPSHOT_STR, |
1884 | MYF(0)); |
1885 | goto error; |
1886 | } else if (!trx->internal_xa || trx->internal_xa_snapshot == 2) |
1887 | { |
1888 | if ((error_num = spider_start_internal_consistent_snapshot(trx, conn, |
1889 | &spider->need_mons[link_idx]))) |
1890 | goto error; |
1891 | } |
1892 | } |
1893 | DBUG_PRINT("info" ,("spider trx->trx_start= %s" , |
1894 | trx->trx_start ? "TRUE" : "FALSE" )); |
1895 | if (!trx->trx_start) |
1896 | { |
1897 | if ( |
1898 | thd->transaction.xid_state.xa_state == XA_ACTIVE && |
1899 | spider_param_support_xa() |
1900 | ) { |
1901 | trx->trx_xa = TRUE; |
1902 | thd_get_xid(thd, (MYSQL_XID*) &trx->xid); |
1903 | } |
1904 | |
1905 | if ( |
1906 | !trx->trx_xa && |
1907 | trx->internal_xa && |
1908 | (!trx->trx_consistent_snapshot || trx->internal_xa_snapshot == 3) && |
1909 | spider->sql_command != SQLCOM_LOCK_TABLES |
1910 | ) { |
1911 | trx->trx_xa = TRUE; |
1912 | trx->xid.formatID = 1; |
1913 | if (spider_param_internal_xa_id_type(thd) == 0) |
1914 | { |
1915 | trx->xid.gtrid_length |
1916 | = my_sprintf(trx->xid.data, |
1917 | (trx->xid.data, "%lx" , thd_get_thread_id(thd))); |
1918 | } else { |
1919 | trx->xid.gtrid_length |
1920 | = my_sprintf(trx->xid.data, |
1921 | (trx->xid.data, "%lx%016llx" , thd_get_thread_id(thd), |
1922 | thd->query_id)); |
1923 | } |
1924 | #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100002 |
1925 | trx->xid.bqual_length |
1926 | = my_sprintf(trx->xid.data + trx->xid.gtrid_length, |
1927 | (trx->xid.data + trx->xid.gtrid_length, "%lx" , |
1928 | thd->variables.server_id)); |
1929 | #else |
1930 | trx->xid.bqual_length |
1931 | = my_sprintf(trx->xid.data + trx->xid.gtrid_length, |
1932 | (trx->xid.data + trx->xid.gtrid_length, "%x" , |
1933 | thd->server_id)); |
1934 | #endif |
1935 | |
1936 | trx->internal_xid_state.xa_state = XA_ACTIVE; |
1937 | trx->internal_xid_state.xid.set(&trx->xid); |
1938 | #ifdef SPIDER_XID_STATE_HAS_in_thd |
1939 | trx->internal_xid_state.in_thd = 1; |
1940 | #endif |
1941 | if ((error_num = spider_xa_lock(&trx->internal_xid_state))) |
1942 | { |
1943 | if (error_num == ER_SPIDER_XA_LOCKED_NUM) |
1944 | my_message(error_num, ER_SPIDER_XA_LOCKED_STR, MYF(0)); |
1945 | goto error; |
1946 | } |
1947 | xa_lock = TRUE; |
1948 | } else |
1949 | trx->internal_xa = FALSE; |
1950 | |
1951 | DBUG_PRINT("info" ,("spider trx->trx_consistent_snapshot= %s" , |
1952 | trx->trx_consistent_snapshot ? "TRUE" : "FALSE" )); |
1953 | if (!trx->trx_consistent_snapshot) |
1954 | { |
1955 | trans_register_ha(thd, FALSE, spider_hton_ptr); |
1956 | if (thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) |
1957 | trans_register_ha(thd, TRUE, spider_hton_ptr); |
1958 | } |
1959 | trx->trx_start = TRUE; |
1960 | trx->trx_xa_prepared = FALSE; |
1961 | trx->updated_in_this_trx = FALSE; |
1962 | DBUG_PRINT("info" ,("spider trx->updated_in_this_trx=FALSE" )); |
1963 | } |
1964 | |
1965 | DBUG_PRINT("info" ,("spider sync_autocommit = %d" , sync_autocommit)); |
1966 | DBUG_PRINT("info" ,("spider conn->semi_trx_chk = %d" , conn->semi_trx_chk)); |
1967 | DBUG_PRINT("info" ,("spider conn->table_lock = %d" , conn->table_lock)); |
1968 | DBUG_PRINT("info" ,("spider conn->autocommit = %d" , conn->autocommit)); |
1969 | DBUG_PRINT("info" ,("spider semi_trx = %d" , spider_param_semi_trx(thd))); |
1970 | conn->semi_trx = FALSE; |
1971 | if (conn->table_lock == 3) |
1972 | { |
1973 | DBUG_PRINT("info" ,("spider conn->table_lock == 3" )); |
1974 | conn->disable_xa = TRUE; |
1975 | } else if (trx->trx_xa) |
1976 | { |
1977 | DBUG_PRINT("info" ,("spider trx->trx_xa" )); |
1978 | if ( |
1979 | sync_autocommit && |
1980 | conn->semi_trx_chk && |
1981 | !conn->table_lock && |
1982 | ( |
1983 | (!conn->queued_autocommit && conn->autocommit == 1) || |
1984 | (conn->queued_autocommit && conn->queued_autocommit_val == TRUE) |
1985 | ) && |
1986 | spider_param_semi_trx(thd) |
1987 | ) { |
1988 | DBUG_PRINT("info" ,("spider semi_trx is set" )); |
1989 | conn->semi_trx = TRUE; |
1990 | } |
1991 | spider_conn_queue_xa_start(conn, &trx->xid); |
1992 | conn->disable_xa = FALSE; |
1993 | } else if ( |
1994 | !trx->trx_consistent_snapshot && |
1995 | !thd_test_options(thd, OPTION_BEGIN) && |
1996 | sync_autocommit && |
1997 | conn->semi_trx_chk && |
1998 | !conn->table_lock && |
1999 | ( |
2000 | (!conn->queued_autocommit && conn->autocommit == 1) || |
2001 | (conn->queued_autocommit && conn->queued_autocommit_val == TRUE) |
2002 | ) && |
2003 | spider_param_semi_trx(thd) |
2004 | ) { |
2005 | DBUG_PRINT("info" ,("spider semi_trx is set" )); |
2006 | spider_conn_queue_start_transaction(conn); |
2007 | conn->semi_trx = TRUE; |
2008 | } else if ( |
2009 | !trx->trx_consistent_snapshot && |
2010 | thd_test_options(thd, OPTION_BEGIN) |
2011 | ) { |
2012 | DBUG_PRINT("info" ,("spider start transaction" )); |
2013 | spider_conn_queue_start_transaction(conn); |
2014 | } |
2015 | |
2016 | conn->join_trx = 1; |
2017 | if (trx->join_trx_top) |
2018 | spider_tree_insert(trx->join_trx_top, conn); |
2019 | else { |
2020 | conn->p_small = NULL; |
2021 | conn->p_big = NULL; |
2022 | conn->c_small = NULL; |
2023 | conn->c_big = NULL; |
2024 | trx->join_trx_top = conn; |
2025 | } |
2026 | DBUG_RETURN(0); |
2027 | |
2028 | error: |
2029 | if (xa_lock) |
2030 | spider_xa_unlock(&trx->internal_xid_state); |
2031 | DBUG_RETURN(error_num); |
2032 | } |
2033 | |
2034 | int spider_internal_xa_commit( |
2035 | THD* thd, |
2036 | SPIDER_TRX *trx, |
2037 | XID* xid, |
2038 | TABLE *table_xa, |
2039 | TABLE *table_xa_member |
2040 | ) { |
2041 | int error_num = 0, tmp_error_num; |
2042 | char xa_key[MAX_KEY_LENGTH]; |
2043 | SPIDER_CONN *conn; |
2044 | uint force_commit = spider_param_force_commit(thd); |
2045 | MEM_ROOT mem_root; |
2046 | #if MYSQL_VERSION_ID < 50500 |
2047 | Open_tables_state open_tables_backup; |
2048 | #else |
2049 | Open_tables_backup open_tables_backup; |
2050 | #endif |
2051 | bool table_xa_opened = FALSE; |
2052 | bool table_xa_member_opened = FALSE; |
2053 | DBUG_ENTER("spider_internal_xa_commit" ); |
2054 | |
2055 | if (trx->updated_in_this_trx || spider_param_xa_register_mode(thd) == 0) |
2056 | { |
2057 | /* |
2058 | select |
2059 | status |
2060 | from |
2061 | mysql.spider_xa |
2062 | where |
2063 | format_id = xid->format_id and |
2064 | gtrid_length = xid->gtrid_length and |
2065 | data = xid->data |
2066 | */ |
2067 | if ( |
2068 | !(table_xa = spider_open_sys_table( |
2069 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2070 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2071 | ) |
2072 | goto error_open_table; |
2073 | table_xa_opened = TRUE; |
2074 | spider_store_xa_pk(table_xa, &trx->xid); |
2075 | if ( |
2076 | (error_num = spider_check_sys_table(table_xa, xa_key)) |
2077 | ) { |
2078 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
2079 | { |
2080 | table_xa->file->print_error(error_num, MYF(0)); |
2081 | goto error; |
2082 | } |
2083 | my_message(ER_SPIDER_XA_NOT_EXISTS_NUM, ER_SPIDER_XA_NOT_EXISTS_STR, |
2084 | MYF(0)); |
2085 | error_num = ER_SPIDER_XA_NOT_EXISTS_NUM; |
2086 | goto error; |
2087 | } |
2088 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
2089 | if ( |
2090 | force_commit != 2 && |
2091 | (error_num = spider_check_sys_xa_status( |
2092 | table_xa, |
2093 | SPIDER_SYS_XA_PREPARED_STR, |
2094 | SPIDER_SYS_XA_COMMIT_STR, |
2095 | NULL, |
2096 | ER_SPIDER_XA_NOT_PREPARED_NUM, |
2097 | &mem_root)) |
2098 | ) { |
2099 | free_root(&mem_root, MYF(0)); |
2100 | if (error_num == ER_SPIDER_XA_NOT_PREPARED_NUM) |
2101 | my_message(error_num, ER_SPIDER_XA_NOT_PREPARED_STR, MYF(0)); |
2102 | goto error; |
2103 | } |
2104 | free_root(&mem_root, MYF(0)); |
2105 | |
2106 | /* |
2107 | update |
2108 | mysql.spider_xa |
2109 | set |
2110 | status = 'COMMIT' |
2111 | where |
2112 | format_id = trx->xid.format_id and |
2113 | gtrid_length = trx->xid.gtrid_length and |
2114 | data = trx->xid.data |
2115 | */ |
2116 | if ( |
2117 | (error_num = spider_update_xa( |
2118 | table_xa, &trx->xid, SPIDER_SYS_XA_COMMIT_STR)) |
2119 | ) |
2120 | goto error; |
2121 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2122 | table_xa_opened = FALSE; |
2123 | } |
2124 | |
2125 | SPIDER_BACKUP_DASTATUS; |
2126 | if ((conn = spider_tree_first(trx->join_trx_top))) |
2127 | { |
2128 | do { |
2129 | if (conn->bg_search) |
2130 | spider_bg_conn_break(conn, NULL); |
2131 | DBUG_PRINT("info" ,("spider conn=%p" , conn)); |
2132 | DBUG_PRINT("info" ,("spider conn->join_trx=%u" , conn->join_trx)); |
2133 | if (conn->join_trx) |
2134 | { |
2135 | if ((tmp_error_num = spider_db_xa_commit(conn, &trx->xid))) |
2136 | { |
2137 | if (force_commit == 0 || |
2138 | (force_commit == 1 && tmp_error_num != ER_XAER_NOTA)) |
2139 | { |
2140 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2141 | if (!error_num && tmp_error_num) |
2142 | error_num = tmp_error_num; |
2143 | } |
2144 | spider_sys_log_xa_failed(thd, &trx->xid, conn, |
2145 | SPIDER_SYS_XA_COMMIT_STR, TRUE); |
2146 | } |
2147 | if ((tmp_error_num = spider_end_trx(trx, conn))) |
2148 | { |
2149 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2150 | if (!error_num && tmp_error_num) |
2151 | error_num = tmp_error_num; |
2152 | } |
2153 | conn->join_trx = 0; |
2154 | } |
2155 | } while ((conn = spider_tree_next(conn))); |
2156 | trx->join_trx_top = NULL; |
2157 | } |
2158 | if (error_num) |
2159 | goto error_in_commit; |
2160 | |
2161 | if (trx->updated_in_this_trx || spider_param_xa_register_mode(thd) == 0) |
2162 | { |
2163 | /* |
2164 | delete from |
2165 | mysql.spider_xa_member |
2166 | where |
2167 | format_id = xid->format_id and |
2168 | gtrid_length = xid->gtrid_length and |
2169 | data = xid->data |
2170 | */ |
2171 | if ( |
2172 | !(table_xa_member = spider_open_sys_table( |
2173 | thd, SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR, |
2174 | SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE, |
2175 | &error_num)) |
2176 | ) |
2177 | goto error_open_table; |
2178 | table_xa_member_opened = TRUE; |
2179 | if ((error_num = spider_delete_xa_member(table_xa_member, &trx->xid))) |
2180 | goto error; |
2181 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2182 | table_xa_member_opened = FALSE; |
2183 | |
2184 | /* |
2185 | delete from |
2186 | mysql.spider_xa |
2187 | where |
2188 | format_id = xid->format_id and |
2189 | gtrid_length = xid->gtrid_length and |
2190 | data = xid->data |
2191 | */ |
2192 | if ( |
2193 | !(table_xa = spider_open_sys_table( |
2194 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2195 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2196 | ) |
2197 | goto error_open_table; |
2198 | table_xa_opened = TRUE; |
2199 | if ((error_num = spider_delete_xa(table_xa, &trx->xid))) |
2200 | goto error; |
2201 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2202 | table_xa_opened = FALSE; |
2203 | } |
2204 | spider_xa_unlock(&trx->internal_xid_state); |
2205 | trx->internal_xid_state.xa_state = XA_NOTR; |
2206 | DBUG_RETURN(0); |
2207 | |
2208 | error: |
2209 | if (table_xa_opened) |
2210 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2211 | if (table_xa_member_opened) |
2212 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2213 | error_in_commit: |
2214 | error_open_table: |
2215 | spider_xa_unlock(&trx->internal_xid_state); |
2216 | trx->internal_xid_state.xa_state = XA_NOTR; |
2217 | DBUG_RETURN(error_num); |
2218 | } |
2219 | |
2220 | int spider_internal_xa_rollback( |
2221 | THD* thd, |
2222 | SPIDER_TRX *trx |
2223 | ) { |
2224 | int error_num = 0, tmp_error_num; |
2225 | TABLE *table_xa, *table_xa_member; |
2226 | char xa_key[MAX_KEY_LENGTH]; |
2227 | SPIDER_CONN *conn; |
2228 | uint force_commit = spider_param_force_commit(thd); |
2229 | MEM_ROOT mem_root; |
2230 | #if MYSQL_VERSION_ID < 50500 |
2231 | Open_tables_state open_tables_backup; |
2232 | #else |
2233 | Open_tables_backup open_tables_backup; |
2234 | #endif |
2235 | bool server_lost = FALSE; |
2236 | bool table_xa_opened = FALSE; |
2237 | bool table_xa_member_opened = FALSE; |
2238 | DBUG_ENTER("spider_internal_xa_rollback" ); |
2239 | |
2240 | if ( |
2241 | trx->trx_xa_prepared && |
2242 | ( |
2243 | trx->updated_in_this_trx || |
2244 | spider_param_xa_register_mode(thd) == 0 |
2245 | ) |
2246 | ) { |
2247 | /* |
2248 | select |
2249 | status |
2250 | from |
2251 | mysql.spider_xa |
2252 | where |
2253 | format_id = xid->format_id and |
2254 | gtrid_length = xid->gtrid_length and |
2255 | data = xid->data |
2256 | */ |
2257 | if ( |
2258 | !(table_xa = spider_open_sys_table( |
2259 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2260 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2261 | ) |
2262 | goto error_open_table; |
2263 | table_xa_opened = TRUE; |
2264 | spider_store_xa_pk(table_xa, &trx->xid); |
2265 | if ( |
2266 | (error_num = spider_check_sys_table(table_xa, xa_key)) |
2267 | ) { |
2268 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
2269 | { |
2270 | table_xa->file->print_error(error_num, MYF(0)); |
2271 | goto error; |
2272 | } |
2273 | my_message(ER_SPIDER_XA_NOT_EXISTS_NUM, ER_SPIDER_XA_NOT_EXISTS_STR, |
2274 | MYF(0)); |
2275 | error_num = ER_SPIDER_XA_NOT_EXISTS_NUM; |
2276 | goto error; |
2277 | } |
2278 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
2279 | if ( |
2280 | force_commit != 2 && |
2281 | (error_num = spider_check_sys_xa_status( |
2282 | table_xa, |
2283 | SPIDER_SYS_XA_PREPARED_STR, |
2284 | SPIDER_SYS_XA_ROLLBACK_STR, |
2285 | NULL, |
2286 | ER_SPIDER_XA_NOT_PREPARED_NUM, |
2287 | &mem_root)) |
2288 | ) { |
2289 | free_root(&mem_root, MYF(0)); |
2290 | if (error_num == ER_SPIDER_XA_NOT_PREPARED_NUM) |
2291 | my_message(error_num, ER_SPIDER_XA_NOT_PREPARED_STR, MYF(0)); |
2292 | goto error; |
2293 | } |
2294 | free_root(&mem_root, MYF(0)); |
2295 | |
2296 | /* |
2297 | update |
2298 | mysql.spider_xa |
2299 | set |
2300 | status = 'COMMIT' |
2301 | where |
2302 | format_id = trx->xid.format_id and |
2303 | gtrid_length = trx->xid.gtrid_length and |
2304 | data = trx->xid.data |
2305 | */ |
2306 | if ( |
2307 | (error_num = spider_update_xa( |
2308 | table_xa, &trx->xid, SPIDER_SYS_XA_ROLLBACK_STR)) |
2309 | ) |
2310 | goto error; |
2311 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2312 | table_xa_opened = FALSE; |
2313 | } |
2314 | |
2315 | SPIDER_BACKUP_DASTATUS; |
2316 | if ((conn = spider_tree_first(trx->join_trx_top))) |
2317 | { |
2318 | do { |
2319 | if (conn->bg_search) |
2320 | spider_bg_conn_break(conn, NULL); |
2321 | if (conn->join_trx) |
2322 | { |
2323 | if (conn->disable_xa) |
2324 | { |
2325 | if (conn->table_lock != 3 && !trx->trx_xa_prepared) |
2326 | { |
2327 | if ( |
2328 | !conn->server_lost && |
2329 | (tmp_error_num = spider_db_rollback(conn)) |
2330 | ) { |
2331 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2332 | if (!error_num && tmp_error_num) |
2333 | error_num = tmp_error_num; |
2334 | } |
2335 | } |
2336 | } else { |
2337 | if (!conn->server_lost) |
2338 | { |
2339 | if ( |
2340 | !trx->trx_xa_prepared && |
2341 | (tmp_error_num = spider_db_xa_end(conn, &trx->xid)) |
2342 | ) { |
2343 | if ( |
2344 | force_commit == 0 || |
2345 | (force_commit == 1 && |
2346 | ( |
2347 | tmp_error_num != ER_XAER_NOTA && |
2348 | tmp_error_num != ER_XA_RBTIMEOUT && |
2349 | tmp_error_num != ER_XA_RBDEADLOCK |
2350 | ) |
2351 | ) |
2352 | ) { |
2353 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2354 | if (!error_num && tmp_error_num) |
2355 | error_num = tmp_error_num; |
2356 | } |
2357 | } |
2358 | if ((tmp_error_num = spider_db_xa_rollback(conn, &trx->xid))) |
2359 | { |
2360 | if ( |
2361 | force_commit == 0 || |
2362 | (force_commit == 1 && |
2363 | ( |
2364 | tmp_error_num != ER_XAER_NOTA && |
2365 | tmp_error_num != ER_XA_RBTIMEOUT && |
2366 | tmp_error_num != ER_XA_RBDEADLOCK |
2367 | ) |
2368 | ) |
2369 | ) { |
2370 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2371 | if (!error_num && tmp_error_num) |
2372 | error_num = tmp_error_num; |
2373 | } |
2374 | } |
2375 | } |
2376 | } |
2377 | if ((tmp_error_num = spider_end_trx(trx, conn))) |
2378 | { |
2379 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
2380 | if (!error_num && tmp_error_num) |
2381 | error_num = tmp_error_num; |
2382 | } |
2383 | conn->join_trx = 0; |
2384 | if (conn->server_lost) |
2385 | server_lost = TRUE; |
2386 | } |
2387 | } while ((conn = spider_tree_next(conn))); |
2388 | trx->join_trx_top = NULL; |
2389 | } |
2390 | if (error_num) |
2391 | goto error_in_rollback; |
2392 | |
2393 | if ( |
2394 | trx->trx_xa_prepared && |
2395 | !server_lost && |
2396 | ( |
2397 | trx->updated_in_this_trx || |
2398 | spider_param_xa_register_mode(thd) == 0 |
2399 | ) |
2400 | ) { |
2401 | /* |
2402 | delete from |
2403 | mysql.spider_xa_member |
2404 | where |
2405 | format_id = xid->format_id and |
2406 | gtrid_length = xid->gtrid_length and |
2407 | data = xid->data |
2408 | */ |
2409 | if ( |
2410 | !(table_xa_member = spider_open_sys_table( |
2411 | thd, SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR, |
2412 | SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE, |
2413 | &error_num)) |
2414 | ) |
2415 | goto error_open_table; |
2416 | table_xa_member_opened = TRUE; |
2417 | if ((error_num = spider_delete_xa_member(table_xa_member, &trx->xid))) |
2418 | goto error; |
2419 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2420 | table_xa_member_opened = FALSE; |
2421 | |
2422 | /* |
2423 | delete from |
2424 | mysql.spider_xa |
2425 | where |
2426 | format_id = xid->format_id and |
2427 | gtrid_length = xid->gtrid_length and |
2428 | data = xid->data |
2429 | */ |
2430 | if ( |
2431 | !(table_xa = spider_open_sys_table( |
2432 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2433 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2434 | ) |
2435 | goto error_open_table; |
2436 | table_xa_opened = TRUE; |
2437 | if ((error_num = spider_delete_xa(table_xa, &trx->xid))) |
2438 | goto error; |
2439 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2440 | table_xa_opened = FALSE; |
2441 | } |
2442 | spider_xa_unlock(&trx->internal_xid_state); |
2443 | trx->internal_xid_state.xa_state = XA_NOTR; |
2444 | DBUG_RETURN(0); |
2445 | |
2446 | error: |
2447 | if (table_xa_opened) |
2448 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2449 | if (table_xa_member_opened) |
2450 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2451 | error_in_rollback: |
2452 | error_open_table: |
2453 | spider_xa_unlock(&trx->internal_xid_state); |
2454 | trx->internal_xid_state.xa_state = XA_NOTR; |
2455 | DBUG_RETURN(error_num); |
2456 | } |
2457 | |
2458 | int spider_internal_xa_prepare( |
2459 | THD* thd, |
2460 | SPIDER_TRX *trx, |
2461 | TABLE *table_xa, |
2462 | TABLE *table_xa_member, |
2463 | bool internal_xa |
2464 | ) { |
2465 | int error_num; |
2466 | SPIDER_CONN *conn; |
2467 | uint force_commit = spider_param_force_commit(thd); |
2468 | #if MYSQL_VERSION_ID < 50500 |
2469 | Open_tables_state open_tables_backup; |
2470 | #else |
2471 | Open_tables_backup open_tables_backup; |
2472 | #endif |
2473 | bool table_xa_opened = FALSE; |
2474 | bool table_xa_member_opened = FALSE; |
2475 | DBUG_ENTER("spider_internal_xa_prepare" ); |
2476 | if (trx->updated_in_this_trx || spider_param_xa_register_mode(thd) == 0) |
2477 | { |
2478 | /* |
2479 | insert into mysql.spider_xa |
2480 | (format_id, gtrid_length, bqual_length, data, status) values |
2481 | (trx->xid.format_id, trx->xid.gtrid_length, trx->xid.bqual_length, |
2482 | trx->xid.data, 'NOT YET') |
2483 | */ |
2484 | if ( |
2485 | !(table_xa = spider_open_sys_table( |
2486 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2487 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2488 | ) |
2489 | goto error_open_table; |
2490 | table_xa_opened = TRUE; |
2491 | if ( |
2492 | (error_num = spider_insert_xa( |
2493 | table_xa, &trx->xid, SPIDER_SYS_XA_NOT_YET_STR)) |
2494 | ) |
2495 | goto error; |
2496 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2497 | table_xa_opened = FALSE; |
2498 | |
2499 | if ( |
2500 | !(table_xa_member = spider_open_sys_table( |
2501 | thd, SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR, |
2502 | SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE, |
2503 | &error_num)) |
2504 | ) |
2505 | goto error_open_table; |
2506 | table_xa_member_opened = TRUE; |
2507 | } |
2508 | SPIDER_BACKUP_DASTATUS; |
2509 | if ((conn = spider_tree_first(trx->join_trx_top))) |
2510 | { |
2511 | do { |
2512 | if (conn->bg_search) |
2513 | spider_bg_conn_break(conn, NULL); |
2514 | if (conn->disable_xa) |
2515 | { |
2516 | if (conn->table_lock != 3) |
2517 | { |
2518 | if ((error_num = spider_db_rollback(conn))) |
2519 | { |
2520 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2521 | if (error_num) |
2522 | goto error; |
2523 | } |
2524 | } |
2525 | if ((error_num = spider_end_trx(trx, conn))) |
2526 | { |
2527 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2528 | if (error_num) |
2529 | goto error; |
2530 | } |
2531 | conn->join_trx = 0; |
2532 | } else { |
2533 | if (trx->updated_in_this_trx || spider_param_xa_register_mode(thd) == 0) |
2534 | { |
2535 | /* |
2536 | insert into mysql.spider_xa_member |
2537 | (format_id, gtrid_length, bqual_length, data, |
2538 | scheme, host, port, socket, username, password) values |
2539 | (trx->xid.format_id, trx->xid.gtrid_length, |
2540 | trx->xid.bqual_length, trx->xid.data, |
2541 | conn->tgt_wrapper, |
2542 | conn->tgt_host, |
2543 | conn->tgt_port, |
2544 | conn->tgt_socket, |
2545 | conn->tgt_username, |
2546 | conn->tgt_password) |
2547 | */ |
2548 | if ( |
2549 | (error_num = spider_insert_xa_member( |
2550 | table_xa_member, &trx->xid, conn)) |
2551 | ) { |
2552 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2553 | if (error_num) |
2554 | goto error; |
2555 | } |
2556 | } |
2557 | |
2558 | if ((error_num = spider_db_xa_end(conn, &trx->xid))) |
2559 | { |
2560 | if (force_commit == 0 || |
2561 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
2562 | { |
2563 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2564 | if (error_num) |
2565 | goto error; |
2566 | } |
2567 | } |
2568 | if ((error_num = spider_db_xa_prepare(conn, &trx->xid))) |
2569 | { |
2570 | if (force_commit == 0 || |
2571 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
2572 | { |
2573 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2574 | if (error_num) |
2575 | goto error; |
2576 | } |
2577 | } |
2578 | /* |
2579 | if (!internal_xa) |
2580 | { |
2581 | if ((error_num = spider_end_trx(trx, conn))) |
2582 | DBUG_RETURN(error_num); |
2583 | conn->join_trx = 0; |
2584 | } |
2585 | */ |
2586 | } |
2587 | } while ((conn = spider_tree_next(conn))); |
2588 | /* |
2589 | if (!internal_xa) |
2590 | trx->join_trx_top = NULL; |
2591 | */ |
2592 | } |
2593 | if (trx->updated_in_this_trx || spider_param_xa_register_mode(thd) == 0) |
2594 | { |
2595 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2596 | table_xa_member_opened = FALSE; |
2597 | |
2598 | /* |
2599 | update |
2600 | mysql.spider_xa |
2601 | set |
2602 | status = 'PREPARED' |
2603 | where |
2604 | format_id = trx->xid.format_id and |
2605 | gtrid_length = trx->xid.gtrid_length and |
2606 | data = trx->xid.data |
2607 | */ |
2608 | if ( |
2609 | !(table_xa = spider_open_sys_table( |
2610 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2611 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2612 | ) |
2613 | goto error_open_table; |
2614 | table_xa_opened = TRUE; |
2615 | if ( |
2616 | (error_num = spider_update_xa( |
2617 | table_xa, &trx->xid, SPIDER_SYS_XA_PREPARED_STR)) |
2618 | ) |
2619 | goto error; |
2620 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2621 | table_xa_opened = FALSE; |
2622 | } |
2623 | if (internal_xa) |
2624 | trx->internal_xid_state.xa_state = XA_PREPARED; |
2625 | DBUG_RETURN(0); |
2626 | |
2627 | error: |
2628 | if (table_xa_opened) |
2629 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2630 | if (table_xa_member_opened) |
2631 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2632 | error_open_table: |
2633 | DBUG_RETURN(error_num); |
2634 | } |
2635 | |
2636 | int spider_internal_xa_recover( |
2637 | THD* thd, |
2638 | XID* xid_list, |
2639 | uint len |
2640 | ) { |
2641 | TABLE *table_xa; |
2642 | int cnt = 0; |
2643 | char xa_key[MAX_KEY_LENGTH]; |
2644 | MEM_ROOT mem_root; |
2645 | #if MYSQL_VERSION_ID < 50500 |
2646 | Open_tables_state open_tables_backup; |
2647 | #else |
2648 | Open_tables_backup open_tables_backup; |
2649 | #endif |
2650 | DBUG_ENTER("spider_internal_xa_recover" ); |
2651 | /* |
2652 | select |
2653 | format_id, |
2654 | gtrid_length, |
2655 | bqual_length, |
2656 | data |
2657 | from |
2658 | mysql.spider_xa |
2659 | where |
2660 | status = 'PREPARED' |
2661 | */ |
2662 | if ( |
2663 | !(table_xa = spider_open_sys_table( |
2664 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2665 | FALSE, &open_tables_backup, TRUE, &my_errno)) |
2666 | ) |
2667 | goto error_open_table; |
2668 | spider_store_xa_status(table_xa, SPIDER_SYS_XA_PREPARED_STR); |
2669 | if ( |
2670 | (my_errno = spider_get_sys_table_by_idx(table_xa, xa_key, 1, |
2671 | SPIDER_SYS_XA_IDX1_COL_CNT)) |
2672 | ) { |
2673 | spider_sys_index_end(table_xa); |
2674 | if (my_errno != HA_ERR_KEY_NOT_FOUND && my_errno != HA_ERR_END_OF_FILE) |
2675 | { |
2676 | table_xa->file->print_error(my_errno, MYF(0)); |
2677 | goto error; |
2678 | } |
2679 | goto error; |
2680 | } |
2681 | |
2682 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
2683 | do { |
2684 | spider_get_sys_xid(table_xa, &xid_list[cnt], &mem_root); |
2685 | cnt++; |
2686 | my_errno = spider_sys_index_next_same(table_xa, xa_key); |
2687 | } while (my_errno == 0 && cnt < (int) len); |
2688 | free_root(&mem_root, MYF(0)); |
2689 | spider_sys_index_end(table_xa); |
2690 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2691 | DBUG_RETURN(cnt); |
2692 | |
2693 | error: |
2694 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2695 | error_open_table: |
2696 | DBUG_RETURN(0); |
2697 | } |
2698 | |
2699 | int spider_initinal_xa_recover( |
2700 | XID* xid_list, |
2701 | uint len |
2702 | ) { |
2703 | int error_num; |
2704 | static THD *thd = NULL; |
2705 | static TABLE *table_xa = NULL; |
2706 | static READ_RECORD *read_record = NULL; |
2707 | #if MYSQL_VERSION_ID < 50500 |
2708 | static Open_tables_state *open_tables_backup = NULL; |
2709 | #else |
2710 | static Open_tables_backup *open_tables_backup = NULL; |
2711 | #endif |
2712 | int cnt = 0; |
2713 | MEM_ROOT mem_root; |
2714 | DBUG_ENTER("spider_initinal_xa_recover" ); |
2715 | if (!open_tables_backup) |
2716 | { |
2717 | #if MYSQL_VERSION_ID < 50500 |
2718 | if (!(open_tables_backup = new Open_tables_state)) |
2719 | #else |
2720 | if (!(open_tables_backup = new Open_tables_backup)) |
2721 | #endif |
2722 | { |
2723 | error_num = HA_ERR_OUT_OF_MEM; |
2724 | goto error_create_state; |
2725 | } |
2726 | } |
2727 | if (!read_record) |
2728 | { |
2729 | if (!(read_record = new READ_RECORD)) |
2730 | { |
2731 | error_num = HA_ERR_OUT_OF_MEM; |
2732 | goto error_create_read_record; |
2733 | } |
2734 | } |
2735 | |
2736 | /* |
2737 | if (!thd) |
2738 | { |
2739 | */ |
2740 | if (!(thd = spider_create_tmp_thd())) |
2741 | { |
2742 | error_num = HA_ERR_OUT_OF_MEM; |
2743 | goto error_create_thd; |
2744 | } |
2745 | /* |
2746 | } |
2747 | */ |
2748 | |
2749 | /* |
2750 | select |
2751 | format_id, |
2752 | gtrid_length, |
2753 | bqual_length, |
2754 | data |
2755 | from |
2756 | mysql.spider_xa |
2757 | */ |
2758 | if (!table_xa) |
2759 | { |
2760 | if ( |
2761 | !(table_xa = spider_open_sys_table( |
2762 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2763 | FALSE, open_tables_backup, TRUE, &error_num)) |
2764 | ) |
2765 | goto error_open_table; |
2766 | SPIDER_init_read_record(read_record, thd, table_xa, NULL, NULL, TRUE, |
2767 | FALSE, FALSE); |
2768 | } |
2769 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
2770 | while ((!(read_record->read_record())) && cnt < (int) len) |
2771 | { |
2772 | spider_get_sys_xid(table_xa, &xid_list[cnt], &mem_root); |
2773 | cnt++; |
2774 | } |
2775 | free_root(&mem_root, MYF(0)); |
2776 | |
2777 | /* |
2778 | if (cnt < (int) len) |
2779 | { |
2780 | */ |
2781 | end_read_record(read_record); |
2782 | spider_close_sys_table(thd, table_xa, open_tables_backup, TRUE); |
2783 | table_xa = NULL; |
2784 | spider_free_tmp_thd(thd); |
2785 | thd = NULL; |
2786 | delete read_record; |
2787 | read_record = NULL; |
2788 | delete open_tables_backup; |
2789 | open_tables_backup = NULL; |
2790 | /* |
2791 | } |
2792 | */ |
2793 | DBUG_RETURN(cnt); |
2794 | |
2795 | /* |
2796 | error: |
2797 | end_read_record(&read_record_info); |
2798 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2799 | table_xa = NULL; |
2800 | */ |
2801 | error_open_table: |
2802 | spider_free_tmp_thd(thd); |
2803 | thd = NULL; |
2804 | error_create_thd: |
2805 | delete read_record; |
2806 | read_record = NULL; |
2807 | error_create_read_record: |
2808 | delete open_tables_backup; |
2809 | open_tables_backup = NULL; |
2810 | error_create_state: |
2811 | DBUG_RETURN(0); |
2812 | } |
2813 | |
2814 | int spider_internal_xa_commit_by_xid( |
2815 | THD* thd, |
2816 | SPIDER_TRX *trx, |
2817 | XID* xid |
2818 | ) { |
2819 | TABLE *table_xa, *table_xa_member= 0; |
2820 | int error_num; |
2821 | char xa_key[MAX_KEY_LENGTH]; |
2822 | char xa_member_key[MAX_KEY_LENGTH]; |
2823 | SPIDER_SHARE tmp_share; |
2824 | char *tmp_connect_info[SPIDER_TMP_SHARE_CHAR_PTR_COUNT]; |
2825 | uint tmp_connect_info_length[SPIDER_TMP_SHARE_UINT_COUNT]; |
2826 | long tmp_long[SPIDER_TMP_SHARE_LONG_COUNT]; |
2827 | longlong tmp_longlong[SPIDER_TMP_SHARE_LONGLONG_COUNT]; |
2828 | SPIDER_CONN *conn; |
2829 | uint force_commit = spider_param_force_commit(thd); |
2830 | MEM_ROOT mem_root; |
2831 | #if MYSQL_VERSION_ID < 50500 |
2832 | Open_tables_state open_tables_backup; |
2833 | #else |
2834 | Open_tables_backup open_tables_backup; |
2835 | #endif |
2836 | bool table_xa_opened = FALSE; |
2837 | bool table_xa_member_opened = FALSE; |
2838 | DBUG_ENTER("spider_internal_xa_commit_by_xid" ); |
2839 | /* |
2840 | select |
2841 | status |
2842 | from |
2843 | mysql.spider_xa |
2844 | where |
2845 | format_id = xid->format_id and |
2846 | gtrid_length = xid->gtrid_length and |
2847 | data = xid->data |
2848 | */ |
2849 | if ( |
2850 | !(table_xa = spider_open_sys_table( |
2851 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
2852 | TRUE, &open_tables_backup, TRUE, &error_num)) |
2853 | ) |
2854 | goto error_open_table; |
2855 | table_xa_opened = TRUE; |
2856 | spider_store_xa_pk(table_xa, xid); |
2857 | if ( |
2858 | (error_num = spider_check_sys_table(table_xa, xa_key)) |
2859 | ) { |
2860 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
2861 | { |
2862 | table_xa->file->print_error(error_num, MYF(0)); |
2863 | goto error; |
2864 | } |
2865 | my_message(ER_SPIDER_XA_NOT_EXISTS_NUM, ER_SPIDER_XA_NOT_EXISTS_STR, |
2866 | MYF(0)); |
2867 | error_num = ER_SPIDER_XA_NOT_EXISTS_NUM; |
2868 | goto error; |
2869 | } |
2870 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
2871 | if ( |
2872 | force_commit != 2 && |
2873 | (error_num = spider_check_sys_xa_status( |
2874 | table_xa, |
2875 | SPIDER_SYS_XA_PREPARED_STR, |
2876 | SPIDER_SYS_XA_COMMIT_STR, |
2877 | NULL, |
2878 | ER_SPIDER_XA_NOT_PREPARED_NUM, |
2879 | &mem_root)) |
2880 | ) { |
2881 | free_root(&mem_root, MYF(0)); |
2882 | if (error_num == ER_SPIDER_XA_NOT_PREPARED_NUM) |
2883 | my_message(error_num, ER_SPIDER_XA_NOT_PREPARED_STR, MYF(0)); |
2884 | goto error; |
2885 | } |
2886 | |
2887 | /* |
2888 | update |
2889 | mysql.spider_xa |
2890 | set |
2891 | status = 'COMMIT' |
2892 | where |
2893 | format_id = trx->xid.format_id and |
2894 | gtrid_length = trx->xid.gtrid_length and |
2895 | data = trx->xid.data |
2896 | */ |
2897 | if ( |
2898 | (error_num = spider_update_xa( |
2899 | table_xa, xid, SPIDER_SYS_XA_COMMIT_STR)) |
2900 | ) { |
2901 | free_root(&mem_root, MYF(0)); |
2902 | goto error; |
2903 | } |
2904 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
2905 | table_xa_opened = FALSE; |
2906 | |
2907 | /* |
2908 | select |
2909 | scheme tmp_share.tgt_wrappers, |
2910 | host tmp_share.tgt_hosts, |
2911 | port tmp_share.tgt_ports, |
2912 | socket tmp_share.tgt_sockets, |
2913 | username tmp_share.tgt_usernames, |
2914 | password tmp_share.tgt_passwords |
2915 | from |
2916 | mysql.spider_xa_member |
2917 | where |
2918 | format_id = xid->format_id and |
2919 | gtrid_length = xid->gtrid_length and |
2920 | data = xid->data |
2921 | */ |
2922 | if ( |
2923 | !(table_xa_member = spider_open_sys_table( |
2924 | thd, SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR, |
2925 | SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE, |
2926 | &error_num)) |
2927 | ) { |
2928 | free_root(&mem_root, MYF(0)); |
2929 | goto error_open_table; |
2930 | } |
2931 | table_xa_member_opened = TRUE; |
2932 | spider_store_xa_pk(table_xa_member, xid); |
2933 | if ( |
2934 | (error_num = spider_get_sys_table_by_idx(table_xa_member, xa_member_key, 0, |
2935 | SPIDER_SYS_XA_PK_COL_CNT)) |
2936 | ) { |
2937 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
2938 | { |
2939 | free_root(&mem_root, MYF(0)); |
2940 | table_xa_member->file->print_error(error_num, MYF(0)); |
2941 | goto error; |
2942 | } else { |
2943 | free_root(&mem_root, MYF(0)); |
2944 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
2945 | table_xa_member_opened = FALSE; |
2946 | goto xa_delete; |
2947 | } |
2948 | } |
2949 | |
2950 | memset(&tmp_share, 0, sizeof(SPIDER_SHARE)); |
2951 | memset(&tmp_connect_info, 0, |
2952 | sizeof(char *) * SPIDER_TMP_SHARE_CHAR_PTR_COUNT); |
2953 | spider_set_tmp_share_pointer(&tmp_share, tmp_connect_info, |
2954 | tmp_connect_info_length, tmp_long, tmp_longlong); |
2955 | do { |
2956 | SPIDER_BACKUP_DASTATUS; |
2957 | spider_get_sys_server_info(table_xa_member, &tmp_share, 0, &mem_root); |
2958 | if ((error_num = spider_create_conn_keys(&tmp_share))) |
2959 | { |
2960 | spider_sys_index_end(table_xa_member); |
2961 | free_root(&mem_root, MYF(0)); |
2962 | goto error; |
2963 | } |
2964 | |
2965 | if ( |
2966 | !(conn = spider_get_conn( |
2967 | &tmp_share, 0, tmp_share.conn_keys[0], trx, NULL, FALSE, FALSE, |
2968 | SPIDER_CONN_KIND_MYSQL, &error_num)) && |
2969 | (force_commit == 0 || |
2970 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
2971 | ) { |
2972 | spider_sys_index_end(table_xa_member); |
2973 | spider_free_tmp_share_alloc(&tmp_share); |
2974 | free_root(&mem_root, MYF(0)); |
2975 | goto error; |
2976 | } |
2977 | conn->error_mode &= spider_param_error_read_mode(thd, 0); |
2978 | conn->error_mode &= spider_param_error_write_mode(thd, 0); |
2979 | if ( |
2980 | (error_num = spider_db_xa_commit(conn, xid)) && |
2981 | (force_commit == 0 || |
2982 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
2983 | ) { |
2984 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
2985 | if (error_num) |
2986 | { |
2987 | spider_sys_index_end(table_xa_member); |
2988 | spider_free_tmp_share_alloc(&tmp_share); |
2989 | free_root(&mem_root, MYF(0)); |
2990 | goto error; |
2991 | } |
2992 | } |
2993 | spider_free_tmp_share_alloc(&tmp_share); |
2994 | error_num = spider_sys_index_next_same(table_xa_member, xa_member_key); |
2995 | } while (error_num == 0); |
2996 | if ((error_num = spider_sys_index_end(table_xa_member))) |
2997 | { |
2998 | free_root(&mem_root, MYF(0)); |
2999 | goto error; |
3000 | } |
3001 | free_root(&mem_root, MYF(0)); |
3002 | spider_reuse_trx_ha(trx); |
3003 | spider_free_trx_conn(trx, FALSE); |
3004 | |
3005 | /* |
3006 | delete from |
3007 | mysql.spider_xa_member |
3008 | where |
3009 | format_id = xid->format_id and |
3010 | gtrid_length = xid->gtrid_length and |
3011 | data = xid->data |
3012 | */ |
3013 | if ((error_num = spider_delete_xa_member(table_xa_member, xid))) |
3014 | goto error; |
3015 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
3016 | table_xa_member_opened = FALSE; |
3017 | |
3018 | xa_delete: |
3019 | /* |
3020 | delete from |
3021 | mysql.spider_xa |
3022 | where |
3023 | format_id = xid->format_id and |
3024 | gtrid_length = xid->gtrid_length and |
3025 | data = xid->data |
3026 | */ |
3027 | if ( |
3028 | !(table_xa = spider_open_sys_table( |
3029 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
3030 | TRUE, &open_tables_backup, TRUE, &error_num)) |
3031 | ) |
3032 | goto error_open_table; |
3033 | table_xa_opened = TRUE; |
3034 | if ((error_num = spider_delete_xa(table_xa, xid))) |
3035 | goto error; |
3036 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
3037 | table_xa_opened = FALSE; |
3038 | DBUG_RETURN(0); |
3039 | |
3040 | error: |
3041 | if (table_xa_opened) |
3042 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
3043 | if (table_xa_member_opened) |
3044 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
3045 | error_open_table: |
3046 | DBUG_RETURN(error_num); |
3047 | } |
3048 | |
3049 | int spider_internal_xa_rollback_by_xid( |
3050 | THD* thd, |
3051 | SPIDER_TRX *trx, |
3052 | XID* xid |
3053 | ) { |
3054 | TABLE *table_xa, *table_xa_member= 0; |
3055 | int error_num; |
3056 | char xa_key[MAX_KEY_LENGTH]; |
3057 | char xa_member_key[MAX_KEY_LENGTH]; |
3058 | SPIDER_SHARE tmp_share; |
3059 | char *tmp_connect_info[SPIDER_TMP_SHARE_CHAR_PTR_COUNT]; |
3060 | uint tmp_connect_info_length[SPIDER_TMP_SHARE_UINT_COUNT]; |
3061 | long tmp_long[SPIDER_TMP_SHARE_LONG_COUNT]; |
3062 | longlong tmp_longlong[SPIDER_TMP_SHARE_LONGLONG_COUNT]; |
3063 | SPIDER_CONN *conn; |
3064 | uint force_commit = spider_param_force_commit(thd); |
3065 | MEM_ROOT mem_root; |
3066 | #if MYSQL_VERSION_ID < 50500 |
3067 | Open_tables_state open_tables_backup; |
3068 | #else |
3069 | Open_tables_backup open_tables_backup; |
3070 | #endif |
3071 | bool table_xa_opened = FALSE; |
3072 | bool table_xa_member_opened = FALSE; |
3073 | DBUG_ENTER("spider_internal_xa_rollback_by_xid" ); |
3074 | /* |
3075 | select |
3076 | status |
3077 | from |
3078 | mysql.spider_xa |
3079 | where |
3080 | format_id = xid->format_id and |
3081 | gtrid_length = xid->gtrid_length and |
3082 | data = xid->data |
3083 | */ |
3084 | if ( |
3085 | !(table_xa = spider_open_sys_table( |
3086 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
3087 | TRUE, &open_tables_backup, TRUE, &error_num)) |
3088 | ) |
3089 | goto error_open_table; |
3090 | table_xa_opened = TRUE; |
3091 | spider_store_xa_pk(table_xa, xid); |
3092 | if ( |
3093 | (error_num = spider_check_sys_table(table_xa, xa_key)) |
3094 | ) { |
3095 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
3096 | { |
3097 | table_xa->file->print_error(error_num, MYF(0)); |
3098 | goto error; |
3099 | } |
3100 | error_num = ER_SPIDER_XA_NOT_EXISTS_NUM; |
3101 | goto error; |
3102 | } |
3103 | SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME)); |
3104 | if ( |
3105 | force_commit != 2 && |
3106 | (error_num = spider_check_sys_xa_status( |
3107 | table_xa, |
3108 | SPIDER_SYS_XA_NOT_YET_STR, |
3109 | SPIDER_SYS_XA_PREPARED_STR, |
3110 | SPIDER_SYS_XA_ROLLBACK_STR, |
3111 | ER_SPIDER_XA_PREPARED_NUM, |
3112 | &mem_root)) |
3113 | ) { |
3114 | free_root(&mem_root, MYF(0)); |
3115 | if (error_num == ER_SPIDER_XA_PREPARED_NUM) |
3116 | my_message(error_num, ER_SPIDER_XA_PREPARED_STR, MYF(0)); |
3117 | goto error; |
3118 | } |
3119 | |
3120 | /* |
3121 | update |
3122 | mysql.spider_xa |
3123 | set |
3124 | status = 'ROLLBACK' |
3125 | where |
3126 | format_id = trx->xid.format_id and |
3127 | gtrid_length = trx->xid.gtrid_length and |
3128 | data = trx->xid.data |
3129 | */ |
3130 | if ( |
3131 | (error_num = spider_update_xa( |
3132 | table_xa, xid, SPIDER_SYS_XA_ROLLBACK_STR)) |
3133 | ) { |
3134 | free_root(&mem_root, MYF(0)); |
3135 | goto error; |
3136 | } |
3137 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
3138 | table_xa_opened = FALSE; |
3139 | |
3140 | /* |
3141 | select |
3142 | scheme tmp_share.tgt_wrappers, |
3143 | host tmp_share.tgt_hosts, |
3144 | port tmp_share.tgt_ports, |
3145 | socket tmp_share.tgt_sockets, |
3146 | username tmp_share.tgt_usernames, |
3147 | password tmp_share.tgt_passwords |
3148 | from |
3149 | mysql.spider_xa_member |
3150 | where |
3151 | format_id = xid->format_id and |
3152 | gtrid_length = xid->gtrid_length and |
3153 | data = xid->data |
3154 | */ |
3155 | if ( |
3156 | !(table_xa_member = spider_open_sys_table( |
3157 | thd, SPIDER_SYS_XA_MEMBER_TABLE_NAME_STR, |
3158 | SPIDER_SYS_XA_MEMBER_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE, |
3159 | &error_num)) |
3160 | ) { |
3161 | free_root(&mem_root, MYF(0)); |
3162 | goto error_open_table; |
3163 | } |
3164 | table_xa_member_opened = TRUE; |
3165 | spider_store_xa_pk(table_xa_member, xid); |
3166 | if ( |
3167 | (error_num = spider_get_sys_table_by_idx(table_xa_member, xa_member_key, 0, |
3168 | SPIDER_SYS_XA_PK_COL_CNT)) |
3169 | ) { |
3170 | if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE) |
3171 | { |
3172 | free_root(&mem_root, MYF(0)); |
3173 | table_xa_member->file->print_error(error_num, MYF(0)); |
3174 | goto error; |
3175 | } else { |
3176 | free_root(&mem_root, MYF(0)); |
3177 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
3178 | table_xa_member_opened = FALSE; |
3179 | goto xa_delete; |
3180 | } |
3181 | } |
3182 | |
3183 | memset(&tmp_share, 0, sizeof(SPIDER_SHARE)); |
3184 | memset(&tmp_connect_info, 0, |
3185 | sizeof(char *) * SPIDER_TMP_SHARE_CHAR_PTR_COUNT); |
3186 | spider_set_tmp_share_pointer(&tmp_share, tmp_connect_info, |
3187 | tmp_connect_info_length, tmp_long, tmp_longlong); |
3188 | do { |
3189 | SPIDER_BACKUP_DASTATUS; |
3190 | spider_get_sys_server_info(table_xa_member, &tmp_share, 0, &mem_root); |
3191 | if ((error_num = spider_create_conn_keys(&tmp_share))) |
3192 | { |
3193 | spider_sys_index_end(table_xa_member); |
3194 | free_root(&mem_root, MYF(0)); |
3195 | goto error; |
3196 | } |
3197 | |
3198 | if ( |
3199 | !(conn = spider_get_conn( |
3200 | &tmp_share, 0, tmp_share.conn_keys[0], trx, NULL, FALSE, FALSE, |
3201 | SPIDER_CONN_KIND_MYSQL, &error_num)) && |
3202 | (force_commit == 0 || |
3203 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
3204 | ) { |
3205 | spider_sys_index_end(table_xa_member); |
3206 | spider_free_tmp_share_alloc(&tmp_share); |
3207 | free_root(&mem_root, MYF(0)); |
3208 | goto error; |
3209 | } |
3210 | conn->error_mode &= spider_param_error_read_mode(thd, 0); |
3211 | conn->error_mode &= spider_param_error_write_mode(thd, 0); |
3212 | if ( |
3213 | (error_num = spider_db_xa_rollback(conn, xid)) && |
3214 | (force_commit == 0 || |
3215 | (force_commit == 1 && error_num != ER_XAER_NOTA)) |
3216 | ) { |
3217 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_ERROR_NUM; |
3218 | if (error_num) |
3219 | { |
3220 | spider_sys_index_end(table_xa_member); |
3221 | spider_free_tmp_share_alloc(&tmp_share); |
3222 | free_root(&mem_root, MYF(0)); |
3223 | goto error; |
3224 | } |
3225 | } |
3226 | spider_free_tmp_share_alloc(&tmp_share); |
3227 | error_num = spider_sys_index_next_same(table_xa_member, xa_member_key); |
3228 | } while (error_num == 0); |
3229 | if ((error_num = spider_sys_index_end(table_xa_member))) |
3230 | { |
3231 | free_root(&mem_root, MYF(0)); |
3232 | goto error; |
3233 | } |
3234 | free_root(&mem_root, MYF(0)); |
3235 | spider_reuse_trx_ha(trx); |
3236 | spider_free_trx_conn(trx, FALSE); |
3237 | |
3238 | /* |
3239 | delete from |
3240 | mysql.spider_xa_member |
3241 | where |
3242 | format_id = xid->format_id and |
3243 | gtrid_length = xid->gtrid_length and |
3244 | data = xid->data |
3245 | */ |
3246 | if ((error_num = spider_delete_xa_member(table_xa_member, xid))) |
3247 | goto error; |
3248 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
3249 | table_xa_member_opened = FALSE; |
3250 | |
3251 | xa_delete: |
3252 | /* |
3253 | delete from |
3254 | mysql.spider_xa |
3255 | where |
3256 | format_id = xid->format_id and |
3257 | gtrid_length = xid->gtrid_length and |
3258 | data = xid->data |
3259 | */ |
3260 | if ( |
3261 | !(table_xa = spider_open_sys_table( |
3262 | thd, SPIDER_SYS_XA_TABLE_NAME_STR, SPIDER_SYS_XA_TABLE_NAME_LEN, |
3263 | TRUE, &open_tables_backup, TRUE, &error_num)) |
3264 | ) |
3265 | goto error_open_table; |
3266 | table_xa_opened = TRUE; |
3267 | if ((error_num = spider_delete_xa(table_xa, xid))) |
3268 | goto error; |
3269 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
3270 | table_xa_opened = FALSE; |
3271 | DBUG_RETURN(0); |
3272 | |
3273 | error: |
3274 | if (table_xa_opened) |
3275 | spider_close_sys_table(thd, table_xa, &open_tables_backup, TRUE); |
3276 | if (table_xa_member_opened) |
3277 | spider_close_sys_table(thd, table_xa_member, &open_tables_backup, TRUE); |
3278 | error_open_table: |
3279 | DBUG_RETURN(error_num); |
3280 | } |
3281 | |
3282 | int spider_start_consistent_snapshot( |
3283 | handlerton *hton, |
3284 | THD* thd |
3285 | ) { |
3286 | int error_num; |
3287 | SPIDER_TRX *trx; |
3288 | DBUG_ENTER("spider_start_consistent_snapshot" ); |
3289 | |
3290 | if (!(trx = spider_get_trx(thd, TRUE, &error_num))) |
3291 | DBUG_RETURN(error_num); |
3292 | if (spider_param_use_consistent_snapshot(trx->thd)) |
3293 | { |
3294 | if (spider_param_internal_xa(trx->thd) && |
3295 | spider_param_internal_xa_snapshot(trx->thd) == 1) |
3296 | { |
3297 | error_num = ER_SPIDER_CANT_USE_BOTH_INNER_XA_AND_SNAPSHOT_NUM; |
3298 | my_message(error_num, ER_SPIDER_CANT_USE_BOTH_INNER_XA_AND_SNAPSHOT_STR, |
3299 | MYF(0)); |
3300 | goto error; |
3301 | } else { |
3302 | trx->trx_consistent_snapshot = TRUE; |
3303 | trx->use_consistent_snapshot = TRUE; |
3304 | trx->internal_xa_snapshot = spider_param_internal_xa_snapshot(trx->thd); |
3305 | trans_register_ha(trx->thd, FALSE, spider_hton_ptr); |
3306 | trans_register_ha(trx->thd, TRUE, spider_hton_ptr); |
3307 | if (spider_param_use_all_conns_snapshot(trx->thd)) |
3308 | { |
3309 | trx->internal_xa = FALSE; |
3310 | if ((error_num = spider_open_all_tables(trx, TRUE))) |
3311 | goto error_open_all_tables; |
3312 | if ( |
3313 | spider_param_use_snapshot_with_flush_tables(trx->thd) == 1 && |
3314 | (error_num = spider_trx_all_flush_tables(trx)) |
3315 | ) |
3316 | goto error_trx_all_flush_tables; |
3317 | if (spider_param_use_snapshot_with_flush_tables(trx->thd) == 2) |
3318 | { |
3319 | if ((error_num = spider_trx_another_lock_tables(trx))) |
3320 | goto error_trx_another_lock_tables; |
3321 | if ((error_num = spider_trx_another_flush_tables(trx))) |
3322 | goto error_trx_another_flush_tables; |
3323 | } |
3324 | if ((error_num = spider_trx_all_start_trx(trx))) |
3325 | goto error_trx_all_start_trx; |
3326 | if (spider_param_use_snapshot_with_flush_tables(trx->thd) == 1) |
3327 | { |
3328 | if ( |
3329 | spider_param_use_flash_logs(trx->thd) && |
3330 | (error_num = spider_trx_all_flush_logs(trx)) |
3331 | ) |
3332 | goto error_trx_all_flush_logs; |
3333 | if ((error_num = spider_trx_all_unlock_tables(trx))) |
3334 | goto error_trx_all_unlock_tables; |
3335 | } |
3336 | if (spider_param_use_snapshot_with_flush_tables(trx->thd) == 2) |
3337 | { |
3338 | if ( |
3339 | spider_param_use_flash_logs(trx->thd) && |
3340 | (error_num = spider_trx_all_flush_logs(trx)) |
3341 | ) |
3342 | goto error_trx_all_flush_logs2; |
3343 | if ((error_num = spider_free_trx_another_conn(trx, TRUE))) |
3344 | goto error_free_trx_another_conn; |
3345 | } |
3346 | } else |
3347 | trx->internal_xa = spider_param_internal_xa(trx->thd); |
3348 | } |
3349 | } |
3350 | |
3351 | DBUG_RETURN(0); |
3352 | |
3353 | error_trx_all_flush_logs: |
3354 | error_trx_all_start_trx: |
3355 | error_trx_another_flush_tables: |
3356 | error_trx_another_lock_tables: |
3357 | error_trx_all_flush_tables: |
3358 | if (spider_param_use_snapshot_with_flush_tables(trx->thd) == 1) |
3359 | spider_trx_all_unlock_tables(trx); |
3360 | error_trx_all_flush_logs2: |
3361 | error_trx_all_unlock_tables: |
3362 | error_open_all_tables: |
3363 | if (spider_param_use_snapshot_with_flush_tables(trx->thd) == 2) |
3364 | spider_free_trx_another_conn(trx, TRUE); |
3365 | error_free_trx_another_conn: |
3366 | error: |
3367 | DBUG_RETURN(error_num); |
3368 | } |
3369 | |
3370 | int spider_commit( |
3371 | handlerton *hton, |
3372 | THD *thd, |
3373 | bool all |
3374 | ) { |
3375 | SPIDER_TRX *trx; |
3376 | TABLE *table_xa = NULL; |
3377 | TABLE *table_xa_member = NULL; |
3378 | int error_num = 0; |
3379 | SPIDER_CONN *conn; |
3380 | DBUG_ENTER("spider_commit" ); |
3381 | |
3382 | if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr))) |
3383 | DBUG_RETURN(0); /* transaction is not started */ |
3384 | |
3385 | #ifdef HA_CAN_BULK_ACCESS |
3386 | DBUG_PRINT("info" ,("spider trx->bulk_access_conn_first=%p" , |
3387 | trx->bulk_access_conn_first)); |
3388 | trx->bulk_access_conn_first = NULL; |
3389 | #endif |
3390 | |
3391 | if (all || (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) |
3392 | { |
3393 | if (trx->trx_start) |
3394 | { |
3395 | if (trx->trx_xa) |
3396 | { |
3397 | if (trx->internal_xa && !trx->trx_xa_prepared) |
3398 | { |
3399 | if ( |
3400 | (error_num = spider_internal_xa_prepare( |
3401 | thd, trx, table_xa, table_xa_member, TRUE)) |
3402 | ) { |
3403 | /* |
3404 | if (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) |
3405 | { |
3406 | */ |
3407 | /* rollback for semi_trx */ |
3408 | spider_rollback(hton, thd, all); |
3409 | /* |
3410 | } |
3411 | */ |
3412 | DBUG_RETURN(error_num); |
3413 | } |
3414 | trx->trx_xa_prepared = TRUE; |
3415 | } |
3416 | int tmp_error_num; |
3417 | if ( |
3418 | (tmp_error_num = spider_internal_xa_commit( |
3419 | thd, trx, &trx->xid, table_xa, table_xa_member)) |
3420 | ) { |
3421 | if (tmp_error_num) |
3422 | error_num = tmp_error_num; |
3423 | } |
3424 | trx->trx_xa = FALSE; |
3425 | trx->join_trx_top = NULL; |
3426 | } else { |
3427 | if ((conn = spider_tree_first(trx->join_trx_top))) |
3428 | { |
3429 | SPIDER_BACKUP_DASTATUS; |
3430 | int tmp_error_num; |
3431 | do { |
3432 | if ( |
3433 | (conn->autocommit != 1 || conn->trx_start) && |
3434 | (tmp_error_num = spider_db_commit(conn)) |
3435 | ) { |
3436 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
3437 | if (tmp_error_num) |
3438 | error_num = tmp_error_num; |
3439 | } |
3440 | if ((tmp_error_num = spider_end_trx(trx, conn))) |
3441 | { |
3442 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
3443 | if (tmp_error_num) |
3444 | error_num = tmp_error_num; |
3445 | } |
3446 | conn->join_trx = 0; |
3447 | } while ((conn = spider_tree_next(conn))); |
3448 | trx->join_trx_top = NULL; |
3449 | } |
3450 | } |
3451 | trx->trx_start = FALSE; |
3452 | trx->updated_in_this_trx = FALSE; |
3453 | DBUG_PRINT("info" ,("spider trx->trx_start=FALSE" )); |
3454 | DBUG_PRINT("info" ,("spider trx->updated_in_this_trx=FALSE" )); |
3455 | } |
3456 | spider_reuse_trx_ha(trx); |
3457 | spider_free_trx_conn(trx, FALSE); |
3458 | trx->trx_consistent_snapshot = FALSE; |
3459 | } |
3460 | spider_merge_mem_calc(trx, FALSE); |
3461 | DBUG_RETURN(error_num); |
3462 | } |
3463 | |
3464 | int spider_rollback( |
3465 | handlerton *hton, |
3466 | THD *thd, |
3467 | bool all |
3468 | ) { |
3469 | SPIDER_TRX *trx; |
3470 | int error_num = 0; |
3471 | SPIDER_CONN *conn; |
3472 | DBUG_ENTER("spider_rollback" ); |
3473 | |
3474 | if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr))) |
3475 | DBUG_RETURN(0); /* transaction is not started */ |
3476 | |
3477 | #ifdef HA_CAN_BULK_ACCESS |
3478 | DBUG_PRINT("info" ,("spider trx->bulk_access_conn_first=%p" , |
3479 | trx->bulk_access_conn_first)); |
3480 | trx->bulk_access_conn_first = NULL; |
3481 | #endif |
3482 | |
3483 | if (all || (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) |
3484 | { |
3485 | if (trx->trx_start) |
3486 | { |
3487 | if (trx->trx_xa) |
3488 | { |
3489 | int tmp_error_num; |
3490 | if ( |
3491 | (tmp_error_num = spider_internal_xa_rollback(thd, trx)) |
3492 | ) { |
3493 | if (tmp_error_num) |
3494 | error_num = tmp_error_num; |
3495 | } |
3496 | trx->trx_xa = FALSE; |
3497 | trx->join_trx_top = NULL; |
3498 | } else { |
3499 | if ((conn = spider_tree_first(trx->join_trx_top))) |
3500 | { |
3501 | SPIDER_BACKUP_DASTATUS; |
3502 | int tmp_error_num; |
3503 | do { |
3504 | if ( |
3505 | !conn->server_lost && |
3506 | (conn->autocommit != 1 || conn->trx_start) && |
3507 | (tmp_error_num = spider_db_rollback(conn)) |
3508 | ) { |
3509 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
3510 | if (tmp_error_num) |
3511 | error_num = tmp_error_num; |
3512 | } |
3513 | if ((tmp_error_num = spider_end_trx(trx, conn))) |
3514 | { |
3515 | SPIDER_CONN_RESTORE_DASTATUS_AND_RESET_TMP_ERROR_NUM; |
3516 | if (tmp_error_num) |
3517 | error_num = tmp_error_num; |
3518 | } |
3519 | conn->join_trx = 0; |
3520 | } while ((conn = spider_tree_next(conn))); |
3521 | trx->join_trx_top = NULL; |
3522 | } |
3523 | } |
3524 | trx->trx_start = FALSE; |
3525 | trx->updated_in_this_trx = FALSE; |
3526 | DBUG_PRINT("info" ,("spider trx->trx_start=FALSE" )); |
3527 | DBUG_PRINT("info" ,("spider trx->updated_in_this_trx=FALSE" )); |
3528 | } |
3529 | spider_reuse_trx_ha(trx); |
3530 | spider_free_trx_conn(trx, FALSE); |
3531 | trx->trx_consistent_snapshot = FALSE; |
3532 | } |
3533 | |
3534 | spider_merge_mem_calc(trx, FALSE); |
3535 | DBUG_RETURN(error_num); |
3536 | } |
3537 | |
3538 | int spider_xa_prepare( |
3539 | handlerton *hton, |
3540 | THD* thd, |
3541 | bool all |
3542 | ) { |
3543 | int error_num; |
3544 | SPIDER_TRX *trx; |
3545 | TABLE *table_xa = NULL; |
3546 | TABLE *table_xa_member = NULL; |
3547 | DBUG_ENTER("spider_xa_prepare" ); |
3548 | |
3549 | if (all || (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) |
3550 | { |
3551 | if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr))) |
3552 | DBUG_RETURN(0); /* transaction is not started */ |
3553 | |
3554 | DBUG_PRINT("info" ,("spider trx_start=%s" , |
3555 | trx->trx_start ? "TRUE" : "FALSE" )); |
3556 | DBUG_PRINT("info" ,("spider trx_xa=%s" , |
3557 | trx->trx_xa ? "TRUE" : "FALSE" )); |
3558 | if (trx->trx_start && trx->trx_xa) |
3559 | { |
3560 | if ((error_num = spider_internal_xa_prepare( |
3561 | thd, trx, table_xa, table_xa_member, FALSE))) |
3562 | goto error; |
3563 | trx->trx_xa_prepared = TRUE; |
3564 | } |
3565 | } |
3566 | |
3567 | DBUG_RETURN(0); |
3568 | |
3569 | error: |
3570 | DBUG_RETURN(error_num); |
3571 | } |
3572 | |
3573 | int spider_xa_recover( |
3574 | handlerton *hton, |
3575 | XID* xid_list, |
3576 | uint len |
3577 | ) { |
3578 | THD* thd = current_thd; |
3579 | DBUG_ENTER("spider_xa_recover" ); |
3580 | if (len == 0 || xid_list == NULL) |
3581 | DBUG_RETURN(0); |
3582 | |
3583 | if (thd) |
3584 | DBUG_RETURN(spider_internal_xa_recover(thd, xid_list, len)); |
3585 | else |
3586 | DBUG_RETURN(spider_initinal_xa_recover(xid_list, len)); |
3587 | } |
3588 | |
3589 | int spider_xa_commit_by_xid( |
3590 | handlerton *hton, |
3591 | XID* xid |
3592 | ) { |
3593 | SPIDER_TRX *trx; |
3594 | int error_num; |
3595 | THD* thd = current_thd; |
3596 | DBUG_ENTER("spider_xa_commit_by_xid" ); |
3597 | |
3598 | if (!(trx = spider_get_trx(thd, TRUE, &error_num))) |
3599 | goto error_get_trx; |
3600 | |
3601 | if ((error_num = spider_internal_xa_commit_by_xid(thd, trx, xid))) |
3602 | goto error; |
3603 | |
3604 | DBUG_RETURN(0); |
3605 | |
3606 | error: |
3607 | error_get_trx: |
3608 | DBUG_RETURN(error_num); |
3609 | } |
3610 | |
3611 | int spider_xa_rollback_by_xid( |
3612 | handlerton *hton, |
3613 | XID* xid |
3614 | ) { |
3615 | SPIDER_TRX *trx; |
3616 | int error_num; |
3617 | THD* thd = current_thd; |
3618 | DBUG_ENTER("spider_xa_rollback_by_xid" ); |
3619 | |
3620 | if (!(trx = spider_get_trx(thd, TRUE, &error_num))) |
3621 | goto error_get_trx; |
3622 | |
3623 | if ((error_num = spider_internal_xa_rollback_by_xid(thd, trx, xid))) |
3624 | goto error; |
3625 | |
3626 | DBUG_RETURN(0); |
3627 | |
3628 | error: |
3629 | error_get_trx: |
3630 | DBUG_RETURN(error_num); |
3631 | } |
3632 | |
3633 | void spider_copy_table_free_trx_conn( |
3634 | SPIDER_TRX *trx |
3635 | ) { |
3636 | SPIDER_CONN *conn; |
3637 | DBUG_ENTER("spider_copy_table_free_trx_conn" ); |
3638 | if ((conn = spider_tree_first(trx->join_trx_top))) |
3639 | { |
3640 | do { |
3641 | spider_end_trx(trx, conn); |
3642 | conn->join_trx = 0; |
3643 | } while ((conn = spider_tree_next(conn))); |
3644 | trx->join_trx_top = NULL; |
3645 | } |
3646 | spider_reuse_trx_ha(trx); |
3647 | spider_free_trx_conn(trx, FALSE); |
3648 | trx->trx_consistent_snapshot = FALSE; |
3649 | spider_merge_mem_calc(trx, FALSE); |
3650 | DBUG_VOID_RETURN; |
3651 | } |
3652 | |
3653 | int spider_end_trx( |
3654 | SPIDER_TRX *trx, |
3655 | SPIDER_CONN *conn |
3656 | ) { |
3657 | int error_num = 0, need_mon = 0; |
3658 | DBUG_ENTER("spider_end_trx" ); |
3659 | if (conn->table_lock == 3) |
3660 | { |
3661 | trx->tmp_spider->conns = &conn; |
3662 | conn->table_lock = 0; |
3663 | conn->disable_reconnect = FALSE; |
3664 | if ( |
3665 | !conn->server_lost && |
3666 | (error_num = spider_db_unlock_tables(trx->tmp_spider, 0)) |
3667 | ) { |
3668 | if (error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM) |
3669 | error_num = 0; |
3670 | } |
3671 | } else if (!conn->table_lock) |
3672 | conn->disable_reconnect = FALSE; |
3673 | if ( |
3674 | conn->semi_trx_isolation >= 0 && |
3675 | conn->trx_isolation != conn->semi_trx_isolation |
3676 | ) { |
3677 | DBUG_PRINT("info" ,("spider conn=%p" , conn)); |
3678 | DBUG_PRINT("info" ,("spider conn->trx_isolation=%d" , conn->trx_isolation)); |
3679 | if ( |
3680 | !conn->server_lost && |
3681 | !conn->queued_semi_trx_isolation && |
3682 | (error_num = spider_db_set_trx_isolation( |
3683 | conn, conn->trx_isolation, &need_mon)) |
3684 | ) { |
3685 | if ( |
3686 | !conn->disable_reconnect && |
3687 | error_num == ER_SPIDER_REMOTE_SERVER_GONE_AWAY_NUM |
3688 | ) |
3689 | error_num = 0; |
3690 | } |
3691 | } |
3692 | conn->semi_trx_isolation = -2; |
3693 | conn->semi_trx_isolation_chk = FALSE; |
3694 | conn->semi_trx_chk = FALSE; |
3695 | DBUG_RETURN(error_num); |
3696 | } |
3697 | |
3698 | int spider_check_trx_and_get_conn( |
3699 | THD *thd, |
3700 | ha_spider *spider, |
3701 | bool use_conn_kind |
3702 | ) { |
3703 | int error_num, roop_count, search_link_idx; |
3704 | SPIDER_TRX *trx; |
3705 | SPIDER_SHARE *share = spider->share; |
3706 | SPIDER_CONN *conn; |
3707 | char first_byte, first_byte_bak; |
3708 | int semi_table_lock_conn = spider_param_semi_table_lock_connection(thd, |
3709 | share->semi_table_lock_conn); |
3710 | DBUG_ENTER("spider_check_trx_and_get_conn" ); |
3711 | if (!(trx = spider_get_trx(thd, TRUE, &error_num))) |
3712 | { |
3713 | DBUG_PRINT("info" ,("spider get trx error" )); |
3714 | DBUG_RETURN(error_num); |
3715 | } |
3716 | spider->trx = trx; |
3717 | spider->set_error_mode(); |
3718 | if ( |
3719 | spider->sql_command != SQLCOM_DROP_TABLE && |
3720 | spider->sql_command != SQLCOM_ALTER_TABLE |
3721 | ) { |
3722 | SPIDER_TRX_HA *trx_ha = spider_check_trx_ha(trx, spider); |
3723 | if (!trx_ha || trx_ha->wait_for_reusing) |
3724 | spider_trx_set_link_idx_for_all(spider); |
3725 | |
3726 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3727 | #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS |
3728 | if (use_conn_kind) |
3729 | { |
3730 | for (roop_count = 0; roop_count < (int) share->link_count; roop_count++) |
3731 | { |
3732 | if ( |
3733 | /* |
3734 | spider->conn_kind[roop_count] != SPIDER_CONN_KIND_MYSQL && |
3735 | */ |
3736 | share->hs_dbton_ids[spider->conn_link_idx[roop_count]] == |
3737 | SPIDER_DBTON_SIZE |
3738 | ) { |
3739 | /* can't use hs interface */ |
3740 | spider->conn_kind[roop_count] = SPIDER_CONN_KIND_MYSQL; |
3741 | spider_clear_bit(spider->do_hs_direct_update, roop_count); |
3742 | } |
3743 | } |
3744 | } |
3745 | #endif |
3746 | #endif |
3747 | |
3748 | if (semi_table_lock_conn) |
3749 | first_byte = '0' + |
3750 | spider_param_semi_table_lock(thd, share->semi_table_lock); |
3751 | else |
3752 | first_byte = '0'; |
3753 | DBUG_PRINT("info" ,("spider semi_table_lock_conn = %d" , |
3754 | semi_table_lock_conn)); |
3755 | DBUG_PRINT("info" ,("spider semi_table_lock = %d" , |
3756 | spider_param_semi_table_lock(thd, share->semi_table_lock))); |
3757 | DBUG_PRINT("info" ,("spider first_byte = %d" , first_byte)); |
3758 | if ( |
3759 | !trx_ha || |
3760 | trx_ha->wait_for_reusing || |
3761 | trx->spider_thread_id != spider->spider_thread_id || |
3762 | trx->trx_conn_adjustment != spider->trx_conn_adjustment || |
3763 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3764 | (use_conn_kind && |
3765 | ( |
3766 | trx->trx_hs_r_conn_adjustment != spider->trx_hs_r_conn_adjustment || |
3767 | trx->trx_hs_w_conn_adjustment != spider->trx_hs_w_conn_adjustment |
3768 | ) |
3769 | ) || |
3770 | #endif |
3771 | first_byte != *spider->conn_keys[0] || |
3772 | share->link_statuses[spider->conn_link_idx[spider->search_link_idx]] == |
3773 | SPIDER_LINK_STATUS_NG |
3774 | ) { |
3775 | DBUG_PRINT("info" ,(first_byte != *spider->conn_keys[0] ? |
3776 | "spider change conn type" : trx != spider->trx ? "spider change thd" : |
3777 | "spider next trx" )); |
3778 | spider->trx = trx; |
3779 | spider->trx_conn_adjustment = trx->trx_conn_adjustment; |
3780 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3781 | if (use_conn_kind) |
3782 | { |
3783 | spider->trx_hs_r_conn_adjustment = trx->trx_hs_r_conn_adjustment; |
3784 | spider->trx_hs_w_conn_adjustment = trx->trx_hs_w_conn_adjustment; |
3785 | } |
3786 | #endif |
3787 | if ( |
3788 | spider->spider_thread_id != trx->spider_thread_id || |
3789 | spider->search_link_query_id != thd->query_id |
3790 | ) { |
3791 | search_link_idx = spider_conn_first_link_idx(thd, |
3792 | share->link_statuses, share->access_balances, spider->conn_link_idx, |
3793 | share->link_count, SPIDER_LINK_STATUS_OK); |
3794 | if (search_link_idx == -1) |
3795 | { |
3796 | TABLE *table = spider->get_table(); |
3797 | TABLE_SHARE *table_share = table->s; |
3798 | char *db = (char *) my_alloca( |
3799 | table_share->db.length + 1 + table_share->table_name.length + 1); |
3800 | if (!db) |
3801 | { |
3802 | my_error(HA_ERR_OUT_OF_MEM, MYF(0)); |
3803 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
3804 | } |
3805 | char *table_name = db + table_share->db.length + 1; |
3806 | memcpy(db, table_share->db.str, table_share->db.length); |
3807 | db[table_share->db.length] = '\0'; |
3808 | memcpy(table_name, table_share->table_name.str, |
3809 | table_share->table_name.length); |
3810 | table_name[table_share->table_name.length] = '\0'; |
3811 | my_printf_error(ER_SPIDER_ALL_LINKS_FAILED_NUM, |
3812 | ER_SPIDER_ALL_LINKS_FAILED_STR, MYF(0), db, table_name); |
3813 | my_afree(db); |
3814 | DBUG_RETURN(ER_SPIDER_ALL_LINKS_FAILED_NUM); |
3815 | } else if (search_link_idx == -2) |
3816 | { |
3817 | my_error(HA_ERR_OUT_OF_MEM, MYF(0)); |
3818 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
3819 | } |
3820 | spider->search_link_idx = search_link_idx; |
3821 | spider->search_link_query_id = thd->query_id; |
3822 | } |
3823 | spider->spider_thread_id = trx->spider_thread_id; |
3824 | |
3825 | first_byte_bak = *spider->conn_keys[0]; |
3826 | *spider->conn_keys[0] = first_byte; |
3827 | for (roop_count = 0; roop_count < (int) share->link_count; roop_count++) |
3828 | { |
3829 | if (!spider->handler_opened(roop_count, SPIDER_CONN_KIND_MYSQL)) |
3830 | spider->conns[roop_count] = NULL; |
3831 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3832 | if (!spider->handler_opened(roop_count, SPIDER_CONN_KIND_HS_READ)) |
3833 | spider->hs_r_conns[roop_count] = NULL; |
3834 | if (!spider->handler_opened(roop_count, SPIDER_CONN_KIND_HS_WRITE)) |
3835 | spider->hs_w_conns[roop_count] = NULL; |
3836 | #endif |
3837 | } |
3838 | bool search_link_idx_is_checked = FALSE; |
3839 | for ( |
3840 | roop_count = spider_conn_link_idx_next(share->link_statuses, |
3841 | spider->conn_link_idx, -1, share->link_count, |
3842 | SPIDER_LINK_STATUS_RECOVERY); |
3843 | roop_count < (int) share->link_count; |
3844 | roop_count = spider_conn_link_idx_next(share->link_statuses, |
3845 | spider->conn_link_idx, roop_count, share->link_count, |
3846 | SPIDER_LINK_STATUS_RECOVERY) |
3847 | ) { |
3848 | uint tgt_conn_kind = (use_conn_kind ? spider->conn_kind[roop_count] : |
3849 | SPIDER_CONN_KIND_MYSQL); |
3850 | if (roop_count == spider->search_link_idx) |
3851 | search_link_idx_is_checked = TRUE; |
3852 | if ( |
3853 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3854 | ( |
3855 | #endif |
3856 | tgt_conn_kind == SPIDER_CONN_KIND_MYSQL && |
3857 | !spider->conns[roop_count] |
3858 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3859 | ) || |
3860 | (tgt_conn_kind == SPIDER_CONN_KIND_HS_READ && |
3861 | !spider->hs_r_conns[roop_count]) || |
3862 | (tgt_conn_kind == SPIDER_CONN_KIND_HS_WRITE && |
3863 | !spider->hs_w_conns[roop_count]) |
3864 | #endif |
3865 | ) { |
3866 | *spider->conn_keys[roop_count] = first_byte; |
3867 | if ( |
3868 | !(conn = |
3869 | spider_get_conn(share, roop_count, |
3870 | spider->conn_keys[roop_count], trx, |
3871 | spider, FALSE, TRUE, |
3872 | use_conn_kind ? spider->conn_kind[roop_count] : |
3873 | SPIDER_CONN_KIND_MYSQL, |
3874 | &error_num)) |
3875 | ) { |
3876 | if ( |
3877 | share->monitoring_kind[roop_count] && |
3878 | spider->need_mons[roop_count] |
3879 | ) { |
3880 | error_num = spider_ping_table_mon_from_table( |
3881 | trx, |
3882 | trx->thd, |
3883 | share, |
3884 | roop_count, |
3885 | (uint32) share->monitoring_sid[roop_count], |
3886 | share->table_name, |
3887 | share->table_name_length, |
3888 | spider->conn_link_idx[roop_count], |
3889 | NULL, |
3890 | 0, |
3891 | share->monitoring_kind[roop_count], |
3892 | share->monitoring_limit[roop_count], |
3893 | share->monitoring_flag[roop_count], |
3894 | TRUE |
3895 | ); |
3896 | } |
3897 | DBUG_PRINT("info" ,("spider get conn error" )); |
3898 | *spider->conn_keys[0] = first_byte_bak; |
3899 | spider->spider_thread_id = 0; |
3900 | DBUG_RETURN(error_num); |
3901 | } |
3902 | conn->error_mode &= spider->error_mode; |
3903 | } |
3904 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3905 | #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS |
3906 | if ( |
3907 | spider->do_direct_update && |
3908 | spider_bit_is_set(spider->do_hs_direct_update, roop_count) && |
3909 | !spider->hs_w_conns[roop_count] |
3910 | ) { |
3911 | if ( |
3912 | !(conn = |
3913 | spider_get_conn(share, roop_count, |
3914 | spider->conn_keys[roop_count], trx, |
3915 | spider, FALSE, TRUE, |
3916 | SPIDER_CONN_KIND_HS_WRITE, |
3917 | &error_num)) |
3918 | ) { |
3919 | if ( |
3920 | share->monitoring_kind[roop_count] && |
3921 | spider->need_mons[roop_count] |
3922 | ) { |
3923 | error_num = spider_ping_table_mon_from_table( |
3924 | trx, |
3925 | trx->thd, |
3926 | share, |
3927 | roop_count, |
3928 | (uint32) share->monitoring_sid[roop_count], |
3929 | share->table_name, |
3930 | share->table_name_length, |
3931 | spider->conn_link_idx[roop_count], |
3932 | NULL, |
3933 | 0, |
3934 | share->monitoring_kind[roop_count], |
3935 | share->monitoring_limit[roop_count], |
3936 | share->monitoring_flag[roop_count], |
3937 | TRUE |
3938 | ); |
3939 | } |
3940 | DBUG_PRINT("info" ,("spider get conn error" )); |
3941 | *spider->conn_keys[0] = first_byte_bak; |
3942 | spider->spider_thread_id = 0; |
3943 | DBUG_RETURN(error_num); |
3944 | } |
3945 | conn->error_mode &= spider->error_mode; |
3946 | } |
3947 | #endif |
3948 | #endif |
3949 | } |
3950 | if (!search_link_idx_is_checked) |
3951 | { |
3952 | TABLE *table = spider->get_table(); |
3953 | TABLE_SHARE *table_share = table->s; |
3954 | char *db = (char *) my_alloca( |
3955 | table_share->db.length + 1 + table_share->table_name.length + 1); |
3956 | if (!db) |
3957 | { |
3958 | my_error(HA_ERR_OUT_OF_MEM, MYF(0)); |
3959 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
3960 | } |
3961 | char *table_name = db + table_share->db.length + 1; |
3962 | memcpy(db, table_share->db.str, table_share->db.length); |
3963 | db[table_share->db.length] = '\0'; |
3964 | memcpy(table_name, table_share->table_name.str, |
3965 | table_share->table_name.length); |
3966 | table_name[table_share->table_name.length] = '\0'; |
3967 | my_printf_error(ER_SPIDER_LINK_MON_JUST_NG_NUM, |
3968 | ER_SPIDER_LINK_MON_JUST_NG_STR, MYF(0), db, table_name); |
3969 | my_afree(db); |
3970 | DBUG_RETURN(ER_SPIDER_LINK_MON_JUST_NG_NUM); |
3971 | } |
3972 | } else { |
3973 | DBUG_PRINT("info" ,("spider link_status = %ld" , |
3974 | share->link_statuses[spider->conn_link_idx[spider->search_link_idx]])); |
3975 | bool search_link_idx_is_checked = FALSE; |
3976 | for ( |
3977 | roop_count = spider_conn_link_idx_next(share->link_statuses, |
3978 | spider->conn_link_idx, -1, share->link_count, |
3979 | SPIDER_LINK_STATUS_RECOVERY); |
3980 | roop_count < (int) share->link_count; |
3981 | roop_count = spider_conn_link_idx_next(share->link_statuses, |
3982 | spider->conn_link_idx, roop_count, share->link_count, |
3983 | SPIDER_LINK_STATUS_RECOVERY) |
3984 | ) { |
3985 | if (roop_count == spider->search_link_idx) |
3986 | search_link_idx_is_checked = TRUE; |
3987 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3988 | if ( |
3989 | !use_conn_kind || |
3990 | spider->conn_kind[roop_count] == SPIDER_CONN_KIND_MYSQL |
3991 | ) { |
3992 | #endif |
3993 | conn = spider->conns[roop_count]; |
3994 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
3995 | } else if (spider->conn_kind[roop_count] == SPIDER_CONN_KIND_HS_READ) |
3996 | { |
3997 | conn = spider->hs_r_conns[roop_count]; |
3998 | } else { |
3999 | conn = spider->hs_w_conns[roop_count]; |
4000 | } |
4001 | #endif |
4002 | |
4003 | if (!conn) |
4004 | { |
4005 | DBUG_PRINT("info" ,("spider get conn %d" , roop_count)); |
4006 | if ( |
4007 | !(conn = |
4008 | spider_get_conn(share, roop_count, |
4009 | spider->conn_keys[roop_count], trx, |
4010 | spider, FALSE, TRUE, |
4011 | use_conn_kind ? spider->conn_kind[roop_count] : |
4012 | SPIDER_CONN_KIND_MYSQL, |
4013 | &error_num)) |
4014 | ) { |
4015 | if ( |
4016 | share->monitoring_kind[roop_count] && |
4017 | spider->need_mons[roop_count] |
4018 | ) { |
4019 | error_num = spider_ping_table_mon_from_table( |
4020 | trx, |
4021 | trx->thd, |
4022 | share, |
4023 | roop_count, |
4024 | (uint32) share->monitoring_sid[roop_count], |
4025 | share->table_name, |
4026 | share->table_name_length, |
4027 | spider->conn_link_idx[roop_count], |
4028 | NULL, |
4029 | 0, |
4030 | share->monitoring_kind[roop_count], |
4031 | share->monitoring_limit[roop_count], |
4032 | share->monitoring_flag[roop_count], |
4033 | TRUE |
4034 | ); |
4035 | } |
4036 | DBUG_PRINT("info" ,("spider get conn error" )); |
4037 | DBUG_RETURN(error_num); |
4038 | } |
4039 | } |
4040 | conn->error_mode &= spider->error_mode; |
4041 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
4042 | #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS |
4043 | if ( |
4044 | spider->do_direct_update && |
4045 | spider_bit_is_set(spider->do_hs_direct_update, roop_count) |
4046 | ) { |
4047 | conn = spider->hs_w_conns[roop_count]; |
4048 | if (!conn) |
4049 | { |
4050 | DBUG_PRINT("info" ,("spider get hs_w_conn %d" , roop_count)); |
4051 | if ( |
4052 | !(conn = |
4053 | spider_get_conn(share, roop_count, |
4054 | spider->conn_keys[roop_count], trx, |
4055 | spider, FALSE, TRUE, |
4056 | SPIDER_CONN_KIND_HS_WRITE, |
4057 | &error_num)) |
4058 | ) { |
4059 | if ( |
4060 | share->monitoring_kind[roop_count] && |
4061 | spider->need_mons[roop_count] |
4062 | ) { |
4063 | error_num = spider_ping_table_mon_from_table( |
4064 | trx, |
4065 | trx->thd, |
4066 | share, |
4067 | roop_count, |
4068 | (uint32) share->monitoring_sid[roop_count], |
4069 | share->table_name, |
4070 | share->table_name_length, |
4071 | spider->conn_link_idx[roop_count], |
4072 | NULL, |
4073 | 0, |
4074 | share->monitoring_kind[roop_count], |
4075 | share->monitoring_limit[roop_count], |
4076 | share->monitoring_flag[roop_count], |
4077 | TRUE |
4078 | ); |
4079 | } |
4080 | DBUG_PRINT("info" ,("spider get conn error" )); |
4081 | DBUG_RETURN(error_num); |
4082 | } |
4083 | } |
4084 | } |
4085 | conn->error_mode &= spider->error_mode; |
4086 | #endif |
4087 | #endif |
4088 | } |
4089 | if (!search_link_idx_is_checked) |
4090 | { |
4091 | TABLE *table = spider->get_table(); |
4092 | TABLE_SHARE *table_share = table->s; |
4093 | char *db = (char *) my_alloca( |
4094 | table_share->db.length + 1 + table_share->table_name.length + 1); |
4095 | if (!db) |
4096 | { |
4097 | my_error(HA_ERR_OUT_OF_MEM, MYF(0)); |
4098 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
4099 | } |
4100 | char *table_name = db + table_share->db.length + 1; |
4101 | memcpy(db, table_share->db.str, table_share->db.length); |
4102 | db[table_share->db.length] = '\0'; |
4103 | memcpy(table_name, table_share->table_name.str, |
4104 | table_share->table_name.length); |
4105 | table_name[table_share->table_name.length] = '\0'; |
4106 | my_printf_error(ER_SPIDER_LINK_MON_JUST_NG_NUM, |
4107 | ER_SPIDER_LINK_MON_JUST_NG_STR, MYF(0), db, table_name); |
4108 | my_afree(db); |
4109 | DBUG_RETURN(ER_SPIDER_LINK_MON_JUST_NG_NUM); |
4110 | } |
4111 | } |
4112 | spider->set_first_link_idx(); |
4113 | DBUG_RETURN(spider_create_trx_ha(trx, spider, trx_ha)); |
4114 | } |
4115 | spider->spider_thread_id = trx->spider_thread_id; |
4116 | DBUG_RETURN(0); |
4117 | } |
4118 | |
4119 | THD *spider_create_tmp_thd() |
4120 | { |
4121 | THD *thd; |
4122 | DBUG_ENTER("spider_create_tmp_thd" ); |
4123 | if (!(thd = SPIDER_new_THD((my_thread_id) 0))) |
4124 | DBUG_RETURN(NULL); |
4125 | #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000 |
4126 | thd->killed = NOT_KILLED; |
4127 | #else |
4128 | thd->killed = THD::NOT_KILLED; |
4129 | #endif |
4130 | #if MYSQL_VERSION_ID < 50500 |
4131 | thd->locked_tables = FALSE; |
4132 | #endif |
4133 | thd->proc_info = "" ; |
4134 | #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100200 |
4135 | #else |
4136 | thd->thread_id = thd->variables.pseudo_thread_id = 0; |
4137 | #endif |
4138 | thd->thread_stack = (char*) &thd; |
4139 | if (thd->store_globals()) |
4140 | DBUG_RETURN(NULL); |
4141 | lex_start(thd); |
4142 | DBUG_RETURN(thd); |
4143 | } |
4144 | |
4145 | void spider_free_tmp_thd( |
4146 | THD *thd |
4147 | ) { |
4148 | DBUG_ENTER("spider_free_tmp_thd" ); |
4149 | thd->cleanup(); |
4150 | #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000 |
4151 | thd->reset_globals(); |
4152 | #else |
4153 | thd->restore_globals(); |
4154 | #endif |
4155 | delete thd; |
4156 | DBUG_VOID_RETURN; |
4157 | } |
4158 | |
4159 | int spider_create_trx_ha( |
4160 | SPIDER_TRX *trx, |
4161 | ha_spider *spider, |
4162 | SPIDER_TRX_HA *trx_ha |
4163 | ) { |
4164 | bool need_create; |
4165 | char *tmp_name; |
4166 | uint *conn_link_idx; |
4167 | uchar *conn_can_fo; |
4168 | SPIDER_SHARE *share = spider->share; |
4169 | DBUG_ENTER("spider_create_trx_ha" ); |
4170 | if (!trx_ha) |
4171 | { |
4172 | DBUG_PRINT("info" ,("spider need create" )); |
4173 | need_create = TRUE; |
4174 | } else if ( |
4175 | trx_ha->share != share || |
4176 | trx_ha->link_count != share->link_count || |
4177 | trx_ha->link_bitmap_size != share->link_bitmap_size |
4178 | ) { |
4179 | DBUG_PRINT("info" ,("spider need recreate" )); |
4180 | need_create = TRUE; |
4181 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
4182 | my_hash_delete_with_hash_value(&trx->trx_ha_hash, |
4183 | share->table_name_hash_value, (uchar*) trx_ha); |
4184 | #else |
4185 | my_hash_delete(&trx->trx_ha_hash, (uchar*) trx_ha); |
4186 | #endif |
4187 | spider_free(trx, trx_ha, MYF(0)); |
4188 | } else { |
4189 | DBUG_PRINT("info" ,("spider use this" )); |
4190 | trx_ha->wait_for_reusing = FALSE; |
4191 | need_create = FALSE; |
4192 | } |
4193 | if (need_create) |
4194 | { |
4195 | if (!(trx_ha = (SPIDER_TRX_HA *) |
4196 | spider_bulk_malloc(spider_current_trx, 58, MYF(MY_WME), |
4197 | &trx_ha, sizeof(SPIDER_TRX_HA), |
4198 | &tmp_name, sizeof(char *) * (share->table_name_length + 1), |
4199 | &conn_link_idx, sizeof(uint) * share->link_count, |
4200 | &conn_can_fo, sizeof(uchar) * share->link_bitmap_size, |
4201 | NullS)) |
4202 | ) { |
4203 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
4204 | } |
4205 | trx_ha->table_name = tmp_name; |
4206 | memcpy(trx_ha->table_name, share->table_name, share->table_name_length); |
4207 | trx_ha->table_name[share->table_name_length] = '\0'; |
4208 | trx_ha->table_name_length = share->table_name_length; |
4209 | trx_ha->trx = trx; |
4210 | trx_ha->share = share; |
4211 | trx_ha->link_count = share->link_count; |
4212 | trx_ha->link_bitmap_size = share->link_bitmap_size; |
4213 | trx_ha->conn_link_idx = conn_link_idx; |
4214 | trx_ha->conn_can_fo = conn_can_fo; |
4215 | trx_ha->wait_for_reusing = FALSE; |
4216 | uint old_elements = trx->trx_ha_hash.array.max_element; |
4217 | #ifdef HASH_UPDATE_WITH_HASH_VALUE |
4218 | if (my_hash_insert_with_hash_value(&trx->trx_ha_hash, |
4219 | share->table_name_hash_value, (uchar*) trx_ha)) |
4220 | #else |
4221 | if (my_hash_insert(&trx->trx_ha_hash, (uchar*) trx_ha)) |
4222 | #endif |
4223 | { |
4224 | spider_free(trx, trx_ha, MYF(0)); |
4225 | DBUG_RETURN(HA_ERR_OUT_OF_MEM); |
4226 | } |
4227 | if (trx->trx_ha_hash.array.max_element > old_elements) |
4228 | { |
4229 | spider_alloc_calc_mem(spider_current_trx, |
4230 | trx->trx_ha_hash, |
4231 | (trx->trx_ha_hash.array.max_element - old_elements) * |
4232 | trx->trx_ha_hash.array.size_of_element); |
4233 | } |
4234 | } |
4235 | memcpy(trx_ha->conn_link_idx, spider->conn_link_idx, |
4236 | sizeof(uint) * share->link_count); |
4237 | memcpy(trx_ha->conn_can_fo, spider->conn_can_fo, |
4238 | sizeof(uint) * share->link_bitmap_size); |
4239 | DBUG_RETURN(0); |
4240 | } |
4241 | |
4242 | SPIDER_TRX_HA *spider_check_trx_ha( |
4243 | SPIDER_TRX *trx, |
4244 | ha_spider *spider |
4245 | ) { |
4246 | SPIDER_TRX_HA *trx_ha; |
4247 | SPIDER_SHARE *share = spider->share; |
4248 | DBUG_ENTER("spider_check_trx_ha" ); |
4249 | #ifdef SPIDER_HAS_HASH_VALUE_TYPE |
4250 | if ((trx_ha = (SPIDER_TRX_HA *) my_hash_search_using_hash_value( |
4251 | &trx->trx_ha_hash, share->table_name_hash_value, |
4252 | (uchar*) share->table_name, share->table_name_length))) |
4253 | #else |
4254 | if ((trx_ha = (SPIDER_TRX_HA *) my_hash_search(&trx->trx_ha_hash, |
4255 | (uchar*) share->table_name, share->table_name_length))) |
4256 | #endif |
4257 | { |
4258 | memcpy(spider->conn_link_idx, trx_ha->conn_link_idx, |
4259 | sizeof(uint) * share->link_count); |
4260 | memcpy(spider->conn_can_fo, trx_ha->conn_can_fo, |
4261 | sizeof(uint) * share->link_bitmap_size); |
4262 | DBUG_RETURN(trx_ha); |
4263 | } |
4264 | DBUG_RETURN(NULL); |
4265 | } |
4266 | |
4267 | void spider_free_trx_ha( |
4268 | SPIDER_TRX *trx |
4269 | ) { |
4270 | ulong roop_count; |
4271 | SPIDER_TRX_HA *trx_ha; |
4272 | DBUG_ENTER("spider_free_trx_ha" ); |
4273 | for (roop_count = 0; roop_count < trx->trx_ha_hash.records; roop_count++) |
4274 | { |
4275 | trx_ha = (SPIDER_TRX_HA *) my_hash_element(&trx->trx_ha_hash, roop_count); |
4276 | spider_free(spider_current_trx, trx_ha, MYF(0)); |
4277 | } |
4278 | my_hash_reset(&trx->trx_ha_hash); |
4279 | DBUG_VOID_RETURN; |
4280 | } |
4281 | |
4282 | void spider_reuse_trx_ha( |
4283 | SPIDER_TRX *trx |
4284 | ) { |
4285 | ulong roop_count; |
4286 | SPIDER_TRX_HA *trx_ha; |
4287 | DBUG_ENTER("spider_reuse_trx_ha" ); |
4288 | if (trx->trx_ha_reuse_count < 10000) |
4289 | { |
4290 | trx->trx_ha_reuse_count++; |
4291 | for (roop_count = 0; roop_count < trx->trx_ha_hash.records; roop_count++) |
4292 | { |
4293 | trx_ha = (SPIDER_TRX_HA *) my_hash_element(&trx->trx_ha_hash, |
4294 | roop_count); |
4295 | trx_ha->wait_for_reusing = TRUE; |
4296 | } |
4297 | } else { |
4298 | trx->trx_ha_reuse_count = 0; |
4299 | spider_free_trx_ha(trx); |
4300 | } |
4301 | DBUG_VOID_RETURN; |
4302 | } |
4303 | |
4304 | void spider_trx_set_link_idx_for_all( |
4305 | ha_spider *spider |
4306 | ) { |
4307 | int roop_count, roop_count2; |
4308 | SPIDER_SHARE *share = spider->share; |
4309 | long *link_statuses = share->link_statuses; |
4310 | uint *conn_link_idx = spider->conn_link_idx; |
4311 | int link_count = share->link_count; |
4312 | int all_link_count = share->all_link_count; |
4313 | uchar *conn_can_fo = spider->conn_can_fo; |
4314 | DBUG_ENTER("spider_trx_set_link_idx_for_all" ); |
4315 | DBUG_PRINT("info" ,("spider set link_count=%d" , link_count)); |
4316 | DBUG_PRINT("info" ,("spider set all_link_count=%d" , all_link_count)); |
4317 | memset(conn_can_fo, 0, sizeof(uchar) * share->link_bitmap_size); |
4318 | for (roop_count = 0; roop_count < link_count; roop_count++) |
4319 | { |
4320 | for (roop_count2 = roop_count; roop_count2 < all_link_count; |
4321 | roop_count2 += link_count) |
4322 | { |
4323 | if (link_statuses[roop_count2] <= SPIDER_LINK_STATUS_RECOVERY) |
4324 | break; |
4325 | } |
4326 | if (roop_count2 < all_link_count) |
4327 | { |
4328 | conn_link_idx[roop_count] = roop_count2; |
4329 | if (roop_count2 + link_count < all_link_count) |
4330 | spider_set_bit(conn_can_fo, roop_count); |
4331 | DBUG_PRINT("info" ,("spider set conn_link_idx[%d]=%d" , |
4332 | roop_count, roop_count2)); |
4333 | } else { |
4334 | conn_link_idx[roop_count] = roop_count; |
4335 | DBUG_PRINT("info" ,("spider set2 conn_link_idx[%d]=%d" , |
4336 | roop_count, roop_count)); |
4337 | } |
4338 | spider->conn_keys[roop_count] = |
4339 | ADD_TO_PTR(spider->conn_keys_first_ptr, |
4340 | PTR_BYTE_DIFF(share->conn_keys[conn_link_idx[roop_count]], |
4341 | share->conn_keys[0]), char*); |
4342 | DBUG_PRINT("info" ,("spider conn_keys[%d]=%s" , |
4343 | roop_count, spider->conn_keys[roop_count])); |
4344 | #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET) |
4345 | spider->hs_r_conn_keys[roop_count] = |
4346 | ADD_TO_PTR(spider->conn_keys_first_ptr, |
4347 | PTR_BYTE_DIFF(share->hs_read_conn_keys[conn_link_idx[roop_count]], |
4348 | share->conn_keys[0]), char*); |
4349 | DBUG_PRINT("info" ,("spider hs_r_conn_keys[%d]=%s" , |
4350 | roop_count, spider->hs_r_conn_keys[roop_count])); |
4351 | spider->hs_w_conn_keys[roop_count] = |
4352 | ADD_TO_PTR(spider->conn_keys_first_ptr, |
4353 | PTR_BYTE_DIFF(share->hs_write_conn_keys[conn_link_idx[roop_count]], |
4354 | share->conn_keys[0]), char*); |
4355 | DBUG_PRINT("info" ,("spider hs_w_conn_keys[%d]=%s" , |
4356 | roop_count, spider->hs_w_conn_keys[roop_count])); |
4357 | #endif |
4358 | } |
4359 | DBUG_VOID_RETURN; |
4360 | } |
4361 | |
4362 | int spider_trx_check_link_idx_failed( |
4363 | ha_spider *spider |
4364 | ) { |
4365 | int roop_count; |
4366 | SPIDER_SHARE *share = spider->share; |
4367 | long *link_statuses = share->link_statuses; |
4368 | uint *conn_link_idx = spider->conn_link_idx; |
4369 | int link_count = share->link_count; |
4370 | uchar *conn_can_fo = spider->conn_can_fo; |
4371 | DBUG_ENTER("spider_trx_check_link_idx_failed" ); |
4372 | for (roop_count = 0; roop_count < link_count; roop_count++) |
4373 | { |
4374 | if ( |
4375 | link_statuses[conn_link_idx[roop_count]] == SPIDER_LINK_STATUS_NG && |
4376 | spider_bit_is_set(conn_can_fo, roop_count) |
4377 | ) { |
4378 | my_message(ER_SPIDER_LINK_IS_FAILOVER_NUM, |
4379 | ER_SPIDER_LINK_IS_FAILOVER_STR, MYF(0)); |
4380 | DBUG_RETURN(ER_SPIDER_LINK_IS_FAILOVER_NUM); |
4381 | } |
4382 | } |
4383 | DBUG_RETURN(0); |
4384 | } |
4385 | |
4386 | #ifdef HA_CAN_BULK_ACCESS |
4387 | void spider_trx_add_bulk_access_conn( |
4388 | SPIDER_TRX *trx, |
4389 | SPIDER_CONN *conn |
4390 | ) { |
4391 | DBUG_ENTER("spider_trx_add_bulk_access_conn" ); |
4392 | DBUG_PRINT("info" ,("spider trx=%p" , trx)); |
4393 | DBUG_PRINT("info" ,("spider conn=%p" , conn)); |
4394 | DBUG_PRINT("info" ,("spider conn->bulk_access_requests=%u" , |
4395 | conn->bulk_access_requests)); |
4396 | DBUG_PRINT("info" ,("spider conn->bulk_access_sended=%u" , |
4397 | conn->bulk_access_sended)); |
4398 | DBUG_PRINT("info" ,("spider trx->bulk_access_conn_first=%p" , |
4399 | trx->bulk_access_conn_first)); |
4400 | if (!conn->bulk_access_requests && !conn->bulk_access_sended) |
4401 | { |
4402 | if (!trx->bulk_access_conn_first) |
4403 | { |
4404 | trx->bulk_access_conn_first = conn; |
4405 | } else { |
4406 | trx->bulk_access_conn_last->bulk_access_next = conn; |
4407 | } |
4408 | trx->bulk_access_conn_last = conn; |
4409 | conn->bulk_access_next = NULL; |
4410 | } |
4411 | conn->bulk_access_requests++; |
4412 | DBUG_PRINT("info" ,("spider conn->bulk_access_requests=%u" , |
4413 | conn->bulk_access_requests)); |
4414 | DBUG_VOID_RETURN; |
4415 | } |
4416 | #endif |
4417 | |