MySQL 9.0.0
Source Code Documentation
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 // clang-format off
124 template <class... Args>
125 auto emplace_back(Args &&... args) {
126 return destinations_.emplace_back(std::forward<Args>(args)...);
127 }
128 // clang-format on
129
130 void push_back(value_type &&v) { destinations_.push_back(std::move(v)); }
131
132 /**
133 * check if destination container is empty.
134 *
135 * @retval true if container is empty.
136 */
137 bool empty() const { return destinations_.empty(); }
138
139 /**
140 * clear all values.
141 */
142 void clear() { destinations_.clear(); }
143
144 /**
145 * number of destinations.
146 */
147 size_type size() const { return destinations_.size(); }
148
149 /**
150 * Check if we already used the primaries and don't want to fallback.
151 *
152 * @retval true primaries already used
153 * @retval false primaries are not yet used
154 */
156
157 /**
158 * Mark that the primary destinations are already used.
159 *
160 * @param p true if PRIMARY destinations are already used.
161 */
163
164 /**
165 * Check if destinations are primary destinations.
166 *
167 * @retval true destinations are primary destinations.
168 * @retval false destinations are secondary destinations.
169 */
171
172 /**
173 * Mark that the destinations are primary destinations.
174 *
175 * @param p true if desitnations are PRIMARY destinations.
176 */
178
179 private:
181
184};
185
186#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:142
bool primary_already_used_
Definition: destination.h:182
bool is_primary_destination_
Definition: destination.h:183
container_type destinations_
Definition: destination.h:180
void set_is_primary_destination(const bool p)
Mark that the destinations are primary destinations.
Definition: destination.h:177
iterator begin()
Definition: destination.h:115
const_iterator begin() const
Definition: destination.h:116
size_type size() const
number of destinations.
Definition: destination.h:147
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:162
bool empty() const
check if destination container is empty.
Definition: destination.h:137
bool primary_already_used() const
Check if we already used the primaries and don't want to fallback.
Definition: destination.h:155
bool is_primary_destination() const
Check if destinations are primary destinations.
Definition: destination.h:170
void push_back(value_type &&v)
Definition: destination.h:130
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
auto emplace_back(Args &&... args)
emplace a Destination at the back of the container.
Definition: destination.h:125
const char * p
Definition: ctype-mb.cc:1225
ServerMode
Definition: datatypes.h:55
Definition: gcs_xcom_synode.h:64