MySQL 8.0.40
Source Code Documentation
MetadataCache Class Referenceabstract

The MetadataCache manages cached information fetched from the MySQL Server. More...

#include <metadata_cache.h>

Inheritance diagram for MetadataCache:
[legend]

Classes

struct  RestAuthData
 
struct  Stats
 

Public Member Functions

 MetadataCache (const unsigned router_id, const std::string &clusterset_id, const std::vector< mysql_harness::TCPAddress > &metadata_servers, std::shared_ptr< MetaData > cluster_metadata, const metadata_cache::MetadataCacheTTLConfig &ttl_config, const mysqlrouter::SSLOptions &ssl_options, const mysqlrouter::TargetCluster &target_cluster, const metadata_cache::RouterAttributes &router_attributes, size_t thread_stack_size=mysql_harness::kDefaultStackSizeInKiloBytes, bool use_cluster_notifications=false)
 Initialize a connection to the MySQL Metadata server. More...
 
 ~MetadataCache () override
 
void start ()
 Starts the Metadata Cache. More...
 
void stop () noexcept
 Stops the Metadata Cache. More...
 
metadata_cache::cluster_nodes_list_t get_cluster_nodes ()
 Returns list of managed servers in a cluster. More...
 
metadata_cache::ClusterTopology get_cluster_topology ()
 Returns object containing current Cluster Topology. More...
 
bool wait_primary_failover (const std::string &server_uuid, const std::chrono::seconds &timeout)
 Wait until cluster PRIMARY changes. More...
 
void refresh_thread ()
 refresh cluster information More...
 
void add_state_listener (metadata_cache::ClusterStateListenerInterface *listener) override
 Register observer that is notified when there is a change in the cluster nodes setup/state discovered. More...
 
void remove_state_listener (metadata_cache::ClusterStateListenerInterface *listener) override
 Unregister observer previously registered with add_state_listener() More...
 
void add_acceptor_handler_listener (metadata_cache::AcceptorUpdateHandlerInterface *listener)
 Register observer that is notified when the state of listening socket acceptors should be updated on the next metadata refresh. More...
 
void remove_acceptor_handler_listener (metadata_cache::AcceptorUpdateHandlerInterface *listener)
 Unregister observer previously registered with add_acceptor_handler_listener() More...
 
void add_md_refresh_listener (metadata_cache::MetadataRefreshListenerInterface *listener)
 Register observer that is notified on each metadata refresh event. More...
 
void remove_md_refresh_listener (metadata_cache::MetadataRefreshListenerInterface *listener)
 Unregister observer previously registered with add_md_refresh_listener() More...
 
metadata_cache::MetadataCacheAPIBase::RefreshStatus refresh_status ()
 
std::chrono::milliseconds ttl () const
 
mysqlrouter::TargetCluster target_cluster () const
 
virtual mysqlrouter::ClusterType cluster_type () const noexcept=0
 
std::vector< mysql_harness::TCPAddressmetadata_servers ()
 
void enable_fetch_auth_metadata ()
 
void force_cache_update ()
 
void check_auth_metadata_timers () const
 
std::pair< bool, MetaData::auth_credentials_t::mapped_type > get_rest_user_auth_data (const std::string &user)
 
void handle_sockets_acceptors_on_md_refresh ()
 Toggle socket acceptors state update on next metadata refresh. More...
 
bool fetch_whole_topology () const
 
void fetch_whole_topology (bool val)
 

Static Public Member Functions

static void * run_thread (void *context)
 run refresh thread More...
 

Protected Member Functions

virtual bool refresh (bool needs_writable_node)=0
 Refreshes the cache. More...
 
void on_refresh_failed (bool terminated, bool md_servers_reachable=false)
 
void on_refresh_succeeded (const metadata_cache::metadata_server_t &metadata_server)
 
void on_instances_changed (const bool md_servers_reachable, const metadata_cache::ClusterTopology &cluster_topology, uint64_t view_id=0)
 
