MySQL 8.0.37
Source Code Documentation
udf_utils.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 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 PLUGIN_GR_INCLUDE_UDF_UTILS_H
25#define PLUGIN_GR_INCLUDE_UDF_UTILS_H
26
27#include <assert.h>
31
34
36 "Member must be ONLINE and in the majority partition.";
38 "All members in the group must be reachable.";
40 "A member is joining the group, wait for it to be ONLINE.";
41const char *const server_uuid_not_present_str =
42 "Wrong arguments: You need to specify a server uuid.";
43const char *const server_uuid_not_valid_str =
44 "Wrong arguments: The server uuid is not valid.";
46 "The requested uuid is not a member of the group.";
47
48/**
49 * Result data type for user_has_gr_admin_privilege.
50 * There are three cases:
51 *
52 * error: There was an error fetching the user's privileges
53 * ok: The user has the required privileges
54 * no_privilege: The user does *not* have the required privileges
55 *
56 * In the no_privilege case, the result contains the user's name and host for
57 * the caller to create an helpful error message.
58 */
61 public:
63 char const *get_user() const {
65 "get_user() can only be called if status == no_privilege");
66 return user;
67 }
68 char const *get_host() const {
70 "get_host() can only be called if status == no_privilege");
71 return host;
72 }
75 }
78 }
79 static privilege_result no_privilege(char const *user, char const *host) {
80 return privilege_result(user, host);
81 }
82
83 private:
84 char const *user;
85 char const *host;
89 "privilege_result(status) can only be called if status != "
90 "no_privilege");
91 }
92 privilege_result(char const *user, char const *host)
94};
95
96/**
97 @class UDF_counter
98 Class used to increase an atomic value when UDF functions are being
99 initialized. If initialization fails the value will be decreased.
100
101 number_udfs_running works together with plugin_is_stopping so when group
102 replication is stopping, all new udf will fail to start and server will
103 wait for the running ones to finish.
104*/
105
107 public:
108 static std::atomic<int> number_udfs_running;
109 static void terminated() { number_udfs_running--; }
110 static bool is_zero() { return number_udfs_running == 0; }
111
115 }
116
117 void succeeded() { success = true; }
118
119 private:
121};
122
123/**
124 * Checks whether the user has GROUP_REPLICATION_ADMIN privilege.
125 *
126 * @retval privilege_result::error if there was an error fetching the user's
127 * privileges
128 * @retval privilege_result::no_privilege if the user does not have the
129 * privilege
130 * @retval privilege_result::success if the user has the privilege
131 */
133
134/**
135 * Logs the privilege status of @c privilege into @c message.
136 *
137 * @param privilege the result of @c user_has_gr_admin_privilege()
138 * @param[out] message the buffer where the log message will be written
139 */
141 char *message);
142/**
143 * Checks that `super_read_only` is disabled on the server.
144 *
145 * @returns std::pair<bool, std::string> where each element has the
146 * following meaning:
147 * first element of the pair is the function error value:
148 * false Successful
149 * true Error
150 * second element of the pair is the error message.
151 */
152std::pair<bool, std::string> check_super_read_only_is_disabled();
153
154/**
155 * Checks whether the server is ONLINE and belongs to the majority partition.
156 *
157 * @retval true if the member is online and in the majority partition
158 * @retval false otherwise
159 */
161
162/**
163 * Checks if an unreachable member exists in the group
164 *
165 * @retval true if an unreachable member exists
166 * @retval false otherwise
167 */
169
170/**
171 * Checks if a member in recovery exists in the group
172 *
173 * @retval true if a recovering member exists
174 * @retval false otherwise
175 */
177
178/**
179 * Checks if the uuid is valid to use in a function
180 * It checks:
181 * 1. It is not empty
182 * 2. It is a valid uuid
183 * 3. It belongs to the group
184 *
185 * @param uuid the uuid string
186 * @param ulength the length of the uuid string
187 * @param[out] error_message the returned error message
188 *
189 * @retval true if uuid is not valid
190 * @retval false otherwise
191 */
192bool validate_uuid_parameter(std::string &uuid, size_t ulength,
193 const char **error_message);
194
195/**
196 * Throw a error on a UDF function with mysql_error_service_printf
197 *
198 * @param action_name the action name when the error occurred
199 * @param error_message the error message to print
200 * @param log_error should the error also go to the log (default = false)
201 *
202 * @retval true the function failed to use the mysql_runtime_error service to
203 * throw the error
204 * @retval false everything went OK
205 */
206bool throw_udf_error(const char *action_name, const char *error_message,
207 bool log_error = false);
208
209/**
210 * Logs the group action @c action_name result from @c result_area into
211 * @c result_message.
212 *
213 * @param result_area describes the log message level
214 * @param action_name group action name
215 * @param[out] result_message buffer where the log message will be written
216 * @param[out] length size of the log message written to @c result_message
217 *
218 * @retval true the group action failed and this function threw/logged the group
219 * action's error
220 * @retval false everything went OK
221 */
223 const char *action_name,
224 char *result_message,
225 unsigned long *length);
226
227/**
228 * Checks if tables are locked, and logs to @c message if so.
229 *
230 * @param[out] message buffer where the log message will be written to
231 * @retval true if tables are not locked
232 * @retval false if tables are locked (@c message is written to)
233 */
234bool check_locked_tables(char *message);
235
236/**
237 * Checks whether the group contains a member older than the specified version.
238 *
239 * @param min_required_version Minimum version required
240 * @returns true if there is some older member, false otherwise
241 */
243 Member_version const &min_required_version);
244/**
245 @class Charset_service
246
247 Class that acquire/release the udf_metadata_service from registry service.
248 It provides the APIs to set the character set of return value and arguments
249 of UDFs using the udf_metadata service.
250*/
252 public:
253 /**
254 Acquires the udf_metadata_service from the registry service.
255 @param[in] reg_srv Registry service from which udf_metadata service
256 will be acquired
257
258 @retval true if service could not be acquired
259 @retval false Otherwise
260 */
261 static bool init(SERVICE_TYPE(registry) * reg_srv);
262
263 /**
264 Release the udf_metadata service
265
266 @param[in] reg_srv Registry service from which the udf_metadata
267 service will be released.
268
269 @retval true if service could not be released
270 @retval false Otherwise
271 */
272 static bool deinit(SERVICE_TYPE(registry) * reg_srv);
273
274 /**
275 Set the specified character set of UDF return value
276
277 @param[in] initid UDF_INIT structure
278 @param[in] charset_name Character set that has to be set.
279 The default charset is set to 'latin1'
280
281 @retval true Could not set the character set of return value
282 @retval false Otherwise
283 */
284 static bool set_return_value_charset(
285 UDF_INIT *initid, const std::string &charset_name = "latin1");
286 /**
287 Set the specified character set of all UDF arguments
288
289 @param[in] args UDF_ARGS structure
290 @param[in] charset_name Character set that has to be set.
291 The default charset is set to 'latin1'
292
293 @retval true Could not set the character set of any of the argument
294 @retval false Otherwise
295 */
296 static bool set_args_charset(UDF_ARGS *args,
297 const std::string &charset_name = "latin1");
298
299 private:
300 /* Argument type to specify in the metadata service methods */
301 static const char *arg_type;
302 /* udf_metadata service name */
303 static const char *service_name;
304 /* Handle of udf_metadata_service */
305 static SERVICE_TYPE(mysql_udf_metadata) * udf_metadata_service;
306};
307
308#endif /* PLUGIN_GR_INCLUDE_UDF_UTILS_H */
#define log_error
Definition: log_client.h:111
Class that acquire/release the udf_metadata_service from registry service.
Definition: udf_utils.h:251
static bool init(const mysql_service_registry_t *reg_srv)
Acquires the udf_metadata_service from the registry service.
Definition: udf_utils.cc:280
static bool deinit(const mysql_service_registry_t *reg_srv)
Release the udf_metadata service.
Definition: udf_utils.cc:289
static bool set_args_charset(UDF_ARGS *args, const std::string &charset_name="latin1")
Set the specified character set of all UDF arguments.
Definition: udf_utils.cc:309
static const mysql_service_mysql_udf_metadata_t * udf_metadata_service
Definition: udf_utils.h:305
static bool set_return_value_charset(UDF_INIT *initid, const std::string &charset_name="latin1")
Set the specified character set of UDF return value.
Definition: udf_utils.cc:299
static const char * arg_type
Definition: udf_utils.h:301
static const char * service_name
Definition: udf_utils.h:303
The parent class for group wide operations.
Definition: group_action.h:35
represent the MySQL version of a Member within the Group Replication group.
Definition: member_version.h:35
Class used to increase an atomic value when UDF functions are being initialized.
Definition: udf_utils.h:106
bool success
Definition: udf_utils.h:120
static bool is_zero()
Definition: udf_utils.h:110
UDF_counter()
Definition: udf_utils.h:112
static void terminated()
Definition: udf_utils.h:109
void succeeded()
Definition: udf_utils.h:117
~UDF_counter()
Definition: udf_utils.h:113
static std::atomic< int > number_udfs_running
Definition: udf_utils.h:108
Definition: udf_utils.h:60
privilege_result(privilege_status status)
Definition: udf_utils.h:86
char const * get_host() const
Definition: udf_utils.h:68
privilege_result(char const *user, char const *host)
Definition: udf_utils.h:92
char const * user
Definition: udf_utils.h:84
static privilege_result error()
Definition: udf_utils.h:76
char const * host
Definition: udf_utils.h:85
char const * get_user() const
Definition: udf_utils.h:63
privilege_status status
Definition: udf_utils.h:62
static privilege_result success()
Definition: udf_utils.h:73
static privilege_result no_privilege(char const *user, char const *host)
Definition: udf_utils.h:79
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
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:76
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76
Definition: udf_registration_types.h:48
Information about the result of a user defined function.
Definition: udf_registration_types.h:66
static const mysql_service_registry_t * reg_srv
Initialize parameters required for error logging.
Definition: test_plugin.cc:62
const char *const server_uuid_not_on_group_str
Definition: udf_utils.h:45
const char *const server_uuid_not_present_str
Definition: udf_utils.h:41
bool check_locked_tables(char *message)
Checks if tables are locked, and logs to message if so.
Definition: udf_utils.cc:238
bool group_contains_recovering_member()
Checks if a member in recovery exists in the group.
Definition: udf_utils.cc:151
bool log_group_action_result_message(Group_action_diagnostics *result_area, const char *action_name, char *result_message, unsigned long *length)
Logs the group action action_name result from result_area into result_message.
Definition: udf_utils.cc:203
const char *const recovering_member_on_group_str
Definition: udf_utils.h:39
void log_privilege_status_result(privilege_result const &privilege, char *message)
Logs the privilege status of privilege into message.
Definition: udf_utils.cc:87
bool throw_udf_error(const char *action_name, const char *error_message, bool log_error=false)
Throw a error on a UDF function with mysql_error_service_printf.
Definition: udf_utils.cc:182
bool validate_uuid_parameter(std::string &uuid, size_t ulength, const char **error_message)
Checks if the uuid is valid to use in a function It checks:
Definition: udf_utils.cc:160
bool member_online_with_majority()
Checks whether the server is ONLINE and belongs to the majority partition.
Definition: udf_utils.cc:128
bool group_contains_unreachable_member()
Checks if an unreachable member exists in the group.
Definition: udf_utils.cc:142
std::pair< bool, std::string > check_super_read_only_is_disabled()
Checks that super_read_only is disabled on the server.
Definition: udf_utils.cc:108
privilege_status
Result data type for user_has_gr_admin_privilege.
Definition: udf_utils.h:59
privilege_result user_has_gr_admin_privilege()
Checks whether the user has GROUP_REPLICATION_ADMIN privilege.
Definition: udf_utils.cc:33
const char *const unreachable_member_on_group_str
Definition: udf_utils.h:37
const char *const member_offline_or_minority_str
Definition: udf_utils.h:35
const char *const server_uuid_not_valid_str
Definition: udf_utils.h:43
bool group_contains_member_older_than(Member_version const &min_required_version)
Checks whether the group contains a member older than the specified version.
Definition: udf_utils.cc:253