Skip to content

Commit

Permalink
Merge pull request #278 from cschleiden/explicitly-remove-workflow-fr…
Browse files Browse the repository at this point in the history
…om-cache

Explicitly evict finished workflow instances from cache
  • Loading branch information
cschleiden authored Nov 11, 2023
2 parents d8ce2bd + f06fe8e commit 7ad7240
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backend/test/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ func (*noopWorkflowExecutorCache) Get(ctx context.Context, instance *core.Workfl
return nil, false, nil
}

// Evict implements workflow.ExecutorCache
func (*noopWorkflowExecutorCache) Evict(ctx context.Context, instance *core.WorkflowInstance) error {
return nil
}

// StartEviction implements workflow.ExecutorCache
func (*noopWorkflowExecutorCache) StartEviction(ctx context.Context) {
}
Expand Down
7 changes: 7 additions & 0 deletions internal/worker/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func (wtw *WorkflowTaskWorker) Complete(ctx context.Context, result *workflow.Ex
metrickeys.ContinuedAsNew: fmt.Sprint(state == core.WorkflowInstanceStateContinuedAsNew),
}, 1)
}

// Workflow is finished, explicitly evict from cache (if one is used)
if wtw.cache != nil {
if err := wtw.cache.Evict(ctx, t.WorkflowInstance); err != nil {
wtw.logger.ErrorContext(ctx, "could not evict workflow executor from cache", "error", err)
}
}
}

wtw.backend.Metrics().Counter(metrickeys.ActivityTaskScheduled, metrics.Tags{}, int64(len(result.ActivityEvents)))
Expand Down
1 change: 1 addition & 0 deletions internal/workflow/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type ExecutorCache interface {
Store(ctx context.Context, instance *core.WorkflowInstance, workflow WorkflowExecutor) error
Evict(ctx context.Context, instance *core.WorkflowInstance) error
Get(ctx context.Context, instance *core.WorkflowInstance) (WorkflowExecutor, bool, error)
StartEviction(ctx context.Context)
}
8 changes: 8 additions & 0 deletions internal/workflow/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func (lc *LruCache) Store(ctx context.Context, instance *core.WorkflowInstance,
return nil
}

func (lc *LruCache) Evict(ctx context.Context, instance *core.WorkflowInstance) error {
lc.c.Delete(getKey(instance))

lc.mc.Gauge(metrickeys.WorkflowInstanceCacheSize, metrics.Tags{}, int64(lc.c.Len()))

return nil
}

func (lc *LruCache) StartEviction(ctx context.Context) {
go lc.c.Start()

Expand Down

0 comments on commit 7ad7240

Please sign in to comment.