void on_handle_sockets_acceptors ()
 Called when the listening sockets acceptors state should be updated but replicaset instances has not changed (in that case socket acceptors would be handled when calling on_instances_changed). More...
 
void on_md_refresh (const bool cluster_nodes_changed, const metadata_cache::ClusterTopology &cluster_topology)
 Called on each metadata refresh. More...
 
void on_refresh_requested ()
 
void on_refresh_completed ()
 
bool update_auth_cache ()
 
void update_router_attributes ()
 
void update_router_last_check_in ()
 
bool needs_initial_attributes_update ()
 
bool needs_last_check_in_update ()
 

Protected Attributes

metadata_cache::ClusterTopology cluster_topology_
 
mysqlrouter::TargetCluster target_cluster_
 
const std::string clusterset_id_
 
metadata_cache::metadata_servers_list_t metadata_servers_
 
metadata_cache::MetadataCacheTTLConfig ttl_config_
 
mysqlrouter::SSLOptions ssl_options_
 
unsigned router_id_
 
Monitor< RestAuthDatarest_auth_ {{}}
 
bool auth_metadata_fetch_enabled_ {false}
 
std::shared_ptr< MetaDatameta_data_
 
mysql_harness::MySQLRouterThread refresh_thread_
 refresh thread facade More...
 
mysql_harness::MySQLRouterThread notification_thread_
 notification thread facade More...
 
std::mutex cache_refreshing_mutex_
 
std::mutex metadata_servers_mutex_
 
std::atomic< bool > terminated_ {false}
 
bool refresh_requested_ {false}
 
bool use_cluster_notifications_
 
std::condition_variable refresh_wait_
 
std::mutex refresh_wait_mtx_
 
std::condition_variable refresh_completed_
 
std::mutex refresh_completed_mtx_
 
std::mutex cluster_instances_change_callbacks_mtx_
 
std::mutex acceptor_handler_callbacks_mtx_
 
std::mutex md_refresh_callbacks_mtx_
 
std::set< metadata_cache::ClusterStateListenerInterface * > state_listeners_
 
std::set< metadata_cache::AcceptorUpdateHandlerInterface * > acceptor_update_listeners_
 
std::set< metadata_cache::MetadataRefreshListenerInterface * > md_refresh_listeners_
 
Monitor< Statsstats_ {{}}
 
bool initial_attributes_update_done_ {false}
 
uint32_t periodic_stats_update_counter_ {1}
 
std::chrono::steady_clock::time_point last_periodic_stats_update_timestamp_
 
bool ready_announced_ {false}
 
std::atomic< bool > fetch_whole_topology_ {false}
 
std::atomic< bool > trigger_acceptor_update_on_next_refresh_ {false}
 Flag indicating if socket acceptors state should be updated on next metadata refresh even if instance information has not changed. More...
 
metadata_cache::RouterAttributes router_attributes_
 

Detailed Description

The MetadataCache manages cached information fetched from the MySQL Server.

Constructor & Destructor Documentation

◆ MetadataCache()

MetadataCache::MetadataCache ( const unsigned  router_id,
const std::string &  clusterset_id,
const std::vector< mysql_harness::TCPAddress > &  metadata_servers,
std::shared_ptr< MetaData cluster_metadata,
const metadata_cache::MetadataCacheTTLConfig ttl_config,
const mysqlrouter::SSLOptions ssl_options,
const mysqlrouter::TargetCluster target_cluster,
const metadata_cache::RouterAttributes router_attributes,
size_t  thread_stack_size = mysql_harness::kDefaultStackSizeInKiloBytes,
bool  use_cluster_notifications = false 
)

Initialize a connection to the MySQL Metadata server.

