netapp/src/test.rs

132 lines
3 KiB
Rust
Raw Normal View History

2021-10-13 16:07:34 +00:00
use std::sync::Arc;
use std::time::Duration;
2021-10-13 16:05:49 +00:00
use tokio::select;
use tokio::sync::watch;
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
use tokio_unix_tcp::NamedSocketAddr;
2021-10-13 16:05:49 +00:00
use sodiumoxide::crypto::auth;
use sodiumoxide::crypto::sign::ed25519;
use crate::netapp::*;
2021-10-13 16:07:34 +00:00
use crate::peering::fullmesh::*;
2021-10-13 16:05:49 +00:00
use crate::NodeID;
2021-10-13 16:07:34 +00:00
#[tokio::test(flavor = "current_thread")]
2021-10-13 16:05:49 +00:00
async fn test_with_basic_scheduler() {
env_logger::init();
2021-10-13 16:05:49 +00:00
run_test().await
}
2021-10-13 16:07:34 +00:00
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
2021-10-13 16:05:49 +00:00
async fn test_with_threaded_scheduler() {
run_test().await
}
async fn run_test() {
select! {
_ = run_test_inner() => (),
_ = tokio::time::sleep(Duration::from_secs(20)) => panic!("timeout"),
}
}
async fn run_test_inner() {
let netid = auth::gen_key();
let (pk1, sk1) = ed25519::gen_keypair();
let (pk2, sk2) = ed25519::gen_keypair();
let (pk3, sk3) = ed25519::gen_keypair();
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let addr1: NamedSocketAddr = "127.0.0.1:19991".parse().unwrap();
let addr2: NamedSocketAddr = "127.0.0.1:19992".parse().unwrap();
let addr3: NamedSocketAddr = "127.0.0.1:19993".parse().unwrap();
2021-10-13 16:05:49 +00:00
let (stop_tx, stop_rx) = watch::channel(false);
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let (thread1, _netapp1, peering1) = run_netapp(
netid.clone(),
pk1,
sk1,
addr1.clone(),
vec![],
stop_rx.clone(),
);
2021-10-13 16:05:49 +00:00
tokio::time::sleep(Duration::from_secs(2)).await;
// Connect second node and check it peers with everyone
2021-10-13 16:07:34 +00:00
let (thread2, _netapp2, peering2) = run_netapp(
netid.clone(),
pk2,
sk2,
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
addr2.clone(),
vec![(pk1, addr1.into())],
2021-10-13 16:07:34 +00:00
stop_rx.clone(),
);
2021-10-13 16:05:49 +00:00
tokio::time::sleep(Duration::from_secs(5)).await;
let pl1 = peering1.get_peer_list();
println!("A pl1: {:?}", pl1);
assert_eq!(pl1.len(), 2);
let pl2 = peering2.get_peer_list();
println!("A pl2: {:?}", pl2);
assert_eq!(pl2.len(), 2);
// Connect third ndoe and check it peers with everyone
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
let (thread3, _netapp3, peering3) = run_netapp(
netid,
pk3,
sk3,
addr3,
vec![(pk2, addr2.into())],
stop_rx.clone(),
);
2021-10-13 16:05:49 +00:00
tokio::time::sleep(Duration::from_secs(5)).await;
let pl1 = peering1.get_peer_list();
println!("B pl1: {:?}", pl1);
assert_eq!(pl1.len(), 3);
let pl2 = peering2.get_peer_list();
println!("B pl2: {:?}", pl2);
assert_eq!(pl2.len(), 3);
let pl3 = peering3.get_peer_list();
println!("B pl3: {:?}", pl3);
assert_eq!(pl3.len(), 3);
// Send stop signal and wait for everyone to finish
stop_tx.send(true).unwrap();
thread1.await.unwrap();
thread2.await.unwrap();
thread3.await.unwrap();
}
fn run_netapp(
netid: auth::Key,
_pk: NodeID,
sk: ed25519::SecretKey,
everywhere: support unix sockets This patch adds support for listening on and connecting to unix sockets. This requires having wrapper types for various tokio specific network abstractions while also supporting things like serialization and deserialization. Unfortionately i was unable to find a published crate fulfilling these requirements. For this reason I've published a crate myself. Called `tokio-unix-tcp`, it serves as a drop in replacement for Tokio's TCP and Unix network types. I plan to maintain this library outside the scope of this project as well, in general the code should be simple and stable enough however to not require maintainance going forward. As i said this crate aims to support the requirement mentioned above. In addition to this it also strives to be more correct about handling the different types of unix sockets, which the libraries i reviewed were weak at. A list of these crates can be found in the crate README under "Related work". --- The changes to netapp can be summarized as the following: - `std::net::SocketAddr` has been replaced by `tokio_unix_tcp::NamedSocketAddr` in most places. This enum encapsulates a IP address and port as well as a path in its variants and describes a concrete socket address netapp can bind or connect to. - In some places `tokio_unix_tcp::SocketAddr` is used instead of `tokio_unix_tcp::NamedSocketAddr` as mentioned above. This is due to the way unix sockets work: The remote peer of a client from the perspective of a server is not a concrete path but `unnamed`. They just share a file descriptor for the actual communication channel. The local address of the server is the actual file system path the server is listening on. In some cases netapp might be configured to connect to another peer using a unix socket and to not send a reachable IP address and port or unix socket path using the `HelloMessage`. As per the above (the client's remote address will be `unnamed`), we have no way of connecting back to that peer. This will currently cause the connection to be aborted by the server. - Listening on Unix sockets requires some additional handling like removing a previous file at the bind path and setting a correct mode (defaulting to `0o222` currently). This is handled by `tokio_unix_tcp`. --- I've tested these changes by including them in garage and running basic administration commands against a node and by running the unit tests here. Basalt peering is currently lacking a proper cost calculation for unix sockets - I'm sadly not familiar with this code.
2023-10-20 00:38:41 +00:00
listen_addr: NamedSocketAddr,
bootstrap_peers: Vec<(NodeID, NamedSocketAddr)>,
2021-10-13 16:07:34 +00:00
must_exit: watch::Receiver<bool>,
) -> (
tokio::task::JoinHandle<()>,
Arc<NetApp>,
Arc<FullMeshPeeringStrategy>,
) {
2022-02-21 16:11:15 +00:00
let netapp = NetApp::new(0u64, netid, sk);
2021-10-18 09:29:41 +00:00
let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers, None);
2021-10-13 16:05:49 +00:00
let peering2 = peering.clone();
let netapp2 = netapp.clone();
let fut = tokio::spawn(async move {
tokio::join!(
netapp2.listen(listen_addr, None, must_exit.clone()),
peering2.run(must_exit.clone()),
);
});
(fut, netapp, peering)
}