MySQL  8.0.27
Source Code Documentation
sdi_utils.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2021, 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 DD__SDI_UTILS_INCLUDED
24 #define DD__SDI_UTILS_INCLUDED
25 
26 #include <assert.h>
27 
28 #include "sql/current_thd.h" // inline_current_thd
29 #include "sql/dd/string_type.h" // dd::String_type
30 #include "sql/error_handler.h" // Internal_error_handler
31 #include "sql/mdl.h" // MDL_request
32 #include "sql/sql_class.h" // THD
33 
34 #ifndef NDEBUG
35 #define ENTITY_FMT "(%s, %llu)"
36 #define ENTITY_VAL(obj) (obj).name().c_str(), (obj).id()
37 #endif /* !NDEBUG */
38 
39 /**
40  @file
41  @ingroup sdi
42 
43  Inline utility functions used in different
44  TUs. Declared inline in header to avoid any overhead as they are
45  only a syntactic convnience (macro replacement).
46 */
47 
48 namespace dd {
49 namespace sdi_utils {
50 
51 /**
52  In debug mode, check that a true argument is paired with
53  thd->is_error() or thd->killed being set. In optimized mode it turns into
54  a noop.
55  @param[in] ret return value to check
56  @return same as argument passed in
57  */
58 inline bool checked_return(bool ret) {
59 #ifndef NDEBUG
60  THD *cthd = current_thd;
61  assert(!ret || cthd->is_system_thread() || cthd->is_error() || cthd->killed);
62 #endif /*!NDEBUG*/
63  return ret;
64 }
65 
66 /**
67  Convenience function for obtaining MDL. Sets up the MDL_request
68  struct and populates it, before calling Mdl_context::acquire_lock.
69 
70  @param thd thread context
71  @param ns MDL key namespace
72  @param schema_name schema name
73  @param object_name object name
74  @param mt MDL type
75  @param md MDL duration
76  @return value from Mdl_context::acquire_lock
77  */
79  const String_type &schema_name,
80  const String_type &object_name,
84  MDL_REQUEST_INIT(&mdl_request, ns, schema_name.c_str(), object_name.c_str(),
85  mt, md);
88 }
89 
90 template <typename T>
91 const T &ptr_as_cref(const T *p) {
92  assert(p != nullptr);
93  return *p;
94 }
95 
96 /**
97  Class template which derives from Internal_error_handler and
98  overrides handle_condition with the CONDITION_HANDLER_CLOS template
99  parameter.
100  */
101 template <typename CONDITION_HANDLER_CLOS>
103  CONDITION_HANDLER_CLOS m_ch;
104  bool handle_condition(THD *, uint sql_errno, const char *sqlstate,
106  const char *msg) override {
107  return m_ch(sql_errno, sqlstate, level, msg);
108  }
109 
110  public:
111  // CONDITION_HANDLER_CLOS is *class* template argument, so there is no type
112  // deduction, and ch must refer to an R-value. So it is safe to move.
113  explicit Closure_error_handler(CONDITION_HANDLER_CLOS &&ch)
114  : m_ch(std::move(ch)) {}
115 };
116 
117 /**
118  Set up a custom error handler to use for errors from the execution
119  of a closure.
120 
121  @param thd thread context
122  @param chc closure which implements the
123  Internal_error_handler::handle_condition override
124  @param ac closure action for which error conditions should be handled.
125  @retval true if an error occurs
126  @retval false otherwise
127  */
128 template <typename CH_CLOS, typename ACTION_CLOS>
129 bool handle_errors(THD *thd, CH_CLOS &&chc, ACTION_CLOS &&ac) {
130  Closure_error_handler<CH_CLOS> eh{std::forward<CH_CLOS>(chc)};
131  thd->push_internal_handler(&eh);
132  bool r = ac();
133  thd->pop_internal_handler();
134  return r;
135 }
136 
137 template <typename P_TYPE, typename CLOS_TYPE>
138 std::unique_ptr<P_TYPE, CLOS_TYPE> make_guard(P_TYPE *p, CLOS_TYPE &&clos) {
139  return std::unique_ptr<P_TYPE, CLOS_TYPE>(p, std::forward<CLOS_TYPE>(clos));
140 }
141 
142 } // namespace sdi_utils
143 } // namespace dd
144 #endif // DD__SDI_UTILS_INCLUDED
This class represents the interface for internal error handlers.
Definition: error_handler.h:46
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:3357
A pending metadata lock request.
Definition: mdl.h:798
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:59
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
MDL_context mdl_context
Definition: sql_class.h:844
bool is_error() const
true if there is an error in the error stack.
Definition: sql_class.h:3007
bool is_system_thread() const
Definition: sql_class.h:2259
Internal_error_handler * pop_internal_handler()
Remove the error handler last pushed.
Definition: sql_class.cc:676
void push_internal_handler(Internal_error_handler *handler)
Add an internal error handler to the thread execution context.
Definition: sql_class.cc:655
struct System_variables variables
Definition: sql_class.h:1006
std::atomic< killed_state > killed
Definition: sql_class.h:2440
Class template which derives from Internal_error_handler and overrides handle_condition with the COND...
Definition: sdi_utils.h:102
Closure_error_handler(CONDITION_HANDLER_CLOS &&ch)
Definition: sdi_utils.h:113
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:104
CONDITION_HANDLER_CLOS m_ch
Definition: sdi_utils.h:103
const char * p
Definition: ctype-mb.cc:1236
thread_local THD * current_thd
Definition: current_thd.cc:25
#define MDL_REQUEST_INIT(R, P1, P2, P3, P4, P5)
Definition: mdl.h:901
enum_mdl_duration
Duration of metadata lock.
Definition: mdl.h:332
@ MDL_TRANSACTION
Locks with transaction duration are automatically released at the end of transaction.
Definition: mdl.h:342
enum_mdl_type
Type of metadata lock request.
Definition: mdl.h:195
@ MDL_EXCLUSIVE
Definition: mdl.h:325
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:129
const T & ptr_as_cref(const T *p)
Definition: sdi_utils.h:91
std::unique_ptr< P_TYPE, CLOS_TYPE > make_guard(P_TYPE *p, CLOS_TYPE &&clos)
Definition: sdi_utils.h:138
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:78
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:58
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:244
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:42
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:50
Definition: varlen_sort.h:183
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
enum_mdl_namespace
Object namespaces.
Definition: mdl.h:399
ulong lock_wait_timeout
Definition: system_variables.h:216
unsigned int uint
Definition: uca-dump.cc:29