Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat partitions as unreachable hosts #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ categories = ["asynchronous", "network-programming", "simulation"]
[workspace]
members = ["examples/*"]

[workspace.package]
rust-version = "1.83"

[dependencies]
bytes = "1.4"
futures = "0.3"
Expand Down
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Turmoil examples

You can run these `turmoil` examples with this command: `cargo run -p <example>`. For example:

```
$ cargo run -p axum-example
# To turn on logs
$ RUST_LOG=debug cargo run -p axum-example
```
2 changes: 1 addition & 1 deletion examples/axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ turmoil = { path = "../.." }
tracing = "0.1"
tracing-subscriber = "0.3"
tokio = "1"
tower = "0.4"
tower = { version = "0.4.13", features = ["util"] }
pin-project-lite = "0.2"
2 changes: 1 addition & 1 deletion examples/grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "grpc"
name = "grpc-example"
version = "0.1.0"
edition = "2021"
publish = false
Expand Down
4 changes: 2 additions & 2 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl ToIpAddr for String {
}
}

impl<'a> ToIpAddr for &'a str {
impl ToIpAddr for &'_ str {
fn to_ip_addr(&self, dns: &mut Dns) -> IpAddr {
if let Ok(ipaddr) = self.parse() {
return ipaddr;
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ToSocketAddrs for (String, u16) {
}
}

impl<'a> ToSocketAddrs for (&'a str, u16) {
impl ToSocketAddrs for (&'_ str, u16) {
fn to_socket_addr(&self, dns: &Dns) -> SocketAddr {
// When IP address is passed directly as a str.
if let Ok(ip) = self.0.parse::<IpAddr>() {
Expand Down
41 changes: 17 additions & 24 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
use std::{
fmt::Debug,
io::{self, Error, Result},
net::SocketAddr,
pin::Pin,
sync::Arc,
task::{ready, Context, Poll},
};
use std::fmt::Debug;
use std::io::{self, Error, Result};
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{ready, Context, Poll};

use bytes::{Buf, Bytes};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
runtime::Handle,
sync::{mpsc, oneshot},
time::sleep,
};

use crate::{
envelope::{Envelope, Protocol, Segment, Syn},
host::is_same,
host::SequencedSegment,
net::SocketPair,
world::World,
ToSocketAddrs, TRACING_TARGET,
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::runtime::Handle;
use tokio::sync::{mpsc, oneshot};
use tokio::time::sleep;

use super::split_owned::{OwnedReadHalf, OwnedWriteHalf};
use crate::envelope::{Envelope, Protocol, Segment, Syn};
use crate::host::is_same;
use crate::host::SequencedSegment;
use crate::net::SocketPair;
use crate::world::World;
use crate::{ToSocketAddrs, TRACING_TARGET};

/// A simulated TCP stream between a local and a remote socket.
///
Expand Down Expand Up @@ -430,4 +423,4 @@ impl Drop for WriteHalf {
world.current_host_mut().tcp.close_stream_half(*self.pair);
})
}
}
}
72 changes: 24 additions & 48 deletions src/sim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rand::seq::SliceRandom;
use std::cell::RefCell;
use std::future::Future;
use std::net::IpAddr;
Expand All @@ -7,11 +6,12 @@ use std::sync::Arc;
use std::time::UNIX_EPOCH;

use indexmap::IndexMap;
use rand::seq::SliceRandom;
use tokio::time::Duration;
use tracing::Level;

use crate::{Config, for_pairs, LinksIter, Result, Rt, ToIpAddr, ToIpAddrs, TRACING_TARGET, World};
use crate::host::HostTimer;
use crate::{for_pairs, Config, LinksIter, Result, Rt, ToIpAddr, ToIpAddrs, World, TRACING_TARGET};

/// A handle for interacting with the simulation.
pub struct Sim<'a> {
Expand Down Expand Up @@ -430,17 +430,16 @@ impl<'a> Sim<'a> {
#[cfg(test)]
mod test {
use rand::Rng;
use std::{future, io};
use std::{
net::{IpAddr, Ipv4Addr},
rc::Rc,
sync::{
Mutex,
Arc,
atomic::{AtomicU64, Ordering},
Arc, Mutex,
},
time::Duration,
};
use std::future;

use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
Expand All @@ -450,9 +449,9 @@ mod test {

use crate::net::UdpSocket;
use crate::{
Builder, elapsed,
hold,
net::{TcpListener, TcpStream}, Result, Sim, sim_elapsed, World,
elapsed, hold,
net::{TcpListener, TcpStream},
sim_elapsed, Builder, Result, Sim, World,
};

#[test]
Expand Down Expand Up @@ -674,7 +673,9 @@ mod test {

sim.client("client", async move {
// Peers are partitioned. TCP setup should fail.
let _ = TcpStream::connect("server:1234").await.unwrap_err();
let err = TcpStream::connect("server:1234").await.unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::HostUnreachable);
assert_eq!(err.to_string(), "host unreachable");

Ok(())
});
Expand All @@ -686,7 +687,6 @@ mod test {
Ok(())
}


struct Expectation {
expect_a_receive: bool,
expect_b_receive: bool,
Expand Down Expand Up @@ -723,10 +723,9 @@ mod test {
async move {
let udp_socket =
UdpSocket::bind((IpAddr::V4(Ipv4Addr::UNSPECIFIED), 1234)).await?;
udp_socket
.send_to(&[42], format!("{}:1234", host_b))
.await
.expect("sending packet should appear to work, even if partitioned");
// If hosts are partitioned, this will return an unreachable
// error.
let _ = udp_socket.send_to(&[42], format!("{}:1234", host_b)).await;

*a_did_receive.lock().unwrap() = Some(matches!(
tokio::time::timeout(
Expand All @@ -748,10 +747,9 @@ mod test {
async move {
let udp_socket =
UdpSocket::bind((IpAddr::V4(Ipv4Addr::UNSPECIFIED), 1234)).await?;
udp_socket
.send_to(&[42], format!("{}:1234", host_a))
.await
.expect("sending packet should work");
// If hosts are partitioned, this will return an unreachable
// error.
let _ = udp_socket.send_to(&[42], format!("{}:1234", host_a)).await;

*b_did_receive.lock().unwrap() = Some(matches!(
tokio::time::timeout(
Expand Down Expand Up @@ -848,41 +846,19 @@ mod test {
Ok(())
}

run_with_actions(&[
Action::PartitionOnewayAB,
])?;
run_with_actions(&[
Action::PartitionOnewayBA,
])?;
run_with_actions(&[Action::PartitionOnewayAB])?;
run_with_actions(&[Action::PartitionOnewayBA])?;
run_with_actions(&[Action::Partition, Action::RepairOnewayAB])?;
run_with_actions(&[Action::Partition, Action::RepairOnewayBA])?;
run_with_actions(&[Action::PartitionOnewayAB, Action::Repair])?;
run_with_actions(&[Action::PartitionOnewayBA, Action::Repair])?;
run_with_actions(&[Action::PartitionOnewayBA, Action::RepairOnewayAB])?;
run_with_actions(&[Action::PartitionOnewayAB, Action::PartitionOnewayBA])?;
run_with_actions(&[
Action::Partition,
Action::RepairOnewayAB,
])?;
run_with_actions(&[
Action::Partition,
Action::RepairOnewayBA,
])?;
run_with_actions(&[
Action::PartitionOnewayAB,
Action::Repair,
])?;
run_with_actions(&[
Action::PartitionOnewayBA,
Action::Repair,
])?;
run_with_actions(&[
Action::PartitionOnewayBA,
Action::RepairOnewayAB,
])?;
run_with_actions(&[
Action::PartitionOnewayAB,
Action::PartitionOnewayBA,
])?;
run_with_actions(&[
Action::Partition,
Action::RepairOnewayAB,
Action::RepairOnewayBA
])?;

Ok(())
}
Expand Down
Loading