Skip to content

Commit 09fc99e

Browse files
pass strings instead of serde value between plugins
1 parent aa43549 commit 09fc99e

File tree

9 files changed

+46
-52
lines changed

9 files changed

+46
-52
lines changed

plugins/zenoh-backend-example/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl Default for ExampleStorage {
6060

6161
#[async_trait]
6262
impl Volume for ExampleBackend {
63-
fn get_admin_status(&self) -> serde_json::Value {
64-
serde_json::Value::Null
63+
fn get_admin_status(&self) -> String {
64+
String::default()
6565
}
6666
fn get_capability(&self) -> Capability {
6767
Capability {
@@ -76,8 +76,8 @@ impl Volume for ExampleBackend {
7676

7777
#[async_trait]
7878
impl Storage for ExampleStorage {
79-
fn get_admin_status(&self) -> serde_json::Value {
80-
serde_json::Value::Null
79+
fn get_admin_status(&self) -> String {
80+
String::default()
8181
}
8282
async fn put(
8383
&mut self,

plugins/zenoh-backend-traits/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
//!
4646
//! #[async_trait]
4747
//! impl Volume for MyVolumeType {
48-
//! fn get_admin_status(&self) -> serde_json::Value {
48+
//! fn get_admin_status(&self) -> String {
4949
//! // This operation is called on GET operation on the admin space for the Volume
5050
//! // Here we reply with a static status (containing the configuration properties).
5151
//! // But we could add dynamic properties for Volume monitoring.
52-
//! self.config.to_json_value()
52+
//! self.config.to_json_value().to_string();
5353
//! }
5454
//!
5555
//! fn get_capability(&self) -> Capability {
@@ -80,11 +80,11 @@
8080
//!
8181
//! #[async_trait]
8282
//! impl Storage for MyStorage {
83-
//! fn get_admin_status(&self) -> serde_json::Value {
83+
//! fn get_admin_status(&self) -> String {
8484
//! // This operation is called on GET operation on the admin space for the Storage
8585
//! // Here we reply with a static status (containing the configuration properties).
8686
//! // But we could add dynamic properties for Storage monitoring.
87-
//! self.config.to_json_value()
87+
//! self.config.to_json_value().to_string();
8888
//! }
8989
//!
9090
//! async fn put(&mut self, key: Option<OwnedKeyExpr>, payload: ZBytes, encoding: Encoding, timestamp: Timestamp) -> zenoh::Result<StorageInsertionResult> {
@@ -183,9 +183,9 @@ pub struct StoredData {
183183
/// Trait to be implemented by a Backend.
184184
#[async_trait]
185185
pub trait Volume: Send + Sync {
186-
/// Returns the status that will be sent as a reply to a query
186+
/// Returns the status in the form of json string that will be sent as a reply to a query
187187
/// on the administration space for this backend.
188-
fn get_admin_status(&self) -> serde_json::Value;
188+
fn get_admin_status(&self) -> String;
189189

190190
/// Returns the capability of this backend
191191
fn get_capability(&self) -> Capability;
@@ -209,7 +209,7 @@ impl PluginInstance for VolumeInstance {}
209209
pub trait Storage: Send + Sync {
210210
/// Returns the status that will be sent as a reply to a query
211211
/// on the administration space for this storage.
212-
fn get_admin_status(&self) -> serde_json::Value;
212+
fn get_admin_status(&self) -> String;
213213

214214
/// Function called for each incoming data ([`Sample`](zenoh::sample::Sample)) to be stored in this storage.
215215
/// A key can be `None` if it matches the `strip_prefix` exactly.

plugins/zenoh-plugin-example/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ struct RunningPlugin(Arc<Mutex<RunningPluginInner>>);
130130
impl PluginControl for RunningPlugin {}
131131

132132
impl RunningPluginTrait for RunningPlugin {
133-
fn config_checker(
134-
&self,
135-
path: &str,
136-
old: &serde_json::Map<String, serde_json::Value>,
137-
new: &serde_json::Map<String, serde_json::Value>,
138-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
133+
fn config_checker(&self, path: &str, old: &str, new: &str) -> ZResult<Option<String>> {
134+
let old = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(old)?;
135+
let new = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(new)?;
139136
let mut guard = zlock!(&self.0);
140137
const STORAGE_SELECTOR: &str = "storage-selector";
141138
if path == STORAGE_SELECTOR || path.is_empty() {

plugins/zenoh-plugin-rest/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub struct Config {
4040
__plugin__: Option<String>,
4141
}
4242

43-
impl From<&Config> for serde_json::Value {
43+
impl From<&Config> for String {
4444
fn from(c: &Config) -> Self {
45-
serde_json::to_value(c).unwrap()
45+
serde_json::to_value(c).unwrap().to_string()
4646
}
4747
}
4848

plugins/zenoh-plugin-storage-manager/src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,12 @@ impl PluginControl for StorageRuntime {
316316
}
317317

318318
impl RunningPluginTrait for StorageRuntime {
319-
fn config_checker(
320-
&self,
321-
_: &str,
322-
old: &serde_json::Map<String, serde_json::Value>,
323-
new: &serde_json::Map<String, serde_json::Value>,
324-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
319+
fn config_checker(&self, _: &str, old: &str, new: &str) -> ZResult<Option<String>> {
320+
let old = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(old)?;
321+
let new = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(new)?;
325322
let name = { zlock!(self.0).name.clone() };
326-
let old = PluginConfig::try_from((&name, old))?;
327-
let new = PluginConfig::try_from((&name, new))?;
323+
let old = PluginConfig::try_from((&name, &old))?;
324+
let new = PluginConfig::try_from((&name, &new))?;
328325
tracing::debug!("config change requested for plugin '{}'", name);
329326
tracing::debug!("old config: {:?}", &old);
330327
tracing::debug!("new config: {:?}", &new);
@@ -381,7 +378,7 @@ impl RunningPluginTrait for StorageRuntime {
381378
rx.recv().await
382379
})
383380
}) {
384-
responses.push(Response::new(key.clone(), value))
381+
responses.push(Response::new(key.clone(), value.to_string()))
385382
}
386383
}
387384
})

plugins/zenoh-plugin-storage-manager/src/memory_backend/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl Plugin for MemoryBackend {
5050

5151
#[async_trait]
5252
impl Volume for MemoryBackend {
53-
fn get_admin_status(&self) -> serde_json::Value {
54-
self.config.to_json_value()
53+
fn get_admin_status(&self) -> String {
54+
self.config.to_json_value().to_string()
5555
}
5656

5757
fn get_capability(&self) -> Capability {
@@ -90,8 +90,8 @@ impl MemoryStorage {
9090

9191
#[async_trait]
9292
impl Storage for MemoryStorage {
93-
fn get_admin_status(&self) -> serde_json::Value {
94-
self.config.to_json_value()
93+
fn get_admin_status(&self) -> String {
94+
self.config.to_json_value().to_string()
9595
}
9696

9797
async fn put(

plugins/zenoh-plugin-storage-manager/src/storages_mgt/service.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,12 @@ impl StorageService {
198198
},
199199
StorageMessage::GetStatus(tx) => {
200200
let storage = self.storage.lock().await;
201-
std::mem::drop(tx.send(storage.get_admin_status()).await);
201+
let status = serde_json::from_str::<serde_json::Value>(&storage.get_admin_status());
202202
drop(storage);
203+
match status {
204+
Ok(s) => std::mem::drop(tx.send(s).await),
205+
Err(e) => tracing::error!("Failed to convert admin_status to json for storage '{}': {}", self.name, e),
206+
}
203207
}
204208
};
205209
},

zenoh/src/api/plugins.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ impl PluginControl for RunningPlugin {
4343
impl PluginInstance for RunningPlugin {}
4444

4545
#[non_exhaustive]
46-
#[derive(serde::Serialize, Debug, Clone)]
46+
#[derive(Debug, Clone)]
4747
/// A Response for the administration space.
4848
pub struct Response {
4949
pub key: String,
50-
pub value: serde_json::Value,
50+
pub json_value: String,
5151
}
5252

5353
impl Response {
54-
pub fn new(key: String, value: serde_json::Value) -> Self {
55-
Self { key, value }
54+
pub fn new(key: String, json_value: String) -> Self {
55+
Self { key, json_value }
5656
}
5757
}
5858

@@ -69,12 +69,7 @@ pub trait RunningPluginTrait: Send + Sync + PluginControl {
6969
/// Useful when the changes affect settings that aren't hot-configurable for your plugin.
7070
/// * `Ok(None)` indicates that the plugin has accepted the configuration change.
7171
/// * `Ok(Some(value))` indicates that the plugin would rather the new configuration be `value`.
72-
fn config_checker(
73-
&self,
74-
_path: &str,
75-
_current: &serde_json::Map<String, serde_json::Value>,
76-
_new: &serde_json::Map<String, serde_json::Value>,
77-
) -> ZResult<Option<serde_json::Map<String, serde_json::Value>>> {
72+
fn config_checker(&self, _path: &str, _current: &str, _new: &str) -> ZResult<Option<String>> {
7873
bail!("Runtime configuration change not supported");
7974
}
8075
/// Used to request plugin's status for the administration space.

zenoh/src/net/runtime/adminspace.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ impl ConfigValidator for AdminSpace {
9191
// on config comparison (see `PluginDiff`)
9292
return Ok(None);
9393
};
94-
plugin.instance().config_checker(path, current, new)
94+
let current = serde_json::to_string(current)?;
95+
let new = serde_json::to_string(new)?;
96+
match plugin.instance().config_checker(path, &current, &new)? {
97+
Some(s) => Ok(Some(serde_json::from_str::<
98+
serde_json::Map<String, serde_json::Value>,
99+
>(&s)?)),
100+
None => Ok(None),
101+
}
95102
}
96103
#[cfg(not(feature = "plugins"))]
97104
{
@@ -958,14 +965,8 @@ fn plugins_status(context: &AdminContext, query: Query) {
958965
Ok(Ok(responses)) => {
959966
for response in responses {
960967
if let Ok(key_expr) = KeyExpr::try_from(response.key) {
961-
match serde_json::to_vec(&response.value) {
962-
Ok(bytes) => {
963-
if let Err(e) = query.reply(key_expr, bytes).encoding(Encoding::APPLICATION_JSON).wait() {
964-
tracing::error!("Error sending AdminSpace reply: {:?}", e);
965-
}
966-
}
967-
Err(e) => tracing::debug!("Admin query error: {}", e),
968-
968+
if let Err(e) = query.reply(key_expr, response.json_value).encoding(Encoding::APPLICATION_JSON).wait() {
969+
tracing::error!("Error sending AdminSpace reply: {:?}", e);
969970
}
970971
} else {
971972
tracing::error!("Error: plugin {} replied with an invalid key", plugin_key);

0 commit comments

Comments
 (0)