MySQL 8.0.39
Source Code Documentation
dns_srv_data.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 Without limiting anything contained in the foregoing, this file,
16 which is part of C Driver for MySQL (Connector/C), is also subject to the
17 Universal FOSS Exception, version 1.0, a copy of which can be found at
18 http://oss.oracle.com/licenses/universal-foss-exception.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License, version 2.0, for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
28
29#ifndef DNS_SRV_DATA_H
30#define DNS_SRV_DATA_H
31
32#include <assert.h>
33#include <cstdlib>
34#include <list>
35#include <map>
36#include <string>
37
38/**
39 A RFC2782 compliant SRV records storage
40
41 Stores host/port/weight/priority entries
42 into a data structure that then allows retrieving
43 these via the Dns_srv_data::pop_next() method.
44
45 Entries can be stored by calling Dns_srv_data::add()
46 in any order.
47
48 This usage pattern is roughly as follows:
49 1. Dns_srv_data construct
50 2. one or more Dns_srv_data::add()
51 3. one or more Dns_srv_data::pop_next()
52 4. Dns_srv_data destructor
53
54 @sa mysql_real_connect_dns_srv
55*/
57 class Dns_entry {
58 std::string host_;
59 unsigned port_{0}, weight_{0};
60 unsigned long weight_sum_{0};
61
62 Dns_entry() = delete; // disable copy constructor
63
64 public:
65 Dns_entry(const std::string &host, unsigned port, unsigned weight)
67
68 unsigned port() const { return port_; }
69 std::string host() const { return host_; }
70 unsigned long weight_sum() const { return weight_sum_; }
71 void add_weight_sum(unsigned long &weight_sum) {
73 }
74 };
75 using dns_entry_list_t = std::list<Dns_entry>;
76 using dns_entry_data_t = std::map<unsigned, Dns_srv_data::dns_entry_list_t>;
78
79 public:
80 void clear() { data_.clear(); }
81 void add(const std::string &host, unsigned port, unsigned priority,
82 unsigned weight) {
83 dns_entry_data_t::iterator list = data_.find(priority);
84 if (list == data_.cend())
85 data_.emplace(priority,
87 else {
88 // RFC2782: put the 0 weight at the front, rest at the back
89 if (weight > 0)
90 list->second.emplace_back(host, port, weight);
91 else
92 list->second.emplace_front(host, port, weight);
93 }
94 }
95 bool pop_next(std::string &host, unsigned &port) {
96 if (data_.empty()) return true;
97
98 dns_entry_list_t &list = data_.begin()->second;
99 assert(!list.empty());
100
101 unsigned long sum = 0;
102 for (Dns_entry &elt : list) elt.add_weight_sum(sum);
103
104 unsigned long draw = (std::rand() * 1UL * sum) / RAND_MAX;
105
106 dns_entry_list_t::const_iterator iter = list.cbegin();
107 while (iter->weight_sum() < draw) iter++;
108 assert(iter != list.end());
109
110 host = iter->host();
111 port = iter->port();
112
113 list.erase(iter);
114 if (list.empty()) data_.erase(data_.begin());
115 return false;
116 }
117};
118
119#endif // !DNS_SRV_DATA_H
Definition: dns_srv_data.h:57
unsigned long weight_sum_
Definition: dns_srv_data.h:60
Dns_entry(const std::string &host, unsigned port, unsigned weight)
Definition: dns_srv_data.h:65
unsigned port() const
Definition: dns_srv_data.h:68
void add_weight_sum(unsigned long &weight_sum)
Definition: dns_srv_data.h:71
unsigned weight_
Definition: dns_srv_data.h:59
Dns_entry()=delete
std::string host_
Definition: dns_srv_data.h:58
std::string host() const
Definition: dns_srv_data.h:69
unsigned port_
Definition: dns_srv_data.h:59
unsigned long weight_sum() const
Definition: dns_srv_data.h:70
A RFC2782 compliant SRV records storage.
Definition: dns_srv_data.h:56
std::list< Dns_entry > dns_entry_list_t
Definition: dns_srv_data.h:75
bool pop_next(std::string &host, unsigned &port)
Definition: dns_srv_data.h:95
void add(const std::string &host, unsigned port, unsigned priority, unsigned weight)
Definition: dns_srv_data.h:81
dns_entry_data_t data_
Definition: dns_srv_data.h:77
std::map< unsigned, Dns_srv_data::dns_entry_list_t > dns_entry_data_t
Definition: dns_srv_data.h:76
void clear()
Definition: dns_srv_data.h:80
const char * host
Definition: mysqladmin.cc:59
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2878
required uint64 weight
Definition: replication_asynchronous_connection_failover.proto:35
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
required uint32 priority
Definition: replication_group_member_actions.proto:35
static ORDER * elt(const SQL_I_List< ORDER > &list, uint i)
Return element with index i from list.
Definition: window.cc:230