MySQL 26.7.0
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 {
99 return std::nullopt;
100 }
101
102 private:
104};
105
106/**
107 * a processor base class with helper functions.
108 */
109class Processor : public BasicProcessor {
110 public:
112
113 protected:
115 std::error_code ec);
116
118 std::error_code ec);
119
121 std::error_code ec);
122
124 std::error_code ec);
125
127 std::error_code ec);
128
130 std::error_code ec);
131
132 /**
133 * discard to current message.
134 *
135 * @pre ensure_full_frame() must true.
136 */
138 Channel &src_channel, ClassicProtocolState &src_protocol);
139
140 template <class Proto>
143 return discard_current_msg(conn.channel(), conn.protocol());
144 }
145
146 /**
147 * log a message with error-code as error.
148 */
149 static void log_fatal_error_code(const char *msg, std::error_code ec);
150
151 // see MysqlClassicConnection::trace()
152 [[deprecated(
153 "use 'if (auto &tr = tracer()) { tr.trace(...); } instead")]] void
155
156 Tracer &tracer();
157
158 /**
159 * start a span.
160 *
161 * @param parent_span parent span to nest this trace span in.
162 * @param prefix name of the span.
163 */
164 TraceEvent *trace_span(TraceEvent *parent_span,
165 const std::string_view &prefix);
166
167 /**
168 * end a span and set a status-code.
169 */
172
173 /**
174 * start a command span.
175 *
176 * @param prefix name of the command span.
177 */
178 TraceEvent *trace_command(const std::string_view &prefix);
179
180 /**
181 * start a connect-and-forward span.
182 */
184
185 /**
186 * start a connect span.
187 */
188 TraceEvent *trace_connect(TraceEvent *parent_span);
189
190 /**
191 * start a connect span.
192 */
194
195 /**
196 * start a forward span.
197 */
199
200 /**
201 * end a command span and set a status-code.
202 */
205};
206
207#endif // ROUTING_SRC_PROCESSORS_BASE_PROCESSOR_H_
base class of all the processors.
Definition: processor.h:61
MysqlRoutingClassicConnectionBase * conn_
Definition: processor.h:103
virtual ~BasicProcessor()=default
MysqlRoutingClassicConnectionBase * connection()
Definition: processor.h:84
virtual std::optional< std::string_view > diagnostic_stage_name() const
stage name of a staged processor.
Definition: processor.h:98
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:40
Definition: classic_connection_base.h:57
a processor base class with helper functions.
Definition: processor.h:109
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:210
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:141
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:206
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