Skip to content

Commit 3091304

Browse files
fix(loadgen): resolve review blockers for scenario load generator
- Prevent stalled connect false-positive: track in-flight and established connects in Shared, record error on cancel, abort step if zero clients connected, fail run on cancelled tasks, and add regression test - Differentiate admission limit from throttle in stats: treat 'shared admission unavailable' as error rather than throttling - Fix thread depth width bug in seed: chain replies depth-by-depth and tag root and parent correctly per NIP-10 - Prevent community takeover: enforce create_only and verify existing host ownership before proceeding - Disallow ports in host template when connect_addr is set and strip port in DNS resolve map - Enforce seed_concurrency >= 1 to prevent indefinite buffer_unordered(0) hangs Signed-off-by: presentyourlove <[email protected]>
1 parent 3f4a4bf commit 3091304

5 files changed

Lines changed: 241 additions & 37 deletions

File tree

‎crates/buzz-test-client/src/loadgen/net.rs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ impl Net {
3030
.ok_or_else(|| anyhow::anyhow!("connect_addr {addr} did not resolve"))?;
3131
for community in scenario.communities() {
3232
if let Some(host) = &community.host {
33-
builder = builder.resolve(host, resolved);
33+
let bare_host = host.split(':').next().unwrap_or(host);
34+
builder = builder.resolve(bare_host, resolved);
3435
}
3536
}
3637
}
@@ -90,6 +91,26 @@ impl Net {
9091
Ok((status, response.text().await?))
9192
}
9293

