| 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 arch/arch0log.cc |
| 29 |
Innodb implementation for log archive |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#include "arch0log.h" |
| 34 |
#include "clone0clone.h" |
| 35 |
#include "log0buf.h" |
| 36 |
#include "log0chkp.h" |
| 37 |
#include "log0encryption.h" |
| 38 |
#include "log0files_governor.h" |
| 39 |
#include "log0write.h" |
| 40 |
#include "srv0start.h" |
| 41 |
#include "ut0mutex.h" |
| 42 |
|
| 43 |
/** Chunk size for archiving redo log */ |
| 44 |
const uint ARCH_LOG_CHUNK_SIZE = 1024 * 1024; |
| 45 |
|
| 46 |
os_offset_t Log_Arch_Client_Ctx::get_archived_file_size() const { |
| 47 |
return m_group->get_file_size(); |
| 48 |
} |
| 49 |
|
| 50 |
void Log_Arch_Client_Ctx::get_header_size(uint &header_sz, |
| 51 |
uint &trailer_sz) const { |
| 52 |
header_sz = LOG_FILE_HDR_SIZE; |
| 53 |
trailer_sz = OS_FILE_LOG_BLOCK_SIZE; |
| 54 |
} |
| 55 |
|
| 56 |
int Log_Arch_Client_Ctx::start(byte *header, uint len) { |
| 57 |
ut_ad(len >= LOG_FILE_HDR_SIZE); |
| 58 |
|
| 59 |
auto err = arch_log_sys->start(m_group, m_begin_lsn, header, false); |
| 60 |
|
| 61 |
if (err != 0) { |
| 62 |
return (err); |
| 63 |
} |
| 64 |
|
| 65 |
m_state = ARCH_CLIENT_STATE_STARTED; |
| 66 |
|
| 67 |
ib::info(ER_IB_MSG_15) << "Clone Start LOG ARCH : start LSN : " |
| 68 |
<< m_begin_lsn; |
| 69 |
|
| 70 |
return (0); |
| 71 |
} |
| 72 |
|
| 73 |
/** Stop redo log archiving. Exact trailer length is returned as out |
| 74 |
parameter which could be less than the redo block size. |
| 75 |
@param[out] trailer redo trailer. Caller must allocate buffer. |
| 76 |
@param[in,out] len trailer length |
| 77 |
@param[out] offset trailer block offset |
| 78 |
@return error code */ |
| 79 |
int Log_Arch_Client_Ctx::stop(byte *trailer, uint32_t &len, uint64_t &offset) { |
| 80 |
lsn_t start_lsn; |
| 81 |
lsn_t stop_lsn; |
| 82 |
|
| 83 |
ut_ad(m_state == ARCH_CLIENT_STATE_STARTED); |
| 84 |
ut_ad(trailer == nullptr || len >= OS_FILE_LOG_BLOCK_SIZE); |
| 85 |
|
| 86 |
auto err = arch_log_sys->stop(m_group, m_end_lsn, trailer, len); |
| 87 |
|
| 88 |
start_lsn = m_group->get_begin_lsn(); |
| 89 |
|
| 90 |
start_lsn = ut_uint64_align_down(start_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 91 |
stop_lsn = ut_uint64_align_down(m_end_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 92 |
|
| 93 |
lsn_t file_capacity = m_group->get_file_size(); |
| 94 |
|
| 95 |
file_capacity -= LOG_FILE_HDR_SIZE; |
| 96 |
|
| 97 |
offset = (stop_lsn - start_lsn) % file_capacity; |
| 98 |
|
| 99 |
offset += LOG_FILE_HDR_SIZE; |
| 100 |
|
| 101 |
m_state = ARCH_CLIENT_STATE_STOPPED; |
| 102 |
|
| 103 |
ib::info(ER_IB_MSG_16) << "Clone Stop LOG ARCH : end LSN : " << m_end_lsn; |
| 104 |
|
| 105 |
return (err); |
| 106 |
} |
| 107 |
|
| 108 |
/** Get archived data file details |
| 109 |
@param[in] cbk_func callback called for each file |
| 110 |
@param[in] ctx callback function context |
| 111 |
@return error code */ |
| 112 |
int Log_Arch_Client_Ctx::get_files(Log_Arch_Cbk *cbk_func, void *ctx) { |
| 113 |
ut_ad(m_state == ARCH_CLIENT_STATE_STOPPED); |
| 114 |
|
| 115 |
int err = 0; |
| 116 |
|
| 117 |
auto size = m_group->get_file_size(); |
| 118 |
|
| 119 |
/* Check if the archived redo log is less than one block size. In this |
| 120 |
case we send the data in trailer buffer. */ |
| 121 |
auto low_begin = ut_uint64_align_down(m_begin_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 122 |
|
| 123 |
auto low_end = ut_uint64_align_down(m_end_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 124 |
|
| 125 |
if (low_begin == low_end) { |
| 126 |
err = cbk_func(nullptr, size, 0, ctx); |
| 127 |
return (err); |
| 128 |
} |
| 129 |
|
| 130 |
/* Get the start lsn of the group */ |
| 131 |
auto start_lsn = m_group->get_begin_lsn(); |
| 132 |
start_lsn = ut_uint64_align_down(start_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 133 |
|
| 134 |
ut_ad(m_begin_lsn >= start_lsn); |
| 135 |
|
| 136 |
/* Calculate first file index and offset for this client. */ |
| 137 |
lsn_t lsn_diff = m_begin_lsn - start_lsn; |
| 138 |
uint64_t capacity = size - LOG_FILE_HDR_SIZE; |
| 139 |
|
| 140 |
auto idx = static_cast<uint>(lsn_diff / capacity); |
| 141 |
uint64_t offset = lsn_diff % capacity; |
| 142 |
|
| 143 |
/* Set start lsn to the beginning of file. */ |
| 144 |
start_lsn = m_begin_lsn - offset; |
| 145 |
|
| 146 |
offset += LOG_FILE_HDR_SIZE; |
| 147 |
offset = ut_uint64_align_down(offset, OS_FILE_LOG_BLOCK_SIZE); |
| 148 |
|
| 149 |
/* Callback with all archive file names that holds the range of log |
| 150 |
data for this client. */ |
| 151 |
while (start_lsn < m_end_lsn) { |
| 152 |
char name[MAX_ARCH_LOG_FILE_NAME_LEN]; |
| 153 |
m_group->get_file_name(idx, name, MAX_ARCH_LOG_FILE_NAME_LEN); |
| 154 |
|
| 155 |
idx++; |
| 156 |
start_lsn += capacity; |
| 157 |
|
| 158 |
/* For last file adjust the size based on end lsn. */ |
| 159 |
if (start_lsn >= m_end_lsn) { |
| 160 |
lsn_diff = |
| 161 |
ut_uint64_align_up(start_lsn - m_end_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 162 |
size -= lsn_diff; |
| 163 |
} |
| 164 |
|
| 165 |
err = cbk_func(name, size, offset, ctx); |
| 166 |
|
| 167 |
if (err != 0) { |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
/* offset could be non-zero only for first file. */ |
| 172 |
offset = 0; |
| 173 |
} |
| 174 |
|
| 175 |
return (err); |
| 176 |
} |
| 177 |
|
| 178 |
/** Release archived data so that system can purge it */ |
| 179 |
void Log_Arch_Client_Ctx::release() { |
| 180 |
if (m_state == ARCH_CLIENT_STATE_INIT) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
if (m_state == ARCH_CLIENT_STATE_STARTED) { |
| 185 |
uint64_t dummy_offset; |
| 186 |
uint32_t dummy_len = 0; |
| 187 |
|
| 188 |
/* This is for cleanup in error cases. */ |
| 189 |
stop(nullptr, dummy_len, dummy_offset); |
| 190 |
} |
| 191 |
|
| 192 |
ut_ad(m_state == ARCH_CLIENT_STATE_STOPPED); |
| 193 |
|
| 194 |
arch_log_sys->release(m_group, false); |
| 195 |
|
| 196 |
m_group = nullptr; |
| 197 |
|
| 198 |
m_begin_lsn = LSN_MAX; |
| 199 |
m_end_lsn = LSN_MAX; |
| 200 |
|
| 201 |
m_state = ARCH_CLIENT_STATE_INIT; |
| 202 |
} |
| 203 |
|
| 204 |
os_offset_t Arch_Log_Sys::get_recommended_file_size() const { |
| 205 |
if (log_sys == nullptr) { |
| 206 |
ut_d(ut_error); |
| 207 |
/* This shouldn't be executed, but if there was a bug, |
| 208 |
we would prefer to return some value instead of crash, |
| 209 |
because the archiver must not crash the server. */ |
| 210 |
ut_o(return srv_redo_log_capacity_used / LOG_N_FILES); |
| 211 |
} |
| 212 |
return log_sys->m_capacity.next_file_size(); |
| 213 |
} |
| 214 |
|
| 215 |
void Arch_Log_Sys::update_header(byte *header, lsn_t file_start_lsn, |
| 216 |
lsn_t checkpoint_lsn) { |
| 217 |
ut_a(file_start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 218 |
|
| 219 |
/* Copy Header information. */ |
| 220 |
Log_file_header file_header; |
| 221 |
file_header.m_format = to_int(Log_format::CURRENT); |
| 222 |
file_header.m_start_lsn = file_start_lsn; |
| 223 |
file_header.m_creator_name = LOG_HEADER_CREATOR_CLONE; |
| 224 |
file_header.m_log_flags = 0; |
| 225 |
file_header.m_log_uuid = m_current_group->get_uuid(); |
| 226 |
ut_a(file_header.m_log_uuid > 0); |
| 227 |
log_file_header_serialize(file_header, header); |
| 228 |
|
| 229 |
/* Update checkpoint headers. */ |
| 230 |
Log_checkpoint_header checkpoint_header; |
| 231 |
checkpoint_header.m_checkpoint_lsn = checkpoint_lsn; |
| 232 |
log_checkpoint_header_serialize(checkpoint_header, header + LOG_CHECKPOINT_1); |
| 233 |
log_checkpoint_header_serialize(checkpoint_header, header + LOG_CHECKPOINT_2); |
| 234 |
|
| 235 |
/* Fill encryption information if needed. */ |
| 236 |
if (log_sys == nullptr || !log_can_encrypt(*log_sys)) { |
| 237 |
return; |
| 238 |
} |
| 239 |
byte *dest = header + LOG_ENCRYPTION; |
| 240 |
log_file_header_fill_encryption(log_sys->m_encryption_metadata, false, dest); |
| 241 |
} |
| 242 |
|
| 243 |
/** Start redo log archiving. |
| 244 |
If archiving is already in progress, the client |
| 245 |
is attached to current group. |
| 246 |
@param[out] group log archive group |
| 247 |
@param[out] start_lsn start lsn for client |
| 248 |
@param[out] header redo log header |
| 249 |
@param[in] is_durable if client needs durable archiving |
| 250 |
@return error code */ |
| 251 |
int Arch_Log_Sys::start(Arch_Group *&group, lsn_t &start_lsn, byte *header, |
| 252 |
bool is_durable) { |
| 253 |
bool create_new_group = false; |
| 254 |
|
| 255 |
memset(header, 0, LOG_FILE_HDR_SIZE); |
| 256 |
|
| 257 |
log_request_checkpoint(*log_sys, true); |
| 258 |
|
| 259 |
arch_mutex_enter(); |
| 260 |
|
| 261 |
if (m_state == ARCH_STATE_READ_ONLY) { |
| 262 |
arch_mutex_exit(); |
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/* Wait for idle state, if preparing to idle. */ |
| 267 |
if (!wait_idle()) { |
| 268 |
int err = 0; |
| 269 |
|
| 270 |
if (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) { |
| 271 |
err = ER_QUERY_INTERRUPTED; |
| 272 |
my_error(err, MYF(0)); |
| 273 |
} else { |
| 274 |
err = ER_INTERNAL_ERROR; |
| 275 |
my_error(err, MYF(0), "Log Archiver wait too long"); |
| 276 |
} |
| 277 |
|
| 278 |
arch_mutex_exit(); |
| 279 |
return (err); |
| 280 |
} |
| 281 |
|
| 282 |
ut_ad(m_state != ARCH_STATE_PREPARE_IDLE); |
| 283 |
|
| 284 |
if (m_state == ARCH_STATE_ABORT) { |
| 285 |
arch_mutex_exit(); |
| 286 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 287 |
return (ER_QUERY_INTERRUPTED); |
| 288 |
} |
| 289 |
|
| 290 |
/* Start archiver task, if needed. */ |
| 291 |
if (m_state == ARCH_STATE_INIT) { |
| 292 |
auto err = start_log_archiver_background(); |
| 293 |
|
| 294 |
if (err != 0) { |
| 295 |
arch_mutex_exit(); |
| 296 |
|
| 297 |
ib::error(ER_IB_MSG_17) << "Could not start Archiver" |
| 298 |
<< " background task"; |
| 299 |
|
| 300 |
return (err); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/* Start archiving from checkpoint LSN. */ |
| 305 |
log_writer_mutex_enter(*log_sys); /* protects log_sys->last_checkpoint_lsn */ |
| 306 |
log_files_mutex_enter(*log_sys); /* protects log_sys->m_files */ |
| 307 |
|
| 308 |
start_lsn = log_sys->last_checkpoint_lsn.load(); |
| 309 |
|
| 310 |
const auto file = log_sys->m_files.find(start_lsn); |
| 311 |
|
| 312 |
ut_ad(file != log_sys->m_files.end()); |
| 313 |
|
| 314 |
if (file == log_sys->m_files.end()) { |
| 315 |
ib::error(ER_IB_MSG_17) << "Could not start Archiver" |
| 316 |
<< " background task because of" |
| 317 |
<< " unexpected internal error"; |
| 318 |
log_files_mutex_exit(*log_sys); |
| 319 |
log_writer_mutex_exit(*log_sys); |
| 320 |
return ER_INTERNAL_ERROR; |
| 321 |
} |
| 322 |
|
| 323 |
const auto start_index = file->m_id; |
| 324 |
|
| 325 |
const uint64_t start_offset = |
| 326 |
ut_uint64_align_down(file->offset(start_lsn), OS_FILE_LOG_BLOCK_SIZE); |
| 327 |
|
| 328 |
/* Need to create a new group if archiving is not in progress. */ |
| 329 |
if (m_state == ARCH_STATE_IDLE || m_state == ARCH_STATE_INIT) { |
| 330 |
m_archived_lsn.store( |
| 331 |
ut_uint64_align_down(start_lsn, OS_FILE_LOG_BLOCK_SIZE)); |
| 332 |
create_new_group = true; |
| 333 |
} |
| 334 |
|
| 335 |
/* Set archiver state to active. */ |
| 336 |
if (m_state != ARCH_STATE_ACTIVE) { |
| 337 |
update_state_low(ARCH_STATE_ACTIVE); |
| 338 |
os_event_set(log_archiver_thread_event); |
| 339 |
} |
| 340 |
|
| 341 |
log_files_mutex_exit(*log_sys); |
| 342 |
log_writer_mutex_exit(*log_sys); |
| 343 |
|
| 344 |
/* Create a new group. */ |
| 345 |
if (create_new_group) { |
| 346 |
m_current_group = |
| 347 |
ut::new_withkey<Arch_Group>(ut::make_psi_memory_key(mem_key_archive), |
| 348 |
start_lsn, LOG_FILE_HDR_SIZE, &m_mutex); |
| 349 |
|
| 350 |
if (m_current_group == nullptr) { |
| 351 |
arch_mutex_exit(); |
| 352 |
|
| 353 |
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Arch_Group)); |
| 354 |
return (ER_OUTOFMEMORY); |
| 355 |
} |
| 356 |
|
| 357 |
const Arch_group_uuid uuid{log_generate_uuid()}; |
| 358 |
|
| 359 |
auto db_err = |
| 360 |
m_current_group->init_file_ctx(ARCH_DIR, ARCH_LOG_DIR, ARCH_LOG_FILE, 0, |
| 361 |
get_recommended_file_size(), uuid); |
| 362 |
|
| 363 |
if (db_err != DB_SUCCESS) { |
| 364 |
arch_mutex_exit(); |
| 365 |
|
| 366 |
my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Arch_File_Ctx)); |
| 367 |
|
| 368 |
return (ER_OUTOFMEMORY); |
| 369 |
} |
| 370 |
|
| 371 |
m_start_log_index = start_index; |
| 372 |
m_start_log_offset = start_offset; |
| 373 |
|
| 374 |
m_chunk_size = ARCH_LOG_CHUNK_SIZE; |
| 375 |
|
| 376 |
m_group_list.push_back(m_current_group); |
| 377 |
} |
| 378 |
|
| 379 |
/* Attach to the current group. */ |
| 380 |
m_current_group->attach(is_durable); |
| 381 |
|
| 382 |
group = m_current_group; |
| 383 |
|
| 384 |
arch_mutex_exit(); |
| 385 |
|
| 386 |
/* Update header with checkpoint LSN. Note, that arch mutex is released |
| 387 |
and m_current_group should no longer be accessed. The group cannot be |
| 388 |
freed as we have already attached to it. */ |
| 389 |
const lsn_t file_start_lsn = |
| 390 |
ut_uint64_align_down(group->get_begin_lsn(), OS_FILE_LOG_BLOCK_SIZE); |
| 391 |
|
| 392 |
update_header(header, file_start_lsn, start_lsn); |
| 393 |
|
| 394 |
return (0); |
| 395 |
} |
| 396 |
|
| 397 |
#ifdef UNIV_DEBUG |
| 398 |
void Arch_Group::adjust_end_lsn(lsn_t &stop_lsn, uint32_t &blk_len) { |
| 399 |
stop_lsn = ut_uint64_align_down(get_begin_lsn(), OS_FILE_LOG_BLOCK_SIZE); |
| 400 |
|
| 401 |
stop_lsn += get_file_size() - LOG_FILE_HDR_SIZE; |
| 402 |
blk_len = 0; |
| 403 |
|
| 404 |
/* Increase Stop LSN 64 bytes ahead of file end not exceeding |
| 405 |
redo block size. */ |
| 406 |
DBUG_EXECUTE_IF( |
| 407 |
"clone_arch_log_extra_bytes", blk_len = OS_FILE_LOG_BLOCK_SIZE; |
| 408 |
stop_lsn += 64; stop_lsn = std::min(stop_lsn, log_get_lsn(*log_sys));); |
| 409 |
} |
| 410 |
|
| 411 |
void Arch_Group::adjust_copy_length(lsn_t arch_lsn, uint32_t ©_len) { |
| 412 |
lsn_t end_lsn = LSN_MAX; |
| 413 |
uint32_t blk_len = 0; |
| 414 |
adjust_end_lsn(end_lsn, blk_len); |
| 415 |
|
| 416 |
if (end_lsn <= arch_lsn) { |
| 417 |
copy_len = 0; |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
/* Adjust if copying beyond end LSN. */ |
| 422 |
auto len_left = end_lsn - arch_lsn; |
| 423 |
len_left = ut_uint64_align_down(len_left, OS_FILE_LOG_BLOCK_SIZE); |
| 424 |
|
| 425 |
if (len_left < copy_len) { |
| 426 |
copy_len = static_cast<uint32_t>(len_left); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
#endif /* UNIV_DEBUG */ |
| 431 |
|
| 432 |
/** Stop redo log archiving. |
| 433 |
If other clients are there, the client is detached from |
| 434 |
the current group. |
| 435 |
@param[out] group log archive group |
| 436 |
@param[out] stop_lsn stop lsn for client |
| 437 |
@param[out] log_blk redo log trailer block |
| 438 |
@param[in,out] blk_len length in bytes |
| 439 |
@return error code */ |
| 440 |
int Arch_Log_Sys::stop(Arch_Group *group, lsn_t &stop_lsn, byte *log_blk, |
| 441 |
uint32_t &blk_len) { |
| 442 |
int err = 0; |
| 443 |
blk_len = 0; |
| 444 |
stop_lsn = m_archived_lsn.load(); |
| 445 |
|
| 446 |
if (log_blk != nullptr) { |
| 447 |
/* Get the current LSN and trailer block. */ |
| 448 |
log_buffer_get_last_block(*log_sys, stop_lsn, log_blk, blk_len); |
| 449 |
|
| 450 |
DBUG_EXECUTE_IF("clone_arch_log_stop_file_end", |
| 451 |
group->adjust_end_lsn(stop_lsn, blk_len);); |
| 452 |
|
| 453 |
/* Will throw error, if shutdown. We still continue |
| 454 |
with detach but return the error. */ |
| 455 |
err = wait_archive_complete(stop_lsn); |
| 456 |
} |
| 457 |
|
| 458 |
arch_mutex_enter(); |
| 459 |
|
| 460 |
if (m_state == ARCH_STATE_READ_ONLY) { |
| 461 |
arch_mutex_exit(); |
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
auto count_active_client = group->detach(stop_lsn, nullptr); |
| 466 |
ut_ad(group->is_referenced()); |
| 467 |
|
| 468 |
if (!group->is_active() && err == 0) { |
| 469 |
/* Archiving for the group has already stopped. */ |
| 470 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Clone: Log Archiver failed"); |
| 471 |
err = ER_INTERNAL_ERROR; |
| 472 |
} |
| 473 |
|
| 474 |
if (group->is_active() && count_active_client == 0) { |
| 475 |
/* No other active client. Prepare to get idle. */ |
| 476 |
if (m_state == ARCH_STATE_ACTIVE) { |
| 477 |
/* The active group must be the current group. */ |
| 478 |
ut_ad(group == m_current_group); |
| 479 |
update_state(ARCH_STATE_PREPARE_IDLE); |
| 480 |
os_event_set(log_archiver_thread_event); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
arch_mutex_exit(); |
| 485 |
|
| 486 |
return (err); |
| 487 |
} |
| 488 |
|
| 489 |
void Arch_Log_Sys::force_abort() { |
| 490 |
lsn_t lsn_max = LSN_MAX; /* unused */ |
| 491 |
uint to_archive = 0; /* unused */ |
| 492 |
check_set_state(true, &lsn_max, &to_archive); |
| 493 |
/* Above line changes state to ARCH_STATE_PREPARE_IDLE or ARCH_STATE_ABORT. |
| 494 |
Let us notify the background thread to give it chance to notice the change and |
| 495 |
wait for it to transition to ARCH_STATE_IDLE before returning (in case of |
| 496 |
ARCH_STATE_ABORT, wait_idle() does nothing).*/ |
| 497 |
arch_mutex_enter(); |
| 498 |
wait_idle(); |
| 499 |
arch_mutex_exit(); |
| 500 |
} |
| 501 |
|
| 502 |
/** Release the current group from client. |
| 503 |
@param[in] group group the client is attached to |
| 504 |
@param[in] is_durable if client needs durable archiving */ |
| 505 |
void Arch_Log_Sys::release(Arch_Group *group, bool is_durable) { |
| 506 |
arch_mutex_enter(); |
| 507 |
|
| 508 |
group->release(is_durable); |
| 509 |
|
| 510 |
/* Check if there are other references or archiving is still |
| 511 |
in progress. */ |
| 512 |
if (group->is_referenced() || group->is_active()) { |
| 513 |
arch_mutex_exit(); |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
/* Cleanup the group. */ |
| 518 |
ut_ad(group != m_current_group); |
| 519 |
|
| 520 |
m_group_list.remove(group); |
| 521 |
|
| 522 |
ut::delete_(group); |
| 523 |
|
| 524 |
arch_mutex_exit(); |
| 525 |
} |
| 526 |
|
| 527 |
/** Check and set log archive system state and output the |
| 528 |
amount of redo log available for archiving. |
| 529 |
@param[in] is_abort need to abort |
| 530 |
@param[in,out] archived_lsn LSN up to which redo log is archived |
| 531 |
@param[out] to_archive amount of redo log to be archived */ |
| 532 |
Arch_State Arch_Log_Sys::check_set_state(bool is_abort, lsn_t *archived_lsn, |
| 533 |
uint *to_archive) { |
| 534 |
auto is_shutdown = (srv_shutdown_state.load() == SRV_SHUTDOWN_LAST_PHASE || |
| 535 |
srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS); |
| 536 |
|
| 537 |
auto need_to_abort = (is_abort || is_shutdown); |
| 538 |
|
| 539 |
*to_archive = 0; |
| 540 |
|
| 541 |
arch_mutex_enter(); |
| 542 |
|
| 543 |
switch (m_state) { |
| 544 |
case ARCH_STATE_ACTIVE: |
| 545 |
|
| 546 |
if (*archived_lsn != LSN_MAX) { |
| 547 |
/* Update system archived LSN from input */ |
| 548 |
ut_ad(*archived_lsn >= m_archived_lsn.load()); |
| 549 |
m_archived_lsn.store(*archived_lsn); |
| 550 |
} else { |
| 551 |
/* If input is not initialized, |
| 552 |
set from system archived LSN */ |
| 553 |
*archived_lsn = m_archived_lsn.load(); |
| 554 |
} |
| 555 |
|
| 556 |
lsn_t lsn_diff; |
| 557 |
|
| 558 |
/* Check redo log data ready to archive. */ |
| 559 |
ut_ad(log_sys->write_lsn.load() >= m_archived_lsn.load()); |
| 560 |
|
| 561 |
lsn_diff = log_sys->write_lsn.load() - m_archived_lsn.load(); |
| 562 |
|
| 563 |
lsn_diff = ut_uint64_align_down(lsn_diff, OS_FILE_LOG_BLOCK_SIZE); |
| 564 |
|
| 565 |
/* Adjust archive data length if bigger than chunks size. */ |
| 566 |
if (lsn_diff < m_chunk_size) { |
| 567 |
*to_archive = static_cast<uint>(lsn_diff); |
| 568 |
} else { |
| 569 |
*to_archive = m_chunk_size; |
| 570 |
} |
| 571 |
|
| 572 |
if (!need_to_abort) { |
| 573 |
break; |
| 574 |
} |
| 575 |
|
| 576 |
if (!is_shutdown) { |
| 577 |
ut_ad(is_abort); |
| 578 |
/* If caller asked to abort, move to prepare idle state. Archiver |
| 579 |
thread will move to IDLE state eventually. */ |
| 580 |
update_state(ARCH_STATE_PREPARE_IDLE); |
| 581 |
ib::error(ER_IB_MSG_ABORTING_LOG_ARCHIVER, ulonglong{*archived_lsn}); |
| 582 |
break; |
| 583 |
} |
| 584 |
[[fallthrough]]; |
| 585 |
|
| 586 |
case ARCH_STATE_PREPARE_IDLE: { |
| 587 |
/* No active clients. Mark the group inactive and move |
| 588 |
to idle state. */ |
| 589 |
m_current_group->disable(m_archived_lsn.load()); |
| 590 |
|
| 591 |
/* If no client reference, free the group. */ |
| 592 |
if (!m_current_group->is_referenced()) { |
| 593 |
m_group_list.remove(m_current_group); |
| 594 |
|
| 595 |
ut::delete_(m_current_group); |
| 596 |
} |
| 597 |
|
| 598 |
m_current_group = nullptr; |
| 599 |
|
| 600 |
update_state(ARCH_STATE_IDLE); |
| 601 |
} |
| 602 |
[[fallthrough]]; |
| 603 |
|
| 604 |
case ARCH_STATE_IDLE: |
| 605 |
case ARCH_STATE_INIT: |
| 606 |
|
| 607 |
/* Abort archiver thread only in case of shutdown. */ |
| 608 |
if (is_shutdown) { |
| 609 |
update_state(ARCH_STATE_ABORT); |
| 610 |
} |
| 611 |
break; |
| 612 |
|
| 613 |
case ARCH_STATE_ABORT: |
| 614 |
/* We could abort archiver from log_writer when |
| 615 |
it is already in the aborted state (shutdown). */ |
| 616 |
break; |
| 617 |
|
| 618 |
default: |
| 619 |
ut_d(ut_error); |
| 620 |
} |
| 621 |
|
| 622 |
auto ret_state = m_state; |
| 623 |
arch_mutex_exit(); |
| 624 |
|
| 625 |
return (ret_state); |
| 626 |
} |
| 627 |
|
| 628 |
dberr_t Arch_Log_Sys::copy_log(Arch_File_Ctx *file_ctx, lsn_t start_lsn, |
| 629 |
uint length) { |
| 630 |
dberr_t err = DB_SUCCESS; |
| 631 |
|
| 632 |
if (file_ctx->is_closed()) { |
| 633 |
/* Open system redo log file context */ |
| 634 |
err = |
| 635 |
file_ctx->open(true, LSN_MAX, m_start_log_index, m_start_log_offset, 0); |
| 636 |
if (err != DB_SUCCESS) { |
| 637 |
return err; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
Arch_Group *curr_group; |
| 642 |
uint write_size; |
| 643 |
|
| 644 |
curr_group = arch_log_sys->get_arch_group(); |
| 645 |
|
| 646 |
/* Copy log data into one or more files in archiver group. */ |
| 647 |
while (length > 0) { |
| 648 |
uint64_t len_copy; |
| 649 |
uint64_t len_left; |
| 650 |
|
| 651 |
len_copy = static_cast<uint64_t>(length); |
| 652 |
|
| 653 |
len_left = file_ctx->bytes_left(); |
| 654 |
|
| 655 |
/* Current file is over, switch to next file. */ |
| 656 |
if (len_left == 0) { |
| 657 |
err = file_ctx->open_next(LSN_MAX, LOG_FILE_HDR_SIZE, 0); |
| 658 |
if (err != DB_SUCCESS) { |
| 659 |
return (err); |
| 660 |
} |
| 661 |
|
| 662 |
len_left = file_ctx->bytes_left(); |
| 663 |
|
| 664 |
ut_ad(len_left > 0); |
| 665 |
} |
| 666 |
|
| 667 |
if (len_left == 0) { |
| 668 |
return DB_ERROR; |
| 669 |
} |
| 670 |
|
| 671 |
/* Write as much as possible from current file. */ |
| 672 |
if (len_left < len_copy) { |
| 673 |
write_size = static_cast<uint>(len_left); |
| 674 |
} else { |
| 675 |
write_size = length; |
| 676 |
} |
| 677 |
|
| 678 |
auto get_header_cbk = [start_lsn, this](uint64_t start_offset, |
| 679 |
byte *header) { |
| 680 |
ut_ad(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 681 |
ut_ad(start_offset % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 682 |
|
| 683 |
/* Assertions above verify that the conditions below shouldn't hold. |
| 684 |
However, they are only for debug binary. The release binary must not |
| 685 |
crash in the Archiver. */ |
| 686 |
if (start_lsn % OS_FILE_LOG_BLOCK_SIZE != 0 || |
| 687 |
start_offset % OS_FILE_LOG_BLOCK_SIZE != 0) { |
| 688 |
return DB_ERROR; |
| 689 |
} |
| 690 |
|
| 691 |
/* Do not store checkpoint_lsn inside archived log files, because these |
| 692 |
files become later copied by possibly multiple readers and each of such |
| 693 |
copies would possibly be started at different checkpoint lsn and after |
| 694 |
all data >= checkpoint_lsn is copied, the valid checkpoint header would |
| 695 |
anyway be written to the first file created in the copy. Therefore the |
| 696 |
checkpoint_lsn is irrelevant for headers of archived log files. */ |
| 697 |
update_header(header, start_lsn + start_offset, 0); |
| 698 |
return DB_SUCCESS; |
| 699 |
}; |
| 700 |
|
| 701 |
err = curr_group->write_to_file(file_ctx, nullptr, write_size, false, false, |
| 702 |
get_header_cbk); |
| 703 |
|
| 704 |
if (err != DB_SUCCESS) { |
| 705 |
return (err); |
| 706 |
} |
| 707 |
|
| 708 |
ut_ad(length >= write_size); |
| 709 |
length -= write_size; |
| 710 |
start_lsn += write_size; |
| 711 |
} |
| 712 |
|
| 713 |
return (DB_SUCCESS); |
| 714 |
} |
| 715 |
|
| 716 |
bool Arch_Log_Sys::wait_idle() { |
| 717 |
ut_ad(mutex_own(&m_mutex)); |
| 718 |
|
| 719 |
if (m_state == ARCH_STATE_PREPARE_IDLE) { |
| 720 |
os_event_set(log_archiver_thread_event); |
| 721 |
bool is_timeout = false; |
| 722 |
int alert_count = 0; |
| 723 |
|
| 724 |
auto err = Clone_Sys::wait_default( |
| 725 |
[&](bool alert, bool &result) { |
| 726 |
ut_ad(mutex_own(&m_mutex)); |
| 727 |
result = (m_state == ARCH_STATE_PREPARE_IDLE); |
| 728 |
|
| 729 |
if (srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) { |
| 730 |
return (ER_QUERY_INTERRUPTED); |
| 731 |
} |
| 732 |
|
| 733 |
if (result) { |
| 734 |
os_event_set(log_archiver_thread_event); |
| 735 |
|
| 736 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 737 |
if (alert && ++alert_count == 12) { |
| 738 |
alert_count = 0; |
| 739 |
ib::info(ER_IB_MSG_24) << "Log Archiving start: waiting for " |
| 740 |
"idle state."; |
| 741 |
} |
| 742 |
} |
| 743 |
return (0); |
| 744 |
}, |
| 745 |
&m_mutex, is_timeout); |
| 746 |
|
| 747 |
if (err == 0 && is_timeout) { |
| 748 |
err = ER_INTERNAL_ERROR; |
| 749 |
ib::info(ER_IB_MSG_25) << "Log Archiving start: wait for idle state " |
| 750 |
"timed out"; |
| 751 |
ut_d(ut_error); |
| 752 |
} |
| 753 |
|
| 754 |
if (err != 0) { |
| 755 |
return (false); |
| 756 |
} |
| 757 |
} |
| 758 |
return (true); |
| 759 |
} |
| 760 |
|
| 761 |
/** Wait for redo log archive up to the target LSN. |
| 762 |
We need to wait till current log sys LSN during archive stop. |
| 763 |
@param[in] target_lsn target archive LSN to wait for |
| 764 |
@return error code */ |
| 765 |
int Arch_Log_Sys::wait_archive_complete(lsn_t target_lsn) { |
| 766 |
target_lsn = ut_uint64_align_down(target_lsn, OS_FILE_LOG_BLOCK_SIZE); |
| 767 |
|
| 768 |
/* Check and wait for archiver thread if needed. */ |
| 769 |
if (m_archived_lsn.load() < target_lsn) { |
| 770 |
os_event_set(log_archiver_thread_event); |
| 771 |
|
| 772 |
bool is_timeout = false; |
| 773 |
int alert_count = 0; |
| 774 |
|
| 775 |
auto err = Clone_Sys::wait_default( |
| 776 |
[&](bool alert, bool &result) { |
| 777 |
/* Read consistent state. */ |
| 778 |
arch_mutex_enter(); |
| 779 |
auto state = m_state; |
| 780 |
arch_mutex_exit(); |
| 781 |
|
| 782 |
/* Check if we need to abort. */ |
| 783 |
if (state == ARCH_STATE_ABORT || |
| 784 |
srv_shutdown_state.load() >= SRV_SHUTDOWN_CLEANUP) { |
| 785 |
my_error(ER_QUERY_INTERRUPTED, MYF(0)); |
| 786 |
return (ER_QUERY_INTERRUPTED); |
| 787 |
} |
| 788 |
|
| 789 |
if (state == ARCH_STATE_IDLE || state == ARCH_STATE_PREPARE_IDLE) { |
| 790 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Clone: Log Archiver failed"); |
| 791 |
return (ER_INTERNAL_ERROR); |
| 792 |
} |
| 793 |
|
| 794 |
ut_ad(state == ARCH_STATE_ACTIVE); |
| 795 |
|
| 796 |
/* Check if archived LSN is behind target. */ |
| 797 |
auto archived_lsn = m_archived_lsn.load(); |
| 798 |
result = (archived_lsn < target_lsn); |
| 799 |
|
| 800 |
/* Trigger flush if needed */ |
| 801 |
auto flush = log_sys->write_lsn.load() < target_lsn; |
| 802 |
|
| 803 |
if (result) { |
| 804 |
/* More data needs to be archived. */ |
| 805 |
os_event_set(log_archiver_thread_event); |
| 806 |
|
| 807 |
/* Write system redo log if needed. */ |
| 808 |
if (flush) { |
| 809 |
log_write_up_to(*log_sys, target_lsn, false); |
| 810 |
} |
| 811 |
/* Print messages every 1 minute - default is 5 seconds. */ |
| 812 |
if (alert && ++alert_count == 12) { |
| 813 |
alert_count = 0; |
| 814 |
ib::info(ER_IB_MSG_18) |
| 815 |
<< "Clone Log archive stop: waiting for archiver to " |
| 816 |
"finish archiving log till LSN: " |
| 817 |
<< target_lsn << " Archived LSN: " << archived_lsn; |
| 818 |
} |
| 819 |
} |
| 820 |
return (0); |
| 821 |
}, |
| 822 |
nullptr, is_timeout); |
| 823 |
|
| 824 |
if (err == 0 && is_timeout) { |
| 825 |
ib::info(ER_IB_MSG_19) << "Clone Log archive stop: " |
| 826 |
"wait for Archiver timed out"; |
| 827 |
|
| 828 |
err = ER_INTERNAL_ERROR; |
| 829 |
my_error(ER_INTERNAL_ERROR, MYF(0), "Clone: Log Archiver wait too long"); |
| 830 |
ut_d(ut_error); |
| 831 |
} |
| 832 |
return (err); |
| 833 |
} |
| 834 |
return (0); |
| 835 |
} |
| 836 |
|
| 837 |
/** Archive accumulated redo log in current group. |
| 838 |
This interface is for archiver background task to archive redo log |
| 839 |
data by calling it repeatedly over time. |
| 840 |
@param[in, out] init true when called the first time; it will then |
| 841 |
be set to false |
| 842 |
@param[in] curr_ctx system redo logs to copy data from |
| 843 |
@param[out] arch_lsn LSN up to which archiving is completed |
| 844 |
@param[out] wait true, if no more redo to archive |
| 845 |
@return true, if archiving is aborted */ |
| 846 |
bool Arch_Log_Sys::archive(bool init, Arch_File_Ctx *curr_ctx, lsn_t *arch_lsn, |
| 847 |
bool *wait) { |
| 848 |
Arch_State curr_state; |
| 849 |
uint32_t arch_len; |
| 850 |
|
| 851 |
dberr_t err = DB_SUCCESS; |
| 852 |
bool is_abort = false; |
| 853 |
|
| 854 |
/* Initialize system redo log file context first time. */ |
| 855 |
if (init) { |
| 856 |
/* We will use curr_ctx to read data from existing log files. |
| 857 |
We set the limit for number of files as the biggest value to |
| 858 |
avoid any such limitation in practice. */ |
| 859 |
const auto path = log_directory_path(log_sys->m_files_ctx); |
| 860 |
|
| 861 |
err = curr_ctx->init(path.c_str(), nullptr, LOG_FILE_BASE_NAME, |
| 862 |
std::numeric_limits<uint>::max()); |
| 863 |
|
| 864 |
if (err != DB_SUCCESS) { |
| 865 |
is_abort = true; |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
// If async_abort_if_below(..) was called, we should abort |
| 870 |
if (*arch_lsn < m_abort_if_below_lsn.load()) { |
| 871 |
is_abort = true; |
| 872 |
} |
| 873 |
|
| 874 |
/* Find archive system state and amount of log data to archive. */ |
| 875 |
curr_state = check_set_state(is_abort, arch_lsn, &arch_len); |
| 876 |
|
| 877 |
if (curr_state == ARCH_STATE_ACTIVE) { |
| 878 |
/* Adjust archiver length to no go beyond file end. */ |
| 879 |
DBUG_EXECUTE_IF("clone_arch_log_stop_file_end", |
| 880 |
m_current_group->adjust_copy_length(*arch_lsn, arch_len);); |
| 881 |
|
| 882 |
/* Simulate archive error. */ |
| 883 |
DBUG_EXECUTE_IF("clone_redo_no_archive", arch_len = 0;); |
| 884 |
|
| 885 |
if (arch_len == 0) { |
| 886 |
/* Nothing to archive. Need to wait. */ |
| 887 |
*wait = true; |
| 888 |
return (false); |
| 889 |
} |
| 890 |
|
| 891 |
/* Copy data from system redo log files to archiver files */ |
| 892 |
err = copy_log(curr_ctx, *arch_lsn, arch_len); |
| 893 |
|
| 894 |
/* Simulate archive error. */ |
| 895 |
DBUG_EXECUTE_IF("clone_redo_archive_error", err = DB_ERROR;); |
| 896 |
|
| 897 |
if (err == DB_SUCCESS) { |
| 898 |
*arch_lsn += arch_len; |
| 899 |
*wait = false; |
| 900 |
return (false); |
| 901 |
} |
| 902 |
|
| 903 |
/* Force abort in case of an error archiving data. */ |
| 904 |
curr_state = check_set_state(true, arch_lsn, &arch_len); |
| 905 |
} |
| 906 |
|
| 907 |
if (curr_state == ARCH_STATE_ABORT) { |
| 908 |
curr_ctx->close(); |
| 909 |
return (true); |
| 910 |
} |
| 911 |
|
| 912 |
if (curr_state == ARCH_STATE_IDLE || curr_state == ARCH_STATE_INIT) { |
| 913 |
curr_ctx->close(); |
| 914 |
*arch_lsn = LSN_MAX; |
| 915 |
*wait = true; |
| 916 |
return (false); |
| 917 |
} |
| 918 |
|
| 919 |
ut_ad(curr_state == ARCH_STATE_PREPARE_IDLE); |
| 920 |
*wait = false; |
| 921 |
return (false); |
| 922 |
} |
| 923 |
|
| 924 |
void Arch_Log_Sys::update_state(Arch_State state) { |
| 925 |
log_t &log = *log_sys; |
| 926 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 927 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 928 |
update_state_low(state); |
| 929 |
} |
| 930 |
|
| 931 |
void Arch_Log_Sys::update_state_low(Arch_State state) { |
| 932 |
log_t &log = *log_sys; |
| 933 |
ut_ad(log_writer_mutex_own(log)); |
| 934 |
ut_ad(log_files_mutex_own(log)); |
| 935 |
|
| 936 |
const bool was_active = is_active(); |
| 937 |
m_state = state; |
| 938 |
const bool is_active_now = is_active(); |
| 939 |
|
| 940 |
if (was_active && !is_active_now) { |
| 941 |
// De-register - transiting to active state |
| 942 |
log_consumer_unregister(log, &m_log_consumer); |
| 943 |
} else if (!was_active && is_active_now) { |
| 944 |
// Register - transitioning inactive state |
| 945 |
log_consumer_register(log, &m_log_consumer); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
void Arch_Log_Sys::async_abort_if_below(lsn_t requested_lsn) { |
| 950 |
m_abort_if_below_lsn.store(requested_lsn); |
| 951 |
os_event_set(log_archiver_thread_event); |
| 952 |
} |
| 953 |
|
| 954 |
const std::string &Arch_log_consumer::get_name() const { |
| 955 |
static std::string name{"log_archiver"}; |
| 956 |
return name; |
| 957 |
} |
| 958 |
|
| 959 |
lsn_t Arch_log_consumer::get_consumed_lsn() const { |
| 960 |
ut_a(arch_log_sys != nullptr); |
| 961 |
ut_a(arch_log_sys->is_active()); |
| 962 |
|
| 963 |
lsn_t archiver_lsn = arch_log_sys->get_archived_lsn(); |
| 964 |
ut_a(archiver_lsn % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 965 |
|
| 966 |
return archiver_lsn; |
| 967 |
} |
| 968 |
|
| 969 |
void Arch_log_consumer::consumption_requested(lsn_t request_lsn) { |
| 970 |
/* The logic below accesses m_last_rushed_at and m_problem_started_at to |
| 971 |
make comparison and arithmetic which assumes nobody else is modifying them |
| 972 |
concurrently, so we need to protect it with some mutex. Thankfully, |
| 973 |
the contract of consumption_requested() requires following both mutexes to be |
| 974 |
held by the caller, and any single one of them would be sufficient here. */ |
| 975 |
ut_ad(log_files_mutex_own(*log_sys)); |
| 976 |
ut_ad(log_limits_mutex_own(*log_sys)); |
| 977 |
arch_wake_threads(); |
| 978 |
|
| 979 |
/* If we are lagging behind, the consumption_requested() will be called with a |
| 980 |
frequency unknown to us, but hopefully at least once per second. We want to |
| 981 |
abort if the problem lasts for longer than 2 seconds. The challenge here is to |
| 982 |
define "a single instance of the problem", as the user might attempt CLONE |
| 983 |
operation many times, and also we might make very slow but non-zero progress, |
| 984 |
yet all we observe here are individual moments where "a problem" existed. We |
| 985 |
use the following approach: |
| 986 |
1. If two calls are not further than 2 seconds apart, we consider them to be |
| 987 |
part of the same problem. A gap longer than 2 seconds between consecutive |
| 988 |
calls, means the later call is for a different incident than the previous one. |
| 989 |
2. If a single incident lasts longer than 2 seconds, we take an action. */ |
| 990 |
const auto now = std::chrono::system_clock::now(); |
| 991 |
if (m_last_rushed_at + std::chrono::seconds(2) < now) { |
| 992 |
m_problem_started_at = now; |
| 993 |
} |
| 994 |
m_last_rushed_at = now; |
| 995 |
if (m_problem_started_at + std::chrono::seconds(2) < now) { |
| 996 |
arch_log_sys->async_abort_if_below(request_lsn); |
| 997 |
} |
| 998 |
} |