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 22, 2024
1 parent f17d1fb commit 7a58ecf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/list/raxos/protocal/src/apaxos/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ impl<T: Types> Acceptor<T> {
}

pub(crate) fn handle_phase2_request(&mut self, history: T::History) -> bool {
dbg!("handle_phase2_request", history);
dbg!("handle_phase2_request", &history);

let mut maximals = history.maximal_times();
let new_written_time = maximals.next().unwrap();
{
let mut maximals = history.maximal_times();
let new_written_time = maximals.next().unwrap();

assert!(
maximals.next().is_none(),
"A proposer commit a history reachable from only one single time"
);
assert!(
maximals.next().is_none(),
"A proposer commit a history reachable from only one single time"
);

if self.is_committable(&new_written_time) {
return false;
if self.is_committable(&new_written_time) {
return false;
}
}

self.history.merge(history);
Expand All @@ -66,7 +68,7 @@ impl<T: Types> Acceptor<T> {

/// Check it is allowed to commit at the specified time.
fn is_committable(&self, time: &T::Time) -> bool {
for t in self.forbidden_commit_time {
for t in &self.forbidden_commit_time {
if t.is_gt(time) {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions src/list/raxos/protocal/src/apaxos/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::collections::BTreeMap;

use crate::Types;

#[derive(Debug)]
#[derive(thiserror::Error)]
pub enum APError<T: Types> {
#[error("Read quorum not reached: {accepted:?}")]
ReadQuorumNotReached {
accepted: BTreeMap<T::AcceptorId, ()>,
},
#[error("Write quorum not reached: {accepted:?}")]
WriteQuorumNotReached {
accepted: BTreeMap<T::AcceptorId, ()>,
},
Expand Down
2 changes: 1 addition & 1 deletion src/list/raxos/protocal/src/apaxos/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where Self: Default + Debug + Clone
/// All `maximal` have no order between them.
fn maximals(&self) -> impl Iterator<Item = (T::Time, T::Event)>;

fn maximal_times(&self) -> impl Iterator<Item = T::Time> {
fn maximal_times<'a>(&'a self) -> impl Iterator<Item = T::Time> + 'a {
self.maximals().map(|(t, _)| t)
}

Expand Down
10 changes: 10 additions & 0 deletions src/list/raxos/protocal/src/commonly_used/history/one_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ impl<T: Types> Default for OneSlotHistory<T> {
}
}

impl<T: Types> OneSlotHistory<T> {
pub fn value_time(&self) -> Option<T::Time> {
self.history.clone().map(|(t, _)| t)
}

pub fn value(&self) -> Option<&T::Event> {
self.history.as_ref().map(|(_, e)| e)
}
}

impl<T: Types> History<T> for OneSlotHistory<T> {
fn append(&mut self, time: T::Time, event: T::Event) {
todo!()
Expand Down
16 changes: 11 additions & 5 deletions src/list/raxos/protocal/src/implementations/paxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ mod tests {
let mut proposer = Proposer::new(&mut apaxos, 5, "hello".to_string());
let committed = proposer.run()?;

assert_eq!(committed.propose_time, 5);
assert_eq!(committed.data, "hello".to_string());
assert_eq!(committed.value_time(), Some(5));
assert_eq!(committed.value().cloned(), Some(s("hello")));

let mut proposer = Proposer::new(&mut apaxos, 6, "world".to_string());
let committed = proposer.run();
let committed = proposer.run()?;

assert_eq!(committed.value_time(), Some(5));
assert_eq!(committed.value().cloned(), Some(s("hello")));

assert_eq!(committed.propose_time, 5);
assert_eq!(committed.data, "hello".to_string());
Ok(())

// TODO: rebuild from previous value
}

fn s(s: impl ToString) -> String {
s.to_string()
}
}

0 comments on commit 7a58ecf

Please sign in to comment.