Skip to main content

Message

Enum Message 

Source
pub enum Message {
Show 14 variants PreCampaign { last_index: Index, last_term: Term, }, PreCampaignResponse { vote: bool, }, Campaign { last_index: Index, last_term: Term, }, CampaignResponse { vote: bool, }, Heartbeat { last_index: Index, commit_index: Index, read_seq: ReadSequence, }, HeartbeatResponse { match_index: Index, read_seq: ReadSequence, }, Append { base_index: Index, base_term: Term, entries: Vec<Entry>, }, AppendResponse { match_index: Index, reject_index: Index, }, Read { seq: ReadSequence, }, ReadResponse { seq: ReadSequence, }, ClientRequest { id: RequestID, request: Request, }, ClientResponse { id: RequestID, response: Result<Response>, }, InstallSnapshot { last_included_index: Index, last_included_term: Term, data: Vec<u8>, membership: MembershipEntry, }, InstallSnapshotResponse { last_included_index: Index, },
}
Expand description

Raft 节点之间发送的消息。消息异步发送(非请求/响应模式),可能丢失或乱序。

实践中它们经 TCP 连接与 crossbeam channel 传递;只要连接保持,通常不会丢失或乱序。 一条消息及其响应走各自独立的出站 TCP 连接。

Variants§

§

PreCampaign

预投票请求(Pre-vote):不提升任期、不持久化投票。 用于在真正选举前确认能否获得多数,避免分区节点抬升任期打断稳定领导。

Fields

§last_index: Index

候选人最后一条日志的索引。

§last_term: Term

候选人最后一条日志的任期。

§

PreCampaignResponse

预投票响应。不持久化。

Fields

§vote: bool

为 true 表示授予预选票。

§

Campaign

候选人向同伴拉票竞选领导者。 仅当候选人的日志至少与投票者一样新时才会被授予选票。

Fields

§last_index: Index

候选人最后一条日志的索引。

§last_term: Term

候选人最后一条日志的任期。

§

CampaignResponse

跟随者每个任期只能投一票,且仅当候选人的日志至少与自己一样新时才投票。 候选人隐式投票给自己。

Fields

§vote: bool

为 true 表示授予选票。false 响应并非必须,但为清晰起见仍会发出。

§

Heartbeat

领导者发送的周期性心跳,作用包括:

  • 告知节点当前领导者,并阻止选举。
  • 检测丢失的 append / read,作为重试机制。
  • 推进跟随者的 commit 索引,以便它们应用条目。

Raft 论文没有独立的心跳消息,而是使用空的 AppendEntries RPC; 这里单独定义以便职责更清晰。

Fields

§last_index: Index

领导者最后一条日志的索引。任期即领导者当前任期(当选时会追加 noop)。 跟随者据此与本地日志比较,判断是否跟上。

§commit_index: Index

领导者最后已提交日志的索引。跟随者用它推进 commit 索引并应用条目。 仅当本地日志在 last_index 处与领导者一致时,提交到此索引才安全。

§read_seq: ReadSequence

领导者在本任期内最新的读序列号。

§

HeartbeatResponse

跟随者在仍认可其为领导者时,对心跳作出响应。

Fields

§match_index: Index

非零表示心跳中的 last_index 与跟随者日志匹配;否则跟随者日志分叉或落后。

§read_seq: ReadSequence

心跳中的读序列号。

§

Append

领导者在给定 base 条目之后,向跟随者追加日志条目以进行复制。

若 base 条目与跟随者日志匹配,则两边日志在此之前完全一致(见论文 5.3 节), 可以追加(可能替换冲突条目)。否则拒绝追加,领导者需用更早的 base 索引重试, 直到找到公共 base。

空 append(无条目)用于在日志分叉、节点重启或消息丢失时探测公共 match 索引。 通常通过递减 base 索引探测,匹配后再发送后续条目。

Fields

§base_index: Index

在此日志索引之后追加。

§base_term: Term

base 条目的任期。

§entries: Vec<Entry>

要追加的日志条目,必须从 base_index + 1 开始。

§

AppendResponse

跟随者根据 base 条目是否匹配本地日志,接受或拒绝领导者的 append。

Fields

§match_index: Index

非零表示跟随者已追加到该索引(此前日志与领导者一致)。 若未发送条目(探测),则为匹配的 base 索引。

§reject_index: Index

非零表示在该 base 索引处拒绝(base 索引/任期不匹配)。 若本地日志短于 base 索引,reject 会降到 last_index+1,避免逐个探测缺失索引。

§

Read

领导者在提供读服务前需确认自己仍是领导者,以保证线性一致性 (防止别处已选出新领导者)。读请求在序列号被多数派确认后才执行。

Fields

§

ReadResponse

跟随者确认该读序列号下的领导权。

Fields

§

ClientRequest

客户端请求。可提交给领导者,或提交给跟随者由后者转发给领导者。 若无领导者,或领导者/任期变更,请求会以 Error::Abort 的 ClientResponse 中止,客户端必须重试。

Fields

§id: RequestID

请求 ID。在请求生命周期内必须全局唯一。

§request: Request

请求本体。

§

ClientResponse

客户端响应,通常透传给状态机。

Fields

§id: RequestID

对应原始 ClientRequest 的 ID。

§response: Result<Response>

响应,或错误。

§

InstallSnapshot

安装快照(整包;教学实现不分块)。

Fields

§last_included_index: Index
§last_included_term: Term
§data: Vec<u8>
§membership: MembershipEntry
§

InstallSnapshotResponse

快照安装确认。

Fields

§last_included_index: Index

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Message

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Message

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Message

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.