| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
Copyright (c) 2009, Percona Inc. |
| 6 |
|
| 7 |
Portions of this file contain modifications contributed and copyrighted by |
| 8 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 9 |
briefly in the InnoDB documentation. The contributions by Google are |
| 10 |
incorporated with their permission, and subject to the conditions contained in |
| 11 |
the file COPYING.Google. |
| 12 |
|
| 13 |
Portions of this file contain modifications contributed and copyrighted |
| 14 |
by Percona Inc.. Those modifications are |
| 15 |
gratefully acknowledged and are described briefly in the InnoDB |
| 16 |
documentation. The contributions by Percona Inc. are incorporated with |
| 17 |
their permission, and subject to the conditions contained in the file |
| 18 |
COPYING.Percona. |
| 19 |
|
| 20 |
This program is free software; you can redistribute it and/or modify it under |
| 21 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 22 |
Free Software Foundation. |
| 23 |
|
| 24 |
This program is designed to work with certain software (including |
| 25 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 26 |
as designated in a particular file or component or in included license |
| 27 |
documentation. The authors of MySQL hereby grant you an additional |
| 28 |
permission to link the program and your derivative works with the |
| 29 |
separately licensed software that they have either included with |
| 30 |
the program or referenced in the documentation. |
| 31 |
|
| 32 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 33 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 34 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 35 |
for more details. |
| 36 |
|
| 37 |
You should have received a copy of the GNU General Public License along with |
| 38 |
this program; if not, write to the Free Software Foundation, Inc., |
| 39 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 40 |
|
| 41 |
*****************************************************************************/ |
| 42 |
|
| 43 |
/** @file srv/srv0start.cc |
| 44 |
Starts the InnoDB database server |
| 45 |
|
| 46 |
Created 2/16/1996 Heikki Tuuri |
| 47 |
*************************************************************************/ |
| 48 |
|
| 49 |
#include <errno.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#include <sys/types.h> |
| 53 |
#include <zlib.h> |
| 54 |
|
| 55 |
#include "my_dbug.h" |
| 56 |
|
| 57 |
#include "btr0btr.h" |
| 58 |
#include "btr0cur.h" |
| 59 |
#include "buf0buf.h" |
| 60 |
#include "buf0dump.h" |
| 61 |
#include "current_thd.h" |
| 62 |
#include "data0data.h" |
| 63 |
#include "data0type.h" |
| 64 |
#include "dict0dd.h" |
| 65 |
#include "dict0dict.h" |
| 66 |
#include "fil0fil.h" |
| 67 |
#include "fsp0fsp.h" |
| 68 |
#include "fsp0sysspace.h" |
| 69 |
#include "ha_prototypes.h" |
| 70 |
#include "ibuf0ibuf.h" |
| 71 |
#include "log0buf.h" |
| 72 |
#include "log0chkp.h" |
| 73 |
#include "log0recv.h" |
| 74 |
#include "log0write.h" |
| 75 |
#include "mem0mem.h" |
| 76 |
#include "mtr0mtr.h" |
| 77 |
|
| 78 |
#include "my_dbug.h" |
| 79 |
#include "my_psi_config.h" |
| 80 |
#include "mysql/psi/mysql_stage.h" |
| 81 |
#include "mysqld.h" |
| 82 |
|
| 83 |
#include "ddl0fts.h" |
| 84 |
#include "os0file.h" |
| 85 |
#include "os0thread-create.h" |
| 86 |
#include "os0thread.h" |
| 87 |
#include "page0cur.h" |
| 88 |
#include "page0page.h" |
| 89 |
#include "rem0rec.h" |
| 90 |
#include "srv0srv.h" |
| 91 |
#include "srv0start.h" |
| 92 |
#include "trx0sys.h" |
| 93 |
#include "trx0trx.h" |
| 94 |
#include "ut0mem.h" |
| 95 |
|
| 96 |
#include <zlib.h> |
| 97 |
|
| 98 |
#include "arch0arch.h" |
| 99 |
#include "arch0recv.h" |
| 100 |
#include "btr0pcur.h" |
| 101 |
#include "btr0sea.h" |
| 102 |
#include "buf0flu.h" |
| 103 |
#include "buf0rea.h" |
| 104 |
#include "clone0api.h" |
| 105 |
#include "clone0clone.h" |
| 106 |
#include "dict0boot.h" |
| 107 |
#include "dict0crea.h" |
| 108 |
#include "dict0load.h" |
| 109 |
#include "dict0stats_bg.h" |
| 110 |
#include "lock0lock.h" |
| 111 |
#include "os0event.h" |
| 112 |
#include "os0proc.h" |
| 113 |
#include "pars0pars.h" |
| 114 |
#include "que0que.h" |
| 115 |
#include "rem0cmp.h" |
| 116 |
#include "row0ins.h" |
| 117 |
#include "row0mysql.h" |
| 118 |
#include "row0row.h" |
| 119 |
#include "row0sel.h" |
| 120 |
#include "row0upd.h" |
| 121 |
#include "srv0tmp.h" |
| 122 |
#include "trx0purge.h" |
| 123 |
#include "trx0roll.h" |
| 124 |
#include "trx0rseg.h" |
| 125 |
#include "usr0sess.h" |
| 126 |
#include "ut0crc32.h" |
| 127 |
#include "ut0new.h" |
| 128 |
|
| 129 |
/** fil_space_t::flags for hard-coded tablespaces */ |
| 130 |
extern uint32_t predefined_flags; |
| 131 |
|
| 132 |
/** true if a raw partition is in use */ |
| 133 |
bool srv_start_raw_disk_in_use = false; |
| 134 |
|
| 135 |
/** true if the server is being started */ |
| 136 |
bool srv_is_being_started = false; |
| 137 |
/** true if SYS_TABLESPACES is available for lookups */ |
| 138 |
bool srv_sys_tablespaces_open = false; |
| 139 |
/** true if the server is being started, before rolling back any |
| 140 |
incomplete transactions */ |
| 141 |
bool srv_startup_is_before_trx_rollback_phase = false; |
| 142 |
/** true if srv_start() has been called */ |
| 143 |
static bool srv_start_has_been_called = false; |
| 144 |
|
| 145 |
/** Bit flags for tracking background thread creation. They are used to |
| 146 |
determine which threads need to be stopped if we need to abort during |
| 147 |
the initialisation step. */ |
| 148 |
enum srv_start_state_t { |
| 149 |
/** No thread started */ |
| 150 |
SRV_START_STATE_NONE = 0, |
| 151 |
/** Started IO threads */ |
| 152 |
SRV_START_STATE_IO = 1, |
| 153 |
/** Started purge thread(s) */ |
| 154 |
SRV_START_STATE_PURGE = 2, |
| 155 |
/** Started bufdump + dict stat and FTS optimize thread. */ |
| 156 |
SRV_START_STATE_STAT = 4 |
| 157 |
}; |
| 158 |
|
| 159 |
/** Track server thrd starting phases */ |
| 160 |
static uint64_t srv_start_state = SRV_START_STATE_NONE; |
| 161 |
|
| 162 |
std::atomic<enum srv_shutdown_t> srv_shutdown_state{SRV_SHUTDOWN_NONE}; |
| 163 |
|
| 164 |
/** Name of srv_monitor_file */ |
| 165 |
static char *srv_monitor_file_name; |
| 166 |
|
| 167 |
/** */ |
| 168 |
#define SRV_MAX_N_PENDING_SYNC_IOS 100 |
| 169 |
|
| 170 |
/* Keys to register InnoDB threads with performance schema */ |
| 171 |
#ifdef UNIV_PFS_THREAD |
| 172 |
mysql_pfs_key_t log_archiver_thread_key; |
| 173 |
mysql_pfs_key_t page_archiver_thread_key; |
| 174 |
mysql_pfs_key_t buf_pool_create_thread_key; |
| 175 |
mysql_pfs_key_t buf_dump_thread_key; |
| 176 |
mysql_pfs_key_t buf_resize_thread_key; |
| 177 |
mysql_pfs_key_t clone_ddl_thread_key; |
| 178 |
mysql_pfs_key_t clone_gtid_thread_key; |
| 179 |
mysql_pfs_key_t ddl_thread_key; |
| 180 |
mysql_pfs_key_t dict_stats_thread_key; |
| 181 |
mysql_pfs_key_t fts_optimize_thread_key; |
| 182 |
mysql_pfs_key_t fts_parallel_merge_thread_key; |
| 183 |
mysql_pfs_key_t fts_parallel_tokenization_thread_key; |
| 184 |
mysql_pfs_key_t srv_error_monitor_thread_key; |
| 185 |
mysql_pfs_key_t srv_lock_timeout_thread_key; |
| 186 |
mysql_pfs_key_t srv_master_thread_key; |
| 187 |
mysql_pfs_key_t srv_monitor_thread_key; |
| 188 |
mysql_pfs_key_t srv_purge_thread_key; |
| 189 |
mysql_pfs_key_t srv_worker_thread_key; |
| 190 |
mysql_pfs_key_t trx_recovery_rollback_thread_key; |
| 191 |
mysql_pfs_key_t srv_ts_alter_encrypt_thread_key; |
| 192 |
mysql_pfs_key_t parallel_rseg_init_thread_key; |
| 193 |
#endif /* UNIV_PFS_THREAD */ |
| 194 |
|
| 195 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 196 |
/** Array of all InnoDB stage events for monitoring activities via |
| 197 |
performance schema. */ |
| 198 |
static PSI_stage_info *srv_stages[] = { |
| 199 |
&srv_stage_alter_table_end, |
| 200 |
&srv_stage_alter_table_flush, |
| 201 |
&srv_stage_alter_table_insert, |
| 202 |
&srv_stage_alter_table_log_index, |
| 203 |
&srv_stage_alter_table_log_table, |
| 204 |
&srv_stage_alter_table_merge_sort, |
| 205 |
&srv_stage_alter_table_read_pk_internal_sort, |
| 206 |
&srv_stage_alter_tablespace_encryption, |
| 207 |
&srv_stage_buffer_pool_load, |
| 208 |
&srv_stage_clone_file_copy, |
| 209 |
&srv_stage_clone_redo_copy, |
| 210 |
&srv_stage_clone_page_copy, |
| 211 |
}; |
| 212 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 213 |
|
| 214 |
/** Sleep time in loops which wait for pending tasks during shutdown. */ |
| 215 |
static constexpr uint32_t SHUTDOWN_SLEEP_TIME_US = 100; |
| 216 |
|
| 217 |
/** Number of wait rounds during shutdown, after which error is produced, |
| 218 |
or other policy for timed out wait is applied. */ |
| 219 |
static constexpr uint32_t SHUTDOWN_SLEEP_ROUNDS = |
| 220 |
60 * 1000 * 1000 / SHUTDOWN_SLEEP_TIME_US; |
| 221 |
|
| 222 |
/** Create undo tablespace. |
| 223 |
@param[in] undo_space Undo Tablespace |
| 224 |
@return DB_SUCCESS or error code */ |
| 225 |
static dberr_t srv_undo_tablespace_create(undo::Tablespace &undo_space) { |
| 226 |
pfs_os_file_t fh; |
| 227 |
bool ret; |
| 228 |
dberr_t err = DB_SUCCESS; |
| 229 |
char *file_name = undo_space.file_name(); |
| 230 |
space_id_t space_id = undo_space.id(); |
| 231 |
|
| 232 |
ut_a(!srv_read_only_mode); |
| 233 |
ut_a(!srv_force_recovery); |
| 234 |
|
| 235 |
os_file_create_subdirs_if_needed(file_name); |
| 236 |
|
| 237 |
/* Until this undo tablespace can become active, keep a truncate log |
| 238 |
file around so that if a crash happens it can be rebuilt at startup. */ |
| 239 |
err = undo::start_logging(&undo_space); |
| 240 |
if (err != DB_SUCCESS) { |
| 241 |
ib::error(ER_IB_MSG_1070, undo_space.log_file_name(), |
| 242 |
undo_space.space_name()); |
| 243 |
} |
| 244 |
ut_ad(err == DB_SUCCESS); |
| 245 |
|
| 246 |
fh = os_file_create(innodb_data_file_key, file_name, |
| 247 |
(srv_read_only_mode ? OS_FILE_OPEN : OS_FILE_CREATE) | |
| 248 |
OS_FILE_ON_ERROR_NO_EXIT, |
| 249 |
OS_DATA_FILE, srv_read_only_mode, &ret); |
| 250 |
|
| 251 |
if (ret == false) { |
| 252 |
std::ostringstream stmt; |
| 253 |
|
| 254 |
if (os_file_get_last_error(false) == OS_FILE_ALREADY_EXISTS) { |
| 255 |
stmt << " since '" << file_name << "' already exists."; |
| 256 |
} else { |
| 257 |
stmt << ". os_file_create() returned " << ret << "."; |
| 258 |
} |
| 259 |
|
| 260 |
ib::error(ER_IB_MSG_1214, undo_space.space_name(), stmt.str().c_str()); |
| 261 |
|
| 262 |
err = DB_ERROR; |
| 263 |
} else { |
| 264 |
ut_a(!srv_read_only_mode); |
| 265 |
|
| 266 |
/* We created the data file and now write it full of zeros */ |
| 267 |
undo_space.set_new(); |
| 268 |
|
| 269 |
ib::info(ER_IB_MSG_1071, file_name); |
| 270 |
|
| 271 |
ulint size_mb = UNDO_INITIAL_SIZE >> 20; |
| 272 |
|
| 273 |
ib::info(ER_IB_MSG_1072, file_name, ulonglong{size_mb}); |
| 274 |
|
| 275 |
ib::info(ER_IB_MSG_1073); |
| 276 |
|
| 277 |
ret = os_file_set_size(file_name, fh, 0, UNDO_INITIAL_SIZE, true); |
| 278 |
|
| 279 |
DBUG_EXECUTE_IF("ib_undo_tablespace_create_fail", ret = false;); |
| 280 |
|
| 281 |
if (!ret) { |
| 282 |
ib::info(ER_IB_MSG_1074, file_name); |
| 283 |
err = DB_OUT_OF_FILE_SPACE; |
| 284 |
} |
| 285 |
|
| 286 |
os_file_close(fh); |
| 287 |
|
| 288 |
/* Add this space to the list of undo tablespaces to |
| 289 |
construct by creating header pages. If an old undo |
| 290 |
tablespace needed fixup before it is upgraded, |
| 291 |
there is no need to construct it.*/ |
| 292 |
if (undo::is_reserved(space_id)) { |
| 293 |
undo::add_space_to_construction_list(space_id); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
return (err); |
| 298 |
} |
| 299 |
|
| 300 |
/** Try to enable encryption of an undo log tablespace. |
| 301 |
@param[in] space_id undo tablespace id |
| 302 |
@return DB_SUCCESS if success */ |
| 303 |
static dberr_t srv_undo_tablespace_enable_encryption(space_id_t space_id) { |
| 304 |
dberr_t err; |
| 305 |
|
| 306 |
ut_ad(Encryption::check_keyring()); |
| 307 |
|
| 308 |
/* Set the space flag. The encryption metadata |
| 309 |
will be generated in fsp_header_init later. */ |
| 310 |
fil_space_t *space = fil_space_get(space_id); |
| 311 |
if (!FSP_FLAGS_GET_ENCRYPTION(space->flags)) { |
| 312 |
fsp_flags_set_encryption(space->flags); |
| 313 |
err = fil_set_encryption(space_id, Encryption::AES, nullptr, nullptr); |
| 314 |
if (err != DB_SUCCESS) { |
| 315 |
ib::error(ER_IB_MSG_1075, space->name); |
| 316 |
return (err); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return (DB_SUCCESS); |
| 321 |
} |
| 322 |
|
| 323 |
/** Try to read encryption metadata from an undo tablespace. |
| 324 |
@param[in] fh file handle of undo log file |
| 325 |
@param[in] file_name file name |
| 326 |
@param[in] space undo tablespace |
| 327 |
@return DB_SUCCESS if success */ |
| 328 |
static dberr_t srv_undo_tablespace_read_encryption(pfs_os_file_t fh, |
| 329 |
const char *file_name, |
| 330 |
fil_space_t *space) { |
| 331 |
IORequest request; |
| 332 |
ulint n_read = 0; |
| 333 |
size_t page_size = UNIV_PAGE_SIZE_MAX; |
| 334 |
dberr_t err = DB_ERROR; |
| 335 |
|
| 336 |
/* Align the memory for a possible read from a raw device */ |
| 337 |
byte *first_page = static_cast<byte *>( |
| 338 |
ut::aligned_alloc(UNIV_PAGE_SIZE_MAX, UNIV_PAGE_SIZE)); |
| 339 |
|
| 340 |
/* Don't want unnecessary complaints about partial reads. */ |
| 341 |
request.disable_partial_io_warnings(); |
| 342 |
|
| 343 |
err = os_file_read_no_error_handling(request, file_name, fh, first_page, 0, |
| 344 |
page_size, &n_read); |
| 345 |
|
| 346 |
if (err != DB_SUCCESS) { |
| 347 |
ib::info(ER_IB_MSG_1076, space->name, ut_strerr(err)); |
| 348 |
ut::aligned_free(first_page); |
| 349 |
return (err); |
| 350 |
} |
| 351 |
|
| 352 |
ulint offset; |
| 353 |
const page_size_t space_page_size(space->flags); |
| 354 |
|
| 355 |
offset = fsp_header_get_encryption_offset(space_page_size); |
| 356 |
ut_ad(offset); |
| 357 |
|
| 358 |
/* Return if the encryption metadata is empty. */ |
| 359 |
if (!Encryption::is_encrypted_with_v3(first_page + offset)) { |
| 360 |
ut::aligned_free(first_page); |
| 361 |
return (DB_SUCCESS); |
| 362 |
} |
| 363 |
|
| 364 |
byte key[Encryption::KEY_LEN]; |
| 365 |
byte iv[Encryption::KEY_LEN]; |
| 366 |
Encryption_key e_key{key, iv}; |
| 367 |
if (fsp_header_get_encryption_key(space->flags, e_key, first_page)) { |
| 368 |
fsp_flags_set_encryption(space->flags); |
| 369 |
err = fil_set_encryption(space->id, Encryption::AES, key, iv); |
| 370 |
ut_ad(err == DB_SUCCESS); |
| 371 |
} else { |
| 372 |
ut::aligned_free(first_page); |
| 373 |
return (DB_FAIL); |
| 374 |
} |
| 375 |
|
| 376 |
ut::aligned_free(first_page); |
| 377 |
ib::info(ER_IB_MSG_UNDO_ENCRYPTION_INFO_LOADED, space->name); |
| 378 |
|
| 379 |
return (DB_SUCCESS); |
| 380 |
} |
| 381 |
|
| 382 |
/** Fix up a v5.7 type undo tablespace that was being truncated. |
| 383 |
The space_id is not a reserved undo space_id. We will just delete |
| 384 |
the file since it will be replaced. |
| 385 |
@param[in] space_id Tablespace ID |
| 386 |
@return error code */ |
| 387 |
static dberr_t srv_undo_tablespace_fixup_57(space_id_t space_id) { |
| 388 |
space_id_t space_num = undo::id2num(space_id); |
| 389 |
ut_ad(space_num == space_id); |
| 390 |
if (undo::is_active_truncate_log_present(space_num)) { |
| 391 |
ib::info(ER_IB_MSG_1077, ulong{space_num}); |
| 392 |
|
| 393 |
if (srv_read_only_mode) { |
| 394 |
ib::error(ER_IB_MSG_1078); |
| 395 |
return (DB_READ_ONLY); |
| 396 |
} |
| 397 |
|
| 398 |
undo::Tablespace undo_space(space_id); |
| 399 |
|
| 400 |
/* Flush any changes recovered in REDO */ |
| 401 |
fil_flush(space_id); |
| 402 |
fil_space_close(space_id); |
| 403 |
|
| 404 |
os_file_delete_if_exists(innodb_data_file_key, undo_space.file_name(), |
| 405 |
nullptr); |
| 406 |
|
| 407 |
return (DB_TABLESPACE_DELETED); |
| 408 |
} |
| 409 |
|
| 410 |
return (DB_SUCCESS); |
| 411 |
} |
| 412 |
|
| 413 |
/** Start the fix-up process on an undo tablespace if it was in the process |
| 414 |
of being truncated when the server crashed. At this point, just delete the |
| 415 |
old file if it exists. |
| 416 |
We could do the whole reconstruction here for implicit undo spaces since we |
| 417 |
know the space_id, space_name, and file_name implicitly. But for explicit |
| 418 |
undo spaces, we must wait for the DD to be scanned in boot_tablespaces() |
| 419 |
in order to know the space_id, space_name, and file_name. |
| 420 |
@param[in] space_num undo tablespace number |
| 421 |
@return error code */ |
| 422 |
static dberr_t srv_undo_tablespace_fixup_num(space_id_t space_num) { |
| 423 |
if (!undo::is_active_truncate_log_present(space_num)) { |
| 424 |
return (DB_SUCCESS); |
| 425 |
} |
| 426 |
|
| 427 |
ib::info(ER_IB_MSG_1077, ulong{space_num}); |
| 428 |
|
| 429 |
if (srv_read_only_mode) { |
| 430 |
ib::error(ER_IB_MSG_1078); |
| 431 |
return (DB_READ_ONLY); |
| 432 |
} |
| 433 |
|
| 434 |
/* |
| 435 |
Search for a file that is using any of the space IDs assigned to this |
| 436 |
undo number. The directory scan assured that there are no duplicate files |
| 437 |
with the same space_id or with the same undo space number. |
| 438 |
*/ |
| 439 |
space_id_t space_id = SPACE_UNKNOWN; |
| 440 |
std::string scanned_name; |
| 441 |
fil_system_get_file_by_space_num(space_num, space_id, scanned_name); |
| 442 |
|
| 443 |
/* If the previous file still exists, delete it. */ |
| 444 |
if (scanned_name.length() > 0) { |
| 445 |
/* Flush any changes recovered in REDO */ |
| 446 |
fil_flush(space_id); |
| 447 |
fil_space_close(space_id); |
| 448 |
os_file_delete_if_exists(innodb_data_file_key, scanned_name.c_str(), |
| 449 |
nullptr); |
| 450 |
|
| 451 |
} else if (space_num < FSP_IMPLICIT_UNDO_TABLESPACES) { |
| 452 |
/* If there is any file with the implicit file name, delete it. */ |
| 453 |
undo::Tablespace undo_space(undo::num2id(space_num, 0)); |
| 454 |
os_file_delete_if_exists(innodb_data_file_key, undo_space.file_name(), |
| 455 |
nullptr); |
| 456 |
} |
| 457 |
|
| 458 |
return (DB_SUCCESS); |
| 459 |
} |
| 460 |
|
| 461 |
/** Fix up an undo tablespace if it was in the process of being truncated |
| 462 |
when the server crashed. This is the second call and is done after the DD |
| 463 |
is available so now we know the space_name, file_name and previous space_id. |
| 464 |
@param[in] space_name undo tablespace name |
| 465 |
@param[in] file_name undo tablespace file name |
| 466 |
@param[in] space_id undo tablespace ID |
| 467 |
@return error code */ |
| 468 |
dberr_t srv_undo_tablespace_fixup(const char *space_name, const char *file_name, |
| 469 |
space_id_t space_id) { |
| 470 |
ut_ad(fsp_is_undo_tablespace(space_id)); |
| 471 |
|
| 472 |
space_id_t space_num = undo::id2num(space_id); |
| 473 |
if (!undo::is_active_truncate_log_present(space_num)) { |
| 474 |
return (DB_SUCCESS); |
| 475 |
} |
| 476 |
|
| 477 |
if (srv_read_only_mode) { |
| 478 |
return (DB_READ_ONLY); |
| 479 |
} |
| 480 |
|
| 481 |
ib::info(ER_IB_MSG_1079, ulong{space_num}); |
| 482 |
|
| 483 |
/* It is possible for an explicit undo tablespace to have been truncated and |
| 484 |
recreated but not yet written with a header page when a crash occurred. In |
| 485 |
this case, the empty file would not have been scanned at startup and the |
| 486 |
first call to fixup did not know the filename. Now that we know it, just |
| 487 |
delete any file with that name if it exists. The dictionary claims it is |
| 488 |
an undo tablespace and there is a truncate log file present. */ |
| 489 |
os_file_delete_if_exists(innodb_data_file_key, file_name, nullptr); |
| 490 |
|
| 491 |
/* Mark the space_id for this undo tablespace number as in-use. */ |
| 492 |
undo::spaces->x_lock(); |
| 493 |
undo::unuse_space_id(space_id); |
| 494 |
space_id_t new_space_id = undo::next_space_id(space_id); |
| 495 |
undo::use_space_id(new_space_id); |
| 496 |
undo::spaces->x_unlock(); |
| 497 |
|
| 498 |
dberr_t err = srv_undo_tablespace_create(space_name, file_name, new_space_id); |
| 499 |
if (err != DB_SUCCESS) { |
| 500 |
return (err); |
| 501 |
} |
| 502 |
|
| 503 |
/* Update the DD with the new space ID and state. */ |
| 504 |
undo::spaces->s_lock(); |
| 505 |
undo::Tablespace *undo_space = undo::spaces->find(space_num); |
| 506 |
dd_space_states to_state; |
| 507 |
if (undo_space->is_inactive_explicit()) { |
| 508 |
to_state = DD_SPACE_STATE_EMPTY; |
| 509 |
undo_space->set_empty(); |
| 510 |
} else { |
| 511 |
to_state = DD_SPACE_STATE_ACTIVE; |
| 512 |
undo_space->set_active(); |
| 513 |
} |
| 514 |
undo::spaces->s_unlock(); |
| 515 |
|
| 516 |
bool dd_result = dd_tablespace_get_mdl(space_name); |
| 517 |
if (dd_result == DD_SUCCESS) { |
| 518 |
dd_result = |
| 519 |
dd_tablespace_set_id_and_state(space_name, new_space_id, to_state); |
| 520 |
} |
| 521 |
if (dd_result != DD_SUCCESS) { |
| 522 |
err = DB_ERROR; |
| 523 |
} |
| 524 |
|
| 525 |
return (err); |
| 526 |
} |
| 527 |
|
| 528 |
/** Open an undo tablespace. |
| 529 |
@param[in] undo_space Undo tablespace |
| 530 |
@return DB_SUCCESS or error code */ |
| 531 |
dberr_t srv_undo_tablespace_open(undo::Tablespace &undo_space) { |
| 532 |
DBUG_EXECUTE_IF("ib_undo_tablespace_open_fail", |
| 533 |
return (DB_CANNOT_OPEN_FILE);); |
| 534 |
|
| 535 |
pfs_os_file_t fh; |
| 536 |
bool success; |
| 537 |
uint32_t flags; |
| 538 |
bool atomic_write; |
| 539 |
dberr_t err = DB_ERROR; |
| 540 |
space_id_t space_id = undo_space.id(); |
| 541 |
char *undo_name = undo_space.space_name(); |
| 542 |
char *file_name = undo_space.file_name(); |
| 543 |
|
| 544 |
/* Check if it was already opened during redo recovery. */ |
| 545 |
fil_space_t *space = fil_space_get(space_id); |
| 546 |
|
| 547 |
/* Flush and close any current file handle so we can open |
| 548 |
a local one below. */ |
| 549 |
if (space != nullptr) { |
| 550 |
fil_flush(space_id); |
| 551 |
fil_space_close(space_id); |
| 552 |
} |
| 553 |
|
| 554 |
if (!os_file_check_mode(file_name, srv_read_only_mode)) { |
| 555 |
ib::error(ER_IB_MSG_1081, file_name, |
| 556 |
srv_read_only_mode ? "readable!" : "writable!"); |
| 557 |
|
| 558 |
return (DB_READ_ONLY); |
| 559 |
} |
| 560 |
|
| 561 |
/* Open a local handle. */ |
| 562 |
fh = os_file_create( |
| 563 |
innodb_data_file_key, file_name, |
| 564 |
OS_FILE_OPEN_RETRY | OS_FILE_ON_ERROR_NO_EXIT | OS_FILE_ON_ERROR_SILENT, |
| 565 |
OS_DATA_FILE, srv_read_only_mode, &success); |
| 566 |
if (!success) { |
| 567 |
return (DB_CANNOT_OPEN_FILE); |
| 568 |
} |
| 569 |
|
| 570 |
/* Check if this file supports atomic write. */ |
| 571 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) |
| 572 |
if (!dblwr::is_enabled()) { |
| 573 |
atomic_write = fil_fusionio_enable_atomic_write(fh); |
| 574 |
} else { |
| 575 |
atomic_write = false; |
| 576 |
} |
| 577 |
#else |
| 578 |
atomic_write = false; |
| 579 |
#endif /* !NO_FALLOCATE && UNIV_LINUX */ |
| 580 |
|
| 581 |
if (space == nullptr) { |
| 582 |
/* Load the tablespace into InnoDB's internal data structures. |
| 583 |
Set the compressed page size to 0 (non-compressed) */ |
| 584 |
flags = fsp_flags_init(univ_page_size, false, false, false, false); |
| 585 |
space = fil_space_create(undo_name, space_id, flags, FIL_TYPE_TABLESPACE); |
| 586 |
ut_a(space != nullptr); |
| 587 |
ut_ad(fil_validate()); |
| 588 |
|
| 589 |
os_offset_t size = os_file_get_size(fh); |
| 590 |
ut_a(size != (os_offset_t)-1); |
| 591 |
page_no_t n_pages = static_cast<page_no_t>(size / UNIV_PAGE_SIZE); |
| 592 |
|
| 593 |
if (fil_node_create(file_name, n_pages, space, false, atomic_write) == |
| 594 |
nullptr) { |
| 595 |
os_file_close(fh); |
| 596 |
|
| 597 |
ib::error(ER_IB_MSG_1082, undo_name); |
| 598 |
|
| 599 |
return (DB_ERROR); |
| 600 |
} |
| 601 |
|
| 602 |
} else { |
| 603 |
auto &file = space->files.front(); |
| 604 |
|
| 605 |
file.atomic_write = atomic_write; |
| 606 |
} |
| 607 |
|
| 608 |
/* Read the encryption metadata in this undo tablespace. |
| 609 |
If the encryption info in the first page cannot be decrypted |
| 610 |
by the master key, this table cannot be opened. */ |
| 611 |
err = srv_undo_tablespace_read_encryption(fh, file_name, space); |
| 612 |
|
| 613 |
/* The file handle will no longer be needed. */ |
| 614 |
success = os_file_close(fh); |
| 615 |
ut_ad(success); |
| 616 |
|
| 617 |
if (err != DB_SUCCESS) { |
| 618 |
ib::error(ER_IB_MSG_1083, undo_name); |
| 619 |
return (err); |
| 620 |
} |
| 621 |
|
| 622 |
/* Now that space and node exist, make sure this undo tablespace |
| 623 |
is open so that it stays open until shutdown. |
| 624 |
But if it is under construction, we cannot open it until the |
| 625 |
header page has been written. */ |
| 626 |
if (!undo::is_under_construction(space_id)) { |
| 627 |
bool success = fil_space_open(space_id); |
| 628 |
ut_a(success); |
| 629 |
} |
| 630 |
|
| 631 |
if (undo::is_reserved(space_id)) { |
| 632 |
undo::spaces->add(undo_space); |
| 633 |
} |
| 634 |
|
| 635 |
return (DB_SUCCESS); |
| 636 |
} |
| 637 |
|
| 638 |
/** Open an undo tablespace with a specified space_id. |
| 639 |
@param[in] space_id tablespace ID |
| 640 |
@return DB_SUCCESS or error code */ |
| 641 |
static dberr_t srv_undo_tablespace_open_by_id(space_id_t space_id) { |
| 642 |
undo::Tablespace undo_space(space_id); |
| 643 |
std::string scanned_name; |
| 644 |
|
| 645 |
/* If an undo tablespace with this space_id already exists, |
| 646 |
check if the name found in the file map for this undo space_id |
| 647 |
is the standard name. The directory scan assured that there are |
| 648 |
no duplicates. The filename found must match the standard name |
| 649 |
if this is an implicit undo tablespace. In other words, implicit |
| 650 |
undo tablespaces must be found in srv_undo_dir. */ |
| 651 |
|
| 652 |
bool found = fil_system_get_file_by_space_id(space_id, scanned_name); |
| 653 |
|
| 654 |
if (found && |
| 655 |
!Fil_path::is_same_as(undo_space.file_name(), scanned_name.c_str())) { |
| 656 |
ib::error(ER_IB_MSG_FOUND_WRONG_UNDO_SPACE, undo_space.file_name(), |
| 657 |
ulong{space_id}, scanned_name.c_str()); |
| 658 |
return (DB_WRONG_FILE_NAME); |
| 659 |
} |
| 660 |
|
| 661 |
dberr_t err = srv_undo_tablespace_open(undo_space); |
| 662 |
|
| 663 |
if (err == DB_SUCCESS) { |
| 664 |
fil_space_set_undo_size(space_id, false); |
| 665 |
} |
| 666 |
|
| 667 |
return (err); |
| 668 |
} |
| 669 |
|
| 670 |
/** Open an undo tablespace with a specified undo number. |
| 671 |
@param[in] space_num undo tablespace number |
| 672 |
@return DB_SUCCESS or error code */ |
| 673 |
static dberr_t srv_undo_tablespace_open_by_num(space_id_t space_num) { |
| 674 |
space_id_t space_id = SPACE_UNKNOWN; |
| 675 |
std::string scanned_name; |
| 676 |
|
| 677 |
/* Search for a file that is using any of the space IDs assigned to this |
| 678 |
undo number. The directory scan assured that there are no duplicate files |
| 679 |
with the same space_id or with the same undo space number. */ |
| 680 |
if (!fil_system_get_file_by_space_num(space_num, space_id, scanned_name)) { |
| 681 |
return (DB_CANNOT_OPEN_FILE); |
| 682 |
} |
| 683 |
|
| 684 |
/* The first 2 undo space numbers must be implicit. */ |
| 685 |
bool is_default = (space_num <= FSP_IMPLICIT_UNDO_TABLESPACES); |
| 686 |
|
| 687 |
/* v8.0.12 used innodb_undo_tablespaces to implicitly create undo |
| 688 |
spaces so there may be more than 2 implicit undo tablespaces. They |
| 689 |
must match the default undo filename and must be found in |
| 690 |
srv_undo_directory. */ |
| 691 |
undo::Tablespace undo_space(space_id); |
| 692 |
if (!Fil_path::is_same_as(undo_space.file_name(), scanned_name.c_str())) { |
| 693 |
if (is_default) { |
| 694 |
ib::info(ER_IB_MSG_1080, undo_space.file_name(), scanned_name.c_str(), |
| 695 |
ulong{space_id}); |
| 696 |
|
| 697 |
return (DB_WRONG_FILE_NAME); |
| 698 |
} |
| 699 |
|
| 700 |
/* Explicit undo tablespaces must end with the suffix '.ibu'. */ |
| 701 |
if (!Fil_path::has_suffix(IBU, scanned_name)) { |
| 702 |
ib::info(ER_IB_MSG_NOT_END_WITH_IBU, scanned_name.c_str()); |
| 703 |
|
| 704 |
return (DB_WRONG_FILE_NAME); |
| 705 |
} |
| 706 |
|
| 707 |
/* Use the file name found in the scan. */ |
| 708 |
undo_space.set_file_name(scanned_name.c_str()); |
| 709 |
} |
| 710 |
|
| 711 |
/* Mark the space_id for this undo tablespace number as in-use. */ |
| 712 |
undo::use_space_id(space_id); |
| 713 |
|
| 714 |
ib::info(ER_IB_MSG_USING_UNDO_SPACE, scanned_name.c_str()); |
| 715 |
|
| 716 |
dberr_t err = srv_undo_tablespace_open(undo_space); |
| 717 |
|
| 718 |
if (err == DB_SUCCESS) { |
| 719 |
fil_space_set_undo_size(space_id, false); |
| 720 |
} |
| 721 |
|
| 722 |
return (err); |
| 723 |
} |
| 724 |
|
| 725 |
/* Open existing undo tablespaces up to the number in target_undo_tablespace. |
| 726 |
If we are making a new database, these have been created. |
| 727 |
If doing recovery, these should exist and may be needed for recovery. |
| 728 |
If we fail to open any of these it is a fatal error. |
| 729 |
@return DB_SUCCESS or error code */ |
| 730 |
static dberr_t srv_undo_tablespaces_open() { |
| 731 |
dberr_t err; |
| 732 |
|
| 733 |
/* If upgrading from 5.7, build a list of existing undo tablespaces |
| 734 |
from the references in the TRX_SYS page. (not including the system |
| 735 |
tablespace) */ |
| 736 |
trx_rseg_get_n_undo_tablespaces(trx_sys_undo_spaces); |
| 737 |
|
| 738 |
/* If undo tablespaces are being tracked in trx_sys then these |
| 739 |
will need to be replaced by independent undo tablespaces with |
| 740 |
reserved space_ids and RSEG_ARRAY pages. */ |
| 741 |
if (trx_sys_undo_spaces->size() > 0) { |
| 742 |
/* Open each undo tablespace tracked in TRX_SYS. */ |
| 743 |
for (const auto space_id : *trx_sys_undo_spaces) { |
| 744 |
fil_set_max_space_id_if_bigger(space_id); |
| 745 |
|
| 746 |
/* Check if this undo tablespace was in the process of being truncated. |
| 747 |
If so, just delete the file since it will be replaced. */ |
| 748 |
if (DB_TABLESPACE_DELETED == srv_undo_tablespace_fixup_57(space_id)) { |
| 749 |
continue; |
| 750 |
} |
| 751 |
|
| 752 |
err = srv_undo_tablespace_open_by_id(space_id); |
| 753 |
if (err != DB_SUCCESS) { |
| 754 |
ib::error(ER_IB_MSG_CANNOT_OPEN_57_UNDO, ulong{space_id}); |
| 755 |
return (err); |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
/* Open all existing implicit and explicit undo tablespaces. |
| 761 |
The tablespace scan has completed and the undo::space_id_bank has been |
| 762 |
filled with the space Ids that were found. */ |
| 763 |
undo::spaces->x_lock(); |
| 764 |
ut_ad(undo::spaces->size() == 0); |
| 765 |
|
| 766 |
for (space_id_t num = 1; num <= FSP_MAX_UNDO_TABLESPACES; ++num) { |
| 767 |
/* Check if this undo tablespace was in the process of being truncated. |
| 768 |
If so, recreate it and add it to the construction list. */ |
| 769 |
dberr_t err = srv_undo_tablespace_fixup_num(num); |
| 770 |
if (err != DB_SUCCESS) { |
| 771 |
undo::spaces->x_unlock(); |
| 772 |
return (err); |
| 773 |
} |
| 774 |
|
| 775 |
err = srv_undo_tablespace_open_by_num(num); |
| 776 |
switch (err) { |
| 777 |
case DB_WRONG_FILE_NAME: |
| 778 |
/* An Undo tablespace was found where the mapping |
| 779 |
file said it was. Now we have a different filename |
| 780 |
for it. The undo directory must have changed and |
| 781 |
the the files were not moved. Cannot startup. */ |
| 782 |
case DB_READ_ONLY: |
| 783 |
/* The undo tablespace was found where it should be |
| 784 |
but it cannot be opened in read/write mode. */ |
| 785 |
default: |
| 786 |
/* The undo tablespace was found where it should be |
| 787 |
but it cannot be used. */ |
| 788 |
undo::spaces->x_unlock(); |
| 789 |
return (err); |
| 790 |
|
| 791 |
case DB_SUCCESS: |
| 792 |
|
| 793 |
case DB_CANNOT_OPEN_FILE: |
| 794 |
/* Doesn't exist, keep looking */ |
| 795 |
break; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
ulint n_found_new = undo::spaces->size(); |
| 800 |
ulint n_found_old = trx_sys_undo_spaces->size(); |
| 801 |
undo::spaces->x_unlock(); |
| 802 |
|
| 803 |
if (n_found_old != 0 || n_found_new < FSP_IMPLICIT_UNDO_TABLESPACES) { |
| 804 |
std::ostringstream msg; |
| 805 |
|
| 806 |
if (n_found_old != 0) { |
| 807 |
msg << "Found " << n_found_old << " undo tablespaces that" |
| 808 |
<< " need to be upgraded. "; |
| 809 |
} |
| 810 |
|
| 811 |
if (n_found_new < FSP_IMPLICIT_UNDO_TABLESPACES) { |
| 812 |
msg << "Will create " << (FSP_IMPLICIT_UNDO_TABLESPACES - n_found_new) |
| 813 |
<< " new undo tablespaces."; |
| 814 |
} |
| 815 |
|
| 816 |
ib::info(ER_IB_MSG_1215) << msg.str(); |
| 817 |
} |
| 818 |
|
| 819 |
if (n_found_new + n_found_old) { |
| 820 |
ib::info(ER_IB_MSG_1085, ulonglong{n_found_new + n_found_old}); |
| 821 |
} |
| 822 |
|
| 823 |
return (DB_SUCCESS); |
| 824 |
} |
| 825 |
|
| 826 |
/** Create the implicit undo tablespaces if we are creating a new instance |
| 827 |
or if there was not enough implicit undo tablespaces previously existing. |
| 828 |
@return DB_SUCCESS or error code */ |
| 829 |
static dberr_t srv_undo_tablespaces_create() { |
| 830 |
dberr_t err = DB_SUCCESS; |
| 831 |
|
| 832 |
undo::spaces->x_lock(); |
| 833 |
|
| 834 |
ulint initial_implicit_undo_spaces = 0; |
| 835 |
for (auto undo_space : undo::spaces->m_spaces) { |
| 836 |
if (undo_space->num() <= FSP_IMPLICIT_UNDO_TABLESPACES) { |
| 837 |
initial_implicit_undo_spaces++; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
if (initial_implicit_undo_spaces >= FSP_IMPLICIT_UNDO_TABLESPACES) { |
| 842 |
undo::spaces->x_unlock(); |
| 843 |
return (DB_SUCCESS); |
| 844 |
} |
| 845 |
|
| 846 |
if (srv_read_only_mode || srv_force_recovery > 0) { |
| 847 |
const char *mode; |
| 848 |
|
| 849 |
mode = srv_read_only_mode ? "read_only" : "force_recovery", |
| 850 |
|
| 851 |
ib::warn(ER_IB_MSG_1086, mode, ulonglong{initial_implicit_undo_spaces}); |
| 852 |
|
| 853 |
if (initial_implicit_undo_spaces == 0) { |
| 854 |
ib::error(ER_IB_MSG_1087, mode); |
| 855 |
|
| 856 |
undo::spaces->x_unlock(); |
| 857 |
return (DB_ERROR); |
| 858 |
} |
| 859 |
|
| 860 |
undo::spaces->x_unlock(); |
| 861 |
return (DB_SUCCESS); |
| 862 |
} |
| 863 |
|
| 864 |
/* Create all implicit undo tablespaces that are needed. */ |
| 865 |
for (space_id_t num = 1; num <= FSP_IMPLICIT_UNDO_TABLESPACES; ++num) { |
| 866 |
/* If the trunc log file is present, the fixup process will be |
| 867 |
finished later. */ |
| 868 |
if (undo::is_active_truncate_log_present(num)) { |
| 869 |
continue; |
| 870 |
} |
| 871 |
|
| 872 |
/* Check if an independent undo space for this space_id |
| 873 |
has already been found. */ |
| 874 |
if (undo::spaces->contains(num)) { |
| 875 |
continue; |
| 876 |
} |
| 877 |
|
| 878 |
/* Mark this implicit undo space number as used and return the next |
| 879 |
available space_id. */ |
| 880 |
space_id_t space_id = undo::use_next_space_id(num); |
| 881 |
|
| 882 |
/* Since it is not found, create it. */ |
| 883 |
undo::Tablespace undo_space(space_id); |
| 884 |
undo_space.set_new(); |
| 885 |
err = srv_undo_tablespace_create(undo_space); |
| 886 |
if (err != DB_SUCCESS) { |
| 887 |
ib::info(ER_IB_MSG_1088, undo_space.space_name()); |
| 888 |
break; |
| 889 |
} |
| 890 |
|
| 891 |
/* Open this new undo tablespace. */ |
| 892 |
err = srv_undo_tablespace_open(undo_space); |
| 893 |
if (err != DB_SUCCESS) { |
| 894 |
ib::info(ER_IB_MSG_1089, int{err}, ut_strerr(err), |
| 895 |
undo_space.space_name()); |
| 896 |
|
| 897 |
break; |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
undo::spaces->x_unlock(); |
| 902 |
|
| 903 |
ulint new_spaces = |
| 904 |
FSP_IMPLICIT_UNDO_TABLESPACES - initial_implicit_undo_spaces; |
| 905 |
|
| 906 |
ib::info(ER_IB_MSG_1090, ulonglong{new_spaces}); |
| 907 |
|
| 908 |
return (err); |
| 909 |
} |
| 910 |
|
| 911 |
/** Finish building an undo tablespace. So far these tablespace files in |
| 912 |
the construction list should be created and filled with zeros. |
| 913 |
@return DB_SUCCESS or error code */ |
| 914 |
static dberr_t srv_undo_tablespaces_construct() { |
| 915 |
mtr_t mtr; |
| 916 |
|
| 917 |
if (undo::s_under_construction.empty()) { |
| 918 |
return (DB_SUCCESS); |
| 919 |
} |
| 920 |
|
| 921 |
ut_a(!srv_read_only_mode); |
| 922 |
ut_a(!srv_force_recovery); |
| 923 |
|
| 924 |
if (srv_undo_log_encrypt && Encryption::check_keyring() == false) { |
| 925 |
my_error(ER_CANNOT_FIND_KEY_IN_KEYRING, MYF(0)); |
| 926 |
return (DB_ERROR); |
| 927 |
} |
| 928 |
|
| 929 |
for (auto space_id : undo::s_under_construction) { |
| 930 |
/* Enable undo log encryption if it's ON. */ |
| 931 |
if (srv_undo_log_encrypt) { |
| 932 |
dberr_t err = srv_undo_tablespace_enable_encryption(space_id); |
| 933 |
|
| 934 |
if (err != DB_SUCCESS) { |
| 935 |
ib::error(ER_IB_MSG_1091, ulong{undo::id2num(space_id)}); |
| 936 |
|
| 937 |
return (err); |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
log_free_check(); |
| 942 |
|
| 943 |
mtr_start(&mtr); |
| 944 |
|
| 945 |
mtr_x_lock(fil_space_get_latch(space_id), &mtr, UT_LOCATION_HERE); |
| 946 |
|
| 947 |
if (!fsp_header_init(space_id, UNDO_INITIAL_SIZE_IN_PAGES, &mtr)) { |
| 948 |
ib::error(ER_IB_MSG_1093, ulong{undo::id2num(space_id)}); |
| 949 |
|
| 950 |
mtr_commit(&mtr); |
| 951 |
return (DB_ERROR); |
| 952 |
} |
| 953 |
|
| 954 |
/* Add the RSEG_ARRAY page. */ |
| 955 |
trx_rseg_array_create(space_id, &mtr); |
| 956 |
|
| 957 |
mtr_commit(&mtr); |
| 958 |
|
| 959 |
/* The rollback segments will get created later in |
| 960 |
trx_rseg_add_rollback_segments(). */ |
| 961 |
} |
| 962 |
|
| 963 |
if (srv_undo_log_encrypt) { |
| 964 |
ut_d(bool ret =) srv_enable_undo_encryption(); |
| 965 |
ut_ad(!ret); |
| 966 |
} |
| 967 |
|
| 968 |
return (DB_SUCCESS); |
| 969 |
} |
| 970 |
|
| 971 |
/** Mark the point in which the undo tablespaces in the construction list |
| 972 |
are fully constructed and ready to use. */ |
| 973 |
static void srv_undo_tablespaces_mark_construction_done() { |
| 974 |
/* Remove the truncate log files if they exist. */ |
| 975 |
for (auto space_id : undo::s_under_construction) { |
| 976 |
/* Flush these pages to disk since they were not redo logged. */ |
| 977 |
auto flush_observer = ut::new_withkey<Flush_observer>( |
| 978 |
UT_NEW_THIS_FILE_PSI_KEY, space_id, nullptr, nullptr); |
| 979 |
|
| 980 |
flush_observer->flush(); |
| 981 |
ut::delete_(flush_observer); |
| 982 |
|
| 983 |
space_id_t space_num = undo::id2num(space_id); |
| 984 |
if (undo::is_active_truncate_log_present(space_num)) { |
| 985 |
undo::done_logging(space_num); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
undo::clear_construction_list(); |
| 990 |
} |
| 991 |
|
| 992 |
/** Upgrade undo tablespaces by deleting the old undo tablespaces |
| 993 |
referenced by the TRX_SYS page. |
| 994 |
@return error code */ |
| 995 |
dberr_t srv_undo_tablespaces_upgrade() { |
| 996 |
if (trx_sys_undo_spaces->empty()) { |
| 997 |
goto cleanup; |
| 998 |
} |
| 999 |
|
| 1000 |
/* Recovered transactions in the prepared state prevent the old |
| 1001 |
rsegs and undo tablespaces they are in from being deleted. |
| 1002 |
These transactions must be either committed or rolled back by |
| 1003 |
the mysql server.*/ |
| 1004 |
if (trx_sys->n_prepared_trx > 0) { |
| 1005 |
ib::warn(ER_IB_MSG_1094); |
| 1006 |
return (DB_SUCCESS); |
| 1007 |
} |
| 1008 |
|
| 1009 |
ib::info(ER_IB_MSG_1095, trx_sys_undo_spaces->size(), |
| 1010 |
ulong{FSP_IMPLICIT_UNDO_TABLESPACES}); |
| 1011 |
|
| 1012 |
/* All Undo Tablespaces found in the TRX_SYS page need to be |
| 1013 |
deleted. The new independent undo tablespaces were created in |
| 1014 |
in srv_undo_tablespaces_create() */ |
| 1015 |
for (const auto space_id : *trx_sys_undo_spaces) { |
| 1016 |
undo::Tablespace undo_space(space_id); |
| 1017 |
|
| 1018 |
fil_space_close(undo_space.id()); |
| 1019 |
|
| 1020 |
auto err = fil_delete_tablespace(undo_space.id(), BUF_REMOVE_ALL_NO_WRITE); |
| 1021 |
|
| 1022 |
if (err != DB_SUCCESS) { |
| 1023 |
ib::warn(ER_IB_MSG_57_UNDO_SPACE_DELETE_FAIL, undo_space.space_name()); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* All pages should be removed from the spaces we deleted. We just collect |
| 1028 |
them now, so that the space_id -> shard mapping is correct - it will be |
| 1029 |
changed the second the trx_sys_undo_spaces is cleared.*/ |
| 1030 |
fil_purge(); |
| 1031 |
|
| 1032 |
/* Remove the tracking of these undo tablespaces from TRX_SYS page and |
| 1033 |
trx_sys->rsegs. */ |
| 1034 |
trx_rseg_upgrade_undo_tablespaces(); |
| 1035 |
|
| 1036 |
/* Since we now have new format undo tablespaces, we will no longer |
| 1037 |
look for undo tablespaces or rollback segments in the TRX_SYS page |
| 1038 |
or the trx_sys->rsegs vector. */ |
| 1039 |
trx_sys_undo_spaces->clear(); |
| 1040 |
|
| 1041 |
cleanup: |
| 1042 |
/* Post 5.7 undo tablespaces track their own rsegs. |
| 1043 |
Clear the list of rsegs in old undo tablespaces. */ |
| 1044 |
trx_sys->rsegs.clear(); |
| 1045 |
|
| 1046 |
return (DB_SUCCESS); |
| 1047 |
} |
| 1048 |
|
| 1049 |
/** Downgrade undo tablespaces by deleting the new undo tablespaces which |
| 1050 |
are not referenced by the TRX_SYS page. */ |
| 1051 |
static void srv_undo_tablespaces_downgrade() { |
| 1052 |
ut_ad(srv_downgrade_logs); |
| 1053 |
|
| 1054 |
ib::info(ER_IB_MSG_1096, ulonglong{undo::spaces->size()}); |
| 1055 |
|
| 1056 |
/* All the new independent undo tablespaces that were created in |
| 1057 |
in srv_undo_tablespaces_create() need to be deleted. */ |
| 1058 |
for (const auto undo_space : undo::spaces->m_spaces) { |
| 1059 |
fil_space_close(undo_space->id()); |
| 1060 |
|
| 1061 |
os_file_delete(innodb_data_file_key, undo_space->file_name()); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
/** Create an undo tablespace with an explicit file name |
| 1066 |
This is called during CREATE UNDO TABLESPACE. |
| 1067 |
@param[in] space_name tablespace name |
| 1068 |
@param[in] file_name file name |
| 1069 |
@param[in] space_id Tablespace ID |
| 1070 |
@return DB_SUCCESS or error code */ |
| 1071 |
dberr_t srv_undo_tablespace_create(const char *space_name, |
| 1072 |
const char *file_name, space_id_t space_id) { |
| 1073 |
if (srv_undo_log_encrypt && Encryption::check_keyring() == false) { |
| 1074 |
my_error(ER_CANNOT_FIND_KEY_IN_KEYRING, MYF(0)); |
| 1075 |
return (DB_ERROR); |
| 1076 |
} |
| 1077 |
|
| 1078 |
/* We need to x_lock the undo::spaces list until after this |
| 1079 |
is created and added to it. */ |
| 1080 |
undo::spaces->x_lock(); |
| 1081 |
|
| 1082 |
ut_ad(undo::spaces->find(undo::id2num(space_id)) == nullptr); |
| 1083 |
|
| 1084 |
undo::Tablespace undo_space(space_id); |
| 1085 |
undo_space.set_space_name(space_name); |
| 1086 |
undo_space.set_file_name(file_name); |
| 1087 |
|
| 1088 |
dberr_t err = srv_undo_tablespace_create(undo_space); |
| 1089 |
if (err != DB_SUCCESS) { |
| 1090 |
undo::spaces->x_unlock(); |
| 1091 |
goto cleanup_and_exit; |
| 1092 |
} |
| 1093 |
|
| 1094 |
/* Open this new undo tablespace. */ |
| 1095 |
err = srv_undo_tablespace_open(undo_space); |
| 1096 |
if (err != DB_SUCCESS) { |
| 1097 |
ib::error(ER_IB_MSG_ERROR_OPENING_NEW_UNDO_SPACE, int{err}, space_name); |
| 1098 |
undo::spaces->x_unlock(); |
| 1099 |
goto cleanup_and_exit; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* Unlock the undo::spaces list now that we are no longer changing it. |
| 1103 |
This new undo space will not be used by new transactions until it |
| 1104 |
becomes active. */ |
| 1105 |
undo::spaces->x_unlock(); |
| 1106 |
|
| 1107 |
/* Write header and RSEG_ARRAY pages to this undo tablespace. */ |
| 1108 |
err = srv_undo_tablespaces_construct(); |
| 1109 |
if (err != DB_SUCCESS) { |
| 1110 |
goto cleanup_and_exit; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* Create the rollback segments in this tablespace and add an Rseg object |
| 1114 |
for each one to the Rsegs list. */ |
| 1115 |
if (!trx_rseg_init_rollback_segments(space_id, srv_rollback_segments)) { |
| 1116 |
err = DB_ERROR; |
| 1117 |
goto cleanup_and_exit; |
| 1118 |
} |
| 1119 |
|
| 1120 |
cleanup_and_exit: |
| 1121 |
/* If UNDO tablespace couldn't initialize completely, remove it from |
| 1122 |
undo tablespace list */ |
| 1123 |
if (err != DB_SUCCESS) { |
| 1124 |
undo::spaces->x_lock(); |
| 1125 |
undo::spaces->drop(undo_space); |
| 1126 |
undo::spaces->x_unlock(); |
| 1127 |
|
| 1128 |
/* Remove undo tablespace file (if created) */ |
| 1129 |
os_file_delete_if_exists(innodb_data_file_key, undo_space.file_name(), |
| 1130 |
nullptr); |
| 1131 |
} |
| 1132 |
|
| 1133 |
srv_undo_tablespaces_mark_construction_done(); |
| 1134 |
return (err); |
| 1135 |
} |
| 1136 |
|
| 1137 |
/** Initialize undo::spaces and trx_sys_undo_spaces, |
| 1138 |
called once during srv_start(). */ |
| 1139 |
void undo_spaces_init() { |
| 1140 |
ut_ad(undo::spaces == nullptr); |
| 1141 |
|
| 1142 |
undo::spaces = ut::new_withkey<undo::Tablespaces>( |
| 1143 |
ut::make_psi_memory_key(mem_key_undo_spaces)); |
| 1144 |
|
| 1145 |
trx_sys_undo_spaces_init(); |
| 1146 |
|
| 1147 |
undo::init_space_id_bank(); |
| 1148 |
} |
| 1149 |
|
| 1150 |
/** Free the resources occupied by undo::spaces and trx_sys_undo_spaces, |
| 1151 |
called once during thread de-initialization. */ |
| 1152 |
void undo_spaces_deinit() { |
| 1153 |
if (srv_downgrade_logs) { |
| 1154 |
srv_undo_tablespaces_downgrade(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
if (undo::spaces != nullptr) { |
| 1158 |
/* There can't be any active transactions. */ |
| 1159 |
undo::spaces->clear(); |
| 1160 |
|
| 1161 |
ut::delete_(undo::spaces); |
| 1162 |
undo::spaces = nullptr; |
| 1163 |
} |
| 1164 |
|
| 1165 |
trx_sys_undo_spaces_deinit(); |
| 1166 |
|
| 1167 |
if (undo::space_id_bank != nullptr) { |
| 1168 |
ut::delete_arr(undo::space_id_bank); |
| 1169 |
undo::space_id_bank = nullptr; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/** Open the configured number of implicit undo tablespaces. |
| 1174 |
@param[in] create_new_db true if new db being created |
| 1175 |
@return DB_SUCCESS or error code */ |
| 1176 |
static dberr_t srv_undo_tablespaces_init(bool create_new_db) { |
| 1177 |
dberr_t err = DB_SUCCESS; |
| 1178 |
|
| 1179 |
/* Open any existing implicit undo tablespaces. */ |
| 1180 |
if (!create_new_db) { |
| 1181 |
err = srv_undo_tablespaces_open(); |
| 1182 |
if (err != DB_SUCCESS) { |
| 1183 |
return (err); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
/* If this is opening an existing database, create and open any |
| 1188 |
undo tablespaces that are still needed. For a new DB, create |
| 1189 |
them all. */ |
| 1190 |
mutex_enter(&undo::ddl_mutex); |
| 1191 |
err = srv_undo_tablespaces_create(); |
| 1192 |
if (err != DB_SUCCESS) { |
| 1193 |
mutex_exit(&undo::ddl_mutex); |
| 1194 |
return (err); |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* Finish building any undo tablespaces just created by adding |
| 1198 |
header pages, rseg_array pages, and rollback segments. Then delete |
| 1199 |
any undo truncation log files and clear the construction list. |
| 1200 |
This list includes any tablespace newly created or fixed-up. */ |
| 1201 |
err = srv_undo_tablespaces_construct(); |
| 1202 |
if (err != DB_SUCCESS) { |
| 1203 |
mutex_exit(&undo::ddl_mutex); |
| 1204 |
return (err); |
| 1205 |
} |
| 1206 |
|
| 1207 |
mutex_exit(&undo::ddl_mutex); |
| 1208 |
return (DB_SUCCESS); |
| 1209 |
} |
| 1210 |
|
| 1211 |
/******************************************************************** |
| 1212 |
Wait for the purge thread(s) to start up. */ |
| 1213 |
static void srv_start_wait_for_purge_to_start() { |
| 1214 |
/* Wait for the purge coordinator and master thread to startup. */ |
| 1215 |
|
| 1216 |
purge_state_t state = trx_purge_state(); |
| 1217 |
|
| 1218 |
ut_a(state != PURGE_STATE_DISABLED); |
| 1219 |
|
| 1220 |
while (srv_shutdown_state.load() < SRV_SHUTDOWN_PURGE && |
| 1221 |
srv_force_recovery < SRV_FORCE_NO_BACKGROUND && |
| 1222 |
state == PURGE_STATE_INIT) { |
| 1223 |
switch (state = trx_purge_state()) { |
| 1224 |
case PURGE_STATE_RUN: |
| 1225 |
case PURGE_STATE_STOP: |
| 1226 |
break; |
| 1227 |
|
| 1228 |
case PURGE_STATE_INIT: |
| 1229 |
ib::info(ER_IB_MSG_1097); |
| 1230 |
|
| 1231 |
std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 1232 |
break; |
| 1233 |
|
| 1234 |
case PURGE_STATE_EXIT: |
| 1235 |
case PURGE_STATE_DISABLED: |
| 1236 |
ut_error; |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
/** Create the temporary file tablespace. |
| 1242 |
@param[in] create_new_db whether we are creating a new database |
| 1243 |
@param[in,out] tmp_space Shared Temporary SysTablespace |
| 1244 |
@return DB_SUCCESS or error code. */ |
| 1245 |
static dberr_t srv_open_tmp_tablespace(bool create_new_db, |
| 1246 |
SysTablespace *tmp_space) { |
| 1247 |
page_no_t sum_of_new_sizes; |
| 1248 |
|
| 1249 |
/* Will try to remove if there is existing file left-over by last |
| 1250 |
unclean shutdown */ |
| 1251 |
tmp_space->set_sanity_check_status(true); |
| 1252 |
tmp_space->delete_files(); |
| 1253 |
tmp_space->set_ignore_read_only(true); |
| 1254 |
|
| 1255 |
ib::info(ER_IB_MSG_1098); |
| 1256 |
|
| 1257 |
bool create_new_temp_space = true; |
| 1258 |
|
| 1259 |
tmp_space->set_space_id(dict_sys_t::s_temp_space_id); |
| 1260 |
|
| 1261 |
RECOVERY_CRASH(100); |
| 1262 |
|
| 1263 |
dberr_t err = |
| 1264 |
tmp_space->check_file_spec(create_new_temp_space, 12 * 1024 * 1024); |
| 1265 |
|
| 1266 |
if (err == DB_FAIL) { |
| 1267 |
ib::error(ER_IB_MSG_1099, tmp_space->name()); |
| 1268 |
|
| 1269 |
err = DB_ERROR; |
| 1270 |
|
| 1271 |
} else if (err != DB_SUCCESS) { |
| 1272 |
ib::error(ER_IB_MSG_1100, tmp_space->name()); |
| 1273 |
|
| 1274 |
} else if ((err = tmp_space->open_or_create(true, create_new_db, |
| 1275 |
&sum_of_new_sizes, nullptr)) != |
| 1276 |
DB_SUCCESS) { |
| 1277 |
ib::error(ER_IB_MSG_1101, tmp_space->name()); |
| 1278 |
|
| 1279 |
} else { |
| 1280 |
mtr_t mtr; |
| 1281 |
page_no_t size = tmp_space->get_sum_of_sizes(); |
| 1282 |
|
| 1283 |
/* Open this shared temp tablespace in the fil_system so that |
| 1284 |
it stays open until shutdown. */ |
| 1285 |
if (fil_space_open(tmp_space->space_id())) { |
| 1286 |
/* Initialize the header page */ |
| 1287 |
mtr_start(&mtr); |
| 1288 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 1289 |
|
| 1290 |
fsp_header_init(tmp_space->space_id(), size, &mtr); |
| 1291 |
|
| 1292 |
mtr_commit(&mtr); |
| 1293 |
} else { |
| 1294 |
/* This file was just opened in the code above! */ |
| 1295 |
ib::error(ER_IB_MSG_1102, tmp_space->name()); |
| 1296 |
|
| 1297 |
err = DB_ERROR; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
return (err); |
| 1302 |
} |
| 1303 |
|
| 1304 |
/** Create SDI Indexes in system tablespace. */ |
| 1305 |
static void srv_create_sdi_indexes() { |
| 1306 |
btr_sdi_create_index(SYSTEM_TABLE_SPACE, false); |
| 1307 |
} |
| 1308 |
|
| 1309 |
/** Set state to indicate start of particular group of threads in InnoDB. */ |
| 1310 |
static inline void srv_start_state_set( |
| 1311 |
srv_start_state_t state) /*!< in: indicate current |
| 1312 |
state of thread startup */ |
| 1313 |
{ |
| 1314 |
srv_start_state |= state; |
| 1315 |
} |
| 1316 |
|
| 1317 |
/** Check if following group of threads is started. |
| 1318 |
@return true if started */ |
| 1319 |
static inline bool srv_start_state_is_set( |
| 1320 |
srv_start_state_t state) /*!< in: state to check for */ |
| 1321 |
{ |
| 1322 |
return (srv_start_state & state); |
| 1323 |
} |
| 1324 |
|
| 1325 |
struct Thread_to_stop { |
| 1326 |
/** Name of the thread, printed to the error log if we waited too |
| 1327 |
long (after 60 seconds and then every 60 seconds). */ |
| 1328 |
const char *m_name; |
| 1329 |
|
| 1330 |
/** Future which allows to check if given task is completed. */ |
| 1331 |
const IB_thread &m_thread; |
| 1332 |
|
| 1333 |
/** Function which can be called any number of times to wake |
| 1334 |
the possibly waiting thread, so it could exit. */ |
| 1335 |
std::function<void()> m_notify; |
| 1336 |
|
| 1337 |
/** Shutdown state in which we are waiting until thread is exited |
| 1338 |
(earlier we keep notifying but we don't require it to exit before |
| 1339 |
we may switch to the next state). */ |
| 1340 |
srv_shutdown_t m_wait_on_state; |
| 1341 |
}; |
| 1342 |
|
| 1343 |
static const Thread_to_stop threads_to_stop[]{ |
| 1344 |
{"lock_wait_timeout", srv_threads.m_lock_wait_timeout, |
| 1345 |
lock_set_timeout_event, SRV_SHUTDOWN_CLEANUP}, |
| 1346 |
|
| 1347 |
{"error_monitor", srv_threads.m_error_monitor, |
| 1348 |
[]() { os_event_set(srv_error_event); }, SRV_SHUTDOWN_CLEANUP}, |
| 1349 |
|
| 1350 |
{"monitor", srv_threads.m_monitor, |
| 1351 |
[]() { os_event_set(srv_monitor_event); }, SRV_SHUTDOWN_CLEANUP}, |
| 1352 |
|
| 1353 |
{"buf_dump", srv_threads.m_buf_dump, |
| 1354 |
[]() { os_event_set(srv_buf_dump_event); }, SRV_SHUTDOWN_CLEANUP}, |
| 1355 |
|
| 1356 |
{"buf_resize", srv_threads.m_buf_resize, |
| 1357 |
[]() { os_event_set(srv_buf_resize_event); }, SRV_SHUTDOWN_CLEANUP}, |
| 1358 |
|
| 1359 |
{"master", srv_threads.m_master, srv_wake_master_thread, |
| 1360 |
SRV_SHUTDOWN_MASTER_STOP}}; |
| 1361 |
|
| 1362 |
void srv_shutdown_exit_threads() { |
| 1363 |
srv_shutdown_state.store(SRV_SHUTDOWN_EXIT_THREADS); |
| 1364 |
|
| 1365 |
if (srv_start_state == SRV_START_STATE_NONE) { |
| 1366 |
return; |
| 1367 |
} |
| 1368 |
|
| 1369 |
uint32_t i; |
| 1370 |
|
| 1371 |
/* All threads end up waiting for certain events. Put those events |
| 1372 |
to the signaled state. Then the threads will exit themselves after |
| 1373 |
os_event_wait(). */ |
| 1374 |
for (i = 0; i < SHUTDOWN_SLEEP_ROUNDS; i++) { |
| 1375 |
/* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM |
| 1376 |
HERE OR EARLIER */ |
| 1377 |
|
| 1378 |
/* These threads normally finish when reaching SRV_SHUTDOWN_CLEANUP or |
| 1379 |
SRV_SHUTDOWN_MASTER_STOP state, which we might have jumped over. */ |
| 1380 |
for (const auto &thread_info : threads_to_stop) { |
| 1381 |
if (srv_thread_is_active(thread_info.m_thread)) { |
| 1382 |
thread_info.m_notify(); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (!srv_read_only_mode) { |
| 1387 |
if (srv_start_state_is_set(SRV_START_STATE_PURGE)) { |
| 1388 |
/* Wakeup purge threads. */ |
| 1389 |
srv_purge_wakeup(); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (srv_start_state_is_set(SRV_START_STATE_IO)) { |
| 1394 |
/* Exit the i/o threads */ |
| 1395 |
if (!srv_read_only_mode) { |
| 1396 |
if (recv_sys->flush_start != nullptr) { |
| 1397 |
os_event_set(recv_sys->flush_start); |
| 1398 |
} |
| 1399 |
if (recv_sys->flush_end != nullptr) { |
| 1400 |
os_event_set(recv_sys->flush_end); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
os_event_set(buf_flush_event); |
| 1405 |
|
| 1406 |
if (!buf_flush_page_cleaner_is_active() && os_aio_all_slots_free()) { |
| 1407 |
os_aio_wake_all_threads_at_shutdown(); |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (srv_thread_is_active(srv_threads.m_dict_stats)) { |
| 1412 |
os_event_set(dict_stats_event); |
| 1413 |
} |
| 1414 |
|
| 1415 |
/* Try to stop archiver threads. */ |
| 1416 |
arch_wake_threads(); |
| 1417 |
|
| 1418 |
if (log_sys != nullptr) { |
| 1419 |
/* Preserve the log threads for the 75% of the total |
| 1420 |
time we are waiting here until all threads are stopped. |
| 1421 |
This is because log threads are normally shut down at |
| 1422 |
the very end and we might need their help to stop other |
| 1423 |
threads. */ |
| 1424 |
if (!buf_flush_page_cleaner_is_active() || |
| 1425 |
i >= SHUTDOWN_SLEEP_ROUNDS * 0.75) { |
| 1426 |
log_stop_background_threads_nowait(*log_sys); |
| 1427 |
|
| 1428 |
} else { |
| 1429 |
/* Ensure log threads are working. The redo log is |
| 1430 |
like a blood, we need it for a lot of other systems |
| 1431 |
to work. Ensure the blood flows. */ |
| 1432 |
log_wake_threads(*log_sys); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
bool active = os_thread_any_active(); |
| 1437 |
|
| 1438 |
std::this_thread::sleep_for( |
| 1439 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 1440 |
|
| 1441 |
if (!active) { |
| 1442 |
break; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
if (i == SHUTDOWN_SLEEP_ROUNDS) { |
| 1447 |
ib::warn(ER_IB_MSG_1103, os_thread_count.load()); |
| 1448 |
|
| 1449 |
#ifdef UNIV_DEBUG |
| 1450 |
os_aio_print_pending_io(stderr); |
| 1451 |
ut_d(ut_error); |
| 1452 |
#endif /* UNIV_DEBUG */ |
| 1453 |
} else { |
| 1454 |
/* Reset the start state. */ |
| 1455 |
srv_start_state = SRV_START_STATE_NONE; |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
#ifdef UNIV_DEBUG |
| 1460 |
#define srv_init_abort(_db_err) \ |
| 1461 |
srv_init_abort_low(create_new_db, __FILE__, __LINE__, _db_err) |
| 1462 |
#else |
| 1463 |
#define srv_init_abort(_db_err) srv_init_abort_low(create_new_db, _db_err) |
| 1464 |
#endif /* UNIV_DEBUG */ |
| 1465 |
|
| 1466 |
/** Innobase start-up aborted. Perform cleanup actions. |
| 1467 |
@param[in] create_new_db true if new db is being created |
| 1468 |
@param[in] file File name |
| 1469 |
@param[in] line Line number |
| 1470 |
@param[in] err Reason for aborting InnoDB startup |
| 1471 |
@return DB_SUCCESS or error code. */ |
| 1472 |
static dberr_t srv_init_abort_low(bool create_new_db, |
| 1473 |
IF_DEBUG(const char *file, ulint line, ) |
| 1474 |
dberr_t err) { |
| 1475 |
std::ostringstream msg; |
| 1476 |
|
| 1477 |
#ifdef UNIV_DEBUG |
| 1478 |
msg << "at " << innobase_basename(file) << "[" << line << "] "; |
| 1479 |
#endif /* UNIV_DEBUG */ |
| 1480 |
|
| 1481 |
if (create_new_db) { |
| 1482 |
ib::error(ER_IB_MSG_1104, msg.str().c_str(), ut_strerr(err)); |
| 1483 |
} else { |
| 1484 |
ib::error(ER_IB_MSG_1105, msg.str().c_str(), ut_strerr(err)); |
| 1485 |
} |
| 1486 |
|
| 1487 |
clone_files_error(); |
| 1488 |
srv_shutdown_exit_threads(); |
| 1489 |
|
| 1490 |
return (err); |
| 1491 |
} |
| 1492 |
|
| 1493 |
/** Recreate REDO log files. |
| 1494 |
@param[in,out] flushed_lsn flushed_lsn |
| 1495 |
@return DB_SUCCESS or error code */ |
| 1496 |
static dberr_t recreate_redo_files(lsn_t &flushed_lsn) { |
| 1497 |
ut_d(log_sys->disable_redo_writes = true); |
| 1498 |
|
| 1499 |
/* Emit a message to the error log. */ |
| 1500 |
const auto target_size = log_sys->m_capacity.target_physical_capacity(); |
| 1501 |
const auto target_size_in_M = target_size / (1024 * 1024UL); |
| 1502 |
ib::info(ER_IB_MSG_LOG_FILES_UPGRADE, ulonglong{target_size_in_M}, |
| 1503 |
ulonglong{flushed_lsn}); |
| 1504 |
|
| 1505 |
RECOVERY_CRASH(5); |
| 1506 |
RECOVERY_CRASH(6); |
| 1507 |
ib::info(ER_IB_MSG_LOG_FILES_REWRITING); |
| 1508 |
|
| 1509 |
/* Remove all existing log files. */ |
| 1510 |
log_files_remove(*log_sys); |
| 1511 |
|
| 1512 |
log_sys_close(); |
| 1513 |
ut_a(log_sys == nullptr); |
| 1514 |
|
| 1515 |
/* The checkpoint_lsn found could be larger than flushed_lsn in the system |
| 1516 |
tables space in case the shutdown wasn't slow. In such case we should start |
| 1517 |
from an lsn at least equal to checkpoint_lsn as pages in the tablespace will |
| 1518 |
have lsns larger than flushed_lsn. */ |
| 1519 |
if (recv_sys->checkpoint_lsn != 0) { |
| 1520 |
ut_ad(flushed_lsn <= recv_sys->checkpoint_lsn); |
| 1521 |
flushed_lsn = std::max(flushed_lsn, recv_sys->checkpoint_lsn); |
| 1522 |
} |
| 1523 |
|
| 1524 |
/* This is to provide the property that data byte at given lsn never |
| 1525 |
changes and avoid the need to rewrite the block with flushed_lsn. */ |
| 1526 |
flushed_lsn = ut_uint64_align_up(flushed_lsn, OS_FILE_LOG_BLOCK_SIZE) + |
| 1527 |
LOG_BLOCK_HDR_SIZE; |
| 1528 |
|
| 1529 |
/* `true` parameter makes sure new files are created */ |
| 1530 |
dberr_t err = log_sys_init(true, flushed_lsn, flushed_lsn); |
| 1531 |
if (err != DB_SUCCESS) { |
| 1532 |
return err; |
| 1533 |
} |
| 1534 |
|
| 1535 |
ut_d(log_sys->disable_redo_writes = false); |
| 1536 |
|
| 1537 |
fil_open_system_tablespace_files(); |
| 1538 |
|
| 1539 |
return DB_SUCCESS; |
| 1540 |
} |
| 1541 |
|
| 1542 |
dberr_t srv_start(bool create_new_db) { |
| 1543 |
page_no_t sum_of_data_file_sizes; |
| 1544 |
page_no_t tablespace_size_in_header; |
| 1545 |
dberr_t err; |
| 1546 |
mtr_t mtr; |
| 1547 |
purge_pq_t *purge_queue; |
| 1548 |
|
| 1549 |
/* Reset the start state. */ |
| 1550 |
srv_start_state = SRV_START_STATE_NONE; |
| 1551 |
|
| 1552 |
#ifdef UNIV_LINUX |
| 1553 |
#ifdef HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE |
| 1554 |
ib::info(ER_IB_MSG_1107); |
| 1555 |
#else |
| 1556 |
ib::info(ER_IB_MSG_1108); |
| 1557 |
#endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE */ |
| 1558 |
#endif /* UNIV_LINUX */ |
| 1559 |
|
| 1560 |
static_assert(sizeof(ulint) == sizeof(void *), |
| 1561 |
"Size of InnoDB's ulint is not the same as size of void*. The " |
| 1562 |
"sizes should be the same so that on a 64-bit platforms you " |
| 1563 |
"can allocate more than 4 GB of memory."); |
| 1564 |
|
| 1565 |
if (srv_is_upgrade_mode) { |
| 1566 |
if (srv_force_recovery != 0) { |
| 1567 |
ib::error(ER_IB_MSG_1111); |
| 1568 |
return (srv_init_abort(DB_ERROR)); |
| 1569 |
} |
| 1570 |
if (srv_read_only_mode) { |
| 1571 |
ib::error(ER_IB_MSG_1110); |
| 1572 |
return (srv_init_abort(DB_ERROR)); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
#ifdef UNIV_DEBUG |
| 1577 |
ib::info(ER_IB_MSG_1112) << "!!!!!!!! UNIV_DEBUG switched on !!!!!!!!!"; |
| 1578 |
#endif |
| 1579 |
|
| 1580 |
#ifdef UNIV_IBUF_DEBUG |
| 1581 |
ib::info(ER_IB_MSG_1113) << "!!!!!!!! UNIV_IBUF_DEBUG switched on !!!!!!!!!"; |
| 1582 |
#ifdef UNIV_IBUF_COUNT_DEBUG |
| 1583 |
ib::info(ER_IB_MSG_1114) |
| 1584 |
<< "!!!!!!!! UNIV_IBUF_COUNT_DEBUG switched on !!!!!!!!!"; |
| 1585 |
ib::error(ER_IB_MSG_1115) |
| 1586 |
<< "Crash recovery will fail with UNIV_IBUF_COUNT_DEBUG"; |
| 1587 |
#endif |
| 1588 |
#endif |
| 1589 |
|
| 1590 |
#ifdef UNIV_LOG_LSN_DEBUG |
| 1591 |
ib::info(ER_IB_MSG_1116) |
| 1592 |
<< "!!!!!!!! UNIV_LOG_LSN_DEBUG switched on !!!!!!!!!"; |
| 1593 |
#endif /* UNIV_LOG_LSN_DEBUG */ |
| 1594 |
|
| 1595 |
#if defined(COMPILER_HINTS_ENABLED) |
| 1596 |
ib::info(ER_IB_MSG_1117) << "Compiler hints enabled."; |
| 1597 |
#endif /* defined(COMPILER_HINTS_ENABLED) */ |
| 1598 |
|
| 1599 |
ib::info(ER_IB_MSG_1119) << MUTEX_TYPE; |
| 1600 |
ib::info(ER_IB_MSG_1120) << IB_MEMORY_BARRIER_STARTUP_MSG; |
| 1601 |
|
| 1602 |
if (srv_force_recovery > 0) { |
| 1603 |
ib::info(ER_IB_MSG_1121) << "!!! innodb_force_recovery is set to " |
| 1604 |
<< srv_force_recovery << " !!!"; |
| 1605 |
} |
| 1606 |
|
| 1607 |
#ifndef HAVE_MEMORY_BARRIER |
| 1608 |
#if defined __i386__ || defined __x86_64__ || defined _M_IX86 || \ |
| 1609 |
defined _M_X64 || defined _WIN32 |
| 1610 |
#else |
| 1611 |
ib::warn(ER_IB_MSG_1122); |
| 1612 |
#endif /* IA32 or AMD64 */ |
| 1613 |
#endif /* HAVE_MEMORY_BARRIER */ |
| 1614 |
|
| 1615 |
#ifdef UNIV_ZIP_DEBUG |
| 1616 |
ib::info(ER_IB_MSG_1123, ZLIB_VERSION) << " with validation"; |
| 1617 |
#else |
| 1618 |
ib::info(ER_IB_MSG_1123, ZLIB_VERSION); |
| 1619 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1620 |
|
| 1621 |
#ifdef UNIV_ZIP_COPY |
| 1622 |
ib::info(ER_IB_MSG_1124) << "and extra copying"; |
| 1623 |
#endif /* UNIV_ZIP_COPY */ |
| 1624 |
|
| 1625 |
/* Since InnoDB does not currently clean up all its internal data |
| 1626 |
structures in MySQL Embedded Server Library server_end(), we |
| 1627 |
print an error message if someone tries to start up InnoDB a |
| 1628 |
second time during the process lifetime. */ |
| 1629 |
|
| 1630 |
if (srv_start_has_been_called) { |
| 1631 |
ib::error(ER_IB_MSG_1125); |
| 1632 |
} |
| 1633 |
|
| 1634 |
srv_start_has_been_called = true; |
| 1635 |
|
| 1636 |
srv_is_being_started = true; |
| 1637 |
|
| 1638 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 1639 |
/* Register performance schema stages before any real work has been |
| 1640 |
started which may need to be instrumented. */ |
| 1641 |
mysql_stage_register("innodb", srv_stages, UT_ARR_SIZE(srv_stages)); |
| 1642 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 1643 |
|
| 1644 |
/* Switch latching order checks on in sync0debug.cc, if |
| 1645 |
--innodb-sync-debug=false (default) */ |
| 1646 |
ut_d(sync_check_enable()); |
| 1647 |
|
| 1648 |
srv_boot(); |
| 1649 |
|
| 1650 |
ib::info(ER_IB_MSG_1126) |
| 1651 |
<< "Using " |
| 1652 |
<< (ut_crc32_cpu_enabled ? (ut_poly_mul_cpu_enabled |
| 1653 |
? "hardware accelerated crc32 and " |
| 1654 |
"polynomial multiplication." |
| 1655 |
: "hardware accelerated crc32 and " |
| 1656 |
"software polynomial multiplication.") |
| 1657 |
: "software crc32."); |
| 1658 |
|
| 1659 |
os_create_block_cache(); |
| 1660 |
|
| 1661 |
fil_init(innobase_get_open_files_limit()); |
| 1662 |
|
| 1663 |
/* This is the default directory for IBD and IBU files. Put it first |
| 1664 |
in the list of known directories. */ |
| 1665 |
fil_set_scan_dir(MySQL_datadir_path.path()); |
| 1666 |
|
| 1667 |
/* Add --innodb-data-home-dir as a known location for IBD and IBU files |
| 1668 |
if it is not already there. */ |
| 1669 |
ut_ad(srv_data_home != nullptr && *srv_data_home != '\0'); |
| 1670 |
fil_set_scan_dir(Fil_path::remove_quotes(srv_data_home)); |
| 1671 |
|
| 1672 |
/* Add --innodb-directories as known locations for IBD and IBU files. */ |
| 1673 |
if (srv_innodb_directories != nullptr && *srv_innodb_directories != 0) { |
| 1674 |
fil_set_scan_dirs(Fil_path::remove_quotes(srv_innodb_directories)); |
| 1675 |
} |
| 1676 |
|
| 1677 |
/* Note whether the undo path is different (not the same or under) |
| 1678 |
from all other known directories. If so, this will allow us to keep |
| 1679 |
IBD files out of this unique undo location.*/ |
| 1680 |
MySQL_undo_path_is_unique = !fil_path_is_known(MySQL_undo_path.path()); |
| 1681 |
|
| 1682 |
/* For the purpose of file discovery at startup, we need to scan |
| 1683 |
--innodb-undo-directory also if it is different from the locations above. */ |
| 1684 |
if (MySQL_undo_path_is_unique) { |
| 1685 |
fil_set_scan_dir(Fil_path::remove_quotes(MySQL_undo_path)); |
| 1686 |
} |
| 1687 |
|
| 1688 |
ib::info(ER_IB_MSG_378) << "Directories to scan '" << fil_get_dirs() << "'"; |
| 1689 |
|
| 1690 |
/* Must replace clone files before scanning directories. When |
| 1691 |
clone replaces current database, cloned files are moved to data files |
| 1692 |
at this stage. */ |
| 1693 |
err = clone_init(); |
| 1694 |
|
| 1695 |
if (err != DB_SUCCESS) { |
| 1696 |
return (srv_init_abort(err)); |
| 1697 |
} |
| 1698 |
|
| 1699 |
err = fil_scan_for_tablespaces(); |
| 1700 |
|
| 1701 |
if (err != DB_SUCCESS) { |
| 1702 |
return (srv_init_abort(err)); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (!srv_read_only_mode) { |
| 1706 |
mutex_create(LATCH_ID_SRV_MONITOR_FILE, &srv_monitor_file_mutex); |
| 1707 |
|
| 1708 |
if (srv_innodb_status) { |
| 1709 |
srv_monitor_file_name = static_cast<char *>(ut::malloc_withkey( |
| 1710 |
UT_NEW_THIS_FILE_PSI_KEY, |
| 1711 |
MySQL_datadir_path.len() + 20 + sizeof "/innodb_status.")); |
| 1712 |
|
| 1713 |
sprintf(srv_monitor_file_name, "%s/innodb_status." ULINTPF, |
| 1714 |
static_cast<const char *>(MySQL_datadir_path), |
| 1715 |
os_proc_get_number()); |
| 1716 |
|
| 1717 |
srv_monitor_file = fopen(srv_monitor_file_name, "w+"); |
| 1718 |
|
| 1719 |
if (!srv_monitor_file) { |
| 1720 |
ib::error(ER_IB_MSG_1127, srv_monitor_file_name, strerror(errno)); |
| 1721 |
|
| 1722 |
return (srv_init_abort(DB_ERROR)); |
| 1723 |
} |
| 1724 |
} else { |
| 1725 |
srv_monitor_file_name = nullptr; |
| 1726 |
srv_monitor_file = os_file_create_tmpfile(); |
| 1727 |
|
| 1728 |
if (!srv_monitor_file) { |
| 1729 |
return (srv_init_abort(DB_ERROR)); |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
mutex_create(LATCH_ID_SRV_MISC_TMPFILE, &srv_misc_tmpfile_mutex); |
| 1734 |
|
| 1735 |
srv_misc_tmpfile = os_file_create_tmpfile(); |
| 1736 |
|
| 1737 |
if (!srv_misc_tmpfile) { |
| 1738 |
return (srv_init_abort(DB_ERROR)); |
| 1739 |
} |
| 1740 |
} |
| 1741 |
|
| 1742 |
if (!os_aio_init(srv_n_read_io_threads, srv_n_write_io_threads)) { |
| 1743 |
ib::error(ER_IB_MSG_1129); |
| 1744 |
|
| 1745 |
return (srv_init_abort(DB_ERROR)); |
| 1746 |
} |
| 1747 |
|
| 1748 |
double size; |
| 1749 |
char unit; |
| 1750 |
|
| 1751 |
if (srv_buf_pool_size >= 1024 * 1024 * 1024) { |
| 1752 |
size = ((double)srv_buf_pool_size) / (1024 * 1024 * 1024); |
| 1753 |
unit = 'G'; |
| 1754 |
} else { |
| 1755 |
size = ((double)srv_buf_pool_size) / (1024 * 1024); |
| 1756 |
unit = 'M'; |
| 1757 |
} |
| 1758 |
|
| 1759 |
double chunk_size; |
| 1760 |
char chunk_unit; |
| 1761 |
|
| 1762 |
if (srv_buf_pool_chunk_unit >= 1024 * 1024 * 1024) { |
| 1763 |
chunk_size = srv_buf_pool_chunk_unit / 1024.0 / 1024 / 1024; |
| 1764 |
chunk_unit = 'G'; |
| 1765 |
} else { |
| 1766 |
chunk_size = srv_buf_pool_chunk_unit / 1024.0 / 1024; |
| 1767 |
chunk_unit = 'M'; |
| 1768 |
} |
| 1769 |
|
| 1770 |
ib::info(ER_IB_MSG_1130, size, unit, srv_buf_pool_instances, chunk_size, |
| 1771 |
chunk_unit); |
| 1772 |
|
| 1773 |
err = buf_pool_init(srv_buf_pool_size, srv_buf_pool_instances); |
| 1774 |
|
| 1775 |
if (err != DB_SUCCESS) { |
| 1776 |
ib::error(ER_IB_MSG_1131); |
| 1777 |
|
| 1778 |
return (srv_init_abort(DB_ERROR)); |
| 1779 |
} |
| 1780 |
|
| 1781 |
ib::info(ER_IB_MSG_1132); |
| 1782 |
|
| 1783 |
#ifdef UNIV_DEBUG |
| 1784 |
/* We have observed deadlocks with a 5MB buffer pool but |
| 1785 |
the actual lower limit could very well be a little higher. */ |
| 1786 |
|
| 1787 |
if (srv_buf_pool_size <= 5 * 1024 * 1024) { |
| 1788 |
ib::info(ER_IB_MSG_1133, ulonglong{srv_buf_pool_size / 1024 / 1024}); |
| 1789 |
} |
| 1790 |
#endif /* UNIV_DEBUG */ |
| 1791 |
|
| 1792 |
fsp_init(); |
| 1793 |
pars_init(); |
| 1794 |
recv_sys_create(); |
| 1795 |
recv_sys_init(); |
| 1796 |
trx_sys_create(); |
| 1797 |
lock_sys_create(srv_lock_table_size); |
| 1798 |
|
| 1799 |
/* Create i/o-handler threads: */ |
| 1800 |
os_aio_start_threads(); |
| 1801 |
|
| 1802 |
/* Even in read-only mode there could be flush job generated by |
| 1803 |
intrinsic table operations. */ |
| 1804 |
buf_flush_page_cleaner_init(); |
| 1805 |
|
| 1806 |
srv_start_state_set(SRV_START_STATE_IO); |
| 1807 |
|
| 1808 |
srv_startup_is_before_trx_rollback_phase = !create_new_db; |
| 1809 |
|
| 1810 |
if (create_new_db) { |
| 1811 |
recv_sys_free(); |
| 1812 |
} |
| 1813 |
|
| 1814 |
/* Open or create the data files. */ |
| 1815 |
page_no_t sum_of_new_sizes; |
| 1816 |
lsn_t flushed_lsn; |
| 1817 |
|
| 1818 |
err = srv_sys_space.open_or_create(false, create_new_db, &sum_of_new_sizes, |
| 1819 |
&flushed_lsn); |
| 1820 |
|
| 1821 |
switch (err) { |
| 1822 |
case DB_SUCCESS: |
| 1823 |
break; |
| 1824 |
case DB_CANNOT_OPEN_FILE: |
| 1825 |
ib::error(ER_IB_MSG_1134); |
| 1826 |
[[fallthrough]]; |
| 1827 |
default: |
| 1828 |
|
| 1829 |
/* Other errors might come from |
| 1830 |
Datafile::validate_first_page() */ |
| 1831 |
|
| 1832 |
return (srv_init_abort(err)); |
| 1833 |
} |
| 1834 |
|
| 1835 |
if (flushed_lsn < LOG_START_LSN) { |
| 1836 |
ut_ad(!create_new_db); |
| 1837 |
/* Data directory hasn't been initialized yet. */ |
| 1838 |
ib::error(ER_IB_MSG_DATA_DIRECTORY_NOT_INITIALIZED_OR_CORRUPTED); |
| 1839 |
return srv_init_abort(DB_ERROR); |
| 1840 |
} |
| 1841 |
|
| 1842 |
/* FIXME: This can be done earlier, but we now have to wait for |
| 1843 |
checking of system tablespace. */ |
| 1844 |
dict_persist_init(); |
| 1845 |
|
| 1846 |
mtr_t::s_logging.init(); |
| 1847 |
|
| 1848 |
if (dblwr::is_enabled() && ((err = dblwr::open()) != DB_SUCCESS)) { |
| 1849 |
return srv_init_abort(err); |
| 1850 |
} |
| 1851 |
|
| 1852 |
lsn_t new_files_lsn; |
| 1853 |
|
| 1854 |
err = log_sys_init(create_new_db, flushed_lsn, new_files_lsn); |
| 1855 |
|
| 1856 |
if (err != DB_SUCCESS) { |
| 1857 |
return srv_init_abort(err); |
| 1858 |
} |
| 1859 |
|
| 1860 |
ut_a(log_sys != nullptr); |
| 1861 |
|
| 1862 |
arch_init(); |
| 1863 |
|
| 1864 |
if (create_new_db) { |
| 1865 |
ut_a(buf_are_flush_lists_empty_validate()); |
| 1866 |
|
| 1867 |
ut_a(!srv_read_only_mode); |
| 1868 |
|
| 1869 |
ut_a(log_sys->last_checkpoint_lsn.load() == |
| 1870 |
LOG_START_LSN + LOG_BLOCK_HDR_SIZE); |
| 1871 |
|
| 1872 |
ut_a(new_files_lsn == LOG_START_LSN + LOG_BLOCK_HDR_SIZE); |
| 1873 |
|
| 1874 |
err = log_start(*log_sys, new_files_lsn, new_files_lsn); |
| 1875 |
|
| 1876 |
if (err != DB_SUCCESS) { |
| 1877 |
return srv_init_abort(err); |
| 1878 |
} |
| 1879 |
|
| 1880 |
log_start_background_threads(*log_sys); |
| 1881 |
|
| 1882 |
err = srv_undo_tablespaces_init(true); |
| 1883 |
|
| 1884 |
if (err != DB_SUCCESS) { |
| 1885 |
return (srv_init_abort(err)); |
| 1886 |
} |
| 1887 |
|
| 1888 |
mtr_start(&mtr); |
| 1889 |
|
| 1890 |
bool ret = fsp_header_init(0, sum_of_new_sizes, &mtr); |
| 1891 |
|
| 1892 |
mtr_commit(&mtr); |
| 1893 |
|
| 1894 |
if (!ret) { |
| 1895 |
return (srv_init_abort(DB_ERROR)); |
| 1896 |
} |
| 1897 |
|
| 1898 |
/* To maintain backward compatibility we create only |
| 1899 |
the first rollback segment before the double write buffer. |
| 1900 |
All the remaining rollback segments will be created later, |
| 1901 |
after the double write buffers haves been created. */ |
| 1902 |
trx_sys_create_sys_pages(); |
| 1903 |
|
| 1904 |
trx_purge_sys_mem_create(); |
| 1905 |
|
| 1906 |
purge_queue = trx_sys_init_at_db_start(); |
| 1907 |
|
| 1908 |
/* The purge system needs to create the purge view and |
| 1909 |
therefore requires that the trx_sys is inited. */ |
| 1910 |
|
| 1911 |
trx_purge_sys_initialize(srv_threads.m_purge_workers_n, purge_queue); |
| 1912 |
|
| 1913 |
err = dict_create(); |
| 1914 |
|
| 1915 |
if (err != DB_SUCCESS) { |
| 1916 |
return (srv_init_abort(err)); |
| 1917 |
} |
| 1918 |
|
| 1919 |
srv_create_sdi_indexes(); |
| 1920 |
|
| 1921 |
/* We always create the legacy double write buffer to preserve the |
| 1922 |
expected page ordering of the system tablespace. |
| 1923 |
FIXME: Try and remove this requirement. */ |
| 1924 |
err = dblwr::v1::create(); |
| 1925 |
|
| 1926 |
if (err != DB_SUCCESS) { |
| 1927 |
return srv_init_abort(err); |
| 1928 |
} |
| 1929 |
|
| 1930 |
} else { |
| 1931 |
/* Load the reserved boundaries of the legacy dblwr buffer, this is |
| 1932 |
required to check for stray reads and writes trying to access this |
| 1933 |
reserved region in the sys tablespace. |
| 1934 |
FIXME: Try and remove this requirement. */ |
| 1935 |
err = dblwr::v1::init(); |
| 1936 |
|
| 1937 |
if (err != DB_SUCCESS) { |
| 1938 |
return srv_init_abort(err); |
| 1939 |
} |
| 1940 |
|
| 1941 |
/* Invalidate the buffer pool to ensure that we reread |
| 1942 |
the page that we read above, during recovery. |
| 1943 |
Note that this is not as heavy weight as it seems. At |
| 1944 |
this point there will be only ONE page in the buf_LRU |
| 1945 |
and there must be no page in the buf_flush list. */ |
| 1946 |
buf_pool_invalidate(); |
| 1947 |
|
| 1948 |
/* Open all data files in the system tablespace: |
| 1949 |
we keep them open until database shutdown. */ |
| 1950 |
fil_open_system_tablespace_files(); |
| 1951 |
|
| 1952 |
/* We always try to do a recovery, even if the database had |
| 1953 |
been shut down normally: this is the normal startup path */ |
| 1954 |
RECOVERY_CRASH(1); |
| 1955 |
|
| 1956 |
if (new_files_lsn != 0) { |
| 1957 |
/* This means that either no log files have been found |
| 1958 |
or the existing log files were marked as uninitialized. */ |
| 1959 |
flushed_lsn = new_files_lsn; |
| 1960 |
} |
| 1961 |
|
| 1962 |
ut_a(log_sys->m_format <= Log_format::CURRENT); |
| 1963 |
|
| 1964 |
const bool log_upgrade = log_sys->m_format < Log_format::CURRENT; |
| 1965 |
|
| 1966 |
if (log_upgrade) { |
| 1967 |
if (srv_read_only_mode) { |
| 1968 |
ib::error(ER_IB_MSG_LOG_UPGRADE_IN_READ_ONLY_MODE, |
| 1969 |
ulong{to_int(log_sys->m_format)}); |
| 1970 |
return srv_init_abort(DB_ERROR); |
| 1971 |
} |
| 1972 |
|
| 1973 |
/* Check if the redo log from an older known redo log |
| 1974 |
version is from a clean shutdown. */ |
| 1975 |
err = recv_verify_log_is_clean_pre_8_0_30(*log_sys); |
| 1976 |
if (err != DB_SUCCESS) { |
| 1977 |
return srv_init_abort(err); |
| 1978 |
} |
| 1979 |
|
| 1980 |
/* Redo logs are clean. We need to recreate REDO files */ |
| 1981 |
err = recreate_redo_files(flushed_lsn); |
| 1982 |
if (err != DB_SUCCESS) { |
| 1983 |
return srv_init_abort(err); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
err = recv_recovery_from_checkpoint_start(*log_sys, flushed_lsn); |
| 1988 |
if (err != DB_SUCCESS) { |
| 1989 |
return srv_init_abort(err); |
| 1990 |
} |
| 1991 |
|
| 1992 |
if (err == DB_SUCCESS) { |
| 1993 |
arch_page_sys->post_recovery_init(); |
| 1994 |
|
| 1995 |
/* Initialize the change buffer. */ |
| 1996 |
err = dict_boot(); |
| 1997 |
} |
| 1998 |
|
| 1999 |
if (err != DB_SUCCESS) { |
| 2000 |
return (srv_init_abort(err)); |
| 2001 |
} |
| 2002 |
|
| 2003 |
ut_ad(clone_check_recovery_crashpoint(recv_sys->is_cloned_db)); |
| 2004 |
|
| 2005 |
const bool redo_writes_allowed = !srv_read_only_mode; |
| 2006 |
|
| 2007 |
ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO || !redo_writes_allowed); |
| 2008 |
|
| 2009 |
if (redo_writes_allowed) { |
| 2010 |
/* We need to start log threads now, because recovery |
| 2011 |
could result in execution of ibuf merges. These merges |
| 2012 |
could result in new redo records. In the read-only mode |
| 2013 |
we do not need log threads, because we disallow new redo |
| 2014 |
records in such mode. If upgrade was forced, or the data |
| 2015 |
directory was cloned, we will start redo threads later. */ |
| 2016 |
log_start_background_threads(*log_sys); |
| 2017 |
} |
| 2018 |
|
| 2019 |
if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) { |
| 2020 |
/* Apply the hashed log records to the |
| 2021 |
respective file pages, for the last batch of |
| 2022 |
recv_group_scan_log_recs(). */ |
| 2023 |
|
| 2024 |
RECOVERY_CRASH(2); |
| 2025 |
|
| 2026 |
/* Don't allow IBUF operations for cloned database |
| 2027 |
recovery as it would add extra redo log and we may |
| 2028 |
not have enough margin. |
| 2029 |
|
| 2030 |
Don't allow IBUF operations when redo is written |
| 2031 |
in the older format than the current, because we |
| 2032 |
would write new redo records in the current fmt, |
| 2033 |
and end up with file in both formats = invalid. */ |
| 2034 |
|
| 2035 |
err = recv_apply_hashed_log_recs(*log_sys, |
| 2036 |
!recv_sys->is_cloned_db && !log_upgrade); |
| 2037 |
|
| 2038 |
if (recv_sys->found_corrupt_log || err != DB_SUCCESS) { |
| 2039 |
err = DB_ERROR; |
| 2040 |
return (srv_init_abort(err)); |
| 2041 |
} |
| 2042 |
|
| 2043 |
DBUG_PRINT("ib_log", ("apply completed")); |
| 2044 |
|
| 2045 |
/* Check and print if there were any tablespaces |
| 2046 |
which had redo log records but we couldn't apply |
| 2047 |
them because the filenames were missing. */ |
| 2048 |
|
| 2049 |
/* Recovery complete, start verifying the |
| 2050 |
page LSN on read. */ |
| 2051 |
recv_lsn_checks_on = true; |
| 2052 |
} |
| 2053 |
|
| 2054 |
/* We have gone through the redo log, now check if all the |
| 2055 |
tablespaces were found and recovered. */ |
| 2056 |
|
| 2057 |
if (srv_force_recovery == 0 && fil_check_missing_tablespaces()) { |
| 2058 |
ib::error(ER_IB_MSG_1139); |
| 2059 |
RECOVERY_CRASH(3); |
| 2060 |
|
| 2061 |
/* Set the abort flag to true. */ |
| 2062 |
auto p = recv_recovery_from_checkpoint_finish(true); |
| 2063 |
|
| 2064 |
ut_a(p == nullptr); |
| 2065 |
|
| 2066 |
return (srv_init_abort(DB_ERROR)); |
| 2067 |
} |
| 2068 |
|
| 2069 |
/* We have successfully recovered from the redo log. The |
| 2070 |
data dictionary should now be readable. */ |
| 2071 |
|
| 2072 |
if (recv_sys->found_corrupt_log) { |
| 2073 |
ib::warn(ER_IB_MSG_RECOVERY_CORRUPT); |
| 2074 |
} |
| 2075 |
|
| 2076 |
if (!srv_force_recovery && !srv_read_only_mode) { |
| 2077 |
buf_flush_sync_all_buf_pools(); |
| 2078 |
} |
| 2079 |
|
| 2080 |
RECOVERY_CRASH(3); |
| 2081 |
|
| 2082 |
auto *dict_metadata = recv_recovery_from_checkpoint_finish(false); |
| 2083 |
ut_a(dict_metadata != nullptr); |
| 2084 |
|
| 2085 |
/* We need to save the dynamic metadata collected from redo log to DD |
| 2086 |
buffer table here. This is to make sure that the dynamic metadata is not |
| 2087 |
lost by any future checkpoint. Since DD and data dictionary in memory |
| 2088 |
objects are not fully initialized at this point, the usual mechanism to |
| 2089 |
persist dynamic metadata at checkpoint wouldn't work. */ |
| 2090 |
|
| 2091 |
DBUG_EXECUTE_IF("log_first_rec_group_test", { |
| 2092 |
const lsn_t end_lsn = mtr_commit_mlog_test(); |
| 2093 |
log_write_up_to(*log_sys, end_lsn, true); |
| 2094 |
DBUG_SUICIDE(); |
| 2095 |
}); |
| 2096 |
|
| 2097 |
if (!recv_sys->is_cloned_db && !dict_metadata->empty()) { |
| 2098 |
ut_a(redo_writes_allowed); |
| 2099 |
|
| 2100 |
/* Open this table in case dict_metadata should be applied to this |
| 2101 |
table before checkpoint. And because DD is not fully up yet, the table |
| 2102 |
can be opened by internal APIs. */ |
| 2103 |
|
| 2104 |
fil_space_t *space = |
| 2105 |
fil_space_acquire_silent(dict_sys_t::s_dict_space_id); |
| 2106 |
if (space == nullptr) { |
| 2107 |
dberr_t error = |
| 2108 |
fil_ibd_open(true, FIL_TYPE_TABLESPACE, dict_sys_t::s_dict_space_id, |
| 2109 |
predefined_flags, dict_sys_t::s_dd_space_name, |
| 2110 |
dict_sys_t::s_dd_space_file_name, true, false); |
| 2111 |
if (error != DB_SUCCESS) { |
| 2112 |
ib::error(ER_IB_MSG_1142); |
| 2113 |
return (srv_init_abort(DB_ERROR)); |
| 2114 |
} |
| 2115 |
} else { |
| 2116 |
fil_space_release(space); |
| 2117 |
} |
| 2118 |
|
| 2119 |
dict_persist->table_buffer = |
| 2120 |
ut::new_withkey<DDTableBuffer>(UT_NEW_THIS_FILE_PSI_KEY); |
| 2121 |
/* We write redo log here. We assume that there should be enough room in |
| 2122 |
log files, supposing log_free_check() works fine before crash. */ |
| 2123 |
dict_metadata->store(); |
| 2124 |
|
| 2125 |
/* Flush logs to persist the changes. */ |
| 2126 |
log_buffer_flush_to_disk(*log_sys); |
| 2127 |
} |
| 2128 |
ut::delete_(dict_metadata); |
| 2129 |
|
| 2130 |
RECOVERY_CRASH(4); |
| 2131 |
|
| 2132 |
log_sys->m_allow_checkpoints.store(true, std::memory_order_release); |
| 2133 |
|
| 2134 |
if (recv_sys->is_cloned_db || recv_sys->is_meb_db) { |
| 2135 |
buf_pool_wait_for_no_pending_io(); |
| 2136 |
|
| 2137 |
/* Reset creator for log */ |
| 2138 |
|
| 2139 |
if (redo_writes_allowed) { |
| 2140 |
log_stop_background_threads(*log_sys); |
| 2141 |
} |
| 2142 |
|
| 2143 |
ut_ad(buf_pool_pending_io_reads_count() == 0); |
| 2144 |
|
| 2145 |
err = log_files_reset_creator_and_set_full(*log_sys); |
| 2146 |
if (err != DB_SUCCESS) { |
| 2147 |
return srv_init_abort(err); |
| 2148 |
} |
| 2149 |
|
| 2150 |
log_start_background_threads(*log_sys); |
| 2151 |
|
| 2152 |
} else { |
| 2153 |
ut_a(redo_writes_allowed || srv_read_only_mode); |
| 2154 |
} |
| 2155 |
|
| 2156 |
if (sum_of_new_sizes > 0) { |
| 2157 |
ut_a(!srv_read_only_mode); |
| 2158 |
|
| 2159 |
/* New data file(s) were added */ |
| 2160 |
mtr_start(&mtr); |
| 2161 |
|
| 2162 |
fsp_header_inc_size(0, sum_of_new_sizes, &mtr); |
| 2163 |
|
| 2164 |
mtr_commit(&mtr); |
| 2165 |
|
| 2166 |
/* Immediately write the log record about |
| 2167 |
increased tablespace size to disk, so that it |
| 2168 |
is durable even if mysqld would crash |
| 2169 |
quickly */ |
| 2170 |
|
| 2171 |
log_buffer_flush_to_disk(*log_sys); |
| 2172 |
} |
| 2173 |
|
| 2174 |
err = srv_undo_tablespaces_init(false); |
| 2175 |
|
| 2176 |
if (err != DB_SUCCESS && srv_force_recovery < SRV_FORCE_NO_UNDO_LOG_SCAN) { |
| 2177 |
return (srv_init_abort(err)); |
| 2178 |
} |
| 2179 |
|
| 2180 |
trx_purge_sys_mem_create(); |
| 2181 |
|
| 2182 |
/* The purge system needs to create the purge view and |
| 2183 |
therefore requires that the trx_sys is inited. */ |
| 2184 |
purge_queue = trx_sys_init_at_db_start(); |
| 2185 |
|
| 2186 |
if (srv_is_upgrade_mode) { |
| 2187 |
if (!purge_queue->empty()) { |
| 2188 |
ib::info(ER_IB_MSG_1144); |
| 2189 |
srv_upgrade_old_undo_found = true; |
| 2190 |
} |
| 2191 |
/* Either the old or new undo tablespaces will |
| 2192 |
be deleted later depending on the value of |
| 2193 |
'failed_upgrade' in dd_upgrade_finish(). */ |
| 2194 |
} else { |
| 2195 |
/* New undo tablespaces have been created. |
| 2196 |
Delete the old undo tablespaces and the references |
| 2197 |
to them in the TRX_SYS page. */ |
| 2198 |
srv_undo_tablespaces_upgrade(); |
| 2199 |
} |
| 2200 |
|
| 2201 |
DBUG_EXECUTE_IF("check_no_undo", ut_ad(purge_queue->empty());); |
| 2202 |
|
| 2203 |
/* The purge system needs to create the purge view and |
| 2204 |
therefore requires that the trx_sys and trx lists were |
| 2205 |
initialized in trx_sys_init_at_db_start(). */ |
| 2206 |
trx_purge_sys_initialize(srv_threads.m_purge_workers_n, purge_queue); |
| 2207 |
} |
| 2208 |
|
| 2209 |
/* Open temp-tablespace and keep it open until shutdown. */ |
| 2210 |
err = srv_open_tmp_tablespace(create_new_db, &srv_tmp_space); |
| 2211 |
if (err != DB_SUCCESS) { |
| 2212 |
return (srv_init_abort(err)); |
| 2213 |
} |
| 2214 |
|
| 2215 |
err = ibt::open_or_create(create_new_db); |
| 2216 |
if (err != DB_SUCCESS) { |
| 2217 |
return (srv_init_abort(err)); |
| 2218 |
} |
| 2219 |
|
| 2220 |
/* Here the double write buffer has already been created and so |
| 2221 |
any new rollback segments will be allocated after the double |
| 2222 |
write buffer. The default segment should already exist. |
| 2223 |
We create the new segments only if it's a new database or |
| 2224 |
the database was shutdown cleanly. */ |
| 2225 |
|
| 2226 |
/* Note: When creating the extra rollback segments during an upgrade |
| 2227 |
we violate the latching order, even if the change buffer is empty. |
| 2228 |
We make an exception in sync0sync.cc and check srv_is_being_started |
| 2229 |
for that violation. It cannot create a deadlock because we are still |
| 2230 |
running in single threaded mode essentially. Only the IO threads |
| 2231 |
should be running at this stage. */ |
| 2232 |
|
| 2233 |
ut_a(srv_rollback_segments > 0); |
| 2234 |
ut_a(srv_rollback_segments <= TRX_SYS_N_RSEGS); |
| 2235 |
|
| 2236 |
/* Make sure there are enough rollback segments in each tablespace |
| 2237 |
and that each rollback segment has an associated memory object. |
| 2238 |
If any of these rollback segments contain undo logs, load them into |
| 2239 |
the purge queue */ |
| 2240 |
if (!trx_rseg_adjust_rollback_segments(srv_rollback_segments)) { |
| 2241 |
return (srv_init_abort(DB_ERROR)); |
| 2242 |
} |
| 2243 |
|
| 2244 |
/* Any undo tablespaces under construction are now fully built |
| 2245 |
with all needed rsegs. Delete the trunc.log files and clear the |
| 2246 |
construction list. */ |
| 2247 |
srv_undo_tablespaces_mark_construction_done(); |
| 2248 |
|
| 2249 |
/* Now that all rsegs are ready for use, make them active. */ |
| 2250 |
undo::spaces->s_lock(); |
| 2251 |
for (auto undo_space : undo::spaces->m_spaces) { |
| 2252 |
if (!undo_space->is_empty()) { |
| 2253 |
undo_space->set_active(); |
| 2254 |
} |
| 2255 |
} |
| 2256 |
undo::spaces->s_unlock(); |
| 2257 |
|
| 2258 |
/* Undo Tablespaces and Rollback Segments are ready. */ |
| 2259 |
srv_startup_is_before_trx_rollback_phase = false; |
| 2260 |
|
| 2261 |
if (!srv_read_only_mode) { |
| 2262 |
if (create_new_db) { |
| 2263 |
srv_buffer_pool_load_at_startup = false; |
| 2264 |
} |
| 2265 |
|
| 2266 |
/* Create the thread which watches the timeouts |
| 2267 |
for lock waits */ |
| 2268 |
srv_threads.m_lock_wait_timeout = os_thread_create( |
| 2269 |
srv_lock_timeout_thread_key, 0, lock_wait_timeout_thread); |
| 2270 |
|
| 2271 |
srv_threads.m_lock_wait_timeout.start(); |
| 2272 |
|
| 2273 |
/* Create the thread which warns of long semaphore waits */ |
| 2274 |
srv_threads.m_error_monitor = os_thread_create(srv_error_monitor_thread_key, |
| 2275 |
0, srv_error_monitor_thread); |
| 2276 |
|
| 2277 |
srv_threads.m_error_monitor.start(); |
| 2278 |
|
| 2279 |
/* Create the thread which prints InnoDB monitor info */ |
| 2280 |
srv_threads.m_monitor = |
| 2281 |
os_thread_create(srv_monitor_thread_key, 0, srv_monitor_thread); |
| 2282 |
|
| 2283 |
srv_threads.m_monitor.start(); |
| 2284 |
} |
| 2285 |
|
| 2286 |
srv_sys_tablespaces_open = true; |
| 2287 |
|
| 2288 |
/* Rotate the encryption key for recovery. It's because |
| 2289 |
server could crash in middle of key rotation. Some tablespace |
| 2290 |
didn't complete key rotation. Here, we will resume the |
| 2291 |
rotation. */ |
| 2292 |
if (!srv_read_only_mode && !create_new_db && |
| 2293 |
srv_force_recovery < SRV_FORCE_NO_LOG_REDO) { |
| 2294 |
size_t fail_count = fil_encryption_rotate(); |
| 2295 |
if (fail_count > 0) { |
| 2296 |
ib::info(ER_IB_MSG_1146) |
| 2297 |
<< "During recovery, fil_encryption_rotate() failed for " |
| 2298 |
<< fail_count << " tablespace(s)."; |
| 2299 |
} |
| 2300 |
} |
| 2301 |
|
| 2302 |
srv_is_being_started = false; |
| 2303 |
|
| 2304 |
ut_a(trx_purge_state() == PURGE_STATE_INIT); |
| 2305 |
|
| 2306 |
/* wake main loop of page cleaner up */ |
| 2307 |
os_event_set(buf_flush_event); |
| 2308 |
|
| 2309 |
sum_of_data_file_sizes = srv_sys_space.get_sum_of_sizes(); |
| 2310 |
ut_a(sum_of_new_sizes != FIL_NULL); |
| 2311 |
|
| 2312 |
tablespace_size_in_header = fsp_header_get_tablespace_size(); |
| 2313 |
|
| 2314 |
if (!srv_read_only_mode && !srv_sys_space.can_auto_extend_last_file() && |
| 2315 |
sum_of_data_file_sizes != tablespace_size_in_header) { |
| 2316 |
ib::error(ER_IB_MSG_1147, ulong{tablespace_size_in_header}, |
| 2317 |
ulong{sum_of_data_file_sizes}); |
| 2318 |
|
| 2319 |
if (srv_force_recovery == 0 && |
| 2320 |
sum_of_data_file_sizes < tablespace_size_in_header) { |
| 2321 |
/* This is a fatal error, the tail of a tablespace is |
| 2322 |
missing */ |
| 2323 |
|
| 2324 |
ib::error(ER_IB_MSG_1148); |
| 2325 |
|
| 2326 |
return (srv_init_abort(DB_ERROR)); |
| 2327 |
} |
| 2328 |
} |
| 2329 |
|
| 2330 |
if (!srv_read_only_mode && srv_sys_space.can_auto_extend_last_file() && |
| 2331 |
sum_of_data_file_sizes < tablespace_size_in_header) { |
| 2332 |
ib::error(ER_IB_MSG_1149, ulong{tablespace_size_in_header}, |
| 2333 |
ulong{sum_of_data_file_sizes}); |
| 2334 |
|
| 2335 |
if (srv_force_recovery == 0) { |
| 2336 |
ib::error(ER_IB_MSG_1150); |
| 2337 |
|
| 2338 |
return (srv_init_abort(DB_ERROR)); |
| 2339 |
} |
| 2340 |
} |
| 2341 |
|
| 2342 |
/* Finish clone files recovery. This call is idempotent and is no op |
| 2343 |
if it is already done before creating new log files. */ |
| 2344 |
clone_files_recovery(true); |
| 2345 |
|
| 2346 |
ib::info(ER_IB_MSG_1151, INNODB_VERSION_STR, |
| 2347 |
ulonglong{log_get_lsn(*log_sys)}); |
| 2348 |
|
| 2349 |
return (DB_SUCCESS); |
| 2350 |
} |
| 2351 |
|
| 2352 |
/** Applier of dynamic metadata */ |
| 2353 |
struct metadata_applier { |
| 2354 |
/** Default constructor */ |
| 2355 |
metadata_applier() = default; |
| 2356 |
/** Visitor. |
| 2357 |
@param[in] table table to visit */ |
| 2358 |
void operator()(dict_table_t *table) const { |
| 2359 |
ut_ad(dict_sys->dynamic_metadata != nullptr); |
| 2360 |
uint64_t autoinc = table->autoinc; |
| 2361 |
dict_table_load_dynamic_metadata(table); |
| 2362 |
/* For those tables which were not opened by |
| 2363 |
ha_innobase::open() and not initialized by |
| 2364 |
innobase_initialize_autoinc(), the next counter should be |
| 2365 |
advanced properly */ |
| 2366 |
if (autoinc != table->autoinc && table->autoinc != ~0ULL) { |
| 2367 |
++table->autoinc; |
| 2368 |
} |
| 2369 |
} |
| 2370 |
}; |
| 2371 |
|
| 2372 |
/** Apply the dynamic metadata to all tables */ |
| 2373 |
static void apply_dynamic_metadata() { |
| 2374 |
const metadata_applier applier; |
| 2375 |
|
| 2376 |
dict_sys->for_each_table(applier); |
| 2377 |
} |
| 2378 |
|
| 2379 |
/** On a restart, initialize the remaining InnoDB subsystems so that |
| 2380 |
any tables (including data dictionary tables) can be accessed. */ |
| 2381 |
void srv_dict_recover_on_restart() { |
| 2382 |
/* Resurrect locks for dictionary transactions */ |
| 2383 |
trx_resurrect_locks(false); |
| 2384 |
|
| 2385 |
/* Roll back any recovered data dictionary transactions, so |
| 2386 |
that the data dictionary tables will be free of any locks. |
| 2387 |
The data dictionary latch should guarantee that there is at |
| 2388 |
most one data dictionary transaction active at a time. */ |
| 2389 |
if (srv_force_recovery < SRV_FORCE_NO_TRX_UNDO && trx_sys_need_rollback()) { |
| 2390 |
trx_rollback_or_clean_recovered(false); |
| 2391 |
} |
| 2392 |
|
| 2393 |
/* Resurrect locks for non-dictionary transactions only after rolling back all |
| 2394 |
dictionary transactions. This is required as of today since we read |
| 2395 |
uncommitted data while constructing table object in dd_table_open_on_id_low. |
| 2396 |
This is done only while looking for the DD space object |
| 2397 |
client->acquire_uncached_uncommitted<dd::Tablespace>(). |
| 2398 |
|
| 2399 |
TODO-1: dd_table_open_on_id_low : Reading uncommitted data doesn't seem |
| 2400 |
correct and needs to be analyzed and possibly fixed. |
| 2401 |
|
| 2402 |
Till that time we let all DD transactions to rollback to avoid reading dirty |
| 2403 |
data from incomplete DDL commands while resurrecting locks. It essentially |
| 2404 |
fixes two independent issues. |
| 2405 |
|
| 2406 |
1. Not able to resurrect table locks for uncommitted transaction. |
| 2407 |
|
| 2408 |
2. Not able to load innodb dict_* object for the table involved in the DDL. |
| 2409 |
This could result in much more serious issue when binary log is enabled |
| 2410 |
and crash happens after the transaction is prepared. Currently in binlog |
| 2411 |
transaction recovery path no session THD is created and we rely on cached |
| 2412 |
dict_* object to find out if a table is dropped. If the dict_table_t |
| 2413 |
object is not already loaded, the table is considered dropped and undo |
| 2414 |
apply is skipped. This would further result in uncommitted but prepared |
| 2415 |
transaction data being committed and persisted. |
| 2416 |
|
| 2417 |
TODO-2: Have session (THD) while doing binary log recovery. The lack of |
| 2418 |
THD seems not correct since rollback requires DD metadata. This alone would |
| 2419 |
have prevented transaction inconsistency between innodb and binlog even if we |
| 2420 |
failed to resurrect the table locks. |
| 2421 |
binlog_recover->ha_recover->xarecover_handlerton->innobase_rollback_by_xid |
| 2422 |
->innobase_rollback_trx |
| 2423 |
|
| 2424 |
Note: The current work around fixes both issues but ideally should not be |
| 2425 |
required if base issues [TODOs] are fixed. */ |
| 2426 |
trx_resurrect_locks(true); |
| 2427 |
|
| 2428 |
trx_clear_resurrected_table_ids(); |
| 2429 |
|
| 2430 |
/* Do after all DD transactions recovery, to get consistent metadata */ |
| 2431 |
apply_dynamic_metadata(); |
| 2432 |
|
| 2433 |
if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) { |
| 2434 |
srv_sys_tablespaces_open = true; |
| 2435 |
} |
| 2436 |
} |
| 2437 |
|
| 2438 |
/** Start purge threads. During upgrade we start |
| 2439 |
purge threads early to apply purge. */ |
| 2440 |
void srv_start_purge_threads() { |
| 2441 |
/* Start purge threads only if they are not started earlier. */ |
| 2442 |
if (srv_start_state_is_set(SRV_START_STATE_PURGE)) { |
| 2443 |
return; |
| 2444 |
} |
| 2445 |
|
| 2446 |
srv_threads.m_purge_coordinator = |
| 2447 |
os_thread_create(srv_purge_thread_key, 0, srv_purge_coordinator_thread); |
| 2448 |
|
| 2449 |
srv_threads.m_purge_workers[0] = srv_threads.m_purge_coordinator; |
| 2450 |
|
| 2451 |
/* We've already created the purge coordinator thread above. */ |
| 2452 |
for (size_t i = 1; i < srv_threads.m_purge_workers_n; ++i) { |
| 2453 |
srv_threads.m_purge_workers[i] = |
| 2454 |
os_thread_create(srv_worker_thread_key, i, srv_worker_thread); |
| 2455 |
} |
| 2456 |
|
| 2457 |
for (size_t i = 0; i < srv_threads.m_purge_workers_n; ++i) { |
| 2458 |
srv_threads.m_purge_workers[i].start(); |
| 2459 |
} |
| 2460 |
|
| 2461 |
srv_start_wait_for_purge_to_start(); |
| 2462 |
|
| 2463 |
srv_start_state_set(SRV_START_STATE_PURGE); |
| 2464 |
} |
| 2465 |
|
| 2466 |
/** Start up the InnoDB service threads which are independent of DDL recovery. |
| 2467 |
*/ |
| 2468 |
void srv_start_threads() { |
| 2469 |
if (!srv_read_only_mode) { |
| 2470 |
/* Before 8.0, it was master thread that was doing periodical |
| 2471 |
checkpoints (every 7s). Since 8.0, it is the log checkpointer |
| 2472 |
thread, which is owned by log_sys, that is responsible for |
| 2473 |
periodical checkpoints (every innodb_log_checkpoint_every ms). |
| 2474 |
Note that the log checkpointer thread was created earlier and |
| 2475 |
is already active, but the periodical checkpoints were disabled. |
| 2476 |
Only the required checkpoints were allowed, which includes: |
| 2477 |
- checkpoints because of too old last_checkpoint_lsn, |
| 2478 |
- checkpoints explicitly requested (because of call to |
| 2479 |
log_make_latest_checkpoint()). |
| 2480 |
The reason was to make the situation more deterministic during |
| 2481 |
the startup, because then: |
| 2482 |
- it is easier to write mtr tests, |
| 2483 |
- there are less possible flows - smaller risk of bug. |
| 2484 |
Now we start allowing periodical checkpoints! Since now, it's |
| 2485 |
hard to predict when checkpoints are written! */ |
| 2486 |
log_limits_mutex_enter(*log_sys); |
| 2487 |
log_sys->periodical_checkpoints_enabled = true; |
| 2488 |
log_limits_mutex_exit(*log_sys); |
| 2489 |
} |
| 2490 |
|
| 2491 |
srv_threads.m_buf_resize = |
| 2492 |
os_thread_create(buf_resize_thread_key, 0, buf_resize_thread); |
| 2493 |
|
| 2494 |
srv_threads.m_buf_resize.start(); |
| 2495 |
|
| 2496 |
if (srv_read_only_mode) { |
| 2497 |
purge_sys->state = PURGE_STATE_DISABLED; |
| 2498 |
return; |
| 2499 |
} |
| 2500 |
|
| 2501 |
/* Create the master thread which does purge and other utility |
| 2502 |
operations */ |
| 2503 |
srv_threads.m_master = |
| 2504 |
os_thread_create(srv_master_thread_key, 0, srv_master_thread); |
| 2505 |
|
| 2506 |
srv_threads.m_master.start(); |
| 2507 |
|
| 2508 |
if (srv_force_recovery == 0) { |
| 2509 |
/* In the insert buffer we may have even bigger tablespace |
| 2510 |
id's, because we may have dropped those tablespaces, but |
| 2511 |
insert buffer merge has not had time to clean the records from |
| 2512 |
the ibuf tree. */ |
| 2513 |
|
| 2514 |
ibuf_update_max_tablespace_id(); |
| 2515 |
} |
| 2516 |
|
| 2517 |
/* Create the dict stats gathering thread */ |
| 2518 |
srv_threads.m_dict_stats = |
| 2519 |
os_thread_create(dict_stats_thread_key, 0, dict_stats_thread); |
| 2520 |
|
| 2521 |
dict_stats_thread_init(); |
| 2522 |
|
| 2523 |
srv_threads.m_dict_stats.start(); |
| 2524 |
|
| 2525 |
/* Create the thread that will optimize the FTS sub-system. */ |
| 2526 |
fts_optimize_init(); |
| 2527 |
|
| 2528 |
srv_start_state_set(SRV_START_STATE_STAT); |
| 2529 |
} |
| 2530 |
|
| 2531 |
void srv_start_threads_after_ddl_recovery() { |
| 2532 |
if (srv_force_recovery < SRV_FORCE_NO_TRX_UNDO && trx_sys_need_rollback()) { |
| 2533 |
/* Rollback all recovered transactions that are |
| 2534 |
not in committed nor in XA PREPARE state. */ |
| 2535 |
srv_threads.m_trx_recovery_rollback = os_thread_create( |
| 2536 |
trx_recovery_rollback_thread_key, 0, trx_recovery_rollback_thread); |
| 2537 |
|
| 2538 |
srv_threads.m_trx_recovery_rollback.start(); |
| 2539 |
/* Wait till shared MDL is taken by background thread for all tables, |
| 2540 |
for which rollback is to be performed. */ |
| 2541 |
os_event_wait(recovery_lock_taken); |
| 2542 |
} |
| 2543 |
|
| 2544 |
/* Start the buffer pool dump/load thread, which will access spaces thus |
| 2545 |
must wait for DDL recovery */ |
| 2546 |
srv_threads.m_buf_dump = |
| 2547 |
os_thread_create(buf_dump_thread_key, 0, buf_dump_thread); |
| 2548 |
|
| 2549 |
srv_threads.m_buf_dump.start(); |
| 2550 |
|
| 2551 |
/* Resume unfinished (un)encryption process in background thread. */ |
| 2552 |
if (!ts_encrypt_ddl_records.empty()) { |
| 2553 |
srv_threads.m_ts_alter_encrypt = |
| 2554 |
os_thread_create(srv_ts_alter_encrypt_thread_key, 0, |
| 2555 |
fsp_init_resume_alter_encrypt_tablespace); |
| 2556 |
|
| 2557 |
mysql_mutex_lock(&resume_encryption_cond_m); |
| 2558 |
srv_threads.m_ts_alter_encrypt.start(); |
| 2559 |
/* Wait till shared MDL is taken by background thread for all tablespaces, |
| 2560 |
for which (un)encryption is to be rolled forward. */ |
| 2561 |
mysql_cond_wait(&resume_encryption_cond, &resume_encryption_cond_m); |
| 2562 |
mysql_mutex_unlock(&resume_encryption_cond_m); |
| 2563 |
} |
| 2564 |
|
| 2565 |
/* Start and consume all GTIDs for recovered transactions. */ |
| 2566 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 2567 |
gtid_persistor.start(); |
| 2568 |
|
| 2569 |
DBUG_EXECUTE_IF("crash_before_purge_thread", DBUG_SUICIDE();); |
| 2570 |
|
| 2571 |
/* Now the InnoDB Metadata and file system should be consistent. |
| 2572 |
Start the Purge thread */ |
| 2573 |
srv_start_purge_threads(); |
| 2574 |
|
| 2575 |
/* If recovered, should do write back the dynamic metadata. */ |
| 2576 |
dict_persist_to_dd_table_buffer(); |
| 2577 |
} |
| 2578 |
|
| 2579 |
/** Set srv_shutdown_state to a given state and validate change is proper. |
| 2580 |
@remarks This function is used only from the main thread, and only during |
| 2581 |
startup or shutdown. |
| 2582 |
@param[in] new_state new state to set */ |
| 2583 |
static void srv_shutdown_set_state(srv_shutdown_t new_state) { |
| 2584 |
ut_a(static_cast<int>(srv_shutdown_state.load()) + 1 == |
| 2585 |
static_cast<int>(new_state)); |
| 2586 |
|
| 2587 |
srv_shutdown_state.store(new_state); |
| 2588 |
} |
| 2589 |
|
| 2590 |
static void srv_shutdown_cleanup_and_master_stop(); |
| 2591 |
|
| 2592 |
bool srv_shutdown_waits_for_rollback_of_recovered_transactions() { |
| 2593 |
return (srv_force_recovery < SRV_FORCE_NO_TRX_UNDO && srv_fast_shutdown == 0); |
| 2594 |
} |
| 2595 |
|
| 2596 |
/** Shut down all InnoDB background tasks that may look up objects in |
| 2597 |
the data dictionary. */ |
| 2598 |
void srv_pre_dd_shutdown() { |
| 2599 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_NONE); |
| 2600 |
|
| 2601 |
/* Warn and wait if there are still some query threads alive. |
| 2602 |
If all is correct, then all user threads should already be gone, |
| 2603 |
because before clean_up() -> srv_pre_dd_shutdown() is called, |
| 2604 |
we are joining signal_hand thread, which before exiting waits |
| 2605 |
for all connections to be closed (close_connections()). */ |
| 2606 |
for (size_t count = 0; count < 10; ++count) { |
| 2607 |
const auto threads_count = srv_conc_get_active_threads(); |
| 2608 |
if (threads_count == 0) { |
| 2609 |
break; |
| 2610 |
} |
| 2611 |
ib::warn(ER_IB_MSG_1154, threads_count); |
| 2612 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 2613 |
} |
| 2614 |
/* Crash if some query threads are still alive. */ |
| 2615 |
ut_a(srv_conc_get_active_threads() == 0); |
| 2616 |
|
| 2617 |
ut_a(!srv_thread_is_active(srv_threads.m_recv_writer)); |
| 2618 |
|
| 2619 |
/* Avoid fast shutdown, if redo logging is disabled. Otherwise, we won't be |
| 2620 |
able to recover. */ |
| 2621 |
if (mtr_t::s_logging.is_disabled() && srv_fast_shutdown == 2) { |
| 2622 |
ib::warn(ER_IB_WRN_FAST_SHUTDOWN_REDO_DISABLED); |
| 2623 |
srv_fast_shutdown = 1; |
| 2624 |
} |
| 2625 |
|
| 2626 |
/* Stop service for persisting GTID */ |
| 2627 |
auto >id_persistor = clone_sys->get_gtid_persistor(); |
| 2628 |
gtid_persistor.stop(); |
| 2629 |
|
| 2630 |
if (srv_read_only_mode) { |
| 2631 |
/* Check that goal of SRV_SHUTDOWN_RECOVERY_ROLLBACK is reached: |
| 2632 |
1. In read-only mode, no rollbacks should be executed. |
| 2633 |
2. The trx_recovery_rollback thread should not be started. */ |
| 2634 |
ut_ad(trx_sys_recovered_active_trxs_count() == 0); |
| 2635 |
ut_a(!srv_thread_is_active(srv_threads.m_trx_recovery_rollback)); |
| 2636 |
|
| 2637 |
/* Check the goal of SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS, |
| 2638 |
the following threads should not be started in read-only mode: */ |
| 2639 |
ut_a(!srv_thread_is_active(srv_threads.m_dict_stats)); |
| 2640 |
ut_a(!srv_thread_is_active(srv_threads.m_fts_optimize)); |
| 2641 |
ut_a(!srv_thread_is_active(srv_threads.m_ts_alter_encrypt)); |
| 2642 |
|
| 2643 |
/* In read-only mode, there is no master thread. */ |
| 2644 |
ut_a(!srv_thread_is_active(srv_threads.m_master)); |
| 2645 |
|
| 2646 |
/* In read-only mode, no purge should be done, so goal of the |
| 2647 |
SRV_SHUTDOWN_PURGE is already satisfied (no purge threads). */ |
| 2648 |
ut_a(!srv_purge_threads_active()); |
| 2649 |
|
| 2650 |
/* Advance quickly through all states to SRV_SHUTDOWN_DD. */ |
| 2651 |
srv_shutdown_set_state(SRV_SHUTDOWN_RECOVERY_ROLLBACK); |
| 2652 |
srv_shutdown_set_state(SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS); |
| 2653 |
srv_shutdown_set_state(SRV_SHUTDOWN_PURGE); |
| 2654 |
srv_shutdown_set_state(SRV_SHUTDOWN_DD); |
| 2655 |
return; |
| 2656 |
} |
| 2657 |
|
| 2658 |
srv_shutdown_set_state(SRV_SHUTDOWN_RECOVERY_ROLLBACK); |
| 2659 |
|
| 2660 |
if (srv_shutdown_waits_for_rollback_of_recovered_transactions()) { |
| 2661 |
/* We need to wait for rollback of recovered transactions. */ |
| 2662 |
for (uint32_t count = 0;; ++count) { |
| 2663 |
/* Should not loop and wait if rollback thread isn't there. */ |
| 2664 |
if (!srv_thread_is_active(srv_threads.m_trx_recovery_rollback)) { |
| 2665 |
break; |
| 2666 |
} |
| 2667 |
const auto total_trx = trx_sys_recovered_active_trxs_count(); |
| 2668 |
if (total_trx == 0) { |
| 2669 |
break; |
| 2670 |
} |
| 2671 |
if (count >= SHUTDOWN_SLEEP_ROUNDS) { |
| 2672 |
ib::info(ER_IB_MSG_1249, total_trx); |
| 2673 |
count = 0; |
| 2674 |
} |
| 2675 |
std::this_thread::sleep_for( |
| 2676 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2677 |
} |
| 2678 |
} |
| 2679 |
|
| 2680 |
if (srv_thread_is_active(srv_threads.m_trx_recovery_rollback)) { |
| 2681 |
/* We should wait until rollback after recovery end to avoid |
| 2682 |
adding more for purge and to avoid touching transaction objects |
| 2683 |
since this point. */ |
| 2684 |
srv_threads.m_trx_recovery_rollback.wait(); |
| 2685 |
} |
| 2686 |
|
| 2687 |
srv_shutdown_set_state(SRV_SHUTDOWN_PRE_DD_AND_SYSTEM_TRANSACTIONS); |
| 2688 |
|
| 2689 |
if (srv_start_state_is_set(SRV_START_STATE_STAT)) { |
| 2690 |
fts_optimize_shutdown(); |
| 2691 |
dict_stats_shutdown(); |
| 2692 |
dict_stats_thread_deinit(); |
| 2693 |
} |
| 2694 |
ut_a(!srv_thread_is_active(srv_threads.m_fts_optimize)); |
| 2695 |
ut_a(!srv_thread_is_active(srv_threads.m_dict_stats)); |
| 2696 |
|
| 2697 |
for (uint32_t count = 1; srv_thread_is_active(srv_threads.m_ts_alter_encrypt); |
| 2698 |
++count) { |
| 2699 |
if (count % SHUTDOWN_SLEEP_ROUNDS == 0) { |
| 2700 |
ib::info(ER_IB_MSG_WAIT_FOR_ENCRYPT_THREAD); |
| 2701 |
} |
| 2702 |
std::this_thread::sleep_for( |
| 2703 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2704 |
} |
| 2705 |
|
| 2706 |
/* Wait until the master thread exits its main loop and notices that: |
| 2707 |
- it should do shutdown-cleanup, |
| 2708 |
- and still is allowed to access DD objects. */ |
| 2709 |
if (srv_thread_is_active(srv_threads.m_master)) { |
| 2710 |
srv_wake_master_thread(); |
| 2711 |
os_event_wait(srv_threads.m_master_ready_for_dd_shutdown); |
| 2712 |
} |
| 2713 |
|
| 2714 |
/* Since this point we do not expect accesses to DD coming from InnoDB. */ |
| 2715 |
ut_d(trx_sys_before_pre_dd_shutdown_validate()); |
| 2716 |
|
| 2717 |
srv_shutdown_set_state(SRV_SHUTDOWN_PURGE); |
| 2718 |
|
| 2719 |
for (uint32_t count = 1; srv_purge_threads_active(); ++count) { |
| 2720 |
srv_purge_wakeup(); |
| 2721 |
if (count % SHUTDOWN_SLEEP_ROUNDS == 0) { |
| 2722 |
ib::info(ER_IB_MSG_1152); |
| 2723 |
} |
| 2724 |
std::this_thread::sleep_for( |
| 2725 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2726 |
} |
| 2727 |
switch (trx_purge_state()) { |
| 2728 |
case PURGE_STATE_INIT: |
| 2729 |
case PURGE_STATE_EXIT: |
| 2730 |
case PURGE_STATE_DISABLED: |
| 2731 |
srv_start_state &= ~SRV_START_STATE_PURGE; |
| 2732 |
break; |
| 2733 |
case PURGE_STATE_RUN: |
| 2734 |
case PURGE_STATE_STOP: |
| 2735 |
ut_d(ut_error); |
| 2736 |
} |
| 2737 |
|
| 2738 |
/* After this phase plugins are asked to be shut down, in which case they |
| 2739 |
will be marked as DELETED. Note: we cannot leave any transaction in the THD, |
| 2740 |
because the mechanism which cleans resources in THD would not be able to |
| 2741 |
unregister those transactions from mysql_trx_list, because the handler |
| 2742 |
of close_connection in InnoDB handlerton would not be called, because |
| 2743 |
InnoDB has already been marked as DELETED. You should close your thread |
| 2744 |
here, in the srv_pre_dd_shutdown, if it might do lookups in DD objects. |
| 2745 |
No other transactions should be useful, so for sake of simplicity we |
| 2746 |
require to have no transactions at all here, except transactions: |
| 2747 |
- with state = TRX_STATE_PREPARED, |
| 2748 |
- with state = TRX_STATE_ACTIVE and with is_recovered == true */ |
| 2749 |
|
| 2750 |
ut_d(trx_sys_after_pre_dd_shutdown_validate()); |
| 2751 |
|
| 2752 |
srv_shutdown_set_state(SRV_SHUTDOWN_DD); |
| 2753 |
|
| 2754 |
DBUG_EXECUTE_IF("wait_for_threads_in_pre_dd_shutdown", |
| 2755 |
srv_shutdown_cleanup_and_master_stop();); |
| 2756 |
} |
| 2757 |
|
| 2758 |
/** Shutdown background threads of InnoDB at the start of the shutdown phase. |
| 2759 |
Handles shutdown phases: SRV_SHUTDOWN_CLEANUP and SRV_SHUTDOWN_MASTER_STOP. */ |
| 2760 |
static void srv_shutdown_cleanup_and_master_stop() { |
| 2761 |
DBUG_EXECUTE_IF("threads_wait_on_cleanup", |
| 2762 |
os_event_set(srv_threads.m_shutdown_cleanup_dbg);); |
| 2763 |
|
| 2764 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_DD); |
| 2765 |
|
| 2766 |
srv_shutdown_set_state(SRV_SHUTDOWN_CLEANUP); |
| 2767 |
|
| 2768 |
const srv_shutdown_t max_wait_on_state{SRV_SHUTDOWN_MASTER_STOP}; |
| 2769 |
|
| 2770 |
uint32_t count = 0; |
| 2771 |
|
| 2772 |
for (;;) { |
| 2773 |
/* Print messages every 60 seconds when we are waiting for any |
| 2774 |
of those threads to exit. */ |
| 2775 |
bool print; |
| 2776 |
if (count >= SHUTDOWN_SLEEP_ROUNDS) { |
| 2777 |
print = true; |
| 2778 |
count = 0; |
| 2779 |
} else { |
| 2780 |
print = false; |
| 2781 |
} |
| 2782 |
|
| 2783 |
size_t active_found = 0; |
| 2784 |
for (const auto &thread_info : threads_to_stop) { |
| 2785 |
ut_a(thread_info.m_wait_on_state <= max_wait_on_state); |
| 2786 |
if (thread_info.m_wait_on_state == srv_shutdown_state.load() && |
| 2787 |
srv_thread_is_active(thread_info.m_thread)) { |
| 2788 |
++active_found; |
| 2789 |
if (print) { |
| 2790 |
ib::info(ER_IB_MSG_1248, thread_info.m_name); |
| 2791 |
} |
| 2792 |
thread_info.m_notify(); |
| 2793 |
} |
| 2794 |
} |
| 2795 |
|
| 2796 |
if (active_found == 0) { |
| 2797 |
if (srv_shutdown_state.load() == max_wait_on_state) { |
| 2798 |
break; |
| 2799 |
} |
| 2800 |
srv_shutdown_set_state(static_cast<srv_shutdown_t>( |
| 2801 |
static_cast<int>(srv_shutdown_state.load()) + 1)); |
| 2802 |
} |
| 2803 |
|
| 2804 |
std::this_thread::sleep_for( |
| 2805 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2806 |
++count; |
| 2807 |
} |
| 2808 |
|
| 2809 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_MASTER_STOP); |
| 2810 |
|
| 2811 |
ut_d(trx_sys_after_background_threads_shutdown_validate()); |
| 2812 |
} |
| 2813 |
|
| 2814 |
/** Waits for page cleaners exit. */ |
| 2815 |
static void srv_shutdown_page_cleaners() { |
| 2816 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_MASTER_STOP); |
| 2817 |
ut_a(!srv_master_thread_is_active()); |
| 2818 |
|
| 2819 |
srv_shutdown_set_state(SRV_SHUTDOWN_FLUSH_PHASE); |
| 2820 |
|
| 2821 |
buf_pool_wait_for_no_pending_io(); |
| 2822 |
|
| 2823 |
/* At this point only page_cleaner should be active. We wait |
| 2824 |
here to let it complete the flushing of the buffer pools |
| 2825 |
before proceeding further. */ |
| 2826 |
|
| 2827 |
for (uint32_t count = 0; buf_flush_page_cleaner_is_active(); ++count) { |
| 2828 |
if (count >= SHUTDOWN_SLEEP_ROUNDS) { |
| 2829 |
ib::info(ER_IB_MSG_1251); |
| 2830 |
count = 0; |
| 2831 |
} |
| 2832 |
os_event_set(buf_flush_event); |
| 2833 |
std::this_thread::sleep_for( |
| 2834 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2835 |
} |
| 2836 |
|
| 2837 |
ut_ad(buf_pool_pending_io_reads_count() == 0); |
| 2838 |
ut_ad(buf_pool_pending_io_writes_count() == 0); |
| 2839 |
} |
| 2840 |
|
| 2841 |
/** Closes redo log. If this is not fast shutdown, it forces to write a |
| 2842 |
checkpoint which should be written for logically empty redo log. Note that we |
| 2843 |
forced to flush all dirty pages in the last stages of page cleaners activity |
| 2844 |
(unless it was fast shutdown). After checkpoint is written, the flushed_lsn is |
| 2845 |
updated within header of the system tablespace. This is lsn of the last clean |
| 2846 |
shutdown. */ |
| 2847 |
static lsn_t srv_shutdown_log() { |
| 2848 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_FLUSH_PHASE); |
| 2849 |
ut_a(!buf_flush_page_cleaner_is_active()); |
| 2850 |
ut_ad(buf_pool_pending_io_reads_count() == 0); |
| 2851 |
ut_ad(buf_pool_pending_io_writes_count() == 0); |
| 2852 |
|
| 2853 |
if (srv_fast_shutdown == 2) { |
| 2854 |
if (!srv_read_only_mode) { |
| 2855 |
ib::info(ER_IB_MSG_1253); |
| 2856 |
|
| 2857 |
/* In this fastest shutdown we do not flush the |
| 2858 |
buffer pool: |
| 2859 |
|
| 2860 |
it is essentially a 'crash' of the InnoDB server. |
| 2861 |
Make sure that the log is all flushed to disk, so |
| 2862 |
that we can recover all committed transactions in |
| 2863 |
a crash recovery. We must not write the lsn stamps |
| 2864 |
to the data files, since at a startup InnoDB deduces |
| 2865 |
from the stamps if the previous shutdown was clean. */ |
| 2866 |
|
| 2867 |
log_stop_background_threads(*log_sys); |
| 2868 |
} |
| 2869 |
|
| 2870 |
/* No redo log might be generated since now. */ |
| 2871 |
log_background_threads_inactive_validate(); |
| 2872 |
|
| 2873 |
srv_shutdown_set_state(SRV_SHUTDOWN_LAST_PHASE); |
| 2874 |
|
| 2875 |
return (log_get_lsn(*log_sys)); |
| 2876 |
} |
| 2877 |
|
| 2878 |
if (!srv_read_only_mode) { |
| 2879 |
log_make_empty_and_stop_background_threads(*log_sys); |
| 2880 |
} |
| 2881 |
|
| 2882 |
/* No redo log might be generated since now. */ |
| 2883 |
log_background_threads_inactive_validate(); |
| 2884 |
buf_assert_all_are_replaceable(); |
| 2885 |
|
| 2886 |
const lsn_t lsn = log_get_lsn(*log_sys); |
| 2887 |
|
| 2888 |
if (!srv_read_only_mode) { |
| 2889 |
/* Redo log has been flushed at the log_flusher's exit. */ |
| 2890 |
fil_flush_file_spaces(); |
| 2891 |
} |
| 2892 |
|
| 2893 |
srv_shutdown_set_state(SRV_SHUTDOWN_LAST_PHASE); |
| 2894 |
|
| 2895 |
/* Validate lsn and write it down. */ |
| 2896 |
ut_a(log_is_data_lsn(lsn) || srv_force_recovery >= SRV_FORCE_NO_LOG_REDO); |
| 2897 |
|
| 2898 |
ut_a(lsn == log_sys->last_checkpoint_lsn.load() || |
| 2899 |
srv_force_recovery >= SRV_FORCE_NO_LOG_REDO); |
| 2900 |
|
| 2901 |
ut_a(lsn == log_get_lsn(*log_sys)); |
| 2902 |
|
| 2903 |
if (!srv_read_only_mode) { |
| 2904 |
ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO); |
| 2905 |
|
| 2906 |
auto err = fil_write_flushed_lsn(lsn); |
| 2907 |
ut_a(err == DB_SUCCESS); |
| 2908 |
} |
| 2909 |
|
| 2910 |
buf_assert_all_are_replaceable(); |
| 2911 |
ut_a(lsn == log_get_lsn(*log_sys)); |
| 2912 |
|
| 2913 |
if (srv_downgrade_logs) { |
| 2914 |
ut_a(!srv_read_only_mode); |
| 2915 |
|
| 2916 |
/* InnoDB in any version is able to start on empty set of redo files. */ |
| 2917 |
log_files_remove(*log_sys); |
| 2918 |
} |
| 2919 |
|
| 2920 |
return (lsn); |
| 2921 |
} |
| 2922 |
|
| 2923 |
/** Copy all remaining data and shutdown archiver threads. */ |
| 2924 |
static void srv_shutdown_arch() { |
| 2925 |
uint32_t count = 0; |
| 2926 |
|
| 2927 |
while (arch_wake_threads()) { |
| 2928 |
++count; |
| 2929 |
std::this_thread::sleep_for( |
| 2930 |
std::chrono::microseconds(SHUTDOWN_SLEEP_TIME_US)); |
| 2931 |
|
| 2932 |
if (count > SHUTDOWN_SLEEP_ROUNDS) { |
| 2933 |
ib::info(ER_IB_MSG_1246); |
| 2934 |
count = 0; |
| 2935 |
} |
| 2936 |
} |
| 2937 |
} |
| 2938 |
|
| 2939 |
void srv_thread_delay_cleanup_if_needed(bool wait_for_signal) { |
| 2940 |
DBUG_EXECUTE_IF("threads_wait_on_cleanup", { |
| 2941 |
if (wait_for_signal) { |
| 2942 |
os_event_wait(srv_threads.m_shutdown_cleanup_dbg); |
| 2943 |
} else { |
| 2944 |
/* In some cases we cannot wait for the signal, because we would otherwise |
| 2945 |
never reach the end of pre_dd_shutdown, because pre_dd_shutdown is waiting |
| 2946 |
for this thread before it ends. Then we would never reach shutdown phase |
| 2947 |
in which the signal becomes signalled. Still we would like to have a way |
| 2948 |
to detect situation in which someone broke the code and pre_dd_shutdown |
| 2949 |
no longer waits for this thread. */ |
| 2950 |
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 2951 |
} |
| 2952 |
}); |
| 2953 |
} |
| 2954 |
|
| 2955 |
/** Shut down the InnoDB database. */ |
| 2956 |
void srv_shutdown() { |
| 2957 |
ut_d(trx_sys_after_pre_dd_shutdown_validate()); |
| 2958 |
|
| 2959 |
/* Need to revert partition file names if minor upgrade fails. */ |
| 2960 |
uint data_version = MYSQL_VERSION_ID; |
| 2961 |
|
| 2962 |
if (!fsp_header_dict_get_server_version(&data_version) && |
| 2963 |
data_version != MYSQL_VERSION_ID) { |
| 2964 |
srv_downgrade_partition_files = true; |
| 2965 |
} |
| 2966 |
|
| 2967 |
ib::info(ER_IB_MSG_1247); |
| 2968 |
|
| 2969 |
ut_a(!srv_is_being_started); |
| 2970 |
|
| 2971 |
/* Ensure threads below have been stopped. */ |
| 2972 |
const auto threads_stopped_before_shutdown = { |
| 2973 |
std::cref(srv_threads.m_purge_coordinator), |
| 2974 |
std::cref(srv_threads.m_ts_alter_encrypt), |
| 2975 |
std::cref(srv_threads.m_fts_optimize), |
| 2976 |
std::cref(srv_threads.m_recv_writer), |
| 2977 |
std::cref(srv_threads.m_dict_stats)}; |
| 2978 |
|
| 2979 |
for (const auto &thread : threads_stopped_before_shutdown) { |
| 2980 |
ut_a(!srv_thread_is_active(thread)); |
| 2981 |
} |
| 2982 |
|
| 2983 |
#ifdef UNIV_DEBUG |
| 2984 |
/* In DEBUG we might be testing scenario in which we forced to |
| 2985 |
call srv_shutdown_cleanup_and_master_stop() to stop all threads |
| 2986 |
at the end of the srv_pre_dd_shutdown(). */ |
| 2987 |
DBUG_EXECUTE_IF("wait_for_threads_in_pre_dd_shutdown", |
| 2988 |
srv_shutdown_state.store(SRV_SHUTDOWN_DD);); |
| 2989 |
#endif /* UNIV_DEBUG */ |
| 2990 |
|
| 2991 |
/* The SRV_SHUTDOWN_DD state was set during pre_dd_shutdown phase. */ |
| 2992 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_DD); |
| 2993 |
|
| 2994 |
/* Write dynamic metadata to DD buffer table. */ |
| 2995 |
dict_persist_to_dd_table_buffer(); |
| 2996 |
|
| 2997 |
/* 0. Stop remaining background threads except: |
| 2998 |
- page-cleaners - we are shutting down page cleaners in step 1 |
| 2999 |
- redo-log-threads - these need to be shutdown after page cleaners, |
| 3000 |
- archiver threads - these need to be shutdown after redo threads. |
| 3001 |
After this call the state of shutdown is advanced to SRV_SHUTDOWN_MASTER_STOP. |
| 3002 |
*/ |
| 3003 |
srv_shutdown_cleanup_and_master_stop(); |
| 3004 |
|
| 3005 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_MASTER_STOP); |
| 3006 |
|
| 3007 |
/* Check again and write dynamic metadata to DD buffer table. Ideally we |
| 3008 |
would not have dynamic metadata written so late in shutdown phase but |
| 3009 |
currently we have certain operations done in master thread which could |
| 3010 |
generate metadata. It is safe to check and write it here before we flush |
| 3011 |
buffer pool to disk. */ |
| 3012 |
dict_persist_to_dd_table_buffer(); |
| 3013 |
|
| 3014 |
/* The steps 1-4 is the real InnoDB shutdown. |
| 3015 |
All before was to stop activity which could produce new changes. |
| 3016 |
All after is just cleaning up (freeing memory). */ |
| 3017 |
|
| 3018 |
/* 1. Flush the buffer pool to disk. */ |
| 3019 |
srv_shutdown_page_cleaners(); |
| 3020 |
|
| 3021 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_FLUSH_PHASE); |
| 3022 |
|
| 3023 |
/* 2. Write the current lsn to the tablespace header(s). */ |
| 3024 |
const lsn_t shutdown_lsn = srv_shutdown_log(); |
| 3025 |
|
| 3026 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_LAST_PHASE); |
| 3027 |
|
| 3028 |
/* 3. Close all opened files. */ |
| 3029 |
ibt::close_files(); |
| 3030 |
fil_close_all_files(); |
| 3031 |
if (srv_monitor_file) { |
| 3032 |
fclose(srv_monitor_file); |
| 3033 |
} |
| 3034 |
if (srv_misc_tmpfile) { |
| 3035 |
fclose(srv_misc_tmpfile); |
| 3036 |
} |
| 3037 |
|
| 3038 |
/* 4. Copy all log data to archive and stop archiver threads. */ |
| 3039 |
srv_shutdown_arch(); |
| 3040 |
|
| 3041 |
/* This is to preserve the old style, we should finally get rid of the call |
| 3042 |
here. For that, we need to ensure we have already effectively closed all |
| 3043 |
threads. */ |
| 3044 |
srv_shutdown_exit_threads(); |
| 3045 |
|
| 3046 |
ut_a(srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS); |
| 3047 |
ut_ad(!os_thread_any_active()); |
| 3048 |
|
| 3049 |
/* 5. Free all the resources acquired by InnoDB (mutexes, events, memory). */ |
| 3050 |
ibt::delete_pool_manager(); |
| 3051 |
|
| 3052 |
if (srv_monitor_file) { |
| 3053 |
srv_monitor_file = nullptr; |
| 3054 |
if (srv_monitor_file_name) { |
| 3055 |
unlink(srv_monitor_file_name); |
| 3056 |
ut::free(srv_monitor_file_name); |
| 3057 |
} |
| 3058 |
mutex_free(&srv_monitor_file_mutex); |
| 3059 |
} |
| 3060 |
|
| 3061 |
if (srv_misc_tmpfile) { |
| 3062 |
srv_misc_tmpfile = nullptr; |
| 3063 |
mutex_free(&srv_misc_tmpfile_mutex); |
| 3064 |
} |
| 3065 |
|
| 3066 |
/* This must be disabled before closing the buffer pool |
| 3067 |
and closing the data dictionary. */ |
| 3068 |
btr_search_disable(); |
| 3069 |
|
| 3070 |
ibuf_close(); |
| 3071 |
ddl_log_close(); |
| 3072 |
log_sys_close(); |
| 3073 |
recv_sys_close(); |
| 3074 |
trx_sys_close(); |
| 3075 |
lock_sys_close(); |
| 3076 |
trx_pool_close(); |
| 3077 |
|
| 3078 |
dict_close(); |
| 3079 |
dict_persist_close(); |
| 3080 |
btr_search_sys_free(); |
| 3081 |
undo_spaces_deinit(); |
| 3082 |
os_aio_free(); |
| 3083 |
que_close(); |
| 3084 |
row_mysql_close(); |
| 3085 |
srv_free(); |
| 3086 |
fil_close(); |
| 3087 |
pars_close(); |
| 3088 |
|
| 3089 |
pars_lexer_close(); |
| 3090 |
buf_pool_free_all(); |
| 3091 |
|
| 3092 |
/* 6. Free the thread management resources. */ |
| 3093 |
clone_free(); |
| 3094 |
arch_free(); |
| 3095 |
|
| 3096 |
dblwr::close(); |
| 3097 |
os_thread_close(); |
| 3098 |
|
| 3099 |
/* 6. Free the synchronisation infrastructure. */ |
| 3100 |
sync_check_close(); |
| 3101 |
|
| 3102 |
ib::info(ER_IB_MSG_1155, ulonglong{shutdown_lsn}); |
| 3103 |
|
| 3104 |
srv_start_has_been_called = false; |
| 3105 |
srv_start_state = SRV_START_STATE_NONE; |
| 3106 |
} |
| 3107 |
|
| 3108 |
void srv_get_encryption_data_filename(dict_table_t *table, char *filename, |
| 3109 |
ulint max_len) { |
| 3110 |
/* Make sure the data_dir_path is set. */ |
| 3111 |
dd_get_and_save_data_dir_path<dd::Table>(table, nullptr, false); |
| 3112 |
|
| 3113 |
std::string path = dict_table_get_datadir(table); |
| 3114 |
|
| 3115 |
auto filepath = Fil_path::make(path, table->name.m_name, CFP, true); |
| 3116 |
|
| 3117 |
size_t len = strlen(filepath); |
| 3118 |
ut_a(max_len >= len); |
| 3119 |
|
| 3120 |
strcpy(filename, filepath); |
| 3121 |
|
| 3122 |
ut::free(filepath); |
| 3123 |
} |
| 3124 |
|
| 3125 |
/** Call std::_Exit(3) */ |
| 3126 |
void srv_fatal_error() { |
| 3127 |
ib::error(ER_IB_MSG_1156); |
| 3128 |
|
| 3129 |
fflush(stderr); |
| 3130 |
|
| 3131 |
ut_d(innodb_calling_exit = true); |
| 3132 |
|
| 3133 |
flush_error_log_messages(); |
| 3134 |
|
| 3135 |
std::_Exit(3); |
| 3136 |
} |