Files
tasks/client/matching/loadbalancer.go
Shahab TajikandGitHub faba3e2eb8 Prevent forwarding and sync match when there is a backlog (#4982)
<!-- Describe what has changed in this PR -->
**What changed?**
- Added a config to define a threshold for *negligible* backlog based on
the age of the oldest task in the backlog. The value is configurable per
TQ, but planning to set it to 5 secs by default in prod. A backlog older
than this threshold is considered non-negligible.
- Stop forwarding tasks when there is a non-negligible backlog
- This in turn stops pollers to be forwarded in presence of a
non-negligible backlog.
- This ensures that all the partition accumulate and process backlog at
the same rate. Effectively, the backlog age diff between partitions
should not grow significantly larger than the negligibility threshold
(e.g. 5 secs).
- There is an exception to this: forwarding tasks is allowed when the
partition has not received a poll recently (configurable, default
200ms). This is to ensure that in a TQ with few pollers, tasks and
pollers are do not stuck in different partitions for a long time.
- Prevent sync match when there is a non-negligible backlog. This is
already mostly happening (i.e. history tasks are all spooled and only
synced matched in race conditions, when there is a backlog). However,
forwarded tasks are also “sync match”ed in the root partition. In effect
this change makes sure that the root partition does not dispatch
forwarded tasks when itself has a backlog. This helps further with
keeping the partitions balanced.
- Also added the following metrics to be able to monitor poll and task
dispatch behavior at TQ partition level:
    - `poll_latency` with `forwarded=false/true`
- `task_dispatch_latency` with `forwarded=false/true` and
`source=History/DbBacklog`

<!-- Tell your future self why have you made these changes -->
**Why?**
With previous forwarding logic, when a backlog started to form, the root
partition was effectively receiving more pollers and was able to process
its backlog faster. This mixed up the order of dispatching tasks: a new
task could be dispatched faster by going to the root partitions while
old tasks are backlogged in other partitions. This change prevents that
situation.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
- Unit tests
- Manually verified against 9 workload+worker configurations as defined
here:
https://github.com/ShahabT/temporal-tests/blob/main/balanced-partitions/experiments.go

<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**
The change is config protected and the default value is disabled (by
setting a very high threshold). Will be enabled by default in a future
release after successful verification in prod workloads.

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
No.
2023-11-30 09:59:02 -08:00

283 lines
9.1 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package matching
import (
"math/rand"
"sync"
enumspb "go.temporal.io/api/enums/v1"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/tqname"
)
type (
// LoadBalancer is the interface for implementers of
// component that distributes add/poll api calls across
// available task queue partitions when possible
LoadBalancer interface {
// PickWritePartition returns the task queue partition for adding
// an activity or workflow task. The input is the name of the
// original task queue (with no partition info). When forwardedFrom
// is non-empty, this call is forwardedFrom from a child partition
// to a parent partition in which case, no load balancing should be
// performed
PickWritePartition(
namespaceID namespace.ID,
taskQueue *taskqueuepb.TaskQueue,
taskQueueType enumspb.TaskQueueType,
forwardedFrom string,
) string
// PickReadPartition returns the task queue partition to send a poller to.
// Input is name of the original task queue as specified by caller. When
// forwardedFrom is non-empty, no load balancing should be done.
PickReadPartition(
namespaceID namespace.ID,
taskQueue *taskqueuepb.TaskQueue,
taskQueueType enumspb.TaskQueueType,
forwardedFrom string,
) *pollToken
}
defaultLoadBalancer struct {
namespaceIDToName func(id namespace.ID) (namespace.Name, error)
nReadPartitions dynamicconfig.IntPropertyFnWithTaskQueueInfoFilters
nWritePartitions dynamicconfig.IntPropertyFnWithTaskQueueInfoFilters
forceReadPartition dynamicconfig.IntPropertyFn
forceWritePartition dynamicconfig.IntPropertyFn
lock sync.RWMutex
taskQueueLBs map[taskQueueKey]*tqLoadBalancer
}
// Keeps track of polls per partition. Sends a poll to the partition with the fewest polls
tqLoadBalancer struct {
taskQueue taskQueueKey
pollerCounts []int // keep track of poller count of each partition
lock sync.Mutex
}
taskQueueKey struct {
NamespaceID namespace.ID
Name tqname.Name
Type enumspb.TaskQueueType
}
pollToken struct {
fullName string
partitionID int
balancer *tqLoadBalancer
}
)
// NewLoadBalancer returns an instance of matching load balancer that
// can help distribute api calls across task queue partitions
func NewLoadBalancer(
namespaceIDToName func(id namespace.ID) (namespace.Name, error),
dc *dynamicconfig.Collection,
) LoadBalancer {
lb := &defaultLoadBalancer{
namespaceIDToName: namespaceIDToName,
nReadPartitions: dc.GetTaskQueuePartitionsProperty(dynamicconfig.MatchingNumTaskqueueReadPartitions),
nWritePartitions: dc.GetTaskQueuePartitionsProperty(dynamicconfig.MatchingNumTaskqueueWritePartitions),
forceReadPartition: dc.GetIntProperty(dynamicconfig.TestMatchingLBForceReadPartition, -1),
forceWritePartition: dc.GetIntProperty(dynamicconfig.TestMatchingLBForceWritePartition, -1),
lock: sync.RWMutex{},
taskQueueLBs: make(map[taskQueueKey]*tqLoadBalancer),
}
return lb
}
func (lb *defaultLoadBalancer) PickWritePartition(
namespaceID namespace.ID,
taskQueue *taskqueuepb.TaskQueue,
taskQueueType enumspb.TaskQueueType,
forwardedFrom string,
) string {
if forwardedFrom != "" || taskQueue.GetKind() == enumspb.TASK_QUEUE_KIND_STICKY {
return taskQueue.GetName()
}
tqName, err := tqname.FromBaseName(taskQueue.GetName())
// this should never happen when forwardedFrom is empty
if err != nil {
return taskQueue.GetName()
}
if n := lb.forceWritePartition(); n >= 0 {
return tqName.WithPartition(n).FullName()
}
nsName, err := lb.namespaceIDToName(namespaceID)
if err != nil {
return taskQueue.GetName()
}
n := max(1, lb.nWritePartitions(nsName.String(), tqName.BaseNameString(), taskQueueType))
return tqName.WithPartition(rand.Intn(n)).FullName()
}
// PickReadPartition picks a partition for poller to poll task from, and keeps load balanced between partitions.
// Caller is responsible to call pollToken.Release() after complete the poll.
func (lb *defaultLoadBalancer) PickReadPartition(
namespaceID namespace.ID,
taskQueue *taskqueuepb.TaskQueue,
taskQueueType enumspb.TaskQueueType,
forwardedFrom string,
) *pollToken {
if forwardedFrom != "" || taskQueue.Kind == enumspb.TASK_QUEUE_KIND_STICKY {
// no partition for sticky task queue and forwarded request
return &pollToken{fullName: taskQueue.GetName()}
}
parsedName, err := tqname.Parse(taskQueue.GetName())
if err != nil || err == nil && !parsedName.IsRoot() {
// parse error or partition already picked, use as-is
return &pollToken{fullName: taskQueue.GetName()}
}
tqlb := lb.getTaskQueueLoadBalancer(namespaceID, parsedName, taskQueueType)
// For read path it's safer to return global default partition count instead of root partition, when we fail to
// map namespace ID to name.
var partitionCount = dynamicconfig.GlobalDefaultNumTaskQueuePartitions
namespaceName, err := lb.namespaceIDToName(namespaceID)
if err == nil {
partitionCount = lb.nReadPartitions(string(namespaceName), parsedName.BaseNameString(), taskQueueType)
}
return tqlb.pickReadPartition(partitionCount, lb.forceReadPartition())
}
func (lb *defaultLoadBalancer) getTaskQueueLoadBalancer(
namespaceID namespace.ID, parsedName tqname.Name, tqType enumspb.TaskQueueType,
) *tqLoadBalancer {
key := taskQueueKey{NamespaceID: namespaceID, Name: parsedName, Type: tqType}
lb.lock.RLock()
tqlb, ok := lb.taskQueueLBs[key]
lb.lock.RUnlock()
if ok {
return tqlb
}
lb.lock.Lock()
tqlb, ok = lb.taskQueueLBs[key]
if !ok {
tqlb = newTaskQueueLoadBalancer(key)
lb.taskQueueLBs[key] = tqlb
}
lb.lock.Unlock()
return tqlb
}
func newTaskQueueLoadBalancer(key taskQueueKey) *tqLoadBalancer {
return &tqLoadBalancer{
taskQueue: key,
}
}
func (b *tqLoadBalancer) pickReadPartition(partitionCount int, forcedPartition int) *pollToken {
b.lock.Lock()
defer b.lock.Unlock()
// ensure we reflect dynamic config change if it ever happens
b.ensurePartitionCountLocked(partitionCount)
partitionID := forcedPartition
if partitionID < 0 {
partitionID = b.pickReadPartitionWithFewestPolls(partitionCount)
}
b.pollerCounts[partitionID]++
return &pollToken{
fullName: b.taskQueue.Name.WithPartition(partitionID).FullName(),
partitionID: partitionID,
balancer: b,
}
}
// caller to ensure that lock is obtained before call this function
func (b *tqLoadBalancer) pickReadPartitionWithFewestPolls(partitionCount int) int {
// pick a random partition to start with
startPartitionID := rand.Intn(partitionCount)
pickedPartitionID := startPartitionID
minPollerCount := b.pollerCounts[pickedPartitionID]
for i := 1; i < partitionCount && minPollerCount > 0; i++ {
currPartitionID := (startPartitionID + i) % int(partitionCount)
if b.pollerCounts[currPartitionID] < minPollerCount {
pickedPartitionID = currPartitionID
minPollerCount = b.pollerCounts[currPartitionID]
}
}
return pickedPartitionID
}
// caller to ensure that lock is obtained before call this function
func (b *tqLoadBalancer) ensurePartitionCountLocked(partitionCount int) {
if len(b.pollerCounts) == partitionCount {
return
}
if len(b.pollerCounts) < partitionCount {
// add more partition entries
for i := len(b.pollerCounts); i < partitionCount; i++ {
b.pollerCounts = append(b.pollerCounts, 0)
}
} else {
// truncate existing partition entries
b.pollerCounts = b.pollerCounts[:partitionCount]
}
}
func (b *tqLoadBalancer) Release(partitionID int) {
b.lock.Lock()
defer b.lock.Unlock()
// partitionID could be out of range if dynamic config reduce taskQueue partition count
if len(b.pollerCounts) > partitionID && b.pollerCounts[partitionID] > 0 {
b.pollerCounts[partitionID]--
}
}
func (t *pollToken) Release() {
if t.balancer != nil {
// t.balancer == nil is valid for example sticky task queue.
t.balancer.Release(t.partitionID)
}
}
func (t *pollToken) GetFullName() string {
return t.fullName
}