MySQL 8.4.11
Source Code Documentation
processor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTING_SRC_PROCESSORS_BASE_PROCESSOR_H_
27#define ROUTING_SRC_PROCESSORS_BASE_PROCESSOR_H_
28
30
31#include <optional>
32#include <string>
33#include <string_view>
34
35#include "trace_span.h"
36#include "tracer.h"
37
40
41/**
42 * base class of all the processors.
43 *
44 * Processor
45 *
46 * - have their own internal state
47 * - expose a process() function which will be called until
48 * it returns Result::Done
49 *
50 * Processors are stacked.
51 *
52 * The methods:
53 *
54 * - push_processor()
55 * - pop_processor()
56 *
57 * allow to add and remove elements for the stack.
58 *
59 * The top-most processor's process() function is called.
60 */
62 public:
63 enum class Result {
64 Again, // will invoke the process() of the top-most-processor
65 RecvFromClient, // wait for recv from client and invoke ...
66 SendToClient, // wait for send-to-client and invoke ...
67 RecvFromServer, // wait for recv from server and invoke ...
68 RecvFromBoth, // wait for recv from client and server and invoke ..
69 SendToServer, // wait for send-to-server and invoke ...
71
72 Suspend, // wait for explicit resume
73 Done, // pop this processor and invoke the top-most-processor's process()
74
75 Void,
76 };
77
79
80 virtual ~BasicProcessor() = default;
81
83
85
87
88 /**
89 * best-effort human-readable processor type.
90 */
91 virtual std::string diagnostic_name() const;
92
93 /**
94 * stage name of a staged processor.
95 *
96 * @returns stage name or std::nullopt if processor has no explicit stages.
97 */
98 virtual std::optional<std::string_view> diagnostic_stage_name() const = 0;
99
100 private:
102};
103
104/**
105 * a processor base class with helper functions.
106 */
107class Processor : public BasicProcessor {
108 public:
110
111 protected:
113 std::error_code ec);
114
116 std::error_code ec);
117
119 std::error_code ec);
120
122 std::error_code ec);
123
125 std::error_code ec);
126
128 std::error_code ec);
129
130 /**
131 * discard to current message.
132 *
133 * @pre ensure_full_frame() must true.
134 */
136 Channel &src_channel, ClassicProtocolState &src_protocol);
137
138 template <class Proto>
141 return discard_current_msg(conn.channel(), conn.protocol());
142 }
143
144 /**
145 * log a message with error-code as error.
146 */
147 static void log_fatal_error_code(const char *msg, std::error_code ec);
148
149 // see MysqlClassicConnection::trace()
150 [[deprecated(
151 "use 'if (auto &tr = tracer()) { tr.trace(...); } instead")]] void
153
154 Tracer &tracer();
155
156 /**
157 * start a span.
158 *
159 * @param parent_span parent span to nest this trace span in.
160 * @param prefix name of the span.
161 */
162 TraceEvent *trace_span(TraceEvent *parent_span,
163 const std::string_view &prefix);
164
165 /**
166 * end a span and set a status-code.
167 */
170
171 /**
172 * start a command span.
173 *
174 * @param prefix name of the command span.
175 */
176 TraceEvent *trace_command(const std::string_view &prefix);
177
178 /**
179 * start a connect-and-forward span.
180 */
182
183 /**
184 * start a connect span.
185 */
186 TraceEvent *trace_connect(TraceEvent *parent_span);
187
188 /**
189 * start a connect span.
190 */
192
193 /**
194 * start a forward span.
195 */
197
198 /**
199 * end a command span and set a status-code.
200 */
203};
204
205#endif // ROUTING_SRC_PROCESSORS_BASE_PROCESSOR_H_
base class of all the processors.
Definition: processor.h:61
MysqlRoutingClassicConnectionBase * conn_
Definition: processor.h:101
virtual ~BasicProcessor()=default
MysqlRoutingClassicConnectionBase * connection()
Definition: processor.h:84
virtual std::optional< std::string_view > diagnostic_stage_name() const =0
stage name of a staged processor.
Result
Definition: processor.h:63
const MysqlRoutingClassicConnectionBase * connection() const
Definition: processor.h:82
BasicProcessor(MysqlRoutingClassicConnectionBase *conn)
Definition: processor.h:78
virtual stdx::expected< Result, std::error_code > process()=0
virtual std::string diagnostic_name() const
best-effort human-readable processor type.
Definition: processor.cc:38
SSL aware socket buffers.
Definition: channel.h:65
protocol state of a classic protocol connection.
Definition: classic_protocol_state.h:39
Definition: classic_connection_base.h:56
a processor base class with helper functions.
Definition: processor.h:107
static void log_fatal_error_code(const char *msg, std::error_code ec)
log a message with error-code as error.
Definition: processor.cc:139
stdx::expected< Result, std::error_code > send_server_failed(std::error_code ec)
Definition: processor.cc:43
void trace_command_end(TraceEvent *event, TraceEvent::StatusCode status_code=TraceEvent::StatusCode::kUnset)
end a command span and set a status-code.
Definition: processor.cc:211
stdx::expected< Result, std::error_code > recv_client_failed(std::error_code ec)
Definition: processor.cc:72
stdx::expected< Result, std::error_code > client_socket_failed(std::error_code ec)
Definition: processor.cc:92
TraceEvent * trace_connect(TraceEvent *parent_span)
start a connect span.
Definition: processor.cc:186
TraceEvent * trace_command(const std::string_view &prefix)
start a command span.
Definition: processor.cc:165
void trace(Tracer::Event e)
Definition: processor.cc:144
stdx::expected< Result, std::error_code > recv_server_failed(std::error_code ec)
Definition: processor.cc:52
stdx::expected< Result, std::error_code > send_client_failed(std::error_code ec)
Definition: processor.cc:63
void trace_span_end(TraceEvent *event, TraceEvent::StatusCode status_code=TraceEvent::StatusCode::kUnset)
end a span and set a status-code.
Definition: processor.cc:157
stdx::expected< void, std::error_code > discard_current_msg(TlsSwitchableConnection< Proto > &conn)
Definition: processor.h:139
stdx::expected< Result, std::error_code > server_socket_failed(std::error_code ec)
Definition: processor.cc:83
TraceEvent * trace_span(TraceEvent *parent_span, const std::string_view &prefix)
start a span.
Definition: processor.cc:150
stdx::expected< void, std::error_code > discard_current_msg(Channel &src_channel, ClassicProtocolState &src_protocol)
discard to current message.
Definition: processor.cc:100
TraceEvent * trace_connect_and_forward_command(TraceEvent *parent_span)
start a connect-and-forward span.
Definition: processor.cc:176
void trace_set_connection_attributes(TraceEvent *ev)
start a connect span.
Definition: processor.cc:190
TraceEvent * trace_forward_command(TraceEvent *parent_span)
start a forward span.
Definition: processor.cc:207
Tracer & tracer()
Definition: processor.cc:148
a Connection that can be switched to TLS.
Definition: connection_base.h:265
Channel & channel()
Definition: connection_base.h:360
protocol_state_type & protocol()
Definition: connection_base.h:399
Definition: tracer.h:61
traces the timestamps of events in a stderr log.
Definition: tracer.h:49
Definition: expected.h:286
required string event
Definition: replication_group_member_actions.proto:32
Definition: trace_span.h:35
StatusCode
Definition: trace_span.h:65