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