MySQL  8.0.16
Source Code Documentation
tablespace_stats.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
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 
29 class THD;
30 struct TABLE_LIST;
31 
32 namespace dd {
33 namespace info_schema {
34 
35 // Tablespace statistics that are cached.
37  TS_ID,
38  TS_TYPE,
47  TS_VERSION,
50  TS_STATUS,
51  TS_EXTRA
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  @return void
88  */
89  void cache_stats(const String &tablespace_name, const String &file_name,
91  m_stats = stats;
92  m_found_error = false;
93  set_stat_cached(tablespace_name, file_name);
94  }
95 
96  /**
97  Read dynamic tablespace statistics from SE API OR by reading cached
98  statistics from SELECT_LEX.
99 
100  @param thd - Current thread.
101  @param tablespace_name_ptr - Tablespace name of which we need stats.
102  @param file_name_ptr - File name.
103  @param engine_name_ptr - Engine name.
104  @param ts_se_private_data - Tablespace se private data.
105 
106  @return true if statistics were not fetched from SE, otherwise false.
107  */
108  bool read_stat(THD *thd, const String &tablespace_name_ptr,
109  const String &file_name_ptr, const String &engine_name_ptr,
110  const char *ts_se_private_data);
111 
112  // Invalidate the cache.
113  void invalidate_cache(void) {
114  m_key.clear();
115  m_found_error = false;
116  }
117 
118  /**
119  Mark that error was found for the given key. The combination of
120  tablespace and file name forms the key.
121 
122  @param tablespace_name - Tablespace name.
123  @param file_name - File name.
124 
125  @return void
126  */
127  void mark_as_error_found(const String &tablespace_name,
128  const String &file_name) {
129  m_stats = {};
130  m_found_error = true;
131  m_key = form_key(tablespace_name, file_name);
132  }
133 
134  /**
135  Return statistics of the a given type.
136 
137  @param stype Type of statistics requested.
138  @param[out] result Value for stype.
139 
140  @returns void
141  */
142  void get_stat(enum_tablespace_stats_type stype, ulonglong *result);
143 
145 
146  private:
147  /**
148  Mark the cache as valid for a given table. This creates a key for the
149  cache element. We store just a single table statistics in this cache.
150 
151  @param tablespace_name - Tablespace name.
152  @param file_name - File name.
153 
154  @returns void.
155  */
156  void set_stat_cached(const String &tablespace_name, const String &file_name) {
157  m_key = form_key(tablespace_name, file_name);
158  }
159 
160  /**
161  Build a key representating the table for which stats are cached.
162 
163  @param tablespace_name - Tablespace name.
164  @param file_name - File name.
165 
166  @returns String_type representing the key.
167  */
168  String_type form_key(const String &tablespace_name, const String &file_name) {
169  return String_type(tablespace_name.ptr()) + String_type(file_name.ptr());
170  }
171 
172  /**
173  Check if we have seen a error.
174 
175  @param tablespace_name - Tablespace name.
176  @param file_name - File name.
177 
178  @returns true if there is error reported.
179  false if not.
180  */
181  inline bool check_error_for_key(const String &tablespace_name,
182  const String &file_name) {
183  if (is_stat_cached(tablespace_name, file_name) && m_found_error)
184  return true;
185 
186  return false;
187  }
188 
189  /**
190  Read dynamic tablespace statistics from SE API.
191 
192  @param thd - Current thread.
193  @param tablespace_name_ptr - Tablespace name of which we need stats.
194  @param file_name_ptr - File name.
195  @param engine_name_ptr - Engine name.
196  @param ts_se_private_data - Tablespace se private data.
197 
198  @return true if statistics were not fetched from SE, otherwise false.
199  */
200  bool read_stat_from_SE(THD *thd, const String &tablespace_name_ptr,
201  const String &file_name_ptr,
202  const String &engine_name_ptr,
203  const char *ts_se_private_data);
204 
205  private:
206  // The cache key
207  String_type m_key; // Format '<tablespace_name>'
208 
209  // Error found when reading statistics.
211 
212  public:
213  // Cached statistics.
215 };
216 
217 } // namespace info_schema
218 } // namespace dd
219 
220 #endif // DD_INFO_SCHEMA_TABLESPACE_STATS_INCLUDED
unsigned long long int ulonglong
Definition: my_inttypes.h:69
Our own string classes, used pervasively throughout the executor.
bool check_error_for_key(const String &tablespace_name, const String &file_name)
Check if we have seen a error.
Definition: tablespace_stats.h:181
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
Tablespace_statistics()
Definition: tablespace_stats.h:66
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
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:159
bool m_found_error
Definition: tablespace_stats.h:210
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:127
void invalidate_cache(void)
Definition: tablespace_stats.h:113
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 SELECT_LEX...
Definition: tablespace_stats.cc:139
void get_stat(enum_tablespace_stats_type stype, ulonglong *result)
Return statistics of the a given type.
Definition: tablespace_stats.cc:34
enum_tablespace_stats_type
Definition: tablespace_stats.h:36
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:89
String_type form_key(const String &tablespace_name, const String &file_name)
Build a key representating the table for which stats are cached.
Definition: tablespace_stats.h:168
The class hold dynamic table statistics for a table.
Definition: tablespace_stats.h:64
Global stats.
Definition: mysqlslap.cc:227
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:156
ha_tablespace_statistics m_stats
Definition: tablespace_stats.h:214
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
const char * ptr() const
Definition: sql_string.h:240
Definition: table.h:2439
struct stats stats
Definition: mysqlslap.cc:225
String_type m_key
Definition: tablespace_stats.h:207
Definition: handler.h:6699
#define false
Definition: config_static.h:43
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:776
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