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 25, 2024
1 parent 99dcf86 commit d6acaa3
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/list/raxos/protocal/src/apaxos/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ impl<T: Types> Acceptor<T> {
/// **Classic Paxos** does not have to revert the `Time` but it could.
pub(crate) fn handle_phase1_request(&mut self, commit_time: T::Time) -> (T::Time, T::History) {
if self.is_committable(&commit_time) {
return (commit_time, self.history.visible(commit_time));
return (commit_time, self.history.history_view(commit_time));
}

self.forbidden_commit_time.insert(commit_time);
(commit_time, self.history.visible(commit_time))
(commit_time, self.history.history_view(commit_time))
}

pub(crate) fn handle_phase2_request(&mut self, history: T::History) -> bool {
Expand Down
31 changes: 0 additions & 31 deletions src/list/raxos/protocal/src/apaxos/greater_equal.rs

This file was deleted.

36 changes: 24 additions & 12 deletions src/list/raxos/protocal/src/apaxos/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt::Debug;

use crate::apaxos::greater_equal::GreaterEqual;
use crate::apaxos::history_view::HistoryView;
use crate::commonly_used::history_view::BasicView;
use crate::Types;

pub struct TimeEvent<T: Types> {
Expand All @@ -14,20 +16,29 @@ impl<T: Types> TimeEvent<T> {
}
}

/// A [`History`] contains [`Time`] and [`Event`] in a Partially Ordered Set.
///
/// [`History`] is used by an [`Acceptor`] to store the [`Time`] and [`Event`]
/// pairs.
/// It represents the causal history of events in a distributed system.
pub trait History<T: Types>
where Self: Default + Debug + Clone
{
fn append(&mut self, time: T::Time, event: T::Event);
/// The type representing a view of this History.
///
/// Defaults to `BasicView<T, Self>` but can be overridden by implementors.
type View: HistoryView<T, Self> = BasicView<T, Self>;

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

/// Return a sub set of the history that is visible at `time`.
///
/// In other words, a sub set of TimeEvent that is less than or equal to
/// `time`.
fn visible(&self, time: T::Time) -> Self;
/// 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) -> Self::View;

/// Return the maximal [`Time`] and [`Event`] in the history.
// fn lower_bounds(&self, time: T::Time) -> Self;

/// Return an iterator over the maximal [`Time`] and [`Event`] pairs in the
/// history.
///
/// `maximal` is defined as:
/// g in P is a maximal element:
Expand All @@ -36,7 +47,10 @@ 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<'a>(&'a self) -> impl Iterator<Item = T::Time> + 'a {
fn do_merge(&mut self, other: Self);

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

Expand All @@ -50,13 +64,13 @@ where Self: Default + Debug + Clone

for my_maximal in self.maximal_times() {
if !other.greater_equal(&my_maximal) {
res.do_merge(self.visible(my_maximal));
res.do_merge(self.history_view(my_maximal));
}
}

for other_maximal in other.maximal_times() {
if !self.greater_equal(&other_maximal) {
res.do_merge(other.visible(other_maximal));
res.do_merge(other.history_view(other_maximal));
}
}

Expand All @@ -75,8 +89,6 @@ where Self: Default + Debug + Clone
}
false
}

fn do_merge(&mut self, other: Self);
}

