Ver código fonte

raft: using new p2p net settings

ghassmo 4 anos atrás
pai
commit
0c264469ec
2 arquivos alterados com 12 adições e 9 exclusões
  1. 7 6
      src/raft/consensus.rs
  2. 5 3
      src/raft/primitives.rs

+ 7 - 6
src/raft/consensus.rs

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

+ 5 - 3
src/raft/primitives.rs

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