MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
DestMetadataCacheManager Class Referencefinal

#include <dest_metadata_cache.h>

Inheritance diagram for DestMetadataCacheManager:
[legend]

Public Types

enum  ServerRole { Primary , Secondary , PrimaryAndSecondary }
 
- Public Types inherited from DestinationManager
using DestVector = std::vector< mysql_harness::Destination >
 

Public Member Functions

 DestMetadataCacheManager (net::io_context &io_ctx, MySQLRoutingContext &routing_ctx, const std::string &metadata_cache, const mysqlrouter::URIQuery &query, const ServerRole role, metadata_cache::MetadataCacheAPIBase *cache_api=metadata_cache::MetadataCacheAPI::instance())
 Constructor. More...
 
 DestMetadataCacheManager (const DestMetadataCacheManager &other)=delete
 Copy constructor. More...
 
 DestMetadataCacheManager (DestMetadataCacheManager &&)=delete
 Move constructor. More...
 
DestMetadataCacheManageroperator= (const DestMetadataCacheManager &)=delete
 Copy assignment. More...
 
DestMetadataCacheManageroperator= (DestMetadataCacheManager &&)=delete
 Move assignment. More...
 
 ~DestMetadataCacheManager () override
 
void start (const mysql_harness::PluginFuncEnv *env) override
 Start the destination. More...
 
mysqlrouter::ServerMode purpose () const override
 
stdx::expected< void, std::error_code > init_destinations (const routing_guidelines::Session_info &session_info) override
 Set up destination manager, prepare the destinations. More...
 
metadata_cache::MetadataCacheAPIBasecache_api ()
 
bool refresh_destinations (const routing_guidelines::Session_info &session_info) override
 refresh destinations. More...
 
void handle_sockets_acceptors () override
 Trigger listening socket acceptors state handler based on the destination type. More...
 
std::unique_ptr< Destinationget_next_destination (const routing_guidelines::Session_info &session_info) override
 Get destination that should be used for connection attempt. More...
 
std::vector< mysql_harness::Destinationget_destination_candidates () const override
 Get addresses of all nodes that are a possible destination candidates. More...
 
void connect_status (std::error_code ec) override
 
bool has_read_write () const override
 Check if there are read-write destinations that could be used. More...
 
bool has_read_only () const override
 Check if there are read-only destinations that could be used. More...
 
routing_guidelines::Routing_guidelines_engine::RouteChanges update_routing_guidelines (const std::string &routing_guidelines_document)
 Update routing guidelines engine with a new routing guideline. More...
 
void clear_internal_state ()
 Clear internal state (indexes, last connection status etc). More...
 
- Public Member Functions inherited from DestinationManager
 DestinationManager (net::io_context &io_ctx, MySQLRoutingContext &routing_ctx)
 Default constructor. More...
 
virtual ~DestinationManager ()=default
 Destructor. More...
 
 DestinationManager (const DestinationManager &other)=delete
 
 DestinationManager (DestinationManager &&other)=delete
 
DestinationManageroperator= (const DestinationManager &other)=delete
 
DestinationManageroperator= (DestinationManager &&other)=delete
 
bool routing_guidelines_session_rand_used () const
 Check if routing guidelines uses $.session.rand as a match criterion. More...
 
routing_guidelines::Router_info get_router_info () const
 Get information about this given Router instance. More...
 
- Public Member Functions inherited from DestinationNodesStateNotifier
AllowedNodesChangeCallbacksListIterator register_allowed_nodes_change_callback (const AllowedNodesChangedCallback &clb)
 Registers the callback for notification on the change in the state if the destination nodes. More...
 
void unregister_allowed_nodes_change_callback (const AllowedNodesChangeCallbacksListIterator &it)
 Unregisters the callback registered with register_allowed_nodes_change_callback(). More...
 
void register_start_router_socket_acceptor (const StartSocketAcceptorCallback &clb)
 Registers the callback for notification that the routing socket acceptor should accept new connections. More...
 
