MySQL 8.0.30
Source Code Documentation
routing_common_unreachable_destinations.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2022, 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 MYSQLROUTER_ROUTING_COMMON_UNREACHABLE_DESTINATIONS_INCLUDED
26#define MYSQLROUTER_ROUTING_COMMON_UNREACHABLE_DESTINATIONS_INCLUDED
27
28#include <chrono>
29#include <mutex>
30#include <vector>
31
32#include "destination.h"
37#include "tcp_address.h"
38
40
41/**
42 * Information about unreachable destination candidates that is shared between
43 * routing plugin instances.
44 *
45 * Quarantined destinations will not be used for
46 * routing purposes. Each unreachable destination candidate is periodically
47 * probed for availability and removed from the unreachable destination
48 * candidate set if it became available.
49 */
51 public:
52 using AllowedNodes = std::vector<AvailableDestination>;
53
54 /**
55 * Initialize the unreachable destination candidate mechanism.
56 *
57 * It will set up:
58 * - routing plugin instances callbacks used for probing/updating the
59 * unreachable destinations
60 * - harness context variable used for starting/stopping the routing listening
61 * sockets
62 * - quarantine_refresh_interval Used for unreachable destination candidates
63 * availability checks.
64 *
65 * @param[in] instance_name MySQLRouting plugin instance name.
66 * @param[in] quarantine_refresh_interval Time in seconds that will be used
67 * for unreachable destination candidate availability checks.
68 */
69 void init(const std::string &instance_name,
70 std::chrono::seconds quarantine_refresh_interval);
71
72 /**
73 * Add unreachable destination candidate to quarantine.
74 *
75 * If the destination candidate is not quarantine yet it will starting
76 * the async handler for it, otherwise it will just update the referencing
77 * routing plugins list.
78 *
79 * @param[in] dest Unreachable destination candidate address.
80 */
82 const mysql_harness::TCPAddress &dest);
83
84 /**
85 * Query the quarantined destination candidates set and check if the given
86 * destination candidate is quarantined.
87 *
88 * @param[in] dest Destination candidate address.
89 * @returns true if the destination candidate is quarantined, false otherwise.
90 */
92
93 /**
94 * Refresh the quarantined destination candidates list on metadata refresh.
95 *
96 * 1) if the destination candidates list got updated we have to go through the
97 * quarantined destinations and check if there are still routing plugins that
98 * references them.
99 * 2) for each destination returned in the metadata (which is available from
100 * the md perspective) check if it is still unreachable and should be
101 * quarantined.
102 *
103 * @param[in] instance_name Routing plugin instance name.
104 * @param[in] nodes_changed_on_md_refresh Information if the destination
105 * candidates have been updated for the given routing plugin.
106 * @param[in] available_destinations List of destination candidates that are
107 * available for the given routing plugin after metadata refresh.
108 */
110 const std::string &instance_name, const bool nodes_changed_on_md_refresh,
111 const std::vector<AvailableDestination> &available_destinations);
112
113 /**
114 * Stop all async operations and clear the quarantine list.
115 */
116 void stop_quarantine();
117
118 private:
119 /**
120 * Async handler responsible of periodic checks for destination candidate
121 * availability.
122 *
123 * @param[in] ec Result of async operation.
124 * @param[in] dest Destination candidate address.
125 */
126 void quarantine_handler(const std::error_code &ec,
127 const mysql_harness::TCPAddress &dest);
128
129 /**
130 * Go through all routing instances and check if there are routing plugins
131 * which have all destination candidates added to quarantine, if so lets
132 * close the listening socket of such routing instances.
133 */
135
136 /**
137 * For a given destination get names of all routing instances that references
138 * it.
139 *
140 * @param[in] destination Destination candidate address.
141 * @returns List of referencing routing instance names
142 */
143 std::vector<std::string> get_referencing_routing_instances(
144 const mysql_harness::TCPAddress &destination);
145
146 /**
147 * On metadata refresh we got a destination candidates list that is reported
148 * to be available (from the metadata perspective). Go through this list and
149 * check if any of the destination candidate is quarantined, if so verify
150 * if it is still unreachable and should be kept in quarantine.
151 *
152 * @param[in] destination_list Destination candidates reported to be
153 * available.
154 */
155 void update_destinations_state(const AllowedNodes &destination_list);
156
157 /**
158 * If destination list of a routing instance has changed it is possible that
159 * some destinations are no longer referenced by any routing instance. In
160 * that case we should scan the quarantine list and remove those destinations.
161 *
162 * @param[in] instance_name Routing instance name that got destination
163 * candidates list update.
164 * @param[in] routing_new_destinations List of new destination candidates for
165 * the given routing instance.
166 */
167 void drop_stray_destinations(const std::string &instance_name,
168 const AllowedNodes &routing_new_destinations);
169
170 /**
171 * Class representing a single entry (destination) in quarantined destination
172 * set.
173 *
174 * Each destination has its own timer responsible for doing asynchronous
175 * availability checks and a list of names of routing instances that currently
176 * reference this destination candidate.
177 */
181 std::vector<std::string> referencing_instances)
182 : address_{std::move(addr)},
183 timer_{std::move(timer)},
184 referencing_routing_instances_{std::move(referencing_instances)} {}
185
187
189 default;
193 const Unreachable_destination_candidate &) = delete;
195 const Unreachable_destination_candidate &) = delete;
196
199 std::vector<std::string> referencing_routing_instances_;
200 };
201
202 std::chrono::milliseconds kQuarantinedConnectTimeout{1000};
206 std::vector<Unreachable_destination_candidate>
210 std::vector<std::string> routing_instances_;
211 std::atomic<bool> stopped_{false};
212};
213
214#endif // MYSQLROUTER_ROUTING_COMMON_UNREACHABLE_DESTINATIONS_INCLUDED
static IoComponent & get_instance()
Definition: io_component.cc:146
net::io_context & io_context()
get ref to the io_context.
Definition: io_component.cc:57
Information about unreachable destination candidates that is shared between routing plugin instances.
Definition: routing_common_unreachable_destinations.h:50
void init(const std::string &instance_name, std::chrono::seconds quarantine_refresh_interval)
Initialize the unreachable destination candidate mechanism.
Definition: routing_common_unreachable_destinations.cc:96
std::chrono::milliseconds kQuarantinedConnectTimeout
Definition: routing_common_unreachable_destinations.h:202
void quarantine_handler(const std::error_code &ec, const mysql_harness::TCPAddress &dest)
Async handler responsible of periodic checks for destination candidate availability.
Definition: routing_common_unreachable_destinations.cc:171
void refresh_quarantine(const std::string &instance_name, const bool nodes_changed_on_md_refresh, const std::vector< AvailableDestination > &available_destinations)
Refresh the quarantined destination candidates list on metadata refresh.
Definition: routing_common_unreachable_destinations.cc:151
bool is_quarantined(const mysql_harness::TCPAddress &dest)
Query the quarantined destination candidates set and check if the given destination candidate is quar...
Definition: routing_common_unreachable_destinations.cc:141
void update_destinations_state(const AllowedNodes &destination_list)
On metadata refresh we got a destination candidates list that is reported to be available (from the m...
Definition: routing_common_unreachable_destinations.cc:253
std::vector< Unreachable_destination_candidate > quarantined_destination_candidates_
Definition: routing_common_unreachable_destinations.h:207
std::mutex routing_instances_mutex_
Definition: routing_common_unreachable_destinations.h:209
std::vector< std::string > get_referencing_routing_instances(const mysql_harness::TCPAddress &destination)
For a given destination get names of all routing instances that references it.
Definition: routing_common_unreachable_destinations.cc:236
void add_destination_candidate_to_quarantine(const mysql_harness::TCPAddress &dest)
Add unreachable destination candidate to quarantine.
Definition: routing_common_unreachable_destinations.cc:111
void stop_socket_acceptors_on_all_nodes_quarantined()
Go through all routing instances and check if there are routing plugins which have all destination ca...
Definition: routing_common_unreachable_destinations.cc:219
std::mutex unreachable_destinations_init_mutex_
Definition: routing_common_unreachable_destinations.h:208
std::chrono::seconds quarantine_interval_
Definition: routing_common_unreachable_destinations.h:203
std::atomic< bool > stopped_
Definition: routing_common_unreachable_destinations.h:211
std::vector< AvailableDestination > AllowedNodes
Definition: routing_common_unreachable_destinations.h:52
void drop_stray_destinations(const std::string &instance_name, const AllowedNodes &routing_new_destinations)
If destination list of a routing instance has changed it is possible that some destinations are no lo...
Definition: routing_common_unreachable_destinations.cc:269
net::io_context & io_ctx_
Definition: routing_common_unreachable_destinations.h:204
void stop_quarantine()
Stop all async operations and clear the quarantine list.
Definition: routing_common_unreachable_destinations.cc:161
std::mutex quarantine_mutex_
Definition: routing_common_unreachable_destinations.h:205
std::vector< std::string > routing_instances_
Definition: routing_common_unreachable_destinations.h:210
Defines an IP address with port number
Definition: tcp_address.h:39
Definition: timer.h:56
Definition: io_context.h:59
Definition: varlen_sort.h:183
Definition: destination.h:48
Class representing a single entry (destination) in quarantined destination set.
Definition: routing_common_unreachable_destinations.h:178
Unreachable_destination_candidate(Unreachable_destination_candidate &&)=default
net::steady_timer timer_
Definition: routing_common_unreachable_destinations.h:198
std::vector< std::string > referencing_routing_instances_
Definition: routing_common_unreachable_destinations.h:199
Unreachable_destination_candidate & operator=(Unreachable_destination_candidate &&)=default
Unreachable_destination_candidate(mysql_harness::TCPAddress addr, net::steady_timer timer, std::vector< std::string > referencing_instances)
Definition: routing_common_unreachable_destinations.h:179
Unreachable_destination_candidate & operator=(const Unreachable_destination_candidate &)=delete
mysql_harness::TCPAddress address_
Definition: routing_common_unreachable_destinations.h:197
~Unreachable_destination_candidate()
Definition: routing_common_unreachable_destinations.cc:310
Unreachable_destination_candidate(const Unreachable_destination_candidate &)=delete
double seconds()
Definition: task.cc:309