| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 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/srv0shutdown.h |
| 29 |
Shutdowns the Innobase database server |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#ifndef srv0shutdown_h |
| 34 |
#define srv0shutdown_h |
| 35 |
|
| 36 |
#include "my_compiler.h" |
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
/** Shut down all InnoDB background tasks that may look up objects in |
| 40 |
the data dictionary. */ |
| 41 |
void srv_pre_dd_shutdown(); |
| 42 |
|
| 43 |
/** Shut down the InnoDB database. */ |
| 44 |
void srv_shutdown(); |
| 45 |
|
| 46 |
/** Shutdown state */ |
| 47 |
enum srv_shutdown_t { |
| 48 |
/** Database running normally. */ |
| 49 |
SRV_SHUTDOWN_NONE = 0, |
| 50 |
|
| 51 |
/** Shutdown has started. Stopping the thread responsible for rollback of |
| 52 |
recovered transactions. In case of slow shutdown, this implies waiting |
| 53 |
for completed rollback of all recovered transactions. |
| 54 |
@remarks Note that user transactions are stopped earlier, when the |
| 55 |
shutdown state is still equal to SRV_SHUTDOWN_NONE (user transactions |
| 56 |
are closed when related connections are closed in close_connections()). */ |
| 57 |
SRV_SHUTDOWN_RECOVERY_ROLLBACK, |
| 58 |
|
| 59 |
/** Stopping threads that might use system transactions or DD objects. |
| 60 |
This is important because we need to ensure that in the next phase no |
| 61 |
undo records could be produced (we will be stopping purge threads). |
| 62 |
After next phase DD is shut down, so also no accesses to DD objects |
| 63 |
are allowed then. List of threads being stopped within this phase: |
| 64 |
- dict_stats thread, |
| 65 |
- fts_optimize thread, |
| 66 |
- ts_alter_encrypt thread. |
| 67 |
The master thread exits its main loop and finishes its first phase |
| 68 |
of shutdown (in which it was allowed to touch DD objects). */ |
| 69 |
SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS, |
| 70 |
|
| 71 |
/** Stopping the purge threads. Before we enter this phase, we have |
| 72 |
the guarantee that no new undo records could be produced. */ |
| 73 |
SRV_SHUTDOWN_PURGE, |
| 74 |
|
| 75 |
/** Shutting down the DD. */ |
| 76 |
SRV_SHUTDOWN_DD, |
| 77 |
|
| 78 |
/** Stopping remaining InnoDB background threads except: |
| 79 |
- the master thread, |
| 80 |
- redo log threads, |
| 81 |
- page cleaner threads, |
| 82 |
- archiver threads. |
| 83 |
List of threads being stopped within this phase: |
| 84 |
- lock_wait_timeout thread, |
| 85 |
- error_monitor thread, |
| 86 |
- monitor thread, |
| 87 |
- buf_dump thread, |
| 88 |
- buf_resize thread. |
| 89 |
@remarks If your thread might touch DD objects or use system transactions |
| 90 |
it must be stopped within SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS phase. |
| 91 |
*/ |
| 92 |
SRV_SHUTDOWN_CLEANUP, |
| 93 |
|
| 94 |
/** Stopping the master thread. */ |
| 95 |
SRV_SHUTDOWN_MASTER_STOP, |
| 96 |
|
| 97 |
/** Once we enter this phase, the page cleaners can clean up the buffer pool |
| 98 |
and exit. The redo log threads write and flush the log buffer and exit after |
| 99 |
the page cleaners (and within this phase). */ |
| 100 |
SRV_SHUTDOWN_FLUSH_PHASE, |
| 101 |
|
| 102 |
/** Last phase after ensuring that all data have been flushed to disk and |
| 103 |
the flushed_lsn has been updated in the header of system tablespace. |
| 104 |
During this phase we close all files and ensure archiver has archived all. */ |
| 105 |
SRV_SHUTDOWN_LAST_PHASE, |
| 106 |
|
| 107 |
/** Exit all threads and free resources. We might reach this phase in one |
| 108 |
of two different ways: |
| 109 |
- after visiting all previous states (usual shutdown), |
| 110 |
- or during startup when we failed and we abort the startup. */ |
| 111 |
SRV_SHUTDOWN_EXIT_THREADS |
| 112 |
}; |
| 113 |
|
| 114 |
/** At a shutdown this value climbs from SRV_SHUTDOWN_NONE |
| 115 |
to SRV_SHUTDOWN_EXIT_THREADS. */ |
| 116 |
extern std::atomic<enum srv_shutdown_t> srv_shutdown_state; |
| 117 |
|
| 118 |
/** Call std::quick_exit(3) */ |
| 119 |
[[noreturn]] void srv_fatal_error(); |
| 120 |
|
| 121 |
/** Attempt to shutdown all background threads created by InnoDB. |
| 122 |
NOTE: Does not guarantee they are actually shut down, only does |
| 123 |
the best effort. Changes state of shutdown to SHUTDOWN_EXIT_THREADS, |
| 124 |
wakes up the background threads and waits a little bit. It might be |
| 125 |
used within startup phase or when fatal error is discovered during |
| 126 |
some IO operation. Therefore you must not assume anything related |
| 127 |
to the state in which it might be used. */ |
| 128 |
void srv_shutdown_exit_threads(); |
| 129 |
|
| 130 |
/** Checks if all recovered transactions are supposed to be rolled back |
| 131 |
before shutdown is ended. |
| 132 |
@return value of the check */ |
| 133 |
bool srv_shutdown_waits_for_rollback_of_recovered_transactions(); |
| 134 |
|
| 135 |
/** Allows to safely check value of the current shutdown state. |
| 136 |
Note that the current shutdown state might be changed while the |
| 137 |
check is being executed, but the check is based on a single load |
| 138 |
of the srv_shutdown_state (atomic global variable). */ |
| 139 |
template <typename F> |
| 140 |
bool srv_shutdown_state_matches(F &&f) { |
| 141 |
const auto state = srv_shutdown_state.load(); |
| 142 |
return std::forward<F>(f)(state); |
| 143 |
} |
| 144 |
|
| 145 |
#endif |