Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/bin/relay/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ relay.with_web(web).run().await?;

- `Relay::load` binds every socket, so a taken port fails there, and the
accessors report the bound addresses, including ports assigned for `:0`.
- `run` consumes the relay, so clone the handles your tasks need first.
- `run` consumes the relay, so clone the handles your tasks need first. Stats
publishing starts when `run` drives the cluster; an application building a
`Cluster` itself sets stats in `cluster::Options` and must keep polling the
returned driver while serving.
- Build on `web().routes()`: `with_web` replaces the router, so `Router::new()`
drops the built-in routes.
- A route reads the connection's peer address with axum's
Expand Down
1 change: 0 additions & 1 deletion quest/m1/poll-first/no-spawn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ never runs inside the root. It owns no state and polls no background work.

## Required

- [Admissions and stats are polled](/quest/m1/poll-first/no-spawn/admissions.md) - HTTP admissions run inside their waiter, and the stats publisher runs in the cluster's driver
- [The relay's in-memory tests run on kio's simulator](/quest/m1/poll-first/no-spawn/sim-tests.md) - `rs/moq-relay/tests/integration/connection.rs` runs on the simulator instead of paused tokio

## Related
Expand Down
39 changes: 0 additions & 39 deletions quest/m1/poll-first/no-spawn/admissions.md

This file was deleted.

4 changes: 0 additions & 4 deletions quest/m1/poll-first/no-spawn/sim-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ m2, or require the part of the spawn audit that converts those two helpers.
Decide that when it starts.

Public API: none. Wire: none.

## Required

