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