Debian Codegraph @

storage/innobase/include/row0pread-adapter.h

raw
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 include/row0pread-adapter.h
29 Parallel read adapter interface.
30
31 Created 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
42 adapter. */
43 class Parallel_reader_adapter {
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:
51 using Load_fn = handler::Load_cbk;
52
53 using End_fn = handler::Load_end_cbk;
54
55 using Init_fn = handler::Load_init_cbk;
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. */
63 ~Parallel_reader_adapter() = default;
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 @retval error. */
70 [[nodiscard]] dberr_t add_scan(trx_t *trx,
71 const Parallel_reader::Config &config,
72 Parallel_reader::F &&f);
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 *. */
135 void **m_thread_ctxs{};
136
137 /** Callback called by each parallel load thread at the
138 beginning of the parallel load for the scan. */
139 Init_fn m_init_fn{};
140
141 /** Callback called by each parallel load thread when
142 processing of rows is required for the scan. */
143 Load_fn m_load_fn{};
144
145 /** Callback called by each parallel load thread when
146 processing of rows has ended for the scan. */
147 End_fn m_end_fn{};
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. */
157 Column_meta_data m_offsets{};
158
159 /** Column null bit masks. */
160 Column_meta_data m_null_bit_mask{};
161
162 /** Column null bit offsets. */
163 Column_meta_data m_null_bit_offsets{};
164
165 /** Maximum row length. */
166 ulong m_max_len{};
167 };
168
169 /** Row meta data per scan context. */
170 MySQL_row m_mysql_row{};
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. */
205 row_prebuilt_t *m_prebuilt{};
206
207 /** Parallel reader to use. */
208 Parallel_reader m_parallel_reader;
209 };
210
211 #endif /* !row0pread_adapter_h */