1 /* Copyright (c) 2015, 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 #include "locking_service.h"
24
25 #include "mysqld.h" // current_thd
26 #include "mdl.h" // MDL_request_list
27 #include "sql_class.h" // THD
28
29 /**
30 We want to convert ER_LOCK_DEADLOCK error to ER_LOCK_SERVICE_DEADLOCK error.
31 The former means that implicit rollback of transaction has occurred
32 which doesn't (and should not) happen when we get deadlock while waiting
33 for locking service locks.
34
35 We also want to convert ER_LOCK_WAIT_TIMEOUT as this error indicates that
36 it helps to restart the transaction. This does not apply to locking service
37 locks.
38 */
39
40 class Locking_service_deadlock_error_handler: public Internal_error_handler
41 {
42 public:
handle_condition(THD * thd,uint sql_errno,const char * sqlstate,Sql_condition::enum_severity_level * level,const char * msg)43 virtual bool handle_condition(THD *thd,
44 uint sql_errno,
45 const char *sqlstate,
46 Sql_condition::enum_severity_level *level,
47 const char *msg)
48 {
49 if (sql_errno == ER_LOCK_DEADLOCK)
50 {
51 my_error(ER_LOCKING_SERVICE_DEADLOCK, MYF(0));
52 return true;
53 }
54 else if (sql_errno == ER_LOCK_WAIT_TIMEOUT)
55 {
56 my_error(ER_LOCKING_SERVICE_TIMEOUT, MYF(0));
57 return true;
58 }
59
60 return false;
61 }
62 };
63
64
65 static const size_t MAX_LOCKING_SERVICE_LOCK_NAME_LENGTH= 64;
66
67 /**
68 Check if the given name has valid length.
69
70 @param name Name to check (namespace or lock)
71
72 @retval true if invalid, false otherwise.
73 */
74
check_lock_name(const char * name)75 static inline bool check_lock_name(const char *name)
76 {
77 if (!name || strlen(name) == 0 ||
78 strlen(name) > MAX_LOCKING_SERVICE_LOCK_NAME_LENGTH)
79 {
80 my_error(ER_LOCKING_SERVICE_WRONG_NAME, MYF(0), name);
81 return true;
82 }
83 return false;
84 }
85
86
87 class Release_all_locking_service_locks : public MDL_release_locks_visitor
88 {
89 public:
Release_all_locking_service_locks()90 Release_all_locking_service_locks() { }
91
release(MDL_ticket * ticket)92 virtual bool release(MDL_ticket *ticket)
93 {
94 return ticket->get_key()->mdl_namespace() == MDL_key::LOCKING_SERVICE;
95 }
96 };
97
98
99 class Release_locking_service_locks : public MDL_release_locks_visitor
100 {
101 private:
102 const char *m_lock_namespace;
103
104 public:
Release_locking_service_locks(const char * lock_namespace)105 explicit Release_locking_service_locks(const char *lock_namespace)
106 : m_lock_namespace(lock_namespace) { }
107
release(MDL_ticket * ticket)108 virtual bool release(MDL_ticket *ticket)
109 {
110 return (ticket->get_key()->mdl_namespace() == MDL_key::LOCKING_SERVICE &&
111 strcmp(m_lock_namespace, ticket->get_key()->db_name()) == 0);
112 }
113 };
114
115
acquire_locking_service_locks(MYSQL_THD opaque_thd,const char * lock_namespace,const char ** lock_names,size_t lock_num,enum_locking_service_lock_type lock_type,ulong lock_timeout)116 int acquire_locking_service_locks(MYSQL_THD opaque_thd,
117 const char *lock_namespace,
118 const char **lock_names,
119 size_t lock_num,
120 enum_locking_service_lock_type lock_type,
121 ulong lock_timeout)
122 {
123 if (lock_num == 0)
124 return 0;
125
126 // Check that namespace length is acceptable
127 if (check_lock_name(lock_namespace))
128 return 1;
129
130 THD *thd;
131 if (opaque_thd)
132 thd= static_cast<THD*>(opaque_thd);
133 else
134 thd= current_thd;
135
136 // Initialize MDL_requests.
137 MDL_request_list mdl_requests;
138 for (size_t i= 0; i < lock_num; i++)
139 {
140 // Check that lock name length is acceptable
141 if (check_lock_name(lock_names[i]))
142 return 1;
143
144 MDL_request *new_request= new (thd->mem_root) MDL_request;
145 MDL_REQUEST_INIT(new_request, MDL_key::LOCKING_SERVICE,
146 lock_namespace, lock_names[i],
147 (lock_type == LOCKING_SERVICE_READ ?
148 MDL_SHARED : MDL_EXCLUSIVE),
149 MDL_EXPLICIT);
150 mdl_requests.push_front(new_request);
151 }
152
153 // Acquire locks
154 Locking_service_deadlock_error_handler handler;
155 thd->push_internal_handler(&handler);
156 bool res= thd->mdl_context.acquire_locks(&mdl_requests, lock_timeout);
157 thd->pop_internal_handler();
158 if (res)
159 return 1;
160
161 return 0;
162 }
163
164
release_locking_service_locks(MYSQL_THD opaque_thd,const char * lock_namespace)165 int release_locking_service_locks(MYSQL_THD opaque_thd,
166 const char *lock_namespace)
167 {
168 // Check that namespace length is acceptable
169 if (check_lock_name(lock_namespace))
170 return 1;
171
172 THD *thd;
173 if (opaque_thd)
174 thd= static_cast<THD*>(opaque_thd);
175 else
176 thd= current_thd;
177
178 Release_locking_service_locks lock_visitor(lock_namespace);
179 thd->mdl_context.release_locks(&lock_visitor);
180
181 return 0;
182 }
183
184
release_all_locking_service_locks(THD * thd)185 void release_all_locking_service_locks(THD *thd)
186 {
187 Release_all_locking_service_locks lock_visitor;
188 thd->mdl_context.release_locks(&lock_visitor);
189 }
190
191
192 /*
193 Wrapper functions for the plugin service API. The UDF implementation
194 cannot call these as we get name conficts with the macros defined
195 in service_locking.h as UDFs are built with MYSQL_DYNAMIC_PLUGIN
196 yet are not able to call service API functions.
197 */
198 C_MODE_START
mysql_acquire_locking_service_locks(MYSQL_THD opaque_thd,const char * lock_namespace,const char ** lock_names,size_t lock_num,enum_locking_service_lock_type lock_type,ulong lock_timeout)199 int mysql_acquire_locking_service_locks(MYSQL_THD opaque_thd,
200 const char *lock_namespace,
201 const char **lock_names,
202 size_t lock_num,
203 enum_locking_service_lock_type lock_type,
204 ulong lock_timeout)
205 {
206 return acquire_locking_service_locks(opaque_thd, lock_namespace, lock_names,
207 lock_num, lock_type, lock_timeout);
208 }
209
210
mysql_release_locking_service_locks(MYSQL_THD opaque_thd,const char * lock_namespace)211 int mysql_release_locking_service_locks(MYSQL_THD opaque_thd,
212 const char *lock_namespace)
213 {
214 return release_locking_service_locks(opaque_thd, lock_namespace);
215 }
216 C_MODE_END
217
218