d_engine_proto/exts/
replication_ext.rs

1use crate::common::LogId;
2use crate::server::replication::AppendEntriesResponse;
3use crate::server::replication::ConflictResult;
4use crate::server::replication::SuccessResult;
5use crate::server::replication::append_entries_response;
6
7impl AppendEntriesResponse {
8    /// Generate a successful response (full success)
9    pub fn success(
10        node_id: u32,
11        term: u64,
12        last_match: Option<LogId>,
13    ) -> Self {
14        Self {
15            node_id,
16            term,
17            result: Some(append_entries_response::Result::Success(SuccessResult {
18                last_match,
19            })),
20        }
21    }
22
23    /// Generate conflict response (with conflict details)
24    pub fn conflict(
25        node_id: u32,
26        term: u64,
27        conflict_term: Option<u64>,
28        conflict_index: Option<u64>,
29    ) -> Self {
30        Self {
31            node_id,
32            term,
33            result: Some(append_entries_response::Result::Conflict(ConflictResult {
34                conflict_term,
35                conflict_index,
36            })),
37        }
38    }
39
40    /// Generate a conflict response (Higher term found)
41    pub fn higher_term(
42        node_id: u32,
43        term: u64,
44    ) -> Self {
45        Self {
46            node_id,
47            term,
48            result: Some(append_entries_response::Result::HigherTerm(term)),
49        }
50    }
51
52    /// Check if it is a success response
53    pub fn is_success(&self) -> bool {
54        matches!(
55            &self.result,
56            Some(append_entries_response::Result::Success(_))
57        )
58    }
59
60    /// Check if it is a conflict response
61    pub fn is_conflict(&self) -> bool {
62        matches!(
63            &self.result,
64            Some(append_entries_response::Result::Conflict(_conflict))
65        )
66    }
67
68    /// Check if it is a response of a higher Term
69    pub fn is_higher_term(&self) -> bool {
70        matches!(
71            &self.result,
72            Some(append_entries_response::Result::HigherTerm(_))
73        )
74    }
75}