mod sealed {
Expand Down
34 changes: 34 additions & 0 deletions src/list/raxos/protocal/src/apaxos/history_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::apaxos::history::History;
use crate::Types;

/// Represents a snapshot view of a [`History`] up to a specific time.
///
/// This trait extends the [`History`] trait to represent a subset of the
/// history that includes all time and events **before or equal** a specific
/// "view time".
///
/// This trait is used by a [`Proposer`] to represent the system state it sees.
pub trait HistoryView<T: Types, H>
where H: History<T>
{
/// Returns the "current" time of this snapshot view.
///
/// This time represents the **greatest** single time.
/// All events in the snapshot are causally prior to or concurrent with this
/// time.
///
/// Note: The current time does not necessarily have to be an actual event
/// time present in this History. It can be any valid time that defines
/// the causal "cut" for this snapshot view.
fn current_time(&self) -> T::Time;

/// Attempts to append an [`Event`] at the current time to create a new
/// [`History`].
///
/// The created new [`History`] instance that includes all
/// events from this view, plus the new event at the current view time.
///
/// This method should return an `Err` if there is already an [`Event`] at
/// the ([`current_time()`](Self::current_time)).
fn append(self, event: T::Event) -> Result<H, H>;
}
4 changes: 2 additions & 2 deletions src/list/raxos/protocal/src/apaxos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod acceptor;
pub mod errors;
pub mod greater_equal;
pub mod greater_equal_map;
pub mod history;
pub mod history_view;
pub mod proposal;
pub mod proposer;
pub mod ptime;

pub mod errors;
13 changes: 4 additions & 9 deletions src/list/raxos/protocal/src/apaxos/ptime.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::fmt::Debug;
use std::hash::Hash;

use crate::apaxos::greater_equal::GreaterEqual;

/// Pseudo time that is used in a distributed system.
///
/// `Time`s can be compared with `==` or `>=`.
/// But note that the **greater-or-equal** relation of `Time` is **NOT**
/// transitive and must **NOT** form a cycle, i.e., `Time` is **NOT**
/// `PartialOrd`. `Time` is a [DAG]:
/// `Time` is [Partially-Ordered-set]:
///
/// - Reflexivity: `a >= a`
/// - Antisymmetry: `a >= b` and `b >= a` implies `a == b`
/// - Anti-transitivity: `a >= b` and `b >= c` does **NOT** imply `a >= c`
/// - Transitivity: `a >= b` and `b >= c` implies `a >= c`
///
/// See: [Partially-Ordered-set]
/// See: [DAG]
Expand All @@ -22,9 +17,9 @@ use crate::apaxos::greater_equal::GreaterEqual;
/// [DAG]: https://en.wikipedia.org/wiki/Directed_acyclic_graph
/// [Topological-order]: https://en.wikipedia.org/wiki/Topological_sorting
pub trait Time:
Default + Debug + Clone + Copy + PartialEq + Eq + Hash + GreaterEqual + 'static
Default + Debug + Clone + Copy + PartialEq + Eq + Hash + PartialOrd + 'static
{
}

impl<T> Time for T where T: Default + Debug + Clone + Copy + PartialEq + Eq + Hash + GreaterEqual + 'static
impl<T> Time for T where T: Default + Debug + Clone + Copy + PartialEq + Eq + Hash + PartialOrd + 'static
{}
6 changes: 1 addition & 5 deletions src/list/raxos/protocal/src/commonly_used/history/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ impl<T: Types> Default for LinearHistory<T> {
impl<T: Types> History<T> for LinearHistory<T>
where T::Time: Ord
{
fn append(&mut self, time: T::Time, event: T::Event) {
todo!()
}

fn get(&self, time: &T::Time) -> Option<&T::Event> {
self.history.get(&time)
}

fn visible(&self, time: T::Time) -> Self {
fn history_view(&self, time: T::Time) -> Self::View {
let history = self.history.clone().into_iter().take_while(|(t, _)| t <= &time).collect();
Self { history }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ impl<T: Types> OneSlotHistory<T> {
}

impl<T: Types> History<T> for OneSlotHistory<T> {
fn append(&mut self, time: T::Time, event: T::Event) {
todo!()
}

fn get(&self, time: &T::Time) -> Option<&T::Event> {
todo!()
}

fn visible(&self, time: T::Time) -> Self {
fn history_view(&self, time: T::Time) -> Self::View {
todo!()
}

Expand Down
28 changes: 28 additions & 0 deletions src/list/raxos/protocal/src/commonly_used/history_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::apaxos::history::History;
use crate::apaxos::history_view::HistoryView;
use crate::Types;

pub struct BasicView<T, H>
where
T: Types,
H: History<T>,
{
current_time: T::Time,
history: H,
}

impl<T, H> HistoryView<T, H> for BasicView<T, H>
where
T: Types,
H: History<T>,
{
fn current_time(&self) -> T::Time {
self.current_time
}

fn append(self, event: T::Event) -> Result<H, H> {
let mut history = self.history;
history.do_merge(H::new().append(self.current_time, event));
history
}
}
1 change: 1 addition & 0 deletions src/list/raxos/protocal/src/commonly_used/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! used in paxos.
pub mod history;
pub mod history_view;
pub mod quorum_set;
pub mod time;
pub mod transport;
Expand Down

0 comments on commit d6acaa3

Please sign in to comment.