| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/** @file btr/btr0sea.cc |
| 36 |
The index tree adaptive search |
| 37 |
|
| 38 |
Created 2/17/1996 Heikki Tuuri |
| 39 |
*************************************************************************/ |
| 40 |
|
| 41 |
#include "btr0sea.h" |
| 42 |
|
| 43 |
#include <sys/types.h> |
| 44 |
|
| 45 |
#include "btr0btr.h" |
| 46 |
#include "btr0cur.h" |
| 47 |
#include "btr0pcur.h" |
| 48 |
#include "buf0buf.h" |
| 49 |
#include "ha0ha.h" |
| 50 |
|
| 51 |
#include "page0cur.h" |
| 52 |
#include "page0page.h" |
| 53 |
#include "srv0mon.h" |
| 54 |
#include "sync0sync.h" |
| 55 |
|
| 56 |
#include <scope_guard.h> |
| 57 |
|
| 58 |
/** Flag storing if the search system is in enabled state. While it is false, |
| 59 |
the AHI data structures can't have new entries added, they can only be |
| 60 |
removed. It is changed to false while having all AHI latches X-latched, so any |
| 61 |
section that adds entries to AHI data structures must have at least one S-latch. |
| 62 |
All changes to this flag are protected by the btr_search_enable_mutex. */ |
| 63 |
std::atomic_bool btr_search_enabled = true; |
| 64 |
|
| 65 |
/** A value that basically stores the same as btr_search_enabled, but is not |
| 66 |
atomic and thus can be used as SYSVAR. */ |
| 67 |
bool srv_btr_search_enabled = true; |
| 68 |
|
| 69 |
/** Protects changes of btr_search_enabled flag. */ |
| 70 |
static ib_mutex_t btr_search_enabled_mutex; |
| 71 |
|
| 72 |
/** Number of adaptive hash index partition. */ |
| 73 |
ulong btr_ahi_parts = 8; |
| 74 |
ut::fast_modulo_t btr_ahi_parts_fast_modulo(8); |
| 75 |
|
| 76 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 77 |
/** Number of successful adaptive hash index lookups */ |
| 78 |
ulint btr_search_n_succ = 0; |
| 79 |
/** Number of failed adaptive hash index lookups */ |
| 80 |
ulint btr_search_n_hash_fail = 0; |
| 81 |
#endif /* UNIV_SEARCH_PERF_STAT */ |
| 82 |
|
| 83 |
/* It is not unique_ptr, as destroying it at exit() will destroy its rw_lock |
| 84 |
after the PFS is deinitialized. */ |
| 85 |
btr_search_sys_t *btr_search_sys; |
| 86 |
|
| 87 |
/** If the number of records on the page divided by this parameter |
| 88 |
would have been successfully accessed using a hash index, the index |
| 89 |
is then built on the page, assuming the global limit has been reached */ |
| 90 |
constexpr uint32_t BTR_SEARCH_PAGE_BUILD_LIMIT = 16; |
| 91 |
|
| 92 |
/** The global limit for consecutive potentially successful hash searches, |
| 93 |
before hash index building is started */ |
| 94 |
constexpr uint32_t BTR_SEARCH_BUILD_LIMIT = 100; |
| 95 |
|
| 96 |
/** Compute a value to seed the hash value of a record. |
| 97 |
@param[in] index Index structure |
| 98 |
@return hash value for seed */ |
| 99 |
static ulint btr_hash_seed_for_record(const dict_index_t *index) { |
| 100 |
ut_ad(index != nullptr); |
| 101 |
|
| 102 |
return btr_search_hash_index_id(index); |
| 103 |
} |
| 104 |
|
| 105 |
/** Get the hash-table based on index attributes. |
| 106 |
A table is selected from an array of tables using pair of index-id, space-id. |
| 107 |
@param[in] index index handler |
| 108 |
@return hash table */ |
| 109 |
static inline hash_table_t *btr_get_search_table(const dict_index_t *index) { |
| 110 |
/* One can't use the returned table if these latches are not taken. Any resize |
| 111 |
of the AHI that is run in meantime will delete it. Note that btr_ahi_parts |
| 112 |
can't change once AHI is initialized. */ |
| 113 |
ut_ad(rw_lock_own_flagged(btr_get_search_latch(index), |
| 114 |
RW_LOCK_FLAG_S | RW_LOCK_FLAG_X)); |
| 115 |
return btr_get_search_part(index).hash_table; |
| 116 |
} |
| 117 |
|
| 118 |
/** Determine the number of accessed key fields. |
| 119 |
@param[in] prefix_info prefix information to get number of fields from |
| 120 |
@return number of complete or incomplete fields */ |
| 121 |
[[nodiscard]] inline uint16_t btr_search_get_n_fields( |
| 122 |
btr_search_prefix_info_t prefix_info) { |
| 123 |
return prefix_info.n_fields + (prefix_info.n_bytes > 0 ? 1 : 0); |
| 124 |
} |
| 125 |
|
| 126 |
/** Determine the number of accessed key fields. |
| 127 |
@param[in] cursor b-tree cursor |
| 128 |
@return number of complete or incomplete fields */ |
| 129 |
[[nodiscard]] inline uint16_t btr_search_get_n_fields(const btr_cur_t *cursor) { |
| 130 |
return btr_search_get_n_fields(cursor->ahi.prefix_info); |
| 131 |
} |
| 132 |
|
| 133 |
/** Builds a hash index on a page with the block's recommended parameters. If |
| 134 |
the page already has a hash index with different parameters, the old hash index |
| 135 |
is removed. This function checks if n_fields and n_bytes are sensible, and does |
| 136 |
not build a hash index if not. |
| 137 |
@param[in,out] index index for which to build |
| 138 |
@param[in,out] block index page, s-/x- latched. |
| 139 |
@param[in] update specifies if the page should be only added to index |
| 140 |
(false) or possibly updated if any hash entries are |
| 141 |
already added for the records this page has (true) */ |
| 142 |
static void btr_search_build_page_hash_index(dict_index_t *index, |
| 143 |
buf_block_t *block, bool update); |
| 144 |
|
| 145 |
/** Checks that there is a free buffer frame allocated for hash table heap in |
| 146 |
the btr search system. If not, allocates a free frame for the heap. This |
| 147 |
function should be called before reserving any btr search mutex, if the intended |
| 148 |
operation might add nodes to the search system hash table. The heap frame will |
| 149 |
allow to do some insertions to the AHI hash table, but does not guarantee |
| 150 |
anything, i.e. there may be a space in frame only for a part of the nodes to |
| 151 |
insert or some other concurrent operation on AHI could consume the frame's |
| 152 |
memory before we latch the AHI. |
| 153 |
@param[in] index index handler |
| 154 |
*/ |
| 155 |
static inline void btr_search_check_free_space_in_heap(dict_index_t *index) { |
| 156 |
if (!btr_search_enabled) { |
| 157 |
return; |
| 158 |
} |
| 159 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_S)); |
| 160 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_X)); |
| 161 |
|
| 162 |
auto &free_block_for_heap = btr_get_search_part(index).free_block_for_heap; |
| 163 |
|
| 164 |
const bool no_free_block = free_block_for_heap.load() == nullptr; |
| 165 |
|
| 166 |
/* We can't do this check and alloc a block from Buffer pool only when needed |
| 167 |
while inserting new nodes to AHI hash table, as in case the eviction is needed |
| 168 |
to free up a block from LRU, the AHI latches may be required to complete the |
| 169 |
page eviction. The execution can reach the following path: buf_block_alloc -> |
| 170 |
buf_LRU_get_free_block -> buf_LRU_scan_and_free_block -> |
| 171 |
buf_LRU_free_from_common_LRU_list -> buf_LRU_free_page -> |
| 172 |
btr_search_drop_page_hash_index */ |
| 173 |
if (no_free_block) { |
| 174 |
const auto block = buf_block_alloc(nullptr); |
| 175 |
buf_block_t *expected = nullptr; |
| 176 |
ut_ad(block != nullptr); |
| 177 |
|
| 178 |
if (!free_block_for_heap.compare_exchange_strong(expected, block)) { |
| 179 |
/* Someone must have set the free_block in meantime, return the allocated |
| 180 |
block to pool. */ |
| 181 |
buf_block_free(block); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void btr_search_sys_create(ulint hash_size) { |
| 187 |
/* Copy the initial SYSVAR value. While the Server is starting, the updater |
| 188 |
for SYSVARs is not called to set their initial value. */ |
| 189 |
btr_search_enabled = srv_btr_search_enabled; |
| 190 |
btr_search_sys = ut::new_withkey<btr_search_sys_t>( |
| 191 |
ut::make_psi_memory_key(mem_key_ahi), hash_size); |
| 192 |
mutex_create(LATCH_ID_AHI_ENABLED, &btr_search_enabled_mutex); |
| 193 |
} |
| 194 |
|
| 195 |
btr_search_sys_t::btr_search_sys_t(size_t hash_size) { |
| 196 |
using part_type = btr_search_sys_t::search_part_t; |
| 197 |
parts = ut::make_unique_aligned<part_type[]>( |
| 198 |
ut::make_psi_memory_key(mem_key_ahi), alignof(part_type), btr_ahi_parts); |
| 199 |
static_assert(alignof(part_type) >= ut::INNODB_CACHE_LINE_SIZE); |
| 200 |
/* It is written only from one thread during server initialization, so it is |
| 201 |
safe. */ |
| 202 |
btr_ahi_parts_fast_modulo = ut::fast_modulo_t{btr_ahi_parts}; |
| 203 |
|
| 204 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 205 |
parts[i].initialize(hash_size); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void btr_search_sys_t::search_part_t::initialize(size_t hash_size) { |
| 210 |
/* Step-1: Init latches. */ |
| 211 |
rw_lock_create(btr_search_latch_key, &latch, LATCH_ID_BTR_SEARCH); |
| 212 |
|
| 213 |
/* Step-2: Allocate hash tables. */ |
| 214 |
hash_table = ib_create((hash_size / btr_ahi_parts), LATCH_ID_HASH_TABLE_MUTEX, |
| 215 |
0, MEM_HEAP_FOR_BTR_SEARCH); |
| 216 |
hash_table->heap->free_block_ptr = &free_block_for_heap; |
| 217 |
|
| 218 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 219 |
hash_table->adaptive = true; |
| 220 |
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */ |
| 221 |
} |
| 222 |
|
| 223 |
void btr_search_sys_resize(ulint hash_size) { |
| 224 |
/* Step-1: Lock all search latches in exclusive mode. */ |
| 225 |
btr_search_x_lock_all(UT_LOCATION_HERE); |
| 226 |
|
| 227 |
if (btr_search_enabled) { |
| 228 |
btr_search_x_unlock_all(); |
| 229 |
|
| 230 |
ib::error(ER_IB_MSG_45) << "btr_search_sys_resize failed because" |
| 231 |
" hash index hash table is not empty."; |
| 232 |
ut_d(ut_error); |
| 233 |
ut_o(return); |
| 234 |
} |
| 235 |
|
| 236 |
/* Step-2: Recreate hash tables with new size. */ |
| 237 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 238 |
auto &part = btr_search_sys->parts[i]; |
| 239 |
mem_heap_free(part.hash_table->heap); |
| 240 |
ut::delete_(part.hash_table); |
| 241 |
|
| 242 |
part.hash_table = |
| 243 |
ib_create((hash_size / btr_ahi_parts), LATCH_ID_HASH_TABLE_MUTEX, 0, |
| 244 |
MEM_HEAP_FOR_BTR_SEARCH); |
| 245 |
part.hash_table->heap->free_block_ptr = &part.free_block_for_heap; |
| 246 |
|
| 247 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 248 |
part.hash_table->adaptive = true; |
| 249 |
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */ |
| 250 |
} |
| 251 |
|
| 252 |
/* Step-3: Unlock all search latches from exclusive mode. */ |
| 253 |
btr_search_x_unlock_all(); |
| 254 |
} |
| 255 |
|
| 256 |
void btr_search_sys_free() { |
| 257 |
if (btr_search_sys == nullptr) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 262 |
auto &part = btr_search_sys->parts[i]; |
| 263 |
mem_heap_free(part.hash_table->heap); |
| 264 |
ut::delete_(part.hash_table); |
| 265 |
} |
| 266 |
|
| 267 |
ut::delete_(btr_search_sys); |
| 268 |
btr_search_sys = nullptr; |
| 269 |
|
| 270 |
mutex_destroy(&btr_search_enabled_mutex); |
| 271 |
} |
| 272 |
|
| 273 |
void btr_search_await_no_reference(dict_table_t *table, dict_index_t *index, |
| 274 |
bool force) { |
| 275 |
ut_ad(dict_sys_mutex_own()); |
| 276 |
|
| 277 |
uint sleep_counter = 0; |
| 278 |
|
| 279 |
/* We always create search info whether adaptive hash index is enabled or not. |
| 280 |
*/ |
| 281 |
ut_ad(index->search_info); |
| 282 |
|
| 283 |
while (index->search_info->ref_count.load() != 0 && |
| 284 |
(force || srv_shutdown_state.load() < SRV_SHUTDOWN_CLEANUP)) { |
| 285 |
std::this_thread::sleep_for(std::chrono::milliseconds{10}); |
| 286 |
sleep_counter++; |
| 287 |
|
| 288 |
if (sleep_counter % 500 == 0) { |
| 289 |
ib::error(ER_IB_LONG_AHI_DISABLE_WAIT, sleep_counter / 100, |
| 290 |
index->search_info->ref_count.load(), index->name(), |
| 291 |
table->name.m_name); |
| 292 |
} |
| 293 |
/* To avoid a hang here we commit suicide if the ref_count doesn't drop to |
| 294 |
zero in 600 seconds. */ |
| 295 |
ut_a(sleep_counter < 60000); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** Wait for every index in the specified table to have all references from AHI |
| 300 |
dropped. This can only be called while the AHI is being disabled. The last fact |
| 301 |
causes that no new references to indexes can be added from AHI, so the reference |
| 302 |
count will monotonically drop to zero. |
| 303 |
@param[in,out] table table handler */ |
| 304 |
static void btr_search_await_no_reference(dict_table_t *table) { |
| 305 |
ut_ad(dict_sys_mutex_own()); |
| 306 |
ut_ad(mutex_own(&btr_search_enabled_mutex)); |
| 307 |
|
| 308 |
for (auto index = table->first_index(); index != nullptr; |
| 309 |
index = index->next()) { |
| 310 |
btr_search_await_no_reference(table, index, false); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
bool btr_search_disable() { |
| 315 |
mutex_enter(&btr_search_enabled_mutex); |
| 316 |
if (!btr_search_enabled) { |
| 317 |
mutex_exit(&btr_search_enabled_mutex); |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
btr_search_x_lock_all(UT_LOCATION_HERE); |
| 322 |
|
| 323 |
ut_a(btr_search_enabled); |
| 324 |
|
| 325 |
btr_search_enabled = false; |
| 326 |
srv_btr_search_enabled = false; |
| 327 |
btr_search_x_unlock_all(); |
| 328 |
|
| 329 |
/* Clear AHI info for all non-private blocks from Buffer Pool. */ |
| 330 |
buf_pool_clear_hash_index(); |
| 331 |
|
| 332 |
dict_sys_mutex_enter(); |
| 333 |
/* Wait for every index in the data dictionary cache to have no references to |
| 334 |
AHI. After the buf_pool_clear_hash_index() is called, there might be some |
| 335 |
blocks that are being evicted by buf_LRU_free_page() and they are in |
| 336 |
BUF_BLOCK_REMOVE_HASH state. We will wait for them to be removed from AHI. */ |
| 337 |
for (auto table : dict_sys->table_LRU) { |
| 338 |
btr_search_await_no_reference(table); |
| 339 |
} |
| 340 |
|
| 341 |
for (auto table : dict_sys->table_non_LRU) { |
| 342 |
btr_search_await_no_reference(table); |
| 343 |
} |
| 344 |
|
| 345 |
dict_sys_mutex_exit(); |
| 346 |
|
| 347 |
/* Clear the adaptive hash index. */ |
| 348 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 349 |
const auto hash_table = btr_search_sys->parts[i].hash_table; |
| 350 |
hash_table_clear(hash_table); |
| 351 |
mem_heap_empty(hash_table->heap); |
| 352 |
} |
| 353 |
|
| 354 |
mutex_exit(&btr_search_enabled_mutex); |
| 355 |
|
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
void btr_search_enable() { |
| 360 |
os_rmb; |
| 361 |
/* Don't allow enabling AHI if buffer pool resize is happening. |
| 362 |
Ignore it silently. */ |
| 363 |
if (srv_buf_pool_old_size != srv_buf_pool_size) return; |
| 364 |
|
| 365 |
/* We need to synchronize with any threads that are in the middle of |
| 366 |
btr_search_disable() - they must first clear all structures before we can |
| 367 |
re-enable AHI again. */ |
| 368 |
mutex_enter(&btr_search_enabled_mutex); |
| 369 |
btr_search_enabled = true; |
| 370 |
srv_btr_search_enabled = true; |
| 371 |
mutex_exit(&btr_search_enabled_mutex); |
| 372 |
} |
| 373 |
|
| 374 |
btr_search_t *btr_search_info_create(mem_heap_t *heap) { |
| 375 |
btr_search_t *info; |
| 376 |
|
| 377 |
info = (btr_search_t *)mem_heap_alloc(heap, sizeof(btr_search_t)); |
| 378 |
|
| 379 |
ut_d(info->magic_n = BTR_SEARCH_MAGIC_N); |
| 380 |
|
| 381 |
info->ref_count = 0; |
| 382 |
info->root_guess = nullptr; |
| 383 |
|
| 384 |
info->hash_analysis = 0; |
| 385 |
info->n_hash_potential = 0; |
| 386 |
|
| 387 |
info->last_hash_succ = false; |
| 388 |
|
| 389 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 390 |
info->n_hash_succ = 0; |
| 391 |
info->n_hash_fail = 0; |
| 392 |
info->n_patt_succ = 0; |
| 393 |
info->n_searches = 0; |
| 394 |
#endif /* UNIV_SEARCH_PERF_STAT */ |
| 395 |
|
| 396 |
/* Set some sensible values */ |
| 397 |
info->prefix_info = {0, 1, true}; |
| 398 |
|
| 399 |
return info; |
| 400 |
} |
| 401 |
|
| 402 |
/** Updates the search info of an index about hash successes. NOTE that info |
| 403 |
is NOT protected by any semaphore, to save CPU time! Do not assume its fields |
| 404 |
are consistent. |
| 405 |
@param[in] cursor cursor which was just positioned */ |
| 406 |
static void btr_search_info_update_hash(btr_cur_t *cursor) { |
| 407 |
dict_index_t *index = cursor->index; |
| 408 |
int cmp; |
| 409 |
|
| 410 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_S)); |
| 411 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_X)); |
| 412 |
|
| 413 |
if (dict_index_is_ibuf(index)) { |
| 414 |
/* So many deletes are performed on an insert buffer tree |
| 415 |
that we do not consider a hash index useful on it: */ |
| 416 |
|
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
const uint16_t n_unique = dict_index_get_n_unique_in_tree(index); |
| 421 |
const auto info = index->search_info; |
| 422 |
if (info->n_hash_potential != 0) { |
| 423 |
const auto prefix_info = info->prefix_info.load(); |
| 424 |
|
| 425 |
/* Test if the search would have succeeded using the recommended |
| 426 |
hash prefix */ |
| 427 |
|
| 428 |
/* If AHI uses all unique columns as a key, then each record is in its own |
| 429 |
equal-prefix-group, so it doesn't matter if we use left_side or not. Such |
| 430 |
a cache is only useful for searches with the whole unique part of the key |
| 431 |
specified in the query. */ |
| 432 |
|
| 433 |
ut_a(prefix_info.n_fields <= n_unique); |
| 434 |
ut_ad(cursor->up_match <= n_unique); |
| 435 |
ut_ad(cursor->low_match <= n_unique); |
| 436 |
if (prefix_info.n_fields == n_unique && |
| 437 |
std::max(cursor->up_match, cursor->low_match) == n_unique) { |
| 438 |
info->n_hash_potential++; |
| 439 |
|
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
/* The search in B-tree has stopped at two consecutive tuples 'low' and |
| 444 |
'up', and we'd like the search in AHI to also find one of them. First, it |
| 445 |
means that one of them needs to have same first prefix_info.n_fields fields |
| 446 |
and n_bytes of next field equal to the sought tuple. In other words |
| 447 |
`low_matches_prefix||up_matches_prefix`. But, AHI keeps only one record from |
| 448 |
each equal-prefix-group of records, either the left-most or right-most of |
| 449 |
the group, depending on `prefix_info.left_side`. So if both |
| 450 |
`low_matches_prefix` and `up_matches_prefix` are true, it means there's no |
| 451 |
group boundary between them, and even if one of them is at the boundary it's |
| 452 |
"by accident" and the procedure for recommending a prefix length would not |
| 453 |
choose such a short prefix, as it tries to pick a prefix length which would |
| 454 |
create the boundary between low and up. What we want is that if we cache |
| 455 |
the left-most record from each group, then up matches, and low not, so that |
| 456 |
up is at the boundary, and would get cached. And the opposite if we cache |
| 457 |
right-most. */ |
| 458 |
const bool low_matches_prefix = |
| 459 |
0 >= ut_pair_cmp(prefix_info.n_fields, prefix_info.n_bytes, |
| 460 |
cursor->low_match, cursor->low_bytes); |
| 461 |
const bool up_matches_prefix = |
| 462 |
0 >= ut_pair_cmp(prefix_info.n_fields, prefix_info.n_bytes, |
| 463 |
cursor->up_match, cursor->up_bytes); |
| 464 |
if (prefix_info.left_side ? (!low_matches_prefix && up_matches_prefix) |
| 465 |
: (low_matches_prefix && !up_matches_prefix)) { |
| 466 |
info->n_hash_potential++; |
| 467 |
|
| 468 |
return; |
| 469 |
} |
| 470 |
} |
| 471 |
/* We have to set a new recommendation; skip the hash analysis |
| 472 |
for a while to avoid unnecessary CPU time usage when there is no |
| 473 |
chance for success */ |
| 474 |
|
| 475 |
info->hash_analysis = 0; |
| 476 |
|
| 477 |
cmp = ut_pair_cmp(cursor->up_match, cursor->up_bytes, cursor->low_match, |
| 478 |
cursor->low_bytes); |
| 479 |
if (cmp == 0) { |
| 480 |
info->n_hash_potential = 0; |
| 481 |
|
| 482 |
/* For extra safety, we set some sensible values here */ |
| 483 |
info->prefix_info = {0, 1, true}; |
| 484 |
} else if (cmp > 0) { |
| 485 |
info->n_hash_potential = 1; |
| 486 |
|
| 487 |
ut_ad(cursor->up_match <= n_unique); |
| 488 |
if (cursor->up_match == n_unique) { |
| 489 |
info->prefix_info = {0, n_unique, true}; |
| 490 |
|
| 491 |
} else if (cursor->low_match < cursor->up_match) { |
| 492 |
info->prefix_info = {0, static_cast<uint16_t>(cursor->low_match + 1), |
| 493 |
true}; |
| 494 |
} else { |
| 495 |
info->prefix_info = {static_cast<uint32_t>(cursor->low_bytes + 1), |
| 496 |
static_cast<uint16_t>(cursor->low_match), true}; |
| 497 |
} |
| 498 |
} else { |
| 499 |
info->n_hash_potential = 1; |
| 500 |
|
| 501 |
ut_ad(cursor->low_match <= n_unique); |
| 502 |
if (cursor->low_match == n_unique) { |
| 503 |
info->prefix_info = {0, n_unique, false}; |
| 504 |
} else if (cursor->low_match > cursor->up_match) { |
| 505 |
info->prefix_info = {0, static_cast<uint16_t>(cursor->up_match + 1), |
| 506 |
false}; |
| 507 |
} else { |
| 508 |
info->prefix_info = {static_cast<uint32_t>(cursor->up_bytes + 1), |
| 509 |
static_cast<uint16_t>(cursor->up_match), false}; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
/** Update the block search info on hash successes. NOTE that info and |
| 515 |
block->n_hash_helps, ahi.prefix_info are NOT protected by any |
| 516 |
semaphore, to save CPU time! Do not assume the fields are consistent. |
| 517 |
@return true if building a (new) hash index on the block is recommended |
| 518 |
@param[in,out] block buffer block |
| 519 |
@param[in] cursor cursor */ |
| 520 |
static bool btr_search_update_block_hash_info(buf_block_t *block, |
| 521 |
const btr_cur_t *cursor) { |
| 522 |
ut_ad(!rw_lock_own(btr_get_search_latch(cursor->index), RW_LOCK_S)); |
| 523 |
ut_ad(!rw_lock_own(btr_get_search_latch(cursor->index), RW_LOCK_X)); |
| 524 |
ut_ad(rw_lock_own_flagged(&block->lock, RW_LOCK_FLAG_S | RW_LOCK_FLAG_X)); |
| 525 |
|
| 526 |
const auto info = cursor->index->search_info; |
| 527 |
info->last_hash_succ = false; |
| 528 |
|
| 529 |
ut_a(buf_block_state_valid(block)); |
| 530 |
ut_ad(info->magic_n == BTR_SEARCH_MAGIC_N); |
| 531 |
|
| 532 |
if (block->n_hash_helps > 0 && info->n_hash_potential > 0 && |
| 533 |
block->ahi.recommended_prefix_info.load() == info->prefix_info.load()) { |
| 534 |
/* The current index's prefix info is already used as recommendation for |
| 535 |
this block's prefix. */ |
| 536 |
if (block->ahi.index && |
| 537 |
block->ahi.prefix_info.load() == info->prefix_info.load()) { |
| 538 |
/* The recommended prefix is what is actually being used in this block. |
| 539 |
The search would presumably have succeeded using the hash index. */ |
| 540 |
|
| 541 |
info->last_hash_succ = true; |
| 542 |
} |
| 543 |
|
| 544 |
block->n_hash_helps++; |
| 545 |
} else { |
| 546 |
block->n_hash_helps = 1; |
| 547 |
block->ahi.recommended_prefix_info = info->prefix_info.load(); |
| 548 |
} |
| 549 |
|
| 550 |
#ifdef UNIV_DEBUG |
| 551 |
if (cursor->index->table->does_not_fit_in_memory) { |
| 552 |
block->n_hash_helps = 0; |
| 553 |
} |
| 554 |
#endif /* UNIV_DEBUG */ |
| 555 |
|
| 556 |
if (info->n_hash_potential >= BTR_SEARCH_BUILD_LIMIT && |
| 557 |
block->n_hash_helps > |
| 558 |
page_get_n_recs(block->frame) / BTR_SEARCH_PAGE_BUILD_LIMIT) { |
| 559 |
if (!block->ahi.index || |
| 560 |
block->n_hash_helps > 2 * page_get_n_recs(block->frame) || |
| 561 |
block->ahi.recommended_prefix_info.load() != |
| 562 |
block->ahi.prefix_info.load()) { |
| 563 |
/* Build a new hash index on the page if: |
| 564 |
- the block is not yet in AHI, or |
| 565 |
- we queried 2 times the number of records on this page successfully (TODO |
| 566 |
explain the reason why it is needed), or |
| 567 |
- the recommendation differs from what prefix info is currently used in |
| 568 |
block for hashing in AHI. */ |
| 569 |
|
| 570 |
return true; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
/** Updates a hash node reference when it has been unsuccessfully used in a |
| 578 |
search which could have succeeded with the used hash parameters. This can |
| 579 |
happen because when building a hash index for a page, we do not check |
| 580 |
what happens at page boundaries, and therefore there can be misleading |
| 581 |
hash nodes. Also, collisions in the hash value can lead to misleading |
| 582 |
references. This function lazily fixes these imperfections in the hash |
| 583 |
index. |
| 584 |
@param[in] block buffer block where cursor positioned |
| 585 |
@param[in] cursor cursor */ |
| 586 |
static void btr_search_update_hash_ref(buf_block_t *block, |
| 587 |
const btr_cur_t *cursor) { |
| 588 |
ut_ad(cursor->flag == BTR_CUR_HASH_FAIL); |
| 589 |
ut_ad(!rw_lock_own(btr_get_search_latch(cursor->index), RW_LOCK_X)); |
| 590 |
ut_ad(rw_lock_own_flagged(&(block->lock), RW_LOCK_FLAG_S | RW_LOCK_FLAG_X)); |
| 591 |
ut_ad(page_align(btr_cur_get_rec(cursor)) == buf_block_get_frame(block)); |
| 592 |
block->ahi.validate(); |
| 593 |
|
| 594 |
const auto index = block->ahi.index.load(); |
| 595 |
const auto block_prefix_info = block->ahi.prefix_info.load(); |
| 596 |
|
| 597 |
if (!index) { |
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
ut_ad(block->page.id.space() == index->space); |
| 602 |
ut_a(index == cursor->index); |
| 603 |
ut_a(!dict_index_is_ibuf(index)); |
| 604 |
|
| 605 |
const auto info = index->search_info; |
| 606 |
/* Dirty read without latch, will be repeated after we take the x-latch, which |
| 607 |
we take after we have the hash value ready, to reduce time consumed under the |
| 608 |
latch. If the current index's prefix info is different than current block's |
| 609 |
prefix info used in AHI, then the block will have to be removed (and |
| 610 |
reinserted) from AHI very soon. It does not make sense to update any records |
| 611 |
using outdated prefix info. Note that only records folded using the block's |
| 612 |
current prefix info can be in AHI. */ |
| 613 |
if (info->n_hash_potential > 0 && |
| 614 |
block_prefix_info == info->prefix_info.load()) { |
| 615 |
const auto rec = btr_cur_get_rec(cursor); |
| 616 |
|
| 617 |
if (!page_rec_is_user_rec(rec)) { |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
const auto hash_value = rec_hash( |
| 622 |
rec, Rec_offsets{}.compute(rec, index), block_prefix_info.n_fields, |
| 623 |
block_prefix_info.n_bytes, btr_hash_seed_for_record(index), index); |
| 624 |
|
| 625 |
btr_search_check_free_space_in_heap(cursor->index); |
| 626 |
|
| 627 |
if (!btr_search_x_lock_nowait(cursor->index, UT_LOCATION_HERE)) { |
| 628 |
return; |
| 629 |
} |
| 630 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 631 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 632 |
nullptr then, and later maybe even re-inserted to AHI again in case we don't |
| 633 |
have the block->lock X-latched). The block's prefix info will be current and |
| 634 |
we check if it still matches the prefix info we used to fold the record. If |
| 635 |
it does not match, we can't add the entry to hash table, as it would never |
| 636 |
be deleted and would corrupt the AHI. */ |
| 637 |
if (btr_search_enabled && block->ahi.index != nullptr) { |
| 638 |
ut_ad(block->ahi.index == index); |
| 639 |
if (info->n_hash_potential > 0 && |
| 640 |
block_prefix_info == block->ahi.prefix_info.load()) { |
| 641 |
const auto hash_table = btr_get_search_table(index); |
| 642 |
ha_insert_for_hash(hash_table, hash_value, block, rec); |
| 643 |
} |
| 644 |
} |
| 645 |
btr_search_x_unlock(cursor->index); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
void btr_search_info_update_slow(btr_cur_t *cursor) { |
| 650 |
ut_ad(!rw_lock_own(btr_get_search_latch(cursor->index), RW_LOCK_S)); |
| 651 |
ut_ad(!rw_lock_own(btr_get_search_latch(cursor->index), RW_LOCK_X)); |
| 652 |
|
| 653 |
const auto block = btr_cur_get_block(cursor); |
| 654 |
|
| 655 |
/* NOTE that the following two function calls do NOT protect |
| 656 |
info or block->ahi with any semaphore, to save CPU time! |
| 657 |
We cannot assume the fields are consistent when we return from |
| 658 |
those functions! */ |
| 659 |
btr_search_info_update_hash(cursor); |
| 660 |
|
| 661 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 662 |
if (cursor->flag == BTR_CUR_HASH_FAIL) { |
| 663 |
btr_search_n_hash_fail++; |
| 664 |
} |
| 665 |
#endif /* UNIV_SEARCH_PERF_STAT */ |
| 666 |
|
| 667 |
if (btr_search_update_block_hash_info(block, cursor)) { |
| 668 |
/* Note that since we did not protect block->ahi with any semaphore, the |
| 669 |
values can be inconsistent. We have to check inside the function call that |
| 670 |
they make sense. */ |
| 671 |
btr_search_build_page_hash_index(cursor->index, block, false); |
| 672 |
} else if (cursor->flag == BTR_CUR_HASH_FAIL) { |
| 673 |
/* Update the hash node reference, if appropriate. If |
| 674 |
btr_search_update_block_hash_info decided to build the index for this |
| 675 |
block, the record should be hashed correctly with the rest of the block's |
| 676 |
records. */ |
| 677 |
btr_search_update_hash_ref(block, cursor); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/** Checks if a guessed position for a tree cursor is right. Note that if |
| 682 |
mode is PAGE_CUR_LE, which is used in inserts, and the function returns |
| 683 |
true, then cursor->up_match and cursor->low_match both have sensible values. |
| 684 |
@param[in,out] cursor Guess cursor position |
| 685 |
@param[in] can_only_compare_to_cursor_rec |
| 686 |
If we do not have a latch on the page of cursor, but a |
| 687 |
latch corresponding search system, then ONLY the columns |
| 688 |
of the record UNDER the cursor are protected, not the |
| 689 |
next or previous record in the chain: we cannot look at |
| 690 |
the next or previous record to check our guess! |
| 691 |
@param[in] tuple Data tuple |
| 692 |
@param[in] mode PAGE_CUR_L, PAGE_CUR_LE, PAGE_CUR_G, PAGE_CUR_GE |
| 693 |
@param[in] mtr Mini-transaction |
| 694 |
@return true if success */ |
| 695 |
static bool btr_search_check_guess(btr_cur_t *cursor, |
| 696 |
bool can_only_compare_to_cursor_rec, |
| 697 |
const dtuple_t *tuple, ulint mode, |
| 698 |
mtr_t *mtr) { |
| 699 |
rec_t *rec; |
| 700 |
ulint match; |
| 701 |
|
| 702 |
const auto n_unique = dict_index_get_n_unique_in_tree(cursor->index); |
| 703 |
|
| 704 |
rec = btr_cur_get_rec(cursor); |
| 705 |
|
| 706 |
ut_ad(page_rec_is_user_rec(rec)); |
| 707 |
|
| 708 |
match = 0; |
| 709 |
|
| 710 |
Rec_offsets offsets; |
| 711 |
{ |
| 712 |
const auto cmp = |
| 713 |
tuple->compare(rec, cursor->index, |
| 714 |
offsets.compute(rec, cursor->index, n_unique), &match); |
| 715 |
|
| 716 |
if (mode == PAGE_CUR_GE) { |
| 717 |
if (cmp > 0) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
|
| 721 |
cursor->up_match = match; |
| 722 |
|
| 723 |
if (match >= n_unique) { |
| 724 |
return true; |
| 725 |
} |
| 726 |
} else if (mode == PAGE_CUR_LE) { |
| 727 |
if (cmp < 0) { |
| 728 |
return false; |
| 729 |
} |
| 730 |
|
| 731 |
cursor->low_match = match; |
| 732 |
|
| 733 |
} else if (mode == PAGE_CUR_G) { |
| 734 |
if (cmp >= 0) { |
| 735 |
return false; |
| 736 |
} |
| 737 |
} else if (mode == PAGE_CUR_L) { |
| 738 |
if (cmp <= 0) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
if (can_only_compare_to_cursor_rec) { |
| 744 |
/* Since we could not determine if our guess is right just by |
| 745 |
looking at the record under the cursor, return false */ |
| 746 |
return false; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
match = 0; |
| 751 |
|
| 752 |
if ((mode == PAGE_CUR_G) || (mode == PAGE_CUR_GE)) { |
| 753 |
rec_t *prev_rec; |
| 754 |
|
| 755 |
ut_ad(!page_rec_is_infimum(rec)); |
| 756 |
|
| 757 |
prev_rec = page_rec_get_prev(rec); |
| 758 |
|
| 759 |
if (page_rec_is_infimum(prev_rec)) { |
| 760 |
return btr_page_get_prev(page_align(prev_rec), mtr) == FIL_NULL; |
| 761 |
} |
| 762 |
|
| 763 |
const auto cmp = tuple->compare( |
| 764 |
prev_rec, cursor->index, |
| 765 |
offsets.compute(prev_rec, cursor->index, n_unique), &match); |
| 766 |
bool success; |
| 767 |
if (mode == PAGE_CUR_GE) { |
| 768 |
success = cmp > 0; |
| 769 |
} else { |
| 770 |
success = cmp >= 0; |
| 771 |
} |
| 772 |
|
| 773 |
return success; |
| 774 |
} else { |
| 775 |
rec_t *next_rec; |
| 776 |
|
| 777 |
ut_ad(!page_rec_is_supremum(rec)); |
| 778 |
|
| 779 |
next_rec = page_rec_get_next(rec); |
| 780 |
|
| 781 |
if (page_rec_is_supremum(next_rec)) { |
| 782 |
if (btr_page_get_next(page_align(next_rec), mtr) == FIL_NULL) { |
| 783 |
cursor->up_match = 0; |
| 784 |
return true; |
| 785 |
} |
| 786 |
|
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
const auto cmp = tuple->compare( |
| 791 |
next_rec, cursor->index, |
| 792 |
offsets.compute(next_rec, cursor->index, n_unique), &match); |
| 793 |
bool success; |
| 794 |
if (mode == PAGE_CUR_LE) { |
| 795 |
success = cmp < 0; |
| 796 |
cursor->up_match = match; |
| 797 |
} else { |
| 798 |
success = cmp <= 0; |
| 799 |
} |
| 800 |
return success; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
bool btr_search_guess_on_hash(const dtuple_t *tuple, ulint mode, |
| 805 |
ulint latch_mode, btr_cur_t *cursor, |
| 806 |
ulint has_search_latch, mtr_t *mtr) { |
| 807 |
const rec_t *rec; |
| 808 |
#ifdef notdefined |
| 809 |
btr_cur_t cursor2; |
| 810 |
btr_pcur_t pcur; |
| 811 |
#endif |
| 812 |
|
| 813 |
if (!btr_search_enabled) { |
| 814 |
return false; |
| 815 |
} |
| 816 |
|
| 817 |
ut_ad(tuple && cursor && mtr); |
| 818 |
const auto index = cursor->index; |
| 819 |
ut_ad(index != nullptr); |
| 820 |
const auto info = index->search_info; |
| 821 |
ut_ad(info != nullptr); |
| 822 |
ut_ad(!dict_index_is_ibuf(index)); |
| 823 |
ut_ad((latch_mode == BTR_SEARCH_LEAF) || (latch_mode == BTR_MODIFY_LEAF)); |
| 824 |
|
| 825 |
/* Not supported for spatial index */ |
| 826 |
ut_ad(!dict_index_is_spatial(index)); |
| 827 |
|
| 828 |
/* If we decide to return before doing actual hash search, we will return with |
| 829 |
the following state of the cursor. */ |
| 830 |
cursor->flag = BTR_CUR_HASH_NOT_ATTEMPTED; |
| 831 |
/* Note that, for efficiency, the struct info may not be protected by |
| 832 |
any latch here! */ |
| 833 |
|
| 834 |
if (info->n_hash_potential == 0) { |
| 835 |
return false; |
| 836 |
} |
| 837 |
|
| 838 |
const auto prefix_info = info->prefix_info.load(); |
| 839 |
|
| 840 |
cursor->ahi.prefix_info = prefix_info; |
| 841 |
|
| 842 |
if (dtuple_get_n_fields(tuple) < btr_search_get_n_fields(cursor)) { |
| 843 |
return false; |
| 844 |
} |
| 845 |
|
| 846 |
const auto hash_value = |
| 847 |
dtuple_hash(tuple, prefix_info.n_fields, prefix_info.n_bytes, |
| 848 |
btr_hash_seed_for_record(index)); |
| 849 |
|
| 850 |
cursor->ahi.ahi_hash_value = hash_value; |
| 851 |
|
| 852 |
if (!has_search_latch) { |
| 853 |
if (!btr_search_s_lock_nowait(index, UT_LOCATION_HERE)) { |
| 854 |
return false; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
auto latch_guard = |
| 859 |
create_scope_guard([index]() { btr_search_s_unlock(index); }); |
| 860 |
|
| 861 |
if (!has_search_latch) { |
| 862 |
if (!btr_search_enabled) { |
| 863 |
return false; |
| 864 |
} |
| 865 |
} else { |
| 866 |
/* If we had a latch, then the guard is not needed. */ |
| 867 |
latch_guard.release(); |
| 868 |
} |
| 869 |
|
| 870 |
ut_ad(rw_lock_get_writer(btr_get_search_latch(index)) != RW_LOCK_X); |
| 871 |
ut_ad(rw_lock_get_reader_count(btr_get_search_latch(index)) > 0); |
| 872 |
|
| 873 |
rec = |
| 874 |
(rec_t *)ha_search_and_get_data(btr_get_search_table(index), hash_value); |
| 875 |
|
| 876 |
/* We did the hash search. If we decide to return before successfully |
| 877 |
verifying the search is correct, we will return with the following state of |
| 878 |
the cursor. */ |
| 879 |
cursor->flag = BTR_CUR_HASH_FAIL; |
| 880 |
|
| 881 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 882 |
info->n_hash_fail++; |
| 883 |
#endif /* UNIV_SEARCH_PERF_STAT */ |
| 884 |
|
| 885 |
info->last_hash_succ = false; |
| 886 |
|
| 887 |
if (rec == nullptr) { |
| 888 |
return false; |
| 889 |
} |
| 890 |
|
| 891 |
buf_block_t *block = buf_block_from_ahi(rec); |
| 892 |
|
| 893 |
if (!has_search_latch) { |
| 894 |
if (!buf_page_get_known_nowait(latch_mode, block, Cache_hint::MAKE_YOUNG, |
| 895 |
__FILE__, __LINE__, mtr)) { |
| 896 |
return false; |
| 897 |
} |
| 898 |
|
| 899 |
/* Release the AHI S-latch. It is released after the |
| 900 |
buf_page_get_known_nowait which is latching the block, so no one else can |
| 901 |
remove it. Up to this point we have the AHI is S-latched and since we found |
| 902 |
an AHI entry that leads to this block, the entry can't be removed and thus |
| 903 |
the block must be still in the buffer pool. */ |
| 904 |
latch_guard.reset(); |
| 905 |
|
| 906 |
buf_block_dbg_add_level(block, SYNC_TREE_NODE_FROM_HASH); |
| 907 |
} |
| 908 |
|
| 909 |
if (buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE) { |
| 910 |
ut_ad(buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH); |
| 911 |
|
| 912 |
if (!has_search_latch) { |
| 913 |
btr_leaf_page_release(block, latch_mode, mtr); |
| 914 |
} |
| 915 |
|
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
ut_ad(page_rec_is_user_rec(rec)); |
| 920 |
|
| 921 |
btr_cur_position(index, (rec_t *)rec, block, cursor); |
| 922 |
|
| 923 |
/* Check the validity of the guess within the page */ |
| 924 |
|
| 925 |
/* If we only have the latch on search system, not on the |
| 926 |
page, it only protects the columns of the record the cursor |
| 927 |
is positioned on. We cannot look at the next of the previous |
| 928 |
record to determine if our guess for the cursor position is |
| 929 |
right. */ |
| 930 |
if (index->space != block->page.id.space() || |
| 931 |
index->id != btr_page_get_index_id(block->frame) || |
| 932 |
!btr_search_check_guess(cursor, has_search_latch, tuple, mode, mtr)) { |
| 933 |
if (!has_search_latch) { |
| 934 |
btr_leaf_page_release(block, latch_mode, mtr); |
| 935 |
} |
| 936 |
|
| 937 |
return false; |
| 938 |
} |
| 939 |
|
| 940 |
if (info->n_hash_potential < BTR_SEARCH_BUILD_LIMIT + 5) { |
| 941 |
info->n_hash_potential++; |
| 942 |
} |
| 943 |
|
| 944 |
#ifdef notdefined |
| 945 |
/* These lines of code can be used in a debug version to check |
| 946 |
the correctness of the searched cursor position: */ |
| 947 |
|
| 948 |
info->last_hash_succ = false; |
| 949 |
|
| 950 |
/* Currently, does not work if the following fails: */ |
| 951 |
ut_ad(!has_search_latch); |
| 952 |
|
| 953 |
btr_leaf_page_release(block, latch_mode, mtr); |
| 954 |
|
| 955 |
btr_cur_search_to_nth_level(index, 0, tuple, mode, latch_mode, &cursor2, 0, |
| 956 |
mtr); |
| 957 |
|
| 958 |
if (mode == PAGE_CUR_GE && page_rec_is_supremum(btr_cur_get_rec(&cursor2))) { |
| 959 |
/* If mode is PAGE_CUR_GE, then the binary search |
| 960 |
in the index tree may actually take us to the supremum |
| 961 |
of the previous page */ |
| 962 |
|
| 963 |
info->last_hash_succ = false; |
| 964 |
|
| 965 |
pcur.open_on_user_rec(index, tuple, mode, latch_mode, mtr, |
| 966 |
UT_LOCATION_HERE); |
| 967 |
|
| 968 |
ut_ad(pcur.get_rec() == btr_cur_get_rec(cursor)); |
| 969 |
} else { |
| 970 |
ut_ad(btr_cur_get_rec(&cursor2) == btr_cur_get_rec(cursor)); |
| 971 |
} |
| 972 |
|
| 973 |
/* NOTE that it is theoretically possible that the above assertions |
| 974 |
fail if the page of the cursor gets removed from the buffer pool |
| 975 |
meanwhile! Thus it might not be a bug. */ |
| 976 |
#endif |
| 977 |
|
| 978 |
info->last_hash_succ = true; |
| 979 |
cursor->flag = BTR_CUR_HASH; |
| 980 |
|
| 981 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 982 |
/* Revert the accounting we did for the hash search failure that was prepared |
| 983 |
above. */ |
| 984 |
info->n_hash_fail--; |
| 985 |
|
| 986 |
info->n_hash_succ++; |
| 987 |
btr_search_n_succ++; |
| 988 |
#endif |
| 989 |
if (!has_search_latch && buf_page_peek_if_too_old(&block->page)) { |
| 990 |
buf_page_make_young(&block->page); |
| 991 |
} |
| 992 |
|
| 993 |
/* Increment the page get statistics though we did not really |
| 994 |
fix the page: for user info only */ |
| 995 |
|
| 996 |
{ |
| 997 |
buf_pool_t *buf_pool = buf_pool_from_bpage(&block->page); |
| 998 |
|
| 999 |
Counter::inc(buf_pool->stat.m_n_page_gets, block->page.id.page_no()); |
| 1000 |
} |
| 1001 |
|
| 1002 |
return true; |
| 1003 |
} |
| 1004 |
|
| 1005 |
void btr_search_drop_page_hash_index(buf_block_t *block, bool force) { |
| 1006 |
for (;;) { |
| 1007 |
/* Do a dirty check on block->index, return if the block is |
| 1008 |
not in the adaptive hash index. */ |
| 1009 |
const auto index = block->ahi.index.load(); |
| 1010 |
|
| 1011 |
if (index == nullptr) { |
| 1012 |
return; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/* Is it safe to dereference the index here? |
| 1016 |
If this method is called from a method that uses reference to the index, or |
| 1017 |
a cursor on that index, it should not be freed until they finish. Such |
| 1018 |
methods are: |
| 1019 |
- btr_free_root, |
| 1020 |
- btr_page_reorganize_low, |
| 1021 |
- btr_page_empty, |
| 1022 |
- btr_lift_page_up, |
| 1023 |
- btr_compress, |
| 1024 |
- btr_discard_only_page_on_level, |
| 1025 |
- btr_discard_page, |
| 1026 |
- btr_search_update_hash_on_move, which in turn is called from: |
| 1027 |
- btr_root_raise_and_insert |
| 1028 |
- btr_page_split_and_insert |
| 1029 |
- btr_lift_page_up |
| 1030 |
- page_copy_rec_list_end |
| 1031 |
- page_copy_rec_list_start |
| 1032 |
- btr_search_build_page_hash_index, which in turn is called from |
| 1033 |
- btr_search_info_update_slow |
| 1034 |
- btr_search_update_hash_on_move |
| 1035 |
- page_zip_reorganize |
| 1036 |
|
| 1037 |
There is a btr_search_drop_page_hash_when_freed() that is calling this |
| 1038 |
method, but it asserts: |
| 1039 |
ut_ad(index->table->n_ref_count > 0 || dict_sys_mutex_own()); |
| 1040 |
This checks the table is opened or we have dict sys mutex, either of which |
| 1041 |
should prevent index from being freed. Assuming the current thread is the |
| 1042 |
one that keeps the table open, which we can't assure easily, but checking if |
| 1043 |
the current thread keeps the current block S- or X-latched should make it |
| 1044 |
almost certain. |
| 1045 |
|
| 1046 |
The last other call is from buf_LRU_free_page(). It does not hold index or |
| 1047 |
any latches that could prevent the index from being freed. However, while |
| 1048 |
the block is in AHI, it will prevent the index from being freed - the |
| 1049 |
block.ahi.index->search_info->ref_count will be positive, and the |
| 1050 |
dict_index_remove_from_cache_low() will assure it is 0 before the index is |
| 1051 |
freed. The buf_LRU_free_page() first removes the block from all lists and |
| 1052 |
hashes, so at the point this method is called, it will be held "privately" - |
| 1053 |
no other thread will be able to reach it without iterating every block in |
| 1054 |
chunks in the Buffer Pool. So, for index to be freed here after it was |
| 1055 |
checked to not be nullptr, it would require some other thread to iterate |
| 1056 |
entire Buffer Pool and remove this block from AHI. Something similar is done |
| 1057 |
during btr_search_disable(), but in buf_pool_clear_hash_index() we latch the |
| 1058 |
block's mutex to prevent block state change, and verify the state is not |
| 1059 |
BUF_BLOCK_REMOVE_HASH. Note that the block that we currently process a part |
| 1060 |
of buf_LRU_free_page() must be in this state. */ |
| 1061 |
ut_d(bool block_held_in_private = |
| 1062 |
buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH); |
| 1063 |
ut_d(bool index_is_open = index->table->n_ref_count > 0); |
| 1064 |
ut_d(bool block_is_being_accessed = rw_lock_own_flagged( |
| 1065 |
&block->lock, RW_LOCK_FLAG_S | RW_LOCK_FLAG_X)); |
| 1066 |
ut_d(bool index_cant_be_freed = |
| 1067 |
(index_is_open && block_is_being_accessed) || |
| 1068 |
dict_sys_mutex_own()); |
| 1069 |
ut_ad(block_held_in_private || index_cant_be_freed); |
| 1070 |
/* For now, the only usage of the `force` param is in buf_LRU_free_page() |
| 1071 |
while the block state is BUF_BLOCK_REMOVE_HASH. */ |
| 1072 |
ut_ad(block_held_in_private == force); |
| 1073 |
|
| 1074 |
ut_ad(!btr_search_own_any(RW_LOCK_S)); |
| 1075 |
ut_ad(!btr_search_own_any(RW_LOCK_X)); |
| 1076 |
|
| 1077 |
block->ahi.validate(); |
| 1078 |
const auto prefix_info = block->ahi.prefix_info.load(); |
| 1079 |
|
| 1080 |
ut_ad(!index->disable_ahi); |
| 1081 |
|
| 1082 |
ut_ad(block->page.id.space() == index->space); |
| 1083 |
ut_a(btr_page_get_index_id(block->frame) == index->id); |
| 1084 |
ut_a(!dict_index_is_ibuf(index)); |
| 1085 |
#ifdef UNIV_DEBUG |
| 1086 |
switch (dict_index_get_online_status(index)) { |
| 1087 |
case ONLINE_INDEX_CREATION: |
| 1088 |
/* The index is being created (bulk loaded). */ |
| 1089 |
case ONLINE_INDEX_COMPLETE: |
| 1090 |
/* The index has been published. */ |
| 1091 |
case ONLINE_INDEX_ABORTED: |
| 1092 |
/* Either the index creation was aborted due to an |
| 1093 |
error observed by InnoDB (in which case there should |
| 1094 |
not be any adaptive hash index entries), or it was |
| 1095 |
completed and then flagged aborted in |
| 1096 |
rollback_inplace_alter_table(). */ |
| 1097 |
case ONLINE_INDEX_ABORTED_DROPPED: |
| 1098 |
/* Since dropping the indexes are delayed to post_ddl, |
| 1099 |
this status is similar to ONLINE_INDEX_ABORTED. */ |
| 1100 |
break; |
| 1101 |
default: |
| 1102 |
ut_error; |
| 1103 |
} |
| 1104 |
#endif /* UNIV_DEBUG */ |
| 1105 |
|
| 1106 |
/* NOTE: The AHI fields of block must not be accessed after |
| 1107 |
releasing search latch, as the index page might only be s-latched! */ |
| 1108 |
|
| 1109 |
ut_a(btr_search_get_n_fields(prefix_info) > 0); |
| 1110 |
|
| 1111 |
auto page = block->frame; |
| 1112 |
const auto n_recs = page_get_n_recs(page); |
| 1113 |
|
| 1114 |
/* Calculate and cache fold values into an array for fast deletion |
| 1115 |
from the hash index */ |
| 1116 |
|
| 1117 |
auto hashes = ut::make_unique<uint64_t[]>(UT_NEW_THIS_FILE_PSI_KEY, n_recs); |
| 1118 |
|
| 1119 |
size_t n_cached = 0; |
| 1120 |
|
| 1121 |
const rec_t *rec = page_get_infimum_rec(page); |
| 1122 |
rec = page_rec_get_next_low(rec, page_is_comp(page)); |
| 1123 |
|
| 1124 |
const auto index_hash = btr_hash_seed_for_record(index); |
| 1125 |
|
| 1126 |
uint64_t prev_hash_value = 0; |
| 1127 |
{ |
| 1128 |
Rec_offsets offsets; |
| 1129 |
|
| 1130 |
while (!page_rec_is_supremum(rec)) { |
| 1131 |
const auto hash_value = rec_hash( |
| 1132 |
rec, |
| 1133 |
offsets.compute(rec, index, btr_search_get_n_fields(prefix_info)), |
| 1134 |
prefix_info.n_fields, prefix_info.n_bytes, index_hash, index); |
| 1135 |
|
| 1136 |
if (hash_value != prev_hash_value || prev_hash_value == 0) { |
| 1137 |
/* The fold identifies a single hash chain to possibly contain the |
| 1138 |
record. We will use it after this iteration over the page's records |
| 1139 |
to remove any entries from that chain that point to the page. */ |
| 1140 |
hashes[n_cached] = hash_value; |
| 1141 |
n_cached++; |
| 1142 |
} |
| 1143 |
rec = page_rec_get_next_low(rec, page_rec_is_comp(rec)); |
| 1144 |
prev_hash_value = hash_value; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
btr_search_x_lock(index, UT_LOCATION_HERE); |
| 1149 |
const auto critical_section_guard = create_scope_guard([block, index]() { |
| 1150 |
block->ahi.validate(); |
| 1151 |
btr_search_x_unlock(index); |
| 1152 |
}); |
| 1153 |
|
| 1154 |
{ |
| 1155 |
const auto is_ahi_enabled = btr_search_enabled.load(); |
| 1156 |
/* We need to read the block->ahi.index after we read btr_search_enabled. |
| 1157 |
If the block->ahi.index is nullptr, then it will stay so, because we have |
| 1158 |
the X-latch on AHI part. |
| 1159 |
If it is not null, then it will stay not-null unless all of the following |
| 1160 |
conditions are met: |
| 1161 |
- the AHI is being disabled, |
| 1162 |
- the block is in LRU (i.e. the force == false). |
| 1163 |
So, if we were to read block->ahi.index first, then after reading |
| 1164 |
btr_search_enabled == true, we would not know if it wasn't false when the |
| 1165 |
latched_index was read, and then the block index could be set to nullptr |
| 1166 |
by the buf_pool_clear_hash_index and the AHI was enabled again before we |
| 1167 |
read it. In such case we would remove a block from AHI that was not |
| 1168 |
indexed in AHI (the block.index would be nullptr already). */ |
| 1169 |
const auto latched_index = block->ahi.index.load(); |
| 1170 |
|
| 1171 |
if (latched_index == nullptr) { |
| 1172 |
/* Index is already set to null and we have the X-latch on AHI part, the |
| 1173 |
block's index can't change to non-null. Nothing to do here. */ |
| 1174 |
return; |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (!is_ahi_enabled) { |
| 1178 |
/* So, the AHI is being disabled or was already disabled. */ |
| 1179 |
if (force) { |
| 1180 |
/* We are during a call to buf_LRU_free_page(), so the block is held |
| 1181 |
in private and it is in BUF_BLOCK_REMOVE_HASH state. We have to clear |
| 1182 |
the index and update reference counts instead of |
| 1183 |
buf_pool_clear_hash_index(). The buf_pool_clear_hash_index() is not |
| 1184 |
clearing the index because it could lead to situation where during the |
| 1185 |
call from buf_LRU_free_page() to this method, the index would be |
| 1186 |
already not referenced by AHI and its structure memory freed. Please |
| 1187 |
refer to "Is it safe to dereference the index here?" comment in this |
| 1188 |
method for reference. */ |
| 1189 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 1190 |
block->ahi.n_pointers.store(0); |
| 1191 |
#endif /* UNIV_AHI_DEBUG || UNIV_DEBUG */ |
| 1192 |
/* It is important to have the index reset to nullptr after the |
| 1193 |
n_pointers is set to 0, so it synchronizes correctly with check in |
| 1194 |
buf_block_t::ahi_t::validate(). */ |
| 1195 |
btr_search_set_block_not_cached(block); |
| 1196 |
} |
| 1197 |
/* The block is a regular page and buf_pool_clear_hash_index() will |
| 1198 |
clear the index and update reference count. Any attempt to do that in |
| 1199 |
this thread may result in assertion failure in |
| 1200 |
btr_search_set_block_not_cached() as it expects it is not called in |
| 1201 |
parallel. */ |
| 1202 |
return; |
| 1203 |
} |
| 1204 |
|
| 1205 |
ut_a(latched_index == index); |
| 1206 |
} |
| 1207 |
|
| 1208 |
block->ahi.validate(); |
| 1209 |
if (block->ahi.prefix_info.load() == prefix_info) { |
| 1210 |
const auto hash_table = btr_get_search_table(index); |
| 1211 |
for (size_t i = 0; i < n_cached; i++) { |
| 1212 |
ha_remove_a_node_to_page(hash_table, hashes[i], page); |
| 1213 |
} |
| 1214 |
|
| 1215 |
btr_search_set_block_not_cached(block); |
| 1216 |
MONITOR_ATOMIC_INC_VALUE(MONITOR_ADAPTIVE_HASH_ROW_REMOVED, n_cached); |
| 1217 |
|
| 1218 |
return; |
| 1219 |
} |
| 1220 |
/* Someone else has meanwhile built a new hash index on the page, with |
| 1221 |
different parameters. We need to retry the process of removal. */ |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
void btr_search_set_block_not_cached(buf_block_t *block) { |
| 1226 |
block->ahi.assert_empty(); |
| 1227 |
/* It is important to have the index reset to nullptr after the |
| 1228 |
n_pointers is set to 0, so it synchronizes correctly with check in |
| 1229 |
buf_block_t::ahi_t::validate(). */ |
| 1230 |
const auto old_index = block->ahi.index.exchange(nullptr); |
| 1231 |
/* This only assures we have checked the index is not null and there is no |
| 1232 |
other concurrent thread that had just set it to nullptr. This must be |
| 1233 |
assured by callers. If not that, the above line could be just .store(nullptr); |
| 1234 |
*/ |
| 1235 |
ut_a(old_index != nullptr); |
| 1236 |
|
| 1237 |
/* This check validates assumptions described in latching protocol of the |
| 1238 |
ahi_t::index field. */ |
| 1239 |
ut_ad((buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE && |
| 1240 |
mutex_own(&btr_search_enabled_mutex) && !btr_search_enabled) || |
| 1241 |
(buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH && |
| 1242 |
!btr_search_enabled) || |
| 1243 |
(rw_lock_own(btr_get_search_latch(old_index), RW_LOCK_X) && |
| 1244 |
btr_search_enabled)); |
| 1245 |
|
| 1246 |
/* This must be the last operation we do on the index or table |
| 1247 |
structure. Once it is 0 it can get freed by any other thread. This |
| 1248 |
operation must be at least memory order release to let any other writes |
| 1249 |
be completed before any other thread start to free the index or table |
| 1250 |
structure. */ |
| 1251 |
auto old_ref_count = old_index->search_info->ref_count.fetch_sub(1); |
| 1252 |
ut_a(old_ref_count > 0); |
| 1253 |
block->ahi.assert_empty(); |
| 1254 |
|
| 1255 |
MONITOR_ATOMIC_INC(MONITOR_ADAPTIVE_HASH_PAGE_REMOVED); |
| 1256 |
} |
| 1257 |
|
| 1258 |
void btr_search_drop_page_hash_when_freed(const page_id_t &page_id, |
| 1259 |
const page_size_t &page_size) { |
| 1260 |
buf_block_t *block; |
| 1261 |
mtr_t mtr; |
| 1262 |
|
| 1263 |
ut_d(export_vars.innodb_ahi_drop_lookups++); |
| 1264 |
|
| 1265 |
mtr_start(&mtr); |
| 1266 |
|
| 1267 |
/* If the caller has a latch on the page, then the caller must |
| 1268 |
have a x-latch on the page and it must have already dropped |
| 1269 |
the hash index for the page. Because of the x-latch that we |
| 1270 |
are possibly holding, we cannot s-latch the page, but must |
| 1271 |
(recursively) x-latch it, even though we are only reading. */ |
| 1272 |
|
| 1273 |
block = buf_page_get_gen(page_id, page_size, RW_X_LATCH, nullptr, |
| 1274 |
Page_fetch::PEEK_IF_IN_POOL, UT_LOCATION_HERE, &mtr); |
| 1275 |
|
| 1276 |
if (block) { |
| 1277 |
/* If AHI is still valid, page can't be in free state. |
| 1278 |
AHI is dropped when page is freed. */ |
| 1279 |
ut_ad(!block->page.file_page_was_freed); |
| 1280 |
|
| 1281 |
buf_block_dbg_add_level(block, SYNC_TREE_NODE_FROM_HASH); |
| 1282 |
|
| 1283 |
dict_index_t *index = block->ahi.index; |
| 1284 |
if (index != nullptr) { |
| 1285 |
/* In all our callers, the table handle should |
| 1286 |
be open, or we should be in the process of |
| 1287 |
dropping the table (preventing eviction). */ |
| 1288 |
ut_ad(index->table->n_ref_count > 0 || dict_sys_mutex_own()); |
| 1289 |
btr_search_drop_page_hash_index(block); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
mtr_commit(&mtr); |
| 1294 |
} |
| 1295 |
|
| 1296 |
static void btr_drop_next_batch(const page_size_t &page_size, |
| 1297 |
const dict_index_t **first, |
| 1298 |
const dict_index_t **last) { |
| 1299 |
static constexpr unsigned batch_size = 1024; |
| 1300 |
std::vector<page_id_t> to_drop; |
| 1301 |
to_drop.reserve(batch_size); |
| 1302 |
|
| 1303 |
for (ulint i = 0; i < srv_buf_pool_instances; ++i) { |
| 1304 |
to_drop.clear(); |
| 1305 |
buf_pool_t *buf_pool = buf_pool_from_array(i); |
| 1306 |
mutex_enter(&buf_pool->LRU_list_mutex); |
| 1307 |
const buf_page_t *prev; |
| 1308 |
|
| 1309 |
for (const buf_page_t *bpage = UT_LIST_GET_LAST(buf_pool->LRU); |
| 1310 |
bpage != nullptr; bpage = prev) { |
| 1311 |
prev = UT_LIST_GET_PREV(LRU, bpage); |
| 1312 |
|
| 1313 |
ut_a(buf_page_in_file(bpage)); |
| 1314 |
if (buf_page_get_state(bpage) != BUF_BLOCK_FILE_PAGE || |
| 1315 |
bpage->buf_fix_count > 0) { |
| 1316 |
continue; |
| 1317 |
} |
| 1318 |
|
| 1319 |
const dict_index_t *block_index = |
| 1320 |
reinterpret_cast<const buf_block_t *>(bpage)->ahi.index; |
| 1321 |
|
| 1322 |
/* index == nullptr means the page is no longer in AHI, so no need to |
| 1323 |
attempt freeing it */ |
| 1324 |
if (block_index == nullptr) { |
| 1325 |
continue; |
| 1326 |
} |
| 1327 |
/* pages IO fixed for read have index == nullptr */ |
| 1328 |
ut_ad(!bpage->was_io_fix_read()); |
| 1329 |
|
| 1330 |
if (std::find(first, last, block_index) != last) { |
| 1331 |
to_drop.emplace_back(bpage->id); |
| 1332 |
if (to_drop.size() == batch_size) { |
| 1333 |
break; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
mutex_exit(&buf_pool->LRU_list_mutex); |
| 1339 |
|
| 1340 |
for (const page_id_t &page_id : to_drop) { |
| 1341 |
btr_search_drop_page_hash_when_freed(page_id, page_size); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
void btr_drop_ahi_for_table(dict_table_t *table) { |
| 1347 |
const ulint len = UT_LIST_GET_LEN(table->indexes); |
| 1348 |
|
| 1349 |
if (len == 0) { |
| 1350 |
return; |
| 1351 |
} |
| 1352 |
|
| 1353 |
const dict_index_t *indexes[MAX_INDEXES]; |
| 1354 |
const page_size_t page_size(dict_table_page_size(table)); |
| 1355 |
|
| 1356 |
for (;;) { |
| 1357 |
ulint ref_count = 0; |
| 1358 |
const dict_index_t **end = indexes; |
| 1359 |
|
| 1360 |
for (dict_index_t *index = table->first_index(); index != nullptr; |
| 1361 |
index = index->next()) { |
| 1362 |
if (ulint n_refs = index->search_info->ref_count) { |
| 1363 |
ut_ad(!index->disable_ahi); |
| 1364 |
ut_ad(index->is_committed()); |
| 1365 |
ref_count += n_refs; |
| 1366 |
ut_ad(indexes + len > end); |
| 1367 |
*end++ = index; |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
ut_ad((indexes == end) == (ref_count == 0)); |
| 1372 |
|
| 1373 |
if (ref_count == 0) { |
| 1374 |
return; |
| 1375 |
} |
| 1376 |
|
| 1377 |
btr_drop_next_batch(page_size, indexes, end); |
| 1378 |
|
| 1379 |
std::this_thread::yield(); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
void btr_drop_ahi_for_index(const dict_index_t *index) { |
| 1384 |
ut_ad(index->is_committed()); |
| 1385 |
const auto info = index->search_info; |
| 1386 |
if (index->disable_ahi || info->ref_count == 0) { |
| 1387 |
return; |
| 1388 |
} |
| 1389 |
|
| 1390 |
const dict_table_t *table = index->table; |
| 1391 |
const page_size_t page_size(dict_table_page_size(table)); |
| 1392 |
|
| 1393 |
while (true) { |
| 1394 |
if (info->ref_count == 0) { |
| 1395 |
return; |
| 1396 |
} |
| 1397 |
|
| 1398 |
btr_drop_next_batch(page_size, &index, &index + 1); |
| 1399 |
|
| 1400 |
std::this_thread::yield(); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
static void btr_search_build_page_hash_index(dict_index_t *index, |
| 1405 |
buf_block_t *block, bool update) { |
| 1406 |
if (index->disable_ahi || !btr_search_enabled) { |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
|
| 1410 |
ut_ad(index); |
| 1411 |
ut_ad(block->page.id.space() == index->space); |
| 1412 |
ut_a(!dict_index_is_ibuf(index)); |
| 1413 |
|
| 1414 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_X)); |
| 1415 |
ut_ad(!rw_lock_own(btr_get_search_latch(index), RW_LOCK_S)); |
| 1416 |
ut_ad(rw_lock_own_flagged(&(block->lock), RW_LOCK_FLAG_S | RW_LOCK_FLAG_X)); |
| 1417 |
/* If update is specified, then this thread must hold the block X-latched so |
| 1418 |
there are no other threads that could run any other |
| 1419 |
btr_search_build_page_hash_index in parallel and thus to assure the AHI |
| 1420 |
entries for the specified prefix_info are updated certainly. */ |
| 1421 |
ut_ad(!(update && !rw_lock_own(&(block->lock), RW_LOCK_X))); |
| 1422 |
|
| 1423 |
const auto page = buf_block_get_frame(block); |
| 1424 |
const auto prefix_info = block->ahi.recommended_prefix_info.load(); |
| 1425 |
const auto n_fields_for_offsets = btr_search_get_n_fields(prefix_info); |
| 1426 |
|
| 1427 |
/* We could end up here after the btr_search_update_block_hash_info() |
| 1428 |
returned true. This may have happened for a page that is already indexed |
| 1429 |
in AHI and also even in case with matching current prefix parameters. In such |
| 1430 |
case we will be trying to update all block's record entries in AHI. */ |
| 1431 |
if (block->ahi.index && block->ahi.prefix_info.load() != prefix_info) { |
| 1432 |
btr_search_drop_page_hash_index(block); |
| 1433 |
} |
| 1434 |
|
| 1435 |
/* Check that the values for hash index build are sensible */ |
| 1436 |
|
| 1437 |
if (prefix_info.n_fields == 0 && prefix_info.n_bytes == 0) { |
| 1438 |
return; |
| 1439 |
} |
| 1440 |
|
| 1441 |
ut_ad(dict_index_get_n_unique_in_tree(index) >= n_fields_for_offsets); |
| 1442 |
|
| 1443 |
const auto n_recs = page_get_n_recs(page); |
| 1444 |
|
| 1445 |
if (n_recs == 0) { |
| 1446 |
return; |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* Calculate and cache hash values and corresponding records into |
| 1450 |
an array for fast insertion to the hash index */ |
| 1451 |
|
| 1452 |
auto hashes = ut::make_unique<uint64_t[]>(UT_NEW_THIS_FILE_PSI_KEY, n_recs); |
| 1453 |
auto recs = ut::make_unique<rec_t *[]>(UT_NEW_THIS_FILE_PSI_KEY, n_recs); |
| 1454 |
|
| 1455 |
ut_a(index->id == btr_page_get_index_id(page)); |
| 1456 |
|
| 1457 |
auto rec = page_rec_get_next(page_get_infimum_rec(page)); |
| 1458 |
|
| 1459 |
Rec_offsets offsets; |
| 1460 |
ut_ad( |
| 1461 |
page_rec_is_supremum(rec) || |
| 1462 |
btr_search_get_n_fields(prefix_info) == |
| 1463 |
rec_offs_n_fields(offsets.compute(rec, index, n_fields_for_offsets))); |
| 1464 |
|
| 1465 |
const auto index_hash = btr_hash_seed_for_record(index); |
| 1466 |
|
| 1467 |
auto hash_value = |
| 1468 |
rec_hash(rec, offsets.compute(rec, index, n_fields_for_offsets), |
| 1469 |
prefix_info.n_fields, prefix_info.n_bytes, index_hash, index); |
| 1470 |
|
| 1471 |
size_t n_cached = 0; |
| 1472 |
if (prefix_info.left_side) { |
| 1473 |
hashes[n_cached] = hash_value; |
| 1474 |
recs[n_cached] = rec; |
| 1475 |
n_cached++; |
| 1476 |
} |
| 1477 |
|
| 1478 |
for (;;) { |
| 1479 |
const auto next_rec = page_rec_get_next(rec); |
| 1480 |
|
| 1481 |
if (page_rec_is_supremum(next_rec)) { |
| 1482 |
if (!prefix_info.left_side) { |
| 1483 |
hashes[n_cached] = hash_value; |
| 1484 |
recs[n_cached] = rec; |
| 1485 |
n_cached++; |
| 1486 |
} |
| 1487 |
|
| 1488 |
break; |
| 1489 |
} |
| 1490 |
|
| 1491 |
const auto next_hash_value = rec_hash( |
| 1492 |
next_rec, offsets.compute(next_rec, index, n_fields_for_offsets), |
| 1493 |
prefix_info.n_fields, prefix_info.n_bytes, index_hash, index); |
| 1494 |
|
| 1495 |
if (hash_value != next_hash_value) { |
| 1496 |
/* Insert an entry into the hash index */ |
| 1497 |
|
| 1498 |
if (prefix_info.left_side) { |
| 1499 |
hashes[n_cached] = next_hash_value; |
| 1500 |
recs[n_cached] = next_rec; |
| 1501 |
n_cached++; |
| 1502 |
} else { |
| 1503 |
hashes[n_cached] = hash_value; |
| 1504 |
recs[n_cached] = rec; |
| 1505 |
n_cached++; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
rec = next_rec; |
| 1510 |
hash_value = next_hash_value; |
| 1511 |
} |
| 1512 |
|
| 1513 |
btr_search_check_free_space_in_heap(index); |
| 1514 |
|
| 1515 |
/* The AHI is supposed to be heuristic for speed-up. When adding a block |
| 1516 |
to index, waiting here for the latch would defy the purpose. We will try |
| 1517 |
to add the block to index next time. However, for updates this must |
| 1518 |
succeed so the index doesn't contain wrong entries. */ |
| 1519 |
if (update) { |
| 1520 |
btr_search_x_lock(index, UT_LOCATION_HERE); |
| 1521 |
} else { |
| 1522 |
if (!btr_search_x_lock_nowait(index, UT_LOCATION_HERE)) { |
| 1523 |
return; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 1527 |
auto x_latch_guard = create_scope_guard([block, index]() { |
| 1528 |
block->ahi.validate(); |
| 1529 |
btr_search_x_unlock(index); |
| 1530 |
}); |
| 1531 |
#else |
| 1532 |
auto x_latch_guard = |
| 1533 |
create_scope_guard([index]() { btr_search_x_unlock(index); }); |
| 1534 |
#endif |
| 1535 |
|
| 1536 |
/* After we acquire AHI latch we re-check the AHI is enabled. If it was |
| 1537 |
disabled and re-enabled in meantime then it is not a problem, the |
| 1538 |
block->ahi.index will have to be nullptr then and we will just add it to AHI |
| 1539 |
now. */ |
| 1540 |
if (!btr_search_enabled) { |
| 1541 |
return; |
| 1542 |
} |
| 1543 |
|
| 1544 |
/* Before we re-acquired the AHI latch, someone else might have already change |
| 1545 |
them. In case the block is already indexed and the prefix parameters match, |
| 1546 |
we will just update all record's entries. */ |
| 1547 |
if (block->ahi.index && block->ahi.prefix_info.load() != prefix_info) { |
| 1548 |
/* This can't happen if we are holding X-latch on the block. And (thus) when |
| 1549 |
this method is called with update. */ |
| 1550 |
ut_ad(!rw_lock_own(&(block->lock), RW_LOCK_X)); |
| 1551 |
ut_a(!update); |
| 1552 |
return; |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* This counter is decremented every time we drop page |
| 1556 |
hash index entries and is incremented here. Since we can |
| 1557 |
rebuild hash index for a page that is already hashed, we |
| 1558 |
have to take care not to increment the counter in that |
| 1559 |
case. */ |
| 1560 |
if (block->ahi.index == nullptr) { |
| 1561 |
block->ahi.assert_empty(); |
| 1562 |
index->search_info->ref_count++; |
| 1563 |
} |
| 1564 |
|
| 1565 |
block->n_hash_helps = 0; |
| 1566 |
|
| 1567 |
block->ahi.prefix_info = prefix_info; |
| 1568 |
block->ahi.index = index; |
| 1569 |
|
| 1570 |
const auto table = btr_get_search_table(index); |
| 1571 |
for (size_t i = 0; i < n_cached; i++) { |
| 1572 |
ha_insert_for_hash(table, hashes[i], block, recs[i]); |
| 1573 |
} |
| 1574 |
|
| 1575 |
x_latch_guard.reset(); |
| 1576 |
|
| 1577 |
MONITOR_ATOMIC_INC(MONITOR_ADAPTIVE_HASH_PAGE_ADDED); |
| 1578 |
} |
| 1579 |
|
| 1580 |
void btr_search_update_hash_on_move(buf_block_t *new_block, buf_block_t *block, |
| 1581 |
dict_index_t *index) { |
| 1582 |
/* AHI is disabled for intrinsic table as it depends on index-id |
| 1583 |
which is dynamically assigned for intrinsic table indexes and not |
| 1584 |
through a centralized index generator. */ |
| 1585 |
if (index->disable_ahi || !btr_search_enabled) { |
| 1586 |
return; |
| 1587 |
} |
| 1588 |
|
| 1589 |
ut_ad(!index->table->is_intrinsic()); |
| 1590 |
|
| 1591 |
ut_ad(rw_lock_own(&(block->lock), RW_LOCK_X)); |
| 1592 |
ut_ad(rw_lock_own(&(new_block->lock), RW_LOCK_X)); |
| 1593 |
|
| 1594 |
const auto new_block_index = new_block->ahi.index.load(); |
| 1595 |
const auto old_block_index = block->ahi.index.load(); |
| 1596 |
ut_a(!new_block_index || new_block_index == index); |
| 1597 |
ut_a(!old_block_index || old_block_index == index); |
| 1598 |
ut_a(!(new_block_index || old_block_index) || !dict_index_is_ibuf(index)); |
| 1599 |
|
| 1600 |
/* This method assures that all moved entries from the old block have their |
| 1601 |
AHI entries deleted or updated to point to the new_block. */ |
| 1602 |
|
| 1603 |
/* Are there any outdated entries hashed in the old block? Or maybe none were |
| 1604 |
moved? */ |
| 1605 |
if (old_block_index == nullptr || |
| 1606 |
page_get_n_recs(buf_block_get_frame(new_block)) == 0) { |
| 1607 |
/* New block may have some records cached, but it's not a problem to not |
| 1608 |
have all entries hashed in AHI. */ |
| 1609 |
return; |
| 1610 |
} |
| 1611 |
const auto recommended_settings = |
| 1612 |
old_block_index->search_info->prefix_info.load(); |
| 1613 |
const auto old_settings = block->ahi.prefix_info.load(); |
| 1614 |
/* Will caching the new_block overwrite outdated entries, that is are the old |
| 1615 |
and new block settings matching? And are the old block settings valuable |
| 1616 |
enough to keep in cache? */ |
| 1617 |
if ((!new_block_index || new_block->ahi.prefix_info.load() == old_settings) && |
| 1618 |
(recommended_settings == old_settings)) { |
| 1619 |
/* We need to set recommended prefix so it is used by the |
| 1620 |
btr_search_build_page_hash_index method. Since we are holding X-latch on |
| 1621 |
block->lock, no other thread can modify the recommendation. */ |
| 1622 |
new_block->ahi.recommended_prefix_info = old_settings; |
| 1623 |
btr_search_build_page_hash_index(index, new_block, true); |
| 1624 |
} else { |
| 1625 |
/* We have to get rid of old entries and don't want to use the new block's |
| 1626 |
entries - we forcefully drop all entries on the old block. */ |
| 1627 |
btr_search_drop_page_hash_index(block); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
void btr_search_update_hash_on_delete(btr_cur_t *cursor) { |
| 1632 |
if (cursor->index->disable_ahi || !btr_search_enabled) { |
| 1633 |
return; |
| 1634 |
} |
| 1635 |
|
| 1636 |
const auto block = btr_cur_get_block(cursor); |
| 1637 |
|
| 1638 |
ut_ad(rw_lock_own(&(block->lock), RW_LOCK_X)); |
| 1639 |
|
| 1640 |
block->ahi.validate(); |
| 1641 |
const auto index = block->ahi.index.load(); |
| 1642 |
|
| 1643 |
if (!index) { |
| 1644 |
return; |
| 1645 |
} |
| 1646 |
|
| 1647 |
ut_ad(block->page.id.space() == index->space); |
| 1648 |
ut_a(index == cursor->index); |
| 1649 |
ut_a(!dict_index_is_ibuf(index)); |
| 1650 |
|
| 1651 |
const auto rec = btr_cur_get_rec(cursor); |
| 1652 |
|
| 1653 |
/* Since we hold the X-latch on block's lock, the AHI prefix parameters |
| 1654 |
can't be changed (such change require at least S-latch on block's lock) |
| 1655 |
even if the AHI latches are not held. */ |
| 1656 |
const auto prefix_info = block->ahi.prefix_info.load(); |
| 1657 |
ut_a(btr_search_get_n_fields(prefix_info) > 0); |
| 1658 |
|
| 1659 |
const auto hash_value = |
| 1660 |
rec_hash(rec, Rec_offsets{}.compute(rec, index), prefix_info.n_fields, |
| 1661 |
prefix_info.n_bytes, btr_hash_seed_for_record(index), index); |
| 1662 |
|
| 1663 |
btr_search_x_lock(index, UT_LOCATION_HERE); |
| 1664 |
const auto table = btr_get_search_table(index); |
| 1665 |
block->ahi.validate(); |
| 1666 |
|
| 1667 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 1668 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 1669 |
nullptr then). */ |
| 1670 |
if (btr_search_enabled && block->ahi.index != nullptr) { |
| 1671 |
ut_a(block->ahi.index == index); |
| 1672 |
ut_a(block->ahi.prefix_info.load() == prefix_info); |
| 1673 |
|
| 1674 |
if (ha_search_and_delete_if_found(table, hash_value, rec)) { |
| 1675 |
MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_REMOVED); |
| 1676 |
} else { |
| 1677 |
MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_REMOVE_NOT_FOUND); |
| 1678 |
} |
| 1679 |
|
| 1680 |
block->ahi.validate(); |
| 1681 |
} |
| 1682 |
|
| 1683 |
btr_search_x_unlock(index); |
| 1684 |
} |
| 1685 |
|
| 1686 |
void btr_search_update_hash_node_on_insert(btr_cur_t *cursor) { |
| 1687 |
if (cursor->index->disable_ahi || !btr_search_enabled) { |
| 1688 |
return; |
| 1689 |
} |
| 1690 |
|
| 1691 |
const auto rec = btr_cur_get_rec(cursor); |
| 1692 |
|
| 1693 |
const auto block = btr_cur_get_block(cursor); |
| 1694 |
|
| 1695 |
ut_ad(rw_lock_own(&(block->lock), RW_LOCK_X)); |
| 1696 |
|
| 1697 |
const auto index = block->ahi.index.load(); |
| 1698 |
|
| 1699 |
if (!index) { |
| 1700 |
return; |
| 1701 |
} |
| 1702 |
|
| 1703 |
ut_a(cursor->index == index); |
| 1704 |
ut_a(!dict_index_is_ibuf(index)); |
| 1705 |
|
| 1706 |
const auto prefix_info = block->ahi.prefix_info.load(); |
| 1707 |
|
| 1708 |
if (cursor->flag == BTR_CUR_HASH && !prefix_info.left_side && |
| 1709 |
cursor->ahi.prefix_info.equals_without_left_side(prefix_info)) { |
| 1710 |
if (!btr_search_x_lock_nowait(index, UT_LOCATION_HERE)) { |
| 1711 |
return; |
| 1712 |
} |
| 1713 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 1714 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 1715 |
nullptr then). */ |
| 1716 |
if (btr_search_enabled && block->ahi.index != nullptr) { |
| 1717 |
ut_ad(block->ahi.index == index); |
| 1718 |
const auto table = btr_get_search_table(index); |
| 1719 |
|
| 1720 |
/* Since we hold the X-latch on block's lock, the AHI prefix parameters |
| 1721 |
can't be changed (such change require at least S-latch on block's lock) |
| 1722 |
even if the AHI latches are not held in meantime. */ |
| 1723 |
if (ha_search_and_update_if_found(table, cursor->ahi.ahi_hash_value, rec, |
| 1724 |
block, page_rec_get_next(rec))) { |
| 1725 |
MONITOR_INC(MONITOR_ADAPTIVE_HASH_ROW_UPDATED); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
block->ahi.validate(); |
| 1730 |
btr_search_x_unlock(index); |
| 1731 |
} else { |
| 1732 |
btr_search_update_hash_on_insert(cursor); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
void btr_search_update_hash_on_insert(btr_cur_t *cursor) { |
| 1737 |
const rec_t *ins_rec; |
| 1738 |
const rec_t *next_rec; |
| 1739 |
uint64_t hash_value; |
| 1740 |
uint64_t next_hash = 0; |
| 1741 |
bool locked = false; |
| 1742 |
|
| 1743 |
if (cursor->index->disable_ahi || !btr_search_enabled) { |
| 1744 |
return; |
| 1745 |
} |
| 1746 |
|
| 1747 |
const auto block = btr_cur_get_block(cursor); |
| 1748 |
|
| 1749 |
ut_ad(rw_lock_own(&(block->lock), RW_LOCK_X)); |
| 1750 |
block->ahi.validate(); |
| 1751 |
|
| 1752 |
const auto index = block->ahi.index.load(); |
| 1753 |
|
| 1754 |
if (!index) { |
| 1755 |
return; |
| 1756 |
} |
| 1757 |
|
| 1758 |
const auto x_latch_guard = create_scope_guard([&locked, index]() { |
| 1759 |
if (locked) { |
| 1760 |
btr_search_x_unlock(index); |
| 1761 |
} |
| 1762 |
}); |
| 1763 |
btr_search_check_free_space_in_heap(index); |
| 1764 |
|
| 1765 |
ut_ad(block->page.id.space() == index->space); |
| 1766 |
|
| 1767 |
hash_table_t *table = nullptr; |
| 1768 |
|
| 1769 |
const auto rec = btr_cur_get_rec(cursor); |
| 1770 |
|
| 1771 |
ut_a(!index->disable_ahi); |
| 1772 |
ut_a(index == cursor->index); |
| 1773 |
ut_a(!dict_index_is_ibuf(index)); |
| 1774 |
|
| 1775 |
/* Since we hold the X-latch on block's lock, the AHI prefix parameters |
| 1776 |
can't be changed (such change require at least S-latch on block's lock) |
| 1777 |
even if the AHI latches are not held. */ |
| 1778 |
const auto prefix_info = block->ahi.prefix_info.load(); |
| 1779 |
|
| 1780 |
ins_rec = page_rec_get_next_const(rec); |
| 1781 |
next_rec = page_rec_get_next_const(ins_rec); |
| 1782 |
|
| 1783 |
const auto index_hash = btr_hash_seed_for_record(index); |
| 1784 |
const ulint n_offs = btr_search_get_n_fields(prefix_info); |
| 1785 |
|
| 1786 |
Rec_offsets offsets; |
| 1787 |
const auto ins_hash = |
| 1788 |
rec_hash(ins_rec, offsets.compute(ins_rec, index, n_offs), |
| 1789 |
prefix_info.n_fields, prefix_info.n_bytes, index_hash, index); |
| 1790 |
|
| 1791 |
if (!page_rec_is_supremum(next_rec)) { |
| 1792 |
next_hash = |
| 1793 |
rec_hash(next_rec, offsets.compute(next_rec, index, n_offs), |
| 1794 |
prefix_info.n_fields, prefix_info.n_bytes, index_hash, index); |
| 1795 |
} |
| 1796 |
|
| 1797 |
if (!page_rec_is_infimum(rec)) { |
| 1798 |
hash_value = |
| 1799 |
rec_hash(rec, offsets.compute(rec, index, n_offs), prefix_info.n_fields, |
| 1800 |
prefix_info.n_bytes, index_hash, index); |
| 1801 |
} else { |
| 1802 |
if (prefix_info.left_side) { |
| 1803 |
locked = btr_search_x_lock_nowait(index, UT_LOCATION_HERE); |
| 1804 |
|
| 1805 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 1806 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 1807 |
nullptr then). */ |
| 1808 |
if (!locked || !btr_search_enabled || block->ahi.index == nullptr) { |
| 1809 |
return; |
| 1810 |
} |
| 1811 |
table = btr_get_search_table(index); |
| 1812 |
|
| 1813 |
ha_insert_for_hash(table, ins_hash, block, ins_rec); |
| 1814 |
} |
| 1815 |
|
| 1816 |
goto check_next_rec; |
| 1817 |
} |
| 1818 |
|
| 1819 |
if (hash_value != ins_hash) { |
| 1820 |
if (!locked) { |
| 1821 |
locked = btr_search_x_lock_nowait(index, UT_LOCATION_HERE); |
| 1822 |
|
| 1823 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 1824 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 1825 |
nullptr then). */ |
| 1826 |
if (!locked || !btr_search_enabled || block->ahi.index == nullptr) { |
| 1827 |
return; |
| 1828 |
} |
| 1829 |
table = btr_get_search_table(index); |
| 1830 |
} |
| 1831 |
|
| 1832 |
if (!prefix_info.left_side) { |
| 1833 |
ha_insert_for_hash(table, hash_value, block, rec); |
| 1834 |
} else { |
| 1835 |
ha_insert_for_hash(table, ins_hash, block, ins_rec); |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
check_next_rec: |
| 1840 |
if (page_rec_is_supremum(next_rec)) { |
| 1841 |
if (!prefix_info.left_side) { |
| 1842 |
if (!locked) { |
| 1843 |
locked = btr_search_x_lock_nowait(index, UT_LOCATION_HERE); |
| 1844 |
|
| 1845 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was |
| 1846 |
not disabled and re-enabled in meantime (the block's index would be |
| 1847 |
reset to nullptr then). */ |
| 1848 |
if (!locked || !btr_search_enabled || block->ahi.index == nullptr) { |
| 1849 |
return; |
| 1850 |
} |
| 1851 |
table = btr_get_search_table(index); |
| 1852 |
} |
| 1853 |
|
| 1854 |
ha_insert_for_hash(table, ins_hash, block, ins_rec); |
| 1855 |
} |
| 1856 |
|
| 1857 |
return; |
| 1858 |
} |
| 1859 |
|
| 1860 |
if (ins_hash != next_hash) { |
| 1861 |
if (!locked) { |
| 1862 |
locked = btr_search_x_lock_nowait(index, UT_LOCATION_HERE); |
| 1863 |
|
| 1864 |
/* After we acquire AHI latch we re-check the AHI is enabled, and was not |
| 1865 |
disabled and re-enabled in meantime (the block's index would be reset to |
| 1866 |
nullptr then). */ |
| 1867 |
if (!locked || !btr_search_enabled || block->ahi.index == nullptr) { |
| 1868 |
return; |
| 1869 |
} |
| 1870 |
table = btr_get_search_table(index); |
| 1871 |
} |
| 1872 |
|
| 1873 |
if (!prefix_info.left_side) { |
| 1874 |
ha_insert_for_hash(table, ins_hash, block, ins_rec); |
| 1875 |
} else { |
| 1876 |
ha_insert_for_hash(table, next_hash, block, next_rec); |
| 1877 |
} |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
#if defined UNIV_AHI_DEBUG || defined UNIV_DEBUG |
| 1882 |
|
| 1883 |
/** Validates the search system for given hash table. |
| 1884 |
@param[in] part_id Part ID of AHI, for which hash table is to be |
| 1885 |
validated. |
| 1886 |
@return true if OK */ |
| 1887 |
static bool btr_search_hash_table_validate(ulint part_id) { |
| 1888 |
ha_node_t *node; |
| 1889 |
bool ok = true; |
| 1890 |
ulint i; |
| 1891 |
ulint cell_count; |
| 1892 |
Rec_offsets offsets; |
| 1893 |
|
| 1894 |
mutex_enter(&btr_search_enabled_mutex); |
| 1895 |
if (!btr_search_enabled) { |
| 1896 |
mutex_exit(&btr_search_enabled_mutex); |
| 1897 |
return true; |
| 1898 |
} |
| 1899 |
|
| 1900 |
const auto &part = btr_search_sys->parts[part_id]; |
| 1901 |
|
| 1902 |
ut_ad(part.hash_table->heap->free_block_ptr == &part.free_block_for_heap); |
| 1903 |
|
| 1904 |
/* How many cells to check before temporarily releasing |
| 1905 |
search latches. */ |
| 1906 |
constexpr auto chunk_size = 10000; |
| 1907 |
|
| 1908 |
btr_search_x_lock_all(UT_LOCATION_HERE); |
| 1909 |
|
| 1910 |
cell_count = hash_get_n_cells(part.hash_table); |
| 1911 |
|
| 1912 |
for (i = 0; i < cell_count; i++) { |
| 1913 |
/* We release search latches every once in a while to |
| 1914 |
give other queries a chance to run. */ |
| 1915 |
if ((i != 0) && ((i % chunk_size) == 0)) { |
| 1916 |
btr_search_x_unlock_all(); |
| 1917 |
std::this_thread::yield(); |
| 1918 |
btr_search_x_lock_all(UT_LOCATION_HERE); |
| 1919 |
|
| 1920 |
ulint curr_cell_count = hash_get_n_cells(part.hash_table); |
| 1921 |
|
| 1922 |
if (cell_count != curr_cell_count) { |
| 1923 |
cell_count = curr_cell_count; |
| 1924 |
|
| 1925 |
if (i >= cell_count) { |
| 1926 |
break; |
| 1927 |
} |
| 1928 |
} |
| 1929 |
} |
| 1930 |
|
| 1931 |
node = (ha_node_t *)hash_get_nth_cell(part.hash_table, i)->node; |
| 1932 |
|
| 1933 |
for (; node != nullptr; node = node->next) { |
| 1934 |
buf_block_t *const block = buf_block_from_ahi((byte *)node->data); |
| 1935 |
const buf_block_t *hash_block; |
| 1936 |
|
| 1937 |
const auto buf_pool = buf_pool_from_bpage((buf_page_t *)block); |
| 1938 |
/* Prevent BUF_BLOCK_FILE_PAGE -> BUF_BLOCK_REMOVE_HASH |
| 1939 |
transition until we lock the block mutex */ |
| 1940 |
mutex_enter(&buf_pool->LRU_list_mutex); |
| 1941 |
|
| 1942 |
if (UNIV_LIKELY(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE)) { |
| 1943 |
/* The space and offset are only valid |
| 1944 |
for file blocks. It is possible that |
| 1945 |
the block is being freed |
| 1946 |
(BUF_BLOCK_REMOVE_HASH, see the |
| 1947 |
assertion and the comment below) */ |
| 1948 |
hash_block = buf_block_hash_get(buf_pool, block->page.id); |
| 1949 |
} else { |
| 1950 |
hash_block = nullptr; |
| 1951 |
} |
| 1952 |
|
| 1953 |
if (hash_block) { |
| 1954 |
ut_a(hash_block == block); |
| 1955 |
} else { |
| 1956 |
/* When a block is being freed, |
| 1957 |
buf_LRU_free_page() first |
| 1958 |
removes the block from |
| 1959 |
buf_pool->page_hash by calling |
| 1960 |
buf_LRU_block_remove_hashed_page(). |
| 1961 |
After that, it invokes |
| 1962 |
buf_LRU_block_remove_hashed() to |
| 1963 |
remove the block from |
| 1964 |
btr_search_sys->hash_tables[i]. */ |
| 1965 |
|
| 1966 |
ut_a(buf_block_get_state(block) == BUF_BLOCK_REMOVE_HASH); |
| 1967 |
} |
| 1968 |
|
| 1969 |
mutex_enter(&block->mutex); |
| 1970 |
mutex_exit(&buf_pool->LRU_list_mutex); |
| 1971 |
|
| 1972 |
const auto index = block->ahi.index.load(); |
| 1973 |
const auto prefix_info = block->ahi.prefix_info.load(); |
| 1974 |
|
| 1975 |
ut_a(!dict_index_is_ibuf(index)); |
| 1976 |
ut_ad(block->page.id.space() == index->space); |
| 1977 |
|
| 1978 |
const auto offsets_array = offsets.compute( |
| 1979 |
node->data, index, btr_search_get_n_fields(prefix_info)); |
| 1980 |
const auto hash_value = |
| 1981 |
rec_hash(node->data, offsets_array, prefix_info.n_fields, |
| 1982 |
prefix_info.n_bytes, btr_hash_seed_for_record(index), index); |
| 1983 |
|
| 1984 |
if (node->hash_value != hash_value) { |
| 1985 |
const page_t *page = block->frame; |
| 1986 |
|
| 1987 |
ok = false; |
| 1988 |
|
| 1989 |
ib::error(ER_IB_MSG_46) |
| 1990 |
<< "Error in an adaptive hash" |
| 1991 |
<< " index pointer to page " |
| 1992 |
<< page_id_t(page_get_space_id(page), page_get_page_no(page)) |
| 1993 |
<< ", ptr mem address " |
| 1994 |
<< reinterpret_cast<const void *>(node->data) << ", index id " |
| 1995 |
<< index_id_t{index->space, index->id} << ", node hash " |
| 1996 |
<< node->hash_value << ", rec hash " << hash_value; |
| 1997 |
|
| 1998 |
fputs("InnoDB: Record ", stderr); |
| 1999 |
rec_print_new(stderr, node->data, offsets_array); |
| 2000 |
fprintf(stderr, |
| 2001 |
"\nInnoDB: on that page." |
| 2002 |
" Page mem address %p, is hashed %p," |
| 2003 |
" n fields %lu\n" |
| 2004 |
"InnoDB: side %lu\n", |
| 2005 |
(void *)page, (void *)block->ahi.index.load(), |
| 2006 |
(ulong)prefix_info.n_fields, (ulong)prefix_info.left_side); |
| 2007 |
ut_d(ut_error); |
| 2008 |
} |
| 2009 |
|
| 2010 |
mutex_exit(&block->mutex); |
| 2011 |
} |
| 2012 |
} |
| 2013 |
|
| 2014 |
for (i = 0; i < cell_count; i += chunk_size) { |
| 2015 |
/* We release search latches every once in a while to |
| 2016 |
give other queries a chance to run. */ |
| 2017 |
if (i != 0) { |
| 2018 |
btr_search_x_unlock_all(); |
| 2019 |
std::this_thread::yield(); |
| 2020 |
btr_search_x_lock_all(UT_LOCATION_HERE); |
| 2021 |
|
| 2022 |
ulint curr_cell_count = hash_get_n_cells(part.hash_table); |
| 2023 |
|
| 2024 |
if (cell_count != curr_cell_count) { |
| 2025 |
cell_count = curr_cell_count; |
| 2026 |
|
| 2027 |
if (i >= cell_count) { |
| 2028 |
break; |
| 2029 |
} |
| 2030 |
} |
| 2031 |
} |
| 2032 |
|
| 2033 |
ulint end_index = std::min(i + chunk_size - 1, cell_count - 1); |
| 2034 |
|
| 2035 |
if (!ha_validate(part.hash_table, i, end_index)) { |
| 2036 |
ok = false; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
btr_search_x_unlock_all(); |
| 2041 |
|
| 2042 |
mutex_exit(&btr_search_enabled_mutex); |
| 2043 |
|
| 2044 |
return ok; |
| 2045 |
} |
| 2046 |
|
| 2047 |
bool btr_search_validate() { |
| 2048 |
for (ulint i = 0; i < btr_ahi_parts; ++i) { |
| 2049 |
if (!btr_search_hash_table_validate(i)) { |
| 2050 |
return false; |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
return true; |
| 2055 |
} |
| 2056 |
|
| 2057 |
#endif /* defined UNIV_AHI_DEBUG || defined UNIV_DEBUG */ |