You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1235 lines
29 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
  3. Copyright (c) 2017, 2018, MariaDB Corporation.
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
  13. *****************************************************************************/
  14. /**************************************************//**
  15. @file que/que0que.cc
  16. Query graph
  17. Created 5/27/1996 Heikki Tuuri
  18. *******************************************************/
  19. #include "que0que.h"
  20. #include "trx0trx.h"
  21. #include "trx0roll.h"
  22. #include "row0undo.h"
  23. #include "row0ins.h"
  24. #include "row0upd.h"
  25. #include "row0sel.h"
  26. #include "row0purge.h"
  27. #include "dict0crea.h"
  28. #include "log0log.h"
  29. #include "eval0proc.h"
  30. #define QUE_MAX_LOOPS_WITHOUT_CHECK 16
  31. /* Short introduction to query graphs
  32. ==================================
  33. A query graph consists of nodes linked to each other in various ways. The
  34. execution starts at que_run_threads() which takes a que_thr_t parameter.
  35. que_thr_t contains two fields that control query graph execution: run_node
  36. and prev_node. run_node is the next node to execute and prev_node is the
  37. last node executed.
  38. Each node has a pointer to a 'next' statement, i.e., its brother, and a
  39. pointer to its parent node. The next pointer is NULL in the last statement
  40. of a block.
  41. Loop nodes contain a link to the first statement of the enclosed statement
  42. list. While the loop runs, que_thr_step() checks if execution to the loop
  43. node came from its parent or from one of the statement nodes in the loop. If
  44. it came from the parent of the loop node it starts executing the first
  45. statement node in the loop. If it came from one of the statement nodes in
  46. the loop, then it checks if the statement node has another statement node
  47. following it, and runs it if so.
  48. To signify loop ending, the loop statements (see e.g. while_step()) set
  49. que_thr_t->run_node to the loop node's parent node. This is noticed on the
  50. next call of que_thr_step() and execution proceeds to the node pointed to by
  51. the loop node's 'next' pointer.
  52. For example, the code:
  53. X := 1;
  54. WHILE X < 5 LOOP
  55. X := X + 1;
  56. X := X + 1;
  57. X := 5
  58. will result in the following node hierarchy, with the X-axis indicating
  59. 'next' links and the Y-axis indicating parent/child links:
  60. A - W - A
  61. |
  62. |
  63. A - A
  64. A = assign_node_t, W = while_node_t. */
  65. /* How a stored procedure containing COMMIT or ROLLBACK commands
  66. is executed?
  67. The commit or rollback can be seen as a subprocedure call.
  68. When the transaction starts to handle a rollback or commit.
  69. It builds a query graph which, when executed, will roll back
  70. or commit the incomplete transaction. The transaction
  71. is moved to the TRX_QUE_ROLLING_BACK or TRX_QUE_COMMITTING state.
  72. If specified, the SQL cursors opened by the transaction are closed.
  73. When the execution of the graph completes, it is like returning
  74. from a subprocedure: the query thread which requested the operation
  75. starts running again. */
  76. /**********************************************************************//**
  77. Moves a thread from another state to the QUE_THR_RUNNING state. Increments
  78. the n_active_thrs counters of the query graph and transaction.
  79. ***NOTE***: This is the only function in which such a transition is allowed
  80. to happen! */
  81. static
  82. void
  83. que_thr_move_to_run_state(
  84. /*======================*/
  85. que_thr_t* thr); /*!< in: an query thread */
  86. /***********************************************************************//**
  87. Creates a query graph fork node.
  88. @return own: fork node */
  89. que_fork_t*
  90. que_fork_create(
  91. /*============*/
  92. que_t* graph, /*!< in: graph, if NULL then this
  93. fork node is assumed to be the
  94. graph root */
  95. que_node_t* parent, /*!< in: parent node */
  96. ulint fork_type, /*!< in: fork type */
  97. mem_heap_t* heap) /*!< in: memory heap where created */
  98. {
  99. que_fork_t* fork;
  100. ut_ad(heap);
  101. fork = static_cast<que_fork_t*>(mem_heap_zalloc(heap, sizeof(*fork)));
  102. fork->heap = heap;
  103. fork->fork_type = fork_type;
  104. fork->common.parent = parent;
  105. fork->common.type = QUE_NODE_FORK;
  106. fork->state = QUE_FORK_COMMAND_WAIT;
  107. fork->graph = (graph != NULL) ? graph : fork;
  108. UT_LIST_INIT(fork->thrs, &que_thr_t::thrs);
  109. return(fork);
  110. }
  111. /** Creates a query graph thread node.
  112. @param[in] parent parent node, i.e., a fork node
  113. @param[in] heap memory heap where created
  114. @param[in] prebuilt row prebuilt structure
  115. @return own: query thread node */
  116. que_thr_t*
  117. que_thr_create(
  118. que_fork_t* parent,
  119. mem_heap_t* heap,
  120. row_prebuilt_t* prebuilt)
  121. {
  122. que_thr_t* thr;
  123. ut_ad(parent != NULL);
  124. ut_ad(heap != NULL);
  125. thr = static_cast<que_thr_t*>(mem_heap_zalloc(heap, sizeof(*thr)));
  126. thr->graph = parent->graph;
  127. thr->common.parent = parent;
  128. thr->magic_n = QUE_THR_MAGIC_N;
  129. thr->common.type = QUE_NODE_THR;
  130. thr->state = QUE_THR_COMMAND_WAIT;
  131. thr->lock_state = QUE_THR_LOCK_NOLOCK;
  132. thr->prebuilt = prebuilt;
  133. UT_LIST_ADD_LAST(parent->thrs, thr);
  134. return(thr);
  135. }
  136. /**********************************************************************//**
  137. Moves a suspended query thread to the QUE_THR_RUNNING state and may release
  138. a worker thread to execute it. This function should be used to end
  139. the wait state of a query thread waiting for a lock or a stored procedure
  140. completion.
  141. @return the query thread that needs to be released. */
  142. que_thr_t*
  143. que_thr_end_lock_wait(
  144. /*==================*/
  145. trx_t* trx) /*!< in: transaction with que_state in
  146. QUE_THR_LOCK_WAIT */
  147. {
  148. que_thr_t* thr;
  149. ibool was_active;
  150. ut_ad(lock_mutex_own());
  151. ut_ad(trx_mutex_own(trx));
  152. thr = trx->lock.wait_thr;
  153. ut_ad(thr != NULL);
  154. ut_ad(trx->lock.que_state == TRX_QUE_LOCK_WAIT);
  155. /* In MySQL this is the only possible state here */
  156. ut_a(thr->state == QUE_THR_LOCK_WAIT);
  157. was_active = thr->is_active;
  158. que_thr_move_to_run_state(thr);
  159. trx->lock.que_state = TRX_QUE_RUNNING;
  160. trx->lock.wait_thr = NULL;
  161. /* In MySQL we let the OS thread (not just the query thread) to wait
  162. for the lock to be released: */
  163. return((!was_active && thr != NULL) ? thr : NULL);
  164. }
  165. /**********************************************************************//**
  166. Inits a query thread for a command. */
  167. UNIV_INLINE
  168. void
  169. que_thr_init_command(
  170. /*=================*/
  171. que_thr_t* thr) /*!< in: query thread */
  172. {
  173. thr->run_node = thr;
  174. thr->prev_node = thr->common.parent;
  175. que_thr_move_to_run_state(thr);
  176. }
  177. /**********************************************************************//**
  178. Round robin scheduler.
  179. @return a query thread of the graph moved to QUE_THR_RUNNING state, or
  180. NULL; the query thread should be executed by que_run_threads by the
  181. caller */
  182. que_thr_t*
  183. que_fork_scheduler_round_robin(
  184. /*===========================*/
  185. que_fork_t* fork, /*!< in: a query fork */
  186. que_thr_t* thr) /*!< in: current pos */
  187. {
  188. trx_mutex_enter(fork->trx);
  189. /* If no current, start first available. */
  190. if (thr == NULL) {
  191. thr = UT_LIST_GET_FIRST(fork->thrs);
  192. } else {
  193. thr = UT_LIST_GET_NEXT(thrs, thr);
  194. }
  195. if (thr) {
  196. fork->state = QUE_FORK_ACTIVE;
  197. fork->last_sel_node = NULL;
  198. switch (thr->state) {
  199. case QUE_THR_COMMAND_WAIT:
  200. case QUE_THR_COMPLETED:
  201. ut_a(!thr->is_active);
  202. que_thr_init_command(thr);
  203. break;
  204. case QUE_THR_SUSPENDED:
  205. case QUE_THR_LOCK_WAIT:
  206. default:
  207. ut_error;
  208. }
  209. }
  210. trx_mutex_exit(fork->trx);
  211. return(thr);
  212. }
  213. /**********************************************************************//**
  214. Starts execution of a command in a query fork. Picks a query thread which
  215. is not in the QUE_THR_RUNNING state and moves it to that state. If none
  216. can be chosen, a situation which may arise in parallelized fetches, NULL
  217. is returned.
  218. @return a query thread of the graph moved to QUE_THR_RUNNING state, or
  219. NULL; the query thread should be executed by que_run_threads by the
  220. caller */
  221. que_thr_t*
  222. que_fork_start_command(
  223. /*===================*/
  224. que_fork_t* fork) /*!< in: a query fork */
  225. {
  226. que_thr_t* thr;
  227. que_thr_t* suspended_thr = NULL;
  228. que_thr_t* completed_thr = NULL;
  229. fork->state = QUE_FORK_ACTIVE;
  230. fork->last_sel_node = NULL;
  231. suspended_thr = NULL;
  232. completed_thr = NULL;
  233. /* Choose the query thread to run: usually there is just one thread,
  234. but in a parallelized select, which necessarily is non-scrollable,
  235. there may be several to choose from */
  236. /* First we try to find a query thread in the QUE_THR_COMMAND_WAIT
  237. state. Then we try to find a query thread in the QUE_THR_SUSPENDED
  238. state, finally we try to find a query thread in the QUE_THR_COMPLETED
  239. state */
  240. /* We make a single pass over the thr list within which we note which
  241. threads are ready to run. */
  242. for (thr = UT_LIST_GET_FIRST(fork->thrs);
  243. thr != NULL;
  244. thr = UT_LIST_GET_NEXT(thrs, thr)) {
  245. switch (thr->state) {
  246. case QUE_THR_COMMAND_WAIT:
  247. /* We have to send the initial message to query thread
  248. to start it */
  249. que_thr_init_command(thr);
  250. return(thr);
  251. case QUE_THR_SUSPENDED:
  252. /* In this case the execution of the thread was
  253. suspended: no initial message is needed because
  254. execution can continue from where it was left */
  255. if (!suspended_thr) {
  256. suspended_thr = thr;
  257. }
  258. break;
  259. case QUE_THR_COMPLETED:
  260. if (!completed_thr) {
  261. completed_thr = thr;
  262. }
  263. break;
  264. case QUE_THR_RUNNING:
  265. case QUE_THR_LOCK_WAIT:
  266. case QUE_THR_PROCEDURE_WAIT:
  267. ut_error;
  268. }
  269. }
  270. if (suspended_thr) {
  271. thr = suspended_thr;
  272. que_thr_move_to_run_state(thr);
  273. } else if (completed_thr) {
  274. thr = completed_thr;
  275. que_thr_init_command(thr);
  276. } else {
  277. ut_error;
  278. }
  279. return(thr);
  280. }
  281. /**********************************************************************//**
  282. Calls que_graph_free_recursive for statements in a statement list. */
  283. static
  284. void
  285. que_graph_free_stat_list(
  286. /*=====================*/
  287. que_node_t* node) /*!< in: first query graph node in the list */
  288. {
  289. while (node) {
  290. que_graph_free_recursive(node);
  291. node = que_node_get_next(node);
  292. }
  293. }
  294. /**********************************************************************//**
  295. Frees a query graph, but not the heap where it was created. Does not free
  296. explicit cursor declarations, they are freed in que_graph_free. */
  297. void
  298. que_graph_free_recursive(
  299. /*=====================*/
  300. que_node_t* node) /*!< in: query graph node */
  301. {
  302. que_fork_t* fork;
  303. que_thr_t* thr;
  304. undo_node_t* undo;
  305. sel_node_t* sel;
  306. ins_node_t* ins;
  307. upd_node_t* upd;
  308. tab_node_t* cre_tab;
  309. ind_node_t* cre_ind;
  310. purge_node_t* purge;
  311. DBUG_ENTER("que_graph_free_recursive");
  312. if (node == NULL) {
  313. DBUG_VOID_RETURN;
  314. }
  315. DBUG_PRINT("que_graph_free_recursive",
  316. ("node: %p, type: " ULINTPF, node,
  317. que_node_get_type(node)));
  318. switch (que_node_get_type(node)) {
  319. case QUE_NODE_FORK:
  320. fork = static_cast<que_fork_t*>(node);
  321. thr = UT_LIST_GET_FIRST(fork->thrs);
  322. while (thr) {
  323. que_graph_free_recursive(thr);
  324. thr = UT_LIST_GET_NEXT(thrs, thr);
  325. }
  326. break;
  327. case QUE_NODE_THR:
  328. thr = static_cast<que_thr_t*>(node);
  329. ut_a(thr->magic_n == QUE_THR_MAGIC_N);
  330. thr->magic_n = QUE_THR_MAGIC_FREED;
  331. que_graph_free_recursive(thr->child);
  332. break;
  333. case QUE_NODE_UNDO:
  334. undo = static_cast<undo_node_t*>(node);
  335. mem_heap_free(undo->heap);
  336. break;
  337. case QUE_NODE_SELECT:
  338. sel = static_cast<sel_node_t*>(node);
  339. sel_node_free_private(sel);
  340. break;
  341. case QUE_NODE_INSERT:
  342. ins = static_cast<ins_node_t*>(node);
  343. que_graph_free_recursive(ins->select);
  344. ins->select = NULL;
  345. if (ins->entry_sys_heap != NULL) {
  346. mem_heap_free(ins->entry_sys_heap);
  347. ins->entry_sys_heap = NULL;
  348. }
  349. break;
  350. case QUE_NODE_PURGE:
  351. purge = static_cast<purge_node_t*>(node);
  352. mem_heap_free(purge->heap);
  353. break;
  354. case QUE_NODE_UPDATE:
  355. upd = static_cast<upd_node_t*>(node);
  356. if (upd->in_mysql_interface) {
  357. btr_pcur_free_for_mysql(upd->pcur);
  358. upd->in_mysql_interface = FALSE;
  359. }
  360. que_graph_free_recursive(upd->cascade_node);
  361. if (upd->cascade_heap) {
  362. mem_heap_free(upd->cascade_heap);
  363. upd->cascade_heap = NULL;
  364. }
  365. que_graph_free_recursive(upd->select);
  366. upd->select = NULL;
  367. if (upd->heap != NULL) {
  368. mem_heap_free(upd->heap);
  369. upd->heap = NULL;
  370. }
  371. break;
  372. case QUE_NODE_CREATE_TABLE:
  373. cre_tab = static_cast<tab_node_t*>(node);
  374. que_graph_free_recursive(cre_tab->tab_def);
  375. que_graph_free_recursive(cre_tab->col_def);
  376. que_graph_free_recursive(cre_tab->v_col_def);
  377. mem_heap_free(cre_tab->heap);
  378. break;
  379. case QUE_NODE_CREATE_INDEX:
  380. cre_ind = static_cast<ind_node_t*>(node);
  381. que_graph_free_recursive(cre_ind->ind_def);
  382. que_graph_free_recursive(cre_ind->field_def);
  383. mem_heap_free(cre_ind->heap);
  384. break;
  385. case QUE_NODE_PROC:
  386. que_graph_free_stat_list(((proc_node_t*) node)->stat_list);
  387. break;
  388. case QUE_NODE_IF:
  389. que_graph_free_stat_list(((if_node_t*) node)->stat_list);
  390. que_graph_free_stat_list(((if_node_t*) node)->else_part);
  391. que_graph_free_stat_list(((if_node_t*) node)->elsif_list);
  392. break;
  393. case QUE_NODE_ELSIF:
  394. que_graph_free_stat_list(((elsif_node_t*) node)->stat_list);
  395. break;
  396. case QUE_NODE_WHILE:
  397. que_graph_free_stat_list(((while_node_t*) node)->stat_list);
  398. break;
  399. case QUE_NODE_FOR:
  400. que_graph_free_stat_list(((for_node_t*) node)->stat_list);
  401. break;
  402. case QUE_NODE_ASSIGNMENT:
  403. case QUE_NODE_EXIT:
  404. case QUE_NODE_RETURN:
  405. case QUE_NODE_COMMIT:
  406. case QUE_NODE_ROLLBACK:
  407. case QUE_NODE_LOCK:
  408. case QUE_NODE_FUNC:
  409. case QUE_NODE_ORDER:
  410. case QUE_NODE_ROW_PRINTF:
  411. case QUE_NODE_OPEN:
  412. case QUE_NODE_FETCH:
  413. /* No need to do anything */
  414. break;
  415. default:
  416. ut_error;
  417. }
  418. DBUG_VOID_RETURN;
  419. }
  420. /**********************************************************************//**
  421. Frees a query graph. */
  422. void
  423. que_graph_free(
  424. /*===========*/
  425. que_t* graph) /*!< in: query graph; we assume that the memory
  426. heap where this graph was created is private
  427. to this graph: if not, then use
  428. que_graph_free_recursive and free the heap
  429. afterwards! */
  430. {
  431. ut_ad(graph);
  432. if (graph->sym_tab) {
  433. /* The following call frees dynamic memory allocated
  434. for variables etc. during execution. Frees also explicit
  435. cursor definitions. */
  436. sym_tab_free_private(graph->sym_tab);
  437. }
  438. if (graph->info && graph->info->graph_owns_us) {
  439. pars_info_free(graph->info);
  440. }
  441. que_graph_free_recursive(graph);
  442. mem_heap_free(graph->heap);
  443. }
  444. /****************************************************************//**
  445. Performs an execution step on a thr node.
  446. @return query thread to run next, or NULL if none */
  447. static
  448. que_thr_t*
  449. que_thr_node_step(
  450. /*==============*/
  451. que_thr_t* thr) /*!< in: query thread where run_node must
  452. be the thread node itself */
  453. {
  454. ut_ad(thr->run_node == thr);
  455. if (thr->prev_node == thr->common.parent) {
  456. /* If control to the node came from above, it is just passed
  457. on */
  458. thr->run_node = thr->child;
  459. return(thr);
  460. }
  461. trx_mutex_enter(thr_get_trx(thr));
  462. if (que_thr_peek_stop(thr)) {
  463. trx_mutex_exit(thr_get_trx(thr));
  464. return(thr);
  465. }
  466. /* Thread execution completed */
  467. thr->state = QUE_THR_COMPLETED;
  468. trx_mutex_exit(thr_get_trx(thr));
  469. return(NULL);
  470. }
  471. /**********************************************************************//**
  472. Moves a thread from another state to the QUE_THR_RUNNING state. Increments
  473. the n_active_thrs counters of the query graph and transaction if thr was
  474. not active.
  475. ***NOTE***: This and ..._mysql are the only functions in which such a
  476. transition is allowed to happen! */
  477. static
  478. void
  479. que_thr_move_to_run_state(
  480. /*======================*/
  481. que_thr_t* thr) /*!< in: an query thread */
  482. {
  483. ut_ad(thr->state != QUE_THR_RUNNING);
  484. if (!thr->is_active) {
  485. trx_t* trx;
  486. trx = thr_get_trx(thr);
  487. thr->graph->n_active_thrs++;
  488. trx->lock.n_active_thrs++;
  489. thr->is_active = TRUE;
  490. }
  491. thr->state = QUE_THR_RUNNING;
  492. }
  493. /**********************************************************************//**
  494. Stops a query thread if graph or trx is in a state requiring it. The
  495. conditions are tested in the order (1) graph, (2) trx.
  496. @return TRUE if stopped */
  497. ibool
  498. que_thr_stop(
  499. /*=========*/
  500. que_thr_t* thr) /*!< in: query thread */
  501. {
  502. que_t* graph;
  503. trx_t* trx = thr_get_trx(thr);
  504. graph = thr->graph;
  505. ut_ad(trx_mutex_own(trx));
  506. if (graph->state == QUE_FORK_COMMAND_WAIT) {
  507. thr->state = QUE_THR_SUSPENDED;
  508. } else if (trx->lock.que_state == TRX_QUE_LOCK_WAIT) {
  509. trx->lock.wait_thr = thr;
  510. thr->state = QUE_THR_LOCK_WAIT;
  511. } else if (trx->error_state != DB_SUCCESS
  512. && trx->error_state != DB_LOCK_WAIT) {
  513. /* Error handling built for the MySQL interface */
  514. thr->state = QUE_THR_COMPLETED;
  515. } else if (graph->fork_type == QUE_FORK_ROLLBACK) {
  516. thr->state = QUE_THR_SUSPENDED;
  517. } else {
  518. ut_ad(graph->state == QUE_FORK_ACTIVE);
  519. return(FALSE);
  520. }
  521. return(TRUE);
  522. }
  523. /**********************************************************************//**
  524. Decrements the query thread reference counts in the query graph and the
  525. transaction.
  526. *** NOTE ***:
  527. This and que_thr_stop_for_mysql are the only functions where the reference
  528. count can be decremented and this function may only be called from inside
  529. que_run_threads! These restrictions exist to make the rollback code easier
  530. to maintain. */
  531. static
  532. void
  533. que_thr_dec_refer_count(
  534. /*====================*/
  535. que_thr_t* thr, /*!< in: query thread */
  536. que_thr_t** next_thr) /*!< in/out: next query thread to run;
  537. if the value which is passed in is
  538. a pointer to a NULL pointer, then the
  539. calling function can start running
  540. a new query thread */
  541. {
  542. trx_t* trx;
  543. que_fork_t* fork;
  544. trx = thr_get_trx(thr);
  545. ut_a(thr->is_active);
  546. ut_ad(trx_mutex_own(trx));
  547. if (thr->state == QUE_THR_RUNNING) {
  548. if (!que_thr_stop(thr)) {
  549. ut_a(next_thr != NULL && *next_thr == NULL);
  550. /* The reason for the thr suspension or wait was
  551. already canceled before we came here: continue
  552. running the thread.
  553. This is also possible because in trx_commit_step() we
  554. assume a single query thread. We set the query thread
  555. state to QUE_THR_RUNNING. */
  556. /* fprintf(stderr,
  557. "Wait already ended: trx: %p\n", trx); */
  558. /* Normally srv_suspend_mysql_thread resets
  559. the state to DB_SUCCESS before waiting, but
  560. in this case we have to do it here,
  561. otherwise nobody does it. */
  562. trx->error_state = DB_SUCCESS;
  563. *next_thr = thr;
  564. return;
  565. }
  566. }
  567. fork = static_cast<que_fork_t*>(thr->common.parent);
  568. --trx->lock.n_active_thrs;
  569. --fork->n_active_thrs;
  570. thr->is_active = FALSE;
  571. }
  572. /**********************************************************************//**
  573. A patch for MySQL used to 'stop' a dummy query thread used in MySQL. The
  574. query thread is stopped and made inactive, except in the case where
  575. it was put to the lock wait state in lock0lock.cc, but the lock has already
  576. been granted or the transaction chosen as a victim in deadlock resolution. */
  577. void
  578. que_thr_stop_for_mysql(
  579. /*===================*/
  580. que_thr_t* thr) /*!< in: query thread */
  581. {
  582. trx_t* trx;
  583. trx = thr_get_trx(thr);
  584. trx_mutex_enter(trx);
  585. if (thr->state == QUE_THR_RUNNING) {
  586. if (trx->error_state != DB_SUCCESS
  587. && trx->error_state != DB_LOCK_WAIT) {
  588. /* Error handling built for the MySQL interface */
  589. thr->state = QUE_THR_COMPLETED;
  590. } else {
  591. /* It must have been a lock wait but the lock was
  592. already released, or this transaction was chosen
  593. as a victim in selective deadlock resolution */
  594. trx_mutex_exit(trx);
  595. return;
  596. }
  597. }
  598. ut_ad(thr->is_active == TRUE);
  599. ut_ad(trx->lock.n_active_thrs == 1);
  600. ut_ad(thr->graph->n_active_thrs == 1);
  601. thr->is_active = FALSE;
  602. thr->graph->n_active_thrs--;
  603. trx->lock.n_active_thrs--;
  604. trx_mutex_exit(trx);
  605. }
  606. /**********************************************************************//**
  607. Moves a thread from another state to the QUE_THR_RUNNING state. Increments
  608. the n_active_thrs counters of the query graph and transaction if thr was
  609. not active. */
  610. void
  611. que_thr_move_to_run_state_for_mysql(
  612. /*================================*/
  613. que_thr_t* thr, /*!< in: an query thread */
  614. trx_t* trx) /*!< in: transaction */
  615. {
  616. ut_a(thr->magic_n == QUE_THR_MAGIC_N);
  617. if (!thr->is_active) {
  618. thr->graph->n_active_thrs++;
  619. trx->lock.n_active_thrs++;
  620. thr->is_active = TRUE;
  621. }
  622. thr->state = QUE_THR_RUNNING;
  623. }
  624. /**********************************************************************//**
  625. A patch for MySQL used to 'stop' a dummy query thread used in MySQL
  626. select, when there is no error or lock wait. */
  627. void
  628. que_thr_stop_for_mysql_no_error(
  629. /*============================*/
  630. que_thr_t* thr, /*!< in: query thread */
  631. trx_t* trx) /*!< in: transaction */
  632. {
  633. ut_ad(thr->state == QUE_THR_RUNNING);
  634. ut_ad(thr->is_active == TRUE);
  635. ut_ad(trx->lock.n_active_thrs == 1);
  636. ut_ad(thr->graph->n_active_thrs == 1);
  637. ut_a(thr->magic_n == QUE_THR_MAGIC_N);
  638. thr->state = QUE_THR_COMPLETED;
  639. thr->is_active = FALSE;
  640. thr->graph->n_active_thrs--;
  641. trx->lock.n_active_thrs--;
  642. }
  643. /****************************************************************//**
  644. Get the first containing loop node (e.g. while_node_t or for_node_t) for the
  645. given node, or NULL if the node is not within a loop.
  646. @return containing loop node, or NULL. */
  647. que_node_t*
  648. que_node_get_containing_loop_node(
  649. /*==============================*/
  650. que_node_t* node) /*!< in: node */
  651. {
  652. ut_ad(node);
  653. for (;;) {
  654. ulint type;
  655. node = que_node_get_parent(node);
  656. if (!node) {
  657. break;
  658. }
  659. type = que_node_get_type(node);
  660. if ((type == QUE_NODE_FOR) || (type == QUE_NODE_WHILE)) {
  661. break;
  662. }
  663. }
  664. return(node);
  665. }
  666. #ifndef DBUG_OFF
  667. /** Gets information of an SQL query graph node.
  668. @return type description */
  669. static MY_ATTRIBUTE((warn_unused_result, nonnull))
  670. const char*
  671. que_node_type_string(
  672. /*=================*/
  673. const que_node_t* node) /*!< in: query graph node */
  674. {
  675. switch (que_node_get_type(node)) {
  676. case QUE_NODE_SELECT:
  677. return("SELECT");
  678. case QUE_NODE_INSERT:
  679. return("INSERT");
  680. case QUE_NODE_UPDATE:
  681. return("UPDATE");
  682. case QUE_NODE_WHILE:
  683. return("WHILE");
  684. case QUE_NODE_ASSIGNMENT:
  685. return("ASSIGNMENT");
  686. case QUE_NODE_IF:
  687. return("IF");
  688. case QUE_NODE_FETCH:
  689. return("FETCH");
  690. case QUE_NODE_OPEN:
  691. return("OPEN");
  692. case QUE_NODE_PROC:
  693. return("STORED PROCEDURE");
  694. case QUE_NODE_FUNC:
  695. return("FUNCTION");
  696. case QUE_NODE_LOCK:
  697. return("LOCK");
  698. case QUE_NODE_THR:
  699. return("QUERY THREAD");
  700. case QUE_NODE_COMMIT:
  701. return("COMMIT");
  702. case QUE_NODE_UNDO:
  703. return("UNDO ROW");
  704. case QUE_NODE_PURGE:
  705. return("PURGE ROW");
  706. case QUE_NODE_ROLLBACK:
  707. return("ROLLBACK");
  708. case QUE_NODE_CREATE_TABLE:
  709. return("CREATE TABLE");
  710. case QUE_NODE_CREATE_INDEX:
  711. return("CREATE INDEX");
  712. case QUE_NODE_FOR:
  713. return("FOR LOOP");
  714. case QUE_NODE_RETURN:
  715. return("RETURN");
  716. case QUE_NODE_EXIT:
  717. return("EXIT");
  718. default:
  719. ut_ad(0);
  720. return("UNKNOWN NODE TYPE");
  721. }
  722. }
  723. #endif /* !DBUG_OFF */
  724. /**********************************************************************//**
  725. Performs an execution step on a query thread.
  726. @return query thread to run next: it may differ from the input
  727. parameter if, e.g., a subprocedure call is made */
  728. UNIV_INLINE
  729. que_thr_t*
  730. que_thr_step(
  731. /*=========*/
  732. que_thr_t* thr) /*!< in: query thread */
  733. {
  734. que_node_t* node;
  735. que_thr_t* old_thr;
  736. trx_t* trx;
  737. ulint type;
  738. trx = thr_get_trx(thr);
  739. ut_ad(thr->state == QUE_THR_RUNNING);
  740. ut_a(trx->error_state == DB_SUCCESS);
  741. thr->resource++;
  742. node = thr->run_node;
  743. type = que_node_get_type(node);
  744. old_thr = thr;
  745. DBUG_PRINT("ib_que", ("Execute %u (%s) at %p",
  746. unsigned(type), que_node_type_string(node),
  747. (const void*) node));
  748. if (type & QUE_NODE_CONTROL_STAT) {
  749. if ((thr->prev_node != que_node_get_parent(node))
  750. && que_node_get_next(thr->prev_node)) {
  751. /* The control statements, like WHILE, always pass the
  752. control to the next child statement if there is any
  753. child left */
  754. thr->run_node = que_node_get_next(thr->prev_node);
  755. } else if (type == QUE_NODE_IF) {
  756. if_step(thr);
  757. } else if (type == QUE_NODE_FOR) {
  758. for_step(thr);
  759. } else if (type == QUE_NODE_PROC) {
  760. /* We can access trx->undo_no without reserving
  761. trx->undo_mutex, because there cannot be active query
  762. threads doing updating or inserting at the moment! */
  763. if (thr->prev_node == que_node_get_parent(node)) {
  764. trx->last_sql_stat_start.least_undo_no
  765. = trx->undo_no;
  766. }
  767. proc_step(thr);
  768. } else if (type == QUE_NODE_WHILE) {
  769. while_step(thr);
  770. } else {
  771. ut_error;
  772. }
  773. } else if (type == QUE_NODE_ASSIGNMENT) {
  774. assign_step(thr);
  775. } else if (type == QUE_NODE_SELECT) {
  776. thr = row_sel_step(thr);
  777. } else if (type == QUE_NODE_INSERT) {
  778. thr = row_ins_step(thr);
  779. } else if (type == QUE_NODE_UPDATE) {
  780. thr = row_upd_step(thr);
  781. } else if (type == QUE_NODE_FETCH) {
  782. thr = fetch_step(thr);
  783. } else if (type == QUE_NODE_OPEN) {
  784. thr = open_step(thr);
  785. } else if (type == QUE_NODE_FUNC) {
  786. proc_eval_step(thr);
  787. } else if (type == QUE_NODE_LOCK) {
  788. ut_error;
  789. } else if (type == QUE_NODE_THR) {
  790. thr = que_thr_node_step(thr);
  791. } else if (type == QUE_NODE_COMMIT) {
  792. thr = trx_commit_step(thr);
  793. } else if (type == QUE_NODE_UNDO) {
  794. thr = row_undo_step(thr);
  795. } else if (type == QUE_NODE_PURGE) {
  796. thr = row_purge_step(thr);
  797. } else if (type == QUE_NODE_RETURN) {
  798. thr = return_step(thr);
  799. } else if (type == QUE_NODE_EXIT) {
  800. thr = exit_step(thr);
  801. } else if (type == QUE_NODE_ROLLBACK) {
  802. thr = trx_rollback_step(thr);
  803. } else if (type == QUE_NODE_CREATE_TABLE) {
  804. thr = dict_create_table_step(thr);
  805. } else if (type == QUE_NODE_CREATE_INDEX) {
  806. thr = dict_create_index_step(thr);
  807. } else if (type == QUE_NODE_ROW_PRINTF) {
  808. thr = row_printf_step(thr);
  809. } else {
  810. ut_error;
  811. }
  812. if (type == QUE_NODE_EXIT) {
  813. old_thr->prev_node = que_node_get_containing_loop_node(node);
  814. } else {
  815. old_thr->prev_node = node;
  816. }
  817. if (thr) {
  818. ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
  819. }
  820. return(thr);
  821. }
  822. /**********************************************************************//**
  823. Run a query thread until it finishes or encounters e.g. a lock wait. */
  824. static
  825. void
  826. que_run_threads_low(
  827. /*================*/
  828. que_thr_t* thr) /*!< in: query thread */
  829. {
  830. trx_t* trx;
  831. que_thr_t* next_thr;
  832. ut_ad(thr->state == QUE_THR_RUNNING);
  833. ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
  834. ut_ad(!trx_mutex_own(thr_get_trx(thr)));
  835. /* slot can be received from purge thread for debug_sync setup */
  836. ut_d(srv_slot_t *slot = thr->thread_slot);
  837. /* cumul_resource counts how much resources the OS thread (NOT the
  838. query thread) has spent in this function */
  839. trx = thr_get_trx(thr);
  840. do {
  841. /* Check that there is enough space in the log to accommodate
  842. possible log entries by this query step; if the operation can
  843. touch more than about 4 pages, checks must be made also within
  844. the query step! */
  845. log_free_check();
  846. /* Perform the actual query step: note that the query thread
  847. may change if, e.g., a subprocedure call is made */
  848. /*-------------------------*/
  849. next_thr = que_thr_step(thr);
  850. /*-------------------------*/
  851. trx_mutex_enter(trx);
  852. ut_a(next_thr == NULL || trx->error_state == DB_SUCCESS);
  853. if (next_thr != thr) {
  854. ut_a(next_thr == NULL);
  855. /* This can change next_thr to a non-NULL value
  856. if there was a lock wait that already completed. */
  857. que_thr_dec_refer_count(thr, &next_thr);
  858. if (next_thr != NULL) {
  859. thr = next_thr;
  860. }
  861. }
  862. ut_ad(trx == thr_get_trx(thr));
  863. trx_mutex_exit(trx);
  864. } while (next_thr != NULL);
  865. }
  866. /**********************************************************************//**
  867. Run a query thread. Handles lock waits. */
  868. void
  869. que_run_threads(
  870. /*============*/
  871. que_thr_t* thr) /*!< in: query thread */
  872. {
  873. ut_ad(!trx_mutex_own(thr_get_trx(thr)));
  874. loop:
  875. ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS);
  876. que_run_threads_low(thr);
  877. switch (thr->state) {
  878. case QUE_THR_RUNNING:
  879. /* There probably was a lock wait, but it already ended
  880. before we came here: continue running thr */
  881. goto loop;
  882. case QUE_THR_LOCK_WAIT:
  883. lock_wait_suspend_thread(thr);
  884. trx_mutex_enter(thr_get_trx(thr));
  885. ut_a(thr_get_trx(thr)->id != 0);
  886. if (thr_get_trx(thr)->error_state != DB_SUCCESS) {
  887. /* thr was chosen as a deadlock victim or there was
  888. a lock wait timeout */
  889. que_thr_dec_refer_count(thr, NULL);
  890. trx_mutex_exit(thr_get_trx(thr));
  891. break;
  892. }
  893. trx_mutex_exit(thr_get_trx(thr));
  894. goto loop;
  895. case QUE_THR_COMPLETED:
  896. case QUE_THR_COMMAND_WAIT:
  897. /* Do nothing */
  898. break;
  899. default:
  900. ut_error;
  901. }
  902. }
  903. /*********************************************************************//**
  904. Evaluate the given SQL.
  905. @return error code or DB_SUCCESS */
  906. dberr_t
  907. que_eval_sql(
  908. /*=========*/
  909. pars_info_t* info, /*!< in: info struct, or NULL */
  910. const char* sql, /*!< in: SQL string */
  911. ibool reserve_dict_mutex,
  912. /*!< in: if TRUE, acquire/release
  913. dict_sys->mutex around call to pars_sql. */
  914. trx_t* trx) /*!< in: trx */
  915. {
  916. que_thr_t* thr;
  917. que_t* graph;
  918. DBUG_ENTER("que_eval_sql");
  919. DBUG_PRINT("que_eval_sql", ("query: %s", sql));
  920. ut_a(trx->error_state == DB_SUCCESS);
  921. if (reserve_dict_mutex) {
  922. mutex_enter(&dict_sys->mutex);
  923. }
  924. graph = pars_sql(info, sql);
  925. if (reserve_dict_mutex) {
  926. mutex_exit(&dict_sys->mutex);
  927. }
  928. graph->trx = trx;
  929. trx->graph = NULL;
  930. graph->fork_type = QUE_FORK_MYSQL_INTERFACE;
  931. ut_a(thr = que_fork_start_command(graph));
  932. que_run_threads(thr);
  933. if (reserve_dict_mutex) {
  934. mutex_enter(&dict_sys->mutex);
  935. }
  936. que_graph_free(graph);
  937. if (reserve_dict_mutex) {
  938. mutex_exit(&dict_sys->mutex);
  939. }
  940. DBUG_RETURN(trx->error_state);
  941. }