MySQL 26.10.0
Source Code Documentation
dest_static.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 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_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 std::uint32_t index() const { return index_pos_; }
43
44 protected:
45 std::uint32_t index_pos_{0};
46};
47
48/**
49 * First-available strategy. Move to next destination only if the last
50 * connection was unsuccessful. After successful connection attempt always try
51 * from the beginning.
52 */
54 public:
55 std::optional<std::uint32_t> get_destination_index(
56 const bool last_connection_successful,
57 const std::uint32_t dest_pool_size) override;
58};
59
60/**
61 * First-available strategy. Move to the next destination if the last
62 * connection was unsuccessful. Keep the current position if connection is
63 * successful (not going back, might exhaust the destination list).
64 */
66 public:
67 std::optional<std::uint32_t> get_destination_index(
68 const bool last_connection_successful,
69 const std::uint32_t dest_pool_size) override;
70};
71
72/**
73 * Round-robin strategy. Move to next destination after each connection attempt.
74 * If end of the destination candidates list is reached then loop around. If
75 * destination candidates list is exhausted (after unsuccessful connection we
76 * tried every destinaion from the list and eventually went back to the position
77 * that failed at first) then fail the connection.
78 */
80 public:
81 std::optional<std::uint32_t> get_destination_index(
82 const bool last_connection_successful,
83 const std::uint32_t dest_pool_size) override;
84
85 private:
86 bool started_{false};
87 std::optional<std::uint32_t> failed_instance_index_;
88};
89
91 public:
93 net::io_context &io_ctx,
94 MySQLRoutingContext &routing_ctx);
95
96 /** @brief Adds a destination
97 *
98 * Adds a destination using the given address and port number.
99 *
100 * @param dest destination address
101 */
102 void add(const mysql_harness::Destination &dest);
103
104 void start(const mysql_harness::PluginFuncEnv *) override;
105
106 std::vector<mysql_harness::Destination> get_destination_candidates()
107 const override {
108 return destinations_;
109 }
110
111 /** Destinations not yet skipped by next-available routing. */
113
115 return false;
116 }
117
118 void handle_sockets_acceptors() override {}
119
120 std::unique_ptr<Destination> get_next_destination(
121 const routing_guidelines::Session_info &) override;
122
123 std::unique_ptr<Destination> get_last_used_destination() const override {
124 return std::make_unique<Destination>(last_destination_);
125 }
126
128 const routing_guidelines::Session_info &) override {
129 return {};
130 }
131
132 void connect_status(std::error_code ec) override;
133
134 bool has_read_write() const override { return !destinations_.empty(); }
135 bool has_read_only() const override { return !destinations_.empty(); }
136
137 private:
138 /** Get destination index based on routing strategy for static routes*/
139 std::unique_ptr<StrategyHandler> strategy_handler_;
140
141 /** @brief List of destinations */
144
145 /** @brief Protocol for the endpoint */
147};
148
149#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:53
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:109
MySQLRoutingContext holds data used by MySQLRouting (1 per plugin instances) and MySQLRoutingConnecti...
Definition: context.h:54
First-available strategy.
Definition: dest_static.h:65
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
Round-robin strategy.
Definition: dest_static.h:79
bool started_
Definition: dest_static.h:86
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:128
std::optional< std::uint32_t > failed_instance_index_
Definition: dest_static.h:87
Definition: dest_static.h:90
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:127
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:106
void handle_sockets_acceptors() override
Trigger listening socket acceptors state handler based on the destination type.
Definition: dest_static.h:118
std::unique_ptr< StrategyHandler > strategy_handler_
Get destination index based on routing strategy for static routes.
Definition: dest_static.h:139
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:142
bool has_read_only() const override
Check if there are read-only destinations that could be used.
Definition: dest_static.h:135
void connect_status(std::error_code ec) override
Definition: dest_static.cc:76
Destination last_destination_
Definition: dest_static.h:143
Protocol::Type protocol_
Protocol for the endpoint.
Definition: dest_static.h:146
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:81
bool has_read_write() const override
Check if there are read-write destinations that could be used.
Definition: dest_static.h:134
DestVector get_remaining_destinations()
Destinations not yet skipped by next-available routing.
Definition: dest_static.cc:69
bool refresh_destinations(const routing_guidelines::Session_info &) override
refresh destinations.
Definition: dest_static.h:114
std::unique_ptr< Destination > get_last_used_destination() const override
Get destination that was selected as a destination candidate.
Definition: dest_static.h:123
Base class for routing strategy handler.
Definition: dest_static.h:34
std::uint32_t index() const
Definition: dest_static.h:42
virtual ~StrategyHandler()=default
std::uint32_t index_pos_
Definition: dest_static.h:45
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