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