MySQL 8.4.4
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
routing_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 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_ROUTING_COMPONENT_INCLUDED
27#define MYSQLROUTER_ROUTING_COMPONENT_INCLUDED
28
29#include "mysqlrouter/routing_export.h" // ROUTING_EXPORT
30
31#include <chrono>
32#include <map>
33#include <memory>
34#include <mutex>
35#include <string>
36#include <vector>
37
38#include "connection.h"
40#include "tcp_address.h"
41
43class BaseProtocol;
44
46 public:
47 MySQLRoutingAPI() = default;
48
49 MySQLRoutingAPI(std::shared_ptr<MySQLRoutingBase> r) : r_{std::move(r)} {}
50
51 // config
52 std::string get_bind_address() const;
53 uint16_t get_bind_port() const;
54
55 std::chrono::milliseconds get_client_connect_timeout() const;
56 std::chrono::milliseconds get_destination_connect_timeout() const;
57 std::string get_destination_cluster_name() const;
58 std::string get_destination_replicaset_name() const;
59
60 int get_max_connections() const;
61 uint64_t get_max_connect_errors() const;
62
63 std::string get_name() const;
64
65 std::string get_protocol_name() const;
66 std::string get_routing_strategy() const;
67 std::string get_socket() const;
68
69 explicit operator bool() const noexcept { return r_.operator bool(); }
70
71 std::vector<std::string> get_blocked_client_hosts() const;
72
73 struct ConnData {
74 using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
75
76 ConnData() = default;
77
78 ConnData(std::string src, std::string dst, std::size_t bytes_up,
79 std::size_t bytes_down, time_point_type started,
80 time_point_type connected_to_server,
81 time_point_type last_sent_to_server,
82 time_point_type last_received_from_server)
83 : src(std::move(src)),
84 dst(std::move(dst)),
85 bytes_up(bytes_up),
86 bytes_down(bytes_down),
88 connected_to_server(connected_to_server),
89 last_sent_to_server(last_sent_to_server),
90 last_received_from_server(last_received_from_server) {}
91
92 std::string src;
93 std::string dst;
94
95 std::size_t bytes_up;
96 std::size_t bytes_down;
97
102 };
103
104 std::vector<ConnData> get_connections() const;
105
106 // status
107 int get_active_connections() const;
108 int get_total_connections() const;
109
110 std::vector<mysql_harness::TCPAddress> get_destinations() const;
111
112 void start_accepting_connections();
113 void restart_accepting_connections();
114
115 bool is_accepting_connections() const;
116
117 void stop_socket_acceptors();
118
119 bool is_running() const;
120
121 private:
122 std::shared_ptr<MySQLRoutingBase> r_;
123};
124
126 public:
127 static MySQLRoutingComponent &get_instance();
128
129 void deinit();
130
131 void init(const mysql_harness::Config &config);
132
133 void register_route(const std::string &name,
134 std::shared_ptr<MySQLRoutingBase> srv);
135
136 void erase(const std::string &name);
137
138 MySQLRoutingAPI api(const std::string &name);
139
140 uint64_t current_total_connections();
141 uint64_t max_total_connections() const { return max_total_connections_; }
142
143 MySQLRoutingConnectionBase *get_connection(const std::string &ep);
144
145 std::vector<std::string> route_names() const;
146
147 private:
148 // disable copy, as we are a single-instance
150 void operator=(MySQLRoutingComponent const &) = delete;
151
152 std::mutex routes_mu_;
153 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
154
155 uint64_t max_total_connections_{0};
156
158};
159
160#endif
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:580
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: base_protocol.h:29
Definition: routing_component.h:45
std::shared_ptr< MySQLRoutingBase > r_
Definition: routing_component.h:122
MySQLRoutingAPI(std::shared_ptr< MySQLRoutingBase > r)
Definition: routing_component.h:49
MySQLRoutingAPI()=default
Facade to avoid a tight coupling between Routing component and actual routing endpoint implementation...
Definition: mysql_routing_base.h:41
Definition: routing_component.h:125
MySQLRoutingComponent(MySQLRoutingComponent const &)=delete
std::map< std::string, std::weak_ptr< MySQLRoutingBase > > routes_
Definition: routing_component.h:153
uint64_t max_total_connections() const
Definition: routing_component.h:141
MySQLRoutingComponent()=default
void operator=(MySQLRoutingComponent const &)=delete
std::mutex routes_mu_
Definition: routing_component.h:152
Definition: connection.h:44
Configuration.
Definition: config_parser.h:253
static int get_connection(MEM_ROOT *mem_root, FEDERATED_SHARE *share)
Definition: ha_federated.cc:608
net::ip::tcp::socket * get_socket(net::ip::tcp::socket *s)
Definition: connection.h:66
bool is_running(const PluginFuncEnv *env) noexcept
Definition: loader.cc:203
RoutingStrategy get_routing_strategy(const std::string &value)
Returns RoutingStrategy for its literal representation.
Definition: routing.cc:87
Definition: srv0dynamic_procedures.h:48
Definition: gcs_xcom_synode.h:64
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
#define ROUTING_EXPORT
Definition: routing_export.h:15
case opt name
Definition: sslopt-case.h:29
Definition: routing_component.h:73
std::size_t bytes_up
Definition: routing_component.h:95
time_point_type last_sent_to_server
Definition: routing_component.h:100
time_point_type connected_to_server
Definition: routing_component.h:99
std::string dst
Definition: routing_component.h:93
time_point_type started
Definition: routing_component.h:98
std::string src
Definition: routing_component.h:92
std::chrono::time_point< std::chrono::system_clock > time_point_type
Definition: routing_component.h:74
std::size_t bytes_down
Definition: routing_component.h:96
time_point_type last_received_from_server
Definition: routing_component.h:101
ConnData(std::string src, std::string dst, std::size_t bytes_up, std::size_t bytes_down, time_point_type started, time_point_type connected_to_server, time_point_type last_sent_to_server, time_point_type last_received_from_server)
Definition: routing_component.h:78
ulong get_max_connections(void)
Get max number of connections.
Definition: sql_thd_api.cc:302
static int started(pax_machine *p)
Definition: xcom_base.cc:757