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
2 changes: 2 additions & 0 deletions crates/storage-query-datafusion/src/invocation_state/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use anyhow::anyhow;
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::common::DataFusionError;
use datafusion::physical_plan::metrics::Time;
use datafusion::physical_plan::stream::RecordBatchReceiverStream;
use datafusion::physical_plan::{PhysicalExpr, SendableRecordBatchStream};
use tokio::sync::mpsc::Sender;
Expand Down Expand Up @@ -100,6 +101,7 @@ impl<S: StatusHandle + Send + Sync + Debug + Clone + 'static> ScanPartition for
_predicate: Option<Arc<dyn PhysicalExpr>>,
batch_size: usize,
limit: Option<usize>,
_elapsed_compute: Time,
) -> anyhow::Result<SendableRecordBatchStream> {
let status = self.status_handle.clone();
let partition_store_manager = self.partition_store_manager.clone();
Expand Down
33 changes: 33 additions & 0 deletions crates/storage-query-datafusion/src/partition_store_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

use std::ops::RangeInclusive;
use std::sync::Arc;
use std::time::Instant;
use std::{fmt::Debug, ops::ControlFlow};

use anyhow::anyhow;
use datafusion::arrow::datatypes::SchemaRef;
use datafusion::error::DataFusionError;
use datafusion::execution::SendableRecordBatchStream;
use datafusion::physical_plan::PhysicalExpr;
use datafusion::physical_plan::metrics::Time;
use datafusion::physical_plan::stream::RecordBatchReceiverStream;

use restate_partition_store::{PartitionStore, PartitionStoreManager};
Expand Down Expand Up @@ -72,6 +74,7 @@ where
S: ScanLocalPartition<Builder = RB>,
RB: crate::table_util::Builder + Send + Sync + 'static,
{
#[allow(clippy::too_many_arguments)]
fn scan_partition(
&self,
partition_id: PartitionId,
Expand All @@ -80,6 +83,7 @@ where
predicate: Option<Arc<dyn PhysicalExpr>>,
batch_size: usize,
limit: Option<usize>,
elapsed_compute: Time,
) -> anyhow::Result<SendableRecordBatchStream> {
let partition_store_manager = self.partition_store_manager.clone();
let mut stream_builder = RecordBatchReceiverStream::builder(projection.clone(), 1);
Expand All @@ -94,9 +98,13 @@ where
DataFusionError::External(err.into())
})?;

// timer starts on first row, stops on scanner drop
let mut elapsed_compute = ElapsedCompute::new(elapsed_compute);

let mut batch_sender = BatchSender::new(projection, tx, predicate, batch_size, limit);

S::for_each_row(&partition_store, range, move |row| {
elapsed_compute.start();
match S::append_row(batch_sender.builder_mut(), row) {
Ok(()) => {}
err => return ControlFlow::Break(err),
Expand Down Expand Up @@ -127,6 +135,7 @@ where
predicate: Option<Arc<dyn PhysicalExpr>>,
batch_size: usize,
limit: Option<usize>,
elapsed_compute: Time,
) -> anyhow::Result<SendableRecordBatchStream> {
self.scan_partition(
partition_id,
Expand All @@ -135,6 +144,30 @@ where
predicate,
batch_size,
limit,
elapsed_compute,
)
}
}

struct ElapsedCompute {
time: Time,
start: Option<Instant>,
}

impl ElapsedCompute {
fn new(time: Time) -> Self {
Self { time, start: None }
}

fn start(&mut self) {
self.start.get_or_insert_with(Instant::now);
}
}

impl Drop for ElapsedCompute {
fn drop(&mut self) {
if let Some(start) = &self.start {
self.time.add_elapsed(*start)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use datafusion::arrow::datatypes::SchemaRef;
use datafusion::execution::SendableRecordBatchStream;

use datafusion::physical_plan::PhysicalExpr;
use datafusion::physical_plan::metrics::Time;
use restate_core::Metadata;
use restate_core::partitions::PartitionRouting;
use restate_types::NodeId;
Expand Down Expand Up @@ -184,6 +185,7 @@ impl ScanPartition for RemotePartitionsScanner {
predicate: Option<Arc<dyn PhysicalExpr>>,
batch_size: usize,
limit: Option<usize>,
elapsed_compute: Time,
) -> anyhow::Result<SendableRecordBatchStream> {
match self.manager.get_partition_target_node(partition_id)? {
PartitionLocation::Local => {
Expand All @@ -197,6 +199,7 @@ impl ScanPartition for RemotePartitionsScanner {
predicate,
batch_size,
limit,
elapsed_compute,
)?)
}
PartitionLocation::Remote { node_id } => Ok(remote_scan_as_datafusion_stream(
Expand Down
2 changes: 2 additions & 0 deletions crates/storage-query-datafusion/src/scanner_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use datafusion::arrow::datatypes::SchemaRef;
use datafusion::execution::{SendableRecordBatchStream, TaskContext};
use datafusion::physical_expr::expressions::DynamicFilterPhysicalExpr;
use datafusion::physical_plan::PhysicalExpr;
use datafusion::physical_plan::metrics::Time;
use tokio::sync::mpsc;
use tokio_stream::StreamExt as TokioStreamExt;
use tracing::{debug, warn};
Expand Down Expand Up @@ -92,6 +93,7 @@ impl ScannerTask {
request
.limit
.map(|limit| usize::try_from(limit).expect("limit to fit in a usize")),
Time::new(),
)?;

let (tx, rx) = mpsc::unbounded_channel();
Expand Down
Loading
Loading