MySQL 9.1.0
Source Code Documentation
sdi_utils.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD__SDI_UTILS_INCLUDED
25#define DD__SDI_UTILS_INCLUDED
26
27#include <assert.h>
28
29#include "sql/current_thd.h" // inline_current_thd
30#include "sql/dd/string_type.h" // dd::String_type
31#include "sql/error_handler.h" // Internal_error_handler
32#include "sql/mdl.h" // MDL_request
33#include "sql/sql_class.h" // THD
34
35#ifndef NDEBUG
36#define ENTITY_FMT "(%s, %llu)"
37#define ENTITY_VAL(obj) (obj).name().c_str(), (obj).id()
38#endif /* !NDEBUG */
39
40/**
41 @file
42 @ingroup sdi
43
44 Inline utility functions used in different
45 TUs. Declared inline in header to avoid any overhead as they are
46 only a syntactic convnience (macro replacement).
47*/
48
49namespace dd {
50namespace sdi_utils {
51
52/**
53 In debug mode, check that a true argument is paired with
54 thd->is_error() or thd->killed being set. In optimized mode it turns into
55 a noop.
56 @param[in] ret return value to check
57 @return same as argument passed in
58 */
59inline bool checked_return(bool ret) {
60#ifndef NDEBUG
61 THD *cthd = current_thd;
62 assert(!ret || cthd->is_system_thread() || cthd->is_error() || cthd->killed);
63#endif /*!NDEBUG*/
64 return ret;
65}
66
67/**
68 Convenience function for obtaining MDL. Sets up the MDL_request
69 struct and populates it, before calling Mdl_context::acquire_lock.
70
71 @param thd thread context
72 @param ns MDL key namespace
73 @param schema_name schema name
74 @param object_name object name
75 @param mt MDL type
76 @param md MDL duration
77 @return value from Mdl_context::acquire_lock
78 */
80 const String_type &schema_name,
81 const String_type &object_name,
85 MDL_REQUEST_INIT(&mdl_request, ns, schema_name.c_str(), object_name.c_str(),
86 mt, md);
88 &mdl_request, thd->variables.lock_wait_timeout));
89}
90
91template <typename T>
92const T &ptr_as_cref(const T *p) {
93 assert(p != nullptr);
94 return *p;
95}
96
97/**
98 Class template which derives from Internal_error_handler and
99 overrides handle_condition with the CONDITION_HANDLER_CLOS template
100 parameter.
101 */
102template <typename CONDITION_HANDLER_CLOS>
104 CONDITION_HANDLER_CLOS m_ch;
105 bool handle_condition(THD *, uint sql_errno, const char *sqlstate,
107 const char *msg) override {
108 return m_ch(sql_errno, sqlstate, level, msg);
109 }
110
111 public:
112 // CONDITION_HANDLER_CLOS is *class* template argument, so there is no type
113 // deduction, and ch must refer to an R-value. So it is safe to move.
114 explicit Closure_error_handler(CONDITION_HANDLER_CLOS &&ch)
115 : m_ch(std::move(ch)) {}
116};
117
118/**
119 Set up a custom error handler to use for errors from the execution
120 of a closure.
121
122 @param thd thread context
123 @param chc closure which implements the
124 Internal_error_handler::handle_condition override
125 @param ac closure action for which error conditions should be handled.
126 @retval true if an error occurs
127 @retval false otherwise
128 */
129template <typename CH_CLOS, typename ACTION_CLOS>
130bool handle_errors(THD *thd, CH_CLOS &&chc, ACTION_CLOS &&ac) {
131 Closure_error_handler<CH_CLOS> eh{std::forward<CH_CLOS>(chc)};
132 thd->push_internal_handler(&eh);
133 bool r = ac();
135 return r;
136}
137
138template <typename P_TYPE, typename CLOS_TYPE>
139std::unique_ptr<P_TYPE, CLOS_TYPE> make_guard(P_TYPE *p, CLOS_TYPE &&clos) {
140 return std::unique_ptr<P_TYPE, CLOS_TYPE>(p, std::forward<CLOS_TYPE>(clos));
141}
142
143} // namespace sdi_utils
144} // namespace dd
145#endif // DD__SDI_UTILS_INCLUDED
This class represents the interface for internal error handlers.
Definition: error_handler.h:47
bool acquire_lock(MDL_request *mdl_request, Timeout_type lock_wait_timeout)
Acquire one lock with waiting for conflicting locks to go away if needed.
Definition: mdl.cc:3416
A pending metadata lock request.
Definition: mdl.h:803
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:63
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
MDL_context mdl_context
Definition: sql_class.h:966
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3305
bool is_system_thread() const
Definition: sql_class.h:2564
Internal_error_handler * pop_internal_handler()
Remove the error handler last pushed.
Definition: sql_class.cc:977
void push_internal_handler(Internal_error_handler *handler)
Add an internal error handler to the thread execution context.
Definition: sql_class.cc:956
System_variables variables
Definition: sql_lexer_thd.h:64
std::atomic< killed_state > killed
Definition: sql_class.h:2745
Class template which derives from Internal_error_handler and overrides handle_condition with the COND...
Definition: sdi_utils.h:103
Closure_error_handler(CONDITION_HANDLER_CLOS &&ch)
Definition: sdi_utils.h:114
bool handle_condition(THD *, uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg) override
Handle a sql condition.
Definition: sdi_utils.h:105
CONDITION_HANDLER_CLOS m_ch
Definition: sdi_utils.h:104
const char * p
Definition: ctype-mb.cc:1225
thread_local THD * current_thd
Definition: current_thd.cc:26
#define MDL_REQUEST_INIT(R, P1, P2, P3, P4, P5)
Definition: mdl.h:907
enum_mdl_duration
Duration of metadata lock.
Definition: mdl.h:334
@ MDL_TRANSACTION
Locks with transaction duration are automatically released at the end of transaction.
Definition: mdl.h:344
bool handle_errors(THD *thd, CH_CLOS &&chc, ACTION_CLOS &&ac)
Set up a custom error handler to use for errors from the execution of a closure.
Definition: sdi_utils.h:130
const T & ptr_as_cref(const T *p)
Definition: sdi_utils.h:92
std::unique_ptr< P_TYPE, CLOS_TYPE > make_guard(P_TYPE *p, CLOS_TYPE &&clos)
Definition: sdi_utils.h:139
bool mdl_lock(THD *thd, MDL_key::enum_mdl_namespace ns, const String_type &schema_name, const String_type &object_name, enum_mdl_type mt=MDL_EXCLUSIVE, enum_mdl_duration md=MDL_TRANSACTION)
Convenience function for obtaining MDL.
Definition: sdi_utils.h:79
bool checked_return(bool ret)
In debug mode, check that a true argument is paired with thd->is_error() or thd->killed being set.
Definition: sdi_utils.h:59
MDL_request * mdl_request(const Import_target &t, MEM_ROOT *mem_root)
Creates an MDL_request for exclusive MDL on the table being imported.
Definition: sdi_api.cc:247
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
Definition: gcs_xcom_synode.h:64
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
enum_mdl_type
Type of metadata lock request.
Definition: sql_lexer_yacc_state.h:106
@ MDL_EXCLUSIVE
Definition: sql_lexer_yacc_state.h:236
enum_mdl_namespace
Object namespaces.
Definition: mdl.h:401