MySQL 26.7.0
Source Code Documentation
mysql::csa::Resource_instance_monitor Class Reference

Manages per-channel resources for the Change Stream Applier (CSA). More...

#include <resource_monitor.h>

Public Member Functions

void register_resource (const std::string &name, std::size_t limit, PSI_stage_info *stage=nullptr)
 Registers a named resource with the given limit (maximum available) More...
 
Locked_resource acquire_resource (const std::string &name, std::size_t amount)
 Function locks resource and returns resource guard that will release the resource if locked upon destruction. More...
 
void release_resource (const std::string &name, std::size_t amount)
 Releases the specified amount of the named resource. More...
 
bool lock_resource (const std::string &name, std::size_t amount)
 Waits until the requested resource amount is available or returns an error. More...
 
 Resource_instance_monitor ()=default
 

Static Public Member Functions

static void release_resource (Resource_entry &resource, std::size_t amount)
 Releases the specified amount of the named resource. More...
 
static bool lock_resource (Resource_entry &resource, std::size_t amount)
 Waits until the requested resource amount is available or returns an error. More...
 

Private Types

using Resource_map = std::unordered_map< std::string, std::unique_ptr< Resource_entry > >
 

Static Private Member Functions

static bool try_lock_resource_internal (Resource_entry &resource_entry, std::size_t amount)
 Internal function that optimistically tries to lock resource without spinning. More...
 

Private Attributes

Resource_map m_resources
 

Detailed Description

Manages per-channel resources for the Change Stream Applier (CSA).

Allows registering named resources and locking/releasing them.

This class provides a registry for named resources, each with a configurable limit on maximum available units. Resources can be acquired (locked) using blocking (infinite wait) or non-blocking methods (try lock), and released. It is designed for concurrent access, using atomic operations for counts and spinning with sleep for waiting. One instance is created per channel via Resource_monitor::get(instance_id).

Usage assumptions:

  1. To avoid deadlocks between locking applier resources by different threads and transaction locks, a resource for a transaction should be locked before this transaction starts.
  2. All resources must be registered before first call to concurrent methods (register_resource is non-concurrent)

Main functions:

  • register_resource - for named resource registraction
  • acquire_resource - get resource lock object (releases at destruction)

Key concepts of resource acquisition:

  • Each resource has a 'limit' (maximum available units) set at registration.
  • 'available' tracks current free units.
  • For requests <= limit, standard semaphore-like behavior (wait for available >= amount).
  • For requests > limit, wait for available equal to limit
  • All waiting uses spinning with a constant sleep (1ms) to avoid indefinite blocks.
  • Suitable for resources like channel memory limit in the applier.

Member Typedef Documentation

◆ Resource_map

using mysql::csa::Resource_instance_monitor::Resource_map = std::unordered_map<std::string, std::unique_ptr<Resource_entry> >
private

Constructor & Destructor Documentation

◆ Resource_instance_monitor()

mysql::csa::Resource_instance_monitor::Resource_instance_monitor ( )
default

Member Function Documentation

◆ acquire_resource()

Locked_resource mysql::csa::Resource_instance_monitor::acquire_resource ( const std::string &  name,
std::size_t  amount 
)

Function locks resource and returns resource guard that will release the resource if locked upon destruction.

Parameters
nameThe name of the resource.
amountThe amount to lock.
Returns
Locked_resource object

◆ lock_resource() [1/2]

bool mysql::csa::Resource_instance_monitor::lock_resource ( const std::string &  name,
std::size_t  amount 
)

Waits until the requested resource amount is available or returns an error.

Wait-free in case resource is available in the first check.

Parameters
nameThe name of the resource.
amountThe amount to lock.
Returns
True when successfully locked. False if exited before obtaining.

◆ lock_resource() [2/2]

bool mysql::csa::Resource_instance_monitor::lock_resource ( Resource_entry resource,
std::size_t  amount 
)
static

Waits until the requested resource amount is available or returns an error.

Wait-free in case resource is available in the first check.

Parameters
resourceResource handle
amountThe amount to lock.
Returns
True when successfully locked. False if exited before obtaining.

◆ register_resource()

void mysql::csa::Resource_instance_monitor::register_resource ( const std::string &  name,
std::size_t  limit,
PSI_stage_info stage = nullptr 
)

Registers a named resource with the given limit (maximum available)

Parameters
nameThe name of the resource.
limitThe maximum available count for this resource.
stageOptional PSI stage to set during waiting for lock.

◆ release_resource() [1/2]

void mysql::csa::Resource_instance_monitor::release_resource ( const std::string &  name,
std::size_t  amount 
)

Releases the specified amount of the named resource.

Wait-free.

Parameters
nameThe name of the resource.
amountThe amount to release.

◆ release_resource() [2/2]

void mysql::csa::Resource_instance_monitor::release_resource ( Resource_entry resource,
std::size_t  amount 
)
static

Releases the specified amount of the named resource.

Wait-free.

Parameters
resourceResource handle
amountThe amount to release.

◆ try_lock_resource_internal()

bool mysql::csa::Resource_instance_monitor::try_lock_resource_internal ( Resource_entry resource_entry,
std::size_t  amount 
)
staticprivate

Internal function that optimistically tries to lock resource without spinning.

Performs a single atomic attempt: subtracts the amount and succeeds if the previous available was >= amount or == limit (even for amount > limit). Wait-free; rolls back on failure.

Parameters
resource_entryResource handle
amountAmount of the resource to lock
Returns
True on success, false when could not lock resource.

Member Data Documentation

◆ m_resources

Resource_map mysql::csa::Resource_instance_monitor::m_resources
private

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