| 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/clone0apply.cc |
| 29 |
Innodb apply snapshot data |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#include <fstream> |
| 34 |
#include <sstream> |
| 35 |
|
| 36 |
#include "buf0dump.h" |
| 37 |
#include "clone0api.h" |
| 38 |
#include "clone0clone.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
#include "log0files_io.h" |
| 41 |
#include "sql/handler.h" |
| 42 |
|
| 43 |
int Clone_Snapshot::get_file_from_desc(const Clone_File_Meta *file_meta, |
| 44 |
const char *data_dir, bool desc_create, |
| 45 |
bool &desc_exists, |
| 46 |
Clone_file_ctx *&file_ctx) { |
| 47 |
int err = 0; |
| 48 |
|
| 49 |
mutex_enter(&m_snapshot_mutex); |
| 50 |
|
| 51 |
auto idx = file_meta->m_file_index; |
| 52 |
|
| 53 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_APPLY); |
| 54 |
|
| 55 |
ut_ad(m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY || |
| 56 |
m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY || |
| 57 |
m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY); |
| 58 |
|
| 59 |
desc_exists = false; |
| 60 |
|
| 61 |
/* File metadata is already there, possibly sent by another task. */ |
| 62 |
file_ctx = get_file_ctx_by_index(idx); |
| 63 |
|
| 64 |
if (file_ctx != nullptr) { |
| 65 |
desc_exists = true; |
| 66 |
|
| 67 |
} else if (desc_create) { |
| 68 |
/* Create the descriptor. */ |
| 69 |
err = create_desc(data_dir, file_meta, false, file_ctx); |
| 70 |
} |
| 71 |
|
| 72 |
mutex_exit(&m_snapshot_mutex); |
| 73 |
|
| 74 |
return (err); |
| 75 |
} |
| 76 |
|
| 77 |
int Clone_Snapshot::rename_desc(const Clone_File_Meta *file_meta, |
| 78 |
const char *data_dir, |
| 79 |
Clone_file_ctx *&file_ctx) { |
| 80 |
/* Create new file context with new name. */ |
| 81 |
auto err = create_desc(data_dir, file_meta, true, file_ctx); |
| 82 |
|
| 83 |
if (err != 0) { |
| 84 |
return err; /* purecov: inspected */ |
| 85 |
} |
| 86 |
|
| 87 |
file_ctx->m_state.store(Clone_file_ctx::State::RENAMED); |
| 88 |
|
| 89 |
/* Overwrite with the renamed file context. */ |
| 90 |
add_file_from_desc(file_ctx, false); |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
int Clone_Snapshot::fix_ddl_extension(const char *data_dir, |
| 96 |
Clone_file_ctx *file_ctx) { |
| 97 |
ut_ad(file_ctx->m_extension == Clone_file_ctx::Extension::DDL); |
| 98 |
|
| 99 |
/* If data directory is being replaced. */ |
| 100 |
bool replace_dir = (data_dir == nullptr); |
| 101 |
|
| 102 |
auto file_meta = file_ctx->get_file_meta(); |
| 103 |
bool is_undo_file = fsp_is_undo_tablespace(file_meta->m_space_id); |
| 104 |
bool is_redo_file = file_meta->m_space_id == dict_sys_t::s_log_space_id; |
| 105 |
|
| 106 |
auto extn = Clone_file_ctx::Extension::NONE; |
| 107 |
const std::string file_path(file_meta->m_file_name); |
| 108 |
|
| 109 |
/* Check if file is already present and extension is needed. */ |
| 110 |
auto err = handle_existing_file(replace_dir, is_undo_file, is_redo_file, |
| 111 |
file_meta->m_file_index, file_path, extn); |
| 112 |
if (err == 0) { |
| 113 |
file_ctx->m_extension = extn; |
| 114 |
} |
| 115 |
|
| 116 |
return err; |
| 117 |
} |
| 118 |
|
| 119 |
int Clone_Snapshot::update_sys_file_name(bool replace, |
| 120 |
const Clone_File_Meta *file_meta, |
| 121 |
std::string &file_name) { |
| 122 |
/* Currently needed only while replacing data directory. */ |
| 123 |
if (!replace) { |
| 124 |
return (0); |
| 125 |
} |
| 126 |
auto space_id = file_meta->m_space_id; |
| 127 |
|
| 128 |
/* Update buffer pool dump file path for provisioning. */ |
| 129 |
if (space_id == dict_sys_t::s_invalid_space_id) { |
| 130 |
ut_ad(0 == strcmp(file_name.c_str(), SRV_BUF_DUMP_FILENAME_DEFAULT)); |
| 131 |
|
| 132 |
char path[OS_FILE_MAX_PATH]; |
| 133 |
buf_dump_generate_path(path, sizeof(path)); |
| 134 |
|
| 135 |
file_name.assign(path); |
| 136 |
return (0); |
| 137 |
} |
| 138 |
|
| 139 |
/* Change name to system configured file when replacing current directory. */ |
| 140 |
if (!fsp_is_system_tablespace(space_id)) { |
| 141 |
return (0); |
| 142 |
} |
| 143 |
|
| 144 |
/* Find out the node index of the file within system tablespace. */ |
| 145 |
auto loop_index = file_meta->m_file_index; |
| 146 |
|
| 147 |
if (loop_index >= num_data_files()) { |
| 148 |
/* purecov: begin deadcode */ |
| 149 |
int err = ER_CLONE_PROTOCOL; |
| 150 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid File Index"); |
| 151 |
ut_d(ut_error); |
| 152 |
ut_o(return err); |
| 153 |
/* purecov: end */ |
| 154 |
} |
| 155 |
|
| 156 |
decltype(loop_index) node_index = 0; |
| 157 |
|
| 158 |
while (loop_index > 0) { |
| 159 |
--loop_index; |
| 160 |
auto file_ctx = get_file_ctx_by_index(loop_index); |
| 161 |
auto cur_desc = file_ctx->get_file_meta(); |
| 162 |
/* Loop through all files of current tablespace. */ |
| 163 |
if (cur_desc->m_space_id != space_id) { |
| 164 |
break; |
| 165 |
} |
| 166 |
++node_index; |
| 167 |
} |
| 168 |
|
| 169 |
auto last_file_index = |
| 170 |
static_cast<decltype(node_index)>(srv_sys_space.m_files.size() - 1); |
| 171 |
|
| 172 |
/* Check if the file is beyond maximum configured files. */ |
| 173 |
if (node_index > last_file_index) { |
| 174 |
std::ostringstream err_strm; |
| 175 |
err_strm << "innodb_data_file_path: Recipient file count: " |
| 176 |
<< last_file_index + 1 << " is less than Donor file count."; |
| 177 |
|
| 178 |
std::string err_str(err_strm.str()); |
| 179 |
|
| 180 |
my_error(ER_CLONE_SYS_CONFIG, MYF(0), err_str.c_str()); |
| 181 |
|
| 182 |
return (ER_CLONE_SYS_CONFIG); |
| 183 |
} |
| 184 |
|
| 185 |
auto &file = srv_sys_space.m_files[node_index]; |
| 186 |
page_size_t page_sz(srv_sys_space.flags()); |
| 187 |
|
| 188 |
auto size_bytes = static_cast<uint64_t>(file.size()); |
| 189 |
size_bytes *= page_sz.physical(); |
| 190 |
|
| 191 |
/* Check if the file size matches with configured files. */ |
| 192 |
if (file_meta->m_file_size != size_bytes) { |
| 193 |
/* For last file it could mismatch if auto extend is specified. */ |
| 194 |
if (node_index != last_file_index || |
| 195 |
!srv_sys_space.can_auto_extend_last_file()) { |
| 196 |
/* purecov: begin tested */ |
| 197 |
std::ostringstream err_strm; |
| 198 |
|
| 199 |
err_strm << "innodb_data_file_path: Recipient value for " << node_index |
| 200 |
<< "th file size: " << size_bytes |
| 201 |
<< " doesn't match Donor file size: " << file_meta->m_file_size; |
| 202 |
|
| 203 |
std::string err_str(err_strm.str()); |
| 204 |
|
| 205 |
my_error(ER_CLONE_SYS_CONFIG, MYF(0), err_str.c_str()); |
| 206 |
|
| 207 |
return (ER_CLONE_SYS_CONFIG); |
| 208 |
/* purecov: end */ |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/* Change filename to currently configured name. */ |
| 213 |
file_name.assign(file.filepath()); |
| 214 |
return (0); |
| 215 |
} |
| 216 |
|
| 217 |
int Clone_Snapshot::handle_existing_file(bool replace, bool undo_file, |
| 218 |
bool redo_file, |
| 219 |
uint32_t data_file_index, |
| 220 |
const std::string &data_file, |
| 221 |
Clone_file_ctx::Extension &extn) { |
| 222 |
extn = Clone_file_ctx::Extension::NONE; |
| 223 |
/* For undo tablespace, check for duplicate file name. Currently it |
| 224 |
is possible to create multiple undo tablespaces of same name under |
| 225 |
different directory. This should not be recommended and in future |
| 226 |
we aim to disallow specifying file name for tablespaces and generate |
| 227 |
it internally based on space ID. Till that time, Clone needs to identify |
| 228 |
and disallow undo tablespaces of same name as Clone creates all undo |
| 229 |
tablespaces under innodb_undo_directory configuration in recipient. */ |
| 230 |
if (undo_file) { |
| 231 |
for (auto undo_index : m_undo_file_indexes) { |
| 232 |
auto undo_file_ctx = get_file_ctx_by_index(undo_index); |
| 233 |
if (undo_file_ctx == nullptr || undo_file_ctx->deleted()) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
auto undo_meta = undo_file_ctx->get_file_meta(); |
| 237 |
|
| 238 |
if (0 == strcmp(undo_meta->m_file_name, data_file.c_str())) { |
| 239 |
/* purecov: begin tested */ |
| 240 |
std::ostringstream err_strm; |
| 241 |
err_strm << "Found multiple undo files with same name: " << data_file; |
| 242 |
std::string err_str(err_strm.str()); |
| 243 |
my_error(ER_CLONE_SYS_CONFIG, MYF(0), err_str.c_str()); |
| 244 |
return (ER_CLONE_SYS_CONFIG); |
| 245 |
/* purecov: end */ |
| 246 |
} |
| 247 |
} |
| 248 |
m_undo_file_indexes.push_back(data_file_index); |
| 249 |
/* With concurrent DDL support there could be deleted undo file |
| 250 |
indexes here. At the end of every stage, new undo files could be |
| 251 |
added limited by FSP_MAX_UNDO_TABLESPACES. */ |
| 252 |
ut_ad(m_undo_file_indexes.size() <= |
| 253 |
CLONE_MAX_TRANSFER_STAGES * FSP_MAX_UNDO_TABLESPACES); |
| 254 |
} |
| 255 |
|
| 256 |
auto type = Fil_path::get_file_type(data_file); |
| 257 |
int err = 0; |
| 258 |
|
| 259 |
/* Consider redo files as existing always if we are cloning to |
| 260 |
the same directory on which we are working. */ |
| 261 |
if (redo_file && replace && type == OS_FILE_TYPE_MISSING) { |
| 262 |
type = OS_FILE_TYPE_FILE; |
| 263 |
} |
| 264 |
|
| 265 |
/* Nothing to do if file doesn't exist. */ |
| 266 |
if (type == OS_FILE_TYPE_MISSING) { |
| 267 |
if (replace) { |
| 268 |
/* Add file to new file list to enable rollback. */ |
| 269 |
err = clone_add_to_list_file(CLONE_INNODB_NEW_FILES, data_file.c_str()); |
| 270 |
} |
| 271 |
extn = Clone_file_ctx::Extension::NONE; |
| 272 |
return err; |
| 273 |
} |
| 274 |
|
| 275 |
if (type != OS_FILE_TYPE_FILE) { |
| 276 |
/* purecov: begin inspected */ |
| 277 |
/* Either the stat() call failed or the name is a |
| 278 |
directory/block device, or permission error etc. */ |
| 279 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 280 |
my_error(ER_ERROR_ON_WRITE, MYF(0), data_file.c_str(), errno, |
| 281 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 282 |
return ER_ERROR_ON_WRITE; |
| 283 |
/* purecov: end */ |
| 284 |
} |
| 285 |
|
| 286 |
/* For cloning to different data directory, we must ensure that the |
| 287 |
file is not present. This would always fail for local clone. */ |
| 288 |
if (!replace) { |
| 289 |
my_error(ER_FILE_EXISTS_ERROR, MYF(0), data_file.c_str()); |
| 290 |
return ER_FILE_EXISTS_ERROR; |
| 291 |
} |
| 292 |
|
| 293 |
std::string replace_path, clone_file; |
| 294 |
|
| 295 |
if (redo_file) { |
| 296 |
const auto [directory, file] = Fil_path::split(data_file); |
| 297 |
replace_path = directory; |
| 298 |
clone_file = directory + CLONE_INNODB_REPLACED_FILE_EXTN + file; |
| 299 |
} else { |
| 300 |
replace_path = data_file; |
| 301 |
clone_file = data_file + CLONE_INNODB_REPLACED_FILE_EXTN; |
| 302 |
} |
| 303 |
|
| 304 |
/* Check that file with clone extension is not present */ |
| 305 |
type = Fil_path::get_file_type(clone_file); |
| 306 |
|
| 307 |
if (type != OS_FILE_TYPE_MISSING) { |
| 308 |
/* purecov: begin inspected */ |
| 309 |
my_error(ER_FILE_EXISTS_ERROR, MYF(0), clone_file.c_str()); |
| 310 |
return ER_FILE_EXISTS_ERROR; |
| 311 |
/* purecov: end */ |
| 312 |
} |
| 313 |
|
| 314 |
extn = Clone_file_ctx::Extension::REPLACE; |
| 315 |
|
| 316 |
/* Add file name to files to be replaced before recovery. */ |
| 317 |
err = |
| 318 |
clone_add_to_list_file(CLONE_INNODB_REPLACED_FILES, replace_path.c_str()); |
| 319 |
|
| 320 |
return err; |
| 321 |
} |
| 322 |
|
| 323 |
int Clone_Snapshot::build_file_path(const char *data_dir, |
| 324 |
const Clone_File_Meta *file_meta, |
| 325 |
std::string &built_path) { |
| 326 |
std::string source; |
| 327 |
|
| 328 |
bool redo_file = (m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY); |
| 329 |
bool absolute_path = false; |
| 330 |
|
| 331 |
if (!redo_file) { |
| 332 |
source.assign(file_meta->m_file_name); |
| 333 |
|
| 334 |
bool replace = (data_dir == nullptr); |
| 335 |
auto err = update_sys_file_name(replace, file_meta, source); |
| 336 |
|
| 337 |
if (err != 0) { |
| 338 |
return err; /* purecov: inspected */ |
| 339 |
} |
| 340 |
absolute_path = Fil_path::is_absolute_path(source); |
| 341 |
} |
| 342 |
|
| 343 |
/* For absolute path, copy the name and return. */ |
| 344 |
if (absolute_path) { |
| 345 |
auto is_hard_path = test_if_hard_path(source.c_str()); |
| 346 |
|
| 347 |
/* Check if the absolute path is not in right format */ |
| 348 |
if (is_hard_path == 0) { |
| 349 |
/* purecov: begin inspected */ |
| 350 |
my_error(ER_WRONG_VALUE, MYF(0), "file path", source.c_str()); |
| 351 |
return (ER_WRONG_VALUE); |
| 352 |
/* purecov: end */ |
| 353 |
} |
| 354 |
|
| 355 |
built_path.assign(source); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
bool undo_file = fsp_is_undo_tablespace(file_meta->m_space_id); |
| 360 |
|
| 361 |
/* Append appropriate data directory path. */ |
| 362 |
|
| 363 |
/* Use configured path when cloning into current data directory. */ |
| 364 |
if (data_dir == nullptr) { |
| 365 |
/* Get file path from redo configuration. */ |
| 366 |
if (redo_file) { |
| 367 |
/* Path returned by log_directory_path() will have the |
| 368 |
#innodb_redo directory at the end. */ |
| 369 |
built_path = log_directory_path(log_sys->m_files_ctx); |
| 370 |
} else if (undo_file) { |
| 371 |
/* Get file path from undo configuration. */ |
| 372 |
built_path = std::string{srv_undo_dir}; |
| 373 |
} else { |
| 374 |
built_path = std::string{}; |
| 375 |
} |
| 376 |
} else { |
| 377 |
built_path = std::string{data_dir}; |
| 378 |
/* Add #innodb_redo directory to the path if this is redo file. */ |
| 379 |
if (redo_file) { |
| 380 |
/* Add path separator at the end of file path, if not there. */ |
| 381 |
Fil_path::append_separator(built_path); |
| 382 |
built_path += LOG_DIRECTORY_NAME; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/* Add path separator if required. */ |
| 387 |
Fil_path::append_separator(built_path); |
| 388 |
|
| 389 |
/* Add file name. For redo file use standard name. */ |
| 390 |
if (redo_file) { |
| 391 |
/* This is redo file. Use standard name. */ |
| 392 |
built_path += log_file_name(log_sys->m_files_ctx, |
| 393 |
Log_file_id{file_meta->m_file_index}); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
ut_ad(!source.empty()); |
| 398 |
|
| 399 |
if (Fil_path::has_prefix(source, Fil_path::DOT_SLASH)) { |
| 400 |
source.erase(0, 2); |
| 401 |
} |
| 402 |
|
| 403 |
built_path.append(source); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
int Clone_Snapshot::build_file_ctx(Clone_file_ctx::Extension extn, |
| 408 |
const Clone_File_Meta *file_meta, |
| 409 |
const std::string &file_path, |
| 410 |
Clone_file_ctx *&file_ctx) { |
| 411 |
size_t alloc_size = sizeof(Clone_file_ctx) + file_path.length() + 1; |
| 412 |
|
| 413 |
/* Allocate for file path string. */ |
| 414 |
auto path = static_cast<char *>(mem_heap_alloc(m_snapshot_heap, alloc_size)); |
| 415 |
|
| 416 |
if (path == nullptr) { |
| 417 |
/* purecov: begin inspected */ |
| 418 |
my_error(ER_OUTOFMEMORY, MYF(0), alloc_size); |
| 419 |
return (ER_OUTOFMEMORY); |
| 420 |
/* purecov: end */ |
| 421 |
} |
| 422 |
|
| 423 |
/* Copy file metadata */ |
| 424 |
file_ctx = reinterpret_cast<Clone_file_ctx *>(path); |
| 425 |
file_ctx->init(extn); |
| 426 |
path += sizeof(Clone_file_ctx); |
| 427 |
|
| 428 |
strcpy(path, file_path.c_str()); |
| 429 |
|
| 430 |
auto ctx_file_meta = file_ctx->get_file_meta(); |
| 431 |
*ctx_file_meta = *file_meta; |
| 432 |
|
| 433 |
ctx_file_meta->m_file_name = static_cast<const char *>(path); |
| 434 |
|
| 435 |
ctx_file_meta->m_file_name_len = file_path.length() + 1; |
| 436 |
|
| 437 |
ctx_file_meta->m_file_name_alloc_len = ctx_file_meta->m_file_name_len; |
| 438 |
|
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
/** Add directory path to file |
| 443 |
@param[in] dir directory |
| 444 |
@param[in] file file name |
| 445 |
@param[out] path file along with path. */ |
| 446 |
static void add_directory_path(const char *dir, const char *file, |
| 447 |
std::string &path) { |
| 448 |
path.clear(); |
| 449 |
/* Append directory */ |
| 450 |
if (dir != nullptr) { |
| 451 |
path.assign(dir); |
| 452 |
if (path.back() != OS_PATH_SEPARATOR) { |
| 453 |
path.append(OS_PATH_SEPARATOR_STR); /* purecov: inspected */ |
| 454 |
} |
| 455 |
} |
| 456 |
/* Append file */ |
| 457 |
if (file != nullptr) { |
| 458 |
path.append(file); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
int Clone_Snapshot::create_desc(const char *data_dir, |
| 463 |
const Clone_File_Meta *file_meta, bool is_ddl, |
| 464 |
Clone_file_ctx *&file_ctx) { |
| 465 |
/* Update file path from configuration. */ |
| 466 |
std::string file_path; |
| 467 |
|
| 468 |
auto err = build_file_path(data_dir, file_meta, file_path); |
| 469 |
|
| 470 |
if (err != 0) { |
| 471 |
return (err); |
| 472 |
} |
| 473 |
|
| 474 |
auto extn = Clone_file_ctx::Extension::NONE; |
| 475 |
|
| 476 |
if (is_ddl) { |
| 477 |
extn = Clone_file_ctx::Extension::DDL; |
| 478 |
|
| 479 |
std::string ddl_list_file; |
| 480 |
add_directory_path(data_dir, CLONE_INNODB_DDL_FILES, ddl_list_file); |
| 481 |
|
| 482 |
err = clone_add_to_list_file(ddl_list_file.c_str(), file_path.c_str()); |
| 483 |
|
| 484 |
} else { |
| 485 |
/* If data directory is being replaced. */ |
| 486 |
bool replace_dir = (data_dir == nullptr); |
| 487 |
bool is_undo_file = fsp_is_undo_tablespace(file_meta->m_space_id); |
| 488 |
bool is_redo_file = file_meta->m_space_id == dict_sys_t::s_log_space_id; |
| 489 |
|
| 490 |
/* Check if file is already present in recipient. */ |
| 491 |
err = handle_existing_file(replace_dir, is_undo_file, is_redo_file, |
| 492 |
file_meta->m_file_index, file_path, extn); |
| 493 |
} |
| 494 |
|
| 495 |
if (err == 0) { |
| 496 |
/* Build complete path for the new file to be added. */ |
| 497 |
err = build_file_ctx(extn, file_meta, file_path, file_ctx); |
| 498 |
} |
| 499 |
return (err); |
| 500 |
} |
| 501 |
|
| 502 |
bool Clone_Snapshot::add_file_from_desc(Clone_file_ctx *&file_ctx, |
| 503 |
bool ddl_create) { |
| 504 |
mutex_enter(&m_snapshot_mutex); |
| 505 |
|
| 506 |
ut_ad(m_snapshot_handle_type == CLONE_HDL_APPLY); |
| 507 |
auto file_meta = file_ctx->get_file_meta(); |
| 508 |
|
| 509 |
if (m_snapshot_state == CLONE_SNAPSHOT_FILE_COPY || |
| 510 |
m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY) { |
| 511 |
if (ddl_create) { |
| 512 |
ut_a(file_meta->m_file_index == num_data_files()); |
| 513 |
/* Add data file at the end and extend length. */ |
| 514 |
m_data_file_vector.push_back(file_ctx); |
| 515 |
} else { |
| 516 |
m_data_file_vector[file_meta->m_file_index] = file_ctx; |
| 517 |
} |
| 518 |
} else { |
| 519 |
ut_ad(m_snapshot_state == CLONE_SNAPSHOT_REDO_COPY); |
| 520 |
m_redo_file_vector[file_meta->m_file_index] = file_ctx; |
| 521 |
} |
| 522 |
|
| 523 |
mutex_exit(&m_snapshot_mutex); |
| 524 |
|
| 525 |
/** Check if it the last file */ |
| 526 |
if (file_meta->m_file_index == num_data_files() - 1) { |
| 527 |
return true; |
| 528 |
} |
| 529 |
|
| 530 |
return (false); |
| 531 |
} |
| 532 |
|
| 533 |
int Clone_Handle::apply_task_metadata(Clone_Task *task, |
| 534 |
Ha_clone_cbk *callback) { |
| 535 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 536 |
uint desc_len = 0; |
| 537 |
auto serial_desc = callback->get_data_desc(&desc_len); |
| 538 |
|
| 539 |
Clone_Desc_Task_Meta task_desc; |
| 540 |
auto success = task_desc.deserialize(serial_desc, desc_len); |
| 541 |
|
| 542 |
if (!success) { |
| 543 |
int err = ER_CLONE_PROTOCOL; |
| 544 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Task Descriptor"); |
| 545 |
ut_d(ut_error); |
| 546 |
ut_o(return (err)); |
| 547 |
} |
| 548 |
task->m_task_meta = task_desc.m_task_meta; |
| 549 |
return (0); |
| 550 |
} |
| 551 |
|
| 552 |
int Clone_Handle::check_space(const Clone_Task *task) { |
| 553 |
/* Do space check only during file copy. */ |
| 554 |
auto current_state = m_clone_task_manager.get_state(); |
| 555 |
if (!task->m_is_master || current_state != CLONE_SNAPSHOT_FILE_COPY) { |
| 556 |
return (0); |
| 557 |
} |
| 558 |
uint64_t free_space; |
| 559 |
auto MySQL_datadir_abs_path = MySQL_datadir_path.abs_path(); |
| 560 |
auto data_dir = |
| 561 |
(replace_datadir() ? MySQL_datadir_abs_path.c_str() : get_datadir()); |
| 562 |
|
| 563 |
auto db_err = os_get_free_space(data_dir, free_space); |
| 564 |
/* We skip space check if the OS interface returns error. */ |
| 565 |
if (db_err != DB_SUCCESS) { |
| 566 |
ib::warn(ER_IB_CLONE_VALIDATE) |
| 567 |
<< "Clone could not validate available free space"; |
| 568 |
return (0); |
| 569 |
} |
| 570 |
|
| 571 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 572 |
auto bytes_disk = snapshot->get_disk_estimate(); |
| 573 |
|
| 574 |
std::string avaiable_space; |
| 575 |
std::string clone_space; |
| 576 |
ut_format_byte_value(bytes_disk, clone_space); |
| 577 |
ut_format_byte_value(free_space, avaiable_space); |
| 578 |
|
| 579 |
int err = 0; |
| 580 |
if (bytes_disk > free_space) { |
| 581 |
err = ER_CLONE_DISK_SPACE; |
| 582 |
my_error(err, MYF(0), clone_space.c_str(), avaiable_space.c_str()); |
| 583 |
} |
| 584 |
|
| 585 |
ib::info(ER_IB_CLONE_VALIDATE) |
| 586 |
<< "Clone estimated size: " << clone_space.c_str() |
| 587 |
<< " Available space: " << avaiable_space.c_str(); |
| 588 |
return (err); |
| 589 |
} |
| 590 |
|
| 591 |
int Clone_Handle::apply_state_metadata(Clone_Task *task, |
| 592 |
Ha_clone_cbk *callback) { |
| 593 |
int err = 0; |
| 594 |
uint desc_len = 0; |
| 595 |
auto serial_desc = callback->get_data_desc(&desc_len); |
| 596 |
|
| 597 |
Clone_Desc_State state_desc; |
| 598 |
auto success = state_desc.deserialize(serial_desc, desc_len); |
| 599 |
|
| 600 |
if (!success) { |
| 601 |
err = ER_CLONE_PROTOCOL; |
| 602 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid State Descriptor"); |
| 603 |
ut_d(ut_error); |
| 604 |
ut_o(return (err)); |
| 605 |
} |
| 606 |
if (m_clone_handle_type == CLONE_HDL_COPY) { |
| 607 |
ut_ad(state_desc.m_is_ack); |
| 608 |
m_clone_task_manager.ack_state(&state_desc); |
| 609 |
return (0); |
| 610 |
} |
| 611 |
|
| 612 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 613 |
|
| 614 |
/* ACK descriptor is sent for keeping the connection alive. */ |
| 615 |
if (state_desc.m_is_ack) { |
| 616 |
return (0); |
| 617 |
} |
| 618 |
|
| 619 |
/* Reset current chunk information */ |
| 620 |
auto &task_meta = task->m_task_meta; |
| 621 |
task_meta.m_chunk_num = 0; |
| 622 |
task_meta.m_block_num = 0; |
| 623 |
|
| 624 |
/* Move to the new state */ |
| 625 |
if (state_desc.m_is_start) { |
| 626 |
#ifdef UNIV_DEBUG |
| 627 |
/* Network failure before moving to new state */ |
| 628 |
err = m_clone_task_manager.debug_restart(task, err, 5); |
| 629 |
if (err != 0) { |
| 630 |
return err; |
| 631 |
} |
| 632 |
#endif /* UNIV_DEBUG */ |
| 633 |
|
| 634 |
/** Notify state change via callback. */ |
| 635 |
notify_state_change(task, callback, &state_desc); |
| 636 |
|
| 637 |
err = fix_all_renamed(task); |
| 638 |
|
| 639 |
if (err == 0) { |
| 640 |
err = move_to_next_state(task, nullptr, &state_desc); |
| 641 |
} |
| 642 |
|
| 643 |
#ifdef UNIV_DEBUG |
| 644 |
/* Network failure after moving to new state */ |
| 645 |
err = m_clone_task_manager.debug_restart(task, err, 0); |
| 646 |
#endif /* UNIV_DEBUG */ |
| 647 |
|
| 648 |
/* Check if enough space available on disk */ |
| 649 |
if (err == 0) { |
| 650 |
err = check_space(task); |
| 651 |
} |
| 652 |
|
| 653 |
return (err); |
| 654 |
} |
| 655 |
|
| 656 |
/* It is the end of current state. Close active file. */ |
| 657 |
err = close_file(task); |
| 658 |
|
| 659 |
#ifdef UNIV_DEBUG |
| 660 |
/* Network failure before finishing state */ |
| 661 |
err = m_clone_task_manager.debug_restart(task, err, 2); |
| 662 |
#endif /* UNIV_DEBUG */ |
| 663 |
|
| 664 |
if (err != 0) { |
| 665 |
return (err); |
| 666 |
} |
| 667 |
|
| 668 |
ut_ad(state_desc.m_state == m_clone_task_manager.get_state()); |
| 669 |
|
| 670 |
/* Mark current state finished for the task */ |
| 671 |
err = m_clone_task_manager.finish_state(task); |
| 672 |
|
| 673 |
#ifdef UNIV_DEBUG |
| 674 |
/* Network failure before sending ACK */ |
| 675 |
err = m_clone_task_manager.debug_restart(task, err, 3); |
| 676 |
#endif /* UNIV_DEBUG */ |
| 677 |
|
| 678 |
/* Send acknowledgement back to remote server */ |
| 679 |
if (err == 0 && task->m_is_master) { |
| 680 |
if (state_desc.m_state == CLONE_SNAPSHOT_FILE_COPY) { |
| 681 |
DEBUG_SYNC_C("clone_file_copy_end_before_ack"); |
| 682 |
} |
| 683 |
err = ack_state_metadata(task, callback, &state_desc); |
| 684 |
|
| 685 |
if (err != 0) { |
| 686 |
ib::info(ER_IB_CLONE_OPERATION) |
| 687 |
<< "Clone Apply Master ACK finshed state: " << state_desc.m_state; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
#ifdef UNIV_DEBUG |
| 692 |
/* Network failure after sending ACK */ |
| 693 |
err = m_clone_task_manager.debug_restart(task, err, 4); |
| 694 |
#endif /* UNIV_DEBUG */ |
| 695 |
|
| 696 |
return (err); |
| 697 |
} |
| 698 |
|
| 699 |
void Clone_Handle::notify_state_change(Clone_Task *task, Ha_clone_cbk *callback, |
| 700 |
Clone_Desc_State *state_desc) { |
| 701 |
if (!task->m_is_master) { |
| 702 |
return; |
| 703 |
} |
| 704 |
callback->mark_state_change(state_desc->m_estimate); |
| 705 |
callback->buffer_cbk(nullptr, 0); |
| 706 |
callback->clear_flags(); |
| 707 |
} |
| 708 |
|
| 709 |
int Clone_Handle::ack_state_metadata(Clone_Task *, Ha_clone_cbk *callback, |
| 710 |
Clone_Desc_State *state_desc) { |
| 711 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 712 |
|
| 713 |
state_desc->m_is_ack = true; |
| 714 |
|
| 715 |
byte desc_buf[CLONE_DESC_MAX_BASE_LEN]; |
| 716 |
|
| 717 |
auto serial_desc = &desc_buf[0]; |
| 718 |
uint desc_len = CLONE_DESC_MAX_BASE_LEN; |
| 719 |
|
| 720 |
state_desc->serialize(serial_desc, desc_len, nullptr); |
| 721 |
|
| 722 |
callback->set_data_desc(serial_desc, desc_len); |
| 723 |
callback->clear_flags(); |
| 724 |
|
| 725 |
auto err = callback->buffer_cbk(nullptr, 0); |
| 726 |
|
| 727 |
return (err); |
| 728 |
} |
| 729 |
|
| 730 |
int Clone_Handle::apply_file_delete(Clone_Task *task, Clone_file_ctx *file_ctx, |
| 731 |
const Clone_File_Meta *new_meta) { |
| 732 |
auto err = close_file(task); |
| 733 |
if (err != 0) { |
| 734 |
return err; /* purecov: inspected */ |
| 735 |
} |
| 736 |
|
| 737 |
auto file_meta = file_ctx->get_file_meta(); |
| 738 |
|
| 739 |
if (task->m_current_file_index != file_meta->m_file_index) { |
| 740 |
task->m_current_file_index = file_meta->m_file_index; |
| 741 |
} |
| 742 |
|
| 743 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 744 |
|
| 745 |
auto begin_chunk = file_meta->m_begin_chunk; |
| 746 |
auto end_chunk = file_meta->m_end_chunk; |
| 747 |
auto block_num = snapshot->get_blocks_per_chunk(); |
| 748 |
auto data_size = snapshot->get_chunk_size(); |
| 749 |
|
| 750 |
/* For page copy, we reset one page of the current chunk passed. Chunks |
| 751 |
in file_meta corresponds to chunk in file copy. */ |
| 752 |
if (snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY) { |
| 753 |
begin_chunk = new_meta->m_begin_chunk; |
| 754 |
end_chunk = begin_chunk; |
| 755 |
block_num = 0; |
| 756 |
data_size = UNIV_PAGE_SIZE; |
| 757 |
} |
| 758 |
|
| 759 |
Clone_Task_Meta new_task_meta = task->m_task_meta; |
| 760 |
|
| 761 |
/* Consume all chunks of deleted file. */ |
| 762 |
for (auto cur_chunk = begin_chunk; cur_chunk <= end_chunk; ++cur_chunk) { |
| 763 |
/* Set current chunk details. */ |
| 764 |
new_task_meta.m_chunk_num = cur_chunk; |
| 765 |
new_task_meta.m_block_num = block_num; |
| 766 |
|
| 767 |
if (m_clone_task_manager.is_chunk_reserved(cur_chunk)) { |
| 768 |
continue; |
| 769 |
} |
| 770 |
|
| 771 |
m_clone_task_manager.set_chunk(task, &new_task_meta); |
| 772 |
|
| 773 |
/* Set data size for progress estimation. */ |
| 774 |
task->m_data_size = data_size; |
| 775 |
} |
| 776 |
|
| 777 |
if (!file_ctx->deleted()) { |
| 778 |
file_ctx->m_state.store(Clone_file_ctx::State::DROPPED); |
| 779 |
} |
| 780 |
|
| 781 |
std::string old_file; |
| 782 |
file_ctx->get_file_name(old_file); |
| 783 |
|
| 784 |
std::string mesg("FILE : "); |
| 785 |
mesg.append(old_file); |
| 786 |
mesg.append(" Space ID: "); |
| 787 |
mesg.append(std::to_string(file_meta->m_space_id)); |
| 788 |
mesg.append(" Chunks : "); |
| 789 |
mesg.append(std::to_string(begin_chunk)); |
| 790 |
mesg.append(" - "); |
| 791 |
mesg.append(std::to_string(end_chunk)); |
| 792 |
|
| 793 |
ib::info(ER_IB_MSG_CLONE_DDL_INVALIDATE) << mesg; |
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
int Clone_Handle::apply_ddl(const Clone_File_Meta *new_meta, |
| 798 |
Clone_file_ctx *file_ctx) { |
| 799 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 800 |
ut_ad(snapshot->get_state() == CLONE_SNAPSHOT_FILE_COPY || |
| 801 |
snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 802 |
|
| 803 |
std::string old_file; |
| 804 |
file_ctx->get_file_name(old_file); |
| 805 |
|
| 806 |
std::string mesg("DELETE FILE : "); |
| 807 |
|
| 808 |
if (new_meta->is_deleted()) { |
| 809 |
/* Check if we have already deleted the file context. This is possible |
| 810 |
in case of a network error and restart where donor could send the delete |
| 811 |
request again. */ |
| 812 |
if (file_ctx->m_state.load() == Clone_file_ctx::State::DROPPED_HANDLED) { |
| 813 |
mesg.append(" IGNORE : "); |
| 814 |
|
| 815 |
} else { |
| 816 |
/* File needs to be deleted. */ |
| 817 |
if (!os_file_delete(innodb_clone_file_key, old_file.c_str())) { |
| 818 |
/* purecov: begin inspected */ |
| 819 |
mesg.append("Innodb Clone Apply Failed to delete file: "); |
| 820 |
mesg.append(old_file); |
| 821 |
my_error(ER_INTERNAL_ERROR, MYF(0), mesg.c_str()); |
| 822 |
return ER_INTERNAL_ERROR; |
| 823 |
/* purecov: end */ |
| 824 |
} |
| 825 |
file_ctx->m_state.store(Clone_file_ctx::State::DROPPED_HANDLED); |
| 826 |
} |
| 827 |
mesg.append(old_file); |
| 828 |
mesg.append(" Space ID: "); |
| 829 |
mesg.append(std::to_string(new_meta->m_space_id)); |
| 830 |
|
| 831 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 832 |
return 0; |
| 833 |
} |
| 834 |
|
| 835 |
auto old_meta = file_ctx->get_file_meta(); |
| 836 |
|
| 837 |
/* Check if file needs to be renamed. */ |
| 838 |
if (!new_meta->is_renamed()) { |
| 839 |
std::string update_mesg; |
| 840 |
/* Set new encryption and compression type. */ |
| 841 |
if (old_meta->m_encryption_metadata.m_type != |
| 842 |
new_meta->m_encryption_metadata.m_type) { |
| 843 |
old_meta->m_encryption_metadata.m_type = |
| 844 |
new_meta->m_encryption_metadata.m_type; |
| 845 |
if (!new_meta->can_encrypt()) { |
| 846 |
update_mesg.assign("UNENCRYPTED "); |
| 847 |
} else { |
| 848 |
update_mesg.assign("ENCRYPTED "); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
if (old_meta->m_compress_type != new_meta->m_compress_type) { |
| 853 |
old_meta->m_compress_type = new_meta->m_compress_type; |
| 854 |
if (new_meta->m_compress_type == Compression::NONE) { |
| 855 |
update_mesg.assign("UNCOMPRESSED "); |
| 856 |
} else { |
| 857 |
update_mesg.assign("COMPRESSED "); |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
auto err = set_compression(file_ctx); |
| 862 |
|
| 863 |
std::string mesg("SET FILE "); |
| 864 |
mesg.append(update_mesg); |
| 865 |
mesg.append(": "); |
| 866 |
mesg.append(old_file); |
| 867 |
mesg.append(" Space ID: "); |
| 868 |
mesg.append(std::to_string(new_meta->m_space_id)); |
| 869 |
|
| 870 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 871 |
return err; |
| 872 |
} |
| 873 |
|
| 874 |
Clone_file_ctx *new_ctx = nullptr; |
| 875 |
|
| 876 |
/* Rename file context. */ |
| 877 |
auto err = snapshot->rename_desc(new_meta, m_clone_dir, new_ctx); |
| 878 |
|
| 879 |
if (err != 0) { |
| 880 |
return err; /* purecov: inspected */ |
| 881 |
} |
| 882 |
|
| 883 |
std::string new_file; |
| 884 |
new_ctx->get_file_name(new_file); |
| 885 |
|
| 886 |
/* Preserve the old file size which could have been extended while applying |
| 887 |
page 0 changes and set it to new descriptor. */ |
| 888 |
auto file_meta = new_ctx->get_file_meta(); |
| 889 |
auto file_size = file_meta->m_file_size; |
| 890 |
|
| 891 |
if (file_size < old_meta->m_file_size) { |
| 892 |
file_size = old_meta->m_file_size; |
| 893 |
} |
| 894 |
file_meta->m_file_size = file_size; |
| 895 |
|
| 896 |
/* Do the actual rename. At this point we rename the files with temp DDL |
| 897 |
extension. After all rename and delete requests are received we rename |
| 898 |
the files again removing the ddl extension. This is required as file rename |
| 899 |
requests are not in the real order and there could be conflicts. */ |
| 900 |
ut_ad(new_ctx->m_extension == Clone_file_ctx::Extension::DDL); |
| 901 |
|
| 902 |
std::string rename_mesg("RENAME FILE WITH EXTN: "); |
| 903 |
|
| 904 |
if (old_file.compare(new_file) == 0) { |
| 905 |
rename_mesg.append(" IGNORE : "); |
| 906 |
|
| 907 |
} else { |
| 908 |
bool success = |
| 909 |
os_file_rename(OS_CLONE_DATA_FILE, old_file.c_str(), new_file.c_str()); |
| 910 |
|
| 911 |
if (!success) { |
| 912 |
/* purecov: begin inspected */ |
| 913 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 914 |
err = ER_ERROR_ON_RENAME; |
| 915 |
|
| 916 |
my_error(ER_ERROR_ON_RENAME, MYF(0), old_file.c_str(), new_file.c_str(), |
| 917 |
errno, my_strerror(errbuf, sizeof(errbuf), errno)); |
| 918 |
/* purecov: end */ |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
rename_mesg.append(old_file); |
| 923 |
rename_mesg.append(" to "); |
| 924 |
rename_mesg.append(new_file); |
| 925 |
rename_mesg.append(" Space ID: "); |
| 926 |
rename_mesg.append(std::to_string(new_meta->m_space_id)); |
| 927 |
|
| 928 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << rename_mesg; |
| 929 |
|
| 930 |
if (err == 0) { |
| 931 |
err = set_compression(new_ctx); |
| 932 |
} |
| 933 |
return err; |
| 934 |
} |
| 935 |
|
| 936 |
int Clone_Handle::fix_all_renamed(const Clone_Task *task) { |
| 937 |
/* Do space check only during file copy and page copy. */ |
| 938 |
auto current_state = m_clone_task_manager.get_state(); |
| 939 |
|
| 940 |
bool fix_needed = current_state == CLONE_SNAPSHOT_FILE_COPY || |
| 941 |
current_state == CLONE_SNAPSHOT_PAGE_COPY; |
| 942 |
|
| 943 |
if (!task->m_is_master || !fix_needed) { |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 948 |
|
| 949 |
ut_ad(snapshot->get_state() == CLONE_SNAPSHOT_FILE_COPY || |
| 950 |
snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 951 |
|
| 952 |
auto fix_func = [&](Clone_file_ctx *file_ctx) { |
| 953 |
/* Need to handle files with DDL extension. */ |
| 954 |
if (file_ctx->deleted() || |
| 955 |
file_ctx->m_extension != Clone_file_ctx::Extension::DDL) { |
| 956 |
return 0; |
| 957 |
} |
| 958 |
/* Save old file name */ |
| 959 |
std::string old_file; |
| 960 |
file_ctx->get_file_name(old_file); |
| 961 |
|
| 962 |
auto err = snapshot->fix_ddl_extension(m_clone_dir, file_ctx); |
| 963 |
if (err != 0) { |
| 964 |
return err; /* purecov: inspected */ |
| 965 |
} |
| 966 |
/* Get new file name. */ |
| 967 |
std::string new_file; |
| 968 |
file_ctx->get_file_name(new_file); |
| 969 |
|
| 970 |
/* Rename file */ |
| 971 |
bool success = |
| 972 |
os_file_rename(OS_CLONE_DATA_FILE, old_file.c_str(), new_file.c_str()); |
| 973 |
if (!success) { |
| 974 |
/* purecov: begin inspected */ |
| 975 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 976 |
err = ER_ERROR_ON_RENAME; |
| 977 |
|
| 978 |
my_error(ER_ERROR_ON_RENAME, MYF(0), old_file.c_str(), new_file.c_str(), |
| 979 |
errno, my_strerror(errbuf, sizeof(errbuf), errno)); |
| 980 |
/* purecov: end */ |
| 981 |
} |
| 982 |
|
| 983 |
std::string mesg("RENAMED FILE REMOVED EXTN : "); |
| 984 |
mesg.append(old_file); |
| 985 |
mesg.append(" to "); |
| 986 |
mesg.append(new_file); |
| 987 |
mesg.append(" Space ID: "); |
| 988 |
auto file_meta = file_ctx->get_file_meta_read(); |
| 989 |
mesg.append(std::to_string(file_meta->m_space_id)); |
| 990 |
|
| 991 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 992 |
return err; |
| 993 |
}; |
| 994 |
|
| 995 |
auto err = snapshot->iterate_data_files(fix_func); |
| 996 |
|
| 997 |
/* Delete ddl list file. */ |
| 998 |
if (err == 0) { |
| 999 |
std::string ddl_list_file; |
| 1000 |
add_directory_path(m_clone_dir, CLONE_INNODB_DDL_FILES, ddl_list_file); |
| 1001 |
|
| 1002 |
clone_remove_list_file(ddl_list_file.c_str()); |
| 1003 |
} |
| 1004 |
|
| 1005 |
return err; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* Check and set punch hole for compressed page table. */ |
| 1009 |
int Clone_Handle::set_compression(Clone_file_ctx *file_ctx) { |
| 1010 |
auto file_meta = file_ctx->get_file_meta(); |
| 1011 |
|
| 1012 |
if (file_meta->m_compress_type == Compression::NONE || file_ctx->deleted()) { |
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* Disable punch hole if donor compression is not effective. */ |
| 1017 |
page_size_t page_size(file_meta->m_fsp_flags); |
| 1018 |
|
| 1019 |
if (page_size.is_compressed() || |
| 1020 |
file_meta->m_fsblk_size * 2 > srv_page_size) { |
| 1021 |
/* purecov: begin inspected */ |
| 1022 |
file_meta->m_punch_hole = false; |
| 1023 |
return 0; |
| 1024 |
/* purecov: end */ |
| 1025 |
} |
| 1026 |
|
| 1027 |
os_file_stat_t stat_info; |
| 1028 |
std::string file_name; |
| 1029 |
file_ctx->get_file_name(file_name); |
| 1030 |
|
| 1031 |
os_file_get_status(file_name.c_str(), &stat_info, false, false); |
| 1032 |
|
| 1033 |
/* Check and disable punch hole if recipient cannot support it. */ |
| 1034 |
if (!IORequest::is_punch_hole_supported() || |
| 1035 |
stat_info.block_size * 2 > srv_page_size) { |
| 1036 |
file_meta->m_punch_hole = false; /* purecov: inspected */ |
| 1037 |
} else { |
| 1038 |
file_meta->m_punch_hole = true; |
| 1039 |
} |
| 1040 |
|
| 1041 |
/* Old format for compressed and encrypted page is |
| 1042 |
dependent on file system block size. */ |
| 1043 |
if (file_meta->can_encrypt() && |
| 1044 |
file_meta->m_fsblk_size != stat_info.block_size) { |
| 1045 |
/* purecov: begin tested */ |
| 1046 |
auto donor_str = std::to_string(file_meta->m_fsblk_size); |
| 1047 |
auto recipient_str = std::to_string(stat_info.block_size); |
| 1048 |
|
| 1049 |
my_error(ER_CLONE_CONFIG, MYF(0), "FS Block Size", donor_str.c_str(), |
| 1050 |
recipient_str.c_str()); |
| 1051 |
return ER_CLONE_CONFIG; |
| 1052 |
/* purecov: end */ |
| 1053 |
} |
| 1054 |
|
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
int Clone_Handle::file_create_init(const Clone_file_ctx *file_ctx, |
| 1059 |
ulint file_type, bool init) { |
| 1060 |
/* Create the file and path. */ |
| 1061 |
File_init_cbk init_cbk = [&](pfs_os_file_t file) { |
| 1062 |
if (!init) { |
| 1063 |
return DB_SUCCESS; |
| 1064 |
} |
| 1065 |
|
| 1066 |
bool atomic = false; |
| 1067 |
bool punch_hole = false; |
| 1068 |
|
| 1069 |
std::string file_name; |
| 1070 |
file_ctx->get_file_name(file_name); |
| 1071 |
|
| 1072 |
const auto file_meta = file_ctx->get_file_meta_read(); |
| 1073 |
auto flags = file_meta->m_fsp_flags; |
| 1074 |
|
| 1075 |
bool is_undo_file = fsp_is_undo_tablespace(file_meta->m_space_id); |
| 1076 |
|
| 1077 |
page_no_t size_in_pages = |
| 1078 |
is_undo_file ? UNDO_INITIAL_SIZE_IN_PAGES : FIL_IBD_FILE_INITIAL_SIZE; |
| 1079 |
|
| 1080 |
byte encryption_info[Encryption::INFO_SIZE]; |
| 1081 |
byte *encryption_ptr = nullptr; |
| 1082 |
|
| 1083 |
dberr_t db_err = DB_SUCCESS; |
| 1084 |
std::string mesg("CREATE NEW FILE : "); |
| 1085 |
|
| 1086 |
if (file_meta->m_transfer_encryption_key) { |
| 1087 |
encryption_ptr = &encryption_info[0]; |
| 1088 |
mesg.append(" WRITE KEY: "); |
| 1089 |
|
| 1090 |
bool success = Encryption::fill_encryption_info( |
| 1091 |
file_meta->m_encryption_metadata, true, encryption_info); |
| 1092 |
|
| 1093 |
if (!success) { |
| 1094 |
db_err = DB_ERROR; /* purecov: inspected */ |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (db_err == DB_SUCCESS) { |
| 1099 |
db_err = fil_write_initial_pages( |
| 1100 |
file, file_name.c_str(), FIL_TYPE_TABLESPACE, size_in_pages, |
| 1101 |
encryption_ptr, file_meta->m_space_id, flags, atomic, punch_hole); |
| 1102 |
} |
| 1103 |
|
| 1104 |
mesg.append(file_name); |
| 1105 |
mesg.append(" Space ID: "); |
| 1106 |
mesg.append(std::to_string(file_meta->m_space_id)); |
| 1107 |
|
| 1108 |
if (db_err != DB_SUCCESS) { |
| 1109 |
mesg.append(" FAILED"); /* purecov: inspected */ |
| 1110 |
} |
| 1111 |
|
| 1112 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 1113 |
|
| 1114 |
return db_err; |
| 1115 |
}; |
| 1116 |
|
| 1117 |
auto err = open_file(nullptr, file_ctx, file_type, true, init_cbk); |
| 1118 |
|
| 1119 |
return err; |
| 1120 |
} |
| 1121 |
|
| 1122 |
int Clone_Handle::apply_file_metadata(Clone_Task *task, |
| 1123 |
Ha_clone_cbk *callback) { |
| 1124 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 1125 |
|
| 1126 |
uint desc_len = 0; |
| 1127 |
auto serial_desc = callback->get_data_desc(&desc_len); |
| 1128 |
|
| 1129 |
Clone_Desc_File_MetaData file_desc; |
| 1130 |
auto success = file_desc.deserialize(serial_desc, desc_len); |
| 1131 |
|
| 1132 |
if (!success) { |
| 1133 |
int err = ER_CLONE_PROTOCOL; |
| 1134 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid File Descriptor"); |
| 1135 |
ut_d(ut_error); |
| 1136 |
ut_o(return (err)); |
| 1137 |
} |
| 1138 |
const auto file_desc_meta = &file_desc.m_file_meta; |
| 1139 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1140 |
|
| 1141 |
/* At end of current state DDL file alterations are communicated. */ |
| 1142 |
bool ddl_desc = (file_desc.m_state == snapshot->get_next_state()); |
| 1143 |
|
| 1144 |
ut_ad(ddl_desc || snapshot->get_state() == file_desc.m_state); |
| 1145 |
|
| 1146 |
bool file_deleted = file_desc_meta->is_deleted(); |
| 1147 |
|
| 1148 |
bool desc_exists = false; |
| 1149 |
Clone_file_ctx *file_ctx = nullptr; |
| 1150 |
|
| 1151 |
/* Check file metadata entry based on the descriptor. */ |
| 1152 |
auto err = snapshot->get_file_from_desc(file_desc_meta, m_clone_dir, false, |
| 1153 |
desc_exists, file_ctx); |
| 1154 |
if (err != 0) { |
| 1155 |
return (err); |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (desc_exists) { |
| 1159 |
if (ddl_desc) { |
| 1160 |
err = apply_ddl(file_desc_meta, file_ctx); |
| 1161 |
|
| 1162 |
} else if (file_deleted) { |
| 1163 |
/* File delete notification sent immediately for chunk adjustment. */ |
| 1164 |
err = apply_file_delete(task, file_ctx, file_desc_meta); |
| 1165 |
} |
| 1166 |
return err; |
| 1167 |
} |
| 1168 |
|
| 1169 |
mutex_enter(m_clone_task_manager.get_mutex()); |
| 1170 |
|
| 1171 |
/* Create file metadata entry based on the descriptor. */ |
| 1172 |
err = snapshot->get_file_from_desc(file_desc_meta, m_clone_dir, true, |
| 1173 |
desc_exists, file_ctx); |
| 1174 |
if (err != 0 || desc_exists) { |
| 1175 |
mutex_exit(m_clone_task_manager.get_mutex()); |
| 1176 |
|
| 1177 |
/* Save error with file name. */ |
| 1178 |
if (err != 0) { |
| 1179 |
m_clone_task_manager.set_error(err, file_desc_meta->m_file_name); |
| 1180 |
} |
| 1181 |
return (err); |
| 1182 |
} |
| 1183 |
|
| 1184 |
auto file_meta = file_ctx->get_file_meta(); |
| 1185 |
file_meta->m_punch_hole = false; |
| 1186 |
|
| 1187 |
bool is_file_copy = snapshot->get_state() == CLONE_SNAPSHOT_FILE_COPY; |
| 1188 |
bool is_page_copy = snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY; |
| 1189 |
|
| 1190 |
if (is_file_copy || is_page_copy) { |
| 1191 |
ut_ad(is_file_copy || ddl_desc); |
| 1192 |
|
| 1193 |
auto file_type = OS_CLONE_DATA_FILE; |
| 1194 |
|
| 1195 |
if (file_meta->m_space_id == dict_sys_t::s_invalid_space_id) { |
| 1196 |
file_type = OS_CLONE_LOG_FILE; |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (file_deleted) { |
| 1200 |
/* Mark the newly created descriptor deleted. */ |
| 1201 |
file_ctx->m_state.store(Clone_file_ctx::State::DROPPED_HANDLED); |
| 1202 |
|
| 1203 |
std::string file_name; |
| 1204 |
file_ctx->get_file_name(file_name); |
| 1205 |
|
| 1206 |
std::string mesg("ADD DELETED FILE : "); |
| 1207 |
mesg.append(file_name); |
| 1208 |
mesg.append(" Space ID: "); |
| 1209 |
mesg.append(std::to_string(file_meta->m_space_id)); |
| 1210 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 1211 |
|
| 1212 |
} else { |
| 1213 |
/* Create the file and write initial pages if created by DDL. */ |
| 1214 |
err = file_create_init(file_ctx, file_type, ddl_desc); |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* If last file is received, set all file metadata transferred */ |
| 1218 |
if (snapshot->add_file_from_desc(file_ctx, ddl_desc)) { |
| 1219 |
m_clone_task_manager.set_file_meta_transferred(); |
| 1220 |
} |
| 1221 |
|
| 1222 |
mutex_exit(m_clone_task_manager.get_mutex()); |
| 1223 |
|
| 1224 |
if (err == 0 && file_type == OS_CLONE_DATA_FILE) { |
| 1225 |
err = set_compression(file_ctx); |
| 1226 |
} |
| 1227 |
return err; |
| 1228 |
} |
| 1229 |
|
| 1230 |
ut_ad(snapshot->get_state() == CLONE_SNAPSHOT_REDO_COPY); |
| 1231 |
ut_ad(file_desc.m_state == CLONE_SNAPSHOT_REDO_COPY); |
| 1232 |
ut_ad(!ddl_desc); |
| 1233 |
|
| 1234 |
/* open and reserve the redo file size */ |
| 1235 |
File_init_cbk empty_cbk; |
| 1236 |
|
| 1237 |
err = open_file(nullptr, file_ctx, OS_CLONE_LOG_FILE, true, empty_cbk); |
| 1238 |
|
| 1239 |
snapshot->add_file_from_desc(file_ctx, false); |
| 1240 |
|
| 1241 |
mutex_exit(m_clone_task_manager.get_mutex()); |
| 1242 |
return (err); |
| 1243 |
} |
| 1244 |
|
| 1245 |
bool Clone_Handle::read_compressed_len(unsigned char *buffer, uint32_t len, |
| 1246 |
uint32_t block_size, |
| 1247 |
uint32_t &compressed_len) { |
| 1248 |
ut_a(len >= 2); |
| 1249 |
|
| 1250 |
/* Validate compressed page type */ |
| 1251 |
auto page_type = mach_read_from_2(buffer + FIL_PAGE_TYPE); |
| 1252 |
|
| 1253 |
if (page_type == FIL_PAGE_COMPRESSED || |
| 1254 |
page_type == FIL_PAGE_COMPRESSED_AND_ENCRYPTED) { |
| 1255 |
compressed_len = mach_read_from_2(buffer + FIL_PAGE_COMPRESS_SIZE_V1); |
| 1256 |
compressed_len += FIL_PAGE_DATA; |
| 1257 |
|
| 1258 |
/* Align compressed length */ |
| 1259 |
compressed_len = ut_calc_align(compressed_len, block_size); |
| 1260 |
return true; |
| 1261 |
} |
| 1262 |
|
| 1263 |
return false; |
| 1264 |
} |
| 1265 |
|
| 1266 |
int Clone_Handle::sparse_file_write(Clone_File_Meta *file_meta, |
| 1267 |
unsigned char *buffer, uint32_t len, |
| 1268 |
pfs_os_file_t file, uint64_t start_off) { |
| 1269 |
dberr_t err = DB_SUCCESS; |
| 1270 |
page_size_t page_size(file_meta->m_fsp_flags); |
| 1271 |
auto page_len = page_size.physical(); |
| 1272 |
|
| 1273 |
IORequest request(IORequest::WRITE); |
| 1274 |
request.disable_compression(); |
| 1275 |
request.clear_encrypted(); |
| 1276 |
|
| 1277 |
/* Loop through all pages in current data block */ |
| 1278 |
while (len >= page_len) { |
| 1279 |
uint32_t comp_len; |
| 1280 |
bool is_compressed = read_compressed_len( |
| 1281 |
buffer, len, static_cast<uint32_t>(file_meta->m_fsblk_size), comp_len); |
| 1282 |
|
| 1283 |
auto write_len = is_compressed ? comp_len : page_len; |
| 1284 |
|
| 1285 |
/* Punch hole if needed */ |
| 1286 |
bool first_page = (start_off == 0); |
| 1287 |
|
| 1288 |
/* In rare case during file copy the page could be a torn page |
| 1289 |
and the size may not be correct. In such case the page is going to |
| 1290 |
be replaced later during page copy.*/ |
| 1291 |
if (first_page || write_len > page_len) { |
| 1292 |
write_len = page_len; |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* Write Data Page */ |
| 1296 |
errno = 0; |
| 1297 |
err = os_file_write(request, "Clone data file", file, |
| 1298 |
reinterpret_cast<char *>(buffer), start_off, |
| 1299 |
(start_off == 0) ? page_len : write_len); |
| 1300 |
if (err != DB_SUCCESS) { |
| 1301 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1302 |
my_error(ER_ERROR_ON_WRITE, MYF(0), file_meta->m_file_name, errno, |
| 1303 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1304 |
|
| 1305 |
return (ER_ERROR_ON_WRITE); |
| 1306 |
} |
| 1307 |
|
| 1308 |
os_offset_t offset = start_off + write_len; |
| 1309 |
os_offset_t hole_size = page_len - write_len; |
| 1310 |
|
| 1311 |
if (file_meta->m_punch_hole && hole_size > 0) { |
| 1312 |
err = os_file_punch_hole(file.m_file, offset, hole_size); |
| 1313 |
if (err != DB_SUCCESS) { |
| 1314 |
/* Disable for whole file */ |
| 1315 |
file_meta->m_punch_hole = false; |
| 1316 |
ut_ad(err == DB_IO_NO_PUNCH_HOLE); |
| 1317 |
ib::info(ER_IB_CLONE_PUNCH_HOLE) |
| 1318 |
<< "Innodb Clone Apply failed to punch hole: " |
| 1319 |
<< file_meta->m_file_name; |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
start_off += page_len; |
| 1324 |
buffer += page_len; |
| 1325 |
len -= page_len; |
| 1326 |
} |
| 1327 |
|
| 1328 |
/* Must have consumed all data. */ |
| 1329 |
ut_ad(err != DB_SUCCESS || len == 0); |
| 1330 |
return 0; |
| 1331 |
} |
| 1332 |
|
| 1333 |
int Clone_Handle::modify_and_write(const Clone_Task *task, uint64_t offset, |
| 1334 |
unsigned char *buffer, uint32_t buf_len) { |
| 1335 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 1336 |
|
| 1337 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1338 |
auto file_meta = snapshot->get_file_by_index(task->m_current_file_index); |
| 1339 |
|
| 1340 |
if (file_meta->can_encrypt()) { |
| 1341 |
bool success = true; |
| 1342 |
|
| 1343 |
bool is_page_copy = (snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 1344 |
bool key_page = (is_page_copy && offset == 0); |
| 1345 |
|
| 1346 |
bool is_log_file = (snapshot->get_state() == CLONE_SNAPSHOT_REDO_COPY); |
| 1347 |
bool key_log = (is_log_file && file_meta->m_file_index == 0 && offset == 0); |
| 1348 |
|
| 1349 |
if (key_page) { |
| 1350 |
/* Encrypt tablespace key with master key for encrypted tablespace. */ |
| 1351 |
page_size_t page_size(file_meta->m_fsp_flags); |
| 1352 |
success = snapshot->encrypt_key_in_header(page_size, buffer); |
| 1353 |
|
| 1354 |
} else if (key_log) { |
| 1355 |
/* Encrypt redo log key with master key */ |
| 1356 |
success = snapshot->encrypt_key_in_log_header(buffer, buf_len); |
| 1357 |
} |
| 1358 |
if (!success) { |
| 1359 |
int err = ER_INTERNAL_ERROR; |
| 1360 |
my_error(err, MYF(0), "Innodb Clone Apply Failed to Encrypt Key"); |
| 1361 |
ut_d(ut_error); |
| 1362 |
ut_o(return (err)); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
if (file_meta->m_punch_hole) { |
| 1367 |
auto err = sparse_file_write(file_meta, buffer, buf_len, |
| 1368 |
task->m_current_file_des, offset); |
| 1369 |
return err; |
| 1370 |
} |
| 1371 |
|
| 1372 |
/* No more compression/encryption is needed. */ |
| 1373 |
IORequest request(IORequest::WRITE); |
| 1374 |
request.disable_compression(); |
| 1375 |
request.clear_encrypted(); |
| 1376 |
|
| 1377 |
/* For redo/undo log files and uncompressed tables ,directly write to file */ |
| 1378 |
errno = 0; |
| 1379 |
auto db_err = |
| 1380 |
os_file_write(request, "Clone data file", task->m_current_file_des, |
| 1381 |
reinterpret_cast<char *>(buffer), offset, buf_len); |
| 1382 |
if (db_err != DB_SUCCESS) { |
| 1383 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1384 |
my_error(ER_ERROR_ON_WRITE, MYF(0), file_meta->m_file_name, errno, |
| 1385 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1386 |
|
| 1387 |
return (ER_ERROR_ON_WRITE); |
| 1388 |
} |
| 1389 |
return 0; |
| 1390 |
} |
| 1391 |
|
| 1392 |
int Clone_Handle::receive_data(Clone_Task *task, uint64_t offset, |
| 1393 |
uint64_t file_size, uint32_t size, |
| 1394 |
Ha_clone_cbk *callback) { |
| 1395 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 1396 |
|
| 1397 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 1398 |
|
| 1399 |
auto file_ctx = snapshot->get_file_ctx_by_index(task->m_current_file_index); |
| 1400 |
auto file_meta = file_ctx->get_file_meta(); |
| 1401 |
|
| 1402 |
std::string file_name; |
| 1403 |
file_ctx->get_file_name(file_name); |
| 1404 |
|
| 1405 |
/* If the file is deleted, then fetch the data and ignore. */ |
| 1406 |
if (file_ctx->deleted()) { |
| 1407 |
unsigned char *data_buf = nullptr; |
| 1408 |
uint32_t data_len = 0; |
| 1409 |
callback->apply_buffer_cbk(data_buf, data_len); |
| 1410 |
|
| 1411 |
std::string mesg("IGNORE DATA for DELETED FILE: "); |
| 1412 |
mesg.append(file_name); |
| 1413 |
mesg.append(" Space ID: "); |
| 1414 |
mesg.append(std::to_string(file_meta->m_space_id)); |
| 1415 |
|
| 1416 |
ib::info(ER_IB_MSG_CLONE_DDL_APPLY) << mesg; |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
|
| 1420 |
bool is_page_copy = (snapshot->get_state() == CLONE_SNAPSHOT_PAGE_COPY); |
| 1421 |
bool is_log_file = (snapshot->get_state() == CLONE_SNAPSHOT_REDO_COPY); |
| 1422 |
|
| 1423 |
/* During page and redo copy, we encrypt the key in header page. */ |
| 1424 |
bool key_page = (is_page_copy && offset == 0); |
| 1425 |
bool key_log = (is_log_file && file_meta->m_file_index == 0 && offset == 0); |
| 1426 |
|
| 1427 |
if (key_page) { |
| 1428 |
/* Check and update file size for space header page */ |
| 1429 |
if (file_meta->m_file_size < file_size) { |
| 1430 |
snapshot->update_file_size(task->m_current_file_index, file_size); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
auto file_type = OS_CLONE_DATA_FILE; |
| 1435 |
|
| 1436 |
if (is_log_file || is_page_copy || |
| 1437 |
file_meta->m_space_id == dict_sys_t::s_invalid_space_id || |
| 1438 |
file_meta->m_punch_hole) { |
| 1439 |
file_type = OS_CLONE_LOG_FILE; |
| 1440 |
} |
| 1441 |
|
| 1442 |
/* Open destination file for first block. */ |
| 1443 |
if (task->m_current_file_des.m_file == OS_FILE_CLOSED) { |
| 1444 |
ut_ad(file_meta != nullptr); |
| 1445 |
|
| 1446 |
File_init_cbk empty_cbk; |
| 1447 |
auto err = open_file(task, file_ctx, file_type, true, empty_cbk); |
| 1448 |
|
| 1449 |
if (err != 0) { |
| 1450 |
/* Save error with file name. */ |
| 1451 |
/* purecov: begin inspected */ |
| 1452 |
m_clone_task_manager.set_error(err, file_name.c_str()); |
| 1453 |
return (err); |
| 1454 |
/* purecov: end */ |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
ut_ad(task->m_current_file_index == file_meta->m_file_index); |
| 1459 |
|
| 1460 |
/* Copy data to current destination file using callback. */ |
| 1461 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1462 |
|
| 1463 |
auto file_hdl = task->m_current_file_des.m_file; |
| 1464 |
auto success = os_file_seek(nullptr, file_hdl, offset); |
| 1465 |
|
| 1466 |
if (!success) { |
| 1467 |
/* purecov: begin inspected */ |
| 1468 |
my_error(ER_ERROR_ON_READ, MYF(0), file_name.c_str(), errno, |
| 1469 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1470 |
/* Save error with file name. */ |
| 1471 |
m_clone_task_manager.set_error(ER_ERROR_ON_READ, file_name.c_str()); |
| 1472 |
return (ER_ERROR_ON_READ); |
| 1473 |
/* purecov: end */ |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (task->m_file_cache) { |
| 1477 |
callback->set_os_buffer_cache(); |
| 1478 |
/* For data file recommend zero copy for cached IO. */ |
| 1479 |
if (!is_log_file) { |
| 1480 |
callback->set_zero_copy(); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
callback->set_dest_name(file_meta->m_file_name); |
| 1485 |
|
| 1486 |
bool modify_buffer = false; |
| 1487 |
|
| 1488 |
/* In case of page compression we need to punch hole. */ |
| 1489 |
if (file_meta->m_punch_hole) { |
| 1490 |
ut_ad(!is_log_file); |
| 1491 |
modify_buffer = true; |
| 1492 |
} |
| 1493 |
|
| 1494 |
/* We need to encrypt the tablespace key by master key. */ |
| 1495 |
if (file_meta->can_encrypt() && (key_page || key_log)) { |
| 1496 |
modify_buffer = true; |
| 1497 |
} |
| 1498 |
auto err = file_callback(callback, task, size, modify_buffer, offset |
| 1499 |
#ifdef UNIV_PFS_IO |
| 1500 |
, |
| 1501 |
UT_LOCATION_HERE |
| 1502 |
#endif /* UNIV_PFS_IO */ |
| 1503 |
); |
| 1504 |
|
| 1505 |
task->m_data_size += size; |
| 1506 |
|
| 1507 |
if (err != 0) { |
| 1508 |
/* Save error with file name. */ |
| 1509 |
/* purecov: begin inspected */ |
| 1510 |
m_clone_task_manager.set_error(err, file_name.c_str()); |
| 1511 |
/* purecov: end */ |
| 1512 |
} |
| 1513 |
return (err); |
| 1514 |
} |
| 1515 |
|
| 1516 |
int Clone_Handle::apply_data(Clone_Task *task, Ha_clone_cbk *callback) { |
| 1517 |
ut_ad(m_clone_handle_type == CLONE_HDL_APPLY); |
| 1518 |
|
| 1519 |
/* Extract the data descriptor. */ |
| 1520 |
uint desc_len = 0; |
| 1521 |
auto serial_desc = callback->get_data_desc(&desc_len); |
| 1522 |
|
| 1523 |
Clone_Desc_Data data_desc; |
| 1524 |
auto success = data_desc.deserialize(serial_desc, desc_len); |
| 1525 |
|
| 1526 |
if (!success) { |
| 1527 |
int err = ER_CLONE_PROTOCOL; |
| 1528 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Data Descriptor"); |
| 1529 |
ut_d(ut_error); |
| 1530 |
ut_o(return (err)); |
| 1531 |
} |
| 1532 |
/* Identify the task for the current block of data. */ |
| 1533 |
int err = 0; |
| 1534 |
auto task_meta = &data_desc.m_task_meta; |
| 1535 |
|
| 1536 |
/* The data is from a different file. Close the current one. */ |
| 1537 |
if (task->m_current_file_index != data_desc.m_file_index) { |
| 1538 |
err = close_file(task); |
| 1539 |
if (err != 0) { |
| 1540 |
return (err); |
| 1541 |
} |
| 1542 |
task->m_current_file_index = data_desc.m_file_index; |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* Receive data from callback and apply. */ |
| 1546 |
err = receive_data(task, data_desc.m_file_offset, data_desc.m_file_size, |
| 1547 |
data_desc.m_data_len, callback); |
| 1548 |
|
| 1549 |
/* Close file in case of error. */ |
| 1550 |
if (err != 0) { |
| 1551 |
close_file(task); |
| 1552 |
} else { |
| 1553 |
err = m_clone_task_manager.set_chunk(task, task_meta); |
| 1554 |
} |
| 1555 |
|
| 1556 |
return (err); |
| 1557 |
} |
| 1558 |
|
| 1559 |
int Clone_Handle::apply(THD *, uint task_id, Ha_clone_cbk *callback) { |
| 1560 |
int err = 0; |
| 1561 |
uint desc_len = 0; |
| 1562 |
|
| 1563 |
auto clone_desc = callback->get_data_desc(&desc_len); |
| 1564 |
ut_ad(clone_desc != nullptr); |
| 1565 |
|
| 1566 |
Clone_Desc_Header header; |
| 1567 |
auto success = header.deserialize(clone_desc, desc_len); |
| 1568 |
|
| 1569 |
if (!success) { |
| 1570 |
err = ER_CLONE_PROTOCOL; |
| 1571 |
my_error(err, MYF(0), "Wrong Clone RPC: Invalid Descriptor Header"); |
| 1572 |
ut_d(ut_error); |
| 1573 |
ut_o(return (err)); |
| 1574 |
} |
| 1575 |
|
| 1576 |
/* Check the descriptor type in header and apply */ |
| 1577 |
auto task = m_clone_task_manager.get_task_by_index(task_id); |
| 1578 |
|
| 1579 |
switch (header.m_type) { |
| 1580 |
case CLONE_DESC_TASK_METADATA: |
| 1581 |
err = apply_task_metadata(task, callback); |
| 1582 |
break; |
| 1583 |
|
| 1584 |
case CLONE_DESC_STATE: |
| 1585 |
err = apply_state_metadata(task, callback); |
| 1586 |
break; |
| 1587 |
|
| 1588 |
case CLONE_DESC_FILE_METADATA: |
| 1589 |
err = apply_file_metadata(task, callback); |
| 1590 |
break; |
| 1591 |
|
| 1592 |
case CLONE_DESC_DATA: |
| 1593 |
err = apply_data(task, callback); |
| 1594 |
break; |
| 1595 |
|
| 1596 |
default: |
| 1597 |
ut_d(ut_error); |
| 1598 |
ut_o(break); |
| 1599 |
} |
| 1600 |
|
| 1601 |
if (err != 0) { |
| 1602 |
close_file(task); |
| 1603 |
} |
| 1604 |
|
| 1605 |
return (err); |
| 1606 |
} |
| 1607 |
|
| 1608 |
int Clone_Handle::restart_apply(THD *, const byte *&loc, uint &loc_len) { |
| 1609 |
auto init_loc = m_restart_loc; |
| 1610 |
auto init_len = m_restart_loc_len; |
| 1611 |
auto alloc_len = m_restart_loc_len; |
| 1612 |
|
| 1613 |
/* Get latest locator */ |
| 1614 |
loc = get_locator(loc_len); |
| 1615 |
|
| 1616 |
m_clone_task_manager.reinit_apply_state(loc, loc_len, init_loc, init_len, |
| 1617 |
alloc_len); |
| 1618 |
|
| 1619 |
/* Return the original locator if no state information */ |
| 1620 |
if (init_loc == nullptr) { |
| 1621 |
return (0); |
| 1622 |
} |
| 1623 |
|
| 1624 |
loc = init_loc; |
| 1625 |
loc_len = init_len; |
| 1626 |
|
| 1627 |
/* Reset restart loc buffer if newly allocated */ |
| 1628 |
if (alloc_len > m_restart_loc_len) { |
| 1629 |
m_restart_loc = init_loc; |
| 1630 |
m_restart_loc_len = alloc_len; |
| 1631 |
} |
| 1632 |
|
| 1633 |
ut_ad(loc == m_restart_loc); |
| 1634 |
|
| 1635 |
auto master_task = m_clone_task_manager.get_task_by_index(0); |
| 1636 |
|
| 1637 |
auto err = close_file(master_task); |
| 1638 |
|
| 1639 |
return (err); |
| 1640 |
} |
| 1641 |
|
| 1642 |
void Clone_Snapshot::update_file_size(uint32_t file_index, uint64_t file_size) { |
| 1643 |
/* Update file size when file is extended during page copy */ |
| 1644 |
ut_ad(m_snapshot_state == CLONE_SNAPSHOT_PAGE_COPY); |
| 1645 |
|
| 1646 |
auto cur_file = get_file_by_index(file_index); |
| 1647 |
|
| 1648 |
while (file_size > cur_file->m_file_size) { |
| 1649 |
++file_index; |
| 1650 |
|
| 1651 |
if (file_index >= num_data_files()) { |
| 1652 |
/* Update file size for the last file. */ |
| 1653 |
cur_file->m_file_size = file_size; |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
|
| 1657 |
auto next_file = get_file_by_index(file_index); |
| 1658 |
|
| 1659 |
if (next_file->m_space_id != cur_file->m_space_id) { |
| 1660 |
/* Update file size for the last file. */ |
| 1661 |
cur_file->m_file_size = file_size; |
| 1662 |
break; |
| 1663 |
} |
| 1664 |
|
| 1665 |
/* Only system tablespace can have multiple nodes. */ |
| 1666 |
ut_ad(cur_file->m_space_id == 0); |
| 1667 |
|
| 1668 |
file_size -= cur_file->m_file_size; |
| 1669 |
cur_file = next_file; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
int Clone_Snapshot::init_apply_state(Clone_Desc_State *state_desc) { |
| 1674 |
IB_mutex_guard guard(&m_snapshot_mutex, UT_LOCATION_HERE); |
| 1675 |
|
| 1676 |
set_state_info(state_desc); |
| 1677 |
int err = 0; |
| 1678 |
|
| 1679 |
switch (m_snapshot_state) { |
| 1680 |
case CLONE_SNAPSHOT_FILE_COPY: |
| 1681 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State FILE COPY: "; |
| 1682 |
break; |
| 1683 |
|
| 1684 |
case CLONE_SNAPSHOT_PAGE_COPY: |
| 1685 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State PAGE COPY: "; |
| 1686 |
break; |
| 1687 |
|
| 1688 |
case CLONE_SNAPSHOT_REDO_COPY: |
| 1689 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State REDO COPY: "; |
| 1690 |
break; |
| 1691 |
|
| 1692 |
case CLONE_SNAPSHOT_DONE: |
| 1693 |
/* Extend and flush data files. */ |
| 1694 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State FLUSH DATA: "; |
| 1695 |
err = extend_and_flush_files(false); |
| 1696 |
if (err != 0) { |
| 1697 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1698 |
<< "Clone Apply FLUSH DATA failed code: " << err; |
| 1699 |
break; |
| 1700 |
} |
| 1701 |
/* Flush redo files. */ |
| 1702 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State FLUSH REDO: "; |
| 1703 |
err = extend_and_flush_files(true); |
| 1704 |
if (err != 0) { |
| 1705 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1706 |
<< "Clone Apply FLUSH REDO failed code: " << err; |
| 1707 |
break; |
| 1708 |
} |
| 1709 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply State DONE"; |
| 1710 |
break; |
| 1711 |
|
| 1712 |
case CLONE_SNAPSHOT_NONE: |
| 1713 |
case CLONE_SNAPSHOT_INIT: |
| 1714 |
default: |
| 1715 |
err = ER_INTERNAL_ERROR; |
| 1716 |
my_error(err, MYF(0), "Innodb Clone Snapshot Invalid state"); |
| 1717 |
ut_d(ut_error); |
| 1718 |
ut_o(break); |
| 1719 |
} |
| 1720 |
return (err); |
| 1721 |
} |
| 1722 |
|
| 1723 |
int Clone_Snapshot::extend_and_flush_files(bool flush_redo) { |
| 1724 |
auto &file_vector = (flush_redo) ? m_redo_file_vector : m_data_file_vector; |
| 1725 |
|
| 1726 |
for (auto file_ctx : file_vector) { |
| 1727 |
if (file_ctx->deleted()) { |
| 1728 |
ut_ad(file_ctx->m_state.load() == Clone_file_ctx::State::DROPPED_HANDLED); |
| 1729 |
continue; |
| 1730 |
} |
| 1731 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1732 |
bool success = true; |
| 1733 |
auto file_meta = file_ctx->get_file_meta(); |
| 1734 |
|
| 1735 |
std::string file_name; |
| 1736 |
file_ctx->get_file_name(file_name); |
| 1737 |
|
| 1738 |
auto file = os_file_create( |
| 1739 |
innodb_clone_file_key, file_name.c_str(), |
| 1740 |
OS_FILE_OPEN | OS_FILE_ON_ERROR_NO_EXIT, |
| 1741 |
flush_redo ? OS_CLONE_LOG_FILE : OS_CLONE_DATA_FILE, false, &success); |
| 1742 |
|
| 1743 |
if (!success) { |
| 1744 |
/* purecov: begin inspected */ |
| 1745 |
my_error(ER_CANT_OPEN_FILE, MYF(0), file_name.c_str(), errno, |
| 1746 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1747 |
|
| 1748 |
return (ER_CANT_OPEN_FILE); |
| 1749 |
/* purecov: end */ |
| 1750 |
} |
| 1751 |
|
| 1752 |
auto file_size = os_file_get_size(file); |
| 1753 |
|
| 1754 |
size_t aligned_size = 0; |
| 1755 |
/* If file size is not aligned to extent size, recovery handling has |
| 1756 |
some issues. This work around eliminates dependency with that. */ |
| 1757 |
if (file_meta->m_fsp_flags != UINT32_UNDEFINED) { |
| 1758 |
page_size_t page_size(file_meta->m_fsp_flags); |
| 1759 |
auto extent_size = page_size.physical() * FSP_EXTENT_SIZE; |
| 1760 |
/* Skip extending files smaller than one extent. */ |
| 1761 |
if (file_size > extent_size) { |
| 1762 |
aligned_size = ut_uint64_align_up(file_size, extent_size); |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
if (file_size < file_meta->m_file_size) { |
| 1767 |
success = os_file_set_size(file_name.c_str(), file, file_size, |
| 1768 |
file_meta->m_file_size, true); |
| 1769 |
} else if (file_size < aligned_size) { |
| 1770 |
success = os_file_set_size(file_name.c_str(), file, file_size, |
| 1771 |
aligned_size, true); |
| 1772 |
} else { |
| 1773 |
success = os_file_flush(file); |
| 1774 |
} |
| 1775 |
|
| 1776 |
os_file_close(file); |
| 1777 |
|
| 1778 |
if (!success) { |
| 1779 |
/* purecov: begin inspected */ |
| 1780 |
my_error(ER_ERROR_ON_WRITE, MYF(0), file_name.c_str(), errno, |
| 1781 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1782 |
|
| 1783 |
return (ER_ERROR_ON_WRITE); |
| 1784 |
/* purecov: end */ |
| 1785 |
} |
| 1786 |
} |
| 1787 |
return (0); |
| 1788 |
} |