MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
dest_static.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025, 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_DEST_STATIC_INCLUDED
27#define ROUTING_DEST_STATIC_INCLUDED
28
29#include "destination.h" // DestinationManager
30
31/**
32 * Base class for routing strategy handler.
33 */
35 public:
36 virtual ~StrategyHandler() = default;
37
38 virtual std::optional<std::uint32_t> get_destination_index(
39 const bool last_connection_successful,
40 const std::uint32_t dest_pool_size) = 0;
41
42 protected:
43 std::uint32_t index_pos_{0};
44};
45
46/**
47 * First-available strategy. Move to next destination only if the last
48 * connection was unsuccessful. After successful connection attempt always try
49 * from the beginning.
50 */
52 public:
53 std::optional<std::uint32_t> get_destination_index(
54 const bool last_connection_successful,
55 const std::uint32_t dest_pool_size) override;
56};
57
58/**
59 * First-available strategy. Move to the next destination if the last
60 * connection was unsuccessful. Keep the current position if connection is
61 * successful (not going back, might exhaust the destination list).
62 */
64 public:
65 std::optional<std::uint32_t> get_destination_index(
66 const bool last_connection_successful,
67 const std::uint32_t dest_pool_size) override;
68};
69
70/**
71 * Round-robin strategy. Move to next destination after each connection attempt.
72 * If end of the destination candidates list is reached then loop around. If
73 * destination candidates list is exhausted (after unsuccessful connection we
74 * tried every destinaion from the list and eventually went back to the position
75 * that failed at first) then fail the connection.
76 */
78 public:
79 std::optional<std::uint32_t> get_destination_index(
80 const bool last_connection_successful,
81 const std::uint32_t dest_pool_size) override;
82
83 private:
84 bool started_{false};
85 std::optional<std::uint32_t> failed_instance_index_;
86};
87
89 public:
91 net::io_context &io_ctx,
92 MySQLRoutingContext &routing_ctx);
93
94 /** @brief Adds a destination
95 *
96 * Adds a destination using the given address and port number.
97 *
98 * @param dest destination address
99 */
100 void add(const mysql_harness::Destination &dest);
101
102 void start(const mysql_harness::PluginFuncEnv *) override;
103
104 std::vector<mysql_harness::Destination> get_destination_candidates()
105 const override {
106 return destinations_;
107 }
108
110 return false;
111 }
112
113 void handle_sockets_acceptors() override {}
114
115 std::unique_ptr<Destination> get_next_destination(
116 const routing_guidelines::Session_info &) override;
117
118 std::unique_ptr<Destination> get_last_used_destination() const override {
119 return std::make_unique<Destination>(last_destination_);
120 }
121
123 const routing_guidelines::Session_info &) override {
124 return {};
125 }
126
127 void connect_status(std::error_code ec) override;
128
129 bool has_read_write() const override { return !destinations_.empty(); }
130 bool has_read_only() const override { return !destinations_.empty(); }
131
132 private:
133 /** Get destination index based on routing strategy for static routes*/
134 std::unique_ptr<StrategyHandler> strategy_handler_;
135
136 /** @brief List of destinations */
139
140 /** @brief Protocol for the endpoint */
142};
143
144#endif // ROUTING_DEST_STATIC_INCLUDED
Type
supported protocols
Definition: base_protocol.h:32
Manage destinations for a Connection Routing.
Definition: destination.h:84
std::vector< mysql_harness::Destination > DestVector
Definition: destination.h:86
Destination to forward client connections to.
Definition: destination.h:43
First-available strategy.
Definition: dest_static.h:51
std::optional< std::uint32_t > get_destination_index(const bool last_connection_successful, const std::uint32_t dest_pool_size) override
Definition: dest_static.cc:101
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
First-available strategy.
Definition: dest_static.h:63
std::optional< std::uint32_t > get_destination_index(const bool last_connection_successful, const std::uint32_t dest_pool_size) override
Definition: dest_static.cc:112
Round-robin strategy.
Definition: dest_static.h:77
bool started_
Definition: dest_static.h:84
std::optional< std::uint32_t > get_destination_index(const bool last_connection_successful, const std::uint32_t dest_pool_size) override
Definition: dest_static.cc:120
std::optional< std::uint32_t > failed_instance_index_
Definition: dest_static.h:85
Definition: dest_static.h:88
stdx::expected< void, std::error_code > init_destinations(const routing_guidelines::Session_info &) override
Set up destination manager, prepare the destinations.
Definition: dest_static.h:122
void add(const mysql_harness::Destination &dest)
Adds a destination.
Definition: dest_static.cc:58
std::vector< mysql_harness::Destination > get_destination_candidates() const override
Get addresses of all nodes that are a possible destination candidates.
Definition: dest_static.h:104
void handle_sockets_acceptors() override
Trigger listening socket acceptors state handler based on the destination type.
Definition: dest_static.h:113
std::unique_ptr< StrategyHandler > strategy_handler_
Get destination index based on routing strategy for static routes.
Definition: dest_static.h:134
void start(const mysql_harness::PluginFuncEnv *) override
Start the destination manager.
Definition: dest_static.cc:50
StaticDestinationsManager(routing::RoutingStrategy strategy, net::io_context &io_ctx, MySQLRoutingContext &routing_ctx)
Definition: dest_static.cc:28
DestVector destinations_
List of destinations.
Definition: dest_static.h:137
bool has_read_only() const override
Check if there are read-only destinations that could be used.
Definition: dest_static.h:130
void connect_status(std::error_code ec) override
Definition: dest_static.cc:68
Destination last_destination_
Definition: dest_static.h:138
Protocol::Type protocol_
Protocol for the endpoint.
Definition: dest_static.h:141
std::unique_ptr< Destination > get_next_destination(const routing_guidelines::Session_info &) override
Get destination that should be used for connection attempt.
Definition: dest_static.cc:73
bool has_read_write() const override
Check if there are read-write destinations that could be used.
Definition: dest_static.h:129
bool refresh_destinations(const routing_guidelines::Session_info &) override
refresh destinations.
Definition: dest_static.h:109
std::unique_ptr< Destination > get_last_used_destination() const override
Get destination that was selected as a destination candidate.
Definition: dest_static.h:118
Base class for routing strategy handler.
Definition: dest_static.h:34
virtual ~StrategyHandler()=default
std::uint32_t index_pos_
Definition: dest_static.h:43
virtual std::optional< std::uint32_t > get_destination_index(const bool last_connection_successful, const std::uint32_t dest_pool_size)=0
Definition: destination.h:95
PluginFuncEnv object.
Definition: loader.h:675
Definition: io_context.h:61
Definition: expected.h:286
RoutingStrategy
Routing strategies supported by Routing plugin.
Definition: routing.h:271
Information about incoming session.
Definition: routing_guidelines.h:103