Parameters
router_idid of the router in the cluster metadata
clusterset_idUUID of the ClusterSet the Cluster belongs to (if bootstrapped as a ClusterSet, empty otherwise)
metadata_serversThe servers that store the metadata
cluster_metadatametadata of the cluster
ttl_configmetadata TTL configuration
ssl_optionsSSL related options for connection
target_clusterobject identifying the Cluster this operation refers to
router_attributesRouter attributes to be registered in the metadata
thread_stack_sizeThe maximum memory allocated for thread's stack
use_cluster_notificationsFlag indicating if the metadata cache should use GR notifications as an additional trigger for metadata refresh

◆ ~MetadataCache()

MetadataCache::~MetadataCache ( )
override

Member Function Documentation

◆ add_acceptor_handler_listener()

void MetadataCache::add_acceptor_handler_listener ( metadata_cache::AcceptorUpdateHandlerInterface listener)

Register observer that is notified when the state of listening socket acceptors should be updated on the next metadata refresh.

Parameters
listenerObserver object that is notified when replicaset nodes state is changed.

◆ add_md_refresh_listener()

void MetadataCache::add_md_refresh_listener ( metadata_cache::MetadataRefreshListenerInterface listener)

Register observer that is notified on each metadata refresh event.

Parameters
listenerObserver object that is notified on md refresh.

◆ add_state_listener()

void MetadataCache::add_state_listener ( metadata_cache::ClusterStateListenerInterface listener)
overridevirtual

Register observer that is notified when there is a change in the cluster nodes setup/state discovered.

Parameters
listenerObserver object that is notified when cluster nodes state is changed.

Implements metadata_cache::ClusterStateNotifierInterface.

◆ check_auth_metadata_timers()

void MetadataCache::check_auth_metadata_timers ( ) const

◆ cluster_type()

virtual mysqlrouter::ClusterType MetadataCache::cluster_type ( ) const
pure virtualnoexcept

Implemented in ARMetadataCache, and GRMetadataCache.

◆ enable_fetch_auth_metadata()

void MetadataCache::enable_fetch_auth_metadata ( )
inline

◆ fetch_whole_topology() [1/2]

bool MetadataCache::fetch_whole_topology ( ) const
inline

◆ fetch_whole_topology() [2/2]

void MetadataCache::fetch_whole_topology ( bool  val)

◆ force_cache_update()

void MetadataCache::force_cache_update ( )
inline

◆ get_cluster_nodes()

metadata_cache::cluster_nodes_list_t MetadataCache::get_cluster_nodes ( )

Returns list of managed servers in a cluster.

Return a list of servers that are part of a cluster.

Returns
std::vector containing ManagedInstance objects

◆ get_cluster_topology()

metadata_cache::ClusterTopology MetadataCache::get_cluster_topology ( )

Returns object containing current Cluster Topology.

◆ get_rest_user_auth_data()

std::pair< bool, MetaData::auth_credentials_t::mapped_type > MetadataCache::get_rest_user_auth_data ( const std::string &  user)

◆ handle_sockets_acceptors_on_md_refresh()

void MetadataCache::handle_sockets_acceptors_on_md_refresh ( )
inline

Toggle socket acceptors state update on next metadata refresh.

◆ metadata_servers()

std::vector< mysql_harness::TCPAddress > MetadataCache::metadata_servers ( )

◆ needs_initial_attributes_update()

bool MetadataCache::needs_initial_attributes_update ( )
protected

◆ needs_last_check_in_update()

bool MetadataCache::needs_last_check_in_update ( )
protected

◆ on_handle_sockets_acceptors()

void MetadataCache::on_handle_sockets_acceptors ( )
protected

Called when the listening sockets acceptors state should be updated but replicaset instances has not changed (in that case socket acceptors would be handled when calling on_instances_changed).

◆ on_instances_changed()

void MetadataCache::on_instances_changed ( const bool  md_servers_reachable,
const metadata_cache::ClusterTopology cluster_topology,
uint64_t  view_id = 0 
)
protected

◆ on_md_refresh()

void MetadataCache::on_md_refresh ( const bool  cluster_nodes_changed,
const metadata_cache::ClusterTopology cluster_topology 
)
protected

