| 558 | */ |
| 559 | |
| 560 | bool flush_tables(THD *thd, flush_tables_type flag) |
| 561 | { |
| 562 | bool result= TRUE; |
| 563 | tc_collect_arg collect_arg; |
| 564 | TABLE *tmp_table; |
| 565 | flush_tables_error_handler error_handler; |
| 566 | DBUG_ENTER("flush_tables"); |
| 567 | |
| 568 | purge_tables(); /* Flush unused tables and shares */ |
| 569 | DEBUG_SYNC(thd, "after_purge_tables"); |
| 570 | |
| 571 | /* |
| 572 | Loop over all shares and collect shares that have open tables |
| 573 | TODO: |
| 574 | Optimize this to only collect shares that have been used for |
| 575 | write after last time all tables was closed. |
| 576 | */ |
| 577 | |
| 578 | if (!(tmp_table= (TABLE*) my_malloc(PSI_INSTRUMENT_ME, sizeof(*tmp_table), |
| 579 | MYF(MY_WME | MY_THREAD_SPECIFIC)))) |
| 580 | DBUG_RETURN(1); |
| 581 | |
| 582 | my_init_dynamic_array(PSI_INSTRUMENT_ME, &collect_arg.shares, |
| 583 | sizeof(TABLE_SHARE*), 100, 100, MYF(0)); |
| 584 | collect_arg.flush_type= flag; |
| 585 | if (tdc_iterate(thd, tc_collect_used_shares, &collect_arg, true)) |
| 586 | { |
| 587 | /* Release already collected shares */ |
| 588 | for (uint i= 0 ; i < collect_arg.shares.elements ; i++) |
| 589 | { |
| 590 | TABLE_SHARE *share= *dynamic_element(&collect_arg.shares, i, |
| 591 | TABLE_SHARE**); |
| 592 | tdc_release_share(share); |
| 593 | } |
| 594 | goto err; |
| 595 | } |
| 596 | |
| 597 | /* Call HA_EXTRA_FLUSH on all found shares */ |
| 598 | |
| 599 | thd->push_internal_handler(&error_handler); |
| 600 | for (uint i= 0 ; i < collect_arg.shares.elements ; i++) |
| 601 | { |
| 602 | TABLE_SHARE *share= *dynamic_element(&collect_arg.shares, i, |
| 603 | TABLE_SHARE**); |
| 604 | TABLE *table= tc_acquire_table(thd, share->tdc); |
| 605 | if (table) |
| 606 | { |
| 607 | (void) table->file->extra(HA_EXTRA_FLUSH); |
| 608 | DEBUG_SYNC(table->in_use, "before_tc_release_table"); |
| 609 | tc_release_table(table); |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | /* |
| 614 | No free TABLE instances available. We have to open a new one. |
| 615 | |
| 616 | Try to take a MDL lock to ensure we can open a new table instance. |
| 617 | If the lock fails, it means that some DDL operation or flush tables |
no test coverage detected