MySQL 9.7.4
Source Code Documentation
row0pread-adapter.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2018, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/row0pread-adapter.h
29Parallel read adapter interface.
30
31Created 2018-02-28 by Darshan M N. */
32
33#ifndef row0pread_adapter_h
34#define row0pread_adapter_h
35
36#include "row0pread.h"
37#include "ut0counter.h"
38
39#include "handler.h"
40
41/** Traverse an index in the leaf page block list order and send records to
42adapter. */
44 /** Size of the buffer used to store InnoDB records and sent to the adapter*/
45 static constexpr size_t ADAPTER_SEND_BUFFER_SIZE = 2 * 1024 * 1024;
46
47 /** Forward declaration. */
48 struct Thread_ctx;
49
50 public:
52
54
56
57 /** Constructor.
58 @param[in] max_threads Maximum threads to use for all scan contexts.
59 @param[in] rowlen Row length.
60 @param[in] use_reserved_threads
61 Whether to use reserved parallel-read slots. */
63 bool use_reserved_threads = false);
64
65 /** @return the number of global parallel-read slots reserved. */
66 [[nodiscard]] size_t max_threads() const {
68 }
69
70 /** Destructor. */
72
73 /** Add scan context.
74 @param[in] trx Transaction used for parallel read.
75 @param[in] config (Cluster) Index scan configuration.
76 @param[in] f Callback function.
77 @returns error. */
78 [[nodiscard]] dberr_t add_scan(trx_t *trx,
79 const Parallel_reader::Config &config,
81
82 /** Run the parallel scan.
83 @param[in] thread_contexts Context for each of the spawned threads
84 @param[in] init_fn Callback called by each parallel load thread
85 at the beginning of the parallel load.
86 @param[in] load_fn Callback called by each parallel load thread
87 when processing of rows is required.
88 @param[in] end_fn Callback called by each parallel load thread
89 when processing of rows has ended.
90 @return DB_SUCCESS or error code. */
91 [[nodiscard]] dberr_t run(void **thread_contexts, Init_fn init_fn,
92 Load_fn load_fn, End_fn end_fn);
93
94 /** Convert the record in InnoDB format to MySQL format and send them.
95 @param[in] reader_ctx Parallel read context.
96 @return error code */
97 [[nodiscard]] dberr_t process_rows(const Parallel_reader::Ctx *reader_ctx);
98
99 /** Set up the query processing state cache.
100 @param[in] prebuilt The prebuilt cache for the query. */
101 void set(row_prebuilt_t *prebuilt);
102
103 private:
104 /** Each parallel reader thread's init function.
105 @param[in] reader_thread_ctx context info related to the
106 current thread
107 @param[in] prebuilt prebuilt cache
108 @return DB_SUCCESS or error code. */
109 [[nodiscard]] dberr_t init(Parallel_reader::Thread_ctx *reader_thread_ctx,
110 row_prebuilt_t *prebuilt);
111
112 /** Each parallel reader thread's end function.
113 @param[in] reader_thread_ctx context info related to the current thread
114 @return DB_SUCCESS or error code. */
115 [[nodiscard]] dberr_t end(Parallel_reader::Thread_ctx *reader_thread_ctx);
116
117 /** Send a batch of records.
118 @param[in] reader_thread_ctx reader threads related thread context info
119 @param[in] partition_id partition ID of the index the record belongs to
120 @param[in] n_recs Number of records to send.
121 @return DB_SUCCESS or error code. */
122 [[nodiscard]] dberr_t send_batch(
123 Parallel_reader::Thread_ctx *reader_thread_ctx, size_t partition_id,
124 uint64_t n_recs);
125
126 /** Get the number of rows buffered but not sent.
127 @param[in] ctx adapter related thread context information.
128 @return number of buffered items. */
129 [[nodiscard]] size_t pending(Thread_ctx *ctx) const {
130 return (ctx->m_n_read - ctx->m_n_sent);
131 }
132
133 /** Check if the buffer is full.
134 @param[in] ctx adapter related thread context information.
135 @return true if the buffer is full. */
136 [[nodiscard]] bool is_buffer_full(Thread_ctx *ctx) const {
137 return ctx->m_n_read > 0 && ctx->m_n_read % m_batch_size == 0;
138 }
139
140 private:
141 /** Adapter context for each of the spawned threads. We don't know the
142 type of the context it's passed to us as a void *. */
144
145 /** Callback called by each parallel load thread at the
146 beginning of the parallel load for the scan. */
148
149 /** Callback called by each parallel load thread when
150 processing of rows is required for the scan. */
152
153 /** Callback called by each parallel load thread when
154 processing of rows has ended for the scan. */
156
157 /** Number of records to be sent across to the caller in a batch. */
158 uint64_t m_batch_size{};
159
160 /** MySQL row meta data. This is common across partitions. */
161 struct MySQL_row {
162 using Column_meta_data = std::vector<ulong, ut::allocator<ulong>>;
163
164 /** Column offsets. */
166
167 /** Column null bit masks. */
169
170 /** Column null bit offsets. */
172
173 /** Maximum row length. */
174 ulong m_max_len{};
175 };
176
177 /** Row meta data per scan context. */
179
180 /** Callback thread context for each of the spawned threads. */
181 struct Thread_ctx {
182 /** Constructor. */
183 Thread_ctx();
184
185 /** Destructor. */
186 ~Thread_ctx() = default;
187
188 /** Number of records read. */
189 size_t m_n_read{};
190
191 /** Number of records sent to the adapter. */
192 size_t m_n_sent{};
193
194 /** Partition ID for the records in buffer. Must be set when adding more
195 records to be sent i.e. while incrementing m_n_read. */
197
198 /** Buffer to store records to be sent to the adapter. */
199 std::vector<byte, ut::allocator<byte>> m_buffer;
200 };
201
202 /** Prebuilt to use for conversion to MySQL row format.
203 NOTE: We are sharing this because we don't convert BLOBs yet. There are
204 data members in row_prebuilt_t that cannot be accessed in multi-threaded
205 mode e.g., blob_heap.
206
207 row_prebuilt_t is designed for single threaded access and to share
208 it among threads is not recommended unless "you know what you are doing".
209 This is very fragile code as it stands.
210
211 To solve the blob heap issue in prebuilt we use per thread m_blob_heaps.
212 Pass the blob heap to the InnoDB to MySQL row format conversion function. */
214
215 /** Parallel reader to use. */
217};
218
219#endif /* !row0pread_adapter_h */
Parallel reader execution context.
Definition: row0pread.h:693
Traverse an index in the leaf page block list order and send records to adapter.
Definition: row0pread-adapter.h:43
uint64_t m_batch_size
Number of records to be sent across to the caller in a batch.
Definition: row0pread-adapter.h:158
Parallel_reader_adapter(size_t max_threads, ulint rowlen, bool use_reserved_threads=false)
Constructor.
Definition: row0pread-adapter.cc:39
dberr_t process_rows(const Parallel_reader::Ctx *reader_ctx)
Convert the record in InnoDB format to MySQL format and send them.
Definition: row0pread-adapter.cc:173
size_t pending(Thread_ctx *ctx) const
Get the number of rows buffered but not sent.
Definition: row0pread-adapter.h:129
dberr_t end(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's end function.
Definition: row0pread-adapter.cc:246
Init_fn m_init_fn
Callback called by each parallel load thread at the beginning of the parallel load for the scan.
Definition: row0pread-adapter.h:147
void set(row_prebuilt_t *prebuilt)
Set up the query processing state cache.
Definition: row0pread-adapter.cc:56
bool is_buffer_full(Thread_ctx *ctx) const
Check if the buffer is full.
Definition: row0pread-adapter.h:136
~Parallel_reader_adapter()=default
Destructor.
handler::Load_init_cbk Init_fn
Definition: row0pread-adapter.h:55
row_prebuilt_t * m_prebuilt
Prebuilt to use for conversion to MySQL row format.
Definition: row0pread-adapter.h:213
Load_fn m_load_fn
Callback called by each parallel load thread when processing of rows is required for the scan.
Definition: row0pread-adapter.h:151
handler::Load_end_cbk End_fn
Definition: row0pread-adapter.h:53
handler::Load_cbk Load_fn
Definition: row0pread-adapter.h:51
dberr_t send_batch(Parallel_reader::Thread_ctx *reader_thread_ctx, size_t partition_id, uint64_t n_recs)
Send a batch of records.
Definition: row0pread-adapter.cc:148
dberr_t init(Parallel_reader::Thread_ctx *reader_thread_ctx, row_prebuilt_t *prebuilt)
Each parallel reader thread's init function.
Definition: row0pread-adapter.cc:114
Parallel_reader m_parallel_reader
Parallel reader to use.
Definition: row0pread-adapter.h:216
static constexpr size_t ADAPTER_SEND_BUFFER_SIZE
Size of the buffer used to store InnoDB records and sent to the adapter.
Definition: row0pread-adapter.h:45
MySQL_row m_mysql_row
Row meta data per scan context.
Definition: row0pread-adapter.h:178
size_t max_threads() const
Definition: row0pread-adapter.h:66
dberr_t run(void **thread_contexts, Init_fn init_fn, Load_fn load_fn, End_fn end_fn)
Run the parallel scan.
Definition: row0pread-adapter.cc:102
void ** m_thread_ctxs
Adapter context for each of the spawned threads.
Definition: row0pread-adapter.h:143
End_fn m_end_fn
Callback called by each parallel load thread when processing of rows has ended for the scan.
Definition: row0pread-adapter.h:155
dberr_t add_scan(trx_t *trx, const Parallel_reader::Config &config, Parallel_reader::F &&f)
Add scan context.
Definition: row0pread-adapter.cc:46
The core idea is to find the left and right paths down the B+Tree.These paths correspond to the scan ...
Definition: row0pread.h:101
size_t max_threads() const
Definition: row0pread.h:344
std::function< dberr_t(const Ctx *)> F
Callback to process the rows.
Definition: row0pread.h:143
std::function< void(void *cookie)> Load_end_cbk
This callback is called by each parallel load thread when processing of rows has ended for the adapte...
Definition: handler.h:5215
std::function< bool(void *cookie, ulong ncols, ulong row_len, const ulong *col_offsets, const ulong *null_byte_offsets, const ulong *null_bitmasks)> Load_init_cbk
This callback is called by each parallel load thread at the beginning of the parallel load for the ad...
Definition: handler.h:5193
std::function< bool(void *cookie, uint nrows, void *rowdata, uint64_t partition_id)> Load_cbk
This callback is called by each parallel load thread when processing of rows is required for the adap...
Definition: handler.h:5208
dberr_t
Definition: db0err.h:39
ValueType max(X &&first)
Definition: gtid.h:103
Parallel read interface.
TempTable public handler API declaration.
Scan (Scan_ctx) configuration.
Definition: row0pread.h:171
Thread related context information.
Definition: row0pread.h:215
MySQL row meta data.
Definition: row0pread-adapter.h:161
ulong m_max_len
Maximum row length.
Definition: row0pread-adapter.h:174
std::vector< ulong, ut::allocator< ulong > > Column_meta_data
Definition: row0pread-adapter.h:162
Column_meta_data m_null_bit_mask
Column null bit masks.
Definition: row0pread-adapter.h:168
Column_meta_data m_offsets
Column offsets.
Definition: row0pread-adapter.h:165
Column_meta_data m_null_bit_offsets
Column null bit offsets.
Definition: row0pread-adapter.h:171
Callback thread context for each of the spawned threads.
Definition: row0pread-adapter.h:181
size_t m_n_sent
Number of records sent to the adapter.
Definition: row0pread-adapter.h:192
Thread_ctx()
Constructor.
Definition: row0pread-adapter.cc:52
size_t m_n_read
Number of records read.
Definition: row0pread-adapter.h:189
std::vector< byte, ut::allocator< byte > > m_buffer
Buffer to store records to be sent to the adapter.
Definition: row0pread-adapter.h:199
size_t m_partition_id
Partition ID for the records in buffer.
Definition: row0pread-adapter.h:196
~Thread_ctx()=default
Destructor.
A struct for (sometimes lazily) prebuilt structures in an Innobase table handle used within MySQL; th...
Definition: row0mysql.h:515
Definition: trx0trx.h:675
unsigned long int ulint
Definition: univ.i:406
Counter utility class.