Files
tasks/service/history/queues/action_slice_predicate.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

96 lines
2.5 KiB
Go

package queues
import (
"github.com/hanzoai/tasks/service/history/tasks"
)
const (
moveSliceDefaultReaderMinPendingTaskCount = 50
moveSliceDefaultReaderMinSliceCount = 3
)
var _ Action = (*slicePredicateAction)(nil)
type (
// slicePredicateAction will move all slices in default reader
// with non-universal predicate to the next reader so that upon restart
// task loading for those slices won't blocking loading for other slices
// in the default reader.
//
// Slice.ShrinkScope() will shrink its predicate when there's few
// namespaces left. But those slices may still in the default reader.
// If there are many such slices in the default reader, then upon restart
// task loading for other namespace will be blocked. So we need to move those
// slices to a different reader.
//
// NOTE: When there's no restart/shard movement, this movement won't affect
// anything, as slice with non-universal predicate must have already loaded
// all tasks into memory.
slicePredicateAction struct {
monitor Monitor
maxReaderCount int
}
)
func newSlicePredicateAction(
monitor Monitor,
maxReaderCount int,
) *slicePredicateAction {
return &slicePredicateAction{
monitor: monitor,
maxReaderCount: maxReaderCount,
}
}
func (a *slicePredicateAction) Name() string {
return "slice-predicate"
}
func (a *slicePredicateAction) Run(readerGroup *ReaderGroup) bool {
reader, ok := readerGroup.ReaderByID(DefaultReaderId)
if !ok {
return false
}
if int64(a.maxReaderCount) <= DefaultReaderId+1 {
return false
}
sliceCount := a.monitor.GetSliceCount(DefaultReaderId)
pendingTasks := 0
hasNonUniversalPredicate := false
reader.WalkSlices(func(s Slice) {
pendingTasks += a.monitor.GetSlicePendingTaskCount(s)
if !tasks.IsUniverisalPredicate(s.Scope().Predicate) {
hasNonUniversalPredicate = true
}
})
// only move slices when either default reader slice count or
// pending task count is high
if !hasNonUniversalPredicate ||
(pendingTasks < moveSliceDefaultReaderMinPendingTaskCount &&
sliceCount < moveSliceDefaultReaderMinSliceCount) {
return false
}
var moveSlices []Slice
reader.SplitSlices(func(s Slice) (remaining []Slice, split bool) {
if tasks.IsUniverisalPredicate(s.Scope().Predicate) {
return []Slice{s}, false
}
moveSlices = append(moveSlices, s)
return nil, true
})
if len(moveSlices) == 0 {
return false
}
nextReader := readerGroup.GetOrCreateReader(DefaultReaderId + 1)
nextReader.MergeSlices(moveSlices...)
return true
}