Skip to content

Commit

Permalink
Always build token maps (#962)
Browse files Browse the repository at this point in the history
This means non-proxies take a cost that they don't need, but keeps the code simpler
  • Loading branch information
Jake-Shadle authored May 20, 2024
1 parent d59fd73 commit 2e15001
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
31 changes: 22 additions & 9 deletions crates/test/tests/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use quilkin::{
net::endpoint::Endpoint,
test::TestConfig,
};
use rand::SeedableRng;

trace_test!(relay_routing, {
struct Token {
inner: [u8; 3],
}

impl Token {
fn new() -> Self {
fn new(rng: &mut rand::rngs::SmallRng) -> Self {
const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

use rand::prelude::SliceRandom;
let mut rng = rand::thread_rng();

let mut inner = [0; 3];
for (v, slot) in CHARS
.choose_multiple(&mut rng, inner.len())
.choose_multiple(rng, inner.len())
.zip(inner.iter_mut())
{
*slot = *v;
Expand Down Expand Up @@ -52,7 +52,7 @@ trace_test!(relay_routing, {
}),
})
.unwrap(),
TokenRouter::as_filter_config(None).unwrap(),
HashedTokenRouter::as_filter_config(None).unwrap(),
])
.unwrap(),
..Default::default()
Expand All @@ -63,7 +63,7 @@ trace_test!(relay_routing, {
sc.push(
"agent",
AgentPailConfig {
endpoints: vec![("server", &["abc"])],
endpoints: vec![("server", &[])],
..Default::default()
},
&["server", "relay"],
Expand All @@ -86,17 +86,30 @@ trace_test!(relay_routing, {

let client = sandbox.client();

let mut rng = rand::rngs::SmallRng::seed_from_u64(123);

for _ in 0..5 {
let token = Token::new();
sandbox.sleep(50).await;

let mut token = Token { inner: [0; 3] };

let tokens = (0..2000)
.into_iter()
.map(|i| {
let tok = Token::new(&mut rng);
if i == 1337 {
token.inner = tok.inner;
}

tok.inner.to_vec()
})
.collect();

ap.config_file.update(|config| {
config.clusters.insert_default(
[Endpoint::with_metadata(
(std::net::Ipv6Addr::LOCALHOST, server_port).into(),
quilkin::net::endpoint::Metadata {
tokens: Some(token.inner.to_vec()).into_iter().collect(),
},
quilkin::net::endpoint::Metadata { tokens },
)]
.into(),
);
Expand Down
10 changes: 7 additions & 3 deletions src/net/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,20 @@ impl EndpointSet {
pub fn update(&mut self) {
use std::hash::{Hash, Hasher};
let mut hasher = seahash::SeaHasher::with_seeds(0, 1, 2, 3);
let mut token_map = TokenAddressMap::new();

for ep in &self.endpoints {
ep.hash(&mut hasher);

for tok in &ep.metadata.known.tokens {
let hash = seahash::hash(tok);
token_map.entry(hash).or_default().push(ep.address.clone());
}
}

self.hash = hasher.finish();
self.version += 1;
self.token_map = token_map;
}

/// Creates a map of tokens -> address for the current set
Expand Down Expand Up @@ -209,9 +216,6 @@ impl EndpointSet {
} else {
self.hash = replacement.hash;
self.version += 1;
}

if !self.token_map.is_empty() {
self.build_token_map();
}

Expand Down
2 changes: 1 addition & 1 deletion src/net/xds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) use crate::generated::quilkin::relay::v1alpha1 as relay;
pub(crate) mod client;
pub(crate) mod metrics;
mod resource;
pub(crate) mod server;
pub mod server;

use crate::net::{cluster::EndpointSetVersion, endpoint::Locality};

Expand Down

0 comments on commit 2e15001

Please sign in to comment.