mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-02 12:22:18 +00:00
## What changed and why? Revert most of the changes from #5620 since this doesn't properly detect stale files and we don't want to take the time to fix it at the moment.
325 lines
8.1 KiB
Go
325 lines
8.1 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// 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.
|
|
|
|
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination rescheduler_mock.go
|
|
|
|
package queues
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/backoff"
|
|
"go.temporal.io/server/common/clock"
|
|
"go.temporal.io/server/common/collection"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/common/persistence"
|
|
ctasks "go.temporal.io/server/common/tasks"
|
|
"go.temporal.io/server/common/timer"
|
|
"go.temporal.io/server/common/util"
|
|
)
|
|
|
|
const (
|
|
taskChanFullBackoff = 2 * time.Second
|
|
taskChanFullBackoffJitterCoefficient = 0.5
|
|
|
|
reschedulerPQCleanupDuration = 3 * time.Minute
|
|
reschedulerPQCleanupJitterCoefficient = 0.15
|
|
)
|
|
|
|
type (
|
|
// Rescheduler buffers task executables that are failed to process and
|
|
// resubmit them to the task scheduler when the Reschedule method is called.
|
|
Rescheduler interface {
|
|
// Add task executable to the rescheduler.
|
|
Add(task Executable, rescheduleTime time.Time)
|
|
|
|
// Reschedule triggers an immediate reschedule for provided namespace
|
|
// ignoring executable's reschedule time.
|
|
// Used by namespace failover logic
|
|
Reschedule(namespaceID string)
|
|
|
|
// Len returns the total number of task executables waiting to be rescheduled.
|
|
Len() int
|
|
Start()
|
|
Stop()
|
|
}
|
|
|
|
rescheduledExecuable struct {
|
|
executable Executable
|
|
rescheduleTime time.Time
|
|
}
|
|
|
|
reschedulerImpl struct {
|
|
scheduler Scheduler
|
|
timeSource clock.TimeSource
|
|
logger log.Logger
|
|
metricsHandler metrics.Handler
|
|
|
|
status int32
|
|
shutdownCh chan struct{}
|
|
shutdownWG sync.WaitGroup
|
|
|
|
timerGate timer.Gate
|
|
taskChannelKeyFn TaskChannelKeyFn
|
|
|
|
sync.Mutex
|
|
pqMap map[TaskChannelKey]collection.Queue[rescheduledExecuable]
|
|
numExecutables int
|
|
}
|
|
)
|
|
|
|
func NewRescheduler(
|
|
scheduler Scheduler,
|
|
timeSource clock.TimeSource,
|
|
logger log.Logger,
|
|
metricsHandler metrics.Handler,
|
|
) *reschedulerImpl {
|
|
return &reschedulerImpl{
|
|
scheduler: scheduler,
|
|
timeSource: timeSource,
|
|
logger: logger,
|
|
metricsHandler: metricsHandler,
|
|
|
|
status: common.DaemonStatusInitialized,
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
timerGate: timer.NewLocalGate(timeSource),
|
|
taskChannelKeyFn: scheduler.TaskChannelKeyFn(),
|
|
|
|
pqMap: make(map[TaskChannelKey]collection.Queue[rescheduledExecuable]),
|
|
}
|
|
}
|
|
|
|
func (r *reschedulerImpl) Start() {
|
|
if !atomic.CompareAndSwapInt32(&r.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
|
|
return
|
|
}
|
|
|
|
r.shutdownWG.Add(1)
|
|
go r.rescheduleLoop()
|
|
|
|
r.logger.Info("Task rescheduler started.", tag.LifeCycleStarted)
|
|
}
|
|
|
|
func (r *reschedulerImpl) Stop() {
|
|
if !atomic.CompareAndSwapInt32(&r.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
|
|
return
|
|
}
|
|
|
|
close(r.shutdownCh)
|
|
r.timerGate.Close()
|
|
|
|
if success := common.AwaitWaitGroup(&r.shutdownWG, time.Minute); !success {
|
|
r.logger.Warn("Task rescheduler timedout on shutdown.", tag.LifeCycleStopTimedout)
|
|
}
|
|
|
|
r.logger.Info("Task rescheduler stopped.", tag.LifeCycleStopped)
|
|
}
|
|
|
|
func (r *reschedulerImpl) Add(
|
|
executable Executable,
|
|
rescheduleTime time.Time,
|
|
) {
|
|
r.Lock()
|
|
pq := r.getOrCreatePQLocked(r.taskChannelKeyFn(executable))
|
|
pq.Add(rescheduledExecuable{
|
|
executable: executable,
|
|
rescheduleTime: rescheduleTime,
|
|
})
|
|
r.numExecutables++
|
|
r.timerGate.Update(rescheduleTime)
|
|
r.Unlock()
|
|
|
|
if r.isStopped() {
|
|
r.drain()
|
|
}
|
|
}
|
|
|
|
func (r *reschedulerImpl) Reschedule(
|
|
namespaceID string,
|
|
) {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
now := r.timeSource.Now()
|
|
updatedRescheduleTime := false
|
|
for key, pq := range r.pqMap {
|
|
if key.NamespaceID != namespaceID {
|
|
continue
|
|
}
|
|
|
|
updatedRescheduleTime = true
|
|
// set reschedule time for all tasks in this pq to be now
|
|
items := make([]rescheduledExecuable, 0, pq.Len())
|
|
for !pq.IsEmpty() {
|
|
rescheduled := pq.Remove()
|
|
// scheduled queue pre-fetches tasks,
|
|
// so we need to make sure the reschedule time is not before the task scheduled time
|
|
rescheduled.rescheduleTime = util.MaxTime(
|
|
rescheduled.executable.GetKey().FireTime.Add(persistence.ScheduledTaskMinPrecision),
|
|
now,
|
|
)
|
|
items = append(items, rescheduled)
|
|
}
|
|
r.pqMap[key] = r.newPriorityQueue(items)
|
|
}
|
|
|
|
// then update timer gate to trigger the actual reschedule
|
|
if updatedRescheduleTime {
|
|
r.timerGate.Update(now)
|
|
}
|
|
}
|
|
|
|
func (r *reschedulerImpl) Len() int {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
return r.numExecutables
|
|
}
|
|
|
|
func (r *reschedulerImpl) rescheduleLoop() {
|
|
defer r.shutdownWG.Done()
|
|
|
|
cleanupTimer := time.NewTimer(backoff.Jitter(
|
|
reschedulerPQCleanupDuration,
|
|
reschedulerPQCleanupJitterCoefficient,
|
|
))
|
|
defer cleanupTimer.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-r.shutdownCh:
|
|
r.drain()
|
|
return
|
|
case <-r.timerGate.FireCh():
|
|
r.reschedule()
|
|
case <-cleanupTimer.C:
|
|
r.cleanupPQ()
|
|
cleanupTimer.Reset(backoff.Jitter(
|
|
reschedulerPQCleanupDuration,
|
|
reschedulerPQCleanupJitterCoefficient,
|
|
))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (r *reschedulerImpl) reschedule() {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
metrics.TaskReschedulerPendingTasks.With(r.metricsHandler).Record(int64(r.numExecutables))
|
|
now := r.timeSource.Now()
|
|
for _, pq := range r.pqMap {
|
|
for !pq.IsEmpty() {
|
|
rescheduled := pq.Peek()
|
|
|
|
if rescheduleTime := rescheduled.rescheduleTime; now.Before(rescheduleTime) {
|
|
r.timerGate.Update(rescheduleTime)
|
|
break
|
|
}
|
|
|
|
executable := rescheduled.executable
|
|
if executable.State() == ctasks.TaskStateCancelled {
|
|
pq.Remove()
|
|
r.numExecutables--
|
|
continue
|
|
}
|
|
|
|
executable.SetScheduledTime(now)
|
|
submitted := r.scheduler.TrySubmit(executable)
|
|
if !submitted {
|
|
r.timerGate.Update(now.Add(backoff.Jitter(taskChanFullBackoff, taskChanFullBackoffJitterCoefficient)))
|
|
break
|
|
}
|
|
|
|
pq.Remove()
|
|
r.numExecutables--
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *reschedulerImpl) cleanupPQ() {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
for key, pq := range r.pqMap {
|
|
if pq.IsEmpty() {
|
|
delete(r.pqMap, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *reschedulerImpl) drain() {
|
|
r.Lock()
|
|
defer r.Unlock()
|
|
|
|
for key, pq := range r.pqMap {
|
|
for !pq.IsEmpty() {
|
|
pq.Remove()
|
|
}
|
|
delete(r.pqMap, key)
|
|
}
|
|
|
|
r.numExecutables = 0
|
|
}
|
|
|
|
func (r *reschedulerImpl) isStopped() bool {
|
|
return atomic.LoadInt32(&r.status) == common.DaemonStatusStopped
|
|
}
|
|
|
|
func (r *reschedulerImpl) getOrCreatePQLocked(
|
|
key TaskChannelKey,
|
|
) collection.Queue[rescheduledExecuable] {
|
|
if pq, ok := r.pqMap[key]; ok {
|
|
return pq
|
|
}
|
|
|
|
pq := r.newPriorityQueue(nil)
|
|
r.pqMap[key] = pq
|
|
return pq
|
|
}
|
|
|
|
func (r *reschedulerImpl) newPriorityQueue(
|
|
items []rescheduledExecuable,
|
|
) collection.Queue[rescheduledExecuable] {
|
|
if items == nil {
|
|
return collection.NewPriorityQueue(r.rescheduledExecuableCompareLess)
|
|
}
|
|
|
|
return collection.NewPriorityQueueWithItems(r.rescheduledExecuableCompareLess, items)
|
|
}
|
|
|
|
func (r *reschedulerImpl) rescheduledExecuableCompareLess(
|
|
this rescheduledExecuable,
|
|
that rescheduledExecuable,
|
|
) bool {
|
|
return this.rescheduleTime.Before(that.rescheduleTime)
|
|
}
|