Called on each metadata refresh.

Parameters
[in]cluster_nodes_changedInformation whether there was a change in instances reported by metadata refresh.
[in]cluster_topologycurrent cluster topology

◆ on_refresh_completed()

void MetadataCache::on_refresh_completed ( )
protected

◆ on_refresh_failed()

void MetadataCache::on_refresh_failed ( bool  terminated,
bool  md_servers_reachable = false 
)
protected

◆ on_refresh_requested()

void MetadataCache::on_refresh_requested ( )
protected

◆ on_refresh_succeeded()

void MetadataCache::on_refresh_succeeded ( const metadata_cache::metadata_server_t metadata_server)
protected

◆ refresh()

virtual bool MetadataCache::refresh ( bool  needs_writable_node)
protectedpure virtual

Refreshes the cache.

Implemented in ARMetadataCache, and GRMetadataCache.

◆ refresh_status()

metadata_cache::MetadataCacheAPIBase::RefreshStatus MetadataCache::refresh_status ( )
inline

◆ refresh_thread()

void MetadataCache::refresh_thread ( )

refresh cluster information

◆ remove_acceptor_handler_listener()

void MetadataCache::remove_acceptor_handler_listener ( metadata_cache::AcceptorUpdateHandlerInterface listener)

Unregister observer previously registered with add_acceptor_handler_listener()

Parameters
listenerObserver object that should be unregistered.

◆ remove_md_refresh_listener()

void MetadataCache::remove_md_refresh_listener ( metadata_cache::MetadataRefreshListenerInterface listener)

Unregister observer previously registered with add_md_refresh_listener()

Parameters
listenerObserver object that should be unregistered.

◆ remove_state_listener()

void MetadataCache::remove_state_listener ( metadata_cache::ClusterStateListenerInterface listener)
overridevirtual

Unregister observer previously registered with add_state_listener()

Parameters
listenerObserver object that should be unregistered.

Implements metadata_cache::ClusterStateNotifierInterface.

◆ run_thread()

void * MetadataCache::run_thread ( void *  context)
static

run refresh thread

◆ start()

void MetadataCache::start ( )

Starts the Metadata Cache.

Connect to the metadata servers and refresh the metadata information in the cache.

Starts the Metadata Cache and launch thread.

◆ stop()

void MetadataCache::stop ( )
noexcept

Stops the Metadata Cache.

Stop the refresh thread.

Stops the Metadata Cache and the launch thread.

◆ target_cluster()

mysqlrouter::TargetCluster MetadataCache::target_cluster ( ) const
inline

◆ ttl()

std::chrono::milliseconds MetadataCache::ttl ( ) const
inline

◆ update_auth_cache()

bool MetadataCache::update_auth_cache ( )
protected

◆ update_router_attributes()

void MetadataCache::update_router_attributes ( )
protected

◆ update_router_last_check_in()

void MetadataCache::update_router_last_check_in ( )
protected

◆ wait_primary_failover()

bool MetadataCache::wait_primary_failover ( const std::string &  server_uuid,
const std::chrono::seconds timeout 
)

Wait until cluster PRIMARY changes.

wait until a change of the PRIMARY is noticed

leave early if

  • 'timeout' expires
  • process shutdown is requested

function has to handle two scenarios:

connection to PRIMARY fails because:

  1. PRIMARY died and group relects a new member
  2. network to PRIMARY lost, but GR sees no fault and PRIMARY does not change.

Therefore, if the connection to PRIMARY fails, wait for change of the membership or timeout, whatever happens earlier.

Parameters
server_uuidserver-uuid of the PRIMARY that we failed to connect
timeout- amount of time to wait for a failover
Returns
true if a primary member exists

Member Data Documentation

◆ acceptor_handler_callbacks_mtx_

std::mutex MetadataCache::acceptor_handler_callbacks_mtx_
protected

◆ acceptor_update_listeners_

