MySQL 8.4.4
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
destination.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 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 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 MYSQLROUTER_DESTINATION_INCLUDED
27#define MYSQLROUTER_DESTINATION_INCLUDED
28
29#include <cstddef> // uint16_t
30#include <list> // list
31#include <memory> // unique_ptr
32#include <string> // string
33#include <system_error> // error_code
34
35#include "mysqlrouter/datatypes.h" // ServerMode
36#include "tcp_address.h"
37
38/**
39 * Destination to forward client connections to.
40 *
41 * It is used between the RouteDestination implementations and MySQLRouting
42 */
44 public:
45 Destination(std::string id, std::string hostname, uint16_t port)
46 : id_{std::move(id)}, hostname_{std::move(hostname)}, port_{port} {}
47
48 virtual ~Destination() = default;
49
50 /**
51 * unique, opaque identifier of a destination.
52 *
53 * used by connection container to find allowed destinations.
54 */
55 std::string id() const { return id_; }
56
57 /**
58 * hostname to connect to.
59 */
60 std::string hostname() const { return hostname_; }
61
62 /**
63 * TCP port to connect to.
64 */
65 uint16_t port() const noexcept { return port_; }
66
67 /**
68 * check if the destination is "good".
69 *
70 * If the destination is not "good", it will be skipped by MySQLRouting.
71 *
72 * @retval false if destination is known to be bad
73 * @retval true otherwise
74 */
75 virtual bool good() const { return true; }
76
77 /**
78 * status of the last failed connect().
79 *
80 * called by MySQLRouting after a connect() to all addresses
81 * of the destination failed.
82 */
83 virtual void connect_status(std::error_code /* ec */) {}
84
85 /**
86 * server-mode of the destination.
87 *
88 * may be: unavailable, read-only or read-write.
89 */
92 }
93
94 private:
95 const std::string id_;
96 const std::string hostname_;
97 const uint16_t port_;
98};
99
100/**
101 * A forward iterable container of destinations.
102 *
103 * a PRIMARY destination set won't be failover from.
104 *
105 * @see RouteDestination::refresh_destinations()
106 */
108 public:
109 using value_type = std::unique_ptr<Destination>;
110 using container_type = std::list<value_type>;
111 using iterator = typename container_type::iterator;
112 using const_iterator = typename container_type::const_iterator;
113 using size_type = typename container_type::size_type;
114
115 iterator begin() { return destinations_.begin(); }
116 const_iterator begin() const { return destinations_.begin(); }
117 iterator end() { return destinations_.end(); }
118 const_iterator end() const { return destinations_.end(); }
119
120 /**
121 * emplace a Destination at the back of the container.
122 */
123 template <class... Args>
124 auto emplace_back(Args &&...args) {
125 return destinations_.emplace_back(std::forward<Args>(args)...);
126 }
127
128 void push_back(value_type &&v) { destinations_.push_back(std::move(v)); }
129
130 /**
131 * check if destination container is empty.
132 *
133 * @retval true if container is empty.
134 */
135 bool empty() const { return destinations_.empty(); }
136
137 /**
138 * clear all values.
139 */
140 void clear() { destinations_.clear(); }
141
142 /**
143 * number of destinations.
144 */
145 size_type size() const { return destinations_.size(); }
146
147 /**
148 * Check if we already used the primaries and don't want to fallback.
149 *
150 * @retval true primaries already used
151 * @retval false primaries are not yet used
152 */
154
155 /**
156 * Mark that the primary destinations are already used.
157 *
158 * @param p true if PRIMARY destinations are already used.
159 */
161
162 /**
163 * Check if destinations are primary destinations.
164 *
165 * @retval true destinations are primary destinations.
166 * @retval false destinations are secondary destinations.
167 */
169
170 /**
171 * Mark that the destinations are primary destinations.
172 *
173 * @param p true if desitnations are PRIMARY destinations.
174 */
176
177 private:
179
182};
183
184#endif
Destination to forward client connections to.
Definition: destination.h:43
const std::string id_
Definition: destination.h:95
virtual ~Destination()=default
virtual void connect_status(std::error_code)
status of the last failed connect().
Definition: destination.h:83
Destination(std::string id, std::string hostname, uint16_t port)
Definition: destination.h:45
uint16_t port() const noexcept
TCP port to connect to.
Definition: destination.h:65
std::string hostname() const
hostname to connect to.
Definition: destination.h:60
const uint16_t port_
Definition: destination.h:97
virtual mysqlrouter::ServerMode server_mode() const
server-mode of the destination.
Definition: destination.h:90
std::string id() const
unique, opaque identifier of a destination.
Definition: destination.h:55
const std::string hostname_
Definition: destination.h:96
virtual bool good() const
check if the destination is "good".
Definition: destination.h:75
A forward iterable container of destinations.
Definition: destination.h:107
typename container_type::size_type size_type
Definition: destination.h:113
void clear()
clear all values.
Definition: destination.h:140
bool primary_already_used_
Definition: destination.h:180
bool is_primary_destination_
Definition: destination.h:181
container_type destinations_
Definition: destination.h:178
void set_is_primary_destination(const bool p)
Mark that the destinations are primary destinations.
Definition: destination.h:175
iterator begin()
Definition: destination.h:115
const_iterator begin() const
Definition: destination.h:116
size_type size() const
number of destinations.
Definition: destination.h:145
iterator end()
Definition: destination.h:117
const_iterator end() const
Definition: destination.h:118
typename container_type::const_iterator const_iterator
Definition: destination.h:112
void primary_already_used(const bool p)
Mark that the primary destinations are already used.
Definition: destination.h:160
bool empty() const
check if destination container is empty.
Definition: destination.h:135
auto emplace_back(Args &&...args)
emplace a Destination at the back of the container.
Definition: destination.h:124
bool primary_already_used() const
Check if we already used the primaries and don't want to fallback.
Definition: destination.h:153
bool is_primary_destination() const
Check if destinations are primary destinations.
Definition: destination.h:168
void push_back(value_type &&v)
Definition: destination.h:128
std::unique_ptr< Destination > value_type
Definition: destination.h:109
std::list< value_type > container_type
Definition: destination.h:110
typename container_type::iterator iterator
Definition: destination.h:111
const char * p
Definition: ctype-mb.cc:1235
ServerMode
Definition: datatypes.h:50
Definition: gcs_xcom_synode.h:64