MySQL 8.4.3
Source Code Documentation
tablespace_stats.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD_INFO_SCHEMA_TABLESPACE_STATS_INCLUDED
25#define DD_INFO_SCHEMA_TABLESPACE_STATS_INCLUDED
26
27#include "sql/handler.h" // ha_tablespace_statistics
28#include "sql_string.h" // String
29
30class THD;
31
32namespace dd {
33namespace info_schema {
34
35// Tablespace statistics that are cached.
37 TS_ID,
38 TS_TYPE,
52};
53
54/**
55 The class hold dynamic table statistics for a table.
56 This cache is used by internal UDF's defined for the purpose
57 of INFORMATION_SCHEMA queries which retrieve dynamic table
58 statistics. The class caches statistics for just one table.
59
60 Overall aim of introducing this cache is to avoid making
61 multiple calls to same SE API to retrieve the statistics.
62*/
63
65 public:
67
68 /**
69 Check if the stats are cached for given tablespace_name and file_name.
70
71 @param tablespace_name - Tablespace name.
72 @param file_name - File name.
73
74 @return true if stats are cached, else false.
75 */
76 bool is_stat_cached(const String &tablespace_name, const String &file_name) {
77 return (m_key == form_key(tablespace_name, file_name));
78 }
79
80 /**
81 Store the statistics form the given handler
82
83 @param tablespace_name - Tablespace name.
84 @param file_name - File name.
85 @param stats - ha_tablespace_statistics.
86 */
87 void cache_stats(const String &tablespace_name, const String &file_name,
89 m_stats = stats;
90 m_found_error = false;
91 set_stat_cached(tablespace_name, file_name);
92 }
93
94 /**
95 Read dynamic tablespace statistics from SE API OR by reading cached
96 statistics from Query_block.
97
98 @param thd - Current thread.
99 @param tablespace_name_ptr - Tablespace name of which we need stats.
100 @param file_name_ptr - File name.
101 @param engine_name_ptr - Engine name.
102 @param ts_se_private_data - Tablespace se private data.
103
104 @return true if statistics were not fetched from SE, otherwise false.
105 */
106 bool read_stat(THD *thd, const String &tablespace_name_ptr,
107 const String &file_name_ptr, const String &engine_name_ptr,
108 const char *ts_se_private_data);
109
110 // Invalidate the cache.
111 void invalidate_cache(void) {
112 m_key.clear();
113 m_found_error = false;
114 }
115
116 /**
117 Mark that error was found for the given key. The combination of
118 tablespace and file name forms the key.
119
120 @param tablespace_name - Tablespace name.
121 @param file_name - File name.
122 */
123 void mark_as_error_found(const String &tablespace_name,
124 const String &file_name) {
125 m_stats = {};
126 m_found_error = true;
127 m_key = form_key(tablespace_name, file_name);
128 }
129
130 /**
131 Return statistics of the a given type.
132
133 @param stype Type of statistics requested.
134 @param[out] result Value for stype.
135 */
137
139
140 private:
141 /**
142 Mark the cache as valid for a given table. This creates a key for the
143 cache element. We store just a single table statistics in this cache.
144
145 @param tablespace_name - Tablespace name.
146 @param file_name - File name.
147 */
148 void set_stat_cached(const String &tablespace_name, const String &file_name) {
149 m_key = form_key(tablespace_name, file_name);
150 }
151
152 /**
153 Build a key representing the table for which stats are cached.
154
155 @param tablespace_name - Tablespace name.
156 @param file_name - File name.
157
158 @returns String_type representing the key.
159 */
160 String_type form_key(const String &tablespace_name, const String &file_name) {
161 return String_type(tablespace_name.ptr()) + String_type(file_name.ptr());
162 }
163
164 /**
165 Check if we have seen a error.
166
167 @param tablespace_name - Tablespace name.
168 @param file_name - File name.
169
170 @returns true if there is error reported.
171 false if not.
172 */
173 inline bool check_error_for_key(const String &tablespace_name,
174 const String &file_name) {
175 if (is_stat_cached(tablespace_name, file_name) && m_found_error)
176 return true;
177
178 return false;
179 }
180
181 /**
182 Read dynamic tablespace statistics from SE API.
183
184 @param thd - Current thread.
185 @param tablespace_name_ptr - Tablespace name of which we need stats.
186 @param file_name_ptr - File name.
187 @param engine_name_ptr - Engine name.
188 @param ts_se_private_data - Tablespace se private data.
189
190 @return true if statistics were not fetched from SE, otherwise false.
191 */
192 bool read_stat_from_SE(THD *thd, const String &tablespace_name_ptr,
193 const String &file_name_ptr,
194 const String &engine_name_ptr,
195 const char *ts_se_private_data);
196
197 public:
198 /// Predicate for determinig if cache is valid
199 bool is_valid() const { return !m_key.empty(); }
200
201 private:
202 // The cache key
203 String_type m_key; // Format '<tablespace_name>'
204
205 // Error found when reading statistics.
207
208 public:
209 // Cached statistics.
211};
212
213} // namespace info_schema
214} // namespace dd
215
216#endif // DD_INFO_SCHEMA_TABLESPACE_STATS_INCLUDED
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
const char * ptr() const
Definition: sql_string.h:249
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
The class hold dynamic table statistics for a table.
Definition: tablespace_stats.h:64
bool check_error_for_key(const String &tablespace_name, const String &file_name)
Check if we have seen a error.
Definition: tablespace_stats.h:173
bool is_stat_cached(const String &tablespace_name, const String &file_name)
Check if the stats are cached for given tablespace_name and file_name.
Definition: tablespace_stats.h:76
void set_stat_cached(const String &tablespace_name, const String &file_name)
Mark the cache as valid for a given table.
Definition: tablespace_stats.h:148
void cache_stats(const String &tablespace_name, const String &file_name, ha_tablespace_statistics &stats)
Store the statistics form the given handler.
Definition: tablespace_stats.h:87
bool m_found_error
Definition: tablespace_stats.h:206
void mark_as_error_found(const String &tablespace_name, const String &file_name)
Mark that error was found for the given key.
Definition: tablespace_stats.h:123
String_type m_key
Definition: tablespace_stats.h:203
String_type form_key(const String &tablespace_name, const String &file_name)
Build a key representing the table for which stats are cached.
Definition: tablespace_stats.h:160
void invalidate_cache(void)
Definition: tablespace_stats.h:111
bool read_stat(THD *thd, const String &tablespace_name_ptr, const String &file_name_ptr, const String &engine_name_ptr, const char *ts_se_private_data)
Read dynamic tablespace statistics from SE API OR by reading cached statistics from Query_block.
Definition: tablespace_stats.cc:140
bool read_stat_from_SE(THD *thd, const String &tablespace_name_ptr, const String &file_name_ptr, const String &engine_name_ptr, const char *ts_se_private_data)
Read dynamic tablespace statistics from SE API.
Definition: tablespace_stats.cc:193
ha_tablespace_statistics m_stats
Definition: tablespace_stats.h:210
Tablespace_statistics()
Definition: tablespace_stats.h:66
void get_stat(enum_tablespace_stats_type stype, ulonglong *result)
Return statistics of the a given type.
Definition: tablespace_stats.cc:35
bool is_valid() const
Predicate for determinig if cache is valid.
Definition: tablespace_stats.h:199
Definition: handler.h:7673
unsigned long long int ulonglong
Definition: my_inttypes.h:56
struct stats stats
Definition: mysqlslap.cc:238
enum_tablespace_stats_type
Definition: tablespace_stats.h:36
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:94
Our own string classes, used pervasively throughout the executor.
Definition: result.h:30
Definition: mysqlslap.cc:240