| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file clone/clone0api.cc |
| 29 |
Innodb Clone Interface |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
#include <cstdio> |
| 33 |
#include <fstream> |
| 34 |
#include <iostream> |
| 35 |
|
| 36 |
#include "clone0api.h" |
| 37 |
#include "clone0clone.h" |
| 38 |
#include "os0thread-create.h" |
| 39 |
|
| 40 |
#include "sql/clone_handler.h" |
| 41 |
#include "sql/mysqld.h" |
| 42 |
#include "sql/sql_backup_lock.h" |
| 43 |
#include "sql/sql_class.h" |
| 44 |
#include "sql/sql_prepare.h" |
| 45 |
#include "sql/sql_table.h" |
| 46 |
#include "sql/sql_thd_internal_api.h" |
| 47 |
#include "sql/strfunc.h" |
| 48 |
|
| 49 |
#include "dict0dd.h" |
| 50 |
#include "ha_innodb.h" |
| 51 |
#include "log0files_io.h" |
| 52 |
#include "sql/dd/cache/dictionary_client.h" |
| 53 |
#include "sql/dd/dictionary.h" |
| 54 |
#include "sql/dd/impl/dictionary_impl.h" // dd::dd_tablespace_id() |
| 55 |
#include "sql/dd/impl/sdi.h" |
| 56 |
#include "sql/dd/impl/utils.h" |
| 57 |
#include "sql/dd/types/schema.h" |
| 58 |
#include "sql/dd/types/table.h" |
| 59 |
#include "sql/rpl_msr.h" // is_slave_configured() |
| 60 |
|
| 61 |
/* To get current session thread default THD */ |
| 62 |
THD *thd_get_current_thd(); |
| 63 |
|
| 64 |
/** Check if clone status file exists. |
| 65 |
@param[in] file_name file name |
| 66 |
@return true if file exists. */ |
| 67 |
static bool file_exists(const std::string &file_name) { |
| 68 |
std::ifstream file(file_name.c_str()); |
| 69 |
|
| 70 |
if (file.is_open()) { |
| 71 |
file.close(); |
| 72 |
return (true); |
| 73 |
} |
| 74 |
return (false); |
| 75 |
} |
| 76 |
|
| 77 |
/** Rename clone status file. The operation is expected to be atomic |
| 78 |
when the files belong to same directory. |
| 79 |
@param[in] from_file name of current file |
| 80 |
@param[in] to_file name of new file */ |
| 81 |
static void rename_file(const std::string &from_file, |
| 82 |
const std::string &to_file) { |
| 83 |
auto ret = std::rename(from_file.c_str(), to_file.c_str()); |
| 84 |
|
| 85 |
if (ret != 0) { |
| 86 |
ib::fatal(UT_LOCATION_HERE, ER_IB_CLONE_STATUS_FILE) |
| 87 |
<< "Error renaming file from: " << from_file.c_str() |
| 88 |
<< " to: " << to_file.c_str(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** Create clone status file. |
| 93 |
@param[in] file_name file name */ |
| 94 |
static void create_file(std::string &file_name) { |
| 95 |
std::ofstream file(file_name.c_str()); |
| 96 |
|
| 97 |
if (file.is_open()) { |
| 98 |
file.close(); |
| 99 |
return; |
| 100 |
} |
| 101 |
ib::error(ER_IB_CLONE_STATUS_FILE) |
| 102 |
<< "Error creating file : " << file_name.c_str(); |
| 103 |
} |
| 104 |
|
| 105 |
/** Delete clone status file or directory. |
| 106 |
@param[in] file name of file */ |
| 107 |
static void remove_file(const std::string &file) { |
| 108 |
os_file_type_t file_type; |
| 109 |
|
| 110 |
if (!os_file_status(file.c_str(), nullptr, &file_type)) { |
| 111 |
ib::error(ER_IB_CLONE_STATUS_FILE) |
| 112 |
<< "Error checking a file to remove : " << file.c_str(); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
/* In C++17 there will be std::filesystem::remove_all and the |
| 117 |
code below will no longer be required. */ |
| 118 |
if (file_type == OS_FILE_TYPE_DIR) { |
| 119 |
auto scan_cbk = [](const char *path, const char *file_name) { |
| 120 |
if (strcmp(file_name, ".") == 0 || strcmp(file_name, "..") == 0) { |
| 121 |
return; |
| 122 |
} |
| 123 |
const auto to_remove = std::string{path} + OS_PATH_SEPARATOR + file_name; |
| 124 |
remove_file(to_remove); |
| 125 |
}; |
| 126 |
if (!os_file_scan_directory(file.c_str(), scan_cbk, true)) { |
| 127 |
ib::error(ER_IB_CLONE_STATUS_FILE) |
| 128 |
<< "Error removing directory : " << file.c_str(); |
| 129 |
} |
| 130 |
} else { |
| 131 |
/* Allow non existent file, as the server could have crashed or returned |
| 132 |
with error before creating the file. This is needed during error cleanup. */ |
| 133 |
if (!file_exists(file)) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
auto ret = std::remove(file.c_str()); |
| 138 |
|
| 139 |
if (ret != 0) { |
| 140 |
ib::error(ER_IB_CLONE_STATUS_FILE) |
| 141 |
<< "Error removing file : " << file.c_str(); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** Create clone in progress file and error file. |
| 147 |
@param[in] clone clone handle */ |
| 148 |
static void create_status_file(const Clone_Handle *clone) { |
| 149 |
const char *path = clone->get_datadir(); |
| 150 |
std::string file_name; |
| 151 |
|
| 152 |
if (clone->replace_datadir()) { |
| 153 |
/* Create error file for rollback. */ |
| 154 |
file_name.assign(CLONE_INNODB_ERROR_FILE); |
| 155 |
create_file(file_name); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
file_name.assign(path); |
| 160 |
/* Add path separator if needed. */ |
| 161 |
if (file_name.back() != OS_PATH_SEPARATOR) { |
| 162 |
file_name.append(OS_PATH_SEPARATOR_STR); |
| 163 |
} |
| 164 |
file_name.append(CLONE_INNODB_IN_PROGRESS_FILE); |
| 165 |
|
| 166 |
create_file(file_name); |
| 167 |
} |
| 168 |
|
| 169 |
/** Drop clone in progress file and error file. |
| 170 |
@param[in] clone clone handle */ |
| 171 |
static void drop_status_file(const Clone_Handle *clone) { |
| 172 |
const char *path = clone->get_datadir(); |
| 173 |
std::string file_name; |
| 174 |
|
| 175 |
if (clone->replace_datadir()) { |
| 176 |
/* Indicate that clone needs table fix up on recovery. */ |
| 177 |
file_name.assign(CLONE_INNODB_FIXUP_FILE); |
| 178 |
create_file(file_name); |
| 179 |
|
| 180 |
/* drop error file on success. */ |
| 181 |
file_name.assign(CLONE_INNODB_ERROR_FILE); |
| 182 |
remove_file(file_name); |
| 183 |
|
| 184 |
DBUG_EXECUTE_IF("clone_recovery_crash_point", { |
| 185 |
file_name.assign(CLONE_INNODB_RECOVERY_CRASH_POINT); |
| 186 |
create_file(file_name); |
| 187 |
}); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
std::string path_name(path); |
| 192 |
/* Add path separator if needed. */ |
| 193 |
if (path_name.back() != OS_PATH_SEPARATOR) { |
| 194 |
path_name.append(OS_PATH_SEPARATOR_STR); |
| 195 |
} |
| 196 |
|
| 197 |
/* Indicate that clone needs table fix up on recovery. */ |
| 198 |
file_name.assign(path_name); |
| 199 |
file_name.append(CLONE_INNODB_FIXUP_FILE); |
| 200 |
create_file(file_name); |
| 201 |
|
| 202 |
/* Indicate clone needs to update recovery status. */ |
| 203 |
file_name.assign(path_name); |
| 204 |
file_name.append(CLONE_INNODB_REPLACED_FILES); |
| 205 |
create_file(file_name); |
| 206 |
|
| 207 |
/* Mark successful clone operation. */ |
| 208 |
file_name.assign(path_name); |
| 209 |
file_name.append(CLONE_INNODB_IN_PROGRESS_FILE); |
| 210 |
remove_file(file_name); |
| 211 |
} |
| 212 |
|
| 213 |
void clone_init_list_files() { |
| 214 |
/* Remove any existing list files. */ |
| 215 |
std::string new_files(CLONE_INNODB_NEW_FILES); |
| 216 |
remove_file(new_files); |
| 217 |
|
| 218 |
std::string old_files(CLONE_INNODB_OLD_FILES); |
| 219 |
remove_file(old_files); |
| 220 |
|
| 221 |
std::string replaced_files(CLONE_INNODB_REPLACED_FILES); |
| 222 |
remove_file(replaced_files); |
| 223 |
|
| 224 |
std::string recovery_file(CLONE_INNODB_RECOVERY_FILE); |
| 225 |
remove_file(recovery_file); |
| 226 |
|
| 227 |
std::string ddl_file(CLONE_INNODB_DDL_FILES); |
| 228 |
remove_file(ddl_file); |
| 229 |
} |
| 230 |
|
| 231 |
void clone_remove_list_file(const char *file_name) { |
| 232 |
std::string list_file(file_name); |
| 233 |
remove_file(list_file); |
| 234 |
} |
| 235 |
|
| 236 |
int clone_add_to_list_file(const char *list_file_name, const char *file_name) { |
| 237 |
std::ofstream list_file; |
| 238 |
list_file.open(list_file_name, std::ofstream::app); |
| 239 |
|
| 240 |
if (list_file.is_open()) { |
| 241 |
list_file << file_name << std::endl; |
| 242 |
|
| 243 |
if (list_file.good()) { |
| 244 |
list_file.close(); |
| 245 |
return (0); |
| 246 |
} |
| 247 |
list_file.close(); |
| 248 |
} |
| 249 |
/* This is an error case. Either open or write call failed. */ |
| 250 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 251 |
my_error(ER_ERROR_ON_WRITE, MYF(0), list_file_name, errno, |
| 252 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 253 |
return (ER_ERROR_ON_WRITE); |
| 254 |
} |
| 255 |
|
| 256 |
/** Add redo log directory to the old file list. */ |
| 257 |
static void track_redo_files() { |
| 258 |
const auto path = log_directory_path(log_sys->m_files_ctx); |
| 259 |
|
| 260 |
/* Skip the path separator which is at the end. */ |
| 261 |
ut_ad(!path.empty()); |
| 262 |
ut_ad(path.back() == OS_PATH_SEPARATOR); |
| 263 |
const auto str = path.substr(0, path.size() - 1); |
| 264 |
|
| 265 |
clone_add_to_list_file(CLONE_INNODB_OLD_FILES, str.c_str()); |
| 266 |
} |
| 267 |
|
| 268 |
/** Execute sql statement. |
| 269 |
@param[in,out] thd current THD |
| 270 |
@param[in] sql_stmt SQL statement |
| 271 |
@param[in] thread_number executing thread number |
| 272 |
@param[in] skip_error skip statement on error |
| 273 |
@return false, if successful. */ |
| 274 |
static bool clone_execute_query(THD *thd, const char *sql_stmt, |
| 275 |
size_t thread_number, bool skip_error); |
| 276 |
|
| 277 |
/** Delete all binary logs before clone. |
| 278 |
@param[in] thd current THD |
| 279 |
@return error code */ |
| 280 |
static int clone_drop_binary_logs(THD *thd); |
| 281 |
|
| 282 |
/** Drop all user data before starting clone. |
| 283 |
@param[in,out] thd current THD |
| 284 |
@param[in] allow_threads allow multiple threads |
| 285 |
@return error code */ |
| 286 |
static int clone_drop_user_data(THD *thd, bool allow_threads); |
| 287 |
|
| 288 |
/** Initialize transparent page compression in innodb space by checking |
| 289 |
all innodb tables in DD. Usually this initialization is done later when |
| 290 |
user opens a table. Clone needs to read this from innodb space object. |
| 291 |
@param[in,out] thd session THD */ |
| 292 |
static void clone_init_compression(THD *thd); |
| 293 |
|
| 294 |
/** Open all Innodb tablespaces. |
| 295 |
@param[in,out] thd session THD |
| 296 |
@return error code. */ |
| 297 |
static int clone_init_tablespaces(THD *thd); |
| 298 |
|
| 299 |
/** Set security context to skip privilege check. |
| 300 |
@param[in,out] thd session THD |
| 301 |
@param[in,out] sctx security context */ |
| 302 |
static void skip_grants(THD *thd, Security_context &sctx) { |
| 303 |
/* Take care of the possible side effect of skipping grant i.e. |
| 304 |
setting SYSTEM_USER privilege flag. */ |
| 305 |
bool saved_flag = thd->is_system_user(); |
| 306 |
sctx.skip_grants(); |
| 307 |
ut_ad(thd->is_system_user() == saved_flag); |
| 308 |
thd->set_system_user(saved_flag); |
| 309 |
} |
| 310 |
|
| 311 |
void innodb_clone_get_capability(Ha_clone_flagset &flags) { |
| 312 |
flags.reset(); |
| 313 |
|
| 314 |
flags.set(HA_CLONE_HYBRID); |
| 315 |
flags.set(HA_CLONE_MULTI_TASK); |
| 316 |
flags.set(HA_CLONE_RESTART); |
| 317 |
} |
| 318 |
|
| 319 |
/** Check if clone can be started. |
| 320 |
@param[in,out] thd session THD |
| 321 |
@return error code. */ |
| 322 |
static int clone_begin_check(THD *thd) { |
| 323 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 324 |
int err = 0; |
| 325 |
|
| 326 |
if (!mtr_t::s_logging.is_enabled()) { |
| 327 |
err = ER_INNODB_REDO_DISABLED; |
| 328 |
|
| 329 |
} else if (Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 330 |
err = ER_CLONE_DDL_IN_PROGRESS; |
| 331 |
} |
| 332 |
|
| 333 |
if (err != 0 && thd != nullptr) { |
| 334 |
my_error(err, MYF(0)); |
| 335 |
} |
| 336 |
return err; |
| 337 |
} |
| 338 |
|
| 339 |
/** Get clone timeout configuration value. |
| 340 |
@param[in,out] thd server thread handle |
| 341 |
@param[in] config_name timeout configuration name |
| 342 |
@param[out] timeout timeout value |
| 343 |
@return true iff successful. */ |
| 344 |
static bool get_clone_timeout_config(THD *thd, const std::string &config_name, |
| 345 |
int &timeout) { |
| 346 |
timeout = 0; |
| 347 |
|
| 348 |
ut_ad(clone_protocol_svc != nullptr); |
| 349 |
if (clone_protocol_svc == nullptr) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
/* Get timeout configuration in string format and convert to integer. |
| 354 |
Currently there is no interface to get the integer value directly. The |
| 355 |
variable is in clone plugin and innodb cannot access it directly. */ |
| 356 |
Mysql_Clone_Key_Values timeout_confs = {{config_name, ""}}; |
| 357 |
|
| 358 |
auto err = clone_protocol_svc->mysql_clone_get_configs(thd, timeout_confs); |
| 359 |
|
| 360 |
std::string err_str("Error reading configuration: "); |
| 361 |
err_str.append(config_name); |
| 362 |
|
| 363 |
if (err != 0) { |
| 364 |
ib::error(ER_IB_CLONE_INTERNAL) << err_str; |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
try { |
| 369 |
timeout = std::stoi(timeout_confs[0].second); |
| 370 |
} catch (const std::exception &e) { |
| 371 |
err_str.append(" Exception: "); |
| 372 |
err_str.append(e.what()); |
| 373 |
ib::error(ER_IB_CLONE_INTERNAL) << err_str; |
| 374 |
ut_d(ut_error); |
| 375 |
ut_o(return false); |
| 376 |
} |
| 377 |
|
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
/** Timeout while waiting for DDL commands. |
| 382 |
@param[in,out] thd server thread handle |
| 383 |
@return donor timeout in seconds. */ |
| 384 |
static int get_ddl_timeout(THD *thd) { |
| 385 |
int timeout = 0; |
| 386 |
std::string config_timeout("clone_ddl_timeout"); |
| 387 |
|
| 388 |
if (!get_clone_timeout_config(thd, config_timeout, timeout)) { |
| 389 |
/* Default to five minutes in case error reading configuration. */ |
| 390 |
timeout = 300; |
| 391 |
} |
| 392 |
return timeout; |
| 393 |
} |
| 394 |
|
| 395 |
int innodb_clone_begin(handlerton *, THD *thd, const byte *&loc, uint &loc_len, |
| 396 |
uint &task_id, Ha_clone_type type, Ha_clone_mode mode) { |
| 397 |
/* Check if reference locator is valid */ |
| 398 |
if (loc != nullptr && !clone_validate_locator(loc, loc_len)) { |
| 399 |
int err = ER_CLONE_PROTOCOL; |
| 400 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Locator"); |
| 401 |
return (err); |
| 402 |
} |
| 403 |
|
| 404 |
/* Acquire clone system mutex which would automatically get released |
| 405 |
when we return from the function [RAII]. */ |
| 406 |
IB_mutex_guard sys_mutex(clone_sys->get_mutex(), UT_LOCATION_HERE); |
| 407 |
|
| 408 |
/* Check if concurrent ddl has marked abort. */ |
| 409 |
int err = clone_begin_check(thd); |
| 410 |
|
| 411 |
if (err != 0) { |
| 412 |
return err; |
| 413 |
} |
| 414 |
|
| 415 |
/* Check of clone is already in progress for the reference locator. */ |
| 416 |
auto clone_hdl = clone_sys->find_clone(loc, loc_len, CLONE_HDL_COPY); |
| 417 |
|
| 418 |
switch (mode) { |
| 419 |
case HA_CLONE_MODE_RESTART: |
| 420 |
/* Error out if existing clone is not found */ |
| 421 |
if (clone_hdl == nullptr) { |
| 422 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 423 |
"Innodb Clone Restart could not find existing clone"); |
| 424 |
return (ER_INTERNAL_ERROR); |
| 425 |
} |
| 426 |
|
| 427 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Begin Master Task: Restart"; |
| 428 |
err = clone_hdl->restart_copy(thd, loc, loc_len); |
| 429 |
|
| 430 |
break; |
| 431 |
|
| 432 |
case HA_CLONE_MODE_START: { |
| 433 |
/* Should not find existing clone for the locator */ |
| 434 |
if (clone_hdl != nullptr) { |
| 435 |
clone_sys->drop_clone(clone_hdl); |
| 436 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 437 |
"Innodb Clone Begin refers existing clone"); |
| 438 |
return (ER_INTERNAL_ERROR); |
| 439 |
} |
| 440 |
LEX_CSTRING sctx_user = thd->m_main_security_ctx.user(); |
| 441 |
LEX_CSTRING sctx_host = thd->m_main_security_ctx.host_or_ip(); |
| 442 |
|
| 443 |
/* Should not become a donor when provisioning is started. */ |
| 444 |
if (Clone_handler::is_provisioning()) { |
| 445 |
if (0 == strcmp(my_localhost, sctx_host.str)) { |
| 446 |
my_error(ER_CLONE_LOOPBACK, MYF(0)); |
| 447 |
return (ER_CLONE_LOOPBACK); |
| 448 |
} |
| 449 |
my_error(ER_CLONE_TOO_MANY_CONCURRENT_CLONES, MYF(0), MAX_CLONES); |
| 450 |
return (ER_CLONE_TOO_MANY_CONCURRENT_CLONES); |
| 451 |
} |
| 452 |
|
| 453 |
/* Log user and host beginning clone operation. */ |
| 454 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Begin Master Task by " |
| 455 |
<< sctx_user.str << "@" << sctx_host.str; |
| 456 |
break; |
| 457 |
} |
| 458 |
|
| 459 |
case HA_CLONE_MODE_ADD_TASK: |
| 460 |
/* Should find existing clone for the locator */ |
| 461 |
if (clone_hdl == nullptr) { |
| 462 |
/* Operation has finished already */ |
| 463 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 464 |
"Innodb Clone add task refers non-existing clone"); |
| 465 |
|
| 466 |
return (ER_INTERNAL_ERROR); |
| 467 |
} |
| 468 |
break; |
| 469 |
|
| 470 |
case HA_CLONE_MODE_VERSION: |
| 471 |
case HA_CLONE_MODE_MAX: |
| 472 |
default: |
| 473 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Innodb Clone Begin Invalid Mode"); |
| 474 |
|
| 475 |
ut_d(ut_error); |
| 476 |
ut_o(return (ER_INTERNAL_ERROR)); |
| 477 |
} |
| 478 |
|
| 479 |
if (clone_hdl == nullptr) { |
| 480 |
ut_ad(thd != nullptr); |
| 481 |
ut_ad(mode == HA_CLONE_MODE_START); |
| 482 |
|
| 483 |
/* Create new clone handle for copy. Reference locator |
| 484 |
is used for matching the version. */ |
| 485 |
auto err = clone_sys->add_clone(loc, CLONE_HDL_COPY, clone_hdl); |
| 486 |
if (err != 0) { |
| 487 |
return (err); |
| 488 |
} |
| 489 |
|
| 490 |
err = clone_hdl->init(loc, loc_len, type, nullptr); |
| 491 |
|
| 492 |
/* Check and wait if clone is marked for wait. */ |
| 493 |
if (err == 0) { |
| 494 |
auto timeout = get_ddl_timeout(thd); |
| 495 |
/* zero timeout is special mode when DDL can abort running clone. */ |
| 496 |
if (timeout == 0) { |
| 497 |
clone_hdl->set_ddl_abort(); |
| 498 |
} |
| 499 |
err = clone_sys->wait_for_free(thd); |
| 500 |
} |
| 501 |
|
| 502 |
/* Re-check for initial errors as we could have released sys mutex |
| 503 |
before allocating clone handle. */ |
| 504 |
if (err == 0) { |
| 505 |
err = clone_begin_check(thd); |
| 506 |
} |
| 507 |
|
| 508 |
if (err != 0) { |
| 509 |
clone_sys->drop_clone(clone_hdl); |
| 510 |
return (err); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
/* Add new task for the clone copy operation. */ |
| 515 |
if (err == 0) { |
| 516 |
/* Release clone system mutex here as we might need to wait while |
| 517 |
adding task. It is safe as the clone handle is acquired and cannot |
| 518 |
be freed till we release it. */ |
| 519 |
mutex_exit(clone_sys->get_mutex()); |
| 520 |
err = clone_hdl->add_task(thd, nullptr, 0, task_id); |
| 521 |
|
| 522 |
/* 1. Open all tablespaces in Innodb if not done during bootstrap. |
| 523 |
2. Initialize compression option for all compressed tablespaces. */ |
| 524 |
if (err == 0 && task_id == 0) { |
| 525 |
err = clone_init_tablespaces(thd); |
| 526 |
|
| 527 |
if (err == 0) { |
| 528 |
clone_init_compression(thd); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
mutex_enter(clone_sys->get_mutex()); |
| 533 |
} |
| 534 |
|
| 535 |
if (err != 0) { |
| 536 |
clone_sys->drop_clone(clone_hdl); |
| 537 |
return (err); |
| 538 |
} |
| 539 |
|
| 540 |
if (task_id > 0) { |
| 541 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Begin Task ID: " << task_id; |
| 542 |
} |
| 543 |
|
| 544 |
/* Get the current locator from clone handle. */ |
| 545 |
loc = clone_hdl->get_locator(loc_len); |
| 546 |
return (0); |
| 547 |
} |
| 548 |
|
| 549 |
int innodb_clone_copy(handlerton *hton, THD *thd, const byte *loc, uint loc_len, |
| 550 |
uint task_id, Ha_clone_cbk *cbk) { |
| 551 |
cbk->set_hton(hton); |
| 552 |
|
| 553 |
/* Get clone handle by locator index. */ |
| 554 |
auto clone_hdl = clone_sys->get_clone_by_index(loc, loc_len); |
| 555 |
|
| 556 |
auto err = clone_hdl->check_error(thd); |
| 557 |
if (err != 0) { |
| 558 |
return (err); |
| 559 |
} |
| 560 |
|
| 561 |
/* Start data copy. */ |
| 562 |
err = clone_hdl->copy(task_id, cbk); |
| 563 |
clone_hdl->save_error(err); |
| 564 |
|
| 565 |
return (err); |
| 566 |
} |
| 567 |
|
| 568 |
int innodb_clone_ack(handlerton *hton, THD *thd, const byte *loc, uint loc_len, |
| 569 |
uint task_id, int in_err, Ha_clone_cbk *cbk) { |
| 570 |
cbk->set_hton(hton); |
| 571 |
|
| 572 |
/* Check if reference locator is valid */ |
| 573 |
if (loc != nullptr && !clone_validate_locator(loc, loc_len)) { |
| 574 |
int err = ER_CLONE_PROTOCOL; |
| 575 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Locator"); |
| 576 |
return (err); |
| 577 |
} |
| 578 |
mutex_enter(clone_sys->get_mutex()); |
| 579 |
|
| 580 |
/* Find attach clone handle using the reference locator. */ |
| 581 |
auto clone_hdl = clone_sys->find_clone(loc, loc_len, CLONE_HDL_COPY); |
| 582 |
|
| 583 |
mutex_exit(clone_sys->get_mutex()); |
| 584 |
|
| 585 |
/* Must find existing clone for the locator */ |
| 586 |
if (clone_hdl == nullptr) { |
| 587 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 588 |
|
| 589 |
"Innodb Clone ACK refers non-existing clone"); |
| 590 |
return (ER_INTERNAL_ERROR); |
| 591 |
} |
| 592 |
|
| 593 |
int err = 0; |
| 594 |
|
| 595 |
/* If thread is interrupted, then set interrupt error instead. */ |
| 596 |
if (thd_killed(thd)) { |
| 597 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 598 |
in_err = ER_QUERY_INTERRUPTED; |
| 599 |
} |
| 600 |
|
| 601 |
if (in_err == 0) { |
| 602 |
/* Apply acknowledged data */ |
| 603 |
err = clone_hdl->apply(thd, task_id, cbk); |
| 604 |
|
| 605 |
clone_hdl->save_error(err); |
| 606 |
} else { |
| 607 |
/* For error input, return after saving it */ |
| 608 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone set error ACK: " << in_err; |
| 609 |
clone_hdl->save_error(in_err); |
| 610 |
} |
| 611 |
|
| 612 |
mutex_enter(clone_sys->get_mutex()); |
| 613 |
|
| 614 |
/* Detach from clone handle */ |
| 615 |
clone_sys->drop_clone(clone_hdl); |
| 616 |
|
| 617 |
mutex_exit(clone_sys->get_mutex()); |
| 618 |
|
| 619 |
return (err); |
| 620 |
} |
| 621 |
|
| 622 |
/** Timeout while waiting for recipient after network failure. |
| 623 |
@param[in,out] thd server thread handle |
| 624 |
@return donor timeout in minutes. */ |
| 625 |
static Clone_Min get_donor_timeout(THD *thd) { |
| 626 |
int timeout = 0; |
| 627 |
std::string config_timeout("clone_donor_timeout_after_network_failure"); |
| 628 |
|
| 629 |
if (!get_clone_timeout_config(thd, config_timeout, timeout)) { |
| 630 |
/* Default to five minutes in case error reading configuration. */ |
| 631 |
timeout = 5; |
| 632 |
} |
| 633 |
return Clone_Min(timeout); |
| 634 |
} |
| 635 |
|
| 636 |
int innodb_clone_end(handlerton *, THD *thd, const byte *loc, uint loc_len, |
| 637 |
uint task_id, int in_err) { |
| 638 |
/* Acquire clone system mutex which would automatically get released |
| 639 |
when we return from the function [RAII]. */ |
| 640 |
IB_mutex_guard sys_mutex(clone_sys->get_mutex(), UT_LOCATION_HERE); |
| 641 |
|
| 642 |
/* Get clone handle by locator index. */ |
| 643 |
auto clone_hdl = clone_sys->get_clone_by_index(loc, loc_len); |
| 644 |
|
| 645 |
/* If thread is interrupted, then set interrupt error instead. */ |
| 646 |
if (thd_killed(thd)) { |
| 647 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 648 |
in_err = ER_QUERY_INTERRUPTED; |
| 649 |
} |
| 650 |
/* Set error, if already not set */ |
| 651 |
clone_hdl->save_error(in_err); |
| 652 |
|
| 653 |
/* Drop current task. */ |
| 654 |
bool is_master = false; |
| 655 |
auto wait_reconnect = clone_hdl->drop_task(thd, task_id, is_master); |
| 656 |
auto is_copy = clone_hdl->is_copy_clone(); |
| 657 |
auto is_init = clone_hdl->is_init(); |
| 658 |
auto is_abort = clone_hdl->is_abort(); |
| 659 |
|
| 660 |
if (!wait_reconnect || is_abort) { |
| 661 |
if (is_copy && is_master) { |
| 662 |
if (is_abort) { |
| 663 |
ib::info(ER_IB_CLONE_RESTART) |
| 664 |
<< "Clone Master aborted by concurrent clone"; |
| 665 |
clone_hdl->set_abort(); |
| 666 |
} else if (in_err != 0) { |
| 667 |
/* Make sure re-start attempt fails immediately */ |
| 668 |
clone_hdl->set_abort(); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
if (!is_copy && !is_init && is_master) { |
| 673 |
if (in_err == 0) { |
| 674 |
/* On success for apply handle, drop status file. */ |
| 675 |
drop_status_file(clone_hdl); |
| 676 |
} else if (clone_hdl->replace_datadir()) { |
| 677 |
/* On failure, rollback if replacing current data directory. */ |
| 678 |
clone_files_error(); |
| 679 |
} |
| 680 |
} |
| 681 |
clone_sys->drop_clone(clone_hdl); |
| 682 |
|
| 683 |
auto da = thd->get_stmt_da(); |
| 684 |
ib::info(ER_IB_CLONE_START_STOP) |
| 685 |
<< "Clone" |
| 686 |
<< (is_copy ? " End" : (is_init ? " Apply Version End" : " Apply End")) |
| 687 |
<< (is_master ? " Master" : "") << " Task ID: " << task_id |
| 688 |
<< (in_err != 0 ? " Failed, code: " : " Passed, code: ") << in_err |
| 689 |
<< ": " |
| 690 |
<< ((in_err == 0 || da == nullptr || !da->is_error()) |
| 691 |
? "" |
| 692 |
: da->message_text()); |
| 693 |
return (0); |
| 694 |
} |
| 695 |
|
| 696 |
ut_ad(clone_hdl->is_copy_clone()); |
| 697 |
ut_ad(is_master); |
| 698 |
|
| 699 |
auto da = thd->get_stmt_da(); |
| 700 |
ib::info(ER_IB_CLONE_RESTART) |
| 701 |
<< "Clone Master n/w error code: " << in_err << ": " |
| 702 |
<< ((da == nullptr || !da->is_error()) ? "" : da->message_text()); |
| 703 |
|
| 704 |
auto time_out = get_donor_timeout(thd); |
| 705 |
|
| 706 |
if (time_out.count() <= 0) { |
| 707 |
ib::info(ER_IB_CLONE_RESTART) |
| 708 |
<< "Clone Master Skip wait after n/w error. Dropping Snapshot."; |
| 709 |
clone_sys->drop_clone(clone_hdl); |
| 710 |
return (0); |
| 711 |
} |
| 712 |
|
| 713 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Master wait " << time_out.count() |
| 714 |
<< " minutes for restart after n/w error"; |
| 715 |
|
| 716 |
/* Set state to idle and wait for re-connect */ |
| 717 |
clone_hdl->set_state(CLONE_STATE_IDLE); |
| 718 |
/* Sleep for 1 second */ |
| 719 |
Clone_Msec sleep_time(Clone_Sec(1)); |
| 720 |
/* Generate alert message every minute. */ |
| 721 |
Clone_Sec alert_interval(Clone_Min(1)); |
| 722 |
|
| 723 |
/* Wait for client to reconnect back */ |
| 724 |
bool is_timeout = false; |
| 725 |
auto err = Clone_Sys::wait( |
| 726 |
sleep_time, time_out, alert_interval, |
| 727 |
[&](bool alert, bool &result) { |
| 728 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 729 |
result = !clone_hdl->is_active(); |
| 730 |
|
| 731 |
if (thd_killed(thd) || clone_hdl->is_interrupted()) { |
| 732 |
ib::info(ER_IB_CLONE_RESTART) |
| 733 |
<< "Clone End Master wait for Restart interrupted"; |
| 734 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 735 |
return (ER_QUERY_INTERRUPTED); |
| 736 |
|
| 737 |
} else if (Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 738 |
ib::info(ER_IB_CLONE_RESTART) |
| 739 |
<< "Clone End Master wait for Restart aborted by DDL"; |
| 740 |
my_error(ER_CLONE_DDL_IN_PROGRESS, MYF(0)); |
| 741 |
return (ER_CLONE_DDL_IN_PROGRESS); |
| 742 |
|
| 743 |
} else if (clone_hdl->is_abort()) { |
| 744 |
result = false; |
| 745 |
ib::info(ER_IB_CLONE_RESTART) << "Clone End Master wait for Restart" |
| 746 |
" aborted by concurrent clone"; |
| 747 |
return (0); |
| 748 |
} |
| 749 |
|
| 750 |
if (!result) { |
| 751 |
ib::info(ER_IB_CLONE_RESTART) |
| 752 |
<< "Clone Master restarted successfully by " |
| 753 |
"other task after n/w failure"; |
| 754 |
|
| 755 |
} else if (alert) { |
| 756 |
ib::info(ER_IB_CLONE_RESTART) |
| 757 |
<< "Clone Master still waiting for restart"; |
| 758 |
} |
| 759 |
return (0); |
| 760 |
}, |
| 761 |
clone_sys->get_mutex(), is_timeout); |
| 762 |
|
| 763 |
if (err == 0 && is_timeout && clone_hdl->is_idle()) { |
| 764 |
ib::info(ER_IB_CLONE_TIMEOUT) |
| 765 |
<< "Clone End Master wait " |
| 766 |
"for restart timed out after " |
| 767 |
<< time_out.count() << " minutes. Dropping Snapshot"; |
| 768 |
} |
| 769 |
|
| 770 |
/* If Clone snapshot is not restarted, at this point mark it for |
| 771 |
abort and end the snapshot to allow any waiting DDL to unpin the |
| 772 |
handle and exit. */ |
| 773 |
if (!clone_hdl->is_active()) { |
| 774 |
ut_ad(err != 0 || is_timeout); |
| 775 |
clone_hdl->set_abort(); |
| 776 |
} |
| 777 |
|
| 778 |
/* Last task should drop the clone handle. */ |
| 779 |
clone_sys->drop_clone(clone_hdl); |
| 780 |
return (0); |
| 781 |
} |
| 782 |
|
| 783 |
int innodb_clone_apply_begin(handlerton *, THD *thd, const byte *&loc, |
| 784 |
uint &loc_len, uint &task_id, Ha_clone_mode mode, |
| 785 |
const char *data_dir) { |
| 786 |
/* Check if reference locator is valid */ |
| 787 |
if (loc != nullptr && !clone_validate_locator(loc, loc_len)) { |
| 788 |
int err = ER_CLONE_PROTOCOL; |
| 789 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Locator"); |
| 790 |
return (err); |
| 791 |
} |
| 792 |
|
| 793 |
/* Acquire clone system mutex which would automatically get released |
| 794 |
when we return from the function [RAII]. */ |
| 795 |
IB_mutex_guard sys_mutex(clone_sys->get_mutex(), UT_LOCATION_HERE); |
| 796 |
|
| 797 |
/* Check if clone is already in progress for the reference locator. */ |
| 798 |
auto clone_hdl = clone_sys->find_clone(loc, loc_len, CLONE_HDL_APPLY); |
| 799 |
|
| 800 |
switch (mode) { |
| 801 |
case HA_CLONE_MODE_RESTART: { |
| 802 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Apply Begin Master Task: Restart"; |
| 803 |
auto err = clone_hdl->restart_apply(thd, loc, loc_len); |
| 804 |
|
| 805 |
/* Reduce reference count */ |
| 806 |
clone_sys->drop_clone(clone_hdl); |
| 807 |
|
| 808 |
/* Restart is done by master task */ |
| 809 |
ut_ad(task_id == 0); |
| 810 |
task_id = 0; |
| 811 |
|
| 812 |
return (err); |
| 813 |
} |
| 814 |
case HA_CLONE_MODE_START: |
| 815 |
|
| 816 |
if (clone_hdl != nullptr) { |
| 817 |
clone_sys->drop_clone(clone_hdl); |
| 818 |
ib::error(ER_IB_CLONE_INTERNAL) |
| 819 |
<< "Clone Apply Begin Master found duplicate clone"; |
| 820 |
clone_hdl = nullptr; |
| 821 |
ut_d(ut_error); |
| 822 |
} |
| 823 |
|
| 824 |
/* Check if the locator is from current mysqld server. */ |
| 825 |
clone_hdl = clone_sys->find_clone(loc, loc_len, CLONE_HDL_COPY); |
| 826 |
|
| 827 |
if (clone_hdl != nullptr) { |
| 828 |
clone_sys->drop_clone(clone_hdl); |
| 829 |
clone_hdl = nullptr; |
| 830 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Apply Master Loop Back"; |
| 831 |
ut_ad(data_dir != nullptr); |
| 832 |
} |
| 833 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Apply Begin Master Task"; |
| 834 |
break; |
| 835 |
|
| 836 |
case HA_CLONE_MODE_ADD_TASK: |
| 837 |
/* Should find existing clone for the locator */ |
| 838 |
if (clone_hdl == nullptr) { |
| 839 |
/* Operation has finished already */ |
| 840 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 841 |
"Innodb Clone Apply add task to non-existing clone"); |
| 842 |
|
| 843 |
return (ER_INTERNAL_ERROR); |
| 844 |
} |
| 845 |
break; |
| 846 |
|
| 847 |
case HA_CLONE_MODE_VERSION: |
| 848 |
/* Cannot have input locator or existing clone */ |
| 849 |
ib::info(ER_IB_CLONE_START_STOP) |
| 850 |
<< "Clone Apply Begin Master Version Check"; |
| 851 |
ut_ad(loc == nullptr); |
| 852 |
ut_ad(clone_hdl == nullptr); |
| 853 |
break; |
| 854 |
|
| 855 |
case HA_CLONE_MODE_MAX: |
| 856 |
default: |
| 857 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 858 |
"Innodb Clone Appply Begin Invalid Mode"); |
| 859 |
|
| 860 |
ut_d(ut_error); |
| 861 |
ut_o(return (ER_INTERNAL_ERROR)); |
| 862 |
} |
| 863 |
|
| 864 |
if (clone_hdl == nullptr) { |
| 865 |
ut_ad(thd != nullptr); |
| 866 |
|
| 867 |
ut_ad(mode == HA_CLONE_MODE_VERSION || mode == HA_CLONE_MODE_START); |
| 868 |
|
| 869 |
/* Create new clone handle for apply. Reference locator |
| 870 |
is used for matching the version. */ |
| 871 |
auto err = clone_sys->add_clone(loc, CLONE_HDL_APPLY, clone_hdl); |
| 872 |
if (err != 0) { |
| 873 |
return (err); |
| 874 |
} |
| 875 |
|
| 876 |
err = clone_hdl->init(loc, loc_len, HA_CLONE_HYBRID, data_dir); |
| 877 |
|
| 878 |
if (err != 0) { |
| 879 |
clone_sys->drop_clone(clone_hdl); |
| 880 |
return (err); |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
if (clone_hdl->is_active()) { |
| 885 |
/* Release clone system mutex here as we might need to wait while |
| 886 |
adding task. It is safe as the clone handle is acquired and cannot |
| 887 |
be freed till we release it. */ |
| 888 |
mutex_exit(clone_sys->get_mutex()); |
| 889 |
|
| 890 |
/* Create status file to indicate active clone directory. */ |
| 891 |
if (mode == HA_CLONE_MODE_START) { |
| 892 |
create_status_file(clone_hdl); |
| 893 |
} |
| 894 |
|
| 895 |
int err = 0; |
| 896 |
/* Drop any user data after acquiring backup lock. Don't allow |
| 897 |
concurrent threads as the BACKUP MDL lock would not allow any |
| 898 |
other threads to execute DDL. */ |
| 899 |
if (clone_hdl->replace_datadir() && mode == HA_CLONE_MODE_START) { |
| 900 |
/* Safeguard to throw error if innodb read only mode is on. Currently |
| 901 |
not reachable as we would get error much earlier while dropping user |
| 902 |
tables. */ |
| 903 |
if (srv_read_only_mode) { |
| 904 |
err = ER_INTERNAL_ERROR; |
| 905 |
my_error(err, MYF(0), |
| 906 |
"Clone cannot replace data with innodb_read_only = ON"); |
| 907 |
ut_d(ut_error); |
| 908 |
} else { |
| 909 |
track_redo_files(); |
| 910 |
err = clone_drop_user_data(thd, false); |
| 911 |
if (err != 0) { |
| 912 |
clone_files_error(); |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
/* Add new task for the clone apply operation. */ |
| 918 |
if (err == 0) { |
| 919 |
ut_ad(loc != nullptr); |
| 920 |
err = clone_hdl->add_task(thd, loc, loc_len, task_id); |
| 921 |
} |
| 922 |
mutex_enter(clone_sys->get_mutex()); |
| 923 |
|
| 924 |
if (err != 0) { |
| 925 |
clone_sys->drop_clone(clone_hdl); |
| 926 |
return (err); |
| 927 |
} |
| 928 |
|
| 929 |
} else { |
| 930 |
ut_ad(mode == HA_CLONE_MODE_VERSION); |
| 931 |
|
| 932 |
/* Set all clone status files empty. */ |
| 933 |
if (clone_hdl->replace_datadir()) { |
| 934 |
clone_init_list_files(); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
if (task_id > 0) { |
| 939 |
ib::info(ER_IB_CLONE_START_STOP) |
| 940 |
<< "Clone Apply Begin Task ID: " << task_id; |
| 941 |
} |
| 942 |
/* Get the current locator from clone handle. */ |
| 943 |
if (mode != HA_CLONE_MODE_ADD_TASK) { |
| 944 |
loc = clone_hdl->get_locator(loc_len); |
| 945 |
} |
| 946 |
return (0); |
| 947 |
} |
| 948 |
|
| 949 |
int innodb_clone_apply(handlerton *hton, THD *thd, const byte *loc, |
| 950 |
uint loc_len, uint task_id, int in_err, |
| 951 |
Ha_clone_cbk *cbk) { |
| 952 |
/* Get clone handle by locator index. */ |
| 953 |
auto clone_hdl = clone_sys->get_clone_by_index(loc, loc_len); |
| 954 |
ut_ad(in_err != 0 || cbk != nullptr); |
| 955 |
|
| 956 |
/* For error input, return after saving it */ |
| 957 |
if (in_err != 0 || cbk == nullptr) { |
| 958 |
clone_hdl->save_error(in_err); |
| 959 |
auto da = thd->get_stmt_da(); |
| 960 |
ib::info(ER_IB_CLONE_OPERATION) |
| 961 |
<< "Clone Apply set error code: " << in_err << ": " |
| 962 |
<< ((in_err == 0 || da == nullptr || !da->is_error()) |
| 963 |
? "" |
| 964 |
: da->message_text()); |
| 965 |
return (0); |
| 966 |
} |
| 967 |
|
| 968 |
cbk->set_hton(hton); |
| 969 |
auto err = clone_hdl->check_error(thd); |
| 970 |
if (err != 0) { |
| 971 |
return (err); |
| 972 |
} |
| 973 |
|
| 974 |
/* Apply data received from callback. */ |
| 975 |
err = clone_hdl->apply(thd, task_id, cbk); |
| 976 |
clone_hdl->save_error(err); |
| 977 |
|
| 978 |
return (err); |
| 979 |
} |
| 980 |
|
| 981 |
int innodb_clone_apply_end(handlerton *hton, THD *thd, const byte *loc, |
| 982 |
uint loc_len, uint task_id, int in_err) { |
| 983 |
auto err = innodb_clone_end(hton, thd, loc, loc_len, task_id, in_err); |
| 984 |
return (err); |
| 985 |
} |
| 986 |
|
| 987 |
/* Logical bitmap for clone file state. */ |
| 988 |
|
| 989 |
/** Data file is found. */ |
| 990 |
const int FILE_DATA = 1; |
| 991 |
/** Saved data file is found */ |
| 992 |
const int FILE_SAVED = 10; |
| 993 |
/** Cloned data file is found */ |
| 994 |
const int FILE_CLONED = 100; |
| 995 |
|
| 996 |
/** NONE state: file not present. */ |
| 997 |
const int FILE_STATE_NONE = 0; |
| 998 |
/** Normal state: only data file is present. */ |
| 999 |
const int FILE_STATE_NORMAL = FILE_DATA; |
| 1000 |
/** Saved state: only saved data file is present. */ |
| 1001 |
const int FILE_STATE_SAVED = FILE_SAVED; |
| 1002 |
/** Cloned state: data file and cloned data file are present. */ |
| 1003 |
const int FILE_STATE_CLONED = FILE_DATA + FILE_CLONED; |
| 1004 |
/** Saved clone state: saved data file and cloned data file are present. */ |
| 1005 |
const int FILE_STATE_CLONE_SAVED = FILE_SAVED + FILE_CLONED; |
| 1006 |
/** Replaced state: saved data file and data file are present. */ |
| 1007 |
const int FILE_STATE_REPLACED = FILE_SAVED + FILE_DATA; |
| 1008 |
|
| 1009 |
/* Clone data File state transfer. |
| 1010 |
[FILE_STATE_NORMAL] --> [FILE_STATE_CLONED] |
| 1011 |
Remote data is cloned into another file named <file_name>.clone. |
| 1012 |
|
| 1013 |
[FILE_STATE_CLONED] --> [FILE_STATE_CLONE_SAVED] |
| 1014 |
Before recovery the datafile is saved in a file named <file_name>.save. |
| 1015 |
|
| 1016 |
[FILE_STATE_CLONE_SAVED] --> [FILE_STATE_REPLACED] |
| 1017 |
Before recovery the cloned file is moved to datafile. |
| 1018 |
|
| 1019 |
[FILE_STATE_REPLACED] --> [FILE_STATE_NORMAL] |
| 1020 |
After successful recovery the saved data file is removed. |
| 1021 |
|
| 1022 |
Every state transition involves a single file create, delete or rename and |
| 1023 |
we consider them atomic. In case of a failure the state rolls back exactly |
| 1024 |
in reverse order. |
| 1025 |
*/ |
| 1026 |
|
| 1027 |
/** Get current state of a clone file. |
| 1028 |
@param[in] data_file data file name |
| 1029 |
@return current file state. */ |
| 1030 |
static int get_file_state(const std::string &data_file) { |
| 1031 |
int state = 0; |
| 1032 |
/* Check if data file is there. */ |
| 1033 |
if (os_file_exists(data_file.c_str())) { |
| 1034 |
state += FILE_DATA; |
| 1035 |
} |
| 1036 |
|
| 1037 |
std::string saved_file(data_file); |
| 1038 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1039 |
|
| 1040 |
/* Check if saved old file is there. */ |
| 1041 |
if (os_file_exists(saved_file.c_str())) { |
| 1042 |
state += FILE_SAVED; |
| 1043 |
} |
| 1044 |
|
| 1045 |
std::string cloned_file(data_file); |
| 1046 |
cloned_file.append(CLONE_INNODB_REPLACED_FILE_EXTN); |
| 1047 |
|
| 1048 |
/* Check if cloned file is there. */ |
| 1049 |
if (os_file_exists(cloned_file.c_str())) { |
| 1050 |
state += FILE_CLONED; |
| 1051 |
} |
| 1052 |
return (state); |
| 1053 |
} |
| 1054 |
|
| 1055 |
/** Roll forward clone file state till final state. |
| 1056 |
@param[in] data_file data file name |
| 1057 |
@param[in] final_state data file state to forward to |
| 1058 |
@return previous file state before roll forward. */ |
| 1059 |
static int file_roll_forward(const std::string &data_file, int final_state) { |
| 1060 |
auto cur_state = get_file_state(data_file); |
| 1061 |
|
| 1062 |
switch (cur_state) { |
| 1063 |
case FILE_STATE_CLONED: { |
| 1064 |
if (final_state == FILE_STATE_CLONED) { |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
/* Save data file */ |
| 1068 |
std::string saved_file(data_file); |
| 1069 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1070 |
rename_file(data_file, saved_file); |
| 1071 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1072 |
<< "Clone File Roll Forward: Save data file " << data_file |
| 1073 |
<< " state: " << cur_state; |
| 1074 |
} |
| 1075 |
[[fallthrough]]; |
| 1076 |
|
| 1077 |
case FILE_STATE_CLONE_SAVED: { |
| 1078 |
if (final_state == FILE_STATE_CLONE_SAVED) { |
| 1079 |
break; |
| 1080 |
} |
| 1081 |
/* Replace data file with cloned file. */ |
| 1082 |
std::string cloned_file(data_file); |
| 1083 |
cloned_file.append(CLONE_INNODB_REPLACED_FILE_EXTN); |
| 1084 |
rename_file(cloned_file, data_file); |
| 1085 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1086 |
<< "Clone File Roll Forward: Rename clone to data file " << data_file |
| 1087 |
<< " state: " << cur_state; |
| 1088 |
} |
| 1089 |
[[fallthrough]]; |
| 1090 |
|
| 1091 |
case FILE_STATE_REPLACED: { |
| 1092 |
if (final_state == FILE_STATE_REPLACED) { |
| 1093 |
break; |
| 1094 |
} |
| 1095 |
/* Remove saved data file */ |
| 1096 |
std::string saved_file(data_file); |
| 1097 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1098 |
remove_file(saved_file); |
| 1099 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1100 |
<< "Clone File Roll Forward: Remove saved data file " << data_file |
| 1101 |
<< " state: " << cur_state; |
| 1102 |
} |
| 1103 |
[[fallthrough]]; |
| 1104 |
|
| 1105 |
case FILE_STATE_NORMAL: |
| 1106 |
/* Nothing to do. */ |
| 1107 |
break; |
| 1108 |
|
| 1109 |
default: |
| 1110 |
ib::fatal(UT_LOCATION_HERE, ER_IB_CLONE_STATUS_FILE) |
| 1111 |
<< "Clone File Roll Forward: Invalid File State: " << cur_state; |
| 1112 |
} |
| 1113 |
return (cur_state); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/** Roll back clone file state to normal state. |
| 1117 |
@param[in] data_file data file name */ |
| 1118 |
static void file_rollback(const std::string &data_file) { |
| 1119 |
auto cur_state = get_file_state(data_file); |
| 1120 |
|
| 1121 |
switch (cur_state) { |
| 1122 |
case FILE_STATE_REPLACED: { |
| 1123 |
/* Replace data file back to cloned file. */ |
| 1124 |
std::string cloned_file(data_file); |
| 1125 |
cloned_file.append(CLONE_INNODB_REPLACED_FILE_EXTN); |
| 1126 |
rename_file(data_file, cloned_file); |
| 1127 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1128 |
<< "Clone File Roll Back: Rename data to cloned file " << data_file |
| 1129 |
<< " state: " << cur_state; |
| 1130 |
} |
| 1131 |
[[fallthrough]]; |
| 1132 |
|
| 1133 |
case FILE_STATE_CLONE_SAVED: { |
| 1134 |
/* Replace data file with saved file. */ |
| 1135 |
std::string saved_file(data_file); |
| 1136 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1137 |
rename_file(saved_file, data_file); |
| 1138 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1139 |
<< "Clone File Roll Back: Rename saved to data file " << data_file |
| 1140 |
<< " state: " << cur_state; |
| 1141 |
} |
| 1142 |
[[fallthrough]]; |
| 1143 |
|
| 1144 |
case FILE_STATE_CLONED: { |
| 1145 |
/* Remove cloned data file. */ |
| 1146 |
std::string cloned_file(data_file); |
| 1147 |
cloned_file.append(CLONE_INNODB_REPLACED_FILE_EXTN); |
| 1148 |
remove_file(cloned_file); |
| 1149 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1150 |
<< "Clone File Roll Back: Remove cloned file " << data_file |
| 1151 |
<< " state: " << cur_state; |
| 1152 |
} |
| 1153 |
[[fallthrough]]; |
| 1154 |
|
| 1155 |
case FILE_STATE_NORMAL: |
| 1156 |
/* Nothing to do. */ |
| 1157 |
break; |
| 1158 |
|
| 1159 |
default: |
| 1160 |
ib::fatal(UT_LOCATION_HERE, ER_IB_CLONE_STATUS_FILE) |
| 1161 |
<< "Clone File Roll Back: Invalid File State: " << cur_state; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* Clone old data File state transfer. These files are present only in |
| 1166 |
recipient and we haven't drop the database objects (table/tablespace) |
| 1167 |
before clone. Currently used for user created undo tablespace. Dropping |
| 1168 |
undo tablespace could be expensive as we need to wait for purge to finish. |
| 1169 |
[FILE_STATE_NORMAL] --> [FILE_STATE_SAVED] |
| 1170 |
Before recovery the old datafile is saved in a file named <file_name>.save. |
| 1171 |
|
| 1172 |
[FILE_STATE_SAVED] --> [FILE_STATE_NONE] |
| 1173 |
After successful recovery the saved data file is removed. |
| 1174 |
|
| 1175 |
These state transitions involve a single file delete or rename and |
| 1176 |
we consider them atomic. In case of a failure the state rolls back. |
| 1177 |
|
| 1178 |
[FILE_STATE_SAVED] --> [FILE_STATE_NORMAL] |
| 1179 |
On failure saved data file is moved back to original data file. |
| 1180 |
*/ |
| 1181 |
|
| 1182 |
/** Roll forward old data file state till final state. |
| 1183 |
@param[in] data_file data file name |
| 1184 |
@param[in] final_state data file state to forward to */ |
| 1185 |
static void old_file_roll_forward(const std::string &data_file, |
| 1186 |
int final_state) { |
| 1187 |
auto cur_state = get_file_state(data_file); |
| 1188 |
|
| 1189 |
switch (cur_state) { |
| 1190 |
case FILE_STATE_CLONED: |
| 1191 |
case FILE_STATE_CLONE_SAVED: |
| 1192 |
case FILE_STATE_REPLACED: |
| 1193 |
/* If the file is also cloned, we can skip here as it would be handled |
| 1194 |
with other cloned files. */ |
| 1195 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1196 |
<< "Clone Old File Roll Forward: Skipped cloned file " << data_file |
| 1197 |
<< " state: " << cur_state; |
| 1198 |
break; |
| 1199 |
case FILE_STATE_NORMAL: { |
| 1200 |
if (final_state == FILE_STATE_NORMAL) { |
| 1201 |
ut_d(ut_error); |
| 1202 |
ut_o(break); |
| 1203 |
} |
| 1204 |
/* Save data file */ |
| 1205 |
std::string saved_file(data_file); |
| 1206 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1207 |
rename_file(data_file, saved_file); |
| 1208 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1209 |
<< "Clone Old File Roll Forward: Saved data file " << data_file |
| 1210 |
<< " state: " << cur_state; |
| 1211 |
} |
| 1212 |
[[fallthrough]]; |
| 1213 |
|
| 1214 |
case FILE_STATE_SAVED: { |
| 1215 |
if (final_state == FILE_STATE_SAVED) { |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
/* Remove saved data file */ |
| 1219 |
std::string saved_file(data_file); |
| 1220 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1221 |
remove_file(saved_file); |
| 1222 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1223 |
<< "Clone Old File Roll Forward: Remove saved file " << data_file |
| 1224 |
<< " state: " << cur_state; |
| 1225 |
} |
| 1226 |
[[fallthrough]]; |
| 1227 |
|
| 1228 |
case FILE_STATE_NONE: |
| 1229 |
/* Nothing to do. */ |
| 1230 |
break; |
| 1231 |
|
| 1232 |
default: |
| 1233 |
ib::fatal(UT_LOCATION_HERE, ER_IB_CLONE_STATUS_FILE) |
| 1234 |
<< "Clone Old File Roll Forward: Invalid File State: " << cur_state; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
/** Roll back old data file state to normal state. |
| 1239 |
@param[in] data_file data file name */ |
| 1240 |
static void old_file_rollback(const std::string &data_file) { |
| 1241 |
auto cur_state = get_file_state(data_file); |
| 1242 |
|
| 1243 |
switch (cur_state) { |
| 1244 |
case FILE_STATE_CLONED: |
| 1245 |
case FILE_STATE_CLONE_SAVED: |
| 1246 |
case FILE_STATE_REPLACED: |
| 1247 |
/* If the file is also cloned, we can skip here as it would be handled |
| 1248 |
with other cloned files. */ |
| 1249 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1250 |
<< "Clone Old File Roll Back: Skip cloned file " << data_file |
| 1251 |
<< " state: " << cur_state; |
| 1252 |
break; |
| 1253 |
|
| 1254 |
case FILE_STATE_SAVED: { |
| 1255 |
/* Replace data file with saved file. */ |
| 1256 |
std::string saved_file(data_file); |
| 1257 |
saved_file.append(CLONE_INNODB_SAVED_FILE_EXTN); |
| 1258 |
rename_file(saved_file, data_file); |
| 1259 |
ib::info(ER_IB_CLONE_STATUS_FILE) |
| 1260 |
<< "Clone Old File Roll Back: Renamed saved data file " << data_file |
| 1261 |
<< " state: " << cur_state; |
| 1262 |
} |
| 1263 |
[[fallthrough]]; |
| 1264 |
|
| 1265 |
case FILE_STATE_NORMAL: |
| 1266 |
case FILE_STATE_NONE: |
| 1267 |
/* Nothing to do. */ |
| 1268 |
break; |
| 1269 |
|
| 1270 |
default: |
| 1271 |
ib::fatal(UT_LOCATION_HERE, ER_IB_CLONE_STATUS_FILE) |
| 1272 |
<< "Clone Old File Roll Back: Invalid File State: " << cur_state; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
/** Fatal error callback function. Don't call other functions from here. Don't |
| 1277 |
use ut_a, ut_ad asserts or ib::fatal to avoid recursive invocation. */ |
| 1278 |
static void clone_files_fatal_error() { |
| 1279 |
/* Safeguard to avoid recursive call. */ |
| 1280 |
static bool started_error_handling = false; |
| 1281 |
if (started_error_handling) { |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
started_error_handling = true; |
| 1285 |
|
| 1286 |
std::ifstream err_file(CLONE_INNODB_ERROR_FILE); |
| 1287 |
if (err_file.is_open()) { |
| 1288 |
err_file.close(); |
| 1289 |
} else { |
| 1290 |
/* Create error file if not there. */ |
| 1291 |
std::ofstream new_file(CLONE_INNODB_ERROR_FILE); |
| 1292 |
/* On creation failure, return and abort. */ |
| 1293 |
if (!new_file.is_open()) { |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
new_file.close(); |
| 1297 |
} |
| 1298 |
/* In case of fatal error, from ib::fatal and ut_a asserts |
| 1299 |
we terminate the process here and send the exit status so that a |
| 1300 |
managed server can be restarted with older data files. */ |
| 1301 |
std::_Exit(MYSQLD_RESTART_EXIT); |
| 1302 |
} |
| 1303 |
|
| 1304 |
/** Update recovery status file at end of clone recovery. |
| 1305 |
@param[in] finished true if finishing clone recovery |
| 1306 |
@param[in] is_error if recovery error |
| 1307 |
@param[in] is_replace true, if replacing current directory */ |
| 1308 |
static void clone_update_recovery_status(bool finished, bool is_error, |
| 1309 |
bool is_replace) { |
| 1310 |
/* true, when we are recovering a cloned database. */ |
| 1311 |
static bool recovery_in_progress = false; |
| 1312 |
/* true, when replacing current data directory. */ |
| 1313 |
static bool recovery_replace = false; |
| 1314 |
|
| 1315 |
std::function<void()> callback_function; |
| 1316 |
|
| 1317 |
/* Mark the beginning of clone recovery. */ |
| 1318 |
if (!finished) { |
| 1319 |
recovery_in_progress = true; |
| 1320 |
if (is_replace) { |
| 1321 |
recovery_replace = true; |
| 1322 |
callback_function = clone_files_fatal_error; |
| 1323 |
ut_set_assert_callback(callback_function); |
| 1324 |
} |
| 1325 |
return; |
| 1326 |
} |
| 1327 |
is_replace = recovery_replace; |
| 1328 |
recovery_replace = false; |
| 1329 |
|
| 1330 |
/* Update status only if clone recovery in progress. */ |
| 1331 |
if (!recovery_in_progress) { |
| 1332 |
return; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/* Mark end of clone recovery process. */ |
| 1336 |
recovery_in_progress = false; |
| 1337 |
ut_set_assert_callback(callback_function); |
| 1338 |
|
| 1339 |
std::string file_name; |
| 1340 |
|
| 1341 |
file_name.assign(CLONE_INNODB_RECOVERY_FILE); |
| 1342 |
if (!file_exists(file_name)) { |
| 1343 |
return; |
| 1344 |
} |
| 1345 |
|
| 1346 |
std::ofstream status_file; |
| 1347 |
status_file.open(file_name, std::ofstream::app); |
| 1348 |
if (!status_file.is_open()) { |
| 1349 |
return; |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* Write zero for unsuccessful recovery. */ |
| 1353 |
uint64_t end_time = 0; |
| 1354 |
if (is_error) { |
| 1355 |
status_file << end_time << std::endl; |
| 1356 |
status_file.close(); |
| 1357 |
/* Set recovery error so that server can restart only for replace. */ |
| 1358 |
clone_recovery_error = is_replace; |
| 1359 |
return; |
| 1360 |
} |
| 1361 |
|
| 1362 |
/* Write recovery end time */ |
| 1363 |
end_time = my_micro_time(); |
| 1364 |
status_file << end_time << std::endl; |
| 1365 |
if (!status_file.good()) { |
| 1366 |
status_file.close(); |
| 1367 |
return; |
| 1368 |
} |
| 1369 |
|
| 1370 |
mtr_t mtr; |
| 1371 |
mtr.start(); |
| 1372 |
byte *binlog_pos = trx_sysf_get(&mtr) + TRX_SYS_MYSQL_LOG_INFO; |
| 1373 |
|
| 1374 |
/* Check logfile magic number. */ |
| 1375 |
if (mach_read_from_4(binlog_pos + TRX_SYS_MYSQL_LOG_MAGIC_N_FLD) != |
| 1376 |
TRX_SYS_MYSQL_LOG_MAGIC_N) { |
| 1377 |
mtr.commit(); |
| 1378 |
status_file.close(); |
| 1379 |
return; |
| 1380 |
} |
| 1381 |
/* Write binary log file name. */ |
| 1382 |
status_file << binlog_pos + TRX_SYS_MYSQL_LOG_NAME << std::endl; |
| 1383 |
if (!status_file.good()) { |
| 1384 |
mtr.commit(); |
| 1385 |
status_file.close(); |
| 1386 |
return; |
| 1387 |
} |
| 1388 |
|
| 1389 |
auto high = mach_read_from_4(binlog_pos + TRX_SYS_MYSQL_LOG_OFFSET_HIGH); |
| 1390 |
auto low = mach_read_from_4(binlog_pos + TRX_SYS_MYSQL_LOG_OFFSET_LOW); |
| 1391 |
|
| 1392 |
auto log_offset = static_cast<uint64_t>(high); |
| 1393 |
log_offset = (log_offset << 32); |
| 1394 |
log_offset |= static_cast<uint64_t>(low); |
| 1395 |
|
| 1396 |
/* Write log file offset. */ |
| 1397 |
status_file << log_offset << std::endl; |
| 1398 |
|
| 1399 |
mtr.commit(); |
| 1400 |
status_file.close(); |
| 1401 |
/* Set clone startup for GR, only during replace. */ |
| 1402 |
clone_startup = is_replace; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/** Initialize recovery status for cloned recovery. |
| 1406 |
@param[in] replace we are replacing current directory. */ |
| 1407 |
static void clone_init_recovery_status(bool replace) { |
| 1408 |
std::string file_name; |
| 1409 |
file_name.assign(CLONE_INNODB_RECOVERY_FILE); |
| 1410 |
|
| 1411 |
std::ofstream status_file; |
| 1412 |
status_file.open(file_name, std::ofstream::out | std::ofstream::trunc); |
| 1413 |
if (!status_file.is_open()) { |
| 1414 |
return; |
| 1415 |
} |
| 1416 |
/* Write recovery begin time */ |
| 1417 |
uint64_t begin_time = my_micro_time(); |
| 1418 |
status_file << begin_time << std::endl; |
| 1419 |
status_file.close(); |
| 1420 |
clone_update_recovery_status(false, false, replace); |
| 1421 |
} |
| 1422 |
|
| 1423 |
void clone_update_gtid_status(std::string >ids) { |
| 1424 |
/* Return if not clone database recovery. */ |
| 1425 |
std::string replace_files(CLONE_INNODB_REPLACED_FILES); |
| 1426 |
if (!file_exists(replace_files)) { |
| 1427 |
return; |
| 1428 |
} |
| 1429 |
/* Return if status file is not created. */ |
| 1430 |
std::string recovery_file(CLONE_INNODB_RECOVERY_FILE); |
| 1431 |
if (!file_exists(recovery_file)) { |
| 1432 |
ut_d(ut_error); |
| 1433 |
ut_o(return); |
| 1434 |
} |
| 1435 |
/* Open status file to append GTID. */ |
| 1436 |
std::ofstream status_file; |
| 1437 |
status_file.open(recovery_file, std::ofstream::app); |
| 1438 |
if (!status_file.is_open()) { |
| 1439 |
return; |
| 1440 |
} |
| 1441 |
status_file << gtids << std::endl; |
| 1442 |
status_file.close(); |
| 1443 |
|
| 1444 |
/* Remove replace file after successful recovery and status update. */ |
| 1445 |
std::ifstream files; |
| 1446 |
files.open(replace_files); |
| 1447 |
|
| 1448 |
if (files.is_open()) { |
| 1449 |
/* If file is not empty, we are replacing data directory. */ |
| 1450 |
std::string file_name; |
| 1451 |
if (std::getline(files, file_name)) { |
| 1452 |
clone_startup = true; |
| 1453 |
} |
| 1454 |
files.close(); |
| 1455 |
} |
| 1456 |
remove_file(replace_files); |
| 1457 |
} |
| 1458 |
|
| 1459 |
/** Type of function which is supposed to handle a single file during |
| 1460 |
Clone operations, accepting the file's name (string). |
| 1461 |
@see clone_files_for_each_file */ |
| 1462 |
typedef std::function<void(const std::string &)> Clone_file_handler; |
| 1463 |
|
| 1464 |
/** Processes each file name listed in the given status file, executing a given |
| 1465 |
function for each of them. |
| 1466 |
@param[in] status_file_name status file name |
| 1467 |
@param[in] process the given function, accepting file name string |
| 1468 |
@return true iff status file was successfully opened */ |
| 1469 |
static bool clone_files_for_each_file(const char *status_file_name, |
| 1470 |
const Clone_file_handler &process) { |
| 1471 |
std::ifstream files; |
| 1472 |
files.open(status_file_name); |
| 1473 |
if (!files.is_open()) { |
| 1474 |
return false; |
| 1475 |
} |
| 1476 |
std::string data_file; |
| 1477 |
/* Extract and process all files listed in file with name=status_file_name */ |
| 1478 |
while (std::getline(files, data_file)) { |
| 1479 |
process(data_file); |
| 1480 |
} |
| 1481 |
files.close(); |
| 1482 |
return true; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/** Process all entries and remove status file. |
| 1486 |
@param[in] file_name status file name |
| 1487 |
@param[in] process callback to process entries */ |
| 1488 |
static void process_remove_file(const char *file_name, |
| 1489 |
const Clone_file_handler &process) { |
| 1490 |
if (clone_files_for_each_file(file_name, process)) { |
| 1491 |
std::string file_str(file_name); |
| 1492 |
remove_file(file_str); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
|
| 1496 |
void clone_files_error() { |
| 1497 |
/* Check if clone file directory exists. */ |
| 1498 |
if (!os_file_exists(CLONE_FILES_DIR)) { |
| 1499 |
return; |
| 1500 |
} |
| 1501 |
|
| 1502 |
std::string err_file(CLONE_INNODB_ERROR_FILE); |
| 1503 |
|
| 1504 |
/* Create error status file if not there. */ |
| 1505 |
if (!file_exists(err_file)) { |
| 1506 |
create_file(err_file); |
| 1507 |
} |
| 1508 |
|
| 1509 |
/* Process all old files to be moved. */ |
| 1510 |
Clone_file_handler cbk = old_file_rollback; |
| 1511 |
process_remove_file(CLONE_INNODB_OLD_FILES, cbk); |
| 1512 |
|
| 1513 |
/* Process all files to be replaced. */ |
| 1514 |
cbk = file_rollback; |
| 1515 |
process_remove_file(CLONE_INNODB_REPLACED_FILES, cbk); |
| 1516 |
|
| 1517 |
/* Process all new files to be deleted. */ |
| 1518 |
cbk = remove_file; |
| 1519 |
process_remove_file(CLONE_INNODB_NEW_FILES, cbk); |
| 1520 |
|
| 1521 |
/* Process all temp ddl files to be deleted. */ |
| 1522 |
process_remove_file(CLONE_INNODB_DDL_FILES, cbk); |
| 1523 |
|
| 1524 |
/* Remove error status file. */ |
| 1525 |
remove_file(err_file); |
| 1526 |
|
| 1527 |
/* Update recovery status file for recovery error. */ |
| 1528 |
clone_update_recovery_status(true, true, true); |
| 1529 |
} |
| 1530 |
|
| 1531 |
#ifdef UNIV_DEBUG |
| 1532 |
bool clone_check_recovery_crashpoint(bool is_cloned_db) { |
| 1533 |
if (!is_cloned_db) { |
| 1534 |
return (true); |
| 1535 |
} |
| 1536 |
std::string crash_file(CLONE_INNODB_RECOVERY_CRASH_POINT); |
| 1537 |
|
| 1538 |
if (file_exists(crash_file)) { |
| 1539 |
remove_file(crash_file); |
| 1540 |
return (false); |
| 1541 |
} |
| 1542 |
return (true); |
| 1543 |
} |
| 1544 |
#endif |
| 1545 |
|
| 1546 |
void clone_files_recovery(bool finished) { |
| 1547 |
/* Clone error file is present in case of error. */ |
| 1548 |
std::string file_name; |
| 1549 |
file_name.assign(CLONE_INNODB_ERROR_FILE); |
| 1550 |
|
| 1551 |
if (file_exists(file_name)) { |
| 1552 |
ut_ad(!finished); |
| 1553 |
clone_files_error(); |
| 1554 |
return; |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* if replace file is not present, remove old file. */ |
| 1558 |
if (!finished) { |
| 1559 |
std::string replace_files(CLONE_INNODB_REPLACED_FILES); |
| 1560 |
std::string old_files(CLONE_INNODB_OLD_FILES); |
| 1561 |
if (!file_exists(replace_files) && file_exists(old_files)) { |
| 1562 |
remove_file(old_files); |
| 1563 |
ut_d(ut_error); |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
/* Open files to get all old files to be saved or removed. Must handle |
| 1568 |
the old files before cloned files. This is because during old file |
| 1569 |
processing we need to skip the common files based on cloned state. If |
| 1570 |
the cloned state is reset then these files would be considered as old |
| 1571 |
files and removed. */ |
| 1572 |
int end_state = finished ? FILE_STATE_NONE : FILE_STATE_SAVED; |
| 1573 |
|
| 1574 |
auto old_file_handler = [end_state](const std::string &fname) { |
| 1575 |
old_file_roll_forward(fname, end_state); |
| 1576 |
}; |
| 1577 |
|
| 1578 |
if (clone_files_for_each_file(CLONE_INNODB_OLD_FILES, old_file_handler)) { |
| 1579 |
/* Remove clone file after successful recovery. */ |
| 1580 |
if (finished) { |
| 1581 |
std::string old_files(CLONE_INNODB_OLD_FILES); |
| 1582 |
remove_file(old_files); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
/* Open file to get all files to be replaced. */ |
| 1587 |
end_state = finished ? FILE_STATE_NORMAL : FILE_STATE_REPLACED; |
| 1588 |
|
| 1589 |
std::ifstream files; |
| 1590 |
|
| 1591 |
files.open(CLONE_INNODB_REPLACED_FILES); |
| 1592 |
|
| 1593 |
if (files.is_open()) { |
| 1594 |
int prev_state = FILE_STATE_NORMAL; |
| 1595 |
/* If file is empty, it is not replace. */ |
| 1596 |
bool replace = false; |
| 1597 |
|
| 1598 |
/* Extract and process all files to be replaced */ |
| 1599 |
while (std::getline(files, file_name)) { |
| 1600 |
replace = true; |
| 1601 |
prev_state = file_roll_forward(file_name, end_state); |
| 1602 |
} |
| 1603 |
|
| 1604 |
files.close(); |
| 1605 |
|
| 1606 |
if (finished) { |
| 1607 |
/* Update recovery status file at the end of clone recovery. We don't |
| 1608 |
remove the replace file here. It would be removed only after updating |
| 1609 |
GTID state. */ |
| 1610 |
clone_update_recovery_status(true, false, replace); |
| 1611 |
} else { |
| 1612 |
/* If previous state was normal, clone recovery is already done. */ |
| 1613 |
if (!replace || prev_state != FILE_STATE_NORMAL) { |
| 1614 |
/* Clone database recovery is started. */ |
| 1615 |
clone_init_recovery_status(replace); |
| 1616 |
} |
| 1617 |
} |
| 1618 |
} |
| 1619 |
|
| 1620 |
file_name.assign(CLONE_INNODB_NEW_FILES); |
| 1621 |
auto exists = file_exists(file_name); |
| 1622 |
|
| 1623 |
if (exists && finished) { |
| 1624 |
/* Remove clone file after successful recovery. */ |
| 1625 |
std::string new_files(CLONE_INNODB_NEW_FILES); |
| 1626 |
remove_file(new_files); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
dberr_t clone_init() { |
| 1631 |
/* Check if incomplete cloned data directory */ |
| 1632 |
if (os_file_exists(CLONE_INNODB_IN_PROGRESS_FILE)) { |
| 1633 |
return (DB_ABORT_INCOMPLETE_CLONE); |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* Initialize clone files before starting recovery. */ |
| 1637 |
clone_files_recovery(false); |
| 1638 |
|
| 1639 |
if (clone_sys == nullptr) { |
| 1640 |
ut_ad(Clone_Sys::s_clone_sys_state == CLONE_SYS_INACTIVE); |
| 1641 |
clone_sys = |
| 1642 |
ut::new_withkey<Clone_Sys>(ut::make_psi_memory_key(mem_key_clone)); |
| 1643 |
} |
| 1644 |
Clone_Sys::s_clone_sys_state = CLONE_SYS_ACTIVE; |
| 1645 |
Clone_handler::init_xa(); |
| 1646 |
|
| 1647 |
return (DB_SUCCESS); |
| 1648 |
} |
| 1649 |
|
| 1650 |
void clone_free() { |
| 1651 |
Clone_handler::uninit_xa(); |
| 1652 |
if (clone_sys != nullptr) { |
| 1653 |
ut_ad(Clone_Sys::s_clone_sys_state == CLONE_SYS_ACTIVE); |
| 1654 |
|
| 1655 |
ut::delete_(clone_sys); |
| 1656 |
clone_sys = nullptr; |
| 1657 |
} |
| 1658 |
|
| 1659 |
Clone_Sys::s_clone_sys_state = CLONE_SYS_INACTIVE; |
| 1660 |
} |
| 1661 |
|
| 1662 |
bool clone_check_provisioning() { return Clone_handler::is_provisioning(); } |
| 1663 |
|
| 1664 |
bool clone_check_active() { |
| 1665 |
mutex_enter(clone_sys->get_mutex()); |
| 1666 |
auto is_active = clone_sys->check_active_clone(false); |
| 1667 |
mutex_exit(clone_sys->get_mutex()); |
| 1668 |
|
| 1669 |
return (is_active || Clone_handler::is_provisioning()); |
| 1670 |
} |
| 1671 |
|
| 1672 |
template <typename T> |
| 1673 |
using DD_Objs = std::vector<const T *>; |
| 1674 |
|
| 1675 |
template <typename T> |
| 1676 |
using DD_Objs_Iter = typename DD_Objs<T>::const_iterator; |
| 1677 |
|
| 1678 |
using Releaser = dd::cache::Dictionary_client::Auto_releaser; |
| 1679 |
|
| 1680 |
namespace { |
| 1681 |
|
| 1682 |
/** Fix schema, table and tablespace. Used for two different purposes. |
| 1683 |
1. After recovery from cloned database: |
| 1684 |
A. Create empty data file for non-Innodb tables that are not cloned. |
| 1685 |
B. Create any schema directory that is not present. |
| 1686 |
|
| 1687 |
2. Before cloning into current data directory: |
| 1688 |
A. Drop all user tables. |
| 1689 |
B. Drop all user schema |
| 1690 |
C. Drop all user tablespaces. */ |
| 1691 |
|
| 1692 |
class Fixup_data { |
| 1693 |
public: |
| 1694 |
/** Constructor. |
| 1695 |
@param[in] concurrent spawn multiple threads |
| 1696 |
@param[in] is_drop the operation is drop */ |
| 1697 |
Fixup_data(bool concurrent, bool is_drop) |
| 1698 |
: m_num_tasks(), m_concurrent(concurrent), m_drop(is_drop) { |
| 1699 |
m_num_errors.store(0); |
| 1700 |
} |
| 1701 |
|
| 1702 |
/** Fix tables for which data is not cloned. |
| 1703 |
@param[in,out] thd current THD |
| 1704 |
@param[in] dd_objects table/schema/tablespace from DD |
| 1705 |
@return true if error */ |
| 1706 |
template <typename T> |
| 1707 |
bool fix(THD *thd, const DD_Objs<T> &dd_objects) { |
| 1708 |
set_num_tasks(dd_objects.size()); |
| 1709 |
|
| 1710 |
using namespace std::placeholders; |
| 1711 |
auto fixup_function = |
| 1712 |
std::bind(&Fixup_data::fix_objects<T>, this, thd, _1, _2, _3); |
| 1713 |
|
| 1714 |
par_for(PFS_NOT_INSTRUMENTED, dd_objects, get_num_tasks(), fixup_function); |
| 1715 |
|
| 1716 |
return (failed()); |
| 1717 |
} |
| 1718 |
|
| 1719 |
/** Remove data cloned from configuration tables which are not relevant |
| 1720 |
in recipient. |
| 1721 |
@param[in,out] thd current THD |
| 1722 |
@return true if error */ |
| 1723 |
bool fix_config_tables(THD *thd); |
| 1724 |
|
| 1725 |
/** Number of system configuration tables. */ |
| 1726 |
static const size_t S_NUM_CONFIG_TABLES = 0; |
| 1727 |
|
| 1728 |
/** Array of configuration tables. */ |
| 1729 |
static const std::array<const char *, S_NUM_CONFIG_TABLES> s_config_tables; |
| 1730 |
|
| 1731 |
private: |
| 1732 |
/** Check and fix specific DD object. |
| 1733 |
@param[in,out] thd current THD |
| 1734 |
@param[in] object DD object |
| 1735 |
@param[in] thread_number current thread number. */ |
| 1736 |
template <typename T> |
| 1737 |
bool fix_one_object(THD *thd, const T *object, size_t thread_number); |
| 1738 |
|
| 1739 |
/** Check and fix a rangle of DD objects |
| 1740 |
@param[in,out] thd current THD |
| 1741 |
@param[in] begin first element in current slice |
| 1742 |
@param[in] end last element in current slice |
| 1743 |
@param[in] thread_number current thread number. */ |
| 1744 |
template <typename T> |
| 1745 |
void fix_objects(THD *thd, const DD_Objs_Iter<T> &begin, |
| 1746 |
const DD_Objs_Iter<T> &end, size_t thread_number); |
| 1747 |
|
| 1748 |
/** @return number of tasks. */ |
| 1749 |
size_t get_num_tasks() const { return (m_num_tasks); } |
| 1750 |
|
| 1751 |
/** Calculate and set number of new tasks to spawn. |
| 1752 |
@param[in] num_entries number of entries to handle */ |
| 1753 |
void set_num_tasks(size_t num_entries) { |
| 1754 |
/* Check if we are allowed to spawn multiple threads. Disable |
| 1755 |
multithreading while dropping objects for now. We need more |
| 1756 |
work to handle and pass interrupt signal to workers. */ |
| 1757 |
if (is_drop() || !allow_concurrent()) { |
| 1758 |
m_num_tasks = 0; |
| 1759 |
return; |
| 1760 |
} |
| 1761 |
/* Have one task for every 100 entries. */ |
| 1762 |
m_num_tasks = num_entries / 100; |
| 1763 |
|
| 1764 |
#ifdef UNIV_DEBUG |
| 1765 |
/* Test operation in newly spawned thread. */ |
| 1766 |
if (m_num_tasks == 0) { |
| 1767 |
++m_num_tasks; |
| 1768 |
} |
| 1769 |
#endif /* UNIV_DEBUG */ |
| 1770 |
|
| 1771 |
/* Don't go beyond 8 threads for now. */ |
| 1772 |
if (m_num_tasks > 8) { |
| 1773 |
m_num_tasks = 8; |
| 1774 |
} |
| 1775 |
m_num_errors.store(0); |
| 1776 |
} |
| 1777 |
|
| 1778 |
/** @return true, if current operation is drop. */ |
| 1779 |
bool is_drop() const { return (m_drop); } |
| 1780 |
|
| 1781 |
/** @return true, if concurrency is allowed. */ |
| 1782 |
bool allow_concurrent() const { return (m_concurrent); } |
| 1783 |
|
| 1784 |
/** Get the table operation string. |
| 1785 |
@return sql key word for the operation. */ |
| 1786 |
const char *sql_operation() { |
| 1787 |
if (is_drop()) { |
| 1788 |
return ("DROP"); |
| 1789 |
} |
| 1790 |
/* Alternative action is truncate. */ |
| 1791 |
return ("TRUNCATE"); |
| 1792 |
} |
| 1793 |
|
| 1794 |
/** Check if the current SE type should be skipped. |
| 1795 |
@param[in] type SE type |
| 1796 |
@return true iff the SE needs to be skipped. */ |
| 1797 |
bool skip_se_tables(enum legacy_db_type type) { |
| 1798 |
/* Don't skip any specific DB during drop operation. All existing |
| 1799 |
user tables are dropped before cloning a remote database. */ |
| 1800 |
if (is_drop()) { |
| 1801 |
return (false); |
| 1802 |
} |
| 1803 |
/* Truncate only MyISAM and CSV tables. After clone we need to create |
| 1804 |
empty tables for engines that are not cloned. */ |
| 1805 |
if (type == DB_TYPE_MYISAM || type == DB_TYPE_CSV_DB) { |
| 1806 |
return (false); |
| 1807 |
} |
| 1808 |
return (true); |
| 1809 |
} |
| 1810 |
|
| 1811 |
/** Check if the schema is performance schema. |
| 1812 |
@param[in] schema_name schema name |
| 1813 |
@return true iff performance schema. */ |
| 1814 |
bool is_performance_schema(const char *schema_name) const { |
| 1815 |
return (0 == strcmp(schema_name, PERFORMANCE_SCHEMA_DB_NAME.str)); |
| 1816 |
} |
| 1817 |
|
| 1818 |
/** Check if the current schema is system schema |
| 1819 |
@param[in] schema_name schema name |
| 1820 |
@return true iff system schema. */ |
| 1821 |
bool is_system_schema(const char *schema_name) const { |
| 1822 |
if (0 == strcmp(schema_name, MYSQL_SCHEMA_NAME.str) || |
| 1823 |
0 == strcmp(schema_name, "sys") || |
| 1824 |
0 == strcmp(schema_name, PERFORMANCE_SCHEMA_DB_NAME.str) || |
| 1825 |
0 == strcmp(schema_name, INFORMATION_SCHEMA_NAME.str)) { |
| 1826 |
return (true); |
| 1827 |
} |
| 1828 |
return (false); |
| 1829 |
} |
| 1830 |
|
| 1831 |
/** Check if the current schema tables needs to be skipped. |
| 1832 |
@param[in] table DD table |
| 1833 |
@param[in] table_name table name |
| 1834 |
@param[in] schema_name schema name |
| 1835 |
@return true iff table needs to be skipped. */ |
| 1836 |
bool skip_schema_tables(const dd::Table *table, const char *table_name, |
| 1837 |
const char *schema_name) { |
| 1838 |
/* Skip specific tables only during drop. */ |
| 1839 |
if (!is_drop()) { |
| 1840 |
return (false); |
| 1841 |
} |
| 1842 |
|
| 1843 |
/* Handle only visible base tables. */ |
| 1844 |
if (table->type() != dd::enum_table_type::BASE_TABLE || |
| 1845 |
table->hidden() != dd::Abstract_table::HT_VISIBLE) { |
| 1846 |
return (true); |
| 1847 |
} |
| 1848 |
|
| 1849 |
/* Don't Skip tables in non-system schemas. */ |
| 1850 |
if (!is_system_schema(schema_name)) { |
| 1851 |
return (false); |
| 1852 |
} |
| 1853 |
|
| 1854 |
/* Skip DD system tables. */ |
| 1855 |
if (table->is_explicit_tablespace() && |
| 1856 |
table->tablespace_id() == dd::Dictionary_impl::dd_tablespace_id()) { |
| 1857 |
return (true); |
| 1858 |
} |
| 1859 |
|
| 1860 |
/* Skip all in information_schema and performance_schema tables. */ |
| 1861 |
if (0 == strcmp(schema_name, PERFORMANCE_SCHEMA_DB_NAME.str) || |
| 1862 |
0 == strcmp(schema_name, INFORMATION_SCHEMA_NAME.str)) { |
| 1863 |
return (true); |
| 1864 |
} |
| 1865 |
|
| 1866 |
/* Skip specific tables in mysql schema. */ |
| 1867 |
if (0 == strcmp(schema_name, MYSQL_SCHEMA_NAME.str) && |
| 1868 |
(0 == strcmp(table_name, GENERAL_LOG_NAME.str) || |
| 1869 |
0 == strcmp(table_name, SLOW_LOG_NAME.str))) { |
| 1870 |
return (true); |
| 1871 |
} |
| 1872 |
|
| 1873 |
/* Skip specific tables in sys schema. */ |
| 1874 |
if (0 == strcmp(schema_name, "sys") && |
| 1875 |
0 == strcmp(table_name, "sys_config")) { |
| 1876 |
return (true); |
| 1877 |
} |
| 1878 |
|
| 1879 |
return (false); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/** Check if the current schema needs to be skipped. |
| 1883 |
@param[in] schema_name schema name |
| 1884 |
@return true iff schema needs to be skipped. */ |
| 1885 |
bool skip_schema(const char *schema_name) { |
| 1886 |
/* Don't drop system schema. */ |
| 1887 |
if (is_drop()) { |
| 1888 |
return (is_system_schema(schema_name)); |
| 1889 |
} |
| 1890 |
/* Information schema has no directory */ |
| 1891 |
if (0 == strcmp(schema_name, INFORMATION_SCHEMA_NAME.str)) { |
| 1892 |
return (true); |
| 1893 |
} |
| 1894 |
return (false); |
| 1895 |
} |
| 1896 |
|
| 1897 |
/** Check if the current tablespace needs to be skipped. |
| 1898 |
@param[in,out] thd current THD |
| 1899 |
@param[in] dd_space dd tablespace |
| 1900 |
@return true iff tablespace needs to be skipped. */ |
| 1901 |
bool skip_tablespace(THD *thd, const dd::Tablespace *dd_space) { |
| 1902 |
/* System tablespaces are in Innodb. Skip other engines. */ |
| 1903 |
auto se = |
| 1904 |
ha_resolve_by_name_raw(thd, lex_cstring_handle(dd_space->engine())); |
| 1905 |
auto se_type = ha_legacy_type(se ? plugin_data<handlerton *>(se) : nullptr); |
| 1906 |
plugin_unlock(thd, se); |
| 1907 |
if (se_type != DB_TYPE_INNODB) { |
| 1908 |
return (false); |
| 1909 |
} |
| 1910 |
|
| 1911 |
/* Skip system tablespace by name. */ |
| 1912 |
const auto space_name = dd_space->name().c_str(); |
| 1913 |
const char *innodb_prefix = "innodb_"; |
| 1914 |
const char *sys_prefix = "sys/"; |
| 1915 |
if (0 == strcmp(space_name, "mysql") || |
| 1916 |
0 == strncmp(space_name, sys_prefix, strlen(sys_prefix)) || |
| 1917 |
0 == strncmp(space_name, innodb_prefix, strlen(innodb_prefix))) { |
| 1918 |
return (true); |
| 1919 |
} |
| 1920 |
|
| 1921 |
/* Skip undo tablespaces. */ |
| 1922 |
auto &se_data = dd_space->se_private_data(); |
| 1923 |
space_id_t space_id = SPACE_UNKNOWN; |
| 1924 |
|
| 1925 |
if (se_data.get(dd_space_key_strings[DD_SPACE_ID], &space_id) || |
| 1926 |
space_id == SPACE_UNKNOWN) { |
| 1927 |
ut_d(ut_error); |
| 1928 |
ut_o(return (false)); |
| 1929 |
} |
| 1930 |
bool is_undo = fsp_is_undo_tablespace(space_id); |
| 1931 |
|
| 1932 |
/* Add skipped undo tablespace files to list of old files to remove. */ |
| 1933 |
if (is_undo && !allow_concurrent()) { |
| 1934 |
auto dd_file = *(dd_space->files().begin()); |
| 1935 |
clone_add_to_list_file(CLONE_INNODB_OLD_FILES, |
| 1936 |
dd_file->filename().c_str()); |
| 1937 |
/* In rare case, the undo might be kept halfway truncated due to some |
| 1938 |
error during truncate. Check and add truncate log file as old file if |
| 1939 |
present. */ |
| 1940 |
undo::Tablespace undo_space(space_id); |
| 1941 |
const char *log_file_name = undo_space.log_file_name(); |
| 1942 |
|
| 1943 |
if (os_file_exists(log_file_name)) { |
| 1944 |
clone_add_to_list_file(CLONE_INNODB_OLD_FILES, log_file_name); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
/* Skip all undo tablespaces. */ |
| 1949 |
if (is_undo) { |
| 1950 |
return (true); |
| 1951 |
} |
| 1952 |
|
| 1953 |
/* Check and skip file per table tablespace. */ |
| 1954 |
uint32_t flags = 0; |
| 1955 |
if (se_data.get(dd_space_key_strings[DD_SPACE_FLAGS], &flags)) { |
| 1956 |
ut_d(ut_error); |
| 1957 |
ut_o(return (false)); |
| 1958 |
} |
| 1959 |
|
| 1960 |
if (fsp_is_file_per_table(space_id, flags)) { |
| 1961 |
return (true); |
| 1962 |
} |
| 1963 |
return (false); |
| 1964 |
} |
| 1965 |
|
| 1966 |
/** Form and execute sql command. |
| 1967 |
@param[in,out] thd current THD |
| 1968 |
@param[in] schema_name schema name |
| 1969 |
@param[in] table_name table name |
| 1970 |
@param[in] tablespace_name tablespace name |
| 1971 |
@param[in] thread_number current thread number. */ |
| 1972 |
bool execute_sql(THD *thd, const char *schema_name, const char *table_name, |
| 1973 |
const char *tablespace_name, size_t thread_number); |
| 1974 |
|
| 1975 |
/** @return true, if any thread has failed. */ |
| 1976 |
bool failed() const { return (m_num_errors.load() != 0); } |
| 1977 |
|
| 1978 |
private: |
| 1979 |
/** Number of tasks failed. */ |
| 1980 |
std::atomic_size_t m_num_errors; |
| 1981 |
|
| 1982 |
/** Number of tasks. */ |
| 1983 |
size_t m_num_tasks; |
| 1984 |
|
| 1985 |
/** Allow concurrent threads. */ |
| 1986 |
bool m_concurrent; |
| 1987 |
|
| 1988 |
/** If the objects need to be dropped. */ |
| 1989 |
bool m_drop; |
| 1990 |
}; |
| 1991 |
|
| 1992 |
/* All configuration tables for which data should not be cloned. From |
| 1993 |
replication configurations only clone slave_master_info table needed by GR. */ |
| 1994 |
const std::array<const char *, Fixup_data::S_NUM_CONFIG_TABLES> |
| 1995 |
Fixup_data::s_config_tables = {}; |
| 1996 |
|
| 1997 |
bool Fixup_data::fix_config_tables(THD *thd) { |
| 1998 |
/* No privilege check needed for individual tables. */ |
| 1999 |
auto saved_sctx = thd->security_context(); |
| 2000 |
Security_context sctx(*saved_sctx); |
| 2001 |
skip_grants(thd, sctx); |
| 2002 |
thd->set_security_context(&sctx); |
| 2003 |
|
| 2004 |
/* Disable binary logging. */ |
| 2005 |
char sql_stmt[FN_LEN + FN_LEN + 64]; |
| 2006 |
snprintf(sql_stmt, sizeof(sql_stmt), "SET SQL_LOG_BIN = OFF"); |
| 2007 |
static_cast<void>(clone_execute_query(thd, &sql_stmt[0], 1, false)); |
| 2008 |
|
| 2009 |
/* Loop through all objects and fix. */ |
| 2010 |
bool ret = false; |
| 2011 |
for (auto table : s_config_tables) { |
| 2012 |
ret = execute_sql(thd, "mysql", table, nullptr, 1); |
| 2013 |
if (ret) break; |
| 2014 |
} |
| 2015 |
/* Set back old security context. */ |
| 2016 |
thd->set_security_context(saved_sctx); |
| 2017 |
return (ret); |
| 2018 |
} |
| 2019 |
|
| 2020 |
template <typename T> |
| 2021 |
void Fixup_data::fix_objects(THD *thd, const DD_Objs_Iter<T> &begin, |
| 2022 |
const DD_Objs_Iter<T> &end, size_t thread_number) { |
| 2023 |
ib::info(ER_IB_CLONE_SQL) << "Clone: Fix Object count: " << (end - begin) |
| 2024 |
<< " task: " << thread_number; |
| 2025 |
|
| 2026 |
bool thread_created = false; |
| 2027 |
|
| 2028 |
/* For newly spawned threads, create server THD */ |
| 2029 |
if (thread_number != get_num_tasks()) { |
| 2030 |
thd = create_internal_thd(); |
| 2031 |
thread_created = true; |
| 2032 |
} |
| 2033 |
|
| 2034 |
/* Save system thread type to be safe. */ |
| 2035 |
auto saved_thd_system = thd->system_thread; |
| 2036 |
|
| 2037 |
/* No privilege check needed for individual tables. */ |
| 2038 |
auto saved_sctx = thd->security_context(); |
| 2039 |
Security_context sctx(*saved_sctx); |
| 2040 |
skip_grants(thd, sctx); |
| 2041 |
thd->set_security_context(&sctx); |
| 2042 |
|
| 2043 |
char sql_stmt[FN_LEN + FN_LEN + 64]; |
| 2044 |
|
| 2045 |
/* Disable binary logging. */ |
| 2046 |
snprintf(sql_stmt, sizeof(sql_stmt), "SET SQL_LOG_BIN = OFF"); |
| 2047 |
if (clone_execute_query(thd, &sql_stmt[0], thread_number, false)) { |
| 2048 |
++m_num_errors; |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* Disable foreign key check. */ |
| 2052 |
snprintf(sql_stmt, sizeof(sql_stmt), "SET FOREIGN_KEY_CHECKS=0"); |
| 2053 |
if (clone_execute_query(thd, &sql_stmt[0], thread_number, false)) { |
| 2054 |
++m_num_errors; |
| 2055 |
} |
| 2056 |
|
| 2057 |
if (thread_created) { |
| 2058 |
/* For concurrent worker threads set timeout for MDL lock. */ |
| 2059 |
snprintf(sql_stmt, sizeof(sql_stmt), "SET LOCAL LOCK_WAIT_TIMEOUT=1"); |
| 2060 |
if (clone_execute_query(thd, &sql_stmt[0], thread_number, false)) { |
| 2061 |
++m_num_errors; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* Loop through all objects and fix. */ |
| 2066 |
for (auto it = begin; it != end && m_num_errors == 0; ++it) { |
| 2067 |
if (fix_one_object<T>(thd, *it, thread_number)) { |
| 2068 |
break; |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* Set back old security context. */ |
| 2073 |
thd->set_security_context(saved_sctx); |
| 2074 |
thd->system_thread = saved_thd_system; |
| 2075 |
|
| 2076 |
/* Destroy thread if newly spawned task */ |
| 2077 |
if (thread_created) { |
| 2078 |
destroy_internal_thd(thd); |
| 2079 |
} |
| 2080 |
} |
| 2081 |
|
| 2082 |
template <> |
| 2083 |
bool Fixup_data::fix_one_object(THD *thd, const dd::Table *table, |
| 2084 |
size_t thread_number) { |
| 2085 |
auto se = ha_resolve_by_name_raw(thd, lex_cstring_handle(table->engine())); |
| 2086 |
auto se_type = ha_legacy_type(se ? plugin_data<handlerton *>(se) : nullptr); |
| 2087 |
|
| 2088 |
plugin_unlock(thd, se); |
| 2089 |
|
| 2090 |
if (skip_se_tables(se_type)) { |
| 2091 |
return (false); |
| 2092 |
} |
| 2093 |
|
| 2094 |
auto dc = dd::get_dd_client(thd); |
| 2095 |
Releaser releaser(dc); |
| 2096 |
|
| 2097 |
const dd::Schema *table_schema = nullptr; |
| 2098 |
|
| 2099 |
auto saved_thread_type = thd->system_thread; |
| 2100 |
thd->system_thread = SYSTEM_THREAD_DD_INITIALIZE; |
| 2101 |
|
| 2102 |
if (dc->acquire(table->schema_id(), &table_schema)) { |
| 2103 |
++m_num_errors; |
| 2104 |
thd->system_thread = saved_thread_type; |
| 2105 |
return (true); |
| 2106 |
} |
| 2107 |
|
| 2108 |
const auto schema_name = table_schema->name().c_str(); |
| 2109 |
const auto table_name = table->name().c_str(); |
| 2110 |
|
| 2111 |
/* For performance schema drop the SDI table. */ |
| 2112 |
if (is_drop() && is_performance_schema(schema_name)) { |
| 2113 |
dd::sdi::drop(thd, table); |
| 2114 |
} |
| 2115 |
thd->system_thread = saved_thread_type; |
| 2116 |
|
| 2117 |
if (skip_schema_tables(table, table_name, schema_name)) { |
| 2118 |
return (false); |
| 2119 |
} |
| 2120 |
|
| 2121 |
/* Throw warning for MyIsam and CSV tables for which data is |
| 2122 |
not cloned. These tables would be empty after clone. */ |
| 2123 |
if (!is_drop() && !is_system_schema(schema_name)) { |
| 2124 |
ib::warn(ER_IB_CLONE_NON_INNODB_TABLE, schema_name, table_name); |
| 2125 |
} |
| 2126 |
|
| 2127 |
auto ret_val = |
| 2128 |
execute_sql(thd, schema_name, table_name, nullptr, thread_number); |
| 2129 |
return (ret_val); |
| 2130 |
} |
| 2131 |
|
| 2132 |
bool Fixup_data::execute_sql(THD *thd, const char *schema_name, |
| 2133 |
const char *table_name, |
| 2134 |
const char *tablespace_name, |
| 2135 |
size_t thread_number) { |
| 2136 |
char sql_stmt[FN_LEN + FN_LEN + 64]; |
| 2137 |
|
| 2138 |
if (tablespace_name != nullptr) { |
| 2139 |
/* TABLESPACE operation */ |
| 2140 |
snprintf(sql_stmt, sizeof(sql_stmt), "DROP TABLESPACE `%s`", |
| 2141 |
tablespace_name); |
| 2142 |
|
| 2143 |
} else if (table_name != nullptr) { |
| 2144 |
/* TABLE operation */ |
| 2145 |
snprintf(sql_stmt, sizeof(sql_stmt), "%s TABLE `%s`.`%s`", sql_operation(), |
| 2146 |
schema_name, table_name); |
| 2147 |
} else { |
| 2148 |
/* SCHEMA operation */ |
| 2149 |
snprintf(sql_stmt, sizeof(sql_stmt), "DROP SCHEMA `%s`", schema_name); |
| 2150 |
} |
| 2151 |
|
| 2152 |
auto saved_thread_type = thd->system_thread; |
| 2153 |
if (!is_drop()) { |
| 2154 |
/* No MDL locks during initialization phase. */ |
| 2155 |
thd->system_thread = SYSTEM_THREAD_DD_INITIALIZE; |
| 2156 |
} |
| 2157 |
|
| 2158 |
/* Skip error while attempting drop concurrently using multiple workers. |
| 2159 |
We will handle the skipped objects later in in main thread.*/ |
| 2160 |
bool skip_error = is_drop() && allow_concurrent(); |
| 2161 |
|
| 2162 |
auto ret_val = |
| 2163 |
clone_execute_query(thd, &sql_stmt[0], thread_number, skip_error); |
| 2164 |
if (ret_val) { |
| 2165 |
++m_num_errors; |
| 2166 |
} |
| 2167 |
|
| 2168 |
thd->system_thread = saved_thread_type; |
| 2169 |
|
| 2170 |
if (is_drop() && !ret_val && !thd->check_clone_vio()) { |
| 2171 |
auto err = ER_QUERY_INTERRUPTED; |
| 2172 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 2173 |
++m_num_errors; |
| 2174 |
|
| 2175 |
auto da = thd->get_stmt_da(); |
| 2176 |
ib::info(ER_IB_CLONE_SQL) |
| 2177 |
<< "Clone: Failed to " << sql_stmt << " task: " << thread_number |
| 2178 |
<< " code: " << err << ": " |
| 2179 |
<< ((da == nullptr || !da->is_error()) ? "" : da->message_text()); |
| 2180 |
} |
| 2181 |
return (ret_val); |
| 2182 |
} |
| 2183 |
|
| 2184 |
template <> |
| 2185 |
bool Fixup_data::fix_one_object(THD *thd, const dd::Schema *schema, |
| 2186 |
size_t thread_number) { |
| 2187 |
const auto schema_name = schema->name().c_str(); |
| 2188 |
|
| 2189 |
if (skip_schema(schema_name)) { |
| 2190 |
return (false); |
| 2191 |
} |
| 2192 |
|
| 2193 |
if (is_drop()) { |
| 2194 |
auto ret_val = |
| 2195 |
execute_sql(thd, schema_name, nullptr, nullptr, thread_number); |
| 2196 |
return (ret_val); |
| 2197 |
} |
| 2198 |
|
| 2199 |
/* Convert schema name to directory name to handle special characters. */ |
| 2200 |
char schema_dir[FN_REFLEN]; |
| 2201 |
static_cast<void>( |
| 2202 |
tablename_to_filename(schema_name, schema_dir, sizeof(schema_dir))); |
| 2203 |
MY_STAT stat_info; |
| 2204 |
if (mysql_file_stat(key_file_misc, schema_dir, &stat_info, MYF(0)) != |
| 2205 |
nullptr) { |
| 2206 |
/* Schema directory exists */ |
| 2207 |
return (false); |
| 2208 |
} |
| 2209 |
|
| 2210 |
if (my_mkdir(schema_dir, 0777, MYF(0)) < 0) { |
| 2211 |
ib::error(ER_IB_CLONE_INTERNAL) |
| 2212 |
<< "Clone: Failed to create schema directory: " << schema_name |
| 2213 |
<< " task: " << thread_number; |
| 2214 |
++m_num_errors; |
| 2215 |
return (true); |
| 2216 |
} |
| 2217 |
|
| 2218 |
ib::info(ER_IB_CLONE_SQL) |
| 2219 |
<< "Clone: Fixed Schema: " << schema_name << " task: " << thread_number; |
| 2220 |
return (false); |
| 2221 |
} |
| 2222 |
|
| 2223 |
template <> |
| 2224 |
bool Fixup_data::fix_one_object(THD *thd, const dd::Tablespace *tablespace, |
| 2225 |
size_t thread_number) { |
| 2226 |
ut_ad(is_drop()); |
| 2227 |
|
| 2228 |
if (skip_tablespace(thd, tablespace)) { |
| 2229 |
return (false); |
| 2230 |
} |
| 2231 |
|
| 2232 |
const auto tablespace_name = tablespace->name().c_str(); |
| 2233 |
|
| 2234 |
auto ret_val = |
| 2235 |
execute_sql(thd, nullptr, nullptr, tablespace_name, thread_number); |
| 2236 |
return (ret_val); |
| 2237 |
} |
| 2238 |
} /* namespace */ |
| 2239 |
|
| 2240 |
bool fix_cloned_tables(THD *thd) { |
| 2241 |
std::string fixup_file(CLONE_INNODB_FIXUP_FILE); |
| 2242 |
|
| 2243 |
/* Check if table fix up is needed. */ |
| 2244 |
if (!file_exists(fixup_file)) { |
| 2245 |
return (false); |
| 2246 |
} |
| 2247 |
|
| 2248 |
auto dc = dd::get_dd_client(thd); |
| 2249 |
Releaser releaser(dc); |
| 2250 |
|
| 2251 |
Fixup_data clone_fixup(true, false); |
| 2252 |
|
| 2253 |
ib::info(ER_IB_CLONE_SQL) << "Clone Fixup: check and create schema directory"; |
| 2254 |
DD_Objs<dd::Schema> schemas; |
| 2255 |
|
| 2256 |
if (dc->fetch_global_components(&schemas) || clone_fixup.fix(thd, schemas)) { |
| 2257 |
return (true); |
| 2258 |
} |
| 2259 |
|
| 2260 |
ib::info(ER_IB_CLONE_SQL) |
| 2261 |
<< "Clone Fixup: create empty MyIsam and CSV tables"; |
| 2262 |
DD_Objs<dd::Table> tables; |
| 2263 |
|
| 2264 |
if (dc->fetch_global_components(&tables) || clone_fixup.fix(thd, tables)) { |
| 2265 |
return (true); |
| 2266 |
} |
| 2267 |
|
| 2268 |
ib::info(ER_IB_CLONE_SQL) << "Clone Fixup: replication configuration tables"; |
| 2269 |
if (clone_fixup.fix_config_tables(thd)) { |
| 2270 |
return (true); |
| 2271 |
} |
| 2272 |
|
| 2273 |
ib::info(ER_IB_CLONE_SQL) << "Clone Fixup: finished successfully"; |
| 2274 |
remove_file(fixup_file); |
| 2275 |
return (false); |
| 2276 |
} |
| 2277 |
|
| 2278 |
static bool clone_execute_query(THD *thd, const char *sql_stmt, |
| 2279 |
size_t thread_number, bool skip_error) { |
| 2280 |
thd->set_query_id(next_query_id()); |
| 2281 |
|
| 2282 |
/* We use the code from dd::excute_query here to capture the error. */ |
| 2283 |
Ed_connection con(thd); |
| 2284 |
std::string query(sql_stmt); |
| 2285 |
|
| 2286 |
LEX_STRING str; |
| 2287 |
lex_string_strmake(thd->mem_root, &str, query.c_str(), query.length()); |
| 2288 |
|
| 2289 |
auto saved_thd_system = thd->system_thread; |
| 2290 |
/* For visibility in SHOW PROCESS LIST during execute direct. */ |
| 2291 |
if (thd->system_thread == NON_SYSTEM_THREAD) { |
| 2292 |
thd->system_thread = SYSTEM_THREAD_BACKGROUND; |
| 2293 |
} |
| 2294 |
|
| 2295 |
if (con.execute_direct(str)) { |
| 2296 |
thd->system_thread = saved_thd_system; |
| 2297 |
auto sql_errno = con.get_last_errno(); |
| 2298 |
const char *sql_state = mysql_errno_to_sqlstate(sql_errno); |
| 2299 |
const char *sql_errmsg = con.get_last_error(); |
| 2300 |
|
| 2301 |
/* Skip error, if asked. Don't skip query interruption request. */ |
| 2302 |
if (skip_error && sql_errno != ER_QUERY_INTERRUPTED) { |
| 2303 |
ib::info(ER_IB_CLONE_SQL) |
| 2304 |
<< "Clone: Skipped " << sql_stmt << " task: " << thread_number |
| 2305 |
<< " Reason = " << sql_errno << ": " << sql_errmsg; |
| 2306 |
return (false); |
| 2307 |
} |
| 2308 |
|
| 2309 |
ib::info(ER_IB_CLONE_SQL) |
| 2310 |
<< "Clone: Failed to " << sql_stmt << " task: " << thread_number |
| 2311 |
<< " code: " << sql_errno << ": " << sql_errmsg; |
| 2312 |
|
| 2313 |
/* Update the error to THD. */ |
| 2314 |
auto da = thd->get_stmt_da(); |
| 2315 |
if (da != nullptr) { |
| 2316 |
da->set_overwrite_status(true); |
| 2317 |
da->set_error_status(sql_errno, sql_errmsg, sql_state); |
| 2318 |
da->push_warning(thd, sql_errno, sql_state, Sql_condition::SL_ERROR, |
| 2319 |
sql_errmsg); |
| 2320 |
da->set_overwrite_status(false); |
| 2321 |
} |
| 2322 |
return (true); |
| 2323 |
} |
| 2324 |
|
| 2325 |
thd->system_thread = saved_thd_system; |
| 2326 |
return (false); |
| 2327 |
} |
| 2328 |
|
| 2329 |
static int clone_drop_binary_logs(THD *thd) { |
| 2330 |
int err = 0; |
| 2331 |
/* No privilege check needed for individual tables. */ |
| 2332 |
auto saved_sctx = thd->security_context(); |
| 2333 |
Security_context sctx(*saved_sctx); |
| 2334 |
skip_grants(thd, sctx); |
| 2335 |
thd->set_security_context(&sctx); |
| 2336 |
|
| 2337 |
/* 1. Attempt to stop slaves if any. */ |
| 2338 |
char sql_stmt[FN_LEN + FN_LEN + 64]; |
| 2339 |
snprintf(sql_stmt, sizeof(sql_stmt), "STOP SLAVE"); |
| 2340 |
|
| 2341 |
channel_map.rdlock(); |
| 2342 |
auto is_slave = is_slave_configured(); |
| 2343 |
channel_map.unlock(); |
| 2344 |
|
| 2345 |
if (is_slave && clone_execute_query(thd, &sql_stmt[0], 1, false)) { |
| 2346 |
err = ER_INTERNAL_ERROR; |
| 2347 |
my_error(err, MYF(0), "Clone failed to stop slave"); |
| 2348 |
} |
| 2349 |
|
| 2350 |
if (err == 0) { |
| 2351 |
/* Clear warnings if any. */ |
| 2352 |
thd->clear_error(); |
| 2353 |
|
| 2354 |
/* 2. Clear all binary logs and GTID. */ |
| 2355 |
snprintf(sql_stmt, sizeof(sql_stmt), "RESET MASTER"); |
| 2356 |
|
| 2357 |
if (clone_execute_query(thd, &sql_stmt[0], 1, false)) { |
| 2358 |
err = ER_INTERNAL_ERROR; |
| 2359 |
my_error(err, MYF(0), "Clone failed to reset binary logs"); |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
/* Set back old security context. */ |
| 2364 |
thd->set_security_context(saved_sctx); |
| 2365 |
return (err); |
| 2366 |
} |
| 2367 |
|
| 2368 |
static int clone_drop_user_data(THD *thd, bool allow_threads) { |
| 2369 |
ib::warn(ER_IB_CLONE_USER_DATA, "Started"); |
| 2370 |
Clone_handler::set_drop_data(); |
| 2371 |
|
| 2372 |
auto dc = dd::get_dd_client(thd); |
| 2373 |
Releaser releaser(dc); |
| 2374 |
Fixup_data clone_fixup(allow_threads, true); |
| 2375 |
|
| 2376 |
ib::info(ER_IB_CLONE_SQL) << "Clone Drop all user data"; |
| 2377 |
DD_Objs<dd::Table> tables; |
| 2378 |
|
| 2379 |
if (dc->fetch_global_components(&tables) || clone_fixup.fix(thd, tables)) { |
| 2380 |
ib::info(ER_IB_CLONE_SQL) << "Clone failed to drop all user tables"; |
| 2381 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Clone failed to drop all user tables"); |
| 2382 |
|
| 2383 |
/* Get the first error reported. */ |
| 2384 |
auto da = thd->get_stmt_da(); |
| 2385 |
return (da->mysql_errno()); |
| 2386 |
} |
| 2387 |
|
| 2388 |
ib::info(ER_IB_CLONE_SQL) << "Clone Drop User schemas"; |
| 2389 |
DD_Objs<dd::Schema> schemas; |
| 2390 |
|
| 2391 |
if (dc->fetch_global_components(&schemas) || clone_fixup.fix(thd, schemas)) { |
| 2392 |
ib::info(ER_IB_CLONE_SQL) << "Clone failed to drop all user schemas"; |
| 2393 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 2394 |
"Clone failed to drop all user schemas"); |
| 2395 |
|
| 2396 |
/* Get the first error reported. */ |
| 2397 |
auto da = thd->get_stmt_da(); |
| 2398 |
return (da->mysql_errno()); |
| 2399 |
} |
| 2400 |
|
| 2401 |
ib::info(ER_IB_CLONE_SQL) << "Clone Drop User tablespaces"; |
| 2402 |
DD_Objs<dd::Tablespace> tablesps; |
| 2403 |
|
| 2404 |
if (dc->fetch_global_components(&tablesps) || |
| 2405 |
clone_fixup.fix(thd, tablesps)) { |
| 2406 |
ib::info(ER_IB_CLONE_SQL) << "Clone failed to drop all user tablespaces"; |
| 2407 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 2408 |
"Clone failed to drop all user tablespaces"); |
| 2409 |
|
| 2410 |
/* Get the first error reported. */ |
| 2411 |
auto da = thd->get_stmt_da(); |
| 2412 |
return (da->mysql_errno()); |
| 2413 |
} |
| 2414 |
|
| 2415 |
/* Clean binary logs after removing all user data. */ |
| 2416 |
if (!allow_threads) { |
| 2417 |
auto err = clone_drop_binary_logs(thd); |
| 2418 |
if (err != 0) { |
| 2419 |
return (err); |
| 2420 |
} |
| 2421 |
} |
| 2422 |
ib::info(ER_IB_CLONE_SQL) << "Clone Drop: finished successfully"; |
| 2423 |
ib::warn(ER_IB_CLONE_USER_DATA, "Finished"); |
| 2424 |
return (0); |
| 2425 |
} |
| 2426 |
|
| 2427 |
static void clone_init_compression(THD *thd) { |
| 2428 |
/* Need to call once in server lifetime. No Concurrency involved as |
| 2429 |
one clone operation is supported at a time. */ |
| 2430 |
static bool compression_initialized = false; |
| 2431 |
if (compression_initialized) { |
| 2432 |
return; |
| 2433 |
} |
| 2434 |
|
| 2435 |
ib::info(ER_IB_CLONE_SQL) << "Clone: Started initializing compressed tables"; |
| 2436 |
|
| 2437 |
auto dc = dd::get_dd_client(thd); |
| 2438 |
Releaser releaser(dc); |
| 2439 |
|
| 2440 |
std::vector<dd::Object_id> dd_table_ids; |
| 2441 |
|
| 2442 |
if (dc->fetch_global_component_ids<dd::Table>(&dd_table_ids)) { |
| 2443 |
ut_d(ut_error); |
| 2444 |
ut_o(return); |
| 2445 |
} |
| 2446 |
|
| 2447 |
for (auto dd_table_id : dd_table_ids) { |
| 2448 |
Releaser releaser_loop(dc); |
| 2449 |
dd::Table *dd_table = nullptr; |
| 2450 |
|
| 2451 |
/* Acquire a local copy, without MDL lock. Any transaction consistent |
| 2452 |
snapshot from DD metadata tables should do here. */ |
| 2453 |
auto fail = dc->acquire_uncached<dd::Table>(dd_table_id, &dd_table); |
| 2454 |
|
| 2455 |
if (fail || dd_table == nullptr) { |
| 2456 |
continue; |
| 2457 |
} |
| 2458 |
|
| 2459 |
/* Skip non-innodb tables. */ |
| 2460 |
auto se = |
| 2461 |
ha_resolve_by_name_raw(thd, lex_cstring_handle(dd_table->engine())); |
| 2462 |
auto se_type = ha_legacy_type(se ? plugin_data<handlerton *>(se) : nullptr); |
| 2463 |
plugin_unlock(thd, se); |
| 2464 |
|
| 2465 |
if (se_type != DB_TYPE_INNODB) { |
| 2466 |
continue; |
| 2467 |
} |
| 2468 |
|
| 2469 |
auto &options = dd_table->options(); |
| 2470 |
|
| 2471 |
if (!options.exists("compress")) { |
| 2472 |
continue; |
| 2473 |
} |
| 2474 |
|
| 2475 |
dd::String_type compress_option; |
| 2476 |
options.get("compress", &compress_option); |
| 2477 |
auto dd_index = dd_first_index(dd_table); |
| 2478 |
|
| 2479 |
if (dd_index == nullptr) { |
| 2480 |
/* Innodb table must have index. */ |
| 2481 |
ut_d(ut_error); |
| 2482 |
ut_o(continue); |
| 2483 |
} |
| 2484 |
|
| 2485 |
dd_set_tablespace_compression(dc, compress_option.c_str(), |
| 2486 |
dd_index->tablespace_id()); |
| 2487 |
} |
| 2488 |
compression_initialized = true; |
| 2489 |
ib::info(ER_IB_CLONE_SQL) << "Clone: Finished initializing compressed tables"; |
| 2490 |
} |
| 2491 |
|
| 2492 |
Clone_notify::Clone_notify(Clone_notify::Type type, space_id_t space, |
| 2493 |
bool no_wait) |
| 2494 |
: m_space_id(space), |
| 2495 |
m_type(type), |
| 2496 |
m_wait(Wait_at::NONE), |
| 2497 |
m_blocked_state(), |
| 2498 |
m_error() { |
| 2499 |
DEBUG_SYNC_C("clone_notify_ddl"); |
| 2500 |
|
| 2501 |
if (fsp_is_system_temporary(space) || m_type == Type::SPACE_ALTER_INPLACE) { |
| 2502 |
/* No need to block clone. */ |
| 2503 |
return; |
| 2504 |
} |
| 2505 |
|
| 2506 |
std::string ntfn_mesg; |
| 2507 |
IB_mutex_guard sys_mutex(clone_sys->get_mutex(), UT_LOCATION_HERE); |
| 2508 |
|
| 2509 |
bool clone_active = false; |
| 2510 |
Clone_Handle *clone_donor = nullptr; |
| 2511 |
|
| 2512 |
std::tie(clone_active, clone_donor) = clone_sys->check_active_clone(); |
| 2513 |
|
| 2514 |
/* This is for special case when clone_ddl_timeout is set to zero. DDL |
| 2515 |
needs to abort any running clone in this case. */ |
| 2516 |
if (clone_active && clone_donor->abort_by_ddl()) { |
| 2517 |
clone_sys->mark_abort(true); |
| 2518 |
m_wait = Wait_at::ABORT; |
| 2519 |
return; |
| 2520 |
} |
| 2521 |
|
| 2522 |
if (type == Type::SYSTEM_REDO_DISABLE || type == Type::SPACE_IMPORT) { |
| 2523 |
if (clone_active) { |
| 2524 |
get_mesg(true, ntfn_mesg); |
| 2525 |
ib::info(ER_IB_MSG_CLONE_DDL_NTFN) << ntfn_mesg; |
| 2526 |
|
| 2527 |
m_error = ER_CLONE_IN_PROGRESS; |
| 2528 |
my_error(ER_CLONE_IN_PROGRESS, MYF(0)); |
| 2529 |
return; |
| 2530 |
} |
| 2531 |
|
| 2532 |
clone_sys->mark_abort(false); |
| 2533 |
m_wait = Wait_at::ABORT; |
| 2534 |
return; |
| 2535 |
} |
| 2536 |
|
| 2537 |
if (!clone_active) { |
| 2538 |
/* Let any new clone block at the beginning. */ |
| 2539 |
clone_sys->mark_wait(); |
| 2540 |
m_wait = Wait_at::ENTER; |
| 2541 |
return; |
| 2542 |
} |
| 2543 |
|
| 2544 |
bool abort_if_failed = false; |
| 2545 |
|
| 2546 |
if (type == Type::SPACE_ALTER_ENCRYPT_GENERAL || |
| 2547 |
type == Type::SPACE_ALTER_ENCRYPT_GENERAL_FLAGS) { |
| 2548 |
/* For general tablespace, Encryption of data pages are always rolled |
| 2549 |
forward as of today. Since we cannot rollback the DDL, clone is aborted |
| 2550 |
on any failure here. */ |
| 2551 |
abort_if_failed = true; |
| 2552 |
|
| 2553 |
} else if (type == Type::SPACE_DROP) { |
| 2554 |
/* Post DDL operations should not fail, the transaction is already |
| 2555 |
committed. */ |
| 2556 |
abort_if_failed = true; |
| 2557 |
} |
| 2558 |
|
| 2559 |
get_mesg(true, ntfn_mesg); |
| 2560 |
ib::info(ER_IB_MSG_CLONE_DDL_NTFN) << ntfn_mesg; |
| 2561 |
|
| 2562 |
DEBUG_SYNC_C("clone_notify_ddl_before_state_block"); |
| 2563 |
|
| 2564 |
/* Check if clone needs to block at state change. */ |
| 2565 |
if (clone_sys->begin_ddl_state(m_type, m_space_id, no_wait, true, |
| 2566 |
m_blocked_state, m_error)) { |
| 2567 |
m_wait = Wait_at::STATE_CHANGE; |
| 2568 |
ut_ad(!failed()); |
| 2569 |
return; |
| 2570 |
} |
| 2571 |
|
| 2572 |
DEBUG_SYNC_C("clone_notify_ddl_after_state_block"); |
| 2573 |
|
| 2574 |
DBUG_EXECUTE_IF("clone_ddl_error_abort", abort_if_failed = true;); |
| 2575 |
|
| 2576 |
/* Abort clone on failure, if requested. This is required when caller cannot |
| 2577 |
rollback on failure. Currently enable & disable encryption needs this. In |
| 2578 |
this case we need to force clone to abort. */ |
| 2579 |
if (failed() && abort_if_failed) { |
| 2580 |
/* Clear any error raised. */ |
| 2581 |
m_error = 0; |
| 2582 |
auto thd = thd_get_current_thd(); |
| 2583 |
if (thd != nullptr) { |
| 2584 |
thd->clear_error(); |
| 2585 |
thd->get_stmt_da()->reset_condition_info(thd); |
| 2586 |
} |
| 2587 |
|
| 2588 |
clone_sys->mark_abort(true); |
| 2589 |
m_wait = Wait_at::ABORT; |
| 2590 |
return; |
| 2591 |
} |
| 2592 |
ut_ad(m_wait == Wait_at::NONE); |
| 2593 |
} |
| 2594 |
|
| 2595 |
Clone_notify::~Clone_notify() { |
| 2596 |
IB_mutex_guard sys_mutex(clone_sys->get_mutex(), UT_LOCATION_HERE); |
| 2597 |
|
| 2598 |
switch (m_wait) { |
| 2599 |
case Wait_at::ENTER: |
| 2600 |
clone_sys->mark_free(); |
| 2601 |
break; |
| 2602 |
|
| 2603 |
case Wait_at::STATE_CHANGE: |
| 2604 |
clone_sys->end_ddl_state(m_type, m_space_id, m_blocked_state); |
| 2605 |
break; |
| 2606 |
|
| 2607 |
case Wait_at::ABORT: |
| 2608 |
clone_sys->mark_active(); |
| 2609 |
break; |
| 2610 |
|
| 2611 |
case Wait_at::NONE: |
| 2612 |
[[fallthrough]]; |
| 2613 |
|
| 2614 |
default: |
| 2615 |
return; |
| 2616 |
} |
| 2617 |
|
| 2618 |
if (clone_sys->check_active_clone(false)) { |
| 2619 |
std::string ntfn_mesg; |
| 2620 |
get_mesg(false, ntfn_mesg); |
| 2621 |
ib::info(ER_IB_MSG_CLONE_DDL_NTFN) << ntfn_mesg; |
| 2622 |
} |
| 2623 |
} |
| 2624 |
|
| 2625 |
void Clone_notify::get_mesg(bool begin, std::string &mesg) { |
| 2626 |
if (begin) { |
| 2627 |
mesg.assign("BEGIN "); |
| 2628 |
} else { |
| 2629 |
mesg.assign("END "); |
| 2630 |
} |
| 2631 |
|
| 2632 |
switch (m_type) { |
| 2633 |
case Type::SPACE_CREATE: |
| 2634 |
mesg.append("[SPACE_CREATE] "); |
| 2635 |
break; |
| 2636 |
case Type::SPACE_DROP: |
| 2637 |
mesg.append("[SPACE_DROP] : "); |
| 2638 |
break; |
| 2639 |
case Type::SPACE_RENAME: |
| 2640 |
mesg.append("[SPACE_RENAME] "); |
| 2641 |
break; |
| 2642 |
case Type::SPACE_ALTER_ENCRYPT: |
| 2643 |
mesg.append("[SPACE_ALTER_ENCRYPT] "); |
| 2644 |
break; |
| 2645 |
case Type::SPACE_IMPORT: |
| 2646 |
mesg.append("[SPACE_IMPORT] "); |
| 2647 |
break; |
| 2648 |
case Type::SPACE_ALTER_ENCRYPT_GENERAL: |
| 2649 |
mesg.append("[SPACE_ALTER_ENCRYPT_GENERAL] "); |
| 2650 |
break; |
| 2651 |
case Type::SPACE_ALTER_ENCRYPT_GENERAL_FLAGS: |
| 2652 |
mesg.append("[SPACE_ALTER_ENCRYPT_GENERAL_FLAGS] "); |
| 2653 |
break; |
| 2654 |
/* purecov: begin deadcode */ |
| 2655 |
case Type::SPACE_ALTER_INPLACE: |
| 2656 |
mesg.append("[SPACE_ALTER_INPLACE] "); |
| 2657 |
break; |
| 2658 |
/* purecov: end */ |
| 2659 |
case Type::SPACE_ALTER_INPLACE_BULK: |
| 2660 |
mesg.append("[SPACE_ALTER_INPLACE_BULK] "); |
| 2661 |
break; |
| 2662 |
case Type::SPACE_UNDO_DDL: |
| 2663 |
mesg.append("[SPACE_UNDO_DDL] "); |
| 2664 |
break; |
| 2665 |
case Type::SYSTEM_REDO_DISABLE: |
| 2666 |
mesg.append("[SYSTEM_REDO_DISABLE] Space ID"); |
| 2667 |
break; |
| 2668 |
default: |
| 2669 |
mesg.append("[UNKNOWN] "); |
| 2670 |
break; |
| 2671 |
} |
| 2672 |
|
| 2673 |
mesg.append("Space ID: "); |
| 2674 |
mesg.append(std::to_string(m_space_id)); |
| 2675 |
|
| 2676 |
if (m_space_id == dict_sys_t::s_invalid_space_id) { |
| 2677 |
return; |
| 2678 |
} |
| 2679 |
auto fil_space = fil_space_get(m_space_id); |
| 2680 |
if (fil_space == nullptr) { |
| 2681 |
return; |
| 2682 |
} |
| 2683 |
auto &file = fil_space->files.front(); |
| 2684 |
mesg.append(" File: "); |
| 2685 |
mesg.append(file.name); |
| 2686 |
} |
| 2687 |
|
| 2688 |
static int clone_init_tablespaces(THD *thd) { |
| 2689 |
if (clone_sys->is_space_initialized()) { |
| 2690 |
return 0; |
| 2691 |
} |
| 2692 |
|
| 2693 |
/* We need to acquire X backup lock here to prevent DDLs. Clone by default |
| 2694 |
skips DDL lock. The API can handle recursive calls and it is not an issue |
| 2695 |
if clone has already acquired backup lock. */ |
| 2696 |
auto timeout = static_cast<ulong>(get_ddl_timeout(thd)); |
| 2697 |
|
| 2698 |
if (acquire_exclusive_backup_lock(thd, timeout, false)) { |
| 2699 |
/* Timeout on backup lock. */ |
| 2700 |
my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0)); |
| 2701 |
return ER_LOCK_WAIT_TIMEOUT; |
| 2702 |
} |
| 2703 |
|
| 2704 |
ib::info(ER_IB_CLONE_SQL) << "Clone: Started loading tablespaces"; |
| 2705 |
auto dc = dd::get_dd_client(thd); |
| 2706 |
Releaser releaser(dc); |
| 2707 |
|
| 2708 |
DD_Objs<dd::Tablespace> dd_spaces; |
| 2709 |
|
| 2710 |
if (dc->fetch_global_components(&dd_spaces)) { |
| 2711 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 2712 |
"Innodb Clone failed to load tablespaces"); |
| 2713 |
|
| 2714 |
release_backup_lock(thd); |
| 2715 |
ut_d(ut_error); |
| 2716 |
ut_o(return ER_INTERNAL_ERROR); |
| 2717 |
} |
| 2718 |
|
| 2719 |
for (auto dd_space : dd_spaces) { |
| 2720 |
/* Ignore non-innodb tablespaces. */ |
| 2721 |
if (dd_space->engine() != innobase_hton_name) { |
| 2722 |
continue; |
| 2723 |
} |
| 2724 |
|
| 2725 |
/* Get SE private data and extract space ID, name & flags */ |
| 2726 |
const auto &se_data = dd_space->se_private_data(); |
| 2727 |
|
| 2728 |
/* Get space name. */ |
| 2729 |
const char *space_name = dd_space->name().c_str(); |
| 2730 |
|
| 2731 |
/* Get space ID. */ |
| 2732 |
space_id_t space_id = dict_sys_t::s_invalid_space_id; |
| 2733 |
|
| 2734 |
if (!se_data.exists(dd_space_key_strings[DD_SPACE_ID]) || |
| 2735 |
se_data.get(dd_space_key_strings[DD_SPACE_ID], &space_id)) { |
| 2736 |
ib::error(ER_IB_CLONE_INTERNAL) |
| 2737 |
<< "Clone Error getting ID from DD, space: : " << space_name; |
| 2738 |
ut_d(ut_error); |
| 2739 |
ut_o(continue); |
| 2740 |
} |
| 2741 |
|
| 2742 |
/* This function has a side effect to adjust space name. The operation |
| 2743 |
is idempotent and done under shard mutex. We check first without acquiring |
| 2744 |
expensive dict sys mutex to skip tables that are already loaded. */ |
| 2745 |
if (fil_space_exists_in_mem(space_id, space_name, false, true)) { |
| 2746 |
continue; |
| 2747 |
} |
| 2748 |
|
| 2749 |
/* Get space flags. */ |
| 2750 |
uint32_t space_flags = 0; |
| 2751 |
if (!se_data.exists(dd_space_key_strings[DD_SPACE_FLAGS]) || |
| 2752 |
se_data.get(dd_space_key_strings[DD_SPACE_FLAGS], &space_flags)) { |
| 2753 |
ib::error(ER_IB_CLONE_INTERNAL) |
| 2754 |
<< "Clone Error getting flags from DD, space: : " << space_name; |
| 2755 |
ut_d(ut_error); |
| 2756 |
ut_o(continue); |
| 2757 |
} |
| 2758 |
|
| 2759 |
/* Get the filename. */ |
| 2760 |
const auto file = *dd_space->files().begin(); |
| 2761 |
std::string filename; |
| 2762 |
filename.assign(file->filename().c_str()); |
| 2763 |
|
| 2764 |
/* Acquire dict mutex to prevent race against concurrent DML trying to |
| 2765 |
load the space. */ |
| 2766 |
IB_mutex_guard sys_mutex(&dict_sys->mutex, UT_LOCATION_HERE); |
| 2767 |
|
| 2768 |
/* Re-check if space exists after acquiring dict sys mutex. Concurrent |
| 2769 |
DML could have already loaded the space. Space name is already adjusted |
| 2770 |
in previous call. */ |
| 2771 |
if (fil_space_exists_in_mem(space_id, space_name, false, false)) { |
| 2772 |
continue; |
| 2773 |
} |
| 2774 |
|
| 2775 |
auto err = fil_ibd_open(false, FIL_TYPE_TABLESPACE, space_id, space_flags, |
| 2776 |
space_name, filename.c_str(), false, false); |
| 2777 |
|
| 2778 |
if (err != DB_SUCCESS) { |
| 2779 |
ib::error(ER_IB_CLONE_INTERNAL) |
| 2780 |
<< "Clone Error opening space: " << space_name |
| 2781 |
<< " File: " << filename; |
| 2782 |
} |
| 2783 |
} |
| 2784 |
|
| 2785 |
clone_sys->set_space_initialized(); |
| 2786 |
ib::info(ER_IB_CLONE_SQL) << "Clone: Finished loading tablespaces"; |
| 2787 |
|
| 2788 |
release_backup_lock(thd); |
| 2789 |
return 0; |
| 2790 |
} |
| 2791 |
|
| 2792 |
Clone_Sys::Wait_stage::Wait_stage(const char *new_info) { |
| 2793 |
m_saved_info = nullptr; |
| 2794 |
THD *thd = thd_get_current_thd(); |
| 2795 |
|
| 2796 |
if (thd != nullptr) { |
| 2797 |
m_saved_info = thd->proc_info(); |
| 2798 |
thd->set_proc_info(new_info); |
| 2799 |
} |
| 2800 |
} |
| 2801 |
|
| 2802 |
Clone_Sys::Wait_stage::~Wait_stage() { |
| 2803 |
THD *thd = thd_get_current_thd(); |
| 2804 |
|
| 2805 |
if (thd != nullptr && m_saved_info != nullptr) { |
| 2806 |
thd->set_proc_info(m_saved_info); |
| 2807 |
} |
| 2808 |
} |