1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2020, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 /** @file include/srv0start.h
28  Starts the Innobase database server
29 
30  Created 10/10/1995 Heikki Tuuri
31  *******************************************************/
32 
33 #ifndef srv0start_h
34 #define srv0start_h
35 
36 #include "log0types.h"
37 #include "os0thread-create.h"
38 #ifndef UNIV_HOTBACKUP
39 #include "sync0rw.h"
40 #endif /* !UNIV_HOTBACKUP */
41 #include "trx0purge.h"
42 #include "univ.i"
43 #include "ut0byte.h"
44 
45 // Forward declaration
46 struct dict_table_t;
47 
48 #ifndef UNIV_DEBUG
49 #define RECOVERY_CRASH(x) \
50   do {                    \
51   } while (0)
52 #else
53 #define RECOVERY_CRASH(x)                                  \
54   do {                                                     \
55     if (srv_force_recovery_crash == x) {                   \
56       flush_error_log_messages();                          \
57       fprintf(stderr, "innodb_force_recovery_crash=%lu\n", \
58               srv_force_recovery_crash);                   \
59       fflush(stderr);                                      \
60       _exit(3);                                            \
61     }                                                      \
62   } while (0)
63 #endif /* UNIV_DEBUG */
64 
65 /** If buffer pool is less than the size,
66 only one buffer pool instance is used. */
67 #define BUF_POOL_SIZE_THRESHOLD (1024 * 1024 * 1024)
68 
69 /** Frees the memory allocated by srv_parse_data_file_paths_and_sizes()
70  and srv_parse_log_group_home_dirs(). */
71 void srv_free_paths_and_sizes(void);
72 
73 /** Adds a slash or a backslash to the end of a string if it is missing
74  and the string is not empty.
75  @return string which has the separator if the string is not empty */
76 char *srv_add_path_separator_if_needed(
77     char *str); /*!< in: null-terminated character string */
78 #ifndef UNIV_HOTBACKUP
79 
80 /** Open an undo tablespace.
81 @param[in]  undo_space  Undo tablespace
82 @return DB_SUCCESS or error code */
83 dberr_t srv_undo_tablespace_open(undo::Tablespace &undo_space);
84 
85 /** Upgrade undo tablespaces by deleting the old undo tablespaces
86 referenced by the TRX_SYS page.
87 @return error code */
88 dberr_t srv_undo_tablespaces_upgrade();
89 
90 /** Start InnoDB.
91 @param[in]  create_new_db     Whether to create a new database
92 @param[in]  to_lsn            LSN to stop recovery at
93 @return DB_SUCCESS or error code */
94 dberr_t srv_start(bool create_new_db, lsn_t to_lsn);
95 
96 /** Fix up an undo tablespace if it was in the process of being truncated
97 when the server crashed. This is the second call and is done after the DD
98 is available so now we know the space_name, file_name and previous space_id.
99 @param[in]  space_name  undo tablespace name
100 @param[in]  file_name   undo tablespace file name
101 @param[in]  space_id    undo tablespace ID
102 @return error code */
103 dberr_t srv_undo_tablespace_fixup(const char *space_name, const char *file_name,
104                                   space_id_t space_id);
105 
106 /** On a restart, initialize the remaining InnoDB subsystems so that
107 any tables (including data dictionary tables) can be accessed. */
108 void srv_dict_recover_on_restart();
109 
110 /** Start up the InnoDB service threads which are independent of DDL recovery
111 @param[in]	bootstrap	True if this is in bootstrap */
112 void srv_start_threads(bool bootstrap);
113 
114 /** Start the remaining InnoDB service threads which must wait for
115 complete DD recovery(post the DDL recovery) */
116 void srv_start_threads_after_ddl_recovery();
117 
118 /** Shut down all InnoDB background tasks that may look up objects in
119 the data dictionary. */
120 void srv_pre_dd_shutdown();
121 
122 /** Shut down the InnoDB database. */
123 void srv_shutdown();
124 
125 /** Start purge threads. During upgrade we start
126 purge threads early to apply purge. */
127 void srv_start_purge_threads();
128 
129 /** If early redo/undo log encryption processing is done.
130 @return true if it's done. */
131 bool is_early_redo_undo_encryption_done();
132 
133 /** Copy the file path component of the physical file to parameter. It will
134  copy up to and including the terminating path separator.
135  @return number of bytes copied or ULINT_UNDEFINED if destination buffer
136          is smaller than the path to be copied. */
137 ulint srv_path_copy(char *dest,             /*!< out: destination buffer */
138                     ulint dest_len,         /*!< in: max bytes to copy */
139                     const char *basedir,    /*!< in: base directory */
140                     const char *table_name) /*!< in: source table name */
141     MY_ATTRIBUTE((warn_unused_result));
142 
143 /** Get the encryption-data filename from the table name for a
144 single-table tablespace.
145 @param[in]	table		table object
146 @param[out]	filename	filename
147 @param[in]	max_len		filename max length */
148 void srv_get_encryption_data_filename(dict_table_t *table, char *filename,
149                                       ulint max_len);
150 #endif /* !UNIV_HOTBACKUP */
151 
152 /** true if the server is being started */
153 extern bool srv_is_being_started;
154 /** true if SYS_TABLESPACES is available for lookups */
155 extern bool srv_sys_tablespaces_open;
156 /** true if the server is being started, before rolling back any
157 incomplete transactions */
158 extern bool srv_startup_is_before_trx_rollback_phase;
159 
160 /** TRUE if a raw partition is in use */
161 extern ibool srv_start_raw_disk_in_use;
162 
163 /** Shutdown state */
164 enum srv_shutdown_t {
165   /** Database running normally. */
166   SRV_SHUTDOWN_NONE = 0,
167 
168   /** Shutdown has started. Stopping the thread responsible for rollback of
169   recovered transactions. In case of slow shutdown, this implies waiting
170   for completed rollback of all recovered transactions.
171   @remarks Note that user transactions are stopped earlier, when the
172   shutdown state is still equal to SRV_SHUTDOWN_NONE (user transactions
173   are closed when related connections are closed in close_connections()). */
174   SRV_SHUTDOWN_RECOVERY_ROLLBACK,
175 
176   /** Stopping threads that might use system transactions or DD objects.
177   This is important because we need to ensure that in the next phase no
178   undo records could be produced (we will be stopping purge threads).
179   After next phase DD is shut down, so also no accesses to DD objects
180   are allowed then. List of threads being stopped within this phase:
181     - dict_stats thread,
182     - fts_optimize thread,
183     - ts_alter_encrypt thread.
184   The master thread exits its main loop and finishes its first phase
185   of shutdown (in which it was allowed to touch DD objects). */
186   SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS,
187 
188   /** Stopping the purge threads. Before we enter this phase, we have
189   the guarantee that no new undo records could be produced. */
190   SRV_SHUTDOWN_PURGE,
191 
192   /** Shutting down the DD. */
193   SRV_SHUTDOWN_DD,
194 
195   /** Stopping remaining InnoDB background threads except:
196     - the master thread,
197     - redo log threads,
198     - page cleaner threads,
199     - archiver threads.
200   List of threads being stopped within this phase:
201     - lock_wait_timeout thread,
202     - error_monitor thread,
203     - monitor thread,
204     - buf_dump thread,
205     - buf_resize thread.
206   @remarks If your thread might touch DD objects or use system transactions
207   it must be stopped within SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS phase.
208 */
209   SRV_SHUTDOWN_CLEANUP,
210 
211   /** Stopping the master thread. */
212   SRV_SHUTDOWN_MASTER_STOP,
213 
214   /** Once we enter this phase, the page cleaners can clean up the buffer pool
215   and exit. The redo log threads write and flush the log buffer and exit after
216   the page cleaners (and within this phase). */
217   SRV_SHUTDOWN_FLUSH_PHASE,
218 
219   /** Last phase after ensuring that all data have been flushed to disk and
220   the flushed_lsn has been updated in the header of system tablespace.
221   During this phase we close all files and ensure archiver has archived all. */
222   SRV_SHUTDOWN_LAST_PHASE,
223 
224   /** Exit all threads and free resources. We might reach this phase in one
225   of two different ways:
226     - after visiting all previous states (usual shutdown),
227     - or during startup when we failed and we abort the startup. */
228   SRV_SHUTDOWN_EXIT_THREADS
229 };
230 
231 /** At a shutdown this value climbs from SRV_SHUTDOWN_NONE
232 to SRV_SHUTDOWN_EXIT_THREADS. */
233 extern std::atomic<enum srv_shutdown_t> srv_shutdown_state;
234 
235 /** Call std::quick_exit(3) */
236 void srv_fatal_error() MY_ATTRIBUTE((noreturn));
237 
238 /** Attempt to shutdown all background threads created by InnoDB.
239 NOTE: Does not guarantee they are actually shut down, only does
240 the best effort. Changes state of shutdown to SHUTDOWN_EXIT_THREADS,
241 wakes up the background threads and waits a little bit. It might be
242 used within startup phase or when fatal error is discovered during
243 some IO operation. Therefore you must not assume anything related
244 to the state in which it might be used. */
245 void srv_shutdown_exit_threads();
246 
247 /** Checks if all recovered transactions are supposed to be rolled back
248 before shutdown is ended.
249 @return value of the check */
250 bool srv_shutdown_waits_for_rollback_of_recovered_transactions();
251 
252 /** Allows to safely check value of the current shutdown state.
253 Note that the current shutdown state might be changed while the
254 check is being executed, but the check is based on a single load
255 of the srv_shutdown_state (atomic global variable). */
256 template <typename F>
srv_shutdown_state_matches(F && f)257 bool srv_shutdown_state_matches(F &&f) {
258   const auto state = srv_shutdown_state.load();
259   return std::forward<F>(f)(state);
260 }
261 
262 /** Open the configured number of undo tablespaces.
263 @param[in]  create_new_db true if new db being created
264 @param[in]  true disables reading the system tablespace (used in XtraBackup),
265             false is passed on recovery.
266 @return DB_SUCCESS or error code */
267 dberr_t srv_undo_tablespaces_init(bool create_new_db, bool backup_mode);
268 
269 /** Free the resources occupied by undo::spaces and trx_sys_undo_spaces,
270 called once during thread de-initialization. */
271 void undo_spaces_deinit();
272 
273 #endif
274