Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: runtime module #233

Merged
merged 21 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ jobs:

- name: Test
run: cargo test --no-fail-fast --all-targets --all-features --workspace


- name: Async-std Test
run: cargo test --no-fail-fast --all-targets --no-default-features --features "async-std" --features "storage-fs" --workspace

- name: Doc Test
run: cargo test --no-fail-fast --doc --all-features --workspace
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
[workspace]
resolver = "2"
odysa marked this conversation as resolved.
Show resolved Hide resolved
members = [
"crates/catalog/*",
"crates/examples",
"crates/iceberg",
"crates/integrations/*",
"crates/test_utils",
"crates/catalog/*",
"crates/examples",
"crates/iceberg",
"crates/integrations/*",
"crates/test_utils",
]

[workspace.package]
Expand All @@ -46,6 +46,7 @@ arrow-select = { version = "52" }
arrow-string = { version = "52" }
async-stream = "0.3.5"
async-trait = "0.1"
async-std = "1.12.0"
aws-config = "1.1.8"
aws-sdk-glue = "1.21.0"
bimap = "0.6"
Expand Down
9 changes: 7 additions & 2 deletions crates/iceberg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ license = { workspace = true }
keywords = ["iceberg"]

[features]
default = ["storage-fs", "storage-s3"]
default = ["storage-fs", "storage-s3", "tokio"]
storage-all = ["storage-fs", "storage-s3"]

storage-fs = ["opendal/services-fs"]
storage-s3 = ["opendal/services-s3"]

async-std = ["dep:async-std"]
tokio = ["dep:tokio"]

[dependencies]
anyhow = { workspace = true }
apache-avro = { workspace = true }
Expand All @@ -45,6 +48,7 @@ arrow-ord = { workspace = true }
arrow-schema = { workspace = true }
arrow-select = { workspace = true }
arrow-string = { workspace = true }
async-std = { workspace = true, optional = true, features = ["attributes"] }
async-stream = { workspace = true }
async-trait = { workspace = true }
bimap = { workspace = true }
Expand All @@ -71,7 +75,7 @@ serde_derive = { workspace = true }
serde_json = { workspace = true }
serde_repr = { workspace = true }
serde_with = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true, optional = true }
typed-builder = { workspace = true }
url = { workspace = true }
urlencoding = { workspace = true }
Expand All @@ -82,3 +86,4 @@ iceberg_test_utils = { path = "../test_utils", features = ["tests"] }
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
tera = { workspace = true }
tokio = { workspace = true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was wrong about the dependency resolution of cargo. If we add this line in dev.dependency, we still introduce tokio as dependency unconditonally. The evidency is this issue, e.g. the compilation is supposed to fail if tokio is not introduced. So we should

  1. Remove this line.
  2. Resolve refactor: Remove dependency of tokio. #418 first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @odysa seems this is still not removed?

2 changes: 2 additions & 0 deletions crates/iceberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ pub mod expr;
pub mod transaction;
pub mod transform;

mod runtime;

pub mod arrow;
pub mod writer;
103 changes: 103 additions & 0 deletions crates/iceberg/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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.

// This module contains the async runtime abstraction for iceberg.

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

pub enum JoinHandle<T> {
#[cfg(feature = "tokio")]
Tokio(tokio::task::JoinHandle<T>),
#[cfg(all(feature = "async-std", not(feature = "tokio")))]
AsyncStd(async_std::task::JoinHandle<T>),
}

impl<T: Send + 'static> Future for JoinHandle<T> {
type Output = T;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {
#[cfg(feature = "tokio")]
JoinHandle::Tokio(handle) => Pin::new(handle)
.poll(cx)
.map(|h| h.expect("tokio spawned task failed")),
#[cfg(all(feature = "async-std", not(feature = "tokio")))]
JoinHandle::AsyncStd(handle) => Pin::new(handle).poll(cx),
}
}
}

#[allow(dead_code)]
pub fn spawn<F>(f: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
#[cfg(feature = "tokio")]
return JoinHandle::Tokio(tokio::task::spawn(f));

#[cfg(all(feature = "async-std", not(feature = "tokio")))]
return JoinHandle::AsyncStd(async_std::task::spawn(f));
}

#[allow(dead_code)]
pub fn spawn_blocking<F, T>(f: F) -> JoinHandle<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
#[cfg(feature = "tokio")]
return JoinHandle::Tokio(tokio::task::spawn_blocking(f));

#[cfg(all(feature = "async-std", not(feature = "tokio")))]
return JoinHandle::AsyncStd(async_std::task::spawn_blocking(f));
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(feature = "tokio")]
#[tokio::test]
odysa marked this conversation as resolved.
Show resolved Hide resolved
async fn test_tokio_spawn() {
let handle = spawn(async { 1 + 1 });
assert_eq!(handle.await, 2);
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn test_tokio_spawn_blocking() {
let handle = spawn_blocking(|| 1 + 1);
assert_eq!(handle.await, 2);
}

#[cfg(all(feature = "async-std", not(feature = "tokio")))]
#[async_std::test]
async fn test_async_std_spawn() {
let handle = spawn(async { 1 + 1 });
assert_eq!(handle.await, 2);
}

#[cfg(all(feature = "async-std", not(feature = "tokio")))]
#[async_std::test]
async fn test_async_std_spawn_blocking() {
let handle = spawn_blocking(|| 1 + 1);
assert_eq!(handle.await, 2);
}
}
Loading