Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nubskr/walrus/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheMetadata component maintains the cluster’s shared state through Raft consensus. It tracks all topics, their segments, leadership assignments, and node addresses. This state is replicated across all nodes and serves as the source of truth for routing decisions.
Location: distributed-walrus/src/metadata.rs
Core Data Structures
ClusterState
The top-level replicated state container. Location:metadata.rs:11
| Field | Type | Description |
|---|---|---|
topics | HashMap<TopicName, TopicState> | All topics and their current state |
nodes | HashMap<NodeId, String> | Node ID to Raft/RPC address mappings |
TopicState
Comprehensive state for a single topic, including segment history and leadership. Location:metadata.rs:18
Fields
The active segment number where new writes are directed. Starts at 1 when topic is created.
The node ID currently responsible for handling writes to the current segment.
Cumulative count of entries across all sealed segments. Used for calculating global logical offsets.
Maps sealed segment IDs to their entry counts. A segment becomes sealed when rolling over to a new segment.
Historical record of which node was the leader for each segment. Used to route reads to the correct node.
Type Aliases
Metadata Commands
Commands that modify cluster state through Raft consensus. Location:metadata.rs:33
CreateTopic
Creates a new topic with an initial leader assignment. Fields:name- Topic name (must be unique)initial_leader- Node ID that will lead segment 1
- Inserts new
TopicStatewithcurrent_segment = 1 - Records
initial_leaderas bothleader_nodeand insegment_leaders[1] - Returns
"CREATED"if successful,"EXISTS"if topic already exists
metadata.rs:125
Topic names are hashed deterministically to select an initial leader, ensuring even distribution across the cluster.
RolloverTopic
Seals the current segment and creates a new one with a new leader. Fields:name- Topic namenew_leader- Node ID for the new segmentsealed_segment_entry_count- Number of entries in the segment being sealed
- Seals current segment by adding to
sealed_segmentsmap - Records old leader in
segment_leadersfor the sealed segment - Increments
last_sealed_entry_offsetby the sealed count - Increments
current_segment - Sets
leader_nodetonew_leader - Records new leader in
segment_leadersfor new segment - Returns
"ROLLED"if successful, error if topic not found
metadata.rs:144
UpsertNode
Registers a new node or updates an existing node’s address. Fields:node_id- Unique node identifieraddr- Network address (host:port or IP:port)
- Inserts or updates entry in
nodesmap - Returns
"NODE"on success
metadata.rs:167
Metadata Interface
TheMetadata struct wraps ClusterState with thread-safe access.
Location: metadata.rs:51
Query Methods
get_topic_state
Retrieves the current state of a topic.
Some(TopicState) if topic exists, None otherwise
Location: metadata.rs:62
Use Cases:
- Determining current leader for append routing
- Checking current segment number
- Accessing segment history for reads
get_node_addr
Looks up the network address for a node ID.
Some(addr) if node is registered, None otherwise
Location: metadata.rs:67
Use Cases:
- Resolving target address for RPC forwarding
- Cluster topology discovery
- Health checking
all_node_addrs
Returns all registered nodes and their addresses.
metadata.rs:72
Use Cases:
- Cluster-wide operations
- Leader election (deterministic node ordering)
- Topology snapshots
owned_topics
Finds all topics where the specified node is the current leader.
metadata.rs:83
Use Cases:
- Lease management (determining which WAL keys this node should hold)
- Node responsibility tracking
- Load balancing analysis
sealed_count
Retrieves the entry count for a sealed segment.
Some(count) if segment is sealed, None if not found
Location: metadata.rs:97
Use Cases:
- Read cursor advancement across segments
- Determining when a segment has been fully consumed
- Calculating logical offsets
Only sealed (historical) segments have entry counts recorded. The current active segment’s count is tracked separately in NodeController’s
offsets map.segment_leader
Determines which node was the leader for a specific segment.
Some(node_id)fromsegment_leadersif recorded- Falls back to current
leader_nodeif not found Noneif topic doesn’t exist
metadata.rs:105
Use Cases:
- Routing reads to the correct historical segment owner
- Reconstructing segment ownership history
- Debugging leadership changes
State Machine Implementation
Metadata implements the StateMachineTrait from Octopii for Raft integration.
apply
Applies a committed Raft log entry to the state machine.
Location: metadata.rs:116
- Deserializes command bytes into
MetadataCmd - Acquires write lock on state
- Executes command logic (see MetadataCmd variants above)
- Returns success indicator or error message
snapshot
Creates a point-in-time snapshot of the entire cluster state.
Location: metadata.rs:174
ClusterState
Use Cases:
- Raft snapshot creation for log compaction
- New node bootstrapping
- Backup and disaster recovery
restore
Restores state from a snapshot.
Location: metadata.rs:179
- Deserializes snapshot bytes into
ClusterState - Acquires write lock
- Replaces entire state with recovered data
- Node startup with existing cluster state
- Raft snapshot installation
- Disaster recovery
Consistency Guarantees
Strong Consistency
All metadata operations go through Raft consensus:- Linearizable reads: Querying metadata reflects all committed updates
- Atomic updates: Commands are applied atomically in log order
- Durability: State is replicated across majority before acknowledgment
Read Locks
Query methods use read locks (RwLock::read()), allowing:
- Multiple concurrent readers
- No contention between query operations
- Blocking only on writes (rare)
Write Locks
Modification methods (apply, restore) use write locks, ensuring:
- Exclusive access during state changes
- Serialized command application
- Consistency with Raft log order
Initialization
Location:metadata.rs:56
- No topics
- No registered nodes
- Ready for Raft log application or snapshot restore
Thread Safety
- Clone-friendly:
MetadataisCloneviaArc, allowing sharing across async tasks - Lock poisoning: Gracefully handles lock poisoning by returning
None/errors - No deadlocks: Single lock per operation, no nested locking
State Evolution Examples
Topic Creation Flow
Segment Rollover Flow
Multiple Rollover Example
Integration with NodeController
TheMetadata component works closely with NodeController:
- Lease Determination:
owned_topics()tells NodeController which WAL keys to acquire leases for - Append Routing:
get_topic_state()provides leader information for write routing - Read Routing:
segment_leader()andsealed_count()enable cursor-based reads across segments - Metadata Updates: NodeController calls
propose_metadata()to modify cluster state - Address Resolution:
get_node_addr()enables RPC forwarding to remote nodes
Performance Characteristics
- Read Operations: O(1) hash lookups with read lock contention only
- Write Operations: O(1) plus Raft consensus latency (typically < 100ms)
- Memory: Grows with number of topics and segments (historical data retained)
- Snapshot Size: Linear in number of topics and sealed segments
Monitoring and Debugging
Topic Snapshot
Location:controller/mod.rs:189
Raft Metrics
Access viaNodeController::get_metrics() to see:
- Current leader
- Log indices
- Membership configuration
- Snapshot status
Related Components
- NodeController - Orchestrates metadata usage for routing
- Bucket Storage - Uses metadata-derived leases for write fencing