1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Releases exact query results through a consumer-polled bounded stream.
use std::collections::BTreeSet;
use std::error::Error;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use arrow::datatypes::SchemaRef;
use arrow::record_batch::RecordBatch;
#[cfg(test)]
use datafusion::catalog::TableProvider;
use datafusion::execution::memory_pool::MemoryReservation;
use datafusion::physical_plan::SendableRecordBatchStream;
use futures::{Stream, StreamExt};
use polyc_state::journal::GetJournalSource;
use polyc_state::query_audit::{ErrorClass, QueryOutcome};
use tokio::sync::{OwnedSemaphorePermit, mpsc};
use tokio_util::sync::CancellationToken;
use super::latch::{BatchRelease, DeliveredCounts, TerminalLatch};
use super::{
CoreExecutionError, CoreMetadataAuthority, CoreOperationContext, CurrentCredentialAuthority,
EffectiveCoreBounds, PermitGuardian, QueryScope, classify_error, operation_refusal,
operation_wait,
};
#[cfg(test)]
use crate::core_resolution::CoreTable;
/// Owns a physical plan whose permit, exact pins, and limits cannot change.
pub(crate) struct BoundCoreQuery {
pub(super) guardian: PermitGuardian,
pub(super) dataframe: datafusion::dataframe::DataFrame,
pub(super) manifests: Vec<polyc_state::projection::ProjectionManifest>,
pub(super) original_scope: QueryScope,
pub(super) metadata: Arc<dyn CoreMetadataAuthority>,
pub(super) scope_revalidator: Arc<dyn CurrentCredentialAuthority>,
pub(super) operation: Arc<CoreOperationContext>,
pub(super) cancellation: CancellationToken,
pub(super) bounds: EffectiveCoreBounds,
pub(super) revalidation_interval: Duration,
pub(super) _explain_enabled: bool,
pub(super) execution_admission: OwnedSemaphorePermit,
pub(super) source_decode_reservation: MemoryReservation,
/// Deschedules the producer's terminal report by this much.
///
/// Zero in production. A test uses it to hold the producer away from the
/// guardian after rows were already delivered, which is the exact race a
/// scheduling grace used to resolve wrongly.
#[cfg(test)]
pub(super) report_delay: Duration,
#[cfg(test)]
pub(super) providers: std::collections::BTreeMap<CoreTable, Arc<dyn TableProvider>>,
}
impl BoundCoreQuery {
#[cfg(test)]
pub(super) const fn delay_report_for_test(&mut self, delay: Duration) {
self.report_delay = delay;
}
#[cfg(test)]
pub(super) fn provider(&self, table: CoreTable) -> Arc<dyn TableProvider> {
Arc::clone(
self.providers
.get(&table)
.expect("bound dependency provider"),
)
}
/// Starts the producer task and hands back the consumer-polled stream.
///
/// Nothing is awaited here. The producer waits for the consumer's first
/// readiness signal. No batch is produced before a poll asks for one.
pub(crate) fn execute(self) -> CoreResultStream {
// Captured before the plan is consumed. A caller must be able to
// announce the result schema before the first batch exists, and the
// durable audit's own source vector is what the terminal reports.
let released_schema = self.dataframe.schema().inner().clone();
let released_source = self.guardian.source().clone();
let started = tokio::time::Instant::now();
let Self {
guardian,
dataframe,
manifests,
original_scope,
metadata,
scope_revalidator,
operation,
cancellation,
bounds,
revalidation_interval,
_explain_enabled,
execution_admission,
source_decode_reservation,
#[cfg(test)]
report_delay,
#[cfg(test)]
providers: _,
} = self;
// The latch is the one place the producer's report, the consumer's
// withdrawal, and the delivered counts meet. The guardian reads it, so
// an exact row count never depends on which task ran last.
let latch = guardian.latch();
let consumer_latch = Arc::clone(&latch);
let consumer_cancellation = cancellation.clone();
let terminal_cancellation = cancellation.clone();
let (sender, receiver) = mpsc::channel(1);
let (readiness_signal, readiness_requests) = mpsc::unbounded_channel();
tokio::spawn(async move {
let stream = operation_wait(&operation, &cancellation, dataframe.execute_stream())
.await
.and_then(|result| result.map_err(CoreExecutionError::from));
let end = match stream {
Ok(upstream) => {
let state = CoreStreamState {
upstream,
execution_admission,
_source_decode_reservation: source_decode_reservation,
revalidation: CoreRevalidationWitness {
manifests,
original_scope,
metadata,
scope_revalidator,
operation: Arc::clone(&operation),
},
operation,
cancellation,
bounds,
revalidation_interval,
last_revalidation: None,
released_rows: 0,
released_bytes: 0,
known_extra_row: false,
latch: Arc::clone(&latch),
};
release_from_producer(state, &sender, readiness_requests).await
}
Err(error) => ProducerEnd::Failed(error),
};
// A consumer that withdrew after the last row still withdrew.
let end = match end {
ProducerEnd::Ended
if terminal_cancellation.is_cancelled() || sender.is_closed() =>
{
ProducerEnd::Cancelled
}
end => end,
};
#[cfg(test)]
if !report_delay.is_zero() {
tokio::time::sleep(report_delay).await;
}
// The outcome only. The counts belong to the terminal, and the
// terminal is built inside the latch's own critical section, so
// nothing can be released between reading them and recording it.
let settlement = guardian.finish(producer_outcome(&end)).await;
let frame = match (settlement, end) {
(Err(error), _) | (Ok(()), ProducerEnd::Failed(error)) => {
CoreStreamFrame::Failure(error)
}
(Ok(()), ProducerEnd::Cancelled) => {
CoreStreamFrame::Failure(CoreExecutionError::Cancelled)
}
(Ok(()), ProducerEnd::Ended) => CoreStreamFrame::Complete,
};
let _ = sender.send(frame).await;
});
CoreResultStream {
receiver,
readiness_signal,
poll_outstanding: false,
terminal_seen: false,
cancellation: consumer_cancellation,
latch: consumer_latch,
schema: released_schema,
source: released_source,
started,
}
}
}
/// How the producer's own loop ended.
enum ProducerEnd {
/// The upstream stream ended after every produced row was delivered.
Ended,
/// The consumer withdrew.
Cancelled,
/// The producer measured this exact failure.
Failed(CoreExecutionError),
}
struct CoreStreamState {
upstream: SendableRecordBatchStream,
execution_admission: OwnedSemaphorePermit,
_source_decode_reservation: MemoryReservation,
revalidation: CoreRevalidationWitness,
operation: Arc<CoreOperationContext>,
cancellation: CancellationToken,
bounds: EffectiveCoreBounds,
revalidation_interval: Duration,
last_revalidation: Option<tokio::time::Instant>,
released_rows: u64,
released_bytes: u64,
known_extra_row: bool,
latch: Arc<TerminalLatch>,
}
async fn release_from_producer(
mut state: CoreStreamState,
sender: &mpsc::Sender<CoreStreamFrame>,
mut readiness: mpsc::UnboundedReceiver<()>,
) -> ProducerEnd {
loop {
// The original deadline also bounds the wait for a readiness token. A
// consumer that stops polling without dropping would otherwise pin the
// permit, the admission slot, and the memory reservation forever, and
// leave the durable intent unmatched past its declared deadline.
let Ok(remaining) = state.operation.remaining() else {
state.cancellation.cancel();
return ProducerEnd::Failed(CoreExecutionError::Deadline);
};
tokio::select! {
ready = readiness.recv() => {
if ready.is_none() {
state.cancellation.cancel();
return ProducerEnd::Cancelled;
}
}
() = state.cancellation.cancelled() => return ProducerEnd::Cancelled,
() = tokio::time::sleep(remaining) => {
state.cancellation.cancel();
return ProducerEnd::Failed(CoreExecutionError::Deadline);
}
}
match state.next().await {
Ok(Some((batch, next))) => {
state = next;
if sender.send(CoreStreamFrame::Batch(batch)).await.is_err() {
state.cancellation.cancel();
return ProducerEnd::Cancelled;
}
}
Ok(None) => return ProducerEnd::Ended,
Err(error) => return ProducerEnd::Failed(error),
}
}
}
impl CoreStreamState {
async fn next(mut self) -> Result<Option<(RecordBatch, Self)>, CoreExecutionError> {
loop {
if self.cancellation.is_cancelled() {
return Err(CoreExecutionError::Cancelled);
}
if self.known_extra_row {
return Ok(None);
}
let next =
match operation_wait(&self.operation, &self.cancellation, self.upstream.next())
.await
{
Ok(next) => next,
Err(error) => return Err(error),
};
let Some(batch) = next else {
return Ok(None);
};
let batch = match batch {
Ok(batch) => batch,
Err(error) => return Err(stream_error(error)),
};
if batch.num_rows() == 0 {
continue;
}
if self.released_rows >= self.bounds.rows() {
self.latch.record_truncation();
return Ok(None);
}
let remaining_rows = self.bounds.rows() - self.released_rows;
let release_rows = usize::try_from(remaining_rows)
.unwrap_or(usize::MAX)
.min(batch.num_rows());
let release = batch.slice(0, release_rows);
if release_rows < batch.num_rows() {
self.latch.record_truncation();
self.known_extra_row = true;
}
let bytes = u64::try_from(release.get_array_memory_size()).unwrap_or(u64::MAX);
let total = match self.released_bytes.checked_add(bytes) {
Some(total) if total <= self.bounds.result_release_bytes() => total,
Some(total) => {
return Err(CoreExecutionError::ReleaseBound {
observed: total,
limit: self.bounds.result_release_bytes(),
});
}
None => {
return Err(CoreExecutionError::ReleaseBound {
observed: u64::MAX,
limit: self.bounds.result_release_bytes(),
});
}
};
if self
.last_revalidation
.is_none_or(|last| last.elapsed() >= self.revalidation_interval)
{
self.revalidation.revalidate(&self.cancellation).await?;
self.last_revalidation = Some(tokio::time::Instant::now());
}
self.released_rows += u64::try_from(release_rows).unwrap_or(u64::MAX);
self.released_bytes = total;
return Ok(Some((release, self)));
}
}
}
fn stream_error(error: datafusion::error::DataFusionError) -> CoreExecutionError {
let mut source: Option<&(dyn Error + 'static)> = Some(&error);
while let Some(current) = source {
if matches!(
current.downcast_ref::<CoreExecutionError>(),
Some(CoreExecutionError::Deadline)
) {
return CoreExecutionError::Deadline;
}
if matches!(
current.downcast_ref::<CoreExecutionError>(),
Some(CoreExecutionError::Cancelled)
) {
return CoreExecutionError::Cancelled;
}
source = current.source();
}
CoreExecutionError::DataFusion(error)
}
struct CoreRevalidationWitness {
manifests: Vec<polyc_state::projection::ProjectionManifest>,
original_scope: QueryScope,
metadata: Arc<dyn CoreMetadataAuthority>,
scope_revalidator: Arc<dyn CurrentCredentialAuthority>,
operation: Arc<CoreOperationContext>,
}
impl CoreRevalidationWitness {
async fn revalidate(&self, cancellation: &CancellationToken) -> Result<(), CoreExecutionError> {
operation_wait(&self.operation, cancellation, self.revalidate_inner()).await?
}
async fn revalidate_inner(&self) -> Result<(), CoreExecutionError> {
let current = self
.scope_revalidator
.current_scope(&self.operation)
.await?;
if !scope_contains(¤t, &self.original_scope) {
return Err(CoreExecutionError::AuthorityNarrowed);
}
for manifest in &self.manifests {
self.operation.check().map_err(operation_refusal)?;
let expected = manifest.checkpoint().source();
let observed = self
.metadata
.source_head(
&self.operation,
GetJournalSource::new(expected.partition().clone()),
)
.await?
.ok_or_else(|| CoreExecutionError::SourceChanged(expected.partition().clone()))?;
if observed.source() != expected {
return Err(CoreExecutionError::SourceChanged(
expected.partition().clone(),
));
}
}
Ok(())
}
}
fn scope_contains(current: &QueryScope, original: &QueryScope) -> bool {
match (current, original) {
(QueryScope::Fleet, _) => true,
(QueryScope::Conversations(_), QueryScope::Fleet) => false,
(QueryScope::Conversations(current), QueryScope::Conversations(original)) => {
let current = current.iter().collect::<BTreeSet<_>>();
original
.iter()
.all(|conversation| current.contains(conversation))
}
}
}
/// Returns the outcome the producer measured.
///
/// The outcome only. The rows and truncation that go with it belong to the
/// latch, which builds the completion from the counts it holds, in the same
/// critical section that records the terminal.
fn producer_outcome(end: &ProducerEnd) -> QueryOutcome {
match end {
ProducerEnd::Ended => QueryOutcome::Succeeded,
ProducerEnd::Cancelled => QueryOutcome::Failed(ErrorClass::Cancelled),
ProducerEnd::Failed(error) => QueryOutcome::Failed(classify_error(error)),
}
}
/// Carries the private bounded Arrow stream.
///
/// End-of-stream becomes visible only after the producer durably settles its
/// exact completion. Dropping this value signals cancellation and nothing
/// more. The producer still owns the one-shot permit. It settles under its own
/// terminal budget.
pub(crate) struct CoreResultStream {
receiver: mpsc::Receiver<CoreStreamFrame>,
readiness_signal: mpsc::UnboundedSender<()>,
poll_outstanding: bool,
terminal_seen: bool,
cancellation: CancellationToken,
latch: Arc<TerminalLatch>,
/// The plan's own output schema, captured before the plan was consumed.
schema: SchemaRef,
/// The durable audit's source vector for this execution.
source: polyc_state::query_audit::SourceSnapshot,
started: tokio::time::Instant,
}
impl CoreResultStream {
/// Returns the result schema, available before the first batch.
pub(crate) const fn schema(&self) -> &SchemaRef {
&self.schema
}
/// Returns the exact source vector this execution read.
pub(crate) const fn source(&self) -> &polyc_state::query_audit::SourceSnapshot {
&self.source
}
/// Returns what the consumer has actually received so far.
///
/// Read from the latch, not from a producer-side tally. What "received"
/// means follows the live accounting: batches that left this stream, or —
/// once a consumer re-frames them — the frames that left that consumer.
pub(crate) fn delivered(&self) -> DeliveredCounts {
self.latch.delivered()
}
/// Returns how long this execution has run.
pub(crate) fn elapsed(&self) -> Duration {
self.started.elapsed()
}
/// Hands the delivery count to a consumer that re-frames each batch.
///
/// After this, a batch leaving the stream counts nothing. The consumer
/// admits what it releases, through [`Self::admit_release`], so the
/// durable record counts rows a caller actually received rather than rows
/// this stream handed to a framer.
pub(crate) fn account_at_consumer(&self) {
self.latch.account_at_consumer();
}
/// Admits one release into the record this query will settle.
///
/// Returns whether it was admitted. A refusal means a terminal has
/// already been chosen, so the frame must not reach the caller: the
/// record it would belong to is already fixed without it.
pub(crate) fn admit_release(&self, rows: u64, bytes: u64) -> bool {
self.latch.admit_release(rows, bytes)
}
/// Settles the terminal the consumer measured at its own admitted bound.
///
/// The consumer stopped because it reached a ceiling the caller asked
/// for. It then drops this stream to stop the producer, and that drop
/// must not turn a bounded success into a withdrawal. The stop is always
/// a truncation: release ended because the next frame did not fit.
///
/// Returns the counts the record carries, or `None` when another party
/// had already ended the query.
pub(crate) fn settle_consumer_bound(&self) -> Option<DeliveredCounts> {
self.latch
.settle_consumer_bound(&self.source, self.started.elapsed())
}
/// Records a failure the consumer measured after the batch left this
/// stream.
///
/// A consumer that cannot use a delivered batch ends the query, and
/// dropping this stream would then record a withdrawal. That is the wrong
/// word: the caller withdrew nothing. The latch keeps a measured failure
/// over a later cancellation, so reporting here is what makes the durable
/// terminal say what actually happened.
/// The record's counts are the admitted releases, which is what the
/// caller received: a batch that left this stream but could not be framed
/// reached nobody, and was never admitted.
///
/// Returns whether this failure became the selected terminal. A caller
/// must not publish its local terminal when another terminal won first.
pub(crate) fn report_failure(&self, class: ErrorClass) -> bool {
self.latch.report(
QueryOutcome::Failed(class),
self.started.elapsed(),
&self.source,
)
}
/// Requests one producer batch without dequeuing it.
///
/// A case uses this to hold a batch in front of a producer terminal. It
/// can then prove that a later framing failure does not replace the
/// terminal that already became the durable record.
#[cfg(test)]
pub(crate) fn request_buffered_batch(&mut self) {
if !self.poll_outstanding {
let _ = self.readiness_signal.send(());
self.poll_outstanding = true;
}
}
/// Returns how many producer frames wait in front of this consumer.
#[cfg(test)]
pub(crate) fn buffered_frames(&self) -> usize {
self.receiver.len()
}
/// Returns whether a terminal has won the latch.
#[cfg(test)]
pub(crate) fn terminal_selected(&self) -> bool {
self.latch.is_settled()
}
}
enum CoreStreamFrame {
Batch(RecordBatch),
Failure(CoreExecutionError),
Complete,
}
impl Stream for CoreResultStream {
type Item = Result<RecordBatch, CoreExecutionError>;
fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
if !self.poll_outstanding {
// The producer may close readiness while it is durably
// settling a terminal result. The result channel, not this
// hint, owns EOF.
let _ = self.readiness_signal.send(());
self.poll_outstanding = true;
}
let frame = self.receiver.poll_recv(context);
return match frame {
Poll::Ready(Some(CoreStreamFrame::Batch(batch))) => {
self.poll_outstanding = false;
// Offered at the exact point the batch leaves this stream,
// through the same admission a frame goes through.
let rows = u64::try_from(batch.num_rows()).unwrap_or(u64::MAX);
let bytes = u64::try_from(batch.get_array_memory_size()).unwrap_or(u64::MAX);
match self.latch.record_batch_delivery(rows, bytes) {
// A terminal was already chosen and this batch is not
// in the record. Handing it over would give the caller
// rows the record does not carry, so it is dropped and
// the producer's own terminal is taken instead. The
// producer sends nothing after its terminal, so this
// skips at most what the channel already buffered.
BatchRelease::Refused => continue,
BatchRelease::Admitted | BatchRelease::CountedByConsumer => {
Poll::Ready(Some(Ok(batch)))
}
}
}
Poll::Ready(Some(CoreStreamFrame::Failure(error))) => {
self.poll_outstanding = false;
self.terminal_seen = true;
Poll::Ready(Some(Err(error)))
}
Poll::Ready(Some(CoreStreamFrame::Complete)) => {
self.poll_outstanding = false;
self.terminal_seen = true;
Poll::Ready(None)
}
Poll::Ready(None) if self.terminal_seen => Poll::Ready(None),
Poll::Ready(None) => {
self.poll_outstanding = false;
self.terminal_seen = true;
Poll::Ready(Some(Err(CoreExecutionError::AuditCompletionUnavailable)))
}
Poll::Pending => Poll::Pending,
};
}
}
}
impl Drop for CoreResultStream {
fn drop(&mut self) {
// Withdraw through the latch first, so the transition is recorded
// before the producer can observe the cancellation token and report a
// success the consumer will never see.
self.latch.cancel();
self.cancellation.cancel();
}
}