MySQL 8.0.39
Source Code Documentation
ha_federated.h
Go to the documentation of this file.
1/* Copyright (c) 2004, 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 Please read ha_exmple.cc before reading this file.
26 Please keep in mind that the federated storage engine implements all methods
27 that are required to be implemented. handler.h has a full list of methods
28 that you can implement.
29*/
30
31#include <mysql.h>
32#include <sys/types.h>
33
34#include "my_dbug.h"
35#include "my_inttypes.h"
36#include "prealloced_array.h"
37#include "sql/handler.h"
38#include "thr_lock.h"
39
40/*
41 handler::print_error has a case statement for error numbers.
42 This value is (10000) is far out of range and will envoke the
43 default: case.
44 (Current error range is 120-159 from include/my_base.h)
45*/
46#define HA_FEDERATED_ERROR_WITH_REMOTE_SYSTEM 10000
47
48#define FEDERATED_QUERY_BUFFER_SIZE STRING_BUFFER_USUAL_SIZE * 5
49#define FEDERATED_RECORDS_IN_RANGE 2
50#define FEDERATED_MAX_KEY_LENGTH 3500 // Same as innodb
51
52/*
53 FEDERATED_SHARE is a structure that will be shared among all open handlers
54 The example implements the minimum of what you will probably need.
55*/
58
59 bool parsed;
60 /* this key is unique db/tablename */
61 const char *share_key;
62 /*
63 the primary select query to be used in rnd_init
64 */
66 /*
67 remote host info, parse_url supplies
68 */
71 char *scheme;
73 char *hostname;
74 char *username;
75 char *password;
76 char *database;
78 char *table;
79 const char *socket;
80 char *sport;
82 ushort port;
83
88};
89
90/*
91 Class definition for the storage engine
92*/
93class ha_federated : public handler {
94 THR_LOCK_DATA lock; /* MySQL lock */
95 FEDERATED_SHARE *share; /* Shared lock info */
96 MYSQL *mysql; /* MySQL connection */
98 /**
99 Array of all stored results we get during a query execution.
100 */
103 MYSQL_ROW_OFFSET current_position; // Current position used by ::position()
109 /// Memory area for BLOB data.
111
112 private:
113 /*
114 return 0 on success
115 return errorcode otherwise
116 */
119 bool create_where_from_key(String *to, KEY *key_info,
120 const key_range *start_key,
121 const key_range *end_key, bool records_in_range,
122 bool eq_range);
123 int stash_remote_error();
124
126
129 uint key_len, ha_rkey_function find_flag,
130 MYSQL_RES **result);
131 int real_query(const char *query, size_t length);
132 int real_connect();
133
134 public:
135 ha_federated(handlerton *hton, TABLE_SHARE *table_arg);
136 ~ha_federated() override = default;
137 /* The name that will be used for display purposes */
138 const char *table_type() const override { return "FEDERATED"; }
139 /*
140 Next pointer used in transaction
141 */
143 /*
144 This is a list of flags that says what the storage engine
145 implements. The current table flags are documented in
146 handler.h
147 */
148 ulonglong table_flags() const override {
149 /* fix server to be able to get remote server table flags */
155 HA_NO_TRANSACTIONS /* until fixed by WL#2952 */ |
157 }
158 /*
159 This is a bitmap of flags that says how the storage engine
160 implements indexes. The current index flags are documented in
161 handler.h. If you do not implement indexes, just return zero
162 here.
163
164 part is the key part to check. First key part is 0
165 If all_parts it's set, MySQL want to know the flags for the combined
166 index up to and including 'part'.
167 */
168 /* fix server to be able to get remote server index flags */
169 ulong index_flags(uint, uint, bool) const override {
171 }
173 return HA_MAX_REC_LENGTH;
174 }
175 uint max_supported_keys() const override { return MAX_KEY; }
176 uint max_supported_key_parts() const override { return MAX_REF_PARTS; }
179 }
181 [[maybe_unused]]) const override {
183 }
184 /*
185 Called in test_quick_select to determine if indexes should be used.
186 Normally, we need to know number of blocks . For federated we need to
187 know number of blocks on remote side, and number of packets and blocks
188 on the network side (?)
189 Talk to Kostja about this - how to get the
190 number of rows * ...
191 disk scan time on other side (block size, size of the row) + network time
192 ... The reason for "records * 1000" is that such a large number forces this
193 to use indexes "
194 */
195 double scan_time() override {
196 DBUG_PRINT("info", ("records %lu", (ulong)stats.records));
197 return (double)(stats.records * 1000);
198 }
199 /*
200 The next method will never be called if you do not implement indexes.
201 */
202 double read_time(uint, uint, ha_rows rows) override {
203 /*
204 Per Brian, this number is bogus, but this method must be implemented,
205 and at a later date, he intends to document this issue for handler code
206 */
207 return (double)rows / 20.0 + 1;
208 }
209
210 /*
211 Everything below are methods that we implement in ha_federated.cc.
212
213 Most of these methods are not obligatory, skip them and
214 MySQL will treat them as not implemented
215 */
216 int open(const char *name, int mode, uint test_if_locked,
217 const dd::Table *table_def) override; // required
218 int close(void) override; // required
219
220 void start_bulk_insert(ha_rows rows) override;
221 int end_bulk_insert() override;
222 int write_row(uchar *buf) override;
223 int update_row(const uchar *old_data, uchar *new_data) override;
224 int delete_row(const uchar *buf) override;
225 int index_init(uint keynr, bool sorted) override;
227 int index_read_idx_map(uchar *buf, uint index, const uchar *key,
228 key_part_map keypart_map,
229 enum ha_rkey_function find_flag) override;
230 int index_read(uchar *buf, const uchar *key, uint key_len,
231 enum ha_rkey_function find_flag) override;
232 int index_read_idx(uchar *buf, uint idx, const uchar *key, uint key_len,
233 enum ha_rkey_function find_flag);
234 int index_next(uchar *buf) override;
235 int index_end() override;
236 int read_range_first(const key_range *start_key, const key_range *end_key,
237 bool eq_range, bool sorted) override;
238 int read_range_next() override;
239 /*
240 unlike index_init(), rnd_init() can be called two times
241 without rnd_end() in between (it only makes sense if scan=1).
242 then the second call should prepare for the new table scan
243 (e.g if rnd_init allocates the cursor, second call should
244 position it to the start of the table, no need to deallocate
245 and allocate it again
246 */
247 int rnd_init(bool scan) override; // required
248 int rnd_end() override;
249 int rnd_next(uchar *buf) override; // required
250 int rnd_next_int(uchar *buf);
251 int rnd_pos(uchar *buf, uchar *pos) override; // required
252 void position(const uchar *record) override; // required
253 int info(uint) override; // required
254 int extra(ha_extra_function operation) override;
255
256 void update_auto_increment(void);
257 int repair(THD *thd, HA_CHECK_OPT *check_opt) override;
258 int optimize(THD *thd, HA_CHECK_OPT *check_opt) override;
259
260 int delete_all_rows(void) override;
261 int truncate(dd::Table *table_def) override;
262 int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info,
263 dd::Table *table_def) override; // required
264 ha_rows records_in_range(uint inx, key_range *start_key,
265 key_range *end_key) override;
266
268 THD *thd, THR_LOCK_DATA **to,
269 enum thr_lock_type lock_type) override; // required
270 bool get_error_message(int error, String *buf) override;
271
273 void free_result();
274
275 int external_lock(THD *thd, int lock_type) override;
276 int connection_commit();
278 int connection_autocommit(bool state);
279 int execute_simple_query(const char *query, int len);
280 int reset(void) override;
281 int rnd_pos_by_record(uchar *record) override;
282};
app_data_ptr new_data(u_int n, char *val, cons_type consensus)
Definition: key.h:113
A typesafe replacement for DYNAMIC_ARRAY.
Definition: prealloced_array.h:71
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:47
Definition: ha_federated.h:93
int truncate(dd::Table *table_def) override
Quickly remove all rows from a table.
Definition: ha_federated.cc:2810
uint max_supported_key_length() const override
Definition: ha_federated.h:177
int index_read_idx_with_result_set(uchar *buf, uint index, const uchar *key, uint key_len, ha_rkey_function find_flag, MYSQL_RES **result)
Definition: ha_federated.cc:2258
ha_federated * trx_next
Definition: ha_federated.h:142
int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info, dd::Table *table_def) override
Create table (implementation).
Definition: ha_federated.cc:2902
int read_next(uchar *buf, MYSQL_RES *result)
Definition: ha_federated.cc:2505
int real_connect()
Definition: ha_federated.cc:2914
int index_next(uchar *buf) override
Definition: ha_federated.cc:2375
ha_federated(handlerton *hton, TABLE_SHARE *table_arg)
Definition: ha_federated.cc:883
int rnd_next(uchar *buf) override
Definition: ha_federated.cc:2464
MEM_ROOT m_blob_root
Memory area for BLOB data.
Definition: ha_federated.h:110
int index_read_idx(uchar *buf, uint idx, const uchar *key, uint key_len, enum ha_rkey_function find_flag)
Definition: ha_federated.cc:2235
int delete_row(const uchar *buf) override
Definition: ha_federated.cc:2124
int open(const char *name, int mode, uint test_if_locked, const dd::Table *table_def) override
Definition: ha_federated.cc:1563
FEDERATED_SHARE * share
Definition: ha_federated.h:95
MYSQL_RES * stored_result
Definition: ha_federated.h:97
int index_init(uint keynr, bool sorted) override
Definition: ha_federated.cc:2324
int connection_commit()
Definition: ha_federated.cc:3159
int index_read(uchar *buf, const uchar *key, uint key_len, enum ha_rkey_function find_flag) override
Definition: ha_federated.cc:2211
double scan_time() override
Definition: ha_federated.h:195
uint convert_row_to_internal_format(uchar *buf, MYSQL_ROW row, MYSQL_RES *result)
Definition: ha_federated.cc:912
int reset(void) override
Reset state of file to after 'open'.
Definition: ha_federated.cc:2756
char remote_error_buf[FEDERATED_QUERY_BUFFER_SIZE]
Definition: ha_federated.h:105
bool position_called
Definition: ha_federated.h:102
int stash_remote_error()
Definition: ha_federated.cc:3050
int close(void) override
Definition: ha_federated.cc:1590
int connection_autocommit(bool state)
Definition: ha_federated.cc:3169
bool get_error_message(int error, String *buf) override
Return an error message specific to this handler.
Definition: ha_federated.cc:3063
ulonglong table_flags() const override
Definition: ha_federated.h:148
THR_LOCK_DATA lock
Definition: ha_federated.h:94
int index_end() override
Definition: ha_federated.cc:2446
uint max_supported_key_part_length(HA_CREATE_INFO *create_info) const override
Definition: ha_federated.h:180
int read_range_first(const key_range *start_key, const key_range *end_key, bool eq_range, bool sorted) override
Read first row between two ranges.
Definition: ha_federated.cc:2335
double read_time(uint, uint, ha_rows rows) override
The cost of reading a set of ranges from the table using an index to access it.
Definition: ha_federated.h:202
int connection_rollback()
Definition: ha_federated.cc:3164
void start_bulk_insert(ha_rows rows) override
Prepares the storage engine for bulk inserts.
Definition: ha_federated.cc:1846
int extra(ha_extra_function operation) override
Handles extra signals from MySQL server.
Definition: ha_federated.cc:2716
int delete_all_rows(void) override
Delete all rows in a table.
Definition: ha_federated.cc:2783
MYSQL_ROW_OFFSET current_position
Definition: ha_federated.h:103
int end_bulk_insert() override
End bulk insert.
Definition: ha_federated.cc:1884
DYNAMIC_STRING bulk_insert
Definition: ha_federated.h:108
int index_read_idx_map(uchar *buf, uint index, const uchar *key, key_part_map keypart_map, enum ha_rkey_function find_flag) override
Positions an index cursor to the index specified in argument.
Definition: ha_federated.cc:2190
MYSQL_RES * store_result(MYSQL *mysql)
Store a result set.
Definition: ha_federated.cc:3090
int rnd_pos_by_record(uchar *record) override
This function only works for handlers having HA_PRIMARY_KEY_REQUIRED_FOR_POSITION set.
Definition: ha_federated.cc:3185
MYSQL * mysql
Definition: ha_federated.h:96
int repair(THD *thd, HA_CHECK_OPT *check_opt) override
In this method check_opt can be modified to specify CHECK option to use to call check() upon the tabl...
Definition: ha_federated.cc:1937
bool create_where_from_key(String *to, KEY *key_info, const key_range *start_key, const key_range *end_key, bool records_in_range, bool eq_range)
Definition: ha_federated.cc:1261
int update_row(const uchar *old_data, uchar *new_data) override
Update a single row.
Definition: ha_federated.cc:1977
int real_query(const char *query, size_t length)
Definition: ha_federated.cc:2992
void update_auto_increment(void)
Definition: ha_federated.cc:1909
uint max_supported_record_length() const override
Definition: ha_federated.h:172
~ha_federated() override=default
bool insert_dup_update
Definition: ha_federated.h:107
const char * table_type() const override
The following can be called without an open handler.
Definition: ha_federated.h:138
ulong index_flags(uint, uint, bool) const override
Definition: ha_federated.h:169
int execute_simple_query(const char *query, int len)
Definition: ha_federated.cc:3176
void position(const uchar *record) override
Store a reference to current row.
Definition: ha_federated.cc:2541
ha_rows records_in_range(uint inx, key_range *start_key, key_range *end_key) override
Find number of records in a range.
Definition: ha_federated.cc:1540
int rnd_pos(uchar *buf, uchar *pos) override
Definition: ha_federated.cc:2563
int rnd_end() override
Definition: ha_federated.cc:2441
int rnd_next_int(uchar *buf)
Definition: ha_federated.cc:2471
int rnd_init(bool scan) override
rnd_init() can be called two times without rnd_end() in between (it only makes sense if scan=1).
Definition: ha_federated.cc:2396
int info(uint) override
General method to gather info from handler.
Definition: ha_federated.cc:2625
int external_lock(THD *thd, int lock_type) override
Is not invoked for non-transactional temporary tables.
Definition: ha_federated.cc:3109
void free_result()
Definition: ha_federated.cc:3100
int write_row(uchar *buf) override
Write a row.
Definition: ha_federated.cc:1710
uint max_supported_key_parts() const override
Definition: ha_federated.h:176
int read_range_next() override
Read next row between two endpoints.
Definition: ha_federated.cc:2367
bool append_stmt_insert(String *query)
Construct the INSERT statement.
Definition: ha_federated.cc:1638
bool ignore_duplicates
Definition: ha_federated.h:106
THR_LOCK_DATA ** store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type) override
Is not invoked for non-transactional temporary tables.
Definition: ha_federated.cc:2863
Prealloced_array< MYSQL_RES *, 4 > results
Array of all stored results we get during a query execution.
Definition: ha_federated.h:101
bool replace_duplicates
Definition: ha_federated.h:106
int remote_error_number
Definition: ha_federated.h:104
uint max_supported_keys() const override
Definition: ha_federated.h:175
int optimize(THD *thd, HA_CHECK_OPT *check_opt) override
Definition: ha_federated.cc:1918
ha_rows estimate_rows_upper_bound() override
Return upper bound of current number of records in the table (max.
Definition: ha_federated.cc:2320
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4414
bool eq_range
Definition: handler.h:4474
A table definition from the master.
Definition: rpl_utility.h:248
#define FEDERATED_QUERY_BUFFER_SIZE
Definition: ha_federated.h:48
#define FEDERATED_MAX_KEY_LENGTH
Definition: ha_federated.h:50
ha_rkey_function
Definition: my_base.h:78
@ HA_READ_AFTER_KEY
Definition: my_base.h:82
ulong key_part_map
Definition: my_base.h:1007
my_off_t ha_rows
Definition: my_base.h:1140
ha_extra_function
Definition: my_base.h:184
#define DBUG_PRINT(keyword, arglist)
Definition: my_dbug.h:181
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
static char * query
Definition: myisam_ftdump.cc:45
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
char ** MYSQL_ROW
Definition: mysql.h:145
static int record
Definition: mysqltest.cc:188
Definition: buf0block_hint.cc:30
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
Definition: instrumented_condition_variable.h:32
mode
Definition: file_handle.h:60
required string key
Definition: replication_asynchronous_connection_failover.proto:60
#define HA_BINLOG_ROW_CAPABLE
Definition: handler.h:339
#define HA_FILE_BASED
Definition: handler.h:324
#define HA_CAN_INDEX_BLOBS
Definition: handler.h:257
#define HA_MAX_REC_LENGTH
Definition: handler.h:617
#define HA_READ_NEXT
Definition: handler.h:526
#define HA_CAN_REPAIR
Definition: handler.h:369
#define HA_PARTIAL_COLUMN_READ
Definition: handler.h:211
#define HA_PRIMARY_KEY_IN_READ_INDEX
Definition: handler.h:281
#define HA_NO_PREFIX_CHAR_KEYS
Definition: handler.h:301
#define HA_PRIMARY_KEY_REQUIRED_FOR_POSITION
Definition: handler.h:289
#define HA_BINLOG_STMT_CAPABLE
Definition: handler.h:340
#define HA_READ_RANGE
Definition: handler.h:549
#define HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Definition: handler.h:297
#define HA_NO_TRANSACTIONS
Definition: handler.h:210
#define HA_AUTO_PART_KEY
Definition: handler.h:262
#define HA_NULL_IN_KEY
Definition: handler.h:245
constexpr const unsigned int MAX_KEY
Definition: sql_const.h:46
constexpr const unsigned int MAX_REF_PARTS
Definition: sql_const.h:47
case opt name
Definition: sslopt-case.h:33
Definition: my_sys.h:317
Definition: ha_federated.h:56
char * table
Definition: ha_federated.h:78
char * connect_string
Definition: ha_federated.h:72
int share_key_length
Definition: ha_federated.h:81
char * hostname
Definition: ha_federated.h:73
char * select_query
Definition: ha_federated.h:65
char * database
Definition: ha_federated.h:76
bool parsed
Definition: ha_federated.h:59
char * server_name
Definition: ha_federated.h:69
char * username
Definition: ha_federated.h:74
size_t use_count
Definition: ha_federated.h:85
size_t table_name_length
Definition: ha_federated.h:84
char * password
Definition: ha_federated.h:75
const char * share_key
Definition: ha_federated.h:61
ushort port
Definition: ha_federated.h:82
THR_LOCK lock
Definition: ha_federated.h:87
size_t connect_string_length
Definition: ha_federated.h:84
char * connection_string
Definition: ha_federated.h:70
char * table_name
Definition: ha_federated.h:77
char * sport
Definition: ha_federated.h:80
const char * socket
Definition: ha_federated.h:79
MEM_ROOT mem_root
Definition: ha_federated.h:57
size_t server_name_length
Definition: ha_federated.h:84
mysql_mutex_t mutex
Definition: ha_federated.h:86
char * scheme
Definition: ha_federated.h:71
Definition: handler.h:3632
Struct to hold information about the table that should be created.
Definition: handler.h:3045
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql.h:339
Definition: mysql.h:153
Definition: mysql.h:299
This structure is shared between different table objects.
Definition: table.h:690
Definition: table.h:1399
Definition: thr_lock.h:124
Definition: thr_lock.h:139
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2622
Definition: my_base.h:1124
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
Definition: result.h:30
Definition: mysqlslap.cc:238
thr_lock_type
Definition: thr_lock.h:51
unsigned int uint
Definition: uca9-dump.cc:75