-
Notifications
You must be signed in to change notification settings - Fork 32
fix: package ingestion race condition (TC-3152) #2080
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
Open
mrizzi
wants to merge
2
commits into
guacsec:main
Choose a base branch
from
mrizzi:fix-concurrent-ingestion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| use crate::graph::{ | ||
| advisory::{purl_status::PurlStatus, version::VersionInfo}, | ||
| error::Error, | ||
| }; | ||
| use sea_orm::{ActiveValue::Set, ConnectionTrait, EntityTrait, QueryFilter}; | ||
| use sea_query::{Expr, OnConflict, PgFunc}; | ||
| use std::collections::{BTreeMap, BTreeSet}; | ||
| use tracing::instrument; | ||
| use trustify_common::{cpe::Cpe, db::chunk::EntityChunkedIter, purl::Purl}; | ||
| use trustify_entity::{purl_status, status, version_range}; | ||
| use uuid::Uuid; | ||
|
|
||
| /// Input data for creating a PURL status entry | ||
| #[derive(Clone, Debug)] | ||
| pub struct PurlStatusEntry { | ||
| pub advisory_id: Uuid, | ||
| pub vulnerability_id: String, | ||
| pub purl: Purl, | ||
| pub status: String, | ||
| pub version_info: VersionInfo, | ||
| pub context_cpe: Option<Cpe>, | ||
| } | ||
|
|
||
| /// Creator for batch insertion of PURL statuses | ||
| /// | ||
| /// Follows the Creator pattern used by PurlCreator, CpeCreator, etc. | ||
| /// Collects PURL status entries and creates them in batches to avoid | ||
| /// N+1 query problems and race conditions. | ||
| #[derive(Default)] | ||
| pub struct PurlStatusCreator { | ||
| entries: Vec<PurlStatusEntry>, | ||
| } | ||
|
|
||
| impl PurlStatusCreator { | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Add a PURL status entry to be created | ||
| pub fn add(&mut self, entry: &PurlStatusEntry) { | ||
| self.entries.push(entry.clone()); | ||
| } | ||
|
|
||
| /// Create all collected PURL statuses in batches | ||
| #[instrument(skip_all, fields(num = self.entries.len()), err(level=tracing::Level::INFO))] | ||
| pub async fn create<C>(self, connection: &C) -> Result<(), Error> | ||
| where | ||
| C: ConnectionTrait, | ||
| { | ||
| if self.entries.is_empty() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // 1. Batch lookup all unique status slugs | ||
| let unique_statuses: Vec<String> = self | ||
| .entries | ||
| .iter() | ||
| .map(|e| e.status.clone()) | ||
| .collect::<BTreeSet<_>>() | ||
| .into_iter() | ||
| .collect(); | ||
|
|
||
| let status_models = status::Entity::find() | ||
| .filter(Expr::col(status::Column::Slug).eq(PgFunc::any(unique_statuses))) | ||
| .all(connection) | ||
| .await?; | ||
|
|
||
| let status_map: BTreeMap<String, Uuid> = status_models | ||
| .into_iter() | ||
| .map(|s| (s.slug.clone(), s.id)) | ||
| .collect(); | ||
|
|
||
| // 2. Deduplicate and build ActiveModels | ||
| let mut version_ranges = BTreeMap::new(); | ||
| let mut purl_statuses = BTreeMap::new(); | ||
|
|
||
| for entry in self.entries { | ||
| // Validate status exists | ||
| let status_id = *status_map | ||
| .get(&entry.status) | ||
| .ok_or_else(|| Error::InvalidStatus(entry.status.clone()))?; | ||
|
|
||
| // Create PurlStatus and use its uuid() method | ||
| let purl_status = PurlStatus { | ||
| cpe: entry.context_cpe.clone(), | ||
| purl: entry.purl.clone(), | ||
| status: status_id, | ||
| info: entry.version_info.clone(), | ||
| }; | ||
|
|
||
| let uuid = purl_status.uuid(entry.advisory_id, entry.vulnerability_id.clone()); | ||
| let base_purl_id = entry.purl.package_uuid(); | ||
| let version_range_id = entry.version_info.uuid(); | ||
| let context_cpe_id = entry.context_cpe.as_ref().map(|cpe| cpe.uuid()); | ||
|
|
||
| // Deduplicate version ranges | ||
| version_ranges | ||
| .entry(version_range_id) | ||
| .or_insert_with(|| entry.version_info.clone().into_active_model()); | ||
|
|
||
| // Deduplicate purl_statuses by UUID | ||
| purl_statuses | ||
| .entry(uuid) | ||
| .or_insert_with(|| purl_status::ActiveModel { | ||
| id: Set(uuid), | ||
| advisory_id: Set(entry.advisory_id), | ||
| vulnerability_id: Set(entry.vulnerability_id.clone()), | ||
| status_id: Set(status_id), | ||
| base_purl_id: Set(base_purl_id), | ||
| version_range_id: Set(version_range_id), | ||
| context_cpe_id: Set(context_cpe_id), | ||
| }); | ||
| } | ||
|
|
||
| // 3. Batch insert version ranges | ||
| for batch in &version_ranges.into_values().chunked() { | ||
| version_range::Entity::insert_many(batch) | ||
| .on_conflict(OnConflict::new().do_nothing().to_owned()) | ||
| .do_nothing() | ||
| .exec_without_returning(connection) | ||
| .await?; | ||
| } | ||
|
|
||
| // 4. Batch insert purl_statuses | ||
| for batch in &purl_statuses.into_values().chunked() { | ||
| purl_status::Entity::insert_many(batch) | ||
| .on_conflict(OnConflict::new().do_nothing().to_owned()) | ||
| .do_nothing() | ||
| .exec_without_returning(connection) | ||
| .await?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Error variant Error::InvalidStatus may benefit from more context.
Consider adding relevant fields (e.g., advisory_id or purl) to Error::InvalidStatus to make debugging easier.
Suggested implementation:
You will also need to:
Error::InvalidStatusin your error enum (likely in a file likemodules/ingestor/src/error.rsor similar) to be a struct variant with fields:status: String,advisory_id: AdvisoryIdType,purl: PurlType(replace types as appropriate).Display,Debug, or error handling implementations to format and use these new fields.Error::InvalidStatusto use the new struct variant.