MySQL 8.0.40
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 std::string get_mode() const;
63
64 std::string get_name() const;
65
66 std::string get_protocol_name() const;
67 std::string get_routing_strategy() const;
68 std::string get_socket() const;
69
70 explicit operator bool() const noexcept { return r_.operator bool(); }
71
72 std::vector<std::string> get_blocked_client_hosts() const;
73
74 struct ConnData {
75 using time_point_type = std::chrono::time_point<std::chrono::system_clock>;
76 std::string src;
77 std::string dst;
78
79 std::size_t bytes_up;
80 std::size_t bytes_down;
81
86 };
87
88 std::vector<ConnData> get_connections() const;
89
90 // status
91 int get_active_connections() const;
92 int get_total_connections() const;
93
94 std::vector<mysql_harness::TCPAddress> get_destinations() const;
95
96 void start_accepting_connections();
97 void restart_accepting_connections();
98
99 bool is_accepting_connections() const;
100
101 void stop_socket_acceptors();
102
103 bool is_running() const;
104
105 private:
106 std::shared_ptr<MySQLRoutingBase> r_;
107};
108
110 public:
111 static MySQLRoutingComponent &get_instance();
112
113 void deinit();
114
115 void init(const mysql_harness::Config &config);
116
117 void register_route(const std::string &name,
118 std::shared_ptr<MySQLRoutingBase> srv);
119
120 void erase(const std::string &name);
121
122 MySQLRoutingAPI api(const std::string &name);
123
124 uint64_t current_total_connections();
125 uint64_t max_total_connections() const { return max_total_connections_; }
126
127 MySQLRoutingConnectionBase *get_connection(const std::string &ep);
128
129 std::vector<std::string> route_names() const;
130
131 static const uint64_t kDefaultMaxTotalConnections = 512;
132
133 private:
134 // disable copy, as we are a single-instance
136 void operator=(MySQLRoutingComponent const &) = delete;
137
138 std::mutex routes_mu_;
139 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
140
141 uint64_t max_total_connections_{0};
142
144};
145
146#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:106
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:109
MySQLRoutingComponent(MySQLRoutingComponent const &)=delete
std::map< std::string, std::weak_ptr< MySQLRoutingBase > > routes_
Definition: routing_component.h:139
uint64_t max_total_connections() const
Definition: routing_component.h:125
MySQLRoutingComponent()=default
void operator=(MySQLRoutingComponent const &)=delete
std::mutex routes_mu_
Definition: routing_component.h:138
Definition: connection.h:43
Configuration.
Definition: config_parser.h:253
static int get_connection(MEM_ROOT *mem_root, FEDERATED_SHARE *share)
Definition: ha_federated.cc:605
bool is_running(const PluginFuncEnv *env) noexcept
Definition: loader.cc:202
RoutingStrategy ROUTING_EXPORT get_routing_strategy(const std::string &value)
Returns RoutingStrategy for its literal representation.
Definition: routing.cc:91
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:33
Definition: routing_component.h:74
std::size_t bytes_up
Definition: routing_component.h:79
time_point_type last_sent_to_server
Definition: routing_component.h:84
time_point_type connected_to_server
Definition: routing_component.h:83
std::string dst
Definition: routing_component.h:77
time_point_type started
Definition: routing_component.h:82
std::string src
Definition: routing_component.h:76
std::chrono::time_point< std::chrono::system_clock > time_point_type
Definition: routing_component.h:75
std::size_t bytes_down
Definition: routing_component.h:80
time_point_type last_received_from_server
Definition: routing_component.h:85
ulong get_max_connections(void)
Get max number of connections.
Definition: sql_thd_api.cc:301