MySQL 8.3.0
Source Code Documentation
destination.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2023, 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#include "mysqlrouter/datatypes.h" // ServerMode
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 /**
85 * server-mode of the destination.
86 *
87 * may be: unavailable, read-only or read-write.
88 */
91 }
92
93 private:
94 const std::string id_;
95 const std::string hostname_;
96 const uint16_t port_;
97};
98
99/**
100 * A forward iterable container of destinations.
101 *
102 * a PRIMARY destination set won't be failover from.
103 *
104 * @see RouteDestination::refresh_destinations()
105 */
107 public:
108 using value_type = std::unique_ptr<Destination>;
109 using container_type = std::list<value_type>;
110 using iterator = typename container_type::iterator;
111 using const_iterator = typename container_type::const_iterator;
112 using size_type = typename container_type::size_type;
113
114 iterator begin() { return destinations_.begin(); }
115 const_iterator begin() const { return destinations_.begin(); }
116 iterator end() { return destinations_.end(); }
117 const_iterator end() const { return destinations_.end(); }
118
119 /**
120 * emplace a Destination at the back of the container.
121 */
122 // clang-format off
123 template <class... Args>
124 auto emplace_back(Args &&... args) {
125 return destinations_.emplace_back(std::forward<Args>(args)...);
126 }
127 // clang-format on
128
129 void push_back(value_type &&v) { destinations_.push_back(std::move(v)); }
130
131 /**
132 * check if destination container is empty.
133 *
134 * @retval true if container is empty.
135 */
136 bool empty() const { return destinations_.empty(); }
137
138 /**
139 * clear all values.
140 */
141 void clear() { destinations_.clear(); }
142
143 /**
144 * number of destinations.
145 */
146 size_type size() const { return destinations_.size(); }
147
148 /**
149 * Check if we already used the primaries and don't want to fallback.
150 *
151 * @retval true primaries already used
152 * @retval false primaries are not yet used
153 */
155
156 /**
157 * Mark that the primary destinations are already used.
158 *
159 * @param p true if PRIMARY destinations are already used.
160 */
162
163 /**
164 * Check if destinations are primary destinations.
165 *
166 * @retval true destinations are primary destinations.
167 * @retval false destinations are secondary destinations.
168 */
170
171 /**
172 * Mark that the destinations are primary destinations.
173 *
174 * @param p true if desitnations are PRIMARY destinations.
175 */
177
178 private:
180
183};
184
185#endif
Destination to forward client connections to.
Definition: destination.h:42
const std::string id_
Definition: destination.h:94
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:96
virtual mysqlrouter::ServerMode server_mode() const
server-mode of the destination.
Definition: destination.h:89
std::string id() const
unique, opaque identifier of a destination.
Definition: destination.h:54
const std::string hostname_
Definition: destination.h:95
virtual bool good() const
check if the destination is "good".
Definition: destination.h:74
A forward iterable container of destinations.
Definition: destination.h:106
typename container_type::size_type size_type
Definition: destination.h:112
void clear()
clear all values.
Definition: destination.h:141
bool primary_already_used_
Definition: destination.h:181
bool is_primary_destination_
Definition: destination.h:182
container_type destinations_
Definition: destination.h:179
void set_is_primary_destination(const bool p)
Mark that the destinations are primary destinations.
Definition: destination.h:176
iterator begin()
Definition: destination.h:114
const_iterator begin() const
Definition: destination.h:115
size_type size() const
number of destinations.
Definition: destination.h:146
iterator end()
Definition: destination.h:116
const_iterator end() const
Definition: destination.h:117
typename container_type::const_iterator const_iterator
Definition: destination.h:111
void primary_already_used(const bool p)
Mark that the primary destinations are already used.
Definition: destination.h:161
bool empty() const
check if destination container is empty.
Definition: destination.h:136
bool primary_already_used() const
Check if we already used the primaries and don't want to fallback.
Definition: destination.h:154
bool is_primary_destination() const
Check if destinations are primary destinations.
Definition: destination.h:169
void push_back(value_type &&v)
Definition: destination.h:129
std::unique_ptr< Destination > value_type
Definition: destination.h:108
std::list< value_type > container_type
Definition: destination.h:109
typename container_type::iterator iterator
Definition: destination.h:110
auto emplace_back(Args &&... args)
emplace a Destination at the back of the container.
Definition: destination.h:124
const char * p
Definition: ctype-mb.cc:1234
ServerMode
Definition: datatypes.h:54
Definition: varlen_sort.h:174