Skip to content
Draft
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
112 changes: 96 additions & 16 deletions data_plane/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,11 @@ impl Chunk {
}
allowed_entries.push((index, allowed));
}
let is_field = |label: &str| self.field(label).is_some();
let fields = verdicts.field_mask(|label| self.field(label).is_some());
let kept: Vec<bool> = self
.series
.iter()
.map(|series| verdicts.keeps(series, is_field))
.map(|series| verdicts.keeps(series, fields))
.collect();
self.cut(&kept, &allowed_entries)
}
Expand Down Expand Up @@ -751,50 +751,83 @@ impl RawData {
/// a series is judged once per read, however many chunks hold it.
struct SeriesVerdicts {
selection: Arc<LabelSelection>,
/// By series id: 0 not judged yet, 1 selected, 2 not.
/// By series id: 0 not judged yet, 1 selected, 2 not, 3 selected if
/// the chunk holds as fields the restricted labels the series lacks.
by_id: std::cell::RefCell<Vec<u8>>,
/// By series id, for verdict 3: the restricted labels it lacks, one bit
/// per label in the selection's order.
lacks_by_id: std::cell::RefCell<Vec<u64>>,
}

impl SeriesVerdicts {
fn new(selection: Arc<LabelSelection>) -> Self {
Self {
selection,
by_id: Default::default(),
lacks_by_id: Default::default(),
}
}

/// The restricted labels (one bit each, in the selection's order) that
/// `is_field` says a chunk holds as fields; judged once per chunk.
fn field_mask(&self, is_field: impl Fn(&str) -> bool) -> u64 {
// Bit 63 stands for every label from the 64th on: set only when
// the chunk holds all of them.
let mut tail_fields = true;
let mut mask = 0;
for (index, label) in self.selection.restrictions().keys().enumerate() {
let field = is_field(label);
if index >= 63 {
tail_fields &= field;
} else if field {
mask |= 1 << index;
}
}
if tail_fields {
mask |= 1 << 63;
}
mask
}

/// Whether `series` holds one of the allowed values of every
/// restricted label, a label it lacks passing when `is_field` says the
/// chunk holds it as a field (its rows are then judged by value).
fn keeps(&self, series: &Series, is_field: impl Fn(&str) -> bool) -> bool {
/// restricted label, a label it lacks passing when the chunk holds it
/// as a field (`fields`: [`Self::field_mask`]; its rows are then judged
/// by value). A series is judged once per read; per chunk only its
/// lacking labels are checked against `fields`.
fn keeps(&self, series: &Series, fields: u64) -> bool {
let mut by_id = self.by_id.borrow_mut();
let id = series.id as usize;
if by_id.len() <= id {
by_id.resize(id + 1, 0);
}
if by_id[id] == 0 {
let mut lacks = false;
let mut lacks: u64 = 0;
let mut holds = true;
for (label, values) in self.selection.restrictions() {
for (index, (label, values)) in self.selection.restrictions().iter().enumerate() {
match series.get(label) {
Some(value) => holds &= values.contains(value),
None => lacks = true,
// Beyond 64 labels every one counts as lacking: such a
// series is kept only when its chunk holds them all.
None => lacks |= 1 << index.min(63),
}
}
by_id[id] = match (holds, lacks) {
by_id[id] = match (holds, lacks != 0) {
(false, _) => 2,
(true, false) => 1,
(true, true) => 3,
(true, true) => {
let mut lacks_by_id = self.lacks_by_id.borrow_mut();
if lacks_by_id.len() <= id {
lacks_by_id.resize(id + 1, 0);
}
lacks_by_id[id] = lacks;
3
}
};
}
match by_id[id] {
1 => true,
2 => false,
_ => self
.selection
.restrictions()
.keys()
.all(|label| series.get(label).is_some() || is_field(label)),
_ => self.lacks_by_id.borrow()[id] & !fields == 0,
}
}
}
Expand Down Expand Up @@ -2911,6 +2944,53 @@ mod tests {
assert!(batches.is_empty());
}

/// A field restriction on a keyed table is judged per series once per
/// read, not once per series per chunk: 2,000 series (one per job) over
/// 6 panes, `instance IN (..)` on the field, with a key restriction too
/// and with a pane where the field never takes the values; the rows
/// equal a plain filter's, and with the restriction on the key only the
/// selected series' rows are read.
#[test]
fn field_restrictions_on_many_series_match_a_plain_filter() {
let jobs: Vec<&'static str> = (0..2_000)
.map(|j| &*Box::leak(format!("j{j}").into_boxed_str()))
.collect();
let mut rows: Vec<TableRow> = Vec::new();
for t in (0..30_000).step_by(1_000) {
for (j, job) in jobs.iter().enumerate() {
// i0..i3 rotate over the jobs; i3 never before 20 s.
let i = (j + t as usize / 1_000) % 4;
if i == 3 && t < 20_000 {
continue;
}
rows.push((job, format!("i{i}"), t + 1, (j + t as usize) as f64));
}
}
let mut keyed = table_data(&[keyed_layout(&rows)]);
keyed.seal_through(30_000);
let cases: Vec<(Vec<ScalarExpr<()>>, Box<dyn Fn(&TableRow) -> bool>)> = vec![
(
vec![text_is(2, &["i1", "i3"])],
Box::new(|r| r.1 == "i1" || r.1 == "i3"),
),
(
vec![text_is(1, &["j7", "j1999"]), text_is(2, &["i0", "i2"])],
Box::new(|r| (r.0 == "j7" || r.0 == "j1999") && (r.1 == "i0" || r.1 == "i2")),
),
(vec![text_is(1, &["j42"])], Box::new(|r| r.0 == "j42")),
];
for (predicates, keep) in cases {
for reads in [None, Some(RawReads::default())] {
let (batches, _) = table_read_with(&keyed, None, &predicates, reads.as_ref());
let expected: Vec<TableRow> = rows.iter().filter(|r| keep(r)).cloned().collect();
assert_eq!(
sorted(rows_of(&batches)),
sorted(table_read(&expected, None))
);
}
}
}

/// Eviction drops the panes ending at or before the cutoff, keeps the
/// pane the cutoff falls in, and no read returns a sample at or
/// before the cutoff: a read of a range starting before it fails (its
Expand Down