Files
tasks/client/matching/loadbalancer_test.go
Shahab TajikandGitHub aff4f191eb Balance open polls across TQ partition (#4981)
<!-- Describe what has changed in this PR -->
**What changed?**
For each TQ, Matching client LB keeps track of open pollers per
partition and selects a partition with the fewest open pollers.
Forwarded polls are only counted once, for the original partition.


<!-- Tell your future self why have you made these changes -->
**Why?**
In situation where a TQ has no task to dispatch (or it cannot dispatch
for another reason) it can become a black hole for pollers. This change
prevent that to happen (assuming the number of pollers is greater than
the number of partitions)

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
Unit test
Locally ran against multiple workloads


<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**
None known

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
No
2023-10-18 00:14:57 +00:00

187 lines
5.8 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 (
"sync"
"testing"
"github.com/stretchr/testify/assert"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/server/common/tqname"
)
func TestTQLoadBalancer(t *testing.T) {
partitionCount := 4
key := taskQueueKey{
NamespaceID: "fake-namespace-id",
Name: mustParseTQName("fake-taskqueue"),
Type: enumspb.TASK_QUEUE_TYPE_ACTIVITY,
}
tqlb := newTaskQueueLoadBalancer(key)
// pick 4 times, each partition picked would have one poller
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
p3 := tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
// release one, and pick one, the newly picked one should have one poller
p3.Release()
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
// pick one again, this time it should have 2 pollers
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 2, maxPollerCount(tqlb))
}
func TestTQLoadBalancerForce(t *testing.T) {
partitionCount := 4
key := taskQueueKey{
NamespaceID: "fake-namespace-id",
Name: mustParseTQName("fake-taskqueue"),
Type: enumspb.TASK_QUEUE_TYPE_ACTIVITY,
}
tqlb := newTaskQueueLoadBalancer(key)
// pick 4 times, each partition picked would have one poller
p1 := tqlb.pickReadPartition(partitionCount, 1)
assert.Equal(t, 1, p1.partitionID)
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount, 1)
assert.Equal(t, 2, maxPollerCount(tqlb))
// when we don't force it should balance out
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 2, maxPollerCount(tqlb))
// releasing the forced one and adding another should still be balanced
p1.Release()
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 2, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 3, maxPollerCount(tqlb))
}
func TestLoadBalancerConcurrent(t *testing.T) {
wg := &sync.WaitGroup{}
partitionCount := 4
key := taskQueueKey{
NamespaceID: "fake-namespace-id",
Name: mustParseTQName("fake-taskqueue"),
Type: enumspb.TASK_QUEUE_TYPE_ACTIVITY,
}
tqlb := newTaskQueueLoadBalancer(key)
concurrentCount := 10 * partitionCount
wg.Add(concurrentCount)
for i := 0; i < concurrentCount; i++ {
go func() {
defer wg.Done()
tqlb.pickReadPartition(partitionCount, -1)
}()
}
wg.Wait()
// verify all partition have same pollers.
assert.Equal(t, 10, tqlb.pollerCounts[0])
assert.Equal(t, 10, tqlb.pollerCounts[1])
assert.Equal(t, 10, tqlb.pollerCounts[2])
assert.Equal(t, 10, tqlb.pollerCounts[3])
}
func TestLoadBalancer_ReducedPartitionCount(t *testing.T) {
partitionCount := 2
key := taskQueueKey{
NamespaceID: "fake-namespace-id",
Name: mustParseTQName("fake-taskqueue"),
Type: enumspb.TASK_QUEUE_TYPE_ACTIVITY,
}
tqlb := newTaskQueueLoadBalancer(key)
p1 := tqlb.pickReadPartition(partitionCount, -1)
p2 := tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
partitionCount += 2 // increase partition count
p3 := tqlb.pickReadPartition(partitionCount, -1)
p4 := tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
partitionCount -= 2 // reduce partition count
p5 := tqlb.pickReadPartition(partitionCount, -1)
p6 := tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 2, maxPollerCount(tqlb))
assert.Equal(t, 2, maxPollerCount(tqlb))
p7 := tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 3, maxPollerCount(tqlb))
// release all of them and it should be ok.
p1.Release()
p2.Release()
p3.Release()
p4.Release()
p5.Release()
p6.Release()
p7.Release()
tqlb.pickReadPartition(partitionCount, -1)
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount, -1)
assert.Equal(t, 2, maxPollerCount(tqlb))
}
func mustParseTQName(baseName string) tqname.Name {
n, err := tqname.Parse(baseName)
if err != nil {
panic(err)
}
return n
}
func maxPollerCount(tqlb *tqLoadBalancer) int {
res := -1
for _, c := range tqlb.pollerCounts {
if c > res {
res = c
}
}
return res
}