void unregister_start_router_socket_acceptor ()
 Unregisters the callback registered with register_start_router_socket_acceptor(). More...
 
void register_stop_router_socket_acceptor (const StopSocketAcceptorCallback &clb)
 Registers the callback for notification that the routing socket acceptor should stop accepting new connections. More...
 
void unregister_stop_router_socket_acceptor ()
 Unregisters the callback registered with register_stop_router_socket_acceptor(). More...
 
void register_md_refresh_callback (const MetadataRefreshCallback &callback)
 Registers a callback that is going to be used on metadata refresh. More...
 
void unregister_md_refresh_callback ()
 Unregisters the callback registered with register_md_refresh_callback(). More...
 

Private Types

enum class  ConnectionStatus { InProgress , Failed , NotSet }
 Status of a last connection. More...
 

Private Member Functions

std::unordered_map< std::string, net::ip::addressresolve_routing_guidelines_hostnames (const std::vector< routing_guidelines::Resolve_host > &hostnames)
 Resolve hostnames used in routing guidelines document. More...
 
void prepare_destination_groups ()
 Fill each destination group with destination candidates, according to the routing guideline that is being used. More...
 
bool change_group ()
 Change destination group that is currently being used. More...
 
void validate_current_sharing_settings (std::string_view route_name, Destination *dest) const
 If the routing guideline enables the connection sharing then it validates if the sharing prerequisites are met and it could be used. More...
 
std::unique_ptr< Destinationget_next_destination_impl ()
 
void set_last_connect_successful (const bool state)
 Set information if the last connection was successful. More...
 
std::vector< routing_guidelines::Server_infoget_nodes_allowed_by_routing_guidelines () const
 Get addresses of nodes allowed by the auto-generated routing guideline. More...
 
std::vector< routing_guidelines::Server_infoget_all_nodes () const
 Get addresses of all nodes in the topology. More...
 
void init ()
 Initializes. More...
 
virtual std::vector< routing_guidelines::Server_infoget_nodes_from_topology (const metadata_cache::ClusterTopology &cluster_topology, const bool drop_all_hidden) const
 Get destination candidates details from the given topology. More...
 
std::vector< routing_guidelines::Server_infoget_new_connection_nodes () const
 Get information about nodes available for new connections. More...
 
std::vector< routing_guidelines::Server_infoget_old_connection_nodes () const
 Get information about nodes available for existing connections. More...
 
std::unique_ptr< Destinationget_last_used_destination () const override
 Get a destination candidate that was already selected by the Destination Manager, this will not balance destinations or change the Destination Manager internal state. More...
 
void on_instances_change (const bool md_servers_reachable)
 
void subscribe_for_metadata_cache_changes ()
 
void subscribe_for_acceptor_handler ()
 
void subscribe_for_md_refresh_handler ()
 
void notify_instances_changed (const bool md_servers_reachable, const uint64_t) noexcept override
 Callback function that is called when state of cluster is changed. More...
 
bool update_socket_acceptor_state () noexcept override
 Callback function that is called when the state of the sockets acceptors is handled during the metadata refresh. More...
 
void on_md_refresh (const bool instances_changed) override
 Callback that is going to be used on each metadata refresh. More...
 

Private Attributes

const std::string cache_name_
 The Metadata Cache to use. More...
 
const mysqlrouter::URIQuery uri_query_
 Query part of the URI given as destination in the configuration. More...
 
ServerRole server_role_
 
metadata_cache::MetadataCacheAPIBasecache_api_
 
bool subscribed_for_metadata_cache_changes_ {false}
 
bool disconnect_on_promoted_to_primary_ {false}
 
bool disconnect_on_metadata_unavailable_ {false}
 
std::shared_ptr< routing_guidelines::Routing_guidelines_enginerouting_guidelines_ {nullptr}
 Routing guideline engine. More...
 
Protocol::Type protocol_
 Protocol for the destination. More...
 
