Skip to content

Commit

Permalink
fix(db): retrieve manifests before expensive step on prognosis (#1992)
Browse files Browse the repository at this point in the history
Ref: SRX-YNEAHU

---------

Co-authored-by: Sven Urbanski <[email protected]>
  • Loading branch information
1 parent ef4af10 commit 2569696
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
78 changes: 78 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,84 @@ func (h *DBHandler) DBSelectReleaseByVersion(ctx context.Context, tx *sql.Tx, ap
return processedRows[0], nil
}

func (h *DBHandler) DBSelectAllManifestsForAllReleases(ctx context.Context, tx *sql.Tx) (map[string]map[uint64]DBReleaseManifests, error) {
selectQuery := h.AdaptQuery(
`
SELECT DISTINCT
releases.appname,
releases.releaseVersion,
releases.manifests
FROM (
SELECT
MAX(releaseVersion) AS latestRelease,
appname,
releaseversion
FROM
"releases"
GROUP BY
appname, releaseversion) AS latest
JOIN
releases AS releases
ON
latest.latestRelease=releases.releaseVersion
AND latest.appname=releases.appname;
`)
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllManifestsForAllReleases")
defer span.Finish()
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
ctx,
selectQuery,
)

return h.processReleaseManifestRows(ctx, err, rows)
}

func (h *DBHandler) processReleaseManifestRows(ctx context.Context, err error, rows *sql.Rows) (map[string]map[uint64]DBReleaseManifests, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "processReleaseManifestRows")
defer span.Finish()

if err != nil {
return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err)
}
}(rows)
//exhaustruct:ignore
var result = make(map[string]map[uint64]DBReleaseManifests)
for rows.Next() {
var manifestStr string
var appName string
var releaseVersion uint64
err := rows.Scan(&appName, &releaseVersion, &manifestStr)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err)
}
var manifestData = DBReleaseManifests{
Manifests: map[string]string{},
}
err = json.Unmarshal(([]byte)(manifestStr), &manifestData)
if err != nil {
return nil, fmt.Errorf("Error during json unmarshal of manifests for releases. Error: %w. Data: %s\n", err, manifestStr)
}
if _, exists := result[appName]; !exists {
result[appName] = make(map[uint64]DBReleaseManifests)
}
result[appName][releaseVersion] = manifestData
}
err = closeRows(rows)
if err != nil {
return nil, err
}
return result, nil
}

func (h *DBHandler) DBSelectReleasesByApp(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
defer span.Finish()
Expand Down
19 changes: 16 additions & 3 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3684,6 +3684,18 @@ func (c *envReleaseTrain) prognosis(
AppsPrognoses: nil,
}
}
var allLatestManifests map[string]map[uint64]db.DBReleaseManifests
if state.DBHandler.ShouldUseOtherTables() {
allLatestManifests, err = state.DBHandler.DBSelectAllManifestsForAllReleases(ctx, transaction)
}
if err != nil {
return ReleaseTrainEnvironmentPrognosis{
SkipCause: nil,
Error: grpc.PublicError(ctx, fmt.Errorf("Error getting all releases of all apps: %w", err)),
Locks: nil,
AppsPrognoses: nil,
}
}
for _, appName := range apps {
if c.Parent.Team != "" {
if team, err := state.GetApplicationTeamOwner(ctx, transaction, appName); err != nil {
Expand Down Expand Up @@ -3773,7 +3785,8 @@ func (c *envReleaseTrain) prognosis(
}

if state.DBHandler.ShouldUseOtherTables() {
release, err := state.DBHandler.DBSelectReleaseByVersion(ctx, transaction, appName, versionToDeploy, true)
release, exists := allLatestManifests[appName][versionToDeploy]

if err != nil {
return ReleaseTrainEnvironmentPrognosis{
SkipCause: nil,
Expand All @@ -3782,7 +3795,7 @@ func (c *envReleaseTrain) prognosis(
AppsPrognoses: nil,
}
}
if release == nil {
if !exists {
return ReleaseTrainEnvironmentPrognosis{
SkipCause: nil,
Error: fmt.Errorf("No release found for app %s and versionToDeploy %d", appName, versionToDeploy),
Expand All @@ -3791,7 +3804,7 @@ func (c *envReleaseTrain) prognosis(
}
}

_, ok := release.Manifests.Manifests[c.Env]
_, ok := release.Manifests[c.Env]

if !ok {
appsPrognoses[appName] = ReleaseTrainApplicationPrognosis{
Expand Down

0 comments on commit 2569696

Please sign in to comment.