Ferricstore.Config (ferricstore v0.3.4)

Copy Markdown View Source

GenServer managing runtime configuration for FerricStore.

Provides a Redis-compatible CONFIG GET/CONFIG SET interface for reading and writing server parameters at runtime. All values are stored as strings to match Redis behaviour.

Parameter categories

Read-only (CONFIG GET only -- CONFIG SET returns an error):

  • "maxmemory" -- max memory budget in bytes from MemoryGuard
  • "maxclients" -- maximum simultaneous client connections
  • "tcp-port" -- TCP port the server is listening on
  • "data-dir" -- Bitcask data directory path
  • "tls-port" -- TLS port the server is listening on ("0" if not configured)
  • "tls-cert-file" -- path to PEM certificate file
  • "tls-key-file" -- path to PEM private key file
  • "tls-ca-cert-file" -- path to CA certificate bundle
  • "require-tls" -- whether plaintext connections are rejected ("true" / "false")

Read-write (CONFIG GET and CONFIG SET):

  • "maxmemory-policy" -- eviction policy (volatile-lru, allkeys-lru, volatile-ttl, noeviction)
  • "notify-keyspace-events" -- keyspace notification flag string
  • "slowlog-log-slower-than" -- slowlog threshold in microseconds
  • "slowlog-max-len" -- maximum slowlog entries
  • "hz" -- server tick frequency (stub, always reports 10)

Plus legacy parameters: timeout, tcp-keepalive, databases, bind, port, save, appendonly, loglevel, requirepass.

Telemetry

On every successful CONFIG SET, emits:

[:ferricstore, :config, :changed]

with measurements %{} and metadata %{param: key, value: new_value, old_value: previous_value}.

Usage

Ferricstore.Config.get("max*")
#=> [{"maxmemory", "1073741824"}, {"maxclients", "10000"}, {"maxmemory-policy", "volatile-lru"}]

Ferricstore.Config.set("maxmemory-policy", "allkeys-lru")
#=> :ok

Ferricstore.Config.set("maxmemory", "999")
#=> {:error, "ERR Unsupported CONFIG parameter: maxmemory (read-only)"}

Summary

Types

Configuration parameter name.

Configuration parameter value (always a string).

Functions

Returns a specification to start this module under a supervisor.

Returns the path where CONFIG REWRITE persists configuration.

Returns the full map of default configuration values.

Returns all config key-value pairs whose keys match the given glob pattern.

Returns the current value for a single config key, or nil if not set.

Persists the current runtime configuration to disk.

Sets a runtime configuration parameter.

Starts the Config GenServer and registers it under Ferricstore.Config.

Types

param_name()

@type param_name() :: binary()

Configuration parameter name.

param_value()

@type param_value() :: binary()

Configuration parameter value (always a string).

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config_file_path()

@spec config_file_path() :: binary()

Returns the path where CONFIG REWRITE persists configuration.

The path is <data_dir>/ferricstore.conf.

defaults()

@spec defaults() :: %{required(param_name()) => param_value()}

Returns the full map of default configuration values.

Note: this returns the static defaults only, not the live state which includes values derived from Application env.

get(pattern)

@spec get(binary()) :: [{param_name(), param_value()}]

Returns all config key-value pairs whose keys match the given glob pattern.

The pattern supports * (match any sequence) and ? (match single char).

Parameters

  • pattern -- glob pattern string (e.g. "*", "max*", "hz")

Returns

A list of {key, value} tuples for every matching parameter, sorted by key.

Examples

iex> Ferricstore.Config.get("hz")
[{"hz", "10"}]

iex> Ferricstore.Config.get("nonexistent")
[]

get_value(key)

@spec get_value(binary()) :: binary() | nil

Returns the current value for a single config key, or nil if not set.

Reads directly from ETS (~100ns) instead of GenServer.call (~1-5us). The ETS table is updated on every CONFIG SET and at init. This eliminates the Config GenServer as a contention point for requires_auth? checks which run on every command.

rewrite()

@spec rewrite() :: :ok | {:error, binary()}

Persists the current runtime configuration to disk.

Writes all current configuration key-value pairs to a file at <data_dir>/ferricstore.conf. Each line is formatted as key value.

Returns

  • :ok on success
  • {:error, reason} when the file cannot be written

set(key, value)

@spec set(binary(), binary()) :: :ok | {:error, binary()}

Sets a runtime configuration parameter.

Validates the parameter name (must be a known read-write parameter) and the value (type/range checks). Applies side-effects for parameters that affect runtime behaviour (e.g. updating Application env for slowlog thresholds, updating MemoryGuard eviction policy).

Emits a [:ferricstore, :config, :changed] telemetry event on success.

Parameters

  • key -- parameter name (e.g. "hz", "maxmemory-policy")
  • value -- new value as a string

Returns

  • :ok on success
  • {:error, reason} when the parameter is read-only, unknown, or the value fails validation

Examples

iex> Ferricstore.Config.set("maxmemory-policy", "allkeys-lru")
:ok

iex> Ferricstore.Config.set("maxmemory", "999")
{:error, "ERR Unsupported CONFIG parameter: maxmemory (read-only)"}

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Config GenServer and registers it under Ferricstore.Config.

Reads initial values for read-only parameters from Application env and MemoryGuard configuration.