| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 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 trx/trx0purge.cc |
| 29 |
Purge old versions |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <sys/types.h> |
| 35 |
#include <new> |
| 36 |
#include <unordered_map> |
| 37 |
|
| 38 |
#include "clone0api.h" |
| 39 |
#include "clone0clone.h" |
| 40 |
#include "dict0dd.h" |
| 41 |
#include "fil0fil.h" |
| 42 |
#include "fsp0fsp.h" |
| 43 |
#include "fsp0sysspace.h" |
| 44 |
#include "fsp0types.h" |
| 45 |
#include "fut0fut.h" |
| 46 |
#include "ha_prototypes.h" |
| 47 |
#include "log0buf.h" |
| 48 |
#include "log0chkp.h" |
| 49 |
#include "mach0data.h" |
| 50 |
#include "mtr0log.h" |
| 51 |
#include "my_compiler.h" |
| 52 |
#include "my_dbug.h" |
| 53 |
#include "my_inttypes.h" |
| 54 |
#include "os0thread.h" |
| 55 |
#include "que0que.h" |
| 56 |
#include "read0read.h" |
| 57 |
#include "row0purge.h" |
| 58 |
#include "row0upd.h" |
| 59 |
#include "srv0mon.h" |
| 60 |
#include "srv0srv.h" |
| 61 |
#include "srv0start.h" |
| 62 |
#include "sync0sync.h" |
| 63 |
#include "trx0purge.h" |
| 64 |
#include "trx0rec.h" |
| 65 |
#include "trx0roll.h" |
| 66 |
#include "trx0rseg.h" |
| 67 |
#include "trx0trx.h" |
| 68 |
#include "ut0math.h" |
| 69 |
|
| 70 |
/** Maximum allowable purge history length. <=0 means 'infinite'. */ |
| 71 |
ulong srv_max_purge_lag = 0; |
| 72 |
|
| 73 |
/** Max DML user threads delay in micro-seconds. */ |
| 74 |
ulong srv_max_purge_lag_delay = 0; |
| 75 |
|
| 76 |
/** The global data structure coordinating a purge */ |
| 77 |
trx_purge_t *purge_sys = nullptr; |
| 78 |
|
| 79 |
/** Wait for a short delay between checks. */ |
| 80 |
#ifdef UNIV_DEBUG |
| 81 |
static constexpr int64_t PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS = 10; |
| 82 |
#else |
| 83 |
static constexpr int64_t PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS = 1000; |
| 84 |
#endif /* UNIV_DEBUG */ |
| 85 |
|
| 86 |
#ifdef UNIV_DEBUG |
| 87 |
bool srv_purge_view_update_only_debug; |
| 88 |
#endif /* UNIV_DEBUG */ |
| 89 |
|
| 90 |
/** Sentinel value */ |
| 91 |
const TrxUndoRsegs TrxUndoRsegsIterator::NullElement(UINT64_UNDEFINED); |
| 92 |
|
| 93 |
/** A sentinel undo record used as a return value when we have a whole |
| 94 |
undo log which can be skipped by purge */ |
| 95 |
static trx_undo_rec_t trx_purge_ignore_rec; |
| 96 |
|
| 97 |
/** Constructor */ |
| 98 |
TrxUndoRsegsIterator::TrxUndoRsegsIterator(trx_purge_t *purge_sys) |
| 99 |
: m_purge_sys(purge_sys), |
| 100 |
m_trx_undo_rsegs(NullElement), |
| 101 |
m_iter(m_trx_undo_rsegs.end()) {} |
| 102 |
|
| 103 |
/** Sets the next rseg to purge in m_purge_sys. |
| 104 |
@return page size of the table for which the log is. |
| 105 |
NOTE: if rseg is NULL when this function returns this means that |
| 106 |
there are no rollback segments to purge and then the returned page |
| 107 |
size object should not be used. */ |
| 108 |
const page_size_t TrxUndoRsegsIterator::set_next() { |
| 109 |
mutex_enter(&m_purge_sys->pq_mutex); |
| 110 |
|
| 111 |
/* Only purge consumes events from the priority queue, user |
| 112 |
threads only produce the events. */ |
| 113 |
|
| 114 |
/* Check if there are more rsegs to process in the |
| 115 |
current element. */ |
| 116 |
if (m_iter != m_trx_undo_rsegs.end()) { |
| 117 |
/* We are still processing rollback segment from |
| 118 |
the same transaction and so expected transaction |
| 119 |
number shouldn't increase. Undo increment of |
| 120 |
expected trx_no done by caller assuming rollback |
| 121 |
segments from given transaction are done. */ |
| 122 |
m_purge_sys->iter.trx_no = (*m_iter)->last_trx_no; |
| 123 |
|
| 124 |
} else if (!m_purge_sys->purge_queue->empty()) { |
| 125 |
/* Read the next element from the queue. |
| 126 |
Combine elements if they have same transaction number. |
| 127 |
This can happen if a transaction shares redo rollback segment |
| 128 |
with another transaction that has already added it to purge |
| 129 |
queue and former transaction also needs to schedule non-redo |
| 130 |
rollback segment for purge. */ |
| 131 |
m_trx_undo_rsegs = NullElement; |
| 132 |
|
| 133 |
while (!m_purge_sys->purge_queue->empty()) { |
| 134 |
if (m_trx_undo_rsegs.get_trx_no() == UINT64_UNDEFINED) { |
| 135 |
m_trx_undo_rsegs = purge_sys->purge_queue->top(); |
| 136 |
} else if (purge_sys->purge_queue->top().get_trx_no() == |
| 137 |
m_trx_undo_rsegs.get_trx_no()) { |
| 138 |
m_trx_undo_rsegs.insert(purge_sys->purge_queue->top()); |
| 139 |
} else { |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
m_purge_sys->purge_queue->pop(); |
| 144 |
} |
| 145 |
|
| 146 |
m_iter = m_trx_undo_rsegs.begin(); |
| 147 |
|
| 148 |
} else { |
| 149 |
/* Queue is empty, reset iterator. */ |
| 150 |
m_trx_undo_rsegs = NullElement; |
| 151 |
m_iter = m_trx_undo_rsegs.end(); |
| 152 |
|
| 153 |
mutex_exit(&m_purge_sys->pq_mutex); |
| 154 |
|
| 155 |
m_purge_sys->rseg = nullptr; |
| 156 |
|
| 157 |
/* return a dummy object, not going to be used by the caller */ |
| 158 |
return (univ_page_size); |
| 159 |
} |
| 160 |
|
| 161 |
m_purge_sys->rseg = *m_iter++; |
| 162 |
|
| 163 |
mutex_exit(&m_purge_sys->pq_mutex); |
| 164 |
|
| 165 |
ut_a(m_purge_sys->rseg != nullptr); |
| 166 |
|
| 167 |
m_purge_sys->rseg->latch(); |
| 168 |
|
| 169 |
ut_a(m_purge_sys->rseg->last_page_no != FIL_NULL); |
| 170 |
ut_ad(m_purge_sys->rseg->last_trx_no == m_trx_undo_rsegs.get_trx_no()); |
| 171 |
|
| 172 |
/* The space_id must be a tablespace that contains rollback segments. |
| 173 |
That includes the system, temporary and all undo tablespaces. */ |
| 174 |
ut_a(fsp_is_system_or_temp_tablespace(m_purge_sys->rseg->space_id) || |
| 175 |
fsp_is_undo_tablespace(m_purge_sys->rseg->space_id)); |
| 176 |
|
| 177 |
const page_size_t page_size(m_purge_sys->rseg->page_size); |
| 178 |
|
| 179 |
ut_a(purge_sys->iter.trx_no <= purge_sys->rseg->last_trx_no); |
| 180 |
|
| 181 |
m_purge_sys->iter.trx_no = m_purge_sys->rseg->last_trx_no; |
| 182 |
m_purge_sys->hdr_offset = m_purge_sys->rseg->last_offset; |
| 183 |
m_purge_sys->hdr_page_no = m_purge_sys->rseg->last_page_no; |
| 184 |
|
| 185 |
m_purge_sys->rseg->unlatch(); |
| 186 |
|
| 187 |
return (page_size); |
| 188 |
} |
| 189 |
|
| 190 |
/** Builds a purge 'query' graph. The actual purge is performed by executing |
| 191 |
this query graph. |
| 192 |
@param[in] trx transaction |
| 193 |
@param[in] n_purge_threads number of purge threads |
| 194 |
@return own: the query graph */ |
| 195 |
static que_t *trx_purge_graph_build(trx_t *trx, ulint n_purge_threads) { |
| 196 |
ulint i; |
| 197 |
mem_heap_t *heap; |
| 198 |
que_fork_t *fork; |
| 199 |
|
| 200 |
heap = mem_heap_create(512, UT_LOCATION_HERE); |
| 201 |
fork = que_fork_create(nullptr, nullptr, QUE_FORK_PURGE, heap); |
| 202 |
fork->trx = trx; |
| 203 |
|
| 204 |
for (i = 0; i < n_purge_threads; ++i) { |
| 205 |
que_thr_t *thr; |
| 206 |
|
| 207 |
thr = que_thr_create(fork, heap, nullptr); |
| 208 |
|
| 209 |
thr->child = row_purge_node_create(thr, heap); |
| 210 |
} |
| 211 |
|
| 212 |
return (fork); |
| 213 |
} |
| 214 |
|
| 215 |
void trx_purge_sys_mem_create() { |
| 216 |
purge_sys = static_cast<trx_purge_t *>( |
| 217 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*purge_sys))); |
| 218 |
|
| 219 |
purge_sys->state = PURGE_STATE_INIT; |
| 220 |
purge_sys->event = os_event_create(); |
| 221 |
|
| 222 |
new (&purge_sys->iter) purge_iter_t; |
| 223 |
new (&purge_sys->limit) purge_iter_t; |
| 224 |
new (&purge_sys->undo_trunc) undo::Truncate; |
| 225 |
new (&purge_sys->rsegs_queue) std::vector<trx_rseg_t *>; |
| 226 |
#ifdef UNIV_DEBUG |
| 227 |
new (&purge_sys->done) purge_iter_t; |
| 228 |
#endif /* UNIV_DEBUG */ |
| 229 |
|
| 230 |
rw_lock_create(trx_purge_latch_key, &purge_sys->latch, LATCH_ID_TRX_PURGE); |
| 231 |
|
| 232 |
mutex_create(LATCH_ID_PURGE_SYS_PQ, &purge_sys->pq_mutex); |
| 233 |
|
| 234 |
purge_sys->heap = mem_heap_create(8 * 1024, UT_LOCATION_HERE); |
| 235 |
} |
| 236 |
|
| 237 |
void trx_purge_sys_initialize(uint32_t n_purge_threads, |
| 238 |
purge_pq_t *purge_queue) { |
| 239 |
/* Take ownership of purge_queue, we are responsible for freeing it. */ |
| 240 |
purge_sys->purge_queue = purge_queue; |
| 241 |
|
| 242 |
ut_a(n_purge_threads > 0); |
| 243 |
|
| 244 |
purge_sys->sess = sess_open(); |
| 245 |
|
| 246 |
purge_sys->trx = purge_sys->sess->trx; |
| 247 |
|
| 248 |
ut_a(purge_sys->trx->sess == purge_sys->sess); |
| 249 |
|
| 250 |
/* A purge transaction is not a real transaction, we use a transaction |
| 251 |
here only because the query threads code requires it. It is otherwise |
| 252 |
quite unnecessary. We should get rid of it eventually. */ |
| 253 |
purge_sys->trx->id = 0; |
| 254 |
purge_sys->trx->start_time.store(std::chrono::system_clock::now(), |
| 255 |
std::memory_order_relaxed); |
| 256 |
purge_sys->trx->state.store(TRX_STATE_ACTIVE, std::memory_order_relaxed); |
| 257 |
purge_sys->trx->op_info = "purge trx"; |
| 258 |
purge_sys->trx->purge_sys_trx = true; |
| 259 |
|
| 260 |
purge_sys->query = trx_purge_graph_build(purge_sys->trx, n_purge_threads); |
| 261 |
|
| 262 |
new (&purge_sys->view) ReadView(); |
| 263 |
|
| 264 |
trx_sys->mvcc->clone_oldest_view(&purge_sys->view); |
| 265 |
|
| 266 |
purge_sys->view_active = true; |
| 267 |
|
| 268 |
purge_sys->rseg_iter = ut::new_withkey<TrxUndoRsegsIterator>( |
| 269 |
UT_NEW_THIS_FILE_PSI_KEY, purge_sys); |
| 270 |
} |
| 271 |
|
| 272 |
void trx_purge_sys_close() { |
| 273 |
que_graph_free(purge_sys->query); |
| 274 |
|
| 275 |
ut_a(purge_sys->trx->id == 0); |
| 276 |
ut_a(purge_sys->sess->trx == purge_sys->trx); |
| 277 |
|
| 278 |
purge_sys->trx->state.store(TRX_STATE_NOT_STARTED, std::memory_order_relaxed); |
| 279 |
|
| 280 |
sess_close(purge_sys->sess); |
| 281 |
|
| 282 |
purge_sys->sess = nullptr; |
| 283 |
|
| 284 |
purge_sys->view.close(); |
| 285 |
purge_sys->view.~ReadView(); |
| 286 |
|
| 287 |
rw_lock_free(&purge_sys->latch); |
| 288 |
mutex_free(&purge_sys->pq_mutex); |
| 289 |
|
| 290 |
if (purge_sys->purge_queue != nullptr) { |
| 291 |
ut::delete_(purge_sys->purge_queue); |
| 292 |
purge_sys->purge_queue = nullptr; |
| 293 |
} |
| 294 |
|
| 295 |
os_event_destroy(purge_sys->event); |
| 296 |
|
| 297 |
purge_sys->event = nullptr; |
| 298 |
|
| 299 |
mem_heap_free(purge_sys->heap); |
| 300 |
|
| 301 |
purge_sys->heap = nullptr; |
| 302 |
|
| 303 |
ut::delete_(purge_sys->rseg_iter); |
| 304 |
|
| 305 |
call_destructor(&purge_sys->undo_trunc); |
| 306 |
call_destructor(&purge_sys->rsegs_queue); |
| 307 |
|
| 308 |
ut::free(purge_sys); |
| 309 |
|
| 310 |
purge_sys = nullptr; |
| 311 |
} |
| 312 |
|
| 313 |
/*================ UNDO LOG HISTORY LIST =============================*/ |
| 314 |
|
| 315 |
/** Adds the update undo log as the first log in the history list. Removes the |
| 316 |
update undo log segment from the rseg slot if it is too big for reuse. */ |
| 317 |
void trx_purge_add_update_undo_to_history( |
| 318 |
trx_t *trx, /*!< in: transaction */ |
| 319 |
trx_undo_ptr_t *undo_ptr, /*!< in/out: update undo log. */ |
| 320 |
page_t *undo_page, /*!< in: update undo log header page, |
| 321 |
x-latched */ |
| 322 |
bool update_rseg_history_len, |
| 323 |
/*!< in: if true: update rseg history |
| 324 |
len else skip updating it. */ |
| 325 |
ulint n_added_logs, /*!< in: number of logs added */ |
| 326 |
mtr_t *mtr) /*!< in: mtr */ |
| 327 |
{ |
| 328 |
trx_undo_t *undo; |
| 329 |
trx_rseg_t *rseg; |
| 330 |
trx_rsegf_t *rseg_header; |
| 331 |
trx_ulogf_t *undo_header; |
| 332 |
|
| 333 |
undo = undo_ptr->update_undo; |
| 334 |
rseg = undo->rseg; |
| 335 |
|
| 336 |
rseg_header = trx_rsegf_get(undo->rseg->space_id, undo->rseg->page_no, |
| 337 |
undo->rseg->page_size, mtr); |
| 338 |
|
| 339 |
undo_header = undo_page + undo->hdr_offset; |
| 340 |
|
| 341 |
if (undo->state != TRX_UNDO_CACHED) { |
| 342 |
ulint hist_size; |
| 343 |
#ifdef UNIV_DEBUG |
| 344 |
trx_usegf_t *seg_header = undo_page + TRX_UNDO_SEG_HDR; |
| 345 |
#endif /* UNIV_DEBUG */ |
| 346 |
|
| 347 |
/* The undo log segment will not be reused */ |
| 348 |
|
| 349 |
if (UNIV_UNLIKELY(undo->id >= TRX_RSEG_N_SLOTS)) { |
| 350 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1165) << "undo->id is " << undo->id; |
| 351 |
} |
| 352 |
|
| 353 |
trx_rsegf_set_nth_undo(rseg_header, undo->id, FIL_NULL, mtr); |
| 354 |
|
| 355 |
MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_USED); |
| 356 |
|
| 357 |
hist_size = |
| 358 |
mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, mtr); |
| 359 |
|
| 360 |
ut_ad(undo->size == flst_get_len(seg_header + TRX_UNDO_PAGE_LIST)); |
| 361 |
|
| 362 |
mlog_write_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, |
| 363 |
hist_size + undo->size, MLOG_4BYTES, mtr); |
| 364 |
} |
| 365 |
|
| 366 |
/* Add the log as the first in the history list */ |
| 367 |
flst_add_first(rseg_header + TRX_RSEG_HISTORY, |
| 368 |
undo_header + TRX_UNDO_HISTORY_NODE, mtr); |
| 369 |
|
| 370 |
if (update_rseg_history_len) { |
| 371 |
trx_sys->rseg_history_len.fetch_add(n_added_logs); |
| 372 |
if (trx_sys->rseg_history_len.load() > |
| 373 |
srv_n_purge_threads * srv_purge_batch_size) { |
| 374 |
srv_wake_purge_thread_if_not_active(); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/* Update maximum transaction number for this rollback segment. */ |
| 379 |
mlog_write_ull(rseg_header + TRX_RSEG_MAX_TRX_NO, trx->no, mtr); |
| 380 |
|
| 381 |
/* Write the trx number to the undo log header */ |
| 382 |
mlog_write_ull(undo_header + TRX_UNDO_TRX_NO, trx->no, mtr); |
| 383 |
|
| 384 |
/* Write information about delete markings to the undo log header */ |
| 385 |
|
| 386 |
if (!undo->del_marks) { |
| 387 |
mlog_write_ulint(undo_header + TRX_UNDO_DEL_MARKS, false, MLOG_2BYTES, mtr); |
| 388 |
} |
| 389 |
|
| 390 |
/* Write GTID information if there. */ |
| 391 |
trx_undo_gtid_write(trx, undo_header, undo, mtr, false); |
| 392 |
|
| 393 |
if (rseg->last_page_no == FIL_NULL) { |
| 394 |
rseg->last_page_no = undo->hdr_page_no; |
| 395 |
rseg->last_offset = undo->hdr_offset; |
| 396 |
rseg->last_trx_no = trx->no; |
| 397 |
rseg->last_del_marks = undo->del_marks; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
/** Remove an rseg header from the history list. |
| 402 |
@param[in,out] rseg_hdr Rollback segment header |
| 403 |
@param[in] log_hdr Undo log segment header |
| 404 |
@param[in,out] mtr Mini-transaction. */ |
| 405 |
static void trx_purge_remove_log_hdr(trx_rsegf_t *rseg_hdr, |
| 406 |
trx_ulogf_t *log_hdr, mtr_t *mtr) { |
| 407 |
flst_remove(rseg_hdr + TRX_RSEG_HISTORY, log_hdr + TRX_UNDO_HISTORY_NODE, |
| 408 |
mtr); |
| 409 |
|
| 410 |
trx_sys->rseg_history_len.fetch_sub(1); |
| 411 |
} |
| 412 |
|
| 413 |
/** Frees a rollback segment which is in the history list. |
| 414 |
Removes the rseg hdr from the history list. |
| 415 |
@param[in,out] rseg rollback segment |
| 416 |
@param[in] hdr_addr file address of log_hdr |
| 417 |
@param[in] noredo skip redo logging. */ |
| 418 |
static void trx_purge_free_segment(trx_rseg_t *rseg, fil_addr_t hdr_addr, |
| 419 |
bool noredo) { |
| 420 |
mtr_t mtr; |
| 421 |
trx_rsegf_t *rseg_hdr; |
| 422 |
trx_ulogf_t *log_hdr; |
| 423 |
trx_usegf_t *seg_hdr; |
| 424 |
ulint seg_size; |
| 425 |
ulint hist_size; |
| 426 |
bool marked = noredo; |
| 427 |
|
| 428 |
for (;;) { |
| 429 |
page_t *undo_page; |
| 430 |
|
| 431 |
mtr_start(&mtr); |
| 432 |
|
| 433 |
if (noredo) { |
| 434 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 435 |
} |
| 436 |
|
| 437 |
rseg->latch(); |
| 438 |
|
| 439 |
rseg_hdr = |
| 440 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 441 |
|
| 442 |
undo_page = trx_undo_page_get(page_id_t(rseg->space_id, hdr_addr.page), |
| 443 |
rseg->page_size, &mtr); |
| 444 |
|
| 445 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 446 |
log_hdr = undo_page + hdr_addr.boffset; |
| 447 |
|
| 448 |
/* Mark the last undo log totally purged, so that if the |
| 449 |
system crashes, the tail of the undo log will not get accessed |
| 450 |
again. The list of pages in the undo log tail gets inconsistent |
| 451 |
during the freeing of the segment, and therefore purge should |
| 452 |
not try to access them again. */ |
| 453 |
|
| 454 |
if (!marked) { |
| 455 |
marked = true; |
| 456 |
mlog_write_ulint(log_hdr + TRX_UNDO_DEL_MARKS, false, MLOG_2BYTES, &mtr); |
| 457 |
} |
| 458 |
|
| 459 |
if (fseg_free_step_not_header(seg_hdr + TRX_UNDO_FSEG_HEADER, false, |
| 460 |
&mtr)) { |
| 461 |
break; |
| 462 |
} |
| 463 |
|
| 464 |
rseg->unlatch(); |
| 465 |
|
| 466 |
mtr_commit(&mtr); |
| 467 |
} |
| 468 |
|
| 469 |
/* The page list may now be inconsistent, but the length field |
| 470 |
stored in the list base node tells us how big it was before we |
| 471 |
started the freeing. */ |
| 472 |
|
| 473 |
seg_size = flst_get_len(seg_hdr + TRX_UNDO_PAGE_LIST); |
| 474 |
|
| 475 |
/* We may free the undo log segment header page; it must be freed |
| 476 |
within the same mtr as the undo log header is removed from the |
| 477 |
history list: otherwise, in case of a database crash, the segment |
| 478 |
could become inaccessible garbage in the file space. */ |
| 479 |
|
| 480 |
trx_purge_remove_log_hdr(rseg_hdr, log_hdr, &mtr); |
| 481 |
|
| 482 |
do { |
| 483 |
/* Here we assume that a file segment with just the header |
| 484 |
page can be freed in a few steps, so that the buffer pool |
| 485 |
is not flooded with bufferfixed pages: see the note in |
| 486 |
fsp0fsp.cc. */ |
| 487 |
|
| 488 |
} while (!fseg_free_step(seg_hdr + TRX_UNDO_FSEG_HEADER, false, &mtr)); |
| 489 |
|
| 490 |
hist_size = |
| 491 |
mtr_read_ulint(rseg_hdr + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, &mtr); |
| 492 |
ut_ad(hist_size >= seg_size); |
| 493 |
|
| 494 |
mlog_write_ulint(rseg_hdr + TRX_RSEG_HISTORY_SIZE, hist_size - seg_size, |
| 495 |
MLOG_4BYTES, &mtr); |
| 496 |
|
| 497 |
rseg->decr_curr_size(seg_size); |
| 498 |
rseg->unlatch(); |
| 499 |
mtr_commit(&mtr); |
| 500 |
} |
| 501 |
|
| 502 |
/** Removes unnecessary history data from a rollback segment. */ |
| 503 |
static void trx_purge_truncate_rseg_history( |
| 504 |
trx_rseg_t *rseg, /*!< in: rollback segment */ |
| 505 |
const purge_iter_t *limit) /*!< in: truncate offset */ |
| 506 |
{ |
| 507 |
fil_addr_t hdr_addr; |
| 508 |
fil_addr_t prev_hdr_addr; |
| 509 |
trx_rsegf_t *rseg_hdr; |
| 510 |
page_t *undo_page; |
| 511 |
trx_ulogf_t *log_hdr; |
| 512 |
trx_usegf_t *seg_hdr; |
| 513 |
mtr_t mtr; |
| 514 |
trx_id_t undo_trx_no; |
| 515 |
const bool is_temp = fsp_is_system_temporary(rseg->space_id); |
| 516 |
|
| 517 |
mtr_start(&mtr); |
| 518 |
|
| 519 |
if (is_temp) { |
| 520 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 521 |
} |
| 522 |
|
| 523 |
rseg->latch(); |
| 524 |
|
| 525 |
rseg_hdr = |
| 526 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 527 |
|
| 528 |
hdr_addr = trx_purge_get_log_from_hist( |
| 529 |
flst_get_last(rseg_hdr + TRX_RSEG_HISTORY, &mtr)); |
| 530 |
loop: |
| 531 |
if (hdr_addr.page == FIL_NULL) { |
| 532 |
rseg->unlatch(); |
| 533 |
mtr_commit(&mtr); |
| 534 |
|
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
undo_page = trx_undo_page_get(page_id_t(rseg->space_id, hdr_addr.page), |
| 539 |
rseg->page_size, &mtr); |
| 540 |
|
| 541 |
log_hdr = undo_page + hdr_addr.boffset; |
| 542 |
|
| 543 |
undo_trx_no = mach_read_from_8(log_hdr + TRX_UNDO_TRX_NO); |
| 544 |
|
| 545 |
if (undo_trx_no >= limit->trx_no) { |
| 546 |
/* limit space_id should match the rollback segment |
| 547 |
space id to avoid freeing if the page belongs to a |
| 548 |
different rollback segment for the same trx_no. */ |
| 549 |
if (undo_trx_no == limit->trx_no && |
| 550 |
rseg->space_id == limit->undo_rseg_space) { |
| 551 |
trx_undo_truncate_start(rseg, hdr_addr.page, hdr_addr.boffset, |
| 552 |
limit->undo_no); |
| 553 |
} |
| 554 |
|
| 555 |
rseg->unlatch(); |
| 556 |
mtr_commit(&mtr); |
| 557 |
|
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
prev_hdr_addr = trx_purge_get_log_from_hist( |
| 562 |
flst_get_prev_addr(log_hdr + TRX_UNDO_HISTORY_NODE, &mtr)); |
| 563 |
|
| 564 |
seg_hdr = undo_page + TRX_UNDO_SEG_HDR; |
| 565 |
|
| 566 |
if ((mach_read_from_2(seg_hdr + TRX_UNDO_STATE) == TRX_UNDO_TO_PURGE) && |
| 567 |
(mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG) == 0)) { |
| 568 |
/* We can free the whole log segment */ |
| 569 |
|
| 570 |
rseg->unlatch(); |
| 571 |
mtr_commit(&mtr); |
| 572 |
|
| 573 |
/* calls the trx_purge_remove_log_hdr() |
| 574 |
inside trx_purge_free_segment(). */ |
| 575 |
trx_purge_free_segment(rseg, hdr_addr, is_temp); |
| 576 |
|
| 577 |
} else { |
| 578 |
/* Remove the log hdr from the rseg history. */ |
| 579 |
|
| 580 |
trx_purge_remove_log_hdr(rseg_hdr, log_hdr, &mtr); |
| 581 |
|
| 582 |
rseg->unlatch(); |
| 583 |
mtr_commit(&mtr); |
| 584 |
} |
| 585 |
|
| 586 |
mtr_start(&mtr); |
| 587 |
|
| 588 |
if (is_temp) { |
| 589 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 590 |
} |
| 591 |
|
| 592 |
rseg->latch(); |
| 593 |
|
| 594 |
rseg_hdr = |
| 595 |
trx_rsegf_get(rseg->space_id, rseg->page_no, rseg->page_size, &mtr); |
| 596 |
|
| 597 |
hdr_addr = prev_hdr_addr; |
| 598 |
|
| 599 |
goto loop; |
| 600 |
} |
| 601 |
|
| 602 |
namespace undo { |
| 603 |
|
| 604 |
/** Mutext for serializing undo tablespace related DDL. These have to do with |
| 605 |
creating and dropping undo tablespaces. */ |
| 606 |
ib_mutex_t ddl_mutex; |
| 607 |
|
| 608 |
/** A global object that contains a vector of undo::Tablespace structs. */ |
| 609 |
Tablespaces *spaces; |
| 610 |
|
| 611 |
/** List of currently used undo space IDs for each undo space number |
| 612 |
along with a boolean showing whether the undo space number is in use. */ |
| 613 |
struct space_id_account *space_id_bank; |
| 614 |
|
| 615 |
/** Initialize the undo tablespace space_id bank which is a lock free |
| 616 |
repository for information about the space IDs used for undo tablespaces. |
| 617 |
It is used during creation in order to assign an unused space number and |
| 618 |
during truncation in order to assign the next space_id within that |
| 619 |
space_number range. |
| 620 |
It is initialized with the minimum value in the range so that if a new |
| 621 |
space ID is needed in that range the max space ID will be used first. |
| 622 |
As truncation occurs, the space_ids are assigned from max down to min. */ |
| 623 |
void init_space_id_bank() { |
| 624 |
space_id_bank = ut::new_arr_withkey<struct space_id_account>( |
| 625 |
ut::make_psi_memory_key(mem_key_undo_spaces), |
| 626 |
ut::Count{FSP_MAX_UNDO_TABLESPACES}); |
| 627 |
|
| 628 |
for (size_t slot = 0; slot < FSP_MAX_UNDO_TABLESPACES; slot++) { |
| 629 |
undo::space_id_bank[slot].space_id = SPACE_UNKNOWN; |
| 630 |
undo::space_id_bank[slot].in_use = false; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
/** Note that the undo space number for a space ID is being used. |
| 635 |
Put that space_id into the space_id_bank. |
| 636 |
@param[in] space_id undo tablespace number */ |
| 637 |
void use_space_id(space_id_t space_id) { |
| 638 |
size_t slot = undo::id2num(space_id) - 1; |
| 639 |
|
| 640 |
ut_ad(!space_id_bank[slot].in_use); |
| 641 |
|
| 642 |
space_id_bank[slot].space_id = space_id; |
| 643 |
space_id_bank[slot].in_use = true; |
| 644 |
} |
| 645 |
|
| 646 |
/** Mark an undo number associated with a given space_id as unused and |
| 647 |
available to be resused. This happens when the fil_space_t is closed |
| 648 |
associated with a drop undo tablespace. |
| 649 |
@param[in] space_id Undo Tablespace ID */ |
| 650 |
void unuse_space_id(space_id_t space_id) { |
| 651 |
ut_ad(fsp_is_undo_tablespace(space_id)); |
| 652 |
|
| 653 |
space_id_t space_num = undo::id2num(space_id); |
| 654 |
size_t slot = space_num - 1; |
| 655 |
|
| 656 |
space_id_bank[slot].in_use = false; |
| 657 |
} |
| 658 |
|
| 659 |
/** Given a valid undo space_id or SPACE_UNKNOWN, return the next space_id |
| 660 |
for the given space number. |
| 661 |
@param[in] space_id undo tablespace ID |
| 662 |
@param[in] space_num undo tablespace number |
| 663 |
@return the next tablespace ID to use */ |
| 664 |
space_id_t next_space_id(space_id_t space_id, space_id_t space_num) { |
| 665 |
ut_ad(space_id == SPACE_UNKNOWN || fsp_is_undo_tablespace(space_id)); |
| 666 |
ut_ad(space_id != SPACE_UNKNOWN || |
| 667 |
(space_num > 0 && space_num <= FSP_MAX_UNDO_TABLESPACES)); |
| 668 |
|
| 669 |
space_id_t first_id = dict_sys_t::s_max_undo_space_id + 1 - space_num; |
| 670 |
space_id_t last_id = first_id - (FSP_MAX_UNDO_TABLESPACES * |
| 671 |
(dict_sys_t::s_undo_space_id_range - 1)); |
| 672 |
return (space_id == SPACE_UNKNOWN || space_id == last_id |
| 673 |
? first_id |
| 674 |
: space_id - FSP_MAX_UNDO_TABLESPACES); |
| 675 |
} |
| 676 |
|
| 677 |
/** Given a valid undo space_id, return the next space_id for that |
| 678 |
space number. |
| 679 |
@param[in] space_id undo tablespace ID |
| 680 |
@return the next tablespace ID to use */ |
| 681 |
space_id_t next_space_id(space_id_t space_id) { |
| 682 |
ut_ad(space_id != SPACE_UNKNOWN); |
| 683 |
ut_ad(fsp_is_undo_tablespace(space_id)); |
| 684 |
|
| 685 |
space_id_t space_num = undo::id2num(space_id); |
| 686 |
|
| 687 |
return (next_space_id(space_id, space_num)); |
| 688 |
} |
| 689 |
|
| 690 |
/** Mark that the given undo space number is being used and |
| 691 |
return the next available space_id for that space number. |
| 692 |
@param[in] space_num undo tablespace number |
| 693 |
@return the next tablespace ID to use */ |
| 694 |
space_id_t use_next_space_id(space_id_t space_num) { |
| 695 |
size_t slot = space_num - 1; |
| 696 |
|
| 697 |
ut_ad(!space_id_bank[slot].in_use); |
| 698 |
|
| 699 |
space_id_t cur_id = space_id_bank[slot].space_id; |
| 700 |
space_id_t next_id = next_space_id(cur_id, space_num); |
| 701 |
|
| 702 |
space_id_bank[slot].space_id = next_id; |
| 703 |
space_id_bank[slot].in_use = true; |
| 704 |
|
| 705 |
return (next_id); |
| 706 |
} |
| 707 |
|
| 708 |
/** Return the next available undo space ID to be used for a new explicit |
| 709 |
undo tablespaces. The slot will be marked as in-use. |
| 710 |
@return next available undo space number if successful. |
| 711 |
@return SPACE_UNKNOWN if failed */ |
| 712 |
space_id_t get_next_available_space_num() { |
| 713 |
for (space_id_t slot = FSP_IMPLICIT_UNDO_TABLESPACES; |
| 714 |
slot < FSP_MAX_UNDO_TABLESPACES; ++slot) { |
| 715 |
space_id_t space_num = slot + 1; |
| 716 |
|
| 717 |
if (!space_id_bank[slot].in_use) { |
| 718 |
return (use_next_space_id(space_num)); |
| 719 |
} |
| 720 |
/* Slot is in use. Try the next slot. */ |
| 721 |
} |
| 722 |
|
| 723 |
return (SPACE_UNKNOWN); |
| 724 |
} |
| 725 |
|
| 726 |
bool Tablespace::needs_truncation() { |
| 727 |
/* If it is already inactive, even implicitly, then proceed. */ |
| 728 |
m_rsegs->s_lock(); |
| 729 |
if (m_rsegs->is_inactive_implicit() || m_rsegs->is_inactive_explicit()) { |
| 730 |
m_rsegs->s_unlock(); |
| 731 |
return (true); |
| 732 |
} |
| 733 |
|
| 734 |
/* If implicit undo truncation is turned off, or if the rsegs don't exist |
| 735 |
yet, don't bother checking the size. */ |
| 736 |
if (!srv_undo_log_truncate || m_rsegs == nullptr || m_rsegs->is_empty() || |
| 737 |
m_rsegs->is_init()) { |
| 738 |
m_rsegs->s_unlock(); |
| 739 |
return (false); |
| 740 |
} |
| 741 |
ut_ad(m_rsegs->is_active()); |
| 742 |
m_rsegs->s_unlock(); |
| 743 |
|
| 744 |
/* Check if undo truncation is happening so often that too many pages |
| 745 |
from old space IDs are still in memory. Since undo spaces are deleted |
| 746 |
with BUF_REMOVE_NONE, the actual space is not deleted for that old |
| 747 |
space ID until all pages have been passively removed from the buffer |
| 748 |
pool. */ |
| 749 |
auto count = fil_count_undo_deleted(undo::id2num(m_id)); |
| 750 |
if (count > CONCURRENT_UNDO_TRUNCATE_LIMIT) { |
| 751 |
ib::warn(ER_IB_MSG_UNDO_TRUNCATE_TOO_OFTEN); |
| 752 |
return (false); |
| 753 |
} |
| 754 |
|
| 755 |
ut_ad(fil_space_get_undo_initial_size(m_id) != 0); |
| 756 |
|
| 757 |
page_no_t trunc_size = std::max( |
| 758 |
static_cast<page_no_t>(srv_max_undo_tablespace_size / srv_page_size), |
| 759 |
fil_space_get_undo_initial_size(m_id)); |
| 760 |
|
| 761 |
if (fil_space_get_size(m_id) > trunc_size) { |
| 762 |
return (true); |
| 763 |
} |
| 764 |
|
| 765 |
return (false); |
| 766 |
} |
| 767 |
|
| 768 |
/** Change the space_id from its current value. |
| 769 |
@param[in] space_id The new undo tablespace ID */ |
| 770 |
void Tablespace::set_space_id(space_id_t space_id) { |
| 771 |
ut_ad(m_num == id2num(space_id)); |
| 772 |
m_id = space_id; |
| 773 |
} |
| 774 |
|
| 775 |
/** Build a standard undo tablespace name from a space_id. |
| 776 |
@param[in] space_id id of the undo tablespace. |
| 777 |
@return tablespace name of the undo tablespace file */ |
| 778 |
char *make_space_name(space_id_t space_id) { |
| 779 |
/* 8.0 undo tablespace names have an extra '_' */ |
| 780 |
bool old = (id2num(space_id) == space_id); |
| 781 |
|
| 782 |
size_t size = sizeof(undo_space_name) + 3 + (old ? 0 : 1); |
| 783 |
|
| 784 |
char *name = |
| 785 |
static_cast<char *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, size)); |
| 786 |
|
| 787 |
snprintf(name, size, (old ? "%s%03" SPACE_ID_PFS : "%s_%03" SPACE_ID_PFS), |
| 788 |
undo_space_name, static_cast<unsigned>(id2num(space_id))); |
| 789 |
|
| 790 |
return (name); |
| 791 |
} |
| 792 |
|
| 793 |
/** Build a standard undo tablespace file name from a space_id. |
| 794 |
This will create a name like 'undo_001' if the space_id is in the |
| 795 |
reserved range, else it will be like 'undo001'. |
| 796 |
@param[in] space_id id of the undo tablespace. |
| 797 |
@return file_name of the undo tablespace file */ |
| 798 |
char *make_file_name(space_id_t space_id) { |
| 799 |
/* 8.0 undo tablespace names have an extra '_' */ |
| 800 |
size_t len = strlen(srv_undo_dir); |
| 801 |
bool with_sep = (srv_undo_dir[len - 1] == OS_PATH_SEPARATOR); |
| 802 |
bool old = (id2num(space_id) == space_id); |
| 803 |
|
| 804 |
size_t size = strlen(srv_undo_dir) + (with_sep ? 0 : 1) + sizeof("undo000") + |
| 805 |
(old ? 0 : 1); |
| 806 |
|
| 807 |
char *name = |
| 808 |
static_cast<char *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, size)); |
| 809 |
|
| 810 |
memcpy(name, srv_undo_dir, len); |
| 811 |
|
| 812 |
if (!with_sep) { |
| 813 |
name[len++] = OS_PATH_SEPARATOR; |
| 814 |
} |
| 815 |
|
| 816 |
memcpy(&name[len], "undo", 4); |
| 817 |
len += 4; |
| 818 |
|
| 819 |
if (!old) { |
| 820 |
name[len++] = '_'; |
| 821 |
} |
| 822 |
|
| 823 |
snprintf(&name[len], size - len, "%03" SPACE_ID_PFS, |
| 824 |
static_cast<unsigned>(id2num(space_id))); |
| 825 |
|
| 826 |
return (name); |
| 827 |
} |
| 828 |
|
| 829 |
void Tablespace::set_space_name(const char *new_space_name) { |
| 830 |
if (m_space_name != nullptr) { |
| 831 |
ut::free(m_space_name); |
| 832 |
m_space_name = nullptr; |
| 833 |
} |
| 834 |
|
| 835 |
size_t size = strlen(new_space_name) + 1; |
| 836 |
m_space_name = |
| 837 |
static_cast<char *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, size)); |
| 838 |
|
| 839 |
strncpy(m_space_name, new_space_name, size); |
| 840 |
} |
| 841 |
|
| 842 |
void Tablespace::set_file_name(const char *file_name) { |
| 843 |
/* Make a copy of the filename and normalize it. */ |
| 844 |
char norm_fn[FN_REFLEN]; |
| 845 |
strncpy(norm_fn, file_name, FN_REFLEN - 1); |
| 846 |
Fil_path::normalize(norm_fn); |
| 847 |
std::string tmp_fn{norm_fn}; |
| 848 |
|
| 849 |
/* Explicit undo tablespaces use an IBU extension. */ |
| 850 |
m_implicit = (Fil_path::has_suffix(IBU, tmp_fn) ? false : true); |
| 851 |
|
| 852 |
/* This name can come in three forms: absolute path, relative path, |
| 853 |
and basename. ADD DATAFILE for undo tablespaces does not accept a |
| 854 |
relative path. If a relative path comes in here, it was the scanned |
| 855 |
name and is relative to the datadir. So only prepend the undo_dir if |
| 856 |
this is just a basename. */ |
| 857 |
std::string final_fn; |
| 858 |
if (tmp_fn.find_first_of(":/\\") == std::string::npos) { |
| 859 |
/* Prepend the undo directory. */ |
| 860 |
bool is_circ = MySQL_undo_path.is_circular(); |
| 861 |
final_fn += (is_circ ? MySQL_undo_path.abs_path() : MySQL_undo_path.path()); |
| 862 |
char back = (is_circ ? MySQL_undo_path.abs_path().back() |
| 863 |
: MySQL_undo_path.path().back()); |
| 864 |
final_fn += (back == OS_PATH_SEPARATOR ? "" : OS_PATH_SEPARATOR_STR); |
| 865 |
} |
| 866 |
final_fn += tmp_fn; |
| 867 |
|
| 868 |
/* We are going to replace any existing m_file_name. */ |
| 869 |
if (m_file_name != nullptr) { |
| 870 |
ut::free(m_file_name); |
| 871 |
} |
| 872 |
|
| 873 |
size_t len = final_fn.size(); |
| 874 |
m_file_name = static_cast<char *>( |
| 875 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len + 1)); |
| 876 |
memcpy(m_file_name, final_fn.c_str(), len); |
| 877 |
m_file_name[len] = '\0'; |
| 878 |
} |
| 879 |
|
| 880 |
char *Tablespace::make_log_file_name(space_id_t space_id, |
| 881 |
const char *location) { |
| 882 |
size_t size = strlen(location) + 22 + 1 /* NUL */ |
| 883 |
+ strlen(undo::s_log_prefix) + strlen(undo::s_log_ext); |
| 884 |
|
| 885 |
char *name = |
| 886 |
static_cast<char *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, size)); |
| 887 |
|
| 888 |
memset(name, 0, size); |
| 889 |
|
| 890 |
strcpy(name, location); |
| 891 |
ulint len = strlen(name); |
| 892 |
|
| 893 |
if (name[len - 1] != OS_PATH_SEPARATOR) { |
| 894 |
name[len] = OS_PATH_SEPARATOR; |
| 895 |
len = strlen(name); |
| 896 |
} |
| 897 |
|
| 898 |
snprintf(name + len, size - len, "%s%lu_%s", undo::s_log_prefix, |
| 899 |
(ulong)id2num(space_id), s_log_ext); |
| 900 |
|
| 901 |
return (name); |
| 902 |
} |
| 903 |
|
| 904 |
void Tablespace::alter_active() { |
| 905 |
m_rsegs->x_lock(); |
| 906 |
ut_d(ib::info(ER_IB_MSG_UNDO_ALTERED_ACTIVE, file_name())); |
| 907 |
if (m_rsegs->is_empty()) { |
| 908 |
m_rsegs->set_active(); |
| 909 |
} else if (m_rsegs->is_inactive_explicit()) { |
| 910 |
if (purge_sys->undo_trunc.get_marked_space_num() == m_num) { |
| 911 |
m_rsegs->set_inactive_implicit(); |
| 912 |
} else { |
| 913 |
m_rsegs->set_active(); |
| 914 |
} |
| 915 |
} |
| 916 |
m_rsegs->x_unlock(); |
| 917 |
} |
| 918 |
|
| 919 |
#ifdef UNIV_DEBUG |
| 920 |
void inject_crash(const char *injection_point_name) { |
| 921 |
DBUG_EXECUTE_IF(injection_point_name, |
| 922 |
ib::info(ER_IB_MSG_INJECT_CRASH, injection_point_name); |
| 923 |
log_buffer_flush_to_disk(); DBUG_SUICIDE();); |
| 924 |
} |
| 925 |
|
| 926 |
bool Inject_failure_once::should_fail() { |
| 927 |
DBUG_EXECUTE_IF(m_inject_name, { |
| 928 |
if (!m_already_failed) { |
| 929 |
m_already_failed = true; |
| 930 |
ib::info(ER_IB_MSG_INJECT_FAILURE, m_inject_name); |
| 931 |
return true; |
| 932 |
} |
| 933 |
}); |
| 934 |
return false; |
| 935 |
} |
| 936 |
|
| 937 |
#endif /* UNIV_DEBUG */ |
| 938 |
|
| 939 |
dberr_t start_logging(Tablespace *undo_space) { |
| 940 |
#ifdef UNIV_DEBUG |
| 941 |
static undo::Inject_failure_once injector("ib_undo_trunc_fail_start_logging"); |
| 942 |
if (injector.should_fail()) { |
| 943 |
return (DB_OUT_OF_MEMORY); |
| 944 |
} |
| 945 |
#endif /* UNIV_DEBUG */ |
| 946 |
|
| 947 |
dberr_t err; |
| 948 |
char *log_file_name = undo_space->log_file_name(); |
| 949 |
|
| 950 |
/* Delete the log file if it exists. */ |
| 951 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 952 |
|
| 953 |
/* Create the log file, open it and write 0 to indicate |
| 954 |
init phase. */ |
| 955 |
bool ret; |
| 956 |
pfs_os_file_t handle = |
| 957 |
os_file_create(innodb_log_file_key, log_file_name, OS_FILE_CREATE, |
| 958 |
OS_LOG_FILE, srv_read_only_mode, &ret); |
| 959 |
if (!ret) { |
| 960 |
return (DB_IO_ERROR); |
| 961 |
} |
| 962 |
|
| 963 |
ulint sz = UNIV_PAGE_SIZE; |
| 964 |
void *buf = ut::aligned_zalloc(sz, UNIV_PAGE_SIZE); |
| 965 |
if (buf == nullptr) { |
| 966 |
os_file_close(handle); |
| 967 |
return (DB_OUT_OF_MEMORY); |
| 968 |
} |
| 969 |
|
| 970 |
IORequest request(IORequest::WRITE); |
| 971 |
|
| 972 |
request.disable_compression(); |
| 973 |
|
| 974 |
err = os_file_write(request, log_file_name, handle, buf, 0, sz); |
| 975 |
|
| 976 |
os_file_flush(handle); |
| 977 |
os_file_close(handle); |
| 978 |
ut::aligned_free(buf); |
| 979 |
|
| 980 |
return (err); |
| 981 |
} |
| 982 |
|
| 983 |
/** Mark completion of undo truncate action by writing magic number |
| 984 |
to the log file and then removing it from the disk. |
| 985 |
If we are going to remove it from disk then why write magic number? |
| 986 |
This is to safeguard from unlink (file-system) anomalies that will |
| 987 |
keep the link to the file even after unlink action is successful |
| 988 |
and ref-count = 0. |
| 989 |
@param[in] space_num number of the undo tablespace to truncate. */ |
| 990 |
void done_logging(space_id_t space_num) { |
| 991 |
dberr_t err; |
| 992 |
/* Calling id2num(space_num) will return the first space_id for this |
| 993 |
space_num. That is good enough since we only need the log_file_name. */ |
| 994 |
Tablespace undo_space(id2num(space_num)); |
| 995 |
char *log_file_name = undo_space.log_file_name(); |
| 996 |
|
| 997 |
/* If this file does not exist, there is nothing to do. */ |
| 998 |
if (!os_file_exists(log_file_name)) { |
| 999 |
return; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* Open log file and write magic number to indicate |
| 1003 |
done phase. */ |
| 1004 |
bool ret; |
| 1005 |
pfs_os_file_t handle = os_file_create_simple_no_error_handling( |
| 1006 |
innodb_log_file_key, log_file_name, OS_FILE_OPEN, OS_FILE_READ_WRITE, |
| 1007 |
srv_read_only_mode, &ret); |
| 1008 |
|
| 1009 |
if (!ret) { |
| 1010 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 1011 |
return; |
| 1012 |
} |
| 1013 |
|
| 1014 |
ulint sz = UNIV_PAGE_SIZE; |
| 1015 |
byte *buf = static_cast<byte *>(ut::aligned_zalloc(sz, UNIV_PAGE_SIZE)); |
| 1016 |
if (buf == nullptr) { |
| 1017 |
os_file_close(handle); |
| 1018 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 1019 |
return; |
| 1020 |
} |
| 1021 |
|
| 1022 |
mach_write_to_4(buf, undo::s_magic); |
| 1023 |
|
| 1024 |
IORequest request(IORequest::WRITE); |
| 1025 |
|
| 1026 |
request.disable_compression(); |
| 1027 |
|
| 1028 |
err = os_file_write(request, log_file_name, handle, buf, 0, sz); |
| 1029 |
|
| 1030 |
ut_a(err == DB_SUCCESS); |
| 1031 |
|
| 1032 |
os_file_flush(handle); |
| 1033 |
os_file_close(handle); |
| 1034 |
|
| 1035 |
ut::aligned_free(buf); |
| 1036 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 1037 |
} |
| 1038 |
|
| 1039 |
/** Check if TRUNCATE_DDL_LOG file exist. |
| 1040 |
@param[in] space_num undo tablespace number |
| 1041 |
@return true if exist else false. */ |
| 1042 |
bool is_active_truncate_log_present(space_id_t space_num) { |
| 1043 |
/* Calling id2num(space_num) will return the first space_id for this |
| 1044 |
space_num. That is good enough since we only need the log_file_name. */ |
| 1045 |
Tablespace undo_space(id2num(space_num)); |
| 1046 |
|
| 1047 |
/* The truncation log file location changed to a new default location. |
| 1048 |
Check if it exists in either location. */ |
| 1049 |
char *log_file_name = undo_space.log_file_name(); |
| 1050 |
if (!os_file_exists(log_file_name)) { |
| 1051 |
log_file_name = undo_space.log_file_name_old(); |
| 1052 |
if (!os_file_exists(log_file_name)) { |
| 1053 |
log_file_name = nullptr; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* If the log file exists, check it for presence of magic |
| 1058 |
number. If found, then delete the file and report file |
| 1059 |
doesn't exist as presence of magic number suggest that |
| 1060 |
truncate action was complete. */ |
| 1061 |
if (log_file_name != nullptr) { |
| 1062 |
bool ret; |
| 1063 |
pfs_os_file_t handle = os_file_create_simple_no_error_handling( |
| 1064 |
innodb_log_file_key, log_file_name, OS_FILE_OPEN, OS_FILE_READ_WRITE, |
| 1065 |
srv_read_only_mode, &ret); |
| 1066 |
if (!ret) { |
| 1067 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 1068 |
return (false); |
| 1069 |
} |
| 1070 |
|
| 1071 |
ulint sz = UNIV_PAGE_SIZE; |
| 1072 |
byte *buf = static_cast<byte *>(ut::aligned_zalloc(sz, UNIV_PAGE_SIZE)); |
| 1073 |
if (buf == nullptr) { |
| 1074 |
os_file_close(handle); |
| 1075 |
os_file_delete_if_exists(innodb_log_file_key, log_file_name, nullptr); |
| 1076 |
return (false); |
| 1077 |
} |
| 1078 |
|
| 1079 |
IORequest request(IORequest::READ); |
| 1080 |
|
| 1081 |
request.disable_compression(); |
| 1082 |
|
| 1083 |
dberr_t err; |
| 1084 |
|
| 1085 |
err = os_file_read(request, log_file_name, handle, buf, 0, sz); |
| 1086 |
|
| 1087 |
os_file_close(handle); |
| 1088 |
|
| 1089 |
if (err != DB_SUCCESS) { |
| 1090 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_FAIL_TO_READ_LOG_FILE, log_file_name, |
| 1091 |
ut_strerr(err)); |
| 1092 |
|
| 1093 |
os_file_delete(innodb_log_file_key, log_file_name); |
| 1094 |
|
| 1095 |
ut::aligned_free(buf); |
| 1096 |
|
| 1097 |
return (false); |
| 1098 |
} |
| 1099 |
|
| 1100 |
ulint magic_no = mach_read_from_4(buf); |
| 1101 |
|
| 1102 |
ut::aligned_free(buf); |
| 1103 |
|
| 1104 |
if (magic_no == undo::s_magic) { |
| 1105 |
/* Found magic number. */ |
| 1106 |
os_file_delete(innodb_log_file_key, log_file_name); |
| 1107 |
return (false); |
| 1108 |
} |
| 1109 |
|
| 1110 |
return (true); |
| 1111 |
} |
| 1112 |
|
| 1113 |
return (false); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/** Add undo tablespace to s_under_construction vector. |
| 1117 |
@param[in] space_id space id of tablespace to |
| 1118 |
truncate */ |
| 1119 |
void add_space_to_construction_list(space_id_t space_id) { |
| 1120 |
s_under_construction.push_back(space_id); |
| 1121 |
} |
| 1122 |
|
| 1123 |
/** Clear the s_under_construction vector. */ |
| 1124 |
void clear_construction_list() { s_under_construction.clear(); } |
| 1125 |
|
| 1126 |
/** Is an undo tablespace under construction at the moment. |
| 1127 |
@param[in] space_id space id to check |
| 1128 |
@return true if marked for truncate, else false. */ |
| 1129 |
bool is_under_construction(space_id_t space_id) { |
| 1130 |
for (auto construct_id : s_under_construction) { |
| 1131 |
if (construct_id == space_id) { |
| 1132 |
return (true); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
return (false); |
| 1137 |
} |
| 1138 |
|
| 1139 |
/* Set an undo tablespace active. */ |
| 1140 |
void set_active(space_id_t space_id) { |
| 1141 |
ut_ad(spaces != nullptr); |
| 1142 |
ut_ad(fsp_is_undo_tablespace(space_id)); |
| 1143 |
|
| 1144 |
spaces->s_lock(); |
| 1145 |
Tablespace *undo_space = spaces->find(id2num(space_id)); |
| 1146 |
|
| 1147 |
if (undo_space != nullptr) { |
| 1148 |
undo_space->set_active(); |
| 1149 |
} |
| 1150 |
spaces->s_unlock(); |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* Return whether the undo tablespace is active. If this is a |
| 1154 |
non-undo tablespace, then it will not be found in spaces and it |
| 1155 |
will not be under construction, so this function will return true. |
| 1156 |
@param[in] space_id Undo Tablespace ID |
| 1157 |
@param[in] get_latch Specifies whether the rsegs->s_lock() is needed. |
| 1158 |
@return true if active (non-undo spaces are always active) */ |
| 1159 |
bool is_active(space_id_t space_id, bool get_latch) { |
| 1160 |
if (!fsp_is_undo_tablespace(space_id)) { |
| 1161 |
return (true); |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (spaces == nullptr) { |
| 1165 |
return (!is_under_construction(space_id)); |
| 1166 |
} |
| 1167 |
|
| 1168 |
if (get_latch) { |
| 1169 |
undo::spaces->s_lock(); |
| 1170 |
} |
| 1171 |
Tablespace *undo_space = spaces->find(id2num(space_id)); |
| 1172 |
|
| 1173 |
if (undo_space == nullptr) { |
| 1174 |
if (get_latch) { |
| 1175 |
undo::spaces->s_unlock(); |
| 1176 |
} |
| 1177 |
return (!is_under_construction(space_id)); |
| 1178 |
} |
| 1179 |
|
| 1180 |
bool ret = |
| 1181 |
(get_latch ? undo_space->is_active() : undo_space->is_active_no_latch()); |
| 1182 |
|
| 1183 |
if (get_latch) { |
| 1184 |
undo::spaces->s_unlock(); |
| 1185 |
} |
| 1186 |
|
| 1187 |
return (ret); |
| 1188 |
} |
| 1189 |
} // namespace undo |
| 1190 |
|
| 1191 |
/* Declare this global object. */ |
| 1192 |
Space_Ids undo::s_under_construction; |
| 1193 |
|
| 1194 |
/** Decide if an undo truncation needs to be done at this time. If an undo |
| 1195 |
tablespace is already marked, return that so that this truncation will get |
| 1196 |
finished. If none is marked, iterate over all the UNDO tablespaces and check |
| 1197 |
if any qualify to be truncated. |
| 1198 |
Normal operation; Choose the marked space. |
| 1199 |
If none are marked, choose an explicitly inactive space. |
| 1200 |
If none exist, check if conditions allow implicit |
| 1201 |
truncation. |
| 1202 |
If conditions allow, choose an implicitly inactive space. |
| 1203 |
If none exist and no space has been truncated yet, |
| 1204 |
look for an undo space that is too big. |
| 1205 |
Fast shutdown; Do not truncate. This routine is not called. |
| 1206 |
Slow shutdown; Choose the marked space. |
| 1207 |
If none are marked, choose an explicitly inactive |
| 1208 |
space. |
| 1209 |
If none exist, check if conditions allow implicit |
| 1210 |
truncation. |
| 1211 |
If conditions allow, ignoring the previous truncate count, |
| 1212 |
look for an undo space that is too big. |
| 1213 |
@param[in] truncate_count number of times this is called in a loop |
| 1214 |
@return true if an undo tablespace was marked for truncate. */ |
| 1215 |
static bool trx_purge_mark_undo_for_truncate(size_t truncate_count) { |
| 1216 |
/* We always have at least 2 undo spaces, even though one of them may be |
| 1217 |
inactive. */ |
| 1218 |
ut_a(undo::spaces->size() >= FSP_IMPLICIT_UNDO_TABLESPACES); |
| 1219 |
|
| 1220 |
/* Note if we are currently in a fast or slow shutdown. */ |
| 1221 |
bool normal_operation = (srv_shutdown_state == SRV_SHUTDOWN_NONE); |
| 1222 |
bool in_fast_shutdown = (!normal_operation && srv_fast_shutdown > 0); |
| 1223 |
|
| 1224 |
/* Save time during a fast shutdown by skipping undo truncation. |
| 1225 |
This does not affect correctness since undo tablespaces that need |
| 1226 |
truncation can be truncated during or after startup.*/ |
| 1227 |
if (in_fast_shutdown) { |
| 1228 |
return (false); |
| 1229 |
} |
| 1230 |
|
| 1231 |
/* Return true if an undo tablespace is already marked for truncate. */ |
| 1232 |
auto undo_trunc = &purge_sys->undo_trunc; |
| 1233 |
if (undo_trunc->is_marked()) { |
| 1234 |
return (true); |
| 1235 |
} |
| 1236 |
|
| 1237 |
undo::spaces->s_lock(); |
| 1238 |
|
| 1239 |
/* In order to implicitly select an undo space to truncate, we need |
| 1240 |
at least 2 active UNDO tablespaces. As long as there is one undo |
| 1241 |
tablespace active the server will continue to operate. */ |
| 1242 |
size_t num_active = 0; |
| 1243 |
|
| 1244 |
/* Look for any undo space that is inactive explicitly. */ |
| 1245 |
auto undo_ts = undo::spaces->find_first_inactive_explicit(&num_active); |
| 1246 |
if (undo_ts != nullptr) { |
| 1247 |
undo_trunc->mark(undo_ts); |
| 1248 |
undo::spaces->s_unlock(); |
| 1249 |
return (true); |
| 1250 |
} |
| 1251 |
|
| 1252 |
undo::spaces->s_unlock(); |
| 1253 |
|
| 1254 |
/* If we get here, there are no undo spaces currently being truncated |
| 1255 |
and none that are SET INACTIVE explicitly. */ |
| 1256 |
ut_a(num_active > 0); |
| 1257 |
|
| 1258 |
/* There may be some reasons not to truncate implicitly. |
| 1259 |
If truncate is disabled, do not truncate. */ |
| 1260 |
if (!srv_undo_log_truncate) { |
| 1261 |
return (false); |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (normal_operation) { |
| 1265 |
/* Skip truncate if there is only one active undo tablespace to check. */ |
| 1266 |
if (num_active == 1) { |
| 1267 |
return (false); |
| 1268 |
} |
| 1269 |
|
| 1270 |
/* Skip truncate if the caller has already truncated an undo space. */ |
| 1271 |
if (truncate_count > 0) { |
| 1272 |
return (false); |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* Wait at least one second between searches. */ |
| 1276 |
if (undo_trunc->check_timer() < PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS) { |
| 1277 |
return (false); |
| 1278 |
} |
| 1279 |
undo_trunc->reset_timer(); |
| 1280 |
} |
| 1281 |
|
| 1282 |
/* Find an undo tablespace that is too big and needs to be truncated. */ |
| 1283 |
undo::spaces->s_lock(); |
| 1284 |
|
| 1285 |
/* Avoid bias selection and so start the scan immediately after the |
| 1286 |
last space selected for truncate. Scan through all undo tablespaces. */ |
| 1287 |
space_id_t space_num = undo_trunc->get_scan_space_num(); |
| 1288 |
space_id_t first_space_num_scanned = space_num; |
| 1289 |
|
| 1290 |
do { |
| 1291 |
auto undo_space = undo::spaces->find(space_num); |
| 1292 |
|
| 1293 |
if (undo_space->needs_truncation()) { |
| 1294 |
/* Tablespace qualifies for truncate. */ |
| 1295 |
undo_trunc->increment_scan(); |
| 1296 |
undo_trunc->mark(undo_space); |
| 1297 |
break; |
| 1298 |
} |
| 1299 |
|
| 1300 |
space_num = undo_trunc->increment_scan(); |
| 1301 |
|
| 1302 |
} while (space_num != first_space_num_scanned); |
| 1303 |
|
| 1304 |
undo::spaces->s_unlock(); |
| 1305 |
|
| 1306 |
/* Return false if no undo space needs to be truncated. */ |
| 1307 |
if (!undo_trunc->is_marked()) { |
| 1308 |
return (false); |
| 1309 |
} |
| 1310 |
|
| 1311 |
ut_ad(space_num == undo_trunc->get_marked_space_num()); |
| 1312 |
|
| 1313 |
return (true); |
| 1314 |
} |
| 1315 |
|
| 1316 |
void undo::Truncate::mark(Tablespace *undo_space) { |
| 1317 |
/* Set the internal state of this undo space to inactive_implicit so that its |
| 1318 |
rsegs will not be allocated to any new transaction. |
| 1319 |
If the space is already in the inactive_explicit state, it will stay there. |
| 1320 |
Note that the DD is not modified since in case of crash, the action must be |
| 1321 |
completed before the DD is available. Set both the state and this marked id |
| 1322 |
while this routine has an x_lock on m_rsegs because a concurrent user thread |
| 1323 |
might issue undo_space->alter_active(). */ |
| 1324 |
undo_space->set_inactive_implicit(&m_space_id_marked); |
| 1325 |
|
| 1326 |
m_marked_space_is_empty = false; |
| 1327 |
|
| 1328 |
ut_d(ib::info(ER_IB_MSG_UNDO_MARKED_FOR_TRUNCATE, undo_space->file_name())); |
| 1329 |
} |
| 1330 |
|
| 1331 |
size_t undo::Truncate::s_scan_pos; |
| 1332 |
|
| 1333 |
/** Iterate over selected UNDO tablespace and check if all the rsegs |
| 1334 |
that resides in the tablespace have been freed. */ |
| 1335 |
static bool trx_purge_check_if_marked_undo_is_empty() { |
| 1336 |
undo::Truncate *undo_trunc = &purge_sys->undo_trunc; |
| 1337 |
|
| 1338 |
ut_ad(undo_trunc->is_marked()); |
| 1339 |
|
| 1340 |
/* Return immediately if the marked UNDO tablespace has already been |
| 1341 |
found to be empty. */ |
| 1342 |
if (undo_trunc->is_marked_space_empty()) { |
| 1343 |
return (true); |
| 1344 |
} |
| 1345 |
|
| 1346 |
undo::spaces->s_lock(); |
| 1347 |
space_id_t space_num = undo_trunc->get_marked_space_num(); |
| 1348 |
undo::Tablespace *marked_space = undo::spaces->find(space_num); |
| 1349 |
Rsegs *marked_rsegs = marked_space->rsegs(); |
| 1350 |
|
| 1351 |
/* Scan over each rseg in this inactive undo tablespace and ensure that it |
| 1352 |
does not hold any active undo records. */ |
| 1353 |
bool all_free = true; |
| 1354 |
|
| 1355 |
marked_rsegs->x_lock(); |
| 1356 |
|
| 1357 |
/* If an undo tablespace is marked, its rsegs are inactive. */ |
| 1358 |
ut_ad(!marked_rsegs->is_active()); |
| 1359 |
|
| 1360 |
for (auto rseg : *marked_rsegs) { |
| 1361 |
rseg->latch(); |
| 1362 |
|
| 1363 |
if (rseg->trx_ref_count > 0) { |
| 1364 |
/* This rseg is still being held by an active transaction. */ |
| 1365 |
all_free = false; |
| 1366 |
} else if (rseg->last_page_no != FIL_NULL) { |
| 1367 |
/* This rseg still has data to be purged. */ |
| 1368 |
all_free = false; |
| 1369 |
} |
| 1370 |
|
| 1371 |
rseg->unlatch(); |
| 1372 |
|
| 1373 |
if (!all_free) { |
| 1374 |
break; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (all_free) { |
| 1379 |
undo_trunc->set_marked_space_empty(); |
| 1380 |
} |
| 1381 |
|
| 1382 |
marked_rsegs->x_unlock(); |
| 1383 |
|
| 1384 |
undo::spaces->s_unlock(); |
| 1385 |
|
| 1386 |
return (all_free); |
| 1387 |
} |
| 1388 |
|
| 1389 |
/** Truncate the marked undo tablespace. This layer handles locking of |
| 1390 |
marked_rsegs and undo::spaces. |
| 1391 |
@return true for success, false for failure */ |
| 1392 |
static bool trx_purge_truncate_marked_undo_low(space_id_t space_num, |
| 1393 |
std::string space_name) { |
| 1394 |
undo::Truncate *undo_trunc = &purge_sys->undo_trunc; |
| 1395 |
|
| 1396 |
/* Get the undo space pointer again. */ |
| 1397 |
undo::spaces->s_lock(); |
| 1398 |
|
| 1399 |
undo::Tablespace *marked_space = undo::spaces->find(space_num); |
| 1400 |
|
| 1401 |
undo::spaces->s_unlock(); |
| 1402 |
|
| 1403 |
#ifdef UNIV_DEBUG |
| 1404 |
static undo::Inject_failure_once inject_marked_space( |
| 1405 |
"ib_undo_trunc_fail_marked_space"); |
| 1406 |
if (inject_marked_space.should_fail()) { |
| 1407 |
marked_space = nullptr; |
| 1408 |
}; |
| 1409 |
#endif /* UNIV_DEBUG */ |
| 1410 |
|
| 1411 |
if (marked_space == nullptr) { |
| 1412 |
return (false); |
| 1413 |
} |
| 1414 |
|
| 1415 |
ut_d(undo::inject_crash("ib_undo_trunc_before_ddl_log_start")); |
| 1416 |
|
| 1417 |
MONITOR_INC_VALUE(MONITOR_UNDO_TRUNCATE_START_LOGGING_COUNT, 1); |
| 1418 |
dberr_t err = undo::start_logging(marked_space); |
| 1419 |
if (err != DB_SUCCESS) { |
| 1420 |
ib::error(ER_IB_MSG_UNDO_TRUNCATE_DELAY_BY_LOG_CREATE, space_name.c_str()); |
| 1421 |
return (false); |
| 1422 |
} |
| 1423 |
ut_ad(err == DB_SUCCESS); |
| 1424 |
|
| 1425 |
ut_d(undo::inject_crash("ib_undo_trunc_before_truncate")); |
| 1426 |
|
| 1427 |
/* Don't do the actual truncate if we are doing a fast shutdown. |
| 1428 |
The fixup routines will do it at startup. */ |
| 1429 |
bool in_fast_shutdown = (srv_shutdown_state.load() != SRV_SHUTDOWN_NONE && |
| 1430 |
srv_fast_shutdown != 0); |
| 1431 |
#ifdef UNIV_DEBUG |
| 1432 |
static undo::Inject_failure_once inject_fast_shutdown( |
| 1433 |
"ib_undo_trunc_fail_fast_shutdown"); |
| 1434 |
if (inject_fast_shutdown.should_fail()) { |
| 1435 |
in_fast_shutdown = true; |
| 1436 |
}; |
| 1437 |
#endif /* UNIV_DEBUG */ |
| 1438 |
|
| 1439 |
if (in_fast_shutdown) { |
| 1440 |
return (false); |
| 1441 |
} |
| 1442 |
|
| 1443 |
Clone_notify notifier(Clone_notify::Type::SPACE_UNDO_DDL, marked_space->id(), |
| 1444 |
true); |
| 1445 |
|
| 1446 |
if (notifier.failed()) { |
| 1447 |
/* purecov: begin inspected */ |
| 1448 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_DELAY_BY_CLONE, space_name.c_str()); |
| 1449 |
return false; |
| 1450 |
/* purecov: end */ |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* Do the truncate. This will change the space_id of the marked_space. */ |
| 1454 |
bool success = trx_undo_truncate_tablespace(marked_space); |
| 1455 |
|
| 1456 |
if (!success) { |
| 1457 |
/* Note: In case of error we don't enable the rsegs nor unmark the |
| 1458 |
tablespace. So the tablespace will continue to remain inactive. */ |
| 1459 |
ib::warn(ER_IB_MSG_UNDO_TRUNCATE_DELAY_BY_FAILURE, space_name.c_str()); |
| 1460 |
return (false); |
| 1461 |
} |
| 1462 |
|
| 1463 |
ut_d(undo::inject_crash("ib_undo_trunc_before_state_update")); |
| 1464 |
|
| 1465 |
space_id_t new_space_id = marked_space->id(); |
| 1466 |
|
| 1467 |
/* Determine the next state. */ |
| 1468 |
dd_space_states next_state; |
| 1469 |
|
| 1470 |
undo::spaces->s_lock(); |
| 1471 |
|
| 1472 |
Rsegs *marked_rsegs = marked_space->rsegs(); |
| 1473 |
marked_rsegs->x_lock(); |
| 1474 |
|
| 1475 |
if (marked_rsegs->is_inactive_explicit()) { |
| 1476 |
next_state = DD_SPACE_STATE_EMPTY; |
| 1477 |
|
| 1478 |
/* This was made inactive and truncated due to an ALTER TABLESPACE SET |
| 1479 |
INACTIVE statement. Mark it empty now so that it can be DROPPED. */ |
| 1480 |
marked_rsegs->set_empty(); |
| 1481 |
ut_d(ib::info(ER_IB_MSG_UNDO_MARKED_EMPTY, marked_space->file_name())); |
| 1482 |
|
| 1483 |
} else { |
| 1484 |
ut_ad(marked_rsegs->is_inactive_implicit()); |
| 1485 |
next_state = DD_SPACE_STATE_ACTIVE; |
| 1486 |
|
| 1487 |
/* This was made inactive and truncated due to normal background undo |
| 1488 |
tablespace truncation. Make it 'active' again. */ |
| 1489 |
marked_rsegs->set_active(); |
| 1490 |
ut_d(ib::info(ER_IB_MSG_UNDO_MARKED_ACTIVE, marked_space->file_name())); |
| 1491 |
} |
| 1492 |
|
| 1493 |
marked_rsegs->x_unlock(); |
| 1494 |
undo::spaces->s_unlock(); |
| 1495 |
|
| 1496 |
undo_trunc->reset(); |
| 1497 |
|
| 1498 |
ut_d(undo::inject_crash("ib_undo_trunc_before_dd_update")); |
| 1499 |
|
| 1500 |
/* Update the DD with the new space ID and state. */ |
| 1501 |
if (DD_FAILURE == dd_tablespace_set_id_and_state(space_name.c_str(), |
| 1502 |
new_space_id, next_state)) { |
| 1503 |
return (false); |
| 1504 |
} |
| 1505 |
|
| 1506 |
return (true); |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** Truncate the marked undo tablespace. |
| 1510 |
This wrapper does initial preparation and handles cleanup. |
| 1511 |
@return true for success, false for failure */ |
| 1512 |
static bool trx_purge_truncate_marked_undo() { |
| 1513 |
MONITOR_INC_VALUE(MONITOR_UNDO_TRUNCATE_COUNT, 1); |
| 1514 |
auto counter_time_truncate = std::chrono::steady_clock::now(); |
| 1515 |
|
| 1516 |
/* Initialize variables */ |
| 1517 |
undo::Truncate *undo_trunc = &purge_sys->undo_trunc; |
| 1518 |
ut_ad(undo_trunc->is_marked()); |
| 1519 |
ut_ad(undo_trunc->is_marked_space_empty()); |
| 1520 |
|
| 1521 |
undo::spaces->s_lock(); |
| 1522 |
space_id_t space_num = undo_trunc->get_marked_space_num(); |
| 1523 |
undo::Tablespace *marked_space = undo::spaces->find(space_num); |
| 1524 |
std::string space_name = marked_space->space_name(); |
| 1525 |
undo::spaces->s_unlock(); |
| 1526 |
|
| 1527 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_START, space_name.c_str()); |
| 1528 |
|
| 1529 |
ut_d(undo::inject_crash("ib_undo_trunc_before_mdl")); |
| 1530 |
|
| 1531 |
/* Get the MDL lock to prevent an ALTER or DROP command from interfering |
| 1532 |
with this undo tablespace while it is being truncated. */ |
| 1533 |
MDL_ticket *mdl_ticket; |
| 1534 |
bool dd_result = |
| 1535 |
dd_tablespace_get_mdl(space_name.c_str(), &mdl_ticket, false); |
| 1536 |
|
| 1537 |
#ifdef UNIV_DEBUG |
| 1538 |
static undo::Inject_failure_once injector("ib_undo_trunc_fail_get_mdl"); |
| 1539 |
if (injector.should_fail()) { |
| 1540 |
dd_release_mdl(mdl_ticket); |
| 1541 |
dd_result = DD_FAILURE; |
| 1542 |
}; |
| 1543 |
#endif /* UNIV_DEBUG */ |
| 1544 |
|
| 1545 |
if (dd_result != DD_SUCCESS) { |
| 1546 |
MONITOR_INC_TIME(MONITOR_UNDO_TRUNCATE_MICROSECOND, counter_time_truncate); |
| 1547 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_DELAY_BY_MDL, space_name.c_str()); |
| 1548 |
return (false); |
| 1549 |
} |
| 1550 |
ut_ad(mdl_ticket != nullptr); |
| 1551 |
|
| 1552 |
/* Re-check for clone after acquiring MDL. The Backup MDL from clone is |
| 1553 |
released by clone during shutdown while provisioning. We should not allow |
| 1554 |
truncate to proceed here. */ |
| 1555 |
if (clone_check_provisioning()) { |
| 1556 |
dd_release_mdl(mdl_ticket); |
| 1557 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_DELAY_BY_CLONE, space_name.c_str()); |
| 1558 |
return (false); |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* Serialize this truncate with all undo tablespace DDLs */ |
| 1562 |
mutex_enter(&undo::ddl_mutex); |
| 1563 |
|
| 1564 |
if (!trx_purge_truncate_marked_undo_low(space_num, space_name)) { |
| 1565 |
mutex_exit(&undo::ddl_mutex); |
| 1566 |
dd_release_mdl(mdl_ticket); |
| 1567 |
MONITOR_INC_TIME(MONITOR_UNDO_TRUNCATE_MICROSECOND, counter_time_truncate); |
| 1568 |
return (false); |
| 1569 |
} |
| 1570 |
|
| 1571 |
ut_d(undo::inject_crash("ib_undo_trunc_before_done_logging")); |
| 1572 |
|
| 1573 |
undo::spaces->x_lock(); |
| 1574 |
undo::done_logging(space_num); |
| 1575 |
undo::spaces->x_unlock(); |
| 1576 |
MONITOR_INC_VALUE(MONITOR_UNDO_TRUNCATE_DONE_LOGGING_COUNT, 1); |
| 1577 |
|
| 1578 |
/* Truncate is complete. Now it is safe to re-use the tablespace. */ |
| 1579 |
ib::info(ER_IB_MSG_UNDO_TRUNCATE_COMPLETE, space_name.c_str()); |
| 1580 |
|
| 1581 |
dd_release_mdl(mdl_ticket); |
| 1582 |
|
| 1583 |
ut_d(undo::inject_crash("ib_undo_trunc_done")); |
| 1584 |
|
| 1585 |
mutex_exit(&undo::ddl_mutex); |
| 1586 |
|
| 1587 |
MONITOR_INC_TIME(MONITOR_UNDO_TRUNCATE_MICROSECOND, counter_time_truncate); |
| 1588 |
return (true); |
| 1589 |
} |
| 1590 |
|
| 1591 |
/** Removes unnecessary history data from rollback segments. |
| 1592 |
NOTE that when this function is called, the caller must not |
| 1593 |
have any latches on undo log pages! |
| 1594 |
@param[in] limit Truncate limit |
| 1595 |
@param[in] view Purge view */ |
| 1596 |
static void trx_purge_truncate_history(purge_iter_t *limit, |
| 1597 |
const ReadView *view) { |
| 1598 |
MONITOR_INC_VALUE(MONITOR_PURGE_TRUNCATE_HISTORY_COUNT, 1); |
| 1599 |
|
| 1600 |
auto counter_time_truncate_history = std::chrono::steady_clock::now(); |
| 1601 |
|
| 1602 |
/* We play safe and set the truncate limit at most to the purge view |
| 1603 |
low_limit number, though this is not necessary */ |
| 1604 |
|
| 1605 |
if (limit->trx_no >= view->low_limit_no()) { |
| 1606 |
limit->trx_no = view->low_limit_no(); |
| 1607 |
limit->undo_no = 0; |
| 1608 |
limit->undo_rseg_space = SPACE_UNKNOWN; |
| 1609 |
} |
| 1610 |
|
| 1611 |
ut_ad(limit->trx_no <= purge_sys->view.low_limit_no()); |
| 1612 |
|
| 1613 |
/* Purge rollback segments in all undo tablespaces. This may take |
| 1614 |
some time and we do not want an undo DDL to attempt an x_lock during |
| 1615 |
this time. If it did, all other transactions seeking a short s_lock() |
| 1616 |
would line up behind it. So get the ddl_mutex before this s_lock(). */ |
| 1617 |
mutex_enter(&undo::ddl_mutex); |
| 1618 |
undo::spaces->s_lock(); |
| 1619 |
for (auto undo_space : undo::spaces->m_spaces) { |
| 1620 |
/* Skip undo tablespace that is already empty and marked for truncation. */ |
| 1621 |
undo::Truncate &ut = purge_sys->undo_trunc; |
| 1622 |
|
| 1623 |
if (ut.is_equal(undo_space->id()) && ut.is_marked() && |
| 1624 |
ut.is_marked_space_empty()) { |
| 1625 |
continue; |
| 1626 |
} |
| 1627 |
|
| 1628 |
/* Purge rollback segments in this undo tablespace. */ |
| 1629 |
undo_space->rsegs()->s_lock(); |
| 1630 |
|
| 1631 |
for (auto rseg : *undo_space->rsegs()) { |
| 1632 |
trx_purge_truncate_rseg_history(rseg, limit); |
| 1633 |
} |
| 1634 |
undo_space->rsegs()->s_unlock(); |
| 1635 |
} |
| 1636 |
|
| 1637 |
undo::spaces->s_unlock(); |
| 1638 |
mutex_exit(&undo::ddl_mutex); |
| 1639 |
|
| 1640 |
/* Purge rollback segments in the system tablespace, if any. |
| 1641 |
Use an s-lock for the whole list since it can have gaps and |
| 1642 |
may be sorted when added to. */ |
| 1643 |
trx_sys->rsegs.s_lock(); |
| 1644 |
for (auto rseg : trx_sys->rsegs) { |
| 1645 |
trx_purge_truncate_rseg_history(rseg, limit); |
| 1646 |
} |
| 1647 |
trx_sys->rsegs.s_unlock(); |
| 1648 |
|
| 1649 |
/* Purge rollback segments in the temporary tablespace. */ |
| 1650 |
trx_sys->tmp_rsegs.s_lock(); |
| 1651 |
for (auto rseg : trx_sys->tmp_rsegs) { |
| 1652 |
trx_purge_truncate_rseg_history(rseg, limit); |
| 1653 |
} |
| 1654 |
trx_sys->tmp_rsegs.s_unlock(); |
| 1655 |
|
| 1656 |
MONITOR_INC_TIME(MONITOR_PURGE_TRUNCATE_HISTORY_MICROSECOND, |
| 1657 |
counter_time_truncate_history); |
| 1658 |
} |
| 1659 |
|
| 1660 |
/** Select an undo tablespace to truncate, make sure it is empty of undo logs, |
| 1661 |
then finally truncate it. */ |
| 1662 |
static void trx_purge_truncate_undo_spaces() { |
| 1663 |
/* If the server has been started for the purpose of upgrading from a |
| 1664 |
previous version, do not do undo truncation. */ |
| 1665 |
if (srv_is_upgrade_mode) { |
| 1666 |
return; |
| 1667 |
} |
| 1668 |
|
| 1669 |
auto &undo_trunc = purge_sys->undo_trunc; |
| 1670 |
|
| 1671 |
/* Truncate as many undo spaces as can be truncated. |
| 1672 |
Break the loop and return whenever the process cannot be completed. */ |
| 1673 |
for (size_t i = 0; i < undo::spaces->size(); ++i) { |
| 1674 |
/* Check current activity and if conditions allow, mark the undo space that |
| 1675 |
needs to be truncated. */ |
| 1676 |
if (!trx_purge_mark_undo_for_truncate(i)) { |
| 1677 |
break; /* No truncation is needed at this time. */ |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* A space was marked but may not be yet empty. */ |
| 1681 |
ut_a(undo_trunc.is_marked()); |
| 1682 |
|
| 1683 |
/* If any undo logs need to be purged from this marked space, try again |
| 1684 |
later. */ |
| 1685 |
if (!trx_purge_check_if_marked_undo_is_empty()) { |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
|
| 1689 |
/* A space has been marked and is now empty. */ |
| 1690 |
ut_a(undo_trunc.is_marked_space_empty()); |
| 1691 |
|
| 1692 |
/* Truncate the marked space. */ |
| 1693 |
if (!trx_purge_truncate_marked_undo()) { |
| 1694 |
/* If the marked and empty space did not get truncated due to a concurrent |
| 1695 |
clone or something else, try again later. */ |
| 1696 |
break; |
| 1697 |
} |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
/** Updates the last not yet purged history log info in rseg when we have purged |
| 1702 |
a whole undo log. Advances also purge_sys->purge_trx_no past the purged log. */ |
| 1703 |
static void trx_purge_rseg_get_next_history_log( |
| 1704 |
trx_rseg_t *rseg, /*!< in: rollback segment */ |
| 1705 |
ulint *n_pages_handled) /*!< in/out: number of UNDO pages |
| 1706 |
handled */ |
| 1707 |
{ |
| 1708 |
mtr_t mtr; |
| 1709 |
|
| 1710 |
rseg->latch(); |
| 1711 |
|
| 1712 |
ut_a(rseg->last_page_no != FIL_NULL); |
| 1713 |
|
| 1714 |
purge_sys->iter.trx_no = rseg->last_trx_no + 1; |
| 1715 |
purge_sys->iter.undo_no = 0; |
| 1716 |
purge_sys->iter.undo_rseg_space = SPACE_UNKNOWN; |
| 1717 |
purge_sys->next_stored = false; |
| 1718 |
|
| 1719 |
mtr_start(&mtr); |
| 1720 |
|
| 1721 |
auto undo_page = trx_undo_page_get_s_latched( |
| 1722 |
page_id_t(rseg->space_id, rseg->last_page_no), rseg->page_size, &mtr); |
| 1723 |
|
| 1724 |
auto log_hdr = undo_page + rseg->last_offset; |
| 1725 |
|
| 1726 |
/* Increase the purge page count by one for every handled log */ |
| 1727 |
|
| 1728 |
(*n_pages_handled)++; |
| 1729 |
|
| 1730 |
auto prev_log_addr = trx_purge_get_log_from_hist( |
| 1731 |
flst_get_prev_addr(log_hdr + TRX_UNDO_HISTORY_NODE, &mtr)); |
| 1732 |
|
| 1733 |
if (prev_log_addr.page == FIL_NULL) { |
| 1734 |
/* No logs left in the history list */ |
| 1735 |
|
| 1736 |
rseg->last_page_no = FIL_NULL; |
| 1737 |
|
| 1738 |
mtr_commit(&mtr); |
| 1739 |
rseg->unlatch(); |
| 1740 |
|
| 1741 |
#ifdef UNIV_DEBUG |
| 1742 |
/* Add debug code to track history list corruption reported on the MySQL |
| 1743 |
mailing list on Nov 9, 2004. The fut0lst.cc file-based list was corrupt. The |
| 1744 |
prev node pointer was FIL_NULL, even though the list length was over 8 |
| 1745 |
million nodes! We assume that purge truncates the history list in large size |
| 1746 |
pieces, and if we here reach the head of the list, the |
| 1747 |
list cannot be longer than 2000 000 undo logs now. */ |
| 1748 |
|
| 1749 |
const auto rseg_history_len = trx_sys->rseg_history_len.load(); |
| 1750 |
if (rseg_history_len > 2000000) { |
| 1751 |
ib::warn(ER_IB_MSG_1177) |
| 1752 |
<< "Purge reached the head of the history" |
| 1753 |
" list, but its length is still reported as " |
| 1754 |
<< rseg_history_len << " which is unusually high."; |
| 1755 |
ib::info(ER_IB_MSG_1178) << "This can happen for multiple reasons"; |
| 1756 |
ib::info(ER_IB_MSG_1179) << "1. A long running transaction is" |
| 1757 |
" withholding purging of undo logs or a read" |
| 1758 |
" view is open. Please try to commit the long" |
| 1759 |
" running transaction."; |
| 1760 |
ib::info(ER_IB_MSG_1180) << "2. Try increasing the number of purge" |
| 1761 |
" threads to expedite purging of undo logs."; |
| 1762 |
} |
| 1763 |
#endif |
| 1764 |
return; |
| 1765 |
} |
| 1766 |
|
| 1767 |
mtr_commit(&mtr); |
| 1768 |
rseg->unlatch(); |
| 1769 |
|
| 1770 |
/* Read the trx number and del marks from the previous log header */ |
| 1771 |
mtr_start(&mtr); |
| 1772 |
|
| 1773 |
log_hdr = |
| 1774 |
trx_undo_page_get_s_latched(page_id_t(rseg->space_id, prev_log_addr.page), |
| 1775 |
rseg->page_size, &mtr) + |
| 1776 |
prev_log_addr.boffset; |
| 1777 |
|
| 1778 |
trx_id_t trx_no = mach_read_from_8(log_hdr + TRX_UNDO_TRX_NO); |
| 1779 |
|
| 1780 |
auto del_marks = mach_read_from_2(log_hdr + TRX_UNDO_DEL_MARKS); |
| 1781 |
|
| 1782 |
mtr_commit(&mtr); |
| 1783 |
|
| 1784 |
rseg->latch(); |
| 1785 |
|
| 1786 |
rseg->last_page_no = prev_log_addr.page; |
| 1787 |
rseg->last_offset = prev_log_addr.boffset; |
| 1788 |
rseg->last_trx_no = trx_no; |
| 1789 |
rseg->last_del_marks = del_marks; |
| 1790 |
|
| 1791 |
TrxUndoRsegs elem(rseg->last_trx_no); |
| 1792 |
elem.insert(rseg); |
| 1793 |
|
| 1794 |
/* Purge can also produce events, however these are already ordered in the |
| 1795 |
rollback segment and any user generated event will be greater than the events |
| 1796 |
that Purge produces. ie. Purge can never produce |
| 1797 |
events from an empty rollback segment. */ |
| 1798 |
|
| 1799 |
mutex_enter(&purge_sys->pq_mutex); |
| 1800 |
|
| 1801 |
purge_sys->purge_queue->push(std::move(elem)); |
| 1802 |
|
| 1803 |
mutex_exit(&purge_sys->pq_mutex); |
| 1804 |
|
| 1805 |
rseg->unlatch(); |
| 1806 |
} |
| 1807 |
|
| 1808 |
/** Position the purge sys "iterator" on the undo record to use for purging. |
| 1809 |
@param[in,out] purge_sys purge instance |
| 1810 |
@param[in] page_size page size */ |
| 1811 |
static void trx_purge_read_undo_rec(trx_purge_t *purge_sys, |
| 1812 |
const page_size_t &page_size) { |
| 1813 |
ulint offset; |
| 1814 |
page_no_t page_no; |
| 1815 |
uint64_t undo_no; |
| 1816 |
space_id_t undo_rseg_space; |
| 1817 |
trx_id_t modifier_trx_id; |
| 1818 |
|
| 1819 |
purge_sys->hdr_offset = purge_sys->rseg->last_offset; |
| 1820 |
page_no = purge_sys->hdr_page_no = purge_sys->rseg->last_page_no; |
| 1821 |
|
| 1822 |
if (purge_sys->rseg->last_del_marks) { |
| 1823 |
mtr_t mtr; |
| 1824 |
trx_undo_rec_t *undo_rec = nullptr; |
| 1825 |
|
| 1826 |
mtr_start(&mtr); |
| 1827 |
|
| 1828 |
undo_rec = trx_undo_get_first_rec( |
| 1829 |
&modifier_trx_id, purge_sys->rseg->space_id, page_size, |
| 1830 |
purge_sys->hdr_page_no, purge_sys->hdr_offset, RW_S_LATCH, &mtr); |
| 1831 |
|
| 1832 |
if (undo_rec != nullptr) { |
| 1833 |
offset = page_offset(undo_rec); |
| 1834 |
undo_no = trx_undo_rec_get_undo_no(undo_rec); |
| 1835 |
undo_rseg_space = purge_sys->rseg->space_id; |
| 1836 |
page_no = page_get_page_no(page_align(undo_rec)); |
| 1837 |
} else { |
| 1838 |
offset = 0; |
| 1839 |
undo_no = 0; |
| 1840 |
undo_rseg_space = SPACE_UNKNOWN; |
| 1841 |
} |
| 1842 |
|
| 1843 |
mtr_commit(&mtr); |
| 1844 |
} else { |
| 1845 |
offset = 0; |
| 1846 |
undo_no = 0; |
| 1847 |
undo_rseg_space = SPACE_UNKNOWN; |
| 1848 |
modifier_trx_id = 0; |
| 1849 |
} |
| 1850 |
|
| 1851 |
purge_sys->offset = offset; |
| 1852 |
purge_sys->page_no = page_no; |
| 1853 |
purge_sys->iter.undo_no = undo_no; |
| 1854 |
purge_sys->iter.modifier_trx_id = modifier_trx_id; |
| 1855 |
purge_sys->iter.undo_rseg_space = undo_rseg_space; |
| 1856 |
|
| 1857 |
purge_sys->next_stored = true; |
| 1858 |
} |
| 1859 |
|
| 1860 |
/** Chooses the next undo log to purge and updates the info in purge_sys. This |
| 1861 |
function is used to initialize purge_sys when the next record to purge is not |
| 1862 |
known, and also to update the purge system info on the next record when purge |
| 1863 |
has handled the whole undo log for a transaction. */ |
| 1864 |
static void trx_purge_choose_next_log(void) { |
| 1865 |
ut_ad(purge_sys->next_stored == false); |
| 1866 |
|
| 1867 |
const page_size_t &page_size = purge_sys->rseg_iter->set_next(); |
| 1868 |
|
| 1869 |
if (purge_sys->rseg != nullptr) { |
| 1870 |
trx_purge_read_undo_rec(purge_sys, page_size); |
| 1871 |
} else { |
| 1872 |
/* There is nothing to do yet. */ |
| 1873 |
std::this_thread::yield(); |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
/** Gets the next record to purge and updates the info in the purge system. |
| 1878 |
@return copy of an undo log record or pointer to the dummy undo log record */ |
| 1879 |
static trx_undo_rec_t *trx_purge_get_next_rec( |
| 1880 |
ulint *n_pages_handled, /*!< in/out: number of UNDO pages |
| 1881 |
handled */ |
| 1882 |
mem_heap_t *heap) /*!< in: memory heap where copied */ |
| 1883 |
{ |
| 1884 |
trx_undo_rec_t *rec; |
| 1885 |
trx_undo_rec_t *rec_copy; |
| 1886 |
trx_undo_rec_t *rec2; |
| 1887 |
page_t *undo_page; |
| 1888 |
page_t *page; |
| 1889 |
ulint offset; |
| 1890 |
page_no_t page_no; |
| 1891 |
space_id_t space; |
| 1892 |
mtr_t mtr; |
| 1893 |
|
| 1894 |
ut_ad(purge_sys->next_stored); |
| 1895 |
ut_ad(purge_sys->iter.trx_no < purge_sys->view.low_limit_no()); |
| 1896 |
|
| 1897 |
space = purge_sys->rseg->space_id; |
| 1898 |
page_no = purge_sys->page_no; |
| 1899 |
offset = purge_sys->offset; |
| 1900 |
|
| 1901 |
const page_size_t page_size(purge_sys->rseg->page_size); |
| 1902 |
|
| 1903 |
if (offset == 0) { |
| 1904 |
/* It is the dummy undo log record, which means that there is no need to |
| 1905 |
purge this undo log */ |
| 1906 |
|
| 1907 |
trx_purge_rseg_get_next_history_log(purge_sys->rseg, n_pages_handled); |
| 1908 |
|
| 1909 |
/* Look for the next undo log and record to purge */ |
| 1910 |
|
| 1911 |
trx_purge_choose_next_log(); |
| 1912 |
|
| 1913 |
return (&trx_purge_ignore_rec); |
| 1914 |
} |
| 1915 |
|
| 1916 |
mtr_start(&mtr); |
| 1917 |
|
| 1918 |
undo_page = |
| 1919 |
trx_undo_page_get_s_latched(page_id_t(space, page_no), page_size, &mtr); |
| 1920 |
|
| 1921 |
rec = undo_page + offset; |
| 1922 |
|
| 1923 |
rec2 = rec; |
| 1924 |
|
| 1925 |
for (;;) { |
| 1926 |
ulint type; |
| 1927 |
trx_undo_rec_t *next_rec; |
| 1928 |
ulint cmpl_info; |
| 1929 |
|
| 1930 |
/* Try first to find the next record which requires a purge operation from |
| 1931 |
the same page of the same undo log */ |
| 1932 |
|
| 1933 |
next_rec = trx_undo_page_get_next_rec(rec2, purge_sys->hdr_page_no, |
| 1934 |
purge_sys->hdr_offset); |
| 1935 |
|
| 1936 |
if (next_rec == nullptr) { |
| 1937 |
rec2 = trx_undo_get_next_rec(rec2, purge_sys->hdr_page_no, |
| 1938 |
purge_sys->hdr_offset, &mtr); |
| 1939 |
break; |
| 1940 |
} |
| 1941 |
|
| 1942 |
rec2 = next_rec; |
| 1943 |
|
| 1944 |
type = trx_undo_rec_get_type(rec2); |
| 1945 |
|
| 1946 |
if (type == TRX_UNDO_DEL_MARK_REC) { |
| 1947 |
break; |
| 1948 |
} |
| 1949 |
|
| 1950 |
cmpl_info = trx_undo_rec_get_cmpl_info(rec2); |
| 1951 |
|
| 1952 |
if (trx_undo_rec_get_extern_storage(rec2)) { |
| 1953 |
break; |
| 1954 |
} |
| 1955 |
|
| 1956 |
if ((type == TRX_UNDO_UPD_EXIST_REC) && |
| 1957 |
!(cmpl_info & UPD_NODE_NO_ORD_CHANGE)) { |
| 1958 |
break; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
|
| 1962 |
if (rec2 == nullptr) { |
| 1963 |
mtr_commit(&mtr); |
| 1964 |
|
| 1965 |
trx_purge_rseg_get_next_history_log(purge_sys->rseg, n_pages_handled); |
| 1966 |
|
| 1967 |
/* Look for the next undo log and record to purge */ |
| 1968 |
|
| 1969 |
trx_purge_choose_next_log(); |
| 1970 |
|
| 1971 |
mtr_start(&mtr); |
| 1972 |
|
| 1973 |
undo_page = |
| 1974 |
trx_undo_page_get_s_latched(page_id_t(space, page_no), page_size, &mtr); |
| 1975 |
|
| 1976 |
} else { |
| 1977 |
page = page_align(rec2); |
| 1978 |
|
| 1979 |
purge_sys->offset = rec2 - page; |
| 1980 |
purge_sys->page_no = page_get_page_no(page); |
| 1981 |
purge_sys->iter.undo_no = trx_undo_rec_get_undo_no(rec2); |
| 1982 |
purge_sys->iter.undo_rseg_space = space; |
| 1983 |
|
| 1984 |
if (undo_page != page) { |
| 1985 |
/* We advance to a new page of the undo log: */ |
| 1986 |
(*n_pages_handled)++; |
| 1987 |
} |
| 1988 |
} |
| 1989 |
|
| 1990 |
rec_copy = trx_undo_rec_copy(undo_page, static_cast<uint32_t>(offset), heap); |
| 1991 |
|
| 1992 |
mtr_commit(&mtr); |
| 1993 |
|
| 1994 |
return (rec_copy); |
| 1995 |
} |
| 1996 |
|
| 1997 |
struct Purge_groups_t { |
| 1998 |
Purge_groups_t(std::size_t n_threads, mem_heap_t *heap) |
| 1999 |
: m_grpid_umap{n_threads, mem_heap_allocator<GroupBy::value_type>{heap}}, |
| 2000 |
m_groups(n_threads, nullptr, |
| 2001 |
mem_heap_allocator<purge_node_t::Recs *>(heap)), |
| 2002 |
m_heap(heap), |
| 2003 |
m_total_rec(0) {} |
| 2004 |
|
| 2005 |
void init() { |
| 2006 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2007 |
|
| 2008 |
/* Initialize the grouping vector. */ |
| 2009 |
for (std::size_t grpid = 0; grpid < n_purge_threads; ++grpid) { |
| 2010 |
void *ptr; |
| 2011 |
purge_node_t::Recs *recs; |
| 2012 |
|
| 2013 |
ptr = mem_heap_alloc(m_heap, sizeof(purge_node_t::Recs)); |
| 2014 |
|
| 2015 |
/* Call the destructor explicitly in row_purge_end() */ |
| 2016 |
recs = new (ptr) |
| 2017 |
purge_node_t::Recs{mem_heap_allocator<purge_node_t::rec_t>{m_heap}}; |
| 2018 |
|
| 2019 |
m_groups[grpid] = recs; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
std::size_t find_smallest_group(); |
| 2024 |
|
| 2025 |
/** Check the history list length and decide if distribution of workload |
| 2026 |
between purge threads is needed or not. If needed, do the distribution, |
| 2027 |
otherwise do nothing. */ |
| 2028 |
void distribute_if_needed(); |
| 2029 |
|
| 2030 |
std::ostream &print(std::ostream &out) const; |
| 2031 |
|
| 2032 |
void assign(que_thr_t **thrs) { |
| 2033 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2034 |
for (std::size_t grpid = 0; grpid < n_purge_threads; ++grpid) { |
| 2035 |
purge_node_t *node = static_cast<purge_node_t *>(thrs[grpid]->child); |
| 2036 |
ut_a(que_node_get_type(node) == QUE_NODE_PURGE); |
| 2037 |
ut_ad(node->recs == nullptr); |
| 2038 |
node->recs = m_groups[grpid]; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
|
| 2042 |
#ifdef UNIV_DEBUG |
| 2043 |
bool is_grouping_uniform() const; |
| 2044 |
#endif /* UNIV_DEBUG */ |
| 2045 |
|
| 2046 |
void add(purge_node_t::rec_t &rec) { |
| 2047 |
/* Identify the table id */ |
| 2048 |
const table_id_t id = trx_undo_rec_get_table_id(rec.undo_rec); |
| 2049 |
std::size_t grpid; |
| 2050 |
|
| 2051 |
GroupBy::iterator lb = m_grpid_umap.find(id); |
| 2052 |
if (lb != m_grpid_umap.end()) { |
| 2053 |
grpid = lb->second; |
| 2054 |
} else { |
| 2055 |
grpid = find_smallest_group(); |
| 2056 |
m_grpid_umap.insert(std::make_pair(id, grpid)); |
| 2057 |
} |
| 2058 |
|
| 2059 |
m_groups[grpid]->push_back(rec); |
| 2060 |
m_total_rec++; |
| 2061 |
} |
| 2062 |
|
| 2063 |
using GroupBy = std::unordered_map< |
| 2064 |
table_id_t, std::size_t, std::hash<table_id_t>, std::equal_to<table_id_t>, |
| 2065 |
mem_heap_allocator<std::pair<const table_id_t, std::size_t>>>; |
| 2066 |
|
| 2067 |
/** Given a table_id obtain the group id to which it belongs. */ |
| 2068 |
GroupBy m_grpid_umap; |
| 2069 |
|
| 2070 |
/** Allocator used for the vector below. */ |
| 2071 |
using vec_alloc = mem_heap_allocator<purge_node_t::Recs *>; |
| 2072 |
|
| 2073 |
/** A vector of groups. The size of this vector is equal to the number of |
| 2074 |
purge threads. Each undo record is assigned to one of the groups, based on |
| 2075 |
its table_id. The index into this vector is the group_id. */ |
| 2076 |
std::vector<purge_node_t::Recs *, vec_alloc> m_groups; |
| 2077 |
|
| 2078 |
/** Memory heap in which memory for unordered_map & vector is allocated.*/ |
| 2079 |
mem_heap_t *m_heap; |
| 2080 |
|
| 2081 |
/** Total number of undo records parsed and grouped. */ |
| 2082 |
std::size_t m_total_rec; |
| 2083 |
|
| 2084 |
private: |
| 2085 |
/** Redistribute the undo records across different groups. If a group has |
| 2086 |
more records than it should, move all the extra records to the next group. |
| 2087 |
Maximum two passes might be needed. */ |
| 2088 |
void distribute(); |
| 2089 |
}; |
| 2090 |
|
| 2091 |
std::size_t Purge_groups_t::find_smallest_group() { |
| 2092 |
std::size_t result = 0; |
| 2093 |
std::size_t n = std::numeric_limits<std::size_t>::max(); |
| 2094 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2095 |
|
| 2096 |
for (std::size_t grpid = 0; grpid < n_purge_threads; ++grpid) { |
| 2097 |
const std::size_t grp_count = m_groups[grpid]->size(); |
| 2098 |
if (grp_count < n) { |
| 2099 |
n = grp_count; |
| 2100 |
result = grpid; |
| 2101 |
} |
| 2102 |
} |
| 2103 |
return result; |
| 2104 |
} |
| 2105 |
|
| 2106 |
std::ostream &Purge_groups_t::print(std::ostream &out) const { |
| 2107 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2108 |
const std::size_t max_n = ut::div_ceil(m_total_rec, n_purge_threads); |
| 2109 |
const std::size_t min_n = |
| 2110 |
(max_n > n_purge_threads) ? max_n - n_purge_threads : 0; |
| 2111 |
|
| 2112 |
if (m_total_rec > 0) { |
| 2113 |
out << "[n_purge_threads=" << n_purge_threads |
| 2114 |
<< ", m_total_rec=" << m_total_rec << ", max=" << max_n |
| 2115 |
<< ", min=" << min_n << ", ["; |
| 2116 |
for (std::size_t i = 0; i < n_purge_threads; ++i) { |
| 2117 |
out << m_groups[i]->size() << ", "; |
| 2118 |
} |
| 2119 |
out << "]]" << std::endl; |
| 2120 |
} |
| 2121 |
return out; |
| 2122 |
} |
| 2123 |
|
| 2124 |
#ifdef UNIV_DEBUG |
| 2125 |
bool Purge_groups_t::is_grouping_uniform() const { |
| 2126 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2127 |
const std::size_t max_n = ut::div_ceil(m_total_rec, n_purge_threads); |
| 2128 |
const std::size_t min_n = |
| 2129 |
(max_n > n_purge_threads) ? max_n - n_purge_threads : 0; |
| 2130 |
bool result = true; |
| 2131 |
|
| 2132 |
for (std::size_t grpid = 0; grpid < n_purge_threads; ++grpid) { |
| 2133 |
const std::size_t grp_count = m_groups[grpid]->size(); |
| 2134 |
if (grp_count < min_n || grp_count > max_n) { |
| 2135 |
result = false; |
| 2136 |
} |
| 2137 |
} |
| 2138 |
return result; |
| 2139 |
} |
| 2140 |
#endif /* UNIV_DEBUG */ |
| 2141 |
|
| 2142 |
void Purge_groups_t::distribute() { |
| 2143 |
const std::size_t n_purge_threads = m_groups.size(); |
| 2144 |
const std::size_t max_n = ut::div_ceil(m_total_rec, n_purge_threads); |
| 2145 |
|
| 2146 |
for (std::size_t i = 0; i < 2; ++i) { |
| 2147 |
bool need_second_pass = false; |
| 2148 |
for (std::size_t grpid = 0; grpid < n_purge_threads; ++grpid) { |
| 2149 |
std::size_t grp_count = m_groups[grpid]->size(); |
| 2150 |
if (grp_count > max_n) { |
| 2151 |
auto from_list = m_groups[grpid]; |
| 2152 |
std::size_t target_grpid = grpid + 1; |
| 2153 |
if (target_grpid == n_purge_threads) { |
| 2154 |
target_grpid = 0; |
| 2155 |
/* Undo records are moved to the first group. So a second pass is |
| 2156 |
needed. */ |
| 2157 |
need_second_pass = true; |
| 2158 |
} |
| 2159 |
auto to_list = m_groups[target_grpid]; |
| 2160 |
auto from_iter = from_list->begin(); |
| 2161 |
std::advance(from_iter, max_n); |
| 2162 |
to_list->splice(to_list->end(), *from_list, from_iter, |
| 2163 |
from_list->end()); |
| 2164 |
} else if (i == 1) { |
| 2165 |
/* In the second pass, stop as soon as we encounter a group with <= |
| 2166 |
max_n records. */ |
| 2167 |
break; |
| 2168 |
} |
| 2169 |
} |
| 2170 |
if (!need_second_pass) { |
| 2171 |
break; |
| 2172 |
} |
| 2173 |
} |
| 2174 |
|
| 2175 |
#ifdef UNIV_DEBUG |
| 2176 |
if (!is_grouping_uniform()) { |
| 2177 |
print(std::cerr); |
| 2178 |
ut_error; |
| 2179 |
} |
| 2180 |
#endif /* UNIV_DEBUG */ |
| 2181 |
} |
| 2182 |
|
| 2183 |
void Purge_groups_t::distribute_if_needed() { |
| 2184 |
const uint64_t rseg_history_len = trx_sys->rseg_history_len.load(); |
| 2185 |
|
| 2186 |
/* If the history list length is greater than maximum allowed purge lag, |
| 2187 |
then distribute the workload across all purge threads. */ |
| 2188 |
if (srv_max_purge_lag > 0 && rseg_history_len > srv_max_purge_lag) { |
| 2189 |
distribute(); |
| 2190 |
} |
| 2191 |
} |
| 2192 |
|
| 2193 |
/** Fetches the next undo log record from the history list to purge. It must |
| 2194 |
be released with the corresponding release function. |
| 2195 |
@return copy of an undo log record or pointer to trx_purge_ignore_rec, |
| 2196 |
if the whole undo log can skipped in purge; NULL if none left */ |
| 2197 |
[[nodiscard]] static trx_undo_rec_t *trx_purge_fetch_next_rec( |
| 2198 |
trx_id_t *modifier_trx_id, |
| 2199 |
/*!< out: modifier trx id. this is the |
| 2200 |
trx that created the undo record. */ |
| 2201 |
roll_ptr_t *roll_ptr, /*!< out: roll pointer to undo record */ |
| 2202 |
ulint *n_pages_handled, /*!< in/out: number of UNDO log pages |
| 2203 |
handled */ |
| 2204 |
mem_heap_t *heap) /*!< in: memory heap where copied */ |
| 2205 |
{ |
| 2206 |
if (!purge_sys->next_stored) { |
| 2207 |
trx_purge_choose_next_log(); |
| 2208 |
|
| 2209 |
if (!purge_sys->next_stored) { |
| 2210 |
DBUG_PRINT("ib_purge", ("no logs left in the history list")); |
| 2211 |
return nullptr; |
| 2212 |
} |
| 2213 |
} |
| 2214 |
|
| 2215 |
if (purge_sys->iter.trx_no >= purge_sys->view.low_limit_no()) { |
| 2216 |
return nullptr; |
| 2217 |
} |
| 2218 |
|
| 2219 |
/* fprintf(stderr, "Thread %s purging trx %llu undo record %llu\n", |
| 2220 |
to_string(std::this_thread::get_id()), iter->trx_no, iter->undo_no); */ |
| 2221 |
|
| 2222 |
*roll_ptr = trx_undo_build_roll_ptr(false, purge_sys->rseg->space_id, |
| 2223 |
purge_sys->page_no, purge_sys->offset); |
| 2224 |
|
| 2225 |
*modifier_trx_id = purge_sys->iter.modifier_trx_id; |
| 2226 |
|
| 2227 |
/* The following call will advance the stored values of the |
| 2228 |
purge iterator. */ |
| 2229 |
|
| 2230 |
return (trx_purge_get_next_rec(n_pages_handled, heap)); |
| 2231 |
} |
| 2232 |
|
| 2233 |
/** This function runs a purge batch. |
| 2234 |
@param[in] n_purge_threads number of purge threads |
| 2235 |
@param[in] batch_size number of pages to purge |
| 2236 |
@return number of undo log pages handled in the batch */ |
| 2237 |
static ulint trx_purge_attach_undo_recs(const ulint n_purge_threads, |
| 2238 |
ulint batch_size) { |
| 2239 |
ulint n_pages_handled = 0; |
| 2240 |
|
| 2241 |
ut_a(n_purge_threads > 0); |
| 2242 |
ut_a(n_purge_threads <= MAX_PURGE_THREADS); |
| 2243 |
|
| 2244 |
purge_sys->limit = purge_sys->iter; |
| 2245 |
|
| 2246 |
que_thr_t *run_thrs[MAX_PURGE_THREADS]; |
| 2247 |
|
| 2248 |
/* Validate some pre-requisites and reset done flag. */ |
| 2249 |
ulint i = 0; |
| 2250 |
|
| 2251 |
for (auto thr : purge_sys->query->thrs) { |
| 2252 |
if (n_purge_threads <= i) break; |
| 2253 |
purge_node_t *node; |
| 2254 |
|
| 2255 |
/* Get the purge node. */ |
| 2256 |
node = static_cast<purge_node_t *>(thr->child); |
| 2257 |
|
| 2258 |
ut_a(que_node_get_type(node) == QUE_NODE_PURGE); |
| 2259 |
ut_a(node->recs == nullptr); |
| 2260 |
ut_a(node->done); |
| 2261 |
|
| 2262 |
node->done = false; |
| 2263 |
|
| 2264 |
ut_a(!thr->is_active); |
| 2265 |
|
| 2266 |
run_thrs[i++] = thr; |
| 2267 |
} |
| 2268 |
|
| 2269 |
/* There should never be fewer nodes than threads, the inverse |
| 2270 |
however is allowed because we only use purge threads as needed. */ |
| 2271 |
ut_a(i == n_purge_threads); |
| 2272 |
ut_ad(trx_purge_check_limit()); |
| 2273 |
|
| 2274 |
mem_heap_t *heap = purge_sys->heap; |
| 2275 |
|
| 2276 |
mem_heap_empty(heap); |
| 2277 |
|
| 2278 |
Purge_groups_t purge_groups(n_purge_threads, heap); |
| 2279 |
purge_groups.init(); |
| 2280 |
|
| 2281 |
while (n_pages_handled < batch_size) { |
| 2282 |
/* Track the max {trx_id, undo_no} for truncating the |
| 2283 |
UNDO logs once we have purged the records. */ |
| 2284 |
|
| 2285 |
if (trx_purge_check_limit()) { |
| 2286 |
purge_sys->limit = purge_sys->iter; |
| 2287 |
} |
| 2288 |
|
| 2289 |
purge_node_t::rec_t rec; |
| 2290 |
|
| 2291 |
/* Fetch the next record, and advance the purge_sys->iter. */ |
| 2292 |
rec.undo_rec = trx_purge_fetch_next_rec(&rec.modifier_trx_id, &rec.roll_ptr, |
| 2293 |
&n_pages_handled, heap); |
| 2294 |
|
| 2295 |
if (rec.undo_rec == &trx_purge_ignore_rec) { |
| 2296 |
continue; |
| 2297 |
|
| 2298 |
} else if (rec.undo_rec == nullptr) { |
| 2299 |
break; |
| 2300 |
} |
| 2301 |
|
| 2302 |
purge_groups.add(rec); |
| 2303 |
} |
| 2304 |
|
| 2305 |
purge_groups.distribute_if_needed(); |
| 2306 |
purge_groups.assign(run_thrs); |
| 2307 |
|
| 2308 |
ut_ad(trx_purge_check_limit()); |
| 2309 |
|
| 2310 |
return (n_pages_handled); |
| 2311 |
} |
| 2312 |
|
| 2313 |
/** Calculate the DML delay required. |
| 2314 |
@return delay in microseconds or ULINT_MAX */ |
| 2315 |
static ulint trx_purge_dml_delay(void) { |
| 2316 |
/* Determine how much data manipulation language (DML) statements |
| 2317 |
need to be delayed in order to reduce the lagging of the purge |
| 2318 |
thread. */ |
| 2319 |
ulint delay = 0; /* in microseconds; default: no delay */ |
| 2320 |
|
| 2321 |
/* If purge lag is set (ie. > 0) then calculate the new DML delay. |
| 2322 |
Note: we do a dirty read of the trx_sys_t data structure here, |
| 2323 |
without holding trx_sys->mutex. */ |
| 2324 |
|
| 2325 |
if (srv_max_purge_lag > 0 && trx_sys->rseg_history_len.load() > |
| 2326 |
srv_n_purge_threads * srv_purge_batch_size) { |
| 2327 |
float ratio; |
| 2328 |
|
| 2329 |
ratio = float(trx_sys->rseg_history_len.load()) / srv_max_purge_lag; |
| 2330 |
|
| 2331 |
if (ratio > 1.0) { |
| 2332 |
/* If the history list length exceeds the srv_max_purge_lag, the data |
| 2333 |
manipulation statements are delayed by at least 5 microseconds. */ |
| 2334 |
delay = (ulint)((ratio - 0.9995) * 10000); |
| 2335 |
} |
| 2336 |
|
| 2337 |
if (delay > srv_max_purge_lag_delay) { |
| 2338 |
delay = srv_max_purge_lag_delay; |
| 2339 |
} |
| 2340 |
|
| 2341 |
MONITOR_SET(MONITOR_DML_PURGE_DELAY, delay); |
| 2342 |
} |
| 2343 |
|
| 2344 |
return (delay); |
| 2345 |
} |
| 2346 |
|
| 2347 |
/** Wait for pending purge jobs to complete. */ |
| 2348 |
static void trx_purge_wait_for_workers_to_complete() { |
| 2349 |
ulint i = 0; |
| 2350 |
ulint n_submitted = purge_sys->n_submitted; |
| 2351 |
|
| 2352 |
/* Ensure that the work queue empties out. */ |
| 2353 |
while (purge_sys->n_completed.load() != n_submitted) { |
| 2354 |
if (++i < 10) { |
| 2355 |
std::this_thread::yield(); |
| 2356 |
} else { |
| 2357 |
if (srv_get_task_queue_length() > 0) { |
| 2358 |
srv_release_threads(SRV_WORKER, 1); |
| 2359 |
} |
| 2360 |
|
| 2361 |
std::this_thread::sleep_for(std::chrono::microseconds(20)); |
| 2362 |
i = 0; |
| 2363 |
} |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* None of the worker threads should be doing any work. */ |
| 2367 |
ut_a(purge_sys->n_submitted == purge_sys->n_completed); |
| 2368 |
|
| 2369 |
/* There should be no outstanding tasks as long |
| 2370 |
as the worker threads are active. */ |
| 2371 |
ut_a(srv_get_task_queue_length() == 0); |
| 2372 |
} |
| 2373 |
|
| 2374 |
/** Remove old historical changes from the rollback segments. */ |
| 2375 |
static void trx_purge_truncate(void) { |
| 2376 |
ut_ad(trx_purge_check_limit()); |
| 2377 |
|
| 2378 |
if (purge_sys->limit.trx_no == 0) { |
| 2379 |
trx_purge_truncate_history(&purge_sys->iter, &purge_sys->view); |
| 2380 |
} else { |
| 2381 |
trx_purge_truncate_history(&purge_sys->limit, &purge_sys->view); |
| 2382 |
} |
| 2383 |
|
| 2384 |
/* Attempt to truncate an undo tablespace. */ |
| 2385 |
trx_purge_truncate_undo_spaces(); |
| 2386 |
} |
| 2387 |
|
| 2388 |
/** This function runs a purge batch. |
| 2389 |
@return number of undo log pages handled in the batch */ |
| 2390 |
ulint trx_purge(ulint n_purge_threads, /*!< in: number of purge tasks |
| 2391 |
to submit to the work queue */ |
| 2392 |
ulint batch_size, /*!< in: the maximum number of records |
| 2393 |
to purge in one batch */ |
| 2394 |
bool truncate) /*!< in: truncate history if true */ |
| 2395 |
{ |
| 2396 |
que_thr_t *thr = nullptr; |
| 2397 |
ulint n_pages_handled; |
| 2398 |
|
| 2399 |
ut_a(n_purge_threads > 0); |
| 2400 |
|
| 2401 |
srv_dml_needed_delay = trx_purge_dml_delay(); |
| 2402 |
|
| 2403 |
/* The number of tasks submitted should be completed. */ |
| 2404 |
ut_a(purge_sys->n_submitted == purge_sys->n_completed); |
| 2405 |
|
| 2406 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2407 |
|
| 2408 |
purge_sys->view_active = false; |
| 2409 |
|
| 2410 |
trx_sys->mvcc->clone_oldest_view(&purge_sys->view); |
| 2411 |
|
| 2412 |
purge_sys->view_active = true; |
| 2413 |
|
| 2414 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2415 |
|
| 2416 |
#ifdef UNIV_DEBUG |
| 2417 |
if (srv_purge_view_update_only_debug) { |
| 2418 |
return (0); |
| 2419 |
} |
| 2420 |
#endif /* UNIV_DEBUG */ |
| 2421 |
|
| 2422 |
/* Fetch the UNDO recs that need to be purged. */ |
| 2423 |
n_pages_handled = trx_purge_attach_undo_recs(n_purge_threads, batch_size); |
| 2424 |
|
| 2425 |
/* Do we do an asynchronous purge or not ? */ |
| 2426 |
if (n_purge_threads > 1) { |
| 2427 |
/* Submit the tasks to the work queue. */ |
| 2428 |
for (ulint i = 0; i < n_purge_threads - 1; ++i) { |
| 2429 |
thr = que_fork_scheduler_round_robin(purge_sys->query, thr); |
| 2430 |
|
| 2431 |
ut_a(thr != nullptr); |
| 2432 |
|
| 2433 |
srv_que_task_enqueue_low(thr); |
| 2434 |
} |
| 2435 |
|
| 2436 |
thr = que_fork_scheduler_round_robin(purge_sys->query, thr); |
| 2437 |
ut_a(thr != nullptr); |
| 2438 |
|
| 2439 |
purge_sys->n_submitted += n_purge_threads - 1; |
| 2440 |
|
| 2441 |
goto run_synchronously; |
| 2442 |
|
| 2443 |
/* Do it synchronously. */ |
| 2444 |
} else { |
| 2445 |
thr = que_fork_scheduler_round_robin(purge_sys->query, nullptr); |
| 2446 |
ut_ad(thr); |
| 2447 |
|
| 2448 |
run_synchronously: |
| 2449 |
++purge_sys->n_submitted; |
| 2450 |
|
| 2451 |
que_run_threads(thr); |
| 2452 |
|
| 2453 |
purge_sys->n_completed.fetch_add(1); |
| 2454 |
|
| 2455 |
if (n_purge_threads > 1) { |
| 2456 |
trx_purge_wait_for_workers_to_complete(); |
| 2457 |
} |
| 2458 |
} |
| 2459 |
|
| 2460 |
ut_a(purge_sys->n_submitted == purge_sys->n_completed); |
| 2461 |
|
| 2462 |
#ifdef UNIV_DEBUG |
| 2463 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2464 |
if (purge_sys->limit.trx_no == 0) { |
| 2465 |
purge_sys->done = purge_sys->iter; |
| 2466 |
} else { |
| 2467 |
purge_sys->done = purge_sys->limit; |
| 2468 |
} |
| 2469 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2470 |
#endif /* UNIV_DEBUG */ |
| 2471 |
|
| 2472 |
/* The first page of LOBs are freed at the end of a purge batch because |
| 2473 |
multiple purge threads will access the same LOB as part of the purge |
| 2474 |
process. Some purge threads will free only portion of the LOB related to |
| 2475 |
the partial update of the LOB. But 1 of the purge thread will free the LOB |
| 2476 |
completely if it is not needed anymore (either because of full update or |
| 2477 |
because of deletion). If the LOB is freed, and a purge thread attempts to |
| 2478 |
access the LOB, then it is a bug. To avoid this, we delay the freeing of |
| 2479 |
the first page of LOB till the end of a purge batch. */ |
| 2480 |
for (thr = UT_LIST_GET_FIRST(purge_sys->query->thrs); thr != nullptr; |
| 2481 |
thr = UT_LIST_GET_NEXT(thrs, thr)) { |
| 2482 |
purge_node_t *node = static_cast<purge_node_t *>(thr->child); |
| 2483 |
node->free_lob_pages(); |
| 2484 |
} |
| 2485 |
|
| 2486 |
/* During upgrade, to know whether purge is empty, |
| 2487 |
we rely on purge history length. So truncate the |
| 2488 |
undo logs during upgrade to update purge history |
| 2489 |
length. */ |
| 2490 |
if (truncate || srv_upgrade_old_undo_found) { |
| 2491 |
trx_purge_truncate(); |
| 2492 |
} |
| 2493 |
|
| 2494 |
MONITOR_INC_VALUE(MONITOR_PURGE_INVOKED, 1); |
| 2495 |
MONITOR_INC_VALUE(MONITOR_PURGE_N_PAGE_HANDLED, n_pages_handled); |
| 2496 |
|
| 2497 |
return (n_pages_handled); |
| 2498 |
} |
| 2499 |
|
| 2500 |
/** Get the purge state. |
| 2501 |
@return purge state. */ |
| 2502 |
purge_state_t trx_purge_state(void) { |
| 2503 |
purge_state_t state; |
| 2504 |
|
| 2505 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2506 |
|
| 2507 |
state = purge_sys->state; |
| 2508 |
|
| 2509 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2510 |
|
| 2511 |
return (state); |
| 2512 |
} |
| 2513 |
|
| 2514 |
/** Stop purge and wait for it to stop, move to PURGE_STATE_STOP. */ |
| 2515 |
void trx_purge_stop(void) { |
| 2516 |
purge_state_t state; |
| 2517 |
int64_t sig_count = os_event_reset(purge_sys->event); |
| 2518 |
|
| 2519 |
ut_a(srv_n_purge_threads > 0); |
| 2520 |
|
| 2521 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2522 |
|
| 2523 |
ut_a(purge_sys->state != PURGE_STATE_INIT); |
| 2524 |
ut_a(purge_sys->state != PURGE_STATE_EXIT); |
| 2525 |
ut_a(purge_sys->state != PURGE_STATE_DISABLED); |
| 2526 |
|
| 2527 |
++purge_sys->n_stop; |
| 2528 |
|
| 2529 |
state = purge_sys->state; |
| 2530 |
|
| 2531 |
if (state == PURGE_STATE_RUN) { |
| 2532 |
ib::info(ER_IB_MSG_1181) << "Stopping purge"; |
| 2533 |
|
| 2534 |
/* We need to wakeup the purge thread in case it is suspended, |
| 2535 |
so that it can acknowledge the state change. */ |
| 2536 |
|
| 2537 |
srv_purge_wakeup(); |
| 2538 |
} |
| 2539 |
|
| 2540 |
purge_sys->state = PURGE_STATE_STOP; |
| 2541 |
|
| 2542 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2543 |
|
| 2544 |
if (state != PURGE_STATE_STOP) { |
| 2545 |
/* Wait for purge coordinator to signal that it |
| 2546 |
is suspended. */ |
| 2547 |
os_event_wait_low(purge_sys->event, sig_count); |
| 2548 |
} else { |
| 2549 |
bool once = true; |
| 2550 |
|
| 2551 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2552 |
|
| 2553 |
/* Wait for purge to signal that it has actually stopped. */ |
| 2554 |
while (purge_sys->running) { |
| 2555 |
if (once) { |
| 2556 |
ib::info(ER_IB_MSG_1182) << "Waiting for purge to stop"; |
| 2557 |
once = false; |
| 2558 |
} |
| 2559 |
|
| 2560 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2561 |
|
| 2562 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 2563 |
|
| 2564 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2565 |
} |
| 2566 |
|
| 2567 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2568 |
} |
| 2569 |
|
| 2570 |
MONITOR_INC_VALUE(MONITOR_PURGE_STOP_COUNT, 1); |
| 2571 |
} |
| 2572 |
|
| 2573 |
/** Resume purge, move to PURGE_STATE_RUN. */ |
| 2574 |
void trx_purge_run(void) { |
| 2575 |
/* Flush any GTIDs to disk so that purge can proceed immediately. */ |
| 2576 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 2577 |
gtid_persistor.wait_flush(false, false, nullptr); |
| 2578 |
|
| 2579 |
rw_lock_x_lock(&purge_sys->latch, UT_LOCATION_HERE); |
| 2580 |
|
| 2581 |
switch (purge_sys->state) { |
| 2582 |
case PURGE_STATE_INIT: |
| 2583 |
case PURGE_STATE_EXIT: |
| 2584 |
case PURGE_STATE_DISABLED: |
| 2585 |
ut_error; |
| 2586 |
|
| 2587 |
case PURGE_STATE_RUN: |
| 2588 |
case PURGE_STATE_STOP: |
| 2589 |
break; |
| 2590 |
} |
| 2591 |
|
| 2592 |
if (purge_sys->n_stop > 0) { |
| 2593 |
ut_a(purge_sys->state == PURGE_STATE_STOP); |
| 2594 |
|
| 2595 |
--purge_sys->n_stop; |
| 2596 |
|
| 2597 |
if (purge_sys->n_stop == 0) { |
| 2598 |
ib::info(ER_IB_MSG_1183) << "Resuming purge"; |
| 2599 |
|
| 2600 |
purge_sys->state = PURGE_STATE_RUN; |
| 2601 |
} |
| 2602 |
|
| 2603 |
MONITOR_INC_VALUE(MONITOR_PURGE_RESUME_COUNT, 1); |
| 2604 |
} else { |
| 2605 |
ut_a(purge_sys->state == PURGE_STATE_RUN); |
| 2606 |
} |
| 2607 |
|
| 2608 |
rw_lock_x_unlock(&purge_sys->latch); |
| 2609 |
|
| 2610 |
srv_purge_wakeup(); |
| 2611 |
} |
| 2612 |
|
| 2613 |
/** Initialize the undo::Tablespaces object. */ |
| 2614 |
void undo::Tablespaces::init() { |
| 2615 |
/** Fix the size of the vector so that it will not do |
| 2616 |
allocations for inserts. This way the contents can be |
| 2617 |
read without using a latch. */ |
| 2618 |
m_spaces.reserve(FSP_MAX_UNDO_TABLESPACES); |
| 2619 |
|
| 2620 |
m_latch = static_cast<rw_lock_t *>( |
| 2621 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*m_latch))); |
| 2622 |
|
| 2623 |
rw_lock_create(undo_spaces_lock_key, m_latch, LATCH_ID_UNDO_SPACES); |
| 2624 |
|
| 2625 |
mutex_create(LATCH_ID_UNDO_DDL, &ddl_mutex); |
| 2626 |
} |
| 2627 |
|
| 2628 |
/** De-initialize the undo::Tablespaces object. */ |
| 2629 |
void undo::Tablespaces::deinit() { |
| 2630 |
clear(); |
| 2631 |
|
| 2632 |
rw_lock_free(m_latch); |
| 2633 |
ut::free(m_latch); |
| 2634 |
m_latch = nullptr; |
| 2635 |
mutex_free(&ddl_mutex); |
| 2636 |
} |
| 2637 |
|
| 2638 |
/** Add a new space_id to the back of the vector. |
| 2639 |
The vector has been pre-allocated to 128 so read threads will |
| 2640 |
not loose what is pointed to. If tablespace_name and file_name |
| 2641 |
are standard names, they are optional. |
| 2642 |
@param[in] ref_undo_space undo tablespace */ |
| 2643 |
void undo::Tablespaces::add(Tablespace &ref_undo_space) { |
| 2644 |
ut_ad(is_reserved(ref_undo_space.id())); |
| 2645 |
|
| 2646 |
if (contains(ref_undo_space.num())) { |
| 2647 |
return; |
| 2648 |
} |
| 2649 |
|
| 2650 |
auto undo_space = |
| 2651 |
ut::new_withkey<Tablespace>(UT_NEW_THIS_FILE_PSI_KEY, ref_undo_space); |
| 2652 |
|
| 2653 |
m_spaces.push_back(undo_space); |
| 2654 |
} |
| 2655 |
|
| 2656 |
/** Drop an existing explicit undo::Tablespace. |
| 2657 |
@param[in] undo_space pointer to undo space */ |
| 2658 |
void undo::Tablespaces::drop(Tablespace *undo_space) { |
| 2659 |
ut_ad(is_reserved(undo_space->id())); |
| 2660 |
ut_ad(contains(undo_space->num())); |
| 2661 |
|
| 2662 |
for (auto it = m_spaces.begin(); it != m_spaces.end(); it++) { |
| 2663 |
if (*it == undo_space) { |
| 2664 |
ut::delete_(undo_space); |
| 2665 |
m_spaces.erase(it); |
| 2666 |
break; |
| 2667 |
} |
| 2668 |
} |
| 2669 |
} |
| 2670 |
|
| 2671 |
/** Drop an existing explicit undo::Tablespace. |
| 2672 |
@param[in] ref_undo_space reference to undo space */ |
| 2673 |
void undo::Tablespaces::drop(Tablespace &ref_undo_space) { |
| 2674 |
ut_ad(is_reserved(ref_undo_space.id())); |
| 2675 |
|
| 2676 |
for (auto it = m_spaces.begin(); it != m_spaces.end(); it++) { |
| 2677 |
if ((*it)->id() == ref_undo_space.id()) { |
| 2678 |
ut::delete_(*it); |
| 2679 |
m_spaces.erase(it); |
| 2680 |
break; |
| 2681 |
} |
| 2682 |
} |
| 2683 |
} |