Fit larger Linux package inventories within bounded snapshots
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -1,8 +1,20 @@
|
||||
//! Additive JSON observation mapping inside the existing snapshot payload. This
|
||||
//! does not introduce a protobuf tag or a fabricated receive time.
|
||||
use anyhow::Result;
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_domain::inventory_document::LINUX_SOFTWARE_OBSERVATION_MAX_BYTES;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn truncate_rows(observation: &mut Value) -> bool {
|
||||
let Some(rows) = observation["data"].as_array_mut().filter(|v| !v.is_empty()) else {
|
||||
return false;
|
||||
};
|
||||
rows.truncate(rows.len() / 2);
|
||||
observation["truncated"] = json!(true);
|
||||
observation["completeness"] = json!("partial");
|
||||
observation["error_code"] = json!("WIRE_SCOPE_LIMIT");
|
||||
true
|
||||
}
|
||||
|
||||
pub fn observations(inventory: &crate::inventory::LinuxInventory) -> Result<Vec<Value>> {
|
||||
let value = serde_json::to_value(inventory)?;
|
||||
let mut result = Vec::new();
|
||||
@@ -15,17 +27,15 @@ pub fn observations(inventory: &crate::inventory::LinuxInventory) -> Result<Vec<
|
||||
// Bound each scope independently. Never drop unsupported/error metadata
|
||||
// or imply that absent/truncated rows were deleted from a device.
|
||||
let max_bytes = if name == "software" {
|
||||
rmm_domain::inventory_document::SOFTWARE_OBSERVATION_MAX_BYTES
|
||||
LINUX_SOFTWARE_OBSERVATION_MAX_BYTES
|
||||
} else {
|
||||
32768
|
||||
};
|
||||
while serde_json::to_vec(&observation)?.len() > max_bytes {
|
||||
observation["truncated"] = json!(true);
|
||||
observation["completeness"] = json!("partial");
|
||||
observation["error_code"] = json!("WIRE_SCOPE_LIMIT");
|
||||
if let Some(rows) = observation["data"].as_array_mut().filter(|v| !v.is_empty()) {
|
||||
rows.truncate(rows.len() / 2);
|
||||
} else {
|
||||
if !truncate_rows(&mut observation) {
|
||||
observation["truncated"] = json!(true);
|
||||
observation["completeness"] = json!("partial");
|
||||
observation["error_code"] = json!("WIRE_SCOPE_LIMIT");
|
||||
observation["data"] = Value::Null;
|
||||
observation["quality"] = json!("unknown");
|
||||
break;
|
||||
@@ -47,6 +57,20 @@ pub fn snapshot(extended: bool) -> Result<Value> {
|
||||
Ok(base)
|
||||
}
|
||||
|
||||
pub fn encode_snapshot(mut inventory: Value) -> Result<Vec<u8>> {
|
||||
let mut bytes = serde_json::to_vec(&inventory)?;
|
||||
// Preserve the other scopes when a large package list fills the snapshot.
|
||||
while bytes.len() > 512 * 1024 {
|
||||
let software = inventory
|
||||
.get_mut("observations")
|
||||
.and_then(Value::as_array_mut)
|
||||
.and_then(|scopes| scopes.iter_mut().find(|scope| scope["scope"] == "linux.software"));
|
||||
ensure!(software.is_some_and(truncate_rows), "INVENTORY_WIRE_LIMIT");
|
||||
bytes = serde_json::to_vec(&inventory)?;
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -67,7 +91,7 @@ mod tests {
|
||||
assert!(!observation["source_id"].as_str().unwrap().is_empty());
|
||||
assert!(observation.get("received_at").is_none());
|
||||
let limit = if observation["scope"] == "linux.software" {
|
||||
rmm_domain::inventory_document::SOFTWARE_OBSERVATION_MAX_BYTES
|
||||
LINUX_SOFTWARE_OBSERVATION_MAX_BYTES
|
||||
} else {
|
||||
32768
|
||||
};
|
||||
@@ -97,4 +121,21 @@ mod tests {
|
||||
assert_eq!(software["truncated"], true);
|
||||
assert_eq!(software["completeness"], "partial");
|
||||
}
|
||||
#[test]
|
||||
fn snapshot_limit_shrinks_software_and_preserves_other_scopes() {
|
||||
let network = json!({"scope":"linux.interfaces","data":"x".repeat(280 * 1024)});
|
||||
let data = vec!["x".repeat(1024); 240];
|
||||
let snapshot = json!({"observations":[
|
||||
network,
|
||||
{"scope":"linux.software","completeness":"complete","data":data},
|
||||
]});
|
||||
let bytes = encode_snapshot(snapshot).unwrap();
|
||||
let bounded: Value = serde_json::from_slice(&bytes).unwrap();
|
||||
assert_eq!(bounded["observations"][0], network);
|
||||
assert_eq!(bounded["observations"][1]["completeness"], "partial");
|
||||
assert_eq!(
|
||||
bounded["observations"][1]["data"].as_array().unwrap().len(),
|
||||
120
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user