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

updated to new plugin API #40

Merged
merged 6 commits into from
Feb 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
62 changes: 34 additions & 28 deletions Cargo.lock

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

19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ log = "0.4.17"
rocksdb = "0.20.1"
serde_json = "1.0.94"
uhlc = "0.5.2"
zenoh = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main", features = ["unstable"] }
zenoh-codec = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-collections = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-core = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-protocol = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-util = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-keyexpr = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh_backend_traits = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main", features = [
"unstable",
] }
zenoh-codec = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-collections = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-core = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-protocol = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-util = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-keyexpr = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh_backend_traits = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }
zenoh-plugin-trait = { git = "https://github.com/eclipse-zenoh/zenoh", branch = "main" }

[build-dependencies]
rustc_version = "0.4.0"
Expand Down
69 changes: 38 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use zenoh_backend_traits::config::{StorageConfig, VolumeConfig};
use zenoh_backend_traits::*;
use zenoh_codec::{RCodec, WCodec, Zenoh080};
use zenoh_core::{bail, zerror};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin};
use zenoh_util::zenoh_home;

/// The environement variable used to configure the root of all storages managed by this RocksdbBackend.
Expand All @@ -53,9 +54,7 @@ pub const NONE_KEY: &str = "@@none_key@@";
const CF_PAYLOADS: &str = rocksdb::DEFAULT_COLUMN_FAMILY_NAME;
const CF_DATA_INFO: &str = "data_info";

const GIT_VERSION: &str = git_version::git_version!(prefix = "v", cargo_prefix = "v");
lazy_static::lazy_static! {
static ref LONG_VERSION: String = format!("{} built with {}", GIT_VERSION, env!("RUSTC_VERSION"));
static ref GC_PERIOD: Duration = Duration::new(5, 0);
static ref MIN_DELAY_BEFORE_REMOVAL: NTP64 = NTP64::from(Duration::new(5, 0));
}
Expand All @@ -65,41 +64,49 @@ pub(crate) enum OnClosure {
DoNothing,
}

#[allow(dead_code)]
const CREATE_BACKEND_TYPECHECK: CreateVolume = create_volume;

#[no_mangle]
pub fn create_volume(_unused: VolumeConfig) -> ZResult<Box<dyn Volume>> {
// For some reasons env_logger is sometime not active in a loaded library.
// Try to activate it here, ignoring failures.
let _ = env_logger::try_init();
debug!("RocksDB backend {}", LONG_VERSION.as_str());

let root = if let Some(dir) = std::env::var_os(SCOPE_ENV_VAR) {
PathBuf::from(dir)
} else {
let mut dir = PathBuf::from(zenoh_home());
dir.push(DEFAULT_ROOT_DIR);
dir
};
let mut properties = Properties::default();
properties.insert("root".into(), root.to_string_lossy().into());
properties.insert("version".into(), LONG_VERSION.clone());

let admin_status = HashMap::from(properties)
.into_iter()
.map(|(k, v)| (k, serde_json::Value::String(v)))
.collect();
Ok(Box::new(RocksdbBackend { admin_status, root }))
pub struct RocksDbBackend {}
zenoh_plugin_trait::declare_plugin!(RocksDbBackend);

impl Plugin for RocksDbBackend {
type StartArgs = VolumeConfig;
type Instance = VolumeInstance;

const DEFAULT_NAME: &'static str = "rocks_backend";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

fn start(_name: &str, _config: &Self::StartArgs) -> ZResult<Self::Instance> {
// For some reasons env_logger is sometime not active in a loaded library.
// Try to activate it here, ignoring failures.
let _ = env_logger::try_init();
debug!("RocksDB backend {}", Self::PLUGIN_LONG_VERSION);

let root = if let Some(dir) = std::env::var_os(SCOPE_ENV_VAR) {
PathBuf::from(dir)
} else {
let mut dir = PathBuf::from(zenoh_home());
dir.push(DEFAULT_ROOT_DIR);
dir
};
let mut properties = Properties::default();
properties.insert("root".into(), root.to_string_lossy().into());
properties.insert("version".into(), Self::PLUGIN_VERSION.into());

let admin_status = HashMap::from(properties)
.into_iter()
.map(|(k, v)| (k, serde_json::Value::String(v)))
.collect();
Ok(Box::new(RocksdbVolume { admin_status, root }))
}
}

pub struct RocksdbBackend {
pub struct RocksdbVolume {
admin_status: serde_json::Value,
root: PathBuf,
}

#[async_trait]
impl Volume for RocksdbBackend {
impl Volume for RocksdbVolume {
fn get_admin_status(&self) -> serde_json::Value {
self.admin_status.clone()
}
Expand All @@ -112,7 +119,7 @@ impl Volume for RocksdbBackend {
}
}

async fn create_storage(&mut self, config: StorageConfig) -> ZResult<Box<dyn Storage>> {
async fn create_storage(&self, config: StorageConfig) -> ZResult<Box<dyn Storage>> {
let volume_cfg = match config.volume_cfg.as_object() {
Some(v) => v,
None => bail!("rocksdb backed storages need volume-specific configurations"),
Expand Down