| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2014, 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 include/ut0stage.h |
| 29 |
Supplementary code to performance schema stage instrumentation. |
| 30 |
|
| 31 |
Created Nov 12, 2014 Vasil Dimov |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef ut0stage_h |
| 35 |
#define ut0stage_h |
| 36 |
|
| 37 |
#include <math.h> |
| 38 |
#include <algorithm> |
| 39 |
|
| 40 |
#include "dict0mem.h" /* dict_index_t */ |
| 41 |
#include "mysql/psi/mysql_stage.h" /* mysql_stage_inc_work_completed */ |
| 42 |
#include "row0log.h" /* row_log_estimate_work() */ |
| 43 |
#include "srv0srv.h" /* Alter_stage */ |
| 44 |
|
| 45 |
// Forward declaration. |
| 46 |
class Alter_stage; |
| 47 |
using Alter_stages = std::vector<Alter_stage *, ut::allocator<Alter_stage *>>; |
| 48 |
|
| 49 |
#ifdef HAVE_PSI_STAGE_INTERFACE |
| 50 |
|
| 51 |
/** Class used to report ALTER TABLE progress via performance_schema. |
| 52 |
The only user of this class is the ALTER TABLE code and it calls the methods |
| 53 |
in the following order |
| 54 |
constructor |
| 55 |
begin_phase_read_pk() |
| 56 |
multiple times: |
| 57 |
n_pk_recs_inc() // once per record read |
| 58 |
inc(1) // once per page read |
| 59 |
end_phase_read_pk() |
| 60 |
if any new indexes are being added, for each one: |
| 61 |
begin_phase_sort() |
| 62 |
multiple times: |
| 63 |
inc(1) // once per every m_recs_req_for_prog records sorted |
| 64 |
begin_phase_insert() |
| 65 |
multiple times: |
| 66 |
inc(1) // once per every m_recs_req_for_prog records inserted |
| 67 |
being_phase_log_index() |
| 68 |
multiple times: |
| 69 |
inc() // once per log-block applied |
| 70 |
begin_phase_flush() |
| 71 |
multiple times: |
| 72 |
inc() // once per page flushed |
| 73 |
begin_phase_log_table() |
| 74 |
multiple times: |
| 75 |
inc() // once per log-block applied |
| 76 |
begin_phase_end() |
| 77 |
destructor |
| 78 |
|
| 79 |
This class knows the specifics of each phase and tries to increment the |
| 80 |
progress in an even manner across the entire ALTER TABLE lifetime. */ |
| 81 |
class Alter_stage { |
| 82 |
public: |
| 83 |
/** Constructor. |
| 84 |
@param[in] pk primary key of the old table */ |
| 85 |
explicit Alter_stage(const dict_index_t *pk) noexcept |
| 86 |
: m_pk(pk), m_recs_req_for_prog(1), m_cur_phase(NOT_STARTED) {} |
| 87 |
|
| 88 |
/** Copy constructor. "Inherits" the current state of rhs. |
| 89 |
@param[in] rhs Instance to copy current state from. */ |
| 90 |
explicit Alter_stage(const Alter_stage &rhs) noexcept; |
| 91 |
|
| 92 |
/** Destructor. */ |
| 93 |
~Alter_stage(); |
| 94 |
|
| 95 |
/** Flag an ALTER TABLE start (read primary key phase). |
| 96 |
@param[in] n_sort_indexes number of indexes that will be sorted |
| 97 |
during ALTER TABLE, used for estimating the total work to be done */ |
| 98 |
void begin_phase_read_pk(size_t n_sort_indexes); |
| 99 |
|
| 100 |
/** Increment the number of records in PK (table) with 1. |
| 101 |
This is used to get more accurate estimate about the number of |
| 102 |
records per page which is needed because some phases work on |
| 103 |
per-page basis while some work on per-record basis and we want |
| 104 |
to get the progress as even as possible. |
| 105 |
@param[in] n Number of new records to report. */ |
| 106 |
void n_pk_recs_inc(uint64_t n = 1); |
| 107 |
|
| 108 |
/** Flag either one record or one page processed, depending on the |
| 109 |
current phase. |
| 110 |
@param[in] inc_val flag this many units processed at once */ |
| 111 |
void inc(uint64_t inc_val); |
| 112 |
|
| 113 |
/** Increment the progress if we have crossed the threshold |
| 114 |
for unreported records, or if it is the last report. |
| 115 |
@param[in, out] unreported_recs Number of records not updated to |
| 116 |
Alter_stage up till now. |
| 117 |
After reporting sets itself to 0. |
| 118 |
@param[in] is_last_report if true, update the status irrespective |
| 119 |
of number of unreported_recs. |
| 120 |
*/ |
| 121 |
void inc_progress_if_needed(uint64_t &unreported_recs, |
| 122 |
bool is_last_report = false); |
| 123 |
|
| 124 |
/** Flag the end of reading of the primary key. |
| 125 |
Here we know the exact number of pages and records and calculate |
| 126 |
the number of records per page and refresh the estimate. */ |
| 127 |
void end_phase_read_pk(); |
| 128 |
|
| 129 |
/** Flag the beginning of the sort phase. |
| 130 |
@param[in] sort_multi_factor since merge sort processes |
| 131 |
one page more than once we only update the estimate once per this |
| 132 |
many pages processed. */ |
| 133 |
void begin_phase_sort(double sort_multi_factor); |
| 134 |
|
| 135 |
/** Flag the beginning of the insert phase. */ |
| 136 |
void begin_phase_insert(); |
| 137 |
|
| 138 |
/** Flag the beginning of the flush phase. |
| 139 |
@param[in] n_flush_pages this many pages are going to be |
| 140 |
flushed */ |
| 141 |
void begin_phase_flush(page_no_t n_flush_pages); |
| 142 |
|
| 143 |
/** Flag the beginning of the log index phase. */ |
| 144 |
void begin_phase_log_index(); |
| 145 |
|
| 146 |
/** Flag the beginning of the log table phase. */ |
| 147 |
void begin_phase_log_table(); |
| 148 |
|
| 149 |
/** Flag the beginning of the end phase. */ |
| 150 |
void begin_phase_end(); |
| 151 |
|
| 152 |
/** Aggregate the results of the build from the sub builds. |
| 153 |
@param[in] alter_stages Sub stages to aggregate. */ |
| 154 |
void aggregate(const Alter_stages &alter_stages) noexcept; |
| 155 |
|
| 156 |
private: |
| 157 |
/** Check whether we have seen enough records to increment the progress. |
| 158 |
@param[in] inc_val this many units processed up til now |
| 159 |
@return true, if we have seen records as much to increment the progress. */ |
| 160 |
bool should_inc_progress(uint64_t inc_val) const; |
| 161 |
|
| 162 |
/** Update the estimate of total work to be done. */ |
| 163 |
void reestimate(); |
| 164 |
|
| 165 |
/** Change the current phase. |
| 166 |
@param[in] new_stage pointer to the new stage to change to */ |
| 167 |
void change_phase(const PSI_stage_info *new_stage); |
| 168 |
|
| 169 |
private: |
| 170 |
using Counter = std::pair<uint64_t, uint64_t>; |
| 171 |
using Progress = std::pair<PSI_stage_progress *, Counter>; |
| 172 |
using Stage = std::pair<const PSI_stage_info *, Progress>; |
| 173 |
using Stages = std::vector<Stage, ut::allocator<Stage>>; |
| 174 |
|
| 175 |
/** Progress counters for the various stages. */ |
| 176 |
Stage m_stage{}; |
| 177 |
|
| 178 |
/** Collection of all [previous + current] stages. |
| 179 |
Can only be modified between stages. */ |
| 180 |
Stages m_stages{}; |
| 181 |
|
| 182 |
/** Mutex required to update values for m_n_pk_pages |
| 183 |
and m_n_flush_pages. |
| 184 |
We also use this mutex for reestimating & updating the progress, |
| 185 |
as it is done parallelly by multiple threads.*/ |
| 186 |
IB_mutex m_mutex{LATCH_ID_ALTER_STAGE}; |
| 187 |
|
| 188 |
/** Old table PK. Used for calculating the estimate. */ |
| 189 |
const dict_index_t *m_pk{}; |
| 190 |
|
| 191 |
/** Number of records in the primary key (table), including delete |
| 192 |
marked records. */ |
| 193 |
std::atomic<uint64_t> m_n_pk_recs{}; |
| 194 |
|
| 195 |
/** Number of leaf pages in the primary key. Protected by m_mutex. */ |
| 196 |
page_no_t m_n_pk_pages{}; |
| 197 |
|
| 198 |
/** Estimated number of records per page in the primary key. |
| 199 |
Can only be modified between stages. */ |
| 200 |
double m_n_recs_per_page{}; |
| 201 |
|
| 202 |
/** Number of records for which we increment the progress. |
| 203 |
For READ_PK phase 1 |
| 204 |
For SORT phase m_n_recs_per_page * m_sort_multi_factor |
| 205 |
For INSERT phase m_n_recs_per_page |
| 206 |
Can only be modified between stages. */ |
| 207 |
uint64_t m_recs_req_for_prog{}; |
| 208 |
|
| 209 |
/** Number of indexes that are being added. |
| 210 |
Is only set during begin_phase_read_pk(). */ |
| 211 |
size_t m_n_sort_indexes{}; |
| 212 |
|
| 213 |
/** During the sort phase, increment the counter once per this |
| 214 |
many pages processed. This is because sort processes one page more |
| 215 |
than once. Can only be modified between stages. */ |
| 216 |
uint64_t m_sort_multi_factor{}; |
| 217 |
|
| 218 |
/** Number of pages to flush. Protected by m_mutex. */ |
| 219 |
page_no_t m_n_flush_pages{}; |
| 220 |
|
| 221 |
/** Current phase. */ |
| 222 |
enum { |
| 223 |
/** Init phase. */ |
| 224 |
NOT_STARTED = 0, |
| 225 |
|
| 226 |
/** Scan phase. */ |
| 227 |
READ_PK = 1, |
| 228 |
|
| 229 |
/** Sort phase. */ |
| 230 |
SORT = 2, |
| 231 |
|
| 232 |
/** Bulk load/insert phase. */ |
| 233 |
INSERT = 3, |
| 234 |
|
| 235 |
/** Flush non-redo logged pages phase. */ |
| 236 |
FLUSH = 4, |
| 237 |
|
| 238 |
/** Apply entries from the row log to the index after creation. */ |
| 239 |
LOG_INDEX = 5, |
| 240 |
|
| 241 |
/** Apply entries from the row log to the table after the build. */ |
| 242 |
LOG_TABLE = 6, |
| 243 |
|
| 244 |
/** End/Stop. */ |
| 245 |
END = 7, |
| 246 |
} m_cur_phase{NOT_STARTED}; |
| 247 |
}; |
| 248 |
|
| 249 |
inline Alter_stage::Alter_stage(const Alter_stage &rhs) noexcept |
| 250 |
: m_pk(rhs.m_pk), m_recs_req_for_prog(1), m_cur_phase(NOT_STARTED) {} |
| 251 |
|
| 252 |
inline Alter_stage::~Alter_stage() { |
| 253 |
auto progress = m_stage.second.first; |
| 254 |
|
| 255 |
if (progress == nullptr) { |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
/* Set completed = estimated before we quit. */ |
| 260 |
mysql_stage_set_work_completed(progress, |
| 261 |
mysql_stage_get_work_estimated(progress)); |
| 262 |
|
| 263 |
mysql_end_stage(); |
| 264 |
} |
| 265 |
|
| 266 |
inline void Alter_stage::n_pk_recs_inc(uint64_t n) { |
| 267 |
m_n_pk_recs.fetch_add(n, std::memory_order_relaxed); |
| 268 |
} |
| 269 |
|
| 270 |
inline void Alter_stage::inc(uint64_t inc_val) { |
| 271 |
if (m_stages.empty()) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
ut_a(m_cur_phase != NOT_STARTED); |
| 276 |
{ |
| 277 |
IB_mutex_guard guard(&m_mutex, UT_LOCATION_HERE); |
| 278 |
if (m_cur_phase == READ_PK) { |
| 279 |
ut_ad(inc_val == 1); |
| 280 |
++m_n_pk_pages; |
| 281 |
|
| 282 |
/* Overall the read pk phase will read all the pages from the |
| 283 |
PK and will do work, proportional to the number of added |
| 284 |
indexes, thus when this is called once per read page we |
| 285 |
increment with 1 + m_n_sort_indexes */ |
| 286 |
inc_val = 1 + m_n_sort_indexes; |
| 287 |
} |
| 288 |
|
| 289 |
auto progress = m_stages.back().second.first; |
| 290 |
mysql_stage_inc_work_completed(progress, inc_val); |
| 291 |
} |
| 292 |
reestimate(); |
| 293 |
} |
| 294 |
|
| 295 |
inline void Alter_stage::inc_progress_if_needed(uint64_t &unreported_recs, |
| 296 |
bool is_last_report) { |
| 297 |
if (m_stages.empty()) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
if (should_inc_progress(unreported_recs) || is_last_report) { |
| 302 |
/* Total estimate is based on pages. See reestimate() |
| 303 |
Now that we have done task equivalent to 1 page, |
| 304 |
increment progress by 1 */ |
| 305 |
inc(1); |
| 306 |
/* All the rows have been updated to the progress. */ |
| 307 |
unreported_recs = 0; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
inline void Alter_stage::begin_phase_read_pk(size_t n_sort_indexes) { |
| 312 |
m_cur_phase = READ_PK; |
| 313 |
|
| 314 |
m_n_sort_indexes = n_sort_indexes; |
| 315 |
|
| 316 |
Stage stage{}; |
| 317 |
|
| 318 |
stage.first = &srv_stage_alter_table_read_pk_internal_sort; |
| 319 |
stage.second.first = mysql_set_stage(stage.first->m_key); |
| 320 |
|
| 321 |
if (stage.second.first != nullptr) { |
| 322 |
m_stages.push_back(stage); |
| 323 |
|
| 324 |
auto progress = stage.second.first; |
| 325 |
mysql_stage_set_work_completed(progress, 0); |
| 326 |
|
| 327 |
reestimate(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
inline void Alter_stage::end_phase_read_pk() { |
| 332 |
reestimate(); |
| 333 |
|
| 334 |
/* PK reading is complete, and we have the final value for |
| 335 |
m_n_pk_recs & m_n_pk_pages to calculate m_n_recs_per_page. */ |
| 336 |
if (m_n_pk_pages == 0) { |
| 337 |
/* The number of pages in the PK could be 0 if the tree is |
| 338 |
empty. In this case we set m_n_recs_per_page to 1 to avoid |
| 339 |
division by zero later. */ |
| 340 |
m_n_recs_per_page = 1.0; |
| 341 |
} else { |
| 342 |
m_n_recs_per_page = std::max( |
| 343 |
static_cast<double>(m_n_pk_recs.load(std::memory_order_relaxed)) / |
| 344 |
m_n_pk_pages, |
| 345 |
1.0); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
inline void Alter_stage::begin_phase_sort(double sort_multi_factor) { |
| 350 |
if (sort_multi_factor <= 1.0) { |
| 351 |
m_sort_multi_factor = 1; |
| 352 |
} else { |
| 353 |
m_sort_multi_factor = static_cast<uint64_t>(round(sort_multi_factor)); |
| 354 |
} |
| 355 |
m_recs_req_for_prog = |
| 356 |
static_cast<uint64_t>(m_sort_multi_factor * m_n_recs_per_page); |
| 357 |
change_phase(&srv_stage_alter_table_merge_sort); |
| 358 |
} |
| 359 |
|
| 360 |
inline void Alter_stage::begin_phase_insert() { |
| 361 |
change_phase(&srv_stage_alter_table_insert); |
| 362 |
m_recs_req_for_prog = static_cast<uint64_t>(m_n_recs_per_page); |
| 363 |
} |
| 364 |
|
| 365 |
inline void Alter_stage::begin_phase_flush(page_no_t n_flush_pages) { |
| 366 |
{ |
| 367 |
IB_mutex_guard guard(&m_mutex, UT_LOCATION_HERE); |
| 368 |
m_n_flush_pages = n_flush_pages; |
| 369 |
} |
| 370 |
|
| 371 |
reestimate(); |
| 372 |
change_phase(&srv_stage_alter_table_flush); |
| 373 |
} |
| 374 |
|
| 375 |
inline void Alter_stage::begin_phase_log_index() { |
| 376 |
change_phase(&srv_stage_alter_table_log_index); |
| 377 |
} |
| 378 |
|
| 379 |
inline void Alter_stage::begin_phase_log_table() { |
| 380 |
change_phase(&srv_stage_alter_table_log_table); |
| 381 |
} |
| 382 |
|
| 383 |
inline void Alter_stage::begin_phase_end() { |
| 384 |
change_phase(&srv_stage_alter_table_end); |
| 385 |
} |
| 386 |
|
| 387 |
inline bool Alter_stage::should_inc_progress(uint64_t inc_val) const { |
| 388 |
/* We only need to increment the progress when we have seen |
| 389 |
m_recs_req_for_prog. */ |
| 390 |
return inc_val >= m_recs_req_for_prog; |
| 391 |
} |
| 392 |
|
| 393 |
inline void Alter_stage::reestimate() { |
| 394 |
if (m_stages.empty()) { |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
IB_mutex_guard guard(&m_mutex, UT_LOCATION_HERE); |
| 399 |
/* During the log table phase we calculate the estimate as |
| 400 |
work done so far + log size remaining. */ |
| 401 |
if (m_cur_phase == LOG_TABLE) { |
| 402 |
auto progress = m_stages.back().second.first; |
| 403 |
|
| 404 |
mysql_stage_set_work_estimated( |
| 405 |
progress, |
| 406 |
mysql_stage_get_work_completed(progress) + row_log_estimate_work(m_pk)); |
| 407 |
|
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
/* During the other phases we use a formula, regardless of |
| 412 |
how much work has been done so far. */ |
| 413 |
|
| 414 |
/* For number of pages in the PK - if the PK has not been |
| 415 |
read yet, use stat_n_leaf_pages (approximate), otherwise |
| 416 |
use the exact number we gathered. */ |
| 417 |
const page_no_t n_pk_pages = |
| 418 |
m_cur_phase != READ_PK ? m_n_pk_pages : m_pk->stat_n_leaf_pages; |
| 419 |
|
| 420 |
/* If flush phase has not started yet and we do not know how |
| 421 |
many pages are to be flushed, then use a wild guess - the |
| 422 |
number of pages in the PK / 2. */ |
| 423 |
if (m_n_flush_pages == 0) { |
| 424 |
m_n_flush_pages = n_pk_pages / 2; |
| 425 |
} |
| 426 |
|
| 427 |
uint64_t estimate = |
| 428 |
n_pk_pages * |
| 429 |
(1 /* read PK */ |
| 430 |
+ m_n_sort_indexes /* row_merge_buf_sort() inside the |
| 431 |
read PK per created index */ |
| 432 |
+ m_n_sort_indexes * 2 /* sort & insert per created index */) + |
| 433 |
m_n_flush_pages + row_log_estimate_work(m_pk); |
| 434 |
|
| 435 |
auto progress = m_stages.back().second.first; |
| 436 |
const auto completed = (uint64_t)mysql_stage_get_work_completed(progress); |
| 437 |
|
| 438 |
/* Prevent estimate < completed */ |
| 439 |
mysql_stage_set_work_estimated(progress, std::max(estimate, completed)); |
| 440 |
} |
| 441 |
|
| 442 |
inline void Alter_stage::change_phase(const PSI_stage_info *new_stage) { |
| 443 |
if (m_stages.empty()) { |
| 444 |
return; |
| 445 |
} |
| 446 |
|
| 447 |
ut_a(new_stage != &srv_stage_alter_table_read_pk_internal_sort); |
| 448 |
|
| 449 |
if (new_stage == &srv_stage_alter_table_merge_sort) { |
| 450 |
m_cur_phase = SORT; |
| 451 |
} else if (new_stage == &srv_stage_alter_table_insert) { |
| 452 |
m_cur_phase = INSERT; |
| 453 |
} else if (new_stage == &srv_stage_alter_table_flush) { |
| 454 |
m_cur_phase = FLUSH; |
| 455 |
} else if (new_stage == &srv_stage_alter_table_log_index) { |
| 456 |
m_cur_phase = LOG_INDEX; |
| 457 |
} else if (new_stage == &srv_stage_alter_table_log_table) { |
| 458 |
m_cur_phase = LOG_TABLE; |
| 459 |
} else if (new_stage == &srv_stage_alter_table_end) { |
| 460 |
m_cur_phase = END; |
| 461 |
} else { |
| 462 |
ut_error; |
| 463 |
} |
| 464 |
|
| 465 |
auto progress = m_stages.back().second.first; |
| 466 |
|
| 467 |
const auto c = mysql_stage_get_work_completed(progress); |
| 468 |
const auto e = mysql_stage_get_work_estimated(progress); |
| 469 |
|
| 470 |
Stage stage{new_stage, {mysql_set_stage(new_stage->m_key), {}}}; |
| 471 |
|
| 472 |
if (stage.second.first != nullptr) { |
| 473 |
m_stages.push_back(stage); |
| 474 |
|
| 475 |
auto &counter = m_stages.back().second.second; |
| 476 |
|
| 477 |
counter.first = c; |
| 478 |
counter.second = e; |
| 479 |
|
| 480 |
mysql_stage_set_work_completed(stage.second.first, c); |
| 481 |
mysql_stage_set_work_estimated(stage.second.first, e); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
inline void Alter_stage::aggregate(const Alter_stages &alter_stages) noexcept { |
| 486 |
if (alter_stages.empty()) { |
| 487 |
return; |
| 488 |
} |
| 489 |
|
| 490 |
ut_a(m_cur_phase == NOT_STARTED); |
| 491 |
|
| 492 |
Stage cur_stage{}; |
| 493 |
|
| 494 |
for (auto alter_stage : alter_stages) { |
| 495 |
alter_stage->begin_phase_end(); |
| 496 |
|
| 497 |
for (auto stage : alter_stage->m_stages) { |
| 498 |
if (stage.first == &srv_stage_alter_table_end) { |
| 499 |
continue; |
| 500 |
} |
| 501 |
auto progress = mysql_set_stage(stage.first->m_key); |
| 502 |
|
| 503 |
if (progress == nullptr) { |
| 504 |
/* The user can disable the instrument clas and that can return |
| 505 |
nullptr. That forces us to skip and will break the state transitions |
| 506 |
and counts.*/ |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
auto c = mysql_stage_get_work_completed(progress); |
| 511 |
auto e = mysql_stage_get_work_estimated(progress); |
| 512 |
|
| 513 |
const auto &counter = stage.second.second; |
| 514 |
|
| 515 |
c += counter.first; |
| 516 |
e += counter.second; |
| 517 |
|
| 518 |
mysql_stage_set_work_completed(progress, c); |
| 519 |
mysql_stage_set_work_estimated(progress, e); |
| 520 |
|
| 521 |
if (stage.first == &srv_stage_alter_table_read_pk_internal_sort) { |
| 522 |
if ((int)m_cur_phase < (int)READ_PK) { |
| 523 |
m_cur_phase = READ_PK; |
| 524 |
cur_stage.first = stage.first; |
| 525 |
cur_stage.second.first = progress; |
| 526 |
} |
| 527 |
} else if (stage.first == &srv_stage_alter_table_merge_sort) { |
| 528 |
if ((int)m_cur_phase < (int)NOT_STARTED) { |
| 529 |
m_cur_phase = SORT; |
| 530 |
cur_stage.first = stage.first; |
| 531 |
cur_stage.second.first = progress; |
| 532 |
} |
| 533 |
} else if (stage.first == &srv_stage_alter_table_insert) { |
| 534 |
if ((int)m_cur_phase < (int)SORT) { |
| 535 |
m_cur_phase = INSERT; |
| 536 |
cur_stage.first = stage.first; |
| 537 |
cur_stage.second.first = progress; |
| 538 |
} |
| 539 |
} else if (stage.first == &srv_stage_alter_table_log_index) { |
| 540 |
if ((int)m_cur_phase < (int)LOG_INDEX) { |
| 541 |
m_cur_phase = LOG_INDEX; |
| 542 |
cur_stage.first = stage.first; |
| 543 |
cur_stage.second.first = progress; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
ut_a(stage.first != &srv_stage_alter_table_flush); |
| 548 |
ut_a(stage.first != &srv_stage_alter_table_log_table); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if (cur_stage.first != nullptr) { |
| 553 |
ut_a(cur_stage.second.first != nullptr); |
| 554 |
m_stages.push_back(cur_stage); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/** class to monitor the progress of 'ALTER TABLESPACE ENCRYPTION' in terms |
| 559 |
of number of pages operated upon. */ |
| 560 |
class Alter_stage_ts { |
| 561 |
public: |
| 562 |
/** Constructor. */ |
| 563 |
Alter_stage_ts() |
| 564 |
: m_progress(nullptr), |
| 565 |
m_work_estimated(0), |
| 566 |
m_work_done(0), |
| 567 |
m_cur_phase(NOT_STARTED) {} |
| 568 |
|
| 569 |
/** Destructor. */ |
| 570 |
inline ~Alter_stage_ts() { |
| 571 |
if (m_progress == nullptr) { |
| 572 |
return; |
| 573 |
} |
| 574 |
mysql_end_stage(); |
| 575 |
} |
| 576 |
|
| 577 |
/** Initialize. |
| 578 |
@param[in] key PFS key. */ |
| 579 |
void init(int key) { |
| 580 |
ut_ad(key != -1); |
| 581 |
ut_ad(m_cur_phase == NOT_STARTED); |
| 582 |
|
| 583 |
m_progress = nullptr; |
| 584 |
m_work_estimated = 0; |
| 585 |
m_work_done = 0; |
| 586 |
|
| 587 |
m_progress = mysql_set_stage(key); |
| 588 |
/* Change phase to INITIATED */ |
| 589 |
change_phase(); |
| 590 |
} |
| 591 |
|
| 592 |
/** Set estimate. |
| 593 |
@param[in] units Units. */ |
| 594 |
void set_estimate(uint64_t units) { |
| 595 |
if (m_progress == nullptr) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
ut_ad(m_cur_phase == INITIATED); |
| 600 |
m_work_estimated = units; |
| 601 |
mysql_stage_set_work_estimated(m_progress, m_work_estimated); |
| 602 |
/* Change phase to WORK_ESTIMATED */ |
| 603 |
change_phase(); |
| 604 |
} |
| 605 |
|
| 606 |
/** Update the progress. |
| 607 |
@param[in] units Update delta. */ |
| 608 |
void update_work(uint64_t units) { |
| 609 |
if (m_progress == nullptr) { |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
ut_ad(m_cur_phase == WORK_ESTIMATED); |
| 614 |
|
| 615 |
m_work_done += units; |
| 616 |
ut_ad(m_work_done <= m_work_estimated); |
| 617 |
mysql_stage_set_work_completed(m_progress, m_work_done); |
| 618 |
|
| 619 |
if (m_work_done == m_work_estimated) { |
| 620 |
/* Change phase to WORK_COMPLETED */ |
| 621 |
change_phase(); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
/** Change phase. */ |
| 626 |
void change_phase() { |
| 627 |
if (m_progress == nullptr) { |
| 628 |
ut_ad(m_cur_phase == NOT_STARTED); |
| 629 |
return; |
| 630 |
} |
| 631 |
|
| 632 |
switch (m_cur_phase) { |
| 633 |
case NOT_STARTED: |
| 634 |
m_cur_phase = INITIATED; |
| 635 |
break; |
| 636 |
case INITIATED: |
| 637 |
m_cur_phase = WORK_ESTIMATED; |
| 638 |
break; |
| 639 |
case WORK_ESTIMATED: |
| 640 |
m_cur_phase = WORK_COMPLETED; |
| 641 |
break; |
| 642 |
case WORK_COMPLETED: |
| 643 |
default: |
| 644 |
ut_error; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
bool is_completed() { |
| 649 |
if (m_progress == nullptr) { |
| 650 |
return true; |
| 651 |
} else { |
| 652 |
return (m_cur_phase == WORK_COMPLETED); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
private: |
| 657 |
/** Performance schema accounting object. */ |
| 658 |
PSI_stage_progress *m_progress; |
| 659 |
|
| 660 |
/** Number of pages to be (un)encrypted . */ |
| 661 |
page_no_t m_work_estimated; |
| 662 |
|
| 663 |
/** Number of pages already (un)encrypted . */ |
| 664 |
page_no_t m_work_done; |
| 665 |
|
| 666 |
/** Current phase. */ |
| 667 |
enum { |
| 668 |
/** Not open phase. */ |
| 669 |
NOT_STARTED = 0, |
| 670 |
|
| 671 |
/** Initialised. */ |
| 672 |
INITIATED = 1, |
| 673 |
|
| 674 |
/** Work estimated phase. */ |
| 675 |
WORK_ESTIMATED = 2, |
| 676 |
|
| 677 |
/** Work completed phase. */ |
| 678 |
WORK_COMPLETED = 3, |
| 679 |
} m_cur_phase; |
| 680 |
}; |
| 681 |
|
| 682 |
#else /* HAVE_PSI_STAGE_INTERFACE */ |
| 683 |
|
| 684 |
/** Dummy alter stage. */ |
| 685 |
class Alter_stage { |
| 686 |
public: |
| 687 |
/** Constructor. */ |
| 688 |
explicit Alter_stage(const dict_index_t *pk) {} |
| 689 |
|
| 690 |
/** Setup the number of indexes to read. |
| 691 |
@param[in] n_sort_indexes Number of indexe.s */ |
| 692 |
void begin_phase_read_pk(size_t n_sort_indexes) {} |
| 693 |
|
| 694 |
/** Increments the number of rows read so far. */ |
| 695 |
void n_pk_recs_inc() {} |
| 696 |
|
| 697 |
/** Increments the number of rows read so far. */ |
| 698 |
void n_pk_recs_inc(uint64_t) {} |
| 699 |
|
| 700 |
/** Increment depending on stage. */ |
| 701 |
void inc(uint64_t inc_val = 1) {} |
| 702 |
|
| 703 |
/** Increment the progress if we have crossed the threshold |
| 704 |
for unreported records. */ |
| 705 |
void inc_progress_if_needed(uint64_t &unreported_recs, |
| 706 |
bool is_last_report = false) {} |
| 707 |
|
| 708 |
/** End scan phase. */ |
| 709 |
void end_phase_read_pk() {} |
| 710 |
|
| 711 |
/** Begin merge sort phase. */ |
| 712 |
void begin_phase_sort(double sort_multi_factor) {} |
| 713 |
|
| 714 |
/** Begin insert phase. */ |
| 715 |
void begin_phase_insert() {} |
| 716 |
|
| 717 |
/** Begin flushing of non-redo logged pages. |
| 718 |
@param[in] n_flush_pages Number of pages to flush. */ |
| 719 |
void begin_phase_flush(page_no_t n_flush_pages) {} |
| 720 |
|
| 721 |
/** Begin row log apply phase to the index. */ |
| 722 |
void begin_phase_log_index() {} |
| 723 |
|
| 724 |
/** Begin row log apply phase to the table. */ |
| 725 |
void begin_phase_log_table() {} |
| 726 |
|
| 727 |
/** Build end phase. */ |
| 728 |
void begin_phase_end() {} |
| 729 |
|
| 730 |
/** Aggregate the sub stages.. |
| 731 |
@param[in] stages Stages to aggregate. */ |
| 732 |
void aggregate(const Alter_stages &alter_stages) noexcept {} |
| 733 |
}; |
| 734 |
|
| 735 |
class Alter_stage_ts { |
| 736 |
public: |
| 737 |
/** Constructor. */ |
| 738 |
Alter_stage_ts() {} |
| 739 |
|
| 740 |
/** Destructor. */ |
| 741 |
inline ~Alter_stage_ts() {} |
| 742 |
|
| 743 |
/** Initialize. |
| 744 |
@param[in] key PFS key. */ |
| 745 |
void init(int key) {} |
| 746 |
|
| 747 |
/** Set estimate. |
| 748 |
@param[in] units Units. */ |
| 749 |
void set_estimate(uint units) {} |
| 750 |
|
| 751 |
/** Update the progress. |
| 752 |
@param[in] units Update delta. */ |
| 753 |
void update_work(uint units) {} |
| 754 |
|
| 755 |
/** Change phase. */ |
| 756 |
void change_phase() {} |
| 757 |
}; |
| 758 |
#endif /* HAVE_PSI_STAGE_INTERFACE */ |
| 759 |
|
| 760 |
#endif /* ut0stage_h */ |