Files
tasks/service/matching/fairness_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

57 lines
1.1 KiB
Go

package matching
import (
"maps"
commonpb "go.temporal.io/api/common/v1"
)
const (
// minWeight * strideFactor must be >= 1
strideFactor = 1000
minWeight = 0.001
)
type fairnessWeightOverrides map[string]float32
func getEffectiveWeight(overrides fairnessWeightOverrides, pri *commonpb.Priority) float32 {
key := pri.GetFairnessKey()
weight, ok := overrides[key]
if !ok {
weight = pri.GetFairnessWeight()
}
// zero means default weight (1.0). negative doesn't make sense, map it to 1.0 also.
if weight <= 0.0 {
weight = 1.0
} else {
weight = max(weight, minWeight)
}
return weight
}
func mergeFairnessWeightOverrides(
existing fairnessWeightOverrides,
set fairnessWeightOverrides,
unset []string,
maxFairnessKeyWeightOverrides int,
) (fairnessWeightOverrides, error) {
if len(existing) == 0 {
// Validation already made sure that no keys of unset and set equal.
return set, nil
}
res := maps.Clone(existing)
for _, k := range unset {
delete(res, k)
}
maps.Copy(res, set)
if len(res) > maxFairnessKeyWeightOverrides {
return nil, errFairnessOverridesUpdateRejected
}
return res, nil
}