Files
tasks/common/backoff/jitter.go
David ReissandAlan Wu e297d96384 Forward polls from sticky partitions to high-priority backlog (#8925)
## What changed?
- Add "ephemeral data" propagated alongside user data.
- Include information about which priority levels have significant
backlog in ephemeral data (currently only propagated down the tree, not
up)
- When sticky partitions see that normal partitions have significant
backlog, set up poll forwarders to forward to those partitions.
- The poll forwarders use min priority to ensure they only get
higher-priority tasks than available local tasks.

## Why?
Without this functionality, sticky queues interfere with priority
dispatch by keeping most of the pollers working on sticky tasks and
potentially starving high-priority tasks that appear on the normal
queue. With the default values of the new settings, sticky pollers
should notice high-priority normal tasks within 10 seconds.

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

## Potential risks
- Clients have to accept that polls on sticky may return normal tasks. I
believe all SDKs are fine with this.
- Increased overhead of user data + ephemeral data.
2026-02-11 20:02:31 -05:00

28 lines
773 B
Go

package backoff
import "math/rand"
// FullJitter return random number from 0 to input, inclusive, exclusive
func FullJitter[T ~int64 | ~int | ~int32 | ~float64 | ~float32](input T) T {
return T(rand.Float64() * float64(input))
}
// Jitter return random number from (1-coefficient)*input to (1+coefficient)*input, inclusive, exclusive
func Jitter[T ~int64 | ~int | ~int32 | ~float64 | ~float32](input T, coefficient float64) T {
validateCoefficient(coefficient)
if coefficient == 0 {
return input
}
base := float64(input) * (1 - coefficient)
addon := rand.Float64() * 2 * (float64(input) - base)
return T(base + addon)
}
func validateCoefficient(coefficient float64) {
if coefficient < 0 || coefficient > 1 {
panic("coefficient cannot be < 0 or > 1")
}
}