Redis.Cluster.Topology (Redis v0.8.0)

Copy Markdown View Source

Parses cluster topology responses into slot-to-node mappings.

Supports both CLUSTER SHARDS (Redis 7.0+, preferred) and CLUSTER SLOTS (deprecated, fallback for Redis 6.x).

Summary

Functions

Builds a replica map: {slot => [{replica_host, replica_port}]} from enriched topology.

Builds an ETS-friendly list of {slot, {host, port}} entries from parsed slot ranges.

Parses the response from CLUSTER SHARDS into the same format as parse_slots/1.

Parses CLUSTER SHARDS with replica info, same format as parse_slots_with_replicas/1.

Parses the response from CLUSTER SLOTS into a list of {slot_start, slot_end, primary_host, primary_port} tuples.

Parses CLUSTER SLOTS into a richer format that includes replicas. Returns [{slot_start, slot_end, {primary_host, primary_port}, [{replica_host, replica_port}]}]

Types

node_addr()

@type node_addr() :: {String.t(), non_neg_integer()}

Functions

build_replica_map(parsed_slots)

@spec build_replica_map([
  {non_neg_integer(), non_neg_integer(), node_addr(), [node_addr()]}
]) :: [
  {non_neg_integer(), [node_addr()]}
]

Builds a replica map: {slot => [{replica_host, replica_port}]} from enriched topology.

build_slot_map(parsed_slots)

@spec build_slot_map([
  {non_neg_integer(), non_neg_integer(), String.t(), non_neg_integer()}
]) :: [
  {non_neg_integer(), {String.t(), non_neg_integer()}}
]

Builds an ETS-friendly list of {slot, {host, port}} entries from parsed slot ranges.

parse_shards(shards_response)

@spec parse_shards(list()) :: [
  {non_neg_integer(), non_neg_integer(), String.t(), non_neg_integer()}
]

Parses the response from CLUSTER SHARDS into the same format as parse_slots/1.

CLUSTER SHARDS returns a list of shard maps:

[%{"slots" => [0, 5460], "nodes" => [%{"ip" => "127.0.0.1", "port" => 7000, "role" => "master", ...}]}]

parse_shards_with_replicas(shards_response)

@spec parse_shards_with_replicas(list()) :: [
  {non_neg_integer(), non_neg_integer(), node_addr(), [node_addr()]}
]

Parses CLUSTER SHARDS with replica info, same format as parse_slots_with_replicas/1.

parse_slots(slots_response)

@spec parse_slots(list()) :: [
  {non_neg_integer(), non_neg_integer(), String.t(), non_neg_integer()}
]

Parses the response from CLUSTER SLOTS into a list of {slot_start, slot_end, primary_host, primary_port} tuples.

parse_slots_with_replicas(slots_response)

@spec parse_slots_with_replicas(list()) :: [
  {non_neg_integer(), non_neg_integer(), node_addr(), [node_addr()]}
]

Parses CLUSTER SLOTS into a richer format that includes replicas. Returns [{slot_start, slot_end, {primary_host, primary_port}, [{replica_host, replica_port}]}]