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

Dependency adapter for LWM (Low Water Mark) based scheduling. More...

#include <dependency_adapter_lwm.h>

Inheritance diagram for mysql::csa::Dependency_adapter_lwm:
[legend]

Public Types

using Task_id = Dependency_adapter::Task_id
 
using Task_id_resolved = Dependency_adapter::Task_id_resolved
 
using Clock_delay = Dependency_adapter::Clock_delay
 
- Public Types inherited from mysql::csa::Dependency_adapter
using Task_id = mysql::scheduler::Task_id
 Alias for task identifier from scheduler. More...
 
using Task_id_resolved = std::optional< Task_id >
 Optional resolved task identifier. More...
 
using Clock_delay = std::optional< uint64_t >
 Optional clock delay value. More...
 

Public Member Functions

 Dependency_adapter_lwm ()
 
std::pair< Clock_delay, Task_id_resolvedsolve (Task_id task_id, int64_t seq_num, int64_t commit_parent) override
 Solves dependencies - figures out after which task a task with the given id should run. More...
 
std::size_t size () const
 Get tracked mapping size for unit tests. More...
 
- Public Member Functions inherited from mysql::csa::Dependency_adapter
virtual ~Dependency_adapter ()=default
 Virtual destructor. More...
 
virtual void set_worker_num (uint32_t)
 Remember the worker pool size if needed. Default - unused. More...
 

Private Attributes

ankerl::unordered_dense::map< int64_t, uint64_t > seq_to_task
 
uint64_t max_task_id {1}
 
uint64_t id_after_barrier {0}
 
int64_t m_prev_seq {0}
 Previous sequence number to check continuity. More...
 
uint64_t m_prev_task_id {0}
 Previous task id for fast-path parent resolution. More...
 
bool m_dense_mapping {false}
 True when current seq->task mapping is dense and can be resolved arithmetically (without hash inserts/lookups for most operations). More...
 
int64_t m_dense_base_seq {0}
 First seq/task pair for the current dense mapping range. More...
 
uint64_t m_dense_base_task_id {0}
 

Detailed Description

Dependency adapter for LWM (Low Water Mark) based scheduling.

Translates transaction sequence numbers into scheduler task IDs for LWM-based execution.

Concepts

This adapter handles key indexing differences between source and replica:

  • seq_num: Originates from the source (e.g., primary server) and starts at 1 for the first transaction in each group. It resets to 1 at group boundaries (e.g., server restarts, configuration changes, or failures). seq_num=0 is reserved for invalid cases (e.g., SEQ_UNINIT).
  • task_id: Assigned sequentially by the scheduler on the replica, starting from 0 and monotonically increasing as long as the applier is running. Unlike seq_num, task_id does not reset and continues growing across seq_num resets. Wrap-around handling: If task_id wraps (uint64_t overflow), the adapter detects it (new < max && max near UINT64_MAX), clears mappings, resets barrier to 0, and continues from the new baseline to maintain order.
  • LWM (Low Water Mark): Tracks the longest prefix of tasks executed in commit order (with replica_preserve_commit_order=1). It is computed on the replica based on task_ids.
    • LWM = 0: No tasks have executed.
    • LWM = 1: Exactly one task has executed (task_id = 0).
    • If tasks with task_id 0 through 3 have executed in order, LWM = 4. LWM advances as tasks commit, representing the next task_id that can proceed without violating order.

In CSA, transactions include a sequence number (seq_num) and last_committed (commit_parent), which is the seq_num of the transaction that must commit before this one, or 0 if the dependency is unknown (e.g., SEQ_UNINIT or first transaction in a group).

This adapter maintains a mapping from seq_num to task_id. The solve() function computes the clock delay for a given task, which is the target LWM value at which the task can execute. This delay represents the dependency relative to the initial LWM (equivalent to the absolute LWM value required). The task is delayed until the current LWM reaches or exceeds this value.

The delay is computed as the maximum of:

  • A barrier value (id_after_barrier), set when commit_parent == 0 to handle unknown dependencies by ensuring the task waits for all prior tasks (including itself if first, but resulting in delay=0 for immediate execution).
  • (task_id of commit_parent + 1), if the mapping for commit_parent exists and this value exceeds the barrier.

