drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
que0que.cc
1/*****************************************************************************
2
3Copyright (C) 1996, 2009, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/**************************************************/
25
26#include "que0que.h"
27
28#ifdef UNIV_NONINL
29#include "que0que.ic"
30#endif
31
32#include "usr0sess.h"
33#include "trx0trx.h"
34#include "trx0roll.h"
35#include "row0undo.h"
36#include "row0ins.h"
37#include "row0upd.h"
38#include "row0sel.h"
39#include "row0purge.h"
40#include "dict0crea.h"
41#include "log0log.h"
42#include "eval0proc.h"
43#include "eval0eval.h"
44#include "pars0types.h"
45
46#define QUE_PARALLELIZE_LIMIT (64 * 256 * 256 * 256)
47#define QUE_ROUND_ROBIN_LIMIT (64 * 256 * 256 * 256)
48#ifdef UNIV_DEBUG
49/* If the following flag is set TRUE, the module will print trace info
50of SQL execution in the UNIV_SQL_DEBUG version */
51UNIV_INTERN ibool que_trace_on = FALSE;
52#endif /* UNIV_DEBUG */
53
54/* Short introduction to query graphs
55 ==================================
56
57A query graph consists of nodes linked to each other in various ways. The
58execution starts at que_run_threads() which takes a que_thr_t parameter.
59que_thr_t contains two fields that control query graph execution: run_node
60and prev_node. run_node is the next node to execute and prev_node is the
61last node executed.
62
63Each node has a pointer to a 'next' statement, i.e., its brother, and a
64pointer to its parent node. The next pointer is NULL in the last statement
65of a block.
66
67Loop nodes contain a link to the first statement of the enclosed statement
68list. While the loop runs, que_thr_step() checks if execution to the loop
69node came from its parent or from one of the statement nodes in the loop. If
70it came from the parent of the loop node it starts executing the first
71statement node in the loop. If it came from one of the statement nodes in
72the loop, then it checks if the statement node has another statement node
73following it, and runs it if so.
74
75To signify loop ending, the loop statements (see e.g. while_step()) set
76que_thr_t->run_node to the loop node's parent node. This is noticed on the
77next call of que_thr_step() and execution proceeds to the node pointed to by
78the loop node's 'next' pointer.
79
80For example, the code:
81
82X := 1;
83WHILE X < 5 LOOP
84 X := X + 1;
85 X := X + 1;
86X := 5
87
88will result in the following node hierarchy, with the X-axis indicating
89'next' links and the Y-axis indicating parent/child links:
90
91A - W - A
92 |
93 |
94 A - A
95
96A = assign_node_t, W = while_node_t. */
97
98/* How a stored procedure containing COMMIT or ROLLBACK commands
99is executed?
100
101The commit or rollback can be seen as a subprocedure call.
102The problem is that if there are several query threads
103currently running within the transaction, their action could
104mess the commit or rollback operation. Or, at the least, the
105operation would be difficult to visualize and keep in control.
106
107Therefore the query thread requesting a commit or a rollback
108sends to the transaction a signal, which moves the transaction
109to TRX_QUE_SIGNALED state. All running query threads of the
110transaction will eventually notice that the transaction is now in
111this state and voluntarily suspend themselves. Only the last
112query thread which suspends itself will trigger handling of
113the signal.
114
115When the transaction starts to handle a rollback or commit
116signal, it builds a query graph which, when executed, will
117roll back or commit the incomplete transaction. The transaction
118is moved to the TRX_QUE_ROLLING_BACK or TRX_QUE_COMMITTING state.
119If specified, the SQL cursors opened by the transaction are closed.
120When the execution of the graph completes, it is like returning
121from a subprocedure: the query thread which requested the operation
122starts running again. */
123
124/**********************************************************************/
129static
130void
131que_thr_move_to_run_state(
132/*======================*/
133 que_thr_t* thr);
134
135/***********************************************************************/
137UNIV_INTERN
138void
140/*==============*/
141 que_t* graph,
142 sess_t* sess)
143{
144 ut_ad(mutex_own(&kernel_mutex));
145
146 UT_LIST_ADD_LAST(graphs, sess->graphs, graph);
147}
148
149/***********************************************************************/
152UNIV_INTERN
153que_fork_t*
155/*============*/
156 que_t* graph,
159 que_node_t* parent,
160 ulint fork_type,
161 mem_heap_t* heap)
162{
163 que_fork_t* fork;
164
165 ut_ad(heap);
166
167 fork = static_cast<que_fork_t *>(mem_heap_alloc(heap, sizeof(que_fork_t)));
168
169 fork->common.type = QUE_NODE_FORK;
170 fork->n_active_thrs = 0;
171
172 fork->state = QUE_FORK_COMMAND_WAIT;
173
174 if (graph != NULL) {
175 fork->graph = graph;
176 } else {
177 fork->graph = fork;
178 }
179
180 fork->common.parent = parent;
181 fork->fork_type = fork_type;
182
183 fork->caller = NULL;
184
185 UT_LIST_INIT(fork->thrs);
186
187 fork->sym_tab = NULL;
188 fork->info = NULL;
189
190 fork->heap = heap;
191
192 return(fork);
193}
194
195/***********************************************************************/
198UNIV_INTERN
199que_thr_t*
201/*===========*/
202 que_fork_t* parent,
203 mem_heap_t* heap)
204{
205 que_thr_t* thr;
206
207 ut_ad(parent && heap);
208
209 thr = static_cast<que_thr_t *>(mem_heap_alloc(heap, sizeof(que_thr_t)));
210
211 thr->common.type = QUE_NODE_THR;
212 thr->common.parent = parent;
213
214 thr->magic_n = QUE_THR_MAGIC_N;
215
216 thr->graph = parent->graph;
217
218 thr->state = QUE_THR_COMMAND_WAIT;
219
220 thr->is_active = FALSE;
221
222 thr->run_node = NULL;
223 thr->resource = 0;
224 thr->lock_state = QUE_THR_LOCK_NOLOCK;
225
226 UT_LIST_ADD_LAST(thrs, parent->thrs, thr);
227
228 return(thr);
229}
230
231/**********************************************************************/
236UNIV_INTERN
237void
239/*=============*/
240 que_thr_t* thr,
244 que_thr_t** next_thr)
250{
251 ibool was_active;
252
253 ut_ad(mutex_own(&kernel_mutex));
254 ut_ad(thr);
255 ut_ad((thr->state == QUE_THR_LOCK_WAIT)
256 || (thr->state == QUE_THR_PROCEDURE_WAIT)
257 || (thr->state == QUE_THR_SIG_REPLY_WAIT));
258 ut_ad(thr->run_node);
259
260 thr->prev_node = thr->run_node;
261
262 was_active = thr->is_active;
263
264 que_thr_move_to_run_state(thr);
265
266 if (was_active) {
267
268 return;
269 }
270
271 if (next_thr && *next_thr == NULL) {
272 *next_thr = thr;
273 } else {
274 ut_a(0);
276 }
277}
278
279/**********************************************************************/
281UNIV_INTERN
282void
284/*=========================*/
285 que_thr_t* thr)
288{
289 ibool was_active;
290
291 ut_a(thr->state == QUE_THR_LOCK_WAIT); /* In MySQL this is the
292 only possible state here */
293 ut_ad(mutex_own(&kernel_mutex));
294 ut_ad(thr);
295 ut_ad((thr->state == QUE_THR_LOCK_WAIT)
296 || (thr->state == QUE_THR_PROCEDURE_WAIT)
297 || (thr->state == QUE_THR_SIG_REPLY_WAIT));
298
299 was_active = thr->is_active;
300
301 que_thr_move_to_run_state(thr);
302
303 if (was_active) {
304
305 return;
306 }
307
308 /* In MySQL we let the OS thread (not just the query thread) to wait
309 for the lock to be released: */
310
312
313 /* srv_que_task_enqueue_low(thr); */
314}
315
316/**********************************************************************/
318UNIV_INLINE
319void
320que_thr_init_command(
321/*=================*/
322 que_thr_t* thr)
323{
324 thr->run_node = thr;
325 thr->prev_node = thr->common.parent;
326
327 que_thr_move_to_run_state(thr);
328}
329
330/**********************************************************************/
338UNIV_INTERN
339que_thr_t*
341/*===================*/
342 que_fork_t* fork)
343{
344 que_thr_t* thr;
345 que_thr_t* suspended_thr = NULL;
346 que_thr_t* completed_thr = NULL;
347
348 fork->state = QUE_FORK_ACTIVE;
349
350 fork->last_sel_node = NULL;
351
352 suspended_thr = NULL;
353 completed_thr = NULL;
354
355 /* Choose the query thread to run: usually there is just one thread,
356 but in a parallelized select, which necessarily is non-scrollable,
357 there may be several to choose from */
358
359 /* First we try to find a query thread in the QUE_THR_COMMAND_WAIT
360 state. Then we try to find a query thread in the QUE_THR_SUSPENDED
361 state, finally we try to find a query thread in the QUE_THR_COMPLETED
362 state */
363
364 thr = UT_LIST_GET_FIRST(fork->thrs);
365
366 /* We make a single pass over the thr list within which we note which
367 threads are ready to run. */
368 while (thr) {
369 switch (thr->state) {
370 case QUE_THR_COMMAND_WAIT:
371
372 /* We have to send the initial message to query thread
373 to start it */
374
375 que_thr_init_command(thr);
376
377 return(thr);
378
379 case QUE_THR_SUSPENDED:
380 /* In this case the execution of the thread was
381 suspended: no initial message is needed because
382 execution can continue from where it was left */
383 if (!suspended_thr) {
384 suspended_thr = thr;
385 }
386
387 break;
388
389 case QUE_THR_COMPLETED:
390 if (!completed_thr) {
391 completed_thr = thr;
392 }
393
394 break;
395
396 case QUE_THR_LOCK_WAIT:
397 ut_error;
398
399 }
400
401 thr = UT_LIST_GET_NEXT(thrs, thr);
402 }
403
404 if (suspended_thr) {
405
406 thr = suspended_thr;
407 que_thr_move_to_run_state(thr);
408
409 } else if (completed_thr) {
410
411 thr = completed_thr;
412 que_thr_init_command(thr);
413 }
414
415 return(thr);
416}
417
418/**********************************************************************/
422UNIV_INTERN
423void
425/*==================*/
426 trx_t* /*trx __attribute__((unused))*/,
427 que_t* fork)
429{
430 que_thr_t* thr;
431
432 ut_ad(mutex_own(&kernel_mutex));
433 ut_ad(trx->sess->state == SESS_ERROR);
434 ut_ad(UT_LIST_GET_LEN(trx->reply_signals) == 0);
435 ut_ad(UT_LIST_GET_LEN(trx->wait_thrs) == 0);
436
437 thr = UT_LIST_GET_FIRST(fork->thrs);
438
439 while (thr != NULL) {
440 ut_ad(!thr->is_active);
441 ut_ad(thr->state != QUE_THR_SIG_REPLY_WAIT);
442 ut_ad(thr->state != QUE_THR_LOCK_WAIT);
443
444 thr->run_node = thr;
445 thr->prev_node = thr->child;
446 thr->state = QUE_THR_COMPLETED;
447
448 thr = UT_LIST_GET_NEXT(thrs, thr);
449 }
450
451 thr = UT_LIST_GET_FIRST(fork->thrs);
452
453 que_thr_move_to_run_state(thr);
454
455 ut_a(0);
457}
458
459/****************************************************************/
463UNIV_INLINE
464ibool
465que_fork_all_thrs_in_state(
466/*=======================*/
467 que_fork_t* fork,
468 ulint state)
469{
470 que_thr_t* thr_node;
471
472 thr_node = UT_LIST_GET_FIRST(fork->thrs);
473
474 while (thr_node != NULL) {
475 if (thr_node->state != state) {
476
477 return(FALSE);
478 }
479
480 thr_node = UT_LIST_GET_NEXT(thrs, thr_node);
481 }
482
483 return(TRUE);
484}
485
486/**********************************************************************/
488static
489void
490que_graph_free_stat_list(
491/*=====================*/
492 que_node_t* node)
493{
494 while (node) {
496
497 node = que_node_get_next(node);
498 }
499}
500
501/**********************************************************************/
504UNIV_INTERN
505void
507/*=====================*/
508 que_node_t* node)
509{
510 que_fork_t* fork;
511 que_thr_t* thr;
512 undo_node_t* undo;
513 sel_node_t* sel;
514 ins_node_t* ins;
515 upd_node_t* upd;
516 tab_node_t* cre_tab;
517 ind_node_t* cre_ind;
518 purge_node_t* purge;
519
520 if (node == NULL) {
521
522 return;
523 }
524
525 switch (que_node_get_type(node)) {
526
527 case QUE_NODE_FORK:
528 fork = static_cast<que_fork_t *>(node);
529
530 thr = UT_LIST_GET_FIRST(fork->thrs);
531
532 while (thr) {
534
535 thr = UT_LIST_GET_NEXT(thrs, thr);
536 }
537
538 break;
539 case QUE_NODE_THR:
540
541 thr = static_cast<que_thr_t *>(node);
542
543 if (thr->magic_n != QUE_THR_MAGIC_N) {
544 fprintf(stderr,
545 "que_thr struct appears corrupt;"
546 " magic n %lu\n",
547 (unsigned long) thr->magic_n);
549 ut_error;
550 }
551
552 thr->magic_n = QUE_THR_MAGIC_FREED;
553
554 que_graph_free_recursive(thr->child);
555
556 break;
557 case QUE_NODE_UNDO:
558
559 undo = static_cast<undo_node_t *>(node);
560
561 mem_heap_free(undo->heap);
562
563 break;
564 case QUE_NODE_SELECT:
565
566 sel = static_cast<sel_node_t *>(node);
567
569
570 break;
571 case QUE_NODE_INSERT:
572
573 ins = static_cast<ins_node_t *>(node);
574
576
577 mem_heap_free(ins->entry_sys_heap);
578
579 break;
580 case QUE_NODE_PURGE:
581 purge = static_cast<purge_node_t *>(node);
582
583 mem_heap_free(purge->heap);
584
585 break;
586
587 case QUE_NODE_UPDATE:
588
589 upd = static_cast<upd_node_t *>(node);
590
591 if (upd->in_mysql_interface) {
592
593 btr_pcur_free_for_mysql(upd->pcur);
594 }
595
596 que_graph_free_recursive(upd->cascade_node);
597
598 if (upd->cascade_heap) {
599 mem_heap_free(upd->cascade_heap);
600 }
601
603
604 mem_heap_free(upd->heap);
605
606 break;
607 case QUE_NODE_CREATE_TABLE:
608 cre_tab = static_cast<tab_node_t *>(node);
609
610 que_graph_free_recursive(cre_tab->tab_def);
611 que_graph_free_recursive(cre_tab->col_def);
612 que_graph_free_recursive(cre_tab->commit_node);
613
614 mem_heap_free(cre_tab->heap);
615
616 break;
617 case QUE_NODE_CREATE_INDEX:
618 cre_ind = static_cast<ind_node_t *>(node);
619
620 que_graph_free_recursive(cre_ind->ind_def);
621 que_graph_free_recursive(cre_ind->field_def);
622 que_graph_free_recursive(cre_ind->commit_node);
623
624 mem_heap_free(cre_ind->heap);
625
626 break;
627 case QUE_NODE_PROC:
628 que_graph_free_stat_list(((proc_node_t*)node)->stat_list);
629
630 break;
631 case QUE_NODE_IF:
632 que_graph_free_stat_list(((if_node_t*)node)->stat_list);
633 que_graph_free_stat_list(((if_node_t*)node)->else_part);
634 que_graph_free_stat_list(((if_node_t*)node)->elsif_list);
635
636 break;
637 case QUE_NODE_ELSIF:
638 que_graph_free_stat_list(((elsif_node_t*)node)->stat_list);
639
640 break;
641 case QUE_NODE_WHILE:
642 que_graph_free_stat_list(((while_node_t*)node)->stat_list);
643
644 break;
645 case QUE_NODE_FOR:
646 que_graph_free_stat_list(((for_node_t*)node)->stat_list);
647
648 break;
649
650 case QUE_NODE_ASSIGNMENT:
651 case QUE_NODE_EXIT:
652 case QUE_NODE_RETURN:
653 case QUE_NODE_COMMIT:
654 case QUE_NODE_ROLLBACK:
655 case QUE_NODE_LOCK:
656 case QUE_NODE_FUNC:
657 case QUE_NODE_ORDER:
658 case QUE_NODE_ROW_PRINTF:
659 case QUE_NODE_OPEN:
660 case QUE_NODE_FETCH:
661 /* No need to do anything */
662
663 break;
664 default:
665 fprintf(stderr,
666 "que_node struct appears corrupt; type %lu\n",
667 (unsigned long) que_node_get_type(node));
669 ut_error;
670 }
671}
672
673/**********************************************************************/
675UNIV_INTERN
676void
678/*===========*/
679 que_t* graph)
684{
685 ut_ad(graph);
686
687 if (graph->sym_tab) {
688 /* The following call frees dynamic memory allocated
689 for variables etc. during execution. Frees also explicit
690 cursor definitions. */
691
693 }
694
695 if (graph->info && graph->info->graph_owns_us) {
696 pars_info_free(graph->info);
697 }
698
700
701 mem_heap_free(graph->heap);
702}
703
704/****************************************************************/
707static
708que_thr_t*
709que_thr_node_step(
710/*==============*/
711 que_thr_t* thr)
713{
714 ut_ad(thr->run_node == thr);
715
716 if (thr->prev_node == thr->common.parent) {
717 /* If control to the node came from above, it is just passed
718 on */
719
720 thr->run_node = thr->child;
721
722 return(thr);
723 }
724
725 mutex_enter(&kernel_mutex);
726
727 if (que_thr_peek_stop(thr)) {
728
729 mutex_exit(&kernel_mutex);
730
731 return(thr);
732 }
733
734 /* Thread execution completed */
735
736 thr->state = QUE_THR_COMPLETED;
737
738 mutex_exit(&kernel_mutex);
739
740 return(NULL);
741}
742
743/**********************************************************************/
749static
750void
751que_thr_move_to_run_state(
752/*======================*/
753 que_thr_t* thr)
754{
755 trx_t* trx;
756
757 ut_ad(thr->state != QUE_THR_RUNNING);
758
759 trx = thr_get_trx(thr);
760
761 if (!thr->is_active) {
762
763 (thr->graph)->n_active_thrs++;
764
765 trx->n_active_thrs++;
766
767 thr->is_active = TRUE;
768
769 ut_ad((thr->graph)->n_active_thrs == 1);
770 ut_ad(trx->n_active_thrs == 1);
771 }
772
773 thr->state = QUE_THR_RUNNING;
774}
775
776/**********************************************************************/
784static
785void
786que_thr_dec_refer_count(
787/*====================*/
788 que_thr_t* thr,
789 que_thr_t** next_thr)
794{
795 que_fork_t* fork;
796 trx_t* trx;
797 ulint fork_type;
798 ibool stopped;
799
800 fork = static_cast<que_fork_t *>(thr->common.parent);
801 trx = thr_get_trx(thr);
802
803 mutex_enter(&kernel_mutex);
804
805 ut_a(thr->is_active);
806
807 if (thr->state == QUE_THR_RUNNING) {
808
809 stopped = que_thr_stop(thr);
810
811 if (!stopped) {
812 /* The reason for the thr suspension or wait was
813 already canceled before we came here: continue
814 running the thread */
815
816 /* fputs("!!!!!!!! Wait already ended: continue thr\n",
817 stderr); */
818
819 if (next_thr && *next_thr == NULL) {
820 /* Normally srv_suspend_mysql_thread resets
821 the state to DB_SUCCESS before waiting, but
822 in this case we have to do it here,
823 otherwise nobody does it. */
824 trx->error_state = DB_SUCCESS;
825
826 *next_thr = thr;
827 } else {
828 ut_error;
830 }
831
832 mutex_exit(&kernel_mutex);
833
834 return;
835 }
836 }
837
838 ut_ad(fork->n_active_thrs == 1);
839 ut_ad(trx->n_active_thrs == 1);
840
841 fork->n_active_thrs--;
842 trx->n_active_thrs--;
843
844 thr->is_active = FALSE;
845
846 if (trx->n_active_thrs > 0) {
847
848 mutex_exit(&kernel_mutex);
849
850 return;
851 }
852
853 fork_type = fork->fork_type;
854
855 /* Check if all query threads in the same fork are completed */
856
857 if (que_fork_all_thrs_in_state(fork, QUE_THR_COMPLETED)) {
858
859 switch (fork_type) {
860 case QUE_FORK_ROLLBACK:
861 /* This is really the undo graph used in rollback,
862 no roll_node in this graph */
863
864 ut_ad(UT_LIST_GET_LEN(trx->signals) > 0);
865 ut_ad(trx->handling_signals == TRUE);
866
867 trx_finish_rollback_off_kernel(fork, trx, next_thr);
868 break;
869
870 case QUE_FORK_PURGE:
871 case QUE_FORK_RECOVERY:
872 case QUE_FORK_MYSQL_INTERFACE:
873
874 /* Do nothing */
875 break;
876
877 default:
878 ut_error;
879 }
880 }
881
882 if (UT_LIST_GET_LEN(trx->signals) > 0 && trx->n_active_thrs == 0) {
883
884 /* If the trx is signaled and its query thread count drops to
885 zero, then we start processing a signal; from it we may get
886 a new query thread to run */
887
888 trx_sig_start_handle(trx, next_thr);
889 }
890
891 if (trx->handling_signals && UT_LIST_GET_LEN(trx->signals) == 0) {
892
894 }
895
896 mutex_exit(&kernel_mutex);
897}
898
899/**********************************************************************/
904UNIV_INTERN
905ibool
907/*=========*/
908 que_thr_t* thr)
909{
910 trx_t* trx;
911 que_t* graph;
912 ibool ret = TRUE;
913
914 ut_ad(mutex_own(&kernel_mutex));
915
916 graph = thr->graph;
917 trx = graph->trx;
918
919 if (graph->state == QUE_FORK_COMMAND_WAIT) {
920 thr->state = QUE_THR_SUSPENDED;
921
922 } else if (trx->que_state == TRX_QUE_LOCK_WAIT) {
923
924 UT_LIST_ADD_FIRST(trx_thrs, trx->wait_thrs, thr);
925 thr->state = QUE_THR_LOCK_WAIT;
926
927 } else if (trx->error_state != DB_SUCCESS
928 && trx->error_state != DB_LOCK_WAIT) {
929
930 /* Error handling built for the MySQL interface */
931 thr->state = QUE_THR_COMPLETED;
932
933 } else if (UT_LIST_GET_LEN(trx->signals) > 0
934 && graph->fork_type != QUE_FORK_ROLLBACK) {
935
936 thr->state = QUE_THR_SUSPENDED;
937 } else {
938 ut_ad(graph->state == QUE_FORK_ACTIVE);
939
940 ret = FALSE;
941 }
942
943 return(ret);
944}
945
946/**********************************************************************/
951UNIV_INTERN
952void
954/*===================*/
955 que_thr_t* thr)
956{
957 trx_t* trx;
958
959 trx = thr_get_trx(thr);
960
961 mutex_enter(&kernel_mutex);
962
963 if (thr->state == QUE_THR_RUNNING) {
964
965 if (trx->error_state != DB_SUCCESS
966 && trx->error_state != DB_LOCK_WAIT) {
967
968 /* Error handling built for the MySQL interface */
969 thr->state = QUE_THR_COMPLETED;
970 } else {
971 /* It must have been a lock wait but the lock was
972 already released, or this transaction was chosen
973 as a victim in selective deadlock resolution */
974
975 mutex_exit(&kernel_mutex);
976
977 return;
978 }
979 }
980
981 ut_ad(thr->is_active == TRUE);
982 ut_ad(trx->n_active_thrs == 1);
983 ut_ad(thr->graph->n_active_thrs == 1);
984
985 thr->is_active = FALSE;
986 (thr->graph)->n_active_thrs--;
987
988 trx->n_active_thrs--;
989
990 mutex_exit(&kernel_mutex);
991}
992
993/**********************************************************************/
997UNIV_INTERN
998void
1000/*================================*/
1001 que_thr_t* thr,
1002 trx_t* trx)
1003{
1004 if (thr->magic_n != QUE_THR_MAGIC_N) {
1005 fprintf(stderr,
1006 "que_thr struct appears corrupt; magic n %lu\n",
1007 (unsigned long) thr->magic_n);
1008
1010
1011 ut_error;
1012 }
1013
1014 if (!thr->is_active) {
1015
1016 thr->graph->n_active_thrs++;
1017
1018 trx->n_active_thrs++;
1019
1020 thr->is_active = TRUE;
1021 }
1022
1023 thr->state = QUE_THR_RUNNING;
1024}
1025
1026/**********************************************************************/
1029UNIV_INTERN
1030void
1032/*============================*/
1033 que_thr_t* thr,
1034 trx_t* trx)
1035{
1036 ut_ad(thr->state == QUE_THR_RUNNING);
1037 ut_ad(thr->is_active == TRUE);
1038 ut_ad(trx->n_active_thrs == 1);
1039 ut_ad(thr->graph->n_active_thrs == 1);
1040
1041 if (thr->magic_n != QUE_THR_MAGIC_N) {
1042 fprintf(stderr,
1043 "que_thr struct appears corrupt; magic n %lu\n",
1044 (unsigned long) thr->magic_n);
1045
1047
1048 ut_error;
1049 }
1050
1051 thr->state = QUE_THR_COMPLETED;
1052
1053 thr->is_active = FALSE;
1054 (thr->graph)->n_active_thrs--;
1055
1056 trx->n_active_thrs--;
1057}
1058
1059/****************************************************************/
1063UNIV_INTERN
1064que_node_t*
1066/*==============================*/
1067 que_node_t* node)
1068{
1069 ut_ad(node);
1070
1071 for (;;) {
1072 ulint type;
1073
1074 node = que_node_get_parent(node);
1075
1076 if (!node) {
1077 break;
1078 }
1079
1080 type = que_node_get_type(node);
1081
1082 if ((type == QUE_NODE_FOR) || (type == QUE_NODE_WHILE)) {
1083 break;
1084 }
1085 }
1086
1087 return(node);
1088}
1089
1090/**********************************************************************/
1092UNIV_INTERN
1093void
1095/*================*/
1096 que_node_t* node)
1097{
1098 ulint type;
1099 const char* str;
1100
1101 type = que_node_get_type(node);
1102
1103 if (type == QUE_NODE_SELECT) {
1104 str = "SELECT";
1105 } else if (type == QUE_NODE_INSERT) {
1106 str = "INSERT";
1107 } else if (type == QUE_NODE_UPDATE) {
1108 str = "UPDATE";
1109 } else if (type == QUE_NODE_WHILE) {
1110 str = "WHILE";
1111 } else if (type == QUE_NODE_ASSIGNMENT) {
1112 str = "ASSIGNMENT";
1113 } else if (type == QUE_NODE_IF) {
1114 str = "IF";
1115 } else if (type == QUE_NODE_FETCH) {
1116 str = "FETCH";
1117 } else if (type == QUE_NODE_OPEN) {
1118 str = "OPEN";
1119 } else if (type == QUE_NODE_PROC) {
1120 str = "STORED PROCEDURE";
1121 } else if (type == QUE_NODE_FUNC) {
1122 str = "FUNCTION";
1123 } else if (type == QUE_NODE_LOCK) {
1124 str = "LOCK";
1125 } else if (type == QUE_NODE_THR) {
1126 str = "QUERY THREAD";
1127 } else if (type == QUE_NODE_COMMIT) {
1128 str = "COMMIT";
1129 } else if (type == QUE_NODE_UNDO) {
1130 str = "UNDO ROW";
1131 } else if (type == QUE_NODE_PURGE) {
1132 str = "PURGE ROW";
1133 } else if (type == QUE_NODE_ROLLBACK) {
1134 str = "ROLLBACK";
1135 } else if (type == QUE_NODE_CREATE_TABLE) {
1136 str = "CREATE TABLE";
1137 } else if (type == QUE_NODE_CREATE_INDEX) {
1138 str = "CREATE INDEX";
1139 } else if (type == QUE_NODE_FOR) {
1140 str = "FOR LOOP";
1141 } else if (type == QUE_NODE_RETURN) {
1142 str = "RETURN";
1143 } else if (type == QUE_NODE_EXIT) {
1144 str = "EXIT";
1145 } else {
1146 str = "UNKNOWN NODE TYPE";
1147 }
1148
1149 fprintf(stderr, "Node type %lu: %s, address %p\n",
1150 (ulong) type, str, (void*) node);
1151}
1152
1153/**********************************************************************/
1157UNIV_INLINE
1158que_thr_t*
1159que_thr_step(
1160/*=========*/
1161 que_thr_t* thr)
1162{
1163 que_node_t* node;
1164 que_thr_t* old_thr;
1165 trx_t* trx;
1166 ulint type;
1167
1168 trx = thr_get_trx(thr);
1169
1170 ut_ad(thr->state == QUE_THR_RUNNING);
1171 ut_a(trx->error_state == DB_SUCCESS);
1172
1173 thr->resource++;
1174
1175 node = thr->run_node;
1176 type = que_node_get_type(node);
1177
1178 old_thr = thr;
1179
1180#ifdef UNIV_DEBUG
1181 if (que_trace_on) {
1182 fputs("To execute: ", stderr);
1183 que_node_print_info(node);
1184 }
1185#endif
1186 if (type & QUE_NODE_CONTROL_STAT) {
1187 if ((thr->prev_node != que_node_get_parent(node))
1188 && que_node_get_next(thr->prev_node)) {
1189
1190 /* The control statements, like WHILE, always pass the
1191 control to the next child statement if there is any
1192 child left */
1193
1194 thr->run_node = que_node_get_next(thr->prev_node);
1195
1196 } else if (type == QUE_NODE_IF) {
1197 if_step(thr);
1198 } else if (type == QUE_NODE_FOR) {
1199 for_step(thr);
1200 } else if (type == QUE_NODE_PROC) {
1201
1202 /* We can access trx->undo_no without reserving
1203 trx->undo_mutex, because there cannot be active query
1204 threads doing updating or inserting at the moment! */
1205
1206 if (thr->prev_node == que_node_get_parent(node)) {
1207 trx->last_sql_stat_start.least_undo_no
1208 = trx->undo_no;
1209 }
1210
1211 proc_step(thr);
1212 } else if (type == QUE_NODE_WHILE) {
1213 while_step(thr);
1214 } else {
1215 ut_error;
1216 }
1217 } else if (type == QUE_NODE_ASSIGNMENT) {
1218 assign_step(thr);
1219 } else if (type == QUE_NODE_SELECT) {
1220 thr = row_sel_step(thr);
1221 } else if (type == QUE_NODE_INSERT) {
1222 thr = row_ins_step(thr);
1223 } else if (type == QUE_NODE_UPDATE) {
1224 thr = row_upd_step(thr);
1225 } else if (type == QUE_NODE_FETCH) {
1226 thr = fetch_step(thr);
1227 } else if (type == QUE_NODE_OPEN) {
1228 thr = open_step(thr);
1229 } else if (type == QUE_NODE_FUNC) {
1230 proc_eval_step(thr);
1231
1232 } else if (type == QUE_NODE_LOCK) {
1233
1234 ut_error;
1235 /*
1236 thr = que_lock_step(thr);
1237 */
1238 } else if (type == QUE_NODE_THR) {
1239 thr = que_thr_node_step(thr);
1240 } else if (type == QUE_NODE_COMMIT) {
1241 thr = trx_commit_step(thr);
1242 } else if (type == QUE_NODE_UNDO) {
1243 thr = row_undo_step(thr);
1244 } else if (type == QUE_NODE_PURGE) {
1245 thr = row_purge_step(thr);
1246 } else if (type == QUE_NODE_RETURN) {
1247 thr = return_step(thr);
1248 } else if (type == QUE_NODE_EXIT) {
1249 thr = exit_step(thr);
1250 } else if (type == QUE_NODE_ROLLBACK) {
1251 thr = trx_rollback_step(thr);
1252 } else if (type == QUE_NODE_CREATE_TABLE) {
1253 thr = dict_create_table_step(thr);
1254 } else if (type == QUE_NODE_CREATE_INDEX) {
1255 thr = dict_create_index_step(thr);
1256 } else if (type == QUE_NODE_ROW_PRINTF) {
1257 thr = row_printf_step(thr);
1258 } else {
1259 ut_error;
1260 }
1261
1262 if (type == QUE_NODE_EXIT) {
1264 } else {
1265 old_thr->prev_node = node;
1266 }
1267
1268 if (thr) {
1269 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
1270 }
1271
1272 return(thr);
1273}
1274
1275/**********************************************************************/
1277static
1278void
1279que_run_threads_low(
1280/*================*/
1281 que_thr_t* thr)
1282{
1283 que_thr_t* next_thr;
1284
1285 ut_ad(thr->state == QUE_THR_RUNNING);
1286 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
1287 ut_ad(!mutex_own(&kernel_mutex));
1288
1289loop:
1290 /* Check that there is enough space in the log to accommodate
1291 possible log entries by this query step; if the operation can touch
1292 more than about 4 pages, checks must be made also within the query
1293 step! */
1294
1296
1297 /* Perform the actual query step: note that the query thread
1298 may change if, e.g., a subprocedure call is made */
1299
1300 /*-------------------------*/
1301 next_thr = que_thr_step(thr);
1302 /*-------------------------*/
1303
1304 ut_a(!next_thr || (thr_get_trx(next_thr)->error_state == DB_SUCCESS));
1305
1306 if (next_thr != thr) {
1307 ut_a(next_thr == NULL);
1308
1309 /* This can change next_thr to a non-NULL value if there was
1310 a lock wait that already completed. */
1311 que_thr_dec_refer_count(thr, &next_thr);
1312
1313 if (next_thr == NULL) {
1314
1315 return;
1316 }
1317
1318 thr = next_thr;
1319 }
1320
1321 goto loop;
1322}
1323
1324/**********************************************************************/
1326UNIV_INTERN
1327void
1329/*============*/
1330 que_thr_t* thr)
1331{
1332loop:
1333 ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
1334 que_run_threads_low(thr);
1335
1336 mutex_enter(&kernel_mutex);
1337
1338 switch (thr->state) {
1339
1340 case QUE_THR_RUNNING:
1341 /* There probably was a lock wait, but it already ended
1342 before we came here: continue running thr */
1343
1344 mutex_exit(&kernel_mutex);
1345
1346 goto loop;
1347
1348 case QUE_THR_LOCK_WAIT:
1349 mutex_exit(&kernel_mutex);
1350
1351 /* The ..._mysql_... function works also for InnoDB's
1352 internal threads. Let us wait that the lock wait ends. */
1353
1355
1356 if (thr_get_trx(thr)->error_state != DB_SUCCESS) {
1357 /* thr was chosen as a deadlock victim or there was
1358 a lock wait timeout */
1359
1360 que_thr_dec_refer_count(thr, NULL);
1361
1362 return;
1363 }
1364
1365 goto loop;
1366
1367 case QUE_THR_COMPLETED:
1368 case QUE_THR_COMMAND_WAIT:
1369 /* Do nothing */
1370 break;
1371
1372 default:
1373 ut_error;
1374 }
1375
1376 mutex_exit(&kernel_mutex);
1377}
1378
1379/*********************************************************************/
1382UNIV_INTERN
1383ulint
1385/*=========*/
1386 pars_info_t* info,
1387 const char* sql,
1388 ibool reserve_dict_mutex,
1391 trx_t* trx)
1392{
1393 que_thr_t* thr;
1394 que_t* graph;
1395
1396 ut_a(trx->error_state == DB_SUCCESS);
1397
1398 if (reserve_dict_mutex) {
1399 mutex_enter(&dict_sys->mutex);
1400 }
1401
1402 graph = pars_sql(info, sql);
1403
1404 if (reserve_dict_mutex) {
1405 mutex_exit(&dict_sys->mutex);
1406 }
1407
1408 ut_a(graph);
1409
1410 graph->trx = trx;
1411 trx->graph = NULL;
1412
1413 graph->fork_type = QUE_FORK_MYSQL_INTERFACE;
1414
1415 ut_a(thr = que_fork_start_command(graph));
1416
1417 que_run_threads(thr);
1418
1419 que_graph_free(graph);
1420
1421 return(trx->error_state);
1422}
UNIV_INLINE que_thr_t * proc_eval_step(que_thr_t *thr)
UNIV_INLINE que_thr_t * proc_step(que_thr_t *thr)
UNIV_INLINE void log_free_check(void)
UNIV_INTERN void mem_analyze_corruption(void *ptr)
Definition mem0dbg.cc:822
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
#define mem_heap_free(heap)
Definition mem0mem.h:111
UNIV_INTERN void pars_info_free(pars_info_t *info)
UNIV_INTERN que_t * pars_sql(pars_info_t *info, const char *str)
UNIV_INTERN void sym_tab_free_private(sym_tab_t *sym_tab)
Definition pars0sym.cc:71
UNIV_INTERN void que_thr_stop_for_mysql(que_thr_t *thr)
Definition que0que.cc:953
UNIV_INLINE ulint que_node_get_type(que_node_t *node)
UNIV_INTERN void que_thr_move_to_run_state_for_mysql(que_thr_t *thr, trx_t *trx)
Definition que0que.cc:999
UNIV_INLINE que_node_t * que_node_get_parent(que_node_t *node)
UNIV_INTERN void que_graph_free(que_t *graph)
Definition que0que.cc:677
UNIV_INTERN void que_fork_error_handle(trx_t *trx, que_t *fork)
Definition que0que.cc:424
UNIV_INTERN ibool que_thr_stop(que_thr_t *thr)
Definition que0que.cc:906
UNIV_INTERN void que_run_threads(que_thr_t *thr)
Definition que0que.cc:1328
UNIV_INTERN ulint que_eval_sql(pars_info_t *info, const char *sql, ibool reserve_dict_mutex, trx_t *trx)
Definition que0que.cc:1384
UNIV_INTERN que_thr_t * que_thr_create(que_fork_t *parent, mem_heap_t *heap)
Definition que0que.cc:200
UNIV_INTERN void que_thr_end_wait(que_thr_t *thr, que_thr_t **next_thr)
Definition que0que.cc:238
UNIV_INLINE ibool que_thr_peek_stop(que_thr_t *thr)
UNIV_INTERN void que_node_print_info(que_node_t *node)
Definition que0que.cc:1094
UNIV_INTERN que_thr_t * que_fork_start_command(que_fork_t *fork)
Definition que0que.cc:340
UNIV_INTERN void que_thr_stop_for_mysql_no_error(que_thr_t *thr, trx_t *trx)
Definition que0que.cc:1031
UNIV_INTERN que_fork_t * que_fork_create(que_t *graph, que_node_t *parent, ulint fork_type, mem_heap_t *heap)
Definition que0que.cc:154
UNIV_INTERN void que_graph_publish(que_t *graph, sess_t *sess)
Definition que0que.cc:139
UNIV_INLINE que_node_t * que_node_get_next(que_node_t *node)
UNIV_INTERN que_node_t * que_node_get_containing_loop_node(que_node_t *node)
Definition que0que.cc:1065
UNIV_INTERN void que_thr_end_wait_no_next_thr(que_thr_t *thr)
Definition que0que.cc:283
UNIV_INTERN void que_graph_free_recursive(que_node_t *node)
Definition que0que.cc:506
UNIV_INLINE trx_t * thr_get_trx(que_thr_t *thr)
UNIV_INTERN que_thr_t * row_ins_step(que_thr_t *thr)
Definition row0ins.cc:2414
UNIV_INTERN que_thr_t * row_purge_step(que_thr_t *thr)
Definition row0purge.cc:789
UNIV_INTERN que_thr_t * row_printf_step(que_thr_t *thr)
Definition row0sel.cc:2225
UNIV_INTERN void sel_node_free_private(sel_node_t *node)
Definition row0sel.cc:275
UNIV_INLINE que_thr_t * open_step(que_thr_t *thr)
UNIV_INTERN que_thr_t * row_sel_step(que_thr_t *thr)
Definition row0sel.cc:2015
UNIV_INTERN que_thr_t * fetch_step(que_thr_t *thr)
Definition row0sel.cc:2118
UNIV_INTERN que_thr_t * row_undo_step(que_thr_t *thr)
Definition row0undo.cc:349
UNIV_INTERN que_thr_t * row_upd_step(que_thr_t *thr)
Definition row0upd.cc:2354
UNIV_INTERN void srv_release_mysql_thread_if_suspended(que_thr_t *thr)
Definition srv0srv.cc:1809
UNIV_INTERN void srv_que_task_enqueue_low(que_thr_t *thr)
Definition srv0srv.cc:3577
UNIV_INTERN void srv_suspend_mysql_thread(que_thr_t *thr)
Definition srv0srv.cc:1610
mem_heap_t * heap
Definition dict0crea.h:184
sel_node_t * select
Definition row0ins.h:117
mem_heap_t * heap
Definition row0purge.h:110
que_node_t * parent
Definition que0types.h:49
pars_info_t * info
Definition que0que.h:415
que_common_t common
Definition que0que.h:395
mem_heap_t * heap
Definition que0que.h:434
ulint n_active_thrs
Definition que0que.h:398
sym_tab_t * sym_tab
Definition que0que.h:412
que_t * graph
Definition que0que.h:396
que_thr_t * caller
Definition que0que.h:406
ulint fork_type
Definition que0que.h:397
trx_t * trx
Definition que0que.h:403
sel_node_t * last_sel_node
Definition que0que.h:428
que_node_t * prev_node
Definition que0que.h:379
mem_heap_t * heap
Definition dict0crea.h:152
undo_no_t least_undo_no
Definition trx0types.h:95
que_t * graph
Definition trx0trx.h:613
ulint que_state
Definition trx0trx.h:540
ulint error_state
Definition trx0trx.h:601
undo_no_t undo_no
Definition trx0trx.h:681
ulint n_active_thrs
Definition trx0trx.h:619
mem_heap_t * heap
Definition row0undo.h:133
sel_node_t * select
Definition row0upd.h:417
btr_pcur_t * pcur
Definition row0upd.h:420
mem_heap_t * heap
Definition row0upd.h:456
UNIV_INTERN que_thr_t * trx_rollback_step(que_thr_t *thr)
Definition trx0roll.cc:1316
UNIV_INTERN void trx_finish_rollback_off_kernel(que_t *graph, trx_t *trx, que_thr_t **next_thr)
Definition trx0roll.cc:1226
UNIV_INTERN que_thr_t * trx_commit_step(que_thr_t *thr)
Definition trx0trx.cc:1548
UNIV_INTERN void trx_end_signal_handling(trx_t *trx)
Definition trx0trx.cc:1346
UNIV_INTERN void trx_sig_start_handle(trx_t *trx, que_thr_t **next_thr)
Definition trx0trx.cc:1367
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_a(EXPR)
Definition ut0dbg.h:105
#define UT_LIST_GET_LEN(BASE)
Definition ut0lst.h:216
#define UT_LIST_GET_FIRST(BASE)
Definition ut0lst.h:223
#define UT_LIST_ADD_LAST(NAME, BASE, N)
Definition ut0lst.h:118
#define UT_LIST_ADD_FIRST(NAME, BASE, N)
Definition ut0lst.h:96
#define UT_LIST_GET_NEXT(NAME, N)
Definition ut0lst.h:200
#define UT_LIST_INIT(BASE)
Definition ut0lst.h:83