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

Migrate from async_std to tokio #170

Merged
merged 2 commits into from
Aug 6, 2024
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: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ dynamic_plugin = []
default = ["dynamic_plugin"]

[dependencies]
async-std = "=1.12.0"
async-trait = "0.1.66"
dunce = "1.0.3"
git-version = "0.3.5"
Expand All @@ -49,6 +48,7 @@ regex = "1.7.1"
rocksdb = "0.22.0"
serde_json = "1.0.117"
tempfile = "3.4.0"
tokio = { version = "1.35.1", default-features = false } # Default features are disabled due to some crates' requirements
tracing = "0.1"
uhlc = "0.5.2"
walkdir = "2.3.2"
Expand All @@ -57,7 +57,6 @@ zenoh = { git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.
"internal",
"plugins"
] }

zenoh_backend_traits = { git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.0" }
zenoh-plugin-trait = { git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "dev/1.0.0" }

Expand Down
3 changes: 2 additions & 1 deletion src/data_info_mgt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
use std::{
borrow::Cow,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use async_std::sync::{Arc, Mutex};
use rocksdb::DB;
use tokio::sync::Mutex;
use tracing::trace;
use zenoh::{
bytes::{Encoding, ZBytes},
Expand Down
5 changes: 2 additions & 3 deletions src/files_mgt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use async_std::task;
use tracing::{debug, trace, warn};
use walkdir::{IntoIter, WalkDir};
use zenoh::{
Expand All @@ -37,7 +36,7 @@ use zenoh::{
Result as ZResult,
};

use crate::data_info_mgt::*;
use crate::{blockon_runtime, data_info_mgt::*};

pub const CONFLICT_SUFFIX: &str = ".##z";

Expand Down Expand Up @@ -378,7 +377,7 @@ impl Drop for FilesMgr {
match self.on_closure {
OnClosure::DeleteAll => {
// Close data_info_mgr at first
task::block_on(async move {
blockon_runtime(async move {
self.data_info_mgr
.close()
.await
Expand Down
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use std::{collections::HashMap, fs::DirBuilder, io::prelude::*, path::PathBuf};
use std::{collections::HashMap, fs::DirBuilder, future::Future, io::prelude::*, path::PathBuf};

use async_trait::async_trait;
use tempfile::tempfile_in;
Expand All @@ -35,6 +35,32 @@ mod data_info_mgt;
mod files_mgt;
use files_mgt::*;

const WORKER_THREAD_NUM: usize = 2;
const MAX_BLOCK_THREAD_NUM: usize = 50;
lazy_static::lazy_static! {
// The global runtime is used in the dynamic plugins, which we can't get the current runtime
static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(WORKER_THREAD_NUM)
.max_blocking_threads(MAX_BLOCK_THREAD_NUM)
.enable_all()
.build()
.expect("Unable to create runtime");
}
#[inline(always)]
fn blockon_runtime<F: Future>(task: F) -> F::Output {
// Check whether able to get the current runtime
match tokio::runtime::Handle::try_current() {
Ok(rt) => {
// Able to get the current runtime (standalone binary), spawn on the current runtime
tokio::task::block_in_place(|| rt.block_on(task))
}
Err(_) => {
// Unable to get the current runtime (dynamic plugins), spawn on the global runtime
tokio::task::block_in_place(|| TOKIO_RUNTIME.block_on(task))
}
}
}

/// The environement variable used to configure the root of all storages managed by this FileSystemBackend.
pub const SCOPE_ENV_VAR: &str = "ZENOH_BACKEND_FS_ROOT";

Expand Down