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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/* Copyright (c) 2016, 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 <assert.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <atomic>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "m_ctype.h"
#include "m_string.h"
#include "mutex_lock.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_sys.h"
#include "mysql/components/service_implementation.h"
#include "mysql/components/services/bits/mysql_mutex_bits.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/bits/psi_mutex_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysqld_error.h"
#include "persistent_dynamic_loader_imp.h"
#include "scope_guard.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h" // commit_and_close_mysql_tables
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/iterators/row_iterator.h"
#include "sql/key.h"
#include "sql/mysqld.h"
#include "sql/sql_base.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_executor.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/transaction.h"
#include "sql_string.h"
#include "thr_lock.h"
#include "thr_mutex.h"
class THD;
SERVICE_TYPE(dynamic_loader) * dynamic_loader_srv;
typedef std::string my_string;
enum enum_component_table_field {
CT_FIELD_COMPONENT_ID = 0,
CT_FIELD_GROUP_ID,
CT_FIELD_COMPONENT_URN,
CT_FIELD_COUNT /* a cool trick to count the number of fields :) */
};
static const TABLE_FIELD_TYPE component_table_fields[CT_FIELD_COUNT] = {
{{STRING_WITH_LEN("component_id")}, {STRING_WITH_LEN("int")}, {nullptr, 0}},
{{STRING_WITH_LEN("component_group_id")},
{STRING_WITH_LEN("int")},
{nullptr, 0}},
{{STRING_WITH_LEN("component_urn")},
{STRING_WITH_LEN("text")},
{STRING_WITH_LEN("utf8mb3")}}};
static const TABLE_FIELD_DEF component_table_def = {CT_FIELD_COUNT,
component_table_fields};
class Component_db_intact : public Table_check_intact {
protected:
void report_error(uint ecode, const char *fmt, ...) override
MY_ATTRIBUTE((format(printf, 3, 4))) {
longlong log_ecode = 0;
switch (ecode) {
case 0:
log_ecode = ER_SERVER_TABLE_CHECK_FAILED;
break;
case ER_CANNOT_LOAD_FROM_TABLE_V2:
log_ecode = ER_SERVER_CANNOT_LOAD_FROM_TABLE_V2;
break;
case ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE_V2:
log_ecode = ER_SERVER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE_V2;
break;
case ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2:
log_ecode = ER_SERVER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2;
break;
default:
assert(false);
return;
}
va_list args;
va_start(args, fmt);
LogEvent()
.type(LOG_TYPE_ERROR)
.prio(ERROR_LEVEL)
.errcode(log_ecode)
.messagev(fmt, args);
va_end(args);
}
};
/** In case of an error, a message is printed to the error log. */
static Component_db_intact table_intact;
/**
Open mysql.component table for read or write.
Note that if the table can't be locked successfully this operation will
close it. Therefore it provides guarantee that it either opens and locks
table or fails without leaving any tables open.
@param thd Thread context
@param lock_type How to lock the table
@param [out] table Pointer to table structure to store the open table into.
@param acl_to_check acl type to check
@return Status of performed operation
@retval true open and lock failed - an error message is pushed into the
stack.
@retval false success
*/
static bool open_component_table(THD *thd, enum thr_lock_type lock_type,
TABLE **table, ulong acl_to_check) {
Table_ref tables("mysql", "component", lock_type);
if (mysql_persistent_dynamic_loader_imp::initialized() && !opt_noacl &&
check_one_table_access(thd, acl_to_check, &tables))
return true;
if (!(*table =
open_ltable(thd, &tables, lock_type, MYSQL_LOCK_IGNORE_TIMEOUT))) {
return true;
}
*table = tables.table;
(*table)->use_all_columns();
if (table_intact.check(thd, *table, &component_table_def)) {
trans_rollback_stmt(thd);
close_mysql_tables(thd);
return true;
}
// System table mysql.component is supported by only InnoDB engine.
if (lock_type == TL_WRITE) {
if (((*table)->file->ht->is_supported_system_table != nullptr) &&
!(*table)->file->ht->is_supported_system_table(
(*table)->s->db.str, (*table)->s->table_name.str, true)) {
my_error(ER_UNSUPPORTED_ENGINE, MYF(0),
ha_resolve_storage_engine_name((*table)->file->ht),
(*table)->s->db.str, (*table)->s->table_name.str);
LogErr(WARNING_LEVEL, ER_SYSTEM_TABLES_NOT_SUPPORTED_BY_STORAGE_ENGINE,
ha_resolve_storage_engine_name((*table)->file->ht),
(*table)->s->db.str, (*table)->s->table_name.str);
return true;
}
}
return false;
}
/**
Initializes persistence store, loads all groups of components registered in
component table. Shouldn't be called multiple times. We assume the order
specified by group ID is correct one. This should be assured by dynamic
loader as long as it will not allow to unload the component that has
dependency on, in case there would be a possibility to switch that
dependency to other component that is not to be unloaded. If this is
assured, then it will not be possible for components with lower group IDs
to have a dependency on component with higher group ID, even after state is
restored in this initialization method.
@param thdp Current thread execution context
@return Status of performed operation
@retval false success
@retval true failure
*/
bool mysql_persistent_dynamic_loader_imp::init(void *thdp) {
try {
THD *thd = reinterpret_cast<THD *>(thdp);
if (mysql_persistent_dynamic_loader_imp::initialized()) {
return true;
}
static PSI_mutex_key key_component_id_by_urn_mutex;
static PSI_mutex_info all_dyloader_mutexes[] = {
{&key_component_id_by_urn_mutex, "key_component_id_by_urn_mutex", 0, 0,
PSI_DOCUMENT_ME}};
int count = (int)array_elements(all_dyloader_mutexes);
mysql_mutex_register("p_dyn_loader", all_dyloader_mutexes, count);
mysql_mutex_init(key_component_id_by_urn_mutex, &component_id_by_urn_mutex,
MY_MUTEX_INIT_SLOW);
TABLE *component_table;
int res;
mysql_persistent_dynamic_loader_imp::s_group_id = 0;
/* Open component table and scan read all records. */
if (open_component_table(thd, TL_READ, &component_table, SELECT_ACL)) {
push_warning(thd, Sql_condition::SL_WARNING, ER_COMPONENT_TABLE_INCORRECT,
ER_THD(thd, ER_COMPONENT_TABLE_INCORRECT));
mysql_persistent_dynamic_loader_imp::is_initialized = true;
return false;
}
auto guard =
create_scope_guard([&thd]() { commit_and_close_mysql_tables(thd); });
unique_ptr_destroy_only<RowIterator> iterator = init_table_iterator(
thd, component_table, /*ignore_not_found_rows=*/false,
/*count_examined_rows=*/false);
if (iterator == nullptr) {
push_warning(thd, Sql_condition::SL_WARNING, ER_COMPONENT_TABLE_INCORRECT,
ER_THD(thd, ER_COMPONENT_TABLE_INCORRECT));
return false;
}
if (component_table->s->fields < CT_FIELD_COUNT) {
push_warning(thd, Sql_condition::SL_WARNING, ER_COMPONENT_TABLE_INCORRECT,
ER_THD(thd, ER_COMPONENT_TABLE_INCORRECT));
return false;
}
/* All read records will be aggregated in groups by group ID. */
std::map<uint64, std::vector<std::string>> component_groups;
for (;;) {
res = iterator->Read();
if (res != 0) {
break;
}
uint64 component_id =
component_table->field[CT_FIELD_COMPONENT_ID]->val_int();
uint64 component_group_id =
component_table->field[CT_FIELD_GROUP_ID]->val_int();
String component_urn_str;
component_table->field[CT_FIELD_COMPONENT_URN]->val_str(
&component_urn_str);
std::string component_urn(component_urn_str.ptr(),
component_urn_str.length());
mysql_persistent_dynamic_loader_imp::s_group_id =
std::max(mysql_persistent_dynamic_loader_imp::s_group_id.load(),
component_group_id);
component_groups[component_group_id].push_back(component_urn);
{
MUTEX_LOCK(lock, &component_id_by_urn_mutex);
mysql_persistent_dynamic_loader_imp::component_id_by_urn.emplace(
component_urn, component_id);
}
}
iterator.reset();
/* res is guaranteed to be != 0, -1 means end of records encountered, which
is interpreted as a success. */
assert(res != 0);
if (res != -1) {
return true;
}
for (auto it = component_groups.begin(); it != component_groups.end();
++it) {
std::vector<const char *> urns;
for (auto group_it = it->second.begin(); group_it != it->second.end();
++group_it) {
urns.push_back(group_it->c_str());
}
/* We continue process despite of any errors. */
dynamic_loader_srv->load(urns.data(), (int)urns.size());
}
mysql_persistent_dynamic_loader_imp::is_initialized = true;
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
De-initializes persistence loader.
*/
void mysql_persistent_dynamic_loader_imp::deinit() {
if (is_initialized) {
mysql_persistent_dynamic_loader_imp::component_id_by_urn.clear();
mysql_mutex_destroy(&component_id_by_urn_mutex);
}
mysql_persistent_dynamic_loader_imp::is_initialized = false;
}
/**
Initialisation status of persistence loader.
@return Status of performed operation
@retval true initialization is done
@retval false initialization is not done
*/
bool mysql_persistent_dynamic_loader_imp::initialized() {
return mysql_persistent_dynamic_loader_imp::is_initialized;
}
int mysql_persistent_dynamic_loader_imp::remove_from_cache(
const char *urns[], int component_count) {
int count_erased = 0;
MUTEX_LOCK(lock, &component_id_by_urn_mutex);
for (int i = 0; i < component_count; ++i)
count_erased +=
mysql_persistent_dynamic_loader_imp::component_id_by_urn.erase(urns[i]);
return count_erased;
}
/**
Loads specified group of components by URN, initializes them and
registers all service implementations present in these components.
Assures all dependencies will be met after loading specified components.
The dependencies may be circular, in such case it's necessary to specify
all components on cycle to load in one batch. From URNs specified the
scheme part of URN (part before "://") is extracted and used to acquire
service implementation of scheme component loader service for specified
scheme. If the loading process successes then a group of Components by their
URN is added to the component table.
@param thd_ptr Current thread execution context.
@param urns List of URNs of Components to load.
@param component_count Number of Components on list to load.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_persistent_dynamic_loader_imp::load,
(void *thd_ptr, const char *urns[], int component_count)) {
try {
THD *thd = (THD *)thd_ptr;
if (!mysql_persistent_dynamic_loader_imp::initialized()) {
my_error(ER_COMPONENT_TABLE_INCORRECT, MYF(0));
return true;
}
MUTEX_LOCK(lock, &component_id_by_urn_mutex);
/* We don't replicate INSTALL COMPONENT */
Disable_binlog_guard binlog_guard(thd);
TABLE *component_table;
auto guard_close_tables = create_scope_guard([&thd] {
trans_rollback_stmt(thd);
close_mysql_tables(thd);
});
if (open_component_table(thd, TL_WRITE, &component_table, INSERT_ACL)) {
my_error(ER_COMPONENT_TABLE_INCORRECT, MYF(0));
return true;
}
uint64 group_id = ++mysql_persistent_dynamic_loader_imp::s_group_id;
/* Making a component_id_by_urn copy so, that if any error occurs it will
be restored
*/
std::map<my_string, uint64> copy_urn_map(
mysql_persistent_dynamic_loader_imp::component_id_by_urn);
auto guard = create_scope_guard([©_urn_map]() {
/* Restoring back the component_id_by_urn */
mysql_persistent_dynamic_loader_imp::component_id_by_urn = copy_urn_map;
});
/* Insert all component URNs into component table into one group. */
CHARSET_INFO *system_charset = system_charset_info;
for (int i = 0; i < component_count; ++i) {
/* Get default values for fields. */
restore_record(component_table, s->default_values);
component_table->next_number_field =
component_table->found_next_number_field;
component_table->field[CT_FIELD_GROUP_ID]->store(group_id, true);
component_table->field[CT_FIELD_COMPONENT_URN]->store(
urns[i], strlen(urns[i]), system_charset);
int res = component_table->file->ha_write_row(component_table->record[0]);
if (res != 0) {
my_error(ER_COMPONENT_MANIPULATE_ROW_FAILED, MYF(0), urns[i], res);
component_table->file->ha_release_auto_increment();
return true;
}
/*
This is an attempt to sync an out of sync memory cache.
This should ideally be removing zero rows.
*/
mysql_persistent_dynamic_loader_imp::component_id_by_urn.erase(urns[i]);
/* Use last insert auto-increment column value and store it by the URN. */
mysql_persistent_dynamic_loader_imp::component_id_by_urn.emplace(
urns[i], component_table->file->insert_id_for_cur_row);
component_table->file->ha_release_auto_increment();
}
if (dynamic_loader_srv->load(urns, component_count)) {
return true;
}
guard.release();
guard_close_tables.release();
trans_commit_stmt(thd);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
/**
Unloads specified group of Components by URN, deinitializes them and
unregisters all service implementations present in these components.
Assumes, although does not check it, all dependencies of not unloaded
components will still be met after unloading specified components.
The dependencies may be circular, in such case it's necessary to specify
all components on cycle to unload in one batch. From URNs specified the
scheme part of URN (part before "://") is extracted and used to acquire
service implementation of scheme component loader service for specified
scheme. URN specified should be identical to ones specified in load()
method, i.e. all letters must have the same case. If the unloading process
successes then a group of Components by their URN is added to the component
table.
@param thd_ptr Current thread execution context.
@param urns List of URNs of components to unload.
@param component_count Number of components on list to unload.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_persistent_dynamic_loader_imp::unload,
(void *thd_ptr, const char *urns[], int component_count)) {
try {
THD *thd = (THD *)thd_ptr;
if (!mysql_persistent_dynamic_loader_imp::initialized()) {
my_error(ER_COMPONENT_TABLE_INCORRECT, MYF(0));
return true;
}
MUTEX_LOCK(lock, &component_id_by_urn_mutex);
TABLE *component_table;
if (open_component_table(thd, TL_WRITE, &component_table, DELETE_ACL)) {
my_error(ER_COMPONENT_TABLE_INCORRECT, MYF(0));
return true;
}
/* We don't replicate UNINSTALL_COMPONENT */
Disable_binlog_guard binlog_guard(thd);
/* Making component_id_by_urn copy so, that if any error occurs it will
be restored
*/
std::map<my_string, uint64> copy_urn_map(
mysql_persistent_dynamic_loader_imp::component_id_by_urn);
auto guard_close_tables = create_scope_guard([&thd, ©_urn_map]() {
/* Restoring back the component_id_by_urn */
mysql_persistent_dynamic_loader_imp::component_id_by_urn = copy_urn_map;
trans_rollback_stmt(thd);
close_mysql_tables(thd);
});
assert(component_table->key_info != nullptr);
for (int i = 0; i < component_count; ++i) {
/* Find component ID used in component table using memory mapping. */
auto it = mysql_persistent_dynamic_loader_imp::component_id_by_urn.find(
urns[i]);
if (it ==
mysql_persistent_dynamic_loader_imp::component_id_by_urn.end()) {
/* Component was loaded with persistence bypassed? If we continue, the
state will be still consistent. */
push_warning_printf(
thd, Sql_condition::SL_WARNING, ER_WARN_UNLOAD_THE_NOT_PERSISTED,
ER_THD(thd, ER_WARN_UNLOAD_THE_NOT_PERSISTED), urns[i]);
continue;
}
component_table->field[CT_FIELD_COMPONENT_ID]->store(it->second, true);
/* Place PK index on specified record and delete it. */
uchar key[MAX_KEY_LENGTH];
key_copy(key, component_table->record[0], component_table->key_info,
component_table->key_info->key_length);
DEBUG_SYNC(thd, "before_ha_index_read_idx_map");
int res = component_table->file->ha_index_read_idx_map(
component_table->record[0], 0, key, HA_WHOLE_KEY, HA_READ_KEY_EXACT);
if (res != 0) {
my_error(ER_COMPONENT_MANIPULATE_ROW_FAILED, MYF(0), urns[i], res);
return true;
}
res = component_table->file->ha_delete_row(component_table->record[0]);
if (res != 0) {
my_error(ER_COMPONENT_MANIPULATE_ROW_FAILED, MYF(0), urns[i], res);
return true;
}
mysql_persistent_dynamic_loader_imp::component_id_by_urn.erase(it);
}
bool result = dynamic_loader_srv->unload(urns, component_count);
if (result) {
/* No need to specify error, underlying service implementation would add
one. */
return result;
}
guard_close_tables.release();
trans_commit_stmt(thd);
return false;
} catch (...) {
mysql_components_handle_std_exception(__func__);
}
return true;
}
std::atomic<uint64> mysql_persistent_dynamic_loader_imp::s_group_id;
std::map<my_string, uint64>
mysql_persistent_dynamic_loader_imp::component_id_by_urn;
bool mysql_persistent_dynamic_loader_imp::is_initialized = false;
mysql_mutex_t mysql_persistent_dynamic_loader_imp::component_id_by_urn_mutex;
/**
Initializes persistence store, loads all groups of components registered in
component table.
@param thd Current thread execution context
@return Status of performed operation
@retval false success
@retval true failure
*/
bool persistent_dynamic_loader_init(void *thd) {
return mysql_persistent_dynamic_loader_imp::init(
reinterpret_cast<THD *>(thd));
}
void persistent_dynamic_loader_deinit() {
mysql_persistent_dynamic_loader_imp::deinit();
}
|