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