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