1 /* Copyright (C) 2008-2020 Kentoku Shiba
2    Copyright (C) 2019-2020 MariaDB corp
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; version 2 of the License.
7 
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License
14   along with this program; if not, write to the Free Software
15   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
16 
17 #define SPIDER_LOCK_MODE_NO_LOCK             0
18 #define SPIDER_LOCK_MODE_SHARED              1
19 #define SPIDER_LOCK_MODE_EXCLUSIVE           2
20 
21 #define SPIDER_SIMPLE_NO_ACTION           0
22 #define SPIDER_SIMPLE_CONNECT             1
23 #define SPIDER_SIMPLE_DISCONNECT          2
24 #define SPIDER_SIMPLE_RECORDS             3
25 #ifdef HA_HAS_CHECKSUM_EXTENDED
26 #define SPIDER_SIMPLE_CHECKSUM_TABLE      4
27 #endif
28 
29 #define SPIDER_LOP_CHK_QUEUED             (1 << 0)
30 #define SPIDER_LOP_CHK_MERAGED            (1 << 1)
31 #define SPIDER_LOP_CHK_IGNORED            (1 << 2)
32 
33 typedef struct st_spider_conn_loop_check
34 {
35   uint               flag;
36 #ifdef SPIDER_HAS_HASH_VALUE_TYPE
37   my_hash_value_type hash_value_to;
38   my_hash_value_type hash_value_full;
39 #endif
40   LEX_CSTRING        from_name;
41   LEX_CSTRING        cur_name;
42   LEX_CSTRING        to_name;
43   LEX_CSTRING        full_name;
44   LEX_CSTRING        from_value;
45   LEX_CSTRING        merged_value;
46   st_spider_conn_loop_check *next;
47 } SPIDER_CONN_LOOP_CHECK;
48 
49 uchar *spider_conn_get_key(
50   SPIDER_CONN *conn,
51   size_t *length,
52   my_bool not_used __attribute__ ((unused))
53 );
54 
55 uchar *spider_ipport_conn_get_key(
56   SPIDER_IP_PORT_CONN *ip_port,
57   size_t *length,
58   my_bool not_used __attribute__ ((unused))
59 );
60 
61 int spider_conn_init(
62   SPIDER_CONN *conn
63 );
64 
65 void spider_conn_done(
66   SPIDER_CONN *conn
67 );
68 
69 int spider_reset_conn_setted_parameter(
70   SPIDER_CONN *conn,
71   THD *thd
72 );
73 
74 int spider_free_conn_alloc(
75   SPIDER_CONN *conn
76 );
77 
78 void spider_free_conn_from_trx(
79   SPIDER_TRX *trx,
80   SPIDER_CONN *conn,
81   bool another,
82   bool trx_free,
83   int *roop_count
84 );
85 
86 SPIDER_CONN *spider_create_conn(
87   SPIDER_SHARE *share,
88   ha_spider *spider,
89   int link_id,
90   int base_link_id,
91   uint conn_kind,
92   int *error_num
93 );
94 
95 SPIDER_CONN *spider_get_conn(
96   SPIDER_SHARE *share,
97   int link_idx,
98   char *conn_key,
99   SPIDER_TRX *trx,
100   ha_spider *spider,
101   bool another,
102   bool thd_chg,
103   uint conn_kind,
104   int *error_num
105 );
106 
107 int spider_free_conn(
108   SPIDER_CONN *conn
109 );
110 
111 int spider_check_and_get_casual_read_conn(
112   THD *thd,
113   ha_spider *spider,
114   int link_idx
115 );
116 
117 int spider_check_and_init_casual_read(
118   THD *thd,
119   ha_spider *spider,
120   int link_idx
121 );
122 
123 void spider_conn_queue_connect(
124   SPIDER_SHARE *share,
125   SPIDER_CONN *conn,
126   int link_idx
127 );
128 
129 void spider_conn_queue_connect_rewrite(
130   SPIDER_SHARE *share,
131   SPIDER_CONN *conn,
132   int link_idx
133 );
134 
135 void spider_conn_queue_ping(
136   ha_spider *spider,
137   SPIDER_CONN *conn,
138   int link_idx
139 );
140 
141 void spider_conn_queue_ping_rewrite(
142   ha_spider *spider,
143   SPIDER_CONN *conn,
144   int link_idx
145 );
146 
147 void spider_conn_queue_trx_isolation(
148   SPIDER_CONN *conn,
149   int trx_isolation
150 );
151 
152 void spider_conn_queue_semi_trx_isolation(
153   SPIDER_CONN *conn,
154   int trx_isolation
155 );
156 
157 void spider_conn_queue_autocommit(
158   SPIDER_CONN *conn,
159   bool autocommit
160 );
161 
162 void spider_conn_queue_sql_log_off(
163   SPIDER_CONN *conn,
164   bool sql_log_off
165 );
166 
167 void spider_conn_queue_wait_timeout(
168   SPIDER_CONN *conn,
169   int wait_timeout
170 );
171 
172 void spider_conn_queue_sql_mode(
173   SPIDER_CONN *conn,
174   sql_mode_t sql_mode
175 );
176 
177 void spider_conn_queue_time_zone(
178   SPIDER_CONN *conn,
179   Time_zone *time_zone
180 );
181 
182 void spider_conn_queue_UTC_time_zone(
183   SPIDER_CONN *conn
184 );
185 
186 int spider_conn_queue_and_merge_loop_check(
187   SPIDER_CONN *conn,
188   SPIDER_CONN_LOOP_CHECK *lcptr
189 );
190 
191 int spider_conn_reset_queue_loop_check(
192   SPIDER_CONN *conn
193 );
194 
195 int spider_conn_queue_loop_check(
196   SPIDER_CONN *conn,
197   ha_spider *spider,
198   int link_idx
199 );
200 
201 void spider_conn_queue_start_transaction(
202   SPIDER_CONN *conn
203 );
204 
205 void spider_conn_queue_xa_start(
206   SPIDER_CONN *conn,
207   XID *xid
208 );
209 
210 void spider_conn_clear_queue(
211   SPIDER_CONN *conn
212 );
213 
214 void spider_conn_clear_queue_at_commit(
215   SPIDER_CONN *conn
216 );
217 
218 void spider_conn_set_timeout(
219   SPIDER_CONN *conn,
220   uint net_read_timeout,
221   uint net_write_timeout
222 );
223 
224 void spider_conn_set_timeout_from_share(
225   SPIDER_CONN *conn,
226   int link_idx,
227   THD *thd,
228   SPIDER_SHARE *share
229 );
230 
231 void spider_conn_set_timeout_from_direct_sql(
232   SPIDER_CONN *conn,
233   THD *thd,
234   SPIDER_DIRECT_SQL *direct_sql
235 );
236 
237 void spider_tree_insert(
238   SPIDER_CONN *top,
239   SPIDER_CONN *conn
240 );
241 
242 SPIDER_CONN *spider_tree_first(
243   SPIDER_CONN *top
244 );
245 
246 SPIDER_CONN *spider_tree_last(
247   SPIDER_CONN *top
248 );
249 
250 SPIDER_CONN *spider_tree_next(
251   SPIDER_CONN *current
252 );
253 
254 SPIDER_CONN *spider_tree_delete(
255   SPIDER_CONN *conn,
256   SPIDER_CONN *top
257 );
258 
259 #ifndef WITHOUT_SPIDER_BG_SEARCH
260 int spider_set_conn_bg_param(
261   ha_spider *spider
262 );
263 
264 int spider_create_conn_thread(
265   SPIDER_CONN *conn
266 );
267 
268 void spider_free_conn_thread(
269   SPIDER_CONN *conn
270 );
271 
272 void spider_bg_conn_wait(
273   SPIDER_CONN *conn
274 );
275 
276 void spider_bg_all_conn_wait(
277   ha_spider *spider
278 );
279 
280 int spider_bg_all_conn_pre_next(
281   ha_spider *spider,
282   int link_idx
283 );
284 
285 void spider_bg_conn_break(
286   SPIDER_CONN *conn,
287   ha_spider *spider
288 );
289 
290 void spider_bg_all_conn_break(
291   ha_spider *spider
292 );
293 
294 bool spider_bg_conn_get_job(
295   SPIDER_CONN *conn
296 );
297 
298 int spider_bg_conn_search(
299   ha_spider *spider,
300   int link_idx,
301   int first_link_idx,
302   bool first,
303   bool pre_next,
304   bool discard_result
305 );
306 
307 void spider_bg_conn_simple_action(
308   SPIDER_CONN *conn,
309   uint simple_action,
310   bool caller_wait,
311   void *target,
312   uint link_idx,
313   int *error_num
314 );
315 
316 void *spider_bg_conn_action(
317   void *arg
318 );
319 
320 int spider_create_sts_thread(
321   SPIDER_SHARE *share
322 );
323 
324 void spider_free_sts_thread(
325   SPIDER_SHARE *share
326 );
327 
328 void *spider_bg_sts_action(
329   void *arg
330 );
331 
332 int spider_create_crd_thread(
333   SPIDER_SHARE *share
334 );
335 
336 void spider_free_crd_thread(
337   SPIDER_SHARE *share
338 );
339 
340 void *spider_bg_crd_action(
341   void *arg
342 );
343 
344 int spider_create_mon_threads(
345   SPIDER_TRX *trx,
346   SPIDER_SHARE *share
347 );
348 
349 void spider_free_mon_threads(
350   SPIDER_SHARE *share
351 );
352 
353 void *spider_bg_mon_action(
354   void *arg
355 );
356 #endif
357 
358 int spider_conn_first_link_idx(
359   THD *thd,
360   long *link_statuses,
361   long *access_balances,
362   uint *conn_link_idx,
363   int link_count,
364   int link_status
365 );
366 
367 int spider_conn_next_link_idx(
368   THD *thd,
369   long *link_statuses,
370   long *access_balances,
371   uint *conn_link_idx,
372   int link_idx,
373   int link_count,
374   int link_status
375 );
376 
377 int spider_conn_link_idx_next(
378   long *link_statuses,
379   uint *conn_link_idx,
380   int link_idx,
381   int link_count,
382   int link_status
383 );
384 
385 int spider_conn_get_link_status(
386   long *link_statuses,
387   uint *conn_link_idx,
388   int link_idx
389 );
390 
391 int spider_conn_lock_mode(
392   ha_spider *spider
393 );
394 
395 bool spider_conn_check_recovery_link(
396   SPIDER_SHARE *share
397 );
398 
399 bool spider_conn_use_handler(
400   ha_spider *spider,
401   int lock_mode,
402   int link_idx
403 );
404 
405 bool spider_conn_need_open_handler(
406   ha_spider *spider,
407   uint idx,
408   int link_idx
409 );
410 
411 SPIDER_IP_PORT_CONN *spider_create_ipport_conn(SPIDER_CONN *conn);
412 SPIDER_CONN* spider_get_conn_from_idle_connection
413 (
414  SPIDER_SHARE *share,
415  int link_idx,
416  char *conn_key,
417  ha_spider *spider,
418  uint conn_kind,
419  int base_link_idx,
420  int *error_num
421  );
422 void spider_free_ipport_conn(void *info);
423