pub struct RaftIndexNode { /* private fields */ }Expand description
Raft node implementation for distributed vector index
This implements the core Raft protocol. In a production deployment, RPCs would be sent over the network (e.g., gRPC or HTTP/2). Here we provide the state machine logic and expose methods for injecting simulated or actual network messages.
Implementations§
Source§impl RaftIndexNode
impl RaftIndexNode
Sourcepub fn new(config: RaftConfig) -> Self
pub fn new(config: RaftConfig) -> Self
Create a new Raft index node
Sourcepub fn start_election(&self) -> RequestVoteRequest
pub fn start_election(&self) -> RequestVoteRequest
Start an election (become candidate)
Sourcepub fn handle_request_vote(
&self,
request: RequestVoteRequest,
) -> RequestVoteResponse
pub fn handle_request_vote( &self, request: RequestVoteRequest, ) -> RequestVoteResponse
Handle a RequestVote RPC from a candidate
Sourcepub fn process_vote_response(&self, response: RequestVoteResponse) -> bool
pub fn process_vote_response(&self, response: RequestVoteResponse) -> bool
Process a vote response from a peer
Returns true if this node just won the election.
Sourcepub fn handle_append_entries(
&self,
request: AppendEntriesRequest,
) -> AppendEntriesResponse
pub fn handle_append_entries( &self, request: AppendEntriesRequest, ) -> AppendEntriesResponse
Handle AppendEntries RPC (from leader)
Sourcepub fn process_append_entries_response(
&self,
peer_id: NodeId,
response: AppendEntriesResponse,
entries_sent_count: usize,
)
pub fn process_append_entries_response( &self, peer_id: NodeId, response: AppendEntriesResponse, entries_sent_count: usize, )
Process AppendEntries response from a follower (leader only)
Sourcepub fn propose(
&self,
command: IndexCommand,
client_id: Option<String>,
) -> Result<LogIndex>
pub fn propose( &self, command: IndexCommand, client_id: Option<String>, ) -> Result<LogIndex>
Propose a new command (leader only)
Returns the log index of the proposed entry, or an error if not leader.
Sourcepub fn create_append_entries_request(
&self,
peer_id: NodeId,
) -> Result<AppendEntriesRequest>
pub fn create_append_entries_request( &self, peer_id: NodeId, ) -> Result<AppendEntriesRequest>
Create an AppendEntries request for a specific follower
Sourcepub fn force_commit_single_node(&self)
pub fn force_commit_single_node(&self)
Force commit a single-node cluster (for testing)
In a single-node cluster, entries are immediately committed.
Sourcepub fn current_term(&self) -> Term
pub fn current_term(&self) -> Term
Get the current term
Sourcepub fn current_leader(&self) -> Option<NodeId>
pub fn current_leader(&self) -> Option<NodeId>
Get the current leader ID (if known)
Sourcepub fn log_length(&self) -> usize
pub fn log_length(&self) -> usize
Get the number of entries in the log
Sourcepub fn commit_index(&self) -> LogIndex
pub fn commit_index(&self) -> LogIndex
Get the commit index
Sourcepub fn last_applied(&self) -> LogIndex
pub fn last_applied(&self) -> LogIndex
Get the last applied index
Sourcepub fn vector_count(&self) -> usize
pub fn vector_count(&self) -> usize
Get the number of vectors in the state machine
Sourcepub fn get_vector(&self, vector_id: &str) -> Option<VectorEntry>
pub fn get_vector(&self, vector_id: &str) -> Option<VectorEntry>
Get a vector from the state machine (read only)
Sourcepub fn search_similar(&self, query: &[f32], k: usize) -> Vec<(String, f32)>
pub fn search_similar(&self, query: &[f32], k: usize) -> Vec<(String, f32)>
Search for similar vectors in the state machine
Sourcepub fn election_timeout_elapsed(&self) -> bool
pub fn election_timeout_elapsed(&self) -> bool
Check if election timeout has elapsed
Sourcepub fn reset_heartbeat(&self)
pub fn reset_heartbeat(&self)
Reset the heartbeat timer (call when receiving valid messages from leader)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RaftIndexNode
impl !RefUnwindSafe for RaftIndexNode
impl Send for RaftIndexNode
impl Sync for RaftIndexNode
impl Unpin for RaftIndexNode
impl UnsafeUnpin for RaftIndexNode
impl !UnwindSafe for RaftIndexNode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.