Skip to content

Commit

Permalink
Update unreplicated model
Browse files Browse the repository at this point in the history
  • Loading branch information
sgdxbc committed Jul 6, 2024
1 parent fc193db commit b7332af
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"endianness",
"Hasher",
"keypair",
"kvstore",
"neatworks",
"pbft",
"prehashed",
Expand All @@ -25,7 +26,8 @@
"seedable",
"serde",
"unreplicated",
"upcall"
"upcall",
"upcalls"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<S: OnEvent<C>, C> OnEvent<C> for &mut S {
pub struct Exit;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TimerId(u32);
pub struct TimerId(pub u32);

pub trait ScheduleEvent<M> {
fn set(
Expand Down
58 changes: 55 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, fmt::Debug, time::Duration};

use crate::{
event::{SendEvent, TimerId},
net::events::Send,
};

pub trait State {
type Event;
Expand All @@ -14,6 +19,53 @@ pub trait State {

#[derive(Debug, PartialEq, Eq, Hash, Default)]
pub struct Network<A, M, T> {
pub messages: BTreeMap<A, M>,
pub timers: BTreeMap<A, T>,
pub messages: BTreeMap<A, Vec<M>>,
pub timers: BTreeMap<A, Vec<(u32, Duration, T)>>, // TODO
pub timer_count: u32,
pub now: Duration,
}

#[derive(Debug)]
pub struct NetworkContext<'a, A, M, T> {
state: &'a mut Network<A, M, T>,
pub addr: A,
}

impl<A, M, T> Network<A, M, T> {
pub fn context(&mut self, addr: A) -> NetworkContext<'_, A, M, T> {
NetworkContext { state: self, addr }
}
}

impl<A: Ord + Debug, M: Into<N>, N, T> SendEvent<Send<A, M>> for NetworkContext<'_, A, N, T> {
fn send(&mut self, Send(remote, message): Send<A, M>) -> anyhow::Result<()> {
let Some(inbox) = self.state.messages.get_mut(&remote) else {
anyhow::bail!("missing inbox for addr {remote:?}")
};
inbox.push(message.into());
Ok(())
}
}

impl<A: Ord + Debug, M, T> NetworkContext<'_, A, M, T> {
pub fn set(&mut self, period: Duration, event: T) -> anyhow::Result<TimerId> {
let Some(inbox) = self.state.timers.get_mut(&self.addr) else {
anyhow::bail!("missing inbox for addr {:?}", self.addr)
};
self.state.timer_count += 1;
let id = self.state.timer_count;
inbox.push((id, self.state.now + period, event));
Ok(TimerId(id))
}

pub fn unset(&mut self, TimerId(id): TimerId) -> anyhow::Result<()> {
let Some(inbox) = self.state.timers.get_mut(&self.addr) else {
anyhow::bail!("missing inbox for addr {:?}", self.addr)
};
let Some(pos) = inbox.iter().position(|(other_id, _, _)| *other_id == id) else {
anyhow::bail!("missing timer {:?}", TimerId(id))
};
inbox.remove(pos);
Ok(())
}
}
75 changes: 73 additions & 2 deletions src/unreplicated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,91 @@ pub mod codec {
}

pub mod model {
use crate::workload::app::kvstore::KVStore;
use derive_more::From;

use crate::{
event::combinators::Transient,
model::{Network, NetworkContext},
workload::app::kvstore::KVStore,
};

use super::*;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Addr {
Client(u8),
Server,
}

impl crate::net::Addr for Addr {}

#[derive(Debug, From)]
pub enum Message {
Request(super::Request<Addr>),
Reply(super::Reply),
}

#[derive(Debug)]
pub enum Timer {
ClientResend,
}

pub struct State {
clients: Vec<ClientState<Addr>>,
client_upcalls: Vec<Transient<InvokeOk<Bytes>>>,
server: ServerState<KVStore>,
network: Network<Addr, Message, Timer>,
}

impl super::ClientContext<Addr> for NetworkContext<'_, Addr, Message, Timer> {
type Net = Self;
type Schedule = Self;
type Upcall = Self;
fn net(&mut self) -> &mut Self::Net {
self
}
fn schedule(&mut self) -> &mut Self::Schedule {
self
}
fn upcall(&mut self) -> &mut Self::Upcall {
self
}
}

impl SendEvent<Send<(), Request<Addr>>> for NetworkContext<'_, Addr, Message, Timer> {
fn send(&mut self, Send((), message): Send<(), Request<Addr>>) -> anyhow::Result<()> {
self.send(Send(Addr::Server, message))
}
}

impl ScheduleEvent<super::client::Resend> for NetworkContext<'_, Addr, Message, Timer> {
fn set(
&mut self,
period: Duration,
_: impl FnMut() -> super::client::Resend + std::marker::Send + 'static,
) -> anyhow::Result<TimerId> {
self.set(period, Timer::ClientResend)
}

fn unset(&mut self, id: TimerId) -> anyhow::Result<()> {
NetworkContext::unset(self, id)
}
}

#[allow(unused)]
impl SendEvent<InvokeOk<Bytes>> for NetworkContext<'_, Addr, Message, Timer> {
fn send(&mut self, event: InvokeOk<Bytes>) -> anyhow::Result<()> {
let Addr::Client(index) = self.addr else {
anyhow::bail!("unimplemented")
};
Ok(())
}
}

impl super::ServerContext<Addr> for NetworkContext<'_, Addr, Message, Timer> {
type Net = Self;
fn net(&mut self) -> &mut Self::Net {
self
}
}
}

0 comments on commit b7332af

Please sign in to comment.