Raxol.Core.Session.DistributedSessionStorage (Raxol v2.0.1)

View Source

Persistent storage layer for distributed session data.

The DistributedSessionStorage provides a fault-tolerant, consistent storage backend for distributed sessions with automatic sharding, replication, and data integrity guarantees.

Features

  • Persistent session storage with configurable backends
  • Automatic data sharding and partitioning
  • Write-ahead logging for durability
  • Snapshot and incremental backup support
  • Data compression and encryption at rest
  • Automatic cleanup of expired sessions
  • Transaction support for atomic operations

Storage Backends

  • ETS: In-memory storage with optional persistence
  • DETS: Disk-based storage for single-node persistence
  • Mnesia: Distributed database with replication
  • External: Plugin interface for external databases

Usage

# Start storage with Mnesia backend
{:ok, pid} = DistributedSessionStorage.start_link(
  backend: :mnesia,
  replication_nodes: [:node1, :node2, :node3],
  storage_options: %{
    disc_copies: [:node1, :node2],
    ram_copies: [:node3]
  }
)

# Store session data
DistributedSessionStorage.store(pid, session_id, session_data, metadata)

# Retrieve session data
{:ok, data} = DistributedSessionStorage.get(pid, session_id)

Summary

Types

backend_type()

@type backend_type() :: :ets | :dets | :mnesia | :external

session_metadata()

@type session_metadata() :: %{
  created_at: DateTime.t(),
  last_accessed: DateTime.t(),
  expires_at: DateTime.t() | nil,
  size_bytes: non_neg_integer(),
  access_count: non_neg_integer()
}

shard_id()

@type shard_id() :: non_neg_integer()

Functions

backup_sessions(pid, backup_path)

@spec backup_sessions(pid(), binary()) :: {:ok, binary()} | {:error, term()}

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup_expired_sessions(pid)

@spec cleanup_expired_sessions(pid()) :: {:ok, non_neg_integer()} | {:error, term()}

delete(pid, session_id)

@spec delete(pid(), binary()) :: :ok | {:error, term()}

get(pid, session_id)

@spec get(pid(), binary()) :: {:ok, term()} | {:error, :not_found} | {:error, term()}

get_metadata(pid, session_id)

@spec get_metadata(pid(), binary()) :: {:ok, session_metadata()} | {:error, term()}

get_storage_stats(pid)

@spec get_storage_stats(pid()) :: %{
  total_sessions: non_neg_integer(),
  total_size_bytes: non_neg_integer(),
  shard_distribution: %{required(shard_id()) => non_neg_integer()},
  backend_stats: term()
}

handle_manager_call(request, from, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_call/3.

handle_manager_cast(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.

handle_manager_info(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.

list_sessions(pid, filters)

@spec list_sessions(pid(), map()) :: {:ok, [binary()]} | {:error, term()}

restore_sessions(pid, backup_path)

@spec restore_sessions(pid(), binary()) :: {:ok, non_neg_integer()} | {:error, term()}

start_link(init_opts \\ [])

store(pid, session_id, data, metadata)

@spec store(pid(), binary(), term(), map()) :: :ok | {:error, term()}

update_access_time(pid, session_id)

@spec update_access_time(pid(), binary()) :: :ok | {:error, term()}