Skip to content

Commit

Permalink
resolving merge conflicts and fix dal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanj-square committed Oct 2, 2024
1 parent 5aea06b commit 0a0f018
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 100 deletions.
14 changes: 12 additions & 2 deletions backend/controller/artefacts/artefact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package artefacts

import (
"context"
"github.com/TBD54566975/ftl/internal/model"
"io"

"github.com/TBD54566975/ftl/internal/sha256"
Expand All @@ -22,8 +23,13 @@ type Artefact struct {

type ArtefactKey struct {
Digest sha256.SHA256
// Temporary
ID int64
id int64
}

type ReleaseArtefact struct {
Artefact ArtefactKey
Path string
Executable bool
}

type Registry interface {
Expand All @@ -33,4 +39,8 @@ type Registry interface {
Upload(context context.Context, artefact Artefact) (sha256.SHA256, error)
// Download performs a streaming download of the artefact identified by the supplied digest
Download(context context.Context, digest sha256.SHA256) (io.ReadCloser, error)
// GetReleaseArtefacts locates the artefacts metadata corresponding with the specified release
GetReleaseArtefacts(ctx context.Context, releaseID int64) ([]ArtefactKey, error)
// AddReleaseArtefact associates the given `release` with the artefact associated with the given `digest`
AddReleaseArtefact(ctx context.Context, key model.DeploymentKey, ra ReleaseArtefact) error
}
30 changes: 29 additions & 1 deletion backend/controller/artefacts/dal_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/artefacts/internal/sql"
"github.com/TBD54566975/ftl/internal/model"
"io"

sets "github.com/deckarep/golang-set/v2"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (s *Service) GetDigestsKeys(ctx context.Context, digests []sha256.SHA256) (
return nil, nil, libdal.TranslatePGError(err)
}
keys = slices.Map(have, func(in sql.GetArtefactDigestsRow) ArtefactKey {
return ArtefactKey{ID: in.ID, Digest: sha256.FromBytes(in.Digest)}
return ArtefactKey{id: in.ID, Digest: sha256.FromBytes(in.Digest)}
})
haveStr := slices.Map(keys, func(in ArtefactKey) sha256.SHA256 {
return in.Digest
Expand All @@ -74,6 +75,33 @@ func (s *Service) Download(ctx context.Context, digest sha256.SHA256) (io.ReadCl
return &dalArtefactStream{db: s.db, id: rows[0].ID}, nil
}

func (s *Service) GetReleaseArtefacts(ctx context.Context, releaseID int64) ([]ReleaseArtefact, error) {
rows, err := s.db.GetDeploymentArtefacts(ctx, releaseID)
if err != nil {
return nil, fmt.Errorf("unable to get release artefacts: %w", libdal.TranslatePGError(err))
}
return slices.Map(rows, func(row sql.GetDeploymentArtefactsRow) ReleaseArtefact {
return ReleaseArtefact{
Artefact: ArtefactKey{Digest: sha256.FromBytes(row.Digest), id: row.ID},
Path: row.Path,
Executable: row.Executable,
}
}), nil
}

func (s *Service) AddReleaseArtefact(ctx context.Context, key model.DeploymentKey, ra ReleaseArtefact) error {
err := s.db.AssociateArtefactWithDeployment(ctx, sql.AssociateArtefactWithDeploymentParams{
Key: key,
Digest: ra.Artefact.Digest[:],
Executable: ra.Executable,
Path: ra.Path,
})
if err != nil {
return libdal.TranslatePGError(err)
}
return nil
}

type dalArtefactStream struct {
db sql.Querier
id int64
Expand Down
3 changes: 3 additions & 0 deletions backend/controller/artefacts/internal/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions backend/controller/artefacts/internal/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ DO UPDATE SET digest = EXCLUDED.digest
SELECT SUBSTRING(a.content FROM @start FOR @count)::BYTEA AS content
FROM artefacts a
WHERE a.id = @id;

-- name: GetDeploymentArtefacts :many
-- Get all artefacts matching the given digests.
SELECT da.created_at, artefact_id AS id, executable, path, digest, executable
FROM deployment_artefacts da
INNER JOIN artefacts ON artefacts.id = da.artefact_id
WHERE deployment_id = $1;

-- name: AssociateArtefactWithDeployment :exec
INSERT INTO deployment_artefacts (deployment_id, artefact_id, executable, path)
VALUES ((SELECT id FROM deployments WHERE key = @key::deployment_key), (SELECT id FROM artefacts WHERE digest = @digest::bytea), $3, $4);
71 changes: 71 additions & 0 deletions backend/controller/artefacts/internal/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 12 additions & 14 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/artefacts"
aregistry "github.com/TBD54566975/ftl/backend/controller/artefacts"
"time"

"github.com/alecthomas/types/optional"
Expand Down Expand Up @@ -58,15 +58,15 @@ func New(ctx context.Context, conn libdal.Connection, encryption *encryption.Ser
leaser: dbleaser.NewDatabaseLeaser(conn),
db: db,
encryption: encryption,
registry: artefacts.New(ctx, conn),
registry: aregistry.New(ctx, conn),
Handle: libdal.New(conn, func(h *libdal.Handle[DAL]) *DAL {
return &DAL{
Handle: h,
db: dalsql.New(h.Connection),
leaser: dbleaser.NewDatabaseLeaser(h.Connection),
pubsub: pubsub,
encryption: d.encryption,
registry: artefacts.New(ctx, h.Connection),
registry: aregistry.New(ctx, h.Connection),
DeploymentChanges: d.DeploymentChanges,
}
}),
Expand All @@ -83,7 +83,7 @@ type DAL struct {
leaser *dbleaser.DatabaseLeaser
pubsub *pubsub.Service
encryption *encryption.Service
registry *artefacts.Service
registry *aregistry.Service

// DeploymentChanges is a Topic that receives changes to the deployments table.
DeploymentChanges *inprocesspubsub.Topic[DeploymentNotification]
Expand Down Expand Up @@ -279,9 +279,8 @@ func (d *DAL) CreateDeployment(ctx context.Context, language string, moduleSchem
// Associate the artefacts with the deployment
for _, row := range keys {
artefact := artefactsByDigest[row.Digest]
err = tx.db.AssociateArtefactWithDeployment(ctx, dalsql.AssociateArtefactWithDeploymentParams{
Key: deploymentKey,
ArtefactID: row.ID,
err = tx.registry.AddReleaseArtefact(ctx, deploymentKey, aregistry.ReleaseArtefact{
Artefact: aregistry.ArtefactKey{Digest: row.Digest},
Executable: artefact.Executable,
Path: artefact.Path,
})
Expand Down Expand Up @@ -686,22 +685,21 @@ func (d *DAL) loadDeployment(ctx context.Context, deployment dalsql.GetDeploymen
Key: deployment.Deployment.Key,
Schema: deployment.Deployment.Schema,
}
darts, err := d.db.GetDeploymentArtefacts(ctx, deployment.Deployment.ID)
ras, err := d.registry.GetReleaseArtefacts(ctx, deployment.Deployment.ID)

if err != nil {
return nil, libdal.TranslatePGError(err)
}
out.Artefacts, err = slices.MapErr(darts, func(row dalsql.GetDeploymentArtefactsRow) (*model.Artefact, error) {
digest := sha256.FromBytes(row.Digest)
content, err := d.registry.Download(ctx, digest)
out.Artefacts, err = slices.MapErr(ras, func(ra aregistry.ReleaseArtefact) (*model.Artefact, error) {
content, err := d.registry.Download(ctx, ra.Artefact.Digest)
if err != nil {
return nil, fmt.Errorf("artefact download failed: %w", libdal.TranslatePGError(err))
}
return &model.Artefact{
Path: row.Path,
Executable: row.Executable,
Path: ra.Path,
Executable: ra.Executable,
Content: content,
Digest: digest,
Digest: ra.Artefact.Digest,
}, nil
})
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions backend/controller/dal/internal/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions backend/controller/dal/internal/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ WHERE id = ANY (@ids::BIGINT[]);
INSERT INTO deployments (module_id, "schema", "key")
VALUES ((SELECT id FROM modules WHERE name = @module_name::TEXT LIMIT 1), @schema::BYTEA, @key::deployment_key);

-- name: GetDeploymentArtefacts :many
-- Get all artefacts matching the given digests.
SELECT da.created_at, artefact_id AS id, executable, path, digest, executable
FROM deployment_artefacts da
INNER JOIN artefacts ON artefacts.id = da.artefact_id
WHERE deployment_id = $1;

-- name: AssociateArtefactWithDeployment :exec
INSERT INTO deployment_artefacts (deployment_id, artefact_id, executable, path)
VALUES ((SELECT id FROM deployments WHERE key = @key::deployment_key), $2, $3, $4);

-- name: GetDeployment :one
SELECT sqlc.embed(d), m.language, m.name AS module_name, d.min_replicas
FROM deployments d
Expand Down
69 changes: 0 additions & 69 deletions backend/controller/dal/internal/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a0f018

Please sign in to comment.