MySQL  8.0.19
Source Code Documentation
row0pread-adapter.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2018, 2019, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 /** @file include/row0pread-adapter.h
28 Parallel read adapter interface.
29 
30 Created 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
41 adapter. */
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  public:
48 
50 
52 
53  /** Constructor.
54  @param[in] max_threads Maximum threads to use for all scan contexts.
55  @param[in] rowlen Row length. */
56  Parallel_reader_adapter(size_t max_threads, ulint rowlen);
57 
58  /** Destructor. */
60 
61  /** Add scan context.
62  @param[in] trx Transaction used for parallel read.
63  @param[in] config (Cluster) Index scan configuration.
64  @param[in] f Callback function.
65  @retval error. */
67  Parallel_reader::F &&f) MY_ATTRIBUTE((warn_unused_result));
68 
69  /** Run the parallel scan.
70  @param[in] thread_contexts Context for each of the spawned threads
71  @param[in] init_fn Callback called by each parallel load thread
72  at the beginning of the parallel load.
73  @param[in] load_fn Callback called by each parallel load thread
74  when processing of rows is required.
75  @param[in] end_fn Callback called by each parallel load thread
76  when processing of rows has ended.
77  @return DB_SUCCESS or error code. */
78  dberr_t run(void **thread_contexts, Init_fn init_fn, Load_fn load_fn,
79  End_fn end_fn) MY_ATTRIBUTE((warn_unused_result));
80 
81  /** Convert the record in InnoDB format to MySQL format and send them.
82  @param[in] ctx Parallel read context.
83  @return error code */
85  MY_ATTRIBUTE((warn_unused_result));
86 
87  /** Set up the query processing state cache.
88  @param[in] prebuilt The prebuilt cache for the query. */
89  void set(row_prebuilt_t *prebuilt);
90 
91  private:
92  /** The callers init function.
93  @param[in] thread_id ID of the thread.
94  @return DB_SUCCESS or error code. */
95  dberr_t init(size_t thread_id) MY_ATTRIBUTE((warn_unused_result));
96 
97  /** For pushing any left over rows to the caller.
98  @param[in] thread_id ID of the thread.
99  @return DB_SUCCESS or error code. */
100  dberr_t end(size_t thread_id) MY_ATTRIBUTE((warn_unused_result));
101 
102  /** Send a batch of records.
103  @param[in] thread_id ID of the thread.
104  @param[in] n_recs Number of records to send.
105  @return DB_SUCCESS or error code. */
106  dberr_t send_batch(size_t thread_id, uint64_t n_recs)
107  MY_ATTRIBUTE((warn_unused_result));
108 
109  /** Get the number of rows buffered but not sent.
110  @param[in] thread_id ID of the thread.
111  @return number of buffered items. */
113  MY_ATTRIBUTE((warn_unused_result)) {
114  return (Counter::get(m_n_read, thread_id) -
116  }
117 
118  /** Check if the buffer is full.
119  @param[in] thread_id ID of the thread.
120  @return true if the buffer is full. */
121  bool is_buffer_full(size_t thread_id) const
122  MY_ATTRIBUTE((warn_unused_result)) {
124  }
125 
126  private:
128 
129  /** Counter to track number of records sent to the caller. */
131 
132  /** Counter to track number of records processed. */
134 
135  /** Adapter context for each of the spawned threads. */
136  void **m_thread_contexts{nullptr};
137 
138  /** Callback called by each parallel load thread at the
139  beginning of the parallel load for the scan. */
141 
142  /** Callback called by each parallel load thread when
143  processing of rows is required for the scan. */
145 
146  /** Callback called by each parallel load thread when
147  processing of rows has ended for the scan. */
149 
150  /** Number of records to be sent across to the caller in a batch. */
151  uint64_t m_batch_size{};
152 
153  /** The row buffer. */
154  using Buffer = std::vector<byte>;
155 
156  /** Buffer to store records to be sent to the caller. */
157  std::vector<Buffer> m_buffers{};
158 
159  /** MySQL row meta data. This is common across partitions. */
160  struct MySQL_row {
161  using Column_meta_data = std::vector<ulong, ut_allocator<ulong>>;
162 
163  /** Column offsets. */
165 
166  /** Column null bit masks. */
168 
169  /** Column null bit offsets. */
171 
172  /** Maximum row length. */
174  };
175 
176  /** Row meta data per scan context. */
178 
179  /** Prebuilt to use for conversion to MySQL row format.
180  NOTE: We are sharing this because we don't convert BLOBs yet. There are
181  data members in row_prebuilt_t that cannot be accessed in multi-threaded
182  mode e.g., blob_heap.
183 
184  row_prebuilt_t is designed for single threaded access and to share
185  it among threads is not recommended unless "you know what you are doing".
186  This is very fragile code as it stands.
187 
188  To solve the blob heap issue in prebuilt we use per thread m_blob_heaps.
189  Pass the blob heap to the InnoDB to MySQL row format conversion function. */
191 
192  /** BLOB heap per thread. */
193  std::vector<mem_heap_t *, ut_allocator<mem_heap_t *>> m_blob_heaps{};
194 
195  /** Parallel reader to use. */
197 };
198 
199 #endif /* !row0pread_adapter_h */
Parallel_reader_adapter::m_load_fn
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:144
Parallel_reader_adapter::end
dberr_t end(size_t thread_id)
For pushing any left over rows to the caller.
Definition: row0pread-adapter.cc:215
Parallel_reader_adapter::m_mysql_row
MySQL_row m_mysql_row
Row meta data per scan context.
Definition: row0pread-adapter.h:177
Parallel_reader_adapter::Shards
Counter::Shards< Parallel_reader::MAX_THREADS > Shards
Definition: row0pread-adapter.h:127
Parallel_reader_adapter::m_n_sent
Shards m_n_sent
Counter to track number of records sent to the caller.
Definition: row0pread-adapter.h:130
Parallel_reader_adapter::m_parallel_reader
Parallel_reader m_parallel_reader
Parallel reader to use.
Definition: row0pread-adapter.h:196
Parallel_reader_adapter::MySQL_row::Column_meta_data
std::vector< ulong, ut_allocator< ulong > > Column_meta_data
Definition: row0pread-adapter.h:161
Parallel_reader_adapter::MySQL_row::m_null_bit_offsets
Column_meta_data m_null_bit_offsets
Column null bit offsets.
Definition: row0pread-adapter.h:170
Parallel_reader_adapter::process_rows
dberr_t process_rows(const Parallel_reader::Ctx *ctx)
Convert the record in InnoDB format to MySQL format and send them.
Definition: row0pread-adapter.cc:150
thread_id
static my_thread_id thread_id
Definition: my_thr_init.cc:62
row0pread.h
config
Definition: default_engine.h:52
Parallel_reader_adapter::MySQL_row
MySQL row meta data.
Definition: row0pread-adapter.h:160
Parallel_reader_adapter::~Parallel_reader_adapter
~Parallel_reader_adapter()
Destructor.
Definition: row0pread-adapter.cc:58
Parallel_reader_adapter::init
dberr_t init(size_t thread_id)
The callers init function.
Definition: row0pread-adapter.cc:141
Parallel_reader::Config
Scan (Scan_ctx) configuration.
Definition: row0pread.h:151
Parallel_reader_adapter::End_fn
handler::Load_end_cbk End_fn
Definition: row0pread-adapter.h:49
Parallel_reader_adapter::Init_fn
handler::Load_init_cbk Init_fn
Definition: row0pread-adapter.h:51
Counter::Type
uint64_t Type
Definition: ut0counter.h:228
dberr_t
dberr_t
Definition: db0err.h:38
Parallel_reader_adapter::pending
Counter::Type pending(size_t thread_id) const
Get the number of rows buffered but not sent.
Definition: row0pread-adapter.h:112
Parallel_reader_adapter::m_buffers
std::vector< Buffer > m_buffers
Buffer to store records to be sent to the caller.
Definition: row0pread-adapter.h:157
handler.h
handler::Load_end_cbk
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:4442
Parallel_reader::F
std::function< dberr_t(const Ctx *)> F
Callback to process the rows.
Definition: row0pread.h:122
Parallel_reader_adapter::Buffer
std::vector< byte > Buffer
The row buffer.
Definition: row0pread-adapter.h:154
Parallel_reader_adapter::m_n_read
Shards m_n_read
Counter to track number of records processed.
Definition: row0pread-adapter.h:133
Parallel_reader_adapter::send_batch
dberr_t send_batch(size_t thread_id, uint64_t n_recs)
Send a batch of records.
Definition: row0pread-adapter.cc:115
Parallel_reader_adapter::add_scan
dberr_t add_scan(trx_t *trx, const Parallel_reader::Config &config, Parallel_reader::F &&f)
Add scan context.
Definition: row0pread-adapter.cc:64
Parallel_reader_adapter::run
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:105
Parallel_reader_adapter::set
void set(row_prebuilt_t *prebuilt)
Set up the query processing state cache.
Definition: row0pread-adapter.cc:70
Parallel_reader_adapter::m_init_fn
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:140
Parallel_reader_adapter::m_end_fn
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:148
Parallel_reader_adapter::Parallel_reader_adapter
Parallel_reader_adapter(size_t max_threads, ulint rowlen)
Constructor.
Definition: row0pread-adapter.cc:37
Parallel_reader_adapter::m_thread_contexts
void ** m_thread_contexts
Adapter context for each of the spawned threads.
Definition: row0pread-adapter.h:136
Parallel_reader_adapter::m_batch_size
uint64_t m_batch_size
Number of records to be sent across to the caller in a batch.
Definition: row0pread-adapter.h:151
Counter::get
Type get(const Shards< COUNT > &shards, size_t id)
Get the counter value for a shard.
Definition: ut0counter.h:272
Parallel_reader_adapter::Load_fn
handler::Load_cbk Load_fn
Definition: row0pread-adapter.h:47
Counter::Shards
std::array< Shard, COUNT > Shards
Definition: ut0counter.h:247
Parallel_reader_adapter::MySQL_row::m_max_len
ulong m_max_len
Maximum row length.
Definition: row0pread-adapter.h:173
Parallel_reader_adapter
Traverse an index in the leaf page block list order and send records to adapter.
Definition: row0pread-adapter.h:42
Parallel_reader
The core idea is to find the left and right paths down the B+Tree.These paths correspond to the scan ...
Definition: row0pread.h:104
ulong
unsigned long ulong
Definition: my_inttypes.h:48
handler::Load_cbk
std::function< bool(void *cookie, uint nrows, void *rowdata)> Load_cbk
This callback is called by each parallel load thread when processing of rows is required for the adap...
Definition: handler.h:4435
Parallel_reader_adapter::m_prebuilt
row_prebuilt_t * m_prebuilt
Prebuilt to use for conversion to MySQL row format.
Definition: row0pread-adapter.h:190
Parallel_reader_adapter::ADAPTER_SEND_BUFFER_SIZE
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
Parallel_reader_adapter::MySQL_row::m_null_bit_mask
Column_meta_data m_null_bit_mask
Column null bit masks.
Definition: row0pread-adapter.h:167
row_prebuilt_t
A struct for (sometimes lazily) prebuilt structures in an Innobase table handle used within MySQL; th...
Definition: row0mysql.h:539
Parallel_reader_adapter::is_buffer_full
bool is_buffer_full(size_t thread_id) const
Check if the buffer is full.
Definition: row0pread-adapter.h:121
Parallel_reader_adapter::m_blob_heaps
std::vector< mem_heap_t *, ut_allocator< mem_heap_t * > > m_blob_heaps
BLOB heap per thread.
Definition: row0pread-adapter.h:193
Parallel_reader_adapter::MySQL_row::m_offsets
Column_meta_data m_offsets
Column offsets.
Definition: row0pread-adapter.h:164
handler::Load_init_cbk
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:4423
trx_t
Definition: trx0trx.h:780
ut0counter.h
Parallel_reader::Ctx
Parallel reader execution context.
Definition: row0pread.h:650