routing::RoutingStrategy strategy_
 Routing strategy that is used within the currently used destination group. More...
 
std::string last_server_uuid_
 UUID of a last destination returned by get_next_destination(), used in wait for primary failover mechanism. More...
 
routing_guidelines::Routing_guidelines_engine::Route_classification route_info_
 Guidelines route which is designated by the guidelines engine to handle connection. More...
 
std::vector< std::vector< Destination > > destination_candidates_
 Destination candidates that are going to be used to create destination groups. More...
 
uint16_t current_destination_group_index_ {0}
 Index of the currently used destination group. More...
 
uint16_t current_group_position_ {0}
 Index of the current position within a destination group. More...
 
std::map< uint16_t, uint16_t > stored_destination_indexes_
 
std::atomic< ConnectionStatuslast_connection_status_
 Information about previous connection status. More...
 
uint16_t available_dests_in_group_ {0}
 How many available destinations are in the currently used destination group. More...
 
bool has_read_write_ {false}
 Destination manager contains read-write destination candidates. More...
 
bool has_read_only_ {false}
 Destination manager contains read-only destination candidates. More...
 
Destination destination_
 Destination thats used for the connection. More...
 

Additional Inherited Members

- Protected Member Functions inherited from DestinationManager
const MySQLRoutingContextget_routing_context () const
 
- Protected Attributes inherited from DestinationManager
net::io_contextio_ctx_
 
MySQLRoutingContextrouting_ctx_
 
std::mutex state_mtx_
 
std::error_code last_ec_
 
- Protected Attributes inherited from DestinationNodesStateNotifier
AllowedNodesChangeCallbacksList allowed_nodes_change_callbacks_
 
MetadataRefreshCallback md_refresh_callback_
 
StartSocketAcceptorCallback start_router_socket_acceptor_callback_
 
StopSocketAcceptorCallback stop_router_socket_acceptor_callback_
 
std::mutex allowed_nodes_change_callbacks_mtx_
 
std::mutex md_refresh_callback_mtx_
 
std::mutex socket_acceptor_handle_callbacks_mtx
 

Member Enumeration Documentation

◆ ConnectionStatus

Status of a last connection.

Enumerator
InProgress 
Failed 
NotSet 

◆ ServerRole

Enumerator
Primary 
Secondary 
PrimaryAndSecondary 

Constructor & Destructor Documentation

◆ DestMetadataCacheManager() [1/3]

DestMetadataCacheManager::DestMetadataCacheManager ( net::io_context io_ctx,
MySQLRoutingContext routing_ctx,
const std::string &  metadata_cache,
const mysqlrouter::URIQuery query,
const ServerRole  role,
metadata_cache::MetadataCacheAPIBase cache_api = metadata_cache::MetadataCacheAPI::instance() 
)

Constructor.

◆ DestMetadataCacheManager() [2/3]

DestMetadataCacheManager::DestMetadataCacheManager ( const DestMetadataCacheManager other)
delete

Copy constructor.

◆ DestMetadataCacheManager() [3/3]

DestMetadataCacheManager::DestMetadataCacheManager ( DestMetadataCacheManager &&  )
delete

Move constructor.

◆ ~DestMetadataCacheManager()

DestMetadataCacheManager::~DestMetadataCacheManager ( )
override

Member Function Documentation

◆ cache_api()

metadata_cache::MetadataCacheAPIBase * DestMetadataCacheManager::cache_api ( )
inline

◆ change_group()

bool DestMetadataCacheManager::change_group ( )
private

Change destination group that is currently being used.

This happens if there was a connection error and current group could not provide a destination candidate.

Return values
truesuccessful group change
falseunsuccessful group change

◆ clear_internal_state()

void DestMetadataCacheManager::clear_internal_state ( )

Clear internal state (indexes, last connection status etc).

Used when guidelines are updated.

◆ connect_status()

void DestMetadataCacheManager::connect_status ( std::error_code  ec)
overridevirtual

Implements DestinationManager.

