| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2018, 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-adapter.cc |
| 29 |
Parallel read adapter interface implementation |
| 30 |
|
| 31 |
Created 2018-02-28 by Darshan M N */ |
| 32 |
|
| 33 |
#ifndef UNIV_HOTBACKUP |
| 34 |
|
| 35 |
#include "row0pread-adapter.h" |
| 36 |
#include "row0sel.h" |
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
Parallel_reader_adapter::Parallel_reader_adapter(size_t max_threads, |
| 40 |
ulint rowlen) |
| 41 |
: m_parallel_reader(max_threads) { |
| 42 |
m_batch_size = ADAPTER_SEND_BUFFER_SIZE / rowlen; |
| 43 |
} |
| 44 |
|
| 45 |
dberr_t Parallel_reader_adapter::add_scan(trx_t *trx, |
| 46 |
const Parallel_reader::Config &config, |
| 47 |
Parallel_reader::F &&f) { |
| 48 |
return m_parallel_reader.add_scan(trx, config, std::move(f)); |
| 49 |
} |
| 50 |
|
| 51 |
Parallel_reader_adapter::Thread_ctx::Thread_ctx() { |
| 52 |
m_buffer.resize(ADAPTER_SEND_BUFFER_SIZE); |
| 53 |
} |
| 54 |
|
| 55 |
void Parallel_reader_adapter::set(row_prebuilt_t *prebuilt) { |
| 56 |
ut_a(prebuilt->n_template > 0); |
| 57 |
ut_a(m_mysql_row.m_offsets.empty()); |
| 58 |
ut_a(m_mysql_row.m_null_bit_mask.empty()); |
| 59 |
ut_a(m_mysql_row.m_null_bit_offsets.empty()); |
| 60 |
|
| 61 |
/* Partition structure should be the same across all partitions. |
| 62 |
Therefore MySQL row meta-data is common across all paritions. */ |
| 63 |
|
| 64 |
for (uint i = 0; i < prebuilt->n_template; ++i) { |
| 65 |
const auto &templt = prebuilt->mysql_template[i]; |
| 66 |
|
| 67 |
m_mysql_row.m_offsets.push_back( |
| 68 |
static_cast<ulong>(templt.mysql_col_offset)); |
| 69 |
m_mysql_row.m_null_bit_mask.push_back( |
| 70 |
static_cast<ulong>(templt.mysql_null_bit_mask)); |
| 71 |
m_mysql_row.m_null_bit_offsets.push_back( |
| 72 |
static_cast<ulong>(templt.mysql_null_byte_offset)); |
| 73 |
} |
| 74 |
|
| 75 |
ut_a(m_mysql_row.m_max_len == 0); |
| 76 |
ut_a(prebuilt->mysql_row_len > 0); |
| 77 |
m_mysql_row.m_max_len = static_cast<ulong>(prebuilt->mysql_row_len); |
| 78 |
|
| 79 |
m_parallel_reader.set_start_callback( |
| 80 |
[=](Parallel_reader::Thread_ctx *reader_thread_ctx) { |
| 81 |
if (reader_thread_ctx->get_state() == Parallel_reader::State::THREAD) { |
| 82 |
return init(reader_thread_ctx, prebuilt); |
| 83 |
} else { |
| 84 |
return DB_SUCCESS; |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
m_parallel_reader.set_finish_callback( |
| 89 |
[=](Parallel_reader::Thread_ctx *reader_thread_ctx) { |
| 90 |
if (reader_thread_ctx->get_state() == Parallel_reader::State::THREAD) { |
| 91 |
return end(reader_thread_ctx); |
| 92 |
} else { |
| 93 |
return DB_SUCCESS; |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
ut_a(m_prebuilt == nullptr); |
| 98 |
m_prebuilt = prebuilt; |
| 99 |
} |
| 100 |
|
| 101 |
dberr_t Parallel_reader_adapter::run(void **thread_ctxs, Init_fn init_fn, |
| 102 |
Load_fn load_fn, End_fn end_fn) { |
| 103 |
m_end_fn = end_fn; |
| 104 |
m_init_fn = init_fn; |
| 105 |
m_load_fn = load_fn; |
| 106 |
m_thread_ctxs = thread_ctxs; |
| 107 |
|
| 108 |
m_parallel_reader.set_n_threads(m_parallel_reader.max_threads()); |
| 109 |
|
| 110 |
return m_parallel_reader.run(m_parallel_reader.max_threads()); |
| 111 |
} |
| 112 |
|
| 113 |
dberr_t Parallel_reader_adapter::init( |
| 114 |
Parallel_reader::Thread_ctx *reader_thread_ctx, row_prebuilt_t *prebuilt) { |
| 115 |
auto thread_ctx = |
| 116 |
ut::new_withkey<Thread_ctx>(ut::make_psi_memory_key(mem_key_archive)); |
| 117 |
|
| 118 |
if (thread_ctx == nullptr) { |
| 119 |
return DB_OUT_OF_MEMORY; |
| 120 |
} |
| 121 |
|
| 122 |
reader_thread_ctx->set_callback_ctx<Thread_ctx>(thread_ctx); |
| 123 |
|
| 124 |
/** There are data members in row_prebuilt_t that cannot be accessed in |
| 125 |
multi-threaded mode e.g., blob_heap. |
| 126 |
|
| 127 |
row_prebuilt_t is designed for single threaded access and to share |
| 128 |
it among threads is not recommended unless "you know what you are doing". |
| 129 |
This is very fragile code as it stands. |
| 130 |
|
| 131 |
To solve the blob heap issue in prebuilt we request parallel reader thread to |
| 132 |
use blob heap per thread and we pass this blob heap to the InnoDB to MySQL |
| 133 |
row format conversion function. */ |
| 134 |
if (prebuilt->templ_contains_blob) { |
| 135 |
reader_thread_ctx->create_blob_heap(); |
| 136 |
} |
| 137 |
|
| 138 |
auto ret = m_init_fn(m_thread_ctxs[reader_thread_ctx->m_thread_id], |
| 139 |
static_cast<ulong>(m_mysql_row.m_offsets.size()), |
| 140 |
m_mysql_row.m_max_len, &m_mysql_row.m_offsets[0], |
| 141 |
&m_mysql_row.m_null_bit_offsets[0], |
| 142 |
&m_mysql_row.m_null_bit_mask[0]); |
| 143 |
|
| 144 |
return (ret ? DB_INTERRUPTED : DB_SUCCESS); |
| 145 |
} |
| 146 |
|
| 147 |
dberr_t Parallel_reader_adapter::send_batch( |
| 148 |
Parallel_reader::Thread_ctx *reader_thread_ctx, size_t partition_id, |
| 149 |
uint64_t n_recs) { |
| 150 |
auto ctx = reader_thread_ctx->get_callback_ctx<Thread_ctx>(); |
| 151 |
const auto thread_id = reader_thread_ctx->m_thread_id; |
| 152 |
|
| 153 |
const auto start = ctx->m_n_sent % m_batch_size; |
| 154 |
|
| 155 |
ut_a(n_recs <= m_batch_size); |
| 156 |
ut_a(start + n_recs <= m_batch_size); |
| 157 |
|
| 158 |
const auto rec_loc = &ctx->m_buffer[start * m_mysql_row.m_max_len]; |
| 159 |
|
| 160 |
dberr_t err{DB_SUCCESS}; |
| 161 |
|
| 162 |
if (m_load_fn(m_thread_ctxs[thread_id], n_recs, rec_loc, partition_id)) { |
| 163 |
err = DB_INTERRUPTED; |
| 164 |
m_parallel_reader.set_error_state(DB_INTERRUPTED); |
| 165 |
} |
| 166 |
|
| 167 |
ctx->m_n_sent += n_recs; |
| 168 |
|
| 169 |
return err; |
| 170 |
} |
| 171 |
|
| 172 |
dberr_t Parallel_reader_adapter::process_rows( |
| 173 |
const Parallel_reader::Ctx *reader_ctx) { |
| 174 |
auto reader_thread_ctx = reader_ctx->thread_ctx(); |
| 175 |
auto ctx = reader_thread_ctx->get_callback_ctx<Thread_ctx>(); |
| 176 |
auto blob_heap = reader_thread_ctx->m_blob_heap; |
| 177 |
|
| 178 |
ut_a(ctx->m_n_read >= ctx->m_n_sent); |
| 179 |
ut_a(ctx->m_n_read - ctx->m_n_sent <= m_batch_size); |
| 180 |
|
| 181 |
dberr_t err{DB_SUCCESS}; |
| 182 |
|
| 183 |
{ |
| 184 |
auto n_pending = pending(ctx); |
| 185 |
|
| 186 |
/* Start of a new range, send what we have buffered. */ |
| 187 |
if ((reader_ctx->m_start && n_pending > 0) || is_buffer_full(ctx)) { |
| 188 |
err = send_batch(reader_thread_ctx, ctx->m_partition_id, n_pending); |
| 189 |
|
| 190 |
if (err != DB_SUCCESS) { |
| 191 |
return (err); |
| 192 |
} |
| 193 |
|
| 194 |
/* Empty the heap for the next batch */ |
| 195 |
if (blob_heap != nullptr) { |
| 196 |
mem_heap_empty(blob_heap); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
mem_heap_t *heap{}; |
| 202 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 203 |
ulint *offsets = offsets_; |
| 204 |
|
| 205 |
rec_offs_init(offsets_); |
| 206 |
|
| 207 |
offsets = rec_get_offsets(reader_ctx->m_rec, reader_ctx->index(), offsets, |
| 208 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 209 |
|
| 210 |
const auto next_rec = ctx->m_n_read % m_batch_size; |
| 211 |
|
| 212 |
const auto buffer_loc = &ctx->m_buffer[0] + next_rec * m_mysql_row.m_max_len; |
| 213 |
|
| 214 |
if (row_sel_store_mysql_rec(buffer_loc, m_prebuilt, reader_ctx->m_rec, |
| 215 |
nullptr, true, reader_ctx->index(), |
| 216 |
reader_ctx->index(), offsets, false, nullptr, |
| 217 |
blob_heap)) { |
| 218 |
/* If there is any pending records, then we should not overwrite the |
| 219 |
partition ID with a different one. */ |
| 220 |
if (pending(ctx) && ctx->m_partition_id != reader_ctx->partition_id()) { |
| 221 |
err = DB_ERROR; |
| 222 |
ut_d(ut_error); |
| 223 |
} else { |
| 224 |
++ctx->m_n_read; |
| 225 |
ctx->m_partition_id = reader_ctx->partition_id(); |
| 226 |
} |
| 227 |
|
| 228 |
if (m_parallel_reader.is_error_set()) { |
| 229 |
/* Simply skip sending the records to RAPID in case of an error in the |
| 230 |
parallel reader and return DB_ERROR as the error could have been |
| 231 |
originated from RAPID threads. */ |
| 232 |
err = DB_ERROR; |
| 233 |
} |
| 234 |
} else { |
| 235 |
err = DB_ERROR; |
| 236 |
} |
| 237 |
|
| 238 |
if (heap != nullptr) { |
| 239 |
mem_heap_free(heap); |
| 240 |
} |
| 241 |
|
| 242 |
return (err); |
| 243 |
} |
| 244 |
|
| 245 |
dberr_t Parallel_reader_adapter::end( |
| 246 |
Parallel_reader::Thread_ctx *reader_thread_ctx) { |
| 247 |
dberr_t err{DB_SUCCESS}; |
| 248 |
|
| 249 |
auto thread_id = reader_thread_ctx->m_thread_id; |
| 250 |
auto thread_ctx = reader_thread_ctx->get_callback_ctx<Thread_ctx>(); |
| 251 |
|
| 252 |
ut_a(thread_ctx->m_n_sent <= thread_ctx->m_n_read); |
| 253 |
ut_a(thread_ctx->m_n_read - thread_ctx->m_n_sent <= m_batch_size); |
| 254 |
|
| 255 |
if (!m_parallel_reader.is_error_set()) { |
| 256 |
/* It's possible that we might not have sent the records in the buffer |
| 257 |
when we have reached the end of records and the buffer is not full. |
| 258 |
Send them now. */ |
| 259 |
size_t n_pending = pending(thread_ctx); |
| 260 |
|
| 261 |
if (n_pending != 0) { |
| 262 |
err = |
| 263 |
send_batch(reader_thread_ctx, thread_ctx->m_partition_id, n_pending); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
m_end_fn(m_thread_ctxs[thread_id]); |
| 268 |
|
| 269 |
ut::delete_(thread_ctx); |
| 270 |
reader_thread_ctx->set_callback_ctx<Thread_ctx>(nullptr); |
| 271 |
|
| 272 |
return (err); |
| 273 |
} |
| 274 |
#endif /* !UNIV_HOTBACKUP */ |