MySQL 26.7.0
Source Code Documentation
classic_lazy_connect.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_SESSION_CLASSIC_LAZY_CONNECT_H_
27#define ROUTING_SRC_PROCESSORS_SESSION_CLASSIC_LAZY_CONNECT_H_
28
29#include <system_error>
30
33#include "router_require.h"
34
35/**
36 * attach a server connection and initialize it.
37 *
38 * - if a server-connection is attached to the client connection, leave.
39 * - otherwise,
40 * - if a connection can be taken from the pool, take it.
41 * - otherwise, connect to the server and authenticate.
42 * - set tracking session-vars
43 * - set the client's schema, if it differs from the server-connection's.
44 *
45 * Precondition:
46 *
47 * - the client's cleartext password must be known.
48 */
50 public:
51 /**
52 * create a lazy-connector.
53 *
54 * @param conn a connection handle
55 * @param in_handshake if true, the client connection is in Greeting or
56 * ChangeUser right now.
57 * @param on_error function that's called if an error happened.
58 * @param parent_event parent event for the tracer
59 *
60 * If "in_handshake" the LazyConnector may ask the client for a
61 * "auth-method-switch" or a "plaintext-password".
62 */
64 MysqlRoutingClassicConnectionBase *conn, bool in_handshake,
65 std::function<void(const classic_protocol::message::server::Error &err)>
66 on_error,
67 TraceEvent *parent_event)
68 : ForwardingProcessor(conn),
69 in_handshake_{in_handshake},
70 on_error_(std::move(on_error)),
71 parent_event_(parent_event) {}
72
73 enum class Stage {
74 Init,
76 Connect,
82 SetVars,
94
97
98 Done,
99 };
100
102
104 [[nodiscard]] Stage stage() const { return stage_; }
105
106 std::optional<std::string_view> diagnostic_stage_name() const override;
107
109 std::optional<bool> ssl;
110 std::optional<bool> x509;
111 std::optional<std::string> issuer;
112 std::optional<std::string> subject;
113 };
114
115 void failed(
116 const std::optional<classic_protocol::message::server::Error> &err) {
117 failed_ = err;
118 }
119
120 std::optional<classic_protocol::message::server::Error> failed() const {
121 return failed_;
122 }
123
124 private:
146
149
151
153
155
156 std::function<void(const classic_protocol::message::server::Error &err)>
158
159 bool retry_connect_{false}; // set on transient failure
160 bool already_fallback_{false}; // set in fallback_to_write()
161
162 // start timepoint to calculate the connect-retry-timeout.
163 std::chrono::steady_clock::time_point started_{
164 std::chrono::steady_clock::now()};
165
166 std::optional<classic_protocol::message::server::Error> failed_;
167
168 std::string trx_stmt_;
169
180};
181
182#endif // ROUTING_SRC_PROCESSORS_SESSION_CLASSIC_LAZY_CONNECT_H_
a processor base class with helper functions.
Definition: forwarding_processor.h:38
attach a server connection and initialize it.
Definition: classic_lazy_connect.h:49
stdx::expected< Processor::Result, std::error_code > fallback_to_write()
Definition: classic_lazy_connect.cc:896
stdx::expected< Processor::Result, std::error_code > authenticated()
Definition: classic_lazy_connect.cc:499
stdx::expected< Processor::Result, std::error_code > set_vars_done()
Definition: classic_lazy_connect.cc:657
void failed(const std::optional< classic_protocol::message::server::Error > &err)
Definition: classic_lazy_connect.h:115
stdx::expected< Processor::Result, std::error_code > connected()
the handshake part.
Definition: classic_lazy_connect.cc:395
stdx::expected< Processor::Result, std::error_code > set_server_option_done()
Definition: classic_lazy_connect.cc:699
stdx::expected< Processor::Result, std::error_code > process() override
Definition: classic_lazy_connect.cc:234
stdx::expected< Processor::Result, std::error_code > wait_gtid_executed()
Definition: classic_lazy_connect.cc:818
stdx::expected< Processor::Result, std::error_code > fetch_sys_vars_done()
Definition: classic_lazy_connect.cc:759
stdx::expected< Processor::Result, std::error_code > set_trx_characteristics()
Definition: classic_lazy_connect.cc:943
std::function< void(const classic_protocol::message::server::Error &err)> on_error_
Definition: classic_lazy_connect.h:157
stdx::expected< Processor::Result, std::error_code > set_schema_done()
Definition: classic_lazy_connect.cc:794
TraceEvent * trace_event_authenticate_
Definition: classic_lazy_connect.h:172
std::string trx_stmt_
Definition: classic_lazy_connect.h:168
stdx::expected< Processor::Result, std::error_code > from_stash()
Definition: classic_lazy_connect.cc:309
stdx::expected< Processor::Result, std::error_code > wait_gtid_executed_done()
Definition: classic_lazy_connect.cc:864
Stage stage_
Definition: classic_lazy_connect.h:150
stdx::expected< Processor::Result, std::error_code > pool_or_close()
Definition: classic_lazy_connect.cc:887
std::optional< std::string_view > diagnostic_stage_name() const override
stage name of a staged processor.
Definition: classic_lazy_connect.cc:1074
TraceEvent * trace_event_set_schema_
Definition: classic_lazy_connect.h:175
TraceEvent * trace_event_fallback_to_write_
Definition: classic_lazy_connect.h:178
RouterRequireFetcher::Result required_connection_attributes_fetcher_result_
Definition: classic_lazy_connect.h:154
stdx::expected< Processor::Result, std::error_code > set_schema()
Definition: classic_lazy_connect.cc:770
bool already_fallback_
Definition: classic_lazy_connect.h:160
bool retry_connect_
Definition: classic_lazy_connect.h:159
bool in_handshake_
Definition: classic_lazy_connect.h:152
stdx::expected< Processor::Result, std::error_code > fetch_sys_vars()
Definition: classic_lazy_connect.cc:717
std::optional< classic_protocol::message::server::Error > failed_
Definition: classic_lazy_connect.h:166
LazyConnector(MysqlRoutingClassicConnectionBase *conn, bool in_handshake, std::function< void(const classic_protocol::message::server::Error &err)> on_error, TraceEvent *parent_event)
create a lazy-connector.
Definition: classic_lazy_connect.h:63
TraceEvent * trace_event_wait_gtid_executed_
Definition: classic_lazy_connect.h:176
TraceEvent * trace_event_set_trx_characteristics_
Definition: classic_lazy_connect.h:179
std::chrono::steady_clock::time_point started_
Definition: classic_lazy_connect.h:163
stdx::expected< Processor::Result, std::error_code > send_auth_ok()
Definition: classic_lazy_connect.cc:1050
stdx::expected< Processor::Result, std::error_code > fetch_user_attrs_done()
Definition: classic_lazy_connect.cc:1022
TraceEvent * trace_event_set_vars_
Definition: classic_lazy_connect.h:173
TraceEvent * trace_event_fetch_sys_vars_
Definition: classic_lazy_connect.h:174
stdx::expected< Processor::Result, std::error_code > set_trx_characteristics_done()
Definition: classic_lazy_connect.cc:983
stdx::expected< Processor::Result, std::error_code > set_server_option()
Definition: classic_lazy_connect.cc:671
Stage stage() const
Definition: classic_lazy_connect.h:104
Stage
Definition: classic_lazy_connect.h:73
stdx::expected< Processor::Result, std::error_code > connect()
Definition: classic_lazy_connect.cc:368
TraceEvent * trace_event_check_read_only_
Definition: classic_lazy_connect.h:177
stdx::expected< Processor::Result, std::error_code > fetch_user_attrs()
Definition: classic_lazy_connect.cc:1003
TraceEvent * parent_event_
Definition: classic_lazy_connect.h:170
TraceEvent * trace_event_connect_
Definition: classic_lazy_connect.h:171
std::optional< classic_protocol::message::server::Error > failed() const
Definition: classic_lazy_connect.h:120
void stage(Stage stage)
Definition: classic_lazy_connect.h:103
stdx::expected< Processor::Result, std::error_code > init()
Definition: classic_lazy_connect.cc:302
stdx::expected< Processor::Result, std::error_code > set_vars()
Definition: classic_lazy_connect.cc:566
Definition: classic_connection_base.h:57
Definition: expected.h:286
borrowable::message::server::Error< false > Error
Definition: classic_protocol_message.h:1411
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:943
Define std::hash<Gtid>.
Definition: gtid.h:355
Definition: classic_lazy_connect.h:108
std::optional< bool > x509
Definition: classic_lazy_connect.h:110
std::optional< std::string > subject
Definition: classic_lazy_connect.h:112
std::optional< std::string > issuer
Definition: classic_lazy_connect.h:111
std::optional< bool > ssl
Definition: classic_lazy_connect.h:109
Definition: trace_span.h:35