MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
connection_container.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 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 ROUTING_CONNECTION_CONTAINER_INCLUDED
27#define ROUTING_CONNECTION_CONTAINER_INCLUDED
28
29#include <algorithm>
30#include <functional>
31#include <map>
32#include <memory>
33#include <mutex>
34#include <vector>
35
36#include "connection.h"
39
41
42/**
43 * @brief Basic Concurrent Map
44 *
45 * The concurrent_map is a hash-map, with fixed number of buckets.
46 * The number of buckets can be specified in constructor parameter
47 * (num_buckets), by default is set to 23.
48 */
49template <typename Key, typename Value, typename Hash = std::hash<Key>>
51 public:
52 using key_type = Key;
54 using hash_type = Hash;
56
57 concurrent_map(unsigned num_buckets = kDefaultNumberOfBucket,
58 const Hash &hasher = Hash())
59 : buckets_(num_buckets), hasher_(hasher) {}
60
61 concurrent_map(const concurrent_map &other) = delete;
62 concurrent_map &operator=(const concurrent_map &other) = delete;
63
64 template <typename Predicate>
65 void for_one(const Key &key, Predicate &p) {
67 }
68
69 template <typename Predicate>
71 for (auto &each_bucket : buckets_) {
72 each_bucket.for_each(p);
73 }
74 }
75
76 void put(const Key &key, Value &&value) {
77 get_bucket(key).put(key, std::move(value));
78 }
79
80 void erase(const Key &key) { get_bucket(key).erase(key); }
81
82 std::size_t size() const {
83 std::size_t result{0};
84 for (auto &each_bucket : buckets_) {
85 result += each_bucket.size();
86 }
87 return result;
88 }
89
90 bool empty() const {
91 for (const auto &each_bucket : buckets_) {
92 if (!each_bucket.empty()) return false;
93 }
94 return true;
95 }
96
97 private:
98 static const unsigned kDefaultNumberOfBucket = 127;
99
100 class Bucket {
101 public:
102 void put(const Key &key, Value &&value) {
103 std::lock_guard<std::mutex> lock(data_mutex_);
104 data_.emplace(key, std::move(value));
105 }
106
107 void erase(const Key &key) {
108 std::lock_guard<std::mutex> lock(data_mutex_);
109 data_.erase(key);
110 }
111
112 template <typename Predicate>
113 void for_one(const Key &key, Predicate &p) {
114 std::lock_guard<std::mutex> lock(data_mutex_);
115 const ConstBucketIterator found = data_.find(key);
116 if (found != data_.end()) p(found->second);
117 }
118
119 template <typename Predicate>
121 std::lock_guard<std::mutex> lock(data_mutex_);
122 std::for_each(data_.begin(), data_.end(), p);
123 }
124
125 std::size_t size() const {
126 std::lock_guard<std::mutex> lock(data_mutex_);
127 return data_.size();
128 }
129
130 bool empty() const {
131 std::lock_guard<std::mutex> lock(data_mutex_);
132 return data_.empty();
133 }
134
135 private:
136 using BucketData = std::map<Key, Value>;
137 using BucketIterator = typename BucketData::iterator;
138 using ConstBucketIterator = typename BucketData::const_iterator;
139
141 mutable std::mutex data_mutex_;
142 };
143
144 std::vector<Bucket> buckets_;
146
148 const std::size_t bucket_index = hasher_(key) % buckets_.size();
149 return buckets_[bucket_index];
150 }
151
152 const Bucket &get_bucket(const Key &key) const {
153 const std::size_t bucket_index = hasher_(key) % buckets_.size();
154 return buckets_[bucket_index];
155 }
156};
157
158/**
159 * @brief container for connections to MySQL Server.
160 *
161 * When thread of execution for connection to MySQL Server is completed, it
162 * should call remove_connection to remove itself from connection container.
163 */
166 std::shared_ptr<MySQLRoutingConnectionBase>>
168
169 public:
171
172 std::vector<ConnData> get_all_connections_info() {
173 std::vector<ConnData> connection_datas;
174
175 connections_.for_each(
176 [&connection_datas](const decltype(connections_)::value_type &conn) {
177 const auto stats = conn.second->get_stats();
178
179 connection_datas.emplace_back(
180 stats.client_address, stats.server_address, stats.bytes_up,
181 stats.bytes_down, stats.started, stats.connected_to_server,
182 stats.last_sent_to_server, stats.last_received_from_server);
183 });
184
185 return connection_datas;
186 }
187 /**
188 * @brief Adds new connection to container.
189 *
190 * @param connection The connection to MySQL server
191 */
192 void add_connection(std::shared_ptr<MySQLRoutingConnectionBase> connection);
193
194 /**
195 * @brief Disconnects all connections to servers that are not allowed any
196 * longer.
197 *
198 * @param nodes Allowed servers. Connections to servers that are not in nodes
199 * are closed.
200 * @returns number of connections marked to be disconnected
201 */
202 unsigned disconnect(const AllowedNodes &nodes);
203
204 /**
205 * @brief Disconnects all connection in the ConnectionContainer.
206 */
207 void disconnect_all();
208
209 /**
210 * @brief Retrieve the connection object for the given client endpoint
211 *
212 * @param client_endpoint The endpoint string
213 * @returns the connection object, or nullptr
214 */
216 const std::string &client_endpoint);
217
218 /**
219 * @brief removes connection from container
220 *
221 * This function should be called by thread of execution when connection
222 * thread completes. Do NOT call this function before connection's thread of
223 * execution completes.
224 *
225 * @param connection The connection to remove from container
226 */
228
231 &affected_routing_sources);
232
233 /** number of active client threads. */
234 std::condition_variable connection_removed_cond_;
236
237 /**
238 * check if container is empty.
239 *
240 * as the map is concurrent, empty() only gives a reasonable result if it is
241 * ensured no other thread is currently adding connections.
242 */
243 bool empty() const { return connections_.empty(); }
244};
245
246#endif /* ROUTING_CONNECTION_CONTAINER_INCLUDED */
container for connections to MySQL Server.
Definition: connection_container.h:164
void disconnect_all()
Disconnects all connection in the ConnectionContainer.
Definition: connection_container.cc:140
void disconnect_on_routing_guidelines_update(const routing_guidelines::Routing_guidelines_engine::RouteChanges &affected_routing_sources)
Definition: connection_container.cc:154
std::vector< ConnData > get_all_connections_info()
Definition: connection_container.h:172
bool empty() const
check if container is empty.
Definition: connection_container.h:243
unsigned disconnect(const AllowedNodes &nodes)
Disconnects all connections to servers that are not allowed any longer.
Definition: connection_container.cc:55
std::mutex connection_removed_cond_m_
Definition: connection_container.h:235
std::condition_variable connection_removed_cond_
number of active client threads.
Definition: connection_container.h:234
MySQLRoutingConnectionBase * get_connection(const std::string &client_endpoint)
Retrieve the connection object for the given client endpoint.
Definition: connection_container.cc:123
void add_connection(std::shared_ptr< MySQLRoutingConnectionBase > connection)
Adds new connection to container.
Definition: connection_container.cc:33
void remove_connection(MySQLRoutingConnectionBase *connection)
removes connection from container
Definition: connection_container.cc:145
concurrent_map< MySQLRoutingConnectionBase *, std::shared_ptr< MySQLRoutingConnectionBase > > connections_
Definition: connection_container.h:167
Definition: connection.h:47
Definition: connection_container.h:100
std::mutex data_mutex_
Definition: connection_container.h:141
std::size_t size() const
Definition: connection_container.h:125
void for_each(Predicate &p)
Definition: connection_container.h:120
BucketData data_
Definition: connection_container.h:140
typename BucketData::iterator BucketIterator
Definition: connection_container.h:137
void put(const Key &key, Value &&value)
Definition: connection_container.h:102
std::map< Key, Value > BucketData
Definition: connection_container.h:136
typename BucketData::const_iterator ConstBucketIterator
Definition: connection_container.h:138
void for_one(const Key &key, Predicate &p)
Definition: connection_container.h:113
void erase(const Key &key)
Definition: connection_container.h:107
bool empty() const
Definition: connection_container.h:130
Basic Concurrent Map.
Definition: connection_container.h:50
concurrent_map(const concurrent_map &other)=delete
concurrent_map(unsigned num_buckets=kDefaultNumberOfBucket, const Hash &hasher=Hash())
Definition: connection_container.h:57
void erase(const Key &key)
Definition: connection_container.h:80
std::size_t size() const
Definition: connection_container.h:82
Bucket & get_bucket(const Key &key)
Definition: connection_container.h:147
static const unsigned kDefaultNumberOfBucket
Definition: connection_container.h:98
Value mapped_type
Definition: connection_container.h:53
void for_each(Predicate p)
Definition: connection_container.h:70
void for_one(const Key &key, Predicate &p)
Definition: connection_container.h:65
concurrent_map & operator=(const concurrent_map &other)=delete
Hash hasher_
Definition: connection_container.h:145
void put(const Key &key, Value &&value)
Definition: connection_container.h:76
const Bucket & get_bucket(const Key &key) const
Definition: connection_container.h:152
bool empty() const
Definition: connection_container.h:90
Hash hash_type
Definition: connection_container.h:54
typename std::map< Key, Value >::value_type value_type
Definition: connection_container.h:55
std::vector< Bucket > buckets_
Definition: connection_container.h:144
Key key_type
Definition: connection_container.h:52
const char * p
Definition: ctype-mb.cc:1225
std::vector< AvailableDestination > AllowedNodes
Definition: destination_status_types.h:62
void for_each(const Shards< COUNT > &shards, Function &&f) noexcept
Iterate over the shards.
Definition: ut0counter.h:323
uint16_t value_type
Definition: vt100.h:184
std::unordered_map< Key, CHARSET_INFO * > Hash
Definition: collations_internal.cc:548
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:108
static std::mutex lock
Definition: net_ns.cc:56
struct result result
Definition: result.h:34
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: routing_component.h:73
A filter of some sort that is not a join condition (those are stored in JoinPredicate objects).
Definition: access_path.h:133
Definition: result.h:30
Type for names of Routes changed during routing guidelines document update.
Definition: routing_guidelines.h:322
Definition: mysqlslap.cc:242