When commit_parent == 0 (unknown dependency, e.g., SEQ_UNINIT or group start/first transaction), the seq_to_task map is cleared to bound its size, a new barrier is set to the current max_task_id (the task_id of this task), and the delay equals this barrier. For the first transaction (task_id=0), barrier=0 and delay=0, allowing immediate execution. For subsequent tasks with unknown dependency, it sets a barrier to wait for prior tasks. This ensures ordering across group boundaries without retaining stale mappings.

Example

Delays indicate the LWM value required for execution. Assume seq_num starts at 1, with commit_parent=0 for the first transaction (unknown dependency, immediate execution):

  • Transaction task_id=0, seq_num=1, commit_parent=0 → delay=0 (unknown dependency; execute immediately as first task)
  • Transaction task_id=1, seq_num=2, commit_parent=1 → delay=1 (wait for LWM >=1, i.e., task 0 committed)
  • Transaction task_id=2, seq_num=3, commit_parent=1 → delay=1 (depends on task 0)
  • Transaction task_id=3, seq_num=4, commit_parent=2 → delay=2 (wait for LWM >=2, i.e., task 1 committed)
  • Transaction task_id=4, seq_num=5, commit_parent=0 → delay=4 (unknown dependency; barrier set to wait for all prior tasks)
  • Transaction task_id=5, seq_num=6, commit_parent=3 → delay=4 (wait for LWM >=4, i.e., task 3 committed; respects barrier)

Group boundary (seq_num resets to 1; map cleared, new barrier set):

  • Transaction task_id=6, seq_num=1, commit_parent=0 → delay=6 (unknown dependency; map cleared, barrier=6; wait for LWM >=6)
  • Transaction task_id=7, seq_num=2, commit_parent=1 → delay=7 (depends on task_id=6 (seq_num=1); wait for LWM >=7)

Member Typedef Documentation

◆ Clock_delay

◆ Task_id

◆ Task_id_resolved

Constructor & Destructor Documentation

◆ Dependency_adapter_lwm()

mysql::csa::Dependency_adapter_lwm::Dependency_adapter_lwm ( )
inline

Member Function Documentation

◆ size()

std::size_t mysql::csa::Dependency_adapter_lwm::size ( ) const
inline

Get tracked mapping size for unit tests.

In dense mode, this is the logical contiguous range size (not hash entries), because seq->task mapping is resolved arithmetically.

Returns
The number of tracked tasks.

◆ solve()

std::pair< Clock_delay, Task_id_resolved > mysql::csa::Dependency_adapter_lwm::solve ( Task_id  id,
int64_t  seq_num,
int64_t  commit_parent 
)
inlineoverridevirtual

Solves dependencies - figures out after which task a task with the given id should run.

Parameters
idTask id
seq_numTransaction sequence number
commit_parentTransaction commit parent (LC)
Returns
A pair containing an optional clock delay and an optional resolved task ID

Implements mysql::csa::Dependency_adapter.

Member Data Documentation

◆ id_after_barrier

uint64_t mysql::csa::Dependency_adapter_lwm::id_after_barrier {0}
private

◆ m_dense_base_seq

int64_t mysql::csa::Dependency_adapter_lwm::m_dense_base_seq {0}
private

First seq/task pair for the current dense mapping range.

◆ m_dense_base_task_id

uint64_t mysql::csa::Dependency_adapter_lwm::m_dense_base_task_id {0}
private

◆ m_dense_mapping

bool mysql::csa::Dependency_adapter_lwm::m_dense_mapping {false}
private

True when current seq->task mapping is dense and can be resolved arithmetically (without hash inserts/lookups for most operations).

◆ m_prev_seq

int64_t mysql::csa::Dependency_adapter_lwm::m_prev_seq {0}
private

Previous sequence number to check continuity.

◆ m_prev_task_id

uint64_t mysql::csa::Dependency_adapter_lwm::m_prev_task_id {0}
private

Previous task id for fast-path parent resolution.

◆ max_task_id

uint64_t mysql::csa::Dependency_adapter_lwm::max_task_id {1}
private

◆ seq_to_task

ankerl::unordered_dense::map<int64_t, uint64_t> mysql::csa::Dependency_adapter_lwm::seq_to_task
private

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