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
4 changes: 3 additions & 1 deletion doc/concept/stats.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ The producer drains its counters every interval (one second by default) and
writes only when a track's frame changed, so silence means nothing moved, not
that the producer is gone. The track ends when the producer does, or at
depth 1 or more when its group's broadcast is unannounced; the group may
return later as a new broadcast.
return later as a new broadcast. A frame too large to cache (32 MB) ends its
track with a `FrameTooLarge` error, while the `.z` flavor, which compresses
under the limit, keeps updating.

### Totals

Expand Down
2 changes: 0 additions & 2 deletions quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ lands before the cut.
- [Binding track resolver](/quest/m1/binding-track-helper.md) - binding apps subscribe to a rendition without inlining `track` or the key
- [Exporters follow a rendition to a new track](/quest/m1/export-rendition-rebind.md) - the fMP4, TS, MKV and FLV exporters rebuild a rendition whose resolved `track` or `broadcast` changes under the same ID

- [Stats fail loud](/quest/m1/stats-frame-too-large.md) - a stats track whose frame outgrows the cache ends with FrameTooLarge instead of serving a stale snapshot

- [C++ and OBS package proof](/quest/m1/cpp-package-check.md) - the installed artifacts pass before their first release

## Related
Expand Down
20 changes: 0 additions & 20 deletions quest/m1/stats-frame-too-large.md

This file was deleted.

87 changes: 76 additions & 11 deletions rs/moq-stats/src/produce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,26 @@ impl<V: Serialize> Snapshot<V> {
}

/// Write one frame. `Some(len)` is the payload written; `None` means the
/// value was unchanged and the encoder emitted nothing.
/// value was unchanged, or the track already ended, and nothing was written.
///
/// A frame over the cache limit aborts the track with
/// [`moq_net::Error::FrameTooLarge`]: skipping it would leave readers on the
/// previous snapshot with no sign it stopped tracking.
fn update(&mut self, value: &Frame<V>) -> moq_json::Result<Option<usize>> {
if self.track.is_closed() {
return Ok(None);
}
let Some(frame) = self.encoder.update(value)? else {
return Ok(None);
};
let len = frame.payload.len();
if len as u64 > moq_net::group::MAX_CACHE_BYTES {
return Err(moq_net::Error::FrameTooLarge.into());
let err = moq_net::Error::FrameTooLarge;
if let Some(group) = self.group.take() {
let _ = group.abort(err.clone());
}
let _ = self.track.clone().abort(err.clone());
return Err(err.into());
}
if frame.keyframe {
if let Some(group) = self.group.take() {
Expand Down Expand Up @@ -568,20 +580,22 @@ impl<V: Serialize> TrackPair<V> {
}

/// Publish this drain's entries on both flavors (`{}` when there are none)
/// and clear them for the next drain; moq-json skips unchanged values.
/// and clear them for the next drain; moq-json skips unchanged values. A
/// flavor that fails ends alone: DEFLATE can shrink a frame too large for
/// the plain track, or grow one that just fits.
fn publish(&mut self, name: &str) {
self.frame.entries.sort_unstable_by(|a, b| a.0.cmp(&b.0));
let plain = match self.plain.update(&self.frame) {
Ok(len) => len,
Err(err) => {
tracing::debug!(?err, name, "stats: failed to write frame");
tracing::warn!(?err, name, "stats: failed to write frame");
None
}
};
let compressed = match self.compressed.update(&self.frame) {
Ok(len) => len,
Err(err) => {
tracing::debug!(?err, name, "stats: failed to write compressed frame");
tracing::warn!(?err, name, "stats: failed to write compressed frame");
None
}
};
Expand Down Expand Up @@ -808,10 +822,10 @@ impl<V: Serialize> TrackFamily<V> {
plain: String,
pending: PendingPair,
) {
// Defensive only: a request racing the pair's creation is fulfilled by
// `create_track` (queued) or adopted by [`Self::flush`] (parked), so it
// never reaches this with the pair already live. Rejecting is still
// safe there - the requester's retry resolves against the live track.
// A request racing the pair's creation is fulfilled by `create_track`
// (queued) or adopted by [`Self::push`] (parked), so a live pair here
// means the requested flavor ended, such as on a frame too large to
// cache. It cannot update again, so refuse it.
if self.tracks.contains_key(&plain) {
pending.reject(moq_net::Error::NotFound);
return;
Expand Down Expand Up @@ -1197,8 +1211,8 @@ impl GroupPublisher {
/// Hold a requested root's presence track, publishing its current counters
/// so the subscription resolves at once. Cumulative from here on.
fn adopt_presence(&mut self, report: &Report, root: PathOwned, pending: PendingPair) {
// Defensive only: a request for a live track is served by the broadcast
// itself. Rejecting is still safe; the retry finds the live track.
// A request for a live track is served by the broadcast itself, so a
// live pair here means the requested flavor ended and cannot update.
if self.presence.contains_key(&root) {
pending.reject(moq_net::Error::NotFound);
return;
Expand Down Expand Up @@ -1397,6 +1411,57 @@ mod tests {
}
}

/// A flavor whose frame outgrows the cache ends with `FrameTooLarge` instead
/// of leaving readers on its last snapshot, while its sibling, which DEFLATE
/// shrinks under the limit, keeps updating.
#[tokio::test]
async fn oversized_frame_aborts_only_its_flavor() {
use futures::FutureExt;
type Value = BTreeMap<String, u64>;

let broadcast = moq_net::broadcast::Info::new().produce();
let consumer = broadcast.consume();
let mut pair = TrackPair::<u64>::create(&broadcast, "t.json", Arc::new(AtomicU64::new(0))).unwrap();
let mut publish = |key: &str, value: u64| {
pair.frame.entries.push((PathOwned::from(key.to_string()), value));
pair.publish("t.json");
};
publish("small", 1);

let read = |name: &str, compression| {
let track = consumer
.track(name)
.unwrap()
.subscribe(None)
.now_or_never()
.unwrap()
.unwrap();
let mut config = moq_json::snapshot::consumer::Config::default();
config.compression = compression;
moq_json::snapshot::Consumer::<Value>::new(track, config)
};
let mut plain = read("t.json", moq_json::Compression::None);
let mut compressed = read("t.json.z", moq_json::Compression::Deflate);
let next = |reader: &mut moq_json::snapshot::Consumer<Value>| reader.next().now_or_never().unwrap();
let small = Value::from([("small".to_string(), 1)]);
assert_eq!(next(&mut plain).unwrap(), Some(small.clone()));
assert_eq!(next(&mut compressed).unwrap(), Some(small));

let huge = "a".repeat(moq_net::group::MAX_CACHE_BYTES as usize);
publish(&huge, 2);
assert!(matches!(
next(&mut plain),
Err(moq_json::Error::Net(moq_net::Error::FrameTooLarge))
));
assert_eq!(next(&mut compressed).unwrap(), Some(Value::from([(huge, 2)])));

publish("small", 3);
assert_eq!(
next(&mut compressed).unwrap(),
Some(Value::from([("small".to_string(), 3)]))
);
}

/// Build an origin producer, spawning its driver on the ambient runtime.
fn produce_origin() -> moq_net::origin::Producer {
let (producer, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::default());
Expand Down