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