Skip to content

Commit

Permalink
plugin API rework, plugin status support (#583)
Browse files Browse the repository at this point in the history
* made runtime state fields private

* RuntimeState private

* removing ValidationFunction unfinished

* make validator function

* removed validation function

* removed cyclic reference

* format fix

* clippy fix

* unfinished

* zenoh-plugin-trait corrected

* version control unfinished

* loader compatibility version

* string version  with features

* concat_enabled_features

* example storage plugin

* config corrected for example storage plugin

* example storage plugin works as memory one

* support load api from example stroage plugin

* unifying plugn loading unfinished

* replaced volumes map to plugin_manager

* static memory unfinihed

* plugin manager refactor

* plugin manager api update

* running plugin index added

* safer plugin load api, unfinished

* compiles

* static memory plugin fix

* compare version improved

* moved feature list to crate level

* linter fix

* unfinished

* unfinished

* send + sync problem

* unfinished

* unfinished

* compilation errors fixed

* compiles

* memory plugin loading fix

* startargs and instance traits

* plruings method  cow

* plugin trait sources reorganised

* get plugin names list recursively

* pligins method unifinshed

* strip_prefix

* adminspace

* renamed loading to manager

* log prints, recurse info

* adminspace works

* fixed compilation of  example-plugin

* debug error removed

* unfinished

* plugin version in compatibility api

* version in plugin status unfinished

* version works

* status method removed from trait

* removed tuple

* format fix

* commented out plugins section in config sample

* doc test fixed

* no-mangled feature added to test

* test for default zenohd features

* tests added, leaking through zenoh-ext fixed

* no feature test added

* cargo fmt

* removed global Arc import as it's under features

* cargo fmt

* removed incorrect test, right one in zenohd

* duplicated import fix

* compilation fix

* compilation fix

* compilation fix

* compilation errors fixed

* nom_mangle in lib removed

* no full failure due to one storage

* config for testing plugins, error s logging

* no_mangle in example

* compile fixes

* dds plugin tested

* cargo fmt

* removed reexport of ZResult and Runtime types from plugin mod

* doc comments updated

* renaming, removed unused type

* constant version string

* macro simplified

* doc updated, version macro added

* version simplified, cargo fmt

* unused use removed

* long_version field added

* default impls for RunningPluginTrait, doc comments

* todos added

* cargo fmt

* diagnostic improved, test config partially made, path renamings

* diagnostic improved, test config partially made, path renamings

* incorrect warn replaced to debug

* no_mangle into plugin's code

* all plugins covered in test config

* comments update

* cargo fmt

* restored cargo.lock

* undo changes in DEFAULT_CONFIG

* compatibility code in separate .rs, minor changes

* comments to declare_plugin

* renamings, removed unnecessary sample configs

* cargo fmt

* debug code removed

* log::info changed to log::debug
  • Loading branch information
milyin authored Jan 23, 2024
1 parent c6d7257 commit 8d7a634
Show file tree
Hide file tree
Showing 43 changed files with 2,236 additions and 963 deletions.
32 changes: 31 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ members = [
"io/zenoh-links/zenoh-link-ws/",
"io/zenoh-links/zenoh-link-unixpipe/",
"io/zenoh-transport",
"plugins/zenoh-backend-example",
"plugins/zenoh-plugin-example",
"plugins/zenoh-backend-traits",
"plugins/zenoh-plugin-rest",
Expand Down
51 changes: 28 additions & 23 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use serde_json::Value;
use std::convert::TryFrom; // This is a false positive from the rust analyser
use std::{
any::Any,
collections::{HashMap, HashSet},
collections::HashSet,
fmt,
io::Read,
marker::PhantomData,
net::SocketAddr,
path::Path,
sync::{Arc, Mutex, MutexGuard},
sync::{Arc, Mutex, MutexGuard, Weak},
};
use validated_struct::ValidatedMapAssociatedTypes;
pub use validated_struct::{GetError, ValidatedMap};
Expand Down Expand Up @@ -70,15 +70,21 @@ impl Zeroize for SecretString {

pub type SecretValue = Secret<SecretString>;

pub type ValidationFunction = std::sync::Arc<
dyn Fn(
&str,
&serde_json::Map<String, serde_json::Value>,
&serde_json::Map<String, serde_json::Value>,
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>>
+ Send
+ Sync,
>;
pub trait ConfigValidator: Send + Sync {
fn check_config(
&self,
_plugin_name: &str,
_path: &str,
_current: &serde_json::Map<String, serde_json::Value>,
_new: &serde_json::Map<String, serde_json::Value>,
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
Ok(None)
}
}

// Necessary to allow to set default emplty weak referece value to plugin.validator field
// because empty weak value is not allowed for Arc<dyn Trait>
impl ConfigValidator for () {}

/// Creates an empty zenoh net Session configuration.
pub fn empty() -> Config {
Expand Down Expand Up @@ -520,8 +526,8 @@ fn config_deser() {
}

impl Config {
pub fn add_plugin_validator(&mut self, name: impl Into<String>, validator: ValidationFunction) {
self.plugins.validators.insert(name.into(), validator);
pub fn set_plugin_validator<T: ConfigValidator + 'static>(&mut self, validator: Weak<T>) {
self.plugins.validator = validator;
}

pub fn plugin(&self, name: &str) -> Option<&Value> {
Expand Down Expand Up @@ -882,7 +888,7 @@ fn user_conf_validator(u: &UsrPwdConf) -> bool {
#[derive(Clone)]
pub struct PluginsConfig {
values: Value,
validators: HashMap<String, ValidationFunction>,
validator: std::sync::Weak<dyn ConfigValidator>,
}
fn sift_privates(value: &mut serde_json::Value) {
match value {
Expand Down Expand Up @@ -948,11 +954,9 @@ impl PluginsConfig {
Some(first_in_plugin) => first_in_plugin,
None => {
self.values.as_object_mut().unwrap().remove(plugin);
self.validators.remove(plugin);
return Ok(());
}
};
let validator = self.validators.get(plugin);
let (old_conf, mut new_conf) = match self.values.get_mut(plugin) {
Some(plugin) => {
let clone = plugin.clone();
Expand Down Expand Up @@ -993,8 +997,9 @@ impl PluginsConfig {
}
other => bail!("{} cannot be indexed", other),
}
let new_conf = if let Some(validator) = validator {
match validator(
let new_conf = if let Some(validator) = self.validator.upgrade() {
match validator.check_config(
plugin,
&key[("plugins/".len() + plugin.len())..],
old_conf.as_object().unwrap(),
new_conf.as_object().unwrap(),
Expand Down Expand Up @@ -1023,7 +1028,7 @@ impl Default for PluginsConfig {
fn default() -> Self {
Self {
values: Value::Object(Default::default()),
validators: Default::default(),
validator: std::sync::Weak::<()>::new(),
}
}
}
Expand All @@ -1033,8 +1038,8 @@ impl<'a> serde::Deserialize<'a> for PluginsConfig {
D: serde::Deserializer<'a>,
{
Ok(PluginsConfig {
validators: Default::default(),
values: serde::Deserialize::deserialize(deserializer)?,
validator: std::sync::Weak::<()>::new(),
})
}
}
Expand Down Expand Up @@ -1122,7 +1127,6 @@ impl validated_struct::ValidatedMap for PluginsConfig {
validated_struct::InsertionError: From<D::Error>,
{
let (plugin, key) = validated_struct::split_once(key, '/');
let validator = self.validators.get(plugin);
let new_value: Value = serde::Deserialize::deserialize(deserializer)?;
let value = self
.values
Expand All @@ -1131,8 +1135,9 @@ impl validated_struct::ValidatedMap for PluginsConfig {
.entry(plugin)
.or_insert(Value::Null);
let mut new_value = value.clone().merge(key, new_value)?;
if let Some(validator) = validator {
match validator(
if let Some(validator) = self.validator.upgrade() {
match validator.check_config(
plugin,
key,
value.as_object().unwrap(),
new_value.as_object().unwrap(),
Expand Down
45 changes: 45 additions & 0 deletions plugins/zenoh-backend-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright (c) 2023 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <[email protected]>
#
[package]
rust-version = { workspace = true }
name = "zenoh-backend-example"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }

[features]
default = ["no_mangle"]
no_mangle = []

[lib]
name = "zenoh_backend_example"
# This crate type will make `cargo` output a dynamic library instead of a rust static library
crate-type = ["cdylib"]

[dependencies]
async-std = { workspace = true, features = ["default"] }
clap = { workspace = true }
const_format = { workspace = true }
env_logger = { workspace = true }
futures = { workspace = true }
git-version = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
zenoh = { workspace = true }
zenoh-core = { workspace = true }
zenoh-plugin-trait = { workspace = true }
zenoh-result = { workspace = true }
zenoh-util = { workspace = true }
async-trait = { workspace = true }
zenoh_backend_traits = { workspace = true }
Loading

0 comments on commit 8d7a634

Please sign in to comment.