Ferricstore.Store.WriteVersion (ferricstore v0.3.4)

Copy Markdown View Source

Per-shard atomic write version counters for WATCH/EXEC support.

When writes bypass the Shard GenServer (quorum durability path), the Shard's internal write_version field is not updated. This module provides a shared atomic counter per shard that can be incremented by any process -- the Router (quorum bypass), the Shard GenServer (async path), or the StateMachine.

The counters are stored in a single :counters reference held in :persistent_term, giving ~5ns read/increment with zero contention on different shard indices.

Usage

# During application boot (called once):
WriteVersion.init(4)

# Increment after a successful write:
WriteVersion.increment(shard_index)

# Read the current version for WATCH:
WriteVersion.get(shard_index)

Summary

Functions

Returns the current write version for shard_index.

Returns the current write version using instance ctx.

Atomically increments the write version for shard_index.

Atomically increments the write version using instance ctx.

Initializes the per-shard write version counters.

Functions

get(shard_index)

@spec get(non_neg_integer()) :: non_neg_integer()

Returns the current write version for shard_index.

get(ctx, shard_index)

Returns the current write version using instance ctx.

increment(shard_index)

@spec increment(non_neg_integer()) :: :ok

Atomically increments the write version for shard_index.

Returns :ok. The shard_index is 0-based but :counters uses 1-based indices, so we add 1 internally.

increment(ctx, shard_index)

@spec increment(FerricStore.Instance.t(), non_neg_integer()) :: :ok

Atomically increments the write version using instance ctx.

init(shard_count)

@spec init(pos_integer()) :: :ok

Initializes the per-shard write version counters.

Must be called once during application startup before any writes occur. shard_count determines the number of independent counters.