MySQL 9.1.0
Source Code Documentation
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 std::string src;
76 std::string dst;
77
78 std::size_t bytes_up;
79 std::size_t bytes_down;
80
85 };
86
87 std::vector<ConnData> get_connections() const;
88
89 // status
90 int get_active_connections() const;
91 int get_total_connections() const;
92
93 std::vector<mysql_harness::TCPAddress> get_destinations() const;
94
95 void start_accepting_connections();
96 void restart_accepting_connections();
97
98 bool is_accepting_connections() const;
99
100 void stop_socket_acceptors();
101
102 bool is_running() const;
103
104 private:
105 std::shared_ptr<MySQLRoutingBase> r_;
106};
107
109 public:
110 static MySQLRoutingComponent &get_instance();
111
112 void deinit();
113
114 void init(const mysql_harness::Config &config);
115
116 void register_route(const std::string &name,
117 std::shared_ptr<MySQLRoutingBase> srv);
118
119 void erase(const std::string &name);
120
121 MySQLRoutingAPI api(const std::string &name);
122
123 uint64_t current_total_connections();
124 uint64_t max_total_connections() const { return max_total_connections_; }
125
126 MySQLRoutingConnectionBase *get_connection(const std::string &ep);
127
128 std::vector<std::string> route_names() const;
129
130 private:
131 // disable copy, as we are a single-instance
133 void operator=(MySQLRoutingComponent const &) = delete;
134
135 std::mutex routes_mu_;
136 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
137
138 uint64_t max_total_connections_{0};
139
141};
142
143#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:105
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:108
MySQLRoutingComponent(MySQLRoutingComponent const &)=delete
std::map< std::string, std::weak_ptr< MySQLRoutingBase > > routes_
Definition: routing_component.h:136
uint64_t max_total_connections() const
Definition: routing_component.h:124
MySQLRoutingComponent()=default
void operator=(MySQLRoutingComponent const &)=delete
std::mutex routes_mu_
Definition: routing_component.h:135
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:71
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:78
time_point_type last_sent_to_server
Definition: routing_component.h:83
time_point_type connected_to_server
Definition: routing_component.h:82
std::string dst
Definition: routing_component.h:76
time_point_type started
Definition: routing_component.h:81
std::string src
Definition: routing_component.h:75
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:79
time_point_type last_received_from_server
Definition: routing_component.h:84
ulong get_max_connections(void)
Get max number of connections.
Definition: sql_thd_api.cc:314