-
Notifications
You must be signed in to change notification settings - Fork 101
libvuln: offline import performance improvements #1986
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
Merged
+339
−38
Merged
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
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,48 @@ | ||
| package jsonblob | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| ) | ||
|
|
||
| var ( | ||
| vulnerability sync.Pool | ||
| enrichment sync.Pool | ||
| ) | ||
|
|
||
| func getVulnerability() *claircore.Vulnerability { | ||
| if v := vulnerability.Get(); v != nil { | ||
| return v.(*claircore.Vulnerability) | ||
| } | ||
| return new(claircore.Vulnerability) | ||
| } | ||
|
|
||
| // ReturnVulnerability can be used by callers to return | ||
| // [claircore.Vulnerability] objects from [Loader.All] iterators to a common | ||
| // pool. | ||
| // | ||
| // This may take some pressure off the garbage collector. | ||
| func ReturnVulnerability(v *claircore.Vulnerability) { | ||
| // Reset the fields. | ||
| *v = claircore.Vulnerability{} | ||
| vulnerability.Put(v) | ||
| } | ||
|
|
||
| func getEnrichment() *driver.EnrichmentRecord { | ||
| if v := enrichment.Get(); v != nil { | ||
| return v.(*driver.EnrichmentRecord) | ||
| } | ||
| return new(driver.EnrichmentRecord) | ||
| } | ||
|
|
||
| // ReturnEnrichment can be used by callers to return [driver.EnrichmentRecord] | ||
| // objects from [Loader.All] iterators to a common pool. | ||
| // | ||
| // This may take some pressure off the garbage collector. | ||
| func ReturnEnrichment(e *driver.EnrichmentRecord) { | ||
| // Reset the fields. | ||
| *e = driver.EnrichmentRecord{} | ||
| enrichment.Put(e) | ||
| } |
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,120 @@ | ||
| package libvuln | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/jackc/pgx/v5/pgxpool" | ||
| "github.com/klauspost/compress/zstd" | ||
|
|
||
| "github.com/quay/claircore/datastore/postgres" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| "github.com/quay/claircore/libvuln/jsonblob" | ||
| "github.com/quay/claircore/test" | ||
| "github.com/quay/claircore/test/integration" | ||
| pgtest "github.com/quay/claircore/test/postgres" | ||
| ) | ||
|
|
||
| var importFile *string | ||
|
|
||
| func init() { | ||
| flag.Func(`load-file`, "run the integration test reading from `FILE` (must be zstd compressed)", func(v string) error { | ||
| importFile = &v | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| var c int | ||
| defer func() { os.Exit(c) }() | ||
| defer integration.DBSetup()() | ||
| c = m.Run() | ||
| } | ||
|
|
||
| // TestLiveOfflineImport is meant to be used for profiling and testing the | ||
| // system on a "real" export as produced by `clairctl`. | ||
| func TestOfflineImport(t *testing.T) { | ||
| if importFile == nil { | ||
| t.Skip(`needed flag "-load-file" not provided`) | ||
| } | ||
| integration.NeedDB(t) | ||
|
|
||
| t.Run("Old", testOneOfflineImport(oldOfflineImport)) | ||
| t.Run("New", testOneOfflineImport(OfflineImport)) | ||
| } | ||
|
|
||
| func testOneOfflineImport(inner func(context.Context, *pgxpool.Pool, io.Reader) error) func(*testing.T) { | ||
| return func(t *testing.T) { | ||
| ctx := test.Logging(t) | ||
|
|
||
| f, err := os.Open(*importFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer f.Close() | ||
| zr, err := zstd.NewReader(f) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer zr.Close() | ||
|
|
||
| pool := pgtest.TestMatcherDB(ctx, t) | ||
| if err := inner(ctx, pool, zr); err != nil { | ||
| t.Error(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // OldOfflineImport is a copy of the previous implementation of [OfflineImport], | ||
| // kept here for the above test. | ||
| func oldOfflineImport(ctx context.Context, pool *pgxpool.Pool, in io.Reader) error { | ||
| s := postgres.NewMatcherStore(pool) | ||
| l, err := jsonblob.NewLoader(ctx, in) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ops, err := s.GetUpdateOperations(ctx, driver.VulnerabilityKind) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| Update: | ||
| for l.Next() { | ||
| e := l.Entry() | ||
| log := slog.With("updater", e.Updater) | ||
| for _, op := range ops[e.Updater] { | ||
| // This only helps if updaters don't keep something that | ||
| // changes in the fingerprint. | ||
| if op.Fingerprint == e.Fingerprint { | ||
| log.InfoContext(ctx, "fingerprint match, skipping") | ||
| continue Update | ||
| } | ||
| } | ||
| var ref uuid.UUID | ||
| if e.Enrichment != nil { | ||
| if ref, err = s.UpdateEnrichments(ctx, e.Updater, e.Fingerprint, e.Enrichment); err != nil { | ||
| return fmt.Errorf("updating enrichements: %w", err) | ||
| } | ||
| } | ||
| if e.Vuln != nil { | ||
| if ref, err = s.UpdateVulnerabilities(ctx, e.Updater, e.Fingerprint, e.Vuln); err != nil { | ||
| return fmt.Errorf("updating vulnerabilities: %w", err) | ||
| } | ||
| } | ||
| log.InfoContext(ctx, "update imported", | ||
| "ref", ref, | ||
| "vuln_count", len(e.Vuln), | ||
| "enrichment_count", len(e.Enrichment)) | ||
| } | ||
| if err := l.Err(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.