| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2012, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file dict/dict0stats_bg.cc |
| 29 |
Code used for background table and index stats gathering. |
| 30 |
|
| 31 |
Created Apr 25, 2012 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "sql_thd_internal_api.h" |
| 35 |
|
| 36 |
#include <stddef.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
#include "dict0dd.h" |
| 41 |
#include "dict0dict.h" |
| 42 |
#include "dict0stats.h" |
| 43 |
#include "dict0stats_bg.h" |
| 44 |
|
| 45 |
#include "os0thread-create.h" |
| 46 |
#include "row0mysql.h" |
| 47 |
#include "srv0start.h" |
| 48 |
#include "ut0new.h" |
| 49 |
|
| 50 |
/** Minimum time interval between stats recalc for a given table */ |
| 51 |
constexpr std::chrono::seconds MIN_RECALC_INTERVAL{10}; |
| 52 |
|
| 53 |
static inline bool SHUTTING_DOWN() { |
| 54 |
return srv_shutdown_state.load() >= |
| 55 |
SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS; |
| 56 |
} |
| 57 |
|
| 58 |
/** Event to wake up the stats thread */ |
| 59 |
os_event_t dict_stats_event = nullptr; |
| 60 |
|
| 61 |
#ifdef UNIV_DEBUG |
| 62 |
/** Used by SET GLOBAL innodb_dict_stats_disabled_debug = 1; */ |
| 63 |
bool innodb_dict_stats_disabled_debug; |
| 64 |
|
| 65 |
static os_event_t dict_stats_disabled_event; |
| 66 |
#endif /* UNIV_DEBUG */ |
| 67 |
|
| 68 |
/** This mutex protects the "recalc_pool" variable. */ |
| 69 |
static ib_mutex_t recalc_pool_mutex; |
| 70 |
|
| 71 |
/** The number of tables that can be added to "recalc_pool" before |
| 72 |
it is enlarged */ |
| 73 |
static const ulint RECALC_POOL_INITIAL_SLOTS = 128; |
| 74 |
|
| 75 |
/** Allocator type, used by std::vector */ |
| 76 |
typedef ut::allocator<table_id_t> recalc_pool_allocator_t; |
| 77 |
|
| 78 |
/** The multitude of tables whose stats are to be automatically |
| 79 |
recalculated - an STL vector */ |
| 80 |
typedef std::vector<table_id_t, recalc_pool_allocator_t> recalc_pool_t; |
| 81 |
|
| 82 |
/** Iterator type for iterating over the elements of objects of type |
| 83 |
recalc_pool_t. */ |
| 84 |
typedef recalc_pool_t::iterator recalc_pool_iterator_t; |
| 85 |
|
| 86 |
/** Pool where we store information on which tables are to be processed |
| 87 |
by background statistics gathering. */ |
| 88 |
static recalc_pool_t *recalc_pool; |
| 89 |
|
| 90 |
/** Initialize the recalc pool, called once during thread initialization. */ |
| 91 |
static void dict_stats_recalc_pool_init() { |
| 92 |
ut_ad(!srv_read_only_mode); |
| 93 |
|
| 94 |
const PSI_memory_key key = mem_key_dict_stats_bg_recalc_pool_t; |
| 95 |
|
| 96 |
recalc_pool = ut::new_withkey<recalc_pool_t>(ut::make_psi_memory_key(key), |
| 97 |
recalc_pool_allocator_t(key)); |
| 98 |
|
| 99 |
recalc_pool->reserve(RECALC_POOL_INITIAL_SLOTS); |
| 100 |
} |
| 101 |
|
| 102 |
/** Free the resources occupied by the recalc pool, called once during |
| 103 |
thread de-initialization. */ |
| 104 |
static void dict_stats_recalc_pool_deinit() { |
| 105 |
ut_ad(!srv_read_only_mode); |
| 106 |
|
| 107 |
recalc_pool->clear(); |
| 108 |
|
| 109 |
ut::delete_(recalc_pool); |
| 110 |
recalc_pool = nullptr; |
| 111 |
} |
| 112 |
|
| 113 |
/** Add a table to the recalc pool, which is processed by the |
| 114 |
background stats gathering thread. Only the table id is added to the |
| 115 |
list, so the table can be closed after being enqueued and it will be |
| 116 |
opened when needed. If the table does not exist later (has been DROPped), |
| 117 |
then it will be removed from the pool and skipped. */ |
| 118 |
void dict_stats_recalc_pool_add( |
| 119 |
const dict_table_t *table) /*!< in: table to add */ |
| 120 |
{ |
| 121 |
ut_ad(!srv_read_only_mode); |
| 122 |
|
| 123 |
mutex_enter(&recalc_pool_mutex); |
| 124 |
|
| 125 |
/* quit if already in the list */ |
| 126 |
for (recalc_pool_iterator_t iter = recalc_pool->begin(); |
| 127 |
iter != recalc_pool->end(); ++iter) { |
| 128 |
if (*iter == table->id) { |
| 129 |
mutex_exit(&recalc_pool_mutex); |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
recalc_pool->push_back(table->id); |
| 135 |
|
| 136 |
mutex_exit(&recalc_pool_mutex); |
| 137 |
|
| 138 |
os_event_set(dict_stats_event); |
| 139 |
} |
| 140 |
|
| 141 |
/** Get a table from the auto recalc pool. The returned table id is removed |
| 142 |
from the pool. |
| 143 |
@return true if the pool was non-empty and "id" was set, false otherwise */ |
| 144 |
static bool dict_stats_recalc_pool_get( |
| 145 |
table_id_t *id) /*!< out: table id, or unmodified if list is |
| 146 |
empty */ |
| 147 |
{ |
| 148 |
ut_ad(!srv_read_only_mode); |
| 149 |
|
| 150 |
mutex_enter(&recalc_pool_mutex); |
| 151 |
|
| 152 |
if (recalc_pool->empty()) { |
| 153 |
mutex_exit(&recalc_pool_mutex); |
| 154 |
return (false); |
| 155 |
} |
| 156 |
|
| 157 |
*id = recalc_pool->at(0); |
| 158 |
|
| 159 |
recalc_pool->erase(recalc_pool->begin()); |
| 160 |
|
| 161 |
mutex_exit(&recalc_pool_mutex); |
| 162 |
|
| 163 |
return (true); |
| 164 |
} |
| 165 |
|
| 166 |
/** Delete a given table from the auto recalc pool. |
| 167 |
dict_stats_recalc_pool_del() */ |
| 168 |
void dict_stats_recalc_pool_del( |
| 169 |
const dict_table_t *table) /*!< in: table to remove */ |
| 170 |
{ |
| 171 |
ut_ad(!srv_read_only_mode); |
| 172 |
ut_ad(dict_sys_mutex_own()); |
| 173 |
|
| 174 |
mutex_enter(&recalc_pool_mutex); |
| 175 |
|
| 176 |
ut_ad(table->id > 0); |
| 177 |
|
| 178 |
for (recalc_pool_iterator_t iter = recalc_pool->begin(); |
| 179 |
iter != recalc_pool->end(); ++iter) { |
| 180 |
if (*iter == table->id) { |
| 181 |
/* erase() invalidates the iterator */ |
| 182 |
recalc_pool->erase(iter); |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
mutex_exit(&recalc_pool_mutex); |
| 188 |
} |
| 189 |
|
| 190 |
/** Wait until background stats thread has stopped using the specified table. |
| 191 |
The caller must have locked the data dictionary using |
| 192 |
row_mysql_lock_data_dictionary() and this function may unlock it temporarily |
| 193 |
and restore the lock before it exits. |
| 194 |
The background stats thread is guaranteed not to start using the specified |
| 195 |
table after this function returns and before the caller unlocks the data |
| 196 |
dictionary because it sets the BG_STAT_IN_PROGRESS bit in table->stats_bg_flag |
| 197 |
under dict_sys->mutex. */ |
| 198 |
void dict_stats_wait_bg_to_stop_using_table( |
| 199 |
dict_table_t *table, /*!< in/out: table */ |
| 200 |
trx_t *trx) /*!< in/out: transaction to use for |
| 201 |
unlocking/locking the data dict */ |
| 202 |
{ |
| 203 |
while (!dict_stats_stop_bg(table)) { |
| 204 |
DICT_STATS_BG_YIELD(trx, UT_LOCATION_HERE); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** Initialize global variables needed for the operation of dict_stats_thread() |
| 209 |
Must be called before dict_stats_thread() is started. */ |
| 210 |
void dict_stats_thread_init() { |
| 211 |
ut_a(!srv_read_only_mode); |
| 212 |
|
| 213 |
dict_stats_event = os_event_create(); |
| 214 |
|
| 215 |
ut_d(dict_stats_disabled_event = os_event_create()); |
| 216 |
|
| 217 |
/* The recalc_pool_mutex is acquired from: |
| 218 |
1) the background stats gathering thread before any other latch |
| 219 |
and released without latching anything else in between (thus |
| 220 |
any level would do here) |
| 221 |
2) from row_update_statistics_if_needed() |
| 222 |
and released without latching anything else in between. We know |
| 223 |
that dict_sys->mutex (SYNC_DICT) is not acquired when |
| 224 |
row_update_statistics_if_needed() is called and it may be acquired |
| 225 |
inside that function (thus a level <=SYNC_DICT would do). |
| 226 |
3) from row_drop_table_for_mysql() after dict_sys->mutex (SYNC_DICT) |
| 227 |
and dict_operation_lock (SYNC_DICT_OPERATION) have been locked |
| 228 |
(thus a level <SYNC_DICT && <SYNC_DICT_OPERATION would do) |
| 229 |
So we choose SYNC_STATS_AUTO_RECALC to be about below SYNC_DICT. */ |
| 230 |
|
| 231 |
mutex_create(LATCH_ID_RECALC_POOL, &recalc_pool_mutex); |
| 232 |
|
| 233 |
dict_stats_recalc_pool_init(); |
| 234 |
} |
| 235 |
|
| 236 |
/** Free resources allocated by dict_stats_thread_init(), must be called |
| 237 |
after dict_stats_thread() has exited. */ |
| 238 |
void dict_stats_thread_deinit() { |
| 239 |
ut_a(!srv_read_only_mode); |
| 240 |
ut_ad(!srv_thread_is_active(srv_threads.m_dict_stats)); |
| 241 |
|
| 242 |
if (recalc_pool == nullptr) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
dict_stats_recalc_pool_deinit(); |
| 247 |
|
| 248 |
mutex_free(&recalc_pool_mutex); |
| 249 |
|
| 250 |
#ifdef UNIV_DEBUG |
| 251 |
os_event_destroy(dict_stats_disabled_event); |
| 252 |
dict_stats_disabled_event = nullptr; |
| 253 |
#endif /* UNIV_DEBUG */ |
| 254 |
|
| 255 |
os_event_destroy(dict_stats_event); |
| 256 |
dict_stats_event = nullptr; |
| 257 |
} |
| 258 |
|
| 259 |
/** Get the first table that has been added for auto recalc and eventually |
| 260 |
update its stats. |
| 261 |
@param[in,out] thd current thread */ |
| 262 |
static void dict_stats_process_entry_from_recalc_pool(THD *thd) { |
| 263 |
table_id_t table_id; |
| 264 |
|
| 265 |
ut_ad(!srv_read_only_mode); |
| 266 |
|
| 267 |
DBUG_EXECUTE_IF("do_not_meta_lock_in_background", return;); |
| 268 |
|
| 269 |
/* pop the first table from the auto recalc pool */ |
| 270 |
if (!dict_stats_recalc_pool_get(&table_id)) { |
| 271 |
/* no tables for auto recalc */ |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
dict_table_t *table; |
| 276 |
MDL_ticket *mdl = nullptr; |
| 277 |
|
| 278 |
/* We need to enter dict_sys->mutex for setting |
| 279 |
table->stats_bg_flag. This is for blocking other DDL, like drop |
| 280 |
table. */ |
| 281 |
dict_sys_mutex_enter(); |
| 282 |
table = dd_table_open_on_id(table_id, thd, &mdl, true, true); |
| 283 |
|
| 284 |
if (table == nullptr) { |
| 285 |
/* table does not exist, must have been DROPped |
| 286 |
after its id was enqueued */ |
| 287 |
dict_sys_mutex_exit(); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
/* Check whether table is corrupted */ |
| 292 |
if (table->is_corrupted()) { |
| 293 |
dd_table_close(table, thd, &mdl, true); |
| 294 |
dict_sys_mutex_exit(); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
/* Set bg flag. */ |
| 299 |
table->stats_bg_flag = BG_STAT_IN_PROGRESS; |
| 300 |
|
| 301 |
dict_sys_mutex_exit(); |
| 302 |
|
| 303 |
/* ut_time_monotonic() could be expensive, the current function |
| 304 |
is called once every time a table has been changed more than 10% and |
| 305 |
on a system with lots of small tables, this could become hot. If we |
| 306 |
find out that this is a problem, then the check below could eventually |
| 307 |
be replaced with something else, though a time interval is the natural |
| 308 |
approach. */ |
| 309 |
|
| 310 |
if (std::chrono::steady_clock::now() - table->stats_last_recalc < |
| 311 |
MIN_RECALC_INTERVAL) { |
| 312 |
/* Stats were (re)calculated not long ago. To avoid |
| 313 |
too frequent stats updates we put back the table on |
| 314 |
the auto recalc list and do nothing. */ |
| 315 |
|
| 316 |
dict_stats_recalc_pool_add(table); |
| 317 |
|
| 318 |
} else { |
| 319 |
dict_stats_update(table, DICT_STATS_RECALC_PERSISTENT); |
| 320 |
} |
| 321 |
|
| 322 |
dict_sys_mutex_enter(); |
| 323 |
|
| 324 |
/* Set back bg flag */ |
| 325 |
table->stats_bg_flag = BG_STAT_NONE; |
| 326 |
|
| 327 |
dict_sys_mutex_exit(); |
| 328 |
|
| 329 |
/* This call can't be moved into dict_sys->mutex protection, |
| 330 |
since it'll cause deadlock while release mdl lock. */ |
| 331 |
dd_table_close(table, thd, &mdl, false); |
| 332 |
} |
| 333 |
|
| 334 |
#ifdef UNIV_DEBUG |
| 335 |
void dict_stats_disabled_debug_update(THD *, SYS_VAR *, void *, |
| 336 |
const void *save) { |
| 337 |
/* This method is protected by mutex, as every SET GLOBAL .. */ |
| 338 |
ut_ad(dict_stats_disabled_event != nullptr); |
| 339 |
|
| 340 |
const bool disable = *static_cast<const bool *>(save); |
| 341 |
|
| 342 |
const int64_t sig_count = os_event_reset(dict_stats_disabled_event); |
| 343 |
|
| 344 |
innodb_dict_stats_disabled_debug = disable; |
| 345 |
|
| 346 |
if (disable) { |
| 347 |
os_event_set(dict_stats_event); |
| 348 |
os_event_wait_low(dict_stats_disabled_event, sig_count); |
| 349 |
} |
| 350 |
} |
| 351 |
#endif /* UNIV_DEBUG */ |
| 352 |
|
| 353 |
/** This is the thread for background stats gathering. It pops tables, from |
| 354 |
the auto recalc list and proceeds them, eventually recalculating their |
| 355 |
statistics. */ |
| 356 |
void dict_stats_thread() { |
| 357 |
ut_a(!srv_read_only_mode); |
| 358 |
THD *thd = create_internal_thd(); |
| 359 |
|
| 360 |
while (!SHUTTING_DOWN()) { |
| 361 |
/* Wake up periodically even if not signaled. This is |
| 362 |
because we may lose an event - if the below call to |
| 363 |
dict_stats_process_entry_from_recalc_pool() puts the entry back |
| 364 |
in the list, the os_event_set() will be lost by the subsequent |
| 365 |
os_event_reset(). */ |
| 366 |
os_event_wait_time(dict_stats_event, MIN_RECALC_INTERVAL); |
| 367 |
|
| 368 |
#ifdef UNIV_DEBUG |
| 369 |
while (innodb_dict_stats_disabled_debug) { |
| 370 |
os_event_set(dict_stats_disabled_event); |
| 371 |
if (SHUTTING_DOWN()) { |
| 372 |
break; |
| 373 |
} |
| 374 |
os_event_wait_time(dict_stats_event, std::chrono::milliseconds{100}); |
| 375 |
} |
| 376 |
#endif /* UNIV_DEBUG */ |
| 377 |
|
| 378 |
if (SHUTTING_DOWN()) { |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
dict_stats_process_entry_from_recalc_pool(thd); |
| 383 |
|
| 384 |
os_event_reset(dict_stats_event); |
| 385 |
} |
| 386 |
|
| 387 |
destroy_internal_thd(thd); |
| 388 |
} |
| 389 |
|
| 390 |
/** Shutdown the dict stats thread. */ |
| 391 |
void dict_stats_shutdown() { |
| 392 |
os_event_set(dict_stats_event); |
| 393 |
srv_threads.m_dict_stats.join(); |
| 394 |
} |