-
Notifications
You must be signed in to change notification settings - Fork 228
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 worker leak in eager dispatcher #1723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,24 +33,37 @@ import ( | |
// eagerWorkflowDispatcher is responsible for finding an available worker for an eager workflow task. | ||
type eagerWorkflowDispatcher struct { | ||
lock sync.RWMutex | ||
workersByTaskQueue map[string][]eagerWorker | ||
workersByTaskQueue map[string]map[eagerWorker]struct{} | ||
} | ||
|
||
// registerWorker registers a worker that can be used for eager workflow dispatch | ||
func (e *eagerWorkflowDispatcher) registerWorker(worker *workflowWorker) { | ||
e.lock.Lock() | ||
defer e.lock.Unlock() | ||
e.workersByTaskQueue[worker.executionParameters.TaskQueue] = append(e.workersByTaskQueue[worker.executionParameters.TaskQueue], worker.worker) | ||
taskQueue := worker.executionParameters.TaskQueue | ||
if e.workersByTaskQueue[taskQueue] == nil { | ||
e.workersByTaskQueue[taskQueue] = make(map[eagerWorker]struct{}) | ||
} | ||
e.workersByTaskQueue[taskQueue][worker.worker] = struct{}{} | ||
} | ||
|
||
// deregisterWorker deregister a worker so that it will not be used for eager workflow dispatch | ||
func (e *eagerWorkflowDispatcher) deregisterWorker(worker *workflowWorker) { | ||
e.lock.Lock() | ||
defer e.lock.Unlock() | ||
delete(e.workersByTaskQueue[worker.executionParameters.TaskQueue], worker.worker) | ||
} | ||
|
||
// applyToRequest updates request if eager workflow dispatch is possible and returns the eagerWorkflowExecutor to use | ||
func (e *eagerWorkflowDispatcher) applyToRequest(request *workflowservice.StartWorkflowExecutionRequest) *eagerWorkflowExecutor { | ||
// Try every worker that is assigned to the desired task queue. | ||
e.lock.RLock() | ||
workers := e.workersByTaskQueue[request.GetTaskQueue().Name] | ||
randWorkers := make([]eagerWorker, len(workers)) | ||
// Copy the slice so we can release the lock. | ||
copy(randWorkers, workers) | ||
randWorkers := make([]eagerWorker, 0, len(workers)) | ||
// Copy the workers so we can release the lock. | ||
for worker := range workers { | ||
randWorkers = append(randWorkers, worker) | ||
} | ||
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since deregister is rare I figure, it could be better maybe to burden it with the heavier work than this call, since I suspect |
||
e.lock.RUnlock() | ||
rand.Shuffle(len(randWorkers), func(i, j int) { randWorkers[i], randWorkers[j] = randWorkers[j], randWorkers[i] }) | ||
for _, worker := range randWorkers { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC it is unfortunate but known that Go does not reclaim memory for map entries on
delete
. At least it was the case in the older Go versions, but maybe it has changed (see issue https://github.com/golang/go/issues/ + 20135).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue is about the memory used by the map, what we care about is making sure nothing is holding a reference to
worker.worker
so it can be GC'd