From b1bea9d96c4c6bb38e4c054de91b47a4c5e0a07c Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Fri, 31 Jan 2025 11:49:41 -0500 Subject: [PATCH] The graph RwLock is no longer needed since the GraphMap is now thread safe. Signed-off-by: Hiram Chirino --- modules/analysis/src/model.rs | 18 +++++------------- modules/analysis/src/service/load.rs | 4 ++-- modules/analysis/src/service/mod.rs | 15 ++++++--------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/modules/analysis/src/model.rs b/modules/analysis/src/model.rs index 22db392d..b7196aec 100644 --- a/modules/analysis/src/model.rs +++ b/modules/analysis/src/model.rs @@ -219,12 +219,9 @@ impl GraphMap { } // Add a new graph with the given key (write access) - pub fn insert( - &mut self, - key: String, - graph: Graph, - ) { + pub fn insert(&self, key: String, graph: Graph) { self.map.insert(key, Arc::new(graph)); + self.map.run_pending_tasks(); } // Retrieve a reference to a graph by its key (read access) @@ -236,13 +233,8 @@ impl GraphMap { } // Clear all graphs from the map - pub fn clear(&mut self) { + pub fn clear(&self) { self.map.invalidate_all(); + self.map.run_pending_tasks(); } -} - -// impl Default for GraphMap { -// fn default() -> Self { -// Self::new() -// } -// } +} \ No newline at end of file diff --git a/modules/analysis/src/service/load.rs b/modules/analysis/src/service/load.rs index 33dfd0db..be0be4f4 100644 --- a/modules/analysis/src/service/load.rs +++ b/modules/analysis/src/service/load.rs @@ -216,7 +216,7 @@ impl AnalysisService { /// Load the SBOM matching the provided ID #[instrument(skip(self, connection))] pub async fn load_graph(&self, connection: &C, distinct_sbom_id: &str) { - if self.graph.read().contains_key(distinct_sbom_id) { + if self.graph.contains_key(distinct_sbom_id) { // early return if we already loaded it return; } @@ -327,7 +327,7 @@ impl AnalysisService { // Set the result. A parallel call might have done the same. We wasted some time, but the // state is still correct. - self.graph.write().insert(distinct_sbom_id.to_string(), g); + self.graph.insert(distinct_sbom_id.to_string(), g); } /// Load all SBOMs by the provided IDs diff --git a/modules/analysis/src/service/mod.rs b/modules/analysis/src/service/mod.rs index 312648d6..c67a383e 100644 --- a/modules/analysis/src/service/mod.rs +++ b/modules/analysis/src/service/mod.rs @@ -13,7 +13,7 @@ use crate::{ }, Error, }; -use parking_lot::RwLock; + use petgraph::{ algo::is_cyclic_directed, graph::{Graph, NodeIndex}, @@ -38,7 +38,7 @@ use uuid::Uuid; #[derive(Clone)] pub struct AnalysisService { - graph: Arc>, + graph: Arc, } pub fn dep_nodes( @@ -157,7 +157,7 @@ impl AnalysisService { #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { - graph: Arc::new(RwLock::new(GraphMap::new(1024 * 1024 * 100))), + graph: Arc::new(GraphMap::new(1024 * 1024 * 100)), } } @@ -181,8 +181,7 @@ impl AnalysisService { } pub fn clear_all_graphs(&self) -> Result<(), Error> { - let mut manager = self.graph.write(); - manager.clear(); + self.graph.clear(); Ok(()) } @@ -197,10 +196,9 @@ impl AnalysisService { .all(connection) .await?; - let manager = self.graph.read(); Ok(AnalysisStatus { sbom_count: distinct_sbom_ids.len() as u32, - graph_count: manager.len() as u32, + graph_count: self.graph.len() as u32, }) } @@ -241,9 +239,8 @@ impl AnalysisService { let query = query.into(); // RwLock for reading hashmap - let graph_read_guard = self.graph.read(); for distinct_sbom_id in &distinct_sbom_ids { - if let Some(graph) = graph_read_guard.get(distinct_sbom_id.to_string().as_str()) { + if let Some(graph) = self.graph.get(distinct_sbom_id.to_string().as_str()) { if is_cyclic_directed(graph.deref()) { log::warn!( "analysis graph of sbom {} has circular references!",