- [Admissions and stats are polled](/quest/m1/poll-first/no-spawn/admissions.md) - admissions stop spawning on tokio
16 changes: 16 additions & 0 deletions rs/moq-relay/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ Rolling a cluster across a release also follows the [upgrade guide](https://moq.

## Unreleased

### Stats are configured in `cluster::Options` ([#7673](https://github.com/moq-dev/moq/pull/7673))

`Cluster::with_stats` is removed, and `stats::Config::build` is no longer public. Pass the stats config to `Cluster::new` instead, which builds the producer on the cluster's origin and runs its publisher in the returned `Driver`:

```rust
// Before
let (cluster, driver) = Cluster::new(options)?;
let (stats, publisher) = stats_config.build(cluster.origin.clone())?;
let cluster = cluster.with_stats(stats, publisher);

// After
let (cluster, driver) = Cluster::new(options.with_stats(stats_config))?;
```

Read the registry from `cluster.stats`; `Relay::stats` still returns the producer. Publishing runs only while the driver is polled, which `Relay::run` does, and stops when the driver drops. Building stats needs no Tokio runtime, and an invalid stats config now fails `Cluster::new`.

### The iroh transport is removed ([#7345](https://github.com/moq-dev/moq/pull/7345))

The `iroh` feature, the `[iroh]` table, the `--iroh-*` flags, and `Cluster::with_iroh` are gone.
Expand Down
123 changes: 88 additions & 35 deletions rs/moq-relay/benches/http_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
//! holds waiting on the decider. A slope means admitting one request costs more as others
//! wait, rather than what the request itself touches.
//! The real web listener runs over loopback TCP; timings include one request round trip.
#![expect(clippy::disallowed_methods, reason = "host time measures benchmark throughput")]

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use futures::{StreamExt, stream::FuturesUnordered};
use moq_auth::{Grant, lease};
use moq_relay::{auth, cluster, web};
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use std::task::Poll;
use std::time::Duration;

type Send = h2::client::SendRequest<bytes::Bytes>;
Expand All @@ -30,25 +27,35 @@ impl Drop for Fixture {
}

impl Fixture {
async fn new(connections: usize, streams: usize) -> Self {
async fn new(connections: usize, streams: usize, server: bool) -> Self {
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
let (auth, mut admissions) = auth::Auth::embedded("bench");
let waiting = Arc::new(AtomicUsize::new(0));
let decider = tokio::spawn({
let waiting = kio::Shared::new(0usize);
let (auth, decider) = if server {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let mut config = auth::Config::default();
config.url = Some(format!("http://{}/", listener.local_addr().unwrap()).parse().unwrap());
let auth = config.init("bench", &Default::default(), false).unwrap();
let app = axum::Router::new()
.route("/", axum::routing::post(decide))
.with_state(waiting.clone());
let decider = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
(auth, decider)
} else {
let (auth, mut admissions) = auth::Auth::embedded("bench");
let waiting = waiting.clone();
async move {
let decider = tokio::spawn(async move {
let mut held = Vec::new();
while let Some(admission) = admissions.next().await {
if admission.request.path.starts_with("/held") {
held.push(admission);
waiting.store(held.len(), Ordering::Release);
*waiting.lock() = held.len();
continue;
}
let grant = Grant::new(moq_auth::Patterns::new(), moq_auth::Pattern::all());
admission.grant(lease::Consumer::fixed(grant));
admission.grant(lease::Consumer::fixed(grant()));
}
}
});
});
(auth, decider)
};

let pem = rcgen::generate_simple_self_signed(vec!["localhost".into()])
.unwrap()
Expand All @@ -58,7 +65,7 @@ impl Fixture {
let mut config = web::Config::default();
config.http.listen = Some("127.0.0.1:0".parse().unwrap());
let (cluster, driver) = cluster::Cluster::new(Default::default()).unwrap();
tokio::spawn(driver.run());
let driver = tokio::spawn(async move { driver.run().await.unwrap() });
// No admission timeout, so held requests stay waiting however long setup and the
// measurement take.
let web = web::Web::new(auth, cluster, certificates, config)
Expand All @@ -71,7 +78,7 @@ impl Fixture {
let _ = web.serve(routes).await;
});

let mut tasks = vec![decider, server];
let mut tasks = vec![decider, server, driver];
let mut held = Vec::new();
for connection in 0..connections {
let mut send = connect(addr, &mut tasks).await;
Expand All @@ -84,9 +91,25 @@ impl Fixture {
held.push((send, responses));
}
let measured = connect(addr, &mut tasks).await;
while waiting.load(Ordering::Acquire) < connections * streams {
tokio::time::sleep(Duration::from_millis(1)).await;
let mut responses: FuturesUnordered<_> = held
.iter_mut()
.flat_map(|(_, responses)| responses.iter_mut())
.collect();
tokio::select! {
biased;
result = responses.next(), if !responses.is_empty() => {
panic!("a held admission completed during setup: {result:?}");
}
_ = waiting
.wait(|count| {
if **count == connections * streams {
Poll::Ready(())
} else {
Poll::Pending
}
}) => {}
}
drop(responses);

Self {
measured,
Expand All @@ -106,6 +129,22 @@ impl Fixture {
}
}

fn grant() -> Grant {
Grant::new(moq_auth::Patterns::new(), moq_auth::Pattern::all())
}

async fn decide(
axum::extract::State(waiting): axum::extract::State<kio::Shared<usize>>,
axum::Json(request): axum::Json<moq_auth::Request>,
) -> axum::Json<Grant> {
if request.path.starts_with("/held") {
*waiting.lock() += 1;
std::future::pending().await
} else {
axum::Json(grant())
}
}

async fn connect(addr: std::net::SocketAddr, tasks: &mut Vec<tokio::task::JoinHandle<()>>) -> Send {
let tcp = tokio::net::TcpStream::connect(addr).await.unwrap();
let (send, connection) = h2::client::handshake(tcp).await.unwrap();
Expand All @@ -126,24 +165,38 @@ fn admission(c: &mut Criterion) {
.enable_all()
.build()
.unwrap();
let mut group = c.benchmark_group("relay_http_admission");
group.sample_size(10);
group.warm_up_time(Duration::from_millis(100));
group.measurement_time(Duration::from_millis(300));
// Each connection may hold up to the verification budget (128 by default) at once.
for connections in [1, 16, 64] {
for streams in [0, 16, 64] {
let mut fixture = runtime.block_on(Fixture::new(connections, streams));
group.bench_with_input(
BenchmarkId::new(format!("connections={connections}"), streams),
&(connections, streams),
|b, _| {
b.iter(|| runtime.block_on(fixture.round()));
},
);
for server in [false, true] {
let name = if server {
"relay_http_auth_server"
} else {
"relay_http_admission"
};
let mut group = c.benchmark_group(name);
group.sample_size(10);
group.warm_up_time(Duration::from_millis(100));
group.measurement_time(Duration::from_millis(300));
// A held HTTP/1.1 auth POST takes two sockets, unlike a multiplexed web
// stream. Keep its full sweep below a process's standard 1024-fd budget.
let (connections, streams) = if server {
([1, 4, 16], [0, 4, 16])
} else {
([1, 16, 64], [0, 16, 64])
};
// Each connection may hold up to the verification budget (128 by default) at once.
for connections in connections {
for streams in streams {
let mut fixture = runtime.block_on(Fixture::new(connections, streams, server));
group.bench_with_input(
BenchmarkId::new(format!("connections={connections}"), streams),
&(connections, streams),
|b, _| {
b.iter(|| runtime.block_on(fixture.round()));
},
);
}
}
group.finish();
}
group.finish();
}

criterion_group!(benches, admission);
Expand Down
6 changes: 2 additions & 4 deletions rs/moq-relay/benches/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ impl Backend for Relay {
options.config.id = Some(id);
// The shared sweeps include more announcements than a default ordinary client.
options.session.announces = options.peer.announces;
let (cluster, driver) = cluster::Cluster::new(options.with_cache(cache)).unwrap();
tokio::spawn(driver.run());
let mut stats = stats::Config::default();
stats.enabled = true;
stats.node = Some(id.to_string());
let (producer, publisher) = stats.build(cluster.origin.clone()).unwrap();
let cluster = cluster.with_stats(producer, publisher);
let (cluster, driver) = cluster::Cluster::new(options.with_cache(cache).with_stats(stats)).unwrap();
tokio::spawn(driver.run());

let (auth, mut admissions) = auth::Auth::embedded(format!("bench-{id}"));
let decider = tokio::spawn(async move {
Expand Down
Loading
Loading