◆ get_all_nodes()

std::vector< routing_guidelines::Server_info > DestMetadataCacheManager::get_all_nodes ( ) const
private

Get addresses of all nodes in the topology.

◆ get_destination_candidates()

std::vector< mysql_harness::Destination > DestMetadataCacheManager::get_destination_candidates ( ) const
overridevirtual

Get addresses of all nodes that are a possible destination candidates.

Implements DestinationManager.

◆ get_last_used_destination()

std::unique_ptr< Destination > DestMetadataCacheManager::get_last_used_destination ( ) const
inlineoverrideprivatevirtual

Get a destination candidate that was already selected by the Destination Manager, this will not balance destinations or change the Destination Manager internal state.

Implements DestinationManager.

◆ get_new_connection_nodes()

std::vector< routing_guidelines::Server_info > DestMetadataCacheManager::get_new_connection_nodes ( ) const
private

Get information about nodes available for new connections.

◆ get_next_destination()

std::unique_ptr< Destination > DestMetadataCacheManager::get_next_destination ( const routing_guidelines::Session_info )
overridevirtual

Get destination that should be used for connection attempt.

It uses routing strategies and internal information (last used indexes, failed attempt information) for destination selection.

Returns
Destination candidate used for connection attempt.

Implements DestinationManager.

◆ get_next_destination_impl()

std::unique_ptr< Destination > DestMetadataCacheManager::get_next_destination_impl ( )
private

◆ get_nodes_allowed_by_routing_guidelines()

std::vector< routing_guidelines::Server_info > DestMetadataCacheManager::get_nodes_allowed_by_routing_guidelines ( ) const
private

Get addresses of nodes allowed by the auto-generated routing guideline.

Should not be called when user-provided guideline is used as in such case it might be impossible to determine the list upfront (matching criteria might depend on source IP info for example).

◆ get_nodes_from_topology()

std::vector< routing_guidelines::Server_info > DestMetadataCacheManager::get_nodes_from_topology ( const metadata_cache::ClusterTopology cluster_topology,
const bool  drop_all_hidden 
) const
privatevirtual

Get destination candidates details from the given topology.

Parameters
cluster_topologytopology of the cluster
drop_all_hiddenshoud the hidden nodes be included in the result
Returns
list of destination candidates

◆ get_old_connection_nodes()

std::vector< routing_guidelines::Server_info > DestMetadataCacheManager::get_old_connection_nodes ( ) const
private

Get information about nodes available for existing connections.

◆ handle_sockets_acceptors()

void DestMetadataCacheManager::handle_sockets_acceptors ( )
inlineoverridevirtual

Trigger listening socket acceptors state handler based on the destination type.

Implements DestinationManager.

◆ has_read_only()

bool DestMetadataCacheManager::has_read_only ( ) const
inlineoverridevirtual

Check if there are read-only destinations that could be used.

Implements DestinationManager.

◆ has_read_write()

bool DestMetadataCacheManager::has_read_write ( ) const
inlineoverridevirtual

Check if there are read-write destinations that could be used.

Implements DestinationManager.

◆ init()

void DestMetadataCacheManager::init ( )
private

Initializes.

This method initialized the object. It goes of the URI query information and sets members accordingly.

◆ init_destinations()

stdx::expected< void, std::error_code > DestMetadataCacheManager::init_destinations ( const routing_guidelines::Session_info session_info)
overridevirtual

Set up destination manager, prepare the destinations.

Returns
error code on failure

Implements DestinationManager.

◆ notify_instances_changed()

void DestMetadataCacheManager::notify_instances_changed ( const bool  md_servers_reachable,
const uint64_t  view_id 
)
overrideprivatevirtualnoexcept

Callback function that is called when state of cluster is changed.

Parameters
md_servers_reachabletrue if metadata changed, false if metadata unavailable
view_idcurrent metadata view_id in case of ReplicaSet cluster

Implements metadata_cache::ClusterStateListenerInterface.

◆ on_instances_change()

