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
|
/* 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 RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
#define RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "m_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "mysql/components/service.h"
#include "mysql/components/services/bits/mysql_rwlock_bits.h"
#include "mysql/components/services/bits/psi_thread_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/components/services/pfs_notification.h"
#include "mysql/components/services/pfs_resource_group.h"
#include "mysql/components/services/registry.h"
#include "mysql/psi/mysql_mutex.h"
#include "sql/debug_sync.h"
#include "sql/log.h"
#include "sql/mdl.h"
#include "sql/resourcegroups/resource_group_basic_types.h"
#include "sql/sql_class.h"
namespace dd {
class Resource_group;
} // namespace dd
namespace resourcegroups {
class Resource_group;
} // namespace resourcegroups
template <class Key, class Value>
class collation_unordered_map;
namespace resourcegroups {
extern const char *SYS_DEFAULT_RESOURCE_GROUP_NAME;
extern const char *USR_DEFAULT_RESOURCE_GROUP_NAME;
/**
This is a singleton class that provides various functionalities related to
Resource group management, more importantly the managing and the mapping of
resource group names to the corresponding in-memory resource group object.
*/
class Resource_group_mgr {
public:
/**
Singleton method to return an instance of this class.
*/
static Resource_group_mgr *instance();
/**
Destroy the singleton instance.
*/
static void destroy_instance();
/**
Check if support for Resource group exists at runtime.
@return true if resource group feature is supported else false.
*/
bool resource_group_support() { return m_resource_group_support; }
/**
Reason for resource group not being supported.
@return pointer to string which indicate reason for resource group
unsupported
*/
const char *unsupport_reason() { return m_unsupport_reason.c_str(); }
/**
Set reason for resource group not being supported.
@param reason string representing reason for resource group unsupported.
*/
void set_unsupport_reason(const std::string &reason) {
m_unsupport_reason = reason;
}
/**
Initialize the Resource group manager.
Must be called before instance() can be used.
@return true if initialization failed, false otherwise.
*/
bool init();
/**
Post initialization sequence during mysqld startup.
@return true if post initialization failed, else false.
*/
bool post_init();
/**
Disable and deinitialize the resource group if it was initialized before.
*/
void disable_resource_group() {
if (m_resource_group_support) {
LogErr(INFORMATION_LEVEL, ER_RES_GRP_FEATURE_NOT_AVAILABLE);
deinit();
m_resource_group_support = false;
}
}
/**
Get the in-memory Resource group object corresponding
to a resource group name.
@param resource_group_name Name of the resource group.
@return Pointer to the Resource group if it exists else nullptr.
*/
Resource_group *get_resource_group(const std::string &resource_group_name);
/**
Get the thread attributes identified by PSI thread ID.
@param [out] pfs_thread_attr Pointer to thread attribute object which
shall be populated if the call is successful.
@param thread_id PFS thread identifier representing a thread of mysqld.
@return true if call failed else false.
*/
bool get_thread_attributes(PSI_thread_attrs *pfs_thread_attr,
ulonglong thread_id) {
assert(m_resource_group_support);
return m_resource_group_svc->get_thread_system_attrs_by_id(
nullptr, thread_id, pfs_thread_attr) != 0;
}
/**
Add the resource group to the Resource group map.
@param resource_group_ptr pointer to in-memory Resource_group.
@return true if the call failed else false.
*/
bool add_resource_group(std::unique_ptr<Resource_group> resource_group_ptr);
/**
Remove the resource group from the map identified by it's name.
@param name of the resource group.
*/
void remove_resource_group(const std::string &name);
/**
Create an in-memory resource group identified by its attributes
and add it to the resource group map.
@param name Name of resource group.
@param type Type of resource group.
@param enabled Is resource group enabled?
@param cpus Pointer to list of Range objects representing CPU IDS.
@param thr_priority Thread priority.
@returns Pointer to Resource Group object if call is successful else null.
*/
Resource_group *create_and_add_in_resource_group_hash(
const LEX_CSTRING &name, Type type, bool enabled,
std::unique_ptr<std::vector<Range>> cpus, int thr_priority);
/**
Move the Resource group of the current thread identified by from_res_group
to the Resource group to_res_grp.
@param from_res_grp Pointer to current Resource_group of the thread.
@param to_res_grp Pointer to the destination Resource group which the
thread will be switched to.
@return true if move_resource_group failed else false.
*/
bool move_resource_group(Resource_group *from_res_grp,
Resource_group *to_res_grp);
/**
Deserialize a DD resource group object into an
in-memory Resource group object.
@param res_grp Pointer to DD Resource_group object.
@return Pointer to in-memory Resource_group subject if
Successful else null.
*/
Resource_group *deserialize_resource_group(const dd::Resource_group *res_grp);
/**
Set Resource group name in the PFS table performance_schema.threads for
the PFS thread id.
@param name Pointer to name of resource group.
@param length length of the resource group name.
@param thread_id PFS thread id of the thread,
*/
void set_res_grp_in_pfs(const char *name, int length, ulonglong thread_id) {
assert(m_resource_group_support);
m_resource_group_svc->set_thread_resource_group_by_id(
nullptr, thread_id, name, length, nullptr);
}
/**
Return the SYS_default resource group instance.
@return pointer to the SYS_default resource group.
*/
Resource_group *sys_default_resource_group() {
return m_sys_default_resource_group;
}
/**
Return the USR_default resource group instance.
@return pointer to the USR_default resource group.
*/
Resource_group *usr_default_resource_group() {
return m_usr_default_resource_group;
}
/**
Get names of SYS_default and USR_default resource groups.
Called by thread_create_callback only.
*/
const char *sys_default_resource_group_name() {
return SYS_DEFAULT_RESOURCE_GROUP_NAME;
}
const char *usr_default_resource_group_name() {
return USR_DEFAULT_RESOURCE_GROUP_NAME;
}
/**
Check if a given Resource group is either SYS_default or USR_default.
@return true if resource is USR_default or SYS_default else false.
*/
bool is_resource_group_default(const Resource_group *res_grp) {
return (res_grp == m_usr_default_resource_group ||
res_grp == m_sys_default_resource_group);
}
/**
Check if a thread priority setting can be done.
@return true if thread priority setting could be done else false.
*/
bool thread_priority_available() {
assert(m_resource_group_support);
return m_thread_priority_available;
}
/**
Acquire an shared MDL lock on resource group name.
@param thd Pointer to THD context.
@param res_grp_name Resource group name.
@param lock_duration Duration of lock.
@param[out] ticket reference to ticket object.
@param acquire_lock true if one needs to wait on lock
else false.
@return true if lock acquisition failed else false.
*/
bool acquire_shared_mdl_for_resource_group(THD *thd, const char *res_grp_name,
enum_mdl_duration lock_duration,
MDL_ticket **ticket,
bool acquire_lock);
/**
Release the shared MDL lock held on a resource group.
@param thd THD context.
@param ticket Pointer to lock ticket object.
*/
void release_shared_mdl_for_resource_group(THD *thd, MDL_ticket *ticket) {
assert(ticket != nullptr);
thd->mdl_context.release_lock(ticket);
}
/**
String corresponding to the type of resource group.
@param type Type of resource group.
@return string corresponding to resource group type.
*/
const char *resource_group_type_str(const Type &type) {
return type == Type::USER_RESOURCE_GROUP ? "User" : "System";
}
/**
Number of VCPUs present in the system.
@returns Number of VCPUs in the system.
*/
uint32_t num_vcpus() { return m_num_vcpus; }
void deinit();
#ifndef NDEBUG // The belows methods are required in debug build for testing.
bool disable_pfs_notification();
#endif
/**
Switch Resource Group if it is requested by environment.
@param thd THD context.
@param[out] src_res_grp Original resource group from that
switching is performed
@param[out] dest_res_grp Destination resource group to that
switching is performed
@param[out] ticket Pointer to MDL ticket object.
@param[out] cur_ticket Pointer to current resource group MDL ticket
object.
@return true if switching was performed, else false
*/
bool switch_resource_group_if_needed(
THD *thd, resourcegroups::Resource_group **src_res_grp,
resourcegroups::Resource_group **dest_res_grp, MDL_ticket **ticket,
MDL_ticket **cur_ticket);
/**
Restore original resource group if
resource group switching was performed before.
@param thd Thread context.
@param[out] src_res_grp resource group to that
switching was performed
@param[out] dest_res_grp original resource group to that
switching is performed
*/
void restore_original_resource_group(
THD *thd, resourcegroups::Resource_group *src_res_grp,
resourcegroups::Resource_group *dest_res_grp) {
mysql_mutex_lock(&thd->LOCK_thd_data);
if (thd->resource_group_ctx()->m_cur_resource_group == nullptr ||
thd->resource_group_ctx()->m_cur_resource_group == src_res_grp) {
resourcegroups::Resource_group_mgr::instance()->move_resource_group(
dest_res_grp, thd->resource_group_ctx()->m_cur_resource_group);
}
mysql_mutex_unlock(&thd->LOCK_thd_data);
DBUG_EXECUTE_IF("pause_after_rg_switch", {
const char act[] =
"now "
"SIGNAL restore_finished";
assert(!debug_sync_set_action(thd, STRING_WITH_LEN(act)));
};);
}
private:
/**
Pointer to singleton instance of the Resource_group_mgr class.
*/
static Resource_group_mgr *m_instance;
/**
Handles to the PFS registry, Resource Group and
Notification services.
*/
SERVICE_TYPE(registry) * m_registry_svc;
SERVICE_TYPE(pfs_resource_group_v3) * m_resource_group_svc;
SERVICE_TYPE(pfs_notification_v3) * m_notify_svc;
my_h_service m_h_res_grp_svc;
my_h_service m_h_notification_svc;
int m_notify_handle;
/**
Pointer to USR_default and SYS_default resource groups.
Owernship of these pointers is resource group hash.
*/
Resource_group *m_usr_default_resource_group;
Resource_group *m_sys_default_resource_group;
/**
Map mapping resource group name with it's corresponding in-memory
Resource_group object
*/
collation_unordered_map<std::string, std::unique_ptr<Resource_group>>
*m_resource_group_hash;
/**
Lock protecting the resource group map.
*/
mysql_rwlock_t m_map_rwlock;
/**
Boolean value indicating whether setting of thread priority is allowed.
*/
bool m_thread_priority_available;
/**
Bool value indicating support for resource group.
*/
bool m_resource_group_support;
/**
String indicating reason for resource group not being supported.
*/
std::string m_unsupport_reason;
/**
Value indicating the number of vcpus in the system. This is initialized
during startup so that we do not call the platform API every time
which is expensive.
*/
uint32_t m_num_vcpus;
Resource_group_mgr()
: m_registry_svc(nullptr),
m_resource_group_svc(nullptr),
m_notify_svc(nullptr),
m_h_res_grp_svc(nullptr),
m_h_notification_svc(nullptr),
m_notify_handle(0),
m_usr_default_resource_group(nullptr),
m_sys_default_resource_group(nullptr),
m_resource_group_hash(nullptr),
m_thread_priority_available(false),
m_resource_group_support(false),
m_num_vcpus(0) {}
~Resource_group_mgr() = default;
// Disable copy construction and assignment for Resource_group_mgr class.
Resource_group_mgr(const Resource_group_mgr &) = delete;
void operator=(const Resource_group_mgr &) = delete;
};
} // namespace resourcegroups
#endif // RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
|