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