Skip to content

Commit

Permalink
fix(cd-service): optimize some queries in UpdateOverview in ReleaseTr…
Browse files Browse the repository at this point in the history
…ain (#1990)

Ref: SRX-YNEAHU
  • Loading branch information
AminSlk authored Sep 30, 2024
1 parent f95be84 commit ef4af10
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
55 changes: 34 additions & 21 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/valid"
"io"
"net/http"
"os"
Expand All @@ -35,6 +34,8 @@ import (
"sync"
"time"

"github.com/freiheit-com/kuberpult/pkg/valid"

"github.com/freiheit-com/kuberpult/pkg/event"

"github.com/freiheit-com/kuberpult/pkg/conversion"
Expand Down Expand Up @@ -2418,7 +2419,7 @@ func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction
}

shouldDelete := stateChange == db.AppStateChangeDelete
err = s.UpdateTopLevelAppInOverview(ctx, transaction, appName, cache, shouldDelete)
err = s.UpdateTopLevelAppInOverview(ctx, transaction, appName, cache, shouldDelete, map[string][]int64{})
if err != nil {
return err
}
Expand All @@ -2432,7 +2433,7 @@ func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction
if shouldDelete {
delete(env.Applications, appName)
} else {
envApp, err := s.UpdateOneAppEnvInOverview(ctx, transaction, appName, env.Name, nil)
envApp, err := s.UpdateOneAppEnvInOverview(ctx, transaction, appName, env.Name, nil, map[string]*int64{})
if err != nil {
return err
}
Expand All @@ -2453,7 +2454,7 @@ func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction
return nil
}

func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sql.Tx, appName string, result *api.GetOverviewResponse, deleteApp bool) error {
func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sql.Tx, appName string, result *api.GetOverviewResponse, deleteApp bool, allReleasesOfAllApps map[string][]int64) error {
if deleteApp {
delete(result.Applications, appName)
return nil
Expand All @@ -2466,25 +2467,32 @@ func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sq
SourceRepoUrl: "",
Team: "",
}
if rels, err := s.GetAllApplicationReleases(ctx, transaction, appName); err != nil {
logger.FromContext(ctx).Sugar().Warnf("app without releases: %v", err)
// continue, apps are not required to have releases
allReleasesOfApp, found := allReleasesOfAllApps[appName]
var rels []uint64
if found {
rels = conversion.ToUint64Slice(allReleasesOfApp)
} else {
for _, id := range rels {
if rel, err := s.GetApplicationRelease(ctx, transaction, appName, id); err != nil {
return err
retrievedReleasesOfApp, err := s.GetAllApplicationReleases(ctx, transaction, appName)
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("app without releases: %v", err)
}
rels = retrievedReleasesOfApp
}
for _, id := range rels {
if rel, err := s.GetApplicationRelease(ctx, transaction, appName, id); err != nil {
return err
} else {
if rel == nil {
// ignore
} else {
if rel == nil {
// ignore
} else {
release := rel.ToProto()
release.Version = id
release.UndeployVersion = rel.UndeployVersion
app.Releases = append(app.Releases, release)
}
release := rel.ToProto()
release.Version = id
release.UndeployVersion = rel.UndeployVersion
app.Releases = append(app.Releases, release)
}
}
}

if team, err := s.GetApplicationTeamOwner(ctx, transaction, appName); err != nil {
return err
} else {
Expand Down Expand Up @@ -2605,7 +2613,7 @@ func deriveUndeploySummary(appName string, groups []*api.EnvironmentGroup) api.U

}

func (s *State) UpdateOneAppEnvInOverview(ctx context.Context, transaction *sql.Tx, appName string, envName string, configParam *config.EnvironmentConfig) (*api.Environment_Application, error) {
func (s *State) UpdateOneAppEnvInOverview(ctx context.Context, transaction *sql.Tx, appName string, envName string, configParam *config.EnvironmentConfig, allEnvironmentApplicationVersions map[string]*int64) (*api.Environment_Application, error) {
var envConfig = configParam
if envConfig == nil {
var err error
Expand Down Expand Up @@ -2649,8 +2657,13 @@ func (s *State) UpdateOneAppEnvInOverview(ctx context.Context, transaction *sql.
}
} // Err != nil means no team name was found so no need to parse team locks

var version *uint64
version, err = s.GetEnvironmentApplicationVersion(ctx, transaction, envName, appName)
var version *uint64 = new(uint64)
allAppsVersion, found := allEnvironmentApplicationVersions[appName]
if found {
*version = uint64(*allAppsVersion)
} else {
version, err = s.GetEnvironmentApplicationVersion(ctx, transaction, envName, appName)
}
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
} else {
Expand Down
15 changes: 12 additions & 3 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4034,13 +4034,22 @@ func (c *envReleaseTrain) Transform(
if err := t.Execute(ctx, d, transaction); err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while deploying app %q to env %q: %w", appName, c.Env, err))
}

}
allEnvironmentApplicationVersions, err := state.GetAllLatestDeployments(ctx, transaction, c.Env, appNames)
if err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while retrieving all environment application versions: %w", err))
}
allReleasesOfAllApps, err := state.GetAllLatestReleases(ctx, transaction, appNames)
if err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while retrieving all releases of all apps: %w", err))
}
for _, appName := range appNames {
if envOfOverview != nil {
err := state.UpdateTopLevelAppInOverview(ctx, transaction, appName, overview, false)
err := state.UpdateTopLevelAppInOverview(ctx, transaction, appName, overview, false, allReleasesOfAllApps)
if err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while updating top level app %q to env %q: %w", appName, c.Env, err))
}
envApp, err := state.UpdateOneAppEnvInOverview(ctx, transaction, appName, c.Env, &envConfig)
envApp, err := state.UpdateOneAppEnvInOverview(ctx, transaction, appName, c.Env, &envConfig, allEnvironmentApplicationVersions)
if err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while updating top level app %q to env %q: %w", appName, c.Env, err))
}
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/service/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (o *OverviewServiceServer) getOverview(
return nil, err
} else {
for _, appName := range apps {
app, err2 := s.UpdateOneAppEnvInOverview(ctx, transaction, appName, envName, &config)
app, err2 := s.UpdateOneAppEnvInOverview(ctx, transaction, appName, envName, &config, map[string]*int64{})
if err2 != nil {
return nil, err2
}
Expand All @@ -189,7 +189,7 @@ func (o *OverviewServiceServer) getOverview(
return nil, err
} else {
for _, appName := range apps {
err2 := s.UpdateTopLevelAppInOverview(ctx, transaction, appName, &result, false)
err2 := s.UpdateTopLevelAppInOverview(ctx, transaction, appName, &result, false, map[string][]int64{})
if err2 != nil {
return nil, err2
}
Expand Down

0 comments on commit ef4af10

Please sign in to comment.