MySQL 9.0.0
Source Code Documentation
row0pread-adapter.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2018, 2024, 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 Parallel_reader_adapter(size_t max_threads, ulint rowlen);
61
62 /** Destructor. */
64
65 /** Add scan context.
66 @param[in] trx Transaction used for parallel read.
67 @param[in] config (Cluster) Index scan configuration.
68 @param[in] f Callback function.
69 @returns error. */
70 [[nodiscard]] dberr_t add_scan(trx_t *trx,
71 const Parallel_reader::Config &config,
73
74 /** Run the parallel scan.
75 @param[in] thread_contexts Context for each of the spawned threads
76 @param[in] init_fn Callback called by each parallel load thread
77 at the beginning of the parallel load.
78 @param[in] load_fn Callback called by each parallel load thread
79 when processing of rows is required.
80 @param[in] end_fn Callback called by each parallel load thread
81 when processing of rows has ended.
82 @return DB_SUCCESS or error code. */
83 [[nodiscard]] dberr_t run(void **thread_contexts, Init_fn init_fn,
84 Load_fn load_fn, End_fn end_fn);
85
86 /** Convert the record in InnoDB format to MySQL format and send them.
87 @param[in] reader_ctx Parallel read context.
88 @return error code */
89 [[nodiscard]] dberr_t process_rows(const Parallel_reader::Ctx *reader_ctx);
90
91 /** Set up the query processing state cache.
92 @param[in] prebuilt The prebuilt cache for the query. */
93 void set(row_prebuilt_t *prebuilt);
94
95 private:
96 /** Each parallel reader thread's init function.
97 @param[in] reader_thread_ctx context info related to the
98 current thread
99 @param[in] prebuilt prebuilt cache
100 @return DB_SUCCESS or error code. */
101 [[nodiscard]] dberr_t init(Parallel_reader::Thread_ctx *reader_thread_ctx,
102 row_prebuilt_t *prebuilt);
103
104 /** Each parallel reader thread's end function.
105 @param[in] reader_thread_ctx context info related to the current thread
106 @return DB_SUCCESS or error code. */
107 [[nodiscard]] dberr_t end(Parallel_reader::Thread_ctx *reader_thread_ctx);
108
109 /** Send a batch of records.
110 @param[in] reader_thread_ctx reader threads related thread context info
111 @param[in] partition_id partition ID of the index the record belongs to
112 @param[in] n_recs Number of records to send.
113 @return DB_SUCCESS or error code. */
114 [[nodiscard]] dberr_t send_batch(
115 Parallel_reader::Thread_ctx *reader_thread_ctx, size_t partition_id,
116 uint64_t n_recs);
117
118 /** Get the number of rows buffered but not sent.
119 @param[in] ctx adapter related thread context information.
120 @return number of buffered items. */
121 [[nodiscard]] size_t pending(Thread_ctx *ctx) const {
122 return (ctx->m_n_read - ctx->m_n_sent);
123 }
124
125 /** Check if the buffer is full.
126 @param[in] ctx adapter related thread context information.
127 @return true if the buffer is full. */
128 [[nodiscard]] bool is_buffer_full(Thread_ctx *ctx) const {
129 return ctx->m_n_read > 0 && ctx->m_n_read % m_batch_size == 0;
130 }
131
132 private:
133 /** Adapter context for each of the spawned threads. We don't know the
134 type of the context it's passed to us as a void *. */
136
137 /** Callback called by each parallel load thread at the
138 beginning of the parallel load for the scan. */
140
141 /** Callback called by each parallel load thread when
142 processing of rows is required for the scan. */
144
145 /** Callback called by each parallel load thread when
146 processing of rows has ended for the scan. */
148
149 /** Number of records to be sent across to the caller in a batch. */
150 uint64_t m_batch_size{};
151
152 /** MySQL row meta data. This is common across partitions. */
153 struct MySQL_row {
154 using Column_meta_data = std::vector<ulong, ut::allocator<ulong>>;
155
156 /** Column offsets. */
158
159 /** Column null bit masks. */
161
162 /** Column null bit offsets. */
164
165 /** Maximum row length. */
166 ulong m_max_len{};
167 };
168
169 /** Row meta data per scan context. */
171
172 /** Callback thread context for each of the spawned threads. */
173 struct Thread_ctx {
174 /** Constructor. */
175 Thread_ctx();
176
177 /** Destructor. */
178 ~Thread_ctx() = default;
179
180 /** Number of records read. */
181 size_t m_n_read{};
182
183 /** Number of records sent to the adapter. */
184 size_t m_n_sent{};
185
186 /** Partition ID for the records in buffer. Must be set when adding more
187 records to be sent i.e. while incrementing m_n_read. */
188 size_t m_partition_id{std::numeric_limits<size_t>::max()};
189
190 /** Buffer to store records to be sent to the adapter. */
191 std::vector<byte, ut::allocator<byte>> m_buffer;
192 };
193
194 /** Prebuilt to use for conversion to MySQL row format.
195 NOTE: We are sharing this because we don't convert BLOBs yet. There are
196 data members in row_prebuilt_t that cannot be accessed in multi-threaded
197 mode e.g., blob_heap.
198
199 row_prebuilt_t is designed for single threaded access and to share
200 it among threads is not recommended unless "you know what you are doing".
201 This is very fragile code as it stands.
202
203 To solve the blob heap issue in prebuilt we use per thread m_blob_heaps.
204 Pass the blob heap to the InnoDB to MySQL row format conversion function. */
206
207 /** Parallel reader to use. */
209};
210
211#endif /* !row0pread_adapter_h */
Parallel reader execution context.
Definition: row0pread.h:680
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:150
Parallel_reader_adapter(size_t max_threads, ulint rowlen)
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:172
size_t pending(Thread_ctx *ctx) const
Get the number of rows buffered but not sent.
Definition: row0pread-adapter.h:121
dberr_t end(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's end function.
Definition: row0pread-adapter.cc:245
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:139
void set(row_prebuilt_t *prebuilt)
Set up the query processing state cache.
Definition: row0pread-adapter.cc:55
bool is_buffer_full(Thread_ctx *ctx) const
Check if the buffer is full.
Definition: row0pread-adapter.h:128
~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:205
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:143
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:147
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:113
Parallel_reader m_parallel_reader
Parallel reader to use.
Definition: row0pread-adapter.h:208
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:170
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:101
void ** m_thread_ctxs
Adapter context for each of the spawned threads.
Definition: row0pread-adapter.h:135
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:147
dberr_t add_scan(trx_t *trx, const Parallel_reader::Config &config, Parallel_reader::F &&f)
Add scan context.
Definition: row0pread-adapter.cc:45
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
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:5034
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:5012
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:5027
dberr_t
Definition: db0err.h:39
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:153
ulong m_max_len
Maximum row length.
Definition: row0pread-adapter.h:166
std::vector< ulong, ut::allocator< ulong > > Column_meta_data
Definition: row0pread-adapter.h:154
Column_meta_data m_null_bit_mask
Column null bit masks.
Definition: row0pread-adapter.h:160
Column_meta_data m_offsets
Column offsets.
Definition: row0pread-adapter.h:157
Column_meta_data m_null_bit_offsets
Column null bit offsets.
Definition: row0pread-adapter.h:163
Callback thread context for each of the spawned threads.
Definition: row0pread-adapter.h:173
size_t m_n_sent
Number of records sent to the adapter.
Definition: row0pread-adapter.h:184
Thread_ctx()
Constructor.
Definition: row0pread-adapter.cc:51
size_t m_n_read
Number of records read.
Definition: row0pread-adapter.h:181
std::vector< byte, ut::allocator< byte > > m_buffer
Buffer to store records to be sent to the adapter.
Definition: row0pread-adapter.h:191
size_t m_partition_id
Partition ID for the records in buffer.
Definition: row0pread-adapter.h:188
~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:684
unsigned long int ulint
Definition: univ.i:406
Counter utility class.