MySQL 8.3.0
Source Code Documentation
gcs_plugin_messages.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2023, 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#ifndef GCS_PLUGIN_MESSAGES_INCLUDED
24#define GCS_PLUGIN_MESSAGES_INCLUDED
25
26/*
27 Since this file is used on unit tests, through member_info.h,
28 includes must set here and not through plugin_server_include.h.
29*/
30#include <string>
31#include <vector>
32
33#include "my_inttypes.h"
34
35/**
36 This is the base GCS plugin message.
37 It is composed by a fixed header and 1 or more payload items.
38
39 The on-the-wire layout looks like this:
40
41 +-----------------------------------+
42 | fixed header | payload |
43 +-----------------------------------+
44
45 The on-the-wire representation of the message is:
46
47 +-------------------+-----------+--------------------------------------+
48 | field | wire size | description |
49 +===================+===========+======================================+
50 | version | 4 bytes | protocol version |
51 | fixed_hdr_len | 2 bytes | length of the fixed header |
52 | message_len | 8 bytes | length of the message |
53 | cargo_type | 2 bytes | the cargo type in the payload |
54 +-------------------+-----------+--------------------------------------+
55 | payload_item_type | 2 bytes | the item type in the payload |
56 | payload_item_len | 8 bytes | length of the payload item |
57 | payload_item | X bytes | payload item |
58 +-------------------+-----------+--------------------------------------+
59
60 The last tree lines can occur one or more times.
61*/
62
64 public:
65 /**
66 The protocol version number.
67 */
69
70 /**
71 The protocol version number.
72 */
73 static const unsigned int WIRE_VERSION_SIZE;
74
75 /**
76 The on-the-wire size of the header length field.
77 */
78 static const unsigned int WIRE_HD_LEN_SIZE;
79
80 /**
81 The on-the-wire size of the message size field.
82 */
83 static const unsigned int WIRE_MSG_LEN_SIZE;
84
85 /**
86 The on-the-wire size of the cargo type field.
87 */
88 static const unsigned int WIRE_CARGO_TYPE_SIZE;
89
90 /**
91 The on-the-wire size of the fixed header.
92 */
93 static const unsigned int WIRE_FIXED_HEADER_SIZE;
94
95 /**
96 The on-the-wire size of the each payload item type field.
97 */
98 static const unsigned int WIRE_PAYLOAD_ITEM_TYPE_SIZE;
99
100 /**
101 The on-the-wire size of the each payload item size field.
102 */
103 static const unsigned int WIRE_PAYLOAD_ITEM_LEN_SIZE;
104
105 /**
106 The on-the-wire size of the payload item header.
107 */
108 static const unsigned int WIRE_PAYLOAD_ITEM_HEADER_SIZE;
109
110 /**
111 The different cargo type codes.
112
113 NOTE: all type values must fit into WIRE_CARGO_TYPE_SIZE bytes storage.
114 */
116 // This type should not be used anywhere.
118
119 // This cargo type is used for certification events, GTID_EXECUTED
120 // broadcast.
122
123 // This cargo type is used for transaction data.
125
126 // This cargo type is used for recovery events, signal when a given member
127 // becomes online.
129
130 // This cargo type is used for messaging related to stage exchanges,
131 // on which it represents one member.
133
134 // This cargo type is used for messaging related to stage exchanges,
135 // on which it represents a set of members.
137
138 // This cargo type is used for messaging related to members pipeline
139 // stats.
141
142 // This cargo type is used for messaging related to single primary
143 // mode.
145
146 // This cargo type is used for messaging related to group coordinated
147 // actions.
149
150 // This cargo type is used for messaging when checking if a group is valid
151 // for some task
153
154 // This cargo type is used for synchronization before executing a
155 // transaction.
157
158 // This cargo type is used for transaction data with guarantee.
160
161 // This cargo type is used to inform about prepared transactions.
163
164 // This cargo type is used for messages that are for
165 // senders/consumers outside the GR plugin.
167
168 // This cargo type is used for GR Recovery Metadata
170
171 // No valid type codes can appear after this one.
172 CT_MAX = 15
173 };
174
175 private:
176 /**
177 This header instance protocol version.
178 */
180
181 /**
182 This header instance length.
183 */
184 unsigned short m_fixed_header_len;
185
186 /**
187 This is the message length field.
188 */
189 unsigned long long m_msg_len;
190
191 /**
192 The cargo type code.
193 */
195
196 public:
197 virtual ~Plugin_gcs_message() = default;
198
199 /**
200 @return the value of the version field.
201 */
202 int get_version() { return m_version; }
203
204 /**
205 @return the value of the header length field value.
206 */
207 unsigned short get_header_length() { return m_fixed_header_len; }
208
209 /**
210 @return the cargo type.
211 */
213
214 /**
215 @return the message length field value.
216 */
217 unsigned long long get_msg_length() { return m_msg_len; }
218
219 /**
220 Encodes the contents of this instance into the buffer.
221
222 @param[out] buffer the buffer to encode to.
223 */
224 void encode(std::vector<unsigned char> *buffer) const;
225
226 /**
227 Decodes the contents of the buffer and sets the field values
228 according to the values decoded.
229
230 @param[in] buffer the buffer to decode from.
231 @param[in] length the length of the buffer.
232 */
233 void decode(const unsigned char *buffer, size_t length);
234
235 /**
236 Return the cargo type of a given message buffer, without decode
237 the complete message.
238
239 @param[in] buffer the buffer to decode from.
240
241 @return the cargo type of a given message buffer
242 */
243 static enum_cargo_type get_cargo_type(const unsigned char *buffer);
244
245 /**
246 Return the raw data of the first payload item of a given message buffer,
247 without decode the complete message.
248
249 @param[out] buffer the buffer to decode from.
250 @param[out] payload_item_data the data.
251 @param[out] payload_item_length the length of the data.
252 */
254 const unsigned char *buffer, const unsigned char **payload_item_data,
255 size_t *payload_item_length);
256
257 /**
258 Return the raw data of the payload item of a given payload type of a given
259 message buffer.
260
261 @param[in] buffer the buffer to decode from.
262 @param[in] end the end of buffer from which it decode.
263 @param[in] payload_item_type the payload type to be searched.
264 @param[out] payload_item_data the data for the given payload type.
265 @param[out] payload_item_length the length of the data for the given
266 payload type.
267
268 @return the operation status
269 @retval false OK
270 @retval true Error
271 */
273 const unsigned char *buffer, const unsigned char *end,
274 uint16 payload_item_type, const unsigned char **payload_item_data,
275 unsigned long long *payload_item_length);
276
277 protected:
278 /**
279 Plugin_gcs_message constructor. Only to be called by derivative classes
280
281 @param[in] cargo_type Message type to be sent
282 */
283 explicit Plugin_gcs_message(enum_cargo_type cargo_type);
284
285 /**
286 Return the time at which the message contained in the buffer was sent.
287 @see Metrics_handler::get_current_time()
288
289 @note The method
290 static uint64_t get_sent_timestamp(const unsigned char *buffer,
291 size_t length);
292 must be implemented on all children classes in order to allow read
293 the sent timestamp without requiring a object creation and complete
294 message deserialization.
295
296 @param[in] buffer the buffer to decode from.
297 @param[in] length the buffer length
298 @param[in] timestamp_payload_item_type the payload item type of the
299 timestamp.
300
301 @return the time on which the message was sent.
302 */
303 static int64_t get_sent_timestamp(const unsigned char *buffer, size_t length,
304 const uint16 timestamp_payload_item_type);
305
306 /**
307 Encodes the header of this instance into the buffer.
308
309 @param[out] buffer the buffer to encode to.
310 */
311 void encode_header(std::vector<unsigned char> *buffer) const;
312
313 /**
314 Decodes the header of the buffer into this instance.
315
316 @param[out] slider before call `decode_header`: the start of the buffer
317 after call `decode_header`: the position on which the
318 header ends on the buffer.
319 */
320 void decode_header(const unsigned char **slider);
321
322 /**
323 Encodes the contents of this instance payload into the buffer.
324
325 @param[out] buffer the buffer to encode to.
326 */
327 virtual void encode_payload(std::vector<unsigned char> *buffer) const = 0;
328
329 /**
330 Decodes the contents of the buffer and sets the payload field
331 values according to the values decoded.
332
333 @param[in] buffer the buffer to decode from.
334 @param[in] end the end of the buffer.
335 */
336 virtual void decode_payload(const unsigned char *buffer,
337 const unsigned char *end) = 0;
338
339 /**
340 Encodes the given payload item type and length into the buffer.
341
342 @param[out] buffer the buffer to encode to
343 @param[in] payload_item_type the type of the payload item
344 @param[in] payload_item_length the length of the payload item
345 */
347 std::vector<unsigned char> *buffer, uint16 payload_item_type,
348 unsigned long long payload_item_length) const;
349
350 /**
351 Decodes the given payload item type and length from the buffer.
352
353 @param[in] buffer the buffer to encode from
354 @param[out] payload_item_type the type of the payload item
355 @param[out] payload_item_length the length of the payload item
356 */
358 const unsigned char **buffer, uint16 *payload_item_type,
359 unsigned long long *payload_item_length);
360
361 /**
362 Encodes the given payload item (type, length and value) into the buffer as
363 a char (1 byte).
364
365 @param[out] buffer the buffer to encode to
366 @param[in] type the type of the payload item
367 @param[in] value the value of the payload item
368 */
369 void encode_payload_item_char(std::vector<unsigned char> *buffer, uint16 type,
370 unsigned char value) const;
371
372 /**
373 Decodes the given payload item (type, length and value) from the buffer as
374 a char (1 byte).
375
376 @param[in] buffer the buffer to encode from
377 @param[out] type the type of the payload item
378 @param[out] value the value of the payload item
379 */
380 static void decode_payload_item_char(const unsigned char **buffer,
381 uint16 *type, unsigned char *value);
382
383 /**
384 Encodes the given payload item (type, length and value) into the buffer as
385 a 2 bytes integer.
386
387 @param[out] buffer the buffer to encode to
388 @param[in] type the type of the payload item
389 @param[in] value the value of the payload item
390 */
391 void encode_payload_item_int2(std::vector<unsigned char> *buffer, uint16 type,
392 uint16 value) const;
393
394 /**
395 Decodes the given payload item (type, length and value) from the buffer as
396 a 2 bytes integer.
397
398 @param[in] buffer the buffer to encode from
399 @param[out] type the type of the payload item
400 @param[out] value the value of the payload item
401 */
402 void decode_payload_item_int2(const unsigned char **buffer, uint16 *type,
403 uint16 *value);
404
405 /**
406 Encodes the given payload item (type, length and value) into the buffer as
407 a 4 bytes integer.
408
409 @param[out] buffer the buffer to encode to
410 @param[in] type the type of the payload item
411 @param[in] value the value of the payload item
412 */
413 void encode_payload_item_int4(std::vector<unsigned char> *buffer, uint16 type,
414 uint32 value) const;
415
416 /**
417 Decodes the given payload item (type, length and value) from the buffer as
418 a 4 bytes integer.
419
420 @param[in] buffer the buffer to encode from
421 @param[out] type the type of the payload item
422 @param[out] value the value of the payload item
423 */
424 void decode_payload_item_int4(const unsigned char **buffer, uint16 *type,
425 uint32 *value);
426
427 /**
428 Encodes the given payload item (type, length and value) into the buffer as
429 a 8 bytes integer.
430
431 @param[out] buffer the buffer to encode to
432 @param[in] type the type of the payload item
433 @param[in] value the value of the payload item
434 */
435 void encode_payload_item_int8(std::vector<unsigned char> *buffer, uint16 type,
436 ulonglong value) const;
437
438 /**
439 Decodes the given payload item (type, length and value) from the buffer as
440 a 8 bytes integer.
441
442 @param[in] buffer the buffer to encode from
443 @param[out] type the type of the payload item
444 @param[out] value the value of the payload item
445 */
446 static void decode_payload_item_int8(const unsigned char **buffer,
447 uint16 *type, uint64 *value);
448
449 /**
450 Encodes the given payload item (type, length and value) into the buffer as
451 a char array (variable size).
452
453 @param[out] buffer the buffer to encode to
454 @param[in] type the type of the payload item
455 @param[in] value the value of the payload item
456 @param[in] length the length of the payload item
457 */
458 void encode_payload_item_string(std::vector<unsigned char> *buffer,
459 uint16 type, const char *value,
460 unsigned long long length) const;
461
462 /**
463 Decodes the given payload item (type, length and value) from the buffer as
464 a char array (variable size).
465
466 @param[in] buffer the buffer to encode from
467 @param[out] type the type of the payload item
468 @param[out] value the value of the payload item
469 @param[out] length the length of the payload item
470 */
471 void decode_payload_item_string(const unsigned char **buffer, uint16 *type,
472 std::string *value,
473 unsigned long long *length);
474
475 /**
476 Encodes the given payload item (type, length and value) into the buffer as
477 a byte buffer (variable size).
478
479 @param[out] buffer the buffer to encode to
480 @param[in] type the type of the payload item
481 @param[in] value the value of the payload item
482 @param[in] length the length of the payload item
483 */
484 void encode_payload_item_bytes(std::vector<unsigned char> *buffer,
485 uint16 type, const unsigned char *value,
486 unsigned long long length) const;
487
488 /**
489 Decodes the given payload item (type, length and value) from the buffer as
490 a byte buffer (variable size).
491
492 @param[in] buffer the buffer to encode from
493 @param[out] type the type of the payload item
494 @param[out] value the value of the payload item
495 @param[out] length the length of the payload item
496 */
497 void decode_payload_item_bytes(const unsigned char **buffer, uint16 *type,
498 unsigned char *value,
499 unsigned long long *length);
500};
501
502#endif /* GCS_PLUGIN_MESSAGES_INCLUDED */
This is the base GCS plugin message.
Definition: gcs_plugin_messages.h:63
void decode_payload_item_int2(const unsigned char **buffer, uint16 *type, uint16 *value)
Decodes the given payload item (type, length and value) from the buffer as a 2 bytes integer.
Definition: gcs_plugin_messages.cc:258
enum_cargo_type get_cargo_type() const
Definition: gcs_plugin_messages.h:212
void encode(std::vector< unsigned char > *buffer) const
Encodes the contents of this instance into the buffer.
Definition: gcs_plugin_messages.cc:77
void encode_payload_item_type_and_length(std::vector< unsigned char > *buffer, uint16 payload_item_type, unsigned long long payload_item_length) const
Encodes the given payload item type and length into the buffer.
Definition: gcs_plugin_messages.cc:198
static const int PLUGIN_GCS_MESSAGE_VERSION
The protocol version number.
Definition: gcs_plugin_messages.h:68
int m_version
This header instance protocol version.
Definition: gcs_plugin_messages.h:179
void encode_payload_item_string(std::vector< unsigned char > *buffer, uint16 type, const char *value, unsigned long long length) const
Encodes the given payload item (type, length and value) into the buffer as a char array (variable siz...
Definition: gcs_plugin_messages.cc:308
static void get_first_payload_item_raw_data(const unsigned char *buffer, const unsigned char **payload_item_data, size_t *payload_item_length)
Return the raw data of the first payload item of a given message buffer, without decode the complete ...
Definition: gcs_plugin_messages.cc:127
void encode_header(std::vector< unsigned char > *buffer) const
Encodes the header of this instance into the buffer.
Definition: gcs_plugin_messages.cc:55
unsigned long long get_msg_length()
Definition: gcs_plugin_messages.h:217
void encode_payload_item_int2(std::vector< unsigned char > *buffer, uint16 type, uint16 value) const
Encodes the given payload item (type, length and value) into the buffer as a 2 bytes integer.
Definition: gcs_plugin_messages.cc:248
unsigned short get_header_length()
Definition: gcs_plugin_messages.h:207
static const unsigned int WIRE_PAYLOAD_ITEM_LEN_SIZE
The on-the-wire size of the each payload item size field.
Definition: gcs_plugin_messages.h:103
enum_cargo_type
The different cargo type codes.
Definition: gcs_plugin_messages.h:115
@ CT_TRANSACTION_MESSAGE
Definition: gcs_plugin_messages.h:124
@ CT_CERTIFICATION_MESSAGE
Definition: gcs_plugin_messages.h:121
@ CT_RECOVERY_MESSAGE
Definition: gcs_plugin_messages.h:128
@ CT_RECOVERY_METADATA_MESSAGE
Definition: gcs_plugin_messages.h:169
@ CT_TRANSACTION_WITH_GUARANTEE_MESSAGE
Definition: gcs_plugin_messages.h:159
@ CT_GROUP_ACTION_MESSAGE
Definition: gcs_plugin_messages.h:148
@ CT_SINGLE_PRIMARY_MESSAGE
Definition: gcs_plugin_messages.h:144
@ CT_MAX
Definition: gcs_plugin_messages.h:172
@ CT_SYNC_BEFORE_EXECUTION_MESSAGE
Definition: gcs_plugin_messages.h:156
@ CT_MEMBER_INFO_MESSAGE
Definition: gcs_plugin_messages.h:132
@ CT_TRANSACTION_PREPARED_MESSAGE
Definition: gcs_plugin_messages.h:162
@ CT_UNKNOWN
Definition: gcs_plugin_messages.h:117
@ CT_MESSAGE_SERVICE_MESSAGE
Definition: gcs_plugin_messages.h:166
@ CT_MEMBER_INFO_MANAGER_MESSAGE
Definition: gcs_plugin_messages.h:136
@ CT_GROUP_VALIDATION_MESSAGE
Definition: gcs_plugin_messages.h:152
@ CT_PIPELINE_STATS_MEMBER_MESSAGE
Definition: gcs_plugin_messages.h:140
static const unsigned int WIRE_PAYLOAD_ITEM_TYPE_SIZE
The on-the-wire size of the each payload item type field.
Definition: gcs_plugin_messages.h:98
static const unsigned int WIRE_FIXED_HEADER_SIZE
The on-the-wire size of the fixed header.
Definition: gcs_plugin_messages.h:93
virtual void encode_payload(std::vector< unsigned char > *buffer) const =0
Encodes the contents of this instance payload into the buffer.
static const unsigned int WIRE_PAYLOAD_ITEM_HEADER_SIZE
The on-the-wire size of the payload item header.
Definition: gcs_plugin_messages.h:108
enum_cargo_type m_cargo_type
The cargo type code.
Definition: gcs_plugin_messages.h:194
void encode_payload_item_bytes(std::vector< unsigned char > *buffer, uint16 type, const unsigned char *value, unsigned long long length) const
Encodes the given payload item (type, length and value) into the buffer as a byte buffer (variable si...
Definition: gcs_plugin_messages.cc:327
static int64_t get_sent_timestamp(const unsigned char *buffer, size_t length, const uint16 timestamp_payload_item_type)
Return the time at which the message contained in the buffer was sent.
Definition: gcs_plugin_messages.cc:169
void decode_payload_item_string(const unsigned char **buffer, uint16 *type, std::string *value, unsigned long long *length)
Decodes the given payload item (type, length and value) from the buffer as a char array (variable siz...
Definition: gcs_plugin_messages.cc:317
static void decode_payload_item_char(const unsigned char **buffer, uint16 *type, unsigned char *value)
Decodes the given payload item (type, length and value) from the buffer as a char (1 byte).
Definition: gcs_plugin_messages.cc:237
static const unsigned int WIRE_MSG_LEN_SIZE
The on-the-wire size of the message size field.
Definition: gcs_plugin_messages.h:83
int get_version()
Definition: gcs_plugin_messages.h:202
void decode(const unsigned char *buffer, size_t length)
Decodes the contents of the buffer and sets the field values according to the values decoded.
Definition: gcs_plugin_messages.cc:103
void encode_payload_item_int8(std::vector< unsigned char > *buffer, uint16 type, ulonglong value) const
Encodes the given payload item (type, length and value) into the buffer as a 8 bytes integer.
Definition: gcs_plugin_messages.cc:288
static bool get_payload_item_type_raw_data(const unsigned char *buffer, const unsigned char *end, uint16 payload_item_type, const unsigned char **payload_item_data, unsigned long long *payload_item_length)
Return the raw data of the payload item of a given payload type of a given message buffer.
Definition: gcs_plugin_messages.cc:139
static void decode_payload_item_type_and_length(const unsigned char **buffer, uint16 *payload_item_type, unsigned long long *payload_item_length)
Decodes the given payload item type and length from the buffer.
Definition: gcs_plugin_messages.cc:214
static const unsigned int WIRE_HD_LEN_SIZE
The on-the-wire size of the header length field.
Definition: gcs_plugin_messages.h:78
void encode_payload_item_int4(std::vector< unsigned char > *buffer, uint16 type, uint32 value) const
Encodes the given payload item (type, length and value) into the buffer as a 4 bytes integer.
Definition: gcs_plugin_messages.cc:268
unsigned long long m_msg_len
This is the message length field.
Definition: gcs_plugin_messages.h:189
virtual ~Plugin_gcs_message()=default
void encode_payload_item_char(std::vector< unsigned char > *buffer, uint16 type, unsigned char value) const
Encodes the given payload item (type, length and value) into the buffer as a char (1 byte).
Definition: gcs_plugin_messages.cc:226
static const unsigned int WIRE_VERSION_SIZE
The protocol version number.
Definition: gcs_plugin_messages.h:73
void decode_payload_item_int4(const unsigned char **buffer, uint16 *type, uint32 *value)
Decodes the given payload item (type, length and value) from the buffer as a 4 bytes integer.
Definition: gcs_plugin_messages.cc:278
virtual void decode_payload(const unsigned char *buffer, const unsigned char *end)=0
Decodes the contents of the buffer and sets the payload field values according to the values decoded.
void decode_payload_item_bytes(const unsigned char **buffer, uint16 *type, unsigned char *value, unsigned long long *length)
Decodes the given payload item (type, length and value) from the buffer as a byte buffer (variable si...
Definition: gcs_plugin_messages.cc:337
unsigned short m_fixed_header_len
This header instance length.
Definition: gcs_plugin_messages.h:184
static const unsigned int WIRE_CARGO_TYPE_SIZE
The on-the-wire size of the cargo type field.
Definition: gcs_plugin_messages.h:88
Plugin_gcs_message(enum_cargo_type cargo_type)
Plugin_gcs_message constructor.
Definition: gcs_plugin_messages.cc:49
static void decode_payload_item_int8(const unsigned char **buffer, uint16 *type, uint64 *value)
Decodes the given payload item (type, length and value) from the buffer as a 8 bytes integer.
Definition: gcs_plugin_messages.cc:298
void decode_header(const unsigned char **slider)
Decodes the header of the buffer into this instance.
Definition: gcs_plugin_messages.cc:84
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
uint64_t uint64
Definition: my_inttypes.h:68
uint16_t uint16
Definition: my_inttypes.h:64
uint32_t uint32
Definition: my_inttypes.h:66
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:75
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:417
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
required string type
Definition: replication_group_member_actions.proto:33