Debian Codegraph @

storage/innobase/trx/trx0rseg.cc

raw
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/trx0rseg.cc
29 Rollback segment
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "trx0rseg.h"
35
36 #include <stddef.h>
37 #include <algorithm>
38
39 #include "clone0clone.h"
40 #include "fsp0sysspace.h"
41 #include "fut0lst.h"
42 #include "log0chkp.h"
43 #include "srv0mon.h"
44 #include "srv0srv.h"
45 #include "srv0start.h"
46 #include "trx0purge.h"
47 #include "trx0undo.h"
48
49 static std::atomic<uint32_t> active_rseg_init_threads{1};
50
51 /** Creates a rollback segment header.
52 This function is called only when a new rollback segment is created in
53 the database.
54 @param[in] space_id Space id
55 @param[in] page_size Page size
56 @param[in] max_size Max size in pages
57 @param[in] rseg_slot Rseg id == slot number in RSEG_ARRAY
58 @param[in,out] mtr Mini-transaction
59 @return page number of the created segment, FIL_NULL if fail */
60 page_no_t trx_rseg_header_create(space_id_t space_id,
61 const page_size_t &page_size,
62 page_no_t max_size, ulint rseg_slot,
63 mtr_t *mtr) {
64 page_no_t page_no;
65 trx_rsegf_t *rsegf;
66 trx_sysf_t *sys_header;
67 trx_rsegsf_t *rsegs_header;
68 ulint i;
69 buf_block_t *block;
70
71 ut_ad(mtr);
72 ut_ad(mtr_memo_contains(mtr, fil_space_get_latch(space_id), MTR_MEMO_X_LOCK));
73
74 /* Allocate a new file segment for the rollback segment */
75 block = fseg_create(space_id, 0, TRX_RSEG + TRX_RSEG_FSEG_HEADER, mtr);
76
77 if (block == nullptr) {
78 return (FIL_NULL); /* No space left */
79 }
80
81 buf_block_dbg_add_level(block, SYNC_RSEG_HEADER_NEW);
82
83 page_no = block->page.id.page_no();
84
85 /* Get the rollback segment file page */
86 rsegf = trx_rsegf_get_new(space_id, page_no, page_size, mtr);
87
88 /* Initialize max size field */
89 mlog_write_ulint(rsegf + TRX_RSEG_MAX_SIZE, max_size, MLOG_4BYTES, mtr);
90
91 /* Initialize the history list */
92 mlog_write_ulint(rsegf + TRX_RSEG_HISTORY_SIZE, 0, MLOG_4BYTES, mtr);
93 flst_init(rsegf + TRX_RSEG_HISTORY, mtr);
94
95 /* Reset the undo log slots */
96 for (i = 0; i < TRX_RSEG_N_SLOTS; i++) {
97 trx_rsegf_set_nth_undo(rsegf, i, FIL_NULL, mtr);
98 }
99
100 /* Initialize maximum transaction number. */
101 mlog_write_ull(rsegf + TRX_RSEG_MAX_TRX_NO, 0, mtr);
102
103 if (space_id == TRX_SYS_SPACE) {
104 /* All rollback segments in the system tablespace need
105 to be found in the TRX_SYS page in the rseg_id slot.
106 Add the rollback segment info to the free slot in the
107 trx system header in the TRX_SYS page. */
108
109 sys_header = trx_sysf_get(mtr);
110
111 trx_sysf_rseg_set_space(sys_header, rseg_slot, space_id, mtr);
112
113 trx_sysf_rseg_set_page_no(sys_header, rseg_slot, page_no, mtr);
114
115 } else if (fsp_is_system_temporary(space_id)) {
116 /* Rollback segments in the system temporary tablespace
117 are re-created on restart. So they only need to be
118 referenced in memory. */
119
120 } else {
121 /* Rollback Segments in independent undo tablespaces
122 are tracked in the RSEG_ARRAY page. */
123 rsegs_header = trx_rsegsf_get(space_id, mtr);
124
125 trx_rsegsf_set_page_no(rsegs_header, rseg_slot, page_no, mtr);
126 }
127
128 return (page_no);
129 }
130
131 /** Free an instance of the rollback segment in memory.
132 @param[in] rseg pointer to an rseg to free */
133 void trx_rseg_mem_free(trx_rseg_t *rseg) {
134 mutex_free(&rseg->mutex);
135
136 /* There can't be any active transactions. */
137 ut_a(UT_LIST_GET_LEN(rseg->update_undo_list) == 0);
138 ut_a(UT_LIST_GET_LEN(rseg->insert_undo_list) == 0);
139
140 for (auto undo : rseg->update_undo_cached.removable()) {
141 UT_LIST_REMOVE(rseg->update_undo_cached, undo);
142
143 MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED);
144
145 trx_undo_mem_free(undo);
146 }
147
148 for (auto undo : rseg->insert_undo_cached.removable()) {
149 UT_LIST_REMOVE(rseg->insert_undo_cached, undo);
150
151 MONITOR_DEC(MONITOR_NUM_UNDO_SLOT_CACHED);
152
153 trx_undo_mem_free(undo);
154 }
155
156 ut::free(rseg);
157 }
158
159 static void trx_rseg_persist_gtid(trx_rseg_t *rseg, trx_id_t gtid_trx_no) {
160 /* Old server where GTID persistence were not enabled. */
161 if (gtid_trx_no == 0) {
162 return;
163 }
164 /* The mini-transactions used in this function should not do any
165 modification/write operation. We read the undo header and send GTIDs
166 to the GTID persistor. There is no impact if the server crashes
167 anytime during the operation. */
168 mtr_t mtr;
169 mtr_start(&mtr);
170
171 auto rseg_header =
172 trx_rsegf_get_new(rseg->space_id, rseg->page_no, rseg->page_size, &mtr);
173
174 auto rseg_max_trx_no = mach_read_from_8(rseg_header + TRX_RSEG_MAX_TRX_NO);
175
176 /* Check if GTID for transactions in this rollback segment are persisted. */
177 if (rseg_max_trx_no < gtid_trx_no) {
178 mtr_commit(&mtr);
179 return;
180 }
181
182 /* Head of transaction history list in rollback segment. */
183 auto node = rseg_header + TRX_RSEG_HISTORY;
184
185 fil_addr_t node_addr = flst_get_first(node, &mtr);
186 ut_ad(node_addr.page != FIL_NULL);
187
188 mtr_commit(&mtr);
189
190 while (node_addr.page != FIL_NULL) {
191 mtr_start(&mtr);
192 /* Get the undo page pointed by current node. */
193 page_id_t undo_page_id(rseg->space_id, node_addr.page);
194 auto undo_page = trx_undo_page_get(undo_page_id, rseg->page_size, &mtr);
195
196 /* Get undo log and trx_no for the transaction. */
197 node = undo_page + node_addr.boffset;
198 auto undo_log = node - TRX_UNDO_HISTORY_NODE;
199 auto undo_trx_no = mach_read_from_8(undo_log + TRX_UNDO_TRX_NO);
200
201 /* Check and exit if the transaction GTID is already persisted. We
202 don't need to check any more as history list is ordered by trx_no. */
203 if (undo_trx_no < gtid_trx_no) {
204 mtr_commit(&mtr);
205 break;
206 }
207 trx_undo_gtid_read_and_persist(undo_log);
208
209 /* Move to next node. */
210 node_addr = flst_get_next_addr(node, &mtr);
211 mtr_commit(&mtr);
212 }
213 }
214
215 /* Initialize rollback segment in memory
216 @param[in] id rollback segment id
217 @param[in] space_id space id of the space
218 @param[in] page_no page no for the rollback segment
219 @param[in] page_size page_size
220 @return trx_rseg_t initialized rollback segment */
221 static trx_rseg_t *trx_rseg_mem_initialize(ulint id, space_id_t space_id,
222 page_no_t page_no,
223 const page_size_t &page_size) {
224 auto rseg = static_cast<trx_rseg_t *>(
225 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(trx_rseg_t)));
226
227 rseg->id = id;
228 rseg->space_id = space_id;
229 rseg->page_size.copy_from(page_size);
230 rseg->page_no = page_no;
231 rseg->trx_ref_count = 0;
232
233 if (fsp_is_system_temporary(space_id)) {
234 mutex_create(LATCH_ID_TEMP_SPACE_RSEG, &rseg->mutex);
235 } else if (fsp_is_undo_tablespace(space_id)) {
236 mutex_create(LATCH_ID_UNDO_SPACE_RSEG, &rseg->mutex);
237 } else {
238 mutex_create(LATCH_ID_TRX_SYS_RSEG, &rseg->mutex);
239 }
240
241 UT_LIST_INIT(rseg->update_undo_list);
242 UT_LIST_INIT(rseg->update_undo_cached);
243 UT_LIST_INIT(rseg->insert_undo_list);
244 UT_LIST_INIT(rseg->insert_undo_cached);
245
246 return rseg;
247 }
248
249 static trx_rseg_t *trx_rseg_physical_initialize(trx_rseg_t *rseg,
250 purge_pq_t *purge_queue,
251 trx_id_t gtid_trx_no,
252 mtr_t *mtr) {
253 auto rseg_header =
254 trx_rsegf_get_new(rseg->space_id, rseg->page_no, rseg->page_size, mtr);
255
256 rseg->max_size =
257 mtr_read_ulint(rseg_header + TRX_RSEG_MAX_SIZE, MLOG_4BYTES, mtr);
258
259 /* Initialize the undo log lists according to the rseg header */
260
261 auto sum_of_undo_sizes = trx_undo_lists_init(rseg);
262
263 rseg->set_curr_size(
264 mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, mtr) +
265 1 + sum_of_undo_sizes);
266
267 auto len = flst_get_len(rseg_header + TRX_RSEG_HISTORY);
268
269 if (len > 0) {
270 trx_sys->rseg_history_len.fetch_add(len);
271
272 /* Extract GTID from history and send to GTID persister. */
273 trx_rseg_persist_gtid(rseg, gtid_trx_no);
274
275 auto node_addr = trx_purge_get_log_from_hist(
276 flst_get_last(rseg_header + TRX_RSEG_HISTORY, mtr));
277
278 rseg->last_page_no = node_addr.page;
279 rseg->last_offset = node_addr.boffset;
280
281 auto undo_log_hdr =
282 trx_undo_page_get(page_id_t(rseg->space_id, node_addr.page),
283 rseg->page_size, mtr) +
284 node_addr.boffset;
285
286 rseg->last_trx_no = mach_read_from_8(undo_log_hdr + TRX_UNDO_TRX_NO);
287
288 #ifdef UNIV_DEBUG
289 /* Update last transaction number during recovery. */
290 mutex_enter(&purge_sys->pq_mutex);
291 if (rseg->last_trx_no > trx_sys->rw_max_trx_no) {
292 trx_sys->rw_max_trx_no = rseg->last_trx_no;
293 }
294 mutex_exit(&purge_sys->pq_mutex);
295 #endif /* UNIV_DEBUG */
296
297 rseg->last_del_marks =
298 mtr_read_ulint(undo_log_hdr + TRX_UNDO_DEL_MARKS, MLOG_2BYTES, mtr);
299
300 TrxUndoRsegs elem(rseg->last_trx_no);
301 elem.insert(rseg);
302
303 if (rseg->last_page_no != FIL_NULL) {
304 /* The only time an rseg is added that has existing
305 undo is when the server is being started. So no
306 mutex is needed here. */
307 ut_ad(srv_is_being_started);
308
309 ut_ad(rseg->space_id == TRX_SYS_SPACE ||
310 (srv_is_upgrade_mode != undo::is_reserved(rseg->space_id)));
311
312 mutex_enter(&purge_sys->pq_mutex);
313 purge_queue->push(std::move(elem));
314 mutex_exit(&purge_sys->pq_mutex);
315 }
316 } else {
317 rseg->last_page_no = FIL_NULL;
318 }
319
320 return (rseg);
321 }
322
323 /** Return a page number from a slot in the rseg_array page of an
324 undo tablespace.
325 @param[in] space_id undo tablespace ID
326 @param[in] rseg_id rollback segment ID
327 @return page_no Page number of the rollback segment header page */
328 page_no_t trx_rseg_get_page_no(space_id_t space_id, ulint rseg_id) {
329 mtr_t mtr;
330 mtr.start();
331
332 trx_rsegsf_t *rsegs_header = trx_rsegsf_get(space_id, &mtr);
333
334 page_no_t page_no = trx_rsegsf_get_page_no(rsegs_header, rseg_id, &mtr);
335
336 mtr.commit();
337
338 return (page_no);
339 }
340
341 /** Thread to initialize rollback segments in parallel.
342 @param[in] arg purge queue
343 @param[in] gtid_trx_no GTID to be set in the rollback segment */
344 void trx_rseg_init_thread(void *arg, trx_id_t gtid_trx_no) {
345 trx_rseg_t *rseg = nullptr;
346 purge_pq_t *purge_queue = (purge_pq_t *)arg;
347 while (true) {
348 mutex_enter(&purge_sys->pq_mutex);
349 if (purge_sys->rsegs_queue.empty()) {
350 mutex_exit(&purge_sys->pq_mutex);
351 break;
352 }
353
354 mtr_t mtr;
355 rseg = purge_sys->rsegs_queue.back();
356 purge_sys->rsegs_queue.pop_back();
357 mutex_exit(&purge_sys->pq_mutex);
358
359 mtr_start(&mtr);
360 trx_rseg_physical_initialize(rseg, purge_queue, gtid_trx_no, &mtr);
361 mtr_commit(&mtr);
362 }
363 active_rseg_init_threads.fetch_sub(1);
364 }
365
366 trx_rseg_t *trx_rseg_mem_create(ulint id, space_id_t space_id,
367 page_no_t page_no, const page_size_t &page_size,
368 trx_id_t gtid_trx_no, purge_pq_t *purge_queue,
369 mtr_t *mtr) {
370 auto rseg = static_cast<trx_rseg_t *>(
371 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(trx_rseg_t)));
372
373 rseg->id = id;
374 rseg->space_id = space_id;
375 rseg->page_size.copy_from(page_size);
376 rseg->page_no = page_no;
377 rseg->trx_ref_count = 0;
378
379 if (fsp_is_system_temporary(space_id)) {
380 mutex_create(LATCH_ID_TEMP_SPACE_RSEG, &rseg->mutex);
381 } else if (fsp_is_undo_tablespace(space_id)) {
382 mutex_create(LATCH_ID_UNDO_SPACE_RSEG, &rseg->mutex);
383 } else {
384 mutex_create(LATCH_ID_TRX_SYS_RSEG, &rseg->mutex);
385 }
386
387 UT_LIST_INIT(rseg->update_undo_list);
388 UT_LIST_INIT(rseg->update_undo_cached);
389 UT_LIST_INIT(rseg->insert_undo_list);
390 UT_LIST_INIT(rseg->insert_undo_cached);
391
392 auto rseg_header = trx_rsegf_get_new(space_id, page_no, page_size, mtr);
393
394 rseg->max_size =
395 mtr_read_ulint(rseg_header + TRX_RSEG_MAX_SIZE, MLOG_4BYTES, mtr);
396
397 /* Initialize the undo log lists according to the rseg_header */
398 auto sum_of_undo_sizes = trx_undo_lists_init(rseg);
399
400 rseg->set_curr_size(
401 mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, mtr) +
402 1 + sum_of_undo_sizes);
403
404 auto len = flst_get_len(rseg_header + TRX_RSEG_HISTORY);
405
406 if (len > 0) {
407 trx_sys->rseg_history_len += len;
408
409 /* Extract GTID from history and send to GTID persistor. */
410 trx_rseg_persist_gtid(rseg, gtid_trx_no);
411
412 auto node_addr = trx_purge_get_log_from_hist(
413 flst_get_last(rseg_header + TRX_RSEG_HISTORY, mtr));
414
415 rseg->last_page_no = node_addr.page;
416 rseg->last_offset = node_addr.boffset;
417
418 auto undo_log_hdr =
419 trx_undo_page_get(page_id_t(rseg->space_id, node_addr.page),
420 rseg->page_size, mtr) +
421 node_addr.boffset;
422
423 rseg->last_trx_no = mach_read_from_8(undo_log_hdr + TRX_UNDO_TRX_NO);
424
425 #ifdef UNIV_DEBUG
426 /* Update last transactioin number during recovery. */
427 if (rseg->last_trx_no > trx_sys->rw_max_trx_no) {
428 trx_sys->rw_max_trx_no = rseg->last_trx_no;
429 }
430 #endif // UNIV_DEBUG
431
432 rseg->last_del_marks =
433 mtr_read_ulint(undo_log_hdr + TRX_UNDO_DEL_MARKS, MLOG_2BYTES, mtr);
434
435 TrxUndoRsegs elem(rseg->last_trx_no);
436 elem.insert(rseg);
437
438 if (rseg->last_page_no != FIL_NULL) {
439 /* The only time an rseg is added that has existing
440 undo is when the server is being started. So no
441 mutex is needed here. */
442 ut_ad(srv_is_being_started);
443
444 ut_ad(space_id == TRX_SYS_SPACE ||
445 (srv_is_upgrade_mode != undo::is_reserved(space_id)));
446
447 purge_queue->push(elem);
448 }
449 } else {
450 rseg->last_page_no = FIL_NULL;
451 }
452
453 return rseg;
454 }
455
456 /** Read each rollback segment slot in the TRX_SYS page and the RSEG_ARRAY
457 page of each undo tablespace. Create trx_rseg_t objects for all rollback
458 segments found. This runs at database startup and initializes the in-memory
459 lists of trx_rseg_t objects. We need to look at all slots in TRX_SYS and
460 each RSEG_ARRAY page because we need to look for any existing undo log that
461 may need to be recovered by purge. No latch is needed since this is still
462 single-threaded startup.If we find existing rseg slots in TRX_SYS page
463 that reference undo tablespaces and have active undo logs, then quit.
464 They require an upgrade of undo tablespaces and that cannot happen with
465 active undo logs.
466 @param[in] purge_queue queue of rsegs to purge */
467 void trx_rsegs_init_start(purge_pq_t *purge_queue) {
468 trx_sys->rseg_history_len = 0;
469
470 uint32_t slot;
471 mtr_t mtr;
472 space_id_t space_id;
473 page_no_t page_no;
474 trx_rseg_t *rseg = nullptr;
475
476 for (slot = 0; slot < TRX_SYS_N_RSEGS; slot++) {
477 mtr.start();
478 trx_sysf_t *sys_header = trx_sysf_get(&mtr);
479
480 page_no = trx_sysf_rseg_get_page_no(sys_header, slot, &mtr);
481
482 if (page_no != FIL_NULL) {
483 space_id = trx_sysf_rseg_get_space(sys_header, slot, &mtr);
484
485 if (!undo::is_active_truncate_log_present(undo::id2num(space_id))) {
486 /* Create the trx_rseg_t object.
487 Note that all tablespaces with rollback segments
488 use univ_page_size. (system, temp & undo) */
489 rseg = trx_rseg_mem_initialize(slot, space_id, page_no, univ_page_size);
490
491 ut_a(rseg->id == slot);
492
493 purge_sys->rsegs_queue.push_back(rseg);
494
495 trx_sys->rsegs.push_back(rseg);
496 }
497 }
498 mtr.commit();
499 }
500
501 undo::spaces->s_lock();
502 for (auto undo_space : undo::spaces->m_spaces) {
503 /* Remember the size of the purge queue before processing this
504 undo tablespace. */
505 size_t purge_queue_size = purge_queue->size();
506
507 undo_space->rsegs()->x_lock();
508
509 for (slot = 0; slot < FSP_MAX_ROLLBACK_SEGMENTS; slot++) {
510 page_no = trx_rseg_get_page_no(undo_space->id(), slot);
511
512 /* There are no gaps in an RSEG_ARRAY page. New rsegs
513 are added sequentially and never deleted undo the
514 undo tablespace is truncated. */
515 if (page_no == FIL_NULL) {
516 break;
517 }
518
519 mtr.start();
520
521 /* Create the trx_rseg_t object.
522 Note that all tablespaces with rollback segments
523 use univ_page_size. */
524 rseg = trx_rseg_mem_initialize(slot, undo_space->id(), page_no,
525 univ_page_size);
526
527 ut_a(rseg->id == slot);
528
529 undo_space->rsegs()->push_back(rseg);
530 purge_sys->rsegs_queue.push_back(rseg);
531
532 mtr.commit();
533 }
534 undo_space->rsegs()->x_unlock();
535
536 /* If there are no undo logs in this explicit undo tablespace at
537 startup, mark it empty so that it will not be used until the state
538 recorded in the DD can be applied in apply_dd_undo_state(). */
539 if (undo_space->is_explicit() && !undo_space->is_empty()) {
540 size_t cur_size = purge_queue->size();
541 if (purge_queue_size == cur_size) {
542 undo_space->set_empty();
543 }
544 }
545 }
546 undo::spaces->s_unlock();
547 }
548
549 void trx_rsegs_init_end() {
550 #ifdef UNIV_DEBUG
551 while (active_rseg_init_threads.fetch_add(0) != 0) {
552 ut_d(ut_error);
553 }
554 #endif // UNIV_DEBUG
555
556 for (uint32_t i = 0; i < purge_sys->rsegs_queue.size(); i++) {
557 trx_rseg_t *rseg = purge_sys->rsegs_queue.at(i);
558 ut_a(rseg->get_curr_size() > 0);
559 }
560
561 purge_sys->rsegs_queue.clear();
562 }
563
564 void trx_rsegs_parallel_init(purge_pq_t *purge_queue) /*!< in: rseg queue */
565 {
566 purge_sys->rsegs_queue.clear();
567 std::vector<IB_thread> threads;
568 active_rseg_init_threads = srv_rseg_init_threads;
569
570 /* Get GTID transaction from SYS */
571 mtr_t mtr;
572 mtr.start();
573 trx_sysf_t *sys_header = trx_sysf_get(&mtr);
574 auto page = sys_header - TRX_SYS;
575 auto gtid_trx_no = mach_read_from_8(page + TRX_SYS_TRX_NUM_GTID);
576
577 mtr.commit();
578
579 auto &gtid_persistor = clone_sys->get_gtid_persistor();
580 gtid_persistor.set_oldest_trx_no_recovery(gtid_trx_no);
581
582 trx_rsegs_init_start(purge_queue);
583
584 if (purge_sys->rsegs_queue.empty()) {
585 return;
586 }
587
588 for (uint32_t i = 0; i < srv_rseg_init_threads; i++) {
589 auto thread =
590 os_thread_create(parallel_rseg_init_thread_key, 0, trx_rseg_init_thread,
591 (void *)purge_queue, gtid_trx_no);
592 threads.emplace_back(thread);
593 thread.start();
594 }
595
596 for (auto t : threads) {
597 t.join();
598 }
599
600 ib::info(ER_IB_MSG_PAR_RSEG_INIT_COMPLETE_MSG);
601 trx_rsegs_init_end();
602 }
603
604 /** Read each rollback segment slot in the TRX_SYS page and the RSEG_ARRAY
605 page of each undo tablespace. Create trx_rseg_t objects for all rollback
606 segments found. This runs at database startup and initializes the in-memory
607 lists of trx_rseg_t objects. We need to look at all slots in TRX_SYS and
608 each RSEG_ARRAY page because we need to look for any existing undo log that
609 may need to be recovered by purge. No latch is needed since this is still
610 single-threaded startup. If we find existing rseg slots in TRX_SYS page
611 that reference undo tablespaces and have active undo logs, then quit.
612 They require an upgrade of undo tablespaces and that cannot happen with
613 active undo logs.
614 @param[in] purge_queue queue of rsegs to purge */
615 void trx_rsegs_init(purge_pq_t *purge_queue) {
616 trx_sys->rseg_history_len.store(0);
617
618 ulint slot;
619 mtr_t mtr;
620 space_id_t space_id;
621 page_no_t page_no;
622 trx_rseg_t *rseg = nullptr;
623
624 /* Get GTID transaction number from SYS */
625 mtr.start();
626 trx_sysf_t *sys_header = trx_sysf_get(&mtr);
627 auto page = sys_header - TRX_SYS;
628 auto gtid_trx_no = mach_read_from_8(page + TRX_SYS_TRX_NUM_GTID);
629
630 mtr.commit();
631
632 auto &gtid_persistor = clone_sys->get_gtid_persistor();
633 gtid_persistor.set_oldest_trx_no_recovery(gtid_trx_no);
634
635 for (slot = 0; slot < TRX_SYS_N_RSEGS; slot++) {
636 mtr.start();
637 trx_sysf_t *sys_header = trx_sysf_get(&mtr);
638
639 page_no = trx_sysf_rseg_get_page_no(sys_header, slot, &mtr);
640
641 if (page_no != FIL_NULL) {
642 space_id = trx_sysf_rseg_get_space(sys_header, slot, &mtr);
643
644 if (!undo::is_active_truncate_log_present(undo::id2num(space_id))) {
645 /* Create the trx_rseg_t object.
646 Note that all tablespaces with rollback segments
647 use univ_page_size. (system, temp & undo) */
648 rseg = trx_rseg_mem_create(slot, space_id, page_no, univ_page_size,
649 gtid_trx_no, purge_queue, &mtr);
650
651 ut_a(rseg->id == slot);
652
653 trx_sys->rsegs.push_back(rseg);
654 }
655 }
656 mtr.commit();
657 }
658
659 undo::spaces->s_lock();
660 for (auto undo_space : undo::spaces->m_spaces) {
661 /* Remember the size of the purge queue before processing this
662 undo tablespace. */
663 size_t purge_queue_size = purge_queue->size();
664
665 undo_space->rsegs()->x_lock();
666
667 for (slot = 0; slot < FSP_MAX_ROLLBACK_SEGMENTS; slot++) {
668 page_no = trx_rseg_get_page_no(undo_space->id(), slot);
669
670 /* There are no gaps in an RSEG_ARRAY page. New rsegs
671 are added sequentially and never deleted until the
672 undo tablespace is truncated.*/
673 if (page_no == FIL_NULL) {
674 break;
675 }
676
677 mtr.start();
678
679 /* Create the trx_rseg_t object.
680 Note that all tablespaces with rollback segments
681 use univ_page_size. */
682 rseg =
683 trx_rseg_mem_create(slot, undo_space->id(), page_no, univ_page_size,
684 gtid_trx_no, purge_queue, &mtr);
685
686 ut_a(rseg->id == slot);
687
688 undo_space->rsegs()->push_back(rseg);
689
690 mtr.commit();
691 }
692 undo_space->rsegs()->x_unlock();
693
694 /* If there are no undo logs in this explicit undo tablespace at
695 startup, mark it empty so that it will not be used until the state
696 recorded in the DD can be applied in apply_dd_undo_state(). */
697 if (undo_space->is_explicit() && !undo_space->is_empty()) {
698 size_t cur_size = purge_queue->size();
699 if (purge_queue_size == cur_size) {
700 undo_space->set_empty();
701 }
702 }
703 }
704 undo::spaces->s_unlock();
705 }
706
707 /** Create a rollback segment in the given tablespace. This could be either
708 the system tablespace, the temporary tablespace, or an undo tablespace.
709 @param[in] space_id tablespace to get the rollback segment
710 @param[in] rseg_id slot number of the rseg within this tablespace
711 @return page number of the rollback segment header page created */
712 page_no_t trx_rseg_create(space_id_t space_id, ulint rseg_id) {
713 mtr_t mtr;
714 fil_space_t *space = fil_space_get(space_id);
715
716 log_free_check();
717
718 mtr_start(&mtr);
719
720 /* To obey the latching order, acquire the file space
721 x-latch before the mutex for trx_sys. */
722 mtr_x_lock(&space->latch, &mtr, UT_LOCATION_HERE);
723
724 ut_ad(space->purpose == (fsp_is_system_temporary(space_id)
725 ? FIL_TYPE_TEMPORARY
726 : FIL_TYPE_TABLESPACE));
727 ut_ad(univ_page_size.equals_to(page_size_t(space->flags)));
728
729 if (fsp_is_system_temporary(space_id)) {
730 mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
731 } else if (space_id == TRX_SYS_SPACE) {
732 /* We will modify TRX_SYS_RSEGS in TRX_SYS page. */
733 }
734
735 page_no_t page_no = trx_rseg_header_create(space_id, univ_page_size,
736 PAGE_NO_MAX, rseg_id, &mtr);
737
738 mtr_commit(&mtr);
739
740 return (page_no);
741 }
742
743 /** Initialize */
744 void Rsegs::init() {
745 m_rsegs.reserve(TRX_SYS_N_RSEGS);
746
747 m_latch = static_cast<rw_lock_t *>(
748 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*m_latch)));
749
750 rw_lock_create(rsegs_lock_key, m_latch, LATCH_ID_RSEGS);
751 }
752
753 /** De-initialize */
754 void Rsegs::deinit() {
755 clear();
756
757 rw_lock_free(m_latch);
758 ut::free(m_latch);
759 m_latch = nullptr;
760 }
761
762 /** Clear the vector of cached rollback segments leaving the
763 reserved space allocated. */
764 void Rsegs::clear() {
765 for (auto rseg : m_rsegs) {
766 trx_rseg_mem_free(rseg);
767 }
768 m_rsegs.clear();
769 m_rsegs.shrink_to_fit();
770 }
771
772 /** Find an rseg in the std::vector that uses the rseg_id given.
773 @param[in] rseg_id A slot in a durable array such as
774 the TRX_SYS page or RSEG_ARRAY page.
775 @return a pointer to an trx_rseg_t that uses the rseg_id. */
776 trx_rseg_t *Rsegs::find(ulint rseg_id) {
777 trx_rseg_t *rseg;
778
779 /* In most cases, the rsegs will be in slot order with no gaps. */
780 if (rseg_id < m_rsegs.size()) {
781 rseg = m_rsegs.at(rseg_id);
782 if (rseg->id == rseg_id) {
783 return (rseg);
784 }
785 }
786
787 /* If there are gaps in the numbering, do a search. */
788 for (auto rseg : m_rsegs) {
789 if (rseg->id == rseg_id) {
790 return (rseg);
791 }
792 }
793
794 return (nullptr);
795 }
796
797 /** This does two things to the target tablespace.
798 1. Find or create (trx_rseg_create) the requested number of rollback segments.
799 2. Make sure each rollback segment is tracked in memory (trx_rseg_mem_create).
800 All existing rollback segments were found earlier in trx_rsegs_init().
801 This will add new ones if we need them according to target_rsegs.
802 @param[in] space_id tablespace ID that should contain rollback
803 segments
804 @param[in] target_rsegs target number of rollback segments per
805 tablespace
806 @param[in] rsegs list of rsegs to add to
807 @param[in,out] n_total_created A running total of rollback segment created in
808 undo tablespaces
809 @return true if all rsegs are added, false if not. */
810 bool trx_rseg_add_rollback_segments(space_id_t space_id, ulong target_rsegs,
811 Rsegs *rsegs,
812 ulint *const n_total_created) {
813 bool success = true;
814 mtr_t mtr;
815 page_no_t page_no;
816 trx_rseg_t *rseg;
817 ulint n_created = 0;
818 ulint n_tracked = 0;
819
820 enum space_type_t { TEMP, UNDO } type;
821
822 ut_ad(space_id != TRX_SYS_SPACE);
823
824 type = (fsp_is_undo_tablespace(space_id) ? UNDO : TEMP);
825 ut_ad(type == UNDO || fsp_is_system_temporary(space_id));
826
827 /* Protect against two threads trying to add rollback segments
828 at the same time. */
829 rsegs->x_lock();
830
831 for (ulint num = 0; num < FSP_MAX_ROLLBACK_SEGMENTS; num++) {
832 if (rsegs->size() >= target_rsegs) {
833 break;
834 }
835
836 ulint rseg_id = num;
837
838 /* If the rseg object exists, move to the next rseg_id. */
839 rseg = rsegs->find(rseg_id);
840 if (rseg != nullptr) {
841 ut_ad(rseg->id == rseg_id);
842 continue;
843 }
844
845 /* Look in the tablespace to discover if the rollback segment
846 already exists. */
847 if (type == UNDO) {
848 page_no = trx_rseg_get_page_no(space_id, rseg_id);
849
850 } else {
851 /* There is no durable list of rollback segments in
852 the temporary tablespace. Since it was not found in
853 the rsegs vector, assume the rollback segment does
854 not exist in the temp tablespace. */
855 page_no = FIL_NULL;
856 }
857
858 if (page_no == FIL_NULL) {
859 /* Create the missing rollback segment if allowed. */
860 if (type == TEMP || (!srv_read_only_mode && srv_force_recovery == 0)) {
861 page_no = trx_rseg_create(space_id, rseg_id);
862 if (page_no == FIL_NULL) {
863 /* There may not be enough space in
864 the temporary tablespace since it is
865 possible to limit its size. */
866 ut_ad(type == TEMP);
867 continue;
868 }
869 n_created++;
870 } else {
871 /* trx_rseg_create() is being prevented
872 in an UNDO tablespace. Don't try to create
873 any more. */
874 break;
875 }
876 }
877
878 /* Create the trx_rseg_t object. */
879 mtr.start();
880
881 fil_space_t *space = fil_space_get(space_id);
882 ut_ad(univ_page_size.equals_to(page_size_t(space->flags)));
883 mtr_x_lock(&space->latch, &mtr, UT_LOCATION_HERE);
884
885 if (type == TEMP) {
886 mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
887 }
888
889 rseg = trx_rseg_mem_create(rseg_id, space_id, page_no, univ_page_size, 0,
890 purge_sys->purge_queue, &mtr);
891
892 mtr.commit();
893
894 if (rseg != nullptr) {
895 ut_a(rseg->id == rseg_id);
896 rsegs->push_back(rseg);
897 n_tracked++;
898 }
899 }
900
901 rsegs->x_unlock();
902
903 std::ostringstream loc;
904 switch (type) {
905 case UNDO:
906 loc << "undo tablespace number " << undo::id2num(space_id);
907 break;
908 case TEMP:
909 loc << "the temporary tablespace";
910 break;
911 }
912
913 ulint n_known = rsegs->size();
914 if (n_known < target_rsegs) {
915 if (srv_read_only_mode || srv_force_recovery > 0) {
916 bool use_and = srv_read_only_mode && srv_force_recovery > 0;
917
918 ib::info(ER_IB_MSG_1191)
919 << "Could not create all " << target_rsegs << " rollback segments in "
920 << loc.str() << " because "
921 << (srv_read_only_mode ? " read-only mode is set" : "")
922 << (use_and ? " and" : "")
923 << (srv_force_recovery > 0 ? " innodb_force_recovery is set" : "")
924 << ". Only " << n_known << " are active.";
925
926 srv_rollback_segments =
927 std::min(srv_rollback_segments, static_cast<ulong>(n_known));
928
929 } else {
930 ib::warn(ER_IB_MSG_1192)
931 << "Could not create all " << target_rsegs << " rollback segments in "
932 << loc.str() << ". Only " << n_known << " are active.";
933
934 srv_rollback_segments =
935 std::min(srv_rollback_segments, static_cast<ulong>(n_known));
936
937 success = false;
938 }
939
940 } else if (n_created > 0) {
941 ib::info(ER_IB_MSG_1193)
942 << "Created " << n_created << " and tracked " << n_tracked
943 << " new rollback segment(s) in " << loc.str() << ". " << target_rsegs
944 << " are now active.";
945
946 } else if (n_tracked > 0) {
947 ib::info(ER_IB_MSG_1194)
948 << "Using " << n_tracked << " more rollback segment(s) in " << loc.str()
949 << ". " << target_rsegs << " are now active.";
950
951 } else if (target_rsegs < n_known) {
952 ib::info(ER_IB_MSG_1195)
953 << target_rsegs << " rollback segment(s) are now active in "
954 << loc.str() << ".";
955 }
956
957 if (n_total_created != nullptr) {
958 *n_total_created += n_created;
959 }
960
961 /* Save the size of the undo tablespace now that all rsegs have been created.
962 No need to do this for the system temporary tablespace. */
963 if (type == UNDO) {
964 fil_space_set_undo_size(space_id, true);
965 }
966 return (success);
967 }
968
969 /** Add more rsegs to the rseg list in each tablespace until there are
970 srv_rollback_segments of them. Use any rollback segment that already
971 exists so that the purge_queue can be filled and processed with any
972 existing undo log. If the rollback segments do not exist in this
973 tablespace and we need them according to target_rollback_segments,
974 then build them in the tablespace.
975 @param[in] target_rollback_segments new number of rollback
976 segments per space
977 @return true if all necessary rollback segments and trx_rseg_t objects
978 were created. */
979 bool trx_rseg_adjust_rollback_segments(ulong target_rollback_segments) {
980 /** The number of rollback segments created in the datafile. */
981 ulint n_total_created = 0;
982
983 /* Make sure Temporary Tablespace has enough rsegs. */
984 if (!trx_rseg_add_rollback_segments(srv_tmp_space.space_id(),
985 target_rollback_segments,
986 &(trx_sys->tmp_rsegs), nullptr)) {
987 return (false);
988 }
989
990 /* Only the temp rsegs are used with a high force_recovery. */
991 if (srv_force_recovery >= SRV_FORCE_NO_UNDO_LOG_SCAN) {
992 return (true);
993 }
994
995 /* Adjust the number of rollback segments in each Undo Tablespace
996 whether or not it is currently active. If rollback segments are written
997 to the tablespace, they will be checkpointed. But we cannot hold
998 undo::spaces->s_lock while doing a checkpoint because of latch order
999 violation. So traverse the list by ID. */
1000 undo::spaces->s_lock();
1001 for (auto undo_space : undo::spaces->m_spaces) {
1002 if (!trx_rseg_add_rollback_segments(
1003 undo_space->id(), target_rollback_segments, undo_space->rsegs(),
1004 &n_total_created)) {
1005 undo::spaces->s_unlock();
1006 return (false);
1007 }
1008 }
1009 undo::spaces->s_unlock();
1010
1011 /* Make sure these rollback segments are checkpointed. */
1012 if (n_total_created > 0 && !srv_read_only_mode && srv_force_recovery == 0) {
1013 log_make_latest_checkpoint();
1014 }
1015
1016 return (true);
1017 }
1018
1019 /** Create the requested number of Rollback Segments in a newly created undo
1020 tablespace and add them to the Rsegs object.
1021 @param[in] space_id undo tablespace ID
1022 @param[in] target_rollback_segments number of rollback segments per space
1023 @return true if all necessary rollback segments and trx_rseg_t objects
1024 were created. */
1025 bool trx_rseg_init_rollback_segments(space_id_t space_id,
1026 ulong target_rollback_segments) {
1027 /** The number of rollback segments created in the datafile. */
1028 ulint n_total_created = 0;
1029
1030 undo::spaces->s_lock();
1031 space_id_t space_num = undo::id2num(space_id);
1032 undo::Tablespace *undo_space = undo::spaces->find(space_num);
1033 undo::spaces->s_unlock();
1034
1035 if (!trx_rseg_add_rollback_segments(space_id, target_rollback_segments,
1036 undo_space->rsegs(), &n_total_created)) {
1037 return (false);
1038 }
1039
1040 return (true);
1041 }
1042
1043 /** Build a list of unique undo tablespaces found in the TRX_SYS page.
1044 Do not count the system tablespace. The vector will be sorted on space id.
1045 @param[in,out] spaces_to_open list of undo tablespaces found. */
1046 void trx_rseg_get_n_undo_tablespaces(Space_Ids *spaces_to_open) {
1047 ulint i;
1048 mtr_t mtr;
1049 trx_sysf_t *sys_header;
1050
1051 ut_ad(spaces_to_open->empty());
1052
1053 mtr_start(&mtr);
1054
1055 sys_header = trx_sysf_get(&mtr);
1056
1057 for (i = 0; i < TRX_SYS_N_RSEGS; i++) {
1058 page_no_t page_no;
1059 space_id_t space_id;
1060
1061 page_no = trx_sysf_rseg_get_page_no(sys_header, i, &mtr);
1062
1063 if (page_no == FIL_NULL) {
1064 continue;
1065 }
1066
1067 space_id = trx_sysf_rseg_get_space(sys_header, i, &mtr);
1068
1069 /* The system space id should not be in this array. */
1070 if (space_id != TRX_SYS_SPACE && !spaces_to_open->contains(space_id)) {
1071 spaces_to_open->push_back(space_id);
1072 }
1073 }
1074
1075 mtr_commit(&mtr);
1076
1077 ut_a(spaces_to_open->size() <= TRX_SYS_N_RSEGS);
1078 }
1079
1080 /** Upgrade the TRX_SYS page so that it no longer tracks rsegs in undo
1081 tablespaces. It should only track rollback segments in the system tablespace.
1082 Put FIL_NULL in the slots in TRX_SYS. Latch protection is not needed since
1083 this is during single-threaded startup. */
1084 void trx_rseg_upgrade_undo_tablespaces() {
1085 ulint i;
1086 mtr_t mtr;
1087 trx_sysf_t *sys_header;
1088
1089 mtr_start(&mtr);
1090 fil_space_t *space = fil_space_get(TRX_SYS_SPACE);
1091 mtr_x_lock(&space->latch, &mtr, UT_LOCATION_HERE);
1092
1093 sys_header = trx_sysf_get(&mtr);
1094
1095 /* First, put FIL_NULL in all the slots that contain the space_id
1096 of any non-system tablespace. The rollback segments in those
1097 tablespaces are replaced when the file is replaced. */
1098 for (i = 0; i < TRX_SYS_N_RSEGS; i++) {
1099 page_no_t page_no;
1100 space_id_t space_id;
1101
1102 page_no = trx_sysf_rseg_get_page_no(sys_header, i, &mtr);
1103
1104 if (page_no == FIL_NULL) {
1105 continue;
1106 }
1107
1108 space_id = trx_sysf_rseg_get_space(sys_header, i, &mtr);
1109
1110 /* The TRX_SYS page only tracks older undo tablespaces
1111 that do not use the RSEG_ARRAY page. */
1112 ut_a(space_id < dict_sys_t::s_min_undo_space_id);
1113
1114 /* Leave rollback segments in the system tablespace
1115 untouched in case innodb_undo_tablespaces is later
1116 set back to 0. */
1117 if (space_id != 0) {
1118 trx_sysf_rseg_set_space(sys_header, i, FIL_NULL, &mtr);
1119
1120 trx_sysf_rseg_set_page_no(sys_header, i, FIL_NULL, &mtr);
1121 }
1122 }
1123
1124 mtr_commit(&mtr);
1125 }
1126
1127 /** Create the file page for the rollback segment directory in an undo
1128 tablespace. This function is called just after an undo tablespace is
1129 created so the next page created here should by FSP_FSEG_DIR_PAGE_NUM.
1130 @param[in] space_id Undo Tablespace ID
1131 @param[in] mtr mtr */
1132 void trx_rseg_array_create(space_id_t space_id, mtr_t *mtr) {
1133 trx_rsegsf_t *rsegs_header;
1134 buf_block_t *block;
1135 page_t *page;
1136 byte *ptr;
1137 ulint len;
1138
1139 /* Create the fseg directory file block in a new allocated file segment */
1140 block = fseg_create(space_id, 0,
1141 RSEG_ARRAY_HEADER + RSEG_ARRAY_FSEG_HEADER_OFFSET, mtr);
1142 buf_block_dbg_add_level(block, SYNC_RSEG_ARRAY_HEADER);
1143
1144 ut_a(block->page.id.page_no() == FSP_RSEG_ARRAY_PAGE_NO);
1145
1146 page = buf_block_get_frame(block);
1147
1148 mlog_write_ulint(page + FIL_PAGE_TYPE, FIL_PAGE_TYPE_RSEG_ARRAY, MLOG_2BYTES,
1149 mtr);
1150
1151 rsegs_header = page + RSEG_ARRAY_HEADER;
1152
1153 /* Initialize the rseg array version. */
1154 mach_write_to_4(rsegs_header + RSEG_ARRAY_VERSION_OFFSET, RSEG_ARRAY_VERSION);
1155
1156 /* Initialize the directory size. */
1157 mach_write_to_4(rsegs_header + RSEG_ARRAY_SIZE_OFFSET, 0);
1158
1159 /* Reset the rollback segment header page slots. Use the full page
1160 minus overhead. Reserve some extra room for future use. */
1161 ptr = RSEG_ARRAY_PAGES_OFFSET + rsegs_header;
1162 len = UNIV_PAGE_SIZE - RSEG_ARRAY_HEADER - RSEG_ARRAY_PAGES_OFFSET -
1163 RSEG_ARRAY_RESERVED_BYTES - FIL_PAGE_DATA_END;
1164 memset(ptr, 0xff, len);
1165
1166 mlog_log_string(rsegs_header,
1167 UNIV_PAGE_SIZE - RSEG_ARRAY_HEADER - FIL_PAGE_DATA_END, mtr);
1168 }
1169
1170 #ifdef UNIV_DEBUG
1171 bool trx_rseg_t::validate_curr_size(bool take_mutex) {
1172 mtr_t mtr;
1173 mtr_start(&mtr);
1174
1175 if (take_mutex) {
1176 mutex_enter(&mutex);
1177 } else {
1178 ut_ad(mutex_own(&mutex));
1179 }
1180
1181 /* Obtain the rollback segment header. */
1182 trx_rsegf_t *rseg_hdr = trx_rsegf_get(space_id, page_no, page_size, &mtr);
1183
1184 /* Number of file pages occupied by the logs in the history list */
1185 ulint hist_size =
1186 mtr_read_ulint(rseg_hdr + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, &mtr);
1187
1188 ulint sum_undo_size = 0;
1189
1190 for (ulint i = 0; i < TRX_RSEG_N_SLOTS; i++) {
1191 /* Get the file page number of the nth undo log slot. */
1192 page_no_t undo_page_no = trx_rsegf_get_nth_undo(rseg_hdr, i, &mtr);
1193
1194 if (undo_page_no == FIL_NULL) {
1195 /* Skip the empty slot. */
1196 continue;
1197 }
1198
1199 /* Get the undo log page. */
1200 page_t *undo_page =
1201 trx_undo_page_get(page_id_t(space_id, undo_page_no), page_size, &mtr);
1202
1203 /* Obtain the undo log segment header. */
1204 trx_usegf_t *seg_header = undo_page + TRX_UNDO_SEG_HDR;
1205
1206 /* Get the number of pages in the undo log segment. */
1207 ulint undo_size = flst_get_len(seg_header + TRX_UNDO_PAGE_LIST);
1208
1209 sum_undo_size += undo_size;
1210 }
1211
1212 if (take_mutex) {
1213 mutex_exit(&mutex);
1214 }
1215 mtr_commit(&mtr);
1216
1217 ulint total_size = sum_undo_size + hist_size + 1;
1218
1219 ut_ad(total_size == curr_size);
1220
1221 return (total_size == curr_size);
1222 }
1223 #endif /* UNIV_DEBUG */