Files
tasks/common/priorities/priority_util.go
Stephan BehnkeandGitHub a0067a2aef Fairness weight override API (#8379)
## What changed?

Adds the ability to set/unset fairness weight overrides by fairness key
via `UpdateTaskQueueConfig` API.

From the API:

> // Fairness weight for a task can come from multiple sources for
    // flexibility. From highest to lowest precedence:
// 1. Weights for a small set of keys can be overridden in task queue
    //    configuration with an API.
    // 2. It can be attached to the workflow/activity in this field.
    // 3. The default weight of 1.0 will be used.
    //
    // Weight values are clamped to the range [0.001, 1000].

## Why?

To allow users to fine-tune their fairness key behavior via the API. 

## How did you test it?
- [x] built
- [x] run locally and tested manually
- [ ] covered by existing tests
- [ ] added new unit test(s)
- [x] added new functional test(s)

## Potential risks
N/A
2025-10-17 00:37:31 +00:00

63 lines
1.4 KiB
Go

package priorities
import (
"cmp"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
)
const (
fairnessKeyMaxLength = 64
)
var (
ErrInvalidPriority = serviceerror.NewInvalidArgument("priority key can't be negative")
ErrFairnessKeyLength = serviceerror.NewInvalidArgument("fairness key length exceeds limit")
ErrInvalidFairnessWeight = serviceerror.NewInvalidArgument("must be greater than zero")
)
func Merge(
base *commonpb.Priority,
override *commonpb.Priority,
) *commonpb.Priority {
if base == nil {
return override
} else if override == nil {
return base
}
return &commonpb.Priority{
PriorityKey: cmp.Or(override.PriorityKey, base.PriorityKey),
FairnessKey: cmp.Or(override.FairnessKey, base.FairnessKey),
FairnessWeight: cmp.Or(override.FairnessWeight, base.FairnessWeight),
}
}
func ValidateFairnessKey(key string) error {
if len(key) > fairnessKeyMaxLength {
return ErrFairnessKeyLength
}
return nil
}
func ValidateFairnessWeight(weight float32) error {
if weight <= 0 {
return ErrInvalidFairnessWeight
}
return nil
}
func Validate(p *commonpb.Priority) error {
if p == nil {
return nil
} else if p.PriorityKey < 0 {
return ErrInvalidPriority
} else if err := ValidateFairnessKey(p.FairnessKey); err != nil {
return err
} else if p.FairnessWeight < 0 {
return ErrInvalidFairnessWeight
}
return nil
}