MySQL 8.3.0
Source Code Documentation
classic_lazy_connect.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2023, 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 also distributed 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 included with MySQL.
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 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
25#ifndef ROUTING_CLASSIC_LAZY_CONNECT_INCLUDED
26#define ROUTING_CLASSIC_LAZY_CONNECT_INCLUDED
27
28#include <system_error>
29
32#include "router_require.h"
33
34/**
35 * attach a server connection and initialize it.
36 *
37 * - if a server-connection is attached to the client connection, leave.
38 * - otherwise,
39 * - if a connection can be taken from the pool, take it.
40 * - otherwise, connect to the server and authenticate.
41 * - set tracking session-vars
42 * - set the client's schema, if it differs from the server-connection's.
43 *
44 * Precondition:
45 *
46 * - the client's cleartext password must be known.
47 */
49 public:
50 /**
51 * create a lazy-connector.
52 *
53 * @param conn a connection handle
54 * @param in_handshake if true, the client connection is in Greeting or
55 * ChangeUser right now.
56 * @param on_error function that's called if an error happened.
57 * @param parent_event parent event for the tracer
58 *
59 * If "in_handshake" the LazyConnector may ask the client for a
60 * "auth-method-switch" or a "plaintext-password".
61 */
63 MysqlRoutingClassicConnectionBase *conn, bool in_handshake,
64 std::function<void(const classic_protocol::message::server::Error &err)>
65 on_error,
66 TraceEvent *parent_event)
67 : ForwardingProcessor(conn),
68 in_handshake_{in_handshake},
69 on_error_(std::move(on_error)),
70 parent_event_(parent_event) {}
71
72 enum class Stage {
73 Connect,
79 SetVars,
91
94
95 Done,
96 };
97
99
101 [[nodiscard]] Stage stage() const { return stage_; }
102
104 std::optional<bool> ssl;
105 std::optional<bool> x509;
106 std::optional<std::string> issuer;
107 std::optional<std::string> subject;
108 };
109
110 void failed(
111 const std::optional<classic_protocol::message::server::Error> &err) {
112 failed_ = err;
113 }
114
115 std::optional<classic_protocol::message::server::Error> failed() const {
116 return failed_;
117 }
118
119 private:
139
142
144
146
148
149 std::function<void(const classic_protocol::message::server::Error &err)>
151
152 bool retry_connect_{false}; // set on transient failure
153 bool already_fallback_{false}; // set in fallback_to_write()
154
155 // start timepoint to calculate the connect-retry-timeout.
156 std::chrono::steady_clock::time_point started_{
157 std::chrono::steady_clock::now()};
158
159 std::optional<classic_protocol::message::server::Error> failed_;
160
161 std::string trx_stmt_;
162
173};
174
175#endif
a processor base class with helper functions.
Definition: forwarding_processor.h:36
attach a server connection and initialize it.
Definition: classic_lazy_connect.h:48
stdx::expected< Processor::Result, std::error_code > fallback_to_write()
Definition: classic_lazy_connect.cc:821
stdx::expected< Processor::Result, std::error_code > authenticated()
Definition: classic_lazy_connect.cc:423
stdx::expected< Processor::Result, std::error_code > set_vars_done()
Definition: classic_lazy_connect.cc:568
void failed(const std::optional< classic_protocol::message::server::Error > &err)
Definition: classic_lazy_connect.h:110
stdx::expected< Processor::Result, std::error_code > connected()
the handshake part.
Definition: classic_lazy_connect.cc:320
stdx::expected< Processor::Result, std::error_code > set_server_option_done()
Definition: classic_lazy_connect.cc:610
stdx::expected< Processor::Result, std::error_code > process() override
Definition: classic_lazy_connect.cc:223
stdx::expected< Processor::Result, std::error_code > wait_gtid_executed()
Definition: classic_lazy_connect.cc:730
stdx::expected< Processor::Result, std::error_code > fetch_sys_vars_done()
Definition: classic_lazy_connect.cc:671
stdx::expected< Processor::Result, std::error_code > set_trx_characteristics()
Definition: classic_lazy_connect.cc:865
std::function< void(const classic_protocol::message::server::Error &err)> on_error_
Definition: classic_lazy_connect.h:150
stdx::expected< Processor::Result, std::error_code > set_schema_done()
Definition: classic_lazy_connect.cc:706
TraceEvent * trace_event_authenticate_
Definition: classic_lazy_connect.h:165
std::string trx_stmt_
Definition: classic_lazy_connect.h:161
stdx::expected< Processor::Result, std::error_code > wait_gtid_executed_done()
Definition: classic_lazy_connect.cc:773
Stage stage_
Definition: classic_lazy_connect.h:143
stdx::expected< Processor::Result, std::error_code > pool_or_close()
Definition: classic_lazy_connect.cc:796
TraceEvent * trace_event_set_schema_
Definition: classic_lazy_connect.h:168
TraceEvent * trace_event_fallback_to_write_
Definition: classic_lazy_connect.h:171
RouterRequireFetcher::Result required_connection_attributes_fetcher_result_
Definition: classic_lazy_connect.h:147
stdx::expected< Processor::Result, std::error_code > set_schema()
Definition: classic_lazy_connect.cc:682
bool already_fallback_
Definition: classic_lazy_connect.h:153
bool retry_connect_
Definition: classic_lazy_connect.h:152
bool in_handshake_
Definition: classic_lazy_connect.h:145
stdx::expected< Processor::Result, std::error_code > fetch_sys_vars()
Definition: classic_lazy_connect.cc:628
std::optional< classic_protocol::message::server::Error > failed_
Definition: classic_lazy_connect.h:159
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:62
TraceEvent * trace_event_wait_gtid_executed_
Definition: classic_lazy_connect.h:169
TraceEvent * trace_event_set_trx_characteristics_
Definition: classic_lazy_connect.h:172
std::chrono::steady_clock::time_point started_
Definition: classic_lazy_connect.h:156
stdx::expected< Processor::Result, std::error_code > send_auth_ok()
Definition: classic_lazy_connect.cc:972
stdx::expected< Processor::Result, std::error_code > fetch_user_attrs_done()
Definition: classic_lazy_connect.cc:944
TraceEvent * trace_event_set_vars_
Definition: classic_lazy_connect.h:166
TraceEvent * trace_event_fetch_sys_vars_
Definition: classic_lazy_connect.h:167
stdx::expected< Processor::Result, std::error_code > set_trx_characteristics_done()
Definition: classic_lazy_connect.cc:905
stdx::expected< Processor::Result, std::error_code > set_server_option()
Definition: classic_lazy_connect.cc:582
Stage stage() const
Definition: classic_lazy_connect.h:101
Stage
Definition: classic_lazy_connect.h:72
stdx::expected< Processor::Result, std::error_code > connect()
Definition: classic_lazy_connect.cc:289
TraceEvent * trace_event_check_read_only_
Definition: classic_lazy_connect.h:170
stdx::expected< Processor::Result, std::error_code > fetch_user_attrs()
Definition: classic_lazy_connect.cc:925
TraceEvent * parent_event_
Definition: classic_lazy_connect.h:163
TraceEvent * trace_event_connect_
Definition: classic_lazy_connect.h:164
std::optional< classic_protocol::message::server::Error > failed() const
Definition: classic_lazy_connect.h:115
void stage(Stage stage)
Definition: classic_lazy_connect.h:100
stdx::expected< Processor::Result, std::error_code > set_vars()
Definition: classic_lazy_connect.cc:496
Definition: classic_connection_base.h:257
Definition: expected.h:943
borrowable::message::server::Error< false > Error
Definition: classic_protocol_message.h:1410
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:926
Definition: varlen_sort.h:174
Definition: classic_lazy_connect.h:103
std::optional< bool > x509
Definition: classic_lazy_connect.h:105
std::optional< std::string > subject
Definition: classic_lazy_connect.h:107
std::optional< std::string > issuer
Definition: classic_lazy_connect.h:106
std::optional< bool > ssl
Definition: classic_lazy_connect.h:104
Definition: trace_span.h:34