| 1 |
/* Copyright (c) 2021, 2026, 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 |
#include <cassert> |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#include <mysql/components/services/pfs_plugin_table_service.h> |
| 28 |
#include <mysql/components/services/registry.h> |
| 29 |
#include <mysql/service_plugin_registry.h> |
| 30 |
|
| 31 |
#include "log0chkp.h" |
| 32 |
#include "log0files_dict.h" |
| 33 |
#include "log0files_io.h" |
| 34 |
#include "log0log.h" |
| 35 |
#include "log0pfs.h" |
| 36 |
#include "log0sys.h" |
| 37 |
#include "ut0mutex.h" |
| 38 |
|
| 39 |
#include "sql/auto_thd.h" /* Auto_THD */ |
| 40 |
#include "sql/pfs_priv_util.h" /* drop_native_table_for_pfs */ |
| 41 |
#include "sql/sql_plugin.h" /* end_transaction */ |
| 42 |
#include "sql/table.h" /* PERFORMANCE_SCHEMA_DB_NAME */ |
| 43 |
#include "sql/thd_raii.h" /* Disable_autocommit_guard */ |
| 44 |
|
| 45 |
#include "sql/dd/cache/dictionary_client.h" /* Dictionary_client */ |
| 46 |
|
| 47 |
SERVICE_TYPE_NO_CONST(pfs_plugin_table_v1) * pfs_table{}; |
| 48 |
SERVICE_TYPE_NO_CONST(pfs_plugin_column_tiny_v1) * pfs_col_tinyint{}; |
| 49 |
SERVICE_TYPE_NO_CONST(pfs_plugin_column_integer_v1) * pfs_col_integer{}; |
| 50 |
SERVICE_TYPE_NO_CONST(pfs_plugin_column_bigint_v1) * pfs_col_bigint{}; |
| 51 |
SERVICE_TYPE_NO_CONST(pfs_plugin_column_string_v2) * pfs_col_string{}; |
| 52 |
|
| 53 |
static bool pfs_initialized{false}; |
| 54 |
|
| 55 |
static bool pfs_tables_created{false}; |
| 56 |
|
| 57 |
static constexpr const char *SVC_PFS_TABLE = "pfs_plugin_table_v1"; |
| 58 |
|
| 59 |
static constexpr const char *SVC_PFS_COLUMN_TINYINT = |
| 60 |
"pfs_plugin_column_tiny_v1"; |
| 61 |
|
| 62 |
static constexpr const char *SVC_PFS_COLUMN_INTEGER = |
| 63 |
"pfs_plugin_column_integer_v1"; |
| 64 |
|
| 65 |
static constexpr const char *SVC_PFS_COLUMN_BIGINT = |
| 66 |
"pfs_plugin_column_bigint_v1"; |
| 67 |
|
| 68 |
static constexpr const char *SVC_PFS_COLUMN_STRING = |
| 69 |
"pfs_plugin_column_string_v2"; |
| 70 |
|
| 71 |
/** PFS table with metadata of redo log files. */ |
| 72 |
class Log_files_pfs_table { |
| 73 |
/** Used to store data of a single row in the table. */ |
| 74 |
struct Row { |
| 75 |
Log_file_id m_id; |
| 76 |
lsn_t m_start_lsn, m_end_lsn; |
| 77 |
os_offset_t m_size_in_bytes; |
| 78 |
bool m_is_full; |
| 79 |
int m_consumer_level; |
| 80 |
}; |
| 81 |
|
| 82 |
public: |
| 83 |
int read_column_value(PSI_field *field, uint32_t index) { |
| 84 |
const auto row_index = m_position; |
| 85 |
const bool is_null = row_index == 0; |
| 86 |
ut_a(row_index <= m_rows_n); |
| 87 |
switch (index) { |
| 88 |
case 0: { /* FILE_ID */ |
| 89 |
PSI_ulonglong bigint_value; |
| 90 |
bigint_value.val = m_rows_array[row_index].m_id; |
| 91 |
bigint_value.is_null = is_null; |
| 92 |
pfs_col_bigint->set_unsigned(field, bigint_value); |
| 93 |
break; |
| 94 |
} |
| 95 |
case 1: /* FILE_NAME */ { |
| 96 |
const auto file_name = |
| 97 |
log_file_path(log_sys->m_files_ctx, m_rows_array[row_index].m_id); |
| 98 |
|
| 99 |
pfs_col_string->set_varchar_utf8mb4( |
| 100 |
field, is_null ? nullptr : file_name.c_str()); |
| 101 |
break; |
| 102 |
} |
| 103 |
case 2: { /* START_LSN */ |
| 104 |
PSI_ulonglong bigint_value; |
| 105 |
bigint_value.val = m_rows_array[row_index].m_start_lsn; |
| 106 |
bigint_value.is_null = is_null; |
| 107 |
pfs_col_bigint->set_unsigned(field, bigint_value); |
| 108 |
break; |
| 109 |
} |
| 110 |
case 3: { /* END_LSN */ |
| 111 |
PSI_ulonglong bigint_value; |
| 112 |
bigint_value.val = m_rows_array[row_index].m_end_lsn; |
| 113 |
bigint_value.is_null = is_null; |
| 114 |
pfs_col_bigint->set_unsigned(field, bigint_value); |
| 115 |
break; |
| 116 |
} |
| 117 |
case 4: { /* SIZE_IN_BYTES */ |
| 118 |
PSI_ulonglong bigint_value; |
| 119 |
bigint_value.val = m_rows_array[row_index].m_size_in_bytes; |
| 120 |
bigint_value.is_null = is_null; |
| 121 |
pfs_col_bigint->set_unsigned(field, bigint_value); |
| 122 |
break; |
| 123 |
} |
| 124 |
case 5: { /* IS_FULL */ |
| 125 |
PSI_tinyint tiny_value; |
| 126 |
tiny_value.val = m_rows_array[row_index].m_is_full ? 1 : 0; |
| 127 |
tiny_value.is_null = is_null; |
| 128 |
pfs_col_tinyint->set(field, tiny_value); |
| 129 |
break; |
| 130 |
} |
| 131 |
case 6: { /* CONSUMER_LEVEL */ |
| 132 |
PSI_uint int_value; |
| 133 |
int_value.val = m_rows_array[row_index].m_consumer_level; |
| 134 |
int_value.is_null = is_null; |
| 135 |
pfs_col_integer->set_unsigned(field, int_value); |
| 136 |
break; |
| 137 |
} |
| 138 |
default: |
| 139 |
assert(false); |
| 140 |
} |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
int rnd_init() { |
| 145 |
log_t &log = *log_sys; |
| 146 |
IB_mutex_guard latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 147 |
const auto n_files = log_files_number_of_existing_files(log.m_files); |
| 148 |
m_rows_array.reset(new Row[n_files + 1]); |
| 149 |
size_t i = 0; |
| 150 |
for (const auto &file : log.m_files) { |
| 151 |
m_rows_array[++i] = { |
| 152 |
file.m_id, |
| 153 |
file.m_start_lsn, |
| 154 |
file.m_end_lsn, |
| 155 |
file.m_size_in_bytes, |
| 156 |
file.m_full, |
| 157 |
file.m_consumed ? 1 : 0 // XXX |
| 158 |
}; |
| 159 |
} |
| 160 |
ut_a(i == n_files); |
| 161 |
m_rows_n = n_files; |
| 162 |
m_position = 0; |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
int rnd_next() { |
| 167 |
if (m_rows_n == 0) { |
| 168 |
return PFS_HA_ERR_END_OF_FILE; |
| 169 |
} |
| 170 |
++m_position; |
| 171 |
if (m_position <= m_rows_n) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
ut_a(m_position == m_rows_n + 1); |
| 175 |
return PFS_HA_ERR_END_OF_FILE; |
| 176 |
} |
| 177 |
|
| 178 |
int rnd_pos() { |
| 179 |
if (0 < m_position && m_position <= m_rows_n) { |
| 180 |
return 0; |
| 181 |
} |
| 182 |
return PFS_HA_ERR_END_OF_FILE; |
| 183 |
} |
| 184 |
|
| 185 |
void reset_pos() { m_position = 0; } |
| 186 |
|
| 187 |
void close() { m_position = 0; } |
| 188 |
|
| 189 |
uint32_t *position_address() { return &m_position; } |
| 190 |
|
| 191 |
private: |
| 192 |
uint32_t m_rows_n{}; |
| 193 |
uint32_t m_position{}; |
| 194 |
std::unique_ptr<Row[]> m_rows_array; |
| 195 |
}; |
| 196 |
|
| 197 |
/** PFS table handle which is used to create multiple instances for concurrent |
| 198 |
* queries*/ |
| 199 |
class Log_files_pfs_table_share_proxy : public PFS_engine_table_share_proxy { |
| 200 |
public: |
| 201 |
/** Name of the PFS mysql table (belongs to performance_schema) */ |
| 202 |
static constexpr const char *TABLE_NAME = "innodb_redo_log_files"; |
| 203 |
|
| 204 |
Log_files_pfs_table_share_proxy() { |
| 205 |
m_table_name = TABLE_NAME; |
| 206 |
m_table_name_length = strlen(TABLE_NAME); |
| 207 |
m_table_definition = |
| 208 |
"`FILE_ID` BIGINT NOT NULL" |
| 209 |
" COMMENT 'Id of the file.',\n" |
| 210 |
|
| 211 |
"`FILE_NAME` VARCHAR(2000) NOT NULL" |
| 212 |
" COMMENT 'Path to the file.',\n" |
| 213 |
|
| 214 |
"`START_LSN` BIGINT NOT NULL" |
| 215 |
" COMMENT 'LSN of the first block in the file.',\n" |
| 216 |
|
| 217 |
"`END_LSN` BIGINT NOT NULL" |
| 218 |
" COMMENT 'LSN after the last block in the file.',\n" |
| 219 |
|
| 220 |
"`SIZE_IN_BYTES` BIGINT NOT NULL" |
| 221 |
" COMMENT 'Size of the file (in bytes).',\n" |
| 222 |
|
| 223 |
"`IS_FULL` TINYINT NOT NULL" |
| 224 |
" COMMENT '1 iff file has no free space inside.',\n" |
| 225 |
|
| 226 |
"`CONSUMER_LEVEL` INT NOT NULL" |
| 227 |
" COMMENT 'All redo log consumers registered on smaller levels" |
| 228 |
" than this value, have already consumed this file.'\n"; |
| 229 |
|
| 230 |
m_ref_length = sizeof(uint32_t); |
| 231 |
m_acl = READONLY; |
| 232 |
delete_all_rows = nullptr; |
| 233 |
get_row_count = [] { |
| 234 |
log_t &log = *log_sys; |
| 235 |
IB_mutex_guard latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 236 |
return 1ULL * log_files_number_of_existing_files(log.m_files); |
| 237 |
}; |
| 238 |
|
| 239 |
auto &proxy_table = m_proxy_engine_table; |
| 240 |
proxy_table.open_table = [](PSI_pos **pos) { |
| 241 |
uint32_t **pos_ptr = reinterpret_cast<uint32_t **>(pos); |
| 242 |
Log_files_pfs_table *log_pfs_table = new Log_files_pfs_table(); |
| 243 |
*pos_ptr = log_pfs_table->position_address(); |
| 244 |
return reinterpret_cast<PSI_table_handle *>(log_pfs_table); |
| 245 |
}; |
| 246 |
|
| 247 |
proxy_table.close_table = [](PSI_table_handle *handle) { |
| 248 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 249 |
table->close(); |
| 250 |
delete table; |
| 251 |
}; |
| 252 |
|
| 253 |
proxy_table.rnd_init = [](PSI_table_handle *handle, bool) { |
| 254 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 255 |
return table->rnd_init(); |
| 256 |
}; |
| 257 |
|
| 258 |
proxy_table.rnd_next = [](PSI_table_handle *handle) { |
| 259 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 260 |
return table->rnd_next(); |
| 261 |
}; |
| 262 |
|
| 263 |
proxy_table.rnd_pos = [](PSI_table_handle *handle) { |
| 264 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 265 |
return table->rnd_pos(); |
| 266 |
}; |
| 267 |
|
| 268 |
proxy_table.read_column_value = [](PSI_table_handle *handle, |
| 269 |
PSI_field *field, uint32_t index) { |
| 270 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 271 |
return table->read_column_value(field, index); |
| 272 |
}; |
| 273 |
|
| 274 |
proxy_table.reset_position = [](PSI_table_handle *handle) { |
| 275 |
auto table = reinterpret_cast<Log_files_pfs_table *>(handle); |
| 276 |
table->reset_pos(); |
| 277 |
}; |
| 278 |
|
| 279 |
proxy_table.index_init = nullptr; |
| 280 |
proxy_table.index_read = nullptr; |
| 281 |
proxy_table.index_next = nullptr; |
| 282 |
|
| 283 |
proxy_table.write_column_value = nullptr; |
| 284 |
proxy_table.write_row_values = nullptr; |
| 285 |
proxy_table.update_column_value = nullptr; |
| 286 |
proxy_table.update_row_values = nullptr; |
| 287 |
proxy_table.delete_row_values = nullptr; |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
static Log_files_pfs_table_share_proxy pfs_table_share_proxy; |
| 292 |
|
| 293 |
template <typename T> |
| 294 |
static bool acquire_service(SERVICE_TYPE(registry) * reg_srv, T *service, |
| 295 |
const char *name) { |
| 296 |
my_h_service mysql_service; |
| 297 |
if (reg_srv->acquire(name, &mysql_service)) { |
| 298 |
return true; |
| 299 |
} |
| 300 |
*service = reinterpret_cast<T>(mysql_service); |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
template <typename T> |
| 305 |
static void release_service(SERVICE_TYPE(registry) * reg_srv, T **service) { |
| 306 |
if (*service != nullptr) { |
| 307 |
reg_srv->release(reinterpret_cast<my_h_service>(*service)); |
| 308 |
*service = nullptr; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
void pfs_sdi_enable(); |
| 313 |
void pfs_sdi_disable(); |
| 314 |
|
| 315 |
static bool log_pfs_should_create_tables() { return !srv_read_only_mode; } |
| 316 |
|
| 317 |
bool log_pfs_create_tables() { |
| 318 |
if (!log_pfs_should_create_tables()) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
if (!pfs_initialized) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
ut_a(pfs_table != nullptr); |
| 326 |
|
| 327 |
Auto_THD auto_thd; |
| 328 |
auto thd = auto_thd.thd; |
| 329 |
|
| 330 |
/* |
| 331 |
Set tx_read_only to false to allow installing PFS tables even |
| 332 |
if the server is started with --transaction-read-only=true. |
| 333 |
*/ |
| 334 |
thd->variables.transaction_read_only = false; |
| 335 |
thd->tx_read_only = false; |
| 336 |
|
| 337 |
pfs_sdi_disable(); |
| 338 |
|
| 339 |
{ |
| 340 |
Disable_autocommit_guard autocommit_guard{thd}; |
| 341 |
Disable_binlog_guard disable_binlog(thd); |
| 342 |
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client()); |
| 343 |
|
| 344 |
if (drop_native_table_for_pfs( |
| 345 |
PERFORMANCE_SCHEMA_DB_NAME.str, |
| 346 |
Log_files_pfs_table_share_proxy::TABLE_NAME)) { |
| 347 |
end_transaction(thd, true); |
| 348 |
pfs_sdi_enable(); |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
end_transaction(thd, false); |
| 353 |
} |
| 354 |
|
| 355 |
{ |
| 356 |
Disable_autocommit_guard autocommit_guard{thd}; |
| 357 |
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client()); |
| 358 |
|
| 359 |
PFS_engine_table_share_proxy *pfs_proxy_tables = &pfs_table_share_proxy; |
| 360 |
|
| 361 |
if (pfs_table->add_tables(&pfs_proxy_tables, 1) != 0) { |
| 362 |
end_transaction(thd, true); |
| 363 |
pfs_sdi_enable(); |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
end_transaction(thd, false); |
| 368 |
} |
| 369 |
|
| 370 |
pfs_sdi_enable(); |
| 371 |
|
| 372 |
pfs_tables_created = true; |
| 373 |
|
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
void log_pfs_delete_tables() { |
| 378 |
if (!pfs_tables_created) { |
| 379 |
return; |
| 380 |
} |
| 381 |
PFS_engine_table_share_proxy *pfs_proxy_tables[1]; |
| 382 |
pfs_proxy_tables[0] = &pfs_table_share_proxy; |
| 383 |
pfs_table->delete_tables(pfs_proxy_tables, 1); |
| 384 |
pfs_tables_created = false; |
| 385 |
} |
| 386 |
|
| 387 |
bool log_pfs_acquire_services(SERVICE_TYPE(registry) * reg_srv) { |
| 388 |
extern bool plugin_table_service_initialized; |
| 389 |
ut_a(plugin_table_service_initialized); |
| 390 |
|
| 391 |
ut_a(pfs_table == nullptr); |
| 392 |
|
| 393 |
if (reg_srv == nullptr) { |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
if (acquire_service(reg_srv, &pfs_table, SVC_PFS_TABLE) || |
| 398 |
acquire_service(reg_srv, &pfs_col_tinyint, SVC_PFS_COLUMN_TINYINT) || |
| 399 |
acquire_service(reg_srv, &pfs_col_integer, SVC_PFS_COLUMN_INTEGER) || |
| 400 |
acquire_service(reg_srv, &pfs_col_bigint, SVC_PFS_COLUMN_BIGINT) || |
| 401 |
acquire_service(reg_srv, &pfs_col_string, SVC_PFS_COLUMN_STRING)) { |
| 402 |
log_pfs_release_services(reg_srv); |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
pfs_initialized = true; |
| 407 |
|
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
void log_pfs_release_services(SERVICE_TYPE(registry) * reg_srv) { |
| 412 |
if (reg_srv == nullptr) { |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
release_service(reg_srv, &pfs_col_string); |
| 417 |
release_service(reg_srv, &pfs_col_bigint); |
| 418 |
release_service(reg_srv, &pfs_col_integer); |
| 419 |
release_service(reg_srv, &pfs_col_tinyint); |
| 420 |
release_service(reg_srv, &pfs_table); |
| 421 |
|
| 422 |
pfs_initialized = false; |
| 423 |
} |