forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_async.h
274 lines (232 loc) · 8.88 KB
/
mysql_async.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright (c) 2019, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef MYSQL_ASYNC_INCLUDED
#define MYSQL_ASYNC_INCLUDED
#include <mysql.h>
/**
@file mysql_async.h
Declarations for asynchronous client communication.
@note this file should not be included as part of packaging.
*/
/**
This enum is to represent different asynchronous operations like reading the
network, writing to network, idle state, or complete state.
*/
enum net_async_operation {
NET_ASYNC_OP_IDLE = 0, /**< default state */
NET_ASYNC_OP_READING, /**< used by my_net_read calls */
NET_ASYNC_OP_WRITING, /**< used by my_net_write calls */
NET_ASYNC_OP_COMPLETE /**< network read or write is complete */
};
/** Reading a packet is a multi-step process, so we have a state machine. */
enum net_async_read_packet_state {
NET_ASYNC_PACKET_READ_IDLE = 0, /**< default packet read state */
NET_ASYNC_PACKET_READ_HEADER, /**< read packet header */
NET_ASYNC_PACKET_READ_BODY, /**< read packet contents */
NET_ASYNC_PACKET_READ_COMPLETE /**< state to define if packet is
completely read */
};
/** Different states when reading a query result. */
enum net_read_query_result_status {
NET_ASYNC_READ_QUERY_RESULT_IDLE = 0, /**< default state */
NET_ASYNC_READ_QUERY_RESULT_FIELD_COUNT, /**< read Ok or read field
count sent as part of
COM_QUERY */
NET_ASYNC_READ_QUERY_RESULT_FIELD_INFO /**< read result of above
COM_* command */
};
/** Sending a command involves the write as well as reading the status. */
enum net_send_command_status {
NET_ASYNC_SEND_COMMAND_IDLE = 0, /**< default send command state */
NET_ASYNC_SEND_COMMAND_WRITE_COMMAND, /**< send COM_* command */
NET_ASYNC_SEND_COMMAND_READ_STATUS /**< read result of above COM_*
command */
};
/**
Async operations are broadly classified into 3 phases:
Connection phase, phase of sending data to server (which is writing phase)
and reading data from server (which is reading phase). Below enum describes
the same
*/
enum net_async_block_state {
NET_NONBLOCKING_CONNECT = 0,
NET_NONBLOCKING_READ,
NET_NONBLOCKING_WRITE
};
/**
Represents the packet to be sent on wire asynchronously.
*/
struct io_vec {
void *iov_base; /**< Starting address */
size_t iov_len; /**< Number of bytes to transfer */
};
/** Local state for multipacket processing */
struct mp_state {
net_async_status mp_status;
size_t mp_start_of_packet;
size_t mp_first_packet_offset;
size_t mp_buf_length;
uint mp_multi_byte_packet;
ulong mp_save_pos;
ulong mp_total_length;
void reset() {
mp_status = NET_ASYNC_COMPLETE;
mp_start_of_packet = 0;
mp_first_packet_offset = 0;
mp_buf_length = 0;
mp_multi_byte_packet = 0;
mp_save_pos = 0;
mp_total_length = 0;
}
};
typedef struct NET_ASYNC {
/**
The position in buff we continue reads from when data is next
available
*/
unsigned char *cur_pos;
/** Blocking state */
enum net_async_block_state async_blocking_state;
/** Our current operation */
enum net_async_operation async_operation;
/** How many bytes we want to read */
size_t async_bytes_wanted;
/**
Simple state to know if we're reading the first row, and
command/query statuses.
*/
bool read_rows_is_first_read;
enum net_send_command_status async_send_command_status;
enum net_read_query_result_status async_read_query_result_status;
/** State when waiting on an async read */
enum net_async_read_packet_state async_packet_read_state;
/** Size of the packet we're currently reading */
size_t async_packet_length;
/**
Headers and vector for our async writes; see net_serv.c for
detailed description.
*/
unsigned char *async_write_headers;
struct io_vec *async_write_vector;
size_t async_write_vector_size;
size_t async_write_vector_current;
/**
If the packet length is less than MAX_PACKET_LENGTH then use a static array
to hold the meta packet header. The array either holds the usual packet
header or a compressed meta packet header as following. The compressed
meta packet header is followwed by usual compresses packet heder that is
7 bytes in length.
Packet
Header
~~~~~~~~~~~~~~~~~~~
B1 B2 B3 : Packet length
B4 : Packet number
~~~~~~~~~~~~~~~~~~~
Payload
~~~~~~~~~~~~~~~~~~~
B5 : COM_COMMAND
~~~~~~~~~~~~~~~~~~~
Compressed Packet
Header
~~~~~~~~~~~~~~~~~~~
B1 B2 B3 : Compress packet length
B4 : Compress Packet Nunmber
00 00 00 : Indicates following payload is uncompressed
~~~~~~~~~~~~~~~~~~~
Payload
~~~~~~~~~~~~~~~~~~~
B8 B9 B10 : Packet size
B11 : Packet number
B12 : COM_COMMAND
~~~~~~~~~~~~~~~~~~~
*/
unsigned char inline_async_write_header[NET_HEADER_SIZE + COMP_HEADER_SIZE +
NET_HEADER_SIZE + 1];
struct io_vec inline_async_write_vector[3];
/** Keep track of compressed buffers */
unsigned char **compressed_write_buffers;
/** Size of the compressed buffer */
size_t compressed_buffers_size;
struct mp_state mp_state;
} NET_ASYNC;
struct NET_EXTENSION {
NET_ASYNC *net_async_context;
mysql_compress_context compress_ctx;
};
NET_EXTENSION *net_extension_init();
void net_extension_free(NET *);
#define NET_EXTENSION_PTR(N) \
((NET_EXTENSION *)((N)->extension ? (N)->extension : NULL))
#define NET_ASYNC_DATA(M) \
((NET_EXTENSION_PTR(M)) ? (NET_EXTENSION_PTR(M)->net_async_context) : NULL)
/**
Asynchronous operations are broadly classified into 2 categories.
1. Connection
2. Query execution
This classification is defined in below enum
*/
enum mysql_async_operation_status {
ASYNC_OP_UNSET = 0,
ASYNC_OP_CONNECT,
ASYNC_OP_QUERY
};
/**
Query execution in an asynchronous fashion is broadly divided into 3 states
which is described in below enum
*/
enum mysql_async_query_state_enum {
QUERY_IDLE = 0,
QUERY_SENDING,
QUERY_READING_RESULT
};
typedef struct MYSQL_ASYNC {
/** Buffer storing the rows result for cli_read_rows_nonblocking */
MYSQL_DATA *rows_result_buffer;
/** a pointer to keep track of the previous row of the current result row */
MYSQL_ROWS **prev_row_ptr;
/** Context needed to track the state of a connection being established */
struct mysql_async_connect *connect_context;
/** Status of the current async op */
enum mysql_async_operation_status async_op_status;
/** Size of the current running async query */
size_t async_query_length;
/** If a query is running, this is its state */
enum mysql_async_query_state_enum async_query_state;
/** context needed to support metadata read operation */
unsigned long *async_read_metadata_field_len;
MYSQL_FIELD *async_read_metadata_fields;
MYSQL_ROWS async_read_metadata_data;
unsigned int async_read_metadata_cur_field;
/** a pointer to keep track of the result sets */
struct MYSQL_RES *async_store_result_result;
/** the query parameters data */
uchar *async_qp_data;
/** the query parameters data length */
unsigned long async_qp_data_length;
} MYSQL_ASYNC;
enum net_async_status my_net_write_nonblocking(NET *net,
const unsigned char *packet,
size_t len, bool *res);
enum net_async_status net_write_command_nonblocking(
NET *net, unsigned char command, const unsigned char *prefix,
size_t prefix_len, const unsigned char *packet, size_t packet_len,
bool *res);
enum net_async_status my_net_read_nonblocking(NET *net, unsigned long *len_ptr);
int mysql_get_socket_descriptor(MYSQL *mysql);
#endif /* MYSQL_ASYNC_INCLUDED */