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