mirror of
https://github.com/MariaDB/server.git
synced 2025-01-23 15:24:16 +01:00
417434f12d
When a slow shutdown is performed soon after spawning some work for background threads that can create or commit transactions, it is possible that new transactions are started or committed after the purge has finished. This is violating the specification of innodb_fast_shutdown=0, namely that the purge must be completed. (None of the history of the recent transactions would be purged.) Also, it is possible that the purge threads would exit in slow shutdown while there exist active transactions, such as recovered incomplete transactions that are being rolled back. Thus, the slow shutdown could fail to purge some undo log that becomes purgeable after the transaction commit or rollback. srv_undo_sources: A flag that indicates if undo log can be generated or the persistent, whether by background threads or by user SQL. Even when this flag is clear, active transactions that already exist in the system may be committed or rolled back. innodb_shutdown(): Renamed from innobase_shutdown_for_mysql(). Do not return an error code; the operation never fails. Clear the srv_undo_sources flag, and also ensure that the background DROP TABLE queue is empty. srv_purge_should_exit(): Do not allow the purge to exit if srv_undo_sources are active or the background DROP TABLE queue is not empty, or in slow shutdown, if any active transactions exist (and are being rolled back). srv_purge_coordinator_thread(): Remove some previous workarounds for this bug. innobase_start_or_create_for_mysql(): Set buf_page_cleaner_is_active and srv_dict_stats_thread_active directly. Set srv_undo_sources before starting the purge subsystem, to prevent immediate shutdown of the purge. Create dict_stats_thread and fts_optimize_thread immediately after setting srv_undo_sources, so that shutdown can use this flag to determine if these subsystems were started. dict_stats_shutdown(): Shut down dict_stats_thread. Backported from 10.2. srv_shutdown_table_bg_threads(): Remove (unused).
388 lines
11 KiB
C++
388 lines
11 KiB
C++
/*****************************************************************************
|
|
|
|
Copyright (c) 2012, 2017, Oracle and/or its affiliates. All Rights Reserved.
|
|
Copyright (c) 2017, MariaDB Corporation.
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
this program; if not, write to the Free Software Foundation, Inc.,
|
|
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
/**************************************************//**
|
|
@file dict/dict0stats_bg.cc
|
|
Code used for background table and index stats gathering.
|
|
|
|
Created Apr 25, 2012 Vasil Dimov
|
|
*******************************************************/
|
|
|
|
#include "row0mysql.h"
|
|
#include "srv0start.h"
|
|
#include "dict0stats.h"
|
|
#include "dict0stats_bg.h"
|
|
|
|
#ifdef UNIV_NONINL
|
|
# include "dict0stats_bg.ic"
|
|
#endif
|
|
|
|
#include <vector>
|
|
|
|
/** Minimum time interval between stats recalc for a given table */
|
|
#define MIN_RECALC_INTERVAL 10 /* seconds */
|
|
|
|
/** Event to wake up dict_stats_thread on dict_stats_recalc_pool_add()
|
|
or shutdown. Not protected by any mutex. */
|
|
UNIV_INTERN os_event_t dict_stats_event;
|
|
|
|
/** Variable to initiate shutdown the dict stats thread. Note we don't
|
|
use 'srv_shutdown_state' because we want to shutdown dict stats thread
|
|
before purge thread. */
|
|
static bool dict_stats_start_shutdown;
|
|
|
|
/** Event to wait for shutdown of the dict stats thread */
|
|
static os_event_t dict_stats_shutdown_event;
|
|
|
|
/** This mutex protects the "recalc_pool" variable. */
|
|
static ib_mutex_t recalc_pool_mutex;
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
static mysql_pfs_key_t recalc_pool_mutex_key;
|
|
#endif /* HAVE_PSI_INTERFACE */
|
|
|
|
/** The number of tables that can be added to "recalc_pool" before
|
|
it is enlarged */
|
|
static const ulint RECALC_POOL_INITIAL_SLOTS = 128;
|
|
|
|
/** The multitude of tables whose stats are to be automatically
|
|
recalculated - an STL vector */
|
|
typedef std::vector<table_id_t> recalc_pool_t;
|
|
static recalc_pool_t recalc_pool;
|
|
|
|
typedef recalc_pool_t::iterator recalc_pool_iterator_t;
|
|
|
|
/*****************************************************************//**
|
|
Initialize the recalc pool, called once during thread initialization. */
|
|
static
|
|
void
|
|
dict_stats_recalc_pool_init()
|
|
/*=========================*/
|
|
{
|
|
ut_ad(!srv_read_only_mode);
|
|
|
|
recalc_pool.reserve(RECALC_POOL_INITIAL_SLOTS);
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Free the resources occupied by the recalc pool, called once during
|
|
thread de-initialization. */
|
|
static
|
|
void
|
|
dict_stats_recalc_pool_deinit()
|
|
/*===========================*/
|
|
{
|
|
ut_ad(!srv_read_only_mode);
|
|
|
|
recalc_pool.clear();
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Add a table to the recalc pool, which is processed by the
|
|
background stats gathering thread. Only the table id is added to the
|
|
list, so the table can be closed after being enqueued and it will be
|
|
opened when needed. If the table does not exist later (has been DROPped),
|
|
then it will be removed from the pool and skipped. */
|
|
UNIV_INTERN
|
|
void
|
|
dict_stats_recalc_pool_add(
|
|
/*=======================*/
|
|
const dict_table_t* table) /*!< in: table to add */
|
|
{
|
|
ut_ad(!srv_read_only_mode);
|
|
|
|
mutex_enter(&recalc_pool_mutex);
|
|
|
|
/* quit if already in the list */
|
|
for (recalc_pool_iterator_t iter = recalc_pool.begin();
|
|
iter != recalc_pool.end();
|
|
++iter) {
|
|
|
|
if (*iter == table->id) {
|
|
mutex_exit(&recalc_pool_mutex);
|
|
return;
|
|
}
|
|
}
|
|
|
|
recalc_pool.push_back(table->id);
|
|
|
|
mutex_exit(&recalc_pool_mutex);
|
|
|
|
os_event_set(dict_stats_event);
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Get a table from the auto recalc pool. The returned table id is removed
|
|
from the pool.
|
|
@return true if the pool was non-empty and "id" was set, false otherwise */
|
|
static
|
|
bool
|
|
dict_stats_recalc_pool_get(
|
|
/*=======================*/
|
|
table_id_t* id) /*!< out: table id, or unmodified if list is
|
|
empty */
|
|
{
|
|
ut_ad(!srv_read_only_mode);
|
|
|
|
mutex_enter(&recalc_pool_mutex);
|
|
|
|
if (recalc_pool.empty()) {
|
|
mutex_exit(&recalc_pool_mutex);
|
|
return(false);
|
|
}
|
|
|
|
*id = recalc_pool[0];
|
|
|
|
recalc_pool.erase(recalc_pool.begin());
|
|
|
|
mutex_exit(&recalc_pool_mutex);
|
|
|
|
return(true);
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Delete a given table from the auto recalc pool.
|
|
dict_stats_recalc_pool_del() */
|
|
UNIV_INTERN
|
|
void
|
|
dict_stats_recalc_pool_del(
|
|
/*=======================*/
|
|
const dict_table_t* table) /*!< in: table to remove */
|
|
{
|
|
ut_ad(!srv_read_only_mode);
|
|
ut_ad(mutex_own(&dict_sys->mutex));
|
|
|
|
mutex_enter(&recalc_pool_mutex);
|
|
|
|
ut_ad(table->id > 0);
|
|
|
|
for (recalc_pool_iterator_t iter = recalc_pool.begin();
|
|
iter != recalc_pool.end();
|
|
++iter) {
|
|
|
|
if (*iter == table->id) {
|
|
/* erase() invalidates the iterator */
|
|
recalc_pool.erase(iter);
|
|
break;
|
|
}
|
|
}
|
|
|
|
mutex_exit(&recalc_pool_mutex);
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Wait until background stats thread has stopped using the specified table.
|
|
The caller must have locked the data dictionary using
|
|
row_mysql_lock_data_dictionary() and this function may unlock it temporarily
|
|
and restore the lock before it exits.
|
|
The background stats thread is guaranteed not to start using the specified
|
|
table after this function returns and before the caller unlocks the data
|
|
dictionary because it sets the BG_STAT_IN_PROGRESS bit in table->stats_bg_flag
|
|
under dict_sys->mutex. */
|
|
UNIV_INTERN
|
|
void
|
|
dict_stats_wait_bg_to_stop_using_table(
|
|
/*===================================*/
|
|
dict_table_t* table, /*!< in/out: table */
|
|
trx_t* trx) /*!< in/out: transaction to use for
|
|
unlocking/locking the data dict */
|
|
{
|
|
while (!dict_stats_stop_bg(table)) {
|
|
DICT_STATS_BG_YIELD(trx);
|
|
}
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Initialize global variables needed for the operation of dict_stats_thread()
|
|
Must be called before dict_stats_thread() is started. */
|
|
UNIV_INTERN
|
|
void
|
|
dict_stats_thread_init()
|
|
{
|
|
ut_a(!srv_read_only_mode);
|
|
|
|
dict_stats_event = os_event_create();
|
|
dict_stats_shutdown_event = os_event_create();
|
|
|
|
/* The recalc_pool_mutex is acquired from:
|
|
1) the background stats gathering thread before any other latch
|
|
and released without latching anything else in between (thus
|
|
any level would do here)
|
|
2) from row_update_statistics_if_needed()
|
|
and released without latching anything else in between. We know
|
|
that dict_sys->mutex (SYNC_DICT) is not acquired when
|
|
row_update_statistics_if_needed() is called and it may be acquired
|
|
inside that function (thus a level <=SYNC_DICT would do).
|
|
3) from row_drop_table_for_mysql() after dict_sys->mutex (SYNC_DICT)
|
|
and dict_operation_lock (SYNC_DICT_OPERATION) have been locked
|
|
(thus a level <SYNC_DICT && <SYNC_DICT_OPERATION would do)
|
|
So we choose SYNC_STATS_AUTO_RECALC to be about below SYNC_DICT. */
|
|
mutex_create(recalc_pool_mutex_key, &recalc_pool_mutex,
|
|
SYNC_STATS_AUTO_RECALC);
|
|
|
|
dict_stats_recalc_pool_init();
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Free resources allocated by dict_stats_thread_init(), must be called
|
|
after dict_stats_thread() has exited. */
|
|
UNIV_INTERN
|
|
void
|
|
dict_stats_thread_deinit()
|
|
/*======================*/
|
|
{
|
|
ut_a(!srv_read_only_mode);
|
|
ut_ad(!srv_dict_stats_thread_active);
|
|
|
|
dict_stats_recalc_pool_deinit();
|
|
|
|
mutex_free(&recalc_pool_mutex);
|
|
memset(&recalc_pool_mutex, 0x0, sizeof(recalc_pool_mutex));
|
|
|
|
os_event_free(dict_stats_event);
|
|
dict_stats_event = NULL;
|
|
os_event_free(dict_stats_shutdown_event);
|
|
dict_stats_shutdown_event = NULL;
|
|
dict_stats_start_shutdown = false;
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
Get the first table that has been added for auto recalc and eventually
|
|
update its stats. */
|
|
static
|
|
void
|
|
dict_stats_process_entry_from_recalc_pool()
|
|
/*=======================================*/
|
|
{
|
|
table_id_t table_id;
|
|
|
|
ut_ad(!srv_read_only_mode);
|
|
|
|
/* pop the first table from the auto recalc pool */
|
|
if (!dict_stats_recalc_pool_get(&table_id)) {
|
|
/* no tables for auto recalc */
|
|
return;
|
|
}
|
|
|
|
dict_table_t* table;
|
|
|
|
mutex_enter(&dict_sys->mutex);
|
|
|
|
table = dict_table_open_on_id(table_id, TRUE, DICT_TABLE_OP_NORMAL);
|
|
|
|
if (table == NULL) {
|
|
/* table does not exist, must have been DROPped
|
|
after its id was enqueued */
|
|
mutex_exit(&dict_sys->mutex);
|
|
return;
|
|
}
|
|
|
|
/* Check whether table is corrupted */
|
|
if (table->corrupted) {
|
|
dict_table_close(table, TRUE, FALSE);
|
|
mutex_exit(&dict_sys->mutex);
|
|
return;
|
|
}
|
|
|
|
table->stats_bg_flag = BG_STAT_IN_PROGRESS;
|
|
|
|
mutex_exit(&dict_sys->mutex);
|
|
|
|
/* ut_time() could be expensive, the current function
|
|
is called once every time a table has been changed more than 10% and
|
|
on a system with lots of small tables, this could become hot. If we
|
|
find out that this is a problem, then the check below could eventually
|
|
be replaced with something else, though a time interval is the natural
|
|
approach. */
|
|
|
|
if (ut_difftime(ut_time(), table->stats_last_recalc)
|
|
< MIN_RECALC_INTERVAL) {
|
|
|
|
/* Stats were (re)calculated not long ago. To avoid
|
|
too frequent stats updates we put back the table on
|
|
the auto recalc list and do nothing. */
|
|
|
|
dict_stats_recalc_pool_add(table);
|
|
|
|
} else {
|
|
|
|
dict_stats_update(table, DICT_STATS_RECALC_PERSISTENT);
|
|
}
|
|
|
|
mutex_enter(&dict_sys->mutex);
|
|
|
|
table->stats_bg_flag = BG_STAT_NONE;
|
|
|
|
dict_table_close(table, TRUE, FALSE);
|
|
|
|
mutex_exit(&dict_sys->mutex);
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
This is the thread for background stats gathering. It pops tables, from
|
|
the auto recalc list and proceeds them, eventually recalculating their
|
|
statistics.
|
|
@return this function does not return, it calls os_thread_exit() */
|
|
extern "C" UNIV_INTERN
|
|
os_thread_ret_t
|
|
DECLARE_THREAD(dict_stats_thread)(
|
|
/*==============================*/
|
|
void* arg MY_ATTRIBUTE((unused))) /*!< in: a dummy parameter
|
|
required by os_thread_create */
|
|
{
|
|
my_thread_init();
|
|
ut_a(!srv_read_only_mode);
|
|
|
|
while (!dict_stats_start_shutdown) {
|
|
|
|
/* Wake up periodically even if not signaled. This is
|
|
because we may lose an event - if the below call to
|
|
dict_stats_process_entry_from_recalc_pool() puts the entry back
|
|
in the list, the os_event_set() will be lost by the subsequent
|
|
os_event_reset(). */
|
|
os_event_wait_time(
|
|
dict_stats_event, MIN_RECALC_INTERVAL * 1000000);
|
|
|
|
if (dict_stats_start_shutdown) {
|
|
break;
|
|
}
|
|
|
|
dict_stats_process_entry_from_recalc_pool();
|
|
|
|
os_event_reset(dict_stats_event);
|
|
}
|
|
|
|
srv_dict_stats_thread_active = false;
|
|
|
|
os_event_set(dict_stats_shutdown_event);
|
|
my_thread_end();
|
|
/* We count the number of threads in os_thread_exit(). A created
|
|
thread should always use that to exit instead of return(). */
|
|
os_thread_exit(NULL);
|
|
|
|
OS_THREAD_DUMMY_RETURN;
|
|
}
|
|
|
|
/** Shut down the dict_stats_thread. */
|
|
void
|
|
dict_stats_shutdown()
|
|
{
|
|
dict_stats_start_shutdown = true;
|
|
os_event_set(dict_stats_event);
|
|
os_event_wait(dict_stats_shutdown_event);
|
|
}
|