MySQL 8.0.31
Source Code Documentation
resource_group_mgr.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2022, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
24#define RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
25
26#include <stdint.h>
27#include <memory>
28#include <string>
29#include <vector>
30
31#include "m_string.h"
32#include "my_dbug.h"
33#include "my_inttypes.h"
43#include "sql/debug_sync.h"
44#include "sql/log.h"
45#include "sql/mdl.h"
47#include "sql/sql_class.h"
48
49namespace dd {
50class Resource_group;
51} // namespace dd
52namespace resourcegroups {
53class Resource_group;
54class Resource_group_switch_handler;
55} // namespace resourcegroups
56template <class Key, class Value>
58
59namespace resourcegroups {
60
61extern const char *SYS_DEFAULT_RESOURCE_GROUP_NAME;
62extern const char *USR_DEFAULT_RESOURCE_GROUP_NAME;
63
65 std::pair<Resource_group_switch_handler *, Resource_group_switch_handler *>;
66
67/**
68 This is a singleton class that provides various functionalities related to
69 Resource group management, more importantly the managing and the mapping of
70 resource group names to the corresponding in-memory resource group object.
71*/
72
74 public:
75 /**
76 Singleton method to return an instance of this class.
77 */
78
80
81 /**
82 Destroy the singleton instance.
83 */
84
85 static void destroy_instance();
86
87 /**
88 Check if support for Resource group exists at runtime.
89
90 @return true if resource group feature is supported else false.
91 */
92
94
95 /**
96 Reason for resource group not being supported.
97
98 @return pointer to string which indicate reason for resource group
99 unsupported
100 */
101
102 const char *unsupport_reason() { return m_unsupport_reason.c_str(); }
103
104 /**
105 Set reason for resource group not being supported.
106
107 @param reason string representing reason for resource group unsupported.
108 */
109
110 void set_unsupport_reason(const std::string &reason) {
111 m_unsupport_reason = reason;
112 }
113
114 /**
115 Initialize the Resource group manager.
116 Must be called before instance() can be used.
117
118 @return true if initialization failed, false otherwise.
119 */
120
121 bool init();
122
123 /**
124 Post initialization sequence during mysqld startup.
125
126 @return true if post initialization failed, else false.
127 */
128
129 bool post_init();
130
131 /**
132 Disable and deinitialize the resource group if it was initialized before.
133 */
134
137 LogErr(INFORMATION_LEVEL, ER_RES_GRP_FEATURE_NOT_AVAILABLE);
138 deinit();
140 }
141 }
142
143 /**
144 Get the in-memory Resource group object corresponding
145 to a resource group name.
146
147 @param resource_group_name Name of the resource group.
148 @return Pointer to the Resource group if it exists else nullptr.
149 */
150
151 Resource_group *get_resource_group(const std::string &resource_group_name);
152
153 /**
154 Get the thread attributes identified by PSI thread ID.
155
156 @param [out] pfs_thread_attr Pointer to thread attribute object which
157 shall be populated if the call is successful.
158 @param thread_id PFS thread identifier representing a thread of mysqld.
159
160 @return true if call failed else false.
161 */
162
167 nullptr, thread_id, pfs_thread_attr) != 0;
168 }
169
170 /**
171 Add the resource group to the Resource group map.
172
173 @param resource_group_ptr pointer to in-memory Resource_group.
174 @return true if the call failed else false.
175 */
176
177 bool add_resource_group(std::unique_ptr<Resource_group> resource_group_ptr);
178
179 /**
180 Remove the resource group from the map identified by it's name.
181
182 @param name of the resource group.
183 */
184
185 void remove_resource_group(const std::string &name);
186
187 /**
188 Create an in-memory resource group identified by its attributes
189 and add it to the resource group map.
190
191 @param name Name of resource group.
192 @param type Type of resource group.
193 @param enabled Is resource group enabled?
194 @param cpus Pointer to list of Range objects representing CPU IDS.
195 @param thr_priority Thread priority.
196
197 @returns Pointer to Resource Group object if call is successful else null.
198 */
199
201 const LEX_CSTRING &name, Type type, bool enabled,
202 std::unique_ptr<std::vector<Range>> cpus, int thr_priority);
203
204 /**
205 Move the Resource group of the current thread identified by from_res_group
206 to the Resource group to_res_grp.
207
208 @param from_res_grp Pointer to current Resource_group of the thread.
209 @param to_res_grp Pointer to the destination Resource group which the
210 thread will be switched to.
211 @return true if move_resource_group failed else false.
212 */
213
214 bool move_resource_group(Resource_group *from_res_grp,
215 Resource_group *to_res_grp);
216
217 /**
218 Deserialize a DD resource group object into an
219 in-memory Resource group object.
220
221 @param res_grp Pointer to DD Resource_group object.
222 @return Pointer to in-memory Resource_group subject if
223 Successful else null.
224 */
225
227
228 /**
229 Set Resource group name in the PFS table performance_schema.threads for
230 the PFS thread id.
231
232 @param name Pointer to name of resource group.
233 @param length length of the resource group name.
234 @param thread_id PFS thread id of the thread,
235 */
236
240 nullptr, thread_id, name, length, nullptr);
241 }
242
243 /**
244 Return the SYS_default resource group instance.
245
246 @return pointer to the SYS_default resource group.
247 */
248
251 }
252
253 /**
254 Return the USR_default resource group instance.
255
256 @return pointer to the USR_default resource group.
257 */
258
261 }
262
263 /**
264 Get names of SYS_default and USR_default resource groups.
265 Called by thread_create_callback only.
266 */
269 }
272 }
273
274 /**
275 Check if a given Resource group is either SYS_default or USR_default.
276
277 @return true if resource is USR_default or SYS_default else false.
278 */
279
281 return (res_grp == m_usr_default_resource_group ||
283 }
284
285 /**
286 Check if a thread priority setting can be done.
287
288 @return true if thread priority setting could be done else false.
289 */
290
294 }
295
296 /**
297 Acquire global shared MDL lock on resource groups.
298
299 @param thd Pointer to THD context.
300 @param lock_duration Duration of lock.
301 @param[out] ticket reference to ticket object.
302
303 @return true if lock acquisition failed else false.
304 */
306 THD *thd, enum_mdl_duration lock_duration, MDL_ticket **ticket);
307
308 /**
309 Check if shared or stronger(exclusive) MDL lock is already acquired on
310 the resource group name.
311
312 @param thd Pointer to THD context.
313 @param res_grp_name Resource group name.
314
315 @return true if shared or stronger MDL lock is already acquired else false.
316 */
318 const char *res_grp_name);
319
320 /**
321 Acquire an shared MDL lock on resource group name.
322
323 @param thd Pointer to THD context.
324 @param res_grp_name Resource group name.
325 @param lock_duration Duration of lock.
326 @param[out] ticket reference to ticket object.
327 @param acquire_lock true if one needs to wait on lock
328 else false.
329
330 @return true if lock acquisition failed else false.
331 */
332
333 bool acquire_shared_mdl_for_resource_group(THD *thd, const char *res_grp_name,
334 enum_mdl_duration lock_duration,
335 MDL_ticket **ticket,
336 bool acquire_lock);
337
338 /**
339 Release the shared MDL lock held on a resource group.
340
341 @param thd THD context.
342 @param ticket Pointer to lock ticket object.
343 */
344
346 assert(ticket != nullptr);
347 thd->mdl_context.release_lock(ticket);
348 }
349
350 /**
351 String corresponding to the type of resource group.
352
353 @param type Type of resource group.
354
355 @return string corresponding to resource group type.
356 */
357
358 const char *resource_group_type_str(const Type &type) {
359 return type == Type::USER_RESOURCE_GROUP ? "User" : "System";
360 }
361
362 /**
363 Number of VCPUs present in the system.
364
365 @returns Number of VCPUs in the system.
366 */
367
368 uint32_t num_vcpus() { return m_num_vcpus; }
369
370 void deinit();
371
372#ifndef NDEBUG // The belows methods are required in debug build for testing.
374#endif
375
376 /**
377 Switch Resource Group if it is requested by environment.
378
379 @param thd THD context.
380 @param[out] src_res_grp Original resource group from that
381 switching is performed
382 @param[out] dest_res_grp Destination resource group to that
383 switching is performed
384 @param[out] ticket Pointer to MDL ticket object.
385 @param[out] cur_ticket Pointer to current resource group MDL ticket
386 object.
387
388 @return true if switching was performed, else false
389 */
390
392 THD *thd, resourcegroups::Resource_group **src_res_grp,
393 resourcegroups::Resource_group **dest_res_grp, MDL_ticket **ticket,
394 MDL_ticket **cur_ticket);
395
396 /**
397 Restore original resource group if
398 resource group switching was performed before.
399
400 @param thd Thread context.
401 @param[out] src_res_grp resource group to that
402 switching was performed
403 @param[out] dest_res_grp original resource group to that
404 switching is performed
405*/
406
408 THD *thd, resourcegroups::Resource_group *src_res_grp,
409 resourcegroups::Resource_group *dest_res_grp) {
411 if (thd->resource_group_ctx()->m_cur_resource_group == nullptr ||
412 thd->resource_group_ctx()->m_cur_resource_group == src_res_grp) {
414 dest_res_grp, thd->resource_group_ctx()->m_cur_resource_group);
415 }
417 DBUG_EXECUTE_IF("pause_after_rg_switch", {
418 const char act[] =
419 "now "
420 "SIGNAL restore_finished";
421 assert(!debug_sync_set_action(thd, STRING_WITH_LEN(act)));
422 };);
423 }
424
425 /**
426 Set user thread resource group to the system thread. This method must be
427 called from the system thread.
428
429 @param thd Thread handle.
430 @param user_thread_pfs_id PFS thread id of the user
431 thread.
432 @param[out] resource_grp_switch_context Resource group switch context
433 instance.
434
435 @retval false On Success.
436 @retval true On failure.
437 */
439 THD *thd, ulonglong user_thread_pfs_id,
440 Resource_group_switch_context **resource_grp_switch_context);
441
442 /**
443 If system thread resource group is switched to the user thread resource
444 group then restore original system group's resource group. This method must
445 be called from the system thread.
446
447 @param thd Thread handle.
448 @param[in,out] resource_grp_switch_context Resource group switch context
449 instance.
450
451 @retval false On Success.
452 @retval true On failure.
453 */
455 THD *thd, Resource_group_switch_context **resource_grp_switch_context);
456
457 private:
458 /**
459 Pointer to singleton instance of the Resource_group_mgr class.
460 */
462
463 /**
464 Handles to the PFS registry, Resource Group and
465 Notification services.
466 */
467
469 SERVICE_TYPE(pfs_resource_group_v3) * m_resource_group_svc;
470 SERVICE_TYPE(pfs_notification_v3) * m_notify_svc;
474
475 /**
476 Pointer to USR_default and SYS_default resource groups.
477 Owernship of these pointers is resource group hash.
478 */
481
482 /**
483 Map mapping resource group name with it's corresponding in-memory
484 Resource_group object
485 */
488
489 /**
490 Lock protecting the resource group map.
491 */
493
494 /**
495 Boolean value indicating whether setting of thread priority is allowed.
496 */
498
499 /**
500 Bool value indicating support for resource group.
501 */
503
504 /**
505 String indicating reason for resource group not being supported.
506 */
508
509 /**
510 Value indicating the number of vcpus in the system. This is initialized
511 during startup so that we do not call the platform API every time
512 which is expensive.
513 */
514 uint32_t m_num_vcpus;
515
528 m_num_vcpus(0) {}
529
531
532 // Disable copy construction and assignment for Resource_group_mgr class.
534 void operator=(const Resource_group_mgr &) = delete;
535};
536} // namespace resourcegroups
537#endif // RESOURCEGROUPS_RESOURCE_GROUP_MGR_H_
void release_lock(MDL_ticket *ticket)
Release lock with explicit duration.
Definition: mdl.cc:4203
A granted metadata lock.
Definition: mdl.h:984
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
MDL_context mdl_context
Definition: sql_class.h:953
resourcegroups::Resource_group_ctx * resource_group_ctx()
Get resource group context.
Definition: sql_class.h:3898
mysql_mutex_t LOCK_thd_data
Protects THD data accessed from other threads.
Definition: sql_class.h:1200
std::unordered_map, but with my_malloc and collation-aware comparison.
Definition: map_helpers.h:209
Definition: resource_group.h:48
This is a singleton class that provides various functionalities related to Resource group management,...
Definition: resource_group_mgr.h:73
static Resource_group_mgr * m_instance
Pointer to singleton instance of the Resource_group_mgr class.
Definition: resource_group_mgr.h:461
my_h_service m_h_res_grp_svc
Definition: resource_group_mgr.h:471
void restore_original_resource_group(THD *thd, resourcegroups::Resource_group *src_res_grp, resourcegroups::Resource_group *dest_res_grp)
Restore original resource group if resource group switching was performed before.
Definition: resource_group_mgr.h:407
bool acquire_global_shared_mdl_for_resource_group(THD *thd, enum_mdl_duration lock_duration, MDL_ticket **ticket)
Acquire global shared MDL lock on resource groups.
Definition: resource_group_mgr.cc:262
void deinit()
Definition: resource_group_mgr.cc:306
void set_unsupport_reason(const std::string &reason)
Set reason for resource group not being supported.
Definition: resource_group_mgr.h:110
void remove_resource_group(const std::string &name)
Remove the resource group from the map identified by it's name.
Definition: resource_group_mgr.cc:530
Resource_group_mgr(const Resource_group_mgr &)=delete
bool restore_system_thread_resource_group(THD *thd, Resource_group_switch_context **resource_grp_switch_context)
If system thread resource group is switched to the user thread resource group then restore original s...
Definition: resource_group_mgr.cc:979
bool init()
Initialize the Resource group manager.
Definition: resource_group_mgr.cc:345
bool thread_priority_available()
Check if a thread priority setting can be done.
Definition: resource_group_mgr.h:291
collation_unordered_map< std::string, std::unique_ptr< Resource_group > > * m_resource_group_hash
Map mapping resource group name with it's corresponding in-memory Resource_group object.
Definition: resource_group_mgr.h:487
const char * resource_group_type_str(const Type &type)
String corresponding to the type of resource group.
Definition: resource_group_mgr.h:358
Resource_group * deserialize_resource_group(const dd::Resource_group *res_grp)
Deserialize a DD resource group object into an in-memory Resource group object.
Definition: resource_group_mgr.cc:224
bool owns_shared_or_stronger_mdl_for_resource_group(THD *thd, const char *res_grp_name)
Check if shared or stronger(exclusive) MDL lock is already acquired on the resource group name.
Definition: resource_group_mgr.cc:277
const mysql_service_pfs_resource_group_v3_t * m_resource_group_svc
Definition: resource_group_mgr.h:469
Resource_group_mgr()
Definition: resource_group_mgr.h:516
my_h_service m_h_notification_svc
Definition: resource_group_mgr.h:472
uint32_t m_num_vcpus
Value indicating the number of vcpus in the system.
Definition: resource_group_mgr.h:514
void operator=(const Resource_group_mgr &)=delete
bool disable_pfs_notification()
Definition: resource_group_mgr.cc:564
Resource_group * sys_default_resource_group()
Return the SYS_default resource group instance.
Definition: resource_group_mgr.h:249
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)
Switch Resource Group if it is requested by environment.
Definition: resource_group_mgr.cc:574
bool set_user_thread_resource_group_to_system_thread(THD *thd, ulonglong user_thread_pfs_id, Resource_group_switch_context **resource_grp_switch_context)
Set user thread resource group to the system thread.
Definition: resource_group_mgr.cc:831
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)
Acquire an shared MDL lock on resource group name.
Definition: resource_group_mgr.cc:286
void disable_resource_group()
Disable and deinitialize the resource group if it was initialized before.
Definition: resource_group_mgr.h:135
Resource_group * usr_default_resource_group()
Return the USR_default resource group instance.
Definition: resource_group_mgr.h:259
void release_shared_mdl_for_resource_group(THD *thd, MDL_ticket *ticket)
Release the shared MDL lock held on a resource group.
Definition: resource_group_mgr.h:345
mysql_rwlock_t m_map_rwlock
Lock protecting the resource group map.
Definition: resource_group_mgr.h:492
bool is_resource_group_default(const Resource_group *res_grp)
Check if a given Resource group is either SYS_default or USR_default.
Definition: resource_group_mgr.h:280
const char * usr_default_resource_group_name()
Definition: resource_group_mgr.h:270
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)
Create an in-memory resource group identified by its attributes and add it to the resource group map.
Definition: resource_group_mgr.cc:539
bool add_resource_group(std::unique_ptr< Resource_group > resource_group_ptr)
Add the resource group to the Resource group map.
Definition: resource_group_mgr.cc:518
bool move_resource_group(Resource_group *from_res_grp, Resource_group *to_res_grp)
Move the Resource group of the current thread identified by from_res_group to the Resource group to_r...
Definition: resource_group_mgr.cc:454
bool m_thread_priority_available
Boolean value indicating whether setting of thread priority is allowed.
Definition: resource_group_mgr.h:497
bool m_resource_group_support
Bool value indicating support for resource group.
Definition: resource_group_mgr.h:502
static Resource_group_mgr * instance()
Singleton method to return an instance of this class.
Definition: resource_group_mgr.cc:107
Resource_group * m_usr_default_resource_group
Pointer to USR_default and SYS_default resource groups.
Definition: resource_group_mgr.h:479
const char * unsupport_reason()
Reason for resource group not being supported.
Definition: resource_group_mgr.h:102
bool resource_group_support()
Check if support for Resource group exists at runtime.
Definition: resource_group_mgr.h:93
const mysql_service_pfs_notification_v3_t * m_notify_svc
Definition: resource_group_mgr.h:470
static void destroy_instance()
Destroy the singleton instance.
Definition: resource_group_mgr.cc:493
const char * sys_default_resource_group_name()
Get names of SYS_default and USR_default resource groups.
Definition: resource_group_mgr.h:267
uint32_t num_vcpus()
Number of VCPUs present in the system.
Definition: resource_group_mgr.h:368
const mysql_service_registry_t * m_registry_svc
Handles to the PFS registry, Resource Group and Notification services.
Definition: resource_group_mgr.h:468
int m_notify_handle
Definition: resource_group_mgr.h:473
Resource_group * get_resource_group(const std::string &resource_group_name)
Get the in-memory Resource group object corresponding to a resource group name.
Definition: resource_group_mgr.cc:501
bool get_thread_attributes(PSI_thread_attrs *pfs_thread_attr, ulonglong thread_id)
Get the thread attributes identified by PSI thread ID.
Definition: resource_group_mgr.h:163
Resource_group * m_sys_default_resource_group
Definition: resource_group_mgr.h:480
bool post_init()
Post initialization sequence during mysqld startup.
Definition: resource_group_mgr.cc:321
void set_res_grp_in_pfs(const char *name, int length, ulonglong thread_id)
Set Resource group name in the PFS table performance_schema.threads for the PFS thread id.
Definition: resource_group_mgr.h:237
std::string m_unsupport_reason
String indicating reason for resource group not being supported.
Definition: resource_group_mgr.h:507
Class that represents an abstraction of the Resource Group.
Definition: resource_group.h:45
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:49
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:56
Declarations for the Debug Sync Facility.
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
struct my_h_service_imp * my_h_service
A handle type for acquired Service.
Definition: registry.h:32
Error logging, slow query logging, general query logging: If it's server-internal,...
#define LogErr(severity, ecode,...)
Definition: log_builtins.h:842
#define STRING_WITH_LEN(X)
Definition: m_string.h:314
enum_mdl_duration
Duration of metadata lock.
Definition: mdl.h:332
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:170
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
@ INFORMATION_LEVEL
Definition: my_loglevel.h:44
static my_thread_id thread_id
Definition: my_thr_init.cc:62
Instrumentation helpers for rwlock.
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
Definition: dd_resource_group.h:28
std::pair< Resource_group_switch_handler *, Resource_group_switch_handler * > Resource_group_switch_context
Definition: resource_group_mgr.h:65
Type
Definition: resource_group_basic_types.h:32
const char * SYS_DEFAULT_RESOURCE_GROUP_NAME
Definition: resource_group_mgr.cc:67
const char * USR_DEFAULT_RESOURCE_GROUP_NAME
Definition: resource_group_mgr.cc:68
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2436
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
required string type
Definition: replication_group_member_actions.proto:33
required bool enabled
Definition: replication_group_member_actions.proto:32
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:75
case opt name
Definition: sslopt-case.h:32
Definition: mysql_lex_string.h:39
Performance Schema thread type: user/foreground or system/background.
Definition: psi_thread_bits.h:469
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:50
Resource_group * m_cur_resource_group
Definition: resource_group_basic_types.h:54
get_thread_system_attrs_by_id_v3_t get_thread_system_attrs_by_id
Definition: pfs_resource_group.h:135
set_thread_resource_group_by_id_v1_t set_thread_resource_group_by_id
Definition: pfs_resource_group.h:133