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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
|
/* 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 "component_common.h"
#include "mysql_service_implementation.h"
#include "registry_imp.h"
#include <mysql/components/my_service.h>
#include <mysql/components/service_implementation.h>
#include <mysql/components/services/registry.h>
#include <map>
#include <memory>
// pfs instrumentation headers
#include <mysql/components/services/mysql_rwlock.h>
/**
@page PAGE_COMPONENTS_REGISTRY The Service Registry Service
The Service Registry is a central part of Components subsystem. It maintains
a list of the Service Implementations and allow acquiring them by any actor
that has a reference to main Registry Service Implementation. For each Service
a reference counter is maintained to make sure no one can remove any Service
Implementation being actively used.
The Service Implementations can be acquired by the Service name or by the full
Service Implementation name.
For each Service a default Service Implementation is distinguished, the first
one to be added, but it is possible to change it at will at any point.
A convenient RAII-style wrapper on Service acquisition and automatic release
is provided, the my_service<T> class.
*/
static PSI_rwlock_key key_rwlock_LOCK_registry;
typedef std::map<const char *, mysql_service_implementation *, c_string_less>
my_service_registry;
struct my_h_service_iterator_imp {
my_service_registry::const_iterator m_it;
minimal_chassis::rwlock_scoped_lock m_lock;
};
struct my_h_service_metadata_iterator_imp {
my_metadata::const_iterator m_it;
minimal_chassis::rwlock_scoped_lock m_lock;
};
static PSI_rwlock_info all_registry_rwlocks[] = {
{&::key_rwlock_LOCK_registry, "LOCK_registry", PSI_FLAG_SINGLETON, 0,
PSI_DOCUMENT_ME}};
static void init_registry_psi_keys(void) {
const char *category = "components";
int count;
count = static_cast<int>(array_elements(all_registry_rwlocks));
mysql_rwlock_register(category, all_registry_rwlocks, count);
}
/**
Initializes registry for usage. Initializes RW lock, all other structures
should be empty. Shouldn't be called multiple times.
*/
void mysql_registry_imp::init() {
init_registry_psi_keys();
mysql_rwlock_init(key_rwlock_LOCK_registry,
&mysql_registry_imp::LOCK_registry);
}
/**
De-initializes registry. De-initializes RW lock, all other structures
are cleaned up.
*/
void mysql_registry_imp::deinit() {
mysql_registry_imp::service_registry.clear();
mysql_registry_imp::interface_mapping.clear();
mysql_rwlock_destroy(&mysql_registry_imp::LOCK_registry);
}
/**
De-initializes RW lock
*/
void mysql_registry_imp::rw_lock_deinit() {
mysql_rwlock_destroy(&mysql_registry_imp::LOCK_registry);
}
/**
Locks whole registry for write. For internal use only.
@return A lock acquired wrapped into RAII object.
*/
minimal_chassis::rwlock_scoped_lock
mysql_registry_imp::lock_registry_for_write() {
return minimal_chassis::rwlock_scoped_lock(&LOCK_registry, true, __FILE__,
__LINE__);
}
/**
Finds a Service Implementation data structure based on the pointer to
interface struct supplied. Assumes caller has at least a read lock on the
Registry.
@param interface A pointer to the interface structure of the Service
Implementation to look for.
@return A pointer to respective Service Implementation data structure, or
NULL if no such interface pointer is registered within the Registry.
*/
mysql_service_implementation *
mysql_registry_imp::get_service_implementation_by_interface(
my_h_service interface) {
my_interface_mapping::const_iterator iter =
mysql_registry_imp::interface_mapping.find(interface);
if (iter == mysql_registry_imp::interface_mapping.cend()) {
return nullptr;
}
return iter->second;
}
/**
Gets current reference count for a Service Implementation related to the
specified pointer to the interface structure. Assumes caller has at least
a read lock on the Registry.
@param interface A pointer to the interface structure of the Service
Implementation to get reference count of.
@return A current reference count for specified Service Implementation.
Returns 0 in case there is no such interface or it is not referenced.
*/
uint64_t mysql_registry_imp::get_service_implementation_reference_count(
my_h_service interface) {
my_interface_mapping::const_iterator iter =
mysql_registry_imp::interface_mapping.find(interface);
if (iter == mysql_registry_imp::interface_mapping.cend()) {
return -1;
}
return iter->second->get_reference_count();
}
/**
Finds and acquires a Service by name. A name of the Service or the Service
Implementation can be specified. In case of the Service name, the default
Service Implementation for Service specified will be returned. Assumes
caller has at least a read lock on the Registry.
@param service_name Name of Service or Service Implementation to acquire.
@param [out] out_service Pointer to Service handle to set acquired Service.
@return Status of performed operation
@retval false success
@retval true failure
*/
bool mysql_registry_imp::acquire_nolock(const char *service_name,
my_h_service *out_service) {
try {
if (out_service == nullptr) {
return true;
}
my_service_registry::const_iterator iter;
iter = mysql_registry_imp::service_registry.find(service_name);
if (iter == mysql_registry_imp::service_registry.cend()) return true;
mysql_service_implementation *imp = iter->second;
imp->add_reference();
*out_service = imp->interface();
return false;
} catch (...) {
}
return true;
}
/**
Releases the Service Implementation previously acquired. After the call to
this method the usage of the Service Implementation handle will lead to
unpredicted results. Assumes caller has at least a read lock on the
Registry.
@param service Service Implementation handle of already acquired Service.
@return Status of performed operation
@retval false success
@retval true failure
*/
bool mysql_registry_imp::release_nolock(my_h_service service) {
try {
if (service == nullptr) {
return true;
}
mysql_service_implementation *service_implementation =
mysql_registry_imp::get_service_implementation_by_interface(service);
if (service_implementation == nullptr) {
return true;
}
return service_implementation->release_reference();
} catch (...) {
}
return true;
}
/**
Registers a new Service Implementation. If it is the first Service
Implementation for the specified Service then it is made a default one.
Assumes caller has a write lock on the Registry.
@param service_implementation_name Name of the Service Implementation to
register.
@param ptr Pointer to the Service Implementation structure.
@return Status of performed operation
@retval false success
@retval true failure
*/
bool mysql_registry_imp::register_service_nolock(
const char *service_implementation_name, my_h_service ptr) {
try {
std::unique_ptr<mysql_service_implementation> imp =
std::unique_ptr<mysql_service_implementation>(
new mysql_service_implementation(ptr, service_implementation_name));
if (imp->interface() == nullptr) {
return true;
}
/* Register the implementation name. */
std::pair<my_service_registry::iterator, bool> addition_result =
mysql_registry_imp::service_registry.emplace(imp->name_c_str(),
imp.get());
/* Fail if it was present already. */
if (!addition_result.second) {
return true;
} else {
try {
/* Register interface in mapping */
mysql_registry_imp::interface_mapping.emplace(imp->interface(),
imp.get());
/* Register the Service Implementation as default for Service name in
case none were registered before. */
mysql_registry_imp::service_registry.emplace_hint(
addition_result.first, imp->service_name_c_str(), imp.get());
} catch (...) {
mysql_registry_imp::service_registry.erase(addition_result.first);
/* unique_ptr still has ownership over implementation object, we
don't have to delete it explicitly. */
return true;
}
}
/* Pointer is stored in registry, thous we release ownership. */
imp.release();
return false;
} catch (...) {
}
return true;
}
/**
Removes previously registered Service Implementation from registry. If it is
the default one for specified Service then any one still registered is made
default. If there is no other, the default entry is removed from the
Registry too. Assumes caller has a write lock on the Registry.
@param service_implementation_name Name of the Service Implementation to
unregister.
@return Status of performed operation
@retval false success
@retval true Failure. May happen when Service is still being referenced.
*/
bool mysql_registry_imp::unregister_nolock(
const char *service_implementation_name) {
try {
std::unique_ptr<mysql_service_implementation> imp;
{
/* Find the implementation and check if it is not being referenced. */
my_service_registry::iterator imp_iter =
mysql_registry_imp::service_registry.find(
service_implementation_name);
if (imp_iter == mysql_registry_imp::service_registry.end() ||
imp_iter->second->get_reference_count() > 0) {
return true;
}
/* First remove specified implementation, to not include it in search
for new default one. Take ownership on implementation object. */
imp.reset(imp_iter->second);
mysql_registry_imp::service_registry.erase(imp_iter);
/* After deletion, implementation iterator is not valid, we go out of
scope to prevent it from being reused. */
}
/* Remove interface mapping. */
mysql_registry_imp::interface_mapping.erase(
mysql_registry_imp::interface_mapping.find(imp->interface()));
/* Look if it is the default implementation. */
my_service_registry::iterator default_iter =
mysql_registry_imp::service_registry.find(imp->service_name_c_str());
if (default_iter == mysql_registry_imp::service_registry.end()) {
/* A Service Implementation and no default present. The state is not
consistent. */
return true;
}
if (default_iter->second == imp.get()) {
/* Remove the default implementation too. */
my_service_registry::iterator new_default_iter =
mysql_registry_imp::service_registry.erase(default_iter);
/* Search for a new default implementation. */
if (new_default_iter != mysql_registry_imp::service_registry.end() &&
!strcmp(imp->service_name_c_str(),
new_default_iter->second->service_name_c_str())) {
/* Set as default implementation. */
mysql_service_implementation *new_default = new_default_iter->second;
mysql_registry_imp::service_registry.emplace_hint(
new_default_iter, new_default->service_name_c_str(), new_default);
}
}
return false;
} catch (...) {
}
return true;
}
/**
Finds and acquires a Service by name. A name of the Service or the Service
Implementation can be specified. In case of the Service name, the default
Service Implementation for Service specified will be returned.
@param service_name Name of Service or Service Implementation to acquire.
@param [out] out_service Pointer to Service handle to set acquired Service.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::acquire,
(const char *service_name, my_h_service *out_service)) {
minimal_chassis::rwlock_scoped_lock lock(&LOCK_registry, false, __FILE__,
__LINE__);
return mysql_registry_imp::acquire_nolock(service_name, out_service);
}
/**
Finds a Service by name. If there is a Service Implementation with the same
Component part of name as the input Service then the found Service is
returned.
@param service_name Name of Service or Service Implementation to acquire.
@param service Service handle already acquired Service Implementation.
@param [out] out_service Pointer to Service Implementation handle to set
acquired Service Implementation.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::acquire_related,
(const char *service_name, my_h_service service,
my_h_service *out_service)) {
try {
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
false, __FILE__, __LINE__);
mysql_service_implementation *service_implementation =
mysql_registry_imp::get_service_implementation_by_interface(service);
if (service_implementation == nullptr) {
return true;
}
/* Find dot, the component name is right after the dot. */
const char *component_part =
strchr(service_implementation->name_c_str(), '.');
if (component_part == nullptr) {
return true;
}
/* Assure given service_name is not fully qualified. */
if (strchr(service_name, '.') != nullptr) {
return true;
}
my_string service_implementation_name =
my_string(service_name) + component_part;
/* Try to acquire such Service. */
if (mysql_registry_imp::acquire_nolock(service_implementation_name.c_str(),
out_service)) {
/* service is not found */
return true;
}
return false;
} catch (...) {
}
return true;
}
/**
Releases the Service Implementation previously acquired. After the call to
this method the usage of the Service Implementation handle will lead to
unpredicted results.
@param service Service Implementation handle of already acquired Service.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::release, (my_h_service service)) {
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
false, __FILE__, __LINE__);
return mysql_registry_imp::release_nolock(service);
}
/**
Registers a new Service Implementation. If it is the first Service
Implementation for the specified Service then it is made a default one.
@param service_implementation_name Name of the Service Implementation to
register.
@param ptr Pointer to the Service Implementation structure.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::register_service,
(const char *service_implementation_name,
my_h_service ptr)) {
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
true, __FILE__, __LINE__);
return mysql_registry_imp::register_service_nolock(
service_implementation_name, ptr);
}
/**
Removes previously registered Service Implementation from registry. If it is
the default one for specified Service then any one still registered is made
default. If there is no other, the default entry is removed from the
Registry too.
@param service_implementation_name Name of the Service Implementation to
unregister.
@return Status of performed operation
@retval false success
@retval true Failure. May happen when Service is still being referenced.
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::unregister,
(const char *service_implementation_name)) {
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
true, __FILE__, __LINE__);
return mysql_registry_imp::unregister_nolock(service_implementation_name);
}
/**
Sets new default Service Implementation for corresponding Service name.
@param service_implementation_name Name of the Service Implementation to
set as default one.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::set_default,
(const char *service_implementation_name)) {
try {
my_service_registry::const_iterator iter;
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
true, __FILE__, __LINE__);
/* register the implementation name */
iter =
mysql_registry_imp::service_registry.find(service_implementation_name);
if (iter == mysql_registry_imp::service_registry.cend()) {
return true;
}
mysql_service_implementation *imp = iter->second;
/* We have to remove and reinsert value as key, the string pointer will
not be valid if we unregister previous default implementation. */
iter = mysql_registry_imp::service_registry.erase(
mysql_registry_imp::service_registry.find(imp->service_name_c_str()));
mysql_registry_imp::service_registry.emplace_hint(
iter, imp->service_name_c_str(), imp);
return false;
} catch (...) {
}
return true;
}
/**
Creates iterator that iterates through all registered Service
Implementations. If successful it leaves read lock on the Registry until
iterator is released. The starting point of iteration may be specified
to be on one particular Service Implementation. The iterator will move
through all Service Implementations and additionally through all default
Service Implementation additionally, i.e. the default Service Implementation
will be returned twice. If no name is specified for search, iterator will be
positioned on the first Service Implementation.
@param service_name_pattern Name of Service or Service Implementation to
start iteration from. May be empty string or NULL pointer, in which case
iteration starts from the first Service Implementation.
@param [out] out_iterator Pointer to the Service Implementation iterator
handle.
@return Status of performed operation
@retval false success
@retval true failure
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::iterator_create,
(const char *service_name_pattern,
my_h_service_iterator *out_iterator)) {
try {
*out_iterator = {};
/* This read lock on whole registry will be held, until the iterator is
released. */
minimal_chassis::rwlock_scoped_lock lock(&mysql_registry_imp::LOCK_registry,
false, __FILE__, __LINE__);
my_service_registry::const_iterator r =
(!service_name_pattern || !*service_name_pattern)
? mysql_registry_imp::service_registry.cbegin()
: mysql_registry_imp::service_registry.find(service_name_pattern);
if (r == mysql_registry_imp::service_registry.cend()) {
return true;
}
*out_iterator = new my_h_service_iterator_imp{r, std::move(lock)};
return false;
} catch (...) {
}
return true;
}
/**
Releases Service implementations iterator. Releases read lock on registry.
@param iterator Service Implementation iterator handle.
*/
DEFINE_METHOD(void, mysql_registry_imp::iterator_release,
(my_h_service_iterator iterator)) {
try {
my_h_service_iterator_imp *iter =
reinterpret_cast<my_h_service_iterator_imp *>(iterator);
if (!iter) return;
delete iter;
} catch (...) {
}
}
/**
Gets name of Service pointed to by iterator. The pointer returned will last
at least up to the moment of call to the release() method on the iterator.
@param iterator Service Implementation iterator handle.
@param [out] out_name Pointer to string with name to set result pointer to.
@return Status of performed operation
@retval false success
@retval true Failure, may be caused when called on iterator that went
through all values already.
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::iterator_get,
(my_h_service_iterator iterator, const char **out_name)) {
try {
*out_name = nullptr;
if (!iterator) return true;
my_service_registry::const_iterator &iter =
reinterpret_cast<my_h_service_iterator_imp *>(iterator)->m_it;
if (iter != mysql_registry_imp::service_registry.cend()) {
*out_name = iter->second->name_c_str();
return false;
}
} catch (...) {
}
return true;
}
/**
Advances specified iterator to next element. Will succeed but return true if
it reaches one-past-last element.
@param iterator Service Implementation iterator handle.
@return Status of performed operation and validity of iterator after
operation.
@retval false success
@retval true Failure or called on iterator that was on last element.
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::iterator_next,
(my_h_service_iterator iterator)) {
try {
if (!iterator) return true;
my_service_registry::const_iterator &iter =
reinterpret_cast<my_h_service_iterator_imp *>(iterator)->m_it;
if (iter != mysql_registry_imp::service_registry.cend()) {
++iter;
return iter == mysql_registry_imp::service_registry.cend();
}
} catch (...) {
}
return true;
}
/**
Checks if specified iterator is valid, i.e. have not reached one-past-last
element.
@param iterator Service Implementation iterator handle.
@return Validity of iterator
@retval false Valid
@retval true Invalid or reached one-past-last element.
*/
DEFINE_BOOL_METHOD(mysql_registry_imp::iterator_is_valid,
(my_h_service_iterator iterator)) {
try {
if (!iterator) return true;
my_service_registry::const_iterator &iter =
reinterpret_cast<my_h_service_iterator_imp *>(iterator)->m_it;
return iter == mysql_registry_imp::service_registry.cend();
} catch (...) {
}
return true;
}
/* This includes metadata-related method implementations that are shared
by registry and dynamic_loader, so we don't duplicate the code. Following
defines set up all required symbols. Unfortunately they are not only the
types, but also static members with different name, so usage of templates
is not enough to reuse that part of code. */
#define REGISTRY_IMP mysql_registry_imp
#define REGISTRY mysql_registry_imp::service_registry
#define REGISTRY_TYPE my_service_registry
#define LOCK mysql_registry_imp::LOCK_registry
#define ITERATOR_TYPE my_h_service_iterator_imp
#define METADATA_ITERATOR_TYPE my_h_service_metadata_iterator_imp
#define OBJECT_ITERATOR my_h_service_iterator
#define METADATA_ITERATOR my_h_service_metadata_iterator
#include "registry_metadata.cc.inc"
/* static members for mysql_registry_imp */
my_service_registry mysql_registry_imp::service_registry;
mysql_registry_imp::my_interface_mapping mysql_registry_imp::interface_mapping;
mysql_rwlock_t mysql_registry_imp::LOCK_registry;
|