Files
tasks/client/matching/loadbalancer_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

166 lines
5.2 KiB
Go

package matching
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/server/common/tqid"
)
func TestTQLoadBalancerMapping(t *testing.T) {
lb := &defaultLoadBalancer{
lock: sync.RWMutex{},
taskQueueLBs: make(map[tqid.TaskQueue]*tqLoadBalancer),
}
f, err := tqid.NewTaskQueueFamily("fake-namespace-id", "fake-taskqueue")
assert.NoError(t, err)
taskQueue := f.TaskQueue(enumspb.TASK_QUEUE_TYPE_WORKFLOW)
tqlb := lb.getTaskQueueLoadBalancer(taskQueue)
tqlb2 := lb.getTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_WORKFLOW))
assert.Equal(t, tqlb2, tqlb, "mapping should be based on content, not the pointer value")
taskQueueClone := *taskQueue
tqlb2 = lb.getTaskQueueLoadBalancer(&taskQueueClone)
assert.Equal(t, tqlb2, tqlb, "mapping should be based on content, not the pointer value")
tqlb3 := lb.getTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_ACTIVITY))
assert.NotEqual(t, tqlb3, tqlb, "separate load LB should be created for each task type")
}
func TestTQLoadBalancer(t *testing.T) {
partitionCount := 4
f, err := tqid.NewTaskQueueFamily("fake-namespace-id", "fake-taskqueue")
assert.NoError(t, err)
tqlb := newTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_ACTIVITY))
// pick 4 times, each partition picked would have one poller
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
p3 := tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
// release one, and pick one, the newly picked one should have one poller
p3.Release()
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
// pick one again, this time it should have 2 pollers
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 2, maxPollerCount(tqlb))
}
func TestTQLoadBalancerForce(t *testing.T) {
partitionCount := 4
f, err := tqid.NewTaskQueueFamily("fake-namespace-id", "fake-taskqueue")
assert.NoError(t, err)
tqlb := newTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_ACTIVITY))
// pick 4 times, each partition picked would have one poller
p1 := tqlb.forceReadPartition(partitionCount, 1)
assert.Equal(t, 1, p1.TQPartition.PartitionId())
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.forceReadPartition(partitionCount, 1)
assert.Equal(t, 2, maxPollerCount(tqlb))
// when we don't force it should balance out
tqlb.pickReadPartition(partitionCount)
tqlb.pickReadPartition(partitionCount)
tqlb.pickReadPartition(partitionCount)
tqlb.pickReadPartition(partitionCount)
tqlb.pickReadPartition(partitionCount)
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 2, maxPollerCount(tqlb))
// releasing the forced one and adding another should still be balanced
p1.Release()
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 2, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 3, maxPollerCount(tqlb))
}
func TestLoadBalancerConcurrent(t *testing.T) {
wg := &sync.WaitGroup{}
partitionCount := 4
f, err := tqid.NewTaskQueueFamily("fake-namespace-id", "fake-taskqueue")
assert.NoError(t, err)
tqlb := newTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_ACTIVITY))
concurrentCount := 10 * partitionCount
wg.Add(concurrentCount)
for i := 0; i < concurrentCount; i++ {
go func() {
defer wg.Done()
tqlb.pickReadPartition(partitionCount)
}()
}
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
f, err := tqid.NewTaskQueueFamily("fake-namespace-id", "fake-taskqueue")
assert.NoError(t, err)
tqlb := newTaskQueueLoadBalancer(f.TaskQueue(enumspb.TASK_QUEUE_TYPE_ACTIVITY))
p1 := tqlb.pickReadPartition(partitionCount)
p2 := tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
partitionCount += 2 // increase partition count
p3 := tqlb.pickReadPartition(partitionCount)
p4 := tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
partitionCount -= 2 // reduce partition count
p5 := tqlb.pickReadPartition(partitionCount)
p6 := tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 2, maxPollerCount(tqlb))
assert.Equal(t, 2, maxPollerCount(tqlb))
p7 := tqlb.pickReadPartition(partitionCount)
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)
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 1, maxPollerCount(tqlb))
assert.Equal(t, 1, maxPollerCount(tqlb))
tqlb.pickReadPartition(partitionCount)
assert.Equal(t, 2, maxPollerCount(tqlb))
}
func maxPollerCount(tqlb *tqLoadBalancer) int {
res := -1
for _, c := range tqlb.pollerCounts {
if c > res {
res = c
}
}
return res
}