94+
/// `GET {base}{path}` with a NIP-98 header; returns status and body.
95+
pub async fn get(
96+
&self,
97+
keys: &Keys,
98+
base: &str,
99+
path: &str,
100+
) -> anyhow::Result<(reqwest::StatusCode, String)> {
101+
let url = format!("{base}{path}");
102+
let request = self
103+
.http
104+
.get(&url)
105+
.header(
106+
"Authorization",
107+
identity::nip98_header(keys, "GET", &url, &[]),
108+
);
109+
let response = request.send().await?;
110+
let status = response.status();
111+
Ok((status, response.text().await?))
112+
}
113+
93114
/// Submits `event` through HTTP `/events`; errors unless the relay accepted it.
94115
pub async fn submit(
95116
&self,

‎crates/buzz-test-client/src/loadgen/run.rs‎

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Drives the simulated population through the scenario's ramp.
22
33
use std::collections::HashMap;
4+
use std::sync::atomic::{AtomicUsize, Ordering};
45
use std::sync::Arc;
56
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
67

@@ -64,6 +65,8 @@ struct Shared {
6465
stop: watch::Receiver<bool>,
6566
/// Seeded thread roots per channel id, resolved lazily by whichever client needs them first.
6667
roots: Mutex<HashMap<String, Arc<Vec<String>>>>,
68+
in_flight_connects: AtomicUsize,
69+
established_clients: AtomicUsize,
6770
}
6871

6972
/// Runs every ramp step and returns the JSON report; `Err` when a step aborted.
@@ -77,6 +80,8 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
7780
stats: stats.clone(),
7881
stop,
7982
roots: Mutex::new(HashMap::new()),
83+
in_flight_connects: AtomicUsize::new(0),
84+
established_clients: AtomicUsize::new(0),
8085
});
8186
let scenario = &shared.scenario;
8287
let pop = &scenario.population;
@@ -128,12 +133,16 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
128133
let lost = tasks.iter().filter(|t| t.is_finished()).count();
129134
let reason = if lost > 0 {
130135
Some(format!("{lost} clients exited unexpectedly"))
136+
} else if let Some(reason) = abort_reason(
137+
&summaries,
138+
scenario.abort.max_error_rate,
139+
scenario.abort.max_p99_ms,
140+
) {
141+
Some(reason)
142+
} else if !tasks.is_empty() && shared.established_clients.load(Ordering::SeqCst) == 0 {
143+
Some("zero clients connected".into())
131144
} else {
132-
abort_reason(
133-
&summaries,
134-
scenario.abort.max_error_rate,
135-
scenario.abort.max_p99_ms,
136-
)
145+
None
137146
};
138147
let report = json!({
139148
"step": n,
@@ -168,7 +177,7 @@ pub async fn run(scenario: Scenario, net: Net, shard: Shard) -> Result<Value, Va
168177
"clients_panicked": panicked,
169178
"clients_cancelled": cancelled,
170179
});
171-
if aborted || panicked > 0 {
180+
if aborted || panicked > 0 || cancelled > 0 {
172181
Err(report)
173182
} else {
174183
Ok(report)
@@ -378,13 +387,22 @@ async fn connect(
378387
) -> Option<NostrWsConnection> {
379388
while !stopped(shared) {
380389
let t0 = Instant::now();
390+
shared.in_flight_connects.fetch_add(1, Ordering::SeqCst);
381391
let attempt = tokio::select! {
382-
attempt = shared.net.connect(community, keys, auth) => attempt,
383-
_ = stopping(shared) => return None,
392+
attempt = shared.net.connect(community, keys, auth) => {
393+
shared.in_flight_connects.fetch_sub(1, Ordering::SeqCst);
394+
attempt
395+
}
396+
_ = stopping(shared) => {
397+
shared.in_flight_connects.fetch_sub(1, Ordering::SeqCst);
398+
shared.stats.err(Op::WsConnect, "connection attempt abandoned on stop");
399+
return None;
400+
}
384401
};
385402
match attempt {
386403
Ok(conn) => {
387404
shared.stats.ok(Op::WsConnect, t0.elapsed());
405+
shared.established_clients.fetch_add(1, Ordering::SeqCst);
388406
return Some(conn);
389407
}
390408
Err(e) => shared.stats.err(Op::WsConnect, &e.to_string()),
@@ -587,7 +605,9 @@ async fn human(shared: Arc<Shared>, global: usize) {
587605
if rng.unit() < profile.thread_reply_fraction {
588606
let roots = roots(&shared, community, &keys, channel).await;
589607
if !roots.is_empty() {
590-
tags.push(tag(["e", &roots[rng.below(roots.len())], "", "reply"]));
608+
let root = &roots[rng.below(roots.len())];
609+
tags.push(tag(["e", root, "", "root"]));
610+
tags.push(tag(["e", root, "", "reply"]));
591611
}
592612
}
593613
let event = EventBuilder::new(
@@ -1074,4 +1094,57 @@ hold_secs = 1
10741094
assert_eq!(report["clients_cancelled"], 0);
10751095
held.abort();
10761096
}
1097+
1098+
#[tokio::test]
1099+
async fn stalled_connects_with_long_timeout_aborts_run() {
1100+
// A peer that accepts TCP but never answers the WebSocket upgrade.
1101+
// When connect_timeout_secs > hold_secs, the step must still abort because 0 clients connected.
1102+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1103+
let addr = listener.local_addr().unwrap();
1104+
let held = tokio::spawn(async move {
1105+
let mut open = Vec::new();
1106+
while let Ok((socket, _)) = listener.accept().await {
1107+
open.push(socket);
1108+
}
1109+
});
1110+
let scenario = Scenario::parse(&format!(
1111+
r#"
1112+
run_id = "t"
1113+
[target]
1114+
ws_url = "ws://{addr}"
1115+
http_url = "http://{addr}"
1116+
connect_timeout_secs = 5.0
1117+
[shape]
1118+
communities = 1
1119+
channels_per_community = 1
1120+
[population]
1121+
humans = 2
1122+
agents = 0
1123+
channels_per_member = 1
1124+
connect_rate_per_sec = 100.0
1125+
[[ramp]]
1126+
users = 1.0
1127+
hold_secs = 1
1128+
"#
1129+
))
1130+
.unwrap();
1131+
let net = Net::new(&scenario).await.unwrap();
1132+
let started = Instant::now();
1133+
let report = tokio::time::timeout(
1134+
Duration::from_secs(10),
1135+
run(scenario, net, Shard { index: 0, count: 1 }),
1136+
)
1137+
.await
1138+
.expect("run returns when step aborts on 0 connected clients")
1139+
.expect_err("a step whose clients fail to connect must abort");
1140+
assert!(started.elapsed() < Duration::from_secs(5));
1141+
let steps = report["steps"].as_array().unwrap();
1142+
assert_eq!(steps.len(), 1);
1143+
assert_eq!(steps[0]["aborted"], true);
1144+
assert_eq!(
1145+
steps[0]["abort_reason"].as_str().unwrap(),
1146+
"zero clients connected"
1147+
);
1148+
held.abort();
1149+
}
10771150
}

‎crates/buzz-test-client/src/loadgen/scenario.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,22 @@ impl Scenario {
195195
if self.shape.communities == 0 || self.shape.channels_per_community == 0 {
196196
return Err("shape needs at least one community and one channel".into());
197197
}
198+
if self.shape.seed_concurrency == 0 {
199+
return Err("shape.seed_concurrency must be at least 1".into());
200+
}
198201
if self.shape.communities > 1 && self.target.community_host_template.is_none() {
199202
return Err("more than one community needs target.community_host_template".into());
200203
}
204+
if self.target.connect_addr.is_some() {
205+
if let Some(template) = &self.target.community_host_template {
206+
if template.contains(':') {
207+
return Err(
208+
"target.community_host_template cannot include a port when connect_addr is set"
209+
.into(),
210+
);
211+
}
212+
}
213+
}
201214
if self.population.channels_per_member == 0
202215
|| self.population.channels_per_member > self.shape.channels_per_community
203216
{
@@ -358,11 +371,17 @@ hold_secs = 10
358371
#[test]
359372
fn invalid_shapes_are_rejected() {
360373
assert!(Scenario::parse(&MINIMAL.replace("communities = 1", "communities = 2")).is_err());
374+
assert!(Scenario::parse(&format!("{MINIMAL}\n[shape]\nseed_concurrency = 0\n")).is_err());
361375
assert!(Scenario::parse(
362376
&MINIMAL.replace("channels_per_member = 2", "channels_per_member = 5")
363377
)
364378
.is_err());
365379
assert!(Scenario::parse(&MINIMAL.replace("users = 1.0", "users = 0.0")).is_err());
380+
let template_with_port = MINIMAL.replace(
381+
"http_url = \"http://localhost:3000\"",
382+
"http_url = \"http://localhost:3000\"\nconnect_addr = \"127.0.0.1:3000\"\ncommunity_host_template = \"lg-{run}-{i}.example:8443\"",
383+
);
384+
assert!(Scenario::parse(&template_with_port).is_err());
366385
}
367386

368387
#[test]

0 commit comments

Comments
 (0)