Skip to content
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

feat: update module runtime endpoint in the schema #3615

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,31 +494,27 @@ func (s *Service) PullSchema(ctx context.Context, req *connect.Request[ftlv1.Pul
})
}

func (s *Service) UpdateModuleRuntime(ctx context.Context, req *connect.Request[ftlv1.UpdateModuleRuntimeRequest]) (*connect.Response[ftlv1.UpdateModuleRuntimeResponse], error) {
schemas, err := s.dal.GetActiveDeploymentSchemasByDeploymentKey(ctx)
func (s *Service) UpdateDeploymentRuntime(ctx context.Context, req *connect.Request[ftlv1.UpdateDeploymentRuntimeRequest]) (*connect.Response[ftlv1.UpdateDeploymentRuntimeResponse], error) {
deployment, err := model.ParseDeploymentKey(req.Msg.Deployment)
if err != nil {
return nil, fmt.Errorf("could not get schemas: %w", err)
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid deployment key: %w", err))
}
for deployment, module := range schemas {
if module.Name != req.Msg.Module {
continue
}
key, err := model.ParseDeploymentKey(deployment)
if err != nil {
return nil, fmt.Errorf("invalid deployment key: %w", err)
}
if module.Runtime == nil {
module.Runtime = &schema.ModuleRuntime{}
}
event := schema.ModuleRuntimeEventFromProto(req.Msg.Event)
module.Runtime.ApplyEvent(event)
err = s.dal.UpdateModuleSchema(ctx, key, module)
if err != nil {
return nil, fmt.Errorf("could not update schema for module %s: %w", module.Name, err)
}
break
dep, err := s.dal.GetDeployment(ctx, deployment)
if err != nil {
return nil, fmt.Errorf("could not get schema: %w", err)
}
return connect.NewResponse(&ftlv1.UpdateModuleRuntimeResponse{}), nil
module := dep.Schema
if module.Runtime == nil {
module.Runtime = &schema.ModuleRuntime{}
}
event := schema.ModuleRuntimeEventFromProto(req.Msg.Event)
module.Runtime.ApplyEvent(event)
err = s.dal.UpdateModuleSchema(ctx, deployment, module)
if err != nil {
return nil, fmt.Errorf("could not update schema for module %s: %w", module.Name, err)
}

return connect.NewResponse(&ftlv1.UpdateDeploymentRuntimeResponse{}), nil
}

func (s *Service) UpdateDeploy(ctx context.Context, req *connect.Request[ftlv1.UpdateDeployRequest]) (response *connect.Response[ftlv1.UpdateDeployResponse], err error) {
Expand All @@ -540,18 +536,6 @@ func (s *Service) UpdateDeploy(ctx context.Context, req *connect.Request[ftlv1.U
return nil, fmt.Errorf("could not set deployment replicas: %w", err)
}
}
if req.Msg.Endpoint != nil {
err = s.dal.SetDeploymentEndpoint(ctx, deploymentKey, *req.Msg.Endpoint)
if err != nil {
if errors.Is(err, libdal.ErrNotFound) {
logger.Errorf(err, "Deployment not found: %s", deploymentKey)
return nil, connect.NewError(connect.CodeNotFound, errors.New("deployment not found"))
}
logger.Errorf(err, "Could not set deployment endpoint: %s", deploymentKey)
return nil, fmt.Errorf("could not set deployment endpoint: %w", err)
}
}

return connect.NewResponse(&ftlv1.UpdateDeployResponse{}), nil
}

Expand Down Expand Up @@ -1681,12 +1665,12 @@ func (s *Service) syncRoutesAndSchema(ctx context.Context) (ret time.Duration, e
// And we set its replicas to zero
// It may seem a bit odd to do this here but this is where we are actually updating the routing table
// Which is what makes as a deployment 'live' from a clients POV
if v.Schema.Runtime == nil {
if v.Schema.Runtime == nil || v.Schema.Runtime.Deployment == nil {
deploymentLogger.Debugf("Deployment %s has no runtime metadata", v.Key.String())
continue
}
targetEndpoint, ok := v.Endpoint.Get()
if !ok {
targetEndpoint := v.Schema.Runtime.Deployment.Endpoint
if targetEndpoint == "" {
deploymentLogger.Debugf("Failed to get updated endpoint for deployment %s", v.Key.String())
continue
}
Expand Down
17 changes: 0 additions & 17 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,6 @@ func (d *DAL) SetDeploymentReplicas(ctx context.Context, key model.DeploymentKey
return nil
}

// SetDeploymentEndpoint sets the deployment endpoint
func (d *DAL) SetDeploymentEndpoint(ctx context.Context, key model.DeploymentKey, endpoint string) (err error) {
// Start the transaction
tx, err := d.Begin(ctx)
if err != nil {
return libdal.TranslatePGError(err)
}
defer tx.CommitOrRollback(ctx, &err)

err = tx.db.SetDeploymentEndpoint(ctx, key, optional.Some(endpoint))
if err != nil {
return libdal.TranslatePGError(err)
}
return nil
}

var ErrReplaceDeploymentAlreadyActive = errors.New("deployment already active")

// ReplaceDeployment replaces an old deployment of a module with a new deployment.
Expand Down Expand Up @@ -530,7 +514,6 @@ func (d *DAL) GetActiveDeployments(ctx context.Context) ([]dalmodel.Deployment,
Replicas: optional.Some(int(in.Replicas)),
Schema: in.Deployment.Schema,
CreatedAt: in.Deployment.CreatedAt,
Endpoint: in.Deployment.Endpoint,
}
}), nil
}
Expand Down
1 change: 0 additions & 1 deletion backend/controller/dal/internal/sql/models.go

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

1 change: 0 additions & 1 deletion backend/controller/dal/internal/sql/querier.go

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

8 changes: 1 addition & 7 deletions backend/controller/dal/internal/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ FROM runners r
ORDER BY r.key;

-- name: GetActiveDeployments :many
SELECT sqlc.embed(d), m.name AS module_name, m.language, COUNT(r.id) AS replicas, d.endpoint
SELECT sqlc.embed(d), m.name AS module_name, m.language, COUNT(r.id) AS replicas
FROM deployments d
JOIN modules m ON d.module_id = m.id
LEFT JOIN runners r ON d.id = r.deployment_id
Expand Down Expand Up @@ -147,12 +147,6 @@ SET min_replicas = $2, last_activated_at = CASE WHEN min_replicas = 0 THEN (NOW(
WHERE key = sqlc.arg('key')::deployment_key
RETURNING 1;

-- name: SetDeploymentEndpoint :exec
UPDATE deployments
SET endpoint = $2
WHERE key = sqlc.arg('key')::deployment_key
RETURNING 1;

-- name: GetExistingDeploymentForModule :one
SELECT *
FROM deployments d
Expand Down
34 changes: 6 additions & 28 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.

1 change: 0 additions & 1 deletion backend/controller/dal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ type Deployment struct {
Schema *schema.Module
CreatedAt time.Time
Labels model.Labels
Endpoint optional.Option[string]
}

func (d Deployment) String() string { return d.Key.String() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- migrate:up

ALTER TABLE deployments DROP COLUMN endpoint;

-- migrate:down

Loading
Loading