| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 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 fts/fts0opt.cc |
| 29 |
Full Text Search optimize thread |
| 30 |
|
| 31 |
Created 2007/03/27 Sunny Bains |
| 32 |
Completed 2011/7/10 Sunny and Jimmy Yang |
| 33 |
|
| 34 |
***********************************************************************/ |
| 35 |
|
| 36 |
#include <math.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <sys/types.h> |
| 39 |
#include <time.h> |
| 40 |
#include <zlib.h> |
| 41 |
|
| 42 |
#include "current_thd.h" |
| 43 |
#include "dict0dd.h" |
| 44 |
#include "fts0fts.h" |
| 45 |
#include "fts0opt.h" |
| 46 |
#include "fts0priv.h" |
| 47 |
#include "fts0types.h" |
| 48 |
#include "ha_prototypes.h" |
| 49 |
#include "os0thread-create.h" |
| 50 |
#include "que0types.h" |
| 51 |
#include "row0sel.h" |
| 52 |
#include "sql_thd_internal_api.h" |
| 53 |
#include "srv0start.h" |
| 54 |
#include "ut0list.h" |
| 55 |
#include "ut0wqueue.h" |
| 56 |
|
| 57 |
/** The FTS optimize thread's work queue. */ |
| 58 |
static ib_wqueue_t *fts_optimize_wq; |
| 59 |
|
| 60 |
/** Time to wait for a message. */ |
| 61 |
constexpr std::chrono::seconds FTS_QUEUE_WAIT{5}; |
| 62 |
|
| 63 |
/** Default optimize interval. */ |
| 64 |
constexpr std::chrono::minutes FTS_OPTIMIZE_INTERVAL{5}; |
| 65 |
|
| 66 |
/** Server is shutting down, so does we exiting the optimize thread */ |
| 67 |
static bool fts_opt_start_shutdown = false; |
| 68 |
|
| 69 |
/** Initial size of nodes in fts_word_t. */ |
| 70 |
static const ulint FTS_WORD_NODES_INIT_SIZE = 64; |
| 71 |
|
| 72 |
/** Last time we did check whether system need a sync */ |
| 73 |
static std::chrono::steady_clock::time_point last_check_sync_time; |
| 74 |
|
| 75 |
#if 0 |
| 76 |
/** Check each table in round robin to see whether they'd |
| 77 |
need to be "optimized" */ |
| 78 |
static ulint fts_optimize_sync_iterator = 0; |
| 79 |
#endif |
| 80 |
|
| 81 |
/** State of a table within the optimization sub system. */ |
| 82 |
enum fts_state_t { |
| 83 |
FTS_STATE_LOADED, |
| 84 |
FTS_STATE_RUNNING, |
| 85 |
FTS_STATE_SUSPENDED, |
| 86 |
FTS_STATE_DONE, |
| 87 |
FTS_STATE_EMPTY |
| 88 |
}; |
| 89 |
|
| 90 |
/** FTS optimize thread message types. */ |
| 91 |
enum fts_msg_type_t { |
| 92 |
FTS_MSG_START, /*!< Start optimizing thread */ |
| 93 |
|
| 94 |
FTS_MSG_PAUSE, /*!< Pause optimizing thread */ |
| 95 |
|
| 96 |
FTS_MSG_STOP, /*!< Stop optimizing and exit thread */ |
| 97 |
|
| 98 |
FTS_MSG_ADD_TABLE, /*!< Add table to the optimize thread's |
| 99 |
work queue */ |
| 100 |
|
| 101 |
FTS_MSG_OPTIMIZE_TABLE, /*!< Optimize a table */ |
| 102 |
|
| 103 |
FTS_MSG_DEL_TABLE, /*!< Remove a table from the optimize |
| 104 |
threads work queue */ |
| 105 |
FTS_MSG_SYNC_TABLE /*!< Sync fts cache of a table */ |
| 106 |
}; |
| 107 |
|
| 108 |
/** Compressed list of words that have been read from FTS INDEX |
| 109 |
that needs to be optimized. */ |
| 110 |
struct fts_zip_t { |
| 111 |
lint status; /*!< Status of (un)/zip operation */ |
| 112 |
|
| 113 |
ulint n_words; /*!< Number of words compressed */ |
| 114 |
|
| 115 |
ulint block_sz; /*!< Size of a block in bytes */ |
| 116 |
|
| 117 |
ib_vector_t *blocks; /*!< Vector of compressed blocks */ |
| 118 |
|
| 119 |
ib_alloc_t *heap_alloc; /*!< Heap to use for allocations */ |
| 120 |
|
| 121 |
ulint pos; /*!< Offset into blocks */ |
| 122 |
|
| 123 |
ulint last_big_block; /*!< Offset of last block in the |
| 124 |
blocks array that is of size |
| 125 |
block_sz. Blocks beyond this offset |
| 126 |
are of size FTS_MAX_WORD_LEN */ |
| 127 |
|
| 128 |
z_streamp zp; /*!< ZLib state */ |
| 129 |
|
| 130 |
/*!< The value of the last word read |
| 131 |
from the FTS INDEX table. This is |
| 132 |
used to discard duplicates */ |
| 133 |
|
| 134 |
fts_string_t word; /*!< UTF-8 string */ |
| 135 |
|
| 136 |
ulint max_words; /*!< maximum number of words to read |
| 137 |
in one pass */ |
| 138 |
}; |
| 139 |
|
| 140 |
/** Prepared statemets used during optimize */ |
| 141 |
struct fts_optimize_graph_t { |
| 142 |
/*!< Delete a word from FTS INDEX */ |
| 143 |
que_t *delete_nodes_graph; |
| 144 |
/*!< Insert a word into FTS INDEX */ |
| 145 |
que_t *write_nodes_graph; |
| 146 |
/*!< COMMIT a transaction */ |
| 147 |
que_t *commit_graph; |
| 148 |
/*!< Read the nodes from FTS_INDEX */ |
| 149 |
que_t *read_nodes_graph; |
| 150 |
}; |
| 151 |
|
| 152 |
/** Used by fts_optimize() to store state. */ |
| 153 |
struct fts_optimize_t { |
| 154 |
trx_t *trx; /*!< The transaction used for all SQL */ |
| 155 |
|
| 156 |
ib_alloc_t *self_heap; /*!< Heap to use for allocations */ |
| 157 |
|
| 158 |
char *name_prefix; /*!< FTS table name prefix */ |
| 159 |
|
| 160 |
fts_table_t fts_index_table; /*!< Common table definition */ |
| 161 |
|
| 162 |
/*!< Common table definition */ |
| 163 |
fts_table_t fts_common_table; |
| 164 |
|
| 165 |
dict_table_t *table; /*!< Table that has to be queried */ |
| 166 |
|
| 167 |
dict_index_t *index; /*!< The FTS index to be optimized */ |
| 168 |
|
| 169 |
fts_doc_ids_t *to_delete; /*!< doc ids to delete, we check against |
| 170 |
this vector and purge the matching |
| 171 |
entries during the optimizing |
| 172 |
process. The vector entries are |
| 173 |
sorted on doc id */ |
| 174 |
|
| 175 |
ulint del_pos; /*!< Offset within to_delete vector, |
| 176 |
this is used to keep track of where |
| 177 |
we are up to in the vector */ |
| 178 |
|
| 179 |
bool done; /*!< true when optimize finishes */ |
| 180 |
|
| 181 |
ib_vector_t *words; /*!< Word + Nodes read from FTS_INDEX, |
| 182 |
it contains instances of fts_word_t */ |
| 183 |
|
| 184 |
fts_zip_t *zip; /*!< Words read from the FTS_INDEX */ |
| 185 |
|
| 186 |
fts_optimize_graph_t /*!< Prepared statements used during */ |
| 187 |
graph; /*optimize */ |
| 188 |
|
| 189 |
ulint n_completed; /*!< Number of FTS indexes that have |
| 190 |
been optimized */ |
| 191 |
bool del_list_regenerated; |
| 192 |
/*!< BEING_DELETED list regenerated */ |
| 193 |
}; |
| 194 |
|
| 195 |
/** Used by the optimize, to keep state during compacting nodes. */ |
| 196 |
struct fts_encode_t { |
| 197 |
doc_id_t src_last_doc_id; /*!< Last doc id read from src node */ |
| 198 |
byte *src_ilist_ptr; /*!< Current ptr within src ilist */ |
| 199 |
}; |
| 200 |
|
| 201 |
/** We use this information to determine when to start the optimize |
| 202 |
cycle for a table. */ |
| 203 |
struct fts_slot_t { |
| 204 |
table_id_t table_id = 0; /*!< Table id */ |
| 205 |
|
| 206 |
fts_state_t state = FTS_STATE_LOADED; /*!< State of this slot */ |
| 207 |
|
| 208 |
ulint added = 0; /*!< Number of doc ids added since the |
| 209 |
last time this table was optimized */ |
| 210 |
|
| 211 |
ulint deleted = 0; /*!< Number of doc ids deleted since the |
| 212 |
last time this table was optimized */ |
| 213 |
|
| 214 |
std::chrono::steady_clock::time_point |
| 215 |
last_run{}; /*!< Time last run completed */ |
| 216 |
|
| 217 |
std::chrono::steady_clock::time_point |
| 218 |
completed{}; /*!< Optimize finish time */ |
| 219 |
|
| 220 |
std::chrono::seconds interval_time{0}; /*!< Minimum time to wait before |
| 221 |
optimizing the table again. */ |
| 222 |
}; |
| 223 |
|
| 224 |
/** A table remove message for the FTS optimize thread. */ |
| 225 |
struct fts_msg_id_t { |
| 226 |
table_id_t table_id; /*!< The table to remove */ |
| 227 |
}; |
| 228 |
|
| 229 |
/** The FTS optimize message work queue message type. */ |
| 230 |
struct fts_msg_t { |
| 231 |
fts_msg_type_t type; /*!< Message type */ |
| 232 |
|
| 233 |
void *ptr; /*!< The message contents */ |
| 234 |
|
| 235 |
mem_heap_t *heap; /*!< The heap used to allocate this |
| 236 |
message, the message consumer will |
| 237 |
free the heap. */ |
| 238 |
}; |
| 239 |
|
| 240 |
/** The number of words to read and optimize in a single pass. */ |
| 241 |
ulong fts_num_word_optimize; |
| 242 |
|
| 243 |
// FIXME |
| 244 |
bool fts_enable_diag_print; |
| 245 |
|
| 246 |
/** ZLib compressed block size.*/ |
| 247 |
static ulint FTS_ZIP_BLOCK_SIZE = 1024; |
| 248 |
|
| 249 |
/** The amount of time optimizing in a single pass. */ |
| 250 |
static std::chrono::milliseconds fts_optimize_time_limit{0}; |
| 251 |
|
| 252 |
/** It's defined in fts0fts.cc */ |
| 253 |
extern const char *fts_common_tables[]; |
| 254 |
|
| 255 |
/** SQL Statement for changing state of rows to be deleted from FTS Index. */ |
| 256 |
static const char *fts_init_delete_sql = |
| 257 |
"BEGIN\n" |
| 258 |
"\n" |
| 259 |
"INSERT INTO $being_deleted\n" |
| 260 |
"SELECT doc_id FROM $deleted;\n" |
| 261 |
"\n" |
| 262 |
"INSERT INTO $being_deleted_cache\n" |
| 263 |
"SELECT doc_id FROM $deleted_cache;\n"; |
| 264 |
|
| 265 |
static const char *fts_delete_doc_ids_sql = |
| 266 |
"BEGIN\n" |
| 267 |
"\n" |
| 268 |
"DELETE FROM $deleted WHERE doc_id = :doc_id1;\n" |
| 269 |
"DELETE FROM $deleted_cache WHERE doc_id = :doc_id2;\n"; |
| 270 |
|
| 271 |
static const char *fts_end_delete_sql = |
| 272 |
"BEGIN\n" |
| 273 |
"\n" |
| 274 |
"DELETE FROM $being_deleted;\n" |
| 275 |
"DELETE FROM $being_deleted_cache;\n"; |
| 276 |
|
| 277 |
/** Set fts_zip_t::word to empty string. */ |
| 278 |
static void fts_zip_clear_word(fts_zip_t &zip) { |
| 279 |
zip.word.f_len = 0; |
| 280 |
memset(zip.word.f_str, 0, FTS_MAX_WORD_LEN); |
| 281 |
} |
| 282 |
|
| 283 |
/** Initialize fts_zip_t. */ |
| 284 |
static void fts_zip_initialize( |
| 285 |
fts_zip_t *zip) /*!< out: zip instance to initialize */ |
| 286 |
{ |
| 287 |
zip->pos = 0; |
| 288 |
zip->n_words = 0; |
| 289 |
|
| 290 |
zip->status = Z_OK; |
| 291 |
|
| 292 |
zip->last_big_block = 0; |
| 293 |
|
| 294 |
fts_zip_clear_word(*zip); |
| 295 |
|
| 296 |
ib_vector_reset(zip->blocks); |
| 297 |
|
| 298 |
memset(zip->zp, 0, sizeof(*zip->zp)); |
| 299 |
} |
| 300 |
|
| 301 |
/** Create an instance of fts_zip_t. |
| 302 |
@return a new instance of fts_zip_t */ |
| 303 |
static fts_zip_t *fts_zip_create(mem_heap_t *heap, /*!< in: heap */ |
| 304 |
ulint block_sz, /*!< in: size of a zip block.*/ |
| 305 |
ulint max_words) /*!< in: max words to read */ |
| 306 |
{ |
| 307 |
fts_zip_t *zip; |
| 308 |
|
| 309 |
zip = static_cast<fts_zip_t *>(mem_heap_zalloc(heap, sizeof(*zip))); |
| 310 |
|
| 311 |
zip->word.f_str = |
| 312 |
static_cast<byte *>(mem_heap_zalloc(heap, FTS_MAX_WORD_LEN + 1)); |
| 313 |
|
| 314 |
zip->block_sz = block_sz; |
| 315 |
|
| 316 |
zip->heap_alloc = ib_heap_allocator_create(heap); |
| 317 |
|
| 318 |
zip->blocks = ib_vector_create(zip->heap_alloc, sizeof(void *), 128); |
| 319 |
|
| 320 |
zip->max_words = max_words; |
| 321 |
|
| 322 |
zip->zp = static_cast<z_stream *>(mem_heap_zalloc(heap, sizeof(*zip->zp))); |
| 323 |
|
| 324 |
return (zip); |
| 325 |
} |
| 326 |
|
| 327 |
/** Initialize an instance of fts_zip_t. */ |
| 328 |
static void fts_zip_init( |
| 329 |
|
| 330 |
fts_zip_t *zip) /*!< in: zip instance to init */ |
| 331 |
{ |
| 332 |
memset(zip->zp, 0, sizeof(*zip->zp)); |
| 333 |
|
| 334 |
zip->word.f_len = 0; |
| 335 |
*zip->word.f_str = '\0'; |
| 336 |
} |
| 337 |
|
| 338 |
/** Create a fts_optimizer_word_t instance. |
| 339 |
@return new instance */ |
| 340 |
static fts_word_t *fts_word_init( |
| 341 |
fts_word_t *word, /*!< in: word to initialize */ |
| 342 |
byte *utf8, /*!< in: UTF-8 string */ |
| 343 |
ulint len) /*!< in: length of string in bytes */ |
| 344 |
{ |
| 345 |
mem_heap_t *heap = mem_heap_create(sizeof(fts_node_t), UT_LOCATION_HERE); |
| 346 |
|
| 347 |
memset(word, 0, sizeof(*word)); |
| 348 |
|
| 349 |
word->text.f_len = len; |
| 350 |
word->text.f_str = static_cast<byte *>(mem_heap_alloc(heap, len + 1)); |
| 351 |
|
| 352 |
/* Need to copy the NUL character too. */ |
| 353 |
memcpy(word->text.f_str, utf8, word->text.f_len); |
| 354 |
word->text.f_str[word->text.f_len] = 0; |
| 355 |
|
| 356 |
word->heap_alloc = ib_heap_allocator_create(heap); |
| 357 |
|
| 358 |
word->nodes = ib_vector_create(word->heap_alloc, sizeof(fts_node_t), |
| 359 |
FTS_WORD_NODES_INIT_SIZE); |
| 360 |
|
| 361 |
return (word); |
| 362 |
} |
| 363 |
|
| 364 |
/** Read the FTS INDEX row. |
| 365 |
@return fts_node_t instance */ |
| 366 |
static fts_node_t *fts_optimize_read_node(fts_word_t *word, /*!< in: */ |
| 367 |
que_node_t *exp) /*!< in: */ |
| 368 |
{ |
| 369 |
int i; |
| 370 |
fts_node_t *node = |
| 371 |
static_cast<fts_node_t *>(ib_vector_push(word->nodes, nullptr)); |
| 372 |
|
| 373 |
/* Start from 1 since the first node has been read by the caller */ |
| 374 |
for (i = 1; exp; exp = que_node_get_next(exp), ++i) { |
| 375 |
dfield_t *dfield = que_node_get_val(exp); |
| 376 |
byte *data = static_cast<byte *>(dfield_get_data(dfield)); |
| 377 |
ulint len = dfield_get_len(dfield); |
| 378 |
|
| 379 |
ut_a(len != UNIV_SQL_NULL); |
| 380 |
|
| 381 |
/* Note: The column numbers below must match the SELECT */ |
| 382 |
switch (i) { |
| 383 |
case 1: /* DOC_COUNT */ |
| 384 |
node->doc_count = mach_read_from_4(data); |
| 385 |
break; |
| 386 |
|
| 387 |
case 2: /* FIRST_DOC_ID */ |
| 388 |
node->first_doc_id = fts_read_doc_id(data); |
| 389 |
break; |
| 390 |
|
| 391 |
case 3: /* LAST_DOC_ID */ |
| 392 |
node->last_doc_id = fts_read_doc_id(data); |
| 393 |
break; |
| 394 |
|
| 395 |
case 4: /* ILIST */ |
| 396 |
node->ilist_size_alloc = node->ilist_size = len; |
| 397 |
node->ilist = static_cast<byte *>( |
| 398 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len)); |
| 399 |
if (len > 0) memcpy(node->ilist, data, len); |
| 400 |
break; |
| 401 |
|
| 402 |
default: |
| 403 |
ut_error; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/* Make sure all columns were read. */ |
| 408 |
ut_a(i == 5); |
| 409 |
|
| 410 |
return (node); |
| 411 |
} |
| 412 |
|
| 413 |
/** Callback function to fetch the rows in an FTS INDEX record. |
| 414 |
@return always returns non-NULL */ |
| 415 |
bool fts_optimize_index_fetch_node( |
| 416 |
void *row, /*!< in: sel_node_t* */ |
| 417 |
void *user_arg) /*!< in: pointer to ib_vector_t */ |
| 418 |
{ |
| 419 |
fts_word_t *word; |
| 420 |
sel_node_t *sel_node = static_cast<sel_node_t *>(row); |
| 421 |
fts_fetch_t *fetch = static_cast<fts_fetch_t *>(user_arg); |
| 422 |
ib_vector_t *words = static_cast<ib_vector_t *>(fetch->read_arg); |
| 423 |
que_node_t *exp = sel_node->select_list; |
| 424 |
dfield_t *dfield = que_node_get_val(exp); |
| 425 |
void *data = dfield_get_data(dfield); |
| 426 |
ulint dfield_len = dfield_get_len(dfield); |
| 427 |
fts_node_t *node; |
| 428 |
bool is_word_init = false; |
| 429 |
|
| 430 |
ut_a(dfield_len <= FTS_MAX_WORD_LEN); |
| 431 |
|
| 432 |
if (ib_vector_size(words) == 0) { |
| 433 |
word = static_cast<fts_word_t *>(ib_vector_push(words, nullptr)); |
| 434 |
fts_word_init(word, (byte *)data, dfield_len); |
| 435 |
is_word_init = true; |
| 436 |
} |
| 437 |
|
| 438 |
word = static_cast<fts_word_t *>(ib_vector_last(words)); |
| 439 |
|
| 440 |
if (dfield_len != word->text.f_len || |
| 441 |
memcmp(word->text.f_str, data, dfield_len)) { |
| 442 |
word = static_cast<fts_word_t *>(ib_vector_push(words, nullptr)); |
| 443 |
fts_word_init(word, (byte *)data, dfield_len); |
| 444 |
is_word_init = true; |
| 445 |
} |
| 446 |
|
| 447 |
node = fts_optimize_read_node(word, que_node_get_next(exp)); |
| 448 |
|
| 449 |
fetch->total_memory += node->ilist_size; |
| 450 |
if (is_word_init) { |
| 451 |
fetch->total_memory += sizeof(fts_word_t) + sizeof(ib_alloc_t) + |
| 452 |
sizeof(ib_vector_t) + dfield_len + |
| 453 |
sizeof(fts_node_t) * FTS_WORD_NODES_INIT_SIZE; |
| 454 |
} else if (ib_vector_size(words) > FTS_WORD_NODES_INIT_SIZE) { |
| 455 |
fetch->total_memory += sizeof(fts_node_t); |
| 456 |
} |
| 457 |
|
| 458 |
if (fetch->total_memory >= fts_result_cache_limit) { |
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
return true; |
| 463 |
} |
| 464 |
|
| 465 |
/** Read the rows from the FTS inde. |
| 466 |
@return DB_SUCCESS or error code */ |
| 467 |
dberr_t fts_index_fetch_nodes( |
| 468 |
trx_t *trx, /*!< in: transaction */ |
| 469 |
que_t **graph, /*!< in: prepared statement */ |
| 470 |
fts_table_t *fts_table, /*!< in: table of the FTS INDEX */ |
| 471 |
const fts_string_t *word, /*!< in: the word to fetch */ |
| 472 |
fts_fetch_t *fetch) /*!< in: fetch callback.*/ |
| 473 |
{ |
| 474 |
pars_info_t *info; |
| 475 |
dberr_t error; |
| 476 |
char table_name[MAX_FULL_NAME_LEN]; |
| 477 |
|
| 478 |
trx->op_info = "fetching FTS index nodes"; |
| 479 |
|
| 480 |
if (*graph) { |
| 481 |
info = (*graph)->info; |
| 482 |
} else { |
| 483 |
ulint selected; |
| 484 |
|
| 485 |
info = pars_info_create(); |
| 486 |
|
| 487 |
ut_a(fts_table->type == FTS_INDEX_TABLE); |
| 488 |
|
| 489 |
selected = fts_select_index(fts_table->charset, word->f_str, word->f_len); |
| 490 |
|
| 491 |
fts_table->suffix = fts_get_suffix(selected); |
| 492 |
|
| 493 |
fts_get_table_name(fts_table, table_name); |
| 494 |
|
| 495 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 496 |
} |
| 497 |
|
| 498 |
pars_info_bind_function(info, "my_func", fetch->read_record, fetch); |
| 499 |
pars_info_bind_varchar_literal(info, "word", word->f_str, word->f_len); |
| 500 |
|
| 501 |
if (!*graph) { |
| 502 |
*graph = fts_parse_sql(fts_table, info, |
| 503 |
"DECLARE FUNCTION my_func;\n" |
| 504 |
"DECLARE CURSOR c IS" |
| 505 |
" SELECT word, doc_count, first_doc_id, last_doc_id," |
| 506 |
" ilist\n" |
| 507 |
" FROM $table_name\n" |
| 508 |
" WHERE word LIKE :word\n" |
| 509 |
" ORDER BY first_doc_id;\n" |
| 510 |
"BEGIN\n" |
| 511 |
"\n" |
| 512 |
"OPEN c;\n" |
| 513 |
"WHILE 1 = 1 LOOP\n" |
| 514 |
" FETCH c INTO my_func();\n" |
| 515 |
" IF c % NOTFOUND THEN\n" |
| 516 |
" EXIT;\n" |
| 517 |
" END IF;\n" |
| 518 |
"END LOOP;\n" |
| 519 |
"CLOSE c;"); |
| 520 |
} |
| 521 |
|
| 522 |
for (;;) { |
| 523 |
error = fts_eval_sql(trx, *graph); |
| 524 |
|
| 525 |
if (error == DB_SUCCESS) { |
| 526 |
fts_sql_commit(trx); |
| 527 |
|
| 528 |
break; /* Exit the loop. */ |
| 529 |
} else { |
| 530 |
fts_sql_rollback(trx); |
| 531 |
|
| 532 |
if (error == DB_LOCK_WAIT_TIMEOUT) { |
| 533 |
ib::warn(ER_IB_MSG_486) << "lock wait timeout reading" |
| 534 |
" FTS index. Retrying!"; |
| 535 |
|
| 536 |
trx->error_state = DB_SUCCESS; |
| 537 |
} else { |
| 538 |
ib::error(ER_IB_MSG_487) |
| 539 |
<< "(" << ut_strerr(error) << ") while reading FTS index."; |
| 540 |
|
| 541 |
break; /* Exit the loop. */ |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
return (error); |
| 547 |
} |
| 548 |
|
| 549 |
/** Extract a given number of bytes into a buffer */ |
| 550 |
static bool fts_zip_extract_bytes(fts_zip_t *zip, /*!< in: Zip state + data */ |
| 551 |
byte *buf, /*!< out: buffer to be filled */ |
| 552 |
unsigned int size) /*!< in: size of buffer */ |
| 553 |
{ |
| 554 |
bool complete = false; |
| 555 |
zip->zp->next_out = buf; |
| 556 |
zip->zp->avail_out = size; |
| 557 |
void *null = nullptr; |
| 558 |
int flush = Z_NO_FLUSH; |
| 559 |
while (zip->status == Z_OK && zip->zp->avail_out > 0) { |
| 560 |
/* Finished decompressing block. */ |
| 561 |
if (zip->zp->avail_in == 0) { |
| 562 |
/* Free the block that's been decompressed. */ |
| 563 |
if (zip->pos > 0) { |
| 564 |
ulint prev = zip->pos - 1; |
| 565 |
|
| 566 |
ut_a(zip->pos < ib_vector_size(zip->blocks)); |
| 567 |
|
| 568 |
ut::free(ib_vector_getp(zip->blocks, prev)); |
| 569 |
ib_vector_set(zip->blocks, prev, &null); |
| 570 |
} |
| 571 |
|
| 572 |
/* Any more blocks to decompress. */ |
| 573 |
if (zip->pos < ib_vector_size(zip->blocks)) { |
| 574 |
zip->zp->next_in = |
| 575 |
static_cast<byte *>(ib_vector_getp(zip->blocks, zip->pos)); |
| 576 |
|
| 577 |
if (zip->pos > zip->last_big_block) { |
| 578 |
zip->zp->avail_in = FTS_MAX_WORD_LEN; |
| 579 |
} else { |
| 580 |
zip->zp->avail_in = static_cast<uInt>(zip->block_sz); |
| 581 |
} |
| 582 |
|
| 583 |
++zip->pos; |
| 584 |
} else { |
| 585 |
flush = Z_FINISH; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
switch (zip->status = inflate(zip->zp, flush)) { |
| 590 |
case Z_OK: |
| 591 |
if (zip->zp->avail_out == 0) { |
| 592 |
complete = true; |
| 593 |
} |
| 594 |
break; |
| 595 |
|
| 596 |
case Z_STREAM_END: |
| 597 |
if (zip->zp->avail_out == 0) { |
| 598 |
complete = true; |
| 599 |
} |
| 600 |
|
| 601 |
[[fallthrough]]; |
| 602 |
case Z_BUF_ERROR: /* No progress possible. */ |
| 603 |
inflateEnd(zip->zp); |
| 604 |
/* All blocks must be freed at end of inflate. */ |
| 605 |
for (ulint i = 0; i < ib_vector_size(zip->blocks); ++i) { |
| 606 |
if (ib_vector_getp(zip->blocks, i)) { |
| 607 |
ut::free(ib_vector_getp(zip->blocks, i)); |
| 608 |
ib_vector_set(zip->blocks, i, &null); |
| 609 |
} |
| 610 |
} |
| 611 |
break; |
| 612 |
|
| 613 |
case Z_STREAM_ERROR: |
| 614 |
default: |
| 615 |
ut_error; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
return complete; |
| 620 |
} |
| 621 |
|
| 622 |
/** Read a word */ |
| 623 |
static bool fts_zip_read_word(fts_zip_t *zip, /*!< in: Zip state + data */ |
| 624 |
fts_string_t *word) /*!< out: uncompressed word */ |
| 625 |
{ |
| 626 |
/* Either there was an error or we are at the Z_STREAM_END. */ |
| 627 |
if (zip->status != Z_OK) { |
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
ushort len = 0; |
| 632 |
|
| 633 |
const bool have_len = |
| 634 |
fts_zip_extract_bytes(zip, reinterpret_cast<byte *>(&len), sizeof(len)); |
| 635 |
|
| 636 |
if (!have_len) { |
| 637 |
return false; |
| 638 |
} |
| 639 |
|
| 640 |
ut_a(len <= FTS_MAX_WORD_LEN); |
| 641 |
|
| 642 |
word->f_len = len; |
| 643 |
const bool have_word = fts_zip_extract_bytes(zip, word->f_str, len); |
| 644 |
ut_ad(have_word); |
| 645 |
|
| 646 |
word->f_str[len] = 0; |
| 647 |
ut_ad(word->f_len == strlen((char *)word->f_str)); |
| 648 |
|
| 649 |
return have_word; |
| 650 |
} |
| 651 |
|
| 652 |
/** Callback function to fetch and compress the word in an FTS |
| 653 |
INDEX record. |
| 654 |
@return false on EOF */ |
| 655 |
static bool fts_fetch_index_words( |
| 656 |
void *row, /*!< in: sel_node_t* */ |
| 657 |
void *user_arg) /*!< in: pointer to fts_optimize_t */ |
| 658 |
{ |
| 659 |
const auto *const sel_node = static_cast<sel_node_t *>(row); |
| 660 |
const auto optim = static_cast<fts_optimize_t *>(user_arg); |
| 661 |
fts_zip_t *const zip = optim->zip; |
| 662 |
que_node_t *const exp = sel_node->select_list; |
| 663 |
const CHARSET_INFO *const charset = optim->fts_index_table.charset; |
| 664 |
const dfield_t *const dfield = que_node_get_val(exp); |
| 665 |
ushort len = static_cast<ushort>(dfield_get_len(dfield)); |
| 666 |
void *const data = dfield_get_data(dfield); |
| 667 |
|
| 668 |
/* Ensure each class of collation-equal tokens is compressed only once. */ |
| 669 |
{ |
| 670 |
/* Compare current word with previous using collation. */ |
| 671 |
auto comp = my_strnncoll(charset, static_cast<uint8_t *>(data), len, |
| 672 |
zip->word.f_str, zip->word.f_len); |
| 673 |
/* Duplicate detection is guaranteed by callback invocations being |
| 674 |
grouped into sequences (corresponding to aux tables), where all |
| 675 |
collation-equal words are in the same sequence; between these |
| 676 |
sequences zip->word is reset to an empty string (every non-empty |
| 677 |
word is assumed to be later in sort order than the empty string). |
| 678 |
Within each sequence words are sorted (using collation) in ascending |
| 679 |
order, guaranteeing all collation-equal words are passed to the |
| 680 |
callback in consecutive calls, which allows us to eliminate |
| 681 |
duplicates by comparing each word only with the one preceding it. */ |
| 682 |
ut_ad(comp >= 0); |
| 683 |
/* Skip the duplicate words. */ |
| 684 |
if (comp == 0) { |
| 685 |
return true; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
ut_a(len <= FTS_MAX_WORD_LEN); |
| 690 |
|
| 691 |
memcpy(zip->word.f_str, data, len); |
| 692 |
zip->word.f_len = len; |
| 693 |
|
| 694 |
ut_a(zip->zp->avail_in == 0); |
| 695 |
ut_a(zip->zp->next_in == nullptr); |
| 696 |
|
| 697 |
/* The string is prefixed by len. */ |
| 698 |
zip->zp->next_in = reinterpret_cast<byte *>(&len); |
| 699 |
zip->zp->avail_in = sizeof(len); |
| 700 |
|
| 701 |
/* Compress the word, create output blocks as necessary. */ |
| 702 |
while (zip->zp->avail_in > 0) { |
| 703 |
/* No space left in output buffer, create a new one. */ |
| 704 |
if (zip->zp->avail_out == 0) { |
| 705 |
byte *block; |
| 706 |
|
| 707 |
block = static_cast<byte *>( |
| 708 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, zip->block_sz)); |
| 709 |
|
| 710 |
ib_vector_push(zip->blocks, &block); |
| 711 |
|
| 712 |
zip->zp->next_out = block; |
| 713 |
zip->zp->avail_out = static_cast<uInt>(zip->block_sz); |
| 714 |
} |
| 715 |
|
| 716 |
switch (zip->status = deflate(zip->zp, Z_NO_FLUSH)) { |
| 717 |
case Z_OK: |
| 718 |
if (zip->zp->avail_in == 0) { |
| 719 |
zip->zp->next_in = static_cast<byte *>(data); |
| 720 |
zip->zp->avail_in = len; |
| 721 |
ut_a(len <= FTS_MAX_WORD_LEN); |
| 722 |
len = 0; |
| 723 |
} |
| 724 |
break; |
| 725 |
|
| 726 |
case Z_STREAM_END: |
| 727 |
case Z_BUF_ERROR: |
| 728 |
case Z_STREAM_ERROR: |
| 729 |
default: |
| 730 |
ut_error; |
| 731 |
break; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/* All data should have been compressed. */ |
| 736 |
ut_a(zip->zp->avail_in == 0); |
| 737 |
zip->zp->next_in = nullptr; |
| 738 |
|
| 739 |
++zip->n_words; |
| 740 |
|
| 741 |
return zip->n_words >= zip->max_words ? false : true; |
| 742 |
} |
| 743 |
|
| 744 |
/** Finish Zip deflate. */ |
| 745 |
static void fts_zip_deflate_end( |
| 746 |
fts_zip_t *zip) /*!< in: instance that should be closed*/ |
| 747 |
{ |
| 748 |
ut_a(zip->zp->avail_in == 0); |
| 749 |
ut_a(zip->zp->next_in == nullptr); |
| 750 |
|
| 751 |
zip->status = deflate(zip->zp, Z_FINISH); |
| 752 |
|
| 753 |
ut_a(ib_vector_size(zip->blocks) > 0); |
| 754 |
zip->last_big_block = ib_vector_size(zip->blocks) - 1; |
| 755 |
|
| 756 |
/* Allocate smaller block(s), since this is trailing data. */ |
| 757 |
while (zip->status == Z_OK) { |
| 758 |
byte *block; |
| 759 |
|
| 760 |
ut_a(zip->zp->avail_out == 0); |
| 761 |
|
| 762 |
block = static_cast<byte *>( |
| 763 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, FTS_MAX_WORD_LEN + 1)); |
| 764 |
|
| 765 |
ib_vector_push(zip->blocks, &block); |
| 766 |
|
| 767 |
zip->zp->next_out = block; |
| 768 |
zip->zp->avail_out = FTS_MAX_WORD_LEN; |
| 769 |
|
| 770 |
zip->status = deflate(zip->zp, Z_FINISH); |
| 771 |
} |
| 772 |
|
| 773 |
ut_a(zip->status == Z_STREAM_END); |
| 774 |
|
| 775 |
zip->status = deflateEnd(zip->zp); |
| 776 |
ut_a(zip->status == Z_OK); |
| 777 |
|
| 778 |
/* Reset the ZLib data structure. */ |
| 779 |
memset(zip->zp, 0, sizeof(*zip->zp)); |
| 780 |
} |
| 781 |
|
| 782 |
/** Read the words from the FTS INDEX. |
| 783 |
@return DB_SUCCESS if all OK, DB_TABLE_NOT_FOUND if no more indexes |
| 784 |
to search else error code */ |
| 785 |
[[nodiscard]] static dberr_t fts_index_fetch_words( |
| 786 |
fts_optimize_t *optim, /*!< in: optimize scratch pad */ |
| 787 |
const fts_string_t *word, /*!< in: get words greater than this |
| 788 |
word */ |
| 789 |
ulint n_words) /*!< in: max words to read */ |
| 790 |
{ |
| 791 |
pars_info_t *info; |
| 792 |
que_t *graph; |
| 793 |
ulint selected; |
| 794 |
fts_zip_t *zip = nullptr; |
| 795 |
dberr_t error = DB_SUCCESS; |
| 796 |
mem_heap_t *heap = static_cast<mem_heap_t *>(optim->self_heap->arg); |
| 797 |
bool inited = false; |
| 798 |
|
| 799 |
optim->trx->op_info = "fetching FTS index words"; |
| 800 |
|
| 801 |
if (optim->zip == nullptr) { |
| 802 |
optim->zip = fts_zip_create(heap, FTS_ZIP_BLOCK_SIZE, n_words); |
| 803 |
} else { |
| 804 |
fts_zip_initialize(optim->zip); |
| 805 |
} |
| 806 |
|
| 807 |
for (selected = fts_select_index(optim->fts_index_table.charset, word->f_str, |
| 808 |
word->f_len); |
| 809 |
selected < FTS_NUM_AUX_INDEX; selected++) { |
| 810 |
char table_name[MAX_FULL_NAME_LEN]; |
| 811 |
|
| 812 |
optim->fts_index_table.suffix = fts_get_suffix(selected); |
| 813 |
|
| 814 |
/* The list of words to be processed may not have any duplicates - |
| 815 |
meaning here words that are equal in collation order. Elimination |
| 816 |
of duplicates is performed inside the fts_fetch_index_words callback |
| 817 |
using optim->zip->word as the previous word processed. It relies on |
| 818 |
all collation-equal words being passed to the callback in consecutive |
| 819 |
calls. This property holds because: |
| 820 |
1. All words equal by collation order are indexed in the same aux |
| 821 |
table, |
| 822 |
2. Words from a given aux table are processed by collation |
| 823 |
(ascending) order. |
| 824 |
(1) is ensured by the way words are assigned to aux table number - |
| 825 |
see fts_select_index function. (2) is guaranteed by 'ORDER BY word' |
| 826 |
clause in the query below. |
| 827 |
|
| 828 |
optim->zip->word is cleared out when moving from one aux table to |
| 829 |
another; it is not needed, as there can be no duplicates between |
| 830 |
tables, but doing so allows the callback to assert ordering within |
| 831 |
the table. */ |
| 832 |
|
| 833 |
fts_zip_clear_word(*optim->zip); |
| 834 |
|
| 835 |
info = pars_info_create(); |
| 836 |
|
| 837 |
pars_info_bind_function(info, "my_func", fts_fetch_index_words, optim); |
| 838 |
|
| 839 |
pars_info_bind_varchar_literal(info, "word", word->f_str, word->f_len); |
| 840 |
|
| 841 |
fts_get_table_name(&optim->fts_index_table, table_name); |
| 842 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 843 |
|
| 844 |
graph = fts_parse_sql(&optim->fts_index_table, info, |
| 845 |
"DECLARE FUNCTION my_func;\n" |
| 846 |
"DECLARE CURSOR c IS" |
| 847 |
" SELECT word\n" |
| 848 |
" FROM $table_name\n" |
| 849 |
" WHERE word > :word\n" |
| 850 |
" ORDER BY word;\n" |
| 851 |
"BEGIN\n" |
| 852 |
"\n" |
| 853 |
"OPEN c;\n" |
| 854 |
"WHILE 1 = 1 LOOP\n" |
| 855 |
" FETCH c INTO my_func();\n" |
| 856 |
" IF c % NOTFOUND THEN\n" |
| 857 |
" EXIT;\n" |
| 858 |
" END IF;\n" |
| 859 |
"END LOOP;\n" |
| 860 |
"CLOSE c;"); |
| 861 |
|
| 862 |
zip = optim->zip; |
| 863 |
|
| 864 |
for (;;) { |
| 865 |
int err; |
| 866 |
|
| 867 |
if (!inited && ((err = deflateInit(zip->zp, 9)) != Z_OK)) { |
| 868 |
ib::error(ER_IB_MSG_488) << "ZLib deflateInit() failed: " << err; |
| 869 |
|
| 870 |
error = DB_ERROR; |
| 871 |
break; |
| 872 |
} else { |
| 873 |
inited = true; |
| 874 |
error = fts_eval_sql(optim->trx, graph); |
| 875 |
} |
| 876 |
|
| 877 |
if (error == DB_SUCCESS) { |
| 878 |
// FIXME fts_sql_commit(optim->trx); |
| 879 |
break; |
| 880 |
} else { |
| 881 |
// FIXME fts_sql_rollback(optim->trx); |
| 882 |
|
| 883 |
if (error == DB_LOCK_WAIT_TIMEOUT) { |
| 884 |
ib::warn(ER_IB_MSG_489) << "Lock wait timeout" |
| 885 |
" reading document. Retrying!"; |
| 886 |
|
| 887 |
/* We need to reset the ZLib state. */ |
| 888 |
inited = false; |
| 889 |
deflateEnd(zip->zp); |
| 890 |
fts_zip_init(zip); |
| 891 |
|
| 892 |
optim->trx->error_state = DB_SUCCESS; |
| 893 |
} else { |
| 894 |
ib::error(ER_IB_MSG_490) |
| 895 |
<< "(" << ut_strerr(error) << ") while reading document."; |
| 896 |
|
| 897 |
break; /* Exit the loop. */ |
| 898 |
} |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
fts_que_graph_free(graph); |
| 903 |
|
| 904 |
/* Check if max word to fetch is exceeded */ |
| 905 |
if (optim->zip->n_words >= n_words) { |
| 906 |
break; |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
if (error == DB_SUCCESS && zip->status == Z_OK && zip->n_words > 0) { |
| 911 |
/* All data should have been read. */ |
| 912 |
ut_a(zip->zp->avail_in == 0); |
| 913 |
|
| 914 |
fts_zip_deflate_end(zip); |
| 915 |
} else { |
| 916 |
deflateEnd(zip->zp); |
| 917 |
} |
| 918 |
|
| 919 |
return (error); |
| 920 |
} |
| 921 |
|
| 922 |
/** Callback function to fetch the doc id from the record. |
| 923 |
@return always returns true */ |
| 924 |
static bool fts_fetch_doc_ids(void *row, /*!< in: sel_node_t* */ |
| 925 |
void *user_arg) /*!< in: pointer to ib_vector_t */ |
| 926 |
{ |
| 927 |
que_node_t *exp; |
| 928 |
int i = 0; |
| 929 |
sel_node_t *sel_node = static_cast<sel_node_t *>(row); |
| 930 |
fts_doc_ids_t *fts_doc_ids = static_cast<fts_doc_ids_t *>(user_arg); |
| 931 |
fts_update_t *update = static_cast<fts_update_t *>( |
| 932 |
ib_vector_push(fts_doc_ids->doc_ids, nullptr)); |
| 933 |
|
| 934 |
for (exp = sel_node->select_list; exp; exp = que_node_get_next(exp), ++i) { |
| 935 |
dfield_t *dfield = que_node_get_val(exp); |
| 936 |
void *data = dfield_get_data(dfield); |
| 937 |
ulint len = dfield_get_len(dfield); |
| 938 |
|
| 939 |
ut_a(len != UNIV_SQL_NULL); |
| 940 |
|
| 941 |
/* Note: The column numbers below must match the SELECT. */ |
| 942 |
switch (i) { |
| 943 |
case 0: /* DOC_ID */ |
| 944 |
update->fts_indexes = nullptr; |
| 945 |
update->doc_id = fts_read_doc_id(static_cast<byte *>(data)); |
| 946 |
break; |
| 947 |
|
| 948 |
default: |
| 949 |
ut_error; |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
return true; |
| 954 |
} |
| 955 |
|
| 956 |
/** Read the rows from a FTS common auxiliary table. |
| 957 |
@return DB_SUCCESS or error code */ |
| 958 |
dberr_t fts_table_fetch_doc_ids( |
| 959 |
trx_t *trx, /*!< in: transaction */ |
| 960 |
fts_table_t *fts_table, /*!< in: table */ |
| 961 |
fts_doc_ids_t *doc_ids) /*!< in: For collecting doc ids */ |
| 962 |
{ |
| 963 |
dberr_t error; |
| 964 |
que_t *graph; |
| 965 |
pars_info_t *info = pars_info_create(); |
| 966 |
bool alloc_bk_trx = false; |
| 967 |
char table_name[MAX_FULL_NAME_LEN]; |
| 968 |
|
| 969 |
ut_a(fts_table->suffix != nullptr); |
| 970 |
ut_a(fts_table->type == FTS_COMMON_TABLE); |
| 971 |
|
| 972 |
if (!trx) { |
| 973 |
trx = trx_allocate_for_background(); |
| 974 |
alloc_bk_trx = true; |
| 975 |
} |
| 976 |
|
| 977 |
trx->op_info = "fetching FTS doc ids"; |
| 978 |
|
| 979 |
pars_info_bind_function(info, "my_func", fts_fetch_doc_ids, doc_ids); |
| 980 |
|
| 981 |
fts_get_table_name(fts_table, table_name); |
| 982 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 983 |
|
| 984 |
graph = fts_parse_sql(fts_table, info, |
| 985 |
"DECLARE FUNCTION my_func;\n" |
| 986 |
"DECLARE CURSOR c IS" |
| 987 |
" SELECT doc_id FROM $table_name;\n" |
| 988 |
"BEGIN\n" |
| 989 |
"\n" |
| 990 |
"OPEN c;\n" |
| 991 |
"WHILE 1 = 1 LOOP\n" |
| 992 |
" FETCH c INTO my_func();\n" |
| 993 |
" IF c % NOTFOUND THEN\n" |
| 994 |
" EXIT;\n" |
| 995 |
" END IF;\n" |
| 996 |
"END LOOP;\n" |
| 997 |
"CLOSE c;"); |
| 998 |
|
| 999 |
error = fts_eval_sql(trx, graph); |
| 1000 |
|
| 1001 |
que_graph_free(graph); |
| 1002 |
|
| 1003 |
if (error == DB_SUCCESS) { |
| 1004 |
fts_sql_commit(trx); |
| 1005 |
|
| 1006 |
ib_vector_sort(doc_ids->doc_ids, fts_doc_id_field_cmp<fts_update_t>); |
| 1007 |
} else { |
| 1008 |
fts_sql_rollback(trx); |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (alloc_bk_trx) { |
| 1012 |
trx_free_for_background(trx); |
| 1013 |
} |
| 1014 |
|
| 1015 |
return (error); |
| 1016 |
} |
| 1017 |
|
| 1018 |
/** Do a binary search for a doc id in the array |
| 1019 |
@return +ve index if found -ve index where it should be inserted |
| 1020 |
if not found */ |
| 1021 |
int fts_bsearch(fts_update_t *array, /*!< in: array to sort */ |
| 1022 |
int lower, /*!< in: the array lower bound */ |
| 1023 |
int upper, /*!< in: the array upper bound */ |
| 1024 |
doc_id_t doc_id) /*!< in: the doc id to search for */ |
| 1025 |
{ |
| 1026 |
int orig_size = upper; |
| 1027 |
|
| 1028 |
if (upper == 0) { |
| 1029 |
/* Nothing to search */ |
| 1030 |
return (-1); |
| 1031 |
} else { |
| 1032 |
while (lower < upper) { |
| 1033 |
int i = (lower + upper) >> 1; |
| 1034 |
|
| 1035 |
if (doc_id > array[i].doc_id) { |
| 1036 |
lower = i + 1; |
| 1037 |
} else if (doc_id < array[i].doc_id) { |
| 1038 |
upper = i - 1; |
| 1039 |
} else { |
| 1040 |
return (i); /* Found. */ |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (lower == upper && lower < orig_size) { |
| 1046 |
if (doc_id == array[lower].doc_id) { |
| 1047 |
return (lower); |
| 1048 |
} else if (lower == 0) { |
| 1049 |
return (-1); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* Not found. */ |
| 1054 |
return ((lower == 0) ? -1 : -(lower)); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/** Search in the to delete array whether any of the doc ids within |
| 1058 |
the [first, last] range are to be deleted |
| 1059 |
@return +ve index if found -ve index where it should be inserted |
| 1060 |
if not found */ |
| 1061 |
static int fts_optimize_lookup( |
| 1062 |
ib_vector_t *doc_ids, /*!< in: array to search */ |
| 1063 |
ulint lower, /*!< in: lower limit of array */ |
| 1064 |
doc_id_t first_doc_id, /*!< in: doc id to lookup */ |
| 1065 |
doc_id_t last_doc_id) /*!< in: doc id to lookup */ |
| 1066 |
{ |
| 1067 |
int pos; |
| 1068 |
int upper = static_cast<int>(ib_vector_size(doc_ids)); |
| 1069 |
fts_update_t *array = (fts_update_t *)doc_ids->data; |
| 1070 |
|
| 1071 |
pos = fts_bsearch(array, static_cast<int>(lower), upper, first_doc_id); |
| 1072 |
|
| 1073 |
ut_a(abs(pos) <= upper + 1); |
| 1074 |
|
| 1075 |
if (pos < 0) { |
| 1076 |
int i = abs(pos); |
| 1077 |
|
| 1078 |
/* If i is 1, it could be first_doc_id is less than |
| 1079 |
either the first or second array item, do a |
| 1080 |
double check */ |
| 1081 |
if (i == 1 && array[0].doc_id <= last_doc_id && |
| 1082 |
first_doc_id < array[0].doc_id) { |
| 1083 |
pos = 0; |
| 1084 |
} else if (i < upper && array[i].doc_id <= last_doc_id) { |
| 1085 |
/* Check if the "next" doc id is within the |
| 1086 |
first & last doc id of the node. */ |
| 1087 |
pos = i; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
return (pos); |
| 1092 |
} |
| 1093 |
|
| 1094 |
/** Encode the word pos list into the node |
| 1095 |
@return DB_SUCCESS or error code*/ |
| 1096 |
static dberr_t fts_optimize_encode_node( |
| 1097 |
fts_node_t *node, /*!< in: node to fill*/ |
| 1098 |
doc_id_t doc_id, /*!< in: doc id to encode */ |
| 1099 |
fts_encode_t *enc) /*!< in: encoding state.*/ |
| 1100 |
{ |
| 1101 |
byte *dst; |
| 1102 |
ulint enc_len; |
| 1103 |
ulint pos_enc_len; |
| 1104 |
doc_id_t doc_id_delta; |
| 1105 |
dberr_t error = DB_SUCCESS; |
| 1106 |
byte *src = enc->src_ilist_ptr; |
| 1107 |
|
| 1108 |
if (node->first_doc_id == 0) { |
| 1109 |
ut_a(node->last_doc_id == 0); |
| 1110 |
|
| 1111 |
node->first_doc_id = doc_id; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Calculate the space required to store the ilist. */ |
| 1115 |
ut_ad(doc_id > node->last_doc_id); |
| 1116 |
doc_id_delta = doc_id - node->last_doc_id; |
| 1117 |
enc_len = fts_get_encoded_len(doc_id_delta); |
| 1118 |
|
| 1119 |
/* Calculate the size of the encoded pos array. */ |
| 1120 |
while (*src) { |
| 1121 |
fts_decode_vlc(&src); |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* Skip the 0x00 byte at the end of the word positions list. */ |
| 1125 |
++src; |
| 1126 |
|
| 1127 |
/* Number of encoded pos bytes to copy. */ |
| 1128 |
pos_enc_len = src - enc->src_ilist_ptr; |
| 1129 |
|
| 1130 |
/* Total number of bytes required for copy. */ |
| 1131 |
enc_len += pos_enc_len; |
| 1132 |
|
| 1133 |
/* Check we have enough space in the destination buffer for |
| 1134 |
copying the document word list. */ |
| 1135 |
if (!node->ilist) { |
| 1136 |
ulint new_size; |
| 1137 |
|
| 1138 |
ut_a(node->ilist_size == 0); |
| 1139 |
|
| 1140 |
new_size = enc_len > FTS_ILIST_MAX_SIZE ? enc_len : FTS_ILIST_MAX_SIZE; |
| 1141 |
|
| 1142 |
node->ilist = static_cast<byte *>( |
| 1143 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, new_size)); |
| 1144 |
node->ilist_size_alloc = new_size; |
| 1145 |
|
| 1146 |
} else if ((node->ilist_size + enc_len) > node->ilist_size_alloc) { |
| 1147 |
ulint new_size = node->ilist_size + enc_len; |
| 1148 |
byte *ilist = static_cast<byte *>( |
| 1149 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, new_size)); |
| 1150 |
|
| 1151 |
memcpy(ilist, node->ilist, node->ilist_size); |
| 1152 |
|
| 1153 |
ut::free(node->ilist); |
| 1154 |
|
| 1155 |
node->ilist = ilist; |
| 1156 |
node->ilist_size_alloc = new_size; |
| 1157 |
} |
| 1158 |
|
| 1159 |
src = enc->src_ilist_ptr; |
| 1160 |
dst = node->ilist + node->ilist_size; |
| 1161 |
|
| 1162 |
/* Encode the doc id. */ |
| 1163 |
dst += fts_encode_int(doc_id_delta, dst); |
| 1164 |
|
| 1165 |
/* Copy the encoded pos array. */ |
| 1166 |
memcpy(dst, src, pos_enc_len); |
| 1167 |
|
| 1168 |
node->last_doc_id = doc_id; |
| 1169 |
|
| 1170 |
/* Data copied up to here. */ |
| 1171 |
node->ilist_size += enc_len; |
| 1172 |
enc->src_ilist_ptr += pos_enc_len; |
| 1173 |
|
| 1174 |
ut_a(node->ilist_size <= node->ilist_size_alloc); |
| 1175 |
|
| 1176 |
return (error); |
| 1177 |
} |
| 1178 |
|
| 1179 |
/** Optimize the data contained in a node. |
| 1180 |
@return DB_SUCCESS or error code*/ |
| 1181 |
static dberr_t fts_optimize_node( |
| 1182 |
ib_vector_t *del_vec, /*!< in: vector of doc ids to delete*/ |
| 1183 |
int *del_pos, /*!< in: offset into above vector */ |
| 1184 |
fts_node_t *dst_node, /*!< in: node to fill*/ |
| 1185 |
fts_node_t *src_node, /*!< in: source node for data*/ |
| 1186 |
fts_encode_t *enc) /*!< in: encoding state */ |
| 1187 |
{ |
| 1188 |
ulint copied; |
| 1189 |
dberr_t error = DB_SUCCESS; |
| 1190 |
doc_id_t doc_id = enc->src_last_doc_id; |
| 1191 |
|
| 1192 |
if (!enc->src_ilist_ptr) { |
| 1193 |
enc->src_ilist_ptr = src_node->ilist; |
| 1194 |
} |
| 1195 |
|
| 1196 |
copied = enc->src_ilist_ptr - src_node->ilist; |
| 1197 |
|
| 1198 |
/* While there is data in the source node and space to copy |
| 1199 |
into in the destination node. */ |
| 1200 |
while (copied < src_node->ilist_size && |
| 1201 |
dst_node->ilist_size < FTS_ILIST_MAX_SIZE) { |
| 1202 |
doc_id_t del_doc_id = FTS_NULL_DOC_ID; |
| 1203 |
|
| 1204 |
doc_id_t delta = fts_decode_vlc(&enc->src_ilist_ptr); |
| 1205 |
|
| 1206 |
test_again: |
| 1207 |
/* Check whether the doc id is in the delete list, if |
| 1208 |
so then we skip the entries but we need to track the |
| 1209 |
delta for decoding the entries following this document's |
| 1210 |
entries. */ |
| 1211 |
if (*del_pos >= 0 && *del_pos < (int)ib_vector_size(del_vec)) { |
| 1212 |
fts_update_t *update; |
| 1213 |
|
| 1214 |
update = (fts_update_t *)ib_vector_get(del_vec, *del_pos); |
| 1215 |
|
| 1216 |
del_doc_id = update->doc_id; |
| 1217 |
} |
| 1218 |
|
| 1219 |
if (enc->src_ilist_ptr == src_node->ilist && doc_id == 0) { |
| 1220 |
ut_a(delta == src_node->first_doc_id); |
| 1221 |
} |
| 1222 |
|
| 1223 |
doc_id += delta; |
| 1224 |
|
| 1225 |
if (del_doc_id > 0 && doc_id == del_doc_id) { |
| 1226 |
++*del_pos; |
| 1227 |
|
| 1228 |
/* Skip the entries for this document. */ |
| 1229 |
while (*enc->src_ilist_ptr) { |
| 1230 |
fts_decode_vlc(&enc->src_ilist_ptr); |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* Skip the end of word position marker. */ |
| 1234 |
++enc->src_ilist_ptr; |
| 1235 |
|
| 1236 |
} else { |
| 1237 |
/* DOC ID already becomes larger than |
| 1238 |
del_doc_id, check the next del_doc_id */ |
| 1239 |
if (del_doc_id > 0 && doc_id > del_doc_id) { |
| 1240 |
del_doc_id = 0; |
| 1241 |
++*del_pos; |
| 1242 |
delta = 0; |
| 1243 |
goto test_again; |
| 1244 |
} |
| 1245 |
|
| 1246 |
/* Decode and copy the word positions into |
| 1247 |
the dest node. */ |
| 1248 |
fts_optimize_encode_node(dst_node, doc_id, enc); |
| 1249 |
|
| 1250 |
++dst_node->doc_count; |
| 1251 |
|
| 1252 |
ut_a(dst_node->last_doc_id == doc_id); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* Bytes copied so for from source. */ |
| 1256 |
copied = enc->src_ilist_ptr - src_node->ilist; |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (copied >= src_node->ilist_size) { |
| 1260 |
ut_a(doc_id == src_node->last_doc_id); |
| 1261 |
} |
| 1262 |
|
| 1263 |
enc->src_last_doc_id = doc_id; |
| 1264 |
|
| 1265 |
return (error); |
| 1266 |
} |
| 1267 |
|
| 1268 |
/** Determine the starting pos within the deleted doc id vector for a word. |
| 1269 |
@return delete position */ |
| 1270 |
[[nodiscard]] static int fts_optimize_deleted_pos( |
| 1271 |
fts_optimize_t *optim, /*!< in: optimize state data */ |
| 1272 |
fts_word_t *word) /*!< in: the word data to check */ |
| 1273 |
{ |
| 1274 |
int del_pos; |
| 1275 |
ib_vector_t *del_vec = optim->to_delete->doc_ids; |
| 1276 |
|
| 1277 |
/* Get the first and last dict ids for the word, we will use |
| 1278 |
these values to determine which doc ids need to be removed |
| 1279 |
when we coalesce the nodes. This way we can reduce the number |
| 1280 |
of elements that need to be searched in the deleted doc ids |
| 1281 |
vector and secondly we can remove the doc ids during the |
| 1282 |
coalescing phase. */ |
| 1283 |
if (ib_vector_size(del_vec) > 0) { |
| 1284 |
fts_node_t *node; |
| 1285 |
doc_id_t last_id; |
| 1286 |
doc_id_t first_id; |
| 1287 |
ulint size = ib_vector_size(word->nodes); |
| 1288 |
|
| 1289 |
node = (fts_node_t *)ib_vector_get(word->nodes, 0); |
| 1290 |
first_id = node->first_doc_id; |
| 1291 |
|
| 1292 |
node = (fts_node_t *)ib_vector_get(word->nodes, size - 1); |
| 1293 |
last_id = node->last_doc_id; |
| 1294 |
|
| 1295 |
ut_a(first_id <= last_id); |
| 1296 |
|
| 1297 |
del_pos = fts_optimize_lookup(del_vec, optim->del_pos, first_id, last_id); |
| 1298 |
} else { |
| 1299 |
del_pos = -1; /* Note that there is nothing to delete. */ |
| 1300 |
} |
| 1301 |
|
| 1302 |
return (del_pos); |
| 1303 |
} |
| 1304 |
|
| 1305 |
#define FTS_DEBUG_PRINT |
| 1306 |
/** Compact the nodes for a word, we also remove any doc ids during the |
| 1307 |
compaction pass. |
| 1308 |
@return DB_SUCCESS or error code.*/ |
| 1309 |
static ib_vector_t *fts_optimize_word( |
| 1310 |
fts_optimize_t *optim, /*!< in: optimize state data */ |
| 1311 |
fts_word_t *word) /*!< in: the word to optimize */ |
| 1312 |
{ |
| 1313 |
fts_encode_t enc; |
| 1314 |
ib_vector_t *nodes; |
| 1315 |
ulint i = 0; |
| 1316 |
int del_pos; |
| 1317 |
fts_node_t *dst_node = nullptr; |
| 1318 |
ib_vector_t *del_vec = optim->to_delete->doc_ids; |
| 1319 |
ulint size = ib_vector_size(word->nodes); |
| 1320 |
|
| 1321 |
del_pos = fts_optimize_deleted_pos(optim, word); |
| 1322 |
nodes = ib_vector_create(word->heap_alloc, sizeof(*dst_node), 128); |
| 1323 |
|
| 1324 |
enc.src_last_doc_id = 0; |
| 1325 |
enc.src_ilist_ptr = nullptr; |
| 1326 |
|
| 1327 |
if (fts_enable_diag_print) { |
| 1328 |
word->text.f_str[word->text.f_len] = 0; |
| 1329 |
ib::info(ER_IB_MSG_491) |
| 1330 |
<< "FTS_OPTIMIZE: optimize \"" << word->text.f_str << "\""; |
| 1331 |
} |
| 1332 |
|
| 1333 |
while (i < size) { |
| 1334 |
ulint copied; |
| 1335 |
fts_node_t *src_node; |
| 1336 |
|
| 1337 |
src_node = (fts_node_t *)ib_vector_get(word->nodes, i); |
| 1338 |
|
| 1339 |
if (dst_node == nullptr || dst_node->last_doc_id > src_node->first_doc_id) { |
| 1340 |
dst_node = static_cast<fts_node_t *>(ib_vector_push(nodes, nullptr)); |
| 1341 |
memset(dst_node, 0, sizeof(*dst_node)); |
| 1342 |
} |
| 1343 |
|
| 1344 |
/* Copy from the src to the dst node. */ |
| 1345 |
fts_optimize_node(del_vec, &del_pos, dst_node, src_node, &enc); |
| 1346 |
|
| 1347 |
ut_a(enc.src_ilist_ptr != nullptr); |
| 1348 |
|
| 1349 |
/* Determine the number of bytes copied to dst_node. */ |
| 1350 |
copied = enc.src_ilist_ptr - src_node->ilist; |
| 1351 |
|
| 1352 |
/* Can't copy more than what's in the vlc array. */ |
| 1353 |
ut_a(copied <= src_node->ilist_size); |
| 1354 |
|
| 1355 |
/* We are done with this node release the resources. */ |
| 1356 |
if (copied == src_node->ilist_size) { |
| 1357 |
enc.src_last_doc_id = 0; |
| 1358 |
enc.src_ilist_ptr = nullptr; |
| 1359 |
|
| 1360 |
ut::free(src_node->ilist); |
| 1361 |
|
| 1362 |
src_node->ilist = nullptr; |
| 1363 |
src_node->ilist_size = src_node->ilist_size_alloc = 0; |
| 1364 |
|
| 1365 |
src_node = nullptr; |
| 1366 |
|
| 1367 |
++i; /* Get next source node to OPTIMIZE. */ |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (dst_node->ilist_size >= FTS_ILIST_MAX_SIZE || i >= size) { |
| 1371 |
dst_node = nullptr; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
/* All dst nodes created should have been added to the vector. */ |
| 1376 |
ut_a(dst_node == nullptr); |
| 1377 |
|
| 1378 |
/* Return the OPTIMIZED nodes. */ |
| 1379 |
return (nodes); |
| 1380 |
} |
| 1381 |
|
| 1382 |
/** Update the FTS index table. This is a delete followed by an insert. |
| 1383 |
@return DB_SUCCESS or error code */ |
| 1384 |
[[nodiscard]] static dberr_t fts_optimize_write_word( |
| 1385 |
trx_t *trx, /*!< in: transaction */ |
| 1386 |
fts_table_t *fts_table, /*!< in: table of FTS index */ |
| 1387 |
fts_string_t *word, /*!< in: word data to write */ |
| 1388 |
ib_vector_t *nodes) /*!< in: the nodes to write */ |
| 1389 |
{ |
| 1390 |
ulint i; |
| 1391 |
pars_info_t *info; |
| 1392 |
que_t *graph; |
| 1393 |
ulint selected; |
| 1394 |
dberr_t error = DB_SUCCESS; |
| 1395 |
char table_name[MAX_FULL_NAME_LEN]; |
| 1396 |
|
| 1397 |
info = pars_info_create(); |
| 1398 |
|
| 1399 |
ut_ad(fts_table->charset); |
| 1400 |
|
| 1401 |
if (fts_enable_diag_print) { |
| 1402 |
ib::info(ER_IB_MSG_492) |
| 1403 |
<< "FTS_OPTIMIZE: processed \"" << word->f_str << "\""; |
| 1404 |
} |
| 1405 |
|
| 1406 |
pars_info_bind_varchar_literal(info, "word", word->f_str, word->f_len); |
| 1407 |
|
| 1408 |
selected = fts_select_index(fts_table->charset, word->f_str, word->f_len); |
| 1409 |
|
| 1410 |
fts_table->suffix = fts_get_suffix(selected); |
| 1411 |
fts_get_table_name(fts_table, table_name); |
| 1412 |
pars_info_bind_id(info, true, "table_name", table_name); |
| 1413 |
|
| 1414 |
graph = fts_parse_sql(fts_table, info, |
| 1415 |
"BEGIN DELETE FROM $table_name WHERE word = :word;"); |
| 1416 |
|
| 1417 |
error = fts_eval_sql(trx, graph); |
| 1418 |
|
| 1419 |
if (error != DB_SUCCESS) { |
| 1420 |
ib::error(ER_IB_MSG_493) << "(" << ut_strerr(error) |
| 1421 |
<< ") during optimize," |
| 1422 |
" when deleting a word from the FTS index."; |
| 1423 |
} |
| 1424 |
|
| 1425 |
fts_que_graph_free(graph); |
| 1426 |
graph = nullptr; |
| 1427 |
|
| 1428 |
/* Even if the operation needs to be rolled back and redone, |
| 1429 |
we iterate over the nodes in order to free the ilist. */ |
| 1430 |
for (i = 0; i < ib_vector_size(nodes); ++i) { |
| 1431 |
fts_node_t *node = (fts_node_t *)ib_vector_get(nodes, i); |
| 1432 |
|
| 1433 |
if (error == DB_SUCCESS) { |
| 1434 |
/* Skip empty node. */ |
| 1435 |
if (node->ilist == nullptr) { |
| 1436 |
ut_ad(node->ilist_size == 0); |
| 1437 |
continue; |
| 1438 |
} |
| 1439 |
|
| 1440 |
error = fts_write_node(trx, &graph, fts_table, word, node); |
| 1441 |
|
| 1442 |
if (error != DB_SUCCESS) { |
| 1443 |
ib::error(ER_IB_MSG_494) << "(" << ut_strerr(error) |
| 1444 |
<< ")" |
| 1445 |
" during optimize, while adding a" |
| 1446 |
" word to the FTS index."; |
| 1447 |
} |
| 1448 |
} |
| 1449 |
|
| 1450 |
ut::free(node->ilist); |
| 1451 |
node->ilist = nullptr; |
| 1452 |
node->ilist_size = node->ilist_size_alloc = 0; |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (graph != nullptr) { |
| 1456 |
fts_que_graph_free(graph); |
| 1457 |
} |
| 1458 |
|
| 1459 |
return (error); |
| 1460 |
} |
| 1461 |
|
| 1462 |
/** Free fts_optimizer_word_t instanace.*/ |
| 1463 |
void fts_word_free(fts_word_t *word) /*!< in: instance to free.*/ |
| 1464 |
{ |
| 1465 |
mem_heap_t *heap = static_cast<mem_heap_t *>(word->heap_alloc->arg); |
| 1466 |
|
| 1467 |
#ifdef UNIV_DEBUG |
| 1468 |
memset(word, 0, sizeof(*word)); |
| 1469 |
#endif /* UNIV_DEBUG */ |
| 1470 |
|
| 1471 |
mem_heap_free(heap); |
| 1472 |
} |
| 1473 |
|
| 1474 |
/** Optimize the word ilist and rewrite data to the FTS index. |
| 1475 |
@return status one of RESTART, EXIT, ERROR */ |
| 1476 |
[[nodiscard]] static dberr_t fts_optimize_compact( |
| 1477 |
fts_optimize_t *optim, /*!< in: optimize state data */ |
| 1478 |
dict_index_t *index, /*!< in: current FTS being optimized */ |
| 1479 |
std::chrono::steady_clock::time_point |
| 1480 |
start_time) /*!< in: optimize start time */ |
| 1481 |
{ |
| 1482 |
ulint i; |
| 1483 |
dberr_t error = DB_SUCCESS; |
| 1484 |
ulint size = ib_vector_size(optim->words); |
| 1485 |
|
| 1486 |
for (i = 0; i < size && error == DB_SUCCESS && !optim->done; ++i) { |
| 1487 |
fts_word_t *word; |
| 1488 |
ib_vector_t *nodes; |
| 1489 |
trx_t *trx = optim->trx; |
| 1490 |
|
| 1491 |
word = (fts_word_t *)ib_vector_get(optim->words, i); |
| 1492 |
|
| 1493 |
/* nodes is allocated from the word heap and will be destroyed |
| 1494 |
when the word is freed. We however have to be careful about |
| 1495 |
the ilist, that needs to be freed explicitly. */ |
| 1496 |
nodes = fts_optimize_word(optim, word); |
| 1497 |
|
| 1498 |
/* Update the data on disk. */ |
| 1499 |
error = fts_optimize_write_word(trx, &optim->fts_index_table, &word->text, |
| 1500 |
nodes); |
| 1501 |
|
| 1502 |
if (error == DB_SUCCESS) { |
| 1503 |
/* Write the last word optimized to the config table, |
| 1504 |
we use this value for restarting optimize. */ |
| 1505 |
error = fts_config_set_index_value(optim->trx, index, |
| 1506 |
FTS_LAST_OPTIMIZED_WORD, &word->text); |
| 1507 |
} |
| 1508 |
|
| 1509 |
/* Free the word that was optimized. */ |
| 1510 |
fts_word_free(word); |
| 1511 |
|
| 1512 |
if (fts_optimize_time_limit > std::chrono::seconds::zero() && |
| 1513 |
std::chrono::steady_clock::now() - start_time > |
| 1514 |
fts_optimize_time_limit) { |
| 1515 |
optim->done = true; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
return (error); |
| 1520 |
} |
| 1521 |
|
| 1522 |
/** Create an instance of fts_optimize_t. Also create a new |
| 1523 |
background transaction.*/ |
| 1524 |
static fts_optimize_t *fts_optimize_create( |
| 1525 |
dict_table_t *table) /*!< in: table with FTS indexes */ |
| 1526 |
{ |
| 1527 |
fts_optimize_t *optim; |
| 1528 |
mem_heap_t *heap = mem_heap_create(128, UT_LOCATION_HERE); |
| 1529 |
|
| 1530 |
optim = (fts_optimize_t *)mem_heap_zalloc(heap, sizeof(*optim)); |
| 1531 |
|
| 1532 |
optim->self_heap = ib_heap_allocator_create(heap); |
| 1533 |
|
| 1534 |
optim->to_delete = fts_doc_ids_create(); |
| 1535 |
|
| 1536 |
optim->words = ib_vector_create(optim->self_heap, sizeof(fts_word_t), 256); |
| 1537 |
|
| 1538 |
optim->table = table; |
| 1539 |
|
| 1540 |
optim->trx = trx_allocate_for_background(); |
| 1541 |
|
| 1542 |
optim->fts_common_table.parent = table->name.m_name; |
| 1543 |
optim->fts_common_table.table_id = table->id; |
| 1544 |
optim->fts_common_table.type = FTS_COMMON_TABLE; |
| 1545 |
optim->fts_common_table.table = table; |
| 1546 |
|
| 1547 |
optim->fts_index_table.parent = table->name.m_name; |
| 1548 |
optim->fts_index_table.table_id = table->id; |
| 1549 |
optim->fts_index_table.type = FTS_INDEX_TABLE; |
| 1550 |
optim->fts_index_table.table = table; |
| 1551 |
|
| 1552 |
/* The common prefix for all this parent table's aux tables. */ |
| 1553 |
optim->name_prefix = fts_get_table_name_prefix(&optim->fts_common_table); |
| 1554 |
|
| 1555 |
return (optim); |
| 1556 |
} |
| 1557 |
|
| 1558 |
#ifdef FTS_OPTIMIZE_DEBUG |
| 1559 |
/** Get optimize start time of an FTS index. |
| 1560 |
@return DB_SUCCESS if all OK else error code */ |
| 1561 |
[[nodiscard]] static dberr_t fts_optimize_get_index_start_time( |
| 1562 |
trx_t *trx, /*!< in: transaction */ |
| 1563 |
dict_index_t *index, /*!< in: FTS index */ |
| 1564 |
ib_time_t *start_time) /*!< out: time in secs */ |
| 1565 |
{ |
| 1566 |
return (fts_config_get_index_ulint(trx, index, FTS_OPTIMIZE_START_TIME, |
| 1567 |
(ulint *)start_time)); |
| 1568 |
} |
| 1569 |
|
| 1570 |
/** Set the optimize start time of an FTS index. |
| 1571 |
@return DB_SUCCESS if all OK else error code */ |
| 1572 |
[[nodiscard]] static dberr_t fts_optimize_set_index_start_time( |
| 1573 |
trx_t *trx, /*!< in: transaction */ |
| 1574 |
dict_index_t *index, /*!< in: FTS index */ |
| 1575 |
ib_time_t start_time) /*!< in: start time */ |
| 1576 |
{ |
| 1577 |
return (fts_config_set_index_ulint(trx, index, FTS_OPTIMIZE_START_TIME, |
| 1578 |
(ulint)start_time)); |
| 1579 |
} |
| 1580 |
|
| 1581 |
/** Get optimize end time of an FTS index. |
| 1582 |
@return DB_SUCCESS if all OK else error code */ |
| 1583 |
[[nodiscard]] static dberr_t fts_optimize_get_index_end_time( |
| 1584 |
trx_t *trx, /*!< in: transaction */ |
| 1585 |
dict_index_t *index, /*!< in: FTS index */ |
| 1586 |
ib_time_t *end_time) /*!< out: time in secs */ |
| 1587 |
{ |
| 1588 |
return (fts_config_get_index_ulint(trx, index, FTS_OPTIMIZE_END_TIME, |
| 1589 |
(ulint *)end_time)); |
| 1590 |
} |
| 1591 |
|
| 1592 |
/** Set the optimize end time of an FTS index. |
| 1593 |
@return DB_SUCCESS if all OK else error code */ |
| 1594 |
[[nodiscard]] static dberr_t fts_optimize_set_index_end_time( |
| 1595 |
trx_t *trx, /*!< in: transaction */ |
| 1596 |
dict_index_t *index, /*!< in: FTS index */ |
| 1597 |
ib_time_t end_time) /*!< in: end time */ |
| 1598 |
{ |
| 1599 |
return (fts_config_set_index_ulint(trx, index, FTS_OPTIMIZE_END_TIME, |
| 1600 |
(ulint)end_time)); |
| 1601 |
} |
| 1602 |
#endif |
| 1603 |
|
| 1604 |
/** Free the optimize prepared statements.*/ |
| 1605 |
static void fts_optimize_graph_free( |
| 1606 |
fts_optimize_graph_t *graph) /*!< in/out: The graph instances |
| 1607 |
to free */ |
| 1608 |
{ |
| 1609 |
if (graph->commit_graph) { |
| 1610 |
que_graph_free(graph->commit_graph); |
| 1611 |
graph->commit_graph = nullptr; |
| 1612 |
} |
| 1613 |
|
| 1614 |
if (graph->write_nodes_graph) { |
| 1615 |
que_graph_free(graph->write_nodes_graph); |
| 1616 |
graph->write_nodes_graph = nullptr; |
| 1617 |
} |
| 1618 |
|
| 1619 |
if (graph->delete_nodes_graph) { |
| 1620 |
que_graph_free(graph->delete_nodes_graph); |
| 1621 |
graph->delete_nodes_graph = nullptr; |
| 1622 |
} |
| 1623 |
|
| 1624 |
if (graph->read_nodes_graph) { |
| 1625 |
que_graph_free(graph->read_nodes_graph); |
| 1626 |
graph->read_nodes_graph = nullptr; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
/** Free all optimize resources. */ |
| 1631 |
static void fts_optimize_free( |
| 1632 |
fts_optimize_t *optim) /*!< in: table with on FTS index */ |
| 1633 |
{ |
| 1634 |
mem_heap_t *heap = static_cast<mem_heap_t *>(optim->self_heap->arg); |
| 1635 |
|
| 1636 |
trx_free_for_background(optim->trx); |
| 1637 |
|
| 1638 |
fts_doc_ids_free(optim->to_delete); |
| 1639 |
fts_optimize_graph_free(&optim->graph); |
| 1640 |
|
| 1641 |
ut::free(optim->name_prefix); |
| 1642 |
|
| 1643 |
/* This will free the heap from which optim itself was allocated. */ |
| 1644 |
mem_heap_free(heap); |
| 1645 |
} |
| 1646 |
|
| 1647 |
/** Get the max time optimize should run. |
| 1648 |
@return max optimize time limit. */ |
| 1649 |
static std::chrono::seconds fts_optimize_get_time_limit( |
| 1650 |
trx_t *trx, /*!< in: transaction */ |
| 1651 |
fts_table_t *fts_table) /*!< in: aux table */ |
| 1652 |
{ |
| 1653 |
ulint time_limit = 0; |
| 1654 |
|
| 1655 |
fts_config_get_ulint(trx, fts_table, FTS_OPTIMIZE_LIMIT_IN_SECS, |
| 1656 |
(ulint *)&time_limit); |
| 1657 |
|
| 1658 |
return std::chrono::seconds{time_limit}; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/** Run OPTIMIZE on the given table. Note: this can take a very long time |
| 1662 |
(hours). */ |
| 1663 |
static void fts_optimize_words( |
| 1664 |
fts_optimize_t *optim, /*!< in: optimize instance */ |
| 1665 |
dict_index_t *index, /*!< in: current FTS being optimized */ |
| 1666 |
fts_string_t *word) /*!< in: the starting word to optimize */ |
| 1667 |
{ |
| 1668 |
fts_fetch_t fetch; |
| 1669 |
que_t *graph = nullptr; |
| 1670 |
CHARSET_INFO *charset = optim->fts_index_table.charset; |
| 1671 |
|
| 1672 |
ut_a(!optim->done); |
| 1673 |
|
| 1674 |
/* Get the time limit from the config table. */ |
| 1675 |
fts_optimize_time_limit = |
| 1676 |
fts_optimize_get_time_limit(optim->trx, &optim->fts_common_table); |
| 1677 |
|
| 1678 |
const auto start_time = std::chrono::steady_clock::now(); |
| 1679 |
|
| 1680 |
/* Setup the callback to use for fetching the word ilist etc. */ |
| 1681 |
fetch.read_arg = optim->words; |
| 1682 |
fetch.read_record = fts_optimize_index_fetch_node; |
| 1683 |
|
| 1684 |
while (!optim->done) { |
| 1685 |
dberr_t error; |
| 1686 |
trx_t *trx = optim->trx; |
| 1687 |
ulint selected; |
| 1688 |
|
| 1689 |
ut_a(ib_vector_size(optim->words) == 0); |
| 1690 |
|
| 1691 |
selected = fts_select_index(charset, word->f_str, word->f_len); |
| 1692 |
|
| 1693 |
/* Read the index records to optimize. */ |
| 1694 |
fetch.total_memory = 0; |
| 1695 |
|
| 1696 |
error = fts_index_fetch_nodes(trx, &graph, &optim->fts_index_table, word, |
| 1697 |
&fetch); |
| 1698 |
ut_ad(fetch.total_memory < fts_result_cache_limit); |
| 1699 |
|
| 1700 |
if (error == DB_SUCCESS) { |
| 1701 |
/* There must be some nodes to read. */ |
| 1702 |
ut_a(ib_vector_size(optim->words) > 0); |
| 1703 |
|
| 1704 |
/* Optimize the nodes that were read and write |
| 1705 |
back to DB. */ |
| 1706 |
error = fts_optimize_compact(optim, index, start_time); |
| 1707 |
|
| 1708 |
if (error == DB_SUCCESS) { |
| 1709 |
fts_sql_commit(optim->trx); |
| 1710 |
} else { |
| 1711 |
fts_sql_rollback(optim->trx); |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
ib_vector_reset(optim->words); |
| 1716 |
|
| 1717 |
if (error == DB_SUCCESS) { |
| 1718 |
if (!optim->done) { |
| 1719 |
if (!fts_zip_read_word(optim->zip, word)) { |
| 1720 |
optim->done = true; |
| 1721 |
} else if (selected != |
| 1722 |
fts_select_index(charset, word->f_str, word->f_len) && |
| 1723 |
graph) { |
| 1724 |
fts_que_graph_free(graph); |
| 1725 |
graph = nullptr; |
| 1726 |
} |
| 1727 |
} |
| 1728 |
} else if (error == DB_LOCK_WAIT_TIMEOUT) { |
| 1729 |
ib::warn(ER_IB_MSG_495) << "Lock wait timeout during optimize." |
| 1730 |
" Retrying!"; |
| 1731 |
|
| 1732 |
trx->error_state = DB_SUCCESS; |
| 1733 |
} else if (error == DB_DEADLOCK) { |
| 1734 |
ib::warn(ER_IB_MSG_496) << "Deadlock during optimize. Retrying!"; |
| 1735 |
|
| 1736 |
trx->error_state = DB_SUCCESS; |
| 1737 |
} else { |
| 1738 |
optim->done = true; /* Exit the loop. */ |
| 1739 |
} |
| 1740 |
} |
| 1741 |
|
| 1742 |
if (graph != nullptr) { |
| 1743 |
fts_que_graph_free(graph); |
| 1744 |
} |
| 1745 |
} |
| 1746 |
|
| 1747 |
/** Optimize is complete. Set the completion time, and reset the optimize |
| 1748 |
start string for this FTS index to "". |
| 1749 |
@return DB_SUCCESS if all OK */ |
| 1750 |
[[nodiscard]] static dberr_t fts_optimize_index_completed( |
| 1751 |
fts_optimize_t *optim, /*!< in: optimize instance */ |
| 1752 |
dict_index_t *index) /*!< in: table with one FTS index */ |
| 1753 |
{ |
| 1754 |
fts_string_t word; |
| 1755 |
dberr_t error; |
| 1756 |
byte buf[sizeof(ulint)]; |
| 1757 |
#ifdef FTS_OPTIMIZE_DEBUG |
| 1758 |
ib_time_t end_time = ut_time(); |
| 1759 |
|
| 1760 |
error = fts_optimize_set_index_end_time(optim->trx, index, end_time); |
| 1761 |
#endif |
| 1762 |
|
| 1763 |
/* If we've reached the end of the index then set the start |
| 1764 |
word to the empty string. */ |
| 1765 |
|
| 1766 |
word.f_len = 0; |
| 1767 |
word.f_str = buf; |
| 1768 |
*word.f_str = '\0'; |
| 1769 |
|
| 1770 |
error = fts_config_set_index_value(optim->trx, index, FTS_LAST_OPTIMIZED_WORD, |
| 1771 |
&word); |
| 1772 |
|
| 1773 |
if (error != DB_SUCCESS) { |
| 1774 |
ib::error(ER_IB_MSG_497) << "(" << ut_strerr(error) |
| 1775 |
<< ") while updating" |
| 1776 |
" last optimized word!"; |
| 1777 |
} |
| 1778 |
|
| 1779 |
return (error); |
| 1780 |
} |
| 1781 |
|
| 1782 |
/** Read the list of words from the FTS auxiliary index that will be |
| 1783 |
optimized in this pass. |
| 1784 |
@return DB_SUCCESS if all OK */ |
| 1785 |
[[nodiscard]] static dberr_t fts_optimize_index_read_words( |
| 1786 |
fts_optimize_t *optim, /*!< in: optimize instance */ |
| 1787 |
dict_index_t *index, /*!< in: table with one FTS index */ |
| 1788 |
fts_string_t *word) /*!< in: buffer to use */ |
| 1789 |
{ |
| 1790 |
dberr_t error = DB_SUCCESS; |
| 1791 |
|
| 1792 |
if (optim->del_list_regenerated) { |
| 1793 |
word->f_len = 0; |
| 1794 |
} else { |
| 1795 |
/* Get the last word that was optimized from |
| 1796 |
the config table. */ |
| 1797 |
error = fts_config_get_index_value(optim->trx, index, |
| 1798 |
FTS_LAST_OPTIMIZED_WORD, word); |
| 1799 |
} |
| 1800 |
|
| 1801 |
/* If record not found then we start from the top. */ |
| 1802 |
if (error == DB_RECORD_NOT_FOUND) { |
| 1803 |
word->f_len = 0; |
| 1804 |
error = DB_SUCCESS; |
| 1805 |
} |
| 1806 |
|
| 1807 |
while (error == DB_SUCCESS) { |
| 1808 |
error = fts_index_fetch_words(optim, word, fts_num_word_optimize); |
| 1809 |
|
| 1810 |
if (error == DB_SUCCESS) { |
| 1811 |
/* Reset the last optimized word to '' if no |
| 1812 |
more words could be read from the FTS index. */ |
| 1813 |
if (optim->zip->n_words == 0) { |
| 1814 |
word->f_len = 0; |
| 1815 |
*word->f_str = 0; |
| 1816 |
} |
| 1817 |
|
| 1818 |
break; |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
return (error); |
| 1823 |
} |
| 1824 |
|
| 1825 |
/** Run OPTIMIZE on the given FTS index. Note: this can take a very long |
| 1826 |
time (hours). |
| 1827 |
@return DB_SUCCESS if all OK */ |
| 1828 |
[[nodiscard]] static dberr_t fts_optimize_index( |
| 1829 |
fts_optimize_t *optim, /*!< in: optimize instance */ |
| 1830 |
dict_index_t *index) /*!< in: table with one FTS index */ |
| 1831 |
{ |
| 1832 |
fts_string_t word; |
| 1833 |
dberr_t error; |
| 1834 |
byte str[FTS_MAX_WORD_LEN + 1]; |
| 1835 |
|
| 1836 |
/* Set the current index that we have to optimize. */ |
| 1837 |
optim->fts_index_table.index_id = index->id; |
| 1838 |
optim->fts_index_table.charset = fts_index_get_charset(index); |
| 1839 |
|
| 1840 |
optim->done = false; /* Optimize until !done */ |
| 1841 |
|
| 1842 |
/* We need to read the last word optimized so that we start from |
| 1843 |
the next word. */ |
| 1844 |
word.f_str = str; |
| 1845 |
|
| 1846 |
/* We set the length of word to the size of str since we |
| 1847 |
need to pass the max len info to the fts_get_config_value() function. */ |
| 1848 |
word.f_len = sizeof(str) - 1; |
| 1849 |
|
| 1850 |
memset(word.f_str, 0x0, word.f_len); |
| 1851 |
|
| 1852 |
/* Read the words that will be optimized in this pass. */ |
| 1853 |
error = fts_optimize_index_read_words(optim, index, &word); |
| 1854 |
|
| 1855 |
if (error == DB_SUCCESS) { |
| 1856 |
int zip_error; |
| 1857 |
|
| 1858 |
ut_a(optim->zip->pos == 0); |
| 1859 |
ut_a(optim->zip->zp->total_in == 0); |
| 1860 |
ut_a(optim->zip->zp->total_out == 0); |
| 1861 |
|
| 1862 |
zip_error = inflateInit(optim->zip->zp); |
| 1863 |
ut_a(zip_error == Z_OK); |
| 1864 |
|
| 1865 |
word.f_len = 0; |
| 1866 |
word.f_str = str; |
| 1867 |
|
| 1868 |
/* Read the first word to optimize from the Zip buffer. */ |
| 1869 |
if (!fts_zip_read_word(optim->zip, &word)) { |
| 1870 |
optim->done = true; |
| 1871 |
} else { |
| 1872 |
fts_optimize_words(optim, index, &word); |
| 1873 |
} |
| 1874 |
|
| 1875 |
/* If we couldn't read any records then optimize is |
| 1876 |
complete. Increment the number of indexes that have |
| 1877 |
been optimized and set FTS index optimize state to |
| 1878 |
completed. */ |
| 1879 |
if (error == DB_SUCCESS && optim->zip->n_words == 0) { |
| 1880 |
error = fts_optimize_index_completed(optim, index); |
| 1881 |
|
| 1882 |
if (error == DB_SUCCESS) { |
| 1883 |
++optim->n_completed; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
return (error); |
| 1889 |
} |
| 1890 |
|
| 1891 |
/** Delete the document ids in the delete, and delete cache tables. |
| 1892 |
@return DB_SUCCESS if all OK */ |
| 1893 |
[[nodiscard]] static dberr_t fts_optimize_purge_deleted_doc_ids( |
| 1894 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 1895 |
{ |
| 1896 |
ulint i; |
| 1897 |
pars_info_t *info; |
| 1898 |
que_t *graph; |
| 1899 |
fts_update_t *update; |
| 1900 |
doc_id_t write_doc_id; |
| 1901 |
dberr_t error = DB_SUCCESS; |
| 1902 |
char deleted[MAX_FULL_NAME_LEN]; |
| 1903 |
char deleted_cache[MAX_FULL_NAME_LEN]; |
| 1904 |
dict_table_t *deleted_tbl = nullptr; |
| 1905 |
dict_table_t *deleted_cache_tbl = nullptr; |
| 1906 |
MDL_ticket *deleted_cache_mdl = nullptr; |
| 1907 |
MDL_ticket *deleted_mdl = nullptr; |
| 1908 |
THD *thd = current_thd; |
| 1909 |
|
| 1910 |
info = pars_info_create(); |
| 1911 |
|
| 1912 |
ut_a(ib_vector_size(optim->to_delete->doc_ids) > 0); |
| 1913 |
|
| 1914 |
update = |
| 1915 |
static_cast<fts_update_t *>(ib_vector_get(optim->to_delete->doc_ids, 0)); |
| 1916 |
|
| 1917 |
/* Convert to "storage" byte order. */ |
| 1918 |
fts_write_doc_id((byte *)&write_doc_id, update->doc_id); |
| 1919 |
|
| 1920 |
/* This is required for the SQL parser to work. It must be able |
| 1921 |
to find the following variables. So we do it twice. */ |
| 1922 |
fts_bind_doc_id(info, "doc_id1", &write_doc_id); |
| 1923 |
fts_bind_doc_id(info, "doc_id2", &write_doc_id); |
| 1924 |
|
| 1925 |
/* Make sure the following two names are consistent with the name |
| 1926 |
used in the fts_delete_doc_ids_sql */ |
| 1927 |
optim->fts_common_table.suffix = FTS_SUFFIX_DELETED; |
| 1928 |
fts_get_table_name(&optim->fts_common_table, deleted); |
| 1929 |
pars_info_bind_id(info, true, FTS_SUFFIX_DELETED, deleted); |
| 1930 |
|
| 1931 |
optim->fts_common_table.suffix = FTS_SUFFIX_DELETED_CACHE; |
| 1932 |
fts_get_table_name(&optim->fts_common_table, deleted_cache); |
| 1933 |
pars_info_bind_id(info, true, FTS_SUFFIX_DELETED_CACHE, deleted_cache); |
| 1934 |
|
| 1935 |
deleted_tbl = dd_table_open_on_name(thd, &deleted_mdl, deleted, false, |
| 1936 |
DICT_ERR_IGNORE_NONE); |
| 1937 |
|
| 1938 |
if (deleted_tbl == nullptr) { |
| 1939 |
goto func_exit; |
| 1940 |
} |
| 1941 |
|
| 1942 |
deleted_cache_tbl = dd_table_open_on_name( |
| 1943 |
thd, &deleted_cache_mdl, deleted_cache, false, DICT_ERR_IGNORE_NONE); |
| 1944 |
|
| 1945 |
if (deleted_cache_tbl == nullptr) { |
| 1946 |
goto func_exit; |
| 1947 |
} |
| 1948 |
|
| 1949 |
graph = fts_parse_sql(nullptr, info, fts_delete_doc_ids_sql); |
| 1950 |
|
| 1951 |
/* Delete the doc ids that were copied at the start. */ |
| 1952 |
for (i = 0; i < ib_vector_size(optim->to_delete->doc_ids); ++i) { |
| 1953 |
update = static_cast<fts_update_t *>( |
| 1954 |
ib_vector_get(optim->to_delete->doc_ids, i)); |
| 1955 |
|
| 1956 |
/* Convert to "storage" byte order. */ |
| 1957 |
fts_write_doc_id((byte *)&write_doc_id, update->doc_id); |
| 1958 |
|
| 1959 |
fts_bind_doc_id(info, "doc_id1", &write_doc_id); |
| 1960 |
|
| 1961 |
fts_bind_doc_id(info, "doc_id2", &write_doc_id); |
| 1962 |
|
| 1963 |
error = fts_eval_sql(optim->trx, graph); |
| 1964 |
|
| 1965 |
// FIXME: Check whether delete actually succeeded! |
| 1966 |
if (error != DB_SUCCESS) { |
| 1967 |
fts_sql_rollback(optim->trx); |
| 1968 |
break; |
| 1969 |
} |
| 1970 |
} |
| 1971 |
|
| 1972 |
fts_que_graph_free(graph); |
| 1973 |
|
| 1974 |
func_exit: |
| 1975 |
if (deleted_cache_tbl != nullptr) { |
| 1976 |
dd_table_close(deleted_cache_tbl, thd, &deleted_cache_mdl, false); |
| 1977 |
} |
| 1978 |
|
| 1979 |
if (deleted_tbl != nullptr) { |
| 1980 |
dd_table_close(deleted_tbl, thd, &deleted_mdl, false); |
| 1981 |
} |
| 1982 |
|
| 1983 |
return (error); |
| 1984 |
} |
| 1985 |
|
| 1986 |
/** Delete the document ids in the pending delete, and delete tables. |
| 1987 |
@return DB_SUCCESS if all OK */ |
| 1988 |
[[nodiscard]] static dberr_t fts_optimize_purge_deleted_doc_id_snapshot( |
| 1989 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 1990 |
{ |
| 1991 |
dberr_t error = DB_SUCCESS; |
| 1992 |
que_t *graph; |
| 1993 |
pars_info_t *info; |
| 1994 |
char being_deleted[MAX_FULL_NAME_LEN]; |
| 1995 |
char being_deleted_cache[MAX_FULL_NAME_LEN]; |
| 1996 |
MDL_ticket *being_deleted_mdl = nullptr; |
| 1997 |
MDL_ticket *being_deleted_cache_mdl = nullptr; |
| 1998 |
dict_table_t *being_deleted_cache_tbl = nullptr; |
| 1999 |
dict_table_t *being_deleted_tbl = nullptr; |
| 2000 |
THD *thd = current_thd; |
| 2001 |
|
| 2002 |
info = pars_info_create(); |
| 2003 |
|
| 2004 |
/* Make sure the following two names are consistent with the name |
| 2005 |
used in the fts_end_delete_sql */ |
| 2006 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED; |
| 2007 |
fts_get_table_name(&optim->fts_common_table, being_deleted); |
| 2008 |
pars_info_bind_id(info, true, FTS_SUFFIX_BEING_DELETED, being_deleted); |
| 2009 |
|
| 2010 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED_CACHE; |
| 2011 |
fts_get_table_name(&optim->fts_common_table, being_deleted_cache); |
| 2012 |
pars_info_bind_id(info, true, FTS_SUFFIX_BEING_DELETED_CACHE, |
| 2013 |
being_deleted_cache); |
| 2014 |
|
| 2015 |
being_deleted_tbl = dd_table_open_on_name( |
| 2016 |
thd, &being_deleted_mdl, being_deleted, false, DICT_ERR_IGNORE_NONE); |
| 2017 |
|
| 2018 |
if (being_deleted_tbl == nullptr) { |
| 2019 |
error = DB_ERROR; |
| 2020 |
goto func_exit; |
| 2021 |
} |
| 2022 |
|
| 2023 |
being_deleted_cache_tbl = |
| 2024 |
dd_table_open_on_name(thd, &being_deleted_cache_mdl, being_deleted_cache, |
| 2025 |
false, DICT_ERR_IGNORE_NONE); |
| 2026 |
|
| 2027 |
if (being_deleted_cache_tbl == nullptr) { |
| 2028 |
error = DB_ERROR; |
| 2029 |
goto func_exit; |
| 2030 |
} |
| 2031 |
|
| 2032 |
/* Delete the doc ids that were copied to delete pending state at |
| 2033 |
the start of optimize. */ |
| 2034 |
graph = fts_parse_sql(nullptr, info, fts_end_delete_sql); |
| 2035 |
|
| 2036 |
error = fts_eval_sql(optim->trx, graph); |
| 2037 |
fts_que_graph_free(graph); |
| 2038 |
|
| 2039 |
func_exit: |
| 2040 |
if (being_deleted_cache_tbl != nullptr) { |
| 2041 |
dd_table_close(being_deleted_cache_tbl, thd, &being_deleted_cache_mdl, |
| 2042 |
false); |
| 2043 |
} |
| 2044 |
|
| 2045 |
if (being_deleted_tbl != nullptr) { |
| 2046 |
dd_table_close(being_deleted_tbl, thd, &being_deleted_mdl, false); |
| 2047 |
} |
| 2048 |
|
| 2049 |
return (error); |
| 2050 |
} |
| 2051 |
|
| 2052 |
/** Copy the deleted doc ids that will be purged during this optimize run |
| 2053 |
to the being deleted FTS auxiliary tables. The transaction is committed |
| 2054 |
upon successful copy and rolled back on DB_DUPLICATE_KEY error. |
| 2055 |
@return DB_SUCCESS if all OK */ |
| 2056 |
static ulint fts_optimize_being_deleted_count( |
| 2057 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2058 |
{ |
| 2059 |
fts_table_t fts_table; |
| 2060 |
|
| 2061 |
FTS_INIT_FTS_TABLE(&fts_table, FTS_SUFFIX_BEING_DELETED, FTS_COMMON_TABLE, |
| 2062 |
optim->table); |
| 2063 |
|
| 2064 |
return (fts_get_rows_count(&fts_table)); |
| 2065 |
} |
| 2066 |
|
| 2067 |
/** Copy the deleted doc ids that will be purged during this optimize run |
| 2068 |
to the being deleted FTS auxiliary tables. The transaction is committed |
| 2069 |
upon successful copy and rolled back on DB_DUPLICATE_KEY error. |
| 2070 |
@return DB_SUCCESS if all OK */ |
| 2071 |
[[nodiscard]] static dberr_t fts_optimize_create_deleted_doc_id_snapshot( |
| 2072 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2073 |
{ |
| 2074 |
dberr_t error = DB_SUCCESS; |
| 2075 |
que_t *graph; |
| 2076 |
pars_info_t *info; |
| 2077 |
char being_deleted[MAX_FULL_NAME_LEN]; |
| 2078 |
char deleted[MAX_FULL_NAME_LEN]; |
| 2079 |
char being_deleted_cache[MAX_FULL_NAME_LEN]; |
| 2080 |
char deleted_cache[MAX_FULL_NAME_LEN]; |
| 2081 |
dict_table_t *being_deleted_tbl = nullptr; |
| 2082 |
dict_table_t *deleted_tbl = nullptr; |
| 2083 |
dict_table_t *being_deleted_cache_tbl = nullptr; |
| 2084 |
dict_table_t *deleted_cache_tbl = nullptr; |
| 2085 |
MDL_ticket *being_deleted_mdl = nullptr; |
| 2086 |
MDL_ticket *deleted_mdl = nullptr; |
| 2087 |
MDL_ticket *being_deleted_cache_mdl = nullptr; |
| 2088 |
MDL_ticket *deleted_cache_mdl = nullptr; |
| 2089 |
THD *thd = current_thd; |
| 2090 |
|
| 2091 |
info = pars_info_create(); |
| 2092 |
|
| 2093 |
/* Make sure the following four names are consistent with the name |
| 2094 |
used in the fts_init_delete_sql */ |
| 2095 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED; |
| 2096 |
fts_get_table_name(&optim->fts_common_table, being_deleted); |
| 2097 |
pars_info_bind_id(info, true, FTS_SUFFIX_BEING_DELETED, being_deleted); |
| 2098 |
|
| 2099 |
being_deleted_tbl = dd_table_open_on_name( |
| 2100 |
thd, &being_deleted_mdl, being_deleted, false, DICT_ERR_IGNORE_NONE); |
| 2101 |
|
| 2102 |
if (being_deleted_tbl == nullptr) { |
| 2103 |
goto func_exit; |
| 2104 |
} |
| 2105 |
|
| 2106 |
optim->fts_common_table.suffix = FTS_SUFFIX_DELETED; |
| 2107 |
fts_get_table_name(&optim->fts_common_table, deleted); |
| 2108 |
pars_info_bind_id(info, true, FTS_SUFFIX_DELETED, deleted); |
| 2109 |
|
| 2110 |
deleted_tbl = dd_table_open_on_name(thd, &deleted_mdl, deleted, false, |
| 2111 |
DICT_ERR_IGNORE_NONE); |
| 2112 |
|
| 2113 |
if (deleted_tbl == nullptr) { |
| 2114 |
goto func_exit; |
| 2115 |
} |
| 2116 |
|
| 2117 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED_CACHE; |
| 2118 |
fts_get_table_name(&optim->fts_common_table, being_deleted_cache); |
| 2119 |
pars_info_bind_id(info, true, FTS_SUFFIX_BEING_DELETED_CACHE, |
| 2120 |
being_deleted_cache); |
| 2121 |
|
| 2122 |
being_deleted_cache_tbl = |
| 2123 |
dd_table_open_on_name(thd, &being_deleted_cache_mdl, being_deleted_cache, |
| 2124 |
false, DICT_ERR_IGNORE_NONE); |
| 2125 |
|
| 2126 |
if (being_deleted_cache_tbl == nullptr) { |
| 2127 |
goto func_exit; |
| 2128 |
} |
| 2129 |
|
| 2130 |
optim->fts_common_table.suffix = FTS_SUFFIX_DELETED_CACHE; |
| 2131 |
fts_get_table_name(&optim->fts_common_table, deleted_cache); |
| 2132 |
pars_info_bind_id(info, true, FTS_SUFFIX_DELETED_CACHE, deleted_cache); |
| 2133 |
|
| 2134 |
deleted_cache_tbl = dd_table_open_on_name( |
| 2135 |
thd, &deleted_cache_mdl, deleted_cache, false, DICT_ERR_IGNORE_NONE); |
| 2136 |
|
| 2137 |
if (deleted_cache_tbl == nullptr) { |
| 2138 |
goto func_exit; |
| 2139 |
} |
| 2140 |
|
| 2141 |
/* Move doc_ids that are to be deleted to state being deleted. */ |
| 2142 |
graph = fts_parse_sql(nullptr, info, fts_init_delete_sql); |
| 2143 |
|
| 2144 |
error = fts_eval_sql(optim->trx, graph); |
| 2145 |
|
| 2146 |
fts_que_graph_free(graph); |
| 2147 |
|
| 2148 |
if (error != DB_SUCCESS) { |
| 2149 |
fts_sql_rollback(optim->trx); |
| 2150 |
} else { |
| 2151 |
fts_sql_commit(optim->trx); |
| 2152 |
} |
| 2153 |
|
| 2154 |
optim->del_list_regenerated = true; |
| 2155 |
|
| 2156 |
func_exit: |
| 2157 |
if (being_deleted_tbl != nullptr) { |
| 2158 |
dd_table_close(being_deleted_tbl, thd, &being_deleted_mdl, false); |
| 2159 |
} |
| 2160 |
|
| 2161 |
if (deleted_tbl != nullptr) { |
| 2162 |
dd_table_close(deleted_tbl, thd, &deleted_mdl, false); |
| 2163 |
} |
| 2164 |
|
| 2165 |
if (being_deleted_cache_tbl != nullptr) { |
| 2166 |
dd_table_close(being_deleted_cache_tbl, thd, &being_deleted_cache_mdl, |
| 2167 |
false); |
| 2168 |
} |
| 2169 |
|
| 2170 |
if (deleted_cache_tbl != nullptr) { |
| 2171 |
dd_table_close(deleted_cache_tbl, thd, &deleted_cache_mdl, false); |
| 2172 |
} |
| 2173 |
|
| 2174 |
return (error); |
| 2175 |
} |
| 2176 |
|
| 2177 |
/** Read in the document ids that are to be purged during optimize. The |
| 2178 |
transaction is committed upon successfully read. |
| 2179 |
@return DB_SUCCESS if all OK */ |
| 2180 |
[[nodiscard]] static dberr_t fts_optimize_read_deleted_doc_id_snapshot( |
| 2181 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2182 |
{ |
| 2183 |
dberr_t error; |
| 2184 |
|
| 2185 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED; |
| 2186 |
|
| 2187 |
/* Read the doc_ids to delete. */ |
| 2188 |
error = fts_table_fetch_doc_ids(optim->trx, &optim->fts_common_table, |
| 2189 |
optim->to_delete); |
| 2190 |
|
| 2191 |
if (error == DB_SUCCESS) { |
| 2192 |
optim->fts_common_table.suffix = FTS_SUFFIX_BEING_DELETED; |
| 2193 |
|
| 2194 |
/* Read additional doc_ids to delete. */ |
| 2195 |
error = fts_table_fetch_doc_ids(optim->trx, &optim->fts_common_table, |
| 2196 |
optim->to_delete); |
| 2197 |
} |
| 2198 |
|
| 2199 |
if (error != DB_SUCCESS) { |
| 2200 |
fts_doc_ids_free(optim->to_delete); |
| 2201 |
optim->to_delete = nullptr; |
| 2202 |
} |
| 2203 |
|
| 2204 |
return (error); |
| 2205 |
} |
| 2206 |
|
| 2207 |
/** Optimize all the FTS indexes, skipping those that have already been |
| 2208 |
optimized, since the FTS auxiliary indexes are not guaranteed to be |
| 2209 |
of the same cardinality. |
| 2210 |
@return DB_SUCCESS if all OK */ |
| 2211 |
[[nodiscard]] static dberr_t fts_optimize_indexes( |
| 2212 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2213 |
{ |
| 2214 |
ulint i; |
| 2215 |
dberr_t error = DB_SUCCESS; |
| 2216 |
fts_t *fts = optim->table->fts; |
| 2217 |
|
| 2218 |
/* Optimize the FTS indexes. */ |
| 2219 |
for (i = 0; i < ib_vector_size(fts->indexes); ++i) { |
| 2220 |
dict_index_t *index; |
| 2221 |
|
| 2222 |
#ifdef FTS_OPTIMIZE_DEBUG |
| 2223 |
ib_time_t end_time; |
| 2224 |
ib_time_t start_time; |
| 2225 |
|
| 2226 |
/* Get the start and end optimize times for this index. */ |
| 2227 |
error = fts_optimize_get_index_start_time(optim->trx, index, &start_time); |
| 2228 |
|
| 2229 |
if (error != DB_SUCCESS) { |
| 2230 |
break; |
| 2231 |
} |
| 2232 |
|
| 2233 |
error = fts_optimize_get_index_end_time(optim->trx, index, &end_time); |
| 2234 |
|
| 2235 |
if (error != DB_SUCCESS) { |
| 2236 |
break; |
| 2237 |
} |
| 2238 |
|
| 2239 |
/* Start time will be 0 only for the first time or after |
| 2240 |
completing the optimization of all FTS indexes. */ |
| 2241 |
if (start_time == 0) { |
| 2242 |
start_time = ut_time(); |
| 2243 |
|
| 2244 |
error = fts_optimize_set_index_start_time(optim->trx, index, start_time); |
| 2245 |
} |
| 2246 |
|
| 2247 |
/* Check if this index needs to be optimized or not. */ |
| 2248 |
if (ut_difftime(end_time, start_time) < 0) { |
| 2249 |
error = fts_optimize_index(optim, index); |
| 2250 |
|
| 2251 |
if (error != DB_SUCCESS) { |
| 2252 |
break; |
| 2253 |
} |
| 2254 |
} else { |
| 2255 |
++optim->n_completed; |
| 2256 |
} |
| 2257 |
#endif |
| 2258 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 2259 |
error = fts_optimize_index(optim, index); |
| 2260 |
} |
| 2261 |
|
| 2262 |
if (error == DB_SUCCESS) { |
| 2263 |
fts_sql_commit(optim->trx); |
| 2264 |
} else { |
| 2265 |
fts_sql_rollback(optim->trx); |
| 2266 |
} |
| 2267 |
|
| 2268 |
return (error); |
| 2269 |
} |
| 2270 |
|
| 2271 |
/** Cleanup the snapshot tables and the master deleted table. |
| 2272 |
@return DB_SUCCESS if all OK */ |
| 2273 |
[[nodiscard]] static dberr_t fts_optimize_purge_snapshot( |
| 2274 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2275 |
{ |
| 2276 |
dberr_t error; |
| 2277 |
|
| 2278 |
/* Delete the doc ids from the master deleted tables, that were |
| 2279 |
in the snapshot that was taken at the start of optimize. */ |
| 2280 |
error = fts_optimize_purge_deleted_doc_ids(optim); |
| 2281 |
|
| 2282 |
if (error == DB_SUCCESS) { |
| 2283 |
/* Destroy the deleted doc id snapshot. */ |
| 2284 |
error = fts_optimize_purge_deleted_doc_id_snapshot(optim); |
| 2285 |
} |
| 2286 |
|
| 2287 |
if (error == DB_SUCCESS) { |
| 2288 |
fts_sql_commit(optim->trx); |
| 2289 |
} else { |
| 2290 |
fts_sql_rollback(optim->trx); |
| 2291 |
} |
| 2292 |
|
| 2293 |
return (error); |
| 2294 |
} |
| 2295 |
|
| 2296 |
/** Reset the start time to 0 so that a new optimize can be started. |
| 2297 |
@return DB_SUCCESS if all OK */ |
| 2298 |
[[nodiscard]] static dberr_t fts_optimize_reset_start_time( |
| 2299 |
fts_optimize_t *optim) /*!< in: optimize instance */ |
| 2300 |
{ |
| 2301 |
dberr_t error = DB_SUCCESS; |
| 2302 |
#ifdef FTS_OPTIMIZE_DEBUG |
| 2303 |
fts_t *fts = optim->table->fts; |
| 2304 |
|
| 2305 |
/* Optimization should have been completed for all indexes. */ |
| 2306 |
ut_a(optim->n_completed == ib_vector_size(fts->indexes)); |
| 2307 |
|
| 2308 |
for (uint i = 0; i < ib_vector_size(fts->indexes); ++i) { |
| 2309 |
dict_index_t *index; |
| 2310 |
|
| 2311 |
ib_time_t start_time = 0; |
| 2312 |
|
| 2313 |
/* Reset the start time to 0 for this index. */ |
| 2314 |
error = fts_optimize_set_index_start_time(optim->trx, index, start_time); |
| 2315 |
|
| 2316 |
index = static_cast<dict_index_t *>(ib_vector_getp(fts->indexes, i)); |
| 2317 |
} |
| 2318 |
#endif |
| 2319 |
|
| 2320 |
if (error == DB_SUCCESS) { |
| 2321 |
fts_sql_commit(optim->trx); |
| 2322 |
} else { |
| 2323 |
fts_sql_rollback(optim->trx); |
| 2324 |
} |
| 2325 |
|
| 2326 |
return (error); |
| 2327 |
} |
| 2328 |
|
| 2329 |
/** Run OPTIMIZE on the given table by a background thread. |
| 2330 |
@return DB_SUCCESS if all OK */ |
| 2331 |
static dberr_t fts_optimize_table_bk( |
| 2332 |
fts_slot_t *slot) /*!< in: table to optimize */ |
| 2333 |
{ |
| 2334 |
dberr_t error = DB_SUCCESS; |
| 2335 |
|
| 2336 |
/* Avoid optimizing tables that were optimized recently. */ |
| 2337 |
if (slot->last_run != std::chrono::steady_clock::time_point{} && |
| 2338 |
std::chrono::steady_clock::now() - slot->last_run < slot->interval_time) { |
| 2339 |
return (DB_SUCCESS); |
| 2340 |
|
| 2341 |
} else { |
| 2342 |
dict_table_t *table = nullptr; |
| 2343 |
MDL_ticket *mdl = nullptr; |
| 2344 |
THD *thd = current_thd; |
| 2345 |
|
| 2346 |
table = dd_table_open_on_id(slot->table_id, thd, &mdl, false, true); |
| 2347 |
|
| 2348 |
if (table != nullptr) { |
| 2349 |
fts_t *fts = table->fts; |
| 2350 |
|
| 2351 |
if (fts != nullptr && fts->cache != nullptr && |
| 2352 |
fts->cache->deleted >= FTS_OPTIMIZE_THRESHOLD) { |
| 2353 |
error = fts_optimize_table(table); |
| 2354 |
|
| 2355 |
if (error == DB_SUCCESS) { |
| 2356 |
slot->state = FTS_STATE_DONE; |
| 2357 |
slot->last_run = {}; |
| 2358 |
slot->completed = std::chrono::steady_clock::now(); |
| 2359 |
} |
| 2360 |
} |
| 2361 |
|
| 2362 |
dd_table_close(table, thd, &mdl, false); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* Note time this run completed. */ |
| 2367 |
slot->last_run = std::chrono::steady_clock::now(); |
| 2368 |
|
| 2369 |
return (error); |
| 2370 |
} |
| 2371 |
/** Run OPTIMIZE on the given table. |
| 2372 |
@return DB_SUCCESS if all OK */ |
| 2373 |
dberr_t fts_optimize_table(dict_table_t *table) /*!< in: table to optimize */ |
| 2374 |
{ |
| 2375 |
dberr_t error = DB_SUCCESS; |
| 2376 |
fts_optimize_t *optim = nullptr; |
| 2377 |
fts_t *fts = table->fts; |
| 2378 |
|
| 2379 |
if (fts_enable_diag_print) { |
| 2380 |
ib::info(ER_IB_MSG_498) << "FTS start optimize " << table->name; |
| 2381 |
} |
| 2382 |
|
| 2383 |
optim = fts_optimize_create(table); |
| 2384 |
|
| 2385 |
// FIXME: Call this only at the start of optimize, currently we |
| 2386 |
// rely on DB_DUPLICATE_KEY to handle corrupting the snapshot. |
| 2387 |
|
| 2388 |
/* Check whether there are still records in BEING_DELETED table */ |
| 2389 |
if (fts_optimize_being_deleted_count(optim) == 0) { |
| 2390 |
/* Take a snapshot of the deleted document ids, they are copied |
| 2391 |
to the BEING_ tables. */ |
| 2392 |
error = fts_optimize_create_deleted_doc_id_snapshot(optim); |
| 2393 |
} |
| 2394 |
|
| 2395 |
/* A duplicate error is OK, since we don't erase the |
| 2396 |
doc ids from the being deleted state until all FTS |
| 2397 |
indexes have been optimized. */ |
| 2398 |
if (error == DB_DUPLICATE_KEY) { |
| 2399 |
error = DB_SUCCESS; |
| 2400 |
} |
| 2401 |
|
| 2402 |
if (error == DB_SUCCESS) { |
| 2403 |
/* These document ids will be filtered out during the |
| 2404 |
index optimization phase. They are in the snapshot that we |
| 2405 |
took above, at the start of the optimize. */ |
| 2406 |
error = fts_optimize_read_deleted_doc_id_snapshot(optim); |
| 2407 |
|
| 2408 |
if (error == DB_SUCCESS) { |
| 2409 |
/* Commit the read of being deleted |
| 2410 |
doc ids transaction. */ |
| 2411 |
fts_sql_commit(optim->trx); |
| 2412 |
|
| 2413 |
/* We would do optimization only if there |
| 2414 |
are deleted records to be cleaned up */ |
| 2415 |
if (ib_vector_size(optim->to_delete->doc_ids) > 0) { |
| 2416 |
error = fts_optimize_indexes(optim); |
| 2417 |
} |
| 2418 |
|
| 2419 |
} else { |
| 2420 |
ut_a(optim->to_delete == nullptr); |
| 2421 |
} |
| 2422 |
|
| 2423 |
/* Only after all indexes have been optimized can we |
| 2424 |
delete the (snapshot) doc ids in the pending delete, |
| 2425 |
and master deleted tables. */ |
| 2426 |
if (error == DB_SUCCESS && |
| 2427 |
optim->n_completed == ib_vector_size(fts->indexes)) { |
| 2428 |
if (fts_enable_diag_print) { |
| 2429 |
ib::info(ER_IB_MSG_499) << "FTS_OPTIMIZE: Completed" |
| 2430 |
" Optimize, cleanup DELETED table"; |
| 2431 |
} |
| 2432 |
|
| 2433 |
if (ib_vector_size(optim->to_delete->doc_ids) > 0) { |
| 2434 |
/* Purge the doc ids that were in the |
| 2435 |
snapshot from the snapshot tables and |
| 2436 |
the master deleted table. */ |
| 2437 |
error = fts_optimize_purge_snapshot(optim); |
| 2438 |
} |
| 2439 |
|
| 2440 |
if (error == DB_SUCCESS) { |
| 2441 |
/* Reset the start time of all the FTS indexes |
| 2442 |
so that optimize can be restarted. */ |
| 2443 |
error = fts_optimize_reset_start_time(optim); |
| 2444 |
} |
| 2445 |
} |
| 2446 |
} |
| 2447 |
|
| 2448 |
fts_optimize_free(optim); |
| 2449 |
|
| 2450 |
if (fts_enable_diag_print) { |
| 2451 |
ib::info(ER_IB_MSG_500) << "FTS end optimize " << table->name; |
| 2452 |
} |
| 2453 |
|
| 2454 |
return (error); |
| 2455 |
} |
| 2456 |
|
| 2457 |
/** Add the table to add to the OPTIMIZER's list. |
| 2458 |
@return new message instance */ |
| 2459 |
static fts_msg_t *fts_optimize_create_msg( |
| 2460 |
fts_msg_type_t type, /*!< in: type of message */ |
| 2461 |
void *ptr) /*!< in: message payload */ |
| 2462 |
{ |
| 2463 |
mem_heap_t *heap; |
| 2464 |
fts_msg_t *msg; |
| 2465 |
|
| 2466 |
heap = mem_heap_create(sizeof(*msg) + sizeof(ib_list_node_t) + 16, |
| 2467 |
UT_LOCATION_HERE); |
| 2468 |
msg = static_cast<fts_msg_t *>(mem_heap_alloc(heap, sizeof(*msg))); |
| 2469 |
|
| 2470 |
msg->ptr = ptr; |
| 2471 |
msg->type = type; |
| 2472 |
msg->heap = heap; |
| 2473 |
|
| 2474 |
return (msg); |
| 2475 |
} |
| 2476 |
|
| 2477 |
/** Add the table to add to the OPTIMIZER's list. */ |
| 2478 |
void fts_optimize_add_table(dict_table_t *table) /*!< in: table to add */ |
| 2479 |
{ |
| 2480 |
fts_msg_t *msg; |
| 2481 |
fts_msg_id_t *add; |
| 2482 |
|
| 2483 |
if (!fts_optimize_wq) { |
| 2484 |
return; |
| 2485 |
} |
| 2486 |
|
| 2487 |
/* Make sure table with FTS index cannot be evicted */ |
| 2488 |
dict_table_prevent_eviction(table); |
| 2489 |
|
| 2490 |
msg = fts_optimize_create_msg(FTS_MSG_ADD_TABLE, nullptr); |
| 2491 |
|
| 2492 |
add = static_cast<fts_msg_id_t *>(mem_heap_alloc(msg->heap, sizeof(*add))); |
| 2493 |
|
| 2494 |
add->table_id = table->id; |
| 2495 |
msg->ptr = add; |
| 2496 |
|
| 2497 |
ib_wqueue_add(fts_optimize_wq, msg, msg->heap); |
| 2498 |
} |
| 2499 |
|
| 2500 |
#if 0 |
| 2501 |
/**********************************************************************//** |
| 2502 |
Optimize a table. */ |
| 2503 |
static |
| 2504 |
void |
| 2505 |
fts_optimize_do_table( |
| 2506 |
dict_table_t* table) /*!< in: table to optimize */ |
| 2507 |
{ |
| 2508 |
fts_msg_t* msg; |
| 2509 |
|
| 2510 |
/* Optimizer thread could be shutdown */ |
| 2511 |
if (!fts_optimize_wq) { |
| 2512 |
return; |
| 2513 |
} |
| 2514 |
|
| 2515 |
msg = fts_optimize_create_msg(FTS_MSG_OPTIMIZE_TABLE, table); |
| 2516 |
|
| 2517 |
ib_wqueue_add(fts_optimize_wq, msg, msg->heap); |
| 2518 |
} |
| 2519 |
#endif |
| 2520 |
|
| 2521 |
/** Remove the table from the OPTIMIZER's list. We do wait for |
| 2522 |
acknowledgement from the consumer of the message. */ |
| 2523 |
void fts_optimize_remove_table(dict_table_t *table) /*!< in: table to remove */ |
| 2524 |
{ |
| 2525 |
fts_msg_t *msg; |
| 2526 |
fts_msg_id_t *remove; |
| 2527 |
|
| 2528 |
/* if the optimize system not yet initialized, return */ |
| 2529 |
if (!fts_optimize_wq) { |
| 2530 |
return; |
| 2531 |
} |
| 2532 |
|
| 2533 |
/* FTS optimizer thread is already exited */ |
| 2534 |
if (fts_opt_start_shutdown) { |
| 2535 |
ib::info(ER_IB_MSG_501) << "Try to remove table " << table->name |
| 2536 |
<< " after FTS optimize thread exiting."; |
| 2537 |
return; |
| 2538 |
} |
| 2539 |
|
| 2540 |
msg = fts_optimize_create_msg(FTS_MSG_DEL_TABLE, nullptr); |
| 2541 |
|
| 2542 |
remove = |
| 2543 |
static_cast<fts_msg_id_t *>(mem_heap_alloc(msg->heap, sizeof(*remove))); |
| 2544 |
|
| 2545 |
remove->table_id = table->id; |
| 2546 |
msg->ptr = remove; |
| 2547 |
|
| 2548 |
ib_wqueue_add(fts_optimize_wq, msg, msg->heap); |
| 2549 |
} |
| 2550 |
|
| 2551 |
/** Send sync fts cache for the table. |
| 2552 |
@param[in] table table to sync */ |
| 2553 |
void fts_optimize_request_sync_table(dict_table_t *table) { |
| 2554 |
fts_msg_t *msg; |
| 2555 |
table_id_t *table_id; |
| 2556 |
|
| 2557 |
/* if the optimize system not yet initialized, return */ |
| 2558 |
if (!fts_optimize_wq) { |
| 2559 |
return; |
| 2560 |
} |
| 2561 |
|
| 2562 |
/* FTS optimizer thread is already exited */ |
| 2563 |
if (fts_opt_start_shutdown) { |
| 2564 |
ib::info(ER_IB_MSG_502) << "Try to sync table " << table->name |
| 2565 |
<< " after FTS optimize thread exiting."; |
| 2566 |
return; |
| 2567 |
} |
| 2568 |
|
| 2569 |
msg = fts_optimize_create_msg(FTS_MSG_SYNC_TABLE, nullptr); |
| 2570 |
|
| 2571 |
table_id = |
| 2572 |
static_cast<table_id_t *>(mem_heap_alloc(msg->heap, sizeof(table_id_t))); |
| 2573 |
*table_id = table->id; |
| 2574 |
msg->ptr = table_id; |
| 2575 |
|
| 2576 |
ib_wqueue_add(fts_optimize_wq, msg, msg->heap); |
| 2577 |
DBUG_EXECUTE_IF( |
| 2578 |
"fts_optimize_wq_count_check", |
| 2579 |
if (ib_wqueue_get_count(fts_optimize_wq) > 1000) { DBUG_SUICIDE(); }); |
| 2580 |
} |
| 2581 |
|
| 2582 |
/** Find the slot for a particular table. |
| 2583 |
@return slot if found else NULL. */ |
| 2584 |
static fts_slot_t *fts_optimize_find_slot( |
| 2585 |
ib_vector_t *tables, /*!< in: vector of tables */ |
| 2586 |
table_id_t table_id) /*!< in: table id to find */ |
| 2587 |
{ |
| 2588 |
ulint i; |
| 2589 |
|
| 2590 |
for (i = 0; i < ib_vector_size(tables); ++i) { |
| 2591 |
fts_slot_t *slot; |
| 2592 |
|
| 2593 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, i)); |
| 2594 |
|
| 2595 |
if (slot->table_id == table_id) { |
| 2596 |
return (slot); |
| 2597 |
} |
| 2598 |
} |
| 2599 |
|
| 2600 |
return (nullptr); |
| 2601 |
} |
| 2602 |
|
| 2603 |
/** Start optimizing table. */ |
| 2604 |
static void fts_optimize_start_table( |
| 2605 |
ib_vector_t *tables, /*!< in/out: vector of tables */ |
| 2606 |
dict_table_t *table) /*!< in: table to optimize */ |
| 2607 |
{ |
| 2608 |
fts_slot_t *slot; |
| 2609 |
|
| 2610 |
slot = fts_optimize_find_slot(tables, table->id); |
| 2611 |
|
| 2612 |
if (slot == nullptr) { |
| 2613 |
ib::error(ER_IB_MSG_503) << "Table " << table->name |
| 2614 |
<< " not registered" |
| 2615 |
" with the optimize thread."; |
| 2616 |
} else { |
| 2617 |
slot->last_run = {}; |
| 2618 |
slot->completed = {}; |
| 2619 |
} |
| 2620 |
} |
| 2621 |
|
| 2622 |
/** Add the table to the vector if it doesn't already exist. */ |
| 2623 |
static bool fts_optimize_new_table( |
| 2624 |
ib_vector_t *tables, /*!< in/out: vector of tables */ |
| 2625 |
fts_msg_id_t *msg) /*!< in: table to delete */ |
| 2626 |
{ |
| 2627 |
ulint i; |
| 2628 |
fts_slot_t *slot; |
| 2629 |
ulint empty_slot = ULINT_UNDEFINED; |
| 2630 |
table_id_t table_id = msg->table_id; |
| 2631 |
|
| 2632 |
/* Search for duplicates, also find a free slot if one exists. */ |
| 2633 |
for (i = 0; i < ib_vector_size(tables); ++i) { |
| 2634 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, i)); |
| 2635 |
|
| 2636 |
if (slot->state == FTS_STATE_EMPTY) { |
| 2637 |
empty_slot = i; |
| 2638 |
} else if (slot->table_id == table_id) { |
| 2639 |
/* Already exists in our optimize queue. */ |
| 2640 |
return false; |
| 2641 |
} |
| 2642 |
} |
| 2643 |
|
| 2644 |
/* Reuse old slot. */ |
| 2645 |
if (empty_slot != ULINT_UNDEFINED) { |
| 2646 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, empty_slot)); |
| 2647 |
|
| 2648 |
ut_a(slot->state == FTS_STATE_EMPTY); |
| 2649 |
|
| 2650 |
} else { /* Create a new slot. */ |
| 2651 |
|
| 2652 |
slot = static_cast<fts_slot_t *>(ib_vector_push(tables, nullptr)); |
| 2653 |
} |
| 2654 |
|
| 2655 |
// We need to initialize a temporary to work around a gcc12 bug. |
| 2656 |
fts_slot_t tmp{}; |
| 2657 |
*slot = tmp; |
| 2658 |
|
| 2659 |
slot->table_id = table_id; |
| 2660 |
slot->state = FTS_STATE_LOADED; |
| 2661 |
slot->interval_time = FTS_OPTIMIZE_INTERVAL; |
| 2662 |
|
| 2663 |
return true; |
| 2664 |
} |
| 2665 |
|
| 2666 |
/** Remove the table from the vector if it exists. */ |
| 2667 |
static bool fts_optimize_del_table( |
| 2668 |
ib_vector_t *tables, /*!< in/out: vector of tables */ |
| 2669 |
fts_msg_id_t *msg) /*!< in: table to delete */ |
| 2670 |
{ |
| 2671 |
ulint i; |
| 2672 |
table_id_t table_id = msg->table_id; |
| 2673 |
|
| 2674 |
for (i = 0; i < ib_vector_size(tables); ++i) { |
| 2675 |
fts_slot_t *slot; |
| 2676 |
|
| 2677 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, i)); |
| 2678 |
|
| 2679 |
if (slot->state != FTS_STATE_EMPTY && slot->table_id == table_id) { |
| 2680 |
if (fts_enable_diag_print) { |
| 2681 |
ib::info(ER_IB_MSG_504) << "FTS Optimize Removing table " << table_id; |
| 2682 |
} |
| 2683 |
|
| 2684 |
slot->state = FTS_STATE_EMPTY; |
| 2685 |
|
| 2686 |
return true; |
| 2687 |
} |
| 2688 |
} |
| 2689 |
|
| 2690 |
return false; |
| 2691 |
} |
| 2692 |
|
| 2693 |
/** Calculate how many of the registered tables need to be optimized. |
| 2694 |
@return no. of tables to optimize */ |
| 2695 |
static ulint fts_optimize_how_many( |
| 2696 |
const ib_vector_t *tables) /*!< in: registered tables |
| 2697 |
vector*/ |
| 2698 |
{ |
| 2699 |
ulint i; |
| 2700 |
ulint n_tables = 0; |
| 2701 |
|
| 2702 |
const auto current_time = std::chrono::steady_clock::now(); |
| 2703 |
|
| 2704 |
for (i = 0; i < ib_vector_size(tables); ++i) { |
| 2705 |
const fts_slot_t *slot; |
| 2706 |
|
| 2707 |
slot = static_cast<const fts_slot_t *>(ib_vector_get_const(tables, i)); |
| 2708 |
|
| 2709 |
switch (slot->state) { |
| 2710 |
case FTS_STATE_DONE: |
| 2711 |
case FTS_STATE_LOADED: |
| 2712 |
ut_a(slot->completed <= current_time); |
| 2713 |
|
| 2714 |
/* Skip slots that have been optimized recently. */ |
| 2715 |
if (current_time - slot->completed >= slot->interval_time) { |
| 2716 |
++n_tables; |
| 2717 |
} |
| 2718 |
break; |
| 2719 |
|
| 2720 |
case FTS_STATE_RUNNING: |
| 2721 |
ut_a(slot->last_run <= current_time); |
| 2722 |
|
| 2723 |
if (current_time - slot->last_run > slot->interval_time) { |
| 2724 |
++n_tables; |
| 2725 |
} |
| 2726 |
break; |
| 2727 |
|
| 2728 |
/* Slots in a state other than the above |
| 2729 |
are ignored. */ |
| 2730 |
case FTS_STATE_EMPTY: |
| 2731 |
case FTS_STATE_SUSPENDED: |
| 2732 |
break; |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
return (n_tables); |
| 2737 |
} |
| 2738 |
|
| 2739 |
/** Check if the total memory used by all FTS table exceeds the maximum limit. |
| 2740 |
@return true if a sync is needed, false otherwise */ |
| 2741 |
static bool fts_is_sync_needed(const ib_vector_t *tables) /*!< in: registered |
| 2742 |
tables vector*/ |
| 2743 |
{ |
| 2744 |
ulint total_memory = 0; |
| 2745 |
const auto time_diff = |
| 2746 |
std::chrono::steady_clock::now() - last_check_sync_time; |
| 2747 |
|
| 2748 |
if (fts_need_sync || time_diff < std::chrono::seconds{5}) { |
| 2749 |
return (false); |
| 2750 |
} |
| 2751 |
|
| 2752 |
last_check_sync_time = std::chrono::steady_clock::now(); |
| 2753 |
|
| 2754 |
dict_sys_mutex_enter(); |
| 2755 |
|
| 2756 |
for (ulint i = 0; i < ib_vector_size(tables); ++i) { |
| 2757 |
const fts_slot_t *slot; |
| 2758 |
dict_table_t *table = nullptr; |
| 2759 |
|
| 2760 |
slot = static_cast<const fts_slot_t *>(ib_vector_get_const(tables, i)); |
| 2761 |
|
| 2762 |
if (slot->state != FTS_STATE_EMPTY) { |
| 2763 |
table = dd_table_open_on_id_in_mem(slot->table_id, true); |
| 2764 |
|
| 2765 |
if (table != nullptr && table->fts != nullptr && |
| 2766 |
table->fts->cache != nullptr) { |
| 2767 |
total_memory += table->fts->cache->total_size; |
| 2768 |
} |
| 2769 |
|
| 2770 |
if (table != nullptr) { |
| 2771 |
dict_table_close(table, true, false); |
| 2772 |
} |
| 2773 |
|
| 2774 |
if (total_memory > fts_max_total_cache_size) { |
| 2775 |
dict_sys_mutex_exit(); |
| 2776 |
return (true); |
| 2777 |
} |
| 2778 |
} |
| 2779 |
} |
| 2780 |
|
| 2781 |
dict_sys_mutex_exit(); |
| 2782 |
return (false); |
| 2783 |
} |
| 2784 |
|
| 2785 |
#if 0 |
| 2786 |
/*********************************************************************//** |
| 2787 |
Check whether a table needs to be optimized. */ |
| 2788 |
static |
| 2789 |
void |
| 2790 |
fts_optimize_need_sync( |
| 2791 |
ib_vector_t* tables) /*!< in: list of tables */ |
| 2792 |
{ |
| 2793 |
dict_table_t* table = NULL; |
| 2794 |
fts_slot_t* slot; |
| 2795 |
ulint num_table = ib_vector_size(tables); |
| 2796 |
|
| 2797 |
if (!num_table) { |
| 2798 |
return; |
| 2799 |
} |
| 2800 |
|
| 2801 |
if (fts_optimize_sync_iterator >= num_table) { |
| 2802 |
fts_optimize_sync_iterator = 0; |
| 2803 |
} |
| 2804 |
|
| 2805 |
slot = ib_vector_get(tables, fts_optimize_sync_iterator); |
| 2806 |
table = slot->table; |
| 2807 |
|
| 2808 |
if (!table) { |
| 2809 |
return; |
| 2810 |
} |
| 2811 |
|
| 2812 |
ut_ad(table->fts); |
| 2813 |
|
| 2814 |
if (table->fts->cache) { |
| 2815 |
ulint deleted = table->fts->cache->deleted; |
| 2816 |
|
| 2817 |
if (table->fts->cache->added |
| 2818 |
>= fts_optimize_add_threshold) { |
| 2819 |
fts_sync_table(table); |
| 2820 |
} else if (deleted >= fts_optimize_delete_threshold) { |
| 2821 |
fts_optimize_do_table(table); |
| 2822 |
|
| 2823 |
mutex_enter(&table->fts->cache->deleted_lock); |
| 2824 |
table->fts->cache->deleted -= deleted; |
| 2825 |
mutex_exit(&table->fts->cache->deleted_lock); |
| 2826 |
} |
| 2827 |
} |
| 2828 |
|
| 2829 |
fts_optimize_sync_iterator++; |
| 2830 |
|
| 2831 |
return; |
| 2832 |
} |
| 2833 |
#endif |
| 2834 |
|
| 2835 |
/** Sync fts cache of a table |
| 2836 |
@param[in] table_id table id */ |
| 2837 |
void fts_optimize_sync_table(table_id_t table_id) { |
| 2838 |
dict_table_t *table = nullptr; |
| 2839 |
MDL_ticket *mdl = nullptr; |
| 2840 |
THD *thd = current_thd; |
| 2841 |
|
| 2842 |
table = dd_table_open_on_id(table_id, thd, &mdl, false, true); |
| 2843 |
|
| 2844 |
if (table) { |
| 2845 |
if (dict_table_has_fts_index(table) && table->fts->cache) { |
| 2846 |
fts_sync_table(table, true, false, true); |
| 2847 |
} |
| 2848 |
|
| 2849 |
dd_table_close(table, thd, &mdl, false); |
| 2850 |
} |
| 2851 |
} |
| 2852 |
|
| 2853 |
/** Optimize all FTS tables. */ |
| 2854 |
static void fts_optimize_thread(ib_wqueue_t *wq) { |
| 2855 |
mem_heap_t *heap; |
| 2856 |
ib_vector_t *tables; |
| 2857 |
ib_alloc_t *heap_alloc; |
| 2858 |
ulint current = 0; |
| 2859 |
bool done = false; |
| 2860 |
ulint n_tables = 0; |
| 2861 |
ulint n_optimize = 0; |
| 2862 |
|
| 2863 |
ut_ad(!srv_read_only_mode); |
| 2864 |
|
| 2865 |
THD *thd = create_internal_thd(); |
| 2866 |
|
| 2867 |
heap = mem_heap_create(sizeof(dict_table_t *) * 64, UT_LOCATION_HERE); |
| 2868 |
heap_alloc = ib_heap_allocator_create(heap); |
| 2869 |
|
| 2870 |
tables = ib_vector_create(heap_alloc, sizeof(fts_slot_t), 4); |
| 2871 |
|
| 2872 |
while (!done) { |
| 2873 |
/* If there is no message in the queue and we have tables |
| 2874 |
to optimize then optimize the tables. */ |
| 2875 |
|
| 2876 |
if (!done && ib_wqueue_is_empty(wq) && n_tables > 0 && n_optimize > 0) { |
| 2877 |
fts_slot_t *slot; |
| 2878 |
|
| 2879 |
ut_a(ib_vector_size(tables) > 0); |
| 2880 |
|
| 2881 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, current)); |
| 2882 |
|
| 2883 |
/* Handle the case of empty slots. */ |
| 2884 |
if (slot->state != FTS_STATE_EMPTY) { |
| 2885 |
slot->state = FTS_STATE_RUNNING; |
| 2886 |
|
| 2887 |
fts_optimize_table_bk(slot); |
| 2888 |
} |
| 2889 |
|
| 2890 |
++current; |
| 2891 |
|
| 2892 |
/* Wrap around the counter. */ |
| 2893 |
if (current >= ib_vector_size(tables)) { |
| 2894 |
n_optimize = fts_optimize_how_many(tables); |
| 2895 |
|
| 2896 |
current = 0; |
| 2897 |
} |
| 2898 |
|
| 2899 |
} else if (n_optimize == 0 || !ib_wqueue_is_empty(wq)) { |
| 2900 |
fts_msg_t *msg; |
| 2901 |
|
| 2902 |
msg = static_cast<fts_msg_t *>(ib_wqueue_timedwait(wq, FTS_QUEUE_WAIT)); |
| 2903 |
|
| 2904 |
/* Timeout ? */ |
| 2905 |
if (msg == nullptr) { |
| 2906 |
if (fts_is_sync_needed(tables)) { |
| 2907 |
fts_need_sync = true; |
| 2908 |
} |
| 2909 |
|
| 2910 |
continue; |
| 2911 |
} |
| 2912 |
|
| 2913 |
switch (msg->type) { |
| 2914 |
case FTS_MSG_START: |
| 2915 |
break; |
| 2916 |
|
| 2917 |
case FTS_MSG_PAUSE: |
| 2918 |
break; |
| 2919 |
|
| 2920 |
case FTS_MSG_STOP: |
| 2921 |
done = true; |
| 2922 |
break; |
| 2923 |
|
| 2924 |
case FTS_MSG_ADD_TABLE: |
| 2925 |
ut_a(!done); |
| 2926 |
if (fts_optimize_new_table(tables, |
| 2927 |
static_cast<fts_msg_id_t *>(msg->ptr))) { |
| 2928 |
++n_tables; |
| 2929 |
} |
| 2930 |
break; |
| 2931 |
|
| 2932 |
case FTS_MSG_OPTIMIZE_TABLE: |
| 2933 |
if (!done) { |
| 2934 |
fts_optimize_start_table(tables, |
| 2935 |
static_cast<dict_table_t *>(msg->ptr)); |
| 2936 |
} |
| 2937 |
break; |
| 2938 |
|
| 2939 |
case FTS_MSG_DEL_TABLE: |
| 2940 |
if (fts_optimize_del_table(tables, |
| 2941 |
static_cast<fts_msg_id_t *>(msg->ptr))) { |
| 2942 |
--n_tables; |
| 2943 |
} |
| 2944 |
|
| 2945 |
break; |
| 2946 |
|
| 2947 |
case FTS_MSG_SYNC_TABLE: |
| 2948 |
fts_optimize_sync_table(*static_cast<table_id_t *>(msg->ptr)); |
| 2949 |
break; |
| 2950 |
|
| 2951 |
default: |
| 2952 |
ut_error; |
| 2953 |
} |
| 2954 |
|
| 2955 |
mem_heap_free(msg->heap); |
| 2956 |
|
| 2957 |
if (!done) { |
| 2958 |
n_optimize = fts_optimize_how_many(tables); |
| 2959 |
} else { |
| 2960 |
n_optimize = 0; |
| 2961 |
} |
| 2962 |
} |
| 2963 |
} |
| 2964 |
|
| 2965 |
/* Server is being shutdown, sync the data from FTS cache to disk |
| 2966 |
if needed */ |
| 2967 |
if (n_tables > 0) { |
| 2968 |
ulint i; |
| 2969 |
|
| 2970 |
for (i = 0; i < ib_vector_size(tables); i++) { |
| 2971 |
fts_slot_t *slot; |
| 2972 |
|
| 2973 |
slot = static_cast<fts_slot_t *>(ib_vector_get(tables, i)); |
| 2974 |
|
| 2975 |
if (slot->state != FTS_STATE_EMPTY) { |
| 2976 |
fts_optimize_sync_table(slot->table_id); |
| 2977 |
} |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
ib_vector_free(tables); |
| 2982 |
|
| 2983 |
ib::info(ER_IB_MSG_505) << "FTS optimize thread exiting."; |
| 2984 |
|
| 2985 |
destroy_internal_thd(thd); |
| 2986 |
} |
| 2987 |
|
| 2988 |
/** Startup the optimize thread and create the work queue. */ |
| 2989 |
void fts_optimize_init(void) { |
| 2990 |
ut_ad(!srv_read_only_mode); |
| 2991 |
|
| 2992 |
/* For now we only support one optimize thread. */ |
| 2993 |
ut_a(fts_optimize_wq == nullptr); |
| 2994 |
|
| 2995 |
fts_optimize_wq = ib_wqueue_create(); |
| 2996 |
ut_a(fts_optimize_wq != nullptr); |
| 2997 |
last_check_sync_time = std::chrono::steady_clock::now(); |
| 2998 |
|
| 2999 |
srv_threads.m_fts_optimize = os_thread_create( |
| 3000 |
fts_optimize_thread_key, 0, fts_optimize_thread, fts_optimize_wq); |
| 3001 |
|
| 3002 |
srv_threads.m_fts_optimize.start(); |
| 3003 |
} |
| 3004 |
|
| 3005 |
/** Shutdown fts optimize thread. */ |
| 3006 |
void fts_optimize_shutdown() { |
| 3007 |
ut_ad(!srv_read_only_mode); |
| 3008 |
|
| 3009 |
fts_msg_t *msg; |
| 3010 |
|
| 3011 |
/* If there is an ongoing activity on dictionary, such as |
| 3012 |
srv_master_evict_from_table_cache(), wait for it */ |
| 3013 |
dict_mutex_enter_for_mysql(); |
| 3014 |
|
| 3015 |
/* Tells FTS optimizer system that we are exiting from |
| 3016 |
optimizer thread, message send their after will not be |
| 3017 |
processed */ |
| 3018 |
fts_opt_start_shutdown = true; |
| 3019 |
dict_mutex_exit_for_mysql(); |
| 3020 |
|
| 3021 |
/* We tell the OPTIMIZE thread to switch to state done, we |
| 3022 |
can't delete the work queue here because the add thread needs |
| 3023 |
deregister the FTS tables. */ |
| 3024 |
|
| 3025 |
msg = fts_optimize_create_msg(FTS_MSG_STOP, nullptr); |
| 3026 |
|
| 3027 |
ib_wqueue_add(fts_optimize_wq, msg, msg->heap); |
| 3028 |
|
| 3029 |
srv_threads.m_fts_optimize.join(); |
| 3030 |
|
| 3031 |
ib_wqueue_free(fts_optimize_wq); |
| 3032 |
fts_optimize_wq = nullptr; |
| 3033 |
} |