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
3 changes: 2 additions & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ datafusion-table-providers = { version = "0.11", default-features = false, optio
futures = "0.3"
object_store = { version = "0.13", default-features = false }
prost = "0.14"
tokio = { version = "1", features = ["rt-multi-thread"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tokio-metrics = { version = "0.5", optional = true }
tokio-util = { version = "0.7", features = ["rt"] }
url = "2"

[build-dependencies]
Expand Down
147 changes: 147 additions & 0 deletions native/src/cancellation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//! Cancellation tokens for in-flight queries.
//!
//! Managed handles are opaque `u64` IDs, not raw pointers. A process-global
//! registry maps each live ID to its `Arc<CancellationToken>`. FFI handlers
//! look up by ID and clone the `Arc` out of the registry (under a lock) so a
//! concurrent free can never invalidate a borrow already in flight: the worst
//! a race produces is a token that no longer cancels anything, never a
//! use-after-free of a freed `Box`.
//!
//! The C# layer owns one token per cancellable `collect`/`execute_stream`
//! call and fires it from the thread that owns the managed `CancellationToken`
//! while the calling thread is parked inside `runtime().block_on(...)`.

use std::collections::HashMap;
use std::os::raw::c_int;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};

use datafusion::error::DataFusionError;
use tokio_util::sync::CancellationToken;

use crate::take_result;

/// Sentinel message used to round-trip a query-cancellation signal across the
/// FFI boundary. A cancelled `collect`/`execute_stream` returns a
/// `DataFusionError::Execution` carrying this message; the C# side maps it to
/// an `OperationCanceledException` whenever the managed token also reports a
/// cancellation request.
pub(crate) const CANCELLED_MESSAGE: &str = "datafusion-csharp: query cancelled";

fn registry() -> &'static Mutex<HashMap<u64, Arc<CancellationToken>>> {
static REG: OnceLock<Mutex<HashMap<u64, Arc<CancellationToken>>>> = OnceLock::new();
REG.get_or_init(|| Mutex::new(HashMap::new()))
}

fn next_id() -> u64 {
// Start at 1 so 0 stays reserved as the "no token" sentinel on the managed
// side. Monotonic; 2^64 IDs is enough that reuse is never observed.
static COUNTER: AtomicU64 = AtomicU64::new(1);
COUNTER.fetch_add(1, Ordering::Relaxed)
}

/// Resolve a token handle into an `Arc<CancellationToken>`, or `None` when the
/// handle is the zero "no token" sentinel or refers to an already-freed entry.
/// The cloned `Arc` keeps the inner token alive for the borrow's lifetime, so a
/// concurrent `df_cancellation_token_free` removing the registry entry is safe.
pub(crate) fn token_arc(handle: u64) -> Option<Arc<CancellationToken>> {
if handle == 0 {
return None;
}
let guard = registry().lock().expect("cancellation registry poisoned");
guard.get(&handle).cloned()
}

#[no_mangle]
pub extern "C" fn df_cancellation_token_new(out: *mut u64) -> c_int {
take_result(|| {
if out.is_null() {
return Err("output token handle pointer is null".into());
}
let token: Arc<CancellationToken> = Arc::new(CancellationToken::new());
let id = next_id();
registry()
.lock()
.expect("cancellation registry poisoned")
.insert(id, token);
unsafe {
*out = id;
}
Ok(())
})
}

#[no_mangle]
pub extern "C" fn df_cancellation_token_cancel(handle: u64) -> c_int {
take_result(|| {
// A missing entry (already freed or never registered) is a no-op: the
// C# wrapper disposes its registration before freeing, so a real cancel
// never loses a race, and a defensive no-op avoids spurious failures.
if let Some(token) = token_arc(handle) {
token.cancel();
}
Ok(())
})
}

#[no_mangle]
pub extern "C" fn df_cancellation_token_free(handle: u64) -> c_int {
take_result(|| {
if handle != 0 {
// Remove the registry entry; the underlying `Arc` may still have
// clones held by an in-flight collect/execute_stream future, which
// keep the inner token alive until those futures finish.
registry()
.lock()
.expect("cancellation registry poisoned")
.remove(&handle);
}
Ok(())
})
}

fn cancelled_error() -> DataFusionError {
DataFusionError::Execution(CANCELLED_MESSAGE.to_string())
}

/// Drive `fut` to completion unless `token` fires first. A fired token yields a
/// `DataFusionError::Execution(CANCELLED_MESSAGE)` so the managed side can
/// surface a cancellation. `biased` polls the cancellation branch first so a
/// token that is already cancelled aborts before any query work runs.
pub(crate) async fn run_cancellable<F, T>(
token: &Option<Arc<CancellationToken>>,
fut: F,
) -> Result<T, DataFusionError>
where
F: std::future::Future<Output = Result<T, DataFusionError>>,
{
let Some(token) = token else {
return fut.await;
};
tokio::select! {
biased;
_ = token.cancelled() => Err(cancelled_error()),
result = fut => result,
}
}

pub(crate) fn resolve_token(handle: u64) -> Option<Arc<CancellationToken>> {
token_arc(handle)
}
14 changes: 10 additions & 4 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use tokio::runtime::Runtime;
mod arrow;
mod avro;
mod cache_manager;
mod cancellation;
mod clickhouse;
mod csv;
mod json;
Expand All @@ -72,6 +73,7 @@ mod proto_gen {

use crate::arrow::arrow_options;
use crate::avro::avro_options;
use crate::cancellation::{resolve_token, run_cancellable};
use crate::csv::csv_options;
use crate::json::json_options;
use crate::parquet::parquet_options;
Expand Down Expand Up @@ -721,32 +723,36 @@ pub extern "C" fn df_dataframe_schema_ipc(handle: *mut DataFrame, out: *mut DfBy
#[no_mangle]
pub extern "C" fn df_dataframe_collect_ipc(
handle: *mut DataFrame,
token_handle: u64,
out: *mut DfByteBuffer,
) -> c_int {
take_result(|| {
let df = unsafe { *Box::from_raw(require_ptr(handle, "DataFrame handle")?) };
let token = resolve_token(token_handle);
let schema: SchemaRef = Arc::new(df.schema().as_arrow().clone());
let batches = runtime().block_on(df.collect())?;
let batches = runtime().block_on(run_cancellable(&token, df.collect()))?;
write_buffer(out, batches_ipc(schema, batches)?)
})
}

#[no_mangle]
pub extern "C" fn df_dataframe_execute_stream_ipc(
handle: *mut DataFrame,
token_handle: u64,
out: *mut DfByteBuffer,
) -> c_int {
take_result(|| {
let df = unsafe { *Box::from_raw(require_ptr(handle, "DataFrame handle")?) };
let token = resolve_token(token_handle);
let schema: SchemaRef = Arc::new(df.schema().as_arrow().clone());
let mut stream = runtime().block_on(df.execute_stream())?;
let batches = runtime().block_on(async {
let batches = runtime().block_on(run_cancellable(&token, async move {
let mut stream = df.execute_stream().await?;
let mut batches = Vec::new();
while let Some(batch) = stream.next().await {
batches.push(batch?);
}
Ok::<_, DataFusionError>(batches)
})?;
}))?;
write_buffer(out, batches_ipc(schema, batches)?)
})
}
Expand Down
60 changes: 52 additions & 8 deletions src/Apache.DataFusion/DataFrame/DataFrame.Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,67 @@ namespace Apache.DataFusion;

public sealed partial class DataFrame
{
public ArrowBatchReader Collect()
public ArrowBatchReader Collect() => Collect(CancellationToken.None);

/// <summary>
/// Execute the plan with cooperative cancellation. Firing
/// <paramref name="cancellationToken"/> from another thread aborts the
/// in-flight query at its next poll point and throws
/// <see cref="OperationCanceledException"/>. A token that cannot be
/// cancelled (e.g. <see cref="CancellationToken.None"/>) behaves exactly
/// like the parameterless overload.
/// </summary>
public ArrowBatchReader Collect(CancellationToken cancellationToken)
{
return new ArrowBatchReader(CollectIpcBytes());
return new ArrowBatchReader(CollectIpcBytes(cancellationToken));
}

internal byte[] CollectIpcBytes()
internal byte[] CollectIpcBytes() => CollectIpcBytes(CancellationToken.None);

internal byte[] CollectIpcBytes(CancellationToken cancellationToken)
{
IntPtr current = TakeHandle();
NativeMethods.Check(NativeMethods.df_dataframe_collect_ipc(current, out NativeMethods.ByteBuffer buffer));
return NativeMethods.CopyAndFree(buffer);
if (!cancellationToken.CanBeCanceled)
{
NativeMethods.Check(NativeMethods.df_dataframe_collect_ipc(current, 0UL, out NativeMethods.ByteBuffer buffer));
return NativeMethods.CopyAndFree(buffer);
}

using NativeCancellationToken token = NativeCancellationToken.Create();
using (cancellationToken.Register(static state => ((NativeCancellationToken)state!).Cancel(), token))
{
int status = NativeMethods.df_dataframe_collect_ipc(current, token.Handle, out NativeMethods.ByteBuffer buffer);
NativeMethods.CheckCancellable(status, cancellationToken);
return NativeMethods.CopyAndFree(buffer);
}
}

public ArrowBatchReader ExecuteStream()
public ArrowBatchReader ExecuteStream() => ExecuteStream(CancellationToken.None);

/// <summary>
/// Execute the plan and materialize the result with cooperative
/// cancellation. Firing <paramref name="cancellationToken"/> from another
/// thread aborts the in-flight query at its next poll point and throws
/// <see cref="OperationCanceledException"/>. A token that cannot be
/// cancelled (e.g. <see cref="CancellationToken.None"/>) behaves exactly
/// like the parameterless overload.
/// </summary>
public ArrowBatchReader ExecuteStream(CancellationToken cancellationToken)
{
IntPtr current = TakeHandle();
NativeMethods.Check(NativeMethods.df_dataframe_execute_stream_ipc(current, out NativeMethods.ByteBuffer buffer));
return new ArrowBatchReader(NativeMethods.CopyAndFree(buffer));
if (!cancellationToken.CanBeCanceled)
{
NativeMethods.Check(NativeMethods.df_dataframe_execute_stream_ipc(current, 0UL, out NativeMethods.ByteBuffer buffer));
return new ArrowBatchReader(NativeMethods.CopyAndFree(buffer));
}

using NativeCancellationToken token = NativeCancellationToken.Create();
using (cancellationToken.Register(static state => ((NativeCancellationToken)state!).Cancel(), token))
{
int status = NativeMethods.df_dataframe_execute_stream_ipc(current, token.Handle, out NativeMethods.ByteBuffer buffer);
NativeMethods.CheckCancellable(status, cancellationToken);
return new ArrowBatchReader(NativeMethods.CopyAndFree(buffer));
}
}

public byte[] SchemaIpc()
Expand Down
64 changes: 64 additions & 0 deletions src/Apache.DataFusion/Interop/NativeCancellationToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

namespace Apache.DataFusion.Interop;

/// <summary>
/// Owns a native cancellation token registered in the process-global registry.
/// One instance bridges a managed <see cref="CancellationToken"/> to an
/// in-flight native query: when the managed token fires, <see cref="Cancel"/>
/// signals the native token so a query parked inside the blocking FFI call
/// aborts at its next poll point.
/// </summary>
internal sealed class NativeCancellationToken : IDisposable
{
// Stored as long (not ulong) so Interlocked can guard the close/cancel race.
// The native registry hands out monotonically increasing IDs starting at 1,
// so the value never approaches the signed range and 0 is the freed sentinel.
private long handle;

private NativeCancellationToken(long handle)
{
this.handle = handle;
}

public static NativeCancellationToken Create()
{
NativeMethods.Check(NativeMethods.df_cancellation_token_new(out ulong handle));
return new NativeCancellationToken((long)handle);
}

public ulong Handle => (ulong)Interlocked.Read(ref handle);

public void Cancel()
{
long current = Interlocked.Read(ref handle);
if (current != 0)
{
NativeMethods.Check(NativeMethods.df_cancellation_token_cancel((ulong)current));
}
}

public void Dispose()
{
long current = Interlocked.Exchange(ref handle, 0);
if (current != 0)
{
NativeMethods.Check(NativeMethods.df_cancellation_token_free((ulong)current));
}
}
}
Loading
Loading