-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
544 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package internal | ||
|
||
// eagerWorker is the minimal worker interface needed for eager activities and workflows | ||
type eagerWorker interface { | ||
// tryReserveSlot tries to reserver a task slot on the worker without blocking | ||
// caller is expected to release the slot with releaseSlot | ||
tryReserveSlot() bool | ||
// releaseSlot release a task slot acquired by tryReserveSlot | ||
releaseSlot() | ||
// processTaskAsync process a new task on the worker asynchronously and | ||
// call callback once complete | ||
processTaskAsync(task interface{}, callback func()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package internal | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"go.temporal.io/api/workflowservice/v1" | ||
) | ||
|
||
// eagerWorkflowDispatcher is responsible for finding an available worker for an eager workflow task. | ||
type eagerWorkflowDispatcher struct { | ||
lock sync.RWMutex | ||
workersByTaskQueue map[string][]eagerWorker | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// 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) | ||
e.lock.RUnlock() | ||
rand.Shuffle(len(randWorkers), func(i, j int) { randWorkers[i], randWorkers[j] = randWorkers[j], randWorkers[i] }) | ||
for _, worker := range randWorkers { | ||
if worker.tryReserveSlot() { | ||
request.RequestEagerExecution = true | ||
return &eagerWorkflowExecutor{ | ||
worker: worker, | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// eagerWorkflowExecutor is a worker-scoped executor for an eager workflow task. | ||
type eagerWorkflowExecutor struct { | ||
handledResponse atomic.Bool | ||
worker eagerWorker | ||
} | ||
|
||
// handleResponse of an eager workflow task from a StartWorkflowExecution request. | ||
func (e *eagerWorkflowExecutor) handleResponse(response *workflowservice.PollWorkflowTaskQueueResponse) { | ||
if !e.handledResponse.CompareAndSwap(false, true) { | ||
panic("eagerWorkflowExecutor trying to handle multiple responses") | ||
} | ||
// Asynchronously execute the task | ||
task := &eagerWorkflowTask{ | ||
task: response, | ||
} | ||
e.worker.processTaskAsync(task, func() { | ||
// The processTaskAsync does not do this itself because our task is *eagerWorkflowTask, not *polledTask. | ||
e.worker.releaseSlot() | ||
}) | ||
} | ||
|
||
// release the executor task slot this eagerWorkflowExecutor was holding. | ||
// If it is currently handling a responses or has already released the task slot | ||
// then do nothing. | ||
func (e *eagerWorkflowExecutor) release() { | ||
if e.handledResponse.CompareAndSwap(false, true) { | ||
// Assume there is room because it is reserved on creation, so we make a blocking send. | ||
// The processTask does not do this itself because our task is not *polledTask. | ||
e.worker.releaseSlot() | ||
} else { | ||
panic("trying to release an eagerWorkflowExecutor that has already been released") | ||
} | ||
} |
Oops, something went wrong.