Skip to content

Commit

Permalink
Minor topology manager related fixes (#889)
Browse files Browse the repository at this point in the history
* Fix topology test setup

* Refresh density cache periodically
  • Loading branch information
iduartgomez authored Nov 7, 2023
1 parent 48f6a12 commit 35e937e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
22 changes: 18 additions & 4 deletions crates/core/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,15 @@ impl Ring {
open_at: Instant::now(),
});
self.location_for_peer.write().insert(peer, loc);
std::mem::drop(cbl);
self.refresh_density_request_cache()
}

fn refresh_density_request_cache(&self) {
let cbl = self.connections_by_location.read();
let current_neighbors = &Self::current_neighbors(&cbl);
let topology_manager = &mut *self.topology_manager.write();
topology_manager
.refresh_cache(current_neighbors)
.expect("current neightbors shouldn't be empty here ever, just added at least one")
let _ = topology_manager.refresh_cache(current_neighbors);
}

/// Return the most optimal peer caching a given contract.
Expand Down Expand Up @@ -624,11 +628,14 @@ impl Ring {
#[cfg(test)]
const REMOVAL_TICK_DURATION: Duration = Duration::from_secs(1);
const ACQUIRE_CONNS_TICK_DURATION: Duration = Duration::from_secs(1);
const REGENERATE_DENSITY_MAP_INTERVAL: Duration = Duration::from_secs(60);

let mut check_interval = tokio::time::interval(REMOVAL_TICK_DURATION);
check_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut acquire_max_connections = tokio::time::interval(ACQUIRE_CONNS_TICK_DURATION);
acquire_max_connections.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut refresh_density_map = tokio::time::interval(REGENERATE_DENSITY_MAP_INTERVAL);
refresh_density_map.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

let mut missing = BTreeMap::new();

Expand All @@ -651,6 +658,7 @@ impl Ring {
}
}
}

// eventually peers which failed to return candidates should be retried when enough time has passed
let retry_missing_candidates_until = Instant::now() - retry_interval;
missing.split_off(&Reverse(retry_missing_candidates_until));
Expand Down Expand Up @@ -752,7 +760,13 @@ impl Ring {
})?;
}
}
check_interval.tick().await;

tokio::select! {
_ = refresh_density_map.tick() => {
self.refresh_density_request_cache();
}
_ = check_interval.tick() => {}
}
}
}

Expand Down
25 changes: 8 additions & 17 deletions crates/core/src/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,32 +194,23 @@ mod tests {
current_neighbors.insert(Location::new(i as f64 / 10.0), 0);
}

// Simulate a bunch of random requests clustered around 0.35
for _ in 0..NUM_REQUESTS {
let requested_location = topology_manager.random_location();
topology_manager.record_request(requested_location, TransactionType::Get);
}

topology_manager
.cached_density_map
.set(
&topology_manager.request_density_tracker,
&current_neighbors,
)
.unwrap();
let mut requests = vec![];
// Simulate a bunch of random requests clustered around 0.35
for _ in 0..NUM_REQUESTS {
let requested_location = topology_manager.random_location();
topology_manager.record_request(requested_location, TransactionType::Get);
requests.push(requested_location);
}

let target_dense_location = Location::new(
requests.iter().map(|loc| loc.as_f64()).sum::<f64>() / NUM_REQUESTS as f64,
);
let best_candidate_location = topology_manager.get_best_candidate_location().unwrap();
// Should be close to where most of the requests were done.
let distance = (best_candidate_location.as_f64() - target_dense_location.as_f64()).abs();
assert!(
distance < 0.05,
"most requests done to {target_dense_location}, but candidate is {best_candidate_location} instead, \
distance between them higher than 0.05"
);
// Should be half way between 0.3 and 0.4 as that is where the most requests were.
assert_eq!(best_candidate_location, Location::new(0.35));

// call evaluate_new_connection for locations 0.0 to 1.0 at 0.01 intervals and find the
// location with the highest score
Expand Down

0 comments on commit 35e937e

Please sign in to comment.