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