Skip to content

Commit

Permalink
chore: use runtime dates instead of ones in deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Jan 13, 2025
1 parent c5ba4c2 commit af29a72
Showing 7 changed files with 64 additions and 32 deletions.
7 changes: 4 additions & 3 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
@@ -921,11 +921,12 @@ func (s *Service) CreateDeployment(ctx context.Context, req *connect.Request[ftl
return nil, fmt.Errorf("invalid module schema: %w", err)
}

module.ModRuntime().ModDeployment().CreatedAt = time.Now()

dkey := key.NewDeploymentKey(module.Name)
err = s.schemaState.Publish(ctx, &state.DeploymentCreatedEvent{
Key: dkey,
CreatedAt: time.Now(),
Schema: module,
Key: dkey,
Schema: module,
})
if err != nil {
logger.Errorf(err, "Could not create deployment event")
13 changes: 8 additions & 5 deletions backend/controller/state/controllerstate_test.go
Original file line number Diff line number Diff line change
@@ -76,16 +76,19 @@ func TestDeploymentState(t *testing.T) {
deploymentKey := key.NewDeploymentKey("test-deployment")
create := time.Now()
err = cs.Publish(ctx, &state.DeploymentCreatedEvent{
Key: deploymentKey,
CreatedAt: create,
Schema: &schema.Module{Name: "test"},
Key: deploymentKey,
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Deployment: &schema.ModuleRuntimeDeployment{
CreatedAt: create,
},
}},
})
assert.NoError(t, err)
view, err = cs.View(ctx)
assert.NoError(t, err)
assert.Equal(t, 1, len(view.GetDeployments()))
assert.Equal(t, deploymentKey, view.GetDeployments()[deploymentKey].Key)
assert.Equal(t, create, view.GetDeployments()[deploymentKey].CreatedAt)
assert.Equal(t, create, view.GetDeployments()[deploymentKey].Schema.GetRuntime().GetDeployment().CreatedAt)

activate := time.Now()
err = cs.Publish(ctx, &state.DeploymentActivatedEvent{
@@ -97,7 +100,7 @@ func TestDeploymentState(t *testing.T) {
view, err = cs.View(ctx)
assert.NoError(t, err)
assert.Equal(t, 1, view.GetDeployments()[deploymentKey].Schema.GetRuntime().GetScaling().GetMinReplicas())
assert.Equal(t, activate, view.GetDeployments()[deploymentKey].ActivatedAt.MustGet())
assert.Equal(t, activate, view.GetDeployments()[deploymentKey].Schema.GetRuntime().GetDeployment().ActivatedAt)

err = cs.Publish(ctx, &state.DeploymentDeactivatedEvent{
Key: deploymentKey,
19 changes: 7 additions & 12 deletions backend/controller/state/deployments.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import (
"fmt"
"time"

"github.com/alecthomas/types/optional"
"golang.org/x/exp/maps"

"github.com/block/ftl/common/schema"
@@ -13,10 +12,8 @@ import (
)

type Deployment struct {
Key key.Deployment
Schema *schema.Module
CreatedAt time.Time
ActivatedAt optional.Option[time.Time]
Key key.Deployment
Schema *schema.Module
}

func (r *SchemaState) GetDeployment(deployment key.Deployment) (*Deployment, error) {
@@ -53,19 +50,17 @@ var _ SchemaEvent = (*DeploymentSchemaUpdatedEvent)(nil)
var _ SchemaEvent = (*DeploymentReplicasUpdatedEvent)(nil)

type DeploymentCreatedEvent struct {
Key key.Deployment
CreatedAt time.Time
Schema *schema.Module
Key key.Deployment
Schema *schema.Module
}

func (r *DeploymentCreatedEvent) Handle(t SchemaState) (SchemaState, error) {
if existing := t.deployments[r.Key]; existing != nil {
return t, nil
}
n := Deployment{
Key: r.Key,
CreatedAt: r.CreatedAt,
Schema: r.Schema,
Key: r.Key,
Schema: r.Schema,
}
t.deployments[r.Key] = &n
return t, nil
@@ -111,7 +106,7 @@ func (r *DeploymentActivatedEvent) Handle(t SchemaState) (SchemaState, error) {
return t, fmt.Errorf("deployment %s not found", r.Key)

}
existing.ActivatedAt = optional.Some(r.ActivatedAt)
existing.Schema.ModRuntime().ModDeployment().ActivatedAt = r.ActivatedAt
setMinReplicas(existing.Schema, r.MinReplicas)
t.activeDeployments[r.Key] = true
return t, nil
5 changes: 2 additions & 3 deletions backend/controller/state/eventextractor.go
Original file line number Diff line number Diff line change
@@ -20,9 +20,8 @@ func EventExtractor(diff tuple.Pair[SchemaState, SchemaState]) iter.Seq[SchemaEv
pd, ok := previousAll[deployment.Key]
if !ok {
events = append(events, &DeploymentCreatedEvent{
Key: deployment.Key,
CreatedAt: deployment.CreatedAt,
Schema: deployment.Schema,
Key: deployment.Key,
Schema: deployment.Schema,
})
} else if !pd.Schema.Equals(deployment.Schema) {
events = append(events, &DeploymentSchemaUpdatedEvent{
30 changes: 21 additions & 9 deletions backend/controller/state/eventextractor_test.go
Original file line number Diff line number Diff line change
@@ -26,17 +26,25 @@ func TestEventExtractor(t *testing.T) {
current: SchemaState{
deployments: map[key.Deployment]*Deployment{
deploymentKey(t, "dpl-test-sjkfislfjslfas"): {
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
CreatedAt: now,
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{Base: schema.ModuleRuntimeBase{Language: "go"}}},
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Base: schema.ModuleRuntimeBase{Language: "go"},
Deployment: &schema.ModuleRuntimeDeployment{
CreatedAt: now,
},
}},
},
},
},
want: []SchemaEvent{
&DeploymentCreatedEvent{
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
CreatedAt: now,
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{Base: schema.ModuleRuntimeBase{Language: "go"}}},
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Base: schema.ModuleRuntimeBase{Language: "go"},
Deployment: &schema.ModuleRuntimeDeployment{
CreatedAt: now,
},
}},
},
},
},
@@ -45,9 +53,13 @@ func TestEventExtractor(t *testing.T) {
previous: SchemaState{
deployments: map[key.Deployment]*Deployment{
deploymentKey(t, "dpl-test-sjkfislfjslfas"): {
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
CreatedAt: now,
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{Base: schema.ModuleRuntimeBase{Language: "go"}}},
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Base: schema.ModuleRuntimeBase{Language: "go"},
Deployment: &schema.ModuleRuntimeDeployment{
CreatedAt: now,
},
}},
},
},
},
7 changes: 7 additions & 0 deletions common/schema/module.go
Original file line number Diff line number Diff line change
@@ -308,3 +308,10 @@ func (m *Module) GetRuntime() *ModuleRuntime {
}
return m.Runtime
}

func (m *Module) ModRuntime() *ModuleRuntime {
if m.Runtime == nil {
m.Runtime = &ModuleRuntime{}
}
return m.Runtime
}
15 changes: 15 additions & 0 deletions common/schema/moduleruntime.go
Original file line number Diff line number Diff line change
@@ -138,6 +138,21 @@ func (m *ModuleRuntime) GetScaling() *ModuleRuntimeScaling {
return m.Scaling
}

func (m *ModuleRuntime) GetDeployment() *ModuleRuntimeDeployment {
if m == nil {
return nil
}
return m.Deployment
}

func (m *ModuleRuntime) ModDeployment() *ModuleRuntimeDeployment {
if m.Deployment == nil {
m.Deployment = &ModuleRuntimeDeployment{}
}
return m.Deployment

}

func (m *ModuleRuntimeScaling) GetMinReplicas() int32 {
if m == nil {
return 0

0 comments on commit af29a72

Please sign in to comment.