Bedrock.DataPlane.Demux.Durability (bedrock v0.5.2)
View SourceTracks durable versions across shards using gb_sets for O(log N) min extraction.
The Demux needs to track the minimum durable version across all active ShardServers to know when it's safe for the Log to trim WAL segments. This module provides efficient tracking with:
- O(log N) min extraction via
:gb_sets - O(log N) version updates
- Shard activation tracking
Implementation
We maintain both a map of shard_id => durable_version for lookups and a
:gb_sets ordered set of {version, shard_id} tuples for efficient min extraction.
When a shard's durable version advances, we:
- Remove the old
{old_version, shard_id}from gb_sets - Insert the new
{new_version, shard_id}into gb_sets - Update the map
Usage
durability = Durability.new()
# Activate a shard with initial version
durability = Durability.activate_shard(durability, 0, 1000)
# Update when shard reports durability
durability = Durability.update_shard(durability, 0, 2000)
# Get minimum durable version
Durability.min_durable_version(durability)
#=> 2000
Summary
Functions
Activates a shard with an initial durable version.
Returns true if the given shard is being tracked.
Returns the number of active shards being tracked.
Returns all active shard IDs.
Deactivates a shard, removing it from durability tracking.
Returns the minimum durable version across all tracked shards.
Creates a new empty durability tracker.
Returns the durable version for a specific shard.
Updates a shard's durable version.
Types
@type shard_id() :: non_neg_integer()
@type t() :: %Bedrock.DataPlane.Demux.Durability{ shard_versions: %{required(shard_id()) => version()}, version_index: :gb_sets.set({version(), shard_id()}) }
@type version() :: Bedrock.version()
Functions
Activates a shard with an initial durable version.
Called when the first transaction touches a shard. The shard starts
with initial_version as its durable version (typically the last_seen_version
at the time of activation).
Returns {:ok, updated_durability} or {:error, :already_active} if shard
is already being tracked.
Returns true if the given shard is being tracked.
@spec active_shard_count(t()) :: non_neg_integer()
Returns the number of active shards being tracked.
Returns all active shard IDs.
Deactivates a shard, removing it from durability tracking.
This is typically used during cleanup or when a shard is no longer needed. Returns the updated durability tracker.
Returns the minimum durable version across all tracked shards.
Returns nil if no shards are being tracked.
@spec new() :: t()
Creates a new empty durability tracker.
Returns the durable version for a specific shard.
Returns nil if the shard is not being tracked.
@spec update_shard(t(), shard_id(), version()) :: {:ok, t()} | {:error, :not_active | :version_going_backwards}
Updates a shard's durable version.
Called when a ShardServer reports that it has durably written data up to a certain version. The version must be >= the current durable version for that shard.
Returns {:ok, updated_durability} or an error if the shard isn't tracked
or the version is going backwards.