MySQL 8.0.31
Source Code Documentation
row0pread-histogram.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2019, 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-histogram.h
28Parallel read histogram interface.
29
30Created 2019-04-20 by Darshan M N. */
31
32#ifndef row0pread_histogram_h
33#define row0pread_histogram_h
34
35#include <random>
36#include "row0pread.h"
37#include "ut0counter.h"
38
40 public:
41 /** Constructor.
42 @param[in] max_threads Maximum number of threads to use.
43 @param[in] sampling_seed seed to be used for sampling
44 @param[in] sampling_percentage percentage of sampling that needs to be done
45 @param[in] sampling_method sampling method to be used for sampling */
46 explicit Histogram_sampler(size_t max_threads, int sampling_seed,
47 double sampling_percentage,
48 enum_sampling_method sampling_method);
49
50 /** Destructor. */
52
53 /** Initialize the sampler context.
54 @param[in] trx Transaction used for parallel read.
55 @param[in] index clustered index.
56 @param[in] prebuilt prebuilt info
57 @retval true on success. */
58 bool init(trx_t *trx, dict_index_t *index, row_prebuilt_t *prebuilt);
59
60 /** Buffer next row.
61 @return error code */
63
64 /** End parallel read in case the reader thread is still active and wait for
65 its exit. This can happen if we're ending sampling prematurely. */
66 void buffer_end();
67
68 /** Set the buffer.
69 @param[in] buf buffer to be used to store the row converted to MySQL
70 format. */
71 void set(byte *buf) { m_buf = buf; }
72
73 /** Start the sampling process.
74 @return DB_SUCCESS or error code. */
75 dberr_t run();
76
77 /** Check if the processing of the record needs to be skipped.
78 In case of record belonging to non-leaf page, we decide if the child page
79 pertaining to the record needs to be skipped.
80 In case of record belonging to leaf page, we read the page regardless.
81 @return true if it needs to be skipped, else false. */
82 bool skip();
83
84 private:
85 /** Wait till there is a request to buffer the next row. */
87
88 /** Wait till the buffering of the row is complete. */
90
91 /** Signal that the next row needs to be buffered. */
93
94 /** Signal that the buffering of the row is complete. */
96
97 /** Set the error state.
98 @param[in] err Error state to set to. */
100
101 /** @return true if in error state. */
102 [[nodiscard]] bool is_error_set() const { return (m_err != DB_SUCCESS); }
103
104 /** Each parallel reader thread's init function.
105 @param[in] reader_thread_ctx context information related to the thread
106 @return DB_SUCCESS or error code. */
107 [[nodiscard]] dberr_t start_callback(
108 Parallel_reader::Thread_ctx *reader_thread_ctx);
109
110 /** Each parallel reader thread's end function.
111 @param[in] reader_thread_ctx context information related to the thread
112 @return DB_SUCCESS or error code. */
113 [[nodiscard]] dberr_t finish_callback(
114 Parallel_reader::Thread_ctx *reader_thread_ctx);
115
116 /** Convert the row in InnoDB format to MySQL format and store in the buffer
117 for server to use.
118 @param[in] ctx Parallel read context.
119 @param[in] rec record that needs to be converted
120 @param[in] offsets offsets belonging to the record
121 @param[in] index index of the record
122 @param[in] prebuilt Row meta-data cache.
123 @return DB_SUCCESS or error code. */
124 dberr_t sample_rec(const Parallel_reader::Ctx *ctx, const rec_t *rec,
125 ulint *offsets, const dict_index_t *index,
126 row_prebuilt_t *prebuilt);
127
128 /** For each record in a non-leaf block at level 1 (if leaf level is 0)
129 check if the child page needs to be sampled and if so sample all the rows in
130 the child page.
131 @param[in] ctx Parallel read context.
132 @param[in] prebuilt Row meta-data cache.
133 @return error code */
134 [[nodiscard]] dberr_t process_non_leaf_rec(const Parallel_reader::Ctx *ctx,
135 row_prebuilt_t *prebuilt);
136
137 /** Process the record in the leaf page. This would happen only when the root
138 page is the leaf page and in such a case we process the page regardless of
139 the sampling percentage.
140 @param[in] ctx Parallel read context.
141 @param[in] prebuilt Row meta-data cache.
142 @return error code */
143 [[nodiscard]] dberr_t process_leaf_rec(const Parallel_reader::Ctx *ctx,
144 row_prebuilt_t *prebuilt);
145
146 private:
147 /** Buffer to store the sampled row which is in the MySQL format. */
148 byte *m_buf{nullptr};
149
150 /** Event to notify if the next row needs to be buffered. */
152
153 /** Event to notify if the next row has been buffered. */
155
156 /** Error code when the row was buffered. */
158
159 /** The parallel reader. */
161
162 /** Random generator engine used to provide us random uniformly distributed
163 values required to decide if the row in question needs to be sampled or
164 not. */
165 std::mt19937 m_random_generator;
166
167 /** Uniform distribution used by the random generator. */
168 static std::uniform_real_distribution<double> m_distribution;
169
170 /** Sampling method to be used for sampling. */
172
173 /** Sampling percentage to be used for sampling */
175
176 /** Sampling seed to be used for sampling */
178
179 /** Number of rows sampled */
180 std::atomic_size_t m_n_sampled;
181};
182
183#endif /* !row0pread_histogram_h */
Definition: row0pread-histogram.h:39
std::atomic_size_t m_n_sampled
Number of rows sampled.
Definition: row0pread-histogram.h:180
dberr_t process_leaf_rec(const Parallel_reader::Ctx *ctx, row_prebuilt_t *prebuilt)
Process the record in the leaf page.
Definition: row0pread-histogram.cc:360
int m_sampling_seed
Sampling seed to be used for sampling.
Definition: row0pread-histogram.h:177
os_event_t m_end_buffer_event
Event to notify if the next row has been buffered.
Definition: row0pread-histogram.h:154
bool skip()
Check if the processing of the record needs to be skipped.
Definition: row0pread-histogram.cc:190
void set(byte *buf)
Set the buffer.
Definition: row0pread-histogram.h:71
Parallel_reader m_parallel_reader
The parallel reader.
Definition: row0pread-histogram.h:160
enum_sampling_method m_sampling_method
Sampling method to be used for sampling.
Definition: row0pread-histogram.h:171
void wait_for_end_of_buffering()
Wait till the buffering of the row is complete.
Definition: row0pread-histogram.cc:176
os_event_t m_start_buffer_event
Event to notify if the next row needs to be buffered.
Definition: row0pread-histogram.h:151
static std::uniform_real_distribution< double > m_distribution
Uniform distribution used by the random generator.
Definition: row0pread-histogram.h:168
dberr_t process_non_leaf_rec(const Parallel_reader::Ctx *ctx, row_prebuilt_t *prebuilt)
For each record in a non-leaf block at level 1 (if leaf level is 0) check if the child page needs to ...
Definition: row0pread-histogram.cc:284
double m_sampling_percentage
Sampling percentage to be used for sampling.
Definition: row0pread-histogram.h:174
dberr_t m_err
Error code when the row was buffered.
Definition: row0pread-histogram.h:157
bool is_error_set() const
Definition: row0pread-histogram.h:102
dberr_t run()
Start the sampling process.
Definition: row0pread-histogram.cc:248
dberr_t finish_callback(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's end function.
Definition: row0pread-histogram.cc:114
void signal_start_of_buffering()
Signal that the next row needs to be buffered.
Definition: row0pread-histogram.cc:182
dberr_t sample_rec(const Parallel_reader::Ctx *ctx, const rec_t *rec, ulint *offsets, const dict_index_t *index, row_prebuilt_t *prebuilt)
Convert the row in InnoDB format to MySQL format and store in the buffer for server to use.
Definition: row0pread-histogram.cc:252
bool init(trx_t *trx, dict_index_t *index, row_prebuilt_t *prebuilt)
Initialize the sampler context.
Definition: row0pread-histogram.cc:140
byte * m_buf
Buffer to store the sampled row which is in the MySQL format.
Definition: row0pread-histogram.h:148
dberr_t start_callback(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's init function.
Definition: row0pread-histogram.cc:96
Histogram_sampler(size_t max_threads, int sampling_seed, double sampling_percentage, enum_sampling_method sampling_method)
Constructor.
Definition: row0pread-histogram.cc:44
void set_error_state(dberr_t err)
Set the error state.
Definition: row0pread-histogram.h:99
std::mt19937 m_random_generator
Random generator engine used to provide us random uniformly distributed values required to decide if ...
Definition: row0pread-histogram.h:165
void buffer_end()
End parallel read in case the reader thread is still active and wait for its exit.
Definition: row0pread-histogram.cc:238
~Histogram_sampler()
Destructor.
Definition: row0pread-histogram.cc:89
dberr_t buffer_next()
Buffer next row.
Definition: row0pread-histogram.cc:220
void wait_for_start_of_buffering()
Wait till there is a request to buffer the next row.
Definition: row0pread-histogram.cc:170
void signal_end_of_buffering()
Signal that the buffering of the row is complete.
Definition: row0pread-histogram.cc:186
Parallel reader execution context.
Definition: row0pread.h:679
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
dberr_t
Definition: db0err.h:38
@ DB_SUCCESS
Definition: db0err.h:42
Definition: buf0block_hint.cc:29
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:909
byte rec_t
Definition: rem0types.h:40
Parallel read interface.
enum_sampling_method
Definition: handler.h:705
Thread related context information.
Definition: row0pread.h:214
Data structure for an index.
Definition: dict0mem.h:1021
InnoDB condition variable.
Definition: os0event.cc:62
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.