Debian Codegraph @

storage/innobase/include/trx0purge.h

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 include/trx0purge.h
29 Purge old versions
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef trx0purge_h
35 #define trx0purge_h
36
37 #include "fil0fil.h"
38 #include "mtr0mtr.h"
39 #include "page0page.h"
40 #include "que0types.h"
41 #include "read0types.h"
42 #include "trx0sys.h"
43 #include "trx0types.h"
44 #include "univ.i"
45 #include "usr0sess.h"
46 #ifdef UNIV_HOTBACKUP
47 #include "trx0sys.h"
48 #endif /* UNIV_HOTBACKUP */
49
50 /** The global data structure coordinating a purge */
51 extern trx_purge_t *purge_sys;
52
53 /** Calculates the file address of an undo log header when we have the file
54 address of its history list node.
55 @return file address of the log */
56 static inline fil_addr_t trx_purge_get_log_from_hist(
57 fil_addr_t node_addr); /*!< in: file address of the history
58 list node of the log */
59
60 /** Initialize in-memory purge structures */
61 void trx_purge_sys_mem_create();
62
63 /** Creates the global purge system control structure and inits the history
64 mutex.
65 @param[in] n_purge_threads number of purge threads
66 @param[in,out] purge_queue UNDO log min binary heap */
67 void trx_purge_sys_initialize(uint32_t n_purge_threads,
68 purge_pq_t *purge_queue);
69
70 /** Frees the global purge system control structure. */
71 void trx_purge_sys_close(void);
72
73 /************************************************************************
74 Adds the update undo log as the first log in the history list. Removes the
75 update undo log segment from the rseg slot if it is too big for reuse. */
76 void trx_purge_add_update_undo_to_history(
77 trx_t *trx, /*!< in: transaction */
78 trx_undo_ptr_t *undo_ptr, /*!< in: update undo log. */
79 page_t *undo_page, /*!< in: update undo log header page,
80 x-latched */
81 bool update_rseg_history_len,
82 /*!< in: if true: update rseg history
83 len else skip updating it. */
84 ulint n_added_logs, /*!< in: number of logs added */
85 mtr_t *mtr); /*!< in: mtr */
86
87 /** This function runs a purge batch.
88 @return number of undo log pages handled in the batch */
89 ulint trx_purge(ulint n_purge_threads, /*!< in: number of purge tasks to
90 submit to task queue. */
91 ulint limit, /*!< in: the maximum number of
92 records to purge in one batch */
93 bool truncate); /*!< in: truncate history if true */
94
95 /** Stop purge and wait for it to stop, move to PURGE_STATE_STOP. */
96 void trx_purge_stop(void);
97 /** Resume purge, move to PURGE_STATE_RUN. */
98 void trx_purge_run(void);
99
100 /** Purge states */
101 enum purge_state_t {
102 PURGE_STATE_INIT, /*!< Purge instance created */
103 PURGE_STATE_RUN, /*!< Purge should be running */
104 PURGE_STATE_STOP, /*!< Purge should be stopped */
105 PURGE_STATE_EXIT, /*!< Purge has been shutdown */
106 PURGE_STATE_DISABLED /*!< Purge was never started */
107 };
108
109 /** Get the purge state.
110 @return purge state. */
111 purge_state_t trx_purge_state(void);
112
113 // Forward declaration
114 struct TrxUndoRsegsIterator;
115
116 /** This is the purge pointer/iterator. We need both the undo no and the
117 transaction no up to which purge has parsed and applied the records. */
118 struct purge_iter_t {
119 purge_iter_t() : trx_no(), undo_no(), undo_rseg_space(SPACE_UNKNOWN) {
120 // Do nothing
121 }
122
123 /** Purge has advanced past all transactions whose number
124 is less than this */
125 trx_id_t trx_no;
126
127 /** Purge has advanced past all records whose undo number
128 is less than this. */
129 undo_no_t undo_no;
130
131 /** The last undo record resided in this space id */
132 space_id_t undo_rseg_space;
133
134 /** The transaction that created the undo log record,
135 the Modifier trx id */
136 trx_id_t modifier_trx_id;
137 };
138
139 /* Namespace to hold all the related functions and variables needed
140 to truncate an undo tablespace. */
141 namespace undo {
142
143 /** Magic Number to indicate truncate action is complete. */
144 const uint32_t s_magic = 76845412;
145
146 /** Truncate Log file Prefix. */
147 const char *const s_log_prefix = "undo_";
148
149 /** Truncate Log file Extension. */
150 const char *const s_log_ext = "trunc.log";
151
152 /** The currently used undo space IDs for an undo space number
153 along with a boolean showing whether the undo space number is in use. */
154 struct space_id_account {
155 space_id_t space_id;
156 bool in_use;
157 };
158
159 /** List of currently used undo space IDs for each undo space number
160 along with a boolean showing whether the undo space number is in use. */
161 extern struct space_id_account *space_id_bank;
162
163 /** Check if the space_id is an undo space ID in the reserved range.
164 @param[in] space_id undo tablespace ID
165 @return true if it is in the reserved undo space ID range. */
166 inline bool is_reserved(space_id_t space_id) {
167 return (space_id >= dict_sys_t::s_min_undo_space_id &&
168 space_id <= dict_sys_t::s_max_undo_space_id);
169 }
170
171 /** Convert an undo space number (from 1 to 127) into the undo space_id,
172 given an index indicating which space_id from the pool assigned to that
173 undo number.
174 @param[in] space_num undo tablespace number
175 @param[in] ndx index of the space_id within that undo number
176 @return space_id of the undo tablespace */
177 inline space_id_t num2id(space_id_t space_num, size_t ndx) {
178 ut_ad(space_num > 0);
179 ut_ad(space_num <= FSP_MAX_UNDO_TABLESPACES);
180 ut_ad(ndx < dict_sys_t::s_undo_space_id_range);
181
182 space_id_t space_id = dict_sys_t::s_max_undo_space_id + 1 - space_num -
183 static_cast<space_id_t>(ndx * FSP_MAX_UNDO_TABLESPACES);
184
185 return (space_id);
186 }
187
188 /** Convert an undo space number (from 1 to 127) into an undo space_id.
189 Use the undo::space_id_bank to return the current space_id assigned to
190 that undo number.
191 @param[in] space_num undo tablespace number
192 @return space_id of the undo tablespace */
193 inline space_id_t num2id(space_id_t space_num) {
194 ut_ad(space_num > 0);
195 ut_ad(space_num <= FSP_MAX_UNDO_TABLESPACES);
196
197 size_t slot = space_num - 1;
198
199 /* The space_id_back is normally protected by undo::spaces::m_latch.
200 But this can only be called on a specific slot when truncation is not
201 happening on that slot, i.e. the undo tablespace is in use. */
202 ut_ad(undo::space_id_bank[slot].in_use);
203
204 return (undo::space_id_bank[slot].space_id);
205 }
206
207 /* clang-format off */
208 /** Convert an undo space ID into an undo space number.
209 NOTE: This may be an undo space_id from a pre-exisiting 5.7
210 database which used space_ids from 1 to 127. If so, the
211 space_id is the space_num.
212 The space_ids are assigned to number ranges in reverse from high to low.
213 In addition, the first space IDs for each undo number occur sequentially
214 and descending before the second space_id.
215
216 Since s_max_undo_space_id = 0xFFFFFFEF, FSP_MAX_UNDO_TABLESPACES = 127
217 and s_undo_space_id_range = 400,000:
218 Space ID Space Num Space ID Space Num ... Space ID Space Num
219 0xFFFFFFEF 1 0xFFFFFFEe 2 ... 0xFFFFFF71 127
220 0xFFFFFF70 1 0xFFFFFF6F 2 ... 0xFFFFFEF2 127
221 0xFFFFFEF1 1 0xFFFFFEF0 2 ... 0xFFFFFE73 127
222 ...
223
224 This is done to maintain backward compatibility to when there was only one
225 space_id per undo space number.
226 @param[in] space_id undo tablespace ID
227 @return space number of the undo tablespace */
228 /* clang-format on */
229 inline space_id_t id2num(space_id_t space_id) {
230 if (!is_reserved(space_id)) {
231 return (space_id);
232 }
233
234 return (((dict_sys_t::s_max_undo_space_id - space_id) %
235 FSP_MAX_UNDO_TABLESPACES) +
236 1);
237 }
238
239 /* Given a reserved undo space_id, return the next space_id for the associated
240 undo space number. */
241 inline space_id_t id2next_id(space_id_t space_id) {
242 ut_ad(is_reserved(space_id));
243
244 space_id_t space_num = id2num(space_id);
245 space_id_t first_id = dict_sys_t::s_max_undo_space_id + 1 - space_num;
246 space_id_t last_id = first_id - (FSP_MAX_UNDO_TABLESPACES *
247 (dict_sys_t::s_undo_space_id_range - 1));
248
249 return (space_id == SPACE_UNKNOWN || space_id == last_id
250 ? first_id
251 : space_id - FSP_MAX_UNDO_TABLESPACES);
252 }
253
254 /** Initialize the undo tablespace space_id bank which is a lock free
255 repository for information about the space IDs used for undo tablespaces.
256 It is used during creation in order to assign an unused space number and
257 during truncation in order to assign the next space_id within that
258 space_number range. */
259 void init_space_id_bank();
260
261 /** Note that the undo space number for a space ID is being used.
262 Put that space_id into the space_id_bank.
263 @param[in] space_id undo tablespace number */
264 void use_space_id(space_id_t space_id);
265
266 /** Mark that the given undo space number is being used and
267 return the next available space_id for that space number.
268 @param[in] space_num undo tablespace number
269 @return the next tablespace ID to use */
270 space_id_t use_next_space_id(space_id_t space_num);
271
272 /** Mark an undo number associated with a given space_id as unused and
273 available to be reused. This happens when the fil_space_t is closed
274 associated with a drop undo tablespace.
275 @param[in] space_id Undo Tablespace ID */
276 void unuse_space_id(space_id_t space_id);
277
278 /** Given a valid undo space_id or SPACE_UNKNOWN, return the next space_id
279 for the given space number.
280 @param[in] space_id undo tablespace ID
281 @param[in] space_num undo tablespace number
282 @return the next tablespace ID to use */
283 space_id_t next_space_id(space_id_t space_id, space_id_t space_num);
284
285 /** Given a valid undo space_id, return the next space_id for that
286 space number.
287 @param[in] space_id undo tablespace ID
288 @return the next tablespace ID to use */
289 space_id_t next_space_id(space_id_t space_id);
290
291 /** Return the next available undo space ID to be used for a new explicit
292 undo tablespaces. The slot will be marked as in-use.
293 @return next available undo space number if successful.
294 @return SPACE_UNKNOWN if failed */
295 space_id_t get_next_available_space_num();
296
297 /** Build a standard undo tablespace name from a space_id.
298 @param[in] space_id id of the undo tablespace.
299 @return tablespace name of the undo tablespace file */
300 char *make_space_name(space_id_t space_id);
301
302 /** Build a standard undo tablespace file name from a space_id.
303 This will create a name like 'undo_001' if the space_id is in the
304 reserved range, else it will be like 'undo001'.
305 @param[in] space_id id of the undo tablespace.
306 @return file_name of the undo tablespace file */
307 char *make_file_name(space_id_t space_id);
308
309 /** An undo::Tablespace object is used to easily convert between
310 undo_space_id and undo_space_num and to create the automatic file_name
311 and space name. In addition, it is used in undo::Tablespaces to track
312 the trx_rseg_t objects in an Rsegs vector. So we do not allocate the
313 Rsegs vector for each object, only when requested by the constructor. */
314 struct Tablespace {
315 /** Constructor
316 @param[in] id tablespace id */
317 explicit Tablespace(space_id_t id)
318 : m_id(id),
319 m_num(undo::id2num(id)),
320 m_implicit(true),
321 m_new(false),
322 m_space_name(),
323 m_file_name(),
324 m_log_file_name(),
325 m_log_file_name_old(),
326 m_rsegs() {}
327
328 /** Copy Constructor
329 @param[in] other undo tablespace to copy */
330 Tablespace(Tablespace &other)
331 : m_id(other.id()),
332 m_num(undo::id2num(other.id())),
333 m_implicit(other.is_implicit()),
334 m_new(other.is_new()),
335 m_space_name(),
336 m_file_name(),
337 m_log_file_name(),
338 m_log_file_name_old(),
339 m_rsegs() {
340 ut_ad(m_id == 0 || is_reserved(m_id));
341
342 set_space_name(other.space_name());
343 set_file_name(other.file_name());
344
345 /* When the copy constructor is used, add an Rsegs
346 vector. This constructor is only used in the global
347 undo::Tablespaces object where rollback segments are
348 tracked. */
349 m_rsegs = ut::new_withkey<Rsegs>(UT_NEW_THIS_FILE_PSI_KEY);
350 }
351
352 /** Destructor */
353 ~Tablespace() {
354 if (m_space_name != nullptr) {
355 ut::free(m_space_name);
356 m_space_name = nullptr;
357 }
358
359 if (m_file_name != nullptr) {
360 ut::free(m_file_name);
361 m_file_name = nullptr;
362 }
363
364 if (m_log_file_name != nullptr) {
365 ut::free(m_log_file_name);
366 m_log_file_name = nullptr;
367 }
368
369 if (m_log_file_name_old != nullptr) {
370 ut::free(m_log_file_name_old);
371 m_log_file_name_old = nullptr;
372 }
373
374 /* Clear the cached rollback segments. */
375 if (m_rsegs != nullptr) {
376 ut::delete_(m_rsegs);
377 m_rsegs = nullptr;
378 }
379 }
380
381 /* Determine if this undo space needs to be truncated.
382 @return true if it should be truncated, false if not. */
383 bool needs_truncation();
384
385 /** Change the space_id from its current value.
386 @param[in] space_id The new undo tablespace ID */
387 void set_space_id(space_id_t space_id);
388
389 /** Replace the standard undo space name if it exists with a copy
390 of the undo tablespace name provided.
391 @param[in] new_space_name non-standard undo space name */
392 void set_space_name(const char *new_space_name);
393
394 /** Get the undo tablespace name. Make it if not yet made.
395 NOTE: This is only called from stack objects so there is no
396 race condition. If it is ever called from a shared object
397 like undo::spaces, then it must be protected by the caller.
398 @return tablespace name created from the space_id */
399 char *space_name() {
400 if (m_space_name == nullptr) {
401 #ifndef UNIV_HOTBACKUP
402 m_space_name = make_space_name(m_id);
403 #endif /* !UNIV_HOTBACKUP */
404 }
405
406 return (m_space_name);
407 }
408
409 /** Replace the standard undo file name if it exists with a copy
410 of the file name provided. This name can come in three forms:
411 absolute path, relative path, and basename. Undo ADD DATAFILE
412 does not accept a relative path. So if that comes in here, it
413 was the scanned name and is relative to the datadir.
414 If this is just a basename, add it to srv_undo_dir.
415 @param[in] file_name explicit undo file name */
416 void set_file_name(const char *file_name);
417
418 /** Get the undo space filename. Make it if not yet made.
419 NOTE: This is only called from stack objects so there is no
420 race condition. If it is ever called from a shared object
421 like undo::spaces, then it must be protected by the caller.
422 @return tablespace filename created from the space_id */
423 char *file_name() {
424 if (m_file_name == nullptr) {
425 m_file_name = make_file_name(m_id);
426 }
427
428 return (m_file_name);
429 }
430
431 /** Build a log file name based on space_id
432 @param[in] space_id id of the undo tablespace.
433 @param[in] location directory location of the file.
434 @return DB_SUCCESS or error code */
435 char *make_log_file_name(space_id_t space_id, const char *location);
436
437 /** Get the undo log filename. Make it if not yet made.
438 NOTE: This is only called from stack objects so there is no
439 race condition. If it is ever called from a shared object
440 like undo::spaces, then it must be protected by the caller.
441 @return tablespace filename created from the space_id */
442 char *log_file_name() {
443 if (m_log_file_name == nullptr) {
444 m_log_file_name = make_log_file_name(m_id, srv_undo_dir);
445 }
446
447 return (m_log_file_name);
448 }
449
450 /** Get the old undo log filename from the srv_log_group_home_dir.
451 Make it if not yet made. */
452 char *log_file_name_old() {
453 if (m_log_file_name_old == nullptr) {
454 m_log_file_name_old = make_log_file_name(m_id, srv_log_group_home_dir);
455 }
456
457 return (m_log_file_name_old);
458 }
459
460 /** Get the undo tablespace ID.
461 @return tablespace ID */
462 space_id_t id() { return (m_id); }
463
464 /** Get the undo tablespace number. This is the same as m_id
465 if m_id is 0 or this is a v5.6-5.7 undo tablespace. v8+ undo
466 tablespaces use a space_id from the reserved range.
467 @return undo tablespace number */
468 space_id_t num() {
469 ut_ad(m_num < FSP_MAX_ROLLBACK_SEGMENTS);
470
471 return (m_num);
472 }
473
474 /** Get a reference to the List of rollback segments within
475 this undo tablespace.
476 @return a reference to the Rsegs vector. */
477 Rsegs *rsegs() { return (m_rsegs); }
478
479 /** Report whether this undo tablespace was explicitly created
480 by an SQL statement.
481 @return true if the tablespace was created explicitly. */
482 bool is_explicit() { return (!m_implicit); }
483
484 /** Report whether this undo tablespace was implicitly created.
485 @return true if the tablespace was created implicitly. */
486 bool is_implicit() { return (m_implicit); }
487
488 /** Report whether this undo tablespace was created at startup.
489 @retval true if created at startup.
490 @retval false if pre-existed at startup. */
491 bool is_new() { return (m_new); }
492
493 /** Note that this undo tablespace is being created. */
494 void set_new() { m_new = true; }
495
496 /** Return whether the undo tablespace is active.
497 @return true if active */
498 bool is_active() {
499 if (m_rsegs == nullptr) {
500 return (false);
501 }
502 m_rsegs->s_lock();
503 bool ret = m_rsegs->is_active();
504 m_rsegs->s_unlock();
505 return (ret);
506 }
507
508 /** Return whether the undo tablespace is active. For optimization purposes,
509 do not take a latch.
510 @return true if active */
511 bool is_active_no_latch() {
512 if (m_rsegs == nullptr) {
513 return (false);
514 }
515 return (m_rsegs->is_active());
516 }
517
518 /** Return the rseg at the requested rseg slot if the undo space is active.
519 @param[in] slot The slot of the rseg. 1 to 127
520 @return Rseg pointer of nullptr if the space is not active. */
521 trx_rseg_t *get_active(ulint slot) {
522 m_rsegs->s_lock();
523 if (!m_rsegs->is_active()) {
524 m_rsegs->s_unlock();
525 return (nullptr);
526 }
527
528 /* Mark the chosen rseg so that it will not be selected
529 for UNDO truncation. */
530 trx_rseg_t *rseg = m_rsegs->at(slot);
531 rseg->trx_ref_count++;
532
533 m_rsegs->s_unlock();
534
535 return (rseg);
536 }
537
538 /** Return whether the undo tablespace is inactive due to
539 implicit selection by the purge thread.
540 @return true if marked for truncation by the purge thread */
541 bool is_inactive_implicit() {
542 if (m_rsegs == nullptr) {
543 return (false);
544 }
545 m_rsegs->s_lock();
546 bool ret = m_rsegs->is_inactive_implicit();
547 m_rsegs->s_unlock();
548 return (ret);
549 }
550
551 /** Return whether the undo tablespace was made inactive by
552 ALTER TABLESPACE.
553 @return true if altered inactive */
554 bool is_inactive_explicit() {
555 if (m_rsegs == nullptr) {
556 return (false);
557 }
558 m_rsegs->s_lock();
559 bool ret = m_rsegs->is_inactive_explicit();
560 m_rsegs->s_unlock();
561 return (ret);
562 }
563
564 /** Return whether the undo tablespace is empty and ready
565 to be dropped.
566 @return true if empty */
567 bool is_empty() {
568 if (m_rsegs == nullptr) {
569 return (true);
570 }
571 m_rsegs->s_lock();
572 bool ret = m_rsegs->is_empty();
573 m_rsegs->s_unlock();
574 return (ret);
575 }
576
577 /** Set the undo tablespace active for use by transactions. */
578 void set_active() {
579 m_rsegs->x_lock();
580 m_rsegs->set_active();
581 m_rsegs->x_unlock();
582 }
583
584 /** Set the state of the rollback segments in this undo tablespace to
585 inactive_implicit if currently active. If the state is inactive_explicit,
586 leave as is. Then put the space_id into the callers marked_space_id.
587 This is done when marking a space for truncate. It will not be used
588 for new transactions until it becomes active again. */
589 void set_inactive_implicit(space_id_t *marked_space_id) {
590 m_rsegs->x_lock();
591 if (m_rsegs->is_active()) {
592 m_rsegs->set_inactive_implicit();
593 }
594 *marked_space_id = m_id;
595
596 m_rsegs->x_unlock();
597 }
598
599 /** Make the undo tablespace inactive so that it will not be
600 used for new transactions. The purge thread will clear out
601 all the undo logs, truncate it, and then mark it empty. */
602 void set_inactive_explicit() {
603 m_rsegs->x_lock();
604 m_rsegs->set_inactive_explicit();
605 m_rsegs->x_unlock();
606 }
607
608 /** Make the undo tablespace active again so that it will
609 be used for new transactions.
610 If current State is ___ then do:
611 empty: Set active.
612 active_implicit: Ignore. It was not altered inactive. When it is done
613 being truncated it will go back to active.
614 active_explicit: Depends if it is marked for truncation.
615 marked: Set to inactive_implicit. the next state will be active.
616 not yet: Set to active so that it does not get truncated. */
617 void alter_active();
618
619 /** Set the state of the undo tablespace to empty so that it
620 can be dropped. */
621 void set_empty() {
622 m_rsegs->x_lock();
623 m_rsegs->set_empty();
624 m_rsegs->x_unlock();
625 }
626
627 private:
628 /** Undo Tablespace ID. */
629 space_id_t m_id;
630
631 /** Undo Tablespace number, from 1 to 127. This is the
632 7-bit number that is used in a rollback pointer.
633 Use id2num() to get this number from a space_id. */
634 space_id_t m_num;
635
636 /** True if this is an implicit undo tablespace */
637 bool m_implicit;
638
639 /** True if this undo tablespace was implicitly created when
640 this instance started up. False if it pre-existed. */
641 bool m_new;
642
643 /** The tablespace name, auto-generated when needed from
644 the space number. */
645 char *m_space_name;
646
647 /** The tablespace file name, auto-generated when needed
648 from the space number. */
649 char *m_file_name;
650
651 /** The truncation log file name, auto-generated when needed
652 from the space number and the srv_undo_dir. */
653 char *m_log_file_name;
654
655 /** The old truncation log file name, auto-generated when needed
656 from the space number and the srv_log_group_home_dir. */
657 char *m_log_file_name_old;
658
659 /** List of rollback segments within this tablespace.
660 This is not always used. Must call init_rsegs to use it. */
661 Rsegs *m_rsegs;
662 };
663
664 /** List of undo tablespaces, each containing a list of
665 rollback segments. */
666 class Tablespaces {
667 using Tablespaces_Vector =
668 std::vector<Tablespace *, ut::allocator<Tablespace *>>;
669
670 public:
671 Tablespaces() { init(); }
672
673 ~Tablespaces() { deinit(); }
674
675 /** Initialize */
676 void init();
677
678 /** De-initialize */
679 void deinit();
680
681 /** Clear the contents of the list of Tablespace objects.
682 This does not deallocate any memory. */
683 void clear() {
684 for (auto undo_space : m_spaces) {
685 ut::delete_(undo_space);
686 }
687 m_spaces.clear();
688 }
689
690 /** Get the number of tablespaces tracked by this object. */
691 ulint size() { return (m_spaces.size()); }
692
693 /** See if the list of tablespaces is empty. */
694 bool empty() { return (m_spaces.empty()); }
695
696 /** Get the Tablespace tracked at a position. */
697 Tablespace *at(size_t pos) { return (m_spaces.at(pos)); }
698
699 /** Add a new undo::Tablespace to the back of the vector.
700 The vector has been pre-allocated to 128 so read threads will
701 not loose what is pointed to. If tablespace_name and file_name
702 are standard names, they are optional.
703 @param[in] ref_undo_space undo tablespace */
704 void add(Tablespace &ref_undo_space);
705
706 /** Drop an existing explicit undo::Tablespace.
707 @param[in] undo_space pointer to undo space */
708 void drop(Tablespace *undo_space);
709
710 /** Drop an existing explicit undo::Tablespace.
711 @param[in] ref_undo_space reference to undo space */
712 void drop(Tablespace &ref_undo_space);
713
714 /** Check if the given space_id is in the vector.
715 @param[in] num undo tablespace number
716 @return true if space_id is found, else false */
717 bool contains(space_id_t num) { return (find(num) != nullptr); }
718
719 /** Find the given space_num in the vector.
720 @param[in] num undo tablespace number
721 @return pointer to an undo::Tablespace struct */
722 Tablespace *find(space_id_t num) {
723 if (m_spaces.empty()) {
724 return (nullptr);
725 }
726
727 /* The sort method above puts this vector in order by
728 Tablespace::num. If there are no gaps, then we should
729 be able to find it quickly. */
730 space_id_t slot = num - 1;
731 if (slot < m_spaces.size()) {
732 auto undo_space = m_spaces.at(slot);
733 if (undo_space->num() == num) {
734 return (undo_space);
735 }
736 }
737
738 /* If there are gaps in the numbering, do a search. */
739 for (auto undo_space : m_spaces) {
740 if (undo_space->num() == num) {
741 return (undo_space);
742 }
743 }
744
745 return (nullptr);
746 }
747
748 /** Find the first undo space that is marked inactive explicitly.
749 @param[in,out] num_active If there are no inactive_explicit spaces
750 found, this will contain the number of
751 active spaces found.
752 @return pointer to an undo::Tablespace struct */
753 Tablespace *find_first_inactive_explicit(size_t *num_active) {
754 ut_ad(own_latch());
755
756 if (m_spaces.empty()) {
757 return (nullptr);
758 }
759
760 for (auto undo_space : m_spaces) {
761 if (undo_space->is_inactive_explicit()) {
762 return (undo_space);
763 }
764
765 if (num_active != nullptr && undo_space->is_active()) {
766 (*num_active)++;
767 }
768 }
769
770 return (nullptr);
771 }
772
773 #ifdef UNIV_DEBUG
774 /** Determine if this thread owns a lock on m_latch. */
775 bool own_latch() {
776 return (rw_lock_own(m_latch, RW_LOCK_X) || rw_lock_own(m_latch, RW_LOCK_S));
777 }
778 #endif /* UNIV_DEBUG */
779
780 /** Get a shared lock on m_spaces. */
781 void s_lock() { rw_lock_s_lock(m_latch, UT_LOCATION_HERE); }
782
783 /** Release a shared lock on m_spaces. */
784 void s_unlock() { rw_lock_s_unlock(m_latch); }
785
786 /** Get an exclusive lock on m_spaces. */
787 void x_lock() { rw_lock_x_lock(m_latch, UT_LOCATION_HERE); }
788
789 /** Release an exclusive lock on m_spaces. */
790 void x_unlock() { rw_lock_x_unlock(m_latch); }
791
792 Tablespaces_Vector m_spaces;
793
794 private:
795 /** RW lock to protect m_spaces.
796 x for adding elements, s for scanning, size() etc. */
797 rw_lock_t *m_latch;
798 };
799
800 /** Mutex for serializing undo tablespace related DDL. These have to do with
801 creating and dropping undo tablespaces. */
802 extern ib_mutex_t ddl_mutex;
803
804 /** A global object that contains a vector of undo::Tablespace structs. */
805 extern Tablespaces *spaces;
806
807 #ifdef UNIV_DEBUG
808 /** Inject a crash if a certain SET GLOBAL DEBUG has been set.
809 Before DBUG_SUICIDE(), write an entry about this crash to the error log
810 and flush the redo log. */
811 void inject_crash(const char *injection_point_name);
812
813 /** Inject a failure in the undo truncation debug compiled code at various
814 places so that it fails the first time it hits and succeeds after that. */
815 class Inject_failure_once {
816 bool m_already_failed;
817 const char *m_inject_name;
818
819 public:
820 Inject_failure_once(const char *inject_name)
821 : m_already_failed{false}, m_inject_name{inject_name} {}
822
823 /** If a certain SET GLOBAL DEBUG has been set and this is the first time
824 this has been called for that injection point, write an entry to the
825 error log and return true so that the caller can cause the failure.
826 @return true iff compiled with debug and the debug point has been set
827 and this it the first call for this debug point. */
828 bool should_fail();
829 };
830
831 #endif /* UNIV_DEBUG */
832
833 /** Create the truncate log file. Needed to track the state of truncate during
834 a crash. An auxiliary redo log file undo_<space_id>_trunc.log will be created
835 while the truncate of the UNDO is in progress. This file is required during
836 recovery to complete the truncate.
837 @param[in] undo_space undo tablespace to truncate.
838 @return DB_SUCCESS or error code.*/
839 dberr_t start_logging(Tablespace *undo_space);
840
841 /** Mark completion of undo truncate action by writing magic number
842 to the log file and then removing it from the disk.
843 If we are going to remove it from disk then why write magic number?
844 This is to safeguard from unlink (file-system) anomalies that will
845 keep the link to the file even after unlink action is successful
846 and ref-count = 0.
847 @param[in] space_num number of the undo tablespace to truncate. */
848 void done_logging(space_id_t space_num);
849
850 /** Check if TRUNCATE_DDL_LOG file exist.
851 @param[in] space_num undo tablespace number
852 @return true if exist else false. */
853 bool is_active_truncate_log_present(space_id_t space_num);
854
855 /** list of undo tablespaces that need header pages and rollback
856 segments written to them at startup. This can be because they are
857 newly initialized, were being truncated and the system crashed, or
858 they were an old format at startup and were replaced when they were
859 opened. Old format undo tablespaces do not have space_ids between
860 dict_sys_t::s_min_undo_space_id and dict_sys_t::s_max_undo_space_id
861 and they do not contain an RSEG_ARRAY page. */
862 extern Space_Ids s_under_construction;
863
864 /** Add undo tablespace to s_under_construction vector.
865 @param[in] space_id space id of tablespace to
866 truncate */
867 void add_space_to_construction_list(space_id_t space_id);
868
869 /** Clear the s_under_construction vector. */
870 void clear_construction_list();
871
872 /** Is an undo tablespace under construction at the moment.
873 @param[in] space_id space id to check
874 @return true if marked for truncate, else false. */
875 bool is_under_construction(space_id_t space_id);
876
877 /** Set an undo tablespace active. */
878 void set_active(space_id_t space_id);
879
880 /* Return whether the undo tablespace is active. If this is a
881 non-undo tablespace, then it will not be found in spaces and it
882 will not be under construction, so this function will return true.
883 @param[in] space_id Undo Tablespace ID
884 @param[in] get_latch Specifies whether the rsegs->s_lock() is needed.
885 @return true if active (non-undo spaces are always active) */
886 bool is_active(space_id_t space_id, bool get_latch = true);
887
888 constexpr ulint TRUNCATE_FREQUENCY = 128;
889
890 /** Track an UNDO tablespace marked for truncate. */
891 class Truncate {
892 public:
893 /** Constructor. */
894 Truncate() : m_space_id_marked(SPACE_UNKNOWN), m_timer() {}
895
896 /** Destructor. */
897 ~Truncate() = default;
898
899 /** Is tablespace selected for truncate.
900 @return true if undo tablespace is marked for truncate */
901 bool is_marked() const { return (m_space_id_marked != SPACE_UNKNOWN); }
902
903 /** Mark the undo tablespace selected for truncate as empty
904 so that it will be truncated next. */
905 void set_marked_space_empty() { m_marked_space_is_empty = true; }
906
907 /** Is the tablespace selected for truncate empty of undo logs yet?
908 @return true if the marked undo tablespace has no more undo logs */
909 bool is_marked_space_empty() const { return (m_marked_space_is_empty); }
910
911 /** Mark the tablespace for truncate.
912 @param[in] undo_space undo tablespace to truncate. */
913 void mark(Tablespace *undo_space);
914
915 /** Get the ID of the tablespace marked for truncate.
916 @return tablespace ID marked for truncate. */
917 space_id_t get_marked_space_num() const {
918 return (id2num(m_space_id_marked));
919 }
920
921 /** Reset for next rseg truncate. */
922 void reset() {
923 reset_timer();
924 m_marked_space_is_empty = false;
925 m_space_id_marked = SPACE_UNKNOWN;
926 }
927
928 /** Get the undo tablespace number to start a scan.
929 Re-adjust in case the spaces::size() went down.
930 @return undo space_num to start scanning. */
931 space_id_t get_scan_space_num() const {
932 s_scan_pos = s_scan_pos % undo::spaces->size();
933
934 Tablespace *undo_space = undo::spaces->at(s_scan_pos);
935
936 return (undo_space->num());
937 }
938
939 /** Increment the scanning position in a round-robin fashion.
940 @return undo space_num at incremented scanning position. */
941 space_id_t increment_scan() const {
942 /** Round-robin way of selecting an undo tablespace for the truncate
943 operation. Once we reach the end of the list of known undo tablespace
944 IDs, move back to the first undo tablespace ID. This will scan active
945 as well as inactive undo tablespaces. */
946 s_scan_pos = (s_scan_pos + 1) % undo::spaces->size();
947
948 return (get_scan_space_num());
949 }
950
951 /** Check if the given space id is equal to the space ID that is marked for
952 truncation.
953 @return true if they are equal, false otherwise. */
954 bool is_equal(space_id_t space_id) const {
955 return (m_space_id_marked == space_id);
956 }
957
958 /** @return the number of milliseconds since last reset. */
959 int64_t check_timer() const { return (m_timer.elapsed()); }
960
961 /** Reset the timer. */
962 void reset_timer() { m_timer.reset(); }
963
964 private:
965 /** UNDO space ID that is marked for truncate. */
966 space_id_t m_space_id_marked;
967
968 /** This is true if the marked space is empty of undo logs and ready
969 to truncate. We leave the rsegs object 'inactive' until after it is
970 truncated and rebuilt. This allow the code to do the check for undo
971 logs only once. */
972 bool m_marked_space_is_empty;
973
974 /** Elapsed time since last truncate check. */
975 ib::Timer m_timer;
976
977 /** Start scanning for UNDO tablespace from this vector position. This is
978 to avoid bias selection of one tablespace always. */
979 static size_t s_scan_pos;
980
981 }; /* class Truncate */
982
983 } /* namespace undo */
984
985 /** The control structure used in the purge operation */
986 struct trx_purge_t {
987 /** System session running the purge query */
988 sess_t *sess;
989
990 /** System transaction running the purge query: this trx is not in the trx
991 list of the trx system and it never ends */
992 trx_t *trx;
993 #ifndef UNIV_HOTBACKUP
994 /** The latch protecting the purge view. A purge operation must acquire an
995 x-latch here for the instant at which it changes the purge view: an undo
996 log operation can prevent this by obtaining an s-latch here. It also
997 protects state and running */
998 rw_lock_t latch;
999 #endif /* !UNIV_HOTBACKUP */
1000
1001 /** State signal event */
1002 os_event_t event;
1003
1004 /** Counter to track number stops */
1005 ulint n_stop;
1006
1007 /** true, if purge is active, we check this without the latch too */
1008 volatile bool running;
1009
1010 /** Purge coordinator thread states, we check this in several places without
1011 holding the latch. */
1012 volatile purge_state_t state;
1013
1014 /** The query graph which will do the parallelized purge operation */
1015 que_t *query;
1016
1017 /** The purge will not remove undo logs which are >= this view (purge view) */
1018 ReadView view;
1019
1020 /** true if view is active */
1021 bool view_active;
1022
1023 /** Count of total tasks submitted to the task queue */
1024 volatile ulint n_submitted;
1025
1026 /** Count of total tasks completed */
1027 std::atomic<ulint> n_completed;
1028
1029 /* The following two fields form the 'purge pointer' which advances
1030 during a purge, and which is used in history list truncation */
1031
1032 /** Limit up to which we have read and parsed the UNDO log records. Not
1033 necessarily purged from the indexes. Note that this can never be less than
1034 the limit below, we check for this invariant in trx0purge.cc */
1035 purge_iter_t iter;
1036
1037 /** The 'purge pointer' which advances during a purge, and which is used in
1038 history list truncation */
1039 purge_iter_t limit;
1040 #ifdef UNIV_DEBUG
1041 /** Indicate 'purge pointer' which have purged already accurately. */
1042 purge_iter_t done;
1043 #endif /* UNIV_DEBUG */
1044
1045 /** true if the info of the next record to purge is stored below: if yes, then
1046 the transaction number and the undo number of the record are stored in
1047 purge_trx_no and purge_undo_no above */
1048 bool next_stored;
1049
1050 /** Rollback segment for the next undo record to purge */
1051 trx_rseg_t *rseg;
1052
1053 /** Page number for the next undo record to purge, page number of the log
1054 header, if dummy record */
1055 page_no_t page_no;
1056
1057 /** Page offset for the next undo record to purge, 0 if the dummy record */
1058 ulint offset;
1059
1060 /** Header page of the undo log where the next record to purge belongs */
1061 page_no_t hdr_page_no;
1062
1063 /** Header byte offset on the page */
1064 ulint hdr_offset;
1065
1066 /** Iterator to get the next rseg to process */
1067 TrxUndoRsegsIterator *rseg_iter;
1068
1069 /** Binary min-heap, ordered on TrxUndoRsegs::trx_no. It is protected
1070 by the pq_mutex */
1071 purge_pq_t *purge_queue;
1072
1073 /** Mutex protecting purge_queue */
1074 PQMutex pq_mutex;
1075
1076 /** Track UNDO tablespace marked for truncate. */
1077 undo::Truncate undo_trunc;
1078
1079 /** Heap for reading the undo log records */
1080 mem_heap_t *heap;
1081
1082 /** Is the this thread related to purge? This is false by default and set to
1083 true by srv_purge_coordinator_thread() and srv_worker_thread() only. */
1084 static inline thread_local bool is_this_a_purge_thread{false};
1085
1086 /** Set of all rseg queue. */
1087 std::vector<trx_rseg_t *> rsegs_queue;
1088 };
1089
1090 /** Choose the rollback segment with the smallest trx_no. */
1091 struct TrxUndoRsegsIterator {
1092 /** Constructor */
1093 TrxUndoRsegsIterator(trx_purge_t *purge_sys);
1094
1095 /** Sets the next rseg to purge in m_purge_sys.
1096 @return page size of the table for which the log is.
1097 NOTE: if rseg is NULL when this function returns this means that
1098 there are no rollback segments to purge and then the returned page
1099 size object should not be used. */
1100 const page_size_t set_next();
1101
1102 private:
1103 // Disable copying
1104 TrxUndoRsegsIterator(const TrxUndoRsegsIterator &);
1105 TrxUndoRsegsIterator &operator=(const TrxUndoRsegsIterator &);
1106
1107 /** The purge system pointer */
1108 trx_purge_t *m_purge_sys;
1109
1110 /** The current element to process */
1111 TrxUndoRsegs m_trx_undo_rsegs;
1112
1113 /** Track the current element in m_trx_undo_rseg */
1114 typename Rsegs_array<2>::iterator m_iter;
1115
1116 /** Sentinel value */
1117 static const TrxUndoRsegs NullElement;
1118 };
1119
1120 #include "trx0purge.ic"
1121
1122 #endif /* trx0purge_h */