raft: using new p2p net settings

This commit is contained in:
ghassmo
2022-05-09 12:00:09 +03:00
committed by parazyd
parent 264dc72f03
commit 0c264469ec
2 changed files with 12 additions and 9 deletions

View File

@@ -2,12 +2,13 @@ use async_std::{
sync::{Arc, Mutex},
task,
};
use std::{cmp::min, collections::HashMap, net::SocketAddr, path::PathBuf, time::Duration};
use std::{cmp::min, collections::HashMap, path::PathBuf, time::Duration};
use async_executor::Executor;
use futures::{select, FutureExt};
use log::{debug, error, info, warn};
use rand::{rngs::OsRng, Rng, RngCore};
use url::Url;
use crate::{
net,
@@ -28,7 +29,7 @@ const TIMEOUT: u64 = 300;
const TIMEOUT_NODES: u64 = 300;
async fn load_node_ids_loop(
nodes: Arc<Mutex<HashMap<NodeId, SocketAddr>>>,
nodes: Arc<Mutex<HashMap<NodeId, Url>>>,
p2p: net::P2pPtr,
role: Role,
) -> Result<()> {
@@ -42,7 +43,7 @@ async fn load_node_ids_loop(
let nodes_ip = hosts.load_all().await.clone();
let mut nodes = nodes.lock().await;
for ip in nodes_ip.iter() {
nodes.insert(NodeId::from(*ip), *ip);
nodes.insert(NodeId::from(ip.clone()), ip.clone());
}
drop(nodes);
}
@@ -86,7 +87,7 @@ pub struct Raft<T> {
sent_length: MapLength,
acked_length: MapLength,
nodes: Arc<Mutex<HashMap<NodeId, SocketAddr>>>,
nodes: Arc<Mutex<HashMap<NodeId, Url>>>,
last_term: u64,
@@ -99,7 +100,7 @@ pub struct Raft<T> {
}
impl<T: Decodable + Encodable + Clone> Raft<T> {
pub fn new(addr: Option<SocketAddr>, db_path: PathBuf) -> Result<Self> {
pub fn new(addr: Option<Url>, db_path: PathBuf) -> Result<Self> {
if db_path.to_str().is_none() {
error!(target: "raft", "datastore path is incorrect");
return Err(Error::ParseFailed("unable to parse pathbuf to str"))
@@ -590,7 +591,7 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
}
}
fn acks(&self, nodes: HashMap<NodeId, SocketAddr>, length: u64) -> HashMap<NodeId, SocketAddr> {
fn acks(&self, nodes: HashMap<NodeId, Url>, length: u64) -> HashMap<NodeId, Url> {
nodes
.into_iter()
.filter(|n| {

View File

@@ -1,4 +1,6 @@
use std::{collections::HashMap, io, net::SocketAddr};
use std::{collections::HashMap, io};
use url::Url;
use crate::{
impl_vec,
@@ -82,8 +84,8 @@ pub struct Log {
#[derive(Clone, Debug, Eq, PartialEq, Hash, SerialDecodable, SerialEncodable)]
pub struct NodeId(pub Vec<u8>);
impl From<SocketAddr> for NodeId {
fn from(addr: SocketAddr) -> Self {
impl From<Url> for NodeId {
fn from(addr: Url) -> Self {
let ser = serialize(&addr);
let hash = blake3::hash(&ser).as_bytes().to_vec();
Self(hash)