Skip to content

Commit

Permalink
fix: reduce number of queries to get releases
Browse files Browse the repository at this point in the history
This reduces the number of queries to the releases table,
roughly by a factor of 25 (average number of releases per app).

Ref: SRX-LEIHOZ
  • Loading branch information
sven-urbanski-freiheit-com committed Oct 10, 2024
1 parent 3c47e0e commit cb5cb59
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
43 changes: 43 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,48 @@ func (h *DBHandler) DBSelectReleasesWithoutEnvironments(ctx context.Context, tx
return processedRows, nil
}

func (h *DBHandler) DBSelectReleasesByVersions(ctx context.Context, tx *sql.Tx, app string, releaseVersions []uint64, ignorePrePublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByVersions")
defer span.Finish()

if len(releaseVersions) == 0 {
return []*DBReleaseWithMetaData{}, nil
}
repeatedQuestionMarks := strings.Repeat(",?", len(releaseVersions)-1)
selectQuery := h.AdaptQuery(fmt.Sprintf(
`
SELECT eslVersion, created, appName, metadata, manifests, releaseVersion, deleted, environments
FROM releases
WHERE appName=? AND releaseVersion IN (?` + repeatedQuestionMarks + `)
ORDER BY eslVersion DESC
LIMIT ?;
`))

span.SetTag("query", selectQuery)

args := []any{}
args = append(args, app)
for _, version := range releaseVersions {
args = append(args, version)
}
args = append(args, uint64(len(releaseVersions)))

rows, err := tx.QueryContext(
ctx,
selectQuery,
args...,
)

processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrePublishes)
if err != nil {
return nil, err
}
if len(processedRows) == 0 {
return nil, nil
}
return processedRows, nil
}

func (h *DBHandler) DBSelectReleaseByVersion(ctx context.Context, tx *sql.Tx, app string, releaseVersion uint64, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) {
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, appName, metadata, manifests, releaseVersion, deleted, environments " +
Expand Down Expand Up @@ -887,6 +929,7 @@ func (h *DBHandler) processAllReleasesRow(ctx context.Context, err error, rows *
}
return row, nil
}

func (h *DBHandler) processReleaseRows(ctx context.Context, err error, rows *sql.Rows, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "processReleaseRows")
defer span.Finish()
Expand Down
41 changes: 35 additions & 6 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2512,16 +2512,15 @@ func (s *State) UpdateTopLevelAppInOverview(ctx context.Context, transaction *sq
}
rels = retrievedReleasesOfApp
}
for _, id := range rels {
if rel, err := s.GetApplicationRelease(ctx, transaction, appName, id); err != nil {
return err
} else {

if releasesInDb, err := s.GetApplicationReleasesDB(ctx, transaction, appName, rels); err != nil {
return err
} else {
for _, rel := range releasesInDb {
if rel == nil {
// ignore
} else {
release := rel.ToProto()
release.Version = id
release.UndeployVersion = rel.UndeployVersion
app.Releases = append(app.Releases, release)
}
}
Expand Down Expand Up @@ -3119,6 +3118,36 @@ func (s *State) IsUndeployVersionFromManifest(application string, version uint64
return true, nil
}

func (s *State) GetApplicationReleasesDB(ctx context.Context, transaction *sql.Tx, application string, versions []uint64) ([]*Release, error) {
if s.DBHandler.ShouldUseOtherTables() {
rels, err := s.DBHandler.DBSelectReleasesByVersions(ctx, transaction, application, versions, true)
if err != nil {
return nil, fmt.Errorf("could not get release of app %s: %v", application, err)
}
if rels == nil {
return nil, nil
}
var result []*Release
for _, rel := range rels {
r := &Release{
Version: rel.ReleaseNumber,
UndeployVersion: rel.Metadata.UndeployVersion,
SourceAuthor: rel.Metadata.SourceAuthor,
SourceCommitId: rel.Metadata.SourceCommitId,
SourceMessage: rel.Metadata.SourceMessage,
CreatedAt: rel.Created,
DisplayVersion: rel.Metadata.DisplayVersion,
IsMinor: rel.Metadata.IsMinor,
IsPrepublish: rel.Metadata.IsPrepublish,
}
result = append(result, r)
}
return result, nil
} else {
return nil, fmt.Errorf("unsupported operation, need to enable the DB")
}
}

func (s *State) GetApplicationRelease(ctx context.Context, transaction *sql.Tx, application string, version uint64) (*Release, error) {
if s.DBHandler.ShouldUseOtherTables() {
env, err := s.DBHandler.DBSelectReleaseByVersion(ctx, transaction, application, version, true)
Expand Down

0 comments on commit cb5cb59

Please sign in to comment.