Skip to content

Commit

Permalink
M src/list/raxos/protocal/src/apaxos/acceptor.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Sep 27, 2024
1 parent 75671fe commit a7f3b62
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/list/raxos/protocal/src/apaxos/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::Debug;
use validit::Validate;

use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::decided::Decided;
use crate::apaxos::history::History;
use crate::Types;
Expand Down Expand Up @@ -39,12 +40,12 @@ impl<T: Types> Acceptor<T> {
pub(crate) fn handle_phase1_request(
&mut self,
commit_time: T::Time,
) -> Result<Branch<T>, T::Time> {
) -> Result<Branch<T, { HEAD_UNDECIDED }>, T::Time> {
self.check_committable(&commit_time)?;

self.forbid_smaller_commit_time(commit_time);

Ok(self.history.history_view(commit_time))
Ok(self.history.branch(commit_time))
}

pub(crate) fn handle_phase2_request(&mut self, decided: Decided<T>) -> Result<(), T::Time> {
Expand Down
14 changes: 8 additions & 6 deletions src/list/raxos/protocal/src/apaxos/branch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::apaxos::decided::Decided;
use crate::Types;

// For doc reference
Expand All @@ -11,12 +10,15 @@ use crate::apaxos::{

/// Indicate the [`Branch`] includes an event has been set at the head
/// time.
pub const HEAD_SET: bool = true;
pub const HEAD_DECIDED: bool = true;

/// Indicate the [`Branch`] **may** not include an event at the head
/// time. Note that the head time is already in the internal history.
pub const HEAD_UNSET: bool = false;
pub const HEAD_UNDECIDED: bool = false;

/// Whether the head time of a [`Branch`] has been set an event.
///
/// Note that before setting, there may already be an event at the head time.
pub type HeadEventState = bool;

/// Represents a single branch [`History`] that is reachable upto a designated
Expand All @@ -40,7 +42,7 @@ where T: Types
history: T::History,
}

impl<T> Branch<T, HEAD_UNSET>
impl<T> Branch<T, HEAD_UNDECIDED>
where T: Types
{
pub fn new(head_time: T::Time, history: T::History) -> Self {
Expand All @@ -58,14 +60,14 @@ where T: Types
pub fn add_event(
mut self,
event: T::Event,
) -> Result<Branch<T, HEAD_SET>, Branch<T, HEAD_SET>> {
) -> Result<Branch<T, HEAD_DECIDED>, Branch<T, HEAD_DECIDED>> {
if let Some(_ev) = self.history.get(&self.head_time) {
// Nothing to do
} else {
self.history.append(self.head_time, event).unwrap();
}

Ok(Branch::<T, HEAD_SET> {
Ok(Branch::<T, HEAD_DECIDED> {
head_time: self.head_time,
history: self.history,
})
Expand Down
4 changes: 2 additions & 2 deletions src/list/raxos/protocal/src/apaxos/decided.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_SET;
use crate::apaxos::branch::HEAD_DECIDED;

pub type Decided<T> = Branch<T, { HEAD_SET }>;
pub type Decided<T> = Branch<T, { HEAD_DECIDED }>;
7 changes: 4 additions & 3 deletions src/list/raxos/protocal/src/apaxos/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;

use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::errors::TimeRegression;
use crate::Types;

Expand Down Expand Up @@ -34,9 +35,9 @@ where

fn get(&self, time: &T::Time) -> Option<&T::Event>;

/// Returns a view(subset) of the history that is causally prior to or
/// concurrent with the given `time`.
fn history_view(&self, time: T::Time) -> Branch<T> {
/// Returns a single head [`Branch`] of the history that is causally prior
/// to or concurrent with the given `time`.
fn branch(&self, time: T::Time) -> Branch<T, { HEAD_UNDECIDED }> {
let lower = self.lower_bounds(time);
Branch::new(time, lower)
}
Expand Down
4 changes: 2 additions & 2 deletions src/list/raxos/protocal/src/apaxos/proposer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use phase1::Phase1;
use phase2::Phase2;

use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::errors::APError;
use crate::APaxos;
use crate::Types;
Expand Down Expand Up @@ -44,7 +45,7 @@ impl<'a, T: Types> Proposer<'a, T> {
}
}

fn new_phase2(&mut self, maybe_committed: Branch<T>) -> Phase2<T> {
fn new_phase2(&mut self, maybe_committed: Branch<T, HEAD_UNDECIDED>) -> Phase2<T> {
// If the current time already has an event, no new event is added.

let decided = match maybe_committed.add_event(self.event.clone()) {
Expand All @@ -60,7 +61,6 @@ impl<'a, T: Types> Proposer<'a, T> {

Phase2 {
apaxos: &mut self.apaxos,
time: self.time,
decided,
accepted: Default::default(),
}
Expand Down
3 changes: 2 additions & 1 deletion src/list/raxos/protocal/src/apaxos/proposer/phase1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::errors::APError;
use crate::apaxos::history::History;
use crate::APaxos;
Expand Down Expand Up @@ -29,7 +30,7 @@ pub struct Phase1<'a, T: Types> {
}

impl<'a, T: Types> Phase1<'a, T> {
pub fn run(mut self) -> Result<Branch<T>, APError<T>> {
pub fn run(mut self) -> Result<Branch<T, { HEAD_UNDECIDED }>, APError<T>> {
let apaxos = &mut self.apaxos;

let mut sent = 0;
Expand Down
3 changes: 0 additions & 3 deletions src/list/raxos/protocal/src/apaxos/proposer/phase2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ use crate::Types;
pub struct Phase2<'a, T: Types> {
pub apaxos: &'a mut APaxos<T>,

/// The time of the Proposer that running phase1.
pub time: T::Time,

pub decided: Decided<T>,

pub accepted: BTreeMap<T::AcceptorId, ()>,
Expand Down
13 changes: 11 additions & 2 deletions src/list/raxos/protocal/src/commonly_used/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::VecDeque;

use crate::apaxos::acceptor::Acceptor;
use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::decided::Decided;
use crate::Transport;
use crate::Types;
Expand All @@ -11,7 +12,10 @@ use crate::Types;
pub struct DirectCall<T: Types> {
acceptors: BTreeMap<T::AcceptorId, Acceptor<T>>,

p1_replies: VecDeque<(T::AcceptorId, Result<Branch<T>, T::Time>)>,
p1_replies: VecDeque<(
T::AcceptorId,
Result<Branch<T, { HEAD_UNDECIDED }>, T::Time>,
)>,
p2_replies: VecDeque<(T::AcceptorId, Result<(), T::Time>)>,
}

Expand All @@ -33,7 +37,12 @@ impl<T: Types> Transport<T> for DirectCall<T> {
self.p1_replies.push_back((target, reply));
}

fn recv_phase1_reply(&mut self) -> (T::AcceptorId, Result<Branch<T>, T::Time>) {
fn recv_phase1_reply(
&mut self,
) -> (
T::AcceptorId,
Result<Branch<T, { HEAD_UNDECIDED }>, T::Time>,
) {
self.p1_replies.pop_front().unwrap()
}

Expand Down
8 changes: 7 additions & 1 deletion src/list/raxos/protocal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Debug;
use apaxos::ptime::Time;

use crate::apaxos::branch::Branch;
use crate::apaxos::branch::HEAD_UNDECIDED;
use crate::apaxos::decided::Decided;
use crate::apaxos::history::History;

Expand Down Expand Up @@ -54,7 +55,12 @@ where Self: Default + Debug + Clone + Sized + 'static

pub trait Transport<T: Types> {
fn send_phase1_request(&mut self, target: T::AcceptorId, t: T::Time);
fn recv_phase1_reply(&mut self) -> (T::AcceptorId, Result<Branch<T>, T::Time>);
fn recv_phase1_reply(
&mut self,
) -> (
T::AcceptorId,
Result<Branch<T, { HEAD_UNDECIDED }>, T::Time>,
);

fn send_phase2_request(&mut self, target: T::AcceptorId, decided: Decided<T>);
fn recv_phase2_reply(&mut self) -> (T::AcceptorId, Result<(), T::Time>);
Expand Down

0 comments on commit a7f3b62

Please sign in to comment.