MySQL  8.0.18
Source Code Documentation
clone_handler.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
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
25 Clone 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 
38 class THD;
39 struct Mysql_clone;
40 struct MYSQL_SOCKET;
41 
42 /**
43  Number of PSI_statement_info instruments
44  for clone statements.
45 */
46 
47 #define CLONE_PSI_STATEMENT_COUNT 5
48 
49 /**
50  Clone plugin handler to convenient way to. Takes
51 */
53  public:
54  /** Constructor: Initialize plugin name */
55  Clone_handler(const char *plugin_name_arg) : m_plugin_handle(nullptr) {
56  m_plugin_name.assign(plugin_name_arg);
57  }
58 
59  /** Initialize plugin handle
60  @return error code */
61  int init();
62 
63  /** Clone handler interface for local clone.
64  @param[in] thd server thread handle
65  @param[in] data_dir cloned data directory
66  @return error code */
67  int clone_local(THD *thd, const char *data_dir);
68 
69  /** Clone handler interface for remote clone client.
70  @param[in] thd server thread handle
71  @param[in] remote_host remote host IP address
72  @param[in] remote_port remote server port
73  @param[in] remote_user remote user name
74  @param[in] remote_passwd remote user's password
75  @param[in] data_dir cloned data directory
76  @param[in] ssl_mode remote connection ssl mode
77  @return error code */
78  int clone_remote_client(THD *thd, const char *remote_host, uint remote_port,
79  const char *remote_user, const char *remote_passwd,
80  const char *data_dir, enum mysql_ssl_mode ssl_mode);
81 
82  /** Clone handler interface for remote clone server.
83  @param[in] thd server thread handle
84  @param[in] socket network socket to remote client
85  @return error code */
86  int clone_remote_server(THD *thd, MYSQL_SOCKET socket);
87 
88  /** @return true, if clone provisioning in progress. */
89  static bool is_provisioning() { return (s_provision_in_progress > 0); }
90 
91  /** @return true, if ordered commit should be forced. Currently
92  clone would force ordered commit at end while blocking XA operations */
93  static bool need_commit_order() { return (s_xa_block_op.load()); }
94 
95  /* Initialize XA counters and mutex. */
96  static void init_xa();
97 
98  /* Destroy XA mutex. */
99  static void uninit_xa();
100 
101  /* RAII guard for XA operation synchronization with clone. */
102  struct XA_Operation {
103  /** Constructor: mark XA operation begin.
104  @param[in] thd session thread */
105  explicit XA_Operation(THD *thd);
106 
107  /** Destructor: mark XA operation end. */
108  ~XA_Operation();
109 
110  /** Disable copy construction */
111  XA_Operation(XA_Operation const &) = delete;
112 
113  /** Disable assignment */
114  XA_Operation &operator=(XA_Operation const &) = delete;
115 
116  private:
117  /** Session thread holding the guard. */
119  };
120 
121  /* RAII guard for blocking and unblocking XA operations. */
122  struct XA_Block {
123  /** Constructor: Block all XA operations.
124  @param[in] thd session thread */
125  explicit XA_Block(THD *thd);
126 
127  /** Destructor: unblock XA operations. */
128  ~XA_Block();
129 
130  /** Disable copy construction */
131  XA_Block(XA_Block const &) = delete;
132 
133  /** Disable assignment */
134  XA_Block &operator=(XA_Block const &) = delete;
135 
136  /** @return true, if XA blocking is unsuccessful. */
137  bool failed() const;
138 
139  private:
140  /** If blocking is successful and there is no XA operations. */
141  bool m_success;
142  };
143 
144  private:
145  /** Block new active XA operations and wait for existing ones to complete.
146  @param[in] thd session thread */
147  static bool block_xa_operation(THD *thd);
148 
149  /** Unblock waiting XA operations. */
150  static void unblock_xa_operation();
151 
152  /** Increment XA operation count and wait if blocked by clone.
153  @param[in] thd session thread */
154  static void begin_xa_operation(THD *thd);
155 
156  /** Decrement XA operation count. */
157  static void end_xa_operation();
158 
159  /** Validate clone data directory and convert to os format
160  @param[in] in_dir user specified clone directory
161  @param[out] out_dir data directory in native os format
162  @return error code */
163  int validate_dir(const char *in_dir, char *out_dir);
164 
165  private:
166  /** Number of XA operations (prepare/commit/rollback) in progress. */
167  static std::atomic<int> s_xa_counter;
168 
169  /** Set when clone blocks XA operations. XA operations currently are
170  not ordered between binlog and SE and needs to be synchronized for clone. */
171  static std::atomic<bool> s_xa_block_op;
172 
173  /** True if clone provisioning in progress. */
174  static std::atomic<int> s_provision_in_progress;
175 
176  /** Mutex to synchronize blocking XA operation. */
178 
179  /** Clone plugin name */
180  std::string m_plugin_name;
181 
182  /** Clone plugin handle */
184 };
185 
186 /** Check if the clone plugin is installed and lock. If the plugin is ready,
187 return the handler to caller.
188 @param[in] thd server thread handle
189 @param[out] plugin plugin reference
190 @return clone handler on success otherwise NULL */
192 
193 /** Unlock the clone plugin.
194 @param[in] thd server thread handle
195 @param[out] plugin plugin reference */
197 
198 #endif /* CLONE_HANDLER_INCLUDED */
static bool is_provisioning()
Definition: clone_handler.h:89
Definition: sql_plugin_ref.h:44
static std::atomic< bool > s_xa_block_op
Set when clone blocks XA operations.
Definition: clone_handler.h:171
int clone_local(THD *thd, const char *data_dir)
Clone handler interface for local clone.
Definition: clone_handler.cc:55
XA_Block & operator=(XA_Block const &)=delete
Disable assignment.
Definition: clone_handler.h:102
XA_Block(THD *thd)
Constructor: Block all XA operations.
Definition: clone_handler.cc:330
static std::atomic< int > s_provision_in_progress
True if clone provisioning in progress.
Definition: clone_handler.h:174
static struct st_mysql_daemon plugin
Definition: test_services_host_application_signal.cc:130
THD * m_thd
Session thread holding the guard.
Definition: clone_handler.h:118
std::string m_plugin_name
Clone plugin name.
Definition: clone_handler.h:180
Clone plugin handler to convenient way to.
Definition: clone_handler.h:52
void clone_plugin_unlock(THD *thd, plugin_ref plugin)
Unlock the clone plugin.
Definition: clone_handler.cc:256
bool failed() const
Definition: clone_handler.cc:337
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient...
static void unblock_xa_operation()
Unblock waiting XA operations.
Definition: clone_handler.cc:380
The descriptor structure for the plugin, that is referred from st_mysql_plugin.
Definition: plugin_clone.h:42
XA_Operation(THD *thd)
Constructor: mark XA operation begin.
Definition: clone_handler.cc:275
XA_Operation & operator=(XA_Operation const &)=delete
Disable assignment.
mysql_ssl_mode
Definition: mysql.h:267
static void init_xa()
Definition: clone_handler.cc:266
int init()
Initialize plugin handle.
Definition: clone_handler.cc:113
~XA_Operation()
Destructor: mark XA operation end.
Definition: clone_handler.cc:282
static bool need_commit_order()
Definition: clone_handler.h:93
Definition: clone_handler.h:122
static void end_xa_operation()
Decrement XA operation count.
Definition: clone_handler.cc:328
unsigned int uint
Definition: uca-dump.cc:29
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:68
static mysql_mutex_t s_xa_mutex
Mutex to synchronize blocking XA operation.
Definition: clone_handler.h:177
Clone_handler * clone_plugin_lock(THD *thd, plugin_ref *plugin)
Check if the clone plugin is installed and lock.
Definition: clone_handler.cc:236
An instrumented socket.
Definition: mysql_socket_bits.h:34
~XA_Block()
Destructor: unblock XA operations.
Definition: clone_handler.cc:335
static bool block_xa_operation(THD *thd)
Block new active XA operations and wait for existing ones to complete.
Definition: clone_handler.cc:339
int validate_dir(const char *in_dir, char *out_dir)
Validate clone data directory and convert to os format.
Definition: clone_handler.cc:141
Clone_handler(const char *plugin_name_arg)
Constructor: Initialize plugin name.
Definition: clone_handler.h:55
static void begin_xa_operation(THD *thd)
Increment XA operation count and wait if blocked by clone.
Definition: clone_handler.cc:287
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
int clone_remote_server(THD *thd, MYSQL_SOCKET socket)
Clone handler interface for remote clone server.
Definition: clone_handler.cc:108
static std::atomic< int > s_xa_counter
Number of XA operations (prepare/commit/rollback) in progress.
Definition: clone_handler.h:167
Common #defines and includes for file and socket I/O.
bool m_success
If blocking is successful and there is no XA operations.
Definition: clone_handler.h:141
Mysql_clone * m_plugin_handle
Clone plugin handle.
Definition: clone_handler.h:183
static void uninit_xa()
Definition: clone_handler.cc:273
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778