Ferricstore.Transaction.Coordinator (ferricstore v0.3.2)

Copy Markdown View Source

Transaction coordinator for MULTI/EXEC.

Single-shard transactions are dispatched as a batch to the target shard's GenServer, executing within a single handle_call to guarantee atomicity (no other client can interleave on that shard).

Cross-shard transactions submit a single Raft log entry to an "anchor shard" containing commands for ALL involved shards. The StateMachine's apply/3 writes to all shards' ETS tables and Bitcask files in one deterministic pass.

WATCH conflict detection

WATCH uses per-key value hashes (:erlang.phash2/1 of the value) rather than per-shard write-version counters. At WATCH time the connection snapshots phash2(Router.get(key)) for each watched key. At EXEC time watches_clean?/1 re-reads each key and compares hashes. This eliminates false-positive aborts caused by unrelated writes to the same shard (the old shard-version approach).

Summary

Functions

execute(queue, watched_keys, sandbox_namespace)

@spec execute(
  [{binary(), [binary()]}],
  %{required(binary()) => non_neg_integer()},
  binary() | nil
) ::
  [term()] | nil | {:error, binary()}