MySQL 8.0.39
Source Code Documentation
gcs_internal_message.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 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_MSG_H
25#define GCS_MSG_H
26
27#include <cassert>
28#include <cstdlib>
29#include <memory>
30#include <sstream>
31#include <unordered_set>
32#include <vector>
35
37
39
40/**
41 Deleter for objects managed by a std::unique_ptr that were allocated using
42 the malloc family of functions instead of the new operator.
43 */
45 void operator()(unsigned char *buffer) const { std::free(buffer); }
46};
47
48/**
49 This class is an abstraction for the packet concept. It is used to manipulate
50 the contents of a buffer that is to be sent to the network in an optimal way.
51
52 The on-the-wire layout looks like this:
53
54 +--------------+-----------------+----------------+-----------+
55 | fixed header | dynamic headers | stage metadata | payload |
56 +--------------+-----------------+----------------+-----------+
57 */
59 public:
60 using buffer_ptr = std::unique_ptr<unsigned char, Gcs_packet_buffer_deleter>;
61
62 private:
63 /**
64 Fixed header which is common regardless whether the packet has been
65 changed by a stage or not.
66 */
68
69 /**
70 List of dynamic headers created by the stages by which the packet has
71 passed through and changed it.
72 */
73 std::vector<Gcs_dynamic_header> m_dynamic_headers;
74
75 /**
76 List of stage metadata created by the stages by which the packet has passed
77 through.
78 This list always has the same length as m_dynamic_headers, and the
79 following holds:
80
81 For every i, m_stage_metadata[i] and m_dynamic_headers[i] correspond to
82 the same stage.
83 */
84 std::vector<std::unique_ptr<Gcs_stage_metadata>> m_stage_metadata;
85
86 /**
87 Index of the next stage to apply/revert in both m_dynamic_headers and
88 m_stage_metadata.
89 */
90 std::size_t m_next_stage_index{0};
91
92 /**
93 The buffer containing all serialized data for this packet.
94 */
96
97 /**
98 The capacity of the serialization buffer.
99 */
100 unsigned long long m_serialized_packet_size{0};
101
102 /**
103 The offset in m_serialized_packet where the application payload starts.
104 */
106
107 /**
108 The size of the serialized application payload in m_serialized_packet.
109 */
110 unsigned long long m_serialized_payload_size{0};
111
112 /**
113 The size of the serialized m_stage_metadata in m_serialized_packet.
114 */
115 unsigned long long m_serialized_stage_metadata_size{0};
116
117 /**
118 The XCom synode in which this packet was delivered.
119 */
121
122 /**
123 The XCom synode in which this packet was delivered.
124 */
126
127 public:
128 /**
129 This factory method is to be used when sending a packet.
130
131 @param cargo The message type
132 @param current_version The pipeline version
133 @param dynamic_headers The dynamic headers of the stages the packet will go
134 through
135 @param stage_metadata The stage metadata of the stages the packet will go
136 through
137 @param payload_size The payload size
138 @retval {true, Gcs_packet} If packet is created successfully
139 @retval {false, _} If memory could not be allocated
140 */
141 static std::pair<bool, Gcs_packet> make_outgoing_packet(
142 Cargo_type const &cargo, Gcs_protocol_version const &current_version,
143 std::vector<Gcs_dynamic_header> &&dynamic_headers,
144 std::vector<std::unique_ptr<Gcs_stage_metadata>> &&stage_metadata,
145 unsigned long long const &payload_size);
146
147 /**
148 This factory method is to be used when modifying a packet. This builds a
149 packet with all the same headers, metadata, and state of @c original_packet.
150
151 It is used, for example, by:
152 - The compression stage of the pipeline, to derive the compressed packet from
153 the original, uncompressed packet.
154 - The fragmentation stage of the pipeline, to derive the fragments from the
155 original packet.
156
157 @param original_packet The packet to "clone"
158 @param new_payload_size The payload size of this packet
159 @retval {true, Gcs_packet} If packet is created successfully
160 @retval {false, _} If memory could not be allocated
161 */
162 static std::pair<bool, Gcs_packet> make_from_existing_packet(
163 Gcs_packet const &original_packet,
164 unsigned long long const &new_payload_size);
165
166 /**
167 This factory method is to be used when receiving a packet from the network.
168
169 @param buffer Buffer with a serialized packet
170 @param buffer_size Size of the buffer
171 @param delivery_synode The XCom synode where the packet was decided on
172 @param origin_synode The XCom synode that identifies the origin of the packet
173 @param pipeline The message pipeline
174 @returns A packet initialized from the buffer
175 */
177 unsigned long long buffer_size,
178 synode_no const &delivery_synode,
179 synode_no const &origin_synode,
180 Gcs_message_pipeline const &pipeline);
181
182 Gcs_packet() noexcept;
183
184 /**
185 These constructors are to be used when move semantics may be needed.
186 */
187 Gcs_packet(Gcs_packet &&packet) noexcept;
188 Gcs_packet &operator=(Gcs_packet &&packet) noexcept;
189
190 Gcs_packet(const Gcs_packet &packet) = delete;
191 Gcs_packet &operator=(const Gcs_packet &packet) = delete;
192
193 /**
194 Retrieve this packet's header.
195 @returns The packet's header
196 */
198
199 /**
200 Retrieve this packet's dynamic headers.
201 @returns The packet's dynamic headers
202 */
204
205 /**
206 Retrieve this packet's stage metadata.
207 @returns The packet's stage metadata
208 */
210 const;
211
212 std::size_t const &get_next_stage_index() const;
213
216
218
220
221 unsigned char *get_payload_pointer();
222
223 void set_payload_length(unsigned long long const &new_length);
224
225 /**
226 Return the value of the maximum supported version.
227 */
229
230 /**
231 Return the value of the version in use.
232 */
234
235 /**
236 Return the cargo type.
237 */
239
240 /**
241 Return the total length.
242 */
243 unsigned long long get_total_length() const;
244
245 /**
246 Return the payload length.
247 */
248 unsigned long long const &get_payload_length() const;
249
250 /**
251 Encode the packet content into its serialization buffer, and release
252 ownership of the serialization buffer.
253
254 This method must only be called on a valid packet, i.e. a packet for which
255 @c allocate_serialization_buffer was called and returned true.
256
257 @retval {buffer, buffer_size} The buffer with the serialized packet, and
258 its size
259 */
260 std::pair<buffer_ptr, unsigned long long> serialize();
261
262 /**
263 Create a string representation of the packet to be logged.
264
265 @param output Reference to the output stream where the string will be
266 created.
267 */
268 void dump(std::ostringstream &output) const;
269
271
272 Gcs_xcom_synode const &get_origin_synode() const;
273
274 private:
275 /**
276 Constructor called by @c make_to_send.
277
278 @param cargo The message type
279 @param current_version The pipeline version
280 @param dynamic_headers The dynamic headers of the stages the packet will go
281 through
282 @param stage_metadata The stage metadata of the stages the packet will go
283 through
284 @param payload_size The payload size
285 */
286 explicit Gcs_packet(
287 Cargo_type const &cargo, Gcs_protocol_version const &current_version,
288 std::vector<Gcs_dynamic_header> &&dynamic_headers,
289 std::vector<std::unique_ptr<Gcs_stage_metadata>> &&stage_metadata,
290 unsigned long long const &payload_size);
291
292 /**
293 Constructor called by @c make_from_existing_packet.
294
295 @param original_packet The packet to "clone"
296 @param new_payload_size The payload size of this packet
297 */
298 explicit Gcs_packet(Gcs_packet const &original_packet,
299 unsigned long long const &new_payload_size);
300
301 /**
302 Constructor called by @c make_from_serialized_buffer.
303
304 @param delivery_synode The XCom synode where the packet was decided on
305 @param origin_synode The XCom synode that identifieis the origin of this
306 packet
307 */
308 explicit Gcs_packet(synode_no const &delivery_synode,
309 synode_no const &origin_synode);
310
311 /**
312 Allocates the underlying buffer where the packet will be serialized to using
313 @c serialize.
314
315 @returns true if the required buffer could not be allocated, false otherwise
316 */
318
319 /**
320 Decode the packet content from the given buffer containing a serialized
321 packet.
322
323 @param buffer Buffer containing a serialized packet
324 @param buffer_size Size of the buffer
325 @param pipeline The message pipeline
326 */
327 void deserialize(buffer_ptr &&buffer, unsigned long long buffer_size,
328 Gcs_message_pipeline const &pipeline);
329};
330
331#endif // GCS_MSG_H
This is a default header created per stage and contains information to decode it.
Definition: gcs_internal_message_headers.h:427
This header is internal to the MySQL GCS library and contains metadata information about the message ...
Definition: gcs_internal_message_headers.h:179
This is the pipeline that an outgoing or incoming message has to go through when being sent to or rec...
Definition: gcs_message_stages.h:365
This class is an abstraction for the packet concept.
Definition: gcs_internal_message.h:58
static std::pair< bool, Gcs_packet > make_from_existing_packet(Gcs_packet const &original_packet, unsigned long long const &new_payload_size)
This factory method is to be used when modifying a packet.
Definition: gcs_internal_message.cc:99
void set_payload_length(unsigned long long const &new_length)
Definition: gcs_internal_message.cc:248
Gcs_internal_message_header m_fixed_header
Fixed header which is common regardless whether the packet has been changed by a stage or not.
Definition: gcs_internal_message.h:67
buffer_ptr m_serialized_packet
The buffer containing all serialized data for this packet.
Definition: gcs_internal_message.h:95
std::vector< Gcs_dynamic_header > m_dynamic_headers
List of dynamic headers created by the stages by which the packet has passed through and changed it.
Definition: gcs_internal_message.h:73
Gcs_protocol_version get_used_version() const
Return the value of the version in use.
Definition: gcs_internal_message.cc:258
std::size_t m_serialized_payload_offset
The offset in m_serialized_packet where the application payload starts.
Definition: gcs_internal_message.h:105
std::vector< Gcs_dynamic_header > const & get_dynamic_headers() const
Retrieve this packet's dynamic headers.
Definition: gcs_internal_message.cc:218
void prepare_for_next_outgoing_stage()
Definition: gcs_internal_message.cc:232
static std::pair< bool, Gcs_packet > make_outgoing_packet(Cargo_type const &cargo, Gcs_protocol_version const &current_version, std::vector< Gcs_dynamic_header > &&dynamic_headers, std::vector< std::unique_ptr< Gcs_stage_metadata > > &&stage_metadata, unsigned long long const &payload_size)
This factory method is to be used when sending a packet.
Definition: gcs_internal_message.cc:43
void dump(std::ostringstream &output) const
Create a string representation of the packet to be logged.
Definition: gcs_internal_message.cc:375
std::size_t m_next_stage_index
Index of the next stage to apply/revert in both m_dynamic_headers and m_stage_metadata.
Definition: gcs_internal_message.h:90
Gcs_xcom_synode m_delivery_synode
The XCom synode in which this packet was delivered.
Definition: gcs_internal_message.h:120
unsigned long long const & get_payload_length() const
Return the payload length.
Definition: gcs_internal_message.cc:270
Gcs_xcom_synode const & get_origin_synode() const
Definition: gcs_internal_message.cc:391
Gcs_xcom_synode const & get_delivery_synode() const
Definition: gcs_internal_message.cc:387
Gcs_packet() noexcept
Definition: gcs_internal_message.cc:30
static Gcs_packet make_incoming_packet(buffer_ptr &&buffer, unsigned long long buffer_size, synode_no const &delivery_synode, synode_no const &origin_synode, Gcs_message_pipeline const &pipeline)
This factory method is to be used when receiving a packet from the network.
Definition: gcs_internal_message.cc:143
Gcs_xcom_synode m_origin_synode
The XCom synode in which this packet was delivered.
Definition: gcs_internal_message.h:125
Gcs_stage_metadata & get_current_stage_header()
Definition: gcs_internal_message.cc:240
std::vector< std::unique_ptr< Gcs_stage_metadata > > const & get_stage_metadata() const
Retrieve this packet's stage metadata.
Definition: gcs_internal_message.cc:224
void deserialize(buffer_ptr &&buffer, unsigned long long buffer_size, Gcs_message_pipeline const &pipeline)
Decode the packet content from the given buffer containing a serialized packet.
Definition: gcs_internal_message.cc:324
unsigned long long m_serialized_payload_size
The size of the serialized application payload in m_serialized_packet.
Definition: gcs_internal_message.h:110
std::vector< std::unique_ptr< Gcs_stage_metadata > > m_stage_metadata
List of stage metadata created by the stages by which the packet has passed through.
Definition: gcs_internal_message.h:84
unsigned long long m_serialized_stage_metadata_size
The size of the serialized m_stage_metadata in m_serialized_packet.
Definition: gcs_internal_message.h:115
std::unique_ptr< unsigned char, Gcs_packet_buffer_deleter > buffer_ptr
Definition: gcs_internal_message.h:60
void prepare_for_next_incoming_stage()
Definition: gcs_internal_message.cc:234
Gcs_internal_message_header const & get_fixed_header() const
Retrieve this packet's header.
Definition: gcs_internal_message.cc:214
unsigned long long get_total_length() const
Return the total length.
Definition: gcs_internal_message.cc:266
Cargo_type get_cargo_type() const
Return the cargo type.
Definition: gcs_internal_message.cc:262
unsigned long long m_serialized_packet_size
The capacity of the serialization buffer.
Definition: gcs_internal_message.h:100
bool allocate_serialization_buffer()
Allocates the underlying buffer where the packet will be serialized to using serialize.
Definition: gcs_internal_message.cc:274
unsigned char * get_payload_pointer()
Definition: gcs_internal_message.cc:244
Gcs_protocol_version get_maximum_version() const
Return the value of the maximum supported version.
Definition: gcs_internal_message.cc:254
std::pair< buffer_ptr, unsigned long long > serialize()
Encode the packet content into its serialization buffer, and release ownership of the serialization b...
Definition: gcs_internal_message.cc:293
Gcs_dynamic_header & get_current_dynamic_header()
Definition: gcs_internal_message.cc:236
std::size_t const & get_next_stage_index() const
Definition: gcs_internal_message.cc:228
Abstract class that defines specific metadata associated to a stage if it decides to extend it.
Definition: gcs_internal_message_headers.h:562
Defines a message identifier so that joining members can fetch the associated packet from a remote no...
Definition: gcs_xcom_synode.h:39
Cargo_type
The different cargo type codes.
Definition: gcs_internal_message_headers.h:115
Gcs_protocol_version
The GCS protocol versions.
Definition: gcs_types.h:128
#define free(A)
Definition: lexyy.cc:915
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:315
Definition: gcs_xcom_synode.h:64
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2874
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr
The following is a common type that is returned by all the ut::make_unique (non-aligned) specializati...
Definition: ut0new.h:2438
Deleter for objects managed by a std::unique_ptr that were allocated using the malloc family of funct...
Definition: gcs_internal_message.h:44
void operator()(unsigned char *buffer) const
Definition: gcs_internal_message.h:45