MySQL 8.3.0
Source Code Documentation
routing_component.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 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_ROUTING_COMPONENT_INCLUDED
26#define MYSQLROUTER_ROUTING_COMPONENT_INCLUDED
27
28#include "mysqlrouter/routing_export.h" // ROUTING_EXPORT
29
30#include <chrono>
31#include <map>
32#include <memory>
33#include <mutex>
34#include <string>
35#include <vector>
36
37#include "connection.h"
39#include "tcp_address.h"
40
42class BaseProtocol;
43
45 public:
46 MySQLRoutingAPI() = default;
47
48 MySQLRoutingAPI(std::shared_ptr<MySQLRoutingBase> r) : r_{std::move(r)} {}
49
50 // config
51 std::string get_bind_address() const;
52 uint16_t get_bind_port() const;
53
54 std::chrono::milliseconds get_client_connect_timeout() const;
55 std::chrono::milliseconds get_destination_connect_timeout() const;
56 std::string get_destination_cluster_name() const;
57 std::string get_destination_replicaset_name() const;
58
59 int get_max_connections() const;
60 uint64_t get_max_connect_errors() const;
61 std::string get_mode() 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 static const uint64_t kDefaultMaxTotalConnections = 512;
131
132 private:
133 // disable copy, as we are a single-instance
135 void operator=(MySQLRoutingComponent const &) = delete;
136
137 std::mutex routes_mu_;
138 std::map<std::string, std::weak_ptr<MySQLRoutingBase>> routes_;
139
140 uint64_t max_total_connections_{0};
141
143};
144
145#endif
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:579
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Definition: base_protocol.h:28
Definition: routing_component.h:44
std::shared_ptr< MySQLRoutingBase > r_
Definition: routing_component.h:105
MySQLRoutingAPI(std::shared_ptr< MySQLRoutingBase > r)
Definition: routing_component.h:48
MySQLRoutingAPI()=default
Facade to avoid a tight coupling between Routing component and actual routing endpoint implementation...
Definition: mysql_routing_base.h:40
Definition: routing_component.h:108
MySQLRoutingComponent(MySQLRoutingComponent const &)=delete
std::map< std::string, std::weak_ptr< MySQLRoutingBase > > routes_
Definition: routing_component.h:138
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:137
Definition: connection.h:42
Configuration.
Definition: config_parser.h:252
static int get_connection(MEM_ROOT *mem_root, FEDERATED_SHARE *share)
Definition: ha_federated.cc:607
bool is_running(const PluginFuncEnv *env) noexcept
Definition: loader.cc:201
RoutingStrategy ROUTING_EXPORT get_routing_strategy(const std::string &value)
Returns RoutingStrategy for its literal representation.
Definition: routing.cc:110
Mode ROUTING_EXPORT get_mode(const std::string &value)
Returns Mode for its literal representation.
Definition: routing.cc:61
Definition: srv0dynamic_procedures.h:47
Definition: varlen_sort.h:174
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
#define ROUTING_EXPORT
Definition: routing_export.h:15
case opt name
Definition: sslopt-case.h:32
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:301