Class RaftNode<T>

Core Raft consensus node.

This class implements the Raft algorithm for leader election and log replication. It is transport-agnostic — the caller must wire up sendMessage to deliver RPCs, and call handleMessage when RPCs arrive.

Log indices are 1-based per the Raft paper.

Type Parameters

  • T = unknown

Hierarchy

  • RaftNode

Constructors

Properties

commitIndex: number = 0
currentTerm: number = 0
destroyed: boolean = false
electionTimeoutMax: number
electionTimeoutMin: number
electionTimer: number = null
eventListeners: EventListenerMap<T> = ...
heartbeatInterval: number
heartbeatTimer: number = null
id: string
lastApplied: number = 0
leaderId: string = null
log: RaftLog<T>
matchIndex: Map<string, number> = ...
nextIndex: Map<string, number> = ...
peers: string[] = []
role: RaftRole = 'follower'
sendMessage: ((toPeerId, message) => void) = ...

Type declaration

    • (toPeerId, message): void
    • Called by the node to send an RPC to a peer — must be wired up externally

      Parameters

      Returns void

votedFor: string = null
votesReceived: Set<string> = ...

Methods

  • Advance commitIndex if a majority of matchIndex values allow it

    Returns void

  • Apply all committed but not-yet-applied entries

    Returns void

  • Get the current leader ID (null if unknown)

    Returns string

  • Handle an incoming Raft RPC message from a peer

    Parameters

    Returns void

  • Check if this node is the leader

    Returns boolean

  • Check if a candidate's log is at least as up-to-date as ours

    Parameters

    • candidateLastTerm: number
    • candidateLastIndex: number

    Returns boolean

  • Submit a command to be replicated. Only succeeds on the leader.

    Parameters

    • command: T

    Returns boolean

  • Parameters

    • peer: string

    Returns void

  • Start the node (begin election timer as follower)

    Parameters

    • peerIds: string[]

    Returns void

  • Update the cluster membership dynamically (e.g. when peers join/leave)

    Parameters

    • peerIds: string[]

    Returns void

Generated using TypeDoc