mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
301 lines
6.9 KiB
Go
301 lines
6.9 KiB
Go
//go:generate mockgen -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)
|
|
}
|