Repository navigation
feat(context): delete old scanner runs #1335
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a36df75
Testing
michalkrzyz da4cc99
use int64 in autopatch for all ids
michalkrzyz e5a69bd
Ensure at least two completed runs will be left if exist
michalkrzyz d9072aa
Improvements
michalkrzyz 42466cd
Limit retention number to 2000 to not exceed sql query param size
michalkrzyz 7da8e8f
Merge branch 'main' into mikrzyz/issue-1328
MR2011 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
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,191 @@ | ||
| // SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package mariadb | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| sq "github.com/Masterminds/squirrel" | ||
| "github.com/cloudoperators/heureka/internal/util" | ||
| "github.com/samber/lo" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| func (s *SqlDatabase) Autoclean(ctx context.Context, keepNRuns int) error { | ||
| if keepNRuns == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| if err := s.acquireScannerRunAccess(defaultTimeoutForScannerRunLock); err != nil { | ||
| return err | ||
| } | ||
| defer s.releaseScannerRunAccess() | ||
|
|
||
| runsWithStatus, err := s.fetchRunsWithNewestFirst(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return s.processAutocleanScannerRuns(ctx, runsWithStatus, int(keepNRuns)) | ||
| } | ||
|
|
||
| type scannerRunWithStatus struct { | ||
| id int64 | ||
| completed bool | ||
| } | ||
|
|
||
| func (s *SqlDatabase) fetchRunsWithNewestFirst(ctx context.Context) (map[string][]scannerRunWithStatus, error) { | ||
| query, args, err := sq.Select("scannerrun_tag", "scannerrun_run_id", "scannerrun_is_completed"). | ||
| From("ScannerRun"). | ||
| Where("scannerrun_deleted_at IS NULL"). | ||
| OrderBy("scannerrun_tag, scannerrun_run_id DESC"). | ||
| ToSql() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| rows, err := s.db.QueryContext(ctx, query, args...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer func() { | ||
| if err := rows.Close(); err != nil { | ||
| logrus.Warnf("error during close rows: %s", err) | ||
| } | ||
| }() | ||
|
|
||
| // tag -> list of runs (newest first) | ||
| runs := map[string][]scannerRunWithStatus{} | ||
|
|
||
| for rows.Next() { | ||
| var ( | ||
| tag string | ||
| id int64 | ||
| completed bool | ||
| ) | ||
|
|
||
| if err := rows.Scan(&tag, &id, &completed); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| runs[tag] = append(runs[tag], scannerRunWithStatus{id: id, completed: completed}) | ||
| } | ||
|
|
||
| if rows.Err() != nil { | ||
| return nil, rows.Err() | ||
| } | ||
|
|
||
| return runs, nil | ||
| } | ||
|
|
||
| func (s *SqlDatabase) processAutocleanScannerRuns(ctx context.Context, runs map[string][]scannerRunWithStatus, keepNRuns int) error { | ||
| runsToKeep := getOnlyRunsToKeep(runs, keepNRuns) | ||
| runIdsToKeep := getRunIds(runsToKeep) | ||
|
michalkrzyz marked this conversation as resolved.
Resolved
|
||
|
|
||
| if err := s.deleteErrorsFromNotListedRuns(ctx, runIdsToKeep); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := s.deleteTrackersFromNotListedRuns(ctx, runIdsToKeep); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := s.deleteNotListedRuns(ctx, runIdsToKeep); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func getOnlyRunsToKeep(runsWithStatus map[string][]scannerRunWithStatus, keepNRuns int) map[string][]int64 { | ||
| return lo.MapValues(runsWithStatus, func(v []scannerRunWithStatus, tag string) []int64 { | ||
| completedCnt := 0 | ||
|
|
||
| return lo.FilterMap(v, func(v0 scannerRunWithStatus, n int) (int64, bool) { | ||
| if v0.completed { | ||
| if n < keepNRuns || completedCnt < 2 { | ||
| completedCnt += 1 | ||
| return v0.id, true | ||
| } | ||
| } else { | ||
| if n < keepNRuns { | ||
| return v0.id, true | ||
| } | ||
|
michalkrzyz marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| return 0, false | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func getRunIds(runs map[string][]int64) []int64 { | ||
| result := []int64{} | ||
|
|
||
| for _, r := range runs { | ||
| result = append(result, r...) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func (s *SqlDatabase) deleteErrorsFromNotListedRuns(ctx context.Context, runIds []int64) error { | ||
| query, args, err := sq.Delete("ScannerRunError"). | ||
| Where(sq.NotEq{"scannerrunerror_scannerrun_run_id": runIds}). | ||
| ToSql() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| result, err := s.db.ExecContext(ctx, query, args...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rowsAffected, _ := result.RowsAffected() | ||
| logrus.Infof("Deleted %d Scanner Run Errors", rowsAffected) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *SqlDatabase) deleteTrackersFromNotListedRuns(ctx context.Context, runIds []int64) error { | ||
| query, args, err := sq.Delete("ScannerRunComponentInstanceTracker"). | ||
| Where(sq.NotEq{"scannerruncomponentinstancetracker_scannerrun_run_id": runIds}). | ||
| ToSql() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| result, err := s.db.ExecContext(ctx, query, args...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rowsAffected, _ := result.RowsAffected() | ||
| logrus.Infof("Deleted %d Scanner Run Trackers", rowsAffected) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *SqlDatabase) deleteNotListedRuns(ctx context.Context, runIds []int64) error { | ||
| qb := sq. | ||
| Update("ScannerRun"). | ||
| Set("scannerrun_deleted_at", sq.Expr("NOW()")). | ||
|
michalkrzyz marked this conversation as resolved.
Resolved
|
||
| Set("scannerrun_updated_by", util.SystemUserId). | ||
| Where(sq.NotEq{"scannerrun_run_id": runIds}) | ||
|
|
||
| query, args, err := qb.ToSql() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| result, err := s.db.ExecContext(ctx, query, args...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rowsAffected, _ := result.RowsAffected() | ||
| logrus.Infof("Soft-Deleted %d Scanner Runs", rowsAffected) | ||
|
|
||
| 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.