Fix split point calculation
[dnsseed-rust] / src / datastore.rs
index 1dd976aa4e8863a33d60fca0b6eed9a4f01002bb..1a3d7c94024ce75dd1cd78dabe13dc29eadcf942 100644 (file)
@@ -31,6 +31,61 @@ pub enum AddressState {
        WasGood,
 }
 
+impl AddressState {
+       pub fn from_num(num: u8) -> Option<AddressState> {
+               match num {
+                       0x0 => Some(AddressState::Untested),
+                       0x1 => Some(AddressState::LowBlockCount),
+                       0x2 => Some(AddressState::HighBlockCount),
+                       0x3 => Some(AddressState::LowVersion),
+                       0x4 => Some(AddressState::BadVersion),
+                       0x5 => Some(AddressState::NotFullNode),
+                       0x6 => Some(AddressState::ProtocolViolation),
+                       0x7 => Some(AddressState::Timeout),
+                       0x8 => Some(AddressState::TimeoutDuringRequest),
+                       0x9 => Some(AddressState::Good),
+                       0xa => Some(AddressState::WasGood),
+                       _   => None,
+               }
+       }
+
+       pub fn to_num(&self) -> u8 {
+               match *self {
+                       AddressState::Untested => 0,
+                       AddressState::LowBlockCount => 1,
+                       AddressState::HighBlockCount => 2,
+                       AddressState::LowVersion => 3,
+                       AddressState::BadVersion => 4,
+                       AddressState::NotFullNode => 5,
+                       AddressState::ProtocolViolation => 6,
+                       AddressState::Timeout => 7,
+                       AddressState::TimeoutDuringRequest => 8,
+                       AddressState::Good => 9,
+                       AddressState::WasGood => 10,
+               }
+       }
+
+       pub fn to_str(&self) -> &'static str {
+               match *self {
+                       AddressState::Untested => "Untested",
+                       AddressState::LowBlockCount => "Low Block Count",
+                       AddressState::HighBlockCount => "High Block Count",
+                       AddressState::LowVersion => "Low Version",
+                       AddressState::BadVersion => "Bad Version",
+                       AddressState::NotFullNode => "Not Full Node",
+                       AddressState::ProtocolViolation => "Protocol Violation",
+                       AddressState::Timeout => "Timeout",
+                       AddressState::TimeoutDuringRequest => "Timeout During Request",
+                       AddressState::Good => "Good",
+                       AddressState::WasGood => "Was Good",
+               }
+       }
+
+       pub fn get_count() -> u8 {
+               11
+       }
+}
+
 #[derive(Hash, PartialEq, Eq)]
 pub enum U64Setting {
        ConnsPerSec,
@@ -53,14 +108,14 @@ struct Node {
 }
 
 struct Nodes {
-       good_node_services: HashMap<u8, HashSet<SocketAddr>>,
+       good_node_services: Vec<HashSet<SocketAddr>>,
        nodes_to_state: HashMap<SocketAddr, Node>,
-       state_next_scan: HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
+       state_next_scan: Vec<Vec<(Instant, SocketAddr)>>,
 }
 struct NodesMutRef<'a> {
-       good_node_services: &'a mut HashMap<u8, HashSet<SocketAddr>>,
+       good_node_services: &'a mut Vec<HashSet<SocketAddr>>,
        nodes_to_state: &'a mut HashMap<SocketAddr, Node>,
-       state_next_scan: &'a mut HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
+       state_next_scan: &'a mut Vec<Vec<(Instant, SocketAddr)>>,
 
 }
 impl Nodes {
@@ -137,21 +192,13 @@ impl Store {
 
                macro_rules! nodes_uninitd {
                        () => { {
-                               let mut state_vecs = HashMap::with_capacity(11);
-                               state_vecs.insert(AddressState::Untested, Vec::new());
-                               state_vecs.insert(AddressState::LowBlockCount, Vec::new());
-                               state_vecs.insert(AddressState::HighBlockCount, Vec::new());
-                               state_vecs.insert(AddressState::LowVersion, Vec::new());
-                               state_vecs.insert(AddressState::BadVersion, Vec::new());
-                               state_vecs.insert(AddressState::NotFullNode, Vec::new());
-                               state_vecs.insert(AddressState::ProtocolViolation, Vec::new());
-                               state_vecs.insert(AddressState::Timeout, Vec::new());
-                               state_vecs.insert(AddressState::TimeoutDuringRequest, Vec::new());
-                               state_vecs.insert(AddressState::Good, Vec::new());
-                               state_vecs.insert(AddressState::WasGood, Vec::new());
-                               let mut good_node_services = HashMap::with_capacity(64);
-                               for i in 0..64 {
-                                       good_node_services.insert(i, HashSet::new());
+                               let mut state_vecs = Vec::with_capacity(AddressState::get_count() as usize);
+                               for _ in 0..AddressState::get_count() {
+                                       state_vecs.push(Vec::new());
+                               }
+                               let mut good_node_services = Vec::with_capacity(64);
+                               for _ in 0..64 {
+                                       good_node_services.push(HashSet::new());
                                }
                                Nodes {
                                        good_node_services,
@@ -185,19 +232,9 @@ impl Store {
                                let state = try_read!(line_iter, u8);
                                let last_services = try_read!(line_iter, u64);
                                let node = Node {
-                                       state: match state {
-                                               0x0 => AddressState::Untested,
-                                               0x1 => AddressState::LowBlockCount,
-                                               0x2 => AddressState::HighBlockCount,
-                                               0x3 => AddressState::LowVersion,
-                                               0x4 => AddressState::BadVersion,
-                                               0x5 => AddressState::NotFullNode,
-                                               0x6 => AddressState::ProtocolViolation,
-                                               0x7 => AddressState::Timeout,
-                                               0x8 => AddressState::TimeoutDuringRequest,
-                                               0x9 => AddressState::Good,
-                                               0xa => AddressState::WasGood,
-                                               _   => return future::ok(res),
+                                       state: match AddressState::from_num(state) {
+                                               Some(v) => v,
+                                               None => return future::ok(res),
                                        },
                                        last_services,
                                        last_update: Instant::now(),
@@ -206,11 +243,11 @@ impl Store {
                                if node.state == AddressState::Good {
                                        for i in 0..64 {
                                                if node.last_services & (1 << i) != 0 {
-                                                       res.good_node_services.get_mut(&i).unwrap().insert(sockaddr);
+                                                       res.good_node_services[i].insert(sockaddr);
                                                }
                                        }
                                }
-                               res.state_next_scan.get_mut(&node.state).unwrap().push((Instant::now(), sockaddr));
+                               res.state_next_scan[node.state.to_num() as usize].push((Instant::now(), sockaddr));
                                res.nodes_to_state.insert(sockaddr, node);
                        }
                        future::ok(res)
@@ -236,7 +273,7 @@ impl Store {
        }
 
        pub fn get_node_count(&self, state: AddressState) -> usize {
-               self.nodes.read().unwrap().state_next_scan.get(&state).unwrap().len()
+               self.nodes.read().unwrap().state_next_scan[state.to_num() as usize].len()
        }
 
        pub fn get_regex(&self, _setting: RegexSetting) -> Arc<Regex> {
@@ -247,61 +284,76 @@ impl Store {
                *self.subver_regex.write().unwrap() = Arc::new(value);
        }
 
-       pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) {
+       pub fn add_fresh_addrs<I: Iterator<Item=SocketAddr>>(&self, addresses: I) -> u64 {
+               let mut res = 0;
                let mut nodes = self.nodes.write().unwrap();
                let cur_time = Instant::now();
-               for &(_, ref addr) in addresses {
-                       if let Ok(socketaddr) = addr.socket_addr() {
-                               match nodes.nodes_to_state.entry(socketaddr.clone()) {
-                                       hash_map::Entry::Vacant(e) => {
-                                               e.insert(Node {
-                                                       state: AddressState::Untested,
-                                                       last_services: 0,
-                                                       last_update: cur_time,
-                                                       last_good: Instant::now(),
-                                               });
-                                               nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, socketaddr));
-                                       },
-                                       hash_map::Entry::Occupied(_) => {},
-                               }
-                       } else {
-                               //TODO: Handle onions
+               for addr in addresses {
+                       match nodes.nodes_to_state.entry(addr.clone()) {
+                               hash_map::Entry::Vacant(e) => {
+                                       e.insert(Node {
+                                               state: AddressState::Untested,
+                                               last_services: 0,
+                                               last_update: cur_time,
+                                               last_good: cur_time,
+                                       });
+                                       nodes.state_next_scan[AddressState::Untested.to_num() as usize].push((cur_time, addr));
+                                       res += 1;
+                               },
+                               hash_map::Entry::Occupied(_) => {},
                        }
                }
+               res
        }
 
-       pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> bool {
+       pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) {
+               self.add_fresh_addrs(addresses.iter().filter_map(|(_, addr)| {
+                       match addr.socket_addr() {
+                               Ok(socketaddr) => Some(socketaddr),
+                               Err(_) => None, // TODO: Handle onions
+                       }
+               }));
+       }
+
+       pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> AddressState {
                let mut nodes_lock = self.nodes.write().unwrap();
                let nodes = nodes_lock.borrow_mut();
-               let state_ref = nodes.nodes_to_state.get_mut(&addr).unwrap();
-               let ret = state != state_ref.state;
-               state_ref.last_update = Instant::now();
+               let now = Instant::now();
+
+               let state_ref = nodes.nodes_to_state.entry(addr).or_insert(Node {
+                       state: AddressState::Untested,
+                       last_services: 0,
+                       last_update: now,
+                       last_good: now,
+               });
+               let ret = state_ref.state;
                if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
                                && state != AddressState::Good
-                               && state_ref.last_good >= state_ref.last_update + Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
+                               && state_ref.last_good >= now - Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
                        state_ref.state = AddressState::WasGood;
                        for i in 0..64 {
                                if state_ref.last_services & (1 << i) != 0 {
-                                       nodes.good_node_services.get_mut(&i).unwrap().remove(&addr);
+                                       nodes.good_node_services[i].remove(&addr);
                                }
                        }
                        state_ref.last_services = 0;
-                       nodes.state_next_scan.get_mut(&AddressState::WasGood).unwrap().push((state_ref.last_update, addr));
+                       nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push((now, addr));
                } else {
                        state_ref.state = state;
                        if state == AddressState::Good {
                                for i in 0..64 {
                                        if services & (1 << i) != 0 && state_ref.last_services & (1 << i) == 0 {
-                                               nodes.good_node_services.get_mut(&i).unwrap().insert(addr);
+                                               nodes.good_node_services[i].insert(addr);
                                        } else if services & (1 << i) == 0 && state_ref.last_services & (1 << i) != 0 {
-                                               nodes.good_node_services.get_mut(&i).unwrap().remove(&addr);
+                                               nodes.good_node_services[i].remove(&addr);
                                        }
                                }
                                state_ref.last_services = services;
-                               state_ref.last_good = state_ref.last_update;
+                               state_ref.last_good = now;
                        }
-                       nodes.state_next_scan.get_mut(&state).unwrap().push((state_ref.last_update, addr));
+                       nodes.state_next_scan[state.to_num() as usize].push((now, addr));
                }
+               state_ref.last_update = now;
                ret
        }
 
@@ -341,19 +393,7 @@ impl Store {
                                for (ref sockaddr, ref node) in nodes.nodes_to_state.iter() {
                                        nodes_buff += &sockaddr.to_string();
                                        nodes_buff += ",";
-                                       nodes_buff += &match node.state {
-                                               AddressState::Untested => 0u8,
-                                               AddressState::LowBlockCount => 1u8,
-                                               AddressState::HighBlockCount => 2u8,
-                                               AddressState::LowVersion => 3u8,
-                                               AddressState::BadVersion => 4u8,
-                                               AddressState::NotFullNode => 5u8,
-                                               AddressState::ProtocolViolation => 6u8,
-                                               AddressState::Timeout => 7u8,
-                                               AddressState::TimeoutDuringRequest => 8u8,
-                                               AddressState::Good => 9u8,
-                                               AddressState::WasGood => 10u8,
-                                       }.to_string();
+                                       nodes_buff += &node.state.to_num().to_string();
                                        nodes_buff += ",";
                                        nodes_buff += &node.last_services.to_string();
                                        nodes_buff += "\n";
@@ -378,7 +418,7 @@ impl Store {
                                        if i.count_ones() == 1 {
                                                for j in 0..64 {
                                                        if i & (1 << j) != 0 {
-                                                               let set_ref = nodes.good_node_services.get(&j).unwrap();
+                                                               let set_ref = &nodes.good_node_services[j];
                                                                v4_set = set_ref.iter().filter(|e| e.is_ipv4() && e.port() == 8333)
                                                                        .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect();
                                                                v6_set = set_ref.iter().filter(|e| e.is_ipv6() && e.port() == 8333)
@@ -392,17 +432,17 @@ impl Store {
                                                for j in 0..64 {
                                                        if i & (1 << j) != 0 {
                                                                if first_set == None {
-                                                                       first_set = Some(nodes.good_node_services.get(&j).unwrap());
+                                                                       first_set = Some(&nodes.good_node_services[j]);
                                                                } else {
-                                                                       second_set = Some(nodes.good_node_services.get(&j).unwrap());
+                                                                       second_set = Some(&nodes.good_node_services[j]);
                                                                        break;
                                                                }
                                                        }
                                                }
-                                               v4_set = first_set.unwrap().intersection(second_set.unwrap())
+                                               v4_set = first_set.unwrap().intersection(&second_set.unwrap())
                                                        .filter(|e| e.is_ipv4() && e.port() == 8333)
                                                        .choose_multiple(&mut rng, 21).iter().map(|e| e.ip()).collect();
-                                               v6_set = first_set.unwrap().intersection(second_set.unwrap())
+                                               v6_set = first_set.unwrap().intersection(&second_set.unwrap())
                                                        .filter(|e| e.is_ipv6() && e.port() == 8333)
                                                        .choose_multiple(&mut rng, 12).iter().map(|e| e.ip()).collect();
                                        } else {
@@ -412,10 +452,10 @@ impl Store {
                                                for j in 0..64 {
                                                        if i & (1 << j) != 0 {
                                                                if intersection_set_ref == None {
-                                                                       intersection_set_ref = Some(nodes.good_node_services.get(&j).unwrap());
+                                                                       intersection_set_ref = Some(&nodes.good_node_services[j]);
                                                                } else {
                                                                        let new_intersection = intersection_set_ref.unwrap()
-                                                                               .intersection(nodes.good_node_services.get(&j).unwrap()).map(|e| (*e).clone()).collect();
+                                                                               .intersection(&nodes.good_node_services[j]).map(|e| (*e).clone()).collect();
                                                                        intersection = Some(new_intersection);
                                                                        intersection_set_ref = Some(intersection.as_ref().unwrap());
                                                                }
@@ -447,17 +487,22 @@ impl Store {
        }
 
        pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {
-               let mut res = Vec::with_capacity(600);
+               let results = 30 * self.get_u64(U64Setting::ConnsPerSec) as usize;
+               let per_bucket_results = results / (AddressState::get_count() as usize);
+               let mut res = Vec::with_capacity(results);
                let cur_time = Instant::now();
-               let mut nodes = self.nodes.write().unwrap();
-               for (state, state_nodes) in nodes.state_next_scan.iter_mut() {
-                       let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(*state)));
-                       let split_point = cmp::min(cmp::min(600 - res.len(), 60),
-                                       state_nodes.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx));
-                       let mut new_nodes = state_nodes.split_off(split_point);
-                       mem::swap(&mut new_nodes, state_nodes);
-                       for (_, node) in new_nodes.drain(..) {
-                               res.push(node);
+
+               {
+                       let mut nodes = self.nodes.write().unwrap();
+                       for (idx, state_nodes) in nodes.state_next_scan.iter_mut().enumerate() {
+                               let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(AddressState::from_num(idx as u8).unwrap())));
+                               let split_point = cmp::min(cmp::min(results - res.len(), (per_bucket_results * (idx + 1)) - res.len()),
+                                               state_nodes.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx));
+                               let mut new_nodes = state_nodes.split_off(split_point);
+                               mem::swap(&mut new_nodes, state_nodes);
+                               for (_, node) in new_nodes.drain(..) {
+                                       res.push(node);
+                               }
                        }
                }
                res.shuffle(&mut thread_rng());