diff --git a/src/promql/src/native/execute.rs b/src/promql/src/native/execute.rs index 8f8113bf5da..3e1fab49ade 100644 --- a/src/promql/src/native/execute.rs +++ b/src/promql/src/native/execute.rs @@ -286,6 +286,19 @@ struct NodeSpec { input: Option, } +/// The series whose first chunk this buffer received and whose closing chunk it has not seen. +/// +/// A window reads one series at a time, to the chunk carrying `is_last`, so the chunks of one series +/// arrive together and each of them continues the one before. What is kept here is the identity of +/// the open series and the last position it decided, which is what says whether the next chunk still +/// belongs to it and still moves forward. +struct OpenSeries { + /// The identity of the open series, as the chunk that opened it named it. + metric: MetricName, + /// The last absolute position the open series decided, when it decided any. + last_position: Option, +} + /// The input a window with a [`NodeRuntime::merge`] collects until its input ends. /// /// Such a window cannot evaluate the series under one canonical label set one after another: its @@ -293,6 +306,14 @@ struct NodeSpec { /// boundary needs are exactly the ones that closed series held. The fragments are therefore kept /// whole, merged into one series per label set with /// [`crate::grid_series::merge_same_labelset`], and only then evaluated. +/// +/// Collecting is also where the contract a series keeps is checked, because the merge that follows +/// would hide a stream that broke it: it sorts the rows by position and groups them by label set, so +/// runs that went backwards inside one series and an identity that changed inside one series both +/// come out as a plausible merged series. A streaming window refuses such a stream on the chunk that +/// breaks it, and so does [`MergeInput::check`] before the chunk is kept. An input that ends inside +/// a series is the one thing left to the executor's own check: it is reported before anything is +/// flushed, because the close a merge synthesises would make such a series look like one that ended. struct MergeInput { /// The schema the fragments are addressed on: their grid and their field count. schema: Arc, @@ -305,6 +326,9 @@ struct MergeInput { /// Neither figure walks the buffer again, and the space the fragments' payload shares is /// counted once per fragment rather than deduplicated. bytes: usize, + /// The series the next chunk has to continue, if the last chunk this buffer took did not close + /// one. Nothing is kept once a series closed: the next chunk, if any, starts another series. + open: Option, } impl MergeInput { @@ -314,9 +338,74 @@ impl MergeInput { schema, fragments: Vec::new(), bytes: 0, + open: None, } } + /// Checks one received chunk against the contract a series keeps, and follows the open series. + /// + /// The checks are the ones a streaming window makes on every delivery it reads: the chunk is + /// addressed on this window's input grid, carries one sample per row in every field, holds a + /// position unless it closes its series, continues the identity of the series it belongs to, and + /// only ever moves forward within it. They cost a constant number of reads of the chunk's own + /// axis -- its first and last positions, not its rows, which the merge then copies nothing of -- + /// and they are what keeps the sort below honest: a stream that broke one of them is refused + /// here, on the chunk that broke it, rather than merged into an answer. + /// + /// A chunk carrying `is_last` closes the open series, whether it holds rows or none at all, and + /// nothing is remembered of it: the next chunk starts a series whose identity is its own. + fn check(&mut self, path: &NodePath, series: &Series, is_last: bool) -> Result<()> { + let on_grid = match (&self.schema.eval_grid, series.time.grid()) { + (Some(input), Some(grid)) => input.spec() == grid.spec(), + _ => false, + }; + if !on_grid + || series.fields.len() != self.schema.fields.len() + || series + .fields + .iter() + .any(|field| field.len() != series.time.len()) + { + return Err(DataFusionError::Execution(format!( + "the window at {path} was handed an evaluated series that does not match its input \ + schema" + ))); + } + let (first, last) = (series.time.first_position(), series.time.last_position()); + if !is_last && first.is_none() { + return Err(DataFusionError::Execution(format!( + "the window at {path} was handed an evaluated series that holds no position before \ + the last run of its series" + ))); + } + if let Some(open) = &self.open + && open.metric != series.metric + { + return Err(DataFusionError::Execution(format!( + "the window at {path} was handed an evaluated series that changed identity before \ + its last run" + ))); + } + if let Some(previous) = self.open.as_ref().and_then(|open| open.last_position) + && let Some(first) = first + && first <= previous + { + return Err(DataFusionError::Execution(format!( + "the window at {path} was handed evaluated series runs that are not increasing: \ + {first} after {previous}" + ))); + } + self.open = if is_last { + None + } else { + Some(OpenSeries { + metric: series.metric.clone(), + last_position: last, + }) + }; + Ok(()) + } + /// The bytes of the fragments and of the workspace merging them needs. /// /// The workspace is what the helper allocates for the rows the fragments hold; the sample @@ -958,6 +1047,9 @@ impl NodeRuntime { ))); }; let EvaluatedChunk { series, is_last } = chunk; + // The chunk is checked before it is charged for or kept, so a stream that breaks the + // contract leaves nothing behind. + merge.check(path, &series, is_last)?; let positions = series.time.len(); let bytes = series_bytes(&series); // The fragment is held from here on, so the pool admits it before this node keeps it: a diff --git a/src/promql/src/native/execute/tests.rs b/src/promql/src/native/execute/tests.rs index 925b9cffcec..1f2b962fd0b 100644 --- a/src/promql/src/native/execute/tests.rs +++ b/src/promql/src/native/execute/tests.rs @@ -1098,6 +1098,229 @@ fn a_pool_that_cannot_admit_a_chunk_releases_what_it_held() { ); } +/// The malformed streams a streaming window refuses are refused by the collector too. +/// +/// The window below the map collects the whole fragment stream and hands it to +/// [`crate::grid_series::merge_same_labelset`], which sorts the rows by position and groups them by +/// label set. That is what makes two closed series that share a label set one series, and it is also +/// what would hide a stream that never was one series: runs that went backwards inside a series, and +/// an identity that changed inside a series, both come out as a plausible merged series with a +/// peaceful answer. A window that read the same stream as it arrived refuses it on the chunk that +/// breaks it, and so does the collector, before the chunk is kept. +/// +/// The stream below is a single series per case and the answer it would have produced is the one +/// nothing asked for: `3` for the first (the merge would sort 1 and 2 onto positions 0 and 1), and +/// two answers for the second. +#[test] +fn a_collector_refuses_the_malformed_streams_a_streaming_window_refuses() { + let inner_grid = grid(0, 60_000, 2); + let outer_grid = grid(60_000, 60_000, 1); + let schema = evaluated_schema(Arc::clone(&inner_grid)); + let tree = || { + NativeTree::try_new(window_with( + map(evaluated_input(Arc::clone(&inner_grid)), vec![MathOp::Abs]), + RangeFunction::SumOverTime, + 120_000, + Some(Arc::clone(&inner_grid)), + Arc::clone(&outer_grid), + )) + .unwrap() + }; + + let cases = [ + ( + "runs going backwards inside one series", + evaluated_chunk_of(&schema, named_metric("a", "x"), 1, vec![Some(1.0)], false), + evaluated_chunk_of(&schema, named_metric("a", "x"), 0, vec![Some(2.0)], true), + "not increasing", + ), + ( + "an identity changing inside one series", + evaluated_chunk_of(&schema, named_metric("a", "x"), 0, vec![Some(1.0)], false), + evaluated_chunk_of(&schema, named_metric("b", "y"), 1, vec![Some(2.0)], true), + "changed identity", + ), + ]; + for (what, first, second, phrase) in cases { + let pool = pool(); + let mut execution = executor(&tree(), Arc::clone(&pool), &ExecutionPlanMetricsSet::new()); + // The chunk that only opens the series is what a stream hands over first, and it is legal: + // the collector keeps it and reports nothing. + execution.push_evaluated(first).unwrap(); + assert!( + execution.next_chunk().unwrap().is_none(), + "{what}: the collector reported a piece before its input ended" + ); + execution.push_evaluated(second).unwrap(); + let error = match execution.next_chunk() { + Ok(None) => panic!("{what} was collected as if it were one series"), + Ok(Some(chunk)) => panic!("{what} reached the evaluator as {:?}", decided(&[chunk])), + Err(error) => error, + }; + let message = error.to_string(); + assert!( + message.contains(phrase), + "{what} was refused for another reason: {message}" + ); + // The failure released everything the chain held while the executor is still alive. + assert_eq!(execution.state().reserved_bytes(), 0); + assert_eq!(pool.reserved(), 0); + assert_eq!( + execution.state().nodes_holding_windows(), + 0, + "{what}: the stopped execution still holds a window's samples" + ); + // A stopped execution is not resumed. + let again = execution.next_chunk().unwrap_err(); + assert!( + again.to_string().contains("stopped after an earlier error"), + "{again}" + ); + } +} + +/// The streams a streaming window accepts are accepted by the collector too. +/// +/// One series arriving in chunks that move forward is what a scan hands over, and the chunk that +/// closes a series may carry no row at all. Both are legal to a streaming window, so both are legal +/// to the collector, which merges them into the one series they are and answers over it. The values +/// are the ones the audit's counterexamples would have produced had they been legal: the two forward +/// chunks hold 1 and 3 and sum to 4, and the close that carries no row leaves the 1 it follows. +#[test] +fn a_collector_accepts_forward_runs_and_a_close_without_a_row() { + let inner_grid = grid(0, 60_000, 2); + let outer_grid = grid(60_000, 60_000, 1); + let schema = evaluated_schema(Arc::clone(&inner_grid)); + let tree = || { + NativeTree::try_new(window_with( + map(evaluated_input(Arc::clone(&inner_grid)), vec![MathOp::Abs]), + RangeFunction::SumOverTime, + 120_000, + Some(Arc::clone(&inner_grid)), + Arc::clone(&outer_grid), + )) + .unwrap() + }; + + let cases = [ + ( + "two chunks moving forward", + vec![ + evaluated_chunk_of(&schema, named_metric("a", "x"), 0, vec![Some(-1.0)], false), + evaluated_chunk_of(&schema, named_metric("a", "x"), 1, vec![Some(-3.0)], true), + ], + vec![(0, Some(4.0))], + ), + ( + "a close that carries no row", + vec![ + evaluated_chunk_of(&schema, named_metric("a", "x"), 0, vec![Some(-1.0)], false), + evaluated_chunk_of(&schema, named_metric("a", "x"), 2, Vec::new(), true), + ], + vec![(0, Some(1.0))], + ), + ]; + for (what, chunks, expected) in cases { + let mut execution = executor(&tree(), pool(), &ExecutionPlanMetricsSet::new()); + for chunk in chunks { + execution.push_evaluated(chunk).unwrap(); + assert!( + execution.next_chunk().unwrap().is_none(), + "{what}: the collector reported a piece before its input ended" + ); + } + let out = execution + .finish() + .unwrap_or_else(|error| panic!("{what} was refused: {error}")); + assert_eq!(decided(&out), expected, "{what}"); + assert_eq!( + out.iter().filter(|chunk| chunk.is_last).count(), + 1, + "{what}: the one merged series closed more than once" + ); + } +} + +/// An input that ends inside a series is reported by a window that collects its input too. +/// +/// The collector holds the fragments until the input ends and closes every series it merges with a +/// synthetic `is_last`, so a series the caller never closed would look exactly like one that closed +/// afterwards: the merge would answer it and the barrier would hide the truncation. Nothing is +/// merged here, because the executor refuses the truncation before the flush runs. The check is the +/// input node's own, which follows what it handed over, and it is the only one needed: every closing +/// chunk the caller sent has already reached the collector, which wrote down the series it left open. +/// +/// A root map reports the same truncation, and +/// [`an_input_that_ends_mid_series_is_reported_without_a_window`] is that case on its own; this one +/// is the barrier's, where the flush would have run before the report if the check came later. +#[test] +fn an_input_that_ends_inside_a_collected_series_is_reported() { + let inner_grid = grid(0, 60_000, 2); + let outer_grid = grid(60_000, 60_000, 1); + let tree = NativeTree::try_new(window_with( + map(evaluated_input(Arc::clone(&inner_grid)), vec![MathOp::Abs]), + RangeFunction::SumOverTime, + 120_000, + Some(Arc::clone(&inner_grid)), + Arc::clone(&outer_grid), + )) + .unwrap(); + let schema = evaluated_schema(Arc::clone(&inner_grid)); + let pool = pool(); + let set = ExecutionPlanMetricsSet::new(); + let mut execution = executor(&tree, Arc::clone(&pool), &set); + + // One piece of a series that never says it ended: the collector keeps it and reports nothing. + execution + .push_evaluated(evaluated_chunk_of( + &schema, + named_metric("a", "x"), + 0, + vec![Some(-1.0)], + false, + )) + .unwrap(); + assert!( + execution + .next_chunk() + .expect("the fragment is legal") + .is_none(), + "the collector reported a piece before its input ended" + ); + assert!( + execution.state().reserved_bytes() > 0, + "the collected fragment is not charged for" + ); + + let error = execution + .finish() + .expect_err("an input that ended inside a collected series was accepted"); + assert!( + error + .to_string() + .contains("ended before the last chunk of a series"), + "the truncation was reported as {error}" + ); + // Nothing was merged and nothing was answered, and everything went back while the executor is + // alive. + assert_eq!(execution.state().reserved_bytes(), 0); + assert_eq!(pool.reserved(), 0); + assert_eq!( + execution.state().nodes_holding_windows(), + 0, + "the stopped execution still holds a window's samples" + ); + // The window's own gauge, which is the node that collected the fragment, reads zero. + let current = NodePath::root().metric_name(NodeMetric::CURRENT_RESERVED_BYTES); + assert_eq!(gauge(&set, ¤t), 0); + // A stopped execution is not resumed. + let again = execution.next_chunk().unwrap_err(); + assert!( + again.to_string().contains("stopped after an earlier error"), + "{again}" + ); +} + /// Two series that a map turns into one label set are merged before the window above reads them. /// /// `a{job="x"}` and `b{job="x"}` arrive whole, so each says it is the last chunk of its own source