use std::sync::atomic::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct ProbeSideSummary {
pub(super) non_empty: bool,
pub(super) has_null: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Completion {
NotLast,
Emit(ProbeSideSummary),
Abandoned,
}
macro_rules! define_probe_completion {
(counter: $counter:ty, flag: $flag:ty) => {
type Counter = $counter;
type Flag = $flag;
#[derive(Debug)]
pub(super) struct ProbeCompletion {
running: Counter,
saw_row: Flag,
saw_null_key: Flag,
abandoned: Flag,
}
impl ProbeCompletion {
pub(super) fn new(probe_threads: usize) -> Self {
Self {
running: Counter::new(probe_threads),
saw_row: Flag::new(false),
saw_null_key: Flag::new(false),
abandoned: Flag::new(false),
}
}
pub(super) fn record_batch(&self, non_empty: bool, has_null: bool) {
if non_empty {
self.saw_row.store(true, Ordering::Relaxed);
}
if has_null {
self.saw_null_key.store(true, Ordering::Relaxed);
}
}
pub(super) fn saw_null_key_hint(&self) -> bool {
self.saw_null_key.load(Ordering::Relaxed)
}
pub(super) fn report_completed(&self) -> Completion {
let was_last = self.running.fetch_sub(1, Ordering::AcqRel) == 1;
match (was_last, self.abandoned.load(Ordering::Relaxed)) {
(false, _) => Completion::NotLast,
(true, false) => {
let summary = ProbeSideSummary {
non_empty: self.saw_row.load(Ordering::Relaxed),
has_null: self.saw_null_key.load(Ordering::Relaxed),
};
Completion::Emit(summary)
}
(true, true) => Completion::Abandoned,
}
}
pub(super) fn abandon(&self, count: usize) -> bool {
if count == 0 {
return false;
}
self.abandoned.store(true, Ordering::Relaxed);
self.running.fetch_sub(count, Ordering::AcqRel) == count
}
}
};
}
define_probe_completion!(
counter: std::sync::atomic::AtomicUsize,
flag: std::sync::atomic::AtomicBool
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_last_partition_receives_the_summary() {
let completion = ProbeCompletion::new(3);
completion.record_batch(true, false);
assert_eq!(completion.report_completed(), Completion::NotLast);
completion.record_batch(true, true);
assert_eq!(completion.report_completed(), Completion::NotLast);
assert_eq!(
completion.report_completed(),
Completion::Emit(ProbeSideSummary {
non_empty: true,
has_null: true,
})
);
}
#[test]
fn an_untouched_probe_side_reports_nothing_seen() {
let completion = ProbeCompletion::new(1);
assert_eq!(
completion.report_completed(),
Completion::Emit(ProbeSideSummary {
non_empty: false,
has_null: false,
})
);
}
#[test]
fn the_hint_reflects_recorded_nulls() {
let completion = ProbeCompletion::new(1);
assert!(!completion.saw_null_key_hint());
completion.record_batch(true, false);
assert!(!completion.saw_null_key_hint());
completion.record_batch(true, true);
assert!(completion.saw_null_key_hint());
}
#[test]
fn abandoning_no_partitions_still_emits() {
let completion = ProbeCompletion::new(1);
assert!(!completion.abandon(0));
assert_eq!(
completion.report_completed(),
Completion::Emit(ProbeSideSummary {
non_empty: false,
has_null: false,
})
);
}
}
#[cfg(all(test, datafusion_loom))]
mod loom_tests {
use super::Completion;
use super::ProbeSideSummary;
use loom::sync::Arc;
use std::sync::atomic::Ordering;
define_probe_completion!(
counter: loom::sync::atomic::AtomicUsize,
flag: loom::sync::atomic::AtomicBool
);
#[test]
fn the_last_partition_observes_a_sibling_null() {
loom::model(|| {
let completion = Arc::new(ProbeCompletion::new(2));
let recorder = {
let completion = Arc::clone(&completion);
loom::thread::spawn(move || {
completion.record_batch(true, true);
completion.report_completed()
})
};
let observer = completion.report_completed();
let recorded = recorder.join().unwrap();
let summary = match (recorded, observer) {
(Completion::Emit(summary), Completion::NotLast)
| (Completion::NotLast, Completion::Emit(summary)) => summary,
(Completion::Emit(_), Completion::Emit(_)) => {
panic!("two partitions both finished last")
}
(Completion::NotLast, Completion::NotLast) => {
panic!("no partition finished last")
}
other => panic!("unexpected outcome: {other:?}"),
};
assert!(
summary.has_null,
"the last partition missed the NULL its sibling recorded"
);
assert!(
summary.non_empty,
"the last partition missed the row its sibling recorded"
);
});
}
#[test]
fn the_summary_unions_what_every_partition_recorded() {
loom::model(|| {
let completion = Arc::new(ProbeCompletion::new(2));
let other = {
let completion = Arc::clone(&completion);
loom::thread::spawn(move || {
completion.record_batch(true, false);
completion.report_completed()
})
};
completion.record_batch(false, true);
let here = completion.report_completed();
let there = other.join().unwrap();
let summary = match (here, there) {
(Completion::Emit(summary), Completion::NotLast)
| (Completion::NotLast, Completion::Emit(summary)) => summary,
other => panic!("some partition must finish last: {other:?}"),
};
assert_eq!(
summary,
ProbeSideSummary {
non_empty: true,
has_null: true,
},
"the summary lost a fact one of the partitions recorded"
);
});
}
#[test]
fn exactly_one_partition_finishes_last() {
loom::model(|| {
let completion = Arc::new(ProbeCompletion::new(2));
let other = {
let completion = Arc::clone(&completion);
loom::thread::spawn(move || {
completion.report_completed() != Completion::NotLast
})
};
let here = completion.report_completed() != Completion::NotLast;
let there = other.join().unwrap();
assert!(here ^ there, "exactly one partition must finish last");
});
}
#[test]
fn the_hint_never_reports_an_unrecorded_null() {
loom::model(|| {
let completion = Arc::new(ProbeCompletion::new(2));
let other = {
let completion = Arc::clone(&completion);
loom::thread::spawn(move || {
completion.record_batch(true, false);
completion.saw_null_key_hint()
})
};
let here = completion.saw_null_key_hint();
let there = other.join().unwrap();
assert!(!here && !there, "the hint reported an unrecorded NULL");
});
}
#[test]
fn an_abandoned_partition_counts_down_but_suppresses_the_emit() {
loom::model(|| {
let completion = Arc::new(ProbeCompletion::new(2));
let quitter = {
let completion = Arc::clone(&completion);
loom::thread::spawn(move || completion.abandon(1))
};
let here = completion.report_completed();
let quitter_was_last = quitter.join().unwrap();
match (here, quitter_was_last) {
(Completion::Abandoned, false) => {}
(Completion::NotLast, true) => {}
other => panic!("unexpected outcome: {other:?}"),
}
});
}
}