| 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/clone0clone.cc |
| 29 |
Innodb Clone System |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#include "clone0clone.h" |
| 34 |
#include <string> |
| 35 |
#ifdef UNIV_DEBUG |
| 36 |
#include "current_thd.h" /* current_thd */ |
| 37 |
#include "debug_sync.h" /* DBUG_SIGNAL_WAIT_FOR */ |
| 38 |
#endif /* UNIV_DEBUG */ |
| 39 |
|
| 40 |
/** Global Clone System */ |
| 41 |
Clone_Sys *clone_sys = nullptr; |
| 42 |
|
| 43 |
/** Clone System state */ |
| 44 |
Clone_Sys_State Clone_Sys::s_clone_sys_state = {CLONE_SYS_INACTIVE}; |
| 45 |
|
| 46 |
/** Number of active abort requests */ |
| 47 |
uint Clone_Sys::s_clone_abort_count = 0; |
| 48 |
|
| 49 |
/** Number of active wait requests */ |
| 50 |
uint Clone_Sys::s_clone_wait_count = 0; |
| 51 |
|
| 52 |
Clone_Sys::Clone_Sys() |
| 53 |
: m_clone_arr(), |
| 54 |
m_num_clones(), |
| 55 |
m_num_apply_clones(), |
| 56 |
m_snapshot_arr(), |
| 57 |
m_num_snapshots(), |
| 58 |
m_num_apply_snapshots(), |
| 59 |
m_clone_id_generator() { |
| 60 |
mutex_create(LATCH_ID_CLONE_SYS, &m_clone_sys_mutex); |
| 61 |
m_space_initialized.store(false); |
| 62 |
} |
| 63 |
|
| 64 |
Clone_Sys::~Clone_Sys() { |
| 65 |
mutex_free(&m_clone_sys_mutex); |
| 66 |
|
| 67 |
#ifdef UNIV_DEBUG |
| 68 |
/* Verify that no active clone is present */ |
| 69 |
int idx; |
| 70 |
for (idx = 0; idx < CLONE_ARR_SIZE; idx++) { |
| 71 |
ut_ad(m_clone_arr[idx] == nullptr); |
| 72 |
} |
| 73 |
ut_ad(m_num_clones == 0); |
| 74 |
ut_ad(m_num_apply_clones == 0); |
| 75 |
|
| 76 |
for (idx = 0; idx < SNAPSHOT_ARR_SIZE; idx++) { |
| 77 |
ut_ad(m_snapshot_arr[idx] == nullptr); |
| 78 |
} |
| 79 |
ut_ad(m_num_snapshots == 0); |
| 80 |
ut_ad(m_num_apply_snapshots == 0); |
| 81 |
|
| 82 |
#endif /* UNIV_DEBUG */ |
| 83 |
} |
| 84 |
|
| 85 |
Clone_Handle *Clone_Sys::find_clone(const byte *ref_loc, uint loc_len, |
| 86 |
Clone_Handle_Type hdl_type) { |
| 87 |
int idx; |
| 88 |
bool match_found; |
| 89 |
|
| 90 |
Clone_Desc_Locator loc_desc; |
| 91 |
Clone_Desc_Locator ref_desc; |
| 92 |
Clone_Handle *clone_hdl; |
| 93 |
|
| 94 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 95 |
|
| 96 |
if (ref_loc == nullptr) { |
| 97 |
return (nullptr); |
| 98 |
} |
| 99 |
|
| 100 |
ref_desc.deserialize(ref_loc, loc_len, nullptr); |
| 101 |
|
| 102 |
match_found = false; |
| 103 |
clone_hdl = nullptr; |
| 104 |
|
| 105 |
for (idx = 0; idx < CLONE_ARR_SIZE; idx++) { |
| 106 |
clone_hdl = m_clone_arr[idx]; |
| 107 |
|
| 108 |
if (clone_hdl == nullptr || clone_hdl->is_init()) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
if (clone_hdl->match_hdl_type(hdl_type)) { |
| 113 |
clone_hdl->build_descriptor(&loc_desc); |
| 114 |
|
| 115 |
if (loc_desc.match(&ref_desc)) { |
| 116 |
match_found = true; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (match_found) { |
| 123 |
clone_hdl->attach(); |
| 124 |
return (clone_hdl); |
| 125 |
} |
| 126 |
|
| 127 |
return (nullptr); |
| 128 |
} |
| 129 |
|
| 130 |
int Clone_Sys::find_free_index(Clone_Handle_Type hdl_type, uint &free_index) { |
| 131 |
free_index = CLONE_ARR_SIZE; |
| 132 |
|
| 133 |
uint target_index = CLONE_ARR_SIZE; |
| 134 |
Clone_Handle *target_clone = nullptr; |
| 135 |
|
| 136 |
for (uint idx = 0; idx < CLONE_ARR_SIZE; idx++) { |
| 137 |
auto clone_hdl = m_clone_arr[idx]; |
| 138 |
|
| 139 |
if (clone_hdl == nullptr) { |
| 140 |
free_index = idx; |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
/* If existing clone has some error, it is on its way to exit. */ |
| 145 |
auto err = clone_hdl->check_error(nullptr); |
| 146 |
if (hdl_type == CLONE_HDL_COPY && (clone_hdl->is_idle() || err != 0)) { |
| 147 |
target_clone = clone_hdl; |
| 148 |
target_index = idx; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (free_index == CLONE_ARR_SIZE || |
| 153 |
(hdl_type == CLONE_HDL_COPY && m_num_clones == MAX_CLONES) || |
| 154 |
(hdl_type == CLONE_HDL_APPLY && m_num_apply_clones == MAX_CLONES)) { |
| 155 |
if (target_clone == nullptr) { |
| 156 |
my_error(ER_CLONE_TOO_MANY_CONCURRENT_CLONES, MYF(0), MAX_CLONES); |
| 157 |
return (ER_CLONE_TOO_MANY_CONCURRENT_CLONES); |
| 158 |
} |
| 159 |
} else { |
| 160 |
return (0); |
| 161 |
} |
| 162 |
|
| 163 |
/* We can abort idle clone and use the index. */ |
| 164 |
ut_ad(target_clone != nullptr); |
| 165 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 166 |
ut_ad(hdl_type == CLONE_HDL_COPY); |
| 167 |
|
| 168 |
target_clone->set_state(CLONE_STATE_ABORT); |
| 169 |
|
| 170 |
free_index = target_index; |
| 171 |
|
| 172 |
/* Sleep for 100 milliseconds. */ |
| 173 |
Clone_Msec sleep_time(100); |
| 174 |
/* Generate alert message every second. */ |
| 175 |
Clone_Sec alert_interval(1); |
| 176 |
/* Wait for 5 seconds for idle client to abort. */ |
| 177 |
Clone_Sec time_out(5); |
| 178 |
|
| 179 |
bool is_timeout = false; |
| 180 |
auto err = Clone_Sys::wait( |
| 181 |
sleep_time, time_out, alert_interval, |
| 182 |
[&](bool alert, bool &result) { |
| 183 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 184 |
auto current_clone = m_clone_arr[target_index]; |
| 185 |
result = (current_clone != nullptr); |
| 186 |
|
| 187 |
if (thd_killed(nullptr)) { |
| 188 |
ib::info(ER_IB_CLONE_START_STOP) |
| 189 |
<< "Clone Begin Master wait for abort interrupted"; |
| 190 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 191 |
return (ER_QUERY_INTERRUPTED); |
| 192 |
|
| 193 |
} else if (Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 194 |
ib::info(ER_IB_CLONE_START_STOP) |
| 195 |
<< "Clone Begin Master wait for abort interrupted by DDL"; |
| 196 |
my_error(ER_CLONE_DDL_IN_PROGRESS, MYF(0)); |
| 197 |
return (ER_CLONE_DDL_IN_PROGRESS); |
| 198 |
|
| 199 |
} else if (result) { |
| 200 |
if (!current_clone->is_abort()) { |
| 201 |
/* Another clone has taken over the free index. */ |
| 202 |
ib::info(ER_IB_CLONE_START_STOP) |
| 203 |
<< "Clone Begin Master wait for abort interrupted"; |
| 204 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 205 |
return ER_QUERY_INTERRUPTED; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if (!result) { |
| 210 |
ib::info(ER_IB_CLONE_START_STOP) << "Clone Master aborted idle task"; |
| 211 |
|
| 212 |
} else if (alert) { |
| 213 |
ib::info(ER_IB_CLONE_TIMEOUT) |
| 214 |
<< "Clone Master waiting for idle task abort"; |
| 215 |
} |
| 216 |
return (0); |
| 217 |
}, |
| 218 |
clone_sys->get_mutex(), is_timeout); |
| 219 |
|
| 220 |
if (err == 0 && is_timeout) { |
| 221 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master wait for abort timed out"; |
| 222 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 223 |
"Innodb Clone Copy failed to abort idle clone [timeout]"); |
| 224 |
err = ER_INTERNAL_ERROR; |
| 225 |
} |
| 226 |
return (err); |
| 227 |
} |
| 228 |
|
| 229 |
int Clone_Sys::add_clone(const byte *loc, Clone_Handle_Type hdl_type, |
| 230 |
Clone_Handle *&clone_hdl) { |
| 231 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 232 |
ut_ad(m_num_clones <= MAX_CLONES); |
| 233 |
ut_ad(m_num_apply_clones <= MAX_CLONES); |
| 234 |
|
| 235 |
auto version = choose_desc_version(loc); |
| 236 |
|
| 237 |
/* Find a free index to allocate new clone. */ |
| 238 |
uint free_idx; |
| 239 |
auto err = find_free_index(hdl_type, free_idx); |
| 240 |
if (err != 0) { |
| 241 |
return (err); |
| 242 |
} |
| 243 |
|
| 244 |
/* Create a new clone. */ |
| 245 |
clone_hdl = ut::new_withkey<Clone_Handle>( |
| 246 |
ut::make_psi_memory_key(mem_key_clone), hdl_type, version, free_idx); |
| 247 |
|
| 248 |
if (clone_hdl == nullptr) { |
| 249 |
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Clone_Handle)); |
| 250 |
return (ER_OUTOFMEMORY); |
| 251 |
} |
| 252 |
|
| 253 |
m_clone_arr[free_idx] = clone_hdl; |
| 254 |
|
| 255 |
if (hdl_type == CLONE_HDL_COPY) { |
| 256 |
++m_num_clones; |
| 257 |
} else { |
| 258 |
ut_ad(hdl_type == CLONE_HDL_APPLY); |
| 259 |
++m_num_apply_clones; |
| 260 |
} |
| 261 |
|
| 262 |
clone_hdl->attach(); |
| 263 |
|
| 264 |
return (0); |
| 265 |
} |
| 266 |
|
| 267 |
void Clone_Sys::drop_clone(Clone_Handle *clone_handle) { |
| 268 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 269 |
|
| 270 |
if (clone_handle->detach() > 0) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
auto index = clone_handle->get_index(); |
| 275 |
|
| 276 |
ut_ad(m_clone_arr[index] == clone_handle); |
| 277 |
|
| 278 |
m_clone_arr[index] = nullptr; |
| 279 |
|
| 280 |
if (clone_handle->is_copy_clone()) { |
| 281 |
ut_ad(m_num_clones > 0); |
| 282 |
--m_num_clones; |
| 283 |
|
| 284 |
} else { |
| 285 |
ut_ad(m_num_apply_clones > 0); |
| 286 |
--m_num_apply_clones; |
| 287 |
} |
| 288 |
|
| 289 |
ut::delete_(clone_handle); |
| 290 |
} |
| 291 |
|
| 292 |
Clone_Handle *Clone_Sys::get_clone_by_index(const byte *loc, uint loc_len) { |
| 293 |
Clone_Desc_Locator loc_desc; |
| 294 |
Clone_Handle *clone_hdl; |
| 295 |
|
| 296 |
loc_desc.deserialize(loc, loc_len, nullptr); |
| 297 |
|
| 298 |
#ifdef UNIV_DEBUG |
| 299 |
Clone_Desc_Header *header = &loc_desc.m_header; |
| 300 |
ut_ad(header->m_type == CLONE_DESC_LOCATOR); |
| 301 |
#endif |
| 302 |
clone_hdl = m_clone_arr[loc_desc.m_clone_index]; |
| 303 |
|
| 304 |
ut_ad(clone_hdl != nullptr); |
| 305 |
|
| 306 |
return (clone_hdl); |
| 307 |
} |
| 308 |
|
| 309 |
int Clone_Sys::attach_snapshot(Clone_Handle_Type hdl_type, |
| 310 |
Ha_clone_type clone_type, uint64_t snapshot_id, |
| 311 |
bool is_pfs_monitor, Clone_Snapshot *&snapshot) { |
| 312 |
uint idx; |
| 313 |
uint free_idx = SNAPSHOT_ARR_SIZE; |
| 314 |
|
| 315 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 316 |
|
| 317 |
/* Try to attach to an existing snapshot. */ |
| 318 |
for (idx = 0; idx < SNAPSHOT_ARR_SIZE; idx++) { |
| 319 |
snapshot = m_snapshot_arr[idx]; |
| 320 |
|
| 321 |
if (snapshot != nullptr) { |
| 322 |
if (snapshot->attach(hdl_type, is_pfs_monitor)) { |
| 323 |
return (0); |
| 324 |
} |
| 325 |
} else if (free_idx == SNAPSHOT_ARR_SIZE) { |
| 326 |
free_idx = idx; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (free_idx == SNAPSHOT_ARR_SIZE || |
| 331 |
(hdl_type == CLONE_HDL_COPY && m_num_snapshots == MAX_SNAPSHOTS) || |
| 332 |
(hdl_type == CLONE_HDL_APPLY && m_num_apply_snapshots == MAX_SNAPSHOTS)) { |
| 333 |
my_error(ER_CLONE_TOO_MANY_CONCURRENT_CLONES, MYF(0), MAX_SNAPSHOTS); |
| 334 |
return (ER_CLONE_TOO_MANY_CONCURRENT_CLONES); |
| 335 |
} |
| 336 |
|
| 337 |
/* Create a new snapshot. */ |
| 338 |
snapshot = ut::new_withkey<Clone_Snapshot>( |
| 339 |
ut::make_psi_memory_key(mem_key_clone), hdl_type, clone_type, free_idx, |
| 340 |
snapshot_id); |
| 341 |
|
| 342 |
if (snapshot == nullptr) { |
| 343 |
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Clone_Snapshot)); |
| 344 |
return (ER_OUTOFMEMORY); |
| 345 |
} |
| 346 |
|
| 347 |
m_snapshot_arr[free_idx] = snapshot; |
| 348 |
|
| 349 |
if (hdl_type == CLONE_HDL_COPY) { |
| 350 |
++m_num_snapshots; |
| 351 |
} else { |
| 352 |
ut_ad(hdl_type == CLONE_HDL_APPLY); |
| 353 |
++m_num_apply_snapshots; |
| 354 |
} |
| 355 |
|
| 356 |
snapshot->attach(hdl_type, is_pfs_monitor); |
| 357 |
|
| 358 |
return (0); |
| 359 |
} |
| 360 |
|
| 361 |
void Clone_Sys::detach_snapshot(Clone_Snapshot *snapshot, |
| 362 |
Clone_Handle_Type hdl_type) { |
| 363 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 364 |
snapshot->detach(); |
| 365 |
|
| 366 |
/* Drop the snapshot. */ |
| 367 |
uint index; |
| 368 |
|
| 369 |
index = snapshot->get_index(); |
| 370 |
ut_ad(m_snapshot_arr[index] == snapshot); |
| 371 |
|
| 372 |
ut::delete_(snapshot); |
| 373 |
|
| 374 |
m_snapshot_arr[index] = nullptr; |
| 375 |
|
| 376 |
if (hdl_type == CLONE_HDL_COPY) { |
| 377 |
ut_ad(m_num_snapshots > 0); |
| 378 |
--m_num_snapshots; |
| 379 |
|
| 380 |
} else { |
| 381 |
ut_ad(hdl_type == CLONE_HDL_APPLY); |
| 382 |
ut_ad(m_num_apply_snapshots > 0); |
| 383 |
--m_num_apply_snapshots; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
Clone_Sys::Acquire_clone::Acquire_clone() { |
| 388 |
std::tie(std::ignore, m_clone) = clone_sys->check_active_clone(); |
| 389 |
|
| 390 |
if (m_clone != nullptr) { |
| 391 |
m_clone->attach(); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
Clone_Sys::Acquire_clone::~Acquire_clone() { |
| 396 |
if (m_clone != nullptr) { |
| 397 |
clone_sys->drop_clone(m_clone); |
| 398 |
} |
| 399 |
m_clone = nullptr; |
| 400 |
} |
| 401 |
|
| 402 |
Clone_Snapshot *Clone_Sys::Acquire_clone::get_snapshot() { |
| 403 |
if (m_clone == nullptr) { |
| 404 |
return nullptr; /* purecov: inspected */ |
| 405 |
} |
| 406 |
return m_clone->get_snapshot(); |
| 407 |
} |
| 408 |
|
| 409 |
bool Clone_Sys::check_active_clone(bool print_alert) { |
| 410 |
bool active_clone = false; |
| 411 |
std::tie(active_clone, std::ignore) = check_active_clone(); |
| 412 |
|
| 413 |
if (active_clone && print_alert) { |
| 414 |
/* purecov: begin inspected */ |
| 415 |
ib::info(ER_IB_CLONE_TIMEOUT) << "DDL waiting for CLONE to abort"; |
| 416 |
/* purecov: end */ |
| 417 |
} |
| 418 |
return (active_clone); |
| 419 |
} |
| 420 |
|
| 421 |
std::tuple<bool, Clone_Handle *> Clone_Sys::check_active_clone() { |
| 422 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 423 |
|
| 424 |
bool active_clone = false; |
| 425 |
Clone_Handle *active_handle = nullptr; |
| 426 |
|
| 427 |
/* Check for active clone operations. */ |
| 428 |
for (int idx = 0; idx < CLONE_ARR_SIZE; idx++) { |
| 429 |
auto clone_hdl = m_clone_arr[idx]; |
| 430 |
|
| 431 |
if (clone_hdl != nullptr && clone_hdl->is_copy_clone()) { |
| 432 |
active_clone = true; |
| 433 |
active_handle = clone_hdl; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
return std::make_tuple(active_clone, active_handle); |
| 438 |
} |
| 439 |
|
| 440 |
bool Clone_Sys::mark_abort(bool force) { |
| 441 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 442 |
|
| 443 |
/* Check for active clone operations. Ignore clone, before initializing |
| 444 |
space. It is safe as clone would check for abort request afterwards. We |
| 445 |
require this check to prevent self deadlock when clone needs to create |
| 446 |
space objects while initializing.*/ |
| 447 |
|
| 448 |
auto active_clone = is_space_initialized() && check_active_clone(false); |
| 449 |
|
| 450 |
/* If active clone is running and force is not set then |
| 451 |
return without setting abort state. */ |
| 452 |
if (active_clone && !force) { |
| 453 |
return (false); |
| 454 |
} |
| 455 |
|
| 456 |
++s_clone_abort_count; |
| 457 |
|
| 458 |
if (s_clone_sys_state != CLONE_SYS_ABORT) { |
| 459 |
ut_ad(s_clone_abort_count == 1); |
| 460 |
s_clone_sys_state = CLONE_SYS_ABORT; |
| 461 |
|
| 462 |
DEBUG_SYNC_C("clone_marked_abort"); |
| 463 |
} |
| 464 |
|
| 465 |
if (active_clone) { |
| 466 |
ut_ad(force); |
| 467 |
|
| 468 |
/* Sleep for 1 second */ |
| 469 |
Clone_Msec sleep_time(Clone_Sec(1)); |
| 470 |
/* Generate alert message every minute. */ |
| 471 |
Clone_Sec alert_time(Clone_Min(1)); |
| 472 |
/* Timeout in 15 minutes - safeguard against hang, should not happen */ |
| 473 |
Clone_Sec time_out(Clone_Min(15)); |
| 474 |
|
| 475 |
bool is_timeout = false; |
| 476 |
|
| 477 |
wait( |
| 478 |
sleep_time, time_out, alert_time, |
| 479 |
[&](bool alert, bool &result) { |
| 480 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 481 |
result = check_active_clone(alert); |
| 482 |
|
| 483 |
return (0); |
| 484 |
}, |
| 485 |
&m_clone_sys_mutex, is_timeout); |
| 486 |
|
| 487 |
if (is_timeout) { |
| 488 |
ib::warn(ER_IB_CLONE_TIMEOUT) << "DDL wait for CLONE abort timed out" |
| 489 |
", Continuing DDL."; |
| 490 |
ut_d(ut_error); |
| 491 |
} |
| 492 |
} |
| 493 |
return (true); |
| 494 |
} |
| 495 |
|
| 496 |
void Clone_Sys::mark_active() { |
| 497 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 498 |
|
| 499 |
ut_ad(s_clone_abort_count > 0); |
| 500 |
--s_clone_abort_count; |
| 501 |
|
| 502 |
if (s_clone_abort_count == 0) { |
| 503 |
s_clone_sys_state = CLONE_SYS_ACTIVE; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
void Clone_Sys::mark_wait() { |
| 508 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 509 |
/* Let any new clone operation wait till mark_free is called. */ |
| 510 |
++s_clone_wait_count; |
| 511 |
} |
| 512 |
|
| 513 |
void Clone_Sys::mark_free() { |
| 514 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 515 |
ut_ad(s_clone_wait_count > 0); |
| 516 |
--s_clone_wait_count; |
| 517 |
} |
| 518 |
|
| 519 |
#ifdef UNIV_DEBUG |
| 520 |
void Clone_Sys::debug_wait_clone_begin() { |
| 521 |
mutex_exit(&m_clone_sys_mutex); |
| 522 |
DEBUG_SYNC_C("clone_begin_wait_ddl"); |
| 523 |
mutex_enter(&m_clone_sys_mutex); |
| 524 |
} |
| 525 |
#endif /* UNIV_DEBUG */ |
| 526 |
|
| 527 |
int Clone_Sys::wait_for_free(THD *thd) { |
| 528 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 529 |
|
| 530 |
if (s_clone_wait_count == 0) { |
| 531 |
return (0); |
| 532 |
} |
| 533 |
|
| 534 |
auto wait_condition = [&](bool alert, bool &result) { |
| 535 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 536 |
result = (s_clone_wait_count > 0); |
| 537 |
if (alert) { |
| 538 |
/* purecov: begin inspected */ |
| 539 |
ib::info(ER_IB_CLONE_OPERATION) |
| 540 |
<< "CLONE BEGIN waiting for DDL in critical section"; |
| 541 |
/* purecov: end */ |
| 542 |
} |
| 543 |
|
| 544 |
ut_d(debug_wait_clone_begin()); |
| 545 |
|
| 546 |
if (thd_killed(thd)) { |
| 547 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 548 |
return (ER_QUERY_INTERRUPTED); |
| 549 |
} |
| 550 |
|
| 551 |
if (s_clone_sys_state == CLONE_SYS_ABORT) { |
| 552 |
/* purecov: begin inspected */ |
| 553 |
my_error(ER_CLONE_DDL_IN_PROGRESS, MYF(0)); |
| 554 |
return ER_CLONE_DDL_IN_PROGRESS; |
| 555 |
/* purecov: end */ |
| 556 |
} |
| 557 |
return (0); |
| 558 |
}; |
| 559 |
|
| 560 |
/* Sleep for 100 milliseconds */ |
| 561 |
Clone_Msec sleep_time(100); |
| 562 |
/* Generate alert message 5 second. */ |
| 563 |
Clone_Sec alert_time(5); |
| 564 |
/* Timeout in 5 minutes - safeguard against hang, should not happen */ |
| 565 |
Clone_Sec time_out(Clone_Min(5)); |
| 566 |
|
| 567 |
bool is_timeout = false; |
| 568 |
auto err = wait(sleep_time, time_out, alert_time, wait_condition, |
| 569 |
&m_clone_sys_mutex, is_timeout); |
| 570 |
|
| 571 |
if (err != 0) { |
| 572 |
return (err); |
| 573 |
} |
| 574 |
|
| 575 |
if (is_timeout) { |
| 576 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 577 |
"Clone BEGIN timeout waiting for DDL in critical section"); |
| 578 |
ut_d(ut_error); |
| 579 |
ut_o(return (ER_INTERNAL_ERROR)); |
| 580 |
} |
| 581 |
|
| 582 |
return (0); |
| 583 |
} |
| 584 |
|
| 585 |
bool Clone_Sys::begin_ddl_state(Clone_notify::Type type, space_id_t space, |
| 586 |
bool no_wait, bool check_intr, |
| 587 |
uint32_t &blocked_state, int &error) { |
| 588 |
ut_ad(mutex_own(get_mutex())); |
| 589 |
Acquire_clone clone_handle; |
| 590 |
|
| 591 |
auto snapshot = clone_handle.get_snapshot(); |
| 592 |
ut_ad(snapshot != nullptr); |
| 593 |
blocked_state = CLONE_SNAPSHOT_NONE; |
| 594 |
|
| 595 |
if (snapshot == nullptr) { |
| 596 |
return false; /* purecov: inspected */ |
| 597 |
} |
| 598 |
|
| 599 |
DBUG_EXECUTE_IF("clone_ddl_error_abort", { |
| 600 |
error = ER_INTERNAL_ERROR; |
| 601 |
my_error(error, MYF(0), "Simulated Clone DDL error"); |
| 602 |
return false; |
| 603 |
}); |
| 604 |
|
| 605 |
/* Safe to release mutex after pinning the clone handle. */ |
| 606 |
mutex_exit(get_mutex()); |
| 607 |
bool blocked = |
| 608 |
snapshot->begin_ddl_state(type, space, no_wait, check_intr, error); |
| 609 |
mutex_enter(get_mutex()); |
| 610 |
|
| 611 |
blocked_state = blocked ? snapshot->get_state() : CLONE_SNAPSHOT_NONE; |
| 612 |
|
| 613 |
return blocked; |
| 614 |
} |
| 615 |
|
| 616 |
void Clone_Sys::end_ddl_state(Clone_notify::Type type, space_id_t space, |
| 617 |
uint32_t blocked_state) { |
| 618 |
ut_ad(mutex_own(get_mutex())); |
| 619 |
Acquire_clone clone_handle; |
| 620 |
|
| 621 |
auto snapshot = clone_handle.get_snapshot(); |
| 622 |
|
| 623 |
/* Clone might have exited with error. */ |
| 624 |
if (snapshot == nullptr) { |
| 625 |
return; /* purecov: inspected */ |
| 626 |
} |
| 627 |
|
| 628 |
if (blocked_state != snapshot->get_state()) { |
| 629 |
/* purecov: begin deadcode */ |
| 630 |
ib::error(ER_IB_CLONE_INTERNAL); |
| 631 |
ut_d(ut_error); |
| 632 |
/* purecov: end */ |
| 633 |
} |
| 634 |
|
| 635 |
/* Safe to release mutex after pinning the clone handle. */ |
| 636 |
mutex_exit(get_mutex()); |
| 637 |
snapshot->end_ddl_state(type, space); |
| 638 |
mutex_enter(get_mutex()); |
| 639 |
} |
| 640 |
|
| 641 |
uint64_t Clone_Sys::get_next_id() { |
| 642 |
ut_ad(mutex_own(&m_clone_sys_mutex)); |
| 643 |
|
| 644 |
return (++m_clone_id_generator); |
| 645 |
} |
| 646 |
|
| 647 |
#ifdef UNIV_DEBUG |
| 648 |
bool Clone_Task_Manager::debug_sync_check(uint32_t chunk_num, |
| 649 |
Clone_Task *task) { |
| 650 |
auto nchunks = m_clone_snapshot->get_num_chunks(); |
| 651 |
|
| 652 |
/* Stop somewhere in the middle of current stage */ |
| 653 |
if (!task->m_is_master || task->m_ignore_sync || |
| 654 |
(chunk_num != 0 && chunk_num < (nchunks / 2 + 1))) { |
| 655 |
return false; |
| 656 |
} |
| 657 |
|
| 658 |
/* Ignore sync request for all future requests. */ |
| 659 |
task->m_ignore_sync = true; |
| 660 |
return true; |
| 661 |
} |
| 662 |
|
| 663 |
void Clone_Task_Manager::debug_wait_ddl_meta() { |
| 664 |
auto state = m_clone_snapshot->get_state(); |
| 665 |
|
| 666 |
/* We send DDL metadata of previous state. */ |
| 667 |
if (state == CLONE_SNAPSHOT_PAGE_COPY) { |
| 668 |
DEBUG_SYNC_C("clone_before_file_ddl_meta"); |
| 669 |
|
| 670 |
} else if (state == CLONE_SNAPSHOT_REDO_COPY) { |
| 671 |
DEBUG_SYNC_C("clone_before_page_ddl_meta"); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
Clone_Task *Clone_Task_Manager::find_master_task() { |
| 676 |
Clone_Task *task = nullptr; |
| 677 |
|
| 678 |
for (uint32_t index = 0; index < m_num_tasks; ++index) { |
| 679 |
task = &m_clone_tasks[index]; |
| 680 |
if (task->m_is_master) { |
| 681 |
break; |
| 682 |
} |
| 683 |
} |
| 684 |
return task; |
| 685 |
} |
| 686 |
|
| 687 |
void Clone_Handle::close_master_file() { |
| 688 |
auto task = m_clone_task_manager.find_master_task(); |
| 689 |
close_and_unpin_file(task); |
| 690 |
} |
| 691 |
|
| 692 |
void Clone_Sys::close_donor_master_file() { |
| 693 |
IB_mutex_guard sys_mutex(get_mutex(), UT_LOCATION_HERE); |
| 694 |
|
| 695 |
Clone_Handle *clone_donor = nullptr; |
| 696 |
std::tie(std::ignore, clone_donor) = clone_sys->check_active_clone(); |
| 697 |
|
| 698 |
clone_donor->close_master_file(); |
| 699 |
} |
| 700 |
|
| 701 |
void Clone_Task_Manager::debug_wait(uint chunk_num, Clone_Task *task) { |
| 702 |
auto state = m_clone_snapshot->get_state(); |
| 703 |
|
| 704 |
if (!debug_sync_check(chunk_num, task)) { |
| 705 |
return; |
| 706 |
} |
| 707 |
|
| 708 |
/* We are releasing the donor PIN early in debug mode to allow concurrent DDL |
| 709 |
after blocking here. The test need to ensure that it is local clone so that |
| 710 |
donor master task context can be found. This is in recipient path. */ |
| 711 |
DBUG_EXECUTE_IF("local_release_clone_file_pin", { |
| 712 |
clone_sys->close_donor_master_file(); |
| 713 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone debug close donor master file"; |
| 714 |
}); |
| 715 |
|
| 716 |
if (state == CLONE_SNAPSHOT_FILE_COPY) { |
| 717 |
DBUG_SIGNAL_WAIT_FOR(current_thd, "gr_clone_wait", "gr_clone_paused", |
| 718 |
"gr_clone_continue"); |
| 719 |
|
| 720 |
DEBUG_SYNC_C("clone_file_copy"); |
| 721 |
|
| 722 |
} else if (state == CLONE_SNAPSHOT_PAGE_COPY) { |
| 723 |
DEBUG_SYNC_C("clone_page_copy"); |
| 724 |
|
| 725 |
} else if (state == CLONE_SNAPSHOT_REDO_COPY) { |
| 726 |
DEBUG_SYNC_C("clone_redo_copy"); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
int Clone_Task_Manager::debug_restart(Clone_Task *task, int in_err, |
| 731 |
int restart_count) { |
| 732 |
auto err = in_err; |
| 733 |
|
| 734 |
if (err != 0 || restart_count < task->m_debug_counter || !task->m_is_master) { |
| 735 |
return (err); |
| 736 |
} |
| 737 |
|
| 738 |
/* Restart somewhere in the middle of all chunks */ |
| 739 |
if (restart_count == 1) { |
| 740 |
auto nchunks = m_clone_snapshot->get_num_chunks(); |
| 741 |
auto cur_chunk = task->m_task_meta.m_chunk_num; |
| 742 |
|
| 743 |
if (cur_chunk != 0 && cur_chunk < (nchunks / 2 + 1)) { |
| 744 |
return (err); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
DBUG_EXECUTE_IF("clone_restart_apply", err = ER_NET_READ_ERROR;); |
| 749 |
|
| 750 |
if (err != 0) { |
| 751 |
my_error(err, MYF(0)); |
| 752 |
} |
| 753 |
|
| 754 |
/* Allow restart from next point */ |
| 755 |
task->m_debug_counter = restart_count + 1; |
| 756 |
|
| 757 |
return (err); |
| 758 |
} |
| 759 |
#endif /* UNIV_DEBUG */ |
| 760 |
|
| 761 |
void Clone_Task_Manager::init(Clone_Snapshot *snapshot) { |
| 762 |
uint idx; |
| 763 |
|
| 764 |
m_clone_snapshot = snapshot; |
| 765 |
|
| 766 |
m_current_state = snapshot->get_state(); |
| 767 |
|
| 768 |
/* ACK state is the previous state of current state */ |
| 769 |
if (m_current_state == CLONE_SNAPSHOT_INIT) { |
| 770 |
m_ack_state = CLONE_SNAPSHOT_NONE; |
| 771 |
} else { |
| 772 |
/* If clone is attaching to active snapshot with |
| 773 |
other concurrent clone */ |
| 774 |
ut_ad(m_current_state == CLONE_SNAPSHOT_FILE_COPY); |
| 775 |
m_ack_state = CLONE_SNAPSHOT_INIT; |
| 776 |
} |
| 777 |
|
| 778 |
m_chunk_info.m_total_chunks = 0; |
| 779 |
|
| 780 |
m_chunk_info.m_min_unres_chunk = 1; |
| 781 |
m_chunk_info.m_max_res_chunk = 0; |
| 782 |
|
| 783 |
/* Initialize all tasks in inactive state. */ |
| 784 |
for (idx = 0; idx < CLONE_MAX_TASKS; idx++) { |
| 785 |
Clone_Task *task; |
| 786 |
|
| 787 |
task = m_clone_tasks + idx; |
| 788 |
task->m_task_state = CLONE_TASK_INACTIVE; |
| 789 |
|
| 790 |
task->m_serial_desc = nullptr; |
| 791 |
task->m_alloc_len = 0; |
| 792 |
|
| 793 |
task->m_current_file_des.m_file = OS_FILE_CLOSED; |
| 794 |
task->m_pinned_file = false; |
| 795 |
task->m_current_file_index = 0; |
| 796 |
task->m_file_cache = true; |
| 797 |
|
| 798 |
task->m_current_buffer = nullptr; |
| 799 |
task->m_buffer_alloc_len = 0; |
| 800 |
task->m_is_master = false; |
| 801 |
task->m_has_thd = false; |
| 802 |
task->m_data_size = 0; |
| 803 |
ut_d(task->m_ignore_sync = false); |
| 804 |
ut_d(task->m_debug_counter = 2); |
| 805 |
} |
| 806 |
|
| 807 |
m_num_tasks = 0; |
| 808 |
m_num_tasks_finished = 0; |
| 809 |
m_num_tasks_transit = 0; |
| 810 |
m_restart_count = 0; |
| 811 |
|
| 812 |
m_next_state = CLONE_SNAPSHOT_NONE; |
| 813 |
m_send_state_meta = false; |
| 814 |
m_transferred_file_meta = false; |
| 815 |
m_saved_error = 0; |
| 816 |
|
| 817 |
/* Initialize error file name */ |
| 818 |
m_err_file_name.assign("Clone File"); |
| 819 |
} |
| 820 |
|
| 821 |
void Clone_Task_Manager::reserve_task(THD *thd, uint &task_id) { |
| 822 |
ut_ad(mutex_own(&m_state_mutex)); |
| 823 |
|
| 824 |
Clone_Task *task = nullptr; |
| 825 |
|
| 826 |
task_id = 0; |
| 827 |
|
| 828 |
/* Find inactive task in the array. */ |
| 829 |
for (; task_id < CLONE_MAX_TASKS; task_id++) { |
| 830 |
task = m_clone_tasks + task_id; |
| 831 |
auto task_meta = &task->m_task_meta; |
| 832 |
|
| 833 |
if (task->m_task_state == CLONE_TASK_INACTIVE) { |
| 834 |
task->m_task_state = CLONE_TASK_ACTIVE; |
| 835 |
|
| 836 |
task_meta->m_task_index = task_id; |
| 837 |
task_meta->m_chunk_num = 0; |
| 838 |
task_meta->m_block_num = 0; |
| 839 |
|
| 840 |
/* Set first task as master task */ |
| 841 |
if (task_id == 0) { |
| 842 |
ut_ad(thd != nullptr); |
| 843 |
task->m_is_master = true; |
| 844 |
} |
| 845 |
|
| 846 |
/* Whether the task has an associated user session */ |
| 847 |
task->m_has_thd = (thd != nullptr); |
| 848 |
|
| 849 |
break; |
| 850 |
} |
| 851 |
|
| 852 |
task = nullptr; |
| 853 |
} |
| 854 |
|
| 855 |
ut_ad(task != nullptr); |
| 856 |
} |
| 857 |
|
| 858 |
int Clone_Task_Manager::alloc_buffer(Clone_Task *task) { |
| 859 |
if (task->m_alloc_len != 0) { |
| 860 |
/* Task buffers are already allocated in case |
| 861 |
clone operation is restarted. */ |
| 862 |
|
| 863 |
ut_ad(task->m_buffer_alloc_len != 0); |
| 864 |
ut_ad(task->m_serial_desc != nullptr); |
| 865 |
ut_ad(task->m_current_buffer != nullptr); |
| 866 |
|
| 867 |
return (0); |
| 868 |
} |
| 869 |
|
| 870 |
/* Allocate task descriptor. */ |
| 871 |
auto heap = m_clone_snapshot->lock_heap(); |
| 872 |
|
| 873 |
/* Maximum variable length of descriptor. */ |
| 874 |
auto alloc_len = |
| 875 |
static_cast<uint>(m_clone_snapshot->get_max_file_name_length()); |
| 876 |
|
| 877 |
/* Check with maximum path name length. */ |
| 878 |
if (alloc_len < FN_REFLEN_SE) { |
| 879 |
alloc_len = FN_REFLEN_SE; |
| 880 |
} |
| 881 |
|
| 882 |
/* Maximum fixed length of descriptor */ |
| 883 |
alloc_len += CLONE_DESC_MAX_BASE_LEN; |
| 884 |
|
| 885 |
/* Add some buffer. */ |
| 886 |
alloc_len += CLONE_DESC_MAX_BASE_LEN; |
| 887 |
|
| 888 |
ut_ad(task->m_alloc_len == 0); |
| 889 |
ut_ad(task->m_buffer_alloc_len == 0); |
| 890 |
|
| 891 |
task->m_alloc_len = alloc_len; |
| 892 |
task->m_buffer_alloc_len = m_clone_snapshot->get_dyn_buffer_length(); |
| 893 |
|
| 894 |
alloc_len += task->m_buffer_alloc_len; |
| 895 |
|
| 896 |
alloc_len += CLONE_ALIGN_DIRECT_IO; |
| 897 |
|
| 898 |
ut_ad(task->m_serial_desc == nullptr); |
| 899 |
|
| 900 |
task->m_serial_desc = static_cast<byte *>(mem_heap_zalloc(heap, alloc_len)); |
| 901 |
|
| 902 |
m_clone_snapshot->release_heap(heap); |
| 903 |
|
| 904 |
if (task->m_serial_desc == nullptr) { |
| 905 |
my_error(ER_OUTOFMEMORY, MYF(0), alloc_len); |
| 906 |
return (ER_OUTOFMEMORY); |
| 907 |
} |
| 908 |
|
| 909 |
if (task->m_buffer_alloc_len > 0) { |
| 910 |
task->m_current_buffer = static_cast<byte *>(ut_align( |
| 911 |
task->m_serial_desc + task->m_alloc_len, CLONE_ALIGN_DIRECT_IO)); |
| 912 |
} |
| 913 |
|
| 914 |
return (0); |
| 915 |
} |
| 916 |
|
| 917 |
int Clone_Task_Manager::handle_error_other_task(bool set_error) { |
| 918 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 919 |
|
| 920 |
if (set_error && m_saved_error != 0) { |
| 921 |
ib::info(ER_IB_CLONE_OPERATION) |
| 922 |
<< "Clone error from other task code: " << m_saved_error; |
| 923 |
} |
| 924 |
|
| 925 |
if (!set_error) { |
| 926 |
return (m_saved_error); |
| 927 |
} |
| 928 |
|
| 929 |
/* Handle shutdown and KILL */ |
| 930 |
if (thd_killed(nullptr)) { |
| 931 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 932 |
return (ER_QUERY_INTERRUPTED); |
| 933 |
} |
| 934 |
|
| 935 |
/* Check if DDL has marked for abort. Ignore for client apply. */ |
| 936 |
if ((m_clone_snapshot == nullptr || m_clone_snapshot->is_copy()) && |
| 937 |
Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 938 |
my_error(ER_CLONE_DDL_IN_PROGRESS, MYF(0)); |
| 939 |
return (ER_CLONE_DDL_IN_PROGRESS); |
| 940 |
} |
| 941 |
|
| 942 |
switch (m_saved_error) { |
| 943 |
case ER_CLONE_DDL_IN_PROGRESS: |
| 944 |
case ER_QUERY_INTERRUPTED: |
| 945 |
my_error(m_saved_error, MYF(0)); |
| 946 |
break; |
| 947 |
|
| 948 |
/* Network errors */ |
| 949 |
case ER_NET_PACKET_TOO_LARGE: |
| 950 |
case ER_NET_PACKETS_OUT_OF_ORDER: |
| 951 |
case ER_NET_UNCOMPRESS_ERROR: |
| 952 |
case ER_NET_READ_ERROR: |
| 953 |
case ER_NET_READ_INTERRUPTED: |
| 954 |
case ER_NET_ERROR_ON_WRITE: |
| 955 |
case ER_NET_WRITE_INTERRUPTED: |
| 956 |
case ER_NET_WAIT_ERROR: |
| 957 |
my_error(m_saved_error, MYF(0)); |
| 958 |
break; |
| 959 |
|
| 960 |
/* IO Errors */ |
| 961 |
case ER_CANT_OPEN_FILE: |
| 962 |
case ER_CANT_CREATE_FILE: |
| 963 |
case ER_ERROR_ON_READ: |
| 964 |
case ER_ERROR_ON_WRITE: |
| 965 |
/* purecov: begin inspected */ |
| 966 |
my_error(m_saved_error, MYF(0), m_err_file_name.c_str(), errno, |
| 967 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 968 |
break; |
| 969 |
/* purecov: end */ |
| 970 |
|
| 971 |
case ER_FILE_EXISTS_ERROR: |
| 972 |
my_error(m_saved_error, MYF(0), m_err_file_name.c_str()); |
| 973 |
break; |
| 974 |
|
| 975 |
case ER_WRONG_VALUE: |
| 976 |
my_error(m_saved_error, MYF(0), "file path", m_err_file_name.c_str()); |
| 977 |
break; |
| 978 |
|
| 979 |
case ER_CLONE_DONOR: |
| 980 |
/* Will get the error message from remote */ |
| 981 |
break; |
| 982 |
|
| 983 |
case 0: |
| 984 |
break; |
| 985 |
|
| 986 |
default: |
| 987 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 988 |
"Innodb Clone error in concurrent task"); |
| 989 |
} |
| 990 |
|
| 991 |
return (m_saved_error); |
| 992 |
} |
| 993 |
|
| 994 |
bool Clone_Task_Manager::wait_before_add(const byte *ref_loc, uint loc_len) { |
| 995 |
ut_ad(mutex_own(&m_state_mutex)); |
| 996 |
|
| 997 |
/* 1. Don't wait if master task. */ |
| 998 |
if (m_num_tasks == 0) { |
| 999 |
return (false); |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* 2. Wait for state transition to get over */ |
| 1003 |
if (in_transit_state()) { |
| 1004 |
return (true); |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* 3. For copy state(donor), wait for the state to reach file copy. */ |
| 1008 |
ut_ad(m_current_state != CLONE_SNAPSHOT_NONE); |
| 1009 |
if (ref_loc == nullptr) { |
| 1010 |
return (m_current_state == CLONE_SNAPSHOT_INIT); |
| 1011 |
} |
| 1012 |
|
| 1013 |
Clone_Desc_Locator ref_desc; |
| 1014 |
ref_desc.deserialize(ref_loc, loc_len, nullptr); |
| 1015 |
|
| 1016 |
ut_ad(m_current_state <= ref_desc.m_state); |
| 1017 |
|
| 1018 |
/* 4. For apply state (recipient), wait for apply state to reach |
| 1019 |
the copy state in reference locator. */ |
| 1020 |
if (m_current_state != ref_desc.m_state) { |
| 1021 |
return (true); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* 4A. For file copy state, wait for all metadata to be transferred. */ |
| 1025 |
if (m_current_state == CLONE_SNAPSHOT_FILE_COPY && |
| 1026 |
!is_file_metadata_transferred()) { |
| 1027 |
return (true); |
| 1028 |
} |
| 1029 |
return (false); |
| 1030 |
} |
| 1031 |
|
| 1032 |
int Clone_Task_Manager::add_task(THD *thd, const byte *ref_loc, uint loc_len, |
| 1033 |
uint &task_id) { |
| 1034 |
mutex_enter(&m_state_mutex); |
| 1035 |
|
| 1036 |
/* Check for error from other tasks */ |
| 1037 |
bool raise_error = (thd != nullptr); |
| 1038 |
|
| 1039 |
auto err = handle_error_other_task(raise_error); |
| 1040 |
|
| 1041 |
if (err != 0) { |
| 1042 |
mutex_exit(&m_state_mutex); |
| 1043 |
return (err); |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (wait_before_add(ref_loc, loc_len)) { |
| 1047 |
bool is_timeout = false; |
| 1048 |
int alert_count = 0; |
| 1049 |
err = Clone_Sys::wait_default( |
| 1050 |
[&](bool alert, bool &result) { |
| 1051 |
ut_ad(mutex_own(&m_state_mutex)); |
| 1052 |
result = wait_before_add(ref_loc, loc_len); |
| 1053 |
|
| 1054 |
/* Check for error from other tasks */ |
| 1055 |
err = handle_error_other_task(raise_error); |
| 1056 |
|
| 1057 |
if (err == 0 && result && alert) { |
| 1058 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 1059 |
if (++alert_count == 12) { |
| 1060 |
alert_count = 0; |
| 1061 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Add task waiting " |
| 1062 |
"for state change"; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
return (err); |
| 1066 |
}, |
| 1067 |
&m_state_mutex, is_timeout); |
| 1068 |
|
| 1069 |
if (err != 0) { |
| 1070 |
mutex_exit(&m_state_mutex); |
| 1071 |
return (err); |
| 1072 |
|
| 1073 |
} else if (is_timeout) { |
| 1074 |
ut_d(ut_error); |
| 1075 |
#ifndef UNIV_DEBUG |
| 1076 |
mutex_exit(&m_state_mutex); |
| 1077 |
|
| 1078 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Add task timed out"; |
| 1079 |
|
| 1080 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1081 |
"Clone Add task failed: " |
| 1082 |
"Wait too long for state transition"); |
| 1083 |
return (ER_INTERNAL_ERROR); |
| 1084 |
#endif |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* We wait for state transition before adding new task. */ |
| 1089 |
ut_ad(!in_transit_state()); |
| 1090 |
|
| 1091 |
if (m_num_tasks == CLONE_MAX_TASKS) { |
| 1092 |
err = ER_CLONE_TOO_MANY_CONCURRENT_CLONES; |
| 1093 |
my_error(err, MYF(0), CLONE_MAX_TASKS); |
| 1094 |
|
| 1095 |
mutex_exit(&m_state_mutex); |
| 1096 |
return (err); |
| 1097 |
} |
| 1098 |
|
| 1099 |
reserve_task(thd, task_id); |
| 1100 |
ut_ad(task_id <= m_num_tasks); |
| 1101 |
|
| 1102 |
++m_num_tasks; |
| 1103 |
|
| 1104 |
mutex_exit(&m_state_mutex); |
| 1105 |
return (0); |
| 1106 |
} |
| 1107 |
|
| 1108 |
bool Clone_Task_Manager::drop_task(THD *thd, uint task_id, bool &is_master) { |
| 1109 |
mutex_enter(&m_state_mutex); |
| 1110 |
|
| 1111 |
if (in_transit_state()) { |
| 1112 |
ut_ad(m_num_tasks_transit > 0); |
| 1113 |
--m_num_tasks_transit; |
| 1114 |
} |
| 1115 |
|
| 1116 |
ut_ad(m_num_tasks > 0); |
| 1117 |
--m_num_tasks; |
| 1118 |
|
| 1119 |
auto task = get_task_by_index(task_id); |
| 1120 |
|
| 1121 |
add_incomplete_chunk(task); |
| 1122 |
|
| 1123 |
reset_chunk(task); |
| 1124 |
|
| 1125 |
ut_ad(task->m_task_state == CLONE_TASK_ACTIVE); |
| 1126 |
task->m_task_state = CLONE_TASK_INACTIVE; |
| 1127 |
|
| 1128 |
is_master = task->m_is_master; |
| 1129 |
|
| 1130 |
if (!is_master) { |
| 1131 |
mutex_exit(&m_state_mutex); |
| 1132 |
return (false); |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* Master needs to wait for other tasks to get dropped */ |
| 1136 |
if (m_num_tasks > 0) { |
| 1137 |
bool is_timeout = false; |
| 1138 |
int alert_count = 0; |
| 1139 |
auto err = Clone_Sys::wait_default( |
| 1140 |
[&](bool alert, bool &result) { |
| 1141 |
ut_ad(mutex_own(&m_state_mutex)); |
| 1142 |
result = (m_num_tasks > 0); |
| 1143 |
|
| 1144 |
if (thd_killed(thd)) { |
| 1145 |
return (ER_QUERY_INTERRUPTED); |
| 1146 |
|
| 1147 |
} else if (Clone_Sys::s_clone_sys_state == CLONE_SYS_ABORT) { |
| 1148 |
return (ER_CLONE_DDL_IN_PROGRESS); |
| 1149 |
} |
| 1150 |
if (alert && result) { |
| 1151 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 1152 |
if (++alert_count == 12) { |
| 1153 |
alert_count = 0; |
| 1154 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master drop task waiting " |
| 1155 |
"for other tasks"; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
return (0); |
| 1159 |
}, |
| 1160 |
&m_state_mutex, is_timeout); |
| 1161 |
|
| 1162 |
if (err != 0) { |
| 1163 |
mutex_exit(&m_state_mutex); |
| 1164 |
return (false); |
| 1165 |
|
| 1166 |
} else if (is_timeout) { |
| 1167 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master drop task timed out"; |
| 1168 |
|
| 1169 |
mutex_exit(&m_state_mutex); |
| 1170 |
ut_d(ut_error); |
| 1171 |
ut_o(return (false)); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
mutex_exit(&m_state_mutex); |
| 1176 |
|
| 1177 |
/* Restart after network error */ |
| 1178 |
auto current_err = handle_error_other_task(false); |
| 1179 |
if (is_network_error(current_err)) { |
| 1180 |
return (true); |
| 1181 |
} |
| 1182 |
return (false); |
| 1183 |
} |
| 1184 |
|
| 1185 |
uint32_t Clone_Task_Manager::get_next_chunk() { |
| 1186 |
auto &max_chunk = m_chunk_info.m_max_res_chunk; |
| 1187 |
auto &min_chunk = m_chunk_info.m_min_unres_chunk; |
| 1188 |
|
| 1189 |
ut_ad(max_chunk <= m_chunk_info.m_total_chunks); |
| 1190 |
|
| 1191 |
if (min_chunk > m_chunk_info.m_total_chunks) { |
| 1192 |
/* No more chunks left for current state. */ |
| 1193 |
return (0); |
| 1194 |
} |
| 1195 |
|
| 1196 |
/* Return the minimum unreserved chunk */ |
| 1197 |
auto ret_chunk = min_chunk; |
| 1198 |
|
| 1199 |
/* Mark the chunk reserved. The chunk must be unreserved. */ |
| 1200 |
ut_ad(!m_chunk_info.m_reserved_chunks[min_chunk]); |
| 1201 |
m_chunk_info.m_reserved_chunks[min_chunk] = true; |
| 1202 |
|
| 1203 |
/* Increase max reserved chunk if needed */ |
| 1204 |
if (max_chunk < min_chunk) { |
| 1205 |
max_chunk = min_chunk; |
| 1206 |
} |
| 1207 |
|
| 1208 |
ut_ad(max_chunk == m_chunk_info.m_reserved_chunks.get_max_set_bit()); |
| 1209 |
|
| 1210 |
/* Set the next unreserved chunk */ |
| 1211 |
while (m_chunk_info.m_reserved_chunks[min_chunk]) { |
| 1212 |
++min_chunk; |
| 1213 |
|
| 1214 |
/* Exit if all chunks are over */ |
| 1215 |
if (min_chunk > max_chunk || min_chunk > m_chunk_info.m_total_chunks) { |
| 1216 |
ut_ad(min_chunk > m_chunk_info.m_total_chunks || |
| 1217 |
!m_chunk_info.m_reserved_chunks[min_chunk]); |
| 1218 |
|
| 1219 |
break; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
return (ret_chunk); |
| 1224 |
} |
| 1225 |
|
| 1226 |
uint32_t Clone_Task_Manager::get_next_incomplete_chunk(uint32_t &block_num) { |
| 1227 |
block_num = 0; |
| 1228 |
|
| 1229 |
auto &chunks = m_chunk_info.m_incomplete_chunks; |
| 1230 |
|
| 1231 |
if (chunks.empty()) { |
| 1232 |
return (0); |
| 1233 |
} |
| 1234 |
|
| 1235 |
auto it = chunks.begin(); |
| 1236 |
|
| 1237 |
auto chunk_num = it->first; |
| 1238 |
|
| 1239 |
block_num = it->second; |
| 1240 |
|
| 1241 |
chunks.erase(it); |
| 1242 |
|
| 1243 |
return (chunk_num); |
| 1244 |
} |
| 1245 |
|
| 1246 |
int Clone_Task_Manager::reserve_next_chunk(Clone_Task *task, |
| 1247 |
uint32_t &ret_chunk, |
| 1248 |
uint32_t &ret_block) { |
| 1249 |
mutex_enter(&m_state_mutex); |
| 1250 |
ret_chunk = 0; |
| 1251 |
|
| 1252 |
/* Check for error from other tasks */ |
| 1253 |
auto err = handle_error_other_task(task->m_has_thd); |
| 1254 |
if (err != 0) { |
| 1255 |
mutex_exit(&m_state_mutex); |
| 1256 |
return (err); |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (process_inclomplete_chunk()) { |
| 1260 |
/* Get next incomplete chunk. */ |
| 1261 |
ret_chunk = get_next_incomplete_chunk(ret_block); |
| 1262 |
ut_ad(ret_chunk != 0); |
| 1263 |
|
| 1264 |
} else { |
| 1265 |
/* Get next unreserved chunk. */ |
| 1266 |
ret_block = 0; |
| 1267 |
ret_chunk = get_next_chunk(); |
| 1268 |
} |
| 1269 |
|
| 1270 |
reset_chunk(task); |
| 1271 |
mutex_exit(&m_state_mutex); |
| 1272 |
return (0); |
| 1273 |
} |
| 1274 |
|
| 1275 |
int Clone_Task_Manager::set_chunk(Clone_Task *task, Clone_Task_Meta *new_meta) { |
| 1276 |
auto cur_meta = &task->m_task_meta; |
| 1277 |
int err = 0; |
| 1278 |
|
| 1279 |
ut_ad(cur_meta->m_task_index == new_meta->m_task_index); |
| 1280 |
cur_meta->m_task_index = new_meta->m_task_index; |
| 1281 |
|
| 1282 |
/* Check if this is a new chunk */ |
| 1283 |
if (cur_meta->m_chunk_num != new_meta->m_chunk_num) { |
| 1284 |
mutex_enter(&m_state_mutex); |
| 1285 |
|
| 1286 |
/* Mark the current chunk reserved */ |
| 1287 |
m_chunk_info.m_reserved_chunks[new_meta->m_chunk_num] = true; |
| 1288 |
|
| 1289 |
/* Check and remove the chunk from incomplete chunk list. */ |
| 1290 |
auto &chunks = m_chunk_info.m_incomplete_chunks; |
| 1291 |
|
| 1292 |
auto key_value = chunks.find(new_meta->m_chunk_num); |
| 1293 |
|
| 1294 |
if (key_value != chunks.end()) { |
| 1295 |
ut_ad(key_value->second < new_meta->m_block_num); |
| 1296 |
chunks.erase(key_value); |
| 1297 |
} |
| 1298 |
|
| 1299 |
reset_chunk(task); |
| 1300 |
|
| 1301 |
/* Check for error from other tasks */ |
| 1302 |
err = handle_error_other_task(task->m_has_thd); |
| 1303 |
|
| 1304 |
mutex_exit(&m_state_mutex); |
| 1305 |
|
| 1306 |
cur_meta->m_chunk_num = new_meta->m_chunk_num; |
| 1307 |
|
| 1308 |
#ifdef UNIV_DEBUG |
| 1309 |
/* Network failure in the middle of a state */ |
| 1310 |
err = debug_restart(task, err, 1); |
| 1311 |
|
| 1312 |
/* Wait in the middle of state */ |
| 1313 |
debug_wait(cur_meta->m_chunk_num, task); |
| 1314 |
#endif /* UNIV_DEBUG */ |
| 1315 |
} |
| 1316 |
|
| 1317 |
cur_meta->m_block_num = new_meta->m_block_num; |
| 1318 |
|
| 1319 |
return (err); |
| 1320 |
} |
| 1321 |
|
| 1322 |
void Clone_Task_Manager::add_incomplete_chunk(Clone_Task *task) { |
| 1323 |
/* Track incomplete chunks during apply */ |
| 1324 |
if (m_clone_snapshot->is_copy()) { |
| 1325 |
return; |
| 1326 |
} |
| 1327 |
|
| 1328 |
auto &task_meta = task->m_task_meta; |
| 1329 |
|
| 1330 |
/* The task doesn't have any incomplete chunks */ |
| 1331 |
if (task_meta.m_chunk_num == 0) { |
| 1332 |
return; |
| 1333 |
} |
| 1334 |
|
| 1335 |
auto &chunks = m_chunk_info.m_incomplete_chunks; |
| 1336 |
|
| 1337 |
chunks[task_meta.m_chunk_num] = task_meta.m_block_num; |
| 1338 |
|
| 1339 |
ib::info(ER_IB_CLONE_RESTART) |
| 1340 |
<< "Clone Apply add incomplete Chunk = " << task_meta.m_chunk_num |
| 1341 |
<< " Block = " << task_meta.m_block_num |
| 1342 |
<< " Task = " << task_meta.m_task_index; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/** Print completed chunk information |
| 1346 |
@param[in] chunk_info chunk information */ |
| 1347 |
static void print_chunk_info(Chunk_Info *chunk_info) { |
| 1348 |
for (auto &chunk : chunk_info->m_incomplete_chunks) { |
| 1349 |
ib::info(ER_IB_CLONE_RESTART) |
| 1350 |
<< "Incomplete: Chunk = " << chunk.first << " Block = " << chunk.second; |
| 1351 |
} |
| 1352 |
|
| 1353 |
auto min = chunk_info->m_reserved_chunks.get_min_unset_bit(); |
| 1354 |
auto max = chunk_info->m_reserved_chunks.get_max_set_bit(); |
| 1355 |
|
| 1356 |
auto size = chunk_info->m_reserved_chunks.size_bits(); |
| 1357 |
|
| 1358 |
ib::info(ER_IB_CLONE_RESTART) |
| 1359 |
<< "Number of Chunks: " << size << " Min = " << min << " Max = " << max; |
| 1360 |
|
| 1361 |
ut_ad(min != max); |
| 1362 |
|
| 1363 |
if (max > min) { |
| 1364 |
ib::info(ER_IB_CLONE_RESTART) |
| 1365 |
<< "Reserved Chunk Information : " << min << " - " << max |
| 1366 |
<< " Chunks: " << max - min + 1; |
| 1367 |
|
| 1368 |
for (uint32_t index = min; index <= max;) { |
| 1369 |
uint32_t ind = 0; |
| 1370 |
|
| 1371 |
const int STR_SIZE = 64; |
| 1372 |
char str[STR_SIZE + 1]; |
| 1373 |
|
| 1374 |
while (index <= max && ind < STR_SIZE) { |
| 1375 |
str[ind] = chunk_info->m_reserved_chunks[index] ? '1' : '0'; |
| 1376 |
++index; |
| 1377 |
++ind; |
| 1378 |
} |
| 1379 |
|
| 1380 |
ut_ad(ind <= STR_SIZE); |
| 1381 |
str[ind] = '\0'; |
| 1382 |
|
| 1383 |
ib::info(ER_IB_CLONE_RESTART) << str; |
| 1384 |
} |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
void Clone_Task_Manager::reinit_apply_state(const byte *ref_loc, uint ref_len, |
| 1389 |
byte *&new_loc, uint &new_len, |
| 1390 |
uint &alloc_len) { |
| 1391 |
ut_ad(m_current_state != CLONE_SNAPSHOT_NONE); |
| 1392 |
ut_ad(!m_clone_snapshot->is_copy()); |
| 1393 |
|
| 1394 |
/* Only master task should be present */ |
| 1395 |
ut_ad(m_num_tasks == 1); |
| 1396 |
|
| 1397 |
/* Reset State transition information */ |
| 1398 |
reset_transition(); |
| 1399 |
|
| 1400 |
/* Reset Error information */ |
| 1401 |
reset_error(); |
| 1402 |
|
| 1403 |
/* Check if current state is finished and acknowledged */ |
| 1404 |
ut_ad(m_ack_state <= m_current_state); |
| 1405 |
|
| 1406 |
if (m_ack_state == m_current_state) { |
| 1407 |
++m_num_tasks_finished; |
| 1408 |
} |
| 1409 |
|
| 1410 |
++m_restart_count; |
| 1411 |
|
| 1412 |
switch (m_current_state) { |
| 1413 |
case CLONE_SNAPSHOT_INIT: |
| 1414 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Apply Restarting State: INIT"; |
| 1415 |
break; |
| 1416 |
|
| 1417 |
case CLONE_SNAPSHOT_FILE_COPY: |
| 1418 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1419 |
<< "Clone Apply Restarting State: FILE COPY"; |
| 1420 |
break; |
| 1421 |
|
| 1422 |
case CLONE_SNAPSHOT_PAGE_COPY: |
| 1423 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1424 |
<< "Clone Apply Restarting State: PAGE COPY"; |
| 1425 |
break; |
| 1426 |
|
| 1427 |
case CLONE_SNAPSHOT_REDO_COPY: |
| 1428 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1429 |
<< "Clone Apply Restarting State: REDO COPY"; |
| 1430 |
break; |
| 1431 |
|
| 1432 |
case CLONE_SNAPSHOT_DONE: |
| 1433 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Apply Restarting State: DONE"; |
| 1434 |
break; |
| 1435 |
|
| 1436 |
case CLONE_SNAPSHOT_NONE: |
| 1437 |
default: |
| 1438 |
ut_d(ut_error); |
| 1439 |
} |
| 1440 |
|
| 1441 |
if (m_current_state == CLONE_SNAPSHOT_INIT || |
| 1442 |
m_current_state == CLONE_SNAPSHOT_DONE || |
| 1443 |
m_current_state == CLONE_SNAPSHOT_NONE) { |
| 1444 |
new_loc = nullptr; |
| 1445 |
new_len = 0; |
| 1446 |
return; |
| 1447 |
} |
| 1448 |
|
| 1449 |
/* Add incomplete chunks from master task */ |
| 1450 |
auto task = get_task_by_index(0); |
| 1451 |
|
| 1452 |
add_incomplete_chunk(task); |
| 1453 |
|
| 1454 |
/* Reset task information */ |
| 1455 |
mutex_enter(&m_state_mutex); |
| 1456 |
reset_chunk(task); |
| 1457 |
mutex_exit(&m_state_mutex); |
| 1458 |
|
| 1459 |
/* Allocate for locator if required */ |
| 1460 |
Clone_Desc_Locator temp_locator; |
| 1461 |
|
| 1462 |
temp_locator.deserialize(ref_loc, ref_len, nullptr); |
| 1463 |
|
| 1464 |
/* Update current state information */ |
| 1465 |
temp_locator.m_state = m_current_state; |
| 1466 |
|
| 1467 |
/* Update sub-state information */ |
| 1468 |
temp_locator.m_metadata_transferred = m_transferred_file_meta; |
| 1469 |
|
| 1470 |
auto len = temp_locator.m_header.m_length; |
| 1471 |
len += static_cast<uint>(m_chunk_info.get_serialized_length(0)); |
| 1472 |
|
| 1473 |
if (len > alloc_len) { |
| 1474 |
/* Allocate for more for possible reuse */ |
| 1475 |
len = CLONE_DESC_MAX_BASE_LEN; |
| 1476 |
ut_ad(len >= temp_locator.m_header.m_length); |
| 1477 |
|
| 1478 |
len += static_cast<uint>(m_chunk_info.get_serialized_length( |
| 1479 |
static_cast<uint32_t>(CLONE_MAX_TASKS))); |
| 1480 |
|
| 1481 |
auto heap = m_clone_snapshot->lock_heap(); |
| 1482 |
|
| 1483 |
new_loc = static_cast<byte *>(mem_heap_zalloc(heap, len)); |
| 1484 |
alloc_len = len; |
| 1485 |
|
| 1486 |
m_clone_snapshot->release_heap(heap); |
| 1487 |
} |
| 1488 |
|
| 1489 |
new_len = alloc_len; |
| 1490 |
|
| 1491 |
temp_locator.serialize(new_loc, new_len, &m_chunk_info, nullptr); |
| 1492 |
|
| 1493 |
print_chunk_info(&m_chunk_info); |
| 1494 |
} |
| 1495 |
|
| 1496 |
void Clone_Task_Manager::reinit_copy_state(const byte *loc, uint loc_len) { |
| 1497 |
ut_ad(m_clone_snapshot->is_copy()); |
| 1498 |
ut_ad(m_num_tasks == 0); |
| 1499 |
|
| 1500 |
mutex_enter(&m_state_mutex); |
| 1501 |
|
| 1502 |
/* Reset State transition information */ |
| 1503 |
reset_transition(); |
| 1504 |
|
| 1505 |
/* Reset Error information */ |
| 1506 |
reset_error(); |
| 1507 |
|
| 1508 |
++m_restart_count; |
| 1509 |
|
| 1510 |
switch (m_current_state) { |
| 1511 |
case CLONE_SNAPSHOT_INIT: |
| 1512 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Restarting State: INIT"; |
| 1513 |
break; |
| 1514 |
|
| 1515 |
case CLONE_SNAPSHOT_FILE_COPY: |
| 1516 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Restarting State: FILE COPY"; |
| 1517 |
break; |
| 1518 |
|
| 1519 |
case CLONE_SNAPSHOT_PAGE_COPY: |
| 1520 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Restarting State: PAGE COPY"; |
| 1521 |
break; |
| 1522 |
|
| 1523 |
case CLONE_SNAPSHOT_REDO_COPY: |
| 1524 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Restarting State: REDO COPY"; |
| 1525 |
break; |
| 1526 |
|
| 1527 |
case CLONE_SNAPSHOT_DONE: |
| 1528 |
ib::info(ER_IB_CLONE_RESTART) << "Clone Restarting State: DONE"; |
| 1529 |
break; |
| 1530 |
|
| 1531 |
case CLONE_SNAPSHOT_NONE: |
| 1532 |
default: |
| 1533 |
ut_d(ut_error); |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (m_current_state == CLONE_SNAPSHOT_NONE) { |
| 1537 |
mutex_exit(&m_state_mutex); |
| 1538 |
ut_d(ut_error); |
| 1539 |
ut_o(return); |
| 1540 |
} |
| 1541 |
|
| 1542 |
/* Reset to beginning of current state */ |
| 1543 |
init_state(); |
| 1544 |
|
| 1545 |
/* Compare local and remote state */ |
| 1546 |
Clone_Desc_Locator temp_locator; |
| 1547 |
|
| 1548 |
temp_locator.deserialize(loc, loc_len, nullptr); |
| 1549 |
|
| 1550 |
/* If Local state is ahead, we must have finished the |
| 1551 |
previous state confirmed by ACK. It is enough to |
| 1552 |
start from current state. */ |
| 1553 |
if (temp_locator.m_state != m_current_state) { |
| 1554 |
#ifdef UNIV_DEBUG |
| 1555 |
/* Current state could be just one state ahead */ |
| 1556 |
if (temp_locator.m_state == CLONE_SNAPSHOT_INIT) { |
| 1557 |
ut_ad(m_current_state == CLONE_SNAPSHOT_FILE_COPY); |
| 1558 |
|
| 1559 |
} else if (temp_locator.m_state == CLONE_SNAPSHOT_FILE_COPY) { |
| 1560 |
ut_ad(m_current_state == CLONE_SNAPSHOT_PAGE_COPY); |
| 1561 |
|
| 1562 |
} else if (temp_locator.m_state == CLONE_SNAPSHOT_PAGE_COPY) { |
| 1563 |
ut_ad(m_current_state == CLONE_SNAPSHOT_REDO_COPY); |
| 1564 |
|
| 1565 |
} else if (temp_locator.m_state == CLONE_SNAPSHOT_REDO_COPY) { |
| 1566 |
ut_ad(m_current_state == CLONE_SNAPSHOT_DONE); |
| 1567 |
|
| 1568 |
} else { |
| 1569 |
ut_d(ut_error); |
| 1570 |
} |
| 1571 |
#endif /* UNIV_DEBUG */ |
| 1572 |
|
| 1573 |
/* Apply state is behind. Need to send state metadata */ |
| 1574 |
m_send_state_meta = true; |
| 1575 |
|
| 1576 |
mutex_exit(&m_state_mutex); |
| 1577 |
return; |
| 1578 |
} |
| 1579 |
|
| 1580 |
m_send_state_meta = false; |
| 1581 |
m_transferred_file_meta = temp_locator.m_metadata_transferred; |
| 1582 |
|
| 1583 |
/* Set progress information for current state */ |
| 1584 |
temp_locator.deserialize(loc, loc_len, &m_chunk_info); |
| 1585 |
|
| 1586 |
m_chunk_info.init_chunk_nums(); |
| 1587 |
|
| 1588 |
mutex_exit(&m_state_mutex); |
| 1589 |
|
| 1590 |
print_chunk_info(&m_chunk_info); |
| 1591 |
} |
| 1592 |
|
| 1593 |
void Clone_Task_Manager::init_state() { |
| 1594 |
ut_ad(mutex_own(&m_state_mutex)); |
| 1595 |
|
| 1596 |
auto num_chunks = m_clone_snapshot->get_num_chunks(); |
| 1597 |
|
| 1598 |
auto heap = m_clone_snapshot->lock_heap(); |
| 1599 |
|
| 1600 |
m_chunk_info.m_reserved_chunks.reset(num_chunks, heap); |
| 1601 |
|
| 1602 |
m_clone_snapshot->release_heap(heap); |
| 1603 |
|
| 1604 |
m_chunk_info.m_incomplete_chunks.clear(); |
| 1605 |
|
| 1606 |
m_chunk_info.m_min_unres_chunk = 1; |
| 1607 |
ut_ad(m_chunk_info.m_reserved_chunks.get_min_unset_bit() == 1); |
| 1608 |
|
| 1609 |
m_chunk_info.m_max_res_chunk = 0; |
| 1610 |
ut_ad(m_chunk_info.m_reserved_chunks.get_max_set_bit() == 0); |
| 1611 |
|
| 1612 |
m_chunk_info.m_total_chunks = num_chunks; |
| 1613 |
} |
| 1614 |
|
| 1615 |
void Clone_Task_Manager::ack_state(const Clone_Desc_State *state_desc) { |
| 1616 |
mutex_enter(&m_state_mutex); |
| 1617 |
|
| 1618 |
m_ack_state = state_desc->m_state; |
| 1619 |
ut_ad(m_current_state == m_ack_state); |
| 1620 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1621 |
<< "Clone set state change ACK: " << m_ack_state; |
| 1622 |
|
| 1623 |
mutex_exit(&m_state_mutex); |
| 1624 |
} |
| 1625 |
|
| 1626 |
int Clone_Task_Manager::wait_ack(Clone_Handle *clone, Clone_Task *task, |
| 1627 |
Ha_clone_cbk *callback) { |
| 1628 |
mutex_enter(&m_state_mutex); |
| 1629 |
|
| 1630 |
++m_num_tasks_finished; |
| 1631 |
|
| 1632 |
/* All chunks are finished */ |
| 1633 |
reset_chunk(task); |
| 1634 |
|
| 1635 |
if (!task->m_is_master) { |
| 1636 |
mutex_exit(&m_state_mutex); |
| 1637 |
return (0); |
| 1638 |
} |
| 1639 |
|
| 1640 |
int err = 0; |
| 1641 |
|
| 1642 |
if (m_current_state != m_ack_state) { |
| 1643 |
bool is_timeout = false; |
| 1644 |
int alert_count = 0; |
| 1645 |
err = Clone_Sys::wait_default( |
| 1646 |
[&](bool alert, bool &result) { |
| 1647 |
ut_ad(mutex_own(&m_state_mutex)); |
| 1648 |
result = (m_current_state != m_ack_state); |
| 1649 |
|
| 1650 |
/* Check for error from other tasks */ |
| 1651 |
err = handle_error_other_task(task->m_has_thd); |
| 1652 |
|
| 1653 |
if (err == 0 && result && alert) { |
| 1654 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 1655 |
if (++alert_count == 12) { |
| 1656 |
alert_count = 0; |
| 1657 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master waiting " |
| 1658 |
"for state change ACK "; |
| 1659 |
} |
| 1660 |
err = clone->send_keep_alive(task, callback); |
| 1661 |
} |
| 1662 |
return (err); |
| 1663 |
}, |
| 1664 |
&m_state_mutex, is_timeout); |
| 1665 |
|
| 1666 |
/* Wait too long */ |
| 1667 |
if (err == 0 && is_timeout) { |
| 1668 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Master wait for state change ACK" |
| 1669 |
" timed out"; |
| 1670 |
|
| 1671 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1672 |
"Innodb clone state ack wait too long"); |
| 1673 |
|
| 1674 |
err = ER_INTERNAL_ERROR; |
| 1675 |
ut_d(ut_error); |
| 1676 |
} |
| 1677 |
} |
| 1678 |
mutex_exit(&m_state_mutex); |
| 1679 |
|
| 1680 |
if (err == 0) { |
| 1681 |
ib::info(ER_IB_CLONE_OPERATION) << "Clone Master received state change ACK"; |
| 1682 |
} |
| 1683 |
|
| 1684 |
return (err); |
| 1685 |
} |
| 1686 |
|
| 1687 |
int Clone_Task_Manager::finish_state(Clone_Task *task) { |
| 1688 |
mutex_enter(&m_state_mutex); |
| 1689 |
|
| 1690 |
if (task->m_is_master) { |
| 1691 |
/* Check if ACK was sent before restart */ |
| 1692 |
if (m_ack_state != m_current_state) { |
| 1693 |
ut_ad(m_ack_state < m_current_state); |
| 1694 |
++m_num_tasks_finished; |
| 1695 |
} else { |
| 1696 |
ut_ad(m_restart_count > 0); |
| 1697 |
} |
| 1698 |
m_ack_state = m_current_state; |
| 1699 |
|
| 1700 |
} else { |
| 1701 |
++m_num_tasks_finished; |
| 1702 |
} |
| 1703 |
|
| 1704 |
/* All chunks are finished */ |
| 1705 |
reset_chunk(task); |
| 1706 |
|
| 1707 |
/* Check for error from other tasks */ |
| 1708 |
auto err = handle_error_other_task(task->m_has_thd); |
| 1709 |
|
| 1710 |
if (!task->m_is_master || err != 0) { |
| 1711 |
mutex_exit(&m_state_mutex); |
| 1712 |
return (err); |
| 1713 |
} |
| 1714 |
|
| 1715 |
ut_ad(task->m_is_master); |
| 1716 |
|
| 1717 |
#ifdef UNIV_DEBUG |
| 1718 |
/* Wait before ending state, if needed */ |
| 1719 |
if (!task->m_ignore_sync) { |
| 1720 |
mutex_exit(&m_state_mutex); |
| 1721 |
debug_wait(0, task); |
| 1722 |
mutex_enter(&m_state_mutex); |
| 1723 |
} |
| 1724 |
#endif /* UNIV_DEBUG */ |
| 1725 |
|
| 1726 |
if (m_num_tasks_finished < m_num_tasks) { |
| 1727 |
bool is_timeout = false; |
| 1728 |
int alert_count = 0; |
| 1729 |
err = Clone_Sys::wait_default( |
| 1730 |
[&](bool alert, bool &result) { |
| 1731 |
ut_ad(mutex_own(&m_state_mutex)); |
| 1732 |
result = (m_num_tasks_finished < m_num_tasks); |
| 1733 |
|
| 1734 |
/* Check for error from other tasks */ |
| 1735 |
err = handle_error_other_task(task->m_has_thd); |
| 1736 |
|
| 1737 |
if (err == 0 && result && alert) { |
| 1738 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 1739 |
if (++alert_count == 12) { |
| 1740 |
alert_count = 0; |
| 1741 |
ib::info(ER_IB_CLONE_TIMEOUT) |
| 1742 |
<< "Clone Apply Master waiting for " |
| 1743 |
"workers before sending ACK." |
| 1744 |
<< " Total = " << m_num_tasks |
| 1745 |
<< " Finished = " << m_num_tasks_finished; |
| 1746 |
} |
| 1747 |
} |
| 1748 |
return (err); |
| 1749 |
}, |
| 1750 |
&m_state_mutex, is_timeout); |
| 1751 |
|
| 1752 |
if (err == 0 && is_timeout) { |
| 1753 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone Apply Master wait timed out"; |
| 1754 |
|
| 1755 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 1756 |
"Clone Apply Master wait timed out before sending ACK"); |
| 1757 |
|
| 1758 |
err = ER_INTERNAL_ERROR; |
| 1759 |
ut_d(ut_error); |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
mutex_exit(&m_state_mutex); |
| 1764 |
return (err); |
| 1765 |
} |
| 1766 |
|
| 1767 |
int Clone_Task_Manager::change_state(Clone_Task *task, |
| 1768 |
Clone_Desc_State *state_desc, |
| 1769 |
Snapshot_State new_state, |
| 1770 |
Clone_Alert_Func cbk, uint &num_wait) { |
| 1771 |
mutex_enter(&m_state_mutex); |
| 1772 |
|
| 1773 |
num_wait = 0; |
| 1774 |
|
| 1775 |
/* Check for error from other tasks */ |
| 1776 |
auto err = handle_error_other_task(task->m_has_thd); |
| 1777 |
|
| 1778 |
if (err != 0) { |
| 1779 |
mutex_exit(&m_state_mutex); |
| 1780 |
return (err); |
| 1781 |
} |
| 1782 |
|
| 1783 |
/* First requesting task needs to initiate the state transition. */ |
| 1784 |
if (!in_transit_state()) { |
| 1785 |
m_num_tasks_transit = m_num_tasks; |
| 1786 |
m_next_state = new_state; |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* Master needs to wait for all other tasks. */ |
| 1790 |
if (task->m_is_master && m_num_tasks_transit > 1) { |
| 1791 |
num_wait = m_num_tasks_transit; |
| 1792 |
|
| 1793 |
mutex_exit(&m_state_mutex); |
| 1794 |
return (0); |
| 1795 |
} |
| 1796 |
|
| 1797 |
/* Need to wait for transition to next state */ |
| 1798 |
if (!task->m_is_master) { |
| 1799 |
/* Move the current task over to the next state */ |
| 1800 |
ut_ad(m_num_tasks_transit > 0); |
| 1801 |
--m_num_tasks_transit; |
| 1802 |
|
| 1803 |
num_wait = m_num_tasks_transit; |
| 1804 |
ut_ad(num_wait > 0); |
| 1805 |
|
| 1806 |
mutex_exit(&m_state_mutex); |
| 1807 |
return (0); |
| 1808 |
} |
| 1809 |
|
| 1810 |
/* Last task requesting the state change. All other tasks have |
| 1811 |
already moved over to next state and waiting for the transition |
| 1812 |
to complete. Now it is safe to do the snapshot state transition. */ |
| 1813 |
|
| 1814 |
ut_ad(task->m_is_master); |
| 1815 |
mutex_exit(&m_state_mutex); |
| 1816 |
|
| 1817 |
if (m_clone_snapshot->is_copy()) { |
| 1818 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1819 |
<< "Clone State Change : Number of tasks = " << m_num_tasks; |
| 1820 |
} else { |
| 1821 |
ib::info(ER_IB_CLONE_OPERATION) |
| 1822 |
<< "Clone Apply State Change : Number of tasks = " << m_num_tasks; |
| 1823 |
} |
| 1824 |
|
| 1825 |
err = m_clone_snapshot->change_state(state_desc, m_next_state, |
| 1826 |
task->m_current_buffer, |
| 1827 |
task->m_buffer_alloc_len, cbk); |
| 1828 |
|
| 1829 |
if (err != 0) { |
| 1830 |
return (err); |
| 1831 |
} |
| 1832 |
|
| 1833 |
mutex_enter(&m_state_mutex); |
| 1834 |
|
| 1835 |
/* Check for error from other tasks. Must finish the state transition |
| 1836 |
even in case of an error. */ |
| 1837 |
err = handle_error_other_task(task->m_has_thd); |
| 1838 |
|
| 1839 |
m_current_state = m_next_state; |
| 1840 |
m_next_state = CLONE_SNAPSHOT_NONE; |
| 1841 |
|
| 1842 |
--m_num_tasks_transit; |
| 1843 |
/* In case of error, the other tasks might have exited. */ |
| 1844 |
ut_ad(m_num_tasks_transit == 0 || err != 0); |
| 1845 |
m_num_tasks_transit = 0; |
| 1846 |
|
| 1847 |
/* For restart, m_num_tasks_finished may not be up to date */ |
| 1848 |
ut_ad(m_num_tasks_finished == m_num_tasks || err != 0); |
| 1849 |
m_num_tasks_finished = 0; |
| 1850 |
|
| 1851 |
ut_d(task->m_ignore_sync = false); |
| 1852 |
ut_d(task->m_debug_counter = 0); |
| 1853 |
|
| 1854 |
/* Initialize next state after transition. */ |
| 1855 |
init_state(); |
| 1856 |
|
| 1857 |
mutex_exit(&m_state_mutex); |
| 1858 |
|
| 1859 |
return (err); |
| 1860 |
} |
| 1861 |
|
| 1862 |
int Clone_Task_Manager::check_state(Clone_Task *task, Snapshot_State new_state, |
| 1863 |
bool exit_on_wait, int in_err, |
| 1864 |
uint32_t &num_wait) { |
| 1865 |
mutex_enter(&m_state_mutex); |
| 1866 |
|
| 1867 |
num_wait = 0; |
| 1868 |
|
| 1869 |
if (in_err != 0) { |
| 1870 |
/* Save error for other tasks */ |
| 1871 |
if (m_saved_error == 0) { |
| 1872 |
m_saved_error = in_err; |
| 1873 |
} |
| 1874 |
/* Mark transit incomplete */ |
| 1875 |
if (in_transit_state()) { |
| 1876 |
++m_num_tasks_transit; |
| 1877 |
} |
| 1878 |
mutex_exit(&m_state_mutex); |
| 1879 |
return (in_err); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* Check for error from other tasks */ |
| 1883 |
auto err = handle_error_other_task(task->m_has_thd); |
| 1884 |
|
| 1885 |
if (err != 0) { |
| 1886 |
mutex_exit(&m_state_mutex); |
| 1887 |
return (err); |
| 1888 |
} |
| 1889 |
|
| 1890 |
/* Check if current transition is still in progress. */ |
| 1891 |
if (in_transit_state() && new_state == m_next_state) { |
| 1892 |
num_wait = m_num_tasks_transit; |
| 1893 |
|
| 1894 |
ut_ad(num_wait > 0); |
| 1895 |
|
| 1896 |
if (exit_on_wait) { |
| 1897 |
/* Mark error for other tasks */ |
| 1898 |
m_saved_error = ER_INTERNAL_ERROR; |
| 1899 |
/* Mark transit incomplete */ |
| 1900 |
++m_num_tasks_transit; |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
mutex_exit(&m_state_mutex); |
| 1905 |
|
| 1906 |
return (0); |
| 1907 |
} |
| 1908 |
|
| 1909 |
Clone_Handle::Clone_Handle(Clone_Handle_Type handle_type, uint clone_version, |
| 1910 |
uint clone_index) |
| 1911 |
: m_clone_handle_type(handle_type), |
| 1912 |
m_clone_handle_state(CLONE_STATE_INIT), |
| 1913 |
m_clone_locator(), |
| 1914 |
m_locator_length(), |
| 1915 |
m_restart_loc(), |
| 1916 |
m_restart_loc_len(), |
| 1917 |
m_clone_desc_version(clone_version), |
| 1918 |
m_clone_arr_index(clone_index), |
| 1919 |
m_clone_id(), |
| 1920 |
m_ref_count(), |
| 1921 |
m_allow_restart(false), |
| 1922 |
m_abort_ddl(false), |
| 1923 |
m_clone_dir(), |
| 1924 |
m_clone_task_manager() { |
| 1925 |
mutex_create(LATCH_ID_CLONE_TASK, m_clone_task_manager.get_mutex()); |
| 1926 |
|
| 1927 |
Clone_Desc_Locator loc_desc; |
| 1928 |
loc_desc.init(0, 0, CLONE_SNAPSHOT_NONE, clone_version, clone_index); |
| 1929 |
|
| 1930 |
auto loc = &m_version_locator[0]; |
| 1931 |
uint len = CLONE_DESC_MAX_BASE_LEN; |
| 1932 |
|
| 1933 |
memset(loc, 0, CLONE_DESC_MAX_BASE_LEN); |
| 1934 |
|
| 1935 |
loc_desc.serialize(loc, len, nullptr, nullptr); |
| 1936 |
|
| 1937 |
ut_ad(len <= CLONE_DESC_MAX_BASE_LEN); |
| 1938 |
} |
| 1939 |
|
| 1940 |
Clone_Handle::~Clone_Handle() { |
| 1941 |
mutex_free(m_clone_task_manager.get_mutex()); |
| 1942 |
|
| 1943 |
if (!is_init()) { |
| 1944 |
clone_sys->detach_snapshot(m_clone_task_manager.get_snapshot(), |
| 1945 |
m_clone_handle_type); |
| 1946 |
} |
| 1947 |
ut_ad(m_ref_count == 0); |
| 1948 |
} |
| 1949 |
|
| 1950 |
int Clone_Handle::create_clone_directory() { |
| 1951 |
ut_ad(!is_copy_clone()); |
| 1952 |
dberr_t db_err = DB_SUCCESS; |
| 1953 |
std::string file_name; |
| 1954 |
|
| 1955 |
if (!replace_datadir()) { |
| 1956 |
/* Create data directory, if we not replacing the current one. */ |
| 1957 |
db_err = os_file_create_subdirs_if_needed(m_clone_dir); |
| 1958 |
if (db_err == DB_SUCCESS) { |
| 1959 |
auto status = os_file_create_directory(m_clone_dir, false); |
| 1960 |
/* Create mysql schema directory. */ |
| 1961 |
file_name.assign(m_clone_dir); |
| 1962 |
file_name.append(OS_PATH_SEPARATOR_STR); |
| 1963 |
if (status) { |
| 1964 |
file_name.append("mysql"); |
| 1965 |
status = os_file_create_directory(file_name.c_str(), true); |
| 1966 |
} |
| 1967 |
if (!status) { |
| 1968 |
db_err = DB_ERROR; |
| 1969 |
} |
| 1970 |
} |
| 1971 |
file_name.assign(m_clone_dir); |
| 1972 |
file_name.append(OS_PATH_SEPARATOR_STR); |
| 1973 |
} |
| 1974 |
|
| 1975 |
/* Create clone status directory. */ |
| 1976 |
if (db_err == DB_SUCCESS) { |
| 1977 |
file_name.append(CLONE_FILES_DIR); |
| 1978 |
auto status = os_file_create_directory(file_name.c_str(), false); |
| 1979 |
if (!status) { |
| 1980 |
db_err = DB_ERROR; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
/* Check and report error. */ |
| 1984 |
if (db_err != DB_SUCCESS) { |
| 1985 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 1986 |
my_error(ER_CANT_CREATE_DB, MYF(0), m_clone_dir, errno, |
| 1987 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 1988 |
|
| 1989 |
return (ER_CANT_CREATE_DB); |
| 1990 |
} |
| 1991 |
return (0); |
| 1992 |
} |
| 1993 |
|
| 1994 |
int Clone_Handle::init(const byte *ref_loc, uint ref_len, Ha_clone_type type, |
| 1995 |
const char *data_dir) { |
| 1996 |
uint64_t snapshot_id; |
| 1997 |
Clone_Snapshot *snapshot; |
| 1998 |
|
| 1999 |
m_clone_dir = data_dir; |
| 2000 |
|
| 2001 |
bool enable_monitor = true; |
| 2002 |
|
| 2003 |
/* Generate unique clone identifiers for copy clone handle. */ |
| 2004 |
if (is_copy_clone()) { |
| 2005 |
m_clone_id = clone_sys->get_next_id(); |
| 2006 |
snapshot_id = clone_sys->get_next_id(); |
| 2007 |
|
| 2008 |
/* For local clone, monitor while applying data. */ |
| 2009 |
if (ref_loc == nullptr) { |
| 2010 |
enable_monitor = false; |
| 2011 |
} |
| 2012 |
|
| 2013 |
} else { |
| 2014 |
/* We don't provision instance on which active clone is running. */ |
| 2015 |
if (replace_datadir() && clone_sys->check_active_clone(false)) { |
| 2016 |
my_error(ER_CLONE_TOO_MANY_CONCURRENT_CLONES, MYF(0), MAX_CLONES); |
| 2017 |
return (ER_CLONE_TOO_MANY_CONCURRENT_CLONES); |
| 2018 |
} |
| 2019 |
/* Return keeping the clone in INIT state. The locator |
| 2020 |
would only have the version information. */ |
| 2021 |
if (ref_loc == nullptr) { |
| 2022 |
return (0); |
| 2023 |
} |
| 2024 |
|
| 2025 |
auto err = create_clone_directory(); |
| 2026 |
if (err != 0) { |
| 2027 |
return (err); |
| 2028 |
} |
| 2029 |
|
| 2030 |
/* Set clone identifiers from reference locator for apply clone |
| 2031 |
handle. The reference locator is from copy clone handle. */ |
| 2032 |
Clone_Desc_Locator loc_desc; |
| 2033 |
|
| 2034 |
loc_desc.deserialize(ref_loc, ref_len, nullptr); |
| 2035 |
|
| 2036 |
m_clone_id = loc_desc.m_clone_id; |
| 2037 |
snapshot_id = loc_desc.m_snapshot_id; |
| 2038 |
|
| 2039 |
ut_ad(m_clone_id != CLONE_LOC_INVALID_ID); |
| 2040 |
ut_ad(snapshot_id != CLONE_LOC_INVALID_ID); |
| 2041 |
} |
| 2042 |
|
| 2043 |
/* Create and attach to snapshot. */ |
| 2044 |
auto err = clone_sys->attach_snapshot(m_clone_handle_type, type, snapshot_id, |
| 2045 |
enable_monitor, snapshot); |
| 2046 |
|
| 2047 |
if (err != 0) { |
| 2048 |
return (err); |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* Initialize clone task manager. */ |
| 2052 |
m_clone_task_manager.init(snapshot); |
| 2053 |
|
| 2054 |
m_clone_handle_state = CLONE_STATE_ACTIVE; |
| 2055 |
|
| 2056 |
return (0); |
| 2057 |
} |
| 2058 |
|
| 2059 |
byte *Clone_Handle::get_locator(uint &loc_len) { |
| 2060 |
Clone_Desc_Locator loc_desc; |
| 2061 |
|
| 2062 |
/* Return version locator during initialization. */ |
| 2063 |
if (is_init()) { |
| 2064 |
loc_len = CLONE_DESC_MAX_BASE_LEN; |
| 2065 |
return (&m_version_locator[0]); |
| 2066 |
} |
| 2067 |
|
| 2068 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 2069 |
|
| 2070 |
auto heap = snapshot->lock_heap(); |
| 2071 |
|
| 2072 |
build_descriptor(&loc_desc); |
| 2073 |
|
| 2074 |
loc_desc.serialize(m_clone_locator, m_locator_length, nullptr, heap); |
| 2075 |
|
| 2076 |
loc_len = m_locator_length; |
| 2077 |
|
| 2078 |
snapshot->release_heap(heap); |
| 2079 |
|
| 2080 |
return (m_clone_locator); |
| 2081 |
} |
| 2082 |
|
| 2083 |
void Clone_Handle::build_descriptor(Clone_Desc_Locator *loc_desc) { |
| 2084 |
Clone_Snapshot *snapshot; |
| 2085 |
uint64_t snapshot_id = CLONE_LOC_INVALID_ID; |
| 2086 |
Snapshot_State state = CLONE_SNAPSHOT_NONE; |
| 2087 |
|
| 2088 |
snapshot = m_clone_task_manager.get_snapshot(); |
| 2089 |
|
| 2090 |
if (snapshot) { |
| 2091 |
state = snapshot->get_state(); |
| 2092 |
snapshot_id = snapshot->get_id(); |
| 2093 |
} |
| 2094 |
|
| 2095 |
loc_desc->init(m_clone_id, snapshot_id, state, m_clone_desc_version, |
| 2096 |
m_clone_arr_index); |
| 2097 |
} |
| 2098 |
|
| 2099 |
bool Clone_Handle::drop_task(THD *thd, uint task_id, bool &is_master) { |
| 2100 |
/* No task is added in INIT state. The drop task is still called and |
| 2101 |
should be ignored. */ |
| 2102 |
if (is_init()) { |
| 2103 |
/* Only relevant for apply clone master */ |
| 2104 |
ut_ad(!is_copy_clone()); |
| 2105 |
ut_ad(task_id == 0); |
| 2106 |
is_master = true; |
| 2107 |
return (false); |
| 2108 |
} |
| 2109 |
/* Cannot be in IDLE state as master waits for tasks to drop before idling */ |
| 2110 |
ut_ad(!is_idle()); |
| 2111 |
|
| 2112 |
/* Close and reset file related information */ |
| 2113 |
auto task = m_clone_task_manager.get_task_by_index(task_id); |
| 2114 |
|
| 2115 |
close_file(task); |
| 2116 |
|
| 2117 |
ut_ad(mutex_own(clone_sys->get_mutex())); |
| 2118 |
mutex_exit(clone_sys->get_mutex()); |
| 2119 |
|
| 2120 |
auto wait_restart = m_clone_task_manager.drop_task(thd, task_id, is_master); |
| 2121 |
mutex_enter(clone_sys->get_mutex()); |
| 2122 |
|
| 2123 |
/* Need to wait for restart, if network error */ |
| 2124 |
if (is_copy_clone() && m_allow_restart && wait_restart) { |
| 2125 |
ut_ad(is_master); |
| 2126 |
return (true); |
| 2127 |
} |
| 2128 |
|
| 2129 |
return (false); |
| 2130 |
} |
| 2131 |
|
| 2132 |
int Clone_Handle::move_to_next_state(Clone_Task *task, Ha_clone_cbk *callback, |
| 2133 |
Clone_Desc_State *state_desc) { |
| 2134 |
auto snapshot = m_clone_task_manager.get_snapshot(); |
| 2135 |
/* Use input state only for apply. */ |
| 2136 |
auto next_state = |
| 2137 |
is_copy_clone() ? snapshot->get_next_state() : state_desc->m_state; |
| 2138 |
|
| 2139 |
Clone_Alert_Func alert_callback; |
| 2140 |
|
| 2141 |
if (is_copy_clone()) { |
| 2142 |
/* Send Keep alive to recipient during long wait. */ |
| 2143 |
alert_callback = [&]() { |
| 2144 |
auto err = send_keep_alive(task, callback); |
| 2145 |
return (err); |
| 2146 |
}; |
| 2147 |
} |
| 2148 |
|
| 2149 |
/* Move to new state */ |
| 2150 |
uint num_wait = 0; |
| 2151 |
auto err = m_clone_task_manager.change_state(task, state_desc, next_state, |
| 2152 |
alert_callback, num_wait); |
| 2153 |
|
| 2154 |
/* Need to wait for all other tasks to move over, if any. */ |
| 2155 |
if (num_wait > 0) { |
| 2156 |
bool is_timeout = false; |
| 2157 |
int alert_count = 0; |
| 2158 |
err = Clone_Sys::wait_default( |
| 2159 |
[&](bool alert, bool &result) { |
| 2160 |
/* For multi threaded clone, master task does the state change. */ |
| 2161 |
if (task->m_is_master) { |
| 2162 |
err = m_clone_task_manager.change_state( |
| 2163 |
task, state_desc, next_state, alert_callback, num_wait); |
| 2164 |
} else { |
| 2165 |
err = m_clone_task_manager.check_state(task, next_state, false, 0, |
| 2166 |
num_wait); |
| 2167 |
} |
| 2168 |
result = (num_wait > 0); |
| 2169 |
|
| 2170 |
if (err == 0 && result && alert) { |
| 2171 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 2172 |
if (++alert_count == 12) { |
| 2173 |
alert_count = 0; |
| 2174 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone: master state change " |
| 2175 |
"waiting for workers"; |
| 2176 |
} |
| 2177 |
if (is_copy_clone()) { |
| 2178 |
err = send_keep_alive(task, callback); |
| 2179 |
} |
| 2180 |
} |
| 2181 |
return (err); |
| 2182 |
}, |
| 2183 |
nullptr, is_timeout); |
| 2184 |
|
| 2185 |
if (err == 0 && !is_timeout) { |
| 2186 |
return (0); |
| 2187 |
} |
| 2188 |
|
| 2189 |
if (!task->m_is_master) { |
| 2190 |
/* Exit from state transition */ |
| 2191 |
err = m_clone_task_manager.check_state(task, next_state, is_timeout, err, |
| 2192 |
num_wait); |
| 2193 |
if (err != 0 || num_wait == 0) { |
| 2194 |
return (err); |
| 2195 |
} |
| 2196 |
} |
| 2197 |
|
| 2198 |
if (err == 0 && is_timeout) { |
| 2199 |
ib::info(ER_IB_CLONE_TIMEOUT) << "Clone: state change: " |
| 2200 |
"wait for other tasks timed out"; |
| 2201 |
|
| 2202 |
my_error(ER_INTERNAL_ERROR, MYF(0), |
| 2203 |
"Clone: state change wait for other tasks timed out: " |
| 2204 |
"Wait too long for state transition"); |
| 2205 |
ut_d(ut_error); |
| 2206 |
ut_o(return (ER_INTERNAL_ERROR)); |
| 2207 |
} |
| 2208 |
} |
| 2209 |
return (err); |
| 2210 |
} |
| 2211 |
|
| 2212 |
void Clone_Handle::set_abort() { |
| 2213 |
set_state(CLONE_STATE_ABORT); |
| 2214 |
|
| 2215 |
Clone_Snapshot *snapshot = m_clone_task_manager.get_snapshot(); |
| 2216 |
|
| 2217 |
/* Clone is set to abort state and snapshot can never be reused. It is |
| 2218 |
safe to mark the snapshot aborted to let any waiting DDL exit. There |
| 2219 |
could be other tasks on their way to exit and we should not change |
| 2220 |
the snapshot state yet. */ |
| 2221 |
if (snapshot != nullptr) { |
| 2222 |
snapshot->set_abort(); |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
int Clone_Handle::open_file(Clone_Task *task, const Clone_file_ctx *file_ctx, |
| 2227 |
ulint file_type, bool create_file, |
| 2228 |
File_init_cbk &init_cbk) { |
| 2229 |
os_file_type_t type; |
| 2230 |
bool exists; |
| 2231 |
std::string file_name; |
| 2232 |
|
| 2233 |
file_ctx->get_file_name(file_name); |
| 2234 |
|
| 2235 |
/* Check if file exists */ |
| 2236 |
auto status = os_file_status(file_name.c_str(), &exists, &type); |
| 2237 |
|
| 2238 |
if (!status) { |
| 2239 |
return (0); |
| 2240 |
} |
| 2241 |
|
| 2242 |
ulint option; |
| 2243 |
bool read_only; |
| 2244 |
|
| 2245 |
if (create_file) { |
| 2246 |
option = exists ? OS_FILE_OPEN : OS_FILE_CREATE_PATH; |
| 2247 |
read_only = false; |
| 2248 |
} else { |
| 2249 |
ut_ad(exists); |
| 2250 |
option = OS_FILE_OPEN; |
| 2251 |
read_only = true; |
| 2252 |
} |
| 2253 |
|
| 2254 |
option |= OS_FILE_ON_ERROR_NO_EXIT; |
| 2255 |
bool success = false; |
| 2256 |
|
| 2257 |
auto handle = os_file_create(innodb_clone_file_key, file_name.c_str(), option, |
| 2258 |
file_type, read_only, &success); |
| 2259 |
|
| 2260 |
int err = 0; |
| 2261 |
|
| 2262 |
if (!success) { |
| 2263 |
/* purecov: begin inspected */ |
| 2264 |
err = (option == OS_FILE_OPEN) ? ER_CANT_OPEN_FILE : ER_CANT_CREATE_FILE; |
| 2265 |
/* purecov: end */ |
| 2266 |
|
| 2267 |
} else if (create_file && init_cbk) { |
| 2268 |
auto db_err = init_cbk(handle); |
| 2269 |
|
| 2270 |
if (db_err != DB_SUCCESS) { |
| 2271 |
/* purecov: begin inspected */ |
| 2272 |
os_file_close(handle); |
| 2273 |
err = ER_ERROR_ON_WRITE; |
| 2274 |
/* purecov: end */ |
| 2275 |
} |
| 2276 |
} |
| 2277 |
|
| 2278 |
if (err != 0) { |
| 2279 |
/* purecov: begin inspected */ |
| 2280 |
char errbuf[MYSYS_STRERROR_SIZE]; |
| 2281 |
my_error(err, MYF(0), file_name.c_str(), errno, |
| 2282 |
my_strerror(errbuf, sizeof(errbuf), errno)); |
| 2283 |
return err; |
| 2284 |
/* purecov: end */ |
| 2285 |
} |
| 2286 |
|
| 2287 |
if (task == nullptr) { |
| 2288 |
ut_ad(create_file); |
| 2289 |
os_file_close(handle); |
| 2290 |
return 0; |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* Set file descriptor in task. */ |
| 2294 |
close_file(task); |
| 2295 |
task->m_current_file_des = handle; |
| 2296 |
|
| 2297 |
ut_ad(handle.m_file != OS_FILE_CLOSED); |
| 2298 |
|
| 2299 |
task->m_file_cache = true; |
| 2300 |
|
| 2301 |
/* Set cache to false if direct IO(O_DIRECT) is used. */ |
| 2302 |
if (file_type == OS_CLONE_DATA_FILE) { |
| 2303 |
task->m_file_cache = !srv_is_direct_io(); |
| 2304 |
|
| 2305 |
DBUG_EXECUTE_IF("clone_no_zero_copy", task->m_file_cache = false;); |
| 2306 |
} |
| 2307 |
|
| 2308 |
auto file_meta = file_ctx->get_file_meta_read(); |
| 2309 |
|
| 2310 |
/* If the task has pinned file, the index should be set. */ |
| 2311 |
ut_ad(!task->m_pinned_file || |
| 2312 |
task->m_current_file_index == file_meta->m_file_index); |
| 2313 |
|
| 2314 |
task->m_current_file_index = file_meta->m_file_index; |
| 2315 |
|
| 2316 |
return 0; |
| 2317 |
} |
| 2318 |
|
| 2319 |
int Clone_Handle::close_file(Clone_Task *task) { |
| 2320 |
bool success = true; |
| 2321 |
|
| 2322 |
/* Close file, if opened. */ |
| 2323 |
if (task->m_current_file_des.m_file != OS_FILE_CLOSED) { |
| 2324 |
success = os_file_close(task->m_current_file_des); |
| 2325 |
} |
| 2326 |
|
| 2327 |
task->m_current_file_des.m_file = OS_FILE_CLOSED; |
| 2328 |
task->m_file_cache = true; |
| 2329 |
|
| 2330 |
if (!success) { |
| 2331 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Innodb error while closing file"); |
| 2332 |
return (ER_INTERNAL_ERROR); |
| 2333 |
} |
| 2334 |
|
| 2335 |
return (0); |
| 2336 |
} |
| 2337 |
|
| 2338 |
int Clone_Handle::file_callback(Ha_clone_cbk *cbk, Clone_Task *task, uint len, |
| 2339 |
bool buf_cbk, uint64_t offset |
| 2340 |
#ifdef UNIV_PFS_IO |
| 2341 |
, |
| 2342 |
ut::Location location |
| 2343 |
#endif /* UNIV_PFS_IO */ |
| 2344 |
) { |
| 2345 |
int err; |
| 2346 |
Ha_clone_file file; |
| 2347 |
|
| 2348 |
/* Platform specific code to set file handle */ |
| 2349 |
#ifdef _WIN32 |
| 2350 |
file.type = Ha_clone_file::FILE_HANDLE; |
| 2351 |
file.file_handle = static_cast<void *>(task->m_current_file_des.m_file); |
| 2352 |
#else |
| 2353 |
file.type = Ha_clone_file::FILE_DESC; |
| 2354 |
file.file_desc = task->m_current_file_des.m_file; |
| 2355 |
#endif /* _WIN32 */ |
| 2356 |
|
| 2357 |
/* Register for PFS IO */ |
| 2358 |
#ifdef UNIV_PFS_IO |
| 2359 |
PSI_file_locker_state state; |
| 2360 |
struct PSI_file_locker *locker; |
| 2361 |
enum PSI_file_operation psi_op; |
| 2362 |
|
| 2363 |
locker = nullptr; |
| 2364 |
psi_op = is_copy_clone() ? PSI_FILE_READ : PSI_FILE_WRITE; |
| 2365 |
|
| 2366 |
register_pfs_file_io_begin(&state, locker, task->m_current_file_des, len, |
| 2367 |
psi_op, location); |
| 2368 |
#endif /* UNIV_PFS_IO */ |
| 2369 |
|
| 2370 |
/* Call appropriate callback to transfer data. */ |
| 2371 |
if (is_copy_clone()) { |
| 2372 |
/* Send data from file. */ |
| 2373 |
err = cbk->file_cbk(file, len); |
| 2374 |
|
| 2375 |
} else if (buf_cbk) { |
| 2376 |
unsigned char *data_buf = nullptr; |
| 2377 |
uint32_t data_len = 0; |
| 2378 |
/* Get data buffer */ |
| 2379 |
err = cbk->apply_buffer_cbk(data_buf, data_len); |
| 2380 |
if (err == 0) { |
| 2381 |
/* Modify and write data buffer to file. */ |
| 2382 |
err = modify_and_write(task, offset, data_buf, data_len); |
| 2383 |
} |
| 2384 |
} else { |
| 2385 |
/* Write directly to file. */ |
| 2386 |
err = cbk->apply_file_cbk(file); |
| 2387 |
} |
| 2388 |
|
| 2389 |
#ifdef UNIV_PFS_IO |
| 2390 |
register_pfs_file_io_end(locker, len); |
| 2391 |
#endif /* UNIV_PFS_IO */ |
| 2392 |
|
| 2393 |
return (err); |
| 2394 |
} |