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

fix: cron removed on module change #3512

Merged
merged 2 commits into from
Nov 26, 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
10 changes: 10 additions & 0 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,7 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
}
moduleState := map[string]moduleStateEntry{}
moduleByDeploymentKey := map[string]string{}
mostRecentDeploymentByModule := map[string]string{}

// Seed the notification channel with the current deployments.
seedDeployments, err := s.dal.GetActiveDeployments(ctx)
Expand Down Expand Up @@ -1555,13 +1556,18 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
// Deleted key
if deletion, ok := notification.Deleted.Get(); ok {
name := moduleByDeploymentKey[deletion.String()]
moduleRemoved := mostRecentDeploymentByModule[name] == deletion.String()
response = &ftlv1.PullSchemaResponse{
ModuleName: name,
DeploymentKey: deletion.String(),
ChangeType: ftlv1.DeploymentChangeType_DEPLOYMENT_REMOVED,
ModuleRemoved: moduleRemoved,
}
delete(moduleState, name)
delete(moduleByDeploymentKey, deletion.String())
if moduleRemoved {
delete(mostRecentDeploymentByModule, name)
}
} else if message, ok := notification.Message.Get(); ok {
moduleSchema := message.Schema.ToProto().(*schemapb.Module) //nolint:forcetypeassert
if moduleSchema.Runtime == nil {
Expand All @@ -1586,17 +1592,21 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
if !bytes.Equal(current.hash, newState.hash) || current.minReplicas != newState.minReplicas {
changeType := ftlv1.DeploymentChangeType_DEPLOYMENT_CHANGED
// A deployment is considered removed if its minReplicas is set to 0.
moduleRemoved := false
if current.minReplicas > 0 && message.MinReplicas == 0 {
changeType = ftlv1.DeploymentChangeType_DEPLOYMENT_REMOVED
moduleRemoved = mostRecentDeploymentByModule[message.Schema.Name] == message.Key.String()
}
response = &ftlv1.PullSchemaResponse{
ModuleName: moduleSchema.Name,
DeploymentKey: message.Key.String(),
Schema: moduleSchema,
ChangeType: changeType,
ModuleRemoved: moduleRemoved,
}
}
} else {
mostRecentDeploymentByModule[message.Schema.Name] = message.Key.String()
response = &ftlv1.PullSchemaResponse{
ModuleName: moduleSchema.Name,
DeploymentKey: message.Key.String(),
Expand Down
18 changes: 15 additions & 3 deletions backend/cron/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ func run(ctx context.Context, verbClient CallClient, changes chan *ftlv1.PullSch
next, ok := scheduleNext(cronQueue)
var nextCh <-chan time.Time
if ok {
logger.Tracef("Next cron job scheduled in %s", next)
logger.Debugf("Next cron job scheduled in %s", next)
nextCh = time.After(next)
} else {
logger.Debugf("No cron jobs scheduled")
}
select {
case <-ctx.Done():
return fmt.Errorf("cron service stopped: %w", ctx.Err())

case resp := <-changes:
if err := updateCronJobs(cronJobs, resp); err != nil {
if err := updateCronJobs(ctx, cronJobs, resp); err != nil {
logger.Errorf(err, "Failed to update cron jobs")
continue
}
Expand Down Expand Up @@ -162,12 +164,21 @@ func scheduleNext(cronQueue []cronJob) (time.Duration, bool) {
return time.Until(cronQueue[0].next), true
}

func updateCronJobs(cronJobs map[string][]cronJob, resp *ftlv1.PullSchemaResponse) error {
func updateCronJobs(ctx context.Context, cronJobs map[string][]cronJob, resp *ftlv1.PullSchemaResponse) error {
logger := log.FromContext(ctx).Scope("cron")
switch resp.ChangeType {
case ftlv1.DeploymentChangeType_DEPLOYMENT_REMOVED:
// We see the new state of the module before we see the removed deployment.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, that makes sense. Though if it's immediately ADDED, this shouldn't actually stop cron jobs from working as the ticket describes right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ADDED then REMOVED, so we first get a message with the new cron schema, then get a message that the old one is removed. With the code the way it currently is all the cron jobs after the first deployment basically get removed immediately.

// We only want to actually remove if it was not replaced by a new deployment.
if !resp.ModuleRemoved {
logger.Debugf("Not removing cron jobs for %s as module is still present", resp.DeploymentKey)
return nil
}
logger.Debugf("Removing cron jobs for module %s", resp.ModuleName)
delete(cronJobs, resp.ModuleName)

case ftlv1.DeploymentChangeType_DEPLOYMENT_ADDED, ftlv1.DeploymentChangeType_DEPLOYMENT_CHANGED:
logger.Debugf("Updated cron jobs for module %s", resp.ModuleName)
moduleSchema, err := schema.ModuleFromProto(resp.Schema)
if err != nil {
return fmt.Errorf("failed to extract module schema: %w", err)
Expand All @@ -176,6 +187,7 @@ func updateCronJobs(cronJobs map[string][]cronJob, resp *ftlv1.PullSchemaRespons
if err != nil {
return fmt.Errorf("failed to extract cron jobs: %w", err)
}
logger.Debugf("Adding %d cron jobs for module %s", len(moduleJobs), resp.ModuleName)
cronJobs[resp.ModuleName] = moduleJobs
}
return nil
Expand Down
7 changes: 6 additions & 1 deletion backend/ingress/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ func Start(ctx context.Context, config Config, pullSchemaClient PullSchemaClient
newState.protoSchema.Modules = append(newState.protoSchema.Modules, resp.Schema)
}
} else if existing != nil {
// We see the new state of the module before we see the removed deployment.
// We only want to actually remove if it was not replaced by a new deployment.
if !resp.ModuleRemoved {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah nice.

logger.Debugf("Not removing ingress for %s as it is not the current deployment", resp.DeploymentKey)
return nil
}
for i := range existing.Modules {
if existing.Modules[i].Name != resp.ModuleName {
newState.protoSchema.Modules = append(newState.protoSchema.Modules, existing.Modules[i])
}
}
}

newState.httpRoutes = extractIngressRoutingEntries(newState.protoSchema)
sch, err := schema.FromProto(newState.protoSchema)
if err != nil {
Expand Down
74 changes: 43 additions & 31 deletions backend/protos/xyz/block/ftl/v1/schemaservice.pb.go

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

2 changes: 2 additions & 0 deletions backend/protos/xyz/block/ftl/v1/schemaservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ message PullSchemaResponse {
// If false this is the last schema change in the initial batch, but others may follow later.
bool more = 3;
DeploymentChangeType change_type = 5;
// If this is true then the module was removed as well as the deployment. This is only set for DEPLOYMENT_REMOVED.
bool module_removed = 6;
}

service SchemaService {
Expand Down

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

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

Loading
Loading