| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2019, 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-histogram.h |
| 29 |
Parallel read histogram interface. |
| 30 |
|
| 31 |
Created 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 |
|
| 40 |
class Histogram_sampler { |
| 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. */ |
| 52 |
~Histogram_sampler(); |
| 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 */ |
| 63 |
dberr_t buffer_next(); |
| 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. */ |
| 87 |
void wait_for_start_of_buffering(); |
| 88 |
|
| 89 |
/** Wait till the buffering of the row is complete. */ |
| 90 |
void wait_for_end_of_buffering(); |
| 91 |
|
| 92 |
/** Signal that the next row needs to be buffered. */ |
| 93 |
void signal_start_of_buffering(); |
| 94 |
|
| 95 |
/** Signal that the buffering of the row is complete. */ |
| 96 |
void signal_end_of_buffering(); |
| 97 |
|
| 98 |
/** Set the error state. |
| 99 |
@param[in] err Error state to set to. */ |
| 100 |
void set_error_state(dberr_t err) { m_err = err; } |
| 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. */ |
| 152 |
os_event_t m_start_buffer_event; |
| 153 |
|
| 154 |
/** Event to notify if the next row has been buffered. */ |
| 155 |
os_event_t m_end_buffer_event; |
| 156 |
|
| 157 |
/** Error code when the row was buffered. */ |
| 158 |
dberr_t m_err{DB_SUCCESS}; |
| 159 |
|
| 160 |
/** The parallel reader. */ |
| 161 |
Parallel_reader m_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. */ |
| 172 |
enum_sampling_method m_sampling_method{enum_sampling_method::NONE}; |
| 173 |
|
| 174 |
/** Sampling percentage to be used for sampling */ |
| 175 |
double m_sampling_percentage{}; |
| 176 |
|
| 177 |
/** Sampling seed to be used for sampling */ |
| 178 |
int m_sampling_seed{}; |
| 179 |
|
| 180 |
/** Number of rows sampled */ |
| 181 |
std::atomic_size_t m_n_sampled; |
| 182 |
}; |
| 183 |
|
| 184 |
#endif /* !row0pread_histogram_h */ |