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