MySQL 8.0.30
Source Code Documentation
clone_handler.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2022, 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/**
24@file clone_handler.h
25Clone handler interface to access clone plugin
26*/
27
28#ifndef CLONE_HANDLER_INCLUDED
29#define CLONE_HANDLER_INCLUDED
30
31#include <atomic>
32#include <string>
33
34#include "my_io.h"
35#include "mysql.h"
36#include "sql/sql_plugin_ref.h" // plugin_ref
37
38class THD;
39class Srv_session;
40struct Mysql_clone;
41struct MYSQL_SOCKET;
42
43/**
44 Number of PSI_statement_info instruments
45 for clone statements.
46*/
47
48#define CLONE_PSI_STATEMENT_COUNT 5
49
50/**
51 Clone plugin handler to convenient way to. Takes
52*/
54 public:
55 /** Constructor: Initialize plugin name */
56 Clone_handler(const char *plugin_name_arg) : m_plugin_handle(nullptr) {
57 m_plugin_name.assign(plugin_name_arg);
58 }
59
60 /** Initialize plugin handle
61 @return error code */
62 int init();
63
64 /** Clone handler interface for local clone.
65 @param[in] thd server thread handle
66 @param[in] data_dir cloned data directory
67 @return error code */
68 int clone_local(THD *thd, const char *data_dir);
69
70 /** Clone handler interface for remote clone client.
71 @param[in] thd server thread handle
72 @param[in] remote_host remote host IP address
73 @param[in] remote_port remote server port
74 @param[in] remote_user remote user name
75 @param[in] remote_passwd remote user's password
76 @param[in] data_dir cloned data directory
77 @param[in] ssl_mode remote connection ssl mode
78 @return error code */
79 int clone_remote_client(THD *thd, const char *remote_host, uint remote_port,
80 const char *remote_user, const char *remote_passwd,
81 const char *data_dir, enum mysql_ssl_mode ssl_mode);
82
83 /** Clone handler interface for remote clone server.
84 @param[in] thd server thread handle
85 @param[in] socket network socket to remote client
86 @return error code */
88
89 /** Get donor error and message for ER_CLONE_DONOR error.
90 @param[in] session server session
91 @param[out] error donor error number
92 @param[out] message error message
93 @return true, iff successful. */
94 static bool get_donor_error(Srv_session *session, int &error,
95 const char *&message);
96
97 /** @return false only if no user data is dropped yet. */
98 static bool is_data_dropped() { return (s_is_data_dropped); }
99
100 /** Must set before dropping any user data. */
101 static void set_drop_data() { s_is_data_dropped.store(true); }
102
103 /** @return true, if clone provisioning in progress. */
104 static bool is_provisioning() { return (s_provision_in_progress > 0); }
105
106 /** @return true, if ordered commit should be forced. Currently
107 clone would force ordered commit at end while blocking XA operations */
108 static bool need_commit_order() { return (s_xa_block_op.load()); }
109
110 /* Initialize XA counters and mutex. */
111 static void init_xa();
112
113 /* Destroy XA mutex. */
114 static void uninit_xa();
115
116 /* RAII guard for XA operation synchronization with clone. */
118 /** Constructor: mark XA operation begin.
119 @param[in] thd session thread */
120 explicit XA_Operation(THD *thd);
121
122 /** Destructor: mark XA operation end. */
124
125 /** Disable copy construction */
126 XA_Operation(XA_Operation const &) = delete;
127
128 /** Disable assignment */
130
131 private:
132 /** Session thread holding the guard. */
134 };
135
136 /* RAII guard for blocking and unblocking XA operations. */
137 struct XA_Block {
138 /** Constructor: Block all XA operations.
139 @param[in] thd session thread */
140 explicit XA_Block(THD *thd);
141
142 /** Destructor: unblock XA operations. */
143 ~XA_Block();
144
145 /** Disable copy construction */
146 XA_Block(XA_Block const &) = delete;
147
148 /** Disable assignment */
149 XA_Block &operator=(XA_Block const &) = delete;
150
151 /** @return true, if XA blocking is unsuccessful. */
152 bool failed() const;
153
154 private:
155 /** If blocking is successful and there is no XA operations. */
157 };
158
159 private:
160 /** Block new active XA operations and wait for existing ones to complete.
161 @param[in] thd session thread */
162 static bool block_xa_operation(THD *thd);
163
164 /** Unblock waiting XA operations. */
165 static void unblock_xa_operation();
166
167 /** Increment XA operation count and wait if blocked by clone.
168 @param[in] thd session thread */
169 static void begin_xa_operation(THD *thd);
170
171 /** Decrement XA operation count. */
172 static void end_xa_operation();
173
174 /** Validate clone data directory and convert to os format
175 @param[in] in_dir user specified clone directory
176 @param[out] out_dir data directory in native os format
177 @return error code */
178 int validate_dir(const char *in_dir, char *out_dir);
179
180 private:
181 /** Number of XA operations (prepare/commit/rollback) in progress. */
182 static std::atomic<int> s_xa_counter;
183
184 /** Set when clone blocks XA operations. XA operations currently are
185 not ordered between binlog and SE and needs to be synchronized for clone. */
186 static std::atomic<bool> s_xa_block_op;
187
188 /** True if clone provisioning in progress. */
189 static std::atomic<int> s_provision_in_progress;
190
191 /** True, if any user data is dropped by clone. */
192 static std::atomic<bool> s_is_data_dropped;
193
194 /** Mutex to synchronize blocking XA operation. */
196
197 /** Clone plugin name */
198 std::string m_plugin_name;
199
200 /** Clone plugin handle */
202};
203
204/** Check if the clone plugin is installed and lock. If the plugin is ready,
205return the handler to caller.
206@param[in] thd server thread handle
207@param[out] plugin plugin reference
208@return clone handler on success otherwise NULL */
210
211/** Unlock the clone plugin.
212@param[in] thd server thread handle
213@param[out] plugin plugin reference */
214void clone_plugin_unlock(THD *thd, plugin_ref plugin);
215
216#endif /* CLONE_HANDLER_INCLUDED */
Clone plugin handler to convenient way to.
Definition: clone_handler.h:53
static void init_xa()
Definition: clone_handler.cc:354
static void uninit_xa()
Definition: clone_handler.cc:361
static bool get_donor_error(Srv_session *session, int &error, const char *&message)
Get donor error and message for ER_CLONE_DONOR error.
Definition: clone_handler.cc:62
static mysql_mutex_t s_xa_mutex
Mutex to synchronize blocking XA operation.
Definition: clone_handler.h:195
static std::atomic< int > s_xa_counter
Number of XA operations (prepare/commit/rollback) in progress.
Definition: clone_handler.h:182
static bool is_provisioning()
Definition: clone_handler.h:104
int clone_remote_client(THD *thd, const char *remote_host, uint remote_port, const char *remote_user, const char *remote_passwd, const char *data_dir, enum mysql_ssl_mode ssl_mode)
Clone handler interface for remote clone client.
Definition: clone_handler.cc:141
static bool is_data_dropped()
Definition: clone_handler.h:98
static void unblock_xa_operation()
Unblock waiting XA operations.
Definition: clone_handler.cc:468
std::string m_plugin_name
Clone plugin name.
Definition: clone_handler.h:198
static std::atomic< bool > s_xa_block_op
Set when clone blocks XA operations.
Definition: clone_handler.h:186
static std::atomic< bool > s_is_data_dropped
True, if any user data is dropped by clone.
Definition: clone_handler.h:192
Mysql_clone * m_plugin_handle
Clone plugin handle.
Definition: clone_handler.h:201
static bool block_xa_operation(THD *thd)
Block new active XA operations and wait for existing ones to complete.
Definition: clone_handler.cc:427
static void end_xa_operation()
Decrement XA operation count.
Definition: clone_handler.cc:416
int clone_local(THD *thd, const char *data_dir)
Clone handler interface for local clone.
Definition: clone_handler.cc:128
static bool need_commit_order()
Definition: clone_handler.h:108
int init()
Initialize plugin handle.
Definition: clone_handler.cc:186
int clone_remote_server(THD *thd, MYSQL_SOCKET socket)
Clone handler interface for remote clone server.
Definition: clone_handler.cc:181
static void begin_xa_operation(THD *thd)
Increment XA operation count and wait if blocked by clone.
Definition: clone_handler.cc:375
static void set_drop_data()
Must set before dropping any user data.
Definition: clone_handler.h:101
static std::atomic< int > s_provision_in_progress
True if clone provisioning in progress.
Definition: clone_handler.h:189
int validate_dir(const char *in_dir, char *out_dir)
Validate clone data directory and convert to os format.
Definition: clone_handler.cc:214
Clone_handler(const char *plugin_name_arg)
Constructor: Initialize plugin name.
Definition: clone_handler.h:56
Definition: srv_session.h:55
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
void clone_plugin_unlock(THD *thd, plugin_ref plugin)
Unlock the clone plugin.
Definition: clone_handler.cc:343
Clone_handler * clone_plugin_lock(THD *thd, plugin_ref *plugin)
Check if the clone plugin is installed and lock.
Definition: clone_handler.cc:323
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
Common #defines and includes for file and socket I/O.
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
mysql_ssl_mode
Definition: mysql.h:270
stdx::expected< native_handle_type, error_type > socket(int family, int sock_type, int protocol)
Definition: socket.h:56
Definition: clone_handler.h:137
bool m_success
If blocking is successful and there is no XA operations.
Definition: clone_handler.h:156
bool failed() const
Definition: clone_handler.cc:425
~XA_Block()
Destructor: unblock XA operations.
Definition: clone_handler.cc:423
XA_Block(THD *thd)
Constructor: Block all XA operations.
Definition: clone_handler.cc:418
XA_Block(XA_Block const &)=delete
Disable copy construction.
XA_Block & operator=(XA_Block const &)=delete
Disable assignment.
Definition: clone_handler.h:117
XA_Operation(THD *thd)
Constructor: mark XA operation begin.
Definition: clone_handler.cc:363
XA_Operation & operator=(XA_Operation const &)=delete
Disable assignment.
THD * m_thd
Session thread holding the guard.
Definition: clone_handler.h:133
XA_Operation(XA_Operation const &)=delete
Disable copy construction.
~XA_Operation()
Destructor: mark XA operation end.
Definition: clone_handler.cc:370
An instrumented socket.
Definition: mysql_socket_bits.h:34
The descriptor structure for the plugin, that is referred from st_mysql_plugin.
Definition: plugin_clone.h:42
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
Definition: sql_plugin_ref.h:44
unsigned int uint
Definition: uca-dump.cc:29