| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2019, 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 row/row0pread-histogram.cc |
| 29 |
Parallel read histogram interface implementation |
| 30 |
|
| 31 |
Created 2019-04-20 by Darshan M N */ |
| 32 |
|
| 33 |
#include "row0pread-histogram.h" |
| 34 |
#include "row0row.h" |
| 35 |
#include "row0sel.h" |
| 36 |
#include "srv0srv.h" |
| 37 |
|
| 38 |
#ifdef UNIV_DEBUG |
| 39 |
#include <current_thd.h> |
| 40 |
#endif /* UNIV_DEBUG */ |
| 41 |
|
| 42 |
std::uniform_real_distribution<double> Histogram_sampler::m_distribution(0, |
| 43 |
100); |
| 44 |
|
| 45 |
Histogram_sampler::Histogram_sampler(size_t max_threads, int sampling_seed, |
| 46 |
double sampling_percentage, |
| 47 |
enum_sampling_method sampling_method) |
| 48 |
: m_parallel_reader(max_threads), |
| 49 |
m_random_generator(sampling_seed), |
| 50 |
m_sampling_method(sampling_method), |
| 51 |
m_sampling_percentage(sampling_percentage), |
| 52 |
m_sampling_seed(sampling_seed) { |
| 53 |
ut_ad(max_threads == 1); |
| 54 |
|
| 55 |
m_start_buffer_event = os_event_create(); |
| 56 |
m_end_buffer_event = os_event_create(); |
| 57 |
|
| 58 |
os_event_reset(m_start_buffer_event); |
| 59 |
os_event_reset(m_end_buffer_event); |
| 60 |
|
| 61 |
m_n_sampled = 0; |
| 62 |
|
| 63 |
#ifdef UNIV_DEBUG |
| 64 |
THD *thd = current_thd; |
| 65 |
#endif /* UNIV_DEBUG */ |
| 66 |
|
| 67 |
m_parallel_reader.set_start_callback( |
| 68 |
[=](Parallel_reader::Thread_ctx *thread_ctx) { |
| 69 |
if (thread_ctx->get_state() == Parallel_reader::State::THREAD) { |
| 70 |
#ifdef UNIV_DEBUG |
| 71 |
/* for debug sync calls */ |
| 72 |
current_thd = thd; |
| 73 |
#endif /* UNIV_DEBUG */ |
| 74 |
return start_callback(thread_ctx); |
| 75 |
} else { |
| 76 |
return DB_SUCCESS; |
| 77 |
} |
| 78 |
}); |
| 79 |
|
| 80 |
m_parallel_reader.set_finish_callback( |
| 81 |
[=](Parallel_reader::Thread_ctx *thread_ctx) { |
| 82 |
if (thread_ctx->get_state() == Parallel_reader::State::THREAD) { |
| 83 |
return finish_callback(thread_ctx); |
| 84 |
} else { |
| 85 |
return DB_SUCCESS; |
| 86 |
} |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
Histogram_sampler::~Histogram_sampler() { |
| 91 |
buffer_end(); |
| 92 |
|
| 93 |
os_event_destroy(m_start_buffer_event); |
| 94 |
os_event_destroy(m_end_buffer_event); |
| 95 |
} |
| 96 |
|
| 97 |
dberr_t Histogram_sampler::start_callback( |
| 98 |
Parallel_reader::Thread_ctx *reader_thread_ctx) { |
| 99 |
ut_a(reader_thread_ctx->get_state() == Parallel_reader::State::THREAD); |
| 100 |
/** There are data members in row_prebuilt_t that cannot be accessed in |
| 101 |
multi-threaded mode e.g., blob_heap. |
| 102 |
|
| 103 |
row_prebuilt_t is designed for single threaded access and to share |
| 104 |
it among threads is not recommended unless "you know what you are doing". |
| 105 |
This is very fragile code as it stands. |
| 106 |
|
| 107 |
To solve the blob heap issue in prebuilt we request parallel reader thread |
| 108 |
to use blob heap per thread and we pass this blob heap to the InnoDB to |
| 109 |
MySQL row format conversion function. */ |
| 110 |
reader_thread_ctx->create_blob_heap(); |
| 111 |
|
| 112 |
return DB_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
dberr_t Histogram_sampler::finish_callback( |
| 116 |
Parallel_reader::Thread_ctx *reader_thread_ctx) { |
| 117 |
ut_a(reader_thread_ctx->get_state() == Parallel_reader::State::THREAD); |
| 118 |
|
| 119 |
DBUG_PRINT("histogram_sampler_buffering_print", ("-> Buffering complete.")); |
| 120 |
|
| 121 |
DBUG_LOG("histogram_sampler_buffering_print", |
| 122 |
"Total number of rows sampled : " |
| 123 |
<< m_n_sampled.load(std::memory_order_relaxed)); |
| 124 |
|
| 125 |
if (is_error_set()) { |
| 126 |
signal_end_of_buffering(); |
| 127 |
return m_err; |
| 128 |
} |
| 129 |
|
| 130 |
wait_for_start_of_buffering(); |
| 131 |
|
| 132 |
auto err = m_parallel_reader.get_error_state(); |
| 133 |
|
| 134 |
set_error_state(err == DB_SUCCESS ? DB_END_OF_INDEX : err); |
| 135 |
|
| 136 |
signal_end_of_buffering(); |
| 137 |
|
| 138 |
return DB_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
bool Histogram_sampler::init(trx_t *trx, dict_index_t *index, |
| 142 |
row_prebuilt_t *prebuilt) { |
| 143 |
mtr_t mtr; |
| 144 |
mtr_start(&mtr); |
| 145 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 146 |
|
| 147 |
/* Read pages from one level above the leaf page. */ |
| 148 |
ulint read_level = btr_height_get(index, &mtr); |
| 149 |
|
| 150 |
mtr_commit(&mtr); |
| 151 |
|
| 152 |
if (read_level > 1) { |
| 153 |
read_level = 1; |
| 154 |
} |
| 155 |
|
| 156 |
Parallel_reader::Scan_range full_scan{}; |
| 157 |
Parallel_reader::Config config(full_scan, index, read_level); |
| 158 |
|
| 159 |
dberr_t err = m_parallel_reader.add_scan( |
| 160 |
trx, config, [=](const Parallel_reader::Ctx *ctx) { |
| 161 |
if (read_level == 0) { |
| 162 |
return (process_leaf_rec(ctx, prebuilt)); |
| 163 |
} else { |
| 164 |
return (process_non_leaf_rec(ctx, prebuilt)); |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
return (err == DB_SUCCESS); |
| 169 |
} |
| 170 |
|
| 171 |
void Histogram_sampler::wait_for_start_of_buffering() { |
| 172 |
os_event_wait(m_start_buffer_event); |
| 173 |
|
| 174 |
os_event_reset(m_start_buffer_event); |
| 175 |
} |
| 176 |
|
| 177 |
void Histogram_sampler::wait_for_end_of_buffering() { |
| 178 |
os_event_wait(m_end_buffer_event); |
| 179 |
|
| 180 |
os_event_reset(m_end_buffer_event); |
| 181 |
} |
| 182 |
|
| 183 |
void Histogram_sampler::signal_start_of_buffering() { |
| 184 |
os_event_set(m_start_buffer_event); |
| 185 |
} |
| 186 |
|
| 187 |
void Histogram_sampler::signal_end_of_buffering() { |
| 188 |
os_event_set(m_end_buffer_event); |
| 189 |
} |
| 190 |
|
| 191 |
bool Histogram_sampler::skip() { |
| 192 |
if (m_sampling_percentage == 0.00) { |
| 193 |
return (true); |
| 194 |
} else if (m_sampling_percentage == 100.00) { |
| 195 |
return (false); |
| 196 |
} |
| 197 |
|
| 198 |
bool ret = false; |
| 199 |
|
| 200 |
switch (m_sampling_method) { |
| 201 |
case enum_sampling_method::SYSTEM: { |
| 202 |
double rand = m_distribution(m_random_generator); |
| 203 |
|
| 204 |
DBUG_PRINT("histogram_sampler_buffering_print", |
| 205 |
("-> New page. Random value generated - %lf", rand)); |
| 206 |
|
| 207 |
/* Check if the records in the block needs to be read for sampling. */ |
| 208 |
if (rand > m_sampling_percentage) { |
| 209 |
ret = true; |
| 210 |
} |
| 211 |
} break; |
| 212 |
|
| 213 |
default: |
| 214 |
ut_d(ut_error); |
| 215 |
ut_o(break); |
| 216 |
} |
| 217 |
|
| 218 |
return (ret); |
| 219 |
} |
| 220 |
|
| 221 |
dberr_t Histogram_sampler::buffer_next() { |
| 222 |
/* Return if the tree is empty. */ |
| 223 |
if (m_parallel_reader.is_tree_empty()) { |
| 224 |
return (DB_END_OF_INDEX); |
| 225 |
} |
| 226 |
|
| 227 |
signal_start_of_buffering(); |
| 228 |
|
| 229 |
wait_for_end_of_buffering(); |
| 230 |
|
| 231 |
if (is_error_set()) { |
| 232 |
/* End of records to be buffered. */ |
| 233 |
m_parallel_reader.join(); |
| 234 |
} |
| 235 |
|
| 236 |
return (m_err); |
| 237 |
} |
| 238 |
|
| 239 |
void Histogram_sampler::buffer_end() { |
| 240 |
set_error_state(DB_END_SAMPLE_READ); |
| 241 |
|
| 242 |
signal_start_of_buffering(); |
| 243 |
|
| 244 |
m_parallel_reader.join(); |
| 245 |
|
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
dberr_t Histogram_sampler::run() { |
| 250 |
return m_parallel_reader.spawn(m_parallel_reader.max_threads()); |
| 251 |
} |
| 252 |
|
| 253 |
dberr_t Histogram_sampler::sample_rec(const Parallel_reader::Ctx *reader_ctx, |
| 254 |
const rec_t *rec, ulint *offsets, |
| 255 |
const dict_index_t *index, |
| 256 |
row_prebuilt_t *prebuilt) { |
| 257 |
dberr_t err{DB_SUCCESS}; |
| 258 |
|
| 259 |
auto reader_thread_ctx = reader_ctx->thread_ctx(); |
| 260 |
|
| 261 |
wait_for_start_of_buffering(); |
| 262 |
|
| 263 |
/* Return as the sampler has been requested to end sampling. */ |
| 264 |
if (m_err == DB_END_SAMPLE_READ) { |
| 265 |
signal_end_of_buffering(); |
| 266 |
return (m_err); |
| 267 |
} |
| 268 |
|
| 269 |
if (row_sel_store_mysql_rec(m_buf, prebuilt, rec, nullptr, true, index, index, |
| 270 |
offsets, false, nullptr, |
| 271 |
reader_thread_ctx->m_blob_heap)) { |
| 272 |
m_n_sampled.fetch_add(1, std::memory_order_relaxed); |
| 273 |
} else { |
| 274 |
err = DB_ERROR; |
| 275 |
ut_d(ut_error); |
| 276 |
} |
| 277 |
|
| 278 |
DBUG_EXECUTE_IF("simulate_sample_read_error", err = DB_ERROR;); |
| 279 |
|
| 280 |
signal_end_of_buffering(); |
| 281 |
|
| 282 |
return (err); |
| 283 |
} |
| 284 |
|
| 285 |
dberr_t Histogram_sampler::process_non_leaf_rec( |
| 286 |
const Parallel_reader::Ctx *ctx_const, row_prebuilt_t *prebuilt) { |
| 287 |
DBUG_EXECUTE_IF("parallel_reader_histogram_induce_error", |
| 288 |
set_error_state(DB_ERROR); |
| 289 |
return DB_ERROR;); |
| 290 |
|
| 291 |
if (skip()) { |
| 292 |
srv_stats.n_sampled_pages_skipped.inc(); |
| 293 |
|
| 294 |
DBUG_PRINT("histogram_sampler_buffering_print", ("Skipping block.")); |
| 295 |
return (DB_SUCCESS); |
| 296 |
} |
| 297 |
|
| 298 |
Parallel_reader::Ctx *ctx = const_cast<Parallel_reader::Ctx *>(ctx_const); |
| 299 |
const dict_index_t *index = ctx->index(); |
| 300 |
|
| 301 |
ut_ad(!page_is_leaf(ctx->m_block->frame)); |
| 302 |
|
| 303 |
/* Get the child page pointed to by the record. */ |
| 304 |
|
| 305 |
srv_stats.n_sampled_pages_read.inc(); |
| 306 |
|
| 307 |
mtr_t mtr; |
| 308 |
mtr.start(); |
| 309 |
mtr.set_log_mode(MTR_LOG_NO_REDO); |
| 310 |
|
| 311 |
buf_block_t *leaf_block = |
| 312 |
btr_node_ptr_get_child(ctx->m_rec, const_cast<dict_index_t *>(index), |
| 313 |
ctx->m_offsets, &mtr, RW_S_LATCH); |
| 314 |
|
| 315 |
ut_ad(page_is_leaf(leaf_block->frame)); |
| 316 |
|
| 317 |
/* Sample all the records in the leaf page. */ |
| 318 |
|
| 319 |
page_cur_t cur; |
| 320 |
page_cur_set_before_first(leaf_block, &cur); |
| 321 |
page_cur_move_to_next(&cur); |
| 322 |
|
| 323 |
auto heap = mem_heap_create(srv_page_size / 4, UT_LOCATION_HERE); |
| 324 |
dberr_t err{DB_SUCCESS}; |
| 325 |
|
| 326 |
for (;;) { |
| 327 |
if (page_cur_is_after_last(&cur)) { |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 332 |
ulint *offsets = offsets_; |
| 333 |
rec_offs_init(offsets_); |
| 334 |
|
| 335 |
const rec_t *rec = page_cur_get_rec(&cur); |
| 336 |
offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED, |
| 337 |
UT_LOCATION_HERE, &heap); |
| 338 |
|
| 339 |
if (ctx->is_rec_visible(rec, offsets, heap, &mtr)) { |
| 340 |
err = sample_rec(ctx, rec, offsets, index, prebuilt); |
| 341 |
|
| 342 |
if (err != DB_SUCCESS) { |
| 343 |
set_error_state(err); |
| 344 |
} |
| 345 |
|
| 346 |
if (is_error_set()) { |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
page_cur_move_to_next(&cur); |
| 352 |
} |
| 353 |
|
| 354 |
mtr.commit(); |
| 355 |
|
| 356 |
mem_heap_free(heap); |
| 357 |
|
| 358 |
return (m_err); |
| 359 |
} |
| 360 |
|
| 361 |
dberr_t Histogram_sampler::process_leaf_rec(const Parallel_reader::Ctx *ctx, |
| 362 |
row_prebuilt_t *prebuilt) { |
| 363 |
ut_ad(page_is_leaf(ctx->m_block->frame)); |
| 364 |
|
| 365 |
if (ctx->m_first_rec) { |
| 366 |
srv_stats.n_sampled_pages_read.inc(); |
| 367 |
} |
| 368 |
|
| 369 |
return sample_rec(ctx, ctx->m_rec, ctx->m_offsets, ctx->index(), prebuilt); |
| 370 |
} |