void DestMetadataCacheManager::on_instances_change ( const bool  md_servers_reachable)
private

◆ on_md_refresh()

void DestMetadataCacheManager::on_md_refresh ( const bool  instances_changed)
overrideprivatevirtual

Callback that is going to be used on each metadata refresh.

Parameters
[in]instances_changedInforms if the cluster topology has changed since last md refresh.

Implements metadata_cache::MetadataRefreshListenerInterface.

◆ operator=() [1/2]

DestMetadataCacheManager & DestMetadataCacheManager::operator= ( const DestMetadataCacheManager )
delete

Copy assignment.

◆ operator=() [2/2]

DestMetadataCacheManager & DestMetadataCacheManager::operator= ( DestMetadataCacheManager &&  )
delete

Move assignment.

◆ prepare_destination_groups()

void DestMetadataCacheManager::prepare_destination_groups ( )
private

Fill each destination group with destination candidates, according to the routing guideline that is being used.

For example given the group setting [[d1,d2], [d3]] and d1 containing 127.0.0.1 d2 containing 127.0.1.1 127.0.1.2 d3 containing 127.0.2.1 It will create two destination groups 1) [127.0.0.1, 127.0.1.1, 127.0.1.2] 2) [127.0.2.1] Where group 2) is a backup destination group.

◆ purpose()

mysqlrouter::ServerMode DestMetadataCacheManager::purpose ( ) const
inlineoverridevirtual

Reimplemented from DestinationManager.

◆ refresh_destinations()

bool DestMetadataCacheManager::refresh_destinations ( const routing_guidelines::Session_info )
overridevirtual

refresh destinations.

should be called after connecting to all destinations failed.

Return values
truerefresh suceeded, there are destinations that could be used.
falserefresh failed, there are no destinations that could be used.

Implements DestinationManager.

◆ resolve_routing_guidelines_hostnames()

std::unordered_map< std::string, net::ip::address > DestMetadataCacheManager::resolve_routing_guidelines_hostnames ( const std::vector< routing_guidelines::Resolve_host > &  hostnames)
private

Resolve hostnames used in routing guidelines document.

If hostname could be resolved to multiple addresses (with requested IP version) then only one of the addresses is used, with unspecified order.

Parameters
hostnameslist of hostnames to be resolved, each host contain information about hostname and IP version which should be used.
Returns
hostname address pairs

◆ set_last_connect_successful()

void DestMetadataCacheManager::set_last_connect_successful ( const bool  state)
private

Set information if the last connection was successful.

◆ start()

void DestMetadataCacheManager::start ( const mysql_harness::PluginFuncEnv env)
overridevirtual

Start the destination.

It also overwrites parent class' DestinationManager::start(), which launches Quarantine. For Metadata Cache routing, we don't need it.

Parameters
envpointer to the PluginFuncEnv object

Implements DestinationManager.

◆ subscribe_for_acceptor_handler()

void DestMetadataCacheManager::subscribe_for_acceptor_handler ( )
private

◆ subscribe_for_md_refresh_handler()

void DestMetadataCacheManager::subscribe_for_md_refresh_handler ( )
private

◆ subscribe_for_metadata_cache_changes()

void DestMetadataCacheManager::subscribe_for_metadata_cache_changes ( )
private

◆ update_routing_guidelines()

routing_guidelines::Routing_guidelines_engine::RouteChanges DestMetadataCacheManager::update_routing_guidelines ( const std::string &  routing_guidelines_document)

Update routing guidelines engine with a new routing guideline.

If the new routing guideline is empty then auto-generated guideline is used. If the guidelines engine could not be updated then old guideline is preserved and used.

Returns
List of route names of the routes that have been updated.

◆ update_socket_acceptor_state()

bool DestMetadataCacheManager::update_socket_acceptor_state ( )
overrideprivatevirtualnoexcept

Callback function that is called when the state of the sockets acceptors is handled during the metadata refresh.

Implements metadata_cache::AcceptorUpdateHandlerInterface.

◆ validate_current_sharing_settings()

void DestMetadataCacheManager::validate_current_sharing_settings ( std::string_view  route_name,
Destination dest 
) const
private

If the routing guideline enables the connection sharing then it validates if the sharing prerequisites are met and it could be used.

If not then connection sharing is disabled.

Parameters
route_namename of the route that enables connection sharing
destdestination candidate that is going to be used for connection.

Member Data Documentation

◆ available_dests_in_group_

uint16_t DestMetadataCacheManager::available_dests_in_group_ {0}
private

How many available destinations are in the currently used destination group.

◆ cache_api_

metadata_cache::MetadataCacheAPIBase* DestMetadataCacheManager::cache_api_
private

◆ cache_name_

const std::string DestMetadataCacheManager::cache_name_
private

The Metadata Cache to use.

cache_name_ is the the section key in the configuration of Metadata Cache.

For example, given following Metadata Cache configuration, cache_name_ will be set to "ham":

[metadata_cache.ham]
host = metadata.example.com

◆ current_destination_group_index_

uint16_t DestMetadataCacheManager::current_destination_group_index_ {0}
private

Index of the currently used destination group.

◆ current_group_position_

uint16_t DestMetadataCacheManager::current_group_position_ {0}
private

Index of the current position within a destination group.

◆ destination_

Destination DestMetadataCacheManager::destination_
private

Destination thats used for the connection.

◆ destination_candidates_

std::vector<std::vector<Destination> > DestMetadataCacheManager::destination_candidates_
private

Destination candidates that are going to be used to create destination groups.

◆ disconnect_on_metadata_unavailable_

bool DestMetadataCacheManager::disconnect_on_metadata_unavailable_ {false}
private

◆ disconnect_on_promoted_to_primary_

bool DestMetadataCacheManager::disconnect_on_promoted_to_primary_ {false}
private

◆ has_read_only_

bool DestMetadataCacheManager::has_read_only_ {false}
private

Destination manager contains read-only destination candidates.

◆ has_read_write_

bool DestMetadataCacheManager::has_read_write_ {false}
private

Destination manager contains read-write destination candidates.

◆ last_connection_status_

std::atomic<ConnectionStatus> DestMetadataCacheManager::last_connection_status_
private
Initial value:

Information about previous connection status.

◆ last_server_uuid_

std::string DestMetadataCacheManager::last_server_uuid_
private

UUID of a last destination returned by get_next_destination(), used in wait for primary failover mechanism.

◆ protocol_

Protocol::Type DestMetadataCacheManager::protocol_
private

Protocol for the destination.

◆ route_info_

routing_guidelines::Routing_guidelines_engine::Route_classification DestMetadataCacheManager::route_info_
private

Guidelines route which is designated by the guidelines engine to handle connection.

◆ routing_guidelines_

std::shared_ptr<routing_guidelines::Routing_guidelines_engine> DestMetadataCacheManager::routing_guidelines_ {nullptr}
private

Routing guideline engine.

◆ server_role_

ServerRole DestMetadataCacheManager::server_role_
private

◆ stored_destination_indexes_

std::map<uint16_t, uint16_t> DestMetadataCacheManager::stored_destination_indexes_
private

◆ strategy_

routing::RoutingStrategy DestMetadataCacheManager::strategy_
private

Routing strategy that is used within the currently used destination group.

◆ subscribed_for_metadata_cache_changes_

bool DestMetadataCacheManager::subscribed_for_metadata_cache_changes_ {false}
private

◆ uri_query_

const mysqlrouter::URIQuery DestMetadataCacheManager::uri_query_
private

Query part of the URI given as destination in the configuration.

For example, given following Metadata Cache configuration:

[routing:metadata_read_only]
..
destination =

metadata_cache:///cluster_name/replicaset_name?role=PRIMARY_AND_SECONDARY

The 'role' is part of uri_query_.


The documentation for this class was generated from the following files: