-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathresource_group_sql_cmd.cc
719 lines (595 loc) · 25.8 KB
/
resource_group_sql_cmd.cc
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* 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 "sql/resourcegroups/resource_group_sql_cmd.h"
#include <stdint.h>
#include <string.h>
#include <memory>
#include <new>
#include <string>
#include <utility>
#include <vector>
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/components/services/bits/psi_thread_bits.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "pfs_thread_provider.h"
#include "sql/auth/auth_acls.h" // SUPER_ACL
#include "sql/auth/auth_common.h" // check_readonly
#include "sql/auth/sql_security_ctx.h"
#include "sql/current_thd.h" // current_thd
#include "sql/dd/cache/dictionary_client.h" // dd::cache::Dictionary_client
#include "sql/dd/dd_resource_group.h" // resource_group_exists, etc.
#include "sql/dd/string_type.h" // String_type
#include "sql/derror.h" // ER_THD
#include "sql/lock.h" // acquire_shared_global...
#include "sql/mdl.h"
#include "sql/mysqld_thd_manager.h" // Find_thd_with_id
#include "sql/parse_tree_helpers.h"
#include "sql/resourcegroups/resource_group.h" // Resource_group
#include "sql/resourcegroups/resource_group_mgr.h" // Resource_group_mgr
#include "sql/resourcegroups/thread_resource_control.h" // Thread_resource_control
#include "sql/sql_backup_lock.h" // acquire_shared_backup_lock
#include "sql/sql_class.h" // THD
#include "sql/sql_error.h"
#include "sql/sql_lex.h" // is_invalid_string
#include "sql/system_variables.h"
#include "sql/thd_raii.h"
#include "string_with_len.h"
namespace dd {
class Resource_group;
} // namespace dd
namespace {
/**
Acquire an exclusive MDL lock on resource group name.
@param thd Pointer to THD context.
@param res_grp_name Resource group name.
@return true if lock acquisition failed else false.
*/
static bool acquire_exclusive_mdl_for_resource_group(THD *thd,
const char *res_grp_name) {
DBUG_TRACE;
MDL_key mdl_key;
dd::Resource_group::create_mdl_key(res_grp_name, &mdl_key);
MDL_request mdl_request;
MDL_REQUEST_INIT_BY_KEY(&mdl_request, &mdl_key, MDL_EXCLUSIVE,
MDL_TRANSACTION);
if (thd->mdl_context.acquire_lock(&mdl_request,
thd->variables.lock_wait_timeout))
return true;
return false;
}
/**
Validate CPU id ranges provided by a user in the statements
CREATE RESOURCE GROUP, ALTER RESOURCE GROUP
@param[out] vcpu_range_vector vector of validated resourcegroups::Range
objects.
@param cpu_list Array of resourcegroups::Range objects
representing CPU identifiers or ranges of
CPU identifiers specified in the statements
CREATE RESOURCE GROUP, ALTER RESOURCE GROUP.
@param num_vcpus Number of VCPUS.
*/
bool validate_vcpu_range_vector(
std::vector<resourcegroups::Range> *vcpu_range_vector,
const Mem_root_array<resourcegroups::Range> *cpu_list, uint32_t num_vcpus) {
DBUG_TRACE;
for (auto vcpu_range : *cpu_list) {
if (vcpu_range.m_start > vcpu_range.m_end) {
my_error(ER_INVALID_VCPU_RANGE, MYF(0), vcpu_range.m_start,
vcpu_range.m_end);
return true;
}
if (vcpu_range.m_start >= num_vcpus || vcpu_range.m_end >= num_vcpus) {
my_error(ER_INVALID_VCPU_ID, MYF(0),
vcpu_range.m_start >= num_vcpus ? vcpu_range.m_start
: vcpu_range.m_end);
return true;
}
vcpu_range_vector->emplace_back(
resourcegroups::Range(vcpu_range.m_start, vcpu_range.m_end));
}
return false;
}
/**
This class represents a functional call to move a thread specified by
pfs_thread_id to a resource group specified in class' constructor.
*/
class Move_thread_to_default_group {
public:
explicit Move_thread_to_default_group(
resourcegroups::Resource_group *resource_group)
: m_resource_group(resource_group) {}
void operator()(ulonglong pfs_thread_id) {
auto res_grp_mgr = resourcegroups::Resource_group_mgr::instance();
auto applied_res_grp =
m_resource_group->type() == resourcegroups::Type::SYSTEM_RESOURCE_GROUP
? res_grp_mgr->sys_default_resource_group()
: res_grp_mgr->usr_default_resource_group();
PSI_thread_attrs pfs_thread_attr;
memset(&pfs_thread_attr, 0, sizeof(pfs_thread_attr));
if (!res_grp_mgr->get_thread_attributes(&pfs_thread_attr, pfs_thread_id)) {
applied_res_grp->controller()->apply_control(
pfs_thread_attr.m_thread_os_id);
res_grp_mgr->set_res_grp_in_pfs(applied_res_grp->name().c_str(),
applied_res_grp->name().length(),
pfs_thread_id);
if (!pfs_thread_attr.m_system_thread) {
Find_thd_with_id find_thd_with_id(pfs_thread_attr.m_processlist_id);
THD_ptr thd_ptr =
Global_THD_manager::get_instance()->find_thd(&find_thd_with_id);
if (thd_ptr)
thd_ptr->resource_group_ctx()->m_cur_resource_group = nullptr;
}
}
}
private:
resourcegroups::Resource_group *m_resource_group;
};
/**
Check if given resource group name is a default resource group.
*/
inline bool is_default_resource_group(const char *res_grp_name) {
return my_strcasecmp(system_charset_info,
resourcegroups::USR_DEFAULT_RESOURCE_GROUP_NAME,
res_grp_name) == 0 ||
my_strcasecmp(system_charset_info,
resourcegroups::SYS_DEFAULT_RESOURCE_GROUP_NAME,
res_grp_name) == 0;
}
} // Anonymous namespace
bool resourcegroups::Sql_cmd_create_resource_group::execute(THD *thd) {
DBUG_TRACE;
if (check_readonly(thd, true)) return true;
Security_context *sctx = thd->security_context();
if (!sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN")).first) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "RESOURCE_GROUP_ADMIN");
return true;
}
// Resource group name validation.
if (is_invalid_string(m_name, system_charset_info)) return true;
// VCPU IDs list validation.
uint32_t num_vcpus =
resourcegroups::Resource_group_mgr::instance()->num_vcpus();
DBUG_PRINT("info", ("Number of VCPUS: %u", num_vcpus));
auto vcpu_range_vector = std::unique_ptr<std::vector<Range>>(
new (std::nothrow) std::vector<Range>);
if (vcpu_range_vector == nullptr) {
my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
return true;
}
if (validate_vcpu_range_vector(vcpu_range_vector.get(), m_cpu_list,
num_vcpus))
return true;
if (acquire_shared_global_read_lock(thd, thd->variables.lock_wait_timeout) ||
acquire_shared_backup_lock(thd, thd->variables.lock_wait_timeout))
return true;
// Acquire exclusive lock on the resource group name.
if (acquire_exclusive_mdl_for_resource_group(thd, m_name.str)) return true;
auto res_grp_mgr = Resource_group_mgr::instance();
// Check whether resource group exists in-memory.
if (res_grp_mgr->get_resource_group(m_name.str) != nullptr) {
my_error(ER_RESOURCE_GROUP_EXISTS, MYF(0), m_name.str);
return true;
}
bool resource_group_exists;
// Check the disk also for existence of resource group.
if (dd::resource_group_exists(thd->dd_client(), dd::String_type(m_name.str),
&resource_group_exists))
return true;
if (resource_group_exists) {
my_error(ER_RESOURCE_GROUP_EXISTS, MYF(0), m_name.str);
return true;
}
auto resource_group_ptr = res_grp_mgr->create_and_add_in_resource_group_hash(
m_name, m_type, m_enabled, std::move(vcpu_range_vector), m_priority);
if (resource_group_ptr == nullptr) return true;
Disable_autocommit_guard autocommit_guard(thd);
if (dd::create_resource_group(thd, *resource_group_ptr)) {
Resource_group_mgr::instance()->remove_resource_group(
std::string(m_name.str));
return true;
}
my_ok(thd);
return false;
}
/**
Check if a resource group specified by a name is present in memory.
Load a resource group from the Data Dictionary if it missed in memory.
@param thd THD context.
@param resource_group_name Resource group name.
@return nullptr in case of error, else return a pointer to an instance of
class resourcegroups::Resource_group.
@note in case nullptr is returned an error is set in Diagnostics_area.
*/
static inline resourcegroups::Resource_group *check_and_load_resource_group(
THD *thd, const LEX_CSTRING &resource_group_name) {
resourcegroups::Resource_group *resource_group =
resourcegroups::Resource_group_mgr::instance()->get_resource_group(
resource_group_name.str);
if (resource_group == nullptr) {
// Check if resource group exists on-disk.
bool exists;
if (dd::resource_group_exists(thd->dd_client(),
dd::String_type(resource_group_name.str),
&exists))
// Error is reported by the dictionary subsystem.
return nullptr;
if (!exists) {
my_error(ER_RESOURCE_GROUP_NOT_EXISTS, MYF(0), resource_group_name.str);
return nullptr;
}
const dd::Resource_group *dd_resource_group = nullptr;
dd::cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
if (thd->dd_client()->acquire(dd::String_type(resource_group_name.str),
&dd_resource_group))
// Error is reported by the dictionary subsystem.
return nullptr;
resource_group = resourcegroups::Resource_group_mgr::instance()
->deserialize_resource_group(dd_resource_group);
if (resource_group == nullptr) {
my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
return nullptr;
}
}
return resource_group;
}
bool resourcegroups::Sql_cmd_alter_resource_group::execute(THD *thd) {
DBUG_TRACE;
if (check_readonly(thd, true)) return true;
Security_context *sctx = thd->security_context();
if (!sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN")).first) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "RESOURCE_GROUP_ADMIN");
return true;
}
// Resource group name validation.
if (is_invalid_string(m_name, system_charset_info)) return true;
// Disallow altering USR_default & SYS_default resource group.
if (is_default_resource_group(m_name.str)) {
my_error(ER_DISALLOWED_OPERATION, MYF(0), "Alter",
"default resource groups.");
return true;
}
if (acquire_shared_global_read_lock(thd, thd->variables.lock_wait_timeout) ||
acquire_shared_backup_lock(thd, thd->variables.lock_wait_timeout))
return true;
// Acquire exclusive lock on the resource group name.
if (acquire_exclusive_mdl_for_resource_group(thd, m_name.str)) return true;
auto resource_group = check_and_load_resource_group(thd, m_name);
if (resource_group == nullptr) return true;
// VCPU IDs list validation.
uint32_t num_vcpus = Resource_group_mgr::instance()->num_vcpus();
DBUG_PRINT("info", ("Number of VCPUS: %u", num_vcpus));
auto vcpu_range_vector = std::unique_ptr<std::vector<Range>>(
new (std::nothrow) std::vector<Range>);
if (vcpu_range_vector == nullptr) {
my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
return true;
}
if (validate_vcpu_range_vector(vcpu_range_vector.get(), m_cpu_list,
num_vcpus))
return true;
if (validate_resource_group_priority(thd, &m_priority, m_name,
resource_group->type()))
return true;
// FORCE option is not paired with DISABLE option.
if (m_force && (!m_use_enable || m_enable)) {
my_error(ER_INVALID_USE_OF_FORCE_OPTION, MYF(0));
return true;
}
Thread_resource_control *thr_res_ctrl = resource_group->controller();
bool thr_res_ctrl_change = false;
if (m_priority != thr_res_ctrl->priority()) {
thr_res_ctrl->set_priority(m_priority);
thr_res_ctrl_change = true;
}
if (!vcpu_range_vector->empty()) {
thr_res_ctrl->set_vcpu_vector(*vcpu_range_vector);
thr_res_ctrl_change = true;
}
if (m_use_enable && m_enable != resource_group->enabled()) {
resource_group->set_enabled(m_enable);
thr_res_ctrl_change = m_enable;
} else
thr_res_ctrl_change = resource_group->enabled();
// Update on-disk resource group.
Disable_autocommit_guard autocommit_guard(thd);
dd::String_type name(m_name.str);
if (update_resource_group(thd, name, *resource_group)) return true;
/*
Reapply controls on threads if there was some change in
the thread resource controls and the resource group is enabled.
*/
if (thr_res_ctrl_change) {
resource_group->apply_control_func(
[resource_group](ulonglong pfs_thread_id) {
auto res_grp_mgr_ptr = Resource_group_mgr::instance();
PSI_thread_attrs pfs_thread_attr;
memset(&pfs_thread_attr, 0, sizeof(pfs_thread_attr));
if (!res_grp_mgr_ptr->get_thread_attributes(&pfs_thread_attr,
pfs_thread_id))
resource_group->controller()->apply_control(
pfs_thread_attr.m_thread_os_id);
});
}
/*
If some threads are bound with resource group, then
(i) If FORCE option is specified, move the threads bound with this
resource group to respective default resource groups.
(ii) If FORCE option is not specified, the resource group is just
disabled.
*/
if (resource_group->is_bound_to_threads()) {
if (m_force) {
// Move all threads associated with this to default resource groups.
resource_group->apply_control_func(
Move_thread_to_default_group(resource_group));
resource_group->clear();
}
}
my_ok(thd);
return false;
}
bool resourcegroups::Sql_cmd_drop_resource_group::execute(THD *thd) {
DBUG_TRACE;
if (check_readonly(thd, true)) return true;
Security_context *sctx = thd->security_context();
if (!sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN")).first) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "RESOURCE_GROUP_ADMIN");
return true;
}
// Resource group name validation.
if (is_invalid_string(m_name, system_charset_info)) return true;
// Disallow dropping USR_default & SYS_default resource group.
if (is_default_resource_group(m_name.str)) {
my_error(ER_DISALLOWED_OPERATION, MYF(0), "Drop operation ",
"default resource groups.");
return true;
}
if (acquire_shared_global_read_lock(thd, thd->variables.lock_wait_timeout) ||
acquire_shared_backup_lock(thd, thd->variables.lock_wait_timeout))
return true;
// Acquire exclusive lock on the resource group name.
if (acquire_exclusive_mdl_for_resource_group(thd, m_name.str)) return true;
auto resource_group = check_and_load_resource_group(thd, m_name);
if (resource_group == nullptr) return true;
if (resource_group->is_bound_to_threads()) {
if (m_force) // move all threads to the default resource group.
{
resource_group->apply_control_func(
Move_thread_to_default_group(resource_group));
resource_group->clear();
} else {
my_error(ER_RESOURCE_GROUP_BUSY, MYF(0), m_name.str);
return true;
}
}
// Remove from on-disk resource group.
Disable_autocommit_guard autocommit_guard(thd);
if (dd::drop_resource_group(thd, m_name.str)) return true;
// Remove from in-memory hash the resource group.
if (resource_group != nullptr)
Resource_group_mgr::instance()->remove_resource_group(m_name.str);
my_ok(thd);
return false;
}
/**
Check if resource group controls can be applied to thread
identified by PFS thread id. Apply the controls to thread
if the checks were successful.
@param thd THD context
@param thread_id Thread id of the thread.
@param resource_group Pointer to resource group.
@param error Log error so that error is returned to client.
@returns true if the function fails else false.
*/
static inline bool check_and_apply_resource_grp(
THD *thd, ulonglong thread_id,
resourcegroups::Resource_group *resource_group, bool error) {
PSI_thread_attrs pfs_thread_attr;
memset(&pfs_thread_attr, 0, sizeof(pfs_thread_attr));
auto res_grp_mgr = resourcegroups::Resource_group_mgr::instance();
if (res_grp_mgr->get_thread_attributes(&pfs_thread_attr, thread_id) ||
thread_id != pfs_thread_attr.m_thread_internal_id) {
if (error)
my_error(ER_INVALID_THREAD_ID, MYF(0), thread_id);
else
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_INVALID_THREAD_ID,
ER_THD(current_thd, ER_INVALID_THREAD_ID), thread_id);
return true;
}
bool res_grp_match = pfs_thread_attr.m_system_thread
? (resource_group->type() ==
resourcegroups::Type::SYSTEM_RESOURCE_GROUP)
: (resource_group->type() ==
resourcegroups::Type::USER_RESOURCE_GROUP);
if (!res_grp_match) {
if (error)
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), thread_id,
"Resource group type and thread type doesn't match.");
else
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_RESOURCE_GROUP_BIND_FAILED,
ER_THD(thd, ER_RESOURCE_GROUP_BIND_FAILED),
resource_group->name().c_str(), thread_id,
"Resource group type & thread type doesn't match");
return true;
}
if (!res_grp_mgr->thread_priority_available() &&
resource_group->controller()->priority() != 0)
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_ATTRIBUTE_IGNORED,
ER_THD(current_thd, ER_ATTRIBUTE_IGNORED),
"thread_priority", "using default value");
// Check if resource group is already bound to this thread.
if (resource_group->is_pfs_thread_id_exists(thread_id)) return false;
MDL_ticket *ticket = nullptr;
if (res_grp_mgr->acquire_shared_mdl_for_resource_group(
thd, pfs_thread_attr.m_groupname, MDL_EXPLICIT, &ticket, true)) {
if (error)
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), thread_id,
"Unable to acquire MDL lock.");
else {
thd->clear_error();
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_RESOURCE_GROUP_BIND_FAILED,
ER_THD(current_thd, ER_RESOURCE_GROUP_BIND_FAILED),
resource_group->name().c_str(), thread_id,
"Unable to acquire MDL lock.");
}
return true;
}
resourcegroups::Resource_group *prev_cur_res_grp =
resourcegroups::Resource_group_mgr::instance()->get_resource_group(
std::string(pfs_thread_attr.m_groupname));
if (resource_group->controller()->apply_control(
pfs_thread_attr.m_thread_os_id)) {
if (error)
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), thread_id,
"Failed to apply thread controls.");
else
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_RESOURCE_GROUP_BIND_FAILED,
ER_THD(current_thd, ER_RESOURCE_GROUP_BIND_FAILED),
resource_group->name().c_str(), thread_id,
"Failed to apply thread controls.");
res_grp_mgr->release_shared_mdl_for_resource_group(thd, ticket);
return true;
}
// Set resource group context for non-system threads.
if (!pfs_thread_attr.m_system_thread) {
Find_thd_with_id find_thd_with_id(pfs_thread_attr.m_processlist_id);
THD_ptr cur_thd_ptr =
Global_THD_manager::get_instance()->find_thd(&find_thd_with_id);
if (cur_thd_ptr)
cur_thd_ptr->resource_group_ctx()->m_cur_resource_group = resource_group;
}
if (prev_cur_res_grp != nullptr)
prev_cur_res_grp->remove_pfs_thread_id(thread_id);
resource_group->add_pfs_thread_id(thread_id);
res_grp_mgr->set_res_grp_in_pfs(resource_group->name().c_str(),
resource_group->name().length(), thread_id);
res_grp_mgr->release_shared_mdl_for_resource_group(thd, ticket);
return false;
}
/**
Check if user has sufficient privilege to exercise SET RESOURCE GROUP.
@param sctx Pointer to security context.
@return true if sufficient privilege exists for SET RESOURCE GROUP else false.
*/
static bool check_resource_group_set_privilege(Security_context *sctx) {
if (!(sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN")).first ||
sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_USER")).first)) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
"RESOURCE_GROUP_ADMIN OR RESOURCE_GROUP_USER");
return true;
}
return false;
}
bool resourcegroups::Sql_cmd_set_resource_group::execute(THD *thd) {
DBUG_TRACE;
Security_context *sctx = thd->security_context();
if (check_resource_group_set_privilege(sctx)) return true;
// Acquire exclusive lock on the resource group name to synchronize with hint.
if (acquire_exclusive_mdl_for_resource_group(thd, m_name.str)) return true;
auto resource_group = check_and_load_resource_group(thd, m_name);
if (resource_group == nullptr) return true;
if ((resource_group->type() == Type::SYSTEM_RESOURCE_GROUP) &&
!sctx->has_global_grant(STRING_WITH_LEN("RESOURCE_GROUP_ADMIN")).first) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "RESOURCE_GROUP_ADMIN");
return true;
}
if (!resource_group->enabled()) {
my_error(ER_RESOURCE_GROUP_DISABLED, MYF(0),
resource_group->name().c_str());
return true;
}
if (m_thread_id_list == nullptr || m_thread_id_list->empty()) {
ulonglong pfs_thread_id = 0;
#ifdef HAVE_PSI_THREAD_INTERFACE
pfs_thread_id = PSI_THREAD_CALL(get_current_thread_internal_id)();
#endif
if (resource_group->type() != Type::USER_RESOURCE_GROUP) {
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), pfs_thread_id,
"System resource group can't be applied to user thread.");
return true;
}
auto res_grp_mgr = resourcegroups::Resource_group_mgr::instance();
const char *resource_group_name = nullptr;
mysql_mutex_lock(&thd->LOCK_thd_data);
auto cur_res_grp = thd->resource_group_ctx()->m_cur_resource_group;
if (cur_res_grp != nullptr)
resource_group_name = cur_res_grp->name().c_str();
mysql_mutex_unlock(&thd->LOCK_thd_data);
MDL_ticket *ticket = nullptr;
if (resource_group_name != nullptr &&
res_grp_mgr->acquire_shared_mdl_for_resource_group(
thd, resource_group_name, MDL_EXPLICIT, &ticket, true)) {
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), pfs_thread_id,
"Unable to acquire MDL lock.");
return true;
}
if (resource_group->controller()->apply_control()) {
my_error(ER_RESOURCE_GROUP_BIND_FAILED, MYF(0),
resource_group->name().c_str(), pfs_thread_id,
"Failed to apply thread resource controls");
return true;
}
mysql_mutex_lock(&thd->LOCK_thd_data);
cur_res_grp = thd->resource_group_ctx()->m_cur_resource_group;
thd->resource_group_ctx()->m_cur_resource_group = resource_group;
mysql_mutex_unlock(&thd->LOCK_thd_data);
if (cur_res_grp != nullptr)
cur_res_grp->remove_pfs_thread_id(pfs_thread_id);
resourcegroups::Resource_group_mgr::instance()->set_res_grp_in_pfs(
resource_group->name().c_str(), resource_group->name().length(),
pfs_thread_id);
resource_group->add_pfs_thread_id(pfs_thread_id);
if (ticket != nullptr)
res_grp_mgr->release_shared_mdl_for_resource_group(thd, ticket);
} else {
if (m_thread_id_list->size() == 1 &&
check_and_apply_resource_grp(thd, m_thread_id_list->at(0),
resource_group, true))
return true;
else
for (const auto &thread_id : *m_thread_id_list)
(void)check_and_apply_resource_grp(thd, thread_id, resource_group,
false);
}
my_ok(thd);
return false;
}
bool resourcegroups::Sql_cmd_set_resource_group::prepare(THD *thd) {
DBUG_TRACE;
if (Sql_cmd::prepare(thd)) return true;
bool rc = check_resource_group_set_privilege(thd->security_context());
return rc;
}