File: tablespace_stats.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (272 lines) | stat: -rw-r--r-- 9,612 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* 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 */

#include "sql/dd/info_schema/tablespace_stats.h"  // dd::info_schema::Tables...

#include "sql/dd/properties.h"
#include "sql/error_handler.h"  // Info_schema_error_handler
#include "sql/sql_class.h"      // THD
#include "sql/strfunc.h"        // lex_cstring_handle

namespace dd {
namespace info_schema {

// Returns the required statistics from the cache.
void Tablespace_statistics::get_stat(enum_tablespace_stats_type stype,
                                     ulonglong *result) {
  switch (stype) {
    case enum_tablespace_stats_type::TS_ID:
      *result = m_stats.m_id;
      return;

    case enum_tablespace_stats_type::TS_LOGFILE_GROUP_NUMBER:
      *result = m_stats.m_logfile_group_number;
      return;

    case enum_tablespace_stats_type::TS_FREE_EXTENTS:
      *result = m_stats.m_free_extents;
      return;

    case enum_tablespace_stats_type::TS_TOTAL_EXTENTS:
      *result = m_stats.m_total_extents;
      return;

    case enum_tablespace_stats_type::TS_EXTENT_SIZE:
      *result = m_stats.m_extent_size;
      return;

    case enum_tablespace_stats_type::TS_INITIAL_SIZE:
      *result = m_stats.m_initial_size;
      return;

    case enum_tablespace_stats_type::TS_MAXIMUM_SIZE:
      *result = m_stats.m_maximum_size;
      return;

    case enum_tablespace_stats_type::TS_AUTOEXTEND_SIZE:
      *result = m_stats.m_autoextend_size;
      return;

    case enum_tablespace_stats_type::TS_VERSION:
      *result = m_stats.m_version;
      return;

    case enum_tablespace_stats_type::TS_DATA_FREE:
      *result = m_stats.m_data_free;
      return;

    default:
    case enum_tablespace_stats_type::TS_TYPE:
    case enum_tablespace_stats_type::TS_LOGFILE_GROUP_NAME:
    case enum_tablespace_stats_type::TS_ROW_FORMAT:
    case enum_tablespace_stats_type::TS_STATUS:
    case enum_tablespace_stats_type::TS_EXTRA:
      assert(!"Should not hit here");
      return;
  }

  assert(!"Should not hit here");
  return;
}

// Returns the required statistics from the cache.
void Tablespace_statistics::get_stat(enum_tablespace_stats_type stype,
                                     String_type *result) {
  switch (stype) {
    case enum_tablespace_stats_type::TS_TYPE:
      *result = m_stats.m_type;
      return;

    case enum_tablespace_stats_type::TS_LOGFILE_GROUP_NAME:
      *result = m_stats.m_logfile_group_name;
      return;

    case enum_tablespace_stats_type::TS_ROW_FORMAT:
      *result = m_stats.m_row_format;
      return;

    case enum_tablespace_stats_type::TS_STATUS:
      *result = m_stats.m_status;
      return;

    case enum_tablespace_stats_type::TS_EXTRA:
      *result = m_stats.m_extra;
      return;

    case enum_tablespace_stats_type::TS_ID:
    case enum_tablespace_stats_type::TS_LOGFILE_GROUP_NUMBER:
    case enum_tablespace_stats_type::TS_FREE_EXTENTS:
    case enum_tablespace_stats_type::TS_TOTAL_EXTENTS:
    case enum_tablespace_stats_type::TS_EXTENT_SIZE:
    case enum_tablespace_stats_type::TS_INITIAL_SIZE:
    case enum_tablespace_stats_type::TS_MAXIMUM_SIZE:
    case enum_tablespace_stats_type::TS_AUTOEXTEND_SIZE:
    case enum_tablespace_stats_type::TS_VERSION:
    case enum_tablespace_stats_type::TS_DATA_FREE:
    default:
      assert(!"Should not hit here");

      return;
  }
  assert(!"Should not hit here");

  return;
}

/*
  Read dynamic table statistics from SE OR by reading cached statistics
  from Query_block.
*/
bool Tablespace_statistics::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) {
  DBUG_TRACE;
  bool error = false;

  // Stop we have see and error already for this table.
  if (check_error_for_key(tablespace_name_ptr, file_name_ptr)) return true;

  //
  // Get statistics from cache, if available
  //

  if (is_stat_cached(tablespace_name_ptr, file_name_ptr)) return false;

  // NOTE: read_stat() may generate many "useless" warnings, which will be
  // ignored afterwards. On the other hand, there might be "useful"
  // warnings, which should be presented to the user. Diagnostics_area usually
  // stores no more than THD::variables.max_error_count warnings.
  // The problem is that "useless warnings" may occupy all the slots in the
  // Diagnostics_area, so "useful warnings" get rejected. In order to avoid
  // that problem we create a Diagnostics_area instance, which is capable of
  // storing "unlimited" number of warnings.
  Diagnostics_area *da = thd->get_stmt_da();
  Diagnostics_area tmp_da(true);

  // Don't copy existing conditions from the old DA so we don't get them twice
  // when we call copy_non_errors_from_da below.
  thd->push_diagnostics_area(&tmp_da, false);
  error = read_stat_from_SE(thd, tablespace_name_ptr, file_name_ptr,
                            engine_name_ptr, ts_se_private_data);
  thd->pop_diagnostics_area();

  // Pass an error if any.
  if (!thd->is_error() && tmp_da.is_error()) {
    da->set_error_status(tmp_da.mysql_errno(), tmp_da.message_text(),
                         tmp_da.returned_sqlstate());
    da->push_warning(thd, tmp_da.mysql_errno(), tmp_da.returned_sqlstate(),
                     Sql_condition::SL_ERROR, tmp_da.message_text());
  }

  // Pass warnings (if any).
  //
  // Filter out warnings with SL_ERROR level, because they
  // correspond to the errors which were filtered out in fill_table().
  da->copy_non_errors_from_da(thd, &tmp_da);

  return error;
}

// Fetch stats from SE
bool Tablespace_statistics::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) {
  DBUG_TRACE;

  //
  // Get statistics from the SE
  //
  ha_tablespace_statistics ha_tablespace_stat;

  // Acquire MDL_EXPLICIT lock on table.
  MDL_request mdl_request;
  MDL_REQUEST_INIT(&mdl_request, MDL_key::TABLESPACE, "",
                   tablespace_name_ptr.ptr(), MDL_SHARED, MDL_EXPLICIT);

  // Push deadlock error handler
  bool error = false;
  Info_schema_error_handler info_schema_error_handler(thd,
                                                      &tablespace_name_ptr);
  thd->push_internal_handler(&info_schema_error_handler);

  // Check if engine supports fetching tablespace statistics.
  plugin_ref tmp_plugin = ha_resolve_by_name_raw(
      thd, lex_cstring_handle(dd::String_type(engine_name_ptr.ptr())));
  handlerton *hton = nullptr;
  if (!tmp_plugin) {
    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), engine_name_ptr.ptr());
    error = true;
  } else if (!(hton = plugin_data<handlerton *>(tmp_plugin)) ||
             !hton->get_tablespace_statistics) {
    assert(!hton->get_tablespace_statistics);
    my_error(ER_NOT_IMPLEMENTED_GET_TABLESPACE_STATISTICS, MYF(0),
             engine_name_ptr.ptr());
    error = true;
  }

  error = error || thd->mdl_context.acquire_lock(
                       &mdl_request, thd->variables.lock_wait_timeout);
  thd->pop_internal_handler();

  if (!error) {
    // Prepare dd::Properties object for se_private_data and send it to SE.
    std::unique_ptr<dd::Properties> ts_se_private_data_obj(
        dd::Properties::parse_properties(ts_se_private_data ? ts_se_private_data
                                                            : ""));

    assert(ts_se_private_data_obj.get());

    //
    // Read statistics from SE
    //

    error = hton->get_tablespace_statistics(
        tablespace_name_ptr.ptr(), file_name_ptr.ptr(),
        *ts_se_private_data_obj.get(), &ha_tablespace_stat);

    // Release the lock we got
    thd->mdl_context.release_lock(mdl_request.ticket);
  }

  if (!error) {
    // Cache statistics.
    cache_stats(tablespace_name_ptr, file_name_ptr, ha_tablespace_stat);
  }

  if (thd->is_error()) {
    push_warning(thd, Sql_condition::SL_WARNING,
                 thd->get_stmt_da()->mysql_errno(),
                 thd->get_stmt_da()->message_text());
    thd->clear_error();
    mark_as_error_found(tablespace_name_ptr, file_name_ptr);
  }

  return error;
}

}  // namespace info_schema
}  // namespace dd