std::set<metadata_cache::AcceptorUpdateHandlerInterface *> MetadataCache::acceptor_update_listeners_
protected

◆ auth_metadata_fetch_enabled_

bool MetadataCache::auth_metadata_fetch_enabled_ {false}
protected

◆ cache_refreshing_mutex_

std::mutex MetadataCache::cache_refreshing_mutex_
protected

◆ cluster_instances_change_callbacks_mtx_

std::mutex MetadataCache::cluster_instances_change_callbacks_mtx_
protected

◆ cluster_topology_

metadata_cache::ClusterTopology MetadataCache::cluster_topology_
protected

◆ clusterset_id_

const std::string MetadataCache::clusterset_id_
protected

◆ fetch_whole_topology_

std::atomic<bool> MetadataCache::fetch_whole_topology_ {false}
protected

◆ initial_attributes_update_done_

bool MetadataCache::initial_attributes_update_done_ {false}
protected

◆ last_periodic_stats_update_timestamp_

std::chrono::steady_clock::time_point MetadataCache::last_periodic_stats_update_timestamp_
protected
Initial value:
{
std::chrono::steady_clock::now()}

◆ md_refresh_callbacks_mtx_

std::mutex MetadataCache::md_refresh_callbacks_mtx_
protected

◆ md_refresh_listeners_

std::set<metadata_cache::MetadataRefreshListenerInterface *> MetadataCache::md_refresh_listeners_
protected

◆ meta_data_

std::shared_ptr<MetaData> MetadataCache::meta_data_
protected

◆ metadata_servers_

metadata_cache::metadata_servers_list_t MetadataCache::metadata_servers_
protected

◆ metadata_servers_mutex_

std::mutex MetadataCache::metadata_servers_mutex_
protected

◆ notification_thread_

mysql_harness::MySQLRouterThread MetadataCache::notification_thread_
protected

notification thread facade

◆ periodic_stats_update_counter_

uint32_t MetadataCache::periodic_stats_update_counter_ {1}
protected

◆ ready_announced_

bool MetadataCache::ready_announced_ {false}
protected

◆ refresh_completed_

std::condition_variable MetadataCache::refresh_completed_
protected

◆ refresh_completed_mtx_

std::mutex MetadataCache::refresh_completed_mtx_
protected

◆ refresh_requested_

bool MetadataCache::refresh_requested_ {false}
protected

◆ refresh_thread_

mysql_harness::MySQLRouterThread MetadataCache::refresh_thread_
protected

refresh thread facade

◆ refresh_wait_

std::condition_variable MetadataCache::refresh_wait_
protected

◆ refresh_wait_mtx_

std::mutex MetadataCache::refresh_wait_mtx_
protected

◆ rest_auth_

Monitor<RestAuthData> MetadataCache::rest_auth_ {{}}
protected

◆ router_attributes_

metadata_cache::RouterAttributes MetadataCache::router_attributes_
protected

◆ router_id_

unsigned MetadataCache::router_id_
protected

◆ ssl_options_

mysqlrouter::SSLOptions MetadataCache::ssl_options_
protected

◆ state_listeners_

std::set<metadata_cache::ClusterStateListenerInterface *> MetadataCache::state_listeners_
protected

◆ stats_

Monitor<Stats> MetadataCache::stats_ {{}}
protected

◆ target_cluster_

mysqlrouter::TargetCluster MetadataCache::target_cluster_
protected

◆ terminated_

std::atomic<bool> MetadataCache::terminated_ {false}
protected

◆ trigger_acceptor_update_on_next_refresh_

std::atomic<bool> MetadataCache::trigger_acceptor_update_on_next_refresh_ {false}
protected

Flag indicating if socket acceptors state should be updated on next metadata refresh even if instance information has not changed.

◆ ttl_config_

metadata_cache::MetadataCacheTTLConfig MetadataCache::ttl_config_
protected

◆ use_cluster_notifications_

bool MetadataCache::use_cluster_notifications_
protected

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