1 /* Copyright (C) 2008-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 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 "my_getopt.h"
27 #include "sql_class.h"
28 #include "sql_partition.h"
29 #include "sql_servers.h"
30 #include "sql_select.h"
31 #include "tztime.h"
32 #endif
33 #include "spd_err.h"
34 #include "spd_param.h"
35 #include "spd_db_include.h"
36 #include "spd_include.h"
37 #include "spd_sys_table.h"
38 #include "ha_spider.h"
39 #include "spd_trx.h"
40 #include "spd_db_conn.h"
41 #include "spd_table.h"
42 #include "spd_conn.h"
43 #include "spd_ping_table.h"
44 #include "spd_direct_sql.h"
45 #include "spd_malloc.h"
46 #include "spd_group_by_handler.h"
47 
48 /* Background thread management */
49 #ifdef SPIDER_HAS_NEXT_THREAD_ID
50 #define SPIDER_set_next_thread_id(A)
51 MYSQL_THD create_thd();
52 void destroy_thd(MYSQL_THD thd);
53 #else
54 ulong *spd_db_att_thread_id;
SPIDER_set_next_thread_id(THD * A)55 inline void SPIDER_set_next_thread_id(THD *A)
56 {
57   pthread_mutex_lock(&LOCK_thread_count);
58   A->thread_id = (*spd_db_att_thread_id)++;
59   pthread_mutex_unlock(&LOCK_thread_count);
60 }
create_thd()61 MYSQL_THD create_thd()
62 {
63   THD *thd = SPIDER_new_THD(next_thread_id());
64   if (thd)
65   {
66     thd->thread_stack = (char*) &thd;
67     thd->store_globals();
68     thd->set_command(COM_DAEMON);
69     thd->security_ctx->host_or_ip = "";
70   }
71   return thd;
72 }
destroy_thd(MYSQL_THD thd)73 void destroy_thd(MYSQL_THD thd)
74 {
75   delete thd;
76 }
77 #endif
spider_create_sys_thd(SPIDER_THREAD * thread)78 inline MYSQL_THD spider_create_sys_thd(SPIDER_THREAD *thread)
79 {
80   THD *thd = create_thd();
81   if (thd)
82   {
83     SPIDER_set_next_thread_id(thd);
84     thd->mysys_var->current_cond = &thread->cond;
85     thd->mysys_var->current_mutex = &thread->mutex;
86   }
87   return thd;
88 }
spider_destroy_sys_thd(MYSQL_THD thd)89 inline void spider_destroy_sys_thd(MYSQL_THD thd)
90 {
91   destroy_thd(thd);
92 }
spider_create_thd()93 inline MYSQL_THD spider_create_thd()
94 {
95   THD *thd;
96   my_thread_init();
97   if (!(thd = new THD(next_thread_id())))
98     my_thread_end();
99   else
100   {
101 #ifdef HAVE_PSI_INTERFACE
102     mysql_thread_set_psi_id(thd->thread_id);
103 #endif
104     thd->thread_stack = (char *) &thd;
105     thd->store_globals();
106   }
107   return thd;
108 }
spider_destroy_thd(MYSQL_THD thd)109 inline void spider_destroy_thd(MYSQL_THD thd)
110 {
111   delete thd;
112 }
113 
114 #ifdef SPIDER_XID_USES_xid_cache_iterate
115 #else
116 #ifdef XID_CACHE_IS_SPLITTED
117 uint *spd_db_att_xid_cache_split_num;
118 #endif
119 pthread_mutex_t *spd_db_att_LOCK_xid_cache;
120 HASH *spd_db_att_xid_cache;
121 #endif
122 struct charset_info_st *spd_charset_utf8_bin;
123 const char **spd_defaults_extra_file;
124 const char **spd_defaults_file;
125 bool volatile *spd_abort_loop;
126 Time_zone *spd_tz_system;
127 extern long spider_conn_mutex_id;
128 handlerton *spider_hton_ptr;
129 SPIDER_DBTON spider_dbton[SPIDER_DBTON_SIZE];
130 extern SPIDER_DBTON spider_dbton_mysql;
131 extern SPIDER_DBTON spider_dbton_mariadb;
132 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
133 extern SPIDER_DBTON spider_dbton_handlersocket;
134 #endif
135 #ifdef HAVE_ORACLE_OCI
136 extern SPIDER_DBTON spider_dbton_oracle;
137 #endif
138 #ifndef WITHOUT_SPIDER_BG_SEARCH
139 SPIDER_THREAD *spider_table_sts_threads;
140 SPIDER_THREAD *spider_table_crd_threads;
141 #endif
142 
143 #ifdef HAVE_PSI_INTERFACE
144 PSI_mutex_key spd_key_mutex_tbl;
145 PSI_mutex_key spd_key_mutex_init_error_tbl;
146 #ifdef WITH_PARTITION_STORAGE_ENGINE
147 PSI_mutex_key spd_key_mutex_pt_share;
148 #endif
149 PSI_mutex_key spd_key_mutex_lgtm_tblhnd_share;
150 PSI_mutex_key spd_key_mutex_conn;
151 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
152 PSI_mutex_key spd_key_mutex_hs_r_conn;
153 PSI_mutex_key spd_key_mutex_hs_w_conn;
154 #endif
155 PSI_mutex_key spd_key_mutex_open_conn;
156 PSI_mutex_key spd_key_mutex_allocated_thds;
157 PSI_mutex_key spd_key_mutex_mon_table_cache;
158 PSI_mutex_key spd_key_mutex_udf_table_mon;
159 PSI_mutex_key spd_key_mutex_mta_conn;
160 #ifndef WITHOUT_SPIDER_BG_SEARCH
161 PSI_mutex_key spd_key_mutex_bg_conn_chain;
162 PSI_mutex_key spd_key_mutex_bg_conn_sync;
163 PSI_mutex_key spd_key_mutex_bg_conn;
164 PSI_mutex_key spd_key_mutex_bg_job_stack;
165 PSI_mutex_key spd_key_mutex_bg_mon;
166 PSI_mutex_key spd_key_mutex_bg_direct_sql;
167 #endif
168 PSI_mutex_key spd_key_mutex_mon_list_caller;
169 PSI_mutex_key spd_key_mutex_mon_list_receptor;
170 PSI_mutex_key spd_key_mutex_mon_list_monitor;
171 PSI_mutex_key spd_key_mutex_mon_list_update_status;
172 PSI_mutex_key spd_key_mutex_share;
173 PSI_mutex_key spd_key_mutex_share_sts;
174 PSI_mutex_key spd_key_mutex_share_crd;
175 PSI_mutex_key spd_key_mutex_share_auto_increment;
176 #ifdef WITH_PARTITION_STORAGE_ENGINE
177 PSI_mutex_key spd_key_mutex_pt_share_sts;
178 PSI_mutex_key spd_key_mutex_pt_share_crd;
179 PSI_mutex_key spd_key_mutex_pt_handler;
180 #endif
181 PSI_mutex_key spd_key_mutex_udf_table;
182 PSI_mutex_key spd_key_mutex_mem_calc;
183 PSI_mutex_key spd_key_thread_id;
184 PSI_mutex_key spd_key_conn_id;
185 PSI_mutex_key spd_key_mutex_ipport_count;
186 PSI_mutex_key spd_key_mutex_conn_i;
187 #ifndef WITHOUT_SPIDER_BG_SEARCH
188 PSI_mutex_key spd_key_mutex_bg_stss;
189 PSI_mutex_key spd_key_mutex_bg_crds;
190 #endif
191 
192 static PSI_mutex_info all_spider_mutexes[]=
193 {
194   { &spd_key_mutex_tbl, "tbl", PSI_FLAG_GLOBAL},
195   { &spd_key_mutex_init_error_tbl, "init_error_tbl", PSI_FLAG_GLOBAL},
196 #ifdef WITH_PARTITION_STORAGE_ENGINE
197   { &spd_key_mutex_pt_share, "pt_share", PSI_FLAG_GLOBAL},
198 #endif
199   { &spd_key_mutex_lgtm_tblhnd_share, "lgtm_tblhnd_share", PSI_FLAG_GLOBAL},
200   { &spd_key_mutex_conn, "conn", PSI_FLAG_GLOBAL},
201 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
202   { &spd_key_mutex_hs_r_conn, "hs_r_conn", PSI_FLAG_GLOBAL},
203   { &spd_key_mutex_hs_w_conn, "hs_w_conn", PSI_FLAG_GLOBAL},
204 #endif
205   { &spd_key_mutex_open_conn, "open_conn", PSI_FLAG_GLOBAL},
206   { &spd_key_mutex_allocated_thds, "allocated_thds", PSI_FLAG_GLOBAL},
207   { &spd_key_mutex_mon_table_cache, "mon_table_cache", PSI_FLAG_GLOBAL},
208   { &spd_key_mutex_udf_table_mon, "udf_table_mon", PSI_FLAG_GLOBAL},
209   { &spd_key_mutex_mem_calc, "mem_calc", PSI_FLAG_GLOBAL},
210   { &spd_key_thread_id, "thread_id", PSI_FLAG_GLOBAL},
211   { &spd_key_conn_id, "conn_id", PSI_FLAG_GLOBAL},
212   { &spd_key_mutex_ipport_count, "ipport_count", PSI_FLAG_GLOBAL},
213 #ifndef WITHOUT_SPIDER_BG_SEARCH
214   { &spd_key_mutex_bg_stss, "bg_stss", PSI_FLAG_GLOBAL},
215   { &spd_key_mutex_bg_crds, "bg_crds", PSI_FLAG_GLOBAL},
216 #endif
217   { &spd_key_mutex_conn_i, "conn_i", 0},
218   { &spd_key_mutex_mta_conn, "mta_conn", 0},
219 #ifndef WITHOUT_SPIDER_BG_SEARCH
220   { &spd_key_mutex_bg_conn_chain, "bg_conn_chain", 0},
221   { &spd_key_mutex_bg_conn_sync, "bg_conn_sync", 0},
222   { &spd_key_mutex_bg_conn, "bg_conn", 0},
223   { &spd_key_mutex_bg_job_stack, "bg_job_stack", 0},
224   { &spd_key_mutex_bg_mon, "bg_mon", 0},
225   { &spd_key_mutex_bg_direct_sql, "bg_direct_sql", 0},
226 #endif
227   { &spd_key_mutex_mon_list_caller, "mon_list_caller", 0},
228   { &spd_key_mutex_mon_list_receptor, "mon_list_receptor", 0},
229   { &spd_key_mutex_mon_list_monitor, "mon_list_monitor", 0},
230   { &spd_key_mutex_mon_list_update_status, "mon_list_update_status", 0},
231   { &spd_key_mutex_share, "share", 0},
232   { &spd_key_mutex_share_sts, "share_sts", 0},
233   { &spd_key_mutex_share_crd, "share_crd", 0},
234   { &spd_key_mutex_share_auto_increment, "share_auto_increment", 0},
235 #ifdef WITH_PARTITION_STORAGE_ENGINE
236   { &spd_key_mutex_pt_share_sts, "pt_share_sts", 0},
237   { &spd_key_mutex_pt_share_crd, "pt_share_crd", 0},
238   { &spd_key_mutex_pt_handler, "pt_handler", 0},
239 #endif
240   { &spd_key_mutex_udf_table, "udf_table", 0},
241 };
242 
243 #ifndef WITHOUT_SPIDER_BG_SEARCH
244 PSI_cond_key spd_key_cond_bg_conn_sync;
245 PSI_cond_key spd_key_cond_bg_conn;
246 PSI_cond_key spd_key_cond_bg_sts;
247 PSI_cond_key spd_key_cond_bg_sts_sync;
248 PSI_cond_key spd_key_cond_bg_crd;
249 PSI_cond_key spd_key_cond_bg_crd_sync;
250 PSI_cond_key spd_key_cond_bg_mon;
251 PSI_cond_key spd_key_cond_bg_mon_sleep;
252 PSI_cond_key spd_key_cond_bg_direct_sql;
253 #endif
254 PSI_cond_key spd_key_cond_udf_table_mon;
255 PSI_cond_key spd_key_cond_conn_i;
256 #ifndef WITHOUT_SPIDER_BG_SEARCH
257 PSI_cond_key spd_key_cond_bg_stss;
258 PSI_cond_key spd_key_cond_bg_sts_syncs;
259 PSI_cond_key spd_key_cond_bg_crds;
260 PSI_cond_key spd_key_cond_bg_crd_syncs;
261 #endif
262 
263 static PSI_cond_info all_spider_conds[] = {
264 #ifndef WITHOUT_SPIDER_BG_SEARCH
265   {&spd_key_cond_bg_conn_sync, "bg_conn_sync", 0},
266   {&spd_key_cond_bg_conn, "bg_conn", 0},
267   {&spd_key_cond_bg_sts, "bg_sts", 0},
268   {&spd_key_cond_bg_sts_sync, "bg_sts_sync", 0},
269   {&spd_key_cond_bg_crd, "bg_crd", 0},
270   {&spd_key_cond_bg_crd_sync, "bg_crd_sync", 0},
271   {&spd_key_cond_bg_mon, "bg_mon", 0},
272   {&spd_key_cond_bg_mon_sleep, "bg_mon_sleep", 0},
273   {&spd_key_cond_bg_direct_sql, "bg_direct_sql", 0},
274 #endif
275   {&spd_key_cond_udf_table_mon, "udf_table_mon", 0},
276   {&spd_key_cond_conn_i, "conn_i", 0},
277 #ifndef WITHOUT_SPIDER_BG_SEARCH
278   {&spd_key_cond_bg_stss, "bg_stss", 0},
279   {&spd_key_cond_bg_sts_syncs, "bg_sts_syncs", 0},
280   {&spd_key_cond_bg_crds, "bg_crds", 0},
281   {&spd_key_cond_bg_crd_syncs, "bg_crd_syncs", 0},
282 #endif
283 };
284 
285 #ifndef WITHOUT_SPIDER_BG_SEARCH
286 PSI_thread_key spd_key_thd_bg;
287 PSI_thread_key spd_key_thd_bg_sts;
288 PSI_thread_key spd_key_thd_bg_crd;
289 PSI_thread_key spd_key_thd_bg_mon;
290 PSI_thread_key spd_key_thd_bg_stss;
291 PSI_thread_key spd_key_thd_bg_crds;
292 #endif
293 
294 static PSI_thread_info all_spider_threads[] = {
295 #ifndef WITHOUT_SPIDER_BG_SEARCH
296   {&spd_key_thd_bg, "bg", 0},
297   {&spd_key_thd_bg_sts, "bg_sts", 0},
298   {&spd_key_thd_bg_crd, "bg_crd", 0},
299   {&spd_key_thd_bg_mon, "bg_mon", 0},
300   {&spd_key_thd_bg_stss, "bg_stss", 0},
301   {&spd_key_thd_bg_crds, "bg_crds", 0},
302 #endif
303 };
304 #endif
305 
306 extern HASH spider_open_connections;
307 extern HASH spider_ipport_conns;
308 extern uint spider_open_connections_id;
309 extern const char *spider_open_connections_func_name;
310 extern const char *spider_open_connections_file_name;
311 extern ulong spider_open_connections_line_no;
312 extern pthread_mutex_t spider_conn_mutex;
313 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
314 extern HASH spider_hs_r_conn_hash;
315 extern uint spider_hs_r_conn_hash_id;
316 extern const char *spider_hs_r_conn_hash_func_name;
317 extern const char *spider_hs_r_conn_hash_file_name;
318 extern ulong spider_hs_r_conn_hash_line_no;
319 extern pthread_mutex_t spider_hs_r_conn_mutex;
320 extern HASH spider_hs_w_conn_hash;
321 extern uint spider_hs_w_conn_hash_id;
322 extern const char *spider_hs_w_conn_hash_func_name;
323 extern const char *spider_hs_w_conn_hash_file_name;
324 extern ulong spider_hs_w_conn_hash_line_no;
325 extern pthread_mutex_t spider_hs_w_conn_mutex;
326 #endif
327 extern HASH *spider_udf_table_mon_list_hash;
328 extern uint spider_udf_table_mon_list_hash_id;
329 extern const char *spider_udf_table_mon_list_hash_func_name;
330 extern const char *spider_udf_table_mon_list_hash_file_name;
331 extern ulong spider_udf_table_mon_list_hash_line_no;
332 extern pthread_mutex_t *spider_udf_table_mon_mutexes;
333 extern pthread_cond_t *spider_udf_table_mon_conds;
334 extern pthread_mutex_t spider_open_conn_mutex;
335 extern pthread_mutex_t spider_mon_table_cache_mutex;
336 extern DYNAMIC_ARRAY spider_mon_table_cache;
337 extern uint spider_mon_table_cache_id;
338 extern const char *spider_mon_table_cache_func_name;
339 extern const char *spider_mon_table_cache_file_name;
340 extern ulong spider_mon_table_cache_line_no;
341 
342 HASH spider_open_tables;
343 uint spider_open_tables_id;
344 const char *spider_open_tables_func_name;
345 const char *spider_open_tables_file_name;
346 ulong spider_open_tables_line_no;
347 pthread_mutex_t spider_tbl_mutex;
348 HASH spider_init_error_tables;
349 uint spider_init_error_tables_id;
350 const char *spider_init_error_tables_func_name;
351 const char *spider_init_error_tables_file_name;
352 ulong spider_init_error_tables_line_no;
353 pthread_mutex_t spider_init_error_tbl_mutex;
354 
355 extern pthread_mutex_t spider_thread_id_mutex;
356 extern pthread_mutex_t spider_conn_id_mutex;
357 extern pthread_mutex_t spider_ipport_conn_mutex;
358 
359 #ifdef WITH_PARTITION_STORAGE_ENGINE
360 HASH spider_open_pt_share;
361 uint spider_open_pt_share_id;
362 const char *spider_open_pt_share_func_name;
363 const char *spider_open_pt_share_file_name;
364 ulong spider_open_pt_share_line_no;
365 pthread_mutex_t spider_pt_share_mutex;
366 #endif
367 
368 HASH spider_lgtm_tblhnd_share_hash;
369 uint spider_lgtm_tblhnd_share_hash_id;
370 const char *spider_lgtm_tblhnd_share_hash_func_name;
371 const char *spider_lgtm_tblhnd_share_hash_file_name;
372 ulong spider_lgtm_tblhnd_share_hash_line_no;
373 pthread_mutex_t spider_lgtm_tblhnd_share_mutex;
374 
375 HASH spider_allocated_thds;
376 uint spider_allocated_thds_id;
377 const char *spider_allocated_thds_func_name;
378 const char *spider_allocated_thds_file_name;
379 ulong spider_allocated_thds_line_no;
380 pthread_mutex_t spider_allocated_thds_mutex;
381 
382 #ifndef WITHOUT_SPIDER_BG_SEARCH
383 pthread_attr_t spider_pt_attr;
384 #endif
385 
386 extern pthread_mutex_t spider_mem_calc_mutex;
387 
388 extern const char *spider_alloc_func_name[SPIDER_MEM_CALC_LIST_NUM];
389 extern const char *spider_alloc_file_name[SPIDER_MEM_CALC_LIST_NUM];
390 extern ulong      spider_alloc_line_no[SPIDER_MEM_CALC_LIST_NUM];
391 extern ulonglong  spider_total_alloc_mem[SPIDER_MEM_CALC_LIST_NUM];
392 extern longlong   spider_current_alloc_mem[SPIDER_MEM_CALC_LIST_NUM];
393 extern ulonglong  spider_alloc_mem_count[SPIDER_MEM_CALC_LIST_NUM];
394 extern ulonglong  spider_free_mem_count[SPIDER_MEM_CALC_LIST_NUM];
395 
396 static char spider_wild_many = '%', spider_wild_one = '_',
397   spider_wild_prefix='\\';
398 
399 // for spider_open_tables
spider_tbl_get_key(SPIDER_SHARE * share,size_t * length,my_bool not_used)400 uchar *spider_tbl_get_key(
401   SPIDER_SHARE *share,
402   size_t *length,
403   my_bool not_used __attribute__ ((unused))
404 ) {
405   DBUG_ENTER("spider_tbl_get_key");
406   *length = share->table_name_length;
407   DBUG_RETURN((uchar*) share->table_name);
408 }
409 
410 #ifdef WITH_PARTITION_STORAGE_ENGINE
spider_pt_share_get_key(SPIDER_PARTITION_SHARE * share,size_t * length,my_bool not_used)411 uchar *spider_pt_share_get_key(
412   SPIDER_PARTITION_SHARE *share,
413   size_t *length,
414   my_bool not_used __attribute__ ((unused))
415 ) {
416   DBUG_ENTER("spider_pt_share_get_key");
417   *length = share->table_name_length;
418   DBUG_RETURN((uchar*) share->table_name);
419 }
420 
spider_pt_handler_share_get_key(SPIDER_PARTITION_HANDLER_SHARE * share,size_t * length,my_bool not_used)421 uchar *spider_pt_handler_share_get_key(
422   SPIDER_PARTITION_HANDLER_SHARE *share,
423   size_t *length,
424   my_bool not_used __attribute__ ((unused))
425 ) {
426   DBUG_ENTER("spider_pt_handler_share_get_key");
427   *length = sizeof(TABLE *);
428   DBUG_RETURN((uchar*) &share->table);
429 }
430 #endif
431 
spider_lgtm_tblhnd_share_hash_get_key(SPIDER_LGTM_TBLHND_SHARE * share,size_t * length,my_bool not_used)432 uchar *spider_lgtm_tblhnd_share_hash_get_key(
433   SPIDER_LGTM_TBLHND_SHARE *share,
434   size_t *length,
435   my_bool not_used __attribute__ ((unused))
436 ) {
437   DBUG_ENTER("spider_lgtm_tblhnd_share_hash_get_key");
438   *length = share->table_name_length;
439   DBUG_RETURN((uchar*) share->table_name);
440 }
441 
spider_link_get_key(SPIDER_LINK_FOR_HASH * link_for_hash,size_t * length,my_bool not_used)442 uchar *spider_link_get_key(
443   SPIDER_LINK_FOR_HASH *link_for_hash,
444   size_t *length,
445   my_bool not_used __attribute__ ((unused))
446 ) {
447   DBUG_ENTER("spider_link_get_key");
448   *length = link_for_hash->db_table_str->length();
449   DBUG_RETURN((uchar*) link_for_hash->db_table_str->ptr());
450 }
451 
spider_ha_get_key(ha_spider * spider,size_t * length,my_bool not_used)452 uchar *spider_ha_get_key(
453   ha_spider *spider,
454   size_t *length,
455   my_bool not_used __attribute__ ((unused))
456 ) {
457   DBUG_ENTER("spider_ha_get_key");
458   *length = spider->share->table_name_length;
459   DBUG_RETURN((uchar*) spider->share->table_name);
460 }
461 
spider_udf_tbl_mon_list_key(SPIDER_TABLE_MON_LIST * table_mon_list,size_t * length,my_bool not_used)462 uchar *spider_udf_tbl_mon_list_key(
463   SPIDER_TABLE_MON_LIST *table_mon_list,
464   size_t *length,
465   my_bool not_used __attribute__ ((unused))
466 ) {
467   DBUG_ENTER("spider_udf_tbl_mon_list_key");
468   DBUG_PRINT("info",("spider hash key=%s", table_mon_list->key));
469   DBUG_PRINT("info",("spider hash key length=%u", table_mon_list->key_length));
470   *length = table_mon_list->key_length;
471   DBUG_RETURN((uchar*) table_mon_list->key);
472 }
473 
spider_allocated_thds_get_key(THD * thd,size_t * length,my_bool not_used)474 uchar *spider_allocated_thds_get_key(
475   THD *thd,
476   size_t *length,
477   my_bool not_used __attribute__ ((unused))
478 ) {
479   DBUG_ENTER("spider_allocated_thds_get_key");
480   *length = sizeof(THD *);
481   DBUG_RETURN((uchar*) thd);
482 }
483 
484 #ifdef HAVE_PSI_INTERFACE
init_spider_psi_keys()485 static void init_spider_psi_keys()
486 {
487   DBUG_ENTER("init_spider_psi_keys");
488   if (PSI_server == NULL)
489     DBUG_VOID_RETURN;
490 
491   PSI_server->register_mutex("spider", all_spider_mutexes,
492     array_elements(all_spider_mutexes));
493   PSI_server->register_cond("spider", all_spider_conds,
494     array_elements(all_spider_conds));
495   PSI_server->register_thread("spider", all_spider_threads,
496     array_elements(all_spider_threads));
497   DBUG_VOID_RETURN;
498 }
499 #endif
500 
spider_get_server(SPIDER_SHARE * share,int link_idx)501 int spider_get_server(
502   SPIDER_SHARE *share,
503   int link_idx
504 ) {
505   MEM_ROOT mem_root;
506   int error_num, length;
507   FOREIGN_SERVER *server, server_buf;
508   DBUG_ENTER("spider_get_server");
509   SPD_INIT_ALLOC_ROOT(&mem_root, 128, 0, MYF(MY_WME));
510 
511   if (!(server
512        = get_server_by_name(&mem_root, share->server_names[link_idx],
513          &server_buf)))
514   {
515     error_num = ER_FOREIGN_SERVER_DOESNT_EXIST;
516     goto error;
517   }
518 
519   if (!share->tgt_wrappers[link_idx] && server->scheme)
520   {
521     share->tgt_wrappers_lengths[link_idx] = strlen(server->scheme);
522     if (!(share->tgt_wrappers[link_idx] =
523       spider_create_string(server->scheme,
524       share->tgt_wrappers_lengths[link_idx])))
525     {
526       error_num = HA_ERR_OUT_OF_MEM;
527       goto error;
528     }
529     DBUG_PRINT("info",("spider tgt_wrappers=%s",
530       share->tgt_wrappers[link_idx]));
531   }
532 
533   if (!share->tgt_hosts[link_idx] && server->host)
534   {
535     share->tgt_hosts_lengths[link_idx] = strlen(server->host);
536     if (!(share->tgt_hosts[link_idx] =
537       spider_create_string(server->host, share->tgt_hosts_lengths[link_idx])))
538     {
539       error_num = HA_ERR_OUT_OF_MEM;
540       goto error;
541     }
542     DBUG_PRINT("info",("spider tgt_hosts=%s", share->tgt_hosts[link_idx]));
543   }
544 
545   if (share->tgt_ports[link_idx] == -1)
546   {
547     share->tgt_ports[link_idx] = server->port;
548     DBUG_PRINT("info",("spider tgt_ports=%ld", share->tgt_ports[link_idx]));
549   }
550 
551   if (!share->tgt_sockets[link_idx] && server->socket)
552   {
553     share->tgt_sockets_lengths[link_idx] = strlen(server->socket);
554     if (!(share->tgt_sockets[link_idx] =
555       spider_create_string(server->socket,
556       share->tgt_sockets_lengths[link_idx])))
557     {
558       error_num = HA_ERR_OUT_OF_MEM;
559       goto error;
560     }
561     DBUG_PRINT("info",("spider tgt_sockets=%s", share->tgt_sockets[link_idx]));
562   }
563 
564   if (!share->tgt_dbs[link_idx] && server->db && (length = strlen(server->db)))
565   {
566     share->tgt_dbs_lengths[link_idx] = length;
567     if (!(share->tgt_dbs[link_idx] =
568       spider_create_string(server->db, length)))
569     {
570       error_num = HA_ERR_OUT_OF_MEM;
571       goto error;
572     }
573     DBUG_PRINT("info",("spider tgt_dbs=%s", share->tgt_dbs[link_idx]));
574   }
575 
576   if (!share->tgt_usernames[link_idx] && server->username)
577   {
578     share->tgt_usernames_lengths[link_idx] = strlen(server->username);
579     if (!(share->tgt_usernames[link_idx] =
580       spider_create_string(server->username,
581       share->tgt_usernames_lengths[link_idx])))
582     {
583       error_num = HA_ERR_OUT_OF_MEM;
584       goto error;
585     }
586     DBUG_PRINT("info",("spider tgt_usernames=%s",
587       share->tgt_usernames[link_idx]));
588   }
589 
590   if (!share->tgt_passwords[link_idx] && server->password)
591   {
592     share->tgt_passwords_lengths[link_idx] = strlen(server->password);
593     if (!(share->tgt_passwords[link_idx] =
594       spider_create_string(server->password,
595       share->tgt_passwords_lengths[link_idx])))
596     {
597       error_num = HA_ERR_OUT_OF_MEM;
598       goto error;
599     }
600     DBUG_PRINT("info",("spider tgt_passwords=%s",
601       share->tgt_passwords[link_idx]));
602   }
603 
604   free_root(&mem_root, MYF(0));
605   DBUG_RETURN(0);
606 
607 error:
608   free_root(&mem_root, MYF(0));
609   my_error(error_num, MYF(0), share->server_names[link_idx]);
610   DBUG_RETURN(error_num);
611 }
612 
spider_free_share_alloc(SPIDER_SHARE * share)613 int spider_free_share_alloc(
614   SPIDER_SHARE *share
615 ) {
616   int roop_count;
617   DBUG_ENTER("spider_free_share_alloc");
618   for (roop_count = SPIDER_DBTON_SIZE - 1; roop_count >= 0; roop_count--)
619   {
620     if (share->dbton_share[roop_count])
621     {
622       delete share->dbton_share[roop_count];
623       share->dbton_share[roop_count] = NULL;
624     }
625   }
626   if (share->server_names)
627   {
628     for (roop_count = 0; roop_count < (int) share->server_names_length;
629       roop_count++)
630     {
631       if (share->server_names[roop_count])
632         spider_free(spider_current_trx, share->server_names[roop_count],
633           MYF(0));
634     }
635     spider_free(spider_current_trx, share->server_names, MYF(0));
636   }
637   if (share->tgt_table_names)
638   {
639     for (roop_count = 0; roop_count < (int) share->tgt_table_names_length;
640       roop_count++)
641     {
642       if (share->tgt_table_names[roop_count])
643         spider_free(spider_current_trx, share->tgt_table_names[roop_count],
644           MYF(0));
645     }
646     spider_free(spider_current_trx, share->tgt_table_names, MYF(0));
647   }
648   if (share->tgt_dbs)
649   {
650     for (roop_count = 0; roop_count < (int) share->tgt_dbs_length;
651       roop_count++)
652     {
653       if (share->tgt_dbs[roop_count])
654         spider_free(spider_current_trx, share->tgt_dbs[roop_count], MYF(0));
655     }
656     spider_free(spider_current_trx, share->tgt_dbs, MYF(0));
657   }
658   if (share->tgt_hosts)
659   {
660     for (roop_count = 0; roop_count < (int) share->tgt_hosts_length;
661       roop_count++)
662     {
663       if (share->tgt_hosts[roop_count])
664         spider_free(spider_current_trx, share->tgt_hosts[roop_count], MYF(0));
665     }
666     spider_free(spider_current_trx, share->tgt_hosts, MYF(0));
667   }
668   if (share->tgt_usernames)
669   {
670     for (roop_count = 0; roop_count < (int) share->tgt_usernames_length;
671       roop_count++)
672     {
673       if (share->tgt_usernames[roop_count])
674         spider_free(spider_current_trx, share->tgt_usernames[roop_count],
675           MYF(0));
676     }
677     spider_free(spider_current_trx, share->tgt_usernames, MYF(0));
678   }
679   if (share->tgt_passwords)
680   {
681     for (roop_count = 0; roop_count < (int) share->tgt_passwords_length;
682       roop_count++)
683     {
684       if (share->tgt_passwords[roop_count])
685         spider_free(spider_current_trx, share->tgt_passwords[roop_count],
686           MYF(0));
687     }
688     spider_free(spider_current_trx, share->tgt_passwords, MYF(0));
689   }
690   if (share->tgt_sockets)
691   {
692     for (roop_count = 0; roop_count < (int) share->tgt_sockets_length;
693       roop_count++)
694     {
695       if (share->tgt_sockets[roop_count])
696         spider_free(spider_current_trx, share->tgt_sockets[roop_count],
697           MYF(0));
698     }
699     spider_free(spider_current_trx, share->tgt_sockets, MYF(0));
700   }
701   if (share->tgt_wrappers)
702   {
703     for (roop_count = 0; roop_count < (int) share->tgt_wrappers_length;
704       roop_count++)
705     {
706       if (share->tgt_wrappers[roop_count])
707         spider_free(spider_current_trx, share->tgt_wrappers[roop_count],
708           MYF(0));
709     }
710     spider_free(spider_current_trx, share->tgt_wrappers, MYF(0));
711   }
712   if (share->tgt_ssl_cas)
713   {
714     for (roop_count = 0; roop_count < (int) share->tgt_ssl_cas_length;
715       roop_count++)
716     {
717       if (share->tgt_ssl_cas[roop_count])
718         spider_free(spider_current_trx, share->tgt_ssl_cas[roop_count],
719           MYF(0));
720     }
721     spider_free(spider_current_trx, share->tgt_ssl_cas, MYF(0));
722   }
723   if (share->tgt_ssl_capaths)
724   {
725     for (roop_count = 0; roop_count < (int) share->tgt_ssl_capaths_length;
726       roop_count++)
727     {
728       if (share->tgt_ssl_capaths[roop_count])
729         spider_free(spider_current_trx, share->tgt_ssl_capaths[roop_count],
730           MYF(0));
731     }
732     spider_free(spider_current_trx, share->tgt_ssl_capaths, MYF(0));
733   }
734   if (share->tgt_ssl_certs)
735   {
736     for (roop_count = 0; roop_count < (int) share->tgt_ssl_certs_length;
737       roop_count++)
738     {
739       if (share->tgt_ssl_certs[roop_count])
740         spider_free(spider_current_trx, share->tgt_ssl_certs[roop_count],
741           MYF(0));
742     }
743     spider_free(spider_current_trx, share->tgt_ssl_certs, MYF(0));
744   }
745   if (share->tgt_ssl_ciphers)
746   {
747     for (roop_count = 0; roop_count < (int) share->tgt_ssl_ciphers_length;
748       roop_count++)
749     {
750       if (share->tgt_ssl_ciphers[roop_count])
751         spider_free(spider_current_trx, share->tgt_ssl_ciphers[roop_count],
752           MYF(0));
753     }
754     spider_free(spider_current_trx, share->tgt_ssl_ciphers, MYF(0));
755   }
756   if (share->tgt_ssl_keys)
757   {
758     for (roop_count = 0; roop_count < (int) share->tgt_ssl_keys_length;
759       roop_count++)
760     {
761       if (share->tgt_ssl_keys[roop_count])
762         spider_free(spider_current_trx, share->tgt_ssl_keys[roop_count],
763           MYF(0));
764     }
765     spider_free(spider_current_trx, share->tgt_ssl_keys, MYF(0));
766   }
767   if (share->tgt_default_files)
768   {
769     for (roop_count = 0; roop_count < (int) share->tgt_default_files_length;
770       roop_count++)
771     {
772       if (share->tgt_default_files[roop_count])
773         spider_free(spider_current_trx, share->tgt_default_files[roop_count],
774           MYF(0));
775     }
776     spider_free(spider_current_trx, share->tgt_default_files, MYF(0));
777   }
778   if (share->tgt_default_groups)
779   {
780     for (roop_count = 0; roop_count < (int) share->tgt_default_groups_length;
781       roop_count++)
782     {
783       if (share->tgt_default_groups[roop_count])
784         spider_free(spider_current_trx, share->tgt_default_groups[roop_count],
785           MYF(0));
786     }
787     spider_free(spider_current_trx, share->tgt_default_groups, MYF(0));
788   }
789   if (share->tgt_pk_names)
790   {
791     for (roop_count = 0; roop_count < (int) share->tgt_pk_names_length;
792       roop_count++)
793     {
794       if (share->tgt_pk_names[roop_count])
795         spider_free(spider_current_trx, share->tgt_pk_names[roop_count],
796           MYF(0));
797     }
798     spider_free(spider_current_trx, share->tgt_pk_names, MYF(0));
799   }
800   if (share->tgt_sequence_names)
801   {
802     for (roop_count = 0; roop_count < (int) share->tgt_sequence_names_length;
803       roop_count++)
804     {
805       if (share->tgt_sequence_names[roop_count])
806         spider_free(spider_current_trx, share->tgt_sequence_names[roop_count],
807           MYF(0));
808     }
809     spider_free(spider_current_trx, share->tgt_sequence_names, MYF(0));
810   }
811   if (share->static_link_ids)
812   {
813     for (roop_count = 0; roop_count < (int) share->static_link_ids_length;
814       roop_count++)
815     {
816       if (share->static_link_ids[roop_count])
817         spider_free(spider_current_trx, share->static_link_ids[roop_count],
818           MYF(0));
819     }
820     spider_free(spider_current_trx, share->static_link_ids, MYF(0));
821   }
822 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
823   if (share->hs_read_socks)
824   {
825     for (roop_count = 0; roop_count < (int) share->hs_read_socks_length;
826       roop_count++)
827     {
828       if (share->hs_read_socks[roop_count])
829         spider_free(spider_current_trx, share->hs_read_socks[roop_count],
830           MYF(0));
831     }
832     spider_free(spider_current_trx, share->hs_read_socks, MYF(0));
833   }
834   if (share->hs_write_socks)
835   {
836     for (roop_count = 0; roop_count < (int) share->hs_write_socks_length;
837       roop_count++)
838     {
839       if (share->hs_write_socks[roop_count])
840         spider_free(spider_current_trx, share->hs_write_socks[roop_count],
841           MYF(0));
842     }
843     spider_free(spider_current_trx, share->hs_write_socks, MYF(0));
844   }
845 #endif
846   if (share->bka_engine)
847     spider_free(spider_current_trx, share->bka_engine, MYF(0));
848   if (share->conn_keys)
849     spider_free(spider_current_trx, share->conn_keys, MYF(0));
850   if (share->tgt_ports)
851     spider_free(spider_current_trx, share->tgt_ports, MYF(0));
852   if (share->tgt_ssl_vscs)
853     spider_free(spider_current_trx, share->tgt_ssl_vscs, MYF(0));
854   if (share->link_statuses)
855     spider_free(spider_current_trx, share->link_statuses, MYF(0));
856 #ifndef WITHOUT_SPIDER_BG_SEARCH
857   if (share->monitoring_bg_flag)
858     spider_free(spider_current_trx, share->monitoring_bg_flag, MYF(0));
859   if (share->monitoring_bg_kind)
860     spider_free(spider_current_trx, share->monitoring_bg_kind, MYF(0));
861 #endif
862   if (share->monitoring_binlog_pos_at_failing)
863     spider_free(spider_current_trx, share->monitoring_binlog_pos_at_failing, MYF(0));
864   if (share->monitoring_flag)
865     spider_free(spider_current_trx, share->monitoring_flag, MYF(0));
866   if (share->monitoring_kind)
867     spider_free(spider_current_trx, share->monitoring_kind, MYF(0));
868 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
869   if (share->use_hs_reads)
870     spider_free(spider_current_trx, share->use_hs_reads, MYF(0));
871   if (share->use_hs_writes)
872     spider_free(spider_current_trx, share->use_hs_writes, MYF(0));
873   if (share->hs_read_ports)
874     spider_free(spider_current_trx, share->hs_read_ports, MYF(0));
875   if (share->hs_write_ports)
876     spider_free(spider_current_trx, share->hs_write_ports, MYF(0));
877   if (share->hs_write_to_reads)
878     spider_free(spider_current_trx, share->hs_write_to_reads, MYF(0));
879 #endif
880   if (share->use_handlers)
881     spider_free(spider_current_trx, share->use_handlers, MYF(0));
882   if (share->connect_timeouts)
883     spider_free(spider_current_trx, share->connect_timeouts, MYF(0));
884   if (share->net_read_timeouts)
885     spider_free(spider_current_trx, share->net_read_timeouts, MYF(0));
886   if (share->net_write_timeouts)
887     spider_free(spider_current_trx, share->net_write_timeouts, MYF(0));
888   if (share->access_balances)
889     spider_free(spider_current_trx, share->access_balances, MYF(0));
890   if (share->bka_table_name_types)
891     spider_free(spider_current_trx, share->bka_table_name_types, MYF(0));
892 #ifndef WITHOUT_SPIDER_BG_SEARCH
893   if (share->monitoring_bg_interval)
894     spider_free(spider_current_trx, share->monitoring_bg_interval, MYF(0));
895 #endif
896   if (share->monitoring_limit)
897     spider_free(spider_current_trx, share->monitoring_limit, MYF(0));
898   if (share->monitoring_sid)
899     spider_free(spider_current_trx, share->monitoring_sid, MYF(0));
900   if (share->alter_table.tmp_server_names)
901     spider_free(spider_current_trx, share->alter_table.tmp_server_names,
902       MYF(0));
903   if (share->key_hint)
904   {
905     delete [] share->key_hint;
906     share->key_hint = NULL;
907   }
908 #ifdef WITH_PARTITION_STORAGE_ENGINE
909   if (share->partition_share)
910     spider_free_pt_share(share->partition_share);
911 #endif
912   DBUG_RETURN(0);
913 }
914 
spider_free_tmp_share_alloc(SPIDER_SHARE * share)915 void spider_free_tmp_share_alloc(
916   SPIDER_SHARE *share
917 ) {
918   DBUG_ENTER("spider_free_tmp_share_alloc");
919   if (share->server_names && share->server_names[0])
920   {
921     spider_free(spider_current_trx, share->server_names[0], MYF(0));
922     share->server_names[0] = NULL;
923   }
924   if (share->tgt_table_names && share->tgt_table_names[0])
925   {
926     spider_free(spider_current_trx, share->tgt_table_names[0], MYF(0));
927     share->tgt_table_names[0] = NULL;
928   }
929   if (share->tgt_dbs && share->tgt_dbs[0])
930   {
931     spider_free(spider_current_trx, share->tgt_dbs[0], MYF(0));
932     share->tgt_dbs[0] = NULL;
933   }
934   if (share->tgt_hosts && share->tgt_hosts[0])
935   {
936     spider_free(spider_current_trx, share->tgt_hosts[0], MYF(0));
937     share->tgt_hosts[0] = NULL;
938   }
939   if (share->tgt_usernames && share->tgt_usernames[0])
940   {
941     spider_free(spider_current_trx, share->tgt_usernames[0], MYF(0));
942     share->tgt_usernames[0] = NULL;
943   }
944   if (share->tgt_passwords && share->tgt_passwords[0])
945   {
946     spider_free(spider_current_trx, share->tgt_passwords[0], MYF(0));
947     share->tgt_passwords[0] = NULL;
948   }
949   if (share->tgt_sockets && share->tgt_sockets[0])
950   {
951     spider_free(spider_current_trx, share->tgt_sockets[0], MYF(0));
952     share->tgt_sockets[0] = NULL;
953   }
954   if (share->tgt_wrappers && share->tgt_wrappers[0])
955   {
956     spider_free(spider_current_trx, share->tgt_wrappers[0], MYF(0));
957     share->tgt_wrappers[0] = NULL;
958   }
959   if (share->tgt_ssl_cas && share->tgt_ssl_cas[0])
960   {
961     spider_free(spider_current_trx, share->tgt_ssl_cas[0], MYF(0));
962     share->tgt_ssl_cas[0] = NULL;
963   }
964   if (share->tgt_ssl_capaths && share->tgt_ssl_capaths[0])
965   {
966     spider_free(spider_current_trx, share->tgt_ssl_capaths[0], MYF(0));
967     share->tgt_ssl_capaths[0] = NULL;
968   }
969   if (share->tgt_ssl_certs && share->tgt_ssl_certs[0])
970   {
971     spider_free(spider_current_trx, share->tgt_ssl_certs[0], MYF(0));
972     share->tgt_ssl_certs[0] = NULL;
973   }
974   if (share->tgt_ssl_ciphers && share->tgt_ssl_ciphers[0])
975   {
976     spider_free(spider_current_trx, share->tgt_ssl_ciphers[0], MYF(0));
977     share->tgt_ssl_ciphers[0] = NULL;
978   }
979   if (share->tgt_ssl_keys && share->tgt_ssl_keys[0])
980   {
981     spider_free(spider_current_trx, share->tgt_ssl_keys[0], MYF(0));
982     share->tgt_ssl_keys[0] = NULL;
983   }
984   if (share->tgt_default_files && share->tgt_default_files[0])
985   {
986     spider_free(spider_current_trx, share->tgt_default_files[0], MYF(0));
987     share->tgt_default_files[0] = NULL;
988   }
989   if (share->tgt_default_groups && share->tgt_default_groups[0])
990   {
991     spider_free(spider_current_trx, share->tgt_default_groups[0], MYF(0));
992     share->tgt_default_groups[0] = NULL;
993   }
994   if (share->tgt_pk_names && share->tgt_pk_names[0])
995   {
996     spider_free(spider_current_trx, share->tgt_pk_names[0], MYF(0));
997     share->tgt_pk_names[0] = NULL;
998   }
999   if (share->tgt_sequence_names && share->tgt_sequence_names[0])
1000   {
1001     spider_free(spider_current_trx, share->tgt_sequence_names[0], MYF(0));
1002     share->tgt_sequence_names[0] = NULL;
1003   }
1004   if (share->static_link_ids && share->static_link_ids[0])
1005   {
1006     spider_free(spider_current_trx, share->static_link_ids[0], MYF(0));
1007     share->static_link_ids[0] = NULL;
1008   }
1009 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
1010   if (share->hs_read_socks && share->hs_read_socks[0])
1011   {
1012     spider_free(spider_current_trx, share->hs_read_socks[0], MYF(0));
1013     share->hs_read_socks[0] = NULL;
1014   }
1015   if (share->hs_write_socks && share->hs_write_socks[0])
1016   {
1017     spider_free(spider_current_trx, share->hs_write_socks[0], MYF(0));
1018     share->hs_write_socks[0] = NULL;
1019   }
1020 #endif
1021   if (share->bka_engine)
1022   {
1023     spider_free(spider_current_trx, share->bka_engine, MYF(0));
1024     share->bka_engine = NULL;
1025   }
1026   if (share->conn_keys)
1027   {
1028     spider_free(spider_current_trx, share->conn_keys, MYF(0));
1029     share->conn_keys = NULL;
1030   }
1031   if (share->static_key_cardinality)
1032     spider_free(spider_current_trx, share->static_key_cardinality, MYF(0));
1033   if (share->key_hint)
1034   {
1035     delete [] share->key_hint;
1036     share->key_hint = NULL;
1037   }
1038   DBUG_VOID_RETURN;
1039 }
1040 
spider_get_string_between_quote(char * ptr,bool alloc,SPIDER_PARAM_STRING_PARSE * param_string_parse)1041 char *spider_get_string_between_quote(
1042   char *ptr,
1043   bool alloc,
1044   SPIDER_PARAM_STRING_PARSE *param_string_parse
1045 ) {
1046   char *start_ptr, *end_ptr, *tmp_ptr, *esc_ptr;
1047   bool find_flg = FALSE, esc_flg = FALSE;
1048   DBUG_ENTER("spider_get_string_between_quote");
1049 
1050   start_ptr = strchr(ptr, '\'');
1051   end_ptr = strchr(ptr, '"');
1052   if (start_ptr && (!end_ptr || start_ptr < end_ptr))
1053   {
1054     tmp_ptr = ++start_ptr;
1055     while (!find_flg)
1056     {
1057       if (!(end_ptr = strchr(tmp_ptr, '\'')))
1058         DBUG_RETURN(NULL);
1059       esc_ptr = tmp_ptr;
1060       while (!find_flg)
1061       {
1062         esc_ptr = strchr(esc_ptr, '\\');
1063         if (!esc_ptr || esc_ptr > end_ptr)
1064           find_flg = TRUE;
1065         else if (esc_ptr == end_ptr - 1)
1066         {
1067           esc_flg = TRUE;
1068           tmp_ptr = end_ptr + 1;
1069           break;
1070         } else {
1071           esc_flg = TRUE;
1072           esc_ptr += 2;
1073         }
1074       }
1075     }
1076   } else if (end_ptr)
1077   {
1078     start_ptr = end_ptr;
1079     tmp_ptr = ++start_ptr;
1080     while (!find_flg)
1081     {
1082       if (!(end_ptr = strchr(tmp_ptr, '"')))
1083         DBUG_RETURN(NULL);
1084       esc_ptr = tmp_ptr;
1085       while (!find_flg)
1086       {
1087         esc_ptr = strchr(esc_ptr, '\\');
1088         if (!esc_ptr || esc_ptr > end_ptr)
1089           find_flg = TRUE;
1090         else if (esc_ptr == end_ptr - 1)
1091         {
1092           esc_flg = TRUE;
1093           tmp_ptr = end_ptr + 1;
1094           break;
1095         } else {
1096           esc_flg = TRUE;
1097           esc_ptr += 2;
1098         }
1099       }
1100     }
1101   } else
1102     DBUG_RETURN(NULL);
1103 
1104   *end_ptr = '\0';
1105   if (esc_flg)
1106   {
1107     esc_ptr = start_ptr;
1108     while (TRUE)
1109     {
1110       esc_ptr = strchr(esc_ptr, '\\');
1111       if (!esc_ptr)
1112         break;
1113       switch(*(esc_ptr + 1))
1114       {
1115         case 'b':
1116           *esc_ptr = '\b';
1117           break;
1118         case 'n':
1119           *esc_ptr = '\n';
1120           break;
1121         case 'r':
1122           *esc_ptr = '\r';
1123           break;
1124         case 't':
1125           *esc_ptr = '\t';
1126           break;
1127         default:
1128           *esc_ptr = *(esc_ptr + 1);
1129           break;
1130       }
1131       esc_ptr++;
1132       strcpy(esc_ptr, esc_ptr + 1);
1133     }
1134   }
1135 
1136   if (param_string_parse)
1137     param_string_parse->set_param_value(start_ptr, start_ptr + strlen(start_ptr) + 1);
1138 
1139   if (alloc)
1140   {
1141     DBUG_RETURN(
1142       spider_create_string(
1143       start_ptr,
1144       strlen(start_ptr))
1145     );
1146   } else {
1147     DBUG_RETURN(start_ptr);
1148   }
1149 }
1150 
spider_create_string_list(char *** string_list,uint ** string_length_list,uint * list_length,char * str,uint length,SPIDER_PARAM_STRING_PARSE * param_string_parse)1151 int spider_create_string_list(
1152   char ***string_list,
1153   uint **string_length_list,
1154   uint *list_length,
1155   char *str,
1156   uint length,
1157   SPIDER_PARAM_STRING_PARSE *param_string_parse
1158 ) {
1159   int roop_count;
1160   char *tmp_ptr, *tmp_ptr2, *tmp_ptr3, *esc_ptr;
1161   bool find_flg = FALSE;
1162   DBUG_ENTER("spider_create_string_list");
1163 
1164   *list_length = 0;
1165   param_string_parse->init_param_value();
1166   if (!str)
1167   {
1168     *string_list = NULL;
1169     DBUG_RETURN(0);
1170   }
1171 
1172   tmp_ptr = str;
1173   while (*tmp_ptr == ' ')
1174     tmp_ptr++;
1175   if (*tmp_ptr)
1176     *list_length = 1;
1177   else {
1178     *string_list = NULL;
1179     DBUG_RETURN(0);
1180   }
1181 
1182   while (TRUE)
1183   {
1184     if ((tmp_ptr2 = strchr(tmp_ptr, ' ')))
1185     {
1186       esc_ptr = tmp_ptr;
1187       while (!find_flg)
1188       {
1189         esc_ptr = strchr(esc_ptr, '\\');
1190         if (!esc_ptr || esc_ptr > tmp_ptr2)
1191           find_flg = TRUE;
1192         else if (esc_ptr == tmp_ptr2 - 1)
1193         {
1194           tmp_ptr = tmp_ptr2 + 1;
1195           break;
1196         } else
1197           esc_ptr += 2;
1198       }
1199       if (find_flg)
1200       {
1201         (*list_length)++;
1202         tmp_ptr = tmp_ptr2 + 1;
1203         while (*tmp_ptr == ' ')
1204           tmp_ptr++;
1205       }
1206     } else
1207       break;
1208   }
1209 
1210   if (!(*string_list = (char**)
1211     spider_bulk_malloc(spider_current_trx, 37, MYF(MY_WME | MY_ZEROFILL),
1212       string_list, sizeof(char*) * (*list_length),
1213       string_length_list, sizeof(int) * (*list_length),
1214       NullS))
1215   ) {
1216     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1217     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1218   }
1219 
1220   tmp_ptr = str;
1221   while (*tmp_ptr == ' ')
1222   {
1223     *tmp_ptr = '\0';
1224     tmp_ptr++;
1225   }
1226   tmp_ptr3 = tmp_ptr;
1227 
1228   for (roop_count = 0; roop_count < (int) *list_length - 1; roop_count++)
1229   {
1230     while (TRUE)
1231     {
1232       tmp_ptr2 = strchr(tmp_ptr, ' ');
1233 
1234       esc_ptr = tmp_ptr;
1235       while (!find_flg)
1236       {
1237         esc_ptr = strchr(esc_ptr, '\\');
1238         if (!esc_ptr || esc_ptr > tmp_ptr2)
1239           find_flg = TRUE;
1240         else if (esc_ptr == tmp_ptr2 - 1)
1241         {
1242           tmp_ptr = tmp_ptr2 + 1;
1243           break;
1244         } else
1245           esc_ptr += 2;
1246       }
1247       if (find_flg)
1248         break;
1249     }
1250     tmp_ptr = tmp_ptr2;
1251 
1252     while (*tmp_ptr == ' ')
1253     {
1254       *tmp_ptr = '\0';
1255       tmp_ptr++;
1256     }
1257 
1258     (*string_length_list)[roop_count] = strlen(tmp_ptr3);
1259     if (!((*string_list)[roop_count] = spider_create_string(
1260       tmp_ptr3, (*string_length_list)[roop_count]))
1261     ) {
1262       my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1263       DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1264     }
1265     DBUG_PRINT("info",("spider string_list[%d]=%s", roop_count,
1266       (*string_list)[roop_count]));
1267     tmp_ptr3 = tmp_ptr;
1268   }
1269   (*string_length_list)[roop_count] = strlen(tmp_ptr3);
1270   if (!((*string_list)[roop_count] = spider_create_string(
1271     tmp_ptr3, (*string_length_list)[roop_count]))
1272   ) {
1273     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1274     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1275   }
1276 
1277   param_string_parse->set_param_value(tmp_ptr3,
1278                                       tmp_ptr3 + strlen(tmp_ptr3) + 1);
1279 
1280   DBUG_PRINT("info",("spider string_list[%d]=%s", roop_count,
1281     (*string_list)[roop_count]));
1282 
1283   DBUG_RETURN(0);
1284 }
1285 
spider_create_long_list(long ** long_list,uint * list_length,char * str,uint length,long min_val,long max_val,SPIDER_PARAM_STRING_PARSE * param_string_parse)1286 int spider_create_long_list(
1287   long **long_list,
1288   uint *list_length,
1289   char *str,
1290   uint length,
1291   long min_val,
1292   long max_val,
1293   SPIDER_PARAM_STRING_PARSE *param_string_parse
1294 ) {
1295   int roop_count;
1296   char *tmp_ptr;
1297   DBUG_ENTER("spider_create_long_list");
1298 
1299   *list_length = 0;
1300   param_string_parse->init_param_value();
1301   if (!str)
1302   {
1303     *long_list = NULL;
1304     DBUG_RETURN(0);
1305   }
1306 
1307   tmp_ptr = str;
1308   while (*tmp_ptr == ' ')
1309     tmp_ptr++;
1310   if (*tmp_ptr)
1311     *list_length = 1;
1312   else {
1313     *long_list = NULL;
1314     DBUG_RETURN(0);
1315   }
1316 
1317   while (TRUE)
1318   {
1319     if ((tmp_ptr = strchr(tmp_ptr, ' ')))
1320     {
1321       (*list_length)++;
1322       tmp_ptr = tmp_ptr + 1;
1323       while (*tmp_ptr == ' ')
1324         tmp_ptr++;
1325     } else
1326       break;
1327   }
1328 
1329   if (!(*long_list = (long*)
1330     spider_bulk_malloc(spider_current_trx, 38, MYF(MY_WME | MY_ZEROFILL),
1331       long_list, sizeof(long) * (*list_length),
1332       NullS))
1333   ) {
1334     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1335     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1336   }
1337 
1338   tmp_ptr = str;
1339   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1340   {
1341     if (roop_count != 0)
1342       tmp_ptr = strchr(tmp_ptr, ' ');
1343 
1344     while (*tmp_ptr == ' ')
1345     {
1346       *tmp_ptr = '\0';
1347       tmp_ptr++;
1348     }
1349     (*long_list)[roop_count] = atol(tmp_ptr);
1350     if ((*long_list)[roop_count] < min_val)
1351       (*long_list)[roop_count] = min_val;
1352     else if ((*long_list)[roop_count] > max_val)
1353       (*long_list)[roop_count] = max_val;
1354   }
1355 
1356   param_string_parse->set_param_value(tmp_ptr,
1357                                       tmp_ptr + strlen(tmp_ptr) + 1);
1358 
1359 #ifndef DBUG_OFF
1360   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1361   {
1362     DBUG_PRINT("info",("spider long_list[%d]=%ld", roop_count,
1363       (*long_list)[roop_count]));
1364   }
1365 #endif
1366 
1367   DBUG_RETURN(0);
1368 }
1369 
spider_create_longlong_list(longlong ** longlong_list,uint * list_length,char * str,uint length,longlong min_val,longlong max_val,SPIDER_PARAM_STRING_PARSE * param_string_parse)1370 int spider_create_longlong_list(
1371   longlong **longlong_list,
1372   uint *list_length,
1373   char *str,
1374   uint length,
1375   longlong min_val,
1376   longlong max_val,
1377   SPIDER_PARAM_STRING_PARSE *param_string_parse
1378 ) {
1379   int error_num, roop_count;
1380   char *tmp_ptr;
1381   DBUG_ENTER("spider_create_longlong_list");
1382 
1383   *list_length = 0;
1384   param_string_parse->init_param_value();
1385   if (!str)
1386   {
1387     *longlong_list = NULL;
1388     DBUG_RETURN(0);
1389   }
1390 
1391   tmp_ptr = str;
1392   while (*tmp_ptr == ' ')
1393     tmp_ptr++;
1394   if (*tmp_ptr)
1395     *list_length = 1;
1396   else {
1397     *longlong_list = NULL;
1398     DBUG_RETURN(0);
1399   }
1400 
1401   while (TRUE)
1402   {
1403     if ((tmp_ptr = strchr(tmp_ptr, ' ')))
1404     {
1405       (*list_length)++;
1406       tmp_ptr = tmp_ptr + 1;
1407       while (*tmp_ptr == ' ')
1408         tmp_ptr++;
1409     } else
1410       break;
1411   }
1412 
1413   if (!(*longlong_list = (longlong *)
1414     spider_bulk_malloc(spider_current_trx, 39, MYF(MY_WME | MY_ZEROFILL),
1415       longlong_list, sizeof(longlong) * (*list_length),
1416       NullS))
1417   ) {
1418     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1419     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1420   }
1421 
1422   tmp_ptr = str;
1423   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1424   {
1425     if (roop_count != 0)
1426       tmp_ptr = strchr(tmp_ptr, ' ');
1427 
1428     while (*tmp_ptr == ' ')
1429     {
1430       *tmp_ptr = '\0';
1431       tmp_ptr++;
1432     }
1433     (*longlong_list)[roop_count] = my_strtoll10(tmp_ptr, (char**) NULL,
1434       &error_num);
1435     if ((*longlong_list)[roop_count] < min_val)
1436       (*longlong_list)[roop_count] = min_val;
1437     else if ((*longlong_list)[roop_count] > max_val)
1438       (*longlong_list)[roop_count] = max_val;
1439   }
1440 
1441   param_string_parse->set_param_value(tmp_ptr,
1442                                       tmp_ptr + strlen(tmp_ptr) + 1);
1443 
1444 #ifndef DBUG_OFF
1445   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1446   {
1447     DBUG_PRINT("info",("spider longlong_list[%d]=%lld", roop_count,
1448       (*longlong_list)[roop_count]));
1449   }
1450 #endif
1451 
1452   DBUG_RETURN(0);
1453 }
1454 
spider_increase_string_list(char *** string_list,uint ** string_length_list,uint * list_length,uint * list_charlen,uint link_count)1455 int spider_increase_string_list(
1456   char ***string_list,
1457   uint **string_length_list,
1458   uint *list_length,
1459   uint *list_charlen,
1460   uint link_count
1461 ) {
1462   int roop_count;
1463   char **tmp_str_list, *tmp_str;
1464   uint *tmp_length_list, tmp_length;
1465   DBUG_ENTER("spider_increase_string_list");
1466   if (*list_length == link_count)
1467     DBUG_RETURN(0);
1468   if (*list_length > 1)
1469   {
1470     my_printf_error(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM,
1471       ER_SPIDER_DIFFERENT_LINK_COUNT_STR, MYF(0));
1472     DBUG_RETURN(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM);
1473   }
1474 
1475   if (*string_list)
1476   {
1477     tmp_str = (*string_list)[0];
1478     tmp_length = (*string_length_list)[0];
1479   } else {
1480     tmp_str = NULL;
1481     tmp_length = 0;
1482   }
1483 
1484   if (!(tmp_str_list = (char**)
1485     spider_bulk_malloc(spider_current_trx, 40, MYF(MY_WME | MY_ZEROFILL),
1486       &tmp_str_list, sizeof(char*) * link_count,
1487       &tmp_length_list, sizeof(uint) * link_count,
1488       NullS))
1489   ) {
1490     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1491     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1492   }
1493 
1494   for (roop_count = 0; roop_count < (int) link_count; roop_count++)
1495   {
1496     tmp_length_list[roop_count] = tmp_length;
1497     if (tmp_str)
1498     {
1499       if (!(tmp_str_list[roop_count] = spider_create_string(
1500         tmp_str, tmp_length))
1501       )
1502         goto error;
1503       DBUG_PRINT("info",("spider string_list[%d]=%s", roop_count,
1504         tmp_str_list[roop_count]));
1505     } else
1506       tmp_str_list[roop_count] = NULL;
1507   }
1508   if (*string_list)
1509   {
1510     if ((*string_list)[0])
1511       spider_free(spider_current_trx, (*string_list)[0], MYF(0));
1512     spider_free(spider_current_trx, *string_list, MYF(0));
1513   }
1514   *list_charlen = (tmp_length + 1) * link_count - 1;
1515   *list_length = link_count;
1516   *string_list = tmp_str_list;
1517   *string_length_list = tmp_length_list;
1518 
1519   DBUG_RETURN(0);
1520 
1521 error:
1522   for (roop_count = 0; roop_count < (int) link_count; roop_count++)
1523   {
1524     if (tmp_str_list[roop_count])
1525       spider_free(spider_current_trx, tmp_str_list[roop_count], MYF(0));
1526   }
1527   if (tmp_str_list)
1528     spider_free(spider_current_trx, tmp_str_list, MYF(0));
1529   my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1530   DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1531 }
1532 
spider_increase_null_string_list(char *** string_list,uint ** string_length_list,uint * list_length,uint * list_charlen,uint link_count)1533 int spider_increase_null_string_list(
1534   char ***string_list,
1535   uint **string_length_list,
1536   uint *list_length,
1537   uint *list_charlen,
1538   uint link_count
1539 ) {
1540   int roop_count;
1541   char **tmp_str_list;
1542   uint *tmp_length_list;
1543   DBUG_ENTER("spider_increase_null_string_list");
1544   if (*list_length == link_count)
1545     DBUG_RETURN(0);
1546 
1547   if (!(tmp_str_list = (char**)
1548     spider_bulk_malloc(spider_current_trx, 247, MYF(MY_WME | MY_ZEROFILL),
1549       &tmp_str_list, sizeof(char*) * link_count,
1550       &tmp_length_list, sizeof(uint) * link_count,
1551       NullS))
1552   ) {
1553     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1554     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1555   }
1556 
1557   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1558   {
1559     tmp_str_list[roop_count] = (*string_list)[roop_count];
1560     tmp_length_list[roop_count] = (*string_length_list)[roop_count];
1561   }
1562   if (*string_list)
1563   {
1564     spider_free(spider_current_trx, *string_list, MYF(0));
1565   }
1566   *list_length = link_count;
1567   *string_list = tmp_str_list;
1568   *string_length_list = tmp_length_list;
1569 #ifndef DBUG_OFF
1570   DBUG_PRINT("info",("spider list_length=%u", *list_length));
1571   for (roop_count = 0; roop_count < (int) *list_length; roop_count++)
1572   {
1573     DBUG_PRINT("info",("spider string_list[%d]=%s", roop_count,
1574       (*string_list)[roop_count] ? (*string_list)[roop_count] : "NULL"));
1575     DBUG_PRINT("info",("spider string_length_list[%d]=%u", roop_count,
1576       (*string_length_list)[roop_count]));
1577   }
1578 #endif
1579 
1580   DBUG_RETURN(0);
1581 }
1582 
spider_increase_long_list(long ** long_list,uint * list_length,uint link_count)1583 int spider_increase_long_list(
1584   long **long_list,
1585   uint *list_length,
1586   uint link_count
1587 ) {
1588   int roop_count;
1589   long *tmp_long_list, tmp_long;
1590   DBUG_ENTER("spider_increase_long_list");
1591   if (*list_length == link_count)
1592     DBUG_RETURN(0);
1593   if (*list_length > 1)
1594   {
1595     my_printf_error(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM,
1596       ER_SPIDER_DIFFERENT_LINK_COUNT_STR, MYF(0));
1597     DBUG_RETURN(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM);
1598   }
1599 
1600   if (*long_list)
1601     tmp_long = (*long_list)[0];
1602   else
1603     tmp_long = -1;
1604 
1605   if (!(tmp_long_list = (long*)
1606     spider_bulk_malloc(spider_current_trx, 41, MYF(MY_WME | MY_ZEROFILL),
1607       &tmp_long_list, sizeof(long) * link_count,
1608       NullS))
1609   ) {
1610     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1611     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1612   }
1613 
1614   for (roop_count = 0; roop_count < (int) link_count; roop_count++)
1615   {
1616     tmp_long_list[roop_count] = tmp_long;
1617     DBUG_PRINT("info",("spider long_list[%d]=%ld", roop_count,
1618       tmp_long));
1619   }
1620   if (*long_list)
1621     spider_free(spider_current_trx, *long_list, MYF(0));
1622   *list_length = link_count;
1623   *long_list = tmp_long_list;
1624 
1625   DBUG_RETURN(0);
1626 }
1627 
spider_increase_longlong_list(longlong ** longlong_list,uint * list_length,uint link_count)1628 int spider_increase_longlong_list(
1629   longlong **longlong_list,
1630   uint *list_length,
1631   uint link_count
1632 ) {
1633   int roop_count;
1634   longlong *tmp_longlong_list, tmp_longlong;
1635   DBUG_ENTER("spider_increase_longlong_list");
1636   if (*list_length == link_count)
1637     DBUG_RETURN(0);
1638   if (*list_length > 1)
1639   {
1640     my_printf_error(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM,
1641       ER_SPIDER_DIFFERENT_LINK_COUNT_STR, MYF(0));
1642     DBUG_RETURN(ER_SPIDER_DIFFERENT_LINK_COUNT_NUM);
1643   }
1644 
1645   if (*longlong_list)
1646     tmp_longlong = (*longlong_list)[0];
1647   else
1648     tmp_longlong = -1;
1649 
1650   if (!(tmp_longlong_list = (longlong*)
1651     spider_bulk_malloc(spider_current_trx, 42, MYF(MY_WME | MY_ZEROFILL),
1652       &tmp_longlong_list, sizeof(longlong) * link_count,
1653       NullS))
1654   ) {
1655     my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
1656     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
1657   }
1658 
1659   for (roop_count = 0; roop_count < (int) link_count; roop_count++)
1660   {
1661     tmp_longlong_list[roop_count] = tmp_longlong;
1662     DBUG_PRINT("info",("spider longlong_list[%d]=%lld", roop_count,
1663       tmp_longlong));
1664   }
1665   if (*longlong_list)
1666     spider_free(spider_current_trx, *longlong_list, MYF(0));
1667   *list_length = link_count;
1668   *longlong_list = tmp_longlong_list;
1669 
1670   DBUG_RETURN(0);
1671 }
1672 
spider_set_ll_value(longlong * value,char * str)1673 static int spider_set_ll_value(
1674   longlong *value,
1675   char *str
1676 ) {
1677   int error_num = 0;
1678   DBUG_ENTER("spider_set_ll_value");
1679   *value = my_strtoll10(str, (char**) NULL, &error_num);
1680   DBUG_RETURN(error_num);
1681 }
1682 
1683 /**
1684   Print a parameter string error message.
1685 
1686   @return                   Error code.
1687 */
1688 
print_param_error()1689 int st_spider_param_string_parse::print_param_error()
1690 {
1691   if (start_title_ptr)
1692   {
1693     /* Restore the input delimiter characters */
1694     restore_delims();
1695 
1696     /* Print the error message */
1697     switch (error_num)
1698     {
1699     case ER_SPIDER_INVALID_UDF_PARAM_NUM:
1700       my_printf_error(error_num, ER_SPIDER_INVALID_UDF_PARAM_STR,
1701                       MYF(0), start_title_ptr);
1702       break;
1703     case ER_SPIDER_INVALID_CONNECT_INFO_NUM:
1704     default:
1705       my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_STR,
1706                       MYF(0), start_title_ptr);
1707     }
1708 
1709     return error_num;
1710   }
1711   else
1712     return 0;
1713 }
1714 
1715 #define SPIDER_PARAM_STR_LEN(name) name ## _length
1716 #define SPIDER_PARAM_STR(title_name, param_name) \
1717   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1718   { \
1719     DBUG_PRINT("info",("spider " title_name " start")); \
1720     if (!share->param_name) \
1721     { \
1722       if ((share->param_name = spider_get_string_between_quote( \
1723         start_ptr, TRUE, &connect_string_parse))) \
1724         share->SPIDER_PARAM_STR_LEN(param_name) = strlen(share->param_name); \
1725       else { \
1726         error_num = connect_string_parse.print_param_error(); \
1727         goto error; \
1728       } \
1729       DBUG_PRINT("info",("spider " title_name "=%s", share->param_name)); \
1730     } \
1731     break; \
1732   }
1733 #define SPIDER_PARAM_STR_LENS(name) name ## _lengths
1734 #define SPIDER_PARAM_STR_CHARLEN(name) name ## _charlen
1735 #define SPIDER_PARAM_STR_LIST(title_name, param_name) \
1736   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1737   { \
1738     DBUG_PRINT("info",("spider " title_name " start")); \
1739     if (!share->param_name) \
1740     { \
1741       if ((tmp_ptr2 = spider_get_string_between_quote( \
1742         start_ptr, FALSE))) \
1743       { \
1744         share->SPIDER_PARAM_STR_CHARLEN(param_name) = strlen(tmp_ptr2); \
1745         if ((error_num = spider_create_string_list( \
1746           &share->param_name, \
1747           &share->SPIDER_PARAM_STR_LENS(param_name), \
1748           &share->SPIDER_PARAM_STR_LEN(param_name), \
1749           tmp_ptr2, \
1750           share->SPIDER_PARAM_STR_CHARLEN(param_name), \
1751           &connect_string_parse))) \
1752           goto error; \
1753       } else { \
1754         error_num = connect_string_parse.print_param_error(); \
1755         goto error; \
1756       } \
1757     } \
1758     break; \
1759   }
1760 #define SPIDER_PARAM_HINT(title_name, param_name, check_length, max_size, append_method) \
1761   if (!strncasecmp(tmp_ptr, title_name, check_length)) \
1762   { \
1763     DBUG_PRINT("info",("spider " title_name " start")); \
1764     DBUG_PRINT("info",("spider max_size=%d", max_size)); \
1765     int hint_num = atoi(tmp_ptr + check_length); \
1766     DBUG_PRINT("info",("spider hint_num=%d", hint_num)); \
1767     DBUG_PRINT("info",("spider share->param_name=%p", share->param_name)); \
1768     if (share->param_name) \
1769     { \
1770       if (hint_num < 0 || hint_num >= max_size) \
1771       { \
1772         error_num = connect_string_parse.print_param_error(); \
1773         goto error; \
1774       } else if (share->param_name[hint_num].length() > 0) \
1775         break; \
1776       char *hint_str = spider_get_string_between_quote(start_ptr, FALSE); \
1777       if ((error_num = \
1778         append_method(&share->param_name[hint_num], hint_str))) \
1779         goto error; \
1780       DBUG_PRINT("info",("spider " title_name "[%d]=%s", hint_num, \
1781         share->param_name[hint_num].ptr())); \
1782     } else { \
1783       error_num = connect_string_parse.print_param_error(); \
1784       goto error; \
1785     } \
1786     break; \
1787   }
1788 #define SPIDER_PARAM_NUMHINT(title_name, param_name, check_length, max_size, append_method) \
1789   if (!strncasecmp(tmp_ptr, title_name, check_length)) \
1790   { \
1791     DBUG_PRINT("info",("spider " title_name " start")); \
1792     DBUG_PRINT("info",("spider max_size=%d", max_size)); \
1793     int hint_num = atoi(tmp_ptr + check_length); \
1794     DBUG_PRINT("info",("spider hint_num=%d", hint_num)); \
1795     DBUG_PRINT("info",("spider share->param_name=%p", share->param_name)); \
1796     if (share->param_name) \
1797     { \
1798       if (hint_num < 0 || hint_num >= max_size) \
1799       { \
1800         error_num = connect_string_parse.print_param_error(); \
1801         goto error; \
1802       } else if (share->param_name[hint_num] != -1) \
1803         break; \
1804       char *hint_str = spider_get_string_between_quote(start_ptr, FALSE); \
1805       if ((error_num = \
1806         append_method(&share->param_name[hint_num], hint_str))) \
1807         goto error; \
1808       DBUG_PRINT("info",("spider " title_name "[%d]=%lld", hint_num, \
1809         share->param_name[hint_num])); \
1810     } else { \
1811       error_num = connect_string_parse.print_param_error(); \
1812       goto error; \
1813     } \
1814     break; \
1815   }
1816 #define SPIDER_PARAM_LONG_LEN(name) name ## _length
1817 #define SPIDER_PARAM_LONG_LIST_WITH_MAX(title_name, param_name, \
1818   min_val, max_val) \
1819   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1820   { \
1821     DBUG_PRINT("info",("spider " title_name " start")); \
1822     if (!share->param_name) \
1823     { \
1824       if ((tmp_ptr2 = spider_get_string_between_quote( \
1825         start_ptr, FALSE))) \
1826       { \
1827         if ((error_num = spider_create_long_list( \
1828           &share->param_name, \
1829           &share->SPIDER_PARAM_LONG_LEN(param_name), \
1830           tmp_ptr2, \
1831           strlen(tmp_ptr2), \
1832           min_val, max_val, \
1833           &connect_string_parse))) \
1834           goto error; \
1835       } else { \
1836         error_num = connect_string_parse.print_param_error(); \
1837         goto error; \
1838       } \
1839     } \
1840     break; \
1841   }
1842 #define SPIDER_PARAM_LONGLONG_LEN(name) name ## _length
1843 #define SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(title_name, param_name, \
1844   min_val, max_val) \
1845   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1846   { \
1847     DBUG_PRINT("info",("spider " title_name " start")); \
1848     if (!share->param_name) \
1849     { \
1850       if ((tmp_ptr2 = spider_get_string_between_quote( \
1851         start_ptr, FALSE))) \
1852       { \
1853         if ((error_num = spider_create_longlong_list( \
1854           &share->param_name, \
1855           &share->SPIDER_PARAM_LONGLONG_LEN(param_name), \
1856           tmp_ptr2, \
1857           strlen(tmp_ptr2), \
1858           min_val, max_val, \
1859           &connect_string_parse))) \
1860           goto error; \
1861       } else { \
1862         error_num = connect_string_parse.print_param_error(); \
1863         goto error; \
1864       } \
1865     } \
1866     break; \
1867   }
1868 #define SPIDER_PARAM_INT_WITH_MAX(title_name, param_name, min_val, max_val) \
1869   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1870   { \
1871     DBUG_PRINT("info",("spider " title_name " start")); \
1872     if (share->param_name == -1) \
1873     { \
1874       if ((tmp_ptr2 = spider_get_string_between_quote( \
1875         start_ptr, FALSE))) \
1876       { \
1877         share->param_name = atoi(tmp_ptr2); \
1878         if (share->param_name < min_val) \
1879           share->param_name = min_val; \
1880         else if (share->param_name > max_val) \
1881           share->param_name = max_val; \
1882         connect_string_parse.set_param_value(tmp_ptr2, \
1883                                              tmp_ptr2 + \
1884                                                strlen(tmp_ptr2) + 1); \
1885       } else { \
1886         error_num = connect_string_parse.print_param_error(); \
1887         goto error; \
1888       } \
1889       DBUG_PRINT("info",("spider " title_name "=%d", share->param_name)); \
1890     } \
1891     break; \
1892   }
1893 #define SPIDER_PARAM_INT(title_name, param_name, min_val) \
1894   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1895   { \
1896     DBUG_PRINT("info",("spider " title_name " start")); \
1897     if (share->param_name == -1) \
1898     { \
1899       if ((tmp_ptr2 = spider_get_string_between_quote( \
1900         start_ptr, FALSE))) \
1901       { \
1902         share->param_name = atoi(tmp_ptr2); \
1903         if (share->param_name < min_val) \
1904           share->param_name = min_val; \
1905         connect_string_parse.set_param_value(tmp_ptr2, \
1906                                              tmp_ptr2 + \
1907                                                strlen(tmp_ptr2) + 1); \
1908       } else { \
1909         error_num = connect_string_parse.print_param_error(); \
1910         goto error; \
1911       } \
1912       DBUG_PRINT("info",("spider " title_name "=%d", share->param_name)); \
1913     } \
1914     break; \
1915   }
1916 #define SPIDER_PARAM_DOUBLE(title_name, param_name, min_val) \
1917   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1918   { \
1919     DBUG_PRINT("info",("spider " title_name " start")); \
1920     if (share->param_name == -1) \
1921     { \
1922       if ((tmp_ptr2 = spider_get_string_between_quote( \
1923         start_ptr, FALSE))) \
1924       { \
1925         share->param_name = my_atof(tmp_ptr2); \
1926         if (share->param_name < min_val) \
1927           share->param_name = min_val; \
1928         connect_string_parse.set_param_value(tmp_ptr2, \
1929                                              tmp_ptr2 + \
1930                                                strlen(tmp_ptr2) + 1); \
1931       } else { \
1932         error_num = connect_string_parse.print_param_error(); \
1933         goto error; \
1934       } \
1935       DBUG_PRINT("info",("spider " title_name "=%f", share->param_name)); \
1936     } \
1937     break; \
1938   }
1939 #define SPIDER_PARAM_LONGLONG(title_name, param_name, min_val) \
1940   if (!strncasecmp(tmp_ptr, title_name, title_length)) \
1941   { \
1942     DBUG_PRINT("info",("spider " title_name " start")); \
1943     if (share->param_name == -1) \
1944     { \
1945       if ((tmp_ptr2 = spider_get_string_between_quote( \
1946         start_ptr, FALSE))) \
1947       { \
1948         share->param_name = my_strtoll10(tmp_ptr2, (char**) NULL, &error_num); \
1949         if (share->param_name < min_val) \
1950           share->param_name = min_val; \
1951         connect_string_parse.set_param_value(tmp_ptr2, \
1952                                              tmp_ptr2 + \
1953                                                strlen(tmp_ptr2) + 1); \
1954       } else { \
1955         error_num = connect_string_parse.print_param_error(); \
1956         goto error; \
1957       } \
1958       DBUG_PRINT("info",("spider " title_name "=%lld", share->param_name)); \
1959     } \
1960     break; \
1961   }
1962 
spider_parse_connect_info(SPIDER_SHARE * share,TABLE_SHARE * table_share,partition_info * part_info,uint create_table)1963 int spider_parse_connect_info(
1964   SPIDER_SHARE *share,
1965   TABLE_SHARE *table_share,
1966 #ifdef WITH_PARTITION_STORAGE_ENGINE
1967   partition_info *part_info,
1968 #endif
1969   uint create_table
1970 ) {
1971   int error_num = 0;
1972   char *connect_string = NULL;
1973   char *sprit_ptr[2];
1974   char *tmp_ptr, *tmp_ptr2, *start_ptr;
1975   int roop_count;
1976   int title_length;
1977   SPIDER_PARAM_STRING_PARSE connect_string_parse;
1978   SPIDER_ALTER_TABLE *share_alter;
1979 #ifdef WITH_PARTITION_STORAGE_ENGINE
1980   partition_element *part_elem;
1981   partition_element *sub_elem;
1982 #endif
1983   DBUG_ENTER("spider_parse_connect_info");
1984 #ifdef WITH_PARTITION_STORAGE_ENGINE
1985 #if MYSQL_VERSION_ID < 50500
1986   DBUG_PRINT("info",("spider partition_info=%s", table_share->partition_info));
1987 #else
1988   DBUG_PRINT("info",("spider partition_info=%s",
1989     table_share->partition_info_str));
1990 #endif
1991   DBUG_PRINT("info",("spider part_info=%p", part_info));
1992 #endif
1993   DBUG_PRINT("info",("spider s->db=%s", table_share->db.str));
1994   DBUG_PRINT("info",("spider s->table_name=%s", table_share->table_name.str));
1995   DBUG_PRINT("info",("spider s->path=%s", table_share->path.str));
1996   DBUG_PRINT("info",
1997     ("spider s->normalized_path=%s", table_share->normalized_path.str));
1998 #ifdef WITH_PARTITION_STORAGE_ENGINE
1999   spider_get_partition_info(share->table_name, share->table_name_length,
2000     table_share, part_info, &part_elem, &sub_elem);
2001 #endif
2002 #ifndef WITHOUT_SPIDER_BG_SEARCH
2003   share->sts_bg_mode = -1;
2004 #endif
2005   share->sts_interval = -1;
2006   share->sts_mode = -1;
2007 #ifdef WITH_PARTITION_STORAGE_ENGINE
2008   share->sts_sync = -1;
2009 #endif
2010   share->store_last_sts = -1;
2011   share->load_sts_at_startup = -1;
2012 #ifndef WITHOUT_SPIDER_BG_SEARCH
2013   share->crd_bg_mode = -1;
2014 #endif
2015   share->crd_interval = -1;
2016   share->crd_mode = -1;
2017 #ifdef WITH_PARTITION_STORAGE_ENGINE
2018   share->crd_sync = -1;
2019 #endif
2020   share->store_last_crd = -1;
2021   share->load_crd_at_startup = -1;
2022   share->crd_type = -1;
2023   share->crd_weight = -1;
2024   share->internal_offset = -1;
2025   share->internal_limit = -1;
2026   share->split_read = -1;
2027   share->semi_split_read = -1;
2028   share->semi_split_read_limit = -1;
2029   share->init_sql_alloc_size = -1;
2030   share->reset_sql_alloc = -1;
2031   share->multi_split_read = -1;
2032   share->max_order = -1;
2033   share->semi_table_lock = -1;
2034   share->semi_table_lock_conn = -1;
2035   share->selupd_lock_mode = -1;
2036   share->query_cache = -1;
2037   share->query_cache_sync = -1;
2038   share->internal_delayed = -1;
2039   share->bulk_size = -1;
2040   share->bulk_update_mode = -1;
2041   share->bulk_update_size = -1;
2042   share->internal_optimize = -1;
2043   share->internal_optimize_local = -1;
2044   share->scan_rate = -1;
2045   share->read_rate = -1;
2046   share->priority = -1;
2047   share->quick_mode = -1;
2048   share->quick_page_size = -1;
2049   share->quick_page_byte = -1;
2050   share->low_mem_read = -1;
2051   share->table_count_mode = -1;
2052   share->select_column_mode = -1;
2053 #ifndef WITHOUT_SPIDER_BG_SEARCH
2054   share->bgs_mode = -1;
2055   share->bgs_first_read = -1;
2056   share->bgs_second_read = -1;
2057 #endif
2058   share->first_read = -1;
2059   share->second_read = -1;
2060   share->auto_increment_mode = -1;
2061   share->use_table_charset = -1;
2062   share->use_pushdown_udf = -1;
2063   share->skip_default_condition = -1;
2064   share->skip_parallel_search = -1;
2065   share->direct_dup_insert = -1;
2066   share->direct_order_limit = -1;
2067   share->bka_mode = -1;
2068   share->read_only_mode = -1;
2069   share->error_read_mode = -1;
2070   share->error_write_mode = -1;
2071   share->active_link_count = -1;
2072 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2073   share->hs_result_free_size = -1;
2074 #endif
2075 #ifdef HA_CAN_BULK_ACCESS
2076   share->bulk_access_free = -1;
2077 #endif
2078 #ifdef HA_CAN_FORCE_BULK_UPDATE
2079   share->force_bulk_update = -1;
2080 #endif
2081 #ifdef HA_CAN_FORCE_BULK_DELETE
2082   share->force_bulk_delete = -1;
2083 #endif
2084   share->casual_read = -1;
2085   share->delete_all_rows_type = -1;
2086   share->static_records_for_status = -1;
2087   share->static_mean_rec_length = -1;
2088   for (roop_count = 0; roop_count < (int) table_share->keys; roop_count++)
2089   {
2090     share->static_key_cardinality[roop_count] = -1;
2091   }
2092 
2093 #ifdef WITH_PARTITION_STORAGE_ENGINE
2094   for (roop_count = 4; roop_count > 0; roop_count--)
2095 #else
2096   for (roop_count = 2; roop_count > 0; roop_count--)
2097 #endif
2098   {
2099     if (connect_string)
2100     {
2101       spider_free(spider_current_trx, connect_string, MYF(0));
2102       connect_string = NULL;
2103     }
2104     switch (roop_count)
2105     {
2106 #ifdef WITH_PARTITION_STORAGE_ENGINE
2107       case 4:
2108         if (!sub_elem || !sub_elem->part_comment)
2109           continue;
2110         DBUG_PRINT("info",("spider create sub comment string"));
2111         if (
2112           !(connect_string = spider_create_string(
2113             sub_elem->part_comment,
2114             strlen(sub_elem->part_comment)))
2115         ) {
2116           error_num = HA_ERR_OUT_OF_MEM;
2117           goto error_alloc_conn_string;
2118         }
2119         DBUG_PRINT("info",("spider sub comment string=%s", connect_string));
2120         break;
2121       case 3:
2122         if (!part_elem || !part_elem->part_comment)
2123           continue;
2124         DBUG_PRINT("info",("spider create part comment string"));
2125         if (
2126           !(connect_string = spider_create_string(
2127             part_elem->part_comment,
2128             strlen(part_elem->part_comment)))
2129         ) {
2130           error_num = HA_ERR_OUT_OF_MEM;
2131           goto error_alloc_conn_string;
2132         }
2133         DBUG_PRINT("info",("spider part comment string=%s", connect_string));
2134         break;
2135 #endif
2136       case 2:
2137         if (table_share->comment.length == 0)
2138           continue;
2139         DBUG_PRINT("info",("spider create comment string"));
2140         if (
2141           !(connect_string = spider_create_string(
2142             table_share->comment.str,
2143             table_share->comment.length))
2144         ) {
2145           error_num = HA_ERR_OUT_OF_MEM;
2146           goto error_alloc_conn_string;
2147         }
2148         DBUG_PRINT("info",("spider comment string=%s", connect_string));
2149         break;
2150       default:
2151         if (table_share->connect_string.length == 0)
2152           continue;
2153         DBUG_PRINT("info",("spider create connect_string string"));
2154         if (
2155           !(connect_string = spider_create_string(
2156             table_share->connect_string.str,
2157             table_share->connect_string.length))
2158         ) {
2159           error_num = HA_ERR_OUT_OF_MEM;
2160           goto error_alloc_conn_string;
2161         }
2162         DBUG_PRINT("info",("spider connect_string=%s", connect_string));
2163         break;
2164     }
2165 
2166     sprit_ptr[0] = connect_string;
2167     connect_string_parse.init(connect_string, ER_SPIDER_INVALID_CONNECT_INFO_NUM);
2168     while (sprit_ptr[0])
2169     {
2170       if ((sprit_ptr[1] = strchr(sprit_ptr[0], ',')))
2171       {
2172         *sprit_ptr[1] = '\0';
2173         sprit_ptr[1]++;
2174       }
2175       tmp_ptr = sprit_ptr[0];
2176       sprit_ptr[0] = sprit_ptr[1];
2177       while (*tmp_ptr == ' ' || *tmp_ptr == '\r' ||
2178         *tmp_ptr == '\n' || *tmp_ptr == '\t')
2179         tmp_ptr++;
2180 
2181       if (*tmp_ptr == '\0')
2182         continue;
2183 
2184       title_length = 0;
2185       start_ptr = tmp_ptr;
2186       while (*start_ptr != ' ' && *start_ptr != '\'' &&
2187         *start_ptr != '"' && *start_ptr != '\0' &&
2188         *start_ptr != '\r' && *start_ptr != '\n' &&
2189         *start_ptr != '\t')
2190       {
2191         title_length++;
2192         start_ptr++;
2193       }
2194       connect_string_parse.set_param_title(tmp_ptr, tmp_ptr + title_length);
2195 
2196       switch (title_length)
2197       {
2198         case 0:
2199           error_num = connect_string_parse.print_param_error();
2200           if (error_num)
2201             goto error;
2202           continue;
2203         case 3:
2204           SPIDER_PARAM_LONG_LIST_WITH_MAX("abl", access_balances, 0,
2205             2147483647);
2206           SPIDER_PARAM_INT_WITH_MAX("aim", auto_increment_mode, 0, 3);
2207           SPIDER_PARAM_INT("alc", active_link_count, 1);
2208 #ifdef HA_CAN_BULK_ACCESS
2209           SPIDER_PARAM_INT_WITH_MAX("baf", bulk_access_free, 0, 1);
2210 #endif
2211 #ifndef WITHOUT_SPIDER_BG_SEARCH
2212           SPIDER_PARAM_LONGLONG("bfr", bgs_first_read, 0);
2213           SPIDER_PARAM_INT("bmd", bgs_mode, 0);
2214           SPIDER_PARAM_LONGLONG("bsr", bgs_second_read, 0);
2215 #endif
2216           SPIDER_PARAM_STR("bke", bka_engine);
2217           SPIDER_PARAM_INT_WITH_MAX("bkm", bka_mode, 0, 2);
2218           SPIDER_PARAM_INT("bsz", bulk_size, 0);
2219           SPIDER_PARAM_LONG_LIST_WITH_MAX("btt", bka_table_name_types,
2220             0, 1);
2221           SPIDER_PARAM_INT_WITH_MAX("bum", bulk_update_mode, 0, 2);
2222           SPIDER_PARAM_INT("bus", bulk_update_size, 0);
2223 #ifndef WITHOUT_SPIDER_BG_SEARCH
2224           SPIDER_PARAM_INT_WITH_MAX("cbm", crd_bg_mode, 0, 2);
2225 #endif
2226           SPIDER_PARAM_DOUBLE("civ", crd_interval, 0);
2227           SPIDER_PARAM_INT_WITH_MAX("cmd", crd_mode, 0, 3);
2228           SPIDER_PARAM_INT_WITH_MAX("csr", casual_read, 0, 63);
2229 #ifdef WITH_PARTITION_STORAGE_ENGINE
2230           SPIDER_PARAM_INT_WITH_MAX("csy", crd_sync, 0, 2);
2231 #endif
2232           SPIDER_PARAM_LONG_LIST_WITH_MAX("cto", connect_timeouts, 0,
2233             2147483647);
2234           SPIDER_PARAM_INT_WITH_MAX("ctp", crd_type, 0, 2);
2235           SPIDER_PARAM_DOUBLE("cwg", crd_weight, 1);
2236           SPIDER_PARAM_INT_WITH_MAX("dat", delete_all_rows_type, 0, 1);
2237           SPIDER_PARAM_INT_WITH_MAX("ddi", direct_dup_insert, 0, 1);
2238           SPIDER_PARAM_STR_LIST("dff", tgt_default_files);
2239           SPIDER_PARAM_STR_LIST("dfg", tgt_default_groups);
2240           SPIDER_PARAM_LONGLONG("dol", direct_order_limit, 0);
2241           SPIDER_PARAM_INT_WITH_MAX("erm", error_read_mode, 0, 1);
2242           SPIDER_PARAM_INT_WITH_MAX("ewm", error_write_mode, 0, 1);
2243 #ifdef HA_CAN_FORCE_BULK_DELETE
2244           SPIDER_PARAM_INT_WITH_MAX("fbd", force_bulk_delete, 0, 1);
2245 #endif
2246 #ifdef HA_CAN_FORCE_BULK_UPDATE
2247           SPIDER_PARAM_INT_WITH_MAX("fbu", force_bulk_update, 0, 1);
2248 #endif
2249           SPIDER_PARAM_LONGLONG("frd", first_read, 0);
2250 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2251           SPIDER_PARAM_LONGLONG("hrf", hs_result_free_size, 0);
2252           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2253             "hrp", hs_read_ports, 0, 65535);
2254           SPIDER_PARAM_STR_LIST("hrs", hs_read_socks);
2255           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2256             "hwp", hs_write_ports, 0, 65535);
2257           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2258             "hwr", hs_write_to_reads, 0, 1);
2259           SPIDER_PARAM_STR_LIST("hws", hs_write_socks);
2260 #endif
2261           SPIDER_PARAM_INT("isa", init_sql_alloc_size, 0);
2262           SPIDER_PARAM_INT_WITH_MAX("idl", internal_delayed, 0, 1);
2263           SPIDER_PARAM_LONGLONG("ilm", internal_limit, 0);
2264           SPIDER_PARAM_LONGLONG("ios", internal_offset, 0);
2265           SPIDER_PARAM_INT_WITH_MAX("iom", internal_optimize, 0, 1);
2266           SPIDER_PARAM_INT_WITH_MAX("iol", internal_optimize_local, 0, 1);
2267           SPIDER_PARAM_INT_WITH_MAX("lmr", low_mem_read, 0, 1);
2268           SPIDER_PARAM_INT_WITH_MAX("lcs", load_crd_at_startup, 0, 1);
2269           SPIDER_PARAM_INT_WITH_MAX("lss", load_sts_at_startup, 0, 1);
2270           SPIDER_PARAM_LONG_LIST_WITH_MAX("lst", link_statuses, 0, 3);
2271 #ifndef WITHOUT_SPIDER_BG_SEARCH
2272           SPIDER_PARAM_LONG_LIST_WITH_MAX("mbf", monitoring_bg_flag, 0, 1);
2273           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2274             "mbi", monitoring_bg_interval, 0, 4294967295LL);
2275           SPIDER_PARAM_LONG_LIST_WITH_MAX("mbk", monitoring_bg_kind, 0, 3);
2276 #endif
2277           SPIDER_PARAM_LONG_LIST_WITH_MAX("mbp", monitoring_binlog_pos_at_failing, 0, 2);
2278           SPIDER_PARAM_LONG_LIST_WITH_MAX("mfg", monitoring_flag, 0, 1);
2279           SPIDER_PARAM_LONG_LIST_WITH_MAX("mkd", monitoring_kind, 0, 3);
2280           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2281             "mlt", monitoring_limit, 0, 9223372036854775807LL);
2282           SPIDER_PARAM_INT("mod", max_order, 0);
2283           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2284             "msi", monitoring_sid, 0, 4294967295LL);
2285           SPIDER_PARAM_INT_WITH_MAX("msr", multi_split_read, 0, 2147483647);
2286           SPIDER_PARAM_LONG_LIST_WITH_MAX("nrt", net_read_timeouts, 0,
2287             2147483647);
2288           SPIDER_PARAM_LONG_LIST_WITH_MAX("nwt", net_write_timeouts, 0,
2289             2147483647);
2290           SPIDER_PARAM_STR_LIST("pkn", tgt_pk_names);
2291           SPIDER_PARAM_LONGLONG("prt", priority, 0);
2292           SPIDER_PARAM_INT_WITH_MAX("qch", query_cache, 0, 2);
2293           SPIDER_PARAM_INT_WITH_MAX("qcs", query_cache_sync, 0, 3);
2294           SPIDER_PARAM_INT_WITH_MAX("qmd", quick_mode, 0, 3);
2295           SPIDER_PARAM_LONGLONG("qpb", quick_page_byte, 0);
2296           SPIDER_PARAM_LONGLONG("qps", quick_page_size, 0);
2297           SPIDER_PARAM_INT_WITH_MAX("rom", read_only_mode, 0, 1);
2298           SPIDER_PARAM_DOUBLE("rrt", read_rate, 0);
2299           SPIDER_PARAM_INT_WITH_MAX("rsa", reset_sql_alloc, 0, 1);
2300 #ifndef WITHOUT_SPIDER_BG_SEARCH
2301           SPIDER_PARAM_INT_WITH_MAX("sbm", sts_bg_mode, 0, 2);
2302 #endif
2303           SPIDER_PARAM_STR_LIST("sca", tgt_ssl_cas);
2304           SPIDER_PARAM_STR_LIST("sch", tgt_ssl_ciphers);
2305           SPIDER_PARAM_INT_WITH_MAX("scm", select_column_mode, 0, 1);
2306           SPIDER_PARAM_STR_LIST("scp", tgt_ssl_capaths);
2307           SPIDER_PARAM_STR_LIST("scr", tgt_ssl_certs);
2308           SPIDER_PARAM_INT_WITH_MAX("sdc", skip_default_condition, 0, 1);
2309           SPIDER_PARAM_DOUBLE("siv", sts_interval, 0);
2310           SPIDER_PARAM_STR_LIST("sky", tgt_ssl_keys);
2311           SPIDER_PARAM_STR_LIST("sli", static_link_ids);
2312           SPIDER_PARAM_INT_WITH_MAX("slc", store_last_crd, 0, 1);
2313           SPIDER_PARAM_INT_WITH_MAX("slm", selupd_lock_mode, 0, 2);
2314           SPIDER_PARAM_INT_WITH_MAX("sls", store_last_sts, 0, 1);
2315           SPIDER_PARAM_INT_WITH_MAX("smd", sts_mode, 1, 2);
2316           SPIDER_PARAM_LONGLONG("smr", static_mean_rec_length, 0);
2317           SPIDER_PARAM_LONGLONG("spr", split_read, 0);
2318           SPIDER_PARAM_INT_WITH_MAX("sps", skip_parallel_search, 0, 3);
2319           SPIDER_PARAM_STR_LIST("sqn", tgt_sequence_names);
2320           SPIDER_PARAM_LONGLONG("srd", second_read, 0);
2321           SPIDER_PARAM_DOUBLE("srt", scan_rate, 0);
2322           SPIDER_PARAM_STR_LIST("srv", server_names);
2323           SPIDER_PARAM_DOUBLE("ssr", semi_split_read, 0);
2324           SPIDER_PARAM_LONGLONG("ssl", semi_split_read_limit, 0);
2325 #ifdef WITH_PARTITION_STORAGE_ENGINE
2326           SPIDER_PARAM_INT_WITH_MAX("ssy", sts_sync, 0, 2);
2327 #endif
2328           SPIDER_PARAM_INT_WITH_MAX("stc", semi_table_lock_conn, 0, 1);
2329           SPIDER_PARAM_INT_WITH_MAX("stl", semi_table_lock, 0, 1);
2330           SPIDER_PARAM_LONGLONG("srs", static_records_for_status, 0);
2331           SPIDER_PARAM_LONG_LIST_WITH_MAX("svc", tgt_ssl_vscs, 0, 1);
2332           SPIDER_PARAM_STR_LIST("tbl", tgt_table_names);
2333           SPIDER_PARAM_INT_WITH_MAX("tcm", table_count_mode, 0, 3);
2334           SPIDER_PARAM_LONG_LIST_WITH_MAX("uhd", use_handlers, 0, 3);
2335 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2336           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2337             "uhr", use_hs_reads, 0, 1);
2338           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2339             "uhw", use_hs_writes, 0, 1);
2340 #endif
2341           SPIDER_PARAM_INT_WITH_MAX("upu", use_pushdown_udf, 0, 1);
2342           SPIDER_PARAM_INT_WITH_MAX("utc", use_table_charset, 0, 1);
2343           error_num = connect_string_parse.print_param_error();
2344           goto error;
2345         case 4:
2346           SPIDER_PARAM_STR_LIST("host", tgt_hosts);
2347           SPIDER_PARAM_STR_LIST("user", tgt_usernames);
2348           SPIDER_PARAM_LONG_LIST_WITH_MAX("port", tgt_ports, 0, 65535);
2349           error_num = connect_string_parse.print_param_error();
2350           goto error;
2351         case 5:
2352           SPIDER_PARAM_STR_LIST("table", tgt_table_names);
2353           error_num = connect_string_parse.print_param_error();
2354           goto error;
2355         case 6:
2356           SPIDER_PARAM_STR_LIST("server", server_names);
2357           SPIDER_PARAM_STR_LIST("socket", tgt_sockets);
2358           SPIDER_PARAM_HINT("idx", key_hint, 3, (int) table_share->keys,
2359             spider_db_append_key_hint);
2360           SPIDER_PARAM_STR_LIST("ssl_ca", tgt_ssl_cas);
2361           SPIDER_PARAM_NUMHINT("skc", static_key_cardinality, 3,
2362             (int) table_share->keys, spider_set_ll_value);
2363           error_num = connect_string_parse.print_param_error();
2364           goto error;
2365         case 7:
2366           SPIDER_PARAM_STR_LIST("wrapper", tgt_wrappers);
2367           SPIDER_PARAM_STR_LIST("ssl_key", tgt_ssl_keys);
2368           SPIDER_PARAM_STR_LIST("pk_name", tgt_pk_names);
2369           error_num = connect_string_parse.print_param_error();
2370           goto error;
2371         case 8:
2372           SPIDER_PARAM_STR_LIST("database", tgt_dbs);
2373           SPIDER_PARAM_STR_LIST("password", tgt_passwords);
2374           SPIDER_PARAM_INT_WITH_MAX("sts_mode", sts_mode, 1, 2);
2375 #ifdef WITH_PARTITION_STORAGE_ENGINE
2376           SPIDER_PARAM_INT_WITH_MAX("sts_sync", sts_sync, 0, 2);
2377 #endif
2378           SPIDER_PARAM_INT_WITH_MAX("crd_mode", crd_mode, 0, 3);
2379 #ifdef WITH_PARTITION_STORAGE_ENGINE
2380           SPIDER_PARAM_INT_WITH_MAX("crd_sync", crd_sync, 0, 2);
2381 #endif
2382           SPIDER_PARAM_INT_WITH_MAX("crd_type", crd_type, 0, 2);
2383           SPIDER_PARAM_LONGLONG("priority", priority, 0);
2384 #ifndef WITHOUT_SPIDER_BG_SEARCH
2385           SPIDER_PARAM_INT("bgs_mode", bgs_mode, 0);
2386 #endif
2387           SPIDER_PARAM_STR_LIST("ssl_cert", tgt_ssl_certs);
2388           SPIDER_PARAM_INT_WITH_MAX("bka_mode", bka_mode, 0, 2);
2389           error_num = connect_string_parse.print_param_error();
2390           goto error;
2391         case 9:
2392           SPIDER_PARAM_INT("max_order", max_order, 0);
2393           SPIDER_PARAM_INT("bulk_size", bulk_size, 0);
2394           SPIDER_PARAM_DOUBLE("scan_rate", scan_rate, 0);
2395           SPIDER_PARAM_DOUBLE("read_rate", read_rate, 0);
2396           error_num = connect_string_parse.print_param_error();
2397           goto error;
2398         case 10:
2399           SPIDER_PARAM_DOUBLE("crd_weight", crd_weight, 1);
2400           SPIDER_PARAM_LONGLONG("split_read", split_read, 0);
2401           SPIDER_PARAM_INT_WITH_MAX("quick_mode", quick_mode, 0, 3);
2402           SPIDER_PARAM_STR_LIST("ssl_cipher", tgt_ssl_ciphers);
2403           SPIDER_PARAM_STR_LIST("ssl_capath", tgt_ssl_capaths);
2404           SPIDER_PARAM_STR("bka_engine", bka_engine);
2405           SPIDER_PARAM_LONGLONG("first_read", first_read, 0);
2406           error_num = connect_string_parse.print_param_error();
2407           goto error;
2408         case 11:
2409           SPIDER_PARAM_INT_WITH_MAX("query_cache", query_cache, 0, 2);
2410 #ifndef WITHOUT_SPIDER_BG_SEARCH
2411           SPIDER_PARAM_INT_WITH_MAX("crd_bg_mode", crd_bg_mode, 0, 2);
2412           SPIDER_PARAM_INT_WITH_MAX("sts_bg_mode", sts_bg_mode, 0, 2);
2413 #endif
2414           SPIDER_PARAM_LONG_LIST_WITH_MAX("link_status", link_statuses, 0, 3);
2415           SPIDER_PARAM_LONG_LIST_WITH_MAX("use_handler", use_handlers, 0, 3);
2416 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2417           SPIDER_PARAM_LONG_LIST_WITH_MAX("use_hs_read", use_hs_reads, 0, 1);
2418 #endif
2419           SPIDER_PARAM_INT_WITH_MAX("casual_read", casual_read, 0, 63);
2420           error_num = connect_string_parse.print_param_error();
2421           goto error;
2422         case 12:
2423           SPIDER_PARAM_DOUBLE("sts_interval", sts_interval, 0);
2424           SPIDER_PARAM_DOUBLE("crd_interval", crd_interval, 0);
2425           SPIDER_PARAM_INT_WITH_MAX("low_mem_read", low_mem_read, 0, 1);
2426           SPIDER_PARAM_STR_LIST("default_file", tgt_default_files);
2427 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2428           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2429             "use_hs_write", use_hs_writes, 0, 1);
2430           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2431             "hs_read_port", hs_read_ports, 0, 65535);
2432 #endif
2433           error_num = connect_string_parse.print_param_error();
2434           goto error;
2435         case 13:
2436           SPIDER_PARAM_STR_LIST("default_group", tgt_default_groups);
2437 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2438           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2439             "hs_write_port", hs_write_ports, 0, 65535);
2440 #endif
2441           SPIDER_PARAM_STR_LIST("sequence_name", tgt_sequence_names);
2442           error_num = connect_string_parse.print_param_error();
2443           goto error;
2444         case 14:
2445           SPIDER_PARAM_LONGLONG("internal_limit", internal_limit, 0);
2446 #ifndef WITHOUT_SPIDER_BG_SEARCH
2447           SPIDER_PARAM_LONGLONG("bgs_first_read", bgs_first_read, 0);
2448 #endif
2449 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2450           SPIDER_PARAM_STR_LIST(
2451             "hs_read_socket", hs_read_socks);
2452 #endif
2453           SPIDER_PARAM_INT_WITH_MAX("read_only_mode", read_only_mode, 0, 1);
2454           SPIDER_PARAM_LONG_LIST_WITH_MAX("access_balance", access_balances, 0,
2455             2147483647);
2456           SPIDER_PARAM_STR_LIST("static_link_id", static_link_ids);
2457           SPIDER_PARAM_INT_WITH_MAX("store_last_crd", store_last_crd, 0, 1);
2458           SPIDER_PARAM_INT_WITH_MAX("store_last_sts", store_last_sts, 0, 1);
2459           error_num = connect_string_parse.print_param_error();
2460           goto error;
2461         case 15:
2462           SPIDER_PARAM_LONGLONG("internal_offset", internal_offset, 0);
2463           SPIDER_PARAM_INT_WITH_MAX("reset_sql_alloc", reset_sql_alloc, 0, 1);
2464           SPIDER_PARAM_INT_WITH_MAX("semi_table_lock", semi_table_lock, 0, 1);
2465           SPIDER_PARAM_LONGLONG("quick_page_byte", quick_page_byte, 0);
2466           SPIDER_PARAM_LONGLONG("quick_page_size", quick_page_size, 0);
2467 #ifndef WITHOUT_SPIDER_BG_SEARCH
2468           SPIDER_PARAM_LONGLONG("bgs_second_read", bgs_second_read, 0);
2469 #endif
2470           SPIDER_PARAM_LONG_LIST_WITH_MAX("monitoring_flag", monitoring_flag, 0, 1);
2471           SPIDER_PARAM_LONG_LIST_WITH_MAX("monitoring_kind", monitoring_kind, 0, 3);
2472           SPIDER_PARAM_DOUBLE("semi_split_read", semi_split_read, 0);
2473 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2474           SPIDER_PARAM_STR_LIST(
2475             "hs_write_socket", hs_write_socks);
2476 #endif
2477           SPIDER_PARAM_LONG_LIST_WITH_MAX("connect_timeout", connect_timeouts,
2478             0, 2147483647);
2479           SPIDER_PARAM_INT_WITH_MAX("error_read_mode", error_read_mode, 0, 1);
2480           error_num = connect_string_parse.print_param_error();
2481           goto error;
2482         case 16:
2483           SPIDER_PARAM_INT_WITH_MAX(
2484             "multi_split_read", multi_split_read, 0, 2147483647);
2485           SPIDER_PARAM_INT_WITH_MAX(
2486             "selupd_lock_mode", selupd_lock_mode, 0, 2);
2487           SPIDER_PARAM_INT_WITH_MAX(
2488             "internal_delayed", internal_delayed, 0, 1);
2489           SPIDER_PARAM_INT_WITH_MAX(
2490             "table_count_mode", table_count_mode, 0, 3);
2491           SPIDER_PARAM_INT_WITH_MAX(
2492             "use_pushdown_udf", use_pushdown_udf, 0, 1);
2493           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2494             "monitoring_limit", monitoring_limit, 0, 9223372036854775807LL);
2495           SPIDER_PARAM_INT_WITH_MAX(
2496             "bulk_update_mode", bulk_update_mode, 0, 2);
2497           SPIDER_PARAM_INT("bulk_update_size", bulk_update_size, 0);
2498           SPIDER_PARAM_LONG_LIST_WITH_MAX("net_read_timeout",
2499             net_read_timeouts, 0, 2147483647);
2500 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2501           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2502             "hs_write_to_read", hs_write_to_reads, 0, 1);
2503 #endif
2504           SPIDER_PARAM_INT_WITH_MAX(
2505             "error_write_mode", error_write_mode, 0, 1);
2506 #ifdef HA_CAN_BULK_ACCESS
2507           SPIDER_PARAM_INT_WITH_MAX(
2508             "bulk_access_free", bulk_access_free, 0, 1);
2509 #endif
2510           SPIDER_PARAM_INT_WITH_MAX(
2511             "query_cache_sync", query_cache_sync, 0, 3);
2512           error_num = connect_string_parse.print_param_error();
2513           goto error;
2514         case 17:
2515           SPIDER_PARAM_INT_WITH_MAX(
2516             "internal_optimize", internal_optimize, 0, 1);
2517           SPIDER_PARAM_INT_WITH_MAX(
2518             "use_table_charset", use_table_charset, 0, 1);
2519           SPIDER_PARAM_INT_WITH_MAX(
2520             "direct_dup_insert", direct_dup_insert, 0, 1);
2521           SPIDER_PARAM_INT("active_link_count", active_link_count, 1);
2522           SPIDER_PARAM_LONG_LIST_WITH_MAX("net_write_timeout",
2523             net_write_timeouts, 0, 2147483647);
2524 #ifdef HA_CAN_FORCE_BULK_DELETE
2525           SPIDER_PARAM_INT_WITH_MAX(
2526             "force_bulk_delete", force_bulk_delete, 0, 1);
2527 #endif
2528 #ifdef HA_CAN_FORCE_BULK_UPDATE
2529           SPIDER_PARAM_INT_WITH_MAX(
2530             "force_bulk_update", force_bulk_update, 0, 1);
2531 #endif
2532           error_num = connect_string_parse.print_param_error();
2533           goto error;
2534         case 18:
2535           SPIDER_PARAM_INT_WITH_MAX(
2536             "select_column_mode", select_column_mode, 0, 1);
2537 #ifndef WITHOUT_SPIDER_BG_SEARCH
2538           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2539             "monitoring_bg_flag", monitoring_bg_flag, 0, 1);
2540           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2541             "monitoring_bg_kind", monitoring_bg_kind, 0, 3);
2542 #endif
2543           SPIDER_PARAM_LONGLONG(
2544             "direct_order_limit", direct_order_limit, 0);
2545           error_num = connect_string_parse.print_param_error();
2546           goto error;
2547         case 19:
2548           SPIDER_PARAM_INT("init_sql_alloc_size", init_sql_alloc_size, 0);
2549           SPIDER_PARAM_INT_WITH_MAX(
2550             "auto_increment_mode", auto_increment_mode, 0, 3);
2551 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2552           SPIDER_PARAM_LONGLONG("hs_result_free_size", hs_result_free_size, 0);
2553 #endif
2554           SPIDER_PARAM_LONG_LIST_WITH_MAX("bka_table_name_type",
2555             bka_table_name_types, 0, 1);
2556           SPIDER_PARAM_INT_WITH_MAX(
2557             "load_crd_at_startup", load_crd_at_startup, 0, 1);
2558           SPIDER_PARAM_INT_WITH_MAX(
2559             "load_sts_at_startup", load_sts_at_startup, 0, 1);
2560           error_num = connect_string_parse.print_param_error();
2561           goto error;
2562         case 20:
2563           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2564             "monitoring_server_id", monitoring_sid, 0, 4294967295LL);
2565           SPIDER_PARAM_INT_WITH_MAX(
2566             "delete_all_rows_type", delete_all_rows_type, 0, 1);
2567           SPIDER_PARAM_INT_WITH_MAX(
2568             "skip_parallel_search", skip_parallel_search, 0, 3);
2569           error_num = connect_string_parse.print_param_error();
2570           goto error;
2571         case 21:
2572           SPIDER_PARAM_LONGLONG(
2573             "semi_split_read_limit", semi_split_read_limit, 0);
2574           error_num = connect_string_parse.print_param_error();
2575           goto error;
2576         case 22:
2577           SPIDER_PARAM_LONG_LIST_WITH_MAX(
2578             "ssl_verify_server_cert", tgt_ssl_vscs, 0, 1);
2579 #ifndef WITHOUT_SPIDER_BG_SEARCH
2580           SPIDER_PARAM_LONGLONG_LIST_WITH_MAX(
2581             "monitoring_bg_interval", monitoring_bg_interval, 0, 4294967295LL);
2582 #endif
2583           SPIDER_PARAM_INT_WITH_MAX(
2584             "skip_default_condition", skip_default_condition, 0, 1);
2585           SPIDER_PARAM_LONGLONG(
2586             "static_mean_rec_length", static_mean_rec_length, 0);
2587           error_num = connect_string_parse.print_param_error();
2588           goto error;
2589         case 23:
2590           SPIDER_PARAM_INT_WITH_MAX(
2591             "internal_optimize_local", internal_optimize_local, 0, 1);
2592           error_num = connect_string_parse.print_param_error();
2593           goto error;
2594         case 25:
2595           SPIDER_PARAM_LONGLONG("static_records_for_status",
2596             static_records_for_status, 0);
2597           SPIDER_PARAM_NUMHINT("static_key_cardinality", static_key_cardinality,
2598             3, (int) table_share->keys, spider_set_ll_value);
2599           error_num = connect_string_parse.print_param_error();
2600           goto error;
2601         case 26:
2602           SPIDER_PARAM_INT_WITH_MAX(
2603             "semi_table_lock_connection", semi_table_lock_conn, 0, 1);
2604           error_num = connect_string_parse.print_param_error();
2605           goto error;
2606         case 32:
2607           SPIDER_PARAM_LONG_LIST_WITH_MAX("monitoring_binlog_pos_at_failing",
2608             monitoring_binlog_pos_at_failing, 0, 2);
2609           error_num = connect_string_parse.print_param_error();
2610           goto error;
2611         default:
2612           error_num = connect_string_parse.print_param_error();
2613           goto error;
2614       }
2615 
2616       /* Verify that the remainder of the parameter value is whitespace */
2617       if ((error_num = connect_string_parse.has_extra_parameter_values()))
2618           goto error;
2619     }
2620   }
2621 
2622   /* check all_link_count */
2623   share->all_link_count = 1;
2624   if (share->all_link_count < share->server_names_length)
2625     share->all_link_count = share->server_names_length;
2626   if (share->all_link_count < share->tgt_table_names_length)
2627     share->all_link_count = share->tgt_table_names_length;
2628   if (share->all_link_count < share->tgt_dbs_length)
2629     share->all_link_count = share->tgt_dbs_length;
2630   if (share->all_link_count < share->tgt_hosts_length)
2631     share->all_link_count = share->tgt_hosts_length;
2632   if (share->all_link_count < share->tgt_usernames_length)
2633     share->all_link_count = share->tgt_usernames_length;
2634   if (share->all_link_count < share->tgt_passwords_length)
2635     share->all_link_count = share->tgt_passwords_length;
2636   if (share->all_link_count < share->tgt_sockets_length)
2637     share->all_link_count = share->tgt_sockets_length;
2638   if (share->all_link_count < share->tgt_wrappers_length)
2639     share->all_link_count = share->tgt_wrappers_length;
2640   if (share->all_link_count < share->tgt_ssl_cas_length)
2641     share->all_link_count = share->tgt_ssl_cas_length;
2642   if (share->all_link_count < share->tgt_ssl_capaths_length)
2643     share->all_link_count = share->tgt_ssl_capaths_length;
2644   if (share->all_link_count < share->tgt_ssl_certs_length)
2645     share->all_link_count = share->tgt_ssl_certs_length;
2646   if (share->all_link_count < share->tgt_ssl_ciphers_length)
2647     share->all_link_count = share->tgt_ssl_ciphers_length;
2648   if (share->all_link_count < share->tgt_ssl_keys_length)
2649     share->all_link_count = share->tgt_ssl_keys_length;
2650   if (share->all_link_count < share->tgt_default_files_length)
2651     share->all_link_count = share->tgt_default_files_length;
2652   if (share->all_link_count < share->tgt_default_groups_length)
2653     share->all_link_count = share->tgt_default_groups_length;
2654   if (share->all_link_count < share->tgt_pk_names_length)
2655     share->all_link_count = share->tgt_pk_names_length;
2656   if (share->all_link_count < share->tgt_sequence_names_length)
2657     share->all_link_count = share->tgt_sequence_names_length;
2658   if (share->all_link_count < share->static_link_ids_length)
2659     share->all_link_count = share->static_link_ids_length;
2660   if (share->all_link_count < share->tgt_ports_length)
2661     share->all_link_count = share->tgt_ports_length;
2662   if (share->all_link_count < share->tgt_ssl_vscs_length)
2663     share->all_link_count = share->tgt_ssl_vscs_length;
2664   if (share->all_link_count < share->link_statuses_length)
2665     share->all_link_count = share->link_statuses_length;
2666   if (share->all_link_count < share->monitoring_binlog_pos_at_failing_length)
2667     share->all_link_count = share->monitoring_binlog_pos_at_failing_length;
2668   if (share->all_link_count < share->monitoring_flag_length)
2669     share->all_link_count = share->monitoring_flag_length;
2670   if (share->all_link_count < share->monitoring_kind_length)
2671     share->all_link_count = share->monitoring_kind_length;
2672   if (share->all_link_count < share->monitoring_limit_length)
2673     share->all_link_count = share->monitoring_limit_length;
2674   if (share->all_link_count < share->monitoring_sid_length)
2675     share->all_link_count = share->monitoring_sid_length;
2676 #ifndef WITHOUT_SPIDER_BG_SEARCH
2677   if (share->all_link_count < share->monitoring_bg_flag_length)
2678     share->all_link_count = share->monitoring_bg_flag_length;
2679   if (share->all_link_count < share->monitoring_bg_kind_length)
2680     share->all_link_count = share->monitoring_bg_kind_length;
2681   if (share->all_link_count < share->monitoring_bg_interval_length)
2682     share->all_link_count = share->monitoring_bg_interval_length;
2683 #endif
2684 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2685   if (share->all_link_count < share->use_hs_reads_length)
2686     share->all_link_count = share->use_hs_reads_length;
2687   if (share->all_link_count < share->use_hs_writes_length)
2688     share->all_link_count = share->use_hs_writes_length;
2689   if (share->all_link_count < share->hs_read_ports_length)
2690     share->all_link_count = share->hs_read_ports_length;
2691   if (share->all_link_count < share->hs_write_ports_length)
2692     share->all_link_count = share->hs_write_ports_length;
2693   if (share->all_link_count < share->hs_read_socks_length)
2694     share->all_link_count = share->hs_read_socks_length;
2695   if (share->all_link_count < share->hs_write_socks_length)
2696     share->all_link_count = share->hs_write_socks_length;
2697   if (share->all_link_count < share->hs_write_to_reads_length)
2698     share->all_link_count = share->hs_write_to_reads_length;
2699 #endif
2700   if (share->all_link_count < share->use_handlers_length)
2701     share->all_link_count = share->use_handlers_length;
2702   if (share->all_link_count < share->connect_timeouts_length)
2703     share->all_link_count = share->connect_timeouts_length;
2704   if (share->all_link_count < share->net_read_timeouts_length)
2705     share->all_link_count = share->net_read_timeouts_length;
2706   if (share->all_link_count < share->net_write_timeouts_length)
2707     share->all_link_count = share->net_write_timeouts_length;
2708   if (share->all_link_count < share->access_balances_length)
2709     share->all_link_count = share->access_balances_length;
2710   if (share->all_link_count < share->bka_table_name_types_length)
2711     share->all_link_count = share->bka_table_name_types_length;
2712   if ((error_num = spider_increase_string_list(
2713     &share->server_names,
2714     &share->server_names_lengths,
2715     &share->server_names_length,
2716     &share->server_names_charlen,
2717     share->all_link_count)))
2718     goto error;
2719   if ((error_num = spider_increase_string_list(
2720     &share->tgt_table_names,
2721     &share->tgt_table_names_lengths,
2722     &share->tgt_table_names_length,
2723     &share->tgt_table_names_charlen,
2724     share->all_link_count)))
2725     goto error;
2726   if ((error_num = spider_increase_string_list(
2727     &share->tgt_dbs,
2728     &share->tgt_dbs_lengths,
2729     &share->tgt_dbs_length,
2730     &share->tgt_dbs_charlen,
2731     share->all_link_count)))
2732     goto error;
2733   if ((error_num = spider_increase_string_list(
2734     &share->tgt_hosts,
2735     &share->tgt_hosts_lengths,
2736     &share->tgt_hosts_length,
2737     &share->tgt_hosts_charlen,
2738     share->all_link_count)))
2739     goto error;
2740   if ((error_num = spider_increase_string_list(
2741     &share->tgt_usernames,
2742     &share->tgt_usernames_lengths,
2743     &share->tgt_usernames_length,
2744     &share->tgt_usernames_charlen,
2745     share->all_link_count)))
2746     goto error;
2747   if ((error_num = spider_increase_string_list(
2748     &share->tgt_passwords,
2749     &share->tgt_passwords_lengths,
2750     &share->tgt_passwords_length,
2751     &share->tgt_passwords_charlen,
2752     share->all_link_count)))
2753     goto error;
2754   if ((error_num = spider_increase_string_list(
2755     &share->tgt_sockets,
2756     &share->tgt_sockets_lengths,
2757     &share->tgt_sockets_length,
2758     &share->tgt_sockets_charlen,
2759     share->all_link_count)))
2760     goto error;
2761   if ((error_num = spider_increase_string_list(
2762     &share->tgt_wrappers,
2763     &share->tgt_wrappers_lengths,
2764     &share->tgt_wrappers_length,
2765     &share->tgt_wrappers_charlen,
2766     share->all_link_count)))
2767     goto error;
2768   if ((error_num = spider_increase_string_list(
2769     &share->tgt_ssl_cas,
2770     &share->tgt_ssl_cas_lengths,
2771     &share->tgt_ssl_cas_length,
2772     &share->tgt_ssl_cas_charlen,
2773     share->all_link_count)))
2774     goto error;
2775   if ((error_num = spider_increase_string_list(
2776     &share->tgt_ssl_capaths,
2777     &share->tgt_ssl_capaths_lengths,
2778     &share->tgt_ssl_capaths_length,
2779     &share->tgt_ssl_capaths_charlen,
2780     share->all_link_count)))
2781     goto error;
2782   if ((error_num = spider_increase_string_list(
2783     &share->tgt_ssl_certs,
2784     &share->tgt_ssl_certs_lengths,
2785     &share->tgt_ssl_certs_length,
2786     &share->tgt_ssl_certs_charlen,
2787     share->all_link_count)))
2788     goto error;
2789   if ((error_num = spider_increase_string_list(
2790     &share->tgt_ssl_ciphers,
2791     &share->tgt_ssl_ciphers_lengths,
2792     &share->tgt_ssl_ciphers_length,
2793     &share->tgt_ssl_ciphers_charlen,
2794     share->all_link_count)))
2795     goto error;
2796   if ((error_num = spider_increase_string_list(
2797     &share->tgt_ssl_keys,
2798     &share->tgt_ssl_keys_lengths,
2799     &share->tgt_ssl_keys_length,
2800     &share->tgt_ssl_keys_charlen,
2801     share->all_link_count)))
2802     goto error;
2803   if ((error_num = spider_increase_string_list(
2804     &share->tgt_default_files,
2805     &share->tgt_default_files_lengths,
2806     &share->tgt_default_files_length,
2807     &share->tgt_default_files_charlen,
2808     share->all_link_count)))
2809     goto error;
2810   if ((error_num = spider_increase_string_list(
2811     &share->tgt_default_groups,
2812     &share->tgt_default_groups_lengths,
2813     &share->tgt_default_groups_length,
2814     &share->tgt_default_groups_charlen,
2815     share->all_link_count)))
2816     goto error;
2817   if ((error_num = spider_increase_string_list(
2818     &share->tgt_pk_names,
2819     &share->tgt_pk_names_lengths,
2820     &share->tgt_pk_names_length,
2821     &share->tgt_pk_names_charlen,
2822     share->all_link_count)))
2823     goto error;
2824   if ((error_num = spider_increase_string_list(
2825     &share->tgt_sequence_names,
2826     &share->tgt_sequence_names_lengths,
2827     &share->tgt_sequence_names_length,
2828     &share->tgt_sequence_names_charlen,
2829     share->all_link_count)))
2830     goto error;
2831   if ((error_num = spider_increase_null_string_list(
2832     &share->static_link_ids,
2833     &share->static_link_ids_lengths,
2834     &share->static_link_ids_length,
2835     &share->static_link_ids_charlen,
2836     share->all_link_count)))
2837     goto error;
2838   if ((error_num = spider_increase_long_list(
2839     &share->tgt_ports,
2840     &share->tgt_ports_length,
2841     share->all_link_count)))
2842     goto error;
2843   if ((error_num = spider_increase_long_list(
2844     &share->tgt_ssl_vscs,
2845     &share->tgt_ssl_vscs_length,
2846     share->all_link_count)))
2847     goto error;
2848   if ((error_num = spider_increase_long_list(
2849     &share->link_statuses,
2850     &share->link_statuses_length,
2851     share->all_link_count)))
2852     goto error;
2853 #ifndef WITHOUT_SPIDER_BG_SEARCH
2854   if ((error_num = spider_increase_long_list(
2855     &share->monitoring_bg_flag,
2856     &share->monitoring_bg_flag_length,
2857     share->all_link_count)))
2858     goto error;
2859   if ((error_num = spider_increase_long_list(
2860     &share->monitoring_bg_kind,
2861     &share->monitoring_bg_kind_length,
2862     share->all_link_count)))
2863     goto error;
2864 #endif
2865   if ((error_num = spider_increase_long_list(
2866     &share->monitoring_binlog_pos_at_failing,
2867     &share->monitoring_binlog_pos_at_failing_length,
2868     share->all_link_count)))
2869     goto error;
2870   if ((error_num = spider_increase_long_list(
2871     &share->monitoring_flag,
2872     &share->monitoring_flag_length,
2873     share->all_link_count)))
2874     goto error;
2875   if ((error_num = spider_increase_long_list(
2876     &share->monitoring_kind,
2877     &share->monitoring_kind_length,
2878     share->all_link_count)))
2879     goto error;
2880 #ifndef WITHOUT_SPIDER_BG_SEARCH
2881   if ((error_num = spider_increase_longlong_list(
2882     &share->monitoring_bg_interval,
2883     &share->monitoring_bg_interval_length,
2884     share->all_link_count)))
2885     goto error;
2886 #endif
2887   if ((error_num = spider_increase_longlong_list(
2888     &share->monitoring_limit,
2889     &share->monitoring_limit_length,
2890     share->all_link_count)))
2891     goto error;
2892   if ((error_num = spider_increase_longlong_list(
2893     &share->monitoring_sid,
2894     &share->monitoring_sid_length,
2895     share->all_link_count)))
2896     goto error;
2897 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
2898   if ((error_num = spider_increase_long_list(
2899     &share->use_hs_reads,
2900     &share->use_hs_reads_length,
2901     share->all_link_count)))
2902     goto error;
2903   if ((error_num = spider_increase_long_list(
2904     &share->use_hs_writes,
2905     &share->use_hs_writes_length,
2906     share->all_link_count)))
2907     goto error;
2908   if ((error_num = spider_increase_long_list(
2909     &share->hs_read_ports,
2910     &share->hs_read_ports_length,
2911     share->all_link_count)))
2912     goto error;
2913   if ((error_num = spider_increase_long_list(
2914     &share->hs_write_ports,
2915     &share->hs_write_ports_length,
2916     share->all_link_count)))
2917     goto error;
2918   if ((error_num = spider_increase_string_list(
2919     &share->hs_read_socks,
2920     &share->hs_read_socks_lengths,
2921     &share->hs_read_socks_length,
2922     &share->hs_read_socks_charlen,
2923     share->all_link_count)))
2924     goto error;
2925   if ((error_num = spider_increase_string_list(
2926     &share->hs_write_socks,
2927     &share->hs_write_socks_lengths,
2928     &share->hs_write_socks_length,
2929     &share->hs_write_socks_charlen,
2930     share->all_link_count)))
2931     goto error;
2932   if ((error_num = spider_increase_long_list(
2933     &share->hs_write_to_reads,
2934     &share->hs_write_to_reads_length,
2935     share->all_link_count)))
2936     goto error;
2937 #endif
2938   if ((error_num = spider_increase_long_list(
2939     &share->use_handlers,
2940     &share->use_handlers_length,
2941     share->all_link_count)))
2942     goto error;
2943   if ((error_num = spider_increase_long_list(
2944     &share->connect_timeouts,
2945     &share->connect_timeouts_length,
2946     share->all_link_count)))
2947     goto error;
2948   if ((error_num = spider_increase_long_list(
2949     &share->net_read_timeouts,
2950     &share->net_read_timeouts_length,
2951     share->all_link_count)))
2952     goto error;
2953   if ((error_num = spider_increase_long_list(
2954     &share->net_write_timeouts,
2955     &share->net_write_timeouts_length,
2956     share->all_link_count)))
2957     goto error;
2958   if ((error_num = spider_increase_long_list(
2959     &share->access_balances,
2960     &share->access_balances_length,
2961     share->all_link_count)))
2962     goto error;
2963   if ((error_num = spider_increase_long_list(
2964     &share->bka_table_name_types,
2965     &share->bka_table_name_types_length,
2966     share->all_link_count)))
2967     goto error;
2968 
2969   /* copy for tables start */
2970   share_alter = &share->alter_table;
2971   share_alter->all_link_count = share->all_link_count;
2972   if (!(share_alter->tmp_server_names = (char **)
2973     spider_bulk_malloc(spider_current_trx, 43, MYF(MY_WME | MY_ZEROFILL),
2974       &share_alter->tmp_server_names,
2975       sizeof(char *) * 16 * share->all_link_count,
2976       &share_alter->tmp_server_names_lengths,
2977       sizeof(uint *) * 16 * share->all_link_count,
2978       &share_alter->tmp_tgt_ports,
2979       sizeof(long) * share->all_link_count,
2980       &share_alter->tmp_tgt_ssl_vscs,
2981       sizeof(long) * share->all_link_count,
2982       &share_alter->tmp_monitoring_binlog_pos_at_failing,
2983       sizeof(long) * share->all_link_count,
2984       &share_alter->tmp_link_statuses,
2985       sizeof(long) * share->all_link_count,
2986       NullS))
2987   ) {
2988     error_num = HA_ERR_OUT_OF_MEM;
2989     goto error;
2990   }
2991 
2992 
2993   memcpy(share_alter->tmp_server_names, share->server_names,
2994     sizeof(char *) * share->all_link_count);
2995   share_alter->tmp_tgt_table_names =
2996     share_alter->tmp_server_names + share->all_link_count;
2997   memcpy(share_alter->tmp_tgt_table_names, share->tgt_table_names,
2998     sizeof(char *) * share->all_link_count);
2999   share_alter->tmp_tgt_dbs =
3000     share_alter->tmp_tgt_table_names + share->all_link_count;
3001   memcpy(share_alter->tmp_tgt_dbs, share->tgt_dbs,
3002     sizeof(char *) * share->all_link_count);
3003   share_alter->tmp_tgt_hosts =
3004     share_alter->tmp_tgt_dbs + share->all_link_count;
3005   memcpy(share_alter->tmp_tgt_hosts, share->tgt_hosts,
3006     sizeof(char *) * share->all_link_count);
3007   share_alter->tmp_tgt_usernames =
3008     share_alter->tmp_tgt_hosts + share->all_link_count;
3009   memcpy(share_alter->tmp_tgt_usernames, share->tgt_usernames,
3010     sizeof(char *) * share->all_link_count);
3011   share_alter->tmp_tgt_passwords =
3012     share_alter->tmp_tgt_usernames + share->all_link_count;
3013   memcpy(share_alter->tmp_tgt_passwords, share->tgt_passwords,
3014     sizeof(char *) * share->all_link_count);
3015   share_alter->tmp_tgt_sockets =
3016     share_alter->tmp_tgt_passwords + share->all_link_count;
3017   memcpy(share_alter->tmp_tgt_sockets, share->tgt_sockets,
3018     sizeof(char *) * share->all_link_count);
3019   share_alter->tmp_tgt_wrappers =
3020     share_alter->tmp_tgt_sockets + share->all_link_count;
3021   memcpy(share_alter->tmp_tgt_wrappers, share->tgt_wrappers,
3022     sizeof(char *) * share->all_link_count);
3023   share_alter->tmp_tgt_ssl_cas =
3024     share_alter->tmp_tgt_wrappers + share->all_link_count;
3025   memcpy(share_alter->tmp_tgt_ssl_cas, share->tgt_ssl_cas,
3026     sizeof(char *) * share->all_link_count);
3027   share_alter->tmp_tgt_ssl_capaths =
3028     share_alter->tmp_tgt_ssl_cas + share->all_link_count;
3029   memcpy(share_alter->tmp_tgt_ssl_capaths, share->tgt_ssl_capaths,
3030     sizeof(char *) * share->all_link_count);
3031   share_alter->tmp_tgt_ssl_certs =
3032     share_alter->tmp_tgt_ssl_capaths + share->all_link_count;
3033   memcpy(share_alter->tmp_tgt_ssl_certs, share->tgt_ssl_certs,
3034     sizeof(char *) * share->all_link_count);
3035   share_alter->tmp_tgt_ssl_ciphers =
3036     share_alter->tmp_tgt_ssl_certs + share->all_link_count;
3037   memcpy(share_alter->tmp_tgt_ssl_ciphers, share->tgt_ssl_ciphers,
3038     sizeof(char *) * share->all_link_count);
3039   share_alter->tmp_tgt_ssl_keys =
3040     share_alter->tmp_tgt_ssl_ciphers + share->all_link_count;
3041   memcpy(share_alter->tmp_tgt_ssl_keys, share->tgt_ssl_keys,
3042     sizeof(char *) * share->all_link_count);
3043   share_alter->tmp_tgt_default_files =
3044     share_alter->tmp_tgt_ssl_keys + share->all_link_count;
3045   memcpy(share_alter->tmp_tgt_default_files, share->tgt_default_files,
3046     sizeof(char *) * share->all_link_count);
3047   share_alter->tmp_tgt_default_groups =
3048     share_alter->tmp_tgt_default_files + share->all_link_count;
3049   memcpy(share_alter->tmp_tgt_default_groups, share->tgt_default_groups,
3050     sizeof(char *) * share->all_link_count);
3051   share_alter->tmp_static_link_ids =
3052     share_alter->tmp_tgt_default_groups + share->all_link_count;
3053   memcpy(share_alter->tmp_static_link_ids, share->static_link_ids,
3054     sizeof(char *) * share->all_link_count);
3055 
3056   memcpy(share_alter->tmp_tgt_ports, share->tgt_ports,
3057     sizeof(long) * share->all_link_count);
3058   memcpy(share_alter->tmp_tgt_ssl_vscs, share->tgt_ssl_vscs,
3059     sizeof(long) * share->all_link_count);
3060   memcpy(share_alter->tmp_monitoring_binlog_pos_at_failing,
3061     share->monitoring_binlog_pos_at_failing,
3062     sizeof(long) * share->all_link_count);
3063   memcpy(share_alter->tmp_link_statuses, share->link_statuses,
3064     sizeof(long) * share->all_link_count);
3065 
3066   memcpy(share_alter->tmp_server_names_lengths,
3067     share->server_names_lengths,
3068     sizeof(uint) * share->all_link_count);
3069   share_alter->tmp_tgt_table_names_lengths =
3070     share_alter->tmp_server_names_lengths + share->all_link_count;
3071   memcpy(share_alter->tmp_tgt_table_names_lengths,
3072     share->tgt_table_names_lengths,
3073     sizeof(uint) * share->all_link_count);
3074   share_alter->tmp_tgt_dbs_lengths =
3075     share_alter->tmp_tgt_table_names_lengths + share->all_link_count;
3076   memcpy(share_alter->tmp_tgt_dbs_lengths, share->tgt_dbs_lengths,
3077     sizeof(uint) * share->all_link_count);
3078   share_alter->tmp_tgt_hosts_lengths =
3079     share_alter->tmp_tgt_dbs_lengths + share->all_link_count;
3080   memcpy(share_alter->tmp_tgt_hosts_lengths, share->tgt_hosts_lengths,
3081     sizeof(uint) * share->all_link_count);
3082   share_alter->tmp_tgt_usernames_lengths =
3083     share_alter->tmp_tgt_hosts_lengths + share->all_link_count;
3084   memcpy(share_alter->tmp_tgt_usernames_lengths,
3085     share->tgt_usernames_lengths,
3086     sizeof(uint) * share->all_link_count);
3087   share_alter->tmp_tgt_passwords_lengths =
3088     share_alter->tmp_tgt_usernames_lengths + share->all_link_count;
3089   memcpy(share_alter->tmp_tgt_passwords_lengths,
3090     share->tgt_passwords_lengths,
3091     sizeof(uint) * share->all_link_count);
3092   share_alter->tmp_tgt_sockets_lengths =
3093     share_alter->tmp_tgt_passwords_lengths + share->all_link_count;
3094   memcpy(share_alter->tmp_tgt_sockets_lengths, share->tgt_sockets_lengths,
3095     sizeof(uint) * share->all_link_count);
3096   share_alter->tmp_tgt_wrappers_lengths =
3097     share_alter->tmp_tgt_sockets_lengths + share->all_link_count;
3098   memcpy(share_alter->tmp_tgt_wrappers_lengths,
3099     share->tgt_wrappers_lengths,
3100     sizeof(uint) * share->all_link_count);
3101   share_alter->tmp_tgt_ssl_cas_lengths =
3102     share_alter->tmp_tgt_wrappers_lengths + share->all_link_count;
3103   memcpy(share_alter->tmp_tgt_ssl_cas_lengths,
3104     share->tgt_ssl_cas_lengths,
3105     sizeof(uint) * share->all_link_count);
3106   share_alter->tmp_tgt_ssl_capaths_lengths =
3107     share_alter->tmp_tgt_ssl_cas_lengths + share->all_link_count;
3108   memcpy(share_alter->tmp_tgt_ssl_capaths_lengths,
3109     share->tgt_ssl_capaths_lengths,
3110     sizeof(uint) * share->all_link_count);
3111   share_alter->tmp_tgt_ssl_certs_lengths =
3112     share_alter->tmp_tgt_ssl_capaths_lengths + share->all_link_count;
3113   memcpy(share_alter->tmp_tgt_ssl_certs_lengths,
3114     share->tgt_ssl_certs_lengths,
3115     sizeof(uint) * share->all_link_count);
3116   share_alter->tmp_tgt_ssl_ciphers_lengths =
3117     share_alter->tmp_tgt_ssl_certs_lengths + share->all_link_count;
3118   memcpy(share_alter->tmp_tgt_ssl_ciphers_lengths,
3119     share->tgt_ssl_ciphers_lengths,
3120     sizeof(uint) * share->all_link_count);
3121   share_alter->tmp_tgt_ssl_keys_lengths =
3122     share_alter->tmp_tgt_ssl_ciphers_lengths + share->all_link_count;
3123   memcpy(share_alter->tmp_tgt_ssl_keys_lengths,
3124     share->tgt_ssl_keys_lengths,
3125     sizeof(uint) * share->all_link_count);
3126   share_alter->tmp_tgt_default_files_lengths =
3127     share_alter->tmp_tgt_ssl_keys_lengths + share->all_link_count;
3128   memcpy(share_alter->tmp_tgt_default_files_lengths,
3129     share->tgt_default_files_lengths,
3130     sizeof(uint) * share->all_link_count);
3131   share_alter->tmp_tgt_default_groups_lengths =
3132     share_alter->tmp_tgt_default_files_lengths + share->all_link_count;
3133   memcpy(share_alter->tmp_tgt_default_groups_lengths,
3134     share->tgt_default_groups_lengths,
3135     sizeof(uint) * share->all_link_count);
3136   share_alter->tmp_static_link_ids_lengths =
3137     share_alter->tmp_tgt_default_groups_lengths + share->all_link_count;
3138   memcpy(share_alter->tmp_static_link_ids_lengths,
3139     share->static_link_ids_lengths,
3140     sizeof(uint) * share->all_link_count);
3141 
3142   share_alter->tmp_server_names_charlen = share->server_names_charlen;
3143   share_alter->tmp_tgt_table_names_charlen = share->tgt_table_names_charlen;
3144   share_alter->tmp_tgt_dbs_charlen = share->tgt_dbs_charlen;
3145   share_alter->tmp_tgt_hosts_charlen = share->tgt_hosts_charlen;
3146   share_alter->tmp_tgt_usernames_charlen = share->tgt_usernames_charlen;
3147   share_alter->tmp_tgt_passwords_charlen = share->tgt_passwords_charlen;
3148   share_alter->tmp_tgt_sockets_charlen = share->tgt_sockets_charlen;
3149   share_alter->tmp_tgt_wrappers_charlen = share->tgt_wrappers_charlen;
3150   share_alter->tmp_tgt_ssl_cas_charlen = share->tgt_ssl_cas_charlen;
3151   share_alter->tmp_tgt_ssl_capaths_charlen = share->tgt_ssl_capaths_charlen;
3152   share_alter->tmp_tgt_ssl_certs_charlen = share->tgt_ssl_certs_charlen;
3153   share_alter->tmp_tgt_ssl_ciphers_charlen = share->tgt_ssl_ciphers_charlen;
3154   share_alter->tmp_tgt_ssl_keys_charlen = share->tgt_ssl_keys_charlen;
3155   share_alter->tmp_tgt_default_files_charlen =
3156     share->tgt_default_files_charlen;
3157   share_alter->tmp_tgt_default_groups_charlen =
3158     share->tgt_default_groups_charlen;
3159   share_alter->tmp_static_link_ids_charlen =
3160     share->static_link_ids_charlen;
3161 
3162   share_alter->tmp_server_names_length = share->server_names_length;
3163   share_alter->tmp_tgt_table_names_length = share->tgt_table_names_length;
3164   share_alter->tmp_tgt_dbs_length = share->tgt_dbs_length;
3165   share_alter->tmp_tgt_hosts_length = share->tgt_hosts_length;
3166   share_alter->tmp_tgt_usernames_length = share->tgt_usernames_length;
3167   share_alter->tmp_tgt_passwords_length = share->tgt_passwords_length;
3168   share_alter->tmp_tgt_sockets_length = share->tgt_sockets_length;
3169   share_alter->tmp_tgt_wrappers_length = share->tgt_wrappers_length;
3170   share_alter->tmp_tgt_ssl_cas_length = share->tgt_ssl_cas_length;
3171   share_alter->tmp_tgt_ssl_capaths_length = share->tgt_ssl_capaths_length;
3172   share_alter->tmp_tgt_ssl_certs_length = share->tgt_ssl_certs_length;
3173   share_alter->tmp_tgt_ssl_ciphers_length = share->tgt_ssl_ciphers_length;
3174   share_alter->tmp_tgt_ssl_keys_length = share->tgt_ssl_keys_length;
3175   share_alter->tmp_tgt_default_files_length = share->tgt_default_files_length;
3176   share_alter->tmp_tgt_default_groups_length =
3177     share->tgt_default_groups_length;
3178   share_alter->tmp_static_link_ids_length =
3179     share->static_link_ids_length;
3180   share_alter->tmp_tgt_ports_length = share->tgt_ports_length;
3181   share_alter->tmp_tgt_ssl_vscs_length = share->tgt_ssl_vscs_length;
3182   share_alter->tmp_monitoring_binlog_pos_at_failing_length =
3183     share->monitoring_binlog_pos_at_failing_length;
3184   share_alter->tmp_link_statuses_length = share->link_statuses_length;
3185   /* copy for tables end */
3186 
3187   if ((error_num = spider_set_connect_info_default(
3188     share,
3189 #ifdef WITH_PARTITION_STORAGE_ENGINE
3190     part_elem,
3191     sub_elem,
3192 #endif
3193     table_share
3194   )))
3195     goto error;
3196 
3197   if (create_table)
3198   {
3199     for (roop_count = 0; roop_count < (int) share->all_link_count;
3200       roop_count++)
3201     {
3202       int roop_count2;
3203       for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; roop_count2++)
3204       {
3205         if (
3206           spider_dbton[roop_count2].wrapper &&
3207           !strcmp(share->tgt_wrappers[roop_count],
3208             spider_dbton[roop_count2].wrapper)
3209         ) {
3210           break;
3211         }
3212       }
3213       if (roop_count2 == SPIDER_DBTON_SIZE)
3214       {
3215         DBUG_PRINT("info",("spider err tgt_wrappers[%d]=%s", roop_count,
3216           share->tgt_wrappers[roop_count]));
3217         error_num = ER_SPIDER_INVALID_CONNECT_INFO_NUM;
3218         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_STR,
3219           MYF(0), share->tgt_wrappers[roop_count]);
3220         goto error;
3221       }
3222 
3223       DBUG_PRINT("info",
3224         ("spider server_names_lengths[%d] = %u", roop_count,
3225          share->server_names_lengths[roop_count]));
3226       if (share->server_names_lengths[roop_count] > SPIDER_CONNECT_INFO_MAX_LEN)
3227       {
3228         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3229         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3230           MYF(0), share->server_names[roop_count], "server");
3231         goto error;
3232       }
3233 
3234       DBUG_PRINT("info",
3235         ("spider tgt_table_names_lengths[%d] = %u", roop_count,
3236         share->tgt_table_names_lengths[roop_count]));
3237       if (share->tgt_table_names_lengths[roop_count] >
3238         SPIDER_CONNECT_INFO_MAX_LEN)
3239       {
3240         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3241         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3242           MYF(0), share->tgt_table_names[roop_count], "table");
3243         goto error;
3244       }
3245 
3246       DBUG_PRINT("info",
3247         ("spider tgt_dbs_lengths[%d] = %u", roop_count,
3248         share->tgt_dbs_lengths[roop_count]));
3249       if (share->tgt_dbs_lengths[roop_count] > SPIDER_CONNECT_INFO_MAX_LEN)
3250       {
3251         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3252         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3253           MYF(0), share->tgt_dbs[roop_count], "database");
3254         goto error;
3255       }
3256 
3257       DBUG_PRINT("info",
3258         ("spider tgt_hosts_lengths[%d] = %u", roop_count,
3259         share->tgt_hosts_lengths[roop_count]));
3260       if (share->tgt_hosts_lengths[roop_count] > SPIDER_CONNECT_INFO_MAX_LEN)
3261       {
3262         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3263         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3264           MYF(0), share->tgt_hosts[roop_count], "host");
3265         goto error;
3266       }
3267 
3268       DBUG_PRINT("info",
3269         ("spider tgt_usernames_lengths[%d] = %u", roop_count,
3270         share->tgt_usernames_lengths[roop_count]));
3271       if (share->tgt_usernames_lengths[roop_count] >
3272         SPIDER_CONNECT_INFO_MAX_LEN)
3273       {
3274         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3275         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3276           MYF(0), share->tgt_usernames[roop_count], "user");
3277         goto error;
3278       }
3279 
3280       DBUG_PRINT("info",
3281         ("spider tgt_passwords_lengths[%d] = %u", roop_count,
3282         share->tgt_passwords_lengths[roop_count]));
3283       if (share->tgt_passwords_lengths[roop_count] >
3284         SPIDER_CONNECT_INFO_MAX_LEN)
3285       {
3286         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3287         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3288           MYF(0), share->tgt_passwords[roop_count], "password");
3289         goto error;
3290       }
3291 
3292       DBUG_PRINT("info",
3293         ("spider tgt_sockets_lengths[%d] = %u", roop_count,
3294         share->tgt_sockets_lengths[roop_count]));
3295       if (share->tgt_sockets_lengths[roop_count] >
3296         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3297       {
3298         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3299         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3300           MYF(0), share->tgt_sockets[roop_count], "socket");
3301         goto error;
3302       }
3303 
3304       DBUG_PRINT("info",
3305         ("spider tgt_wrappers_lengths[%d] = %u", roop_count,
3306         share->tgt_wrappers_lengths[roop_count]));
3307       if (share->tgt_wrappers_lengths[roop_count] >
3308         SPIDER_CONNECT_INFO_MAX_LEN)
3309       {
3310         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3311         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3312           MYF(0), share->tgt_wrappers[roop_count], "wrapper");
3313         goto error;
3314       }
3315 
3316       DBUG_PRINT("info",
3317         ("spider tgt_ssl_cas_lengths[%d] = %u", roop_count,
3318         share->tgt_ssl_cas_lengths[roop_count]));
3319       if (share->tgt_ssl_cas_lengths[roop_count] >
3320         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3321       {
3322         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3323         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3324           MYF(0), share->tgt_ssl_cas[roop_count], "ssl_ca");
3325         goto error;
3326       }
3327 
3328       DBUG_PRINT("info",
3329         ("spider tgt_ssl_capaths_lengths[%d] = %u", roop_count,
3330         share->tgt_ssl_capaths_lengths[roop_count]));
3331       if (share->tgt_ssl_capaths_lengths[roop_count] >
3332         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3333       {
3334         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3335         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3336           MYF(0), share->tgt_ssl_capaths[roop_count], "ssl_capath");
3337         goto error;
3338       }
3339 
3340       DBUG_PRINT("info",
3341         ("spider tgt_ssl_certs_lengths[%d] = %u", roop_count,
3342         share->tgt_ssl_certs_lengths[roop_count]));
3343       if (share->tgt_ssl_certs_lengths[roop_count] >
3344         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3345       {
3346         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3347         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3348           MYF(0), share->tgt_ssl_certs[roop_count], "ssl_cert");
3349         goto error;
3350       }
3351 
3352       DBUG_PRINT("info",
3353         ("spider tgt_ssl_ciphers_lengths[%d] = %u", roop_count,
3354         share->tgt_ssl_ciphers_lengths[roop_count]));
3355       if (share->tgt_ssl_ciphers_lengths[roop_count] >
3356         SPIDER_CONNECT_INFO_MAX_LEN)
3357       {
3358         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3359         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3360           MYF(0), share->tgt_ssl_ciphers[roop_count], "ssl_cipher");
3361         goto error;
3362       }
3363 
3364       DBUG_PRINT("info",
3365         ("spider tgt_ssl_keys_lengths[%d] = %u", roop_count,
3366         share->tgt_ssl_keys_lengths[roop_count]));
3367       if (share->tgt_ssl_keys_lengths[roop_count] >
3368         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3369       {
3370         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3371         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3372           MYF(0), share->tgt_ssl_keys[roop_count], "ssl_key");
3373         goto error;
3374       }
3375 
3376       DBUG_PRINT("info",
3377         ("spider tgt_default_files_lengths[%d] = %u", roop_count,
3378         share->tgt_default_files_lengths[roop_count]));
3379       if (share->tgt_default_files_lengths[roop_count] >
3380         SPIDER_CONNECT_INFO_PATH_MAX_LEN)
3381       {
3382         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3383         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3384           MYF(0), share->tgt_default_files[roop_count], "default_file");
3385         goto error;
3386       }
3387 
3388       DBUG_PRINT("info",
3389         ("spider tgt_default_groups_lengths[%d] = %u", roop_count,
3390         share->tgt_default_groups_lengths[roop_count]));
3391       if (share->tgt_default_groups_lengths[roop_count] >
3392         SPIDER_CONNECT_INFO_MAX_LEN)
3393       {
3394         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3395         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3396           MYF(0), share->tgt_default_groups[roop_count], "default_group");
3397         goto error;
3398       }
3399 
3400       DBUG_PRINT("info",
3401         ("spider tgt_pk_names_lengths[%d] = %u", roop_count,
3402         share->tgt_pk_names_lengths[roop_count]));
3403       if (share->tgt_pk_names_lengths[roop_count] >
3404         SPIDER_CONNECT_INFO_MAX_LEN)
3405       {
3406         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3407         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3408           MYF(0), share->tgt_pk_names[roop_count], "pk_name");
3409         goto error;
3410       }
3411 
3412       DBUG_PRINT("info",
3413         ("spider tgt_sequence_names_lengths[%d] = %u", roop_count,
3414         share->tgt_sequence_names_lengths[roop_count]));
3415       if (share->tgt_sequence_names_lengths[roop_count] >
3416         SPIDER_CONNECT_INFO_MAX_LEN)
3417       {
3418         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3419         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3420           MYF(0), share->tgt_sequence_names[roop_count], "sequence_name");
3421         goto error;
3422       }
3423 
3424       DBUG_PRINT("info",
3425         ("spider static_link_ids_lengths[%d] = %u", roop_count,
3426         share->static_link_ids_lengths[roop_count]));
3427       if (share->static_link_ids_lengths[roop_count] >
3428         SPIDER_CONNECT_INFO_MAX_LEN)
3429       {
3430         error_num = ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_NUM;
3431         my_printf_error(error_num, ER_SPIDER_INVALID_CONNECT_INFO_TOO_LONG_STR,
3432           MYF(0), share->static_link_ids[roop_count], "static_link_id");
3433         goto error;
3434       }
3435       if (share->static_link_ids[roop_count])
3436       {
3437         if (
3438           share->static_link_ids_lengths[roop_count] > 0 &&
3439           share->static_link_ids[roop_count][0] >= '0' &&
3440           share->static_link_ids[roop_count][0] <= '9'
3441         ) {
3442           error_num = ER_SPIDER_INVALID_CONNECT_INFO_START_WITH_NUM_NUM;
3443           my_printf_error(error_num,
3444             ER_SPIDER_INVALID_CONNECT_INFO_START_WITH_NUM_STR,
3445             MYF(0), share->static_link_ids[roop_count], "static_link_id");
3446           goto error;
3447         }
3448         for (roop_count2 = roop_count + 1;
3449           roop_count2 < (int) share->all_link_count;
3450           roop_count2++)
3451         {
3452           if (
3453             share->static_link_ids_lengths[roop_count] ==
3454               share->static_link_ids_lengths[roop_count2] &&
3455             !memcmp(share->static_link_ids[roop_count],
3456               share->static_link_ids[roop_count2],
3457               share->static_link_ids_lengths[roop_count])
3458           ) {
3459             error_num = ER_SPIDER_INVALID_CONNECT_INFO_SAME_NUM;
3460             my_printf_error(error_num,
3461               ER_SPIDER_INVALID_CONNECT_INFO_SAME_STR,
3462               MYF(0), share->static_link_ids[roop_count],
3463               "static_link_id");
3464             goto error;
3465           }
3466         }
3467       }
3468     }
3469   }
3470 
3471   DBUG_PRINT("info", ("spider share->active_link_count = %d",
3472     share->active_link_count));
3473   share->link_count = (uint) share->active_link_count;
3474   share_alter->link_count = share->link_count;
3475   share->link_bitmap_size = (share->link_count + 7) / 8;
3476 
3477   if (connect_string)
3478     spider_free(spider_current_trx, connect_string, MYF(0));
3479   DBUG_RETURN(0);
3480 
3481 error:
3482   if (connect_string)
3483     spider_free(spider_current_trx, connect_string, MYF(0));
3484 error_alloc_conn_string:
3485   DBUG_RETURN(error_num);
3486 }
3487 
spider_set_connect_info_default(SPIDER_SHARE * share,partition_element * part_elem,partition_element * sub_elem,TABLE_SHARE * table_share)3488 int spider_set_connect_info_default(
3489   SPIDER_SHARE *share,
3490 #ifdef WITH_PARTITION_STORAGE_ENGINE
3491   partition_element *part_elem,
3492   partition_element *sub_elem,
3493 #endif
3494   TABLE_SHARE *table_share
3495 ) {
3496   int error_num, roop_count;
3497   DBUG_ENTER("spider_set_connect_info_default");
3498   for (roop_count = 0; roop_count < (int) share->all_link_count; roop_count++)
3499   {
3500     if (share->server_names[roop_count])
3501     {
3502       if ((error_num = spider_get_server(share, roop_count)))
3503         DBUG_RETURN(error_num);
3504     }
3505 
3506     if (!share->tgt_wrappers[roop_count])
3507     {
3508       DBUG_PRINT("info",("spider create default tgt_wrappers"));
3509       share->tgt_wrappers_lengths[roop_count] = SPIDER_DB_WRAPPER_LEN;
3510       if (
3511         !(share->tgt_wrappers[roop_count] = spider_create_string(
3512           SPIDER_DB_WRAPPER_STR,
3513           share->tgt_wrappers_lengths[roop_count]))
3514       ) {
3515         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3516       }
3517     }
3518 
3519     if (!share->tgt_hosts[roop_count])
3520     {
3521       DBUG_PRINT("info",("spider create default tgt_hosts"));
3522       share->tgt_hosts_lengths[roop_count] = strlen(my_localhost);
3523       if (
3524         !(share->tgt_hosts[roop_count] = spider_create_string(
3525           my_localhost,
3526           share->tgt_hosts_lengths[roop_count]))
3527       ) {
3528         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3529       }
3530     }
3531 
3532     if (!share->tgt_dbs[roop_count] && table_share)
3533     {
3534       DBUG_PRINT("info",("spider create default tgt_dbs"));
3535       share->tgt_dbs_lengths[roop_count] = table_share->db.length;
3536       if (
3537         !(share->tgt_dbs[roop_count] = spider_create_string(
3538           table_share->db.str,
3539           table_share->db.length))
3540       ) {
3541         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3542       }
3543     }
3544 
3545     if (!share->tgt_table_names[roop_count] && table_share)
3546     {
3547       DBUG_PRINT("info",("spider create default tgt_table_names"));
3548       share->tgt_table_names_lengths[roop_count] = share->table_name_length;
3549       if (
3550         !(share->tgt_table_names[roop_count] = spider_create_table_name_string(
3551           table_share->table_name.str,
3552 #ifdef WITH_PARTITION_STORAGE_ENGINE
3553           (part_elem ? part_elem->partition_name : NULL),
3554           (sub_elem ? sub_elem->partition_name : NULL)
3555 #else
3556           NULL,
3557           NULL
3558 #endif
3559         ))
3560       ) {
3561         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3562       }
3563     }
3564 
3565     if (
3566       !share->tgt_default_files[roop_count] &&
3567       share->tgt_default_groups[roop_count] &&
3568       (*spd_defaults_file || *spd_defaults_extra_file)
3569     ) {
3570       DBUG_PRINT("info",("spider create default tgt_default_files"));
3571       if (*spd_defaults_extra_file)
3572       {
3573         share->tgt_default_files_lengths[roop_count] =
3574           strlen(*spd_defaults_extra_file);
3575         if (
3576           !(share->tgt_default_files[roop_count] = spider_create_string(
3577             *spd_defaults_extra_file,
3578             share->tgt_default_files_lengths[roop_count]))
3579         ) {
3580           my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
3581           DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3582         }
3583       } else {
3584         share->tgt_default_files_lengths[roop_count] =
3585           strlen(*spd_defaults_file);
3586         if (
3587           !(share->tgt_default_files[roop_count] = spider_create_string(
3588             *spd_defaults_file,
3589             share->tgt_default_files_lengths[roop_count]))
3590         ) {
3591           my_error(ER_OUT_OF_RESOURCES, MYF(0), HA_ERR_OUT_OF_MEM);
3592           DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3593         }
3594       }
3595     }
3596 
3597     if (!share->tgt_pk_names[roop_count])
3598     {
3599       DBUG_PRINT("info",("spider create default tgt_pk_names"));
3600       share->tgt_pk_names_lengths[roop_count] = SPIDER_DB_PK_NAME_LEN;
3601       if (
3602         !(share->tgt_pk_names[roop_count] = spider_create_string(
3603           SPIDER_DB_PK_NAME_STR,
3604           share->tgt_pk_names_lengths[roop_count]))
3605       ) {
3606         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3607       }
3608     }
3609 
3610     if (!share->tgt_sequence_names[roop_count])
3611     {
3612       DBUG_PRINT("info",("spider create default tgt_sequence_names"));
3613       share->tgt_sequence_names_lengths[roop_count] =
3614         SPIDER_DB_SEQUENCE_NAME_LEN;
3615       if (
3616         !(share->tgt_sequence_names[roop_count] = spider_create_string(
3617           SPIDER_DB_SEQUENCE_NAME_STR,
3618           share->tgt_sequence_names_lengths[roop_count]))
3619       ) {
3620         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3621       }
3622     }
3623 
3624 /*
3625     if (!share->static_link_ids[roop_count])
3626     {
3627       DBUG_PRINT("info",("spider create default static_link_ids"));
3628       share->static_link_ids_lengths[roop_count] =
3629         SPIDER_DB_STATIC_LINK_ID_LEN;
3630       if (
3631         !(share->static_link_ids[roop_count] = spider_create_string(
3632           SPIDER_DB_STATIC_LINK_ID_STR,
3633           share->static_link_ids_lengths[roop_count]))
3634       ) {
3635         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3636       }
3637     }
3638 */
3639 
3640     if (share->tgt_ports[roop_count] == -1)
3641     {
3642       share->tgt_ports[roop_count] = MYSQL_PORT;
3643     } else if (share->tgt_ports[roop_count] < 0)
3644     {
3645       share->tgt_ports[roop_count] = 0;
3646     } else if (share->tgt_ports[roop_count] > 65535)
3647     {
3648       share->tgt_ports[roop_count] = 65535;
3649     }
3650 
3651     if (share->tgt_ssl_vscs[roop_count] == -1)
3652       share->tgt_ssl_vscs[roop_count] = 0;
3653 
3654     if (
3655       !share->tgt_sockets[roop_count] &&
3656       !strcmp(share->tgt_hosts[roop_count], my_localhost)
3657     ) {
3658       DBUG_PRINT("info",("spider create default tgt_sockets"));
3659       share->tgt_sockets_lengths[roop_count] =
3660         strlen((char *) MYSQL_UNIX_ADDR);
3661       if (
3662         !(share->tgt_sockets[roop_count] = spider_create_string(
3663           (char *) MYSQL_UNIX_ADDR,
3664           share->tgt_sockets_lengths[roop_count]))
3665       ) {
3666         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3667       }
3668     }
3669 
3670     if (share->link_statuses[roop_count] == -1)
3671       share->link_statuses[roop_count] = SPIDER_LINK_STATUS_NO_CHANGE;
3672 
3673 #ifndef WITHOUT_SPIDER_BG_SEARCH
3674     if (share->monitoring_bg_flag[roop_count] == -1)
3675       share->monitoring_bg_flag[roop_count] = 0;
3676     if (share->monitoring_bg_kind[roop_count] == -1)
3677       share->monitoring_bg_kind[roop_count] = 0;
3678 #endif
3679     if (share->monitoring_binlog_pos_at_failing[roop_count] == -1)
3680       share->monitoring_binlog_pos_at_failing[roop_count] = 0;
3681     if (share->monitoring_flag[roop_count] == -1)
3682       share->monitoring_flag[roop_count] = 0;
3683     if (share->monitoring_kind[roop_count] == -1)
3684       share->monitoring_kind[roop_count] = 0;
3685 #ifndef WITHOUT_SPIDER_BG_SEARCH
3686     if (share->monitoring_bg_interval[roop_count] == -1)
3687       share->monitoring_bg_interval[roop_count] = 10000000;
3688 #endif
3689     if (share->monitoring_limit[roop_count] == -1)
3690       share->monitoring_limit[roop_count] = 1;
3691     if (share->monitoring_sid[roop_count] == -1)
3692 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100002
3693       share->monitoring_sid[roop_count] = global_system_variables.server_id;
3694 #else
3695       share->monitoring_sid[roop_count] = current_thd->server_id;
3696 #endif
3697 
3698 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3699     if (share->use_hs_reads[roop_count] == -1)
3700       share->use_hs_reads[roop_count] = 0;
3701     if (share->use_hs_writes[roop_count] == -1)
3702       share->use_hs_writes[roop_count] = 0;
3703     if (share->hs_read_ports[roop_count] == -1)
3704     {
3705       share->hs_read_ports[roop_count] = 9998;
3706     } else if (share->hs_read_ports[roop_count] < 0)
3707     {
3708       share->hs_read_ports[roop_count] = 0;
3709     } else if (share->hs_read_ports[roop_count] > 65535)
3710     {
3711       share->hs_read_ports[roop_count] = 65535;
3712     }
3713     if (share->hs_write_ports[roop_count] == -1)
3714     {
3715       share->hs_write_ports[roop_count] = 9999;
3716     } else if (share->hs_write_ports[roop_count] < 0)
3717     {
3718       share->hs_write_ports[roop_count] = 0;
3719     } else if (share->hs_write_ports[roop_count] > 65535)
3720     {
3721       share->hs_write_ports[roop_count] = 65535;
3722     }
3723     if (share->hs_write_to_reads[roop_count] == -1)
3724     {
3725       share->hs_write_to_reads[roop_count] = 1;
3726     } else if (share->hs_write_to_reads[roop_count] < 0)
3727     {
3728       share->hs_write_to_reads[roop_count] = 0;
3729     } else if (share->hs_write_to_reads[roop_count] > 1)
3730     {
3731       share->hs_write_to_reads[roop_count] = 1;
3732     }
3733 #endif
3734     if (share->use_handlers[roop_count] == -1)
3735       share->use_handlers[roop_count] = 0;
3736     if (share->connect_timeouts[roop_count] == -1)
3737       share->connect_timeouts[roop_count] = 6;
3738     if (share->net_read_timeouts[roop_count] == -1)
3739       share->net_read_timeouts[roop_count] = 600;
3740     if (share->net_write_timeouts[roop_count] == -1)
3741       share->net_write_timeouts[roop_count] = 600;
3742     if (share->access_balances[roop_count] == -1)
3743       share->access_balances[roop_count] = 100;
3744     if (share->bka_table_name_types[roop_count] == -1)
3745       share->bka_table_name_types[roop_count] = 0;
3746   }
3747 
3748 #ifndef WITHOUT_SPIDER_BG_SEARCH
3749   if (share->sts_bg_mode == -1)
3750     share->sts_bg_mode = 2;
3751 #endif
3752   if (share->sts_interval == -1)
3753     share->sts_interval = 10;
3754   if (share->sts_mode == -1)
3755     share->sts_mode = 1;
3756 #ifdef WITH_PARTITION_STORAGE_ENGINE
3757   if (share->sts_sync == -1)
3758     share->sts_sync = 0;
3759 #endif
3760   if (share->store_last_sts == -1)
3761     share->store_last_sts = 1;
3762   if (share->load_sts_at_startup == -1)
3763     share->load_sts_at_startup = 1;
3764 #ifndef WITHOUT_SPIDER_BG_SEARCH
3765   if (share->crd_bg_mode == -1)
3766     share->crd_bg_mode = 2;
3767 #endif
3768   if (share->crd_interval == -1)
3769     share->crd_interval = 51;
3770   if (share->crd_mode == -1)
3771     share->crd_mode = 1;
3772 #ifdef WITH_PARTITION_STORAGE_ENGINE
3773   if (share->crd_sync == -1)
3774     share->crd_sync = 0;
3775 #endif
3776   if (share->store_last_crd == -1)
3777     share->store_last_crd = 1;
3778   if (share->load_crd_at_startup == -1)
3779     share->load_crd_at_startup = 1;
3780   if (share->crd_type == -1)
3781     share->crd_type = 2;
3782   if (share->crd_weight == -1)
3783     share->crd_weight = 2;
3784   if (share->internal_offset == -1)
3785     share->internal_offset = 0;
3786   if (share->internal_limit == -1)
3787     share->internal_limit = 9223372036854775807LL;
3788   if (share->split_read == -1)
3789     share->split_read = 9223372036854775807LL;
3790   if (share->semi_split_read == -1)
3791     share->semi_split_read = 2;
3792   if (share->semi_split_read_limit == -1)
3793     share->semi_split_read_limit = 9223372036854775807LL;
3794   if (share->init_sql_alloc_size == -1)
3795     share->init_sql_alloc_size = 1024;
3796   if (share->reset_sql_alloc == -1)
3797     share->reset_sql_alloc = 1;
3798   if (share->multi_split_read == -1)
3799     share->multi_split_read = 100;
3800   if (share->max_order == -1)
3801     share->max_order = 32767;
3802   if (share->semi_table_lock == -1)
3803     share->semi_table_lock = 0;
3804   if (share->semi_table_lock_conn == -1)
3805     share->semi_table_lock_conn = 1;
3806   if (share->selupd_lock_mode == -1)
3807     share->selupd_lock_mode = 1;
3808   if (share->query_cache == -1)
3809     share->query_cache = 0;
3810   if (share->query_cache_sync == -1)
3811     share->query_cache_sync = 0;
3812   if (share->internal_delayed == -1)
3813     share->internal_delayed = 0;
3814   if (share->bulk_size == -1)
3815     share->bulk_size = 16000;
3816   if (share->bulk_update_mode == -1)
3817     share->bulk_update_mode = 0;
3818   if (share->bulk_update_size == -1)
3819     share->bulk_update_size = 16000;
3820   if (share->internal_optimize == -1)
3821     share->internal_optimize = 0;
3822   if (share->internal_optimize_local == -1)
3823     share->internal_optimize_local = 0;
3824   if (share->scan_rate == -1)
3825     share->scan_rate = 1;
3826   if (share->read_rate == -1)
3827     share->read_rate = 0.0002;
3828   if (share->priority == -1)
3829     share->priority = 1000000;
3830   if (share->quick_mode == -1)
3831     share->quick_mode = 3;
3832   if (share->quick_page_size == -1)
3833     share->quick_page_size = 1024;
3834   if (share->quick_page_byte == -1)
3835     share->quick_page_byte = 10485760;
3836   if (share->low_mem_read == -1)
3837     share->low_mem_read = 1;
3838   if (share->table_count_mode == -1)
3839     share->table_count_mode = 0;
3840   if (share->select_column_mode == -1)
3841     share->select_column_mode = 1;
3842 #ifndef WITHOUT_SPIDER_BG_SEARCH
3843   if (share->bgs_mode == -1)
3844     share->bgs_mode = 0;
3845   if (share->bgs_first_read == -1)
3846     share->bgs_first_read = 2;
3847   if (share->bgs_second_read == -1)
3848     share->bgs_second_read = 100;
3849 #endif
3850   if (share->first_read == -1)
3851     share->first_read = 0;
3852   if (share->second_read == -1)
3853     share->second_read = 0;
3854   if (share->auto_increment_mode == -1)
3855     share->auto_increment_mode = 0;
3856   if (share->use_table_charset == -1)
3857     share->use_table_charset = 1;
3858   if (share->use_pushdown_udf == -1)
3859     share->use_pushdown_udf = 1;
3860   if (share->skip_default_condition == -1)
3861     share->skip_default_condition = 0;
3862   if (share->skip_parallel_search == -1)
3863     share->skip_parallel_search = 0;
3864   if (share->direct_dup_insert == -1)
3865     share->direct_dup_insert = 0;
3866   if (share->direct_order_limit == -1)
3867     share->direct_order_limit = 9223372036854775807LL;
3868   if (share->read_only_mode == -1)
3869     share->read_only_mode = 0;
3870   if (share->error_read_mode == -1)
3871     share->error_read_mode = 0;
3872   if (share->error_write_mode == -1)
3873     share->error_write_mode = 0;
3874   if (share->active_link_count == -1)
3875     share->active_link_count = share->all_link_count;
3876 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
3877   if (share->hs_result_free_size == -1)
3878     share->hs_result_free_size = 1048576;
3879 #endif
3880 #ifdef HA_CAN_BULK_ACCESS
3881   if (share->bulk_access_free == -1)
3882     share->bulk_access_free = 0;
3883 #endif
3884 #ifdef HA_CAN_FORCE_BULK_UPDATE
3885   if (share->force_bulk_update == -1)
3886     share->force_bulk_update = 0;
3887 #endif
3888 #ifdef HA_CAN_FORCE_BULK_DELETE
3889   if (share->force_bulk_delete == -1)
3890     share->force_bulk_delete = 0;
3891 #endif
3892   if (share->casual_read == -1)
3893     share->casual_read = 0;
3894   if (share->delete_all_rows_type == -1)
3895   {
3896 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
3897     share->delete_all_rows_type = 1;
3898 #else
3899     share->delete_all_rows_type = 0;
3900 #endif
3901   }
3902   if (share->bka_mode == -1)
3903     share->bka_mode = 1;
3904   if (!share->bka_engine)
3905   {
3906     DBUG_PRINT("info",("spider create default bka_engine"));
3907     share->bka_engine_length = SPIDER_SQL_TMP_BKA_ENGINE_LEN;
3908     if (
3909       !(share->bka_engine = spider_create_string(
3910         SPIDER_SQL_TMP_BKA_ENGINE_STR,
3911         SPIDER_SQL_TMP_BKA_ENGINE_LEN))
3912     ) {
3913       DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3914     }
3915   }
3916   DBUG_RETURN(0);
3917 }
3918 
spider_set_connect_info_default_db_table(SPIDER_SHARE * share,const char * db_name,uint db_name_length,const char * table_name,uint table_name_length)3919 int spider_set_connect_info_default_db_table(
3920   SPIDER_SHARE *share,
3921   const char *db_name,
3922   uint db_name_length,
3923   const char *table_name,
3924   uint table_name_length
3925 ) {
3926   int roop_count;
3927   DBUG_ENTER("spider_set_connect_info_default_db_table");
3928   for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
3929   {
3930     if (!share->tgt_dbs[roop_count] && db_name)
3931     {
3932       DBUG_PRINT("info",("spider create default tgt_dbs"));
3933       share->tgt_dbs_lengths[roop_count] = db_name_length;
3934       if (
3935         !(share->tgt_dbs[roop_count] = spider_create_string(
3936           db_name,
3937           db_name_length))
3938       ) {
3939         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3940       }
3941     }
3942 
3943     if (!share->tgt_table_names[roop_count] && table_name)
3944     {
3945       const char *tmp_ptr;
3946       DBUG_PRINT("info",("spider create default tgt_table_names"));
3947       if ((tmp_ptr = strstr(table_name, "#P#")))
3948         table_name_length = (uint) PTR_BYTE_DIFF(tmp_ptr, table_name);
3949       share->tgt_table_names_lengths[roop_count] = table_name_length;
3950       if (
3951         !(share->tgt_table_names[roop_count] = spider_create_string(
3952           table_name,
3953           table_name_length))
3954       ) {
3955         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
3956       }
3957     }
3958   }
3959   DBUG_RETURN(0);
3960 }
3961 
spider_set_connect_info_default_dbtable(SPIDER_SHARE * share,const char * dbtable_name,int dbtable_name_length)3962 int spider_set_connect_info_default_dbtable(
3963   SPIDER_SHARE *share,
3964   const char *dbtable_name,
3965   int dbtable_name_length
3966 ) {
3967   const char *ptr_db, *ptr_table;
3968   my_ptrdiff_t ptr_diff_db, ptr_diff_table;
3969   DBUG_ENTER("spider_set_connect_info_default_dbtable");
3970   ptr_db = strchr(dbtable_name, FN_LIBCHAR);
3971   ptr_db++;
3972   ptr_diff_db = PTR_BYTE_DIFF(ptr_db, dbtable_name);
3973   DBUG_PRINT("info",("spider ptr_diff_db = %lld", (longlong) ptr_diff_db));
3974   ptr_table = strchr(ptr_db, FN_LIBCHAR);
3975   ptr_table++;
3976   ptr_diff_table = PTR_BYTE_DIFF(ptr_table, ptr_db);
3977   DBUG_PRINT("info",("spider ptr_diff_table = %lld", (longlong) ptr_diff_table));
3978   DBUG_RETURN(spider_set_connect_info_default_db_table(
3979     share,
3980     ptr_db,
3981     (uint)(ptr_diff_table - 1),
3982     ptr_table,
3983     (uint)(dbtable_name_length - ptr_diff_db - ptr_diff_table)
3984   ));
3985 }
3986 
3987 #ifndef DBUG_OFF
spider_print_keys(const char * key,uint length)3988 void spider_print_keys(
3989   const char *key,
3990   uint length
3991 ) {
3992   const char *end_ptr;
3993   uint roop_count = 1;
3994   DBUG_ENTER("spider_print_keys");
3995   DBUG_PRINT("info",("spider key_length=%u", length));
3996   end_ptr = key + length;
3997   while (key < end_ptr)
3998   {
3999     DBUG_PRINT("info",("spider key[%u]=%s", roop_count, key));
4000     key = strchr(key, '\0') + 1;
4001     roop_count++;
4002   }
4003   DBUG_VOID_RETURN;
4004 }
4005 #endif
4006 
spider_create_conn_keys(SPIDER_SHARE * share)4007 int spider_create_conn_keys(
4008   SPIDER_SHARE *share
4009 ) {
4010   int roop_count, roop_count2;
4011   char *tmp_name, port_str[6];
4012 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4013   char *tmp_hs_r_name, *tmp_hs_w_name;
4014 #endif
4015   uint *conn_keys_lengths;
4016 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4017   uint *hs_r_conn_keys_lengths;
4018   uint *hs_w_conn_keys_lengths;
4019 #endif
4020   DBUG_ENTER("spider_create_conn_keys");
4021   char *ptr;
4022   uint length = sizeof(uint) * share->all_link_count;
4023 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4024   length += (sizeof(uint) * share->all_link_count) * 2;
4025 #endif
4026   ptr = (char *) my_alloca(length);
4027   if (!ptr)
4028   {
4029     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4030   }
4031   conn_keys_lengths = (uint *) ptr;
4032   ptr += (sizeof(uint) * share->all_link_count);
4033 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4034   hs_r_conn_keys_lengths = (uint *) ptr;
4035   ptr += (sizeof(uint) * share->all_link_count);
4036   hs_w_conn_keys_lengths = (uint *) ptr;
4037 #endif
4038 
4039   share->conn_keys_charlen = 0;
4040 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4041   share->hs_read_conn_keys_charlen = 0;
4042   share->hs_write_conn_keys_charlen = 0;
4043 #endif
4044   for (roop_count = 0; roop_count < (int) share->all_link_count; roop_count++)
4045   {
4046     /* tgt_db not use */
4047     conn_keys_lengths[roop_count]
4048       = 1
4049       + share->tgt_wrappers_lengths[roop_count] + 1
4050       + share->tgt_hosts_lengths[roop_count] + 1
4051       + 5 + 1
4052       + share->tgt_sockets_lengths[roop_count] + 1
4053       + share->tgt_usernames_lengths[roop_count] + 1
4054       + share->tgt_passwords_lengths[roop_count] + 1
4055       + share->tgt_ssl_cas_lengths[roop_count] + 1
4056       + share->tgt_ssl_capaths_lengths[roop_count] + 1
4057       + share->tgt_ssl_certs_lengths[roop_count] + 1
4058       + share->tgt_ssl_ciphers_lengths[roop_count] + 1
4059       + share->tgt_ssl_keys_lengths[roop_count] + 1
4060       + 1 + 1
4061       + share->tgt_default_files_lengths[roop_count] + 1
4062       + share->tgt_default_groups_lengths[roop_count];
4063     share->conn_keys_charlen += conn_keys_lengths[roop_count] + 2;
4064 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4065     hs_r_conn_keys_lengths[roop_count]
4066       = 1
4067       + share->tgt_wrappers_lengths[roop_count] + 1
4068       + share->tgt_hosts_lengths[roop_count] + 1
4069       + 5 + 1
4070       + share->hs_read_socks_lengths[roop_count];
4071     share->hs_read_conn_keys_charlen +=
4072       hs_r_conn_keys_lengths[roop_count] + 2;
4073     hs_w_conn_keys_lengths[roop_count]
4074       = 1
4075       + share->tgt_wrappers_lengths[roop_count] + 1
4076       + share->tgt_hosts_lengths[roop_count] + 1
4077       + 5 + 1
4078       + share->hs_write_socks_lengths[roop_count];
4079     share->hs_write_conn_keys_charlen +=
4080       hs_w_conn_keys_lengths[roop_count] + 2;
4081 #endif
4082   }
4083   if (!(share->conn_keys = (char **)
4084     spider_bulk_alloc_mem(spider_current_trx, 45,
4085       __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
4086       &share->conn_keys, sizeof(char *) * share->all_link_count,
4087       &share->conn_keys_lengths, sizeof(uint) * share->all_link_count,
4088 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4089       &share->conn_keys_hash_value,
4090         sizeof(my_hash_value_type) * share->all_link_count,
4091 #endif
4092       &tmp_name, sizeof(char) * share->conn_keys_charlen,
4093 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4094       &share->hs_read_conn_keys, sizeof(char *) * share->all_link_count,
4095       &share->hs_read_conn_keys_lengths, sizeof(uint) * share->all_link_count,
4096 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4097       &share->hs_read_conn_keys_hash_value,
4098         sizeof(my_hash_value_type) * share->all_link_count,
4099 #endif
4100       &tmp_hs_r_name, sizeof(char) * share->hs_read_conn_keys_charlen,
4101       &share->hs_write_conn_keys, sizeof(char *) * share->all_link_count,
4102       &share->hs_write_conn_keys_lengths, sizeof(uint) * share->all_link_count,
4103 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4104       &share->hs_write_conn_keys_hash_value,
4105         sizeof(my_hash_value_type) * share->all_link_count,
4106 #endif
4107       &tmp_hs_w_name, sizeof(char) * share->hs_write_conn_keys_charlen,
4108 #endif
4109       &share->sql_dbton_ids, sizeof(uint) * share->all_link_count,
4110 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4111       &share->hs_dbton_ids, sizeof(uint) * share->all_link_count,
4112 #endif
4113       NullS))
4114   ) {
4115     my_afree(conn_keys_lengths);
4116     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
4117   }
4118   share->conn_keys_length = share->all_link_count;
4119   memcpy(share->conn_keys_lengths, conn_keys_lengths,
4120     sizeof(uint) * share->all_link_count);
4121 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4122   share->hs_read_conn_keys_length = share->all_link_count;
4123   share->hs_write_conn_keys_length = share->all_link_count;
4124   memcpy(share->hs_read_conn_keys_lengths, hs_r_conn_keys_lengths,
4125     sizeof(uint) * share->all_link_count);
4126   memcpy(share->hs_write_conn_keys_lengths, hs_w_conn_keys_lengths,
4127     sizeof(uint) * share->all_link_count);
4128 #endif
4129 
4130   my_afree(conn_keys_lengths);
4131 
4132   for (roop_count = 0; roop_count < (int) share->all_link_count; roop_count++)
4133   {
4134     share->conn_keys[roop_count] = tmp_name;
4135     *tmp_name = '0';
4136     DBUG_PRINT("info",("spider tgt_wrappers[%d]=%s", roop_count,
4137       share->tgt_wrappers[roop_count]));
4138     tmp_name = strmov(tmp_name + 1, share->tgt_wrappers[roop_count]);
4139     DBUG_PRINT("info",("spider tgt_hosts[%d]=%s", roop_count,
4140       share->tgt_hosts[roop_count]));
4141     tmp_name = strmov(tmp_name + 1, share->tgt_hosts[roop_count]);
4142     my_sprintf(port_str, (port_str, "%05ld", share->tgt_ports[roop_count]));
4143     DBUG_PRINT("info",("spider port_str=%s", port_str));
4144     tmp_name = strmov(tmp_name + 1, port_str);
4145     if (share->tgt_sockets[roop_count])
4146     {
4147       DBUG_PRINT("info",("spider tgt_sockets[%d]=%s", roop_count,
4148         share->tgt_sockets[roop_count]));
4149       tmp_name = strmov(tmp_name + 1, share->tgt_sockets[roop_count]);
4150     } else
4151       tmp_name++;
4152     if (share->tgt_usernames[roop_count])
4153     {
4154       DBUG_PRINT("info",("spider tgt_usernames[%d]=%s", roop_count,
4155         share->tgt_usernames[roop_count]));
4156       tmp_name = strmov(tmp_name + 1, share->tgt_usernames[roop_count]);
4157     } else
4158       tmp_name++;
4159     if (share->tgt_passwords[roop_count])
4160     {
4161       DBUG_PRINT("info",("spider tgt_passwords[%d]=%s", roop_count,
4162         share->tgt_passwords[roop_count]));
4163       tmp_name = strmov(tmp_name + 1, share->tgt_passwords[roop_count]);
4164     } else
4165       tmp_name++;
4166     if (share->tgt_ssl_cas[roop_count])
4167     {
4168       DBUG_PRINT("info",("spider tgt_ssl_cas[%d]=%s", roop_count,
4169         share->tgt_ssl_cas[roop_count]));
4170       tmp_name = strmov(tmp_name + 1, share->tgt_ssl_cas[roop_count]);
4171     } else
4172       tmp_name++;
4173     if (share->tgt_ssl_capaths[roop_count])
4174     {
4175       DBUG_PRINT("info",("spider tgt_ssl_capaths[%d]=%s", roop_count,
4176         share->tgt_ssl_capaths[roop_count]));
4177       tmp_name = strmov(tmp_name + 1, share->tgt_ssl_capaths[roop_count]);
4178     } else
4179       tmp_name++;
4180     if (share->tgt_ssl_certs[roop_count])
4181     {
4182       DBUG_PRINT("info",("spider tgt_ssl_certs[%d]=%s", roop_count,
4183         share->tgt_ssl_certs[roop_count]));
4184       tmp_name = strmov(tmp_name + 1, share->tgt_ssl_certs[roop_count]);
4185     } else
4186       tmp_name++;
4187     if (share->tgt_ssl_ciphers[roop_count])
4188     {
4189       DBUG_PRINT("info",("spider tgt_ssl_ciphers[%d]=%s", roop_count,
4190         share->tgt_ssl_ciphers[roop_count]));
4191       tmp_name = strmov(tmp_name + 1, share->tgt_ssl_ciphers[roop_count]);
4192     } else
4193       tmp_name++;
4194     if (share->tgt_ssl_keys[roop_count])
4195     {
4196       DBUG_PRINT("info",("spider tgt_ssl_keys[%d]=%s", roop_count,
4197         share->tgt_ssl_keys[roop_count]));
4198       tmp_name = strmov(tmp_name + 1, share->tgt_ssl_keys[roop_count]);
4199     } else
4200       tmp_name++;
4201     tmp_name++;
4202     *tmp_name = '0' + ((char) share->tgt_ssl_vscs[roop_count]);
4203     if (share->tgt_default_files[roop_count])
4204     {
4205       DBUG_PRINT("info",("spider tgt_default_files[%d]=%s", roop_count,
4206         share->tgt_default_files[roop_count]));
4207       tmp_name = strmov(tmp_name + 1, share->tgt_default_files[roop_count]);
4208     } else
4209       tmp_name++;
4210     if (share->tgt_default_groups[roop_count])
4211     {
4212       DBUG_PRINT("info",("spider tgt_default_groups[%d]=%s", roop_count,
4213         share->tgt_default_groups[roop_count]));
4214       tmp_name = strmov(tmp_name + 1, share->tgt_default_groups[roop_count]);
4215     } else
4216       tmp_name++;
4217     tmp_name++;
4218     tmp_name++;
4219 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4220     share->conn_keys_hash_value[roop_count] = my_calc_hash(
4221       &spider_open_connections, (uchar*) share->conn_keys[roop_count],
4222       share->conn_keys_lengths[roop_count]);
4223 #endif
4224 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4225     share->hs_read_conn_keys[roop_count] = tmp_hs_r_name;
4226     *tmp_hs_r_name = '0';
4227     DBUG_PRINT("info",("spider tgt_wrappers[%d]=%s", roop_count,
4228       share->tgt_wrappers[roop_count]));
4229     tmp_hs_r_name = strmov(tmp_hs_r_name + 1, share->tgt_wrappers[roop_count]);
4230     DBUG_PRINT("info",("spider tgt_hosts[%d]=%s", roop_count,
4231       share->tgt_hosts[roop_count]));
4232     tmp_hs_r_name = strmov(tmp_hs_r_name + 1, share->tgt_hosts[roop_count]);
4233     my_sprintf(port_str, (port_str, "%05ld",
4234       share->hs_read_ports[roop_count]));
4235     DBUG_PRINT("info",("spider port_str=%s", port_str));
4236     tmp_hs_r_name = strmov(tmp_hs_r_name + 1, port_str);
4237     if (share->hs_read_socks[roop_count])
4238     {
4239       DBUG_PRINT("info",("spider hs_read_socks[%d]=%s", roop_count,
4240         share->hs_read_socks[roop_count]));
4241       tmp_hs_r_name = strmov(tmp_hs_r_name + 1,
4242         share->hs_read_socks[roop_count]);
4243     } else
4244       tmp_hs_r_name++;
4245     tmp_hs_r_name++;
4246     tmp_hs_r_name++;
4247 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4248     share->hs_read_conn_keys_hash_value[roop_count] = my_calc_hash(
4249       &spider_open_connections, (uchar*) share->hs_read_conn_keys[roop_count],
4250       share->hs_read_conn_keys_lengths[roop_count]);
4251 #endif
4252     share->hs_write_conn_keys[roop_count] = tmp_hs_w_name;
4253     *tmp_hs_w_name = '0';
4254     DBUG_PRINT("info",("spider tgt_wrappers[%d]=%s", roop_count,
4255       share->tgt_wrappers[roop_count]));
4256     tmp_hs_w_name = strmov(tmp_hs_w_name + 1, share->tgt_wrappers[roop_count]);
4257     DBUG_PRINT("info",("spider tgt_hosts[%d]=%s", roop_count,
4258       share->tgt_hosts[roop_count]));
4259     tmp_hs_w_name = strmov(tmp_hs_w_name + 1, share->tgt_hosts[roop_count]);
4260     my_sprintf(port_str, (port_str, "%05ld",
4261       share->hs_write_ports[roop_count]));
4262     DBUG_PRINT("info",("spider port_str=%s", port_str));
4263     tmp_hs_w_name = strmov(tmp_hs_w_name + 1, port_str);
4264     if (share->hs_write_socks[roop_count])
4265     {
4266       DBUG_PRINT("info",("spider hs_write_socks[%d]=%s", roop_count,
4267         share->hs_write_socks[roop_count]));
4268       tmp_hs_w_name = strmov(tmp_hs_w_name + 1,
4269         share->hs_write_socks[roop_count]);
4270     } else
4271       tmp_hs_w_name++;
4272     tmp_hs_w_name++;
4273     tmp_hs_w_name++;
4274 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4275     share->hs_write_conn_keys_hash_value[roop_count] = my_calc_hash(
4276       &spider_open_connections, (uchar*) share->hs_write_conn_keys[roop_count],
4277       share->hs_write_conn_keys_lengths[roop_count]);
4278 #endif
4279 #endif
4280 
4281     bool get_sql_id = FALSE;
4282 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4283     bool get_nosql_id = FALSE;
4284 #endif
4285     for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; roop_count2++)
4286     {
4287       DBUG_PRINT("info",("spider share->tgt_wrappers[%d]=%s", roop_count,
4288         share->tgt_wrappers[roop_count]));
4289       DBUG_PRINT("info",("spider spider_dbton[%d].wrapper=%s", roop_count2,
4290         spider_dbton[roop_count2].wrapper ?
4291           spider_dbton[roop_count2].wrapper : "NULL"));
4292       if (
4293         spider_dbton[roop_count2].wrapper &&
4294         !strcmp(share->tgt_wrappers[roop_count],
4295           spider_dbton[roop_count2].wrapper)
4296       ) {
4297         spider_set_bit(share->dbton_bitmap, roop_count2);
4298         if (
4299           !get_sql_id &&
4300           spider_dbton[roop_count2].db_access_type == SPIDER_DB_ACCESS_TYPE_SQL
4301         ) {
4302           share->sql_dbton_ids[roop_count] = roop_count2;
4303           get_sql_id = TRUE;
4304 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4305           if (get_nosql_id)
4306 #endif
4307             break;
4308 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4309           else
4310             continue;
4311 #endif
4312         }
4313 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4314         if (
4315           !get_nosql_id &&
4316           spider_dbton[roop_count2].db_access_type ==
4317             SPIDER_DB_ACCESS_TYPE_NOSQL
4318         ) {
4319           share->hs_dbton_ids[roop_count] = roop_count2;
4320           get_nosql_id = TRUE;
4321           if (get_sql_id)
4322             break;
4323         }
4324 #endif
4325       }
4326     }
4327     if (!get_sql_id)
4328       share->sql_dbton_ids[roop_count] = SPIDER_DBTON_SIZE;
4329 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4330     if (!get_nosql_id)
4331       share->hs_dbton_ids[roop_count] = SPIDER_DBTON_SIZE;
4332 #endif
4333   }
4334   for (roop_count2 = 0; roop_count2 < SPIDER_DBTON_SIZE; roop_count2++)
4335   {
4336     if (spider_bit_is_set(share->dbton_bitmap, roop_count2))
4337     {
4338 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4339       if (spider_dbton[roop_count2].db_access_type ==
4340         SPIDER_DB_ACCESS_TYPE_SQL)
4341       {
4342 #endif
4343         share->use_sql_dbton_ids[share->use_dbton_count] = roop_count2;
4344         share->sql_dbton_id_to_seq[roop_count2] = share->use_dbton_count;
4345         share->use_sql_dbton_count++;
4346 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4347       } else {
4348         share->use_hs_dbton_ids[share->use_hs_dbton_count] = roop_count2;
4349         share->hs_dbton_id_to_seq[roop_count2] = share->use_hs_dbton_count;
4350         share->use_hs_dbton_count++;
4351       }
4352 #endif
4353       share->use_dbton_ids[share->use_dbton_count] = roop_count2;
4354       share->dbton_id_to_seq[roop_count2] = share->use_dbton_count;
4355       share->use_dbton_count++;
4356     }
4357   }
4358   DBUG_RETURN(0);
4359 }
4360 
spider_create_share(const char * table_name,TABLE_SHARE * table_share,partition_info * part_info,my_hash_value_type hash_value,int * error_num)4361 SPIDER_SHARE *spider_create_share(
4362   const char *table_name,
4363   TABLE_SHARE *table_share,
4364 #ifdef WITH_PARTITION_STORAGE_ENGINE
4365   partition_info *part_info,
4366 #endif
4367 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4368   my_hash_value_type hash_value,
4369 #endif
4370   int *error_num
4371 ) {
4372   int bitmap_size, roop_count;
4373   uint length;
4374   int use_table_charset;
4375   SPIDER_SHARE *share;
4376   char *tmp_name;
4377   longlong *tmp_cardinality, *tmp_static_key_cardinality;
4378   uchar *tmp_cardinality_upd, *tmp_table_mon_mutex_bitmap;
4379   char buf[MAX_FIELD_WIDTH], *buf_pos;
4380   char link_idx_str[SPIDER_SQL_INT_LEN];
4381   DBUG_ENTER("spider_create_share");
4382   length = (uint) strlen(table_name);
4383   bitmap_size = spider_bitmap_size(table_share->fields);
4384   if (!(share = (SPIDER_SHARE *)
4385     spider_bulk_malloc(spider_current_trx, 46, MYF(MY_WME | MY_ZEROFILL),
4386       &share, sizeof(*share),
4387       &tmp_name, length + 1,
4388       &tmp_static_key_cardinality, sizeof(*tmp_static_key_cardinality) * table_share->keys,
4389       &tmp_cardinality, sizeof(*tmp_cardinality) * table_share->fields,
4390       &tmp_cardinality_upd, sizeof(*tmp_cardinality_upd) * bitmap_size,
4391       &tmp_table_mon_mutex_bitmap, sizeof(*tmp_table_mon_mutex_bitmap) *
4392         ((spider_param_udf_table_mon_mutex_count() + 7) / 8),
4393       NullS))
4394   ) {
4395     *error_num = HA_ERR_OUT_OF_MEM;
4396     goto error_alloc_share;
4397   }
4398 
4399   SPD_INIT_ALLOC_ROOT(&share->mem_root, 4096, 0, MYF(MY_WME));
4400   share->use_count = 0;
4401   share->use_dbton_count = 0;
4402 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4403   share->use_hs_dbton_count = 0;
4404 #endif
4405   share->table_name_length = length;
4406   share->table_name = tmp_name;
4407   strmov(share->table_name, table_name);
4408   share->static_key_cardinality = tmp_static_key_cardinality;
4409   share->cardinality = tmp_cardinality;
4410   share->cardinality_upd = tmp_cardinality_upd;
4411   share->table_mon_mutex_bitmap = tmp_table_mon_mutex_bitmap;
4412   share->bitmap_size = bitmap_size;
4413   share->table_share = table_share;
4414 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4415   share->table_name_hash_value = hash_value;
4416 #ifdef WITH_PARTITION_STORAGE_ENGINE
4417   share->table_path_hash_value = my_calc_hash(&spider_open_tables,
4418     (uchar*) table_share->path.str, table_share->path.length);
4419 #endif
4420 #endif
4421 #ifndef WITHOUT_SPIDER_BG_SEARCH
4422   share->table.s = table_share;
4423   share->table.field = table_share->field;
4424   share->table.key_info = table_share->key_info;
4425   share->table.read_set = &table_share->all_set;
4426 #endif
4427 
4428   if (table_share->keys > 0 &&
4429     !(share->key_hint = new spider_string[table_share->keys])
4430   ) {
4431     *error_num = HA_ERR_OUT_OF_MEM;
4432     goto error_init_hint_string;
4433   }
4434   for (roop_count = 0; roop_count < (int) table_share->keys; roop_count++)
4435     share->key_hint[roop_count].init_calc_mem(95);
4436   DBUG_PRINT("info",("spider share->key_hint=%p", share->key_hint));
4437 
4438   if ((*error_num = spider_parse_connect_info(share, table_share,
4439 #ifdef WITH_PARTITION_STORAGE_ENGINE
4440     part_info,
4441 #endif
4442     0)))
4443     goto error_parse_connect_string;
4444 
4445   for (roop_count = 0; roop_count < (int) share->all_link_count;
4446     roop_count++)
4447   {
4448     my_sprintf(link_idx_str, (link_idx_str, "%010d", roop_count));
4449     buf_pos = strmov(buf, share->table_name);
4450     buf_pos = strmov(buf_pos, link_idx_str);
4451     *buf_pos = '\0';
4452     spider_set_bit(tmp_table_mon_mutex_bitmap,
4453       spider_udf_calc_hash(buf, spider_param_udf_table_mon_mutex_count())
4454     );
4455   }
4456 
4457   use_table_charset = spider_param_use_table_charset(
4458     share->use_table_charset);
4459   if (table_share->table_charset && use_table_charset)
4460     share->access_charset = table_share->table_charset;
4461   else
4462     share->access_charset = system_charset_info;
4463 
4464   if ((*error_num = spider_create_conn_keys(share)))
4465     goto error_create_conn_keys;
4466 
4467   if (share->table_count_mode & 1)
4468     share->additional_table_flags |= HA_STATS_RECORDS_IS_EXACT;
4469   if (share->table_count_mode & 2)
4470     share->additional_table_flags |= HA_HAS_RECORDS;
4471 
4472 #if MYSQL_VERSION_ID < 50500
4473   if (pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST))
4474 #else
4475   if (mysql_mutex_init(spd_key_mutex_share,
4476     &share->mutex, MY_MUTEX_INIT_FAST))
4477 #endif
4478   {
4479     *error_num = HA_ERR_OUT_OF_MEM;
4480     goto error_init_mutex;
4481   }
4482 
4483 #if MYSQL_VERSION_ID < 50500
4484   if (pthread_mutex_init(&share->sts_mutex, MY_MUTEX_INIT_FAST))
4485 #else
4486   if (mysql_mutex_init(spd_key_mutex_share_sts,
4487     &share->sts_mutex, MY_MUTEX_INIT_FAST))
4488 #endif
4489   {
4490     *error_num = HA_ERR_OUT_OF_MEM;
4491     goto error_init_sts_mutex;
4492   }
4493 
4494 #if MYSQL_VERSION_ID < 50500
4495   if (pthread_mutex_init(&share->crd_mutex, MY_MUTEX_INIT_FAST))
4496 #else
4497   if (mysql_mutex_init(spd_key_mutex_share_crd,
4498     &share->crd_mutex, MY_MUTEX_INIT_FAST))
4499 #endif
4500   {
4501     *error_num = HA_ERR_OUT_OF_MEM;
4502     goto error_init_crd_mutex;
4503   }
4504 
4505   thr_lock_init(&share->lock);
4506 
4507 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4508   if (!(share->lgtm_tblhnd_share =
4509     spider_get_lgtm_tblhnd_share(tmp_name, length, hash_value, FALSE, TRUE,
4510     error_num)))
4511 #else
4512   if (!(share->lgtm_tblhnd_share =
4513     spider_get_lgtm_tblhnd_share(tmp_name, length, FALSE, TRUE, error_num)))
4514 #endif
4515   {
4516     goto error_get_lgtm_tblhnd_share;
4517   }
4518 
4519 #ifdef WITH_PARTITION_STORAGE_ENGINE
4520   if (!(share->partition_share =
4521     spider_get_pt_share(share, table_share, error_num)))
4522     goto error_get_pt_share;
4523 #endif
4524 
4525   for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; roop_count++)
4526   {
4527     if (spider_bit_is_set(share->dbton_bitmap, roop_count))
4528     {
4529       if (!(share->dbton_share[roop_count] =
4530         spider_dbton[roop_count].create_db_share(share)))
4531       {
4532         *error_num = HA_ERR_OUT_OF_MEM;
4533         goto error_init_dbton;
4534       }
4535       if ((*error_num = share->dbton_share[roop_count]->init()))
4536       {
4537         goto error_init_dbton;
4538       }
4539     }
4540   }
4541   DBUG_RETURN(share);
4542 
4543 /*
4544   roop_count = SPIDER_DBTON_SIZE - 1;
4545 */
4546 error_init_dbton:
4547   for (; roop_count >= 0; roop_count--)
4548   {
4549     if (share->dbton_share[roop_count])
4550     {
4551       delete share->dbton_share[roop_count];
4552       share->dbton_share[roop_count] = NULL;
4553     }
4554   }
4555 #ifdef WITH_PARTITION_STORAGE_ENGINE
4556   spider_free_pt_share(share->partition_share);
4557 error_get_pt_share:
4558 #endif
4559 error_get_lgtm_tblhnd_share:
4560   thr_lock_delete(&share->lock);
4561   pthread_mutex_destroy(&share->crd_mutex);
4562 error_init_crd_mutex:
4563   pthread_mutex_destroy(&share->sts_mutex);
4564 error_init_sts_mutex:
4565   pthread_mutex_destroy(&share->mutex);
4566 error_init_mutex:
4567 error_create_conn_keys:
4568 error_parse_connect_string:
4569 error_init_hint_string:
4570   spider_free_share_alloc(share);
4571   spider_free(spider_current_trx, share, MYF(0));
4572 error_alloc_share:
4573   DBUG_RETURN(NULL);
4574 }
4575 
spider_get_share(const char * table_name,TABLE * table,THD * thd,ha_spider * spider,int * error_num)4576 SPIDER_SHARE *spider_get_share(
4577   const char *table_name,
4578   TABLE *table,
4579   THD *thd,
4580   ha_spider *spider,
4581   int *error_num
4582 ) {
4583   SPIDER_SHARE *share;
4584   TABLE_SHARE *table_share = table->s;
4585   SPIDER_RESULT_LIST *result_list = &spider->result_list;
4586   uint length, tmp_conn_link_idx = 0;
4587   char *tmp_name, *tmp_cid;
4588 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4589   char *tmp_hs_r_name, *tmp_hs_w_name;
4590 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
4591   uint32 *tmp_hs_r_ret_fields, *tmp_hs_w_ret_fields;
4592 #endif
4593 #endif
4594   int roop_count;
4595   double sts_interval;
4596   int sts_mode;
4597 #ifdef WITH_PARTITION_STORAGE_ENGINE
4598   int sts_sync;
4599   int auto_increment_mode;
4600 #endif
4601   double crd_interval;
4602   int crd_mode;
4603 #ifdef WITH_PARTITION_STORAGE_ENGINE
4604   int crd_sync;
4605 #endif
4606   char first_byte;
4607   int semi_table_lock_conn;
4608   int search_link_idx;
4609   uint sql_command = thd_sql_command(thd);
4610 #if MYSQL_VERSION_ID < 50500
4611   Open_tables_state open_tables_backup;
4612 #else
4613   Open_tables_backup open_tables_backup;
4614 #endif
4615   MEM_ROOT mem_root;
4616   TABLE *table_tables = NULL;
4617   bool init_mem_root = FALSE;
4618   bool same_server_link;
4619   int load_sts_at_startup;
4620   int load_crd_at_startup;
4621   DBUG_ENTER("spider_get_share");
4622 
4623   length = (uint) strlen(table_name);
4624 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4625   my_hash_value_type hash_value = my_calc_hash(&spider_open_tables,
4626     (uchar*) table_name, length);
4627 #endif
4628   pthread_mutex_lock(&spider_tbl_mutex);
4629 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4630   if (!(share = (SPIDER_SHARE*) my_hash_search_using_hash_value(
4631     &spider_open_tables, hash_value, (uchar*) table_name, length)))
4632 #else
4633   if (!(share = (SPIDER_SHARE*) my_hash_search(&spider_open_tables,
4634     (uchar*) table_name, length)))
4635 #endif
4636   {
4637     if (!(share = spider_create_share(
4638       table_name, table_share,
4639 #ifdef WITH_PARTITION_STORAGE_ENGINE
4640       table->part_info,
4641 #endif
4642 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
4643       hash_value,
4644 #endif
4645       error_num
4646     ))) {
4647       goto error_alloc_share;
4648     }
4649 
4650     uint old_elements = spider_open_tables.array.max_element;
4651 #ifdef HASH_UPDATE_WITH_HASH_VALUE
4652     if (my_hash_insert_with_hash_value(&spider_open_tables, hash_value,
4653       (uchar*) share))
4654 #else
4655     if (my_hash_insert(&spider_open_tables, (uchar*) share))
4656 #endif
4657     {
4658       *error_num = HA_ERR_OUT_OF_MEM;
4659       goto error_hash_insert;
4660     }
4661     if (spider_open_tables.array.max_element > old_elements)
4662     {
4663       spider_alloc_calc_mem(spider_current_trx,
4664         spider_open_tables,
4665         (spider_open_tables.array.max_element - old_elements) *
4666         spider_open_tables.array.size_of_element);
4667     }
4668 
4669     spider->share = share;
4670     spider->conn_link_idx = &tmp_conn_link_idx;
4671 
4672     share->use_count++;
4673     pthread_mutex_unlock(&spider_tbl_mutex);
4674 
4675     if (!share->link_status_init)
4676     {
4677       pthread_mutex_lock(&share->mutex);
4678       for (roop_count = 0;
4679         roop_count < (int) spider_param_udf_table_mon_mutex_count();
4680         roop_count++
4681       ) {
4682         if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
4683           pthread_mutex_lock(&spider_udf_table_mon_mutexes[roop_count]);
4684       }
4685       if (!share->link_status_init)
4686       {
4687         /*
4688           The link statuses need to be refreshed from the spider_tables table
4689           if the operation:
4690           - Is not a DROP TABLE on a permanent table; or
4691           - Is an ALTER TABLE.
4692 
4693           Note that SHOW CREATE TABLE is not excluded, because the commands
4694           that follow it require up-to-date link statuses.
4695         */
4696         if ((table_share->tmp_table == NO_TMP_TABLE &&
4697              sql_command != SQLCOM_DROP_TABLE) ||
4698             /* for alter change link status */
4699             sql_command == SQLCOM_ALTER_TABLE)
4700         {
4701           SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME));
4702           init_mem_root = TRUE;
4703           if (
4704             !(table_tables = spider_open_sys_table(
4705               thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
4706               SPIDER_SYS_TABLES_TABLE_NAME_LEN, FALSE, &open_tables_backup,
4707               FALSE, error_num))
4708           ) {
4709             for (roop_count = 0;
4710               roop_count < (int) spider_param_udf_table_mon_mutex_count();
4711               roop_count++
4712             ) {
4713               if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
4714                 pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
4715             }
4716             pthread_mutex_unlock(&share->mutex);
4717             share->init_error = TRUE;
4718             share->init_error_time = (time_t) time((time_t*) 0);
4719             share->init = TRUE;
4720             spider_free_share(share);
4721             goto error_open_sys_table;
4722           }
4723           *error_num = spider_get_link_statuses(table_tables, share,
4724             &mem_root);
4725           if (*error_num)
4726           {
4727             if (
4728               *error_num != HA_ERR_KEY_NOT_FOUND &&
4729               *error_num != HA_ERR_END_OF_FILE
4730             ) {
4731               for (roop_count = 0;
4732                 roop_count < (int) spider_param_udf_table_mon_mutex_count();
4733                 roop_count++
4734               ) {
4735                 if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
4736                   pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
4737               }
4738               pthread_mutex_unlock(&share->mutex);
4739               share->init_error = TRUE;
4740               share->init_error_time = (time_t) time((time_t*) 0);
4741               share->init = TRUE;
4742               spider_free_share(share);
4743               goto error_get_link_statuses;
4744             }
4745           } else {
4746             memcpy(share->alter_table.tmp_link_statuses, share->link_statuses,
4747               sizeof(long) * share->all_link_count);
4748             share->link_status_init = TRUE;
4749           }
4750         }
4751         share->have_recovery_link = spider_conn_check_recovery_link(share);
4752         if (table_tables)
4753         {
4754           spider_close_sys_table(thd, table_tables,
4755             &open_tables_backup, FALSE);
4756           table_tables = NULL;
4757         }
4758         if (init_mem_root)
4759         {
4760           free_root(&mem_root, MYF(0));
4761           init_mem_root = FALSE;
4762         }
4763       }
4764       for (roop_count = 0;
4765         roop_count < (int) spider_param_udf_table_mon_mutex_count();
4766         roop_count++
4767       ) {
4768         if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
4769           pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
4770       }
4771       pthread_mutex_unlock(&share->mutex);
4772     }
4773 
4774     semi_table_lock_conn = spider_param_semi_table_lock_connection(thd,
4775       share->semi_table_lock_conn);
4776     if (semi_table_lock_conn)
4777       first_byte = '0' +
4778         spider_param_semi_table_lock(thd, share->semi_table_lock);
4779     else
4780       first_byte = '0';
4781 
4782     if (!(spider->trx = spider_get_trx(thd, TRUE, error_num)))
4783     {
4784       share->init_error = TRUE;
4785       share->init_error_time = (time_t) time((time_t*) 0);
4786       share->init = TRUE;
4787       spider_free_share(share);
4788       goto error_but_no_delete;
4789     }
4790     spider->set_error_mode();
4791 
4792 #ifndef WITHOUT_SPIDER_BG_SEARCH
4793     if (!share->sts_spider_init)
4794     {
4795       pthread_mutex_lock(&share->mutex);
4796       if (!share->sts_spider_init)
4797       {
4798         if ((*error_num = spider_create_spider_object_for_share(
4799           spider->trx, share, &share->sts_spider)))
4800         {
4801           pthread_mutex_unlock(&share->mutex);
4802           share->init_error = TRUE;
4803           share->init_error_time = (time_t) time((time_t*) 0);
4804           share->init = TRUE;
4805           spider_free_share(share);
4806           goto error_sts_spider_init;
4807         }
4808 #ifdef HASH_UPDATE_WITH_HASH_VALUE
4809         share->sts_thread = &spider_table_sts_threads[
4810           hash_value % spider_param_table_sts_thread_count()];
4811 #else
4812         share->sts_thread = &spider_table_sts_threads[
4813           my_calc_hash(&spider_open_tables, (uchar*) table_name, length) %
4814           spider_param_table_sts_thread_count()];
4815 #endif
4816         share->sts_spider_init = TRUE;
4817       }
4818       pthread_mutex_unlock(&share->mutex);
4819     }
4820 
4821     if (!share->crd_spider_init)
4822     {
4823       pthread_mutex_lock(&share->mutex);
4824       if (!share->crd_spider_init)
4825       {
4826         if ((*error_num = spider_create_spider_object_for_share(
4827           spider->trx, share, &share->crd_spider)))
4828         {
4829           pthread_mutex_unlock(&share->mutex);
4830           share->init_error = TRUE;
4831           share->init_error_time = (time_t) time((time_t*) 0);
4832           share->init = TRUE;
4833           spider_free_share(share);
4834           goto error_crd_spider_init;
4835         }
4836 #ifdef HASH_UPDATE_WITH_HASH_VALUE
4837         share->crd_thread = &spider_table_crd_threads[
4838           hash_value % spider_param_table_crd_thread_count()];
4839 #else
4840         share->crd_thread = &spider_table_crd_threads[
4841           my_calc_hash(&spider_open_tables, (uchar*) table_name, length) %
4842           spider_param_table_crd_thread_count()];
4843 #endif
4844         share->crd_spider_init = TRUE;
4845       }
4846       pthread_mutex_unlock(&share->mutex);
4847     }
4848 #endif
4849 
4850 #ifndef WITHOUT_SPIDER_BG_SEARCH
4851     if (
4852       sql_command != SQLCOM_DROP_TABLE &&
4853       sql_command != SQLCOM_ALTER_TABLE &&
4854       sql_command != SQLCOM_SHOW_CREATE &&
4855       (*error_num = spider_create_mon_threads(spider->trx, share))
4856     ) {
4857       share->init_error = TRUE;
4858       share->init_error_time = (time_t) time((time_t*) 0);
4859       share->init = TRUE;
4860       spider_free_share(share);
4861       goto error_but_no_delete;
4862     }
4863 #endif
4864 
4865     if (!(spider->conn_keys = (char **)
4866       spider_bulk_alloc_mem(spider_current_trx, 47,
4867         __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
4868         &spider->conn_keys, sizeof(char *) * share->link_count,
4869         &tmp_name, sizeof(char) * share->conn_keys_charlen,
4870         &spider->conns, sizeof(SPIDER_CONN *) * share->link_count,
4871         &spider->conn_link_idx, sizeof(uint) * share->link_count,
4872         &spider->conn_can_fo, sizeof(uchar) * share->link_bitmap_size,
4873 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4874         &spider->hs_r_conn_keys, sizeof(char *) * share->link_count,
4875         &tmp_hs_r_name, sizeof(char) * share->hs_read_conn_keys_charlen,
4876         &spider->hs_r_conns, sizeof(SPIDER_CONN *) * share->link_count,
4877         &spider->hs_r_conn_ages, sizeof(ulonglong) * share->link_count,
4878         &spider->hs_w_conn_keys, sizeof(char *) * share->link_count,
4879         &tmp_hs_w_name, sizeof(char) * share->hs_write_conn_keys_charlen,
4880         &spider->hs_w_conns, sizeof(SPIDER_CONN *) * share->link_count,
4881         &spider->hs_w_conn_ages, sizeof(ulonglong) * share->link_count,
4882 #endif
4883         &spider->sql_kind, sizeof(uint) * share->link_count,
4884         &spider->connection_ids, sizeof(ulonglong) * share->link_count,
4885         &spider->conn_kind, sizeof(uint) * share->link_count,
4886         &spider->db_request_id, sizeof(ulonglong) * share->link_count,
4887         &spider->db_request_phase, sizeof(uchar) * share->link_bitmap_size,
4888         &spider->m_handler_opened, sizeof(uchar) * share->link_bitmap_size,
4889         &spider->m_handler_id, sizeof(uint) * share->link_count,
4890         &spider->m_handler_cid, sizeof(char *) * share->link_count,
4891 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4892         &spider->r_handler_opened, sizeof(uchar) * share->link_bitmap_size,
4893         &spider->r_handler_id, sizeof(uint) * share->link_count,
4894         &spider->r_handler_index, sizeof(uint) * share->link_count,
4895         &spider->w_handler_opened, sizeof(uchar) * share->link_bitmap_size,
4896         &spider->w_handler_id, sizeof(uint) * share->link_count,
4897         &spider->w_handler_index, sizeof(uint) * share->link_count,
4898 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
4899         &spider->do_hs_direct_update, sizeof(uchar) * share->link_bitmap_size,
4900         &spider->hs_r_ret_fields, sizeof(uint32 *) * share->link_count,
4901         &spider->hs_w_ret_fields, sizeof(uint32 *) * share->link_count,
4902         &spider->hs_r_ret_fields_num, sizeof(size_t) * share->link_count,
4903         &spider->hs_w_ret_fields_num, sizeof(size_t) * share->link_count,
4904         &tmp_hs_r_ret_fields,
4905           sizeof(uint32) * share->link_count * table_share->fields,
4906         &tmp_hs_w_ret_fields,
4907           sizeof(uint32) * share->link_count * table_share->fields,
4908         &spider->tmp_column_bitmap, sizeof(uchar) * share->bitmap_size,
4909 #endif
4910 #endif
4911         &tmp_cid, sizeof(char) * (SPIDER_SQL_HANDLER_CID_LEN + 1) *
4912           share->link_count,
4913         &spider->need_mons, sizeof(int) * share->link_count,
4914         &spider->quick_targets, sizeof(void *) * share->link_count,
4915         &result_list->upd_tmp_tbls, sizeof(TABLE *) * share->link_count,
4916         &result_list->upd_tmp_tbl_prms,
4917           sizeof(TMP_TABLE_PARAM) * share->link_count,
4918         &result_list->tmp_table_join_first,
4919           sizeof(uchar) * share->link_bitmap_size,
4920         &result_list->tmp_table_created,
4921           sizeof(uchar) * share->link_bitmap_size,
4922 #ifdef HA_CAN_BULK_ACCESS
4923 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4924         &result_list->hs_r_bulk_open_index,
4925           sizeof(uchar) * share->link_bitmap_size,
4926         &result_list->hs_w_bulk_open_index,
4927           sizeof(uchar) * share->link_bitmap_size,
4928 #endif
4929 #endif
4930         &result_list->sql_kind_backup, sizeof(uint) * share->link_count,
4931         &result_list->casual_read, sizeof(int) * share->link_count,
4932         &spider->dbton_handler,
4933           sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE,
4934         NullS))
4935     ) {
4936       share->init_error = TRUE;
4937       share->init_error_time = (time_t) time((time_t*) 0);
4938       share->init = TRUE;
4939       spider_free_share(share);
4940       goto error_but_no_delete;
4941     }
4942     memcpy(tmp_name, share->conn_keys[0], share->conn_keys_charlen);
4943 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4944     memcpy(tmp_hs_r_name, share->hs_read_conn_keys[0],
4945       share->hs_read_conn_keys_charlen);
4946     memcpy(tmp_hs_w_name, share->hs_write_conn_keys[0],
4947       share->hs_write_conn_keys_charlen);
4948 #endif
4949 
4950     spider->conn_keys_first_ptr = tmp_name;
4951     for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
4952     {
4953       spider->conn_keys[roop_count] = tmp_name;
4954       *tmp_name = first_byte;
4955       tmp_name += share->conn_keys_lengths[roop_count] + 1;
4956 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
4957       spider->hs_r_conn_keys[roop_count] = tmp_hs_r_name;
4958       tmp_hs_r_name += share->hs_read_conn_keys_lengths[roop_count] + 1;
4959       spider->hs_w_conn_keys[roop_count] = tmp_hs_w_name;
4960       tmp_hs_w_name += share->hs_write_conn_keys_lengths[roop_count] + 1;
4961 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
4962       spider->hs_r_ret_fields[roop_count] = tmp_hs_r_ret_fields;
4963       tmp_hs_r_ret_fields += table_share->fields;
4964       spider->hs_w_ret_fields[roop_count] = tmp_hs_w_ret_fields;
4965       tmp_hs_w_ret_fields += table_share->fields;
4966 #endif
4967 #endif
4968       spider->m_handler_cid[roop_count] = tmp_cid;
4969       tmp_cid += SPIDER_SQL_HANDLER_CID_LEN + 1;
4970       result_list->upd_tmp_tbl_prms[roop_count].init();
4971       result_list->upd_tmp_tbl_prms[roop_count].field_count = 1;
4972       spider->conn_kind[roop_count] = SPIDER_CONN_KIND_MYSQL;
4973     }
4974     spider_trx_set_link_idx_for_all(spider);
4975 
4976     for (roop_count = 0; roop_count < (int) share->use_dbton_count;
4977       roop_count++)
4978     {
4979       uint dbton_id = share->use_dbton_ids[roop_count];
4980       if (!(spider->dbton_handler[dbton_id] =
4981         spider_dbton[dbton_id].create_db_handler(spider,
4982         share->dbton_share[dbton_id])))
4983       {
4984         *error_num = HA_ERR_OUT_OF_MEM;
4985         break;
4986       }
4987       if ((*error_num = spider->dbton_handler[dbton_id]->init()))
4988       {
4989         break;
4990       }
4991     }
4992     if (roop_count < (int) share->use_dbton_count)
4993     {
4994       for (; roop_count >= 0; roop_count--)
4995       {
4996         uint dbton_id = share->use_dbton_ids[roop_count];
4997         if (spider->dbton_handler[dbton_id])
4998         {
4999           delete spider->dbton_handler[dbton_id];
5000           spider->dbton_handler[dbton_id] = NULL;
5001         }
5002       }
5003       share->init_error = TRUE;
5004       share->init_error_time = (time_t) time((time_t*) 0);
5005       share->init = TRUE;
5006       spider_free_share(share);
5007       goto error_but_no_delete;
5008     }
5009 
5010     if (
5011       sql_command != SQLCOM_DROP_TABLE &&
5012       sql_command != SQLCOM_ALTER_TABLE &&
5013       sql_command != SQLCOM_SHOW_CREATE
5014     ) {
5015       for (
5016         roop_count = spider_conn_link_idx_next(share->link_statuses,
5017           spider->conn_link_idx, -1, share->link_count,
5018           SPIDER_LINK_STATUS_RECOVERY);
5019         roop_count < (int) share->link_count;
5020         roop_count = spider_conn_link_idx_next(share->link_statuses,
5021           spider->conn_link_idx, roop_count, share->link_count,
5022           SPIDER_LINK_STATUS_RECOVERY)
5023       ) {
5024         if (
5025           !(spider->conns[roop_count] =
5026             spider_get_conn(share, roop_count, spider->conn_keys[roop_count],
5027               spider->trx, spider, FALSE, TRUE, SPIDER_CONN_KIND_MYSQL,
5028               error_num))
5029         ) {
5030           if (
5031             share->monitoring_kind[roop_count] &&
5032             spider->need_mons[roop_count]
5033           ) {
5034             *error_num = spider_ping_table_mon_from_table(
5035                 spider->trx,
5036                 spider->trx->thd,
5037                 share,
5038                 roop_count,
5039                 (uint32) share->monitoring_sid[roop_count],
5040                 share->table_name,
5041                 share->table_name_length,
5042                 spider->conn_link_idx[roop_count],
5043                 NULL,
5044                 0,
5045                 share->monitoring_kind[roop_count],
5046                 share->monitoring_limit[roop_count],
5047                 share->monitoring_flag[roop_count],
5048                 FALSE
5049               );
5050           }
5051           share->init_error = TRUE;
5052           share->init_error_time = (time_t) time((time_t*) 0);
5053           share->init = TRUE;
5054           spider_free_share(share);
5055           goto error_but_no_delete;
5056         }
5057         spider->conns[roop_count]->error_mode &= spider->error_mode;
5058       }
5059     }
5060     search_link_idx = spider_conn_first_link_idx(thd,
5061       share->link_statuses, share->access_balances, spider->conn_link_idx,
5062       share->link_count, SPIDER_LINK_STATUS_OK);
5063     if (search_link_idx == -1)
5064     {
5065       char *db = (char *) my_alloca(
5066         table_share->db.length + 1 + table_share->table_name.length + 1);
5067       if (!db)
5068       {
5069         *error_num = HA_ERR_OUT_OF_MEM;
5070         share->init_error = TRUE;
5071         share->init_error_time = (time_t) time((time_t*) 0);
5072         share->init = TRUE;
5073         spider_free_share(share);
5074         goto error_but_no_delete;
5075       }
5076       char *table_name = db + table_share->db.length + 1;
5077       memcpy(db, table_share->db.str, table_share->db.length);
5078       db[table_share->db.length] = '\0';
5079       memcpy(table_name, table_share->table_name.str,
5080         table_share->table_name.length);
5081       table_name[table_share->table_name.length] = '\0';
5082       my_printf_error(ER_SPIDER_ALL_LINKS_FAILED_NUM,
5083         ER_SPIDER_ALL_LINKS_FAILED_STR, MYF(0), db, table_name);
5084       my_afree(db);
5085       *error_num = ER_SPIDER_ALL_LINKS_FAILED_NUM;
5086       share->init_error = TRUE;
5087       share->init_error_time = (time_t) time((time_t*) 0);
5088       share->init = TRUE;
5089       spider_free_share(share);
5090       goto error_but_no_delete;
5091     } else if (search_link_idx == -2)
5092     {
5093       *error_num = HA_ERR_OUT_OF_MEM;
5094       share->init_error = TRUE;
5095       share->init_error_time = (time_t) time((time_t*) 0);
5096       share->init = TRUE;
5097       spider_free_share(share);
5098       goto error_but_no_delete;
5099     }
5100     spider->search_link_idx = search_link_idx;
5101 
5102     same_server_link = spider_param_same_server_link(thd);
5103     load_sts_at_startup =
5104       spider_param_load_sts_at_startup(share->load_sts_at_startup);
5105     load_crd_at_startup =
5106       spider_param_load_crd_at_startup(share->load_crd_at_startup);
5107     if (
5108       sql_command != SQLCOM_DROP_TABLE &&
5109       sql_command != SQLCOM_ALTER_TABLE &&
5110       sql_command != SQLCOM_SHOW_CREATE &&
5111       !spider->error_mode &&
5112       (
5113         !same_server_link ||
5114         load_sts_at_startup ||
5115         load_crd_at_startup
5116       )
5117     ) {
5118       SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
5119       sts_interval = spider_param_sts_interval(thd, share->sts_interval);
5120       sts_mode = spider_param_sts_mode(thd, share->sts_mode);
5121 #ifdef WITH_PARTITION_STORAGE_ENGINE
5122       sts_sync = spider_param_sts_sync(thd, share->sts_sync);
5123       auto_increment_mode = spider_param_auto_increment_mode(thd,
5124         share->auto_increment_mode);
5125       if (auto_increment_mode == 1)
5126         sts_sync = 0;
5127 #endif
5128       crd_interval = spider_param_crd_interval(thd, share->crd_interval);
5129       crd_mode = spider_param_crd_mode(thd, share->crd_mode);
5130       if (crd_mode == 3)
5131         crd_mode = 1;
5132 #ifdef WITH_PARTITION_STORAGE_ENGINE
5133       crd_sync = spider_param_crd_sync(thd, share->crd_sync);
5134 #endif
5135       time_t tmp_time = (time_t) time((time_t*) 0);
5136       pthread_mutex_lock(&share->sts_mutex);
5137       pthread_mutex_lock(&share->crd_mutex);
5138       if ((spider_init_error_table =
5139         spider_get_init_error_table(spider->trx, share, FALSE)))
5140       {
5141         DBUG_PRINT("info",("spider diff1=%f",
5142           difftime(tmp_time, spider_init_error_table->init_error_time)));
5143         if (difftime(tmp_time,
5144           spider_init_error_table->init_error_time) <
5145           spider_param_table_init_error_interval())
5146         {
5147           *error_num = spider_init_error_table->init_error;
5148           if (spider_init_error_table->init_error_with_message)
5149             my_message(spider_init_error_table->init_error,
5150               spider_init_error_table->init_error_msg, MYF(0));
5151           share->init_error = TRUE;
5152           share->init = TRUE;
5153           pthread_mutex_unlock(&share->crd_mutex);
5154           pthread_mutex_unlock(&share->sts_mutex);
5155           spider_free_share(share);
5156           goto error_but_no_delete;
5157         }
5158       }
5159 
5160       if (
5161         (
5162           !same_server_link ||
5163           load_sts_at_startup
5164         ) &&
5165         spider_get_sts(share, spider->search_link_idx, tmp_time,
5166           spider, sts_interval, sts_mode,
5167 #ifdef WITH_PARTITION_STORAGE_ENGINE
5168           sts_sync,
5169 #endif
5170           1, HA_STATUS_VARIABLE | HA_STATUS_CONST | HA_STATUS_AUTO)
5171       ) {
5172         thd->clear_error();
5173       }
5174       if (
5175         (
5176           !same_server_link ||
5177           load_crd_at_startup
5178         ) &&
5179         spider_get_crd(share, spider->search_link_idx, tmp_time,
5180           spider, table, crd_interval, crd_mode,
5181 #ifdef WITH_PARTITION_STORAGE_ENGINE
5182           crd_sync,
5183 #endif
5184           1)
5185       ) {
5186         thd->clear_error();
5187       }
5188       pthread_mutex_unlock(&share->crd_mutex);
5189       pthread_mutex_unlock(&share->sts_mutex);
5190     }
5191 
5192     share->init = TRUE;
5193   } else {
5194     share->use_count++;
5195     pthread_mutex_unlock(&spider_tbl_mutex);
5196 
5197     int sleep_cnt = 0;
5198     while (!share->init)
5199     {
5200       // avoid for dead loop
5201       if (sleep_cnt++ > 1000)
5202       {
5203         fprintf(stderr, " [WARN SPIDER RESULT] "
5204           "Wait share->init too long, table_name %s %s %ld\n",
5205           share->table_name, share->tgt_hosts[0], share->tgt_ports[0]);
5206         *error_num = ER_SPIDER_TABLE_OPEN_TIMEOUT_NUM;
5207         my_printf_error(ER_SPIDER_TABLE_OPEN_TIMEOUT_NUM,
5208           ER_SPIDER_TABLE_OPEN_TIMEOUT_STR, MYF(0),
5209           table_share->db.str, table_share->table_name.str);
5210         goto error_but_no_delete;
5211       }
5212       my_sleep(10000); // wait 10 ms
5213     }
5214 
5215     if (!share->link_status_init)
5216     {
5217       pthread_mutex_lock(&share->mutex);
5218       for (roop_count = 0;
5219         roop_count < (int) spider_param_udf_table_mon_mutex_count();
5220         roop_count++
5221       ) {
5222         if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
5223           pthread_mutex_lock(&spider_udf_table_mon_mutexes[roop_count]);
5224       }
5225       if (!share->link_status_init)
5226       {
5227         /*
5228           The link statuses need to be refreshed from the spider_tables table
5229           if the operation:
5230           - Is not a DROP TABLE on a permanent table; or
5231           - Is an ALTER TABLE.
5232 
5233           Note that SHOW CREATE TABLE is not excluded, because the commands
5234           that follow it require up-to-date link statuses.
5235         */
5236         if ((table_share->tmp_table == NO_TMP_TABLE &&
5237              sql_command != SQLCOM_DROP_TABLE) ||
5238             /* for alter change link status */
5239             sql_command == SQLCOM_ALTER_TABLE)
5240         {
5241           SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME));
5242           init_mem_root = TRUE;
5243           if (
5244             !(table_tables = spider_open_sys_table(
5245               thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
5246               SPIDER_SYS_TABLES_TABLE_NAME_LEN, FALSE, &open_tables_backup,
5247               FALSE, error_num))
5248           ) {
5249             for (roop_count = 0;
5250               roop_count < (int) spider_param_udf_table_mon_mutex_count();
5251               roop_count++
5252             ) {
5253               if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
5254                 pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
5255             }
5256             pthread_mutex_unlock(&share->mutex);
5257             spider_free_share(share);
5258             goto error_open_sys_table;
5259           }
5260           *error_num = spider_get_link_statuses(table_tables, share,
5261             &mem_root);
5262           if (*error_num)
5263           {
5264             if (
5265               *error_num != HA_ERR_KEY_NOT_FOUND &&
5266               *error_num != HA_ERR_END_OF_FILE
5267             ) {
5268               for (roop_count = 0;
5269                 roop_count < (int) spider_param_udf_table_mon_mutex_count();
5270                 roop_count++
5271               ) {
5272                 if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
5273                   pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
5274               }
5275               pthread_mutex_unlock(&share->mutex);
5276               spider_free_share(share);
5277               goto error_get_link_statuses;
5278             }
5279           } else {
5280             memcpy(share->alter_table.tmp_link_statuses, share->link_statuses,
5281               sizeof(long) * share->all_link_count);
5282             share->link_status_init = TRUE;
5283           }
5284         }
5285         share->have_recovery_link = spider_conn_check_recovery_link(share);
5286         if (table_tables)
5287         {
5288           spider_close_sys_table(thd, table_tables,
5289             &open_tables_backup, FALSE);
5290           table_tables = NULL;
5291         }
5292         if (init_mem_root)
5293         {
5294           free_root(&mem_root, MYF(0));
5295           init_mem_root = FALSE;
5296         }
5297       }
5298       for (roop_count = 0;
5299         roop_count < (int) spider_param_udf_table_mon_mutex_count();
5300         roop_count++
5301       ) {
5302         if (spider_bit_is_set(share->table_mon_mutex_bitmap, roop_count))
5303           pthread_mutex_unlock(&spider_udf_table_mon_mutexes[roop_count]);
5304       }
5305       pthread_mutex_unlock(&share->mutex);
5306     }
5307 
5308     semi_table_lock_conn = spider_param_semi_table_lock_connection(thd,
5309       share->semi_table_lock_conn);
5310     if (semi_table_lock_conn)
5311       first_byte = '0' +
5312         spider_param_semi_table_lock(thd, share->semi_table_lock);
5313     else
5314       first_byte = '0';
5315 
5316     spider->share = share;
5317     if (!(spider->trx = spider_get_trx(thd, TRUE, error_num)))
5318     {
5319       spider_free_share(share);
5320       goto error_but_no_delete;
5321     }
5322     spider->set_error_mode();
5323 
5324 #ifndef WITHOUT_SPIDER_BG_SEARCH
5325     if (!share->sts_spider_init)
5326     {
5327       pthread_mutex_lock(&share->mutex);
5328       if (!share->sts_spider_init)
5329       {
5330         if ((*error_num = spider_create_spider_object_for_share(
5331           spider->trx, share, &share->sts_spider)))
5332         {
5333           pthread_mutex_unlock(&share->mutex);
5334           spider_free_share(share);
5335           goto error_sts_spider_init;
5336         }
5337 #ifdef HASH_UPDATE_WITH_HASH_VALUE
5338         share->sts_thread = &spider_table_sts_threads[
5339           hash_value % spider_param_table_sts_thread_count()];
5340 #else
5341         share->sts_thread = &spider_table_sts_threads[
5342           my_calc_hash(&spider_open_tables, (uchar*) table_name, length) %
5343           spider_param_table_sts_thread_count()];
5344 #endif
5345         share->sts_spider_init = TRUE;
5346       }
5347       pthread_mutex_unlock(&share->mutex);
5348     }
5349 
5350     if (!share->crd_spider_init)
5351     {
5352       pthread_mutex_lock(&share->mutex);
5353       if (!share->crd_spider_init)
5354       {
5355         if ((*error_num = spider_create_spider_object_for_share(
5356           spider->trx, share, &share->crd_spider)))
5357         {
5358           pthread_mutex_unlock(&share->mutex);
5359           spider_free_share(share);
5360           goto error_crd_spider_init;
5361         }
5362 #ifdef HASH_UPDATE_WITH_HASH_VALUE
5363         share->crd_thread = &spider_table_crd_threads[
5364           hash_value % spider_param_table_crd_thread_count()];
5365 #else
5366         share->crd_thread = &spider_table_crd_threads[
5367           my_calc_hash(&spider_open_tables, (uchar*) table_name, length) %
5368           spider_param_table_crd_thread_count()];
5369 #endif
5370         share->crd_spider_init = TRUE;
5371       }
5372       pthread_mutex_unlock(&share->mutex);
5373     }
5374 #endif
5375 
5376 #ifndef WITHOUT_SPIDER_BG_SEARCH
5377     if (
5378       sql_command != SQLCOM_DROP_TABLE &&
5379       sql_command != SQLCOM_ALTER_TABLE &&
5380       sql_command != SQLCOM_SHOW_CREATE &&
5381       (*error_num = spider_create_mon_threads(spider->trx, share))
5382     ) {
5383       spider_free_share(share);
5384       goto error_but_no_delete;
5385     }
5386 #endif
5387 
5388     if (!(spider->conn_keys = (char **)
5389       spider_bulk_alloc_mem(spider_current_trx, 49,
5390         __func__, __FILE__, __LINE__, MYF(MY_WME | MY_ZEROFILL),
5391         &spider->conn_keys, sizeof(char *) * share->link_count,
5392         &tmp_name, sizeof(char) * share->conn_keys_charlen,
5393         &spider->conns, sizeof(SPIDER_CONN *) * share->link_count,
5394         &spider->conn_link_idx, sizeof(uint) * share->link_count,
5395         &spider->conn_can_fo, sizeof(uchar) * share->link_bitmap_size,
5396 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5397         &spider->hs_r_conn_keys, sizeof(char *) * share->link_count,
5398         &tmp_hs_r_name, sizeof(char) * share->hs_read_conn_keys_charlen,
5399         &spider->hs_r_conns, sizeof(SPIDER_CONN *) * share->link_count,
5400         &spider->hs_r_conn_ages, sizeof(ulonglong) * share->link_count,
5401         &spider->hs_w_conn_keys, sizeof(char *) * share->link_count,
5402         &tmp_hs_w_name, sizeof(char) * share->hs_write_conn_keys_charlen,
5403         &spider->hs_w_conns, sizeof(SPIDER_CONN *) * share->link_count,
5404         &spider->hs_w_conn_ages, sizeof(ulonglong) * share->link_count,
5405 #endif
5406         &spider->sql_kind, sizeof(uint) * share->link_count,
5407         &spider->connection_ids, sizeof(ulonglong) * share->link_count,
5408         &spider->conn_kind, sizeof(uint) * share->link_count,
5409         &spider->db_request_id, sizeof(ulonglong) * share->link_count,
5410         &spider->db_request_phase, sizeof(uchar) * share->link_bitmap_size,
5411         &spider->m_handler_opened, sizeof(uchar) * share->link_bitmap_size,
5412         &spider->m_handler_id, sizeof(uint) * share->link_count,
5413         &spider->m_handler_cid, sizeof(char *) * share->link_count,
5414 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5415         &spider->r_handler_opened, sizeof(uchar) * share->link_bitmap_size,
5416         &spider->r_handler_id, sizeof(uint) * share->link_count,
5417         &spider->r_handler_index, sizeof(uint) * share->link_count,
5418         &spider->w_handler_opened, sizeof(uchar) * share->link_bitmap_size,
5419         &spider->w_handler_id, sizeof(uint) * share->link_count,
5420         &spider->w_handler_index, sizeof(uint) * share->link_count,
5421 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
5422         &spider->do_hs_direct_update, sizeof(uchar) * share->link_bitmap_size,
5423         &spider->hs_r_ret_fields, sizeof(uint32 *) * share->link_count,
5424         &spider->hs_w_ret_fields, sizeof(uint32 *) * share->link_count,
5425         &spider->hs_r_ret_fields_num, sizeof(size_t) * share->link_count,
5426         &spider->hs_w_ret_fields_num, sizeof(size_t) * share->link_count,
5427         &tmp_hs_r_ret_fields,
5428           sizeof(uint32) * share->link_count * table_share->fields,
5429         &tmp_hs_w_ret_fields,
5430           sizeof(uint32) * share->link_count * table_share->fields,
5431         &spider->tmp_column_bitmap, sizeof(uchar) * share->bitmap_size,
5432 #endif
5433 #endif
5434         &tmp_cid, sizeof(char) * (SPIDER_SQL_HANDLER_CID_LEN + 1) *
5435           share->link_count,
5436         &spider->need_mons, sizeof(int) * share->link_count,
5437         &spider->quick_targets, sizeof(void *) * share->link_count,
5438         &result_list->upd_tmp_tbls, sizeof(TABLE *) * share->link_count,
5439         &result_list->upd_tmp_tbl_prms,
5440           sizeof(TMP_TABLE_PARAM) * share->link_count,
5441         &result_list->tmp_table_join_first,
5442           sizeof(uchar) * share->link_bitmap_size,
5443         &result_list->tmp_table_created,
5444           sizeof(uchar) * share->link_bitmap_size,
5445 #ifdef HA_CAN_BULK_ACCESS
5446 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5447         &result_list->hs_r_bulk_open_index,
5448           sizeof(uchar) * share->link_bitmap_size,
5449         &result_list->hs_w_bulk_open_index,
5450           sizeof(uchar) * share->link_bitmap_size,
5451 #endif
5452 #endif
5453         &result_list->sql_kind_backup, sizeof(uint) * share->link_count,
5454         &result_list->casual_read, sizeof(int) * share->link_count,
5455         &spider->dbton_handler,
5456           sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE,
5457         NullS))
5458     ) {
5459       spider_free_share(share);
5460       goto error_but_no_delete;
5461     }
5462     memcpy(tmp_name, share->conn_keys[0], share->conn_keys_charlen);
5463 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5464     memcpy(tmp_hs_r_name, share->hs_read_conn_keys[0],
5465       share->hs_read_conn_keys_charlen);
5466     memcpy(tmp_hs_w_name, share->hs_write_conn_keys[0],
5467       share->hs_write_conn_keys_charlen);
5468 #endif
5469 
5470     spider->conn_keys_first_ptr = tmp_name;
5471     for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
5472     {
5473       spider->conn_keys[roop_count] = tmp_name;
5474       *tmp_name = first_byte;
5475       tmp_name += share->conn_keys_lengths[roop_count] + 1;
5476 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
5477       spider->hs_r_conn_keys[roop_count] = tmp_hs_r_name;
5478       tmp_hs_r_name += share->hs_read_conn_keys_lengths[roop_count] + 1;
5479       spider->hs_w_conn_keys[roop_count] = tmp_hs_w_name;
5480       tmp_hs_w_name += share->hs_write_conn_keys_lengths[roop_count] + 1;
5481 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
5482       spider->hs_r_ret_fields[roop_count] = tmp_hs_r_ret_fields;
5483       tmp_hs_r_ret_fields += table_share->fields;
5484       spider->hs_w_ret_fields[roop_count] = tmp_hs_w_ret_fields;
5485       tmp_hs_w_ret_fields += table_share->fields;
5486 #endif
5487 #endif
5488       spider->m_handler_cid[roop_count] = tmp_cid;
5489       tmp_cid += SPIDER_SQL_HANDLER_CID_LEN + 1;
5490       result_list->upd_tmp_tbl_prms[roop_count].init();
5491       result_list->upd_tmp_tbl_prms[roop_count].field_count = 1;
5492       spider->conn_kind[roop_count] = SPIDER_CONN_KIND_MYSQL;
5493     }
5494     spider_trx_set_link_idx_for_all(spider);
5495 
5496     for (roop_count = 0; roop_count < (int) share->use_dbton_count;
5497       roop_count++)
5498     {
5499       uint dbton_id = share->use_dbton_ids[roop_count];
5500       if (!(spider->dbton_handler[dbton_id] =
5501         spider_dbton[dbton_id].create_db_handler(spider,
5502         share->dbton_share[dbton_id])))
5503       {
5504         *error_num = HA_ERR_OUT_OF_MEM;
5505         break;
5506       }
5507       if ((*error_num = spider->dbton_handler[dbton_id]->init()))
5508       {
5509         break;
5510       }
5511     }
5512     if (roop_count < (int) share->use_dbton_count)
5513     {
5514       for (; roop_count >= 0; roop_count--)
5515       {
5516         uint dbton_id = share->use_dbton_ids[roop_count];
5517         if (spider->dbton_handler[dbton_id])
5518         {
5519           delete spider->dbton_handler[dbton_id];
5520           spider->dbton_handler[dbton_id] = NULL;
5521         }
5522       }
5523       goto error_but_no_delete;
5524     }
5525 
5526     if (
5527       sql_command != SQLCOM_DROP_TABLE &&
5528       sql_command != SQLCOM_ALTER_TABLE &&
5529       sql_command != SQLCOM_SHOW_CREATE
5530     ) {
5531       for (
5532         roop_count = spider_conn_link_idx_next(share->link_statuses,
5533           spider->conn_link_idx, -1, share->link_count,
5534           SPIDER_LINK_STATUS_RECOVERY);
5535         roop_count < (int) share->link_count;
5536         roop_count = spider_conn_link_idx_next(share->link_statuses,
5537           spider->conn_link_idx, roop_count, share->link_count,
5538           SPIDER_LINK_STATUS_RECOVERY)
5539       ) {
5540         if (
5541           !(spider->conns[roop_count] =
5542             spider_get_conn(share, roop_count, spider->conn_keys[roop_count],
5543               spider->trx, spider, FALSE, TRUE, SPIDER_CONN_KIND_MYSQL,
5544               error_num))
5545         ) {
5546           if (
5547             share->monitoring_kind[roop_count] &&
5548             spider->need_mons[roop_count]
5549           ) {
5550             *error_num = spider_ping_table_mon_from_table(
5551                 spider->trx,
5552                 spider->trx->thd,
5553                 share,
5554                 roop_count,
5555                 (uint32) share->monitoring_sid[roop_count],
5556                 share->table_name,
5557                 share->table_name_length,
5558                 spider->conn_link_idx[roop_count],
5559                 NULL,
5560                 0,
5561                 share->monitoring_kind[roop_count],
5562                 share->monitoring_limit[roop_count],
5563                 share->monitoring_flag[roop_count],
5564                 FALSE
5565               );
5566           }
5567           spider_free_share(share);
5568           goto error_but_no_delete;
5569         }
5570         spider->conns[roop_count]->error_mode &= spider->error_mode;
5571       }
5572     }
5573     search_link_idx = spider_conn_first_link_idx(thd,
5574       share->link_statuses, share->access_balances, spider->conn_link_idx,
5575       share->link_count, SPIDER_LINK_STATUS_OK);
5576     if (search_link_idx == -1)
5577     {
5578       char *db = (char *) my_alloca(
5579         table_share->db.length + 1 + table_share->table_name.length + 1);
5580       if (!db)
5581       {
5582         *error_num = HA_ERR_OUT_OF_MEM;
5583         spider_free_share(share);
5584         goto error_but_no_delete;
5585       }
5586       char *table_name = db + table_share->db.length + 1;
5587       memcpy(db, table_share->db.str, table_share->db.length);
5588       db[table_share->db.length] = '\0';
5589       memcpy(table_name, table_share->table_name.str,
5590         table_share->table_name.length);
5591       table_name[table_share->table_name.length] = '\0';
5592       my_printf_error(ER_SPIDER_ALL_LINKS_FAILED_NUM,
5593         ER_SPIDER_ALL_LINKS_FAILED_STR, MYF(0), db, table_name);
5594       my_afree(db);
5595       *error_num = ER_SPIDER_ALL_LINKS_FAILED_NUM;
5596       spider_free_share(share);
5597       goto error_but_no_delete;
5598     } else if (search_link_idx == -2)
5599     {
5600       *error_num = HA_ERR_OUT_OF_MEM;
5601       spider_free_share(share);
5602       goto error_but_no_delete;
5603     }
5604     spider->search_link_idx = search_link_idx;
5605 
5606     if (share->init_error)
5607     {
5608       pthread_mutex_lock(&share->sts_mutex);
5609       pthread_mutex_lock(&share->crd_mutex);
5610       if (share->init_error)
5611       {
5612         same_server_link = spider_param_same_server_link(thd);
5613         load_sts_at_startup =
5614           spider_param_load_sts_at_startup(share->load_sts_at_startup);
5615         load_crd_at_startup =
5616           spider_param_load_crd_at_startup(share->load_crd_at_startup);
5617         if (
5618           sql_command != SQLCOM_DROP_TABLE &&
5619           sql_command != SQLCOM_ALTER_TABLE &&
5620           sql_command != SQLCOM_SHOW_CREATE &&
5621           !spider->error_mode &&
5622           (
5623             !same_server_link ||
5624             load_sts_at_startup ||
5625             load_crd_at_startup
5626           )
5627         ) {
5628           SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
5629           sts_interval = spider_param_sts_interval(thd, share->sts_interval);
5630           sts_mode = spider_param_sts_mode(thd, share->sts_mode);
5631 #ifdef WITH_PARTITION_STORAGE_ENGINE
5632           sts_sync = spider_param_sts_sync(thd, share->sts_sync);
5633           auto_increment_mode = spider_param_auto_increment_mode(thd,
5634             share->auto_increment_mode);
5635           if (auto_increment_mode == 1)
5636             sts_sync = 0;
5637 #endif
5638           crd_interval = spider_param_crd_interval(thd, share->crd_interval);
5639           crd_mode = spider_param_crd_mode(thd, share->crd_mode);
5640           if (crd_mode == 3)
5641             crd_mode = 1;
5642 #ifdef WITH_PARTITION_STORAGE_ENGINE
5643           crd_sync = spider_param_crd_sync(thd, share->crd_sync);
5644 #endif
5645           time_t tmp_time = (time_t) time((time_t*) 0);
5646           if ((spider_init_error_table =
5647             spider_get_init_error_table(spider->trx, share, FALSE)))
5648           {
5649             DBUG_PRINT("info",("spider diff2=%f",
5650               difftime(tmp_time, spider_init_error_table->init_error_time)));
5651             if (difftime(tmp_time,
5652               spider_init_error_table->init_error_time) <
5653               spider_param_table_init_error_interval())
5654             {
5655               *error_num = spider_init_error_table->init_error;
5656               if (spider_init_error_table->init_error_with_message)
5657                 my_message(spider_init_error_table->init_error,
5658                   spider_init_error_table->init_error_msg, MYF(0));
5659               pthread_mutex_unlock(&share->crd_mutex);
5660               pthread_mutex_unlock(&share->sts_mutex);
5661               spider_free_share(share);
5662               goto error_but_no_delete;
5663             }
5664           }
5665 
5666           if (
5667             (
5668               !same_server_link ||
5669               load_sts_at_startup
5670             ) &&
5671             spider_get_sts(share, spider->search_link_idx,
5672               tmp_time, spider, sts_interval, sts_mode,
5673 #ifdef WITH_PARTITION_STORAGE_ENGINE
5674               sts_sync,
5675 #endif
5676               1, HA_STATUS_VARIABLE | HA_STATUS_CONST | HA_STATUS_AUTO)
5677           ) {
5678             thd->clear_error();
5679           }
5680           if (
5681             (
5682               !same_server_link ||
5683               load_crd_at_startup
5684             ) &&
5685             spider_get_crd(share, spider->search_link_idx,
5686               tmp_time, spider, table, crd_interval, crd_mode,
5687 #ifdef WITH_PARTITION_STORAGE_ENGINE
5688               crd_sync,
5689 #endif
5690               1)
5691           ) {
5692             thd->clear_error();
5693           }
5694         }
5695         share->init_error = FALSE;
5696       }
5697       pthread_mutex_unlock(&share->crd_mutex);
5698       pthread_mutex_unlock(&share->sts_mutex);
5699     }
5700   }
5701 
5702   DBUG_PRINT("info",("spider share=%p", share));
5703   DBUG_RETURN(share);
5704 
5705 error_hash_insert:
5706   spider_free_share_resource_only(share);
5707 error_alloc_share:
5708   pthread_mutex_unlock(&spider_tbl_mutex);
5709 error_get_link_statuses:
5710   if (table_tables)
5711   {
5712     spider_close_sys_table(thd, table_tables,
5713       &open_tables_backup, FALSE);
5714     table_tables = NULL;
5715   }
5716 error_open_sys_table:
5717 #ifndef WITHOUT_SPIDER_BG_SEARCH
5718 error_crd_spider_init:
5719 error_sts_spider_init:
5720 #endif
5721   if (init_mem_root)
5722   {
5723     free_root(&mem_root, MYF(0));
5724     init_mem_root = FALSE;
5725   }
5726 error_but_no_delete:
5727   DBUG_RETURN(NULL);
5728 }
5729 
spider_free_share_resource_only(SPIDER_SHARE * share)5730 void spider_free_share_resource_only(
5731   SPIDER_SHARE *share
5732 ) {
5733   DBUG_ENTER("spider_free_share_resource_only");
5734   spider_free_share_alloc(share);
5735   thr_lock_delete(&share->lock);
5736   pthread_mutex_destroy(&share->crd_mutex);
5737   pthread_mutex_destroy(&share->sts_mutex);
5738   pthread_mutex_destroy(&share->mutex);
5739   spider_free(spider_current_trx, share, MYF(0));
5740   DBUG_VOID_RETURN;
5741 }
5742 
spider_free_share(SPIDER_SHARE * share)5743 int spider_free_share(
5744   SPIDER_SHARE *share
5745 ) {
5746   DBUG_ENTER("spider_free_share");
5747   pthread_mutex_lock(&spider_tbl_mutex);
5748   bool do_delete_thd = false;
5749   THD *thd = current_thd;
5750   if (!--share->use_count)
5751   {
5752 #ifndef WITHOUT_SPIDER_BG_SEARCH
5753     spider_free_sts_thread(share);
5754     spider_free_crd_thread(share);
5755     spider_free_mon_threads(share);
5756     if (share->sts_spider_init)
5757     {
5758       spider_table_remove_share_from_sts_thread(share);
5759       spider_free_spider_object_for_share(&share->sts_spider);
5760     }
5761     if (share->crd_spider_init)
5762     {
5763       spider_table_remove_share_from_crd_thread(share);
5764       spider_free_spider_object_for_share(&share->crd_spider);
5765     }
5766 #endif
5767     if (
5768       share->sts_init &&
5769       spider_param_store_last_sts(share->store_last_sts)
5770     ) {
5771       if (!thd)
5772       {
5773         /* Create a thread for Spider system table update */
5774         thd = spider_create_thd();
5775         if (!thd)
5776           DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5777         do_delete_thd = TRUE;
5778       }
5779       spider_sys_insert_or_update_table_sts(
5780         thd,
5781         share->lgtm_tblhnd_share->table_name,
5782         share->lgtm_tblhnd_share->table_name_length,
5783         &share->data_file_length,
5784         &share->max_data_file_length,
5785         &share->index_file_length,
5786         &share->records,
5787         &share->mean_rec_length,
5788         &share->check_time,
5789         &share->create_time,
5790         &share->update_time,
5791         FALSE
5792       );
5793     }
5794     if (
5795       share->crd_init &&
5796       spider_param_store_last_crd(share->store_last_crd)
5797     ) {
5798       if (!thd)
5799       {
5800         /* Create a thread for Spider system table update */
5801         thd = spider_create_thd();
5802         if (!thd)
5803           DBUG_RETURN(HA_ERR_OUT_OF_MEM);
5804         do_delete_thd = TRUE;
5805       }
5806       spider_sys_insert_or_update_table_crd(
5807         thd,
5808         share->lgtm_tblhnd_share->table_name,
5809         share->lgtm_tblhnd_share->table_name_length,
5810         share->cardinality,
5811         share->table_share->fields,
5812         FALSE
5813       );
5814     }
5815     spider_free_share_alloc(share);
5816 #ifdef HASH_UPDATE_WITH_HASH_VALUE
5817     my_hash_delete_with_hash_value(&spider_open_tables,
5818       share->table_name_hash_value, (uchar*) share);
5819 #else
5820     my_hash_delete(&spider_open_tables, (uchar*) share);
5821 #endif
5822     thr_lock_delete(&share->lock);
5823     pthread_mutex_destroy(&share->crd_mutex);
5824     pthread_mutex_destroy(&share->sts_mutex);
5825     pthread_mutex_destroy(&share->mutex);
5826     free_root(&share->mem_root, MYF(0));
5827     spider_free(spider_current_trx, share, MYF(0));
5828   }
5829   if (do_delete_thd)
5830     spider_destroy_thd(thd);
5831   pthread_mutex_unlock(&spider_tbl_mutex);
5832   DBUG_RETURN(0);
5833 }
5834 
spider_update_link_status_for_share(const char * table_name,uint table_name_length,int link_idx,long link_status)5835 void spider_update_link_status_for_share(
5836   const char *table_name,
5837   uint table_name_length,
5838   int link_idx,
5839   long link_status
5840 ) {
5841   SPIDER_SHARE *share;
5842   DBUG_ENTER("spider_update_link_status_for_share");
5843 
5844 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
5845   my_hash_value_type hash_value = my_calc_hash(&spider_open_tables,
5846     (uchar*) table_name, table_name_length);
5847 #endif
5848   pthread_mutex_lock(&spider_tbl_mutex);
5849 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
5850   if ((share = (SPIDER_SHARE*) my_hash_search_using_hash_value(
5851     &spider_open_tables, hash_value, (uchar*) table_name,
5852     table_name_length)))
5853 #else
5854   if ((share = (SPIDER_SHARE*) my_hash_search(&spider_open_tables,
5855     (uchar*) table_name, table_name_length)))
5856 #endif
5857   {
5858     DBUG_PRINT("info", ("spider share->link_status_init=%s",
5859       share->link_status_init ? "TRUE" : "FALSE"));
5860     if (share->link_status_init)
5861     {
5862       DBUG_PRINT("info", ("spider share->link_statuses[%d]=%ld",
5863         link_idx, link_status));
5864       share->link_statuses[link_idx] = link_status;
5865     }
5866   }
5867   pthread_mutex_unlock(&spider_tbl_mutex);
5868   DBUG_VOID_RETURN;
5869 }
5870 
5871 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
spider_get_lgtm_tblhnd_share(const char * table_name,uint table_name_length,my_hash_value_type hash_value,bool locked,bool need_to_create,int * error_num)5872 SPIDER_LGTM_TBLHND_SHARE *spider_get_lgtm_tblhnd_share(
5873   const char *table_name,
5874   uint table_name_length,
5875   my_hash_value_type hash_value,
5876   bool locked,
5877   bool need_to_create,
5878   int *error_num
5879 )
5880 #else
5881 SPIDER_LGTM_TBLHND_SHARE *spider_get_lgtm_tblhnd_share(
5882   const char *table_name,
5883   uint table_name_length,
5884   bool locked,
5885   bool need_to_create,
5886   int *error_num
5887 )
5888 #endif
5889 {
5890   SPIDER_LGTM_TBLHND_SHARE *lgtm_tblhnd_share;
5891   char *tmp_name;
5892   DBUG_ENTER("spider_get_lgtm_tblhnd_share");
5893 
5894   if (!locked)
5895     pthread_mutex_lock(&spider_lgtm_tblhnd_share_mutex);
5896 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
5897   if (!(lgtm_tblhnd_share = (SPIDER_LGTM_TBLHND_SHARE*)
5898     my_hash_search_using_hash_value(
5899     &spider_lgtm_tblhnd_share_hash, hash_value,
5900     (uchar*) table_name, table_name_length)))
5901 #else
5902   if (!(lgtm_tblhnd_share = (SPIDER_LGTM_TBLHND_SHARE*) my_hash_search(
5903     &spider_lgtm_tblhnd_share_hash,
5904     (uchar*) table_name, table_name_length)))
5905 #endif
5906   {
5907     DBUG_PRINT("info",("spider create new lgtm tblhnd share"));
5908     if (!(lgtm_tblhnd_share = (SPIDER_LGTM_TBLHND_SHARE *)
5909       spider_bulk_malloc(spider_current_trx, 244, MYF(MY_WME | MY_ZEROFILL),
5910         &lgtm_tblhnd_share, sizeof(*lgtm_tblhnd_share),
5911         &tmp_name, table_name_length + 1,
5912         NullS))
5913     ) {
5914       *error_num = HA_ERR_OUT_OF_MEM;
5915       goto error_alloc_share;
5916     }
5917 
5918     lgtm_tblhnd_share->table_name_length = table_name_length;
5919     lgtm_tblhnd_share->table_name = tmp_name;
5920     memcpy(lgtm_tblhnd_share->table_name, table_name,
5921       lgtm_tblhnd_share->table_name_length);
5922 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
5923     lgtm_tblhnd_share->table_path_hash_value = hash_value;
5924 #endif
5925 
5926 #if MYSQL_VERSION_ID < 50500
5927     if (pthread_mutex_init(&lgtm_tblhnd_share->auto_increment_mutex,
5928       MY_MUTEX_INIT_FAST))
5929 #else
5930     if (mysql_mutex_init(spd_key_mutex_share_auto_increment,
5931       &lgtm_tblhnd_share->auto_increment_mutex, MY_MUTEX_INIT_FAST))
5932 #endif
5933     {
5934       *error_num = HA_ERR_OUT_OF_MEM;
5935       goto error_init_auto_increment_mutex;
5936     }
5937 
5938     uint old_elements = spider_lgtm_tblhnd_share_hash.array.max_element;
5939 #ifdef HASH_UPDATE_WITH_HASH_VALUE
5940     if (my_hash_insert_with_hash_value(&spider_lgtm_tblhnd_share_hash,
5941       hash_value, (uchar*) lgtm_tblhnd_share))
5942 #else
5943     if (my_hash_insert(&spider_lgtm_tblhnd_share_hash,
5944       (uchar*) lgtm_tblhnd_share))
5945 #endif
5946     {
5947       *error_num = HA_ERR_OUT_OF_MEM;
5948       goto error_hash_insert;
5949     }
5950     if (spider_lgtm_tblhnd_share_hash.array.max_element > old_elements)
5951     {
5952       spider_alloc_calc_mem(spider_current_trx,
5953         spider_lgtm_tblhnd_share_hash,
5954         (spider_lgtm_tblhnd_share_hash.array.max_element - old_elements) *
5955         spider_lgtm_tblhnd_share_hash.array.size_of_element);
5956     }
5957   }
5958   if (!locked)
5959     pthread_mutex_unlock(&spider_lgtm_tblhnd_share_mutex);
5960 
5961   DBUG_PRINT("info",("spider lgtm_tblhnd_share=%p", lgtm_tblhnd_share));
5962   DBUG_RETURN(lgtm_tblhnd_share);
5963 
5964 error_hash_insert:
5965   pthread_mutex_destroy(&lgtm_tblhnd_share->auto_increment_mutex);
5966 error_init_auto_increment_mutex:
5967   spider_free(spider_current_trx, lgtm_tblhnd_share, MYF(0));
5968 error_alloc_share:
5969   if (!locked)
5970     pthread_mutex_unlock(&spider_lgtm_tblhnd_share_mutex);
5971   DBUG_RETURN(NULL);
5972 }
5973 
spider_free_lgtm_tblhnd_share_alloc(SPIDER_LGTM_TBLHND_SHARE * lgtm_tblhnd_share,bool locked)5974 void spider_free_lgtm_tblhnd_share_alloc(
5975   SPIDER_LGTM_TBLHND_SHARE *lgtm_tblhnd_share,
5976   bool locked
5977 ) {
5978   DBUG_ENTER("spider_free_lgtm_tblhnd_share");
5979   if (!locked)
5980     pthread_mutex_lock(&spider_lgtm_tblhnd_share_mutex);
5981 #ifdef HASH_UPDATE_WITH_HASH_VALUE
5982   my_hash_delete_with_hash_value(&spider_lgtm_tblhnd_share_hash,
5983     lgtm_tblhnd_share->table_path_hash_value, (uchar*) lgtm_tblhnd_share);
5984 #else
5985   my_hash_delete(&spider_lgtm_tblhnd_share_hash, (uchar*) lgtm_tblhnd_share);
5986 #endif
5987   pthread_mutex_destroy(&lgtm_tblhnd_share->auto_increment_mutex);
5988   spider_free(spider_current_trx, lgtm_tblhnd_share, MYF(0));
5989   if (!locked)
5990     pthread_mutex_unlock(&spider_lgtm_tblhnd_share_mutex);
5991   DBUG_VOID_RETURN;
5992 }
5993 
5994 #ifdef WITH_PARTITION_STORAGE_ENGINE
spider_get_pt_share(SPIDER_SHARE * share,TABLE_SHARE * table_share,int * error_num)5995 SPIDER_PARTITION_SHARE *spider_get_pt_share(
5996   SPIDER_SHARE *share,
5997   TABLE_SHARE *table_share,
5998   int *error_num
5999 ) {
6000   SPIDER_PARTITION_SHARE *partition_share;
6001   char *tmp_name;
6002   longlong *tmp_cardinality;
6003   DBUG_ENTER("spider_get_pt_share");
6004 
6005   pthread_mutex_lock(&spider_pt_share_mutex);
6006 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
6007   if (!(partition_share = (SPIDER_PARTITION_SHARE*)
6008     my_hash_search_using_hash_value(
6009     &spider_open_pt_share, share->table_path_hash_value,
6010     (uchar*) table_share->path.str, table_share->path.length)))
6011 #else
6012   if (!(partition_share = (SPIDER_PARTITION_SHARE*) my_hash_search(
6013     &spider_open_pt_share,
6014     (uchar*) table_share->path.str, table_share->path.length)))
6015 #endif
6016   {
6017     DBUG_PRINT("info",("spider create new pt share"));
6018     if (!(partition_share = (SPIDER_PARTITION_SHARE *)
6019       spider_bulk_malloc(spider_current_trx, 51, MYF(MY_WME | MY_ZEROFILL),
6020         &partition_share, sizeof(*partition_share),
6021         &tmp_name, table_share->path.length + 1,
6022         &tmp_cardinality, sizeof(*tmp_cardinality) * table_share->fields,
6023         NullS))
6024     ) {
6025       *error_num = HA_ERR_OUT_OF_MEM;
6026       goto error_alloc_share;
6027     }
6028 
6029     partition_share->use_count = 0;
6030     partition_share->table_name_length = table_share->path.length;
6031     partition_share->table_name = tmp_name;
6032     memcpy(partition_share->table_name, table_share->path.str,
6033       partition_share->table_name_length);
6034 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
6035     partition_share->table_path_hash_value = share->table_path_hash_value;
6036 #endif
6037     partition_share->cardinality = tmp_cardinality;
6038 
6039     partition_share->crd_get_time = partition_share->sts_get_time =
6040       share->crd_get_time;
6041 
6042 #if MYSQL_VERSION_ID < 50500
6043     if (pthread_mutex_init(&partition_share->sts_mutex, MY_MUTEX_INIT_FAST))
6044 #else
6045     if (mysql_mutex_init(spd_key_mutex_pt_share_sts,
6046       &partition_share->sts_mutex, MY_MUTEX_INIT_FAST))
6047 #endif
6048     {
6049       *error_num = HA_ERR_OUT_OF_MEM;
6050       goto error_init_sts_mutex;
6051     }
6052 
6053 #if MYSQL_VERSION_ID < 50500
6054     if (pthread_mutex_init(&partition_share->crd_mutex, MY_MUTEX_INIT_FAST))
6055 #else
6056     if (mysql_mutex_init(spd_key_mutex_pt_share_crd,
6057       &partition_share->crd_mutex, MY_MUTEX_INIT_FAST))
6058 #endif
6059     {
6060       *error_num = HA_ERR_OUT_OF_MEM;
6061       goto error_init_crd_mutex;
6062     }
6063 
6064 #if MYSQL_VERSION_ID < 50500
6065     if (pthread_mutex_init(&partition_share->pt_handler_mutex,
6066       MY_MUTEX_INIT_FAST))
6067 #else
6068     if (mysql_mutex_init(spd_key_mutex_pt_handler,
6069       &partition_share->pt_handler_mutex, MY_MUTEX_INIT_FAST))
6070 #endif
6071     {
6072       *error_num = HA_ERR_OUT_OF_MEM;
6073       goto error_init_pt_handler_mutex;
6074     }
6075 
6076     if(
6077       my_hash_init(&partition_share->pt_handler_hash, spd_charset_utf8_bin,
6078         32, 0, 0, (my_hash_get_key) spider_pt_handler_share_get_key, 0, 0)
6079     ) {
6080       *error_num = HA_ERR_OUT_OF_MEM;
6081       goto error_init_pt_handler_hash;
6082     }
6083     spider_alloc_calc_mem_init(partition_share->pt_handler_hash, 142);
6084     spider_alloc_calc_mem(spider_current_trx,
6085       partition_share->pt_handler_hash,
6086       partition_share->pt_handler_hash.array.max_element *
6087       partition_share->pt_handler_hash.array.size_of_element);
6088 
6089     uint old_elements = spider_open_pt_share.array.max_element;
6090 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6091     if (my_hash_insert_with_hash_value(&spider_open_pt_share,
6092       share->table_path_hash_value,
6093       (uchar*) partition_share))
6094 #else
6095     if (my_hash_insert(&spider_open_pt_share, (uchar*) partition_share))
6096 #endif
6097     {
6098       *error_num = HA_ERR_OUT_OF_MEM;
6099       goto error_hash_insert;
6100     }
6101     if (spider_open_pt_share.array.max_element > old_elements)
6102     {
6103       spider_alloc_calc_mem(spider_current_trx,
6104         spider_open_pt_share,
6105         (spider_open_pt_share.array.max_element - old_elements) *
6106         spider_open_pt_share.array.size_of_element);
6107     }
6108   }
6109   partition_share->use_count++;
6110   pthread_mutex_unlock(&spider_pt_share_mutex);
6111 
6112   DBUG_PRINT("info",("spider partition_share=%p", partition_share));
6113   DBUG_RETURN(partition_share);
6114 
6115 error_hash_insert:
6116   spider_free_mem_calc(spider_current_trx,
6117     partition_share->pt_handler_hash_id,
6118     partition_share->pt_handler_hash.array.max_element *
6119     partition_share->pt_handler_hash.array.size_of_element);
6120   my_hash_free(&partition_share->pt_handler_hash);
6121 error_init_pt_handler_hash:
6122   pthread_mutex_destroy(&partition_share->pt_handler_mutex);
6123 error_init_pt_handler_mutex:
6124   pthread_mutex_destroy(&partition_share->crd_mutex);
6125 error_init_crd_mutex:
6126   pthread_mutex_destroy(&partition_share->sts_mutex);
6127 error_init_sts_mutex:
6128   spider_free(spider_current_trx, partition_share, MYF(0));
6129 error_alloc_share:
6130   pthread_mutex_unlock(&spider_pt_share_mutex);
6131   DBUG_RETURN(NULL);
6132 }
6133 
spider_free_pt_share(SPIDER_PARTITION_SHARE * partition_share)6134 int spider_free_pt_share(
6135   SPIDER_PARTITION_SHARE *partition_share
6136 ) {
6137   DBUG_ENTER("spider_free_pt_share");
6138   pthread_mutex_lock(&spider_pt_share_mutex);
6139   if (!--partition_share->use_count)
6140   {
6141 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6142     my_hash_delete_with_hash_value(&spider_open_pt_share,
6143       partition_share->table_path_hash_value, (uchar*) partition_share);
6144 #else
6145     my_hash_delete(&spider_open_pt_share, (uchar*) partition_share);
6146 #endif
6147     spider_free_mem_calc(spider_current_trx,
6148       partition_share->pt_handler_hash_id,
6149       partition_share->pt_handler_hash.array.max_element *
6150       partition_share->pt_handler_hash.array.size_of_element);
6151     my_hash_free(&partition_share->pt_handler_hash);
6152     pthread_mutex_destroy(&partition_share->pt_handler_mutex);
6153     pthread_mutex_destroy(&partition_share->crd_mutex);
6154     pthread_mutex_destroy(&partition_share->sts_mutex);
6155     spider_free(spider_current_trx, partition_share, MYF(0));
6156   }
6157   pthread_mutex_unlock(&spider_pt_share_mutex);
6158   DBUG_RETURN(0);
6159 }
6160 
spider_copy_sts_to_pt_share(SPIDER_PARTITION_SHARE * partition_share,SPIDER_SHARE * share)6161 void spider_copy_sts_to_pt_share(
6162   SPIDER_PARTITION_SHARE *partition_share,
6163   SPIDER_SHARE *share
6164 ) {
6165   DBUG_ENTER("spider_copy_sts_to_pt_share");
6166   memcpy(&partition_share->data_file_length, &share->data_file_length,
6167     sizeof(ulonglong) * 4 + sizeof(ha_rows) +
6168     sizeof(ulong) + sizeof(time_t) * 3);
6169 /*
6170   partition_share->data_file_length = share->data_file_length;
6171   partition_share->max_data_file_length = share->max_data_file_length;
6172   partition_share->index_file_length = share->index_file_length;
6173   partition_share->auto_increment_value =
6174     share->lgtm_tblhnd_share->auto_increment_value;
6175   partition_share->records = share->records;
6176   partition_share->mean_rec_length = share->mean_rec_length;
6177   partition_share->check_time = share->check_time;
6178   partition_share->create_time = share->create_time;
6179   partition_share->update_time = share->update_time;
6180 */
6181   DBUG_VOID_RETURN;
6182 }
6183 
spider_copy_sts_to_share(SPIDER_SHARE * share,SPIDER_PARTITION_SHARE * partition_share)6184 void spider_copy_sts_to_share(
6185   SPIDER_SHARE *share,
6186   SPIDER_PARTITION_SHARE *partition_share
6187 ) {
6188   DBUG_ENTER("spider_copy_sts_to_share");
6189   memcpy(&share->data_file_length, &partition_share->data_file_length,
6190     sizeof(ulonglong) * 4 + sizeof(ha_rows) +
6191     sizeof(ulong) + sizeof(time_t) * 3);
6192 /*
6193   share->data_file_length = partition_share->data_file_length;
6194   share->max_data_file_length = partition_share->max_data_file_length;
6195   share->index_file_length = partition_share->index_file_length;
6196   share->lgtm_tblhnd_share->auto_increment_value =
6197     partition_share->auto_increment_value;
6198   DBUG_PRINT("info",("spider auto_increment_value=%llu",
6199     share->lgtm_tblhnd_share->auto_increment_value));
6200   share->records = partition_share->records;
6201   share->mean_rec_length = partition_share->mean_rec_length;
6202   share->check_time = partition_share->check_time;
6203   share->create_time = partition_share->create_time;
6204   share->update_time = partition_share->update_time;
6205 */
6206   DBUG_VOID_RETURN;
6207 }
6208 
spider_copy_crd_to_pt_share(SPIDER_PARTITION_SHARE * partition_share,SPIDER_SHARE * share,int fields)6209 void spider_copy_crd_to_pt_share(
6210   SPIDER_PARTITION_SHARE *partition_share,
6211   SPIDER_SHARE *share,
6212   int fields
6213 ) {
6214   DBUG_ENTER("spider_copy_crd_to_pt_share");
6215   memcpy(partition_share->cardinality, share->cardinality,
6216     sizeof(longlong) * fields);
6217   DBUG_VOID_RETURN;
6218 }
6219 
spider_copy_crd_to_share(SPIDER_SHARE * share,SPIDER_PARTITION_SHARE * partition_share,int fields)6220 void spider_copy_crd_to_share(
6221   SPIDER_SHARE *share,
6222   SPIDER_PARTITION_SHARE *partition_share,
6223   int fields
6224 ) {
6225   DBUG_ENTER("spider_copy_crd_to_share");
6226   memcpy(share->cardinality, partition_share->cardinality,
6227     sizeof(longlong) * fields);
6228   DBUG_VOID_RETURN;
6229 }
6230 #endif
6231 
spider_open_all_tables(SPIDER_TRX * trx,bool lock)6232 int spider_open_all_tables(
6233   SPIDER_TRX *trx,
6234   bool lock
6235 ) {
6236   THD *thd = trx->thd;
6237   TABLE *table_tables;
6238   int error_num, *need_mon, mon_val;
6239   SPIDER_SHARE tmp_share;
6240   char *db_name, *table_name;
6241   uint db_name_length, table_name_length;
6242   char *tmp_connect_info[SPIDER_TMP_SHARE_CHAR_PTR_COUNT];
6243   uint tmp_connect_info_length[SPIDER_TMP_SHARE_UINT_COUNT];
6244   long tmp_long[SPIDER_TMP_SHARE_LONG_COUNT];
6245   longlong tmp_longlong[SPIDER_TMP_SHARE_LONGLONG_COUNT];
6246   SPIDER_CONN *conn, **conns;
6247   ha_spider *spider;
6248   SPIDER_SHARE *share;
6249   char **connect_info;
6250   uint *connect_info_length;
6251   long *long_info;
6252   longlong *longlong_info;
6253   MEM_ROOT mem_root;
6254 #if MYSQL_VERSION_ID < 50500
6255   Open_tables_state open_tables_backup;
6256 #else
6257   Open_tables_backup open_tables_backup;
6258 #endif
6259   DBUG_ENTER("spider_open_all_tables");
6260   if (
6261     !(table_tables = spider_open_sys_table(
6262       thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
6263       SPIDER_SYS_TABLES_TABLE_NAME_LEN, TRUE, &open_tables_backup, TRUE,
6264       &error_num))
6265   )
6266     DBUG_RETURN(error_num);
6267   if (
6268     (error_num = spider_sys_index_first(table_tables, 1))
6269   ) {
6270     if (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
6271     {
6272       table_tables->file->print_error(error_num, MYF(0));
6273       spider_close_sys_table(thd, table_tables,
6274         &open_tables_backup, TRUE);
6275       DBUG_RETURN(error_num);
6276     } else {
6277       spider_close_sys_table(thd, table_tables,
6278         &open_tables_backup, TRUE);
6279       DBUG_RETURN(0);
6280     }
6281   }
6282 
6283   SPD_INIT_ALLOC_ROOT(&mem_root, 4096, 0, MYF(MY_WME));
6284   memset((void*)&tmp_share, 0, sizeof(SPIDER_SHARE));
6285   memset(&tmp_connect_info, 0,
6286     sizeof(char *) * SPIDER_TMP_SHARE_CHAR_PTR_COUNT);
6287   memset(tmp_connect_info_length, 0,
6288     sizeof(uint) * SPIDER_TMP_SHARE_UINT_COUNT);
6289   memset(tmp_long, 0, sizeof(long) * SPIDER_TMP_SHARE_LONG_COUNT);
6290   memset(tmp_longlong, 0, sizeof(longlong) * SPIDER_TMP_SHARE_LONGLONG_COUNT);
6291   spider_set_tmp_share_pointer(&tmp_share, (char **) &tmp_connect_info,
6292     tmp_connect_info_length, tmp_long, tmp_longlong);
6293   tmp_share.link_statuses[0] = -1;
6294 
6295   do {
6296     if (
6297       (error_num = spider_get_sys_tables(
6298         table_tables, &db_name, &table_name, &mem_root)) ||
6299       (error_num = spider_get_sys_tables_connect_info(
6300         table_tables, &tmp_share, 0, &mem_root)) ||
6301       (error_num = spider_set_connect_info_default(
6302         &tmp_share,
6303 #ifdef WITH_PARTITION_STORAGE_ENGINE
6304         NULL,
6305         NULL,
6306 #endif
6307         NULL
6308       ))
6309     ) {
6310       spider_sys_index_end(table_tables);
6311       spider_close_sys_table(thd, table_tables,
6312         &open_tables_backup, TRUE);
6313       spider_free_tmp_share_alloc(&tmp_share);
6314       free_root(&mem_root, MYF(0));
6315       DBUG_RETURN(error_num);
6316     }
6317     db_name_length = strlen(db_name);
6318     table_name_length = strlen(table_name);
6319 
6320     if (
6321       (error_num = spider_set_connect_info_default_db_table(
6322         &tmp_share,
6323         db_name,
6324         db_name_length,
6325         table_name,
6326         table_name_length
6327       )) ||
6328       (error_num = spider_create_conn_keys(&tmp_share)) ||
6329 /*
6330       (error_num = spider_db_create_table_names_str(&tmp_share)) ||
6331 */
6332       (error_num = spider_create_tmp_dbton_share(&tmp_share))
6333     ) {
6334       spider_sys_index_end(table_tables);
6335       spider_close_sys_table(thd, table_tables,
6336         &open_tables_backup, TRUE);
6337       spider_free_tmp_share_alloc(&tmp_share);
6338       free_root(&mem_root, MYF(0));
6339       DBUG_RETURN(error_num);
6340     }
6341 
6342     /* create conn */
6343     if (
6344       !(conn = spider_get_conn(
6345         &tmp_share, 0, tmp_share.conn_keys[0], trx, NULL, FALSE, FALSE,
6346         SPIDER_CONN_KIND_MYSQL, &error_num))
6347     ) {
6348       spider_sys_index_end(table_tables);
6349       spider_close_sys_table(thd, table_tables,
6350         &open_tables_backup, TRUE);
6351       spider_free_tmp_dbton_share(&tmp_share);
6352       spider_free_tmp_share_alloc(&tmp_share);
6353       free_root(&mem_root, MYF(0));
6354       DBUG_RETURN(error_num);
6355     }
6356     conn->error_mode &= spider_param_error_read_mode(thd, 0);
6357     conn->error_mode &= spider_param_error_write_mode(thd, 0);
6358     pthread_mutex_assert_not_owner(&conn->mta_conn_mutex);
6359     pthread_mutex_lock(&conn->mta_conn_mutex);
6360     SPIDER_SET_FILE_POS(&conn->mta_conn_mutex_file_pos);
6361     conn->need_mon = &mon_val;
6362     DBUG_ASSERT(!conn->mta_conn_mutex_lock_already);
6363     DBUG_ASSERT(!conn->mta_conn_mutex_unlock_later);
6364     conn->mta_conn_mutex_lock_already = TRUE;
6365     conn->mta_conn_mutex_unlock_later = TRUE;
6366     if ((error_num = spider_db_before_query(conn, &mon_val)))
6367     {
6368       DBUG_ASSERT(conn->mta_conn_mutex_lock_already);
6369       DBUG_ASSERT(conn->mta_conn_mutex_unlock_later);
6370       conn->mta_conn_mutex_lock_already = FALSE;
6371       conn->mta_conn_mutex_unlock_later = FALSE;
6372       SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6373       pthread_mutex_unlock(&conn->mta_conn_mutex);
6374       spider_sys_index_end(table_tables);
6375       spider_close_sys_table(thd, table_tables,
6376         &open_tables_backup, TRUE);
6377       spider_free_tmp_dbton_share(&tmp_share);
6378       spider_free_tmp_share_alloc(&tmp_share);
6379       free_root(&mem_root, MYF(0));
6380       DBUG_RETURN(error_num);
6381     }
6382     DBUG_ASSERT(conn->mta_conn_mutex_lock_already);
6383     DBUG_ASSERT(conn->mta_conn_mutex_unlock_later);
6384     conn->mta_conn_mutex_lock_already = FALSE;
6385     conn->mta_conn_mutex_unlock_later = FALSE;
6386     SPIDER_CLEAR_FILE_POS(&conn->mta_conn_mutex_file_pos);
6387     pthread_mutex_unlock(&conn->mta_conn_mutex);
6388 
6389     if (lock && spider_param_use_snapshot_with_flush_tables(thd) == 2)
6390     {
6391       if (!(spider = new ha_spider()))
6392       {
6393         spider_sys_index_end(table_tables);
6394         spider_close_sys_table(thd, table_tables,
6395           &open_tables_backup, TRUE);
6396         spider_free_tmp_dbton_share(&tmp_share);
6397         spider_free_tmp_share_alloc(&tmp_share);
6398         free_root(&mem_root, MYF(0));
6399         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6400       }
6401       spider->lock_type = TL_READ_NO_INSERT;
6402 
6403       if (!(share = (SPIDER_SHARE *)
6404         spider_bulk_malloc(spider_current_trx, 52, MYF(MY_WME | MY_ZEROFILL),
6405           &share, sizeof(*share),
6406           &connect_info, sizeof(char *) * SPIDER_TMP_SHARE_CHAR_PTR_COUNT,
6407           &connect_info_length, sizeof(uint) * SPIDER_TMP_SHARE_UINT_COUNT,
6408           &long_info, sizeof(long) * SPIDER_TMP_SHARE_LONG_COUNT,
6409           &longlong_info, sizeof(longlong) * SPIDER_TMP_SHARE_LONGLONG_COUNT,
6410           &conns, sizeof(SPIDER_CONN *),
6411           &need_mon, sizeof(int),
6412           &spider->conn_link_idx, sizeof(uint),
6413           &spider->conn_can_fo, sizeof(uchar),
6414           NullS))
6415       ) {
6416         delete spider;
6417         spider_sys_index_end(table_tables);
6418         spider_close_sys_table(thd, table_tables,
6419           &open_tables_backup, TRUE);
6420         spider_free_tmp_dbton_share(&tmp_share);
6421         spider_free_tmp_share_alloc(&tmp_share);
6422         free_root(&mem_root, MYF(0));
6423         DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6424       }
6425       memcpy((void*)share, &tmp_share, sizeof(*share));
6426       spider_set_tmp_share_pointer(share, connect_info,
6427         connect_info_length, long_info, longlong_info);
6428       memcpy(connect_info, &tmp_connect_info, sizeof(char *) *
6429         SPIDER_TMP_SHARE_CHAR_PTR_COUNT);
6430       memcpy(connect_info_length, &tmp_connect_info_length, sizeof(uint) *
6431         SPIDER_TMP_SHARE_UINT_COUNT);
6432       memcpy(long_info, &tmp_long, sizeof(long) * SPIDER_TMP_SHARE_LONG_COUNT);
6433       memcpy(longlong_info, &tmp_longlong, sizeof(longlong) *
6434         SPIDER_TMP_SHARE_LONGLONG_COUNT);
6435       spider->share = share;
6436       spider->trx = trx;
6437       spider->conns = conns;
6438       spider->need_mons = need_mon;
6439       spider->conn_link_idx[0] = 0;
6440       spider->conn_can_fo[0] = 0;
6441       if ((error_num = spider_create_tmp_dbton_handler(spider)))
6442       {
6443         spider_free(trx, share, MYF(0));
6444         delete spider;
6445         spider_sys_index_end(table_tables);
6446         spider_close_sys_table(thd, table_tables,
6447           &open_tables_backup, TRUE);
6448         spider_free_tmp_dbton_share(&tmp_share);
6449         spider_free_tmp_share_alloc(&tmp_share);
6450         free_root(&mem_root, MYF(0));
6451         DBUG_RETURN(error_num);
6452       }
6453 
6454       /* create another conn */
6455       if (
6456         (!(conn = spider_get_conn(
6457         &tmp_share, 0, tmp_share.conn_keys[0], trx, spider, TRUE, FALSE,
6458         SPIDER_CONN_KIND_MYSQL, &error_num)))
6459       ) {
6460         spider_free_tmp_dbton_handler(spider);
6461         spider_free(trx, share, MYF(0));
6462         delete spider;
6463         spider_sys_index_end(table_tables);
6464         spider_close_sys_table(thd, table_tables,
6465           &open_tables_backup, TRUE);
6466         spider_free_tmp_dbton_share(&tmp_share);
6467         spider_free_tmp_share_alloc(&tmp_share);
6468         free_root(&mem_root, MYF(0));
6469         DBUG_RETURN(error_num);
6470       }
6471       conn->error_mode &= spider_param_error_read_mode(thd, 0);
6472       conn->error_mode &= spider_param_error_write_mode(thd, 0);
6473 
6474       spider->next = NULL;
6475       if (conn->another_ha_last)
6476       {
6477         ((ha_spider*) conn->another_ha_last)->next = spider;
6478       } else {
6479         conn->another_ha_first = (void*) spider;
6480       }
6481       conn->another_ha_last = (void*) spider;
6482 
6483       int appended = 0;
6484       if ((error_num = spider->dbton_handler[conn->dbton_id]->
6485         append_lock_tables_list(conn, 0, &appended)))
6486       {
6487         spider_free_tmp_dbton_handler(spider);
6488         spider_free(trx, share, MYF(0));
6489         delete spider;
6490         spider_sys_index_end(table_tables);
6491         spider_close_sys_table(thd, table_tables,
6492           &open_tables_backup, TRUE);
6493         spider_free_tmp_dbton_share(&tmp_share);
6494         spider_free_tmp_share_alloc(&tmp_share);
6495         free_root(&mem_root, MYF(0));
6496         DBUG_RETURN(error_num);
6497       }
6498     } else {
6499       spider_free_tmp_dbton_share(&tmp_share);
6500       spider_free_tmp_share_alloc(&tmp_share);
6501     }
6502     error_num = spider_sys_index_next(table_tables);
6503   } while (error_num == 0);
6504   free_root(&mem_root, MYF(0));
6505 
6506   spider_sys_index_end(table_tables);
6507   spider_close_sys_table(thd, table_tables,
6508     &open_tables_backup, TRUE);
6509   DBUG_RETURN(0);
6510 }
6511 
spider_flush_logs(handlerton * hton)6512 bool spider_flush_logs(
6513   handlerton *hton
6514 ) {
6515   int error_num;
6516   THD* thd = current_thd;
6517   SPIDER_TRX *trx;
6518   DBUG_ENTER("spider_flush_logs");
6519 
6520   if (!(trx = spider_get_trx(thd, TRUE, &error_num)))
6521   {
6522     my_errno = error_num;
6523     DBUG_RETURN(TRUE);
6524   }
6525   if (
6526     spider_param_use_flash_logs(trx->thd) &&
6527     (
6528       !trx->trx_consistent_snapshot ||
6529       !spider_param_use_all_conns_snapshot(trx->thd) ||
6530       !spider_param_use_snapshot_with_flush_tables(trx->thd)
6531     )
6532   ) {
6533     if (
6534       (error_num = spider_open_all_tables(trx, FALSE)) ||
6535       (error_num = spider_trx_all_flush_logs(trx))
6536     ) {
6537       my_errno = error_num;
6538       DBUG_RETURN(TRUE);
6539     }
6540   }
6541 
6542   DBUG_RETURN(FALSE);
6543 }
6544 
spider_create_handler(handlerton * hton,TABLE_SHARE * table,MEM_ROOT * mem_root)6545 handler* spider_create_handler(
6546   handlerton *hton,
6547   TABLE_SHARE *table,
6548   MEM_ROOT *mem_root
6549 ) {
6550   DBUG_ENTER("spider_create_handler");
6551   DBUG_RETURN(new (mem_root) ha_spider(hton, table));
6552 }
6553 
spider_close_connection(handlerton * hton,THD * thd)6554 int spider_close_connection(
6555   handlerton* hton,
6556   THD* thd
6557 ) {
6558   int roop_count = 0;
6559   SPIDER_CONN *conn;
6560   SPIDER_TRX *trx;
6561   DBUG_ENTER("spider_close_connection");
6562   if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)))
6563     DBUG_RETURN(0); /* transaction is not started */
6564 
6565   trx->tmp_spider->conns = &conn;
6566   while ((conn = (SPIDER_CONN*) my_hash_element(&trx->trx_conn_hash,
6567     roop_count)))
6568   {
6569     SPIDER_BACKUP_DASTATUS;
6570     DBUG_PRINT("info",("spider conn->table_lock=%d", conn->table_lock));
6571     if (conn->table_lock > 0)
6572     {
6573       if (!conn->trx_start)
6574         conn->disable_reconnect = FALSE;
6575       if (conn->table_lock != 2)
6576       {
6577         spider_db_unlock_tables(trx->tmp_spider, 0);
6578       }
6579       conn->table_lock = 0;
6580     }
6581     roop_count++;
6582     SPIDER_CONN_RESTORE_DASTATUS;
6583   }
6584 
6585   spider_rollback(spider_hton_ptr, thd, TRUE);
6586   spider_free_trx(trx, TRUE);
6587 
6588   DBUG_RETURN(0);
6589 }
6590 
spider_drop_database(handlerton * hton,char * path)6591 void spider_drop_database(
6592   handlerton *hton,
6593   char* path
6594 ) {
6595   DBUG_ENTER("spider_drop_database");
6596   DBUG_VOID_RETURN;
6597 }
6598 
spider_show_status(handlerton * hton,THD * thd,stat_print_fn * stat_print,enum ha_stat_type stat_type)6599 bool spider_show_status(
6600   handlerton *hton,
6601   THD *thd,
6602   stat_print_fn *stat_print,
6603   enum ha_stat_type stat_type
6604 ) {
6605   DBUG_ENTER("spider_show_status");
6606   switch (stat_type) {
6607     case HA_ENGINE_STATUS:
6608     default:
6609       DBUG_RETURN(FALSE);
6610   }
6611 }
6612 
spider_db_done(void * p)6613 int spider_db_done(
6614   void *p
6615 ) {
6616   int roop_count;
6617   bool do_delete_thd;
6618   THD *thd = current_thd, *tmp_thd;
6619   SPIDER_CONN *conn;
6620   SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
6621   SPIDER_TABLE_MON_LIST *table_mon_list;
6622   SPIDER_LGTM_TBLHND_SHARE *lgtm_tblhnd_share;
6623   DBUG_ENTER("spider_db_done");
6624 
6625   /* Begin Spider plugin deinit */
6626   if (thd)
6627     do_delete_thd = FALSE;
6628   else
6629   {
6630     /* Create a thread for Spider plugin deinit */
6631     thd = spider_create_thd();
6632     if (!thd)
6633       DBUG_RETURN(HA_ERR_OUT_OF_MEM);
6634     do_delete_thd = TRUE;
6635   }
6636 
6637   for (roop_count = SPIDER_DBTON_SIZE - 1; roop_count >= 0; roop_count--)
6638   {
6639     if (spider_dbton[roop_count].deinit)
6640     {
6641       spider_dbton[roop_count].deinit();
6642     }
6643   }
6644 
6645 #ifndef WITHOUT_SPIDER_BG_SEARCH
6646   for (roop_count = spider_param_table_crd_thread_count() - 1;
6647     roop_count >= 0; roop_count--)
6648   {
6649     spider_free_crd_threads(&spider_table_crd_threads[roop_count]);
6650   }
6651   for (roop_count = spider_param_table_sts_thread_count() - 1;
6652     roop_count >= 0; roop_count--)
6653   {
6654     spider_free_sts_threads(&spider_table_sts_threads[roop_count]);
6655   }
6656   spider_free(NULL, spider_table_sts_threads, MYF(0));
6657 #endif
6658 
6659   for (roop_count = spider_param_udf_table_mon_mutex_count() - 1;
6660     roop_count >= 0; roop_count--)
6661   {
6662     while ((table_mon_list = (SPIDER_TABLE_MON_LIST *) my_hash_element(
6663       &spider_udf_table_mon_list_hash[roop_count], 0)))
6664     {
6665 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6666       my_hash_delete_with_hash_value(
6667         &spider_udf_table_mon_list_hash[roop_count],
6668         table_mon_list->key_hash_value, (uchar*) table_mon_list);
6669 #else
6670       my_hash_delete(&spider_udf_table_mon_list_hash[roop_count],
6671         (uchar*) table_mon_list);
6672 #endif
6673       spider_ping_table_free_mon_list(table_mon_list);
6674     }
6675     spider_free_mem_calc(spider_current_trx,
6676       spider_udf_table_mon_list_hash_id,
6677       spider_udf_table_mon_list_hash[roop_count].array.max_element *
6678       spider_udf_table_mon_list_hash[roop_count].array.size_of_element);
6679     my_hash_free(&spider_udf_table_mon_list_hash[roop_count]);
6680   }
6681   for (roop_count = spider_param_udf_table_mon_mutex_count() - 1;
6682     roop_count >= 0; roop_count--)
6683     pthread_cond_destroy(&spider_udf_table_mon_conds[roop_count]);
6684   for (roop_count = spider_param_udf_table_mon_mutex_count() - 1;
6685     roop_count >= 0; roop_count--)
6686     pthread_mutex_destroy(&spider_udf_table_mon_mutexes[roop_count]);
6687   spider_free(NULL, spider_udf_table_mon_mutexes, MYF(0));
6688 
6689   pthread_mutex_lock(&spider_allocated_thds_mutex);
6690   while ((tmp_thd = (THD *) my_hash_element(&spider_allocated_thds, 0)))
6691   {
6692     SPIDER_TRX *trx = (SPIDER_TRX *)
6693                       thd_get_ha_data(tmp_thd, spider_hton_ptr);
6694     if (trx)
6695     {
6696       DBUG_ASSERT(tmp_thd == trx->thd);
6697       spider_free_trx(trx, FALSE);
6698       thd_set_ha_data(tmp_thd, spider_hton_ptr, NULL);
6699     }
6700     else
6701       my_hash_delete(&spider_allocated_thds, (uchar *) tmp_thd);
6702   }
6703   pthread_mutex_unlock(&spider_allocated_thds_mutex);
6704 
6705 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6706   pthread_mutex_lock(&spider_hs_w_conn_mutex);
6707   while ((conn = (SPIDER_CONN*) my_hash_element(&spider_hs_w_conn_hash, 0)))
6708   {
6709 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6710     my_hash_delete_with_hash_value(&spider_hs_w_conn_hash,
6711       conn->conn_key_hash_value, (uchar*) conn);
6712 #else
6713     my_hash_delete(&spider_hs_w_conn_hash, (uchar*) conn);
6714 #endif
6715     spider_free_conn(conn);
6716   }
6717   pthread_mutex_unlock(&spider_hs_w_conn_mutex);
6718   pthread_mutex_lock(&spider_hs_r_conn_mutex);
6719   while ((conn = (SPIDER_CONN*) my_hash_element(&spider_hs_r_conn_hash, 0)))
6720   {
6721 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6722     my_hash_delete_with_hash_value(&spider_hs_r_conn_hash,
6723       conn->conn_key_hash_value, (uchar*) conn);
6724 #else
6725     my_hash_delete(&spider_hs_r_conn_hash, (uchar*) conn);
6726 #endif
6727     spider_free_conn(conn);
6728   }
6729   pthread_mutex_unlock(&spider_hs_r_conn_mutex);
6730 #endif
6731   pthread_mutex_lock(&spider_conn_mutex);
6732   while ((conn = (SPIDER_CONN*) my_hash_element(&spider_open_connections, 0)))
6733   {
6734 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6735     my_hash_delete_with_hash_value(&spider_open_connections,
6736       conn->conn_key_hash_value, (uchar*) conn);
6737 #else
6738     my_hash_delete(&spider_open_connections, (uchar*) conn);
6739 #endif
6740     spider_free_conn(conn);
6741   }
6742   pthread_mutex_unlock(&spider_conn_mutex);
6743   pthread_mutex_lock(&spider_lgtm_tblhnd_share_mutex);
6744   while ((lgtm_tblhnd_share = (SPIDER_LGTM_TBLHND_SHARE*) my_hash_element(
6745     &spider_lgtm_tblhnd_share_hash, 0)))
6746   {
6747     spider_free_lgtm_tblhnd_share_alloc(lgtm_tblhnd_share, TRUE);
6748   }
6749   pthread_mutex_unlock(&spider_lgtm_tblhnd_share_mutex);
6750   spider_free_mem_calc(spider_current_trx,
6751     spider_mon_table_cache_id,
6752     spider_mon_table_cache.max_element *
6753     spider_mon_table_cache.size_of_element);
6754   delete_dynamic(&spider_mon_table_cache);
6755   spider_free_mem_calc(spider_current_trx,
6756     spider_allocated_thds_id,
6757     spider_allocated_thds.array.max_element *
6758     spider_allocated_thds.array.size_of_element);
6759   my_hash_free(&spider_allocated_thds);
6760 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6761   spider_free_mem_calc(spider_current_trx,
6762     spider_hs_w_conn_hash_id,
6763     spider_hs_w_conn_hash.array.max_element *
6764     spider_hs_w_conn_hash.array.size_of_element);
6765   my_hash_free(&spider_hs_w_conn_hash);
6766   spider_free_mem_calc(spider_current_trx,
6767     spider_hs_r_conn_hash_id,
6768     spider_hs_r_conn_hash.array.max_element *
6769     spider_hs_r_conn_hash.array.size_of_element);
6770   my_hash_free(&spider_hs_r_conn_hash);
6771 #endif
6772   spider_free_mem_calc(spider_current_trx,
6773     spider_open_connections_id,
6774     spider_open_connections.array.max_element *
6775     spider_open_connections.array.size_of_element);
6776   my_hash_free(&spider_open_connections);
6777   my_hash_free(&spider_ipport_conns);
6778   spider_free_mem_calc(spider_current_trx,
6779     spider_lgtm_tblhnd_share_hash_id,
6780     spider_lgtm_tblhnd_share_hash.array.max_element *
6781     spider_lgtm_tblhnd_share_hash.array.size_of_element);
6782   my_hash_free(&spider_lgtm_tblhnd_share_hash);
6783 #ifdef WITH_PARTITION_STORAGE_ENGINE
6784   spider_free_mem_calc(spider_current_trx,
6785     spider_open_pt_share_id,
6786     spider_open_pt_share.array.max_element *
6787     spider_open_pt_share.array.size_of_element);
6788   my_hash_free(&spider_open_pt_share);
6789 #endif
6790   pthread_mutex_lock(&spider_init_error_tbl_mutex);
6791   while ((spider_init_error_table = (SPIDER_INIT_ERROR_TABLE*)
6792     my_hash_element(&spider_init_error_tables, 0)))
6793   {
6794 #ifdef HASH_UPDATE_WITH_HASH_VALUE
6795     my_hash_delete_with_hash_value(&spider_init_error_tables,
6796       spider_init_error_table->table_name_hash_value,
6797       (uchar*) spider_init_error_table);
6798 #else
6799     my_hash_delete(&spider_init_error_tables,
6800       (uchar*) spider_init_error_table);
6801 #endif
6802     spider_free(NULL, spider_init_error_table, MYF(0));
6803   }
6804   pthread_mutex_unlock(&spider_init_error_tbl_mutex);
6805   spider_free_mem_calc(spider_current_trx,
6806     spider_init_error_tables_id,
6807     spider_init_error_tables.array.max_element *
6808     spider_init_error_tables.array.size_of_element);
6809   my_hash_free(&spider_init_error_tables);
6810   spider_free_mem_calc(spider_current_trx,
6811     spider_open_tables_id,
6812     spider_open_tables.array.max_element *
6813     spider_open_tables.array.size_of_element);
6814   my_hash_free(&spider_open_tables);
6815   pthread_mutex_destroy(&spider_mem_calc_mutex);
6816   pthread_mutex_destroy(&spider_mon_table_cache_mutex);
6817   pthread_mutex_destroy(&spider_allocated_thds_mutex);
6818   pthread_mutex_destroy(&spider_open_conn_mutex);
6819 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
6820   pthread_mutex_destroy(&spider_hs_w_conn_mutex);
6821   pthread_mutex_destroy(&spider_hs_r_conn_mutex);
6822 #endif
6823   pthread_mutex_destroy(&spider_conn_mutex);
6824   pthread_mutex_destroy(&spider_lgtm_tblhnd_share_mutex);
6825 #ifdef WITH_PARTITION_STORAGE_ENGINE
6826   pthread_mutex_destroy(&spider_pt_share_mutex);
6827 #endif
6828   pthread_mutex_destroy(&spider_init_error_tbl_mutex);
6829   pthread_mutex_destroy(&spider_conn_id_mutex);
6830   pthread_mutex_destroy(&spider_ipport_conn_mutex);
6831   pthread_mutex_destroy(&spider_thread_id_mutex);
6832   pthread_mutex_destroy(&spider_tbl_mutex);
6833 #ifndef WITHOUT_SPIDER_BG_SEARCH
6834   pthread_attr_destroy(&spider_pt_attr);
6835 #endif
6836 
6837   for (roop_count = 0; roop_count < SPIDER_MEM_CALC_LIST_NUM; roop_count++)
6838   {
6839     if (spider_alloc_func_name[roop_count])
6840       DBUG_PRINT("info",("spider %d %s %s %lu %llu %lld %llu %llu %s",
6841         roop_count,
6842         spider_alloc_func_name[roop_count],
6843         spider_alloc_file_name[roop_count],
6844         spider_alloc_line_no[roop_count],
6845         spider_total_alloc_mem[roop_count],
6846         spider_current_alloc_mem[roop_count],
6847         spider_alloc_mem_count[roop_count],
6848         spider_free_mem_count[roop_count],
6849         spider_current_alloc_mem[roop_count] ? "NG" : "OK"
6850       ));
6851   }
6852 
6853   /* End Spider plugin deinit */
6854   if (do_delete_thd)
6855     spider_destroy_thd(thd);
6856 
6857 /*
6858 DBUG_ASSERT(0);
6859 */
6860   DBUG_RETURN(0);
6861 }
6862 
spider_panic(handlerton * hton,ha_panic_function type)6863 int spider_panic(
6864   handlerton *hton,
6865   ha_panic_function type
6866 ) {
6867   DBUG_ENTER("spider_panic");
6868   DBUG_RETURN(0);
6869 }
6870 
spider_db_init(void * p)6871 int spider_db_init(
6872   void *p
6873 ) {
6874   int error_num = HA_ERR_OUT_OF_MEM, roop_count;
6875   uint dbton_id = 0;
6876   handlerton *spider_hton = (handlerton *)p;
6877   DBUG_ENTER("spider_db_init");
6878   spider_hton_ptr = spider_hton;
6879 
6880   spider_hton->state = SHOW_OPTION_YES;
6881   spider_hton->flags = HTON_NO_FLAGS;
6882 #ifdef HTON_CAN_READ_CONNECT_STRING_IN_PARTITION
6883   spider_hton->flags |= HTON_CAN_READ_CONNECT_STRING_IN_PARTITION;
6884 #endif
6885   /* spider_hton->db_type = DB_TYPE_SPIDER; */
6886   /*
6887   spider_hton->savepoint_offset;
6888   spider_hton->savepoint_set = spider_savepoint_set;
6889   spider_hton->savepoint_rollback = spider_savepoint_rollback;
6890   spider_hton->savepoint_release = spider_savepoint_release;
6891   spider_hton->create_cursor_read_view = spider_create_cursor_read_view;
6892   spider_hton->set_cursor_read_view = spider_set_cursor_read_view;
6893   spider_hton->close_cursor_read_view = spider_close_cursor_read_view;
6894   */
6895   spider_hton->panic = spider_panic;
6896   spider_hton->close_connection = spider_close_connection;
6897   spider_hton->start_consistent_snapshot = spider_start_consistent_snapshot;
6898   spider_hton->flush_logs = spider_flush_logs;
6899   spider_hton->commit = spider_commit;
6900   spider_hton->rollback = spider_rollback;
6901 #ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
6902   spider_hton->discover_table_structure = spider_discover_table_structure;
6903 #endif
6904   if (spider_param_support_xa())
6905   {
6906     spider_hton->prepare = spider_xa_prepare;
6907     spider_hton->recover = spider_xa_recover;
6908     spider_hton->commit_by_xid = spider_xa_commit_by_xid;
6909     spider_hton->rollback_by_xid = spider_xa_rollback_by_xid;
6910   }
6911   spider_hton->create = spider_create_handler;
6912   spider_hton->drop_database = spider_drop_database;
6913   spider_hton->show_status = spider_show_status;
6914 #ifdef SPIDER_HAS_GROUP_BY_HANDLER
6915   spider_hton->create_group_by = spider_create_group_by_handler;
6916 #endif
6917 
6918   memset(&spider_alloc_func_name, 0, sizeof(spider_alloc_func_name));
6919   memset(&spider_alloc_file_name, 0, sizeof(spider_alloc_file_name));
6920   memset(&spider_alloc_line_no, 0, sizeof(spider_alloc_line_no));
6921   memset(&spider_total_alloc_mem, 0, sizeof(spider_total_alloc_mem));
6922   memset(&spider_current_alloc_mem, 0, sizeof(spider_current_alloc_mem));
6923   memset(&spider_alloc_mem_count, 0, sizeof(spider_alloc_mem_count));
6924   memset(&spider_free_mem_count, 0, sizeof(spider_free_mem_count));
6925 
6926 #ifdef _WIN32
6927   HMODULE current_module = GetModuleHandle(NULL);
6928 #ifndef SPIDER_HAS_NEXT_THREAD_ID
6929   spd_db_att_thread_id = (ulong *)
6930     GetProcAddress(current_module, "?thread_id@@3KA");
6931 #endif
6932 #ifdef SPIDER_XID_USES_xid_cache_iterate
6933 #else
6934 #ifdef XID_CACHE_IS_SPLITTED
6935   spd_db_att_xid_cache_split_num = (uint *)
6936     GetProcAddress(current_module,
6937       "?opt_xid_cache_split_num@@3IA");
6938   spd_db_att_LOCK_xid_cache = *((pthread_mutex_t **)
6939     GetProcAddress(current_module,
6940       "?LOCK_xid_cache@@3PAUst_mysql_mutex@@A"));
6941   spd_db_att_xid_cache = *((HASH **)
6942     GetProcAddress(current_module, "?xid_cache@@3PAUst_hash@@A"));
6943 #else
6944   spd_db_att_LOCK_xid_cache = (pthread_mutex_t *)
6945 #if MYSQL_VERSION_ID < 50500
6946     GetProcAddress(current_module,
6947       "?LOCK_xid_cache@@3U_RTL_CRITICAL_SECTION@@A");
6948 #else
6949     GetProcAddress(current_module,
6950       "?LOCK_xid_cache@@3Ust_mysql_mutex@@A");
6951 #endif
6952   spd_db_att_xid_cache = (HASH *)
6953     GetProcAddress(current_module, "?xid_cache@@3Ust_hash@@A");
6954 #endif
6955 #endif
6956   spd_charset_utf8_bin = (struct charset_info_st *)
6957     GetProcAddress(current_module, "my_charset_utf8_bin");
6958   spd_defaults_extra_file = (const char **)
6959     GetProcAddress(current_module, "my_defaults_extra_file");
6960   spd_defaults_file = (const char **)
6961     GetProcAddress(current_module, "my_defaults_file");
6962   spd_abort_loop = (bool volatile *)
6963     GetProcAddress(current_module, "?abort_loop@@3_NC");
6964   spd_tz_system = *(Time_zone **)
6965 #ifdef _WIN64
6966     GetProcAddress(current_module, "?my_tz_SYSTEM@@3PEAVTime_zone@@EA");
6967 #else
6968     GetProcAddress(current_module, "?my_tz_SYSTEM@@3PAVTime_zone@@A");
6969 #endif
6970 #else
6971 #ifndef SPIDER_HAS_NEXT_THREAD_ID
6972   spd_db_att_thread_id = &thread_id;
6973 #endif
6974 #ifdef SPIDER_XID_USES_xid_cache_iterate
6975 #else
6976 #ifdef XID_CACHE_IS_SPLITTED
6977   spd_db_att_xid_cache_split_num = &opt_xid_cache_split_num;
6978   spd_db_att_LOCK_xid_cache = LOCK_xid_cache;
6979   spd_db_att_xid_cache = xid_cache;
6980 #else
6981   spd_db_att_LOCK_xid_cache = &LOCK_xid_cache;
6982   spd_db_att_xid_cache = &xid_cache;
6983 #endif
6984 #endif
6985   spd_charset_utf8_bin = &my_charset_utf8_bin;
6986   spd_defaults_extra_file = &my_defaults_extra_file;
6987   spd_defaults_file = &my_defaults_file;
6988   spd_abort_loop = &abort_loop;
6989   spd_tz_system = my_tz_SYSTEM;
6990 #endif
6991 
6992 #ifdef HAVE_PSI_INTERFACE
6993   init_spider_psi_keys();
6994 #endif
6995 
6996 #ifndef WITHOUT_SPIDER_BG_SEARCH
6997   if (pthread_attr_init(&spider_pt_attr))
6998     goto error_pt_attr_init;
6999 /*
7000   if (pthread_attr_setdetachstate(&spider_pt_attr, PTHREAD_CREATE_DETACHED))
7001     goto error_pt_attr_setstate;
7002 */
7003 #endif
7004 
7005 #if MYSQL_VERSION_ID < 50500
7006   if (pthread_mutex_init(&spider_tbl_mutex, MY_MUTEX_INIT_FAST))
7007 #else
7008   if (mysql_mutex_init(spd_key_mutex_tbl,
7009     &spider_tbl_mutex, MY_MUTEX_INIT_FAST))
7010 #endif
7011     goto error_tbl_mutex_init;
7012 #if MYSQL_VERSION_ID < 50500
7013   if (pthread_mutex_init(&spider_thread_id_mutex, MY_MUTEX_INIT_FAST))
7014 #else
7015   if (mysql_mutex_init(spd_key_thread_id,
7016     &spider_thread_id_mutex, MY_MUTEX_INIT_FAST))
7017 #endif
7018     goto error_thread_id_mutex_init;
7019 #if MYSQL_VERSION_ID < 50500
7020   if (pthread_mutex_init(&spider_conn_id_mutex, MY_MUTEX_INIT_FAST))
7021 #else
7022   if (mysql_mutex_init(spd_key_conn_id,
7023     &spider_conn_id_mutex, MY_MUTEX_INIT_FAST))
7024 #endif
7025     goto error_conn_id_mutex_init;
7026 #if MYSQL_VERSION_ID < 50500
7027   if (pthread_mutex_init(&spider_ipport_conn_mutex, MY_MUTEX_INIT_FAST))
7028 #else
7029   if (mysql_mutex_init(spd_key_mutex_ipport_count,
7030     &spider_ipport_conn_mutex, MY_MUTEX_INIT_FAST))
7031 #endif
7032     goto error_ipport_count_mutex_init;
7033 
7034 #if MYSQL_VERSION_ID < 50500
7035   if (pthread_mutex_init(&spider_init_error_tbl_mutex, MY_MUTEX_INIT_FAST))
7036 #else
7037   if (mysql_mutex_init(spd_key_mutex_init_error_tbl,
7038     &spider_init_error_tbl_mutex, MY_MUTEX_INIT_FAST))
7039 #endif
7040     goto error_init_error_tbl_mutex_init;
7041 
7042 #ifdef WITH_PARTITION_STORAGE_ENGINE
7043 #if MYSQL_VERSION_ID < 50500
7044   if (pthread_mutex_init(&spider_pt_share_mutex, MY_MUTEX_INIT_FAST))
7045 #else
7046   if (mysql_mutex_init(spd_key_mutex_pt_share,
7047     &spider_pt_share_mutex, MY_MUTEX_INIT_FAST))
7048 #endif
7049     goto error_pt_share_mutex_init;
7050 
7051 #endif
7052 #if MYSQL_VERSION_ID < 50500
7053   if (pthread_mutex_init(&spider_lgtm_tblhnd_share_mutex, MY_MUTEX_INIT_FAST))
7054 #else
7055   if (mysql_mutex_init(spd_key_mutex_lgtm_tblhnd_share,
7056     &spider_lgtm_tblhnd_share_mutex, MY_MUTEX_INIT_FAST))
7057 #endif
7058     goto error_lgtm_tblhnd_share_mutex_init;
7059 
7060 #if MYSQL_VERSION_ID < 50500
7061   if (pthread_mutex_init(&spider_conn_mutex, MY_MUTEX_INIT_FAST))
7062 #else
7063   if (mysql_mutex_init(spd_key_mutex_conn,
7064     &spider_conn_mutex, MY_MUTEX_INIT_FAST))
7065 #endif
7066     goto error_conn_mutex_init;
7067 
7068 #if MYSQL_VERSION_ID < 50500
7069   if (pthread_mutex_init(&spider_open_conn_mutex, MY_MUTEX_INIT_FAST))
7070 #else
7071   if (mysql_mutex_init(spd_key_mutex_open_conn,
7072     &spider_open_conn_mutex, MY_MUTEX_INIT_FAST))
7073 #endif
7074     goto error_open_conn_mutex_init;
7075 
7076 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7077 #if MYSQL_VERSION_ID < 50500
7078   if (pthread_mutex_init(&spider_hs_r_conn_mutex, MY_MUTEX_INIT_FAST))
7079 #else
7080   if (mysql_mutex_init(spd_key_mutex_hs_r_conn,
7081     &spider_hs_r_conn_mutex, MY_MUTEX_INIT_FAST))
7082 #endif
7083     goto error_hs_r_conn_mutex_init;
7084 
7085 #if MYSQL_VERSION_ID < 50500
7086   if (pthread_mutex_init(&spider_hs_w_conn_mutex, MY_MUTEX_INIT_FAST))
7087 #else
7088   if (mysql_mutex_init(spd_key_mutex_hs_w_conn,
7089     &spider_hs_w_conn_mutex, MY_MUTEX_INIT_FAST))
7090 #endif
7091     goto error_hs_w_conn_mutex_init;
7092 
7093 #endif
7094 #if MYSQL_VERSION_ID < 50500
7095   if (pthread_mutex_init(&spider_allocated_thds_mutex, MY_MUTEX_INIT_FAST))
7096 #else
7097   if (mysql_mutex_init(spd_key_mutex_allocated_thds,
7098     &spider_allocated_thds_mutex, MY_MUTEX_INIT_FAST))
7099 #endif
7100     goto error_allocated_thds_mutex_init;
7101 
7102 #if MYSQL_VERSION_ID < 50500
7103   if (pthread_mutex_init(&spider_mon_table_cache_mutex, MY_MUTEX_INIT_FAST))
7104 #else
7105   if (mysql_mutex_init(spd_key_mutex_mon_table_cache,
7106     &spider_mon_table_cache_mutex, MY_MUTEX_INIT_FAST))
7107 #endif
7108     goto error_mon_table_cache_mutex_init;
7109 
7110 #if MYSQL_VERSION_ID < 50500
7111   if (pthread_mutex_init(&spider_mem_calc_mutex, MY_MUTEX_INIT_FAST))
7112 #else
7113   if (mysql_mutex_init(spd_key_mutex_mem_calc,
7114     &spider_mem_calc_mutex, MY_MUTEX_INIT_FAST))
7115 #endif
7116     goto error_mem_calc_mutex_init;
7117 
7118   if (my_hash_init(&spider_open_tables, spd_charset_utf8_bin, 32, 0, 0,
7119                    (my_hash_get_key) spider_tbl_get_key, 0, 0))
7120     goto error_open_tables_hash_init;
7121 
7122   spider_alloc_calc_mem_init(spider_open_tables, 143);
7123   spider_alloc_calc_mem(NULL,
7124     spider_open_tables,
7125     spider_open_tables.array.max_element *
7126     spider_open_tables.array.size_of_element);
7127   if (my_hash_init(&spider_init_error_tables, spd_charset_utf8_bin, 32, 0, 0,
7128                    (my_hash_get_key) spider_tbl_get_key, 0, 0))
7129     goto error_init_error_tables_hash_init;
7130 
7131   spider_alloc_calc_mem_init(spider_init_error_tables, 144);
7132   spider_alloc_calc_mem(NULL,
7133     spider_init_error_tables,
7134     spider_init_error_tables.array.max_element *
7135     spider_init_error_tables.array.size_of_element);
7136 #ifdef WITH_PARTITION_STORAGE_ENGINE
7137   if (my_hash_init(&spider_open_pt_share, spd_charset_utf8_bin, 32, 0, 0,
7138                    (my_hash_get_key) spider_pt_share_get_key, 0, 0))
7139     goto error_open_pt_share_hash_init;
7140 
7141   spider_alloc_calc_mem_init(spider_open_pt_share, 145);
7142   spider_alloc_calc_mem(NULL,
7143     spider_open_pt_share,
7144     spider_open_pt_share.array.max_element *
7145     spider_open_pt_share.array.size_of_element);
7146 #endif
7147   if (my_hash_init(&spider_lgtm_tblhnd_share_hash, spd_charset_utf8_bin,
7148                    32, 0, 0,
7149                    (my_hash_get_key) spider_lgtm_tblhnd_share_hash_get_key,
7150                    0, 0))
7151     goto error_lgtm_tblhnd_share_hash_init;
7152 
7153   spider_alloc_calc_mem_init(spider_lgtm_tblhnd_share_hash, 245);
7154   spider_alloc_calc_mem(NULL,
7155     spider_lgtm_tblhnd_share_hash,
7156     spider_lgtm_tblhnd_share_hash.array.max_element *
7157     spider_lgtm_tblhnd_share_hash.array.size_of_element);
7158   if (my_hash_init(&spider_open_connections, spd_charset_utf8_bin, 32, 0, 0,
7159                    (my_hash_get_key) spider_conn_get_key, 0, 0))
7160     goto error_open_connections_hash_init;
7161 
7162   if (my_hash_init(&spider_ipport_conns, spd_charset_utf8_bin, 32, 0, 0,
7163                    (my_hash_get_key) spider_ipport_conn_get_key,
7164                    spider_free_ipport_conn, 0))
7165       goto error_ipport_conn__hash_init;
7166 
7167   spider_alloc_calc_mem_init(spider_open_connections, 146);
7168   spider_alloc_calc_mem(NULL,
7169     spider_open_connections,
7170     spider_open_connections.array.max_element *
7171     spider_open_connections.array.size_of_element);
7172 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7173   if (my_hash_init(&spider_hs_r_conn_hash, spd_charset_utf8_bin, 32, 0, 0,
7174                    (my_hash_get_key) spider_conn_get_key, 0, 0))
7175     goto error_hs_r_conn_hash_init;
7176 
7177   spider_alloc_calc_mem_init(spider_hs_r_conn_hash, 147);
7178   spider_alloc_calc_mem(NULL,
7179     spider_hs_r_conn_hash,
7180     spider_hs_r_conn_hash.array.max_element *
7181     spider_hs_r_conn_hash.array.size_of_element);
7182   if (my_hash_init(&spider_hs_w_conn_hash, spd_charset_utf8_bin, 32, 0, 0,
7183                    (my_hash_get_key) spider_conn_get_key, 0, 0))
7184     goto error_hs_w_conn_hash_init;
7185 
7186   spider_alloc_calc_mem_init(spider_hs_w_conn_hash, 148);
7187   spider_alloc_calc_mem(NULL,
7188     spider_hs_w_conn_hash,
7189     spider_hs_w_conn_hash.array.max_element *
7190     spider_hs_w_conn_hash.array.size_of_element);
7191 #endif
7192   if (my_hash_init(&spider_allocated_thds, spd_charset_utf8_bin, 32, 0, 0,
7193                    (my_hash_get_key) spider_allocated_thds_get_key, 0, 0))
7194     goto error_allocated_thds_hash_init;
7195 
7196   spider_alloc_calc_mem_init(spider_allocated_thds, 149);
7197   spider_alloc_calc_mem(NULL,
7198     spider_allocated_thds,
7199     spider_allocated_thds.array.max_element *
7200     spider_allocated_thds.array.size_of_element);
7201 
7202   if (SPD_INIT_DYNAMIC_ARRAY2(&spider_mon_table_cache, sizeof(SPIDER_MON_KEY),
7203       NULL, 64, 64, MYF(MY_WME)))
7204     goto error_mon_table_cache_array_init;
7205 
7206   spider_alloc_calc_mem_init(spider_mon_table_cache, 165);
7207   spider_alloc_calc_mem(NULL,
7208     spider_mon_table_cache,
7209     spider_mon_table_cache.max_element *
7210     spider_mon_table_cache.size_of_element);
7211 
7212   if (!(spider_udf_table_mon_mutexes = (pthread_mutex_t *)
7213     spider_bulk_malloc(NULL, 53, MYF(MY_WME | MY_ZEROFILL),
7214       &spider_udf_table_mon_mutexes, sizeof(pthread_mutex_t) *
7215         spider_param_udf_table_mon_mutex_count(),
7216       &spider_udf_table_mon_conds, sizeof(pthread_cond_t) *
7217         spider_param_udf_table_mon_mutex_count(),
7218       &spider_udf_table_mon_list_hash, sizeof(HASH) *
7219         spider_param_udf_table_mon_mutex_count(),
7220       NullS))
7221   )
7222     goto error_alloc_mon_mutxes;
7223 
7224   for (roop_count = 0;
7225     roop_count < (int) spider_param_udf_table_mon_mutex_count();
7226     roop_count++)
7227   {
7228 #if MYSQL_VERSION_ID < 50500
7229     if (pthread_mutex_init(&spider_udf_table_mon_mutexes[roop_count],
7230       MY_MUTEX_INIT_FAST))
7231 #else
7232     if (mysql_mutex_init(spd_key_mutex_udf_table_mon,
7233       &spider_udf_table_mon_mutexes[roop_count], MY_MUTEX_INIT_FAST))
7234 #endif
7235       goto error_init_udf_table_mon_mutex;
7236   }
7237   for (roop_count = 0;
7238     roop_count < (int) spider_param_udf_table_mon_mutex_count();
7239     roop_count++)
7240   {
7241 #if MYSQL_VERSION_ID < 50500
7242     if (pthread_cond_init(&spider_udf_table_mon_conds[roop_count], NULL))
7243 #else
7244     if (mysql_cond_init(spd_key_cond_udf_table_mon,
7245       &spider_udf_table_mon_conds[roop_count], NULL))
7246 #endif
7247       goto error_init_udf_table_mon_cond;
7248   }
7249   for (roop_count = 0;
7250     roop_count < (int) spider_param_udf_table_mon_mutex_count();
7251     roop_count++)
7252   {
7253     if (my_hash_init(&spider_udf_table_mon_list_hash[roop_count],
7254       spd_charset_utf8_bin, 32, 0, 0,
7255       (my_hash_get_key) spider_udf_tbl_mon_list_key, 0, 0))
7256       goto error_init_udf_table_mon_list_hash;
7257 
7258     spider_alloc_calc_mem_init(spider_udf_table_mon_list_hash, 150);
7259     spider_alloc_calc_mem(NULL,
7260       spider_udf_table_mon_list_hash,
7261       spider_udf_table_mon_list_hash[roop_count].array.max_element *
7262       spider_udf_table_mon_list_hash[roop_count].array.size_of_element);
7263   }
7264 
7265 #ifndef WITHOUT_SPIDER_BG_SEARCH
7266   if (!(spider_table_sts_threads = (SPIDER_THREAD *)
7267     spider_bulk_malloc(NULL, 256, MYF(MY_WME | MY_ZEROFILL),
7268       &spider_table_sts_threads, sizeof(SPIDER_THREAD) *
7269         spider_param_table_sts_thread_count(),
7270       &spider_table_crd_threads, sizeof(SPIDER_THREAD) *
7271         spider_param_table_crd_thread_count(),
7272       NullS))
7273   )
7274     goto error_alloc_mon_mutxes;
7275 
7276   for (roop_count = 0;
7277     roop_count < (int) spider_param_table_sts_thread_count();
7278     roop_count++)
7279   {
7280     if ((error_num = spider_create_sts_threads(&spider_table_sts_threads[roop_count])))
7281     {
7282       goto error_init_table_sts_threads;
7283     }
7284   }
7285   for (roop_count = 0;
7286     roop_count < (int) spider_param_table_crd_thread_count();
7287     roop_count++)
7288   {
7289     if ((error_num = spider_create_crd_threads(&spider_table_crd_threads[roop_count])))
7290     {
7291       goto error_init_table_crd_threads;
7292     }
7293   }
7294 #endif
7295 
7296   spider_dbton_mysql.dbton_id = dbton_id;
7297   spider_dbton_mysql.db_util->dbton_id = dbton_id;
7298   spider_dbton[dbton_id] = spider_dbton_mysql;
7299   ++dbton_id;
7300   spider_dbton_mariadb.dbton_id = dbton_id;
7301   spider_dbton_mariadb.db_util->dbton_id = dbton_id;
7302   spider_dbton[dbton_id] = spider_dbton_mariadb;
7303   ++dbton_id;
7304 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7305   spider_dbton_handlersocket.dbton_id = dbton_id;
7306   spider_dbton_handlersocket.db_util->dbton_id = dbton_id;
7307   spider_dbton[dbton_id] = spider_dbton_handlersocket;
7308   ++dbton_id;
7309 #endif
7310 #ifdef HAVE_ORACLE_OCI
7311   spider_dbton_oracle.dbton_id = dbton_id;
7312   spider_dbton_oracle.db_util->dbton_id = dbton_id;
7313   spider_dbton[dbton_id] = spider_dbton_oracle;
7314   ++dbton_id;
7315 #endif
7316   for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; roop_count++)
7317   {
7318     if (spider_dbton[roop_count].init)
7319     {
7320       if ((error_num = spider_dbton[roop_count].init()))
7321       {
7322         goto error_init_dbton;
7323       }
7324     }
7325   }
7326 
7327   DBUG_RETURN(0);
7328 
7329 #ifndef WITHOUT_SPIDER_BG_SEARCH
7330 error_init_dbton:
7331   for (roop_count--; roop_count >= 0; roop_count--)
7332   {
7333     if (spider_dbton[roop_count].deinit)
7334     {
7335       spider_dbton[roop_count].deinit();
7336     }
7337   }
7338   roop_count = spider_param_table_crd_thread_count() - 1;
7339 error_init_table_crd_threads:
7340   for (; roop_count >= 0; roop_count--)
7341   {
7342     spider_free_crd_threads(&spider_table_crd_threads[roop_count]);
7343   }
7344   roop_count = spider_param_table_sts_thread_count() - 1;
7345 error_init_table_sts_threads:
7346   for (; roop_count >= 0; roop_count--)
7347   {
7348     spider_free_sts_threads(&spider_table_sts_threads[roop_count]);
7349   }
7350   spider_free(NULL, spider_table_sts_threads, MYF(0));
7351   roop_count = spider_param_udf_table_mon_mutex_count() - 1;
7352 #endif
7353 error_init_udf_table_mon_list_hash:
7354   for (; roop_count >= 0; roop_count--)
7355   {
7356     spider_free_mem_calc(NULL,
7357       spider_udf_table_mon_list_hash_id,
7358       spider_udf_table_mon_list_hash[roop_count].array.max_element *
7359       spider_udf_table_mon_list_hash[roop_count].array.size_of_element);
7360     my_hash_free(&spider_udf_table_mon_list_hash[roop_count]);
7361   }
7362   roop_count = spider_param_udf_table_mon_mutex_count() - 1;
7363 error_init_udf_table_mon_cond:
7364   for (; roop_count >= 0; roop_count--)
7365     pthread_cond_destroy(&spider_udf_table_mon_conds[roop_count]);
7366   roop_count = spider_param_udf_table_mon_mutex_count() - 1;
7367 error_init_udf_table_mon_mutex:
7368   for (; roop_count >= 0; roop_count--)
7369     pthread_mutex_destroy(&spider_udf_table_mon_mutexes[roop_count]);
7370   spider_free(NULL, spider_udf_table_mon_mutexes, MYF(0));
7371 error_alloc_mon_mutxes:
7372   spider_free_mem_calc(NULL,
7373     spider_mon_table_cache_id,
7374     spider_mon_table_cache.max_element *
7375     spider_mon_table_cache.size_of_element);
7376   delete_dynamic(&spider_mon_table_cache);
7377 error_mon_table_cache_array_init:
7378   spider_free_mem_calc(NULL,
7379     spider_allocated_thds_id,
7380     spider_allocated_thds.array.max_element *
7381     spider_allocated_thds.array.size_of_element);
7382   my_hash_free(&spider_allocated_thds);
7383 error_allocated_thds_hash_init:
7384   my_hash_free(&spider_ipport_conns);
7385 error_ipport_conn__hash_init:
7386 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7387   spider_free_mem_calc(NULL,
7388     spider_hs_w_conn_hash_id,
7389     spider_hs_w_conn_hash.array.max_element *
7390     spider_hs_w_conn_hash.array.size_of_element);
7391   my_hash_free(&spider_hs_w_conn_hash);
7392 error_hs_w_conn_hash_init:
7393   spider_free_mem_calc(NULL,
7394     spider_hs_r_conn_hash_id,
7395     spider_hs_r_conn_hash.array.max_element *
7396     spider_hs_r_conn_hash.array.size_of_element);
7397   my_hash_free(&spider_hs_r_conn_hash);
7398 error_hs_r_conn_hash_init:
7399 #endif
7400   spider_free_mem_calc(NULL,
7401     spider_open_connections_id,
7402     spider_open_connections.array.max_element *
7403     spider_open_connections.array.size_of_element);
7404   my_hash_free(&spider_open_connections);
7405 error_open_connections_hash_init:
7406   spider_free_mem_calc(NULL,
7407     spider_lgtm_tblhnd_share_hash_id,
7408     spider_lgtm_tblhnd_share_hash.array.max_element *
7409     spider_lgtm_tblhnd_share_hash.array.size_of_element);
7410   my_hash_free(&spider_lgtm_tblhnd_share_hash);
7411 error_lgtm_tblhnd_share_hash_init:
7412 #ifdef WITH_PARTITION_STORAGE_ENGINE
7413   spider_free_mem_calc(NULL,
7414     spider_open_pt_share_id,
7415     spider_open_pt_share.array.max_element *
7416     spider_open_pt_share.array.size_of_element);
7417   my_hash_free(&spider_open_pt_share);
7418 error_open_pt_share_hash_init:
7419 #endif
7420   spider_free_mem_calc(NULL,
7421     spider_init_error_tables_id,
7422     spider_init_error_tables.array.max_element *
7423     spider_init_error_tables.array.size_of_element);
7424   my_hash_free(&spider_init_error_tables);
7425 error_init_error_tables_hash_init:
7426   spider_free_mem_calc(NULL,
7427     spider_open_tables_id,
7428     spider_open_tables.array.max_element *
7429     spider_open_tables.array.size_of_element);
7430   my_hash_free(&spider_open_tables);
7431 error_open_tables_hash_init:
7432   pthread_mutex_destroy(&spider_mem_calc_mutex);
7433 error_mem_calc_mutex_init:
7434   pthread_mutex_destroy(&spider_mon_table_cache_mutex);
7435 error_mon_table_cache_mutex_init:
7436   pthread_mutex_destroy(&spider_allocated_thds_mutex);
7437 error_allocated_thds_mutex_init:
7438 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
7439   pthread_mutex_destroy(&spider_hs_w_conn_mutex);
7440 error_hs_w_conn_mutex_init:
7441   pthread_mutex_destroy(&spider_hs_r_conn_mutex);
7442 error_hs_r_conn_mutex_init:
7443 #endif
7444   pthread_mutex_destroy(&spider_open_conn_mutex);
7445 error_open_conn_mutex_init:
7446   pthread_mutex_destroy(&spider_conn_mutex);
7447 error_conn_mutex_init:
7448   pthread_mutex_destroy(&spider_lgtm_tblhnd_share_mutex);
7449 error_lgtm_tblhnd_share_mutex_init:
7450 #ifdef WITH_PARTITION_STORAGE_ENGINE
7451   pthread_mutex_destroy(&spider_pt_share_mutex);
7452 error_pt_share_mutex_init:
7453 #endif
7454   pthread_mutex_destroy(&spider_init_error_tbl_mutex);
7455 error_init_error_tbl_mutex_init:
7456   pthread_mutex_destroy(&spider_ipport_conn_mutex);
7457 error_ipport_count_mutex_init:
7458   pthread_mutex_destroy(&spider_conn_id_mutex);
7459 error_conn_id_mutex_init:
7460   pthread_mutex_destroy(&spider_thread_id_mutex);
7461 error_thread_id_mutex_init:
7462   pthread_mutex_destroy(&spider_tbl_mutex);
7463 error_tbl_mutex_init:
7464 #ifndef WITHOUT_SPIDER_BG_SEARCH
7465 /*
7466 error_pt_attr_setstate:
7467 */
7468   pthread_attr_destroy(&spider_pt_attr);
7469 error_pt_attr_init:
7470 #endif
7471   DBUG_RETURN(error_num);
7472 }
7473 
spider_create_string(const char * str,uint length)7474 char *spider_create_string(
7475   const char *str,
7476   uint length
7477 ) {
7478   char *res;
7479   DBUG_ENTER("spider_create_string");
7480   if (!(res = (char*) spider_malloc(spider_current_trx, 13, length + 1,
7481     MYF(MY_WME))))
7482     DBUG_RETURN(NULL);
7483   memcpy(res, str, length);
7484   res[length] = '\0';
7485   DBUG_RETURN(res);
7486 }
7487 
spider_create_table_name_string(const char * table_name,const char * part_name,const char * sub_name)7488 char *spider_create_table_name_string(
7489   const char *table_name,
7490   const char *part_name,
7491   const char *sub_name
7492 ) {
7493   char *res, *tmp;
7494   uint length = strlen(table_name);
7495   DBUG_ENTER("spider_create_table_name_string");
7496   if (part_name)
7497   {
7498     length += sizeof("#P#") - 1 + strlen(part_name);
7499     if (sub_name)
7500       length += sizeof("#SP#") - 1 + strlen(sub_name);
7501   }
7502   if (!(res = (char*) spider_malloc(spider_current_trx, 14, length + 1,
7503     MYF(MY_WME))))
7504     DBUG_RETURN(NULL);
7505   tmp = strmov(res, table_name);
7506   if (part_name)
7507   {
7508     tmp = strmov(tmp, "#P#");
7509     tmp = strmov(tmp, part_name);
7510     if (sub_name)
7511     {
7512       tmp = strmov(tmp, "#SP#");
7513       tmp = strmov(tmp, sub_name);
7514     }
7515   }
7516   DBUG_RETURN(res);
7517 }
7518 
7519 #ifdef WITH_PARTITION_STORAGE_ENGINE
spider_get_partition_info(const char * table_name,uint table_name_length,const TABLE_SHARE * table_share,partition_info * part_info,partition_element ** part_elem,partition_element ** sub_elem)7520 void spider_get_partition_info(
7521   const char *table_name,
7522   uint table_name_length,
7523   const TABLE_SHARE *table_share,
7524   partition_info *part_info,
7525   partition_element **part_elem,
7526   partition_element **sub_elem
7527 ) {
7528   char tmp_name[FN_REFLEN + 1];
7529   partition_element *tmp_part_elem = NULL, *tmp_sub_elem = NULL;
7530   bool tmp_flg = FALSE, tmp_find_flg = FALSE;
7531   DBUG_ENTER("spider_get_partition_info");
7532   *part_elem = NULL;
7533   *sub_elem = NULL;
7534   if (!part_info)
7535     DBUG_VOID_RETURN;
7536 
7537   if (!memcmp(table_name + table_name_length - 5, "#TMP#", 5))
7538     tmp_flg = TRUE;
7539 
7540   DBUG_PRINT("info",("spider table_name=%s", table_name));
7541   List_iterator<partition_element> part_it(part_info->partitions);
7542   while ((*part_elem = part_it++))
7543   {
7544     if ((*part_elem)->subpartitions.elements)
7545     {
7546       List_iterator<partition_element> sub_it((*part_elem)->subpartitions);
7547       while ((*sub_elem = sub_it++))
7548       {
7549         if (SPIDER_create_subpartition_name(
7550           tmp_name, FN_REFLEN + 1, table_share->path.str,
7551           (*part_elem)->partition_name, (*sub_elem)->partition_name,
7552           NORMAL_PART_NAME))
7553         {
7554           DBUG_VOID_RETURN;
7555         }
7556         DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
7557         if (!memcmp(table_name, tmp_name, table_name_length + 1))
7558           DBUG_VOID_RETURN;
7559         if (
7560           tmp_flg &&
7561           *(tmp_name + table_name_length - 5) == '\0' &&
7562           !memcmp(table_name, tmp_name, table_name_length - 5)
7563         ) {
7564           tmp_part_elem = *part_elem;
7565           tmp_sub_elem = *sub_elem;
7566           tmp_flg = FALSE;
7567           tmp_find_flg = TRUE;
7568         }
7569       }
7570     } else {
7571       if (SPIDER_create_partition_name(
7572         tmp_name, FN_REFLEN + 1, table_share->path.str,
7573         (*part_elem)->partition_name, NORMAL_PART_NAME, TRUE))
7574       {
7575         DBUG_VOID_RETURN;
7576       }
7577       DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
7578       if (!memcmp(table_name, tmp_name, table_name_length + 1))
7579         DBUG_VOID_RETURN;
7580       if (
7581         tmp_flg &&
7582         *(tmp_name + table_name_length - 5) == '\0' &&
7583         !memcmp(table_name, tmp_name, table_name_length - 5)
7584       ) {
7585         tmp_part_elem = *part_elem;
7586         tmp_flg = FALSE;
7587         tmp_find_flg = TRUE;
7588       }
7589     }
7590   }
7591   if (tmp_find_flg)
7592   {
7593     *part_elem = tmp_part_elem;
7594     *sub_elem = tmp_sub_elem;
7595     DBUG_PRINT("info",("spider tmp find"));
7596     DBUG_VOID_RETURN;
7597   }
7598   *part_elem = NULL;
7599   *sub_elem = NULL;
7600   DBUG_PRINT("info",("spider no hit"));
7601   DBUG_VOID_RETURN;
7602 }
7603 #endif
7604 
spider_get_sts(SPIDER_SHARE * share,int link_idx,time_t tmp_time,ha_spider * spider,double sts_interval,int sts_mode,int sts_sync,int sts_sync_level,uint flag)7605 int spider_get_sts(
7606   SPIDER_SHARE *share,
7607   int link_idx,
7608   time_t tmp_time,
7609   ha_spider *spider,
7610   double sts_interval,
7611   int sts_mode,
7612 #ifdef WITH_PARTITION_STORAGE_ENGINE
7613   int sts_sync,
7614 #endif
7615   int sts_sync_level,
7616   uint flag
7617 ) {
7618   int get_type __attribute__ ((unused));
7619   int error_num = 0;
7620   bool need_to_get = TRUE;
7621   DBUG_ENTER("spider_get_sts");
7622 
7623 #ifdef WITH_PARTITION_STORAGE_ENGINE
7624   if (
7625     sts_sync == 0
7626   ) {
7627 #endif
7628     /* get */
7629     get_type = 1;
7630 #ifdef WITH_PARTITION_STORAGE_ENGINE
7631   } else if (
7632     !share->partition_share->sts_init
7633   ) {
7634     pthread_mutex_lock(&share->partition_share->sts_mutex);
7635     if (!share->partition_share->sts_init)
7636     {
7637       /* get after mutex_lock */
7638       get_type = 2;
7639     } else {
7640       pthread_mutex_unlock(&share->partition_share->sts_mutex);
7641       /* copy */
7642       get_type = 0;
7643     }
7644   } else if (
7645     difftime(share->sts_get_time, share->partition_share->sts_get_time) <
7646       sts_interval
7647   ) {
7648     /* copy */
7649     get_type = 0;
7650   } else if (
7651     !pthread_mutex_trylock(&share->partition_share->sts_mutex)
7652   ) {
7653     /* get after mutex_trylock */
7654     get_type = 3;
7655   } else {
7656     /* copy */
7657     get_type = 0;
7658   }
7659 #endif
7660   if (
7661     !share->sts_init &&
7662     spider_param_load_sts_at_startup(share->load_sts_at_startup) &&
7663     (!share->init || share->init_error)
7664   ) {
7665     error_num = spider_sys_get_table_sts(
7666       current_thd,
7667       share->lgtm_tblhnd_share->table_name,
7668       share->lgtm_tblhnd_share->table_name_length,
7669       &share->data_file_length,
7670       &share->max_data_file_length,
7671       &share->index_file_length,
7672       &share->records,
7673       &share->mean_rec_length,
7674       &share->check_time,
7675       &share->create_time,
7676       &share->update_time,
7677       FALSE
7678     );
7679     if (
7680       !error_num ||
7681       (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
7682     )
7683     need_to_get = FALSE;
7684   }
7685 
7686   if (need_to_get)
7687   {
7688 #ifdef WITH_PARTITION_STORAGE_ENGINE
7689     if (get_type == 0)
7690       spider_copy_sts_to_share(share, share->partition_share);
7691     else {
7692 #endif
7693       error_num = spider_db_show_table_status(spider, link_idx, sts_mode, flag);
7694 #ifdef WITH_PARTITION_STORAGE_ENGINE
7695     }
7696 #endif
7697   }
7698 #ifdef WITH_PARTITION_STORAGE_ENGINE
7699   if (get_type >= 2)
7700     pthread_mutex_unlock(&share->partition_share->sts_mutex);
7701 #endif
7702   if (error_num)
7703   {
7704 #ifdef WITH_PARTITION_STORAGE_ENGINE
7705     SPIDER_PARTITION_HANDLER_SHARE *partition_handler_share =
7706       spider->partition_handler_share;
7707     if (
7708       !share->partition_share->sts_init &&
7709       sts_sync >= sts_sync_level &&
7710       get_type > 1 &&
7711       partition_handler_share &&
7712       partition_handler_share->handlers &&
7713       partition_handler_share->handlers[0] == spider
7714     ) {
7715       int roop_count;
7716       ha_spider *tmp_spider;
7717       SPIDER_SHARE *tmp_share;
7718       double tmp_sts_interval;
7719       int tmp_sts_mode;
7720       int tmp_sts_sync;
7721       THD *thd = spider->trx->thd;
7722       for (roop_count = 1;
7723         roop_count < (int) partition_handler_share->use_count;
7724         roop_count++)
7725       {
7726         tmp_spider =
7727           (ha_spider *) partition_handler_share->handlers[roop_count];
7728         tmp_share = tmp_spider->share;
7729         tmp_sts_interval = spider_param_sts_interval(thd, share->sts_interval);
7730         tmp_sts_mode = spider_param_sts_mode(thd, share->sts_mode);
7731         tmp_sts_sync = spider_param_sts_sync(thd, share->sts_sync);
7732         spider_get_sts(tmp_share, tmp_spider->search_link_idx,
7733           tmp_time, tmp_spider, tmp_sts_interval, tmp_sts_mode, tmp_sts_sync,
7734           1, flag);
7735         if (share->partition_share->sts_init)
7736         {
7737           error_num = 0;
7738           thd->clear_error();
7739           get_type = 0;
7740           spider_copy_sts_to_share(share, share->partition_share);
7741           break;
7742         }
7743       }
7744     }
7745     if (error_num)
7746 #endif
7747       DBUG_RETURN(error_num);
7748   }
7749 #ifdef WITH_PARTITION_STORAGE_ENGINE
7750   if (sts_sync >= sts_sync_level && get_type > 0)
7751   {
7752     spider_copy_sts_to_pt_share(share->partition_share, share);
7753     share->partition_share->sts_get_time = tmp_time;
7754     share->partition_share->sts_init = TRUE;
7755   }
7756 #endif
7757   share->sts_get_time = tmp_time;
7758   share->sts_init = TRUE;
7759   DBUG_RETURN(0);
7760 }
7761 
spider_get_crd(SPIDER_SHARE * share,int link_idx,time_t tmp_time,ha_spider * spider,TABLE * table,double crd_interval,int crd_mode,int crd_sync,int crd_sync_level)7762 int spider_get_crd(
7763   SPIDER_SHARE *share,
7764   int link_idx,
7765   time_t tmp_time,
7766   ha_spider *spider,
7767   TABLE *table,
7768   double crd_interval,
7769   int crd_mode,
7770 #ifdef WITH_PARTITION_STORAGE_ENGINE
7771   int crd_sync,
7772 #endif
7773   int crd_sync_level
7774 ) {
7775   int get_type __attribute__ ((unused));
7776   int error_num = 0;
7777   bool need_to_get = TRUE;
7778   DBUG_ENTER("spider_get_crd");
7779 
7780 #ifdef WITH_PARTITION_STORAGE_ENGINE
7781   if (
7782     crd_sync == 0
7783   ) {
7784 #endif
7785     /* get */
7786     get_type = 1;
7787 #ifdef WITH_PARTITION_STORAGE_ENGINE
7788   } else if (
7789     !share->partition_share->crd_init
7790   ) {
7791     pthread_mutex_lock(&share->partition_share->crd_mutex);
7792     if (!share->partition_share->crd_init)
7793     {
7794       /* get after mutex_lock */
7795       get_type = 2;
7796     } else {
7797       pthread_mutex_unlock(&share->partition_share->crd_mutex);
7798       /* copy */
7799       get_type = 0;
7800     }
7801   } else if (
7802     difftime(share->crd_get_time, share->partition_share->crd_get_time) <
7803       crd_interval
7804   ) {
7805     /* copy */
7806     get_type = 0;
7807   } else if (
7808     !pthread_mutex_trylock(&share->partition_share->crd_mutex)
7809   ) {
7810     /* get after mutex_trylock */
7811     get_type = 3;
7812   } else {
7813     /* copy */
7814     get_type = 0;
7815   }
7816 #endif
7817   if (
7818     !share->crd_init &&
7819     spider_param_load_sts_at_startup(share->load_crd_at_startup)
7820   ) {
7821     error_num = spider_sys_get_table_crd(
7822       current_thd,
7823       share->lgtm_tblhnd_share->table_name,
7824       share->lgtm_tblhnd_share->table_name_length,
7825       share->cardinality,
7826       table->s->fields,
7827       FALSE
7828     );
7829     if (
7830       !error_num ||
7831       (error_num != HA_ERR_KEY_NOT_FOUND && error_num != HA_ERR_END_OF_FILE)
7832     )
7833     need_to_get = FALSE;
7834   }
7835 
7836   if (need_to_get)
7837   {
7838 #ifdef WITH_PARTITION_STORAGE_ENGINE
7839     if (get_type == 0)
7840       spider_copy_crd_to_share(share, share->partition_share,
7841         table->s->fields);
7842     else {
7843 #endif
7844       error_num = spider_db_show_index(spider, link_idx, table, crd_mode);
7845 #ifdef WITH_PARTITION_STORAGE_ENGINE
7846     }
7847 #endif
7848   }
7849 #ifdef WITH_PARTITION_STORAGE_ENGINE
7850   if (get_type >= 2)
7851     pthread_mutex_unlock(&share->partition_share->crd_mutex);
7852 #endif
7853   if (error_num)
7854   {
7855 #ifdef WITH_PARTITION_STORAGE_ENGINE
7856     SPIDER_PARTITION_HANDLER_SHARE *partition_handler_share =
7857       spider->partition_handler_share;
7858     if (
7859       !share->partition_share->crd_init &&
7860       crd_sync >= crd_sync_level &&
7861       get_type > 1 &&
7862       partition_handler_share &&
7863       partition_handler_share->handlers &&
7864       partition_handler_share->handlers[0] == spider
7865     ) {
7866       int roop_count;
7867       ha_spider *tmp_spider;
7868       SPIDER_SHARE *tmp_share;
7869       double tmp_crd_interval;
7870       int tmp_crd_mode;
7871       int tmp_crd_sync;
7872       THD *thd = spider->trx->thd;
7873       for (roop_count = 1;
7874         roop_count < (int) partition_handler_share->use_count;
7875         roop_count++)
7876       {
7877         tmp_spider =
7878           (ha_spider *) partition_handler_share->handlers[roop_count];
7879         tmp_share = tmp_spider->share;
7880         tmp_crd_interval = spider_param_crd_interval(thd, share->crd_interval);
7881         tmp_crd_mode = spider_param_crd_mode(thd, share->crd_mode);
7882         tmp_crd_sync = spider_param_crd_sync(thd, share->crd_sync);
7883         spider_get_crd(tmp_share, tmp_spider->search_link_idx,
7884           tmp_time, tmp_spider, table, tmp_crd_interval, tmp_crd_mode,
7885           tmp_crd_sync, 1);
7886         if (share->partition_share->crd_init)
7887         {
7888           error_num = 0;
7889           thd->clear_error();
7890           get_type = 0;
7891           spider_copy_crd_to_share(share, share->partition_share,
7892             table->s->fields);
7893           break;
7894         }
7895       }
7896     }
7897     if (error_num)
7898 #endif
7899       DBUG_RETURN(error_num);
7900   }
7901 #ifdef WITH_PARTITION_STORAGE_ENGINE
7902   if (crd_sync >= crd_sync_level && get_type > 0)
7903   {
7904     spider_copy_crd_to_pt_share(share->partition_share, share,
7905       table->s->fields);
7906     share->partition_share->crd_get_time = tmp_time;
7907     share->partition_share->crd_init = TRUE;
7908   }
7909 #endif
7910   share->crd_get_time = tmp_time;
7911   share->crd_init = TRUE;
7912   DBUG_RETURN(0);
7913 }
7914 
spider_set_result_list_param(ha_spider * spider)7915 void spider_set_result_list_param(
7916   ha_spider *spider
7917 ) {
7918   SPIDER_RESULT_LIST *result_list = &spider->result_list;
7919   SPIDER_SHARE *share = spider->share;
7920   THD *thd = spider->trx->thd;
7921   DBUG_ENTER("spider_set_result_list_param");
7922   result_list->internal_offset =
7923     spider_param_internal_offset(thd, share->internal_offset);
7924   result_list->internal_limit =
7925 #ifdef INFO_KIND_FORCE_LIMIT_BEGIN
7926     spider->info_limit < 9223372036854775807LL ?
7927     spider->info_limit :
7928 #endif
7929     spider_param_internal_limit(thd, share->internal_limit);
7930   result_list->split_read = spider_split_read_param(spider);
7931   if (spider->support_multi_split_read_sql())
7932   {
7933     result_list->multi_split_read =
7934       spider_param_multi_split_read(thd, share->multi_split_read);
7935   } else {
7936     result_list->multi_split_read = 1;
7937   }
7938   result_list->max_order =
7939     spider_param_max_order(thd, share->max_order);
7940   result_list->quick_mode =
7941     spider_param_quick_mode(thd, share->quick_mode);
7942   result_list->quick_page_size =
7943     spider_param_quick_page_size(thd, share->quick_page_size);
7944   result_list->quick_page_byte =
7945     spider_param_quick_page_byte(thd, share->quick_page_byte);
7946   result_list->low_mem_read =
7947     spider_param_low_mem_read(thd, share->low_mem_read);
7948   DBUG_VOID_RETURN;
7949 }
7950 
spider_get_init_error_table(SPIDER_TRX * trx,SPIDER_SHARE * share,bool create)7951 SPIDER_INIT_ERROR_TABLE *spider_get_init_error_table(
7952   SPIDER_TRX *trx,
7953   SPIDER_SHARE *share,
7954   bool create
7955 ) {
7956   SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
7957   char *tmp_name;
7958   DBUG_ENTER("spider_get_init_error_table");
7959   pthread_mutex_lock(&spider_init_error_tbl_mutex);
7960 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
7961   if (!(spider_init_error_table = (SPIDER_INIT_ERROR_TABLE *)
7962     my_hash_search_using_hash_value(
7963     &spider_init_error_tables, share->table_name_hash_value,
7964     (uchar*) share->table_name, share->table_name_length)))
7965 #else
7966   if (!(spider_init_error_table = (SPIDER_INIT_ERROR_TABLE *) my_hash_search(
7967     &spider_init_error_tables,
7968     (uchar*) share->table_name, share->table_name_length)))
7969 #endif
7970   {
7971     if (!create)
7972     {
7973       pthread_mutex_unlock(&spider_init_error_tbl_mutex);
7974       DBUG_RETURN(NULL);
7975     }
7976     if (!(spider_init_error_table = (SPIDER_INIT_ERROR_TABLE *)
7977       spider_bulk_malloc(spider_current_trx, 54, MYF(MY_WME | MY_ZEROFILL),
7978         &spider_init_error_table, sizeof(*spider_init_error_table),
7979         &tmp_name, share->table_name_length + 1,
7980         NullS))
7981     ) {
7982       pthread_mutex_unlock(&spider_init_error_tbl_mutex);
7983       DBUG_RETURN(NULL);
7984     }
7985     memcpy(tmp_name, share->table_name, share->table_name_length);
7986     spider_init_error_table->table_name = tmp_name;
7987     spider_init_error_table->table_name_length = share->table_name_length;
7988 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
7989     spider_init_error_table->table_name_hash_value =
7990       share->table_name_hash_value;
7991 #endif
7992     uint old_elements = spider_init_error_tables.array.max_element;
7993 #ifdef HASH_UPDATE_WITH_HASH_VALUE
7994     if (my_hash_insert_with_hash_value(&spider_init_error_tables,
7995       share->table_name_hash_value, (uchar*) spider_init_error_table))
7996 #else
7997     if (my_hash_insert(&spider_init_error_tables,
7998       (uchar*) spider_init_error_table))
7999 #endif
8000     {
8001       spider_free(trx, spider_init_error_table, MYF(0));
8002       pthread_mutex_unlock(&spider_init_error_tbl_mutex);
8003       DBUG_RETURN(NULL);
8004     }
8005     if (spider_init_error_tables.array.max_element > old_elements)
8006     {
8007       spider_alloc_calc_mem(spider_current_trx,
8008         spider_init_error_tables,
8009         (spider_init_error_tables.array.max_element - old_elements) *
8010         spider_init_error_tables.array.size_of_element);
8011     }
8012   }
8013   pthread_mutex_unlock(&spider_init_error_tbl_mutex);
8014   DBUG_RETURN(spider_init_error_table);
8015 }
8016 
spider_delete_init_error_table(const char * name)8017 void spider_delete_init_error_table(
8018   const char *name
8019 ) {
8020   SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
8021   uint length = strlen(name);
8022 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
8023   my_hash_value_type hash_value = my_calc_hash(&spider_open_tables,
8024     (uchar*) name, length);
8025 #endif
8026   DBUG_ENTER("spider_delete_init_error_table");
8027   pthread_mutex_lock(&spider_init_error_tbl_mutex);
8028 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
8029   if ((spider_init_error_table = (SPIDER_INIT_ERROR_TABLE *)
8030     my_hash_search_using_hash_value(&spider_init_error_tables, hash_value,
8031       (uchar*) name, length)))
8032 #else
8033   if ((spider_init_error_table = (SPIDER_INIT_ERROR_TABLE *) my_hash_search(
8034     &spider_init_error_tables, (uchar*) name, length)))
8035 #endif
8036   {
8037 #ifdef HASH_UPDATE_WITH_HASH_VALUE
8038     my_hash_delete_with_hash_value(&spider_init_error_tables,
8039       spider_init_error_table->table_name_hash_value,
8040       (uchar*) spider_init_error_table);
8041 #else
8042     my_hash_delete(&spider_init_error_tables,
8043       (uchar*) spider_init_error_table);
8044 #endif
8045     spider_free(spider_current_trx, spider_init_error_table, MYF(0));
8046   }
8047   pthread_mutex_unlock(&spider_init_error_tbl_mutex);
8048   DBUG_VOID_RETURN;
8049 }
8050 
spider_check_pk_update(TABLE * table)8051 bool spider_check_pk_update(
8052   TABLE *table
8053 ) {
8054   int roop_count;
8055   TABLE_SHARE *table_share = table->s;
8056   KEY *key_info;
8057   KEY_PART_INFO *key_part;
8058   DBUG_ENTER("spider_check_pk_update");
8059   if (table_share->primary_key == MAX_KEY)
8060     DBUG_RETURN(FALSE);
8061 
8062   key_info = &table_share->key_info[table_share->primary_key];
8063   key_part = key_info->key_part;
8064   for (roop_count = 0;
8065     roop_count < (int) spider_user_defined_key_parts(key_info); roop_count++)
8066   {
8067     if (bitmap_is_set(table->write_set,
8068       key_part[roop_count].field->field_index))
8069       DBUG_RETURN(TRUE);
8070   }
8071   DBUG_RETURN(FALSE);
8072 }
8073 
8074 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8075 #ifdef HANDLER_HAS_DIRECT_UPDATE_ROWS
spider_check_hs_pk_update(ha_spider * spider,key_range * key)8076 bool spider_check_hs_pk_update(
8077   ha_spider *spider,
8078   key_range *key
8079 ) {
8080   uint roop_count, field_index, set_count = 0;
8081   TABLE *table = spider->get_table();
8082   TABLE_SHARE *table_share = table->s;
8083   SPIDER_SHARE *share = spider->share;
8084   KEY *key_info;
8085   KEY_PART_INFO *key_part;
8086   char buf[MAX_FIELD_WIDTH], buf2[MAX_FIELD_WIDTH];
8087   spider_string tmp_str(buf, MAX_FIELD_WIDTH, &my_charset_bin),
8088     tmp_str2(buf2, MAX_FIELD_WIDTH, &my_charset_bin);
8089   String *str, *str2;
8090   DBUG_ENTER("spider_check_hs_pk_update");
8091   tmp_str.init_calc_mem(137);
8092 
8093   if (table_share->primary_key == MAX_KEY)
8094     DBUG_RETURN(FALSE);
8095   memset(spider->tmp_column_bitmap, 0, sizeof(uchar) * share->bitmap_size);
8096   key_info = &table->key_info[table_share->primary_key];
8097   key_part = key_info->key_part;
8098   for (roop_count = 0; roop_count < spider_user_defined_key_parts(key_info);
8099     roop_count++)
8100   {
8101     field_index = key_part[roop_count].field->field_index;
8102     if (bitmap_is_set(table->write_set, field_index))
8103     {
8104       DBUG_PRINT("info", ("spider set key_part=%u field_index=%u",
8105         roop_count, field_index));
8106       spider_set_bit(spider->tmp_column_bitmap, field_index);
8107       set_count++;
8108     }
8109   }
8110   DBUG_PRINT("info", ("spider set_count=%u", set_count));
8111 
8112   Field *field;
8113   uint store_length, length, var_len;
8114   const uchar *ptr;
8115   bool key_eq;
8116   key_part_map tgt_key_part_map = key->keypart_map;
8117   key_info = &table->key_info[spider->active_index];
8118   for (
8119     key_part = key_info->key_part,
8120     length = 0;
8121     tgt_key_part_map;
8122     length += store_length,
8123     tgt_key_part_map >>= 1,
8124     key_part++
8125   ) {
8126     store_length = key_part->store_length;
8127     field = key_part->field;
8128     field_index = field->field_index;
8129     if (spider_bit_is_set(spider->tmp_column_bitmap, field_index))
8130     {
8131       ptr = key->key + length;
8132       key_eq = (tgt_key_part_map > 1);
8133       if (key_part->null_bit && *ptr++)
8134       {
8135         if (key->flag != HA_READ_KEY_EXACT || !field->is_null())
8136         {
8137           DBUG_PRINT("info", ("spider flag=%u is_null=%s",
8138             key->flag, field->is_null() ? "TRUE" : "FALSE"));
8139           DBUG_RETURN(TRUE);
8140         }
8141       } else {
8142         if (
8143           field->type() == MYSQL_TYPE_BLOB ||
8144           field->real_type() == MYSQL_TYPE_VARCHAR ||
8145           field->type() == MYSQL_TYPE_GEOMETRY
8146         ) {
8147           var_len = uint2korr(ptr);
8148           tmp_str.set_quick((char *) ptr + HA_KEY_BLOB_LENGTH, var_len,
8149             &my_charset_bin);
8150           str = tmp_str.get_str();
8151         } else {
8152           str = field->val_str(tmp_str.get_str(), ptr);
8153           tmp_str.mem_calc();
8154         }
8155         str2 = field->val_str(tmp_str2.get_str());
8156         tmp_str2.mem_calc();
8157         if (
8158           str->length() != str2->length() ||
8159           memcmp(str->ptr(), str2->ptr(), str->length())
8160         ) {
8161           DBUG_PRINT("info", ("spider length=%u %u",
8162             str->length(), str2->length()));
8163           DBUG_PRINT("info", ("spider length=%s %s",
8164             str->c_ptr_safe(), str2->c_ptr_safe()));
8165           DBUG_RETURN(TRUE);
8166         }
8167       }
8168       set_count--;
8169     }
8170   }
8171   DBUG_PRINT("info", ("spider set_count=%u", set_count));
8172   if (set_count)
8173   {
8174     DBUG_RETURN(TRUE);
8175   }
8176   DBUG_RETURN(FALSE);
8177 }
8178 #endif
8179 #endif
8180 
spider_set_tmp_share_pointer(SPIDER_SHARE * tmp_share,char ** tmp_connect_info,uint * tmp_connect_info_length,long * tmp_long,longlong * tmp_longlong)8181 void spider_set_tmp_share_pointer(
8182   SPIDER_SHARE *tmp_share,
8183   char **tmp_connect_info,
8184   uint *tmp_connect_info_length,
8185   long *tmp_long,
8186   longlong *tmp_longlong
8187 ) {
8188   DBUG_ENTER("spider_set_tmp_share_pointer");
8189   tmp_share->link_count = 1;
8190   tmp_share->all_link_count = 1;
8191   tmp_share->server_names = &tmp_connect_info[0];
8192   tmp_share->tgt_table_names = &tmp_connect_info[1];
8193   tmp_share->tgt_dbs = &tmp_connect_info[2];
8194   tmp_share->tgt_hosts = &tmp_connect_info[3];
8195   tmp_share->tgt_usernames = &tmp_connect_info[4];
8196   tmp_share->tgt_passwords = &tmp_connect_info[5];
8197   tmp_share->tgt_sockets = &tmp_connect_info[6];
8198   tmp_share->tgt_wrappers = &tmp_connect_info[7];
8199   tmp_share->tgt_ssl_cas = &tmp_connect_info[8];
8200   tmp_share->tgt_ssl_capaths = &tmp_connect_info[9];
8201   tmp_share->tgt_ssl_certs = &tmp_connect_info[10];
8202   tmp_share->tgt_ssl_ciphers = &tmp_connect_info[11];
8203   tmp_share->tgt_ssl_keys = &tmp_connect_info[12];
8204   tmp_share->tgt_default_files = &tmp_connect_info[13];
8205   tmp_share->tgt_default_groups = &tmp_connect_info[14];
8206   tmp_share->tgt_pk_names = &tmp_connect_info[15];
8207   tmp_share->tgt_sequence_names = &tmp_connect_info[16];
8208   tmp_share->static_link_ids = &tmp_connect_info[17];
8209 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8210   tmp_share->hs_read_socks = &tmp_connect_info[18];
8211   tmp_share->hs_write_socks = &tmp_connect_info[19];
8212 #endif
8213   tmp_share->tgt_ports = &tmp_long[0];
8214   tmp_share->tgt_ssl_vscs = &tmp_long[1];
8215   tmp_share->link_statuses = &tmp_long[2];
8216   tmp_share->monitoring_binlog_pos_at_failing = &tmp_long[3];
8217   tmp_share->monitoring_flag = &tmp_long[4];
8218   tmp_share->monitoring_kind = &tmp_long[5];
8219 #ifndef WITHOUT_SPIDER_BG_SEARCH
8220   tmp_share->monitoring_bg_flag = &tmp_long[6];
8221   tmp_share->monitoring_bg_kind = &tmp_long[7];
8222 #endif
8223 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8224   tmp_share->use_hs_reads = &tmp_long[8];
8225   tmp_share->use_hs_writes = &tmp_long[9];
8226   tmp_share->hs_read_ports = &tmp_long[10];
8227   tmp_share->hs_write_ports = &tmp_long[11];
8228   tmp_share->hs_write_to_reads = &tmp_long[12];
8229 #endif
8230   tmp_share->use_handlers = &tmp_long[13];
8231   tmp_share->connect_timeouts = &tmp_long[14];
8232   tmp_long[13] = -1;
8233   tmp_share->net_read_timeouts = &tmp_long[15];
8234   tmp_long[14] = -1;
8235   tmp_share->net_write_timeouts = &tmp_long[16];
8236   tmp_long[15] = -1;
8237   tmp_share->access_balances = &tmp_long[17];
8238   tmp_share->bka_table_name_types = &tmp_long[18];
8239   tmp_share->monitoring_limit = &tmp_longlong[0];
8240   tmp_share->monitoring_sid = &tmp_longlong[1];
8241 #ifndef WITHOUT_SPIDER_BG_SEARCH
8242   tmp_share->monitoring_bg_interval = &tmp_longlong[2];
8243 #endif
8244   tmp_share->server_names_lengths = &tmp_connect_info_length[0];
8245   tmp_share->tgt_table_names_lengths = &tmp_connect_info_length[1];
8246   tmp_share->tgt_dbs_lengths = &tmp_connect_info_length[2];
8247   tmp_share->tgt_hosts_lengths = &tmp_connect_info_length[3];
8248   tmp_share->tgt_usernames_lengths = &tmp_connect_info_length[4];
8249   tmp_share->tgt_passwords_lengths = &tmp_connect_info_length[5];
8250   tmp_share->tgt_sockets_lengths = &tmp_connect_info_length[6];
8251   tmp_share->tgt_wrappers_lengths = &tmp_connect_info_length[7];
8252   tmp_share->tgt_ssl_cas_lengths = &tmp_connect_info_length[8];
8253   tmp_share->tgt_ssl_capaths_lengths = &tmp_connect_info_length[9];
8254   tmp_share->tgt_ssl_certs_lengths = &tmp_connect_info_length[10];
8255   tmp_share->tgt_ssl_ciphers_lengths = &tmp_connect_info_length[11];
8256   tmp_share->tgt_ssl_keys_lengths = &tmp_connect_info_length[12];
8257   tmp_share->tgt_default_files_lengths = &tmp_connect_info_length[13];
8258   tmp_share->tgt_default_groups_lengths = &tmp_connect_info_length[14];
8259   tmp_share->tgt_pk_names_lengths = &tmp_connect_info_length[15];
8260   tmp_share->tgt_sequence_names_lengths = &tmp_connect_info_length[16];
8261   tmp_share->static_link_ids_lengths = &tmp_connect_info_length[17];
8262 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8263   tmp_share->hs_read_socks_lengths = &tmp_connect_info_length[18];
8264   tmp_share->hs_write_socks_lengths = &tmp_connect_info_length[19];
8265 #endif
8266   tmp_share->server_names_length = 1;
8267   tmp_share->tgt_table_names_length = 1;
8268   tmp_share->tgt_dbs_length = 1;
8269   tmp_share->tgt_hosts_length = 1;
8270   tmp_share->tgt_usernames_length = 1;
8271   tmp_share->tgt_passwords_length = 1;
8272   tmp_share->tgt_sockets_length = 1;
8273   tmp_share->tgt_wrappers_length = 1;
8274   tmp_share->tgt_ssl_cas_length = 1;
8275   tmp_share->tgt_ssl_capaths_length = 1;
8276   tmp_share->tgt_ssl_certs_length = 1;
8277   tmp_share->tgt_ssl_ciphers_length = 1;
8278   tmp_share->tgt_ssl_keys_length = 1;
8279   tmp_share->tgt_default_files_length = 1;
8280   tmp_share->tgt_default_groups_length = 1;
8281   tmp_share->tgt_pk_names_length = 1;
8282   tmp_share->tgt_sequence_names_length = 1;
8283   tmp_share->static_link_ids_length = 1;
8284   tmp_share->tgt_ports_length = 1;
8285   tmp_share->tgt_ssl_vscs_length = 1;
8286   tmp_share->link_statuses_length = 1;
8287   tmp_share->monitoring_binlog_pos_at_failing_length = 1;
8288   tmp_share->monitoring_flag_length = 1;
8289   tmp_share->monitoring_kind_length = 1;
8290 #ifndef WITHOUT_SPIDER_BG_SEARCH
8291   tmp_share->monitoring_bg_flag_length = 1;
8292   tmp_share->monitoring_bg_kind_length = 1;
8293 #endif
8294   tmp_share->monitoring_limit_length = 1;
8295   tmp_share->monitoring_sid_length = 1;
8296 #ifndef WITHOUT_SPIDER_BG_SEARCH
8297   tmp_share->monitoring_bg_interval_length = 1;
8298 #endif
8299 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8300   tmp_share->hs_read_socks_length = 1;
8301   tmp_share->hs_write_socks_length = 1;
8302   tmp_share->use_hs_reads_length = 1;
8303   tmp_share->use_hs_writes_length = 1;
8304   tmp_share->hs_read_ports_length = 1;
8305   tmp_share->hs_write_ports_length = 1;
8306   tmp_share->hs_write_to_reads_length = 1;
8307 #endif
8308   tmp_share->use_handlers_length = 1;
8309   tmp_share->connect_timeouts_length = 1;
8310   tmp_share->net_read_timeouts_length = 1;
8311   tmp_share->net_write_timeouts_length = 1;
8312   tmp_share->access_balances_length = 1;
8313   tmp_share->bka_table_name_types_length = 1;
8314 
8315 #ifndef WITHOUT_SPIDER_BG_SEARCH
8316   tmp_share->monitoring_bg_flag[0] = -1;
8317   tmp_share->monitoring_bg_kind[0] = -1;
8318 #endif
8319   tmp_share->monitoring_binlog_pos_at_failing[0] = -1;
8320   tmp_share->monitoring_flag[0] = -1;
8321   tmp_share->monitoring_kind[0] = -1;
8322 #ifndef WITHOUT_SPIDER_BG_SEARCH
8323   tmp_share->monitoring_bg_interval[0] = -1;
8324 #endif
8325   tmp_share->monitoring_limit[0] = -1;
8326   tmp_share->monitoring_sid[0] = -1;
8327   tmp_share->bka_engine = NULL;
8328   tmp_share->use_dbton_count = 0;
8329 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
8330   tmp_share->use_hs_dbton_count = 0;
8331 #endif
8332   DBUG_VOID_RETURN;
8333 }
8334 
spider_create_tmp_dbton_share(SPIDER_SHARE * tmp_share)8335 int spider_create_tmp_dbton_share(
8336   SPIDER_SHARE *tmp_share
8337 ) {
8338   int error_num;
8339   uint dbton_id = tmp_share->use_dbton_ids[0];
8340   DBUG_ENTER("spider_create_tmp_dbton_share");
8341   if (!(tmp_share->dbton_share[dbton_id] =
8342     spider_dbton[dbton_id].create_db_share(tmp_share)))
8343   {
8344     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8345   }
8346   if ((error_num = tmp_share->dbton_share[dbton_id]->init()))
8347   {
8348     delete tmp_share->dbton_share[dbton_id];
8349     tmp_share->dbton_share[dbton_id] = NULL;
8350     DBUG_RETURN(error_num);
8351   }
8352   DBUG_RETURN(0);
8353 }
8354 
spider_free_tmp_dbton_share(SPIDER_SHARE * tmp_share)8355 void spider_free_tmp_dbton_share(
8356   SPIDER_SHARE *tmp_share
8357 ) {
8358   uint dbton_id = tmp_share->use_dbton_ids[0];
8359   DBUG_ENTER("spider_free_tmp_dbton_share");
8360   if (tmp_share->dbton_share[dbton_id])
8361   {
8362     delete tmp_share->dbton_share[dbton_id];
8363     tmp_share->dbton_share[dbton_id] = NULL;
8364   }
8365   DBUG_VOID_RETURN;
8366 }
8367 
spider_create_tmp_dbton_handler(ha_spider * tmp_spider)8368 int spider_create_tmp_dbton_handler(
8369   ha_spider *tmp_spider
8370 ) {
8371   int error_num;
8372   SPIDER_SHARE *tmp_share = tmp_spider->share;
8373   uint dbton_id = tmp_share->use_dbton_ids[0];
8374   DBUG_ENTER("spider_create_tmp_dbton_handler");
8375   if (!(tmp_spider->dbton_handler[dbton_id] =
8376     spider_dbton[dbton_id].create_db_handler(tmp_spider,
8377     tmp_share->dbton_share[dbton_id])))
8378   {
8379     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
8380   }
8381   if ((error_num = tmp_spider->dbton_handler[dbton_id]->init()))
8382   {
8383     delete tmp_spider->dbton_handler[dbton_id];
8384     tmp_spider->dbton_handler[dbton_id] = NULL;
8385     DBUG_RETURN(error_num);
8386   }
8387   DBUG_RETURN(0);
8388 }
8389 
spider_free_tmp_dbton_handler(ha_spider * tmp_spider)8390 void spider_free_tmp_dbton_handler(
8391   ha_spider *tmp_spider
8392 ) {
8393   SPIDER_SHARE *tmp_share = tmp_spider->share;
8394   uint dbton_id = tmp_share->use_dbton_ids[0];
8395   DBUG_ENTER("spider_create_tmp_dbton_handler");
8396   if (tmp_spider->dbton_handler[dbton_id])
8397   {
8398     delete tmp_spider->dbton_handler[dbton_id];
8399     tmp_spider->dbton_handler[dbton_id] = NULL;
8400   }
8401   DBUG_VOID_RETURN;
8402 }
8403 
spider_get_parent_table_list(ha_spider * spider)8404 TABLE_LIST *spider_get_parent_table_list(
8405   ha_spider *spider
8406 ) {
8407   TABLE *table = spider->get_table();
8408   TABLE_LIST *table_list = table->pos_in_table_list;
8409   DBUG_ENTER("spider_get_parent_table_list");
8410   if (table_list)
8411   {
8412     while (table_list->parent_l)
8413       table_list = table_list->parent_l;
8414     DBUG_RETURN(table_list);
8415   }
8416   DBUG_RETURN(NULL);
8417 }
8418 
spider_get_index_hints(ha_spider * spider)8419 List<Index_hint> *spider_get_index_hints(
8420   ha_spider *spider
8421   ) {
8422     TABLE_LIST *table_list = spider_get_parent_table_list(spider);
8423     DBUG_ENTER("spider_get_index_hint");
8424     if (table_list)
8425     {
8426       DBUG_RETURN(table_list->index_hints);
8427     }
8428     DBUG_RETURN(NULL);
8429 }
8430 
8431 
spider_get_select_lex(ha_spider * spider)8432 st_select_lex *spider_get_select_lex(
8433   ha_spider *spider
8434 ) {
8435   TABLE_LIST *table_list = spider_get_parent_table_list(spider);
8436   DBUG_ENTER("spider_get_select_lex");
8437   if (table_list)
8438   {
8439     DBUG_RETURN(table_list->select_lex);
8440   }
8441   DBUG_RETURN(NULL);
8442 }
8443 
spider_get_select_limit_from_select_lex(st_select_lex * select_lex,longlong * select_limit,longlong * offset_limit)8444 void spider_get_select_limit_from_select_lex(
8445   st_select_lex *select_lex,
8446   longlong *select_limit,
8447   longlong *offset_limit
8448 ) {
8449   DBUG_ENTER("spider_get_select_limit_from_select_lex");
8450   *select_limit = 9223372036854775807LL;
8451   *offset_limit = 0;
8452   if (select_lex && select_lex->explicit_limit)
8453   {
8454     *select_limit = select_lex->select_limit ?
8455       select_lex->select_limit->val_int() : 0;
8456     *offset_limit = select_lex->offset_limit ?
8457       select_lex->offset_limit->val_int() : 0;
8458   }
8459   DBUG_VOID_RETURN;
8460 }
8461 
spider_get_select_limit(ha_spider * spider,st_select_lex ** select_lex,longlong * select_limit,longlong * offset_limit)8462 void spider_get_select_limit(
8463   ha_spider *spider,
8464   st_select_lex **select_lex,
8465   longlong *select_limit,
8466   longlong *offset_limit
8467 ) {
8468   DBUG_ENTER("spider_get_select_limit");
8469   *select_lex = spider_get_select_lex(spider);
8470   spider_get_select_limit_from_select_lex(
8471     *select_lex, select_limit, offset_limit);
8472   DBUG_VOID_RETURN;
8473 }
8474 
spider_split_read_param(ha_spider * spider)8475 longlong spider_split_read_param(
8476   ha_spider *spider
8477 ) {
8478   SPIDER_SHARE *share = spider->share;
8479   SPIDER_RESULT_LIST *result_list = &spider->result_list;
8480   THD *thd = spider->trx->thd;
8481   st_select_lex *select_lex;
8482   longlong select_limit;
8483   longlong offset_limit;
8484   double semi_split_read;
8485   longlong split_read;
8486   DBUG_ENTER("spider_split_read_param");
8487   result_list->set_split_read_count = 1;
8488 #ifdef INFO_KIND_FORCE_LIMIT_BEGIN
8489   if (spider->info_limit < 9223372036854775807LL)
8490   {
8491     DBUG_PRINT("info",("spider info_limit=%lld", spider->info_limit));
8492     longlong info_limit = spider->info_limit;
8493     result_list->split_read_base = info_limit;
8494     result_list->semi_split_read = 0;
8495     result_list->first_read = info_limit;
8496     result_list->second_read = info_limit;
8497     result_list->semi_split_read_base = 0;
8498     result_list->set_split_read = FALSE;
8499     DBUG_RETURN(info_limit);
8500   }
8501 #endif
8502   if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
8503   {
8504     DBUG_RETURN(result_list->semi_split_read_base);
8505   }
8506   spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
8507   DBUG_PRINT("info",("spider result_list->set_split_read=%s", result_list->set_split_read ? "TRUE" : "FALSE"));
8508   if (!result_list->set_split_read)
8509   {
8510     int bulk_update_mode = spider_param_bulk_update_mode(thd,
8511       share->bulk_update_mode);
8512     DBUG_PRINT("info",("spider sql_command=%u", spider->sql_command));
8513     DBUG_PRINT("info",("spider bulk_update_mode=%d", bulk_update_mode));
8514     DBUG_PRINT("info",("spider support_bulk_update_sql=%s",
8515       spider->support_bulk_update_sql() ? "TRUE" : "FALSE"));
8516 #ifdef SPIDER_HAS_GROUP_BY_HANDLER
8517     bool inserting =
8518       (
8519 #ifdef HS_HAS_SQLCOM
8520         spider->sql_command == SQLCOM_HS_INSERT ||
8521 #endif
8522         spider->sql_command == SQLCOM_INSERT ||
8523         spider->sql_command == SQLCOM_INSERT_SELECT
8524       );
8525 #endif
8526     bool updating =
8527       (
8528 #ifdef HS_HAS_SQLCOM
8529         spider->sql_command == SQLCOM_HS_UPDATE ||
8530 #endif
8531         spider->sql_command == SQLCOM_UPDATE ||
8532         spider->sql_command == SQLCOM_UPDATE_MULTI
8533       );
8534     bool deleting =
8535       (
8536 #ifdef HS_HAS_SQLCOM
8537         spider->sql_command == SQLCOM_HS_DELETE ||
8538 #endif
8539         spider->sql_command == SQLCOM_DELETE ||
8540         spider->sql_command == SQLCOM_DELETE_MULTI
8541       );
8542     bool replacing =
8543       (
8544         spider->sql_command == SQLCOM_REPLACE ||
8545         spider->sql_command == SQLCOM_REPLACE_SELECT
8546       );
8547     DBUG_PRINT("info",("spider updating=%s", updating ? "TRUE" : "FALSE"));
8548     DBUG_PRINT("info",("spider deleting=%s", deleting ? "TRUE" : "FALSE"));
8549     DBUG_PRINT("info",("spider replacing=%s", replacing ? "TRUE" : "FALSE"));
8550     TABLE *table = spider->get_table();
8551     if (
8552 #ifdef SPIDER_HAS_GROUP_BY_HANDLER
8553       (
8554         inserting &&
8555         spider->use_fields
8556       ) ||
8557 #endif
8558       replacing ||
8559       (
8560         (
8561           updating ||
8562           deleting
8563         ) &&
8564         (
8565           bulk_update_mode != 2 ||
8566           !spider->support_bulk_update_sql() ||
8567           (
8568             updating &&
8569             table->triggers &&
8570 #ifdef HA_CAN_FORCE_BULK_UPDATE
8571             !(table->file->ha_table_flags() & HA_CAN_FORCE_BULK_UPDATE) &&
8572 #endif
8573             table->triggers->has_triggers(TRG_EVENT_UPDATE, TRG_ACTION_AFTER)
8574           ) ||
8575           (
8576             deleting &&
8577             table->triggers &&
8578 #ifdef HA_CAN_FORCE_BULK_DELETE
8579             !(table->file->ha_table_flags() & HA_CAN_FORCE_BULK_DELETE) &&
8580 #endif
8581             table->triggers->has_triggers(TRG_EVENT_DELETE, TRG_ACTION_AFTER)
8582           )
8583         )
8584       )
8585     ) {
8586       /* This case must select by one shot */
8587       DBUG_PRINT("info",("spider cancel split read"));
8588       result_list->split_read_base = 9223372036854775807LL;
8589       result_list->semi_split_read = 0;
8590       result_list->semi_split_read_limit = 9223372036854775807LL;
8591       result_list->first_read = 9223372036854775807LL;
8592       result_list->second_read = 9223372036854775807LL;
8593       result_list->semi_split_read_base = 0;
8594       result_list->set_split_read = TRUE;
8595       DBUG_RETURN(9223372036854775807LL);
8596     }
8597 #ifdef SPIDER_HAS_EXPLAIN_QUERY
8598     Explain_query *explain = thd->lex->explain;
8599     bool filesort = FALSE;
8600     if (explain)
8601     {
8602       DBUG_PRINT("info",("spider explain=%p", explain));
8603       Explain_select *explain_select = NULL;
8604       if (select_lex)
8605       {
8606         DBUG_PRINT("info",("spider select_lex=%p", select_lex));
8607         DBUG_PRINT("info",("spider select_number=%u",
8608           select_lex->select_number));
8609         explain_select =
8610           explain->get_select(select_lex->select_number);
8611       }
8612       if (explain_select)
8613       {
8614         DBUG_PRINT("info",("spider explain_select=%p", explain_select));
8615         if (explain_select->using_filesort)
8616         {
8617           DBUG_PRINT("info",("spider using filesort"));
8618           filesort = TRUE;
8619         }
8620       }
8621     }
8622 #endif
8623     result_list->split_read_base =
8624       spider_param_split_read(thd, share->split_read);
8625 #ifdef SPIDER_HAS_EXPLAIN_QUERY
8626     if (filesort)
8627     {
8628       result_list->semi_split_read = 0;
8629       result_list->semi_split_read_limit = 9223372036854775807LL;
8630     } else {
8631 #endif
8632       result_list->semi_split_read =
8633         spider_param_semi_split_read(thd, share->semi_split_read);
8634       result_list->semi_split_read_limit =
8635         spider_param_semi_split_read_limit(thd, share->semi_split_read_limit);
8636 #ifdef SPIDER_HAS_EXPLAIN_QUERY
8637     }
8638 #endif
8639     result_list->first_read =
8640       spider_param_first_read(thd, share->first_read);
8641     result_list->second_read =
8642       spider_param_second_read(thd, share->second_read);
8643     result_list->semi_split_read_base = 0;
8644     result_list->set_split_read = TRUE;
8645   }
8646   DBUG_PRINT("info",("spider result_list->semi_split_read=%f", result_list->semi_split_read));
8647   DBUG_PRINT("info",("spider select_lex->explicit_limit=%d", select_lex ? select_lex->explicit_limit : 0));
8648   DBUG_PRINT("info",("spider OPTION_FOUND_ROWS=%s", select_lex && (select_lex->options & OPTION_FOUND_ROWS) ? "TRUE" : "FALSE"));
8649   DBUG_PRINT("info",("spider select_lex->group_list.elements=%u", select_lex ? select_lex->group_list.elements : 0));
8650   DBUG_PRINT("info",("spider select_lex->with_sum_func=%s", select_lex && select_lex->with_sum_func ? "TRUE" : "FALSE"));
8651   if (
8652     result_list->semi_split_read > 0 &&
8653     select_lex && select_lex->explicit_limit &&
8654     !(select_lex->options & OPTION_FOUND_ROWS) &&
8655     !select_lex->group_list.elements &&
8656     !select_lex->with_sum_func
8657   ) {
8658     semi_split_read = result_list->semi_split_read *
8659       (select_limit + offset_limit);
8660     DBUG_PRINT("info",("spider semi_split_read=%f", semi_split_read));
8661     if (semi_split_read >= result_list->semi_split_read_limit)
8662     {
8663       result_list->semi_split_read_base = result_list->semi_split_read_limit;
8664       DBUG_RETURN(result_list->semi_split_read_limit);
8665     } else {
8666       split_read = (longlong) semi_split_read;
8667       if (split_read < 0)
8668       {
8669         result_list->semi_split_read_base = result_list->semi_split_read_limit;
8670         DBUG_RETURN(result_list->semi_split_read_limit);
8671       } else if (split_read == 0)
8672       {
8673         result_list->semi_split_read_base = 1;
8674         DBUG_RETURN(1);
8675       } else {
8676         result_list->semi_split_read_base = split_read;
8677         DBUG_RETURN(split_read);
8678       }
8679     }
8680   } else if (result_list->first_read > 0)
8681     DBUG_RETURN(result_list->first_read);
8682   DBUG_RETURN(result_list->split_read_base);
8683 }
8684 
spider_bg_split_read_param(ha_spider * spider)8685 longlong spider_bg_split_read_param(
8686   ha_spider *spider
8687 ) {
8688   SPIDER_RESULT_LIST *result_list = &spider->result_list;
8689   DBUG_ENTER("spider_bg_split_read_param");
8690   if (result_list->semi_split_read_base)
8691     DBUG_RETURN(result_list->semi_split_read_base);
8692   DBUG_RETURN(result_list->split_read_base);
8693 }
8694 
spider_first_split_read_param(ha_spider * spider)8695 void spider_first_split_read_param(
8696   ha_spider *spider
8697 ) {
8698   SPIDER_RESULT_LIST *result_list = &spider->result_list;
8699   DBUG_ENTER("spider_first_split_read_param");
8700   if (result_list->semi_split_read_base)
8701     result_list->split_read = result_list->semi_split_read_base;
8702   else if (result_list->second_read > 0)
8703     result_list->split_read = result_list->first_read;
8704   else
8705     result_list->split_read = result_list->split_read_base;
8706   result_list->set_split_read_count = 1;
8707   DBUG_VOID_RETURN;
8708 }
8709 
spider_next_split_read_param(ha_spider * spider)8710 void spider_next_split_read_param(
8711   ha_spider *spider
8712 ) {
8713   SPIDER_RESULT_LIST *result_list = &spider->result_list;
8714   DBUG_ENTER("spider_next_split_read_param");
8715   if (result_list->semi_split_read_base)
8716     result_list->split_read = result_list->semi_split_read_base;
8717   else if (
8718     result_list->set_split_read_count == 1 &&
8719     result_list->second_read > 0
8720   )
8721     result_list->split_read = result_list->second_read;
8722   else
8723     result_list->split_read = result_list->split_read_base;
8724   result_list->set_split_read_count++;
8725   DBUG_VOID_RETURN;
8726 }
8727 
spider_check_direct_order_limit(ha_spider * spider)8728 bool spider_check_direct_order_limit(
8729   ha_spider *spider
8730 ) {
8731   THD *thd = spider->trx->thd;
8732   SPIDER_SHARE *share = spider->share;
8733   st_select_lex *select_lex;
8734   longlong select_limit;
8735   longlong offset_limit;
8736   DBUG_ENTER("spider_check_direct_order_limit");
8737   if (spider_check_index_merge(spider->get_top_table(),
8738     spider_get_select_lex(spider)))
8739   {
8740     DBUG_PRINT("info",("spider set use_index_merge"));
8741     spider->use_index_merge = TRUE;
8742   }
8743   DBUG_PRINT("info",("spider SQLCOM_HA_READ=%s",
8744     (spider->sql_command == SQLCOM_HA_READ) ? "TRUE" : "FALSE"));
8745   DBUG_PRINT("info",("spider sql_kinds with SPIDER_SQL_KIND_HANDLER=%s",
8746     (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER) ? "TRUE" : "FALSE"));
8747   DBUG_PRINT("info",("spider use_index_merge=%s",
8748     spider->use_index_merge ? "TRUE" : "FALSE"));
8749   DBUG_PRINT("info",("spider is_clone=%s",
8750     spider->is_clone ? "TRUE" : "FALSE"));
8751 #ifdef HA_CAN_BULK_ACCESS
8752   DBUG_PRINT("info",("spider is_bulk_access_clone=%s",
8753     spider->is_bulk_access_clone ? "TRUE" : "FALSE"));
8754 #endif
8755   if (
8756     spider->sql_command != SQLCOM_HA_READ &&
8757     !spider->use_index_merge &&
8758 #ifdef HA_CAN_BULK_ACCESS
8759     (!spider->is_clone || spider->is_bulk_access_clone)
8760 #else
8761     !spider->is_clone
8762 #endif
8763   ) {
8764     spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
8765     bool first_check = TRUE;
8766     DBUG_PRINT("info",("spider select_lex=%p", select_lex));
8767 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
8768     DBUG_PRINT("info",("spider leaf_tables.elements=%u",
8769       select_lex ? select_lex->leaf_tables.elements : 0));
8770 #endif
8771 
8772     if (select_lex && (select_lex->options & SELECT_DISTINCT))
8773     {
8774       DBUG_PRINT("info",("spider with distinct"));
8775       spider->result_list.direct_distinct = TRUE;
8776     }
8777 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8778     spider->result_list.direct_aggregate = TRUE;
8779 #endif
8780     DBUG_PRINT("info",("spider select_limit=%lld", select_limit));
8781     DBUG_PRINT("info",("spider offset_limit=%lld", offset_limit));
8782     if (
8783 #if MYSQL_VERSION_ID < 50500
8784       !thd->variables.engine_condition_pushdown ||
8785 #else
8786 #ifdef SPIDER_ENGINE_CONDITION_PUSHDOWN_IS_ALWAYS_ON
8787 #else
8788       !(thd->variables.optimizer_switch &
8789         OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN) ||
8790 #endif
8791 #endif
8792 #ifdef SPIDER_NEED_CHECK_CONDITION_AT_CHECKING_DIRECT_ORDER_LIMIT
8793       !spider->condition ||
8794 #endif
8795       !select_lex ||
8796 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
8797       select_lex->leaf_tables.elements != 1 ||
8798 #endif
8799       select_lex->table_list.elements != 1
8800     ) {
8801       DBUG_PRINT("info",("spider first_check is FALSE"));
8802       first_check = FALSE;
8803       spider->result_list.direct_distinct = FALSE;
8804 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8805       spider->result_list.direct_aggregate = FALSE;
8806 #endif
8807     } else if (spider_db_append_condition(spider, NULL, 0, TRUE))
8808     {
8809       DBUG_PRINT("info",("spider FALSE by condition"));
8810       first_check = FALSE;
8811       spider->result_list.direct_distinct = FALSE;
8812 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8813       spider->result_list.direct_aggregate = FALSE;
8814 #endif
8815     } else if (spider->sql_kinds & SPIDER_SQL_KIND_HANDLER)
8816     {
8817       DBUG_PRINT("info",("spider sql_kinds with SPIDER_SQL_KIND_HANDLER"));
8818       spider->result_list.direct_distinct = FALSE;
8819 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8820       spider->result_list.direct_aggregate = FALSE;
8821     } else if (
8822       !select_lex->group_list.elements &&
8823       !select_lex->with_sum_func
8824     ) {
8825       DBUG_PRINT("info",("spider this SQL is not aggregate SQL"));
8826       spider->result_list.direct_aggregate = FALSE;
8827     } else {
8828       ORDER *group;
8829       for (group = (ORDER *) select_lex->group_list.first; group;
8830         group = group->next)
8831       {
8832         if (spider->print_item_type((*group->item), NULL, NULL, 0))
8833         {
8834           DBUG_PRINT("info",("spider aggregate FALSE by group"));
8835           spider->result_list.direct_aggregate = FALSE;
8836           break;
8837         }
8838       }
8839       JOIN *join = select_lex->join;
8840       Item_sum **item_sum_ptr;
8841       for (item_sum_ptr = join->sum_funcs; *item_sum_ptr; ++item_sum_ptr)
8842       {
8843         if (spider->print_item_type(*item_sum_ptr, NULL, NULL, 0))
8844         {
8845           DBUG_PRINT("info",("spider aggregate FALSE by not supported"));
8846           spider->result_list.direct_aggregate = FALSE;
8847           break;
8848         }
8849       }
8850 #endif
8851     }
8852 
8853     longlong direct_order_limit = spider_param_direct_order_limit(thd,
8854       share->direct_order_limit);
8855     DBUG_PRINT("info",("spider direct_order_limit=%lld", direct_order_limit));
8856     if (direct_order_limit)
8857     {
8858       DBUG_PRINT("info",("spider first_check=%s",
8859         first_check ? "TRUE" : "FALSE"));
8860       DBUG_PRINT("info",("spider (select_lex->options & OPTION_FOUND_ROWS)=%s",
8861         select_lex && (select_lex->options & OPTION_FOUND_ROWS) ? "TRUE" : "FALSE"));
8862 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8863       DBUG_PRINT("info",("spider direct_aggregate=%s",
8864         spider->result_list.direct_aggregate ? "TRUE" : "FALSE"));
8865 #endif
8866       DBUG_PRINT("info",("spider select_lex->group_list.elements=%u",
8867         select_lex ? select_lex->group_list.elements : 0));
8868       DBUG_PRINT("info",("spider select_lex->with_sum_func=%s",
8869         select_lex && select_lex->with_sum_func ? "TRUE" : "FALSE"));
8870       DBUG_PRINT("info",("spider select_lex->having=%s",
8871         select_lex && select_lex->having ? "TRUE" : "FALSE"));
8872       DBUG_PRINT("info",("spider select_lex->order_list.elements=%u",
8873         select_lex ? select_lex->order_list.elements : 0));
8874       if (
8875         !first_check ||
8876         !select_lex->explicit_limit ||
8877         (select_lex->options & OPTION_FOUND_ROWS) ||
8878         (
8879 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8880           !spider->result_list.direct_aggregate &&
8881 #endif
8882           (
8883             select_lex->group_list.elements ||
8884             select_lex->with_sum_func
8885           )
8886         ) ||
8887         select_lex->having ||
8888         !select_lex->order_list.elements ||
8889         select_limit > direct_order_limit - offset_limit
8890       ) {
8891         DBUG_PRINT("info",("spider FALSE by select_lex"));
8892         DBUG_RETURN(FALSE);
8893       }
8894       ORDER *order;
8895       for (order = (ORDER *) select_lex->order_list.first; order;
8896         order = order->next)
8897       {
8898         if (spider->print_item_type((*order->item), NULL, NULL, 0))
8899         {
8900           DBUG_PRINT("info",("spider FALSE by order"));
8901           DBUG_RETURN(FALSE);
8902         }
8903       }
8904       DBUG_PRINT("info",("spider TRUE"));
8905       spider->result_list.internal_limit = select_limit + offset_limit;
8906       spider->result_list.split_read = select_limit + offset_limit;
8907       spider->trx->direct_order_limit_count++;
8908       DBUG_RETURN(TRUE);
8909     }
8910   }
8911   DBUG_PRINT("info",("spider FALSE by parameter"));
8912   DBUG_RETURN(FALSE);
8913 }
8914 
spider_set_direct_limit_offset(ha_spider * spider)8915 int spider_set_direct_limit_offset(
8916   ha_spider *spider
8917 ) {
8918 #ifndef SPIDER_ENGINE_CONDITION_PUSHDOWN_IS_ALWAYS_ON
8919   THD *thd = spider->trx->thd;
8920 #endif
8921   st_select_lex *select_lex;
8922   longlong select_limit;
8923   longlong offset_limit;
8924   TABLE_LIST *table_list;
8925   DBUG_ENTER("spider_set_direct_limit_offset");
8926 
8927   if (spider->result_list.direct_limit_offset)
8928     DBUG_RETURN(TRUE);
8929 
8930   if (
8931     spider->pt_handler_share_creator &&
8932     spider->pt_handler_share_creator != spider
8933   ) {
8934     if (spider->pt_handler_share_creator->result_list.direct_limit_offset == TRUE)
8935     {
8936       spider->result_list.direct_limit_offset = TRUE;
8937       DBUG_RETURN(TRUE);
8938     } else {
8939       DBUG_RETURN(FALSE);
8940     }
8941   }
8942 
8943   if (
8944     spider->sql_command != SQLCOM_SELECT ||
8945 #ifdef HANDLER_HAS_DIRECT_AGGREGATE
8946     spider->result_list.direct_aggregate ||
8947 #endif
8948     spider->result_list.direct_order_limit ||
8949     spider->prev_index_rnd_init != SPD_RND    // must be RND_INIT and not be INDEX_INIT
8950   )
8951     DBUG_RETURN(FALSE);
8952 
8953   spider_get_select_limit(spider, &select_lex, &select_limit, &offset_limit);
8954 
8955   // limit and offset is non-zero
8956   if (!(select_limit && offset_limit))
8957     DBUG_RETURN(FALSE);
8958 
8959   // more than one table
8960   if (
8961     !select_lex ||
8962     select_lex->table_list.elements != 1
8963   )
8964     DBUG_RETURN(FALSE);
8965 
8966   table_list = (TABLE_LIST *) select_lex->table_list.first;
8967   if (table_list->table->file->partition_ht() != spider_hton_ptr)
8968   {
8969     DBUG_PRINT("info",("spider ht1=%u ht2=%u",
8970       table_list->table->file->partition_ht()->slot,
8971       spider_hton_ptr->slot
8972     ));
8973     DBUG_RETURN(FALSE);
8974   }
8975 
8976   // contain where
8977   if (
8978 #if MYSQL_VERSION_ID < 50500
8979     !thd->variables.engine_condition_pushdown ||
8980 #else
8981 #ifdef SPIDER_ENGINE_CONDITION_PUSHDOWN_IS_ALWAYS_ON
8982 #else
8983     !(thd->variables.optimizer_switch &
8984       OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN) ||
8985 #endif
8986 #endif
8987     spider->condition   // conditions is null may be no where condition in rand_init
8988   )
8989     DBUG_RETURN(FALSE);
8990 
8991   // ignore condition like 1=1
8992 #ifdef SPIDER_has_Item_with_subquery
8993   if (select_lex->where && select_lex->where->with_subquery())
8994 #else
8995   if (select_lex->where && select_lex->where->with_subselect)
8996 #endif
8997     DBUG_RETURN(FALSE);
8998 
8999   if (
9000     select_lex->group_list.elements ||
9001     select_lex->with_sum_func ||
9002     select_lex->having ||
9003     select_lex->order_list.elements
9004   )
9005     DBUG_RETURN(FALSE);
9006 
9007   // must not be derived table
9008   if (SPIDER_get_linkage(select_lex) == DERIVED_TABLE_TYPE)
9009     DBUG_RETURN(FALSE);
9010 
9011   spider->direct_select_offset = offset_limit;
9012   spider->direct_current_offset = offset_limit;
9013   spider->direct_select_limit = select_limit;
9014   spider->result_list.direct_limit_offset = TRUE;
9015   DBUG_RETURN(TRUE);
9016 }
9017 
9018 
spider_check_index_merge(TABLE * table,st_select_lex * select_lex)9019 bool spider_check_index_merge(
9020   TABLE *table,
9021   st_select_lex *select_lex
9022 ) {
9023   uint roop_count;
9024   JOIN *join;
9025   DBUG_ENTER("spider_check_index_merge");
9026   if (!select_lex)
9027   {
9028     DBUG_PRINT("info",("spider select_lex is null"));
9029     DBUG_RETURN(FALSE);
9030   }
9031   join = select_lex->join;
9032   if (!join)
9033   {
9034     DBUG_PRINT("info",("spider join is null"));
9035     DBUG_RETURN(FALSE);
9036   }
9037   if (!join->join_tab)
9038   {
9039     DBUG_PRINT("info",("spider join->join_tab is null"));
9040     DBUG_RETURN(FALSE);
9041   }
9042   for (roop_count = 0; roop_count < spider_join_table_count(join); ++roop_count)
9043   {
9044     JOIN_TAB *join_tab = &join->join_tab[roop_count];
9045     if (join_tab->table == table)
9046     {
9047       DBUG_PRINT("info",("spider join_tab->type=%u", join_tab->type));
9048       if (
9049 #ifdef SPIDER_HAS_JT_HASH_INDEX_MERGE
9050         join_tab->type == JT_HASH_INDEX_MERGE ||
9051 #endif
9052         join_tab->type == JT_INDEX_MERGE
9053       ) {
9054         DBUG_RETURN(TRUE);
9055       }
9056 /*
9057       DBUG_PRINT("info",("spider join_tab->quick->get_type()=%u",
9058         join_tab->quick ? join_tab->quick->get_type() : 0));
9059       if (
9060         join_tab->quick &&
9061         join_tab->quick->get_type() == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE
9062       ) {
9063         DBUG_RETURN(TRUE);
9064       }
9065 */
9066       DBUG_PRINT("info",("spider join_tab->select->quick->get_type()=%u",
9067         join_tab->select && join_tab->select->quick ? join_tab->select->quick->get_type() : 0));
9068       if (
9069         join_tab->select &&
9070         join_tab->select->quick &&
9071         join_tab->select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE
9072       ) {
9073         DBUG_RETURN(TRUE);
9074       }
9075       break;
9076     }
9077   }
9078   DBUG_RETURN(FALSE);
9079 }
9080 
spider_compare_for_sort(SPIDER_SORT * a,SPIDER_SORT * b)9081 int spider_compare_for_sort(
9082   SPIDER_SORT *a,
9083   SPIDER_SORT *b
9084 ) {
9085   DBUG_ENTER("spider_compare_for_sort");
9086   if (a->sort > b->sort)
9087     DBUG_RETURN(-1);
9088   if (a->sort < b->sort)
9089     DBUG_RETURN(1);
9090   DBUG_RETURN(0);
9091 }
9092 
spider_calc_for_sort(uint count,...)9093 ulong spider_calc_for_sort(
9094   uint count,
9095   ...
9096 ) {
9097   ulong sort = 0;
9098   va_list args;
9099   va_start(args, count);
9100   DBUG_ENTER("spider_calc_for_sort");
9101   while (count--)
9102   {
9103     char *start = va_arg(args, char *), *str;
9104     uint wild_pos = 0;
9105 
9106     if ((str = start))
9107     {
9108       wild_pos = 128;
9109       for (; *str; str++)
9110       {
9111         if (*str == spider_wild_prefix && str[1])
9112           str++;
9113         else if (*str == spider_wild_many || *str == spider_wild_one)
9114         {
9115           wild_pos = (uint) (str - start) + 1;
9116           if (wild_pos > 127)
9117             wild_pos = 127;
9118           break;
9119         }
9120       }
9121     }
9122     sort = (sort << 8) + wild_pos;
9123   }
9124   va_end(args);
9125   DBUG_RETURN(sort);
9126 }
9127 
spider_rand(uint32 rand_source)9128 double spider_rand(
9129   uint32 rand_source
9130 ) {
9131 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
9132   struct my_rnd_struct rand;
9133 #else
9134   struct rand_struct rand;
9135 #endif
9136   DBUG_ENTER("spider_rand");
9137   /* generate same as rand function for applications */
9138 #if defined(MARIADB_BASE_VERSION) && MYSQL_VERSION_ID >= 100000
9139   my_rnd_init(&rand, (uint32) (rand_source * 65537L + 55555555L),
9140     (uint32) (rand_source * 268435457L));
9141 #else
9142   randominit(&rand, (uint32) (rand_source * 65537L + 55555555L),
9143     (uint32) (rand_source * 268435457L));
9144 #endif
9145   DBUG_RETURN(my_rnd(&rand));
9146 }
9147 
9148 #ifdef SPIDER_HAS_DISCOVER_TABLE_STRUCTURE
spider_discover_table_structure_internal(SPIDER_TRX * trx,SPIDER_SHARE * spider_share,spider_string * str)9149 int spider_discover_table_structure_internal(
9150   SPIDER_TRX *trx,
9151   SPIDER_SHARE *spider_share,
9152   spider_string *str
9153 ) {
9154   int error_num = 0, roop_count;
9155   DBUG_ENTER("spider_discover_table_structure_internal");
9156   for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; roop_count++)
9157   {
9158     if (spider_bit_is_set(spider_share->dbton_bitmap, roop_count))
9159     {
9160       if ((error_num = spider_share->dbton_share[roop_count]->
9161         discover_table_structure(trx, spider_share, str)))
9162       {
9163         continue;
9164       }
9165       break;
9166     }
9167   }
9168   DBUG_RETURN(error_num);
9169 }
9170 
spider_discover_table_structure(handlerton * hton,THD * thd,TABLE_SHARE * share,HA_CREATE_INFO * info)9171 int spider_discover_table_structure(
9172   handlerton *hton,
9173   THD* thd,
9174   TABLE_SHARE *share,
9175   HA_CREATE_INFO *info
9176 ) {
9177   int error_num = HA_ERR_WRONG_COMMAND, dummy;
9178   SPIDER_SHARE *spider_share;
9179   const char *table_name = share->path.str;
9180   uint table_name_length = (uint) strlen(table_name);
9181   SPIDER_TRX *trx;
9182 #ifdef WITH_PARTITION_STORAGE_ENGINE
9183   partition_info *part_info = thd->work_part_info;
9184 #endif
9185   Open_tables_backup open_tables_backup;
9186   TABLE *table_tables;
9187   uint str_len __attribute__ ((unused));
9188   char buf[MAX_FIELD_WIDTH];
9189   spider_string str(buf, sizeof(buf), system_charset_info);
9190   DBUG_ENTER("spider_discover_table_structure");
9191   str.init_calc_mem(229);
9192   str.length(0);
9193   if (str.reserve(
9194     SPIDER_SQL_CREATE_TABLE_LEN + share->db.length +
9195     SPIDER_SQL_DOT_LEN + share->table_name.length +
9196     /* SPIDER_SQL_LCL_NAME_QUOTE_LEN */ 4 + SPIDER_SQL_OPEN_PAREN_LEN
9197   )) {
9198     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9199   }
9200   str.q_append(SPIDER_SQL_CREATE_TABLE_STR, SPIDER_SQL_CREATE_TABLE_LEN);
9201   str.q_append(SPIDER_SQL_LCL_NAME_QUOTE_STR, SPIDER_SQL_LCL_NAME_QUOTE_LEN);
9202   str.q_append(share->db.str, share->db.length);
9203   str.q_append(SPIDER_SQL_LCL_NAME_QUOTE_STR, SPIDER_SQL_LCL_NAME_QUOTE_LEN);
9204   str.q_append(SPIDER_SQL_DOT_STR, SPIDER_SQL_DOT_LEN);
9205   str.q_append(SPIDER_SQL_LCL_NAME_QUOTE_STR, SPIDER_SQL_LCL_NAME_QUOTE_LEN);
9206   str.q_append(share->table_name.str, share->table_name.length);
9207   str.q_append(SPIDER_SQL_LCL_NAME_QUOTE_STR, SPIDER_SQL_LCL_NAME_QUOTE_LEN);
9208   str.q_append(SPIDER_SQL_OPEN_PAREN_STR, SPIDER_SQL_OPEN_PAREN_LEN);
9209   str_len = str.length();
9210 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9211   my_hash_value_type hash_value = my_calc_hash(&spider_open_tables,
9212     (uchar*) table_name, table_name_length);
9213 #endif
9214   if (!(trx = spider_get_trx(thd, TRUE, &error_num)))
9215   {
9216     DBUG_PRINT("info",("spider spider_get_trx error"));
9217     my_error(error_num, MYF(0));
9218     DBUG_RETURN(error_num);
9219   }
9220   share->table_charset = info->default_table_charset;
9221   share->comment = info->comment;
9222 #ifdef WITH_PARTITION_STORAGE_ENGINE
9223   if (!part_info)
9224   {
9225 #endif
9226     if (!(spider_share = spider_create_share(table_name, share,
9227 #ifdef WITH_PARTITION_STORAGE_ENGINE
9228       NULL,
9229 #endif
9230 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9231       hash_value,
9232 #endif
9233       &error_num
9234     ))) {
9235       DBUG_RETURN(error_num);
9236     }
9237 
9238     error_num = spider_discover_table_structure_internal(trx, spider_share, &str);
9239 
9240     if (!error_num)
9241     {
9242       if (
9243         (table_tables = spider_open_sys_table(
9244           thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
9245           SPIDER_SYS_TABLES_TABLE_NAME_LEN, TRUE, &open_tables_backup, FALSE,
9246           &error_num))
9247       ) {
9248 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9249         if (thd->lex->create_info.or_replace())
9250         {
9251           error_num = spider_delete_tables(table_tables,
9252             spider_share->table_name, &dummy);
9253         }
9254         if (!error_num)
9255         {
9256 #endif
9257           error_num = spider_insert_tables(table_tables, spider_share);
9258 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9259         }
9260 #endif
9261         spider_close_sys_table(thd, table_tables,
9262           &open_tables_backup, FALSE);
9263       }
9264     }
9265 
9266     spider_free_share_resource_only(spider_share);
9267 #ifdef WITH_PARTITION_STORAGE_ENGINE
9268   } else {
9269     char tmp_name[FN_REFLEN + 1];
9270     List_iterator<partition_element> part_it(part_info->partitions);
9271     List_iterator<partition_element> part_it2(part_info->partitions);
9272     partition_element *part_elem, *sub_elem;
9273     while ((part_elem = part_it++))
9274     {
9275       if ((part_elem)->subpartitions.elements)
9276       {
9277         List_iterator<partition_element> sub_it((part_elem)->subpartitions);
9278         while ((sub_elem = sub_it++))
9279         {
9280           str.length(str_len);
9281           if ((error_num = SPIDER_create_subpartition_name(
9282             tmp_name, FN_REFLEN + 1, table_name,
9283             (part_elem)->partition_name, (sub_elem)->partition_name,
9284             NORMAL_PART_NAME)))
9285           {
9286             DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9287           }
9288           DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
9289           if (!(spider_share = spider_create_share(tmp_name, share,
9290             part_info,
9291 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9292             hash_value,
9293 #endif
9294             &error_num
9295           ))) {
9296             DBUG_RETURN(error_num);
9297           }
9298 
9299           error_num = spider_discover_table_structure_internal(
9300             trx, spider_share, &str);
9301 
9302           spider_free_share_resource_only(spider_share);
9303           if (!error_num)
9304             break;
9305         }
9306         if (!error_num)
9307           break;
9308       } else {
9309         str.length(str_len);
9310         if ((error_num = SPIDER_create_partition_name(
9311           tmp_name, FN_REFLEN + 1, table_name,
9312           (part_elem)->partition_name, NORMAL_PART_NAME, TRUE)))
9313         {
9314           DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9315         }
9316         DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
9317         if (!(spider_share = spider_create_share(tmp_name, share,
9318           part_info,
9319 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9320           hash_value,
9321 #endif
9322           &error_num
9323         ))) {
9324           DBUG_RETURN(error_num);
9325         }
9326 
9327         error_num = spider_discover_table_structure_internal(
9328           trx, spider_share, &str);
9329 
9330         spider_free_share_resource_only(spider_share);
9331         if (!error_num)
9332           break;
9333       }
9334     }
9335     if (!error_num)
9336     {
9337       if (
9338         !(table_tables = spider_open_sys_table(
9339           thd, SPIDER_SYS_TABLES_TABLE_NAME_STR,
9340           SPIDER_SYS_TABLES_TABLE_NAME_LEN, TRUE, &open_tables_backup, FALSE,
9341           &error_num))
9342       ) {
9343         DBUG_RETURN(error_num);
9344       }
9345       while ((part_elem = part_it2++))
9346       {
9347         if ((part_elem)->subpartitions.elements)
9348         {
9349           List_iterator<partition_element> sub_it((part_elem)->subpartitions);
9350           while ((sub_elem = sub_it++))
9351           {
9352             if ((error_num = SPIDER_create_subpartition_name(
9353               tmp_name, FN_REFLEN + 1, table_name,
9354               (part_elem)->partition_name, (sub_elem)->partition_name,
9355               NORMAL_PART_NAME)))
9356             {
9357               DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9358             }
9359             DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
9360             if (!(spider_share = spider_create_share(tmp_name, share,
9361               part_info,
9362 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9363               hash_value,
9364 #endif
9365               &error_num
9366             ))) {
9367               DBUG_RETURN(error_num);
9368             }
9369 
9370 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9371             if (thd->lex->create_info.or_replace())
9372             {
9373               error_num = spider_delete_tables(table_tables,
9374                 spider_share->table_name, &dummy);
9375             }
9376             if (!error_num)
9377             {
9378 #endif
9379               error_num = spider_insert_tables(table_tables, spider_share);
9380 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9381             }
9382 #endif
9383 
9384             spider_free_share_resource_only(spider_share);
9385             if (error_num)
9386               break;
9387           }
9388           if (error_num)
9389             break;
9390         } else {
9391           if ((error_num = SPIDER_create_partition_name(
9392             tmp_name, FN_REFLEN + 1, table_name,
9393             (part_elem)->partition_name, NORMAL_PART_NAME, TRUE)))
9394           {
9395             DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9396           }
9397           DBUG_PRINT("info",("spider tmp_name=%s", tmp_name));
9398           if (!(spider_share = spider_create_share(tmp_name, share,
9399             part_info,
9400 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
9401             hash_value,
9402 #endif
9403             &error_num
9404           ))) {
9405             DBUG_RETURN(error_num);
9406           }
9407 
9408 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9409           if (thd->lex->create_info.or_replace())
9410           {
9411             error_num = spider_delete_tables(table_tables,
9412               spider_share->table_name, &dummy);
9413           }
9414           if (!error_num)
9415           {
9416 #endif
9417             error_num = spider_insert_tables(table_tables, spider_share);
9418 #ifdef SPIDER_SUPPORT_CREATE_OR_REPLACE_TABLE
9419           }
9420 #endif
9421 
9422           spider_free_share_resource_only(spider_share);
9423           if (error_num)
9424             break;
9425         }
9426       }
9427       spider_close_sys_table(thd, table_tables,
9428         &open_tables_backup, FALSE);
9429     }
9430   }
9431 #endif
9432 
9433   if (!error_num)
9434     thd->clear_error();
9435   else
9436     DBUG_RETURN(error_num);
9437 
9438   str.length(str.length() - SPIDER_SQL_COMMA_LEN);
9439   CHARSET_INFO *table_charset;
9440   if (share->table_charset)
9441   {
9442     table_charset = share->table_charset;
9443   } else {
9444     table_charset = system_charset_info;
9445   }
9446   uint csnamelen = strlen(table_charset->csname);
9447   uint collatelen = strlen(table_charset->name);
9448   if (str.reserve(SPIDER_SQL_CLOSE_PAREN_LEN + SPIDER_SQL_DEFAULT_CHARSET_LEN +
9449     csnamelen + SPIDER_SQL_COLLATE_LEN + collatelen +
9450     SPIDER_SQL_CONNECTION_LEN + SPIDER_SQL_VALUE_QUOTE_LEN +
9451     (share->comment.length * 2)
9452   )) {
9453     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9454   }
9455   str.q_append(SPIDER_SQL_CLOSE_PAREN_STR, SPIDER_SQL_CLOSE_PAREN_LEN);
9456   str.q_append(SPIDER_SQL_DEFAULT_CHARSET_STR, SPIDER_SQL_DEFAULT_CHARSET_LEN);
9457   str.q_append(table_charset->csname, csnamelen);
9458   str.q_append(SPIDER_SQL_COLLATE_STR, SPIDER_SQL_COLLATE_LEN);
9459   str.q_append(table_charset->name, collatelen);
9460   str.q_append(SPIDER_SQL_COMMENT_STR, SPIDER_SQL_COMMENT_LEN);
9461   str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
9462   str.append_escape_string(share->comment.str, share->comment.length);
9463   if (str.reserve(SPIDER_SQL_CONNECTION_LEN +
9464     (SPIDER_SQL_VALUE_QUOTE_LEN * 2) +
9465     (share->connect_string.length * 2)))
9466   {
9467     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9468   }
9469   str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
9470   str.q_append(SPIDER_SQL_CONNECTION_STR, SPIDER_SQL_CONNECTION_LEN);
9471   str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
9472   str.append_escape_string(share->connect_string.str,
9473     share->connect_string.length);
9474   if (str.reserve(SPIDER_SQL_VALUE_QUOTE_LEN))
9475   {
9476     DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9477   }
9478   str.q_append(SPIDER_SQL_VALUE_QUOTE_STR, SPIDER_SQL_VALUE_QUOTE_LEN);
9479 #ifdef WITH_PARTITION_STORAGE_ENGINE
9480   DBUG_PRINT("info",("spider part_info=%p", part_info));
9481   if (part_info)
9482   {
9483     uint part_syntax_len;
9484     char *part_syntax;
9485     List_iterator<partition_element> part_it(part_info->partitions);
9486     partition_element *part_elem, *sub_elem;
9487     while ((part_elem = part_it++))
9488     {
9489       part_elem->engine_type = hton;
9490       if ((part_elem)->subpartitions.elements)
9491       {
9492         List_iterator<partition_element> sub_it((part_elem)->subpartitions);
9493         while ((sub_elem = sub_it++))
9494         {
9495           sub_elem->engine_type = hton;
9496         }
9497       }
9498     }
9499     if (part_info->fix_parser_data(thd))
9500     {
9501       DBUG_RETURN(ER_SPIDER_UNKNOWN_NUM);
9502     }
9503     if (!(part_syntax = SPIDER_generate_partition_syntax(thd, part_info,
9504       &part_syntax_len, FALSE, TRUE, info, NULL, NULL)))
9505     {
9506       DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9507     }
9508     if (str.reserve(part_syntax_len))
9509     {
9510       DBUG_RETURN(HA_ERR_OUT_OF_MEM);
9511     }
9512     str.q_append(part_syntax, part_syntax_len);
9513     SPIDER_free_part_syntax(part_syntax, MYF(0));
9514   }
9515 #endif
9516   DBUG_PRINT("info",("spider str=%s", str.c_ptr_safe()));
9517 
9518   error_num = share->init_from_sql_statement_string(thd, TRUE, str.ptr(),
9519     str.length());
9520   DBUG_RETURN(error_num);
9521 }
9522 #endif
9523 
9524 #ifndef WITHOUT_SPIDER_BG_SEARCH
spider_create_spider_object_for_share(SPIDER_TRX * trx,SPIDER_SHARE * share,ha_spider ** spider)9525 int spider_create_spider_object_for_share(
9526   SPIDER_TRX *trx,
9527   SPIDER_SHARE *share,
9528   ha_spider **spider
9529 ) {
9530   int error_num, roop_count, *need_mons;
9531   SPIDER_CONN **conns;
9532   uint *conn_link_idx;
9533   uchar *conn_can_fo;
9534   char **conn_keys;
9535 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9536   char **hs_r_conn_keys;
9537   char **hs_w_conn_keys;
9538 #endif
9539   spider_db_handler **dbton_hdl;
9540   DBUG_ENTER("spider_create_spider_object_for_share");
9541   DBUG_PRINT("info",("spider trx=%p", trx));
9542   DBUG_PRINT("info",("spider share=%p", share));
9543   DBUG_PRINT("info",("spider spider_ptr=%p", spider));
9544   DBUG_PRINT("info",("spider spider=%p", (*spider)));
9545 
9546   if (*spider)
9547   {
9548     /* already exists */
9549     DBUG_RETURN(0);
9550   }
9551   (*spider) = new (&share->mem_root) ha_spider();
9552   if (!(*spider))
9553   {
9554     error_num = HA_ERR_OUT_OF_MEM;
9555     goto error_spider_alloc;
9556   }
9557   DBUG_PRINT("info",("spider spider=%p", (*spider)));
9558 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9559   if (!(need_mons = (int *)
9560     spider_bulk_malloc(spider_current_trx, 255, MYF(MY_WME | MY_ZEROFILL),
9561       &need_mons, (sizeof(int) * share->link_count),
9562       &conns, (sizeof(SPIDER_CONN *) * share->link_count),
9563       &conn_link_idx, (sizeof(uint) * share->link_count),
9564       &conn_can_fo, (sizeof(uchar) * share->link_bitmap_size),
9565       &conn_keys, (sizeof(char *) * share->link_count),
9566       &hs_r_conn_keys, (sizeof(char *) * share->link_count),
9567       &hs_w_conn_keys, (sizeof(char *) * share->link_count),
9568       &dbton_hdl, (sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE),
9569       NullS))
9570   )
9571 #else
9572   if (!(need_mons = (int *)
9573     spider_bulk_malloc(spider_current_trx, 255, MYF(MY_WME | MY_ZEROFILL),
9574       &need_mons, (sizeof(int) * share->link_count),
9575       &conns, (sizeof(SPIDER_CONN *) * share->link_count),
9576       &conn_link_idx, (sizeof(uint) * share->link_count),
9577       &conn_can_fo, (sizeof(uchar) * share->link_bitmap_size),
9578       &conn_keys, (sizeof(char *) * share->link_count),
9579       &dbton_hdl, (sizeof(spider_db_handler *) * SPIDER_DBTON_SIZE),
9580       NullS))
9581   )
9582 #endif
9583   {
9584     error_num = HA_ERR_OUT_OF_MEM;
9585     goto error_need_mons_alloc;
9586   }
9587   DBUG_PRINT("info",("spider need_mons=%p", need_mons));
9588   (*spider)->trx = trx;
9589   (*spider)->change_table_ptr(&share->table, share->table_share);
9590   (*spider)->share = share;
9591   (*spider)->conns = conns;
9592   (*spider)->conn_link_idx = conn_link_idx;
9593   (*spider)->conn_can_fo = conn_can_fo;
9594   (*spider)->need_mons = need_mons;
9595   (*spider)->conn_keys_first_ptr = share->conn_keys[0];
9596   (*spider)->conn_keys = conn_keys;
9597 #if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
9598   (*spider)->hs_r_conn_keys = hs_r_conn_keys;
9599   (*spider)->hs_w_conn_keys = hs_w_conn_keys;
9600 #endif
9601   (*spider)->dbton_handler = dbton_hdl;
9602   (*spider)->search_link_idx = -1;
9603   for (roop_count = 0; roop_count < SPIDER_DBTON_SIZE; roop_count++)
9604   {
9605     if (
9606       spider_bit_is_set(share->dbton_bitmap, roop_count) &&
9607       spider_dbton[roop_count].create_db_handler
9608     ) {
9609       if (!(dbton_hdl[roop_count] = spider_dbton[roop_count].create_db_handler(
9610         *spider, share->dbton_share[roop_count])))
9611       {
9612         error_num = HA_ERR_OUT_OF_MEM;
9613         goto error_init_db_handler;
9614       }
9615       if ((error_num = dbton_hdl[roop_count]->init()))
9616         goto error_init_db_handler;
9617     }
9618   }
9619   DBUG_PRINT("info",("spider share=%p", (*spider)->share));
9620   DBUG_PRINT("info",("spider need_mons=%p", (*spider)->need_mons));
9621   DBUG_RETURN(0);
9622 
9623 error_init_db_handler:
9624   for (; roop_count >= 0; --roop_count)
9625   {
9626     if (
9627       spider_bit_is_set(share->dbton_bitmap, roop_count) &&
9628       dbton_hdl[roop_count]
9629     ) {
9630       delete dbton_hdl[roop_count];
9631       dbton_hdl[roop_count] = NULL;
9632     }
9633   }
9634   spider_free(spider_current_trx, (*spider)->need_mons, MYF(0));
9635 error_need_mons_alloc:
9636   delete (*spider);
9637   (*spider) = NULL;
9638 error_spider_alloc:
9639   DBUG_RETURN(error_num);
9640 }
9641 
spider_free_spider_object_for_share(ha_spider ** spider)9642 void spider_free_spider_object_for_share(
9643   ha_spider **spider
9644 ) {
9645   int roop_count;
9646   SPIDER_SHARE *share = (*spider)->share;
9647   spider_db_handler **dbton_hdl = (*spider)->dbton_handler;
9648   DBUG_ENTER("spider_free_spider_object_for_share");
9649   DBUG_PRINT("info",("spider share=%p", share));
9650   DBUG_PRINT("info",("spider spider_ptr=%p", spider));
9651   DBUG_PRINT("info",("spider spider=%p", (*spider)));
9652   for (roop_count = SPIDER_DBTON_SIZE - 1; roop_count >= 0; --roop_count)
9653   {
9654     if (
9655       spider_bit_is_set(share->dbton_bitmap, roop_count) &&
9656       dbton_hdl[roop_count]
9657     ) {
9658       delete dbton_hdl[roop_count];
9659       dbton_hdl[roop_count] = NULL;
9660     }
9661   }
9662   spider_free(spider_current_trx, (*spider)->need_mons, MYF(0));
9663   delete (*spider);
9664   (*spider) = NULL;
9665   DBUG_VOID_RETURN;
9666 }
9667 
spider_create_sts_threads(SPIDER_THREAD * spider_thread)9668 int spider_create_sts_threads(
9669   SPIDER_THREAD *spider_thread
9670 ) {
9671   int error_num;
9672   DBUG_ENTER("spider_create_sts_threads");
9673 #if MYSQL_VERSION_ID < 50500
9674   if (pthread_mutex_init(&spider_thread->mutex,
9675     MY_MUTEX_INIT_FAST))
9676 #else
9677   if (mysql_mutex_init(spd_key_mutex_bg_stss,
9678     &spider_thread->mutex, MY_MUTEX_INIT_FAST))
9679 #endif
9680   {
9681     error_num = HA_ERR_OUT_OF_MEM;
9682     goto error_mutex_init;
9683   }
9684 #if MYSQL_VERSION_ID < 50500
9685   if (pthread_cond_init(&spider_thread->cond, NULL))
9686 #else
9687   if (mysql_cond_init(spd_key_cond_bg_stss,
9688     &spider_thread->cond, NULL))
9689 #endif
9690   {
9691     error_num = HA_ERR_OUT_OF_MEM;
9692     goto error_cond_init;
9693   }
9694 #if MYSQL_VERSION_ID < 50500
9695   if (pthread_cond_init(&spider_thread->sync_cond, NULL))
9696 #else
9697   if (mysql_cond_init(spd_key_cond_bg_sts_syncs,
9698     &spider_thread->sync_cond, NULL))
9699 #endif
9700   {
9701     error_num = HA_ERR_OUT_OF_MEM;
9702     goto error_sync_cond_init;
9703   }
9704 #if MYSQL_VERSION_ID < 50500
9705   if (pthread_create(&spider_thread->thread, &spider_pt_attr,
9706     spider_table_bg_sts_action, (void *) spider_thread)
9707   )
9708 #else
9709   if (mysql_thread_create(spd_key_thd_bg_stss, &spider_thread->thread,
9710     &spider_pt_attr, spider_table_bg_sts_action, (void *) spider_thread)
9711   )
9712 #endif
9713   {
9714     error_num = HA_ERR_OUT_OF_MEM;
9715     goto error_thread_create;
9716   }
9717   DBUG_RETURN(0);
9718 
9719 error_thread_create:
9720   pthread_cond_destroy(&spider_thread->sync_cond);
9721 error_sync_cond_init:
9722   pthread_cond_destroy(&spider_thread->cond);
9723 error_cond_init:
9724   pthread_mutex_destroy(&spider_thread->mutex);
9725 error_mutex_init:
9726   DBUG_RETURN(error_num);
9727 }
9728 
spider_free_sts_threads(SPIDER_THREAD * spider_thread)9729 void spider_free_sts_threads(
9730   SPIDER_THREAD *spider_thread
9731 ) {
9732   bool thread_killed;
9733   DBUG_ENTER("spider_free_sts_threads");
9734   pthread_mutex_lock(&spider_thread->mutex);
9735   thread_killed = spider_thread->killed;
9736   spider_thread->killed = TRUE;
9737   if (!thread_killed)
9738   {
9739     if (spider_thread->thd_wait)
9740     {
9741       pthread_cond_signal(&spider_thread->cond);
9742     }
9743     pthread_cond_wait(&spider_thread->sync_cond, &spider_thread->mutex);
9744   }
9745   pthread_mutex_unlock(&spider_thread->mutex);
9746   pthread_join(spider_thread->thread, NULL);
9747   pthread_cond_destroy(&spider_thread->sync_cond);
9748   pthread_cond_destroy(&spider_thread->cond);
9749   pthread_mutex_destroy(&spider_thread->mutex);
9750   spider_thread->thd_wait = FALSE;
9751   spider_thread->killed = FALSE;
9752   DBUG_VOID_RETURN;
9753 }
9754 
spider_create_crd_threads(SPIDER_THREAD * spider_thread)9755 int spider_create_crd_threads(
9756   SPIDER_THREAD *spider_thread
9757 ) {
9758   int error_num;
9759   DBUG_ENTER("spider_create_crd_threads");
9760 #if MYSQL_VERSION_ID < 50500
9761   if (pthread_mutex_init(&spider_thread->mutex,
9762     MY_MUTEX_INIT_FAST))
9763 #else
9764   if (mysql_mutex_init(spd_key_mutex_bg_crds,
9765     &spider_thread->mutex, MY_MUTEX_INIT_FAST))
9766 #endif
9767   {
9768     error_num = HA_ERR_OUT_OF_MEM;
9769     goto error_mutex_init;
9770   }
9771 #if MYSQL_VERSION_ID < 50500
9772   if (pthread_cond_init(&spider_thread->cond, NULL))
9773 #else
9774   if (mysql_cond_init(spd_key_cond_bg_crds,
9775     &spider_thread->cond, NULL))
9776 #endif
9777   {
9778     error_num = HA_ERR_OUT_OF_MEM;
9779     goto error_cond_init;
9780   }
9781 #if MYSQL_VERSION_ID < 50500
9782   if (pthread_cond_init(&spider_thread->sync_cond, NULL))
9783 #else
9784   if (mysql_cond_init(spd_key_cond_bg_crd_syncs,
9785     &spider_thread->sync_cond, NULL))
9786 #endif
9787   {
9788     error_num = HA_ERR_OUT_OF_MEM;
9789     goto error_sync_cond_init;
9790   }
9791 #if MYSQL_VERSION_ID < 50500
9792   if (pthread_create(&spider_thread->thread, &spider_pt_attr,
9793     spider_table_bg_crd_action, (void *) spider_thread)
9794   )
9795 #else
9796   if (mysql_thread_create(spd_key_thd_bg_crds, &spider_thread->thread,
9797     &spider_pt_attr, spider_table_bg_crd_action, (void *) spider_thread)
9798   )
9799 #endif
9800   {
9801     error_num = HA_ERR_OUT_OF_MEM;
9802     goto error_thread_create;
9803   }
9804   DBUG_RETURN(0);
9805 
9806 error_thread_create:
9807   pthread_cond_destroy(&spider_thread->sync_cond);
9808 error_sync_cond_init:
9809   pthread_cond_destroy(&spider_thread->cond);
9810 error_cond_init:
9811   pthread_mutex_destroy(&spider_thread->mutex);
9812 error_mutex_init:
9813   DBUG_RETURN(error_num);
9814 }
9815 
spider_free_crd_threads(SPIDER_THREAD * spider_thread)9816 void spider_free_crd_threads(
9817   SPIDER_THREAD *spider_thread
9818 ) {
9819   bool thread_killed;
9820   DBUG_ENTER("spider_free_crd_threads");
9821   pthread_mutex_lock(&spider_thread->mutex);
9822   thread_killed = spider_thread->killed;
9823   spider_thread->killed = TRUE;
9824   if (!thread_killed)
9825   {
9826     if (spider_thread->thd_wait)
9827     {
9828       pthread_cond_signal(&spider_thread->cond);
9829     }
9830     pthread_cond_wait(&spider_thread->sync_cond, &spider_thread->mutex);
9831   }
9832   pthread_mutex_unlock(&spider_thread->mutex);
9833   pthread_join(spider_thread->thread, NULL);
9834   pthread_cond_destroy(&spider_thread->sync_cond);
9835   pthread_cond_destroy(&spider_thread->cond);
9836   pthread_mutex_destroy(&spider_thread->mutex);
9837   spider_thread->thd_wait = FALSE;
9838   spider_thread->killed = FALSE;
9839   DBUG_VOID_RETURN;
9840 }
9841 
spider_table_bg_sts_action(void * arg)9842 void *spider_table_bg_sts_action(
9843   void *arg
9844 ) {
9845   SPIDER_THREAD *thread = (SPIDER_THREAD *) arg;
9846   SPIDER_SHARE *share;
9847   SPIDER_TRX *trx;
9848   int error_num;
9849   ha_spider *spider;
9850   SPIDER_CONN **conns;
9851   THD *thd;
9852   my_thread_init();
9853   DBUG_ENTER("spider_table_bg_sts_action");
9854   /* init start */
9855   pthread_mutex_lock(&thread->mutex);
9856   if (!(thd = spider_create_sys_thd(thread)))
9857   {
9858     thread->thd_wait = FALSE;
9859     thread->killed = FALSE;
9860     pthread_mutex_unlock(&thread->mutex);
9861     my_thread_end();
9862     DBUG_RETURN(NULL);
9863   }
9864   SPIDER_set_next_thread_id(thd);
9865 #ifdef HAVE_PSI_INTERFACE
9866   mysql_thread_set_psi_id(thd->thread_id);
9867 #endif
9868   thd_proc_info(thd, "Spider table background statistics action handler");
9869   if (!(trx = spider_get_trx(NULL, FALSE, &error_num)))
9870   {
9871     spider_destroy_sys_thd(thd);
9872     thread->thd_wait = FALSE;
9873     thread->killed = FALSE;
9874     pthread_mutex_unlock(&thread->mutex);
9875 #if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
9876     my_pthread_setspecific_ptr(THR_THD, NULL);
9877 #endif
9878     my_thread_end();
9879     DBUG_RETURN(NULL);
9880   }
9881   trx->thd = thd;
9882   /* init end */
9883 
9884   while (TRUE)
9885   {
9886     DBUG_PRINT("info",("spider bg sts loop start"));
9887     if (thread->killed)
9888     {
9889       DBUG_PRINT("info",("spider bg sts kill start"));
9890       trx->thd = NULL;
9891       spider_free_trx(trx, TRUE);
9892       spider_destroy_sys_thd(thd);
9893       pthread_cond_signal(&thread->sync_cond);
9894       pthread_mutex_unlock(&thread->mutex);
9895 #if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
9896       my_pthread_setspecific_ptr(THR_THD, NULL);
9897 #endif
9898       my_thread_end();
9899       DBUG_RETURN(NULL);
9900     }
9901     if (!thread->queue_first)
9902     {
9903       DBUG_PRINT("info",("spider bg sts has no job"));
9904       thread->thd_wait = TRUE;
9905       pthread_cond_wait(&thread->cond, &thread->mutex);
9906       thread->thd_wait = FALSE;
9907       if (thd->killed)
9908         thread->killed = TRUE;
9909       continue;
9910     }
9911     share = (SPIDER_SHARE *) thread->queue_first;
9912     share->sts_working = TRUE;
9913     pthread_mutex_unlock(&thread->mutex);
9914 
9915     spider = share->sts_spider;
9916     conns = spider->conns;
9917     if (spider->search_link_idx < 0)
9918     {
9919       spider->trx = trx;
9920       spider_trx_set_link_idx_for_all(spider);
9921       spider->search_link_idx = spider_conn_first_link_idx(thd,
9922         share->link_statuses, share->access_balances, spider->conn_link_idx,
9923         share->link_count, SPIDER_LINK_STATUS_OK);
9924     }
9925     if (spider->search_link_idx >= 0)
9926     {
9927       DBUG_PRINT("info",
9928         ("spider difftime=%f",
9929           difftime(share->bg_sts_try_time, share->sts_get_time)));
9930       DBUG_PRINT("info",
9931         ("spider bg_sts_interval=%f", share->bg_sts_interval));
9932       if (difftime(share->bg_sts_try_time, share->sts_get_time) >=
9933         share->bg_sts_interval)
9934       {
9935         if (!conns[spider->search_link_idx])
9936         {
9937           spider_get_conn(share, spider->search_link_idx,
9938             share->conn_keys[spider->search_link_idx],
9939             trx, spider, FALSE, FALSE, SPIDER_CONN_KIND_MYSQL,
9940             &error_num);
9941           if (conns[spider->search_link_idx])
9942           {
9943             conns[spider->search_link_idx]->error_mode = 0;
9944           } else {
9945             spider->search_link_idx = -1;
9946           }
9947         }
9948         DBUG_PRINT("info",
9949           ("spider search_link_idx=%d", spider->search_link_idx));
9950         if (spider->search_link_idx >= 0 && conns[spider->search_link_idx])
9951         {
9952 #ifdef WITH_PARTITION_STORAGE_ENGINE
9953           if (spider_get_sts(share, spider->search_link_idx,
9954             share->bg_sts_try_time, spider,
9955             share->bg_sts_interval, share->bg_sts_mode,
9956             share->bg_sts_sync,
9957             2, HA_STATUS_CONST | HA_STATUS_VARIABLE))
9958 #else
9959           if (spider_get_sts(share, spider->search_link_idx,
9960             share->bg_sts_try_time, spider,
9961             share->bg_sts_interval, share->bg_sts_mode,
9962             2, HA_STATUS_CONST | HA_STATUS_VARIABLE))
9963 #endif
9964           {
9965             spider->search_link_idx = -1;
9966           }
9967         }
9968       }
9969     }
9970     memset(spider->need_mons, 0, sizeof(int) * share->link_count);
9971     pthread_mutex_lock(&thread->mutex);
9972     if (thread->queue_first == thread->queue_last)
9973     {
9974       thread->queue_first = NULL;
9975       thread->queue_last = NULL;
9976     } else {
9977       thread->queue_first = share->sts_next;
9978       share->sts_next->sts_prev = NULL;
9979       share->sts_next = NULL;
9980     }
9981     share->sts_working = FALSE;
9982     share->sts_wait = FALSE;
9983     if (thread->first_free_wait)
9984     {
9985       pthread_cond_signal(&thread->sync_cond);
9986       pthread_cond_wait(&thread->cond, &thread->mutex);
9987       if (thd->killed)
9988         thread->killed = TRUE;
9989     }
9990   }
9991 }
9992 
spider_table_bg_crd_action(void * arg)9993 void *spider_table_bg_crd_action(
9994   void *arg
9995 ) {
9996   SPIDER_THREAD *thread = (SPIDER_THREAD *) arg;
9997   SPIDER_SHARE *share;
9998   SPIDER_TRX *trx;
9999   int error_num;
10000   ha_spider *spider;
10001   TABLE *table;
10002   SPIDER_CONN **conns;
10003   THD *thd;
10004   my_thread_init();
10005   DBUG_ENTER("spider_table_bg_crd_action");
10006   /* init start */
10007   pthread_mutex_lock(&thread->mutex);
10008   if (!(thd = spider_create_sys_thd(thread)))
10009   {
10010     thread->thd_wait = FALSE;
10011     thread->killed = FALSE;
10012     pthread_mutex_unlock(&thread->mutex);
10013     my_thread_end();
10014     DBUG_RETURN(NULL);
10015   }
10016   SPIDER_set_next_thread_id(thd);
10017 #ifdef HAVE_PSI_INTERFACE
10018   mysql_thread_set_psi_id(thd->thread_id);
10019 #endif
10020   thd_proc_info(thd, "Spider table background cardinality action handler");
10021   if (!(trx = spider_get_trx(NULL, FALSE, &error_num)))
10022   {
10023     spider_destroy_sys_thd(thd);
10024     thread->thd_wait = FALSE;
10025     thread->killed = FALSE;
10026     pthread_mutex_unlock(&thread->mutex);
10027 #if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
10028     my_pthread_setspecific_ptr(THR_THD, NULL);
10029 #endif
10030     my_thread_end();
10031     DBUG_RETURN(NULL);
10032   }
10033   trx->thd = thd;
10034   /* init end */
10035 
10036   while (TRUE)
10037   {
10038     DBUG_PRINT("info",("spider bg crd loop start"));
10039     if (thread->killed)
10040     {
10041       DBUG_PRINT("info",("spider bg crd kill start"));
10042       trx->thd = NULL;
10043       spider_free_trx(trx, TRUE);
10044       spider_destroy_sys_thd(thd);
10045       pthread_cond_signal(&thread->sync_cond);
10046       pthread_mutex_unlock(&thread->mutex);
10047 #if !defined(MYSQL_DYNAMIC_PLUGIN) || !defined(_WIN32)
10048       my_pthread_setspecific_ptr(THR_THD, NULL);
10049 #endif
10050       my_thread_end();
10051       DBUG_RETURN(NULL);
10052     }
10053     if (!thread->queue_first)
10054     {
10055       DBUG_PRINT("info",("spider bg crd has no job"));
10056       thread->thd_wait = TRUE;
10057       pthread_cond_wait(&thread->cond, &thread->mutex);
10058       thread->thd_wait = FALSE;
10059       if (thd->killed)
10060         thread->killed = TRUE;
10061       continue;
10062     }
10063     share = (SPIDER_SHARE *) thread->queue_first;
10064     share->crd_working = TRUE;
10065     pthread_mutex_unlock(&thread->mutex);
10066 
10067     table = &share->table;
10068     spider = share->crd_spider;
10069     conns = spider->conns;
10070     if (spider->search_link_idx < 0)
10071     {
10072       spider->trx = trx;
10073       spider_trx_set_link_idx_for_all(spider);
10074       spider->search_link_idx = spider_conn_first_link_idx(thd,
10075         share->link_statuses, share->access_balances, spider->conn_link_idx,
10076         share->link_count, SPIDER_LINK_STATUS_OK);
10077     }
10078     if (spider->search_link_idx >= 0)
10079     {
10080       DBUG_PRINT("info",
10081         ("spider difftime=%f",
10082           difftime(share->bg_crd_try_time, share->crd_get_time)));
10083       DBUG_PRINT("info",
10084         ("spider bg_crd_interval=%f", share->bg_crd_interval));
10085       if (difftime(share->bg_crd_try_time, share->crd_get_time) >=
10086         share->bg_crd_interval)
10087       {
10088         if (!conns[spider->search_link_idx])
10089         {
10090           spider_get_conn(share, spider->search_link_idx,
10091             share->conn_keys[spider->search_link_idx],
10092             trx, spider, FALSE, FALSE, SPIDER_CONN_KIND_MYSQL,
10093             &error_num);
10094           if (conns[spider->search_link_idx])
10095           {
10096             conns[spider->search_link_idx]->error_mode = 0;
10097           } else {
10098             spider->search_link_idx = -1;
10099           }
10100         }
10101         DBUG_PRINT("info",
10102           ("spider search_link_idx=%d", spider->search_link_idx));
10103         if (spider->search_link_idx >= 0 && conns[spider->search_link_idx])
10104         {
10105 #ifdef WITH_PARTITION_STORAGE_ENGINE
10106           if (spider_get_crd(share, spider->search_link_idx,
10107             share->bg_crd_try_time, spider, table,
10108             share->bg_crd_interval, share->bg_crd_mode,
10109             share->bg_crd_sync,
10110             2))
10111 #else
10112           if (spider_get_crd(share, spider->search_link_idx,
10113             share->bg_crd_try_time, spider, table,
10114             share->bg_crd_interval, share->bg_crd_mode,
10115             2))
10116 #endif
10117           {
10118             spider->search_link_idx = -1;
10119           }
10120         }
10121       }
10122     }
10123     memset(spider->need_mons, 0, sizeof(int) * share->link_count);
10124     pthread_mutex_lock(&thread->mutex);
10125     if (thread->queue_first == thread->queue_last)
10126     {
10127       thread->queue_first = NULL;
10128       thread->queue_last = NULL;
10129     } else {
10130       thread->queue_first = share->crd_next;
10131       share->crd_next->crd_prev = NULL;
10132       share->crd_next = NULL;
10133     }
10134     share->crd_working = FALSE;
10135     share->crd_wait = FALSE;
10136     if (thread->first_free_wait)
10137     {
10138       pthread_cond_signal(&thread->sync_cond);
10139       pthread_cond_wait(&thread->cond, &thread->mutex);
10140       if (thd->killed)
10141         thread->killed = TRUE;
10142     }
10143   }
10144 }
10145 
spider_table_add_share_to_sts_thread(SPIDER_SHARE * share)10146 void spider_table_add_share_to_sts_thread(
10147   SPIDER_SHARE *share
10148 ) {
10149   SPIDER_THREAD *spider_thread = share->sts_thread;
10150   DBUG_ENTER("spider_table_add_share_to_sts_thread");
10151   if (
10152     !share->sts_wait &&
10153     !pthread_mutex_trylock(&spider_thread->mutex)
10154   ) {
10155     if (!share->sts_wait)
10156     {
10157       if (spider_thread->queue_last)
10158       {
10159         DBUG_PRINT("info",("spider add to last"));
10160         share->sts_prev = spider_thread->queue_last;
10161         spider_thread->queue_last->sts_next = share;
10162       } else {
10163         spider_thread->queue_first = share;
10164       }
10165       spider_thread->queue_last = share;
10166       share->sts_wait = TRUE;
10167 
10168       if (spider_thread->thd_wait)
10169       {
10170         pthread_cond_signal(&spider_thread->cond);
10171       }
10172     }
10173     pthread_mutex_unlock(&spider_thread->mutex);
10174   }
10175   DBUG_VOID_RETURN;
10176 }
10177 
spider_table_add_share_to_crd_thread(SPIDER_SHARE * share)10178 void spider_table_add_share_to_crd_thread(
10179   SPIDER_SHARE *share
10180 ) {
10181   SPIDER_THREAD *spider_thread = share->crd_thread;
10182   DBUG_ENTER("spider_table_add_share_to_crd_thread");
10183   if (
10184     !share->crd_wait &&
10185     !pthread_mutex_trylock(&spider_thread->mutex)
10186   ) {
10187     if (!share->crd_wait)
10188     {
10189       if (spider_thread->queue_last)
10190       {
10191         DBUG_PRINT("info",("spider add to last"));
10192         share->crd_prev = spider_thread->queue_last;
10193         spider_thread->queue_last->crd_next = share;
10194       } else {
10195         spider_thread->queue_first = share;
10196       }
10197       spider_thread->queue_last = share;
10198       share->crd_wait = TRUE;
10199 
10200       if (spider_thread->thd_wait)
10201       {
10202         pthread_cond_signal(&spider_thread->cond);
10203       }
10204     }
10205     pthread_mutex_unlock(&spider_thread->mutex);
10206   }
10207   DBUG_VOID_RETURN;
10208 }
10209 
spider_table_remove_share_from_sts_thread(SPIDER_SHARE * share)10210 void spider_table_remove_share_from_sts_thread(
10211   SPIDER_SHARE *share
10212 ) {
10213   SPIDER_THREAD *spider_thread = share->sts_thread;
10214   DBUG_ENTER("spider_table_remove_share_from_sts_thread");
10215   if (share->sts_wait)
10216   {
10217     pthread_mutex_lock(&spider_thread->mutex);
10218     if (share->sts_wait)
10219     {
10220       if (share->sts_working)
10221       {
10222         DBUG_PRINT("info",("spider waiting bg sts start"));
10223         spider_thread->first_free_wait = TRUE;
10224         pthread_cond_wait(&spider_thread->sync_cond, &spider_thread->mutex);
10225         spider_thread->first_free_wait = FALSE;
10226         pthread_cond_signal(&spider_thread->cond);
10227         DBUG_PRINT("info",("spider waiting bg sts end"));
10228       }
10229 
10230       if (share->sts_prev)
10231       {
10232         if (share->sts_next)
10233         {
10234           DBUG_PRINT("info",("spider remove middle one"));
10235           share->sts_prev->sts_next = share->sts_next;
10236           share->sts_next->sts_prev = share->sts_prev;
10237         } else {
10238           DBUG_PRINT("info",("spider remove last one"));
10239           share->sts_prev->sts_next = NULL;
10240           spider_thread->queue_last = share->sts_prev;
10241         }
10242       } else if (share->sts_next) {
10243         DBUG_PRINT("info",("spider remove first one"));
10244         share->sts_next->sts_prev = NULL;
10245         spider_thread->queue_first = share->sts_next;
10246       } else {
10247         DBUG_PRINT("info",("spider empty"));
10248         spider_thread->queue_first = NULL;
10249         spider_thread->queue_last = NULL;
10250       }
10251     }
10252     pthread_mutex_unlock(&spider_thread->mutex);
10253   }
10254   DBUG_VOID_RETURN;
10255 }
10256 
spider_table_remove_share_from_crd_thread(SPIDER_SHARE * share)10257 void spider_table_remove_share_from_crd_thread(
10258   SPIDER_SHARE *share
10259 ) {
10260   SPIDER_THREAD *spider_thread = share->crd_thread;
10261   DBUG_ENTER("spider_table_remove_share_from_crd_thread");
10262   if (share->crd_wait)
10263   {
10264     pthread_mutex_lock(&spider_thread->mutex);
10265     if (share->crd_wait)
10266     {
10267       if (share->crd_working)
10268       {
10269         DBUG_PRINT("info",("spider waiting bg crd start"));
10270         spider_thread->first_free_wait = TRUE;
10271         pthread_cond_wait(&spider_thread->sync_cond, &spider_thread->mutex);
10272         spider_thread->first_free_wait = FALSE;
10273         pthread_cond_signal(&spider_thread->cond);
10274         DBUG_PRINT("info",("spider waiting bg crd end"));
10275       }
10276 
10277       if (share->crd_prev)
10278       {
10279         if (share->crd_next)
10280         {
10281           DBUG_PRINT("info",("spider remove middle one"));
10282           share->crd_prev->crd_next = share->crd_next;
10283           share->crd_next->crd_prev = share->crd_prev;
10284         } else {
10285           DBUG_PRINT("info",("spider remove last one"));
10286           share->crd_prev->crd_next = NULL;
10287           spider_thread->queue_last = share->crd_prev;
10288         }
10289       } else if (share->crd_next) {
10290         DBUG_PRINT("info",("spider remove first one"));
10291         share->crd_next->crd_prev = NULL;
10292         spider_thread->queue_first = share->crd_next;
10293       } else {
10294         DBUG_PRINT("info",("spider empty"));
10295         spider_thread->queue_first = NULL;
10296         spider_thread->queue_last = NULL;
10297       }
10298     }
10299     pthread_mutex_unlock(&spider_thread->mutex);
10300   }
10301   DBUG_VOID_RETURN;
10302 }
10303 #endif
10304