Files
tasks/service/frontend/configs/quotas_test.go
1e31c76ed1 Versioning cherry-pick for OSS v1.30.2 (#9543)
## What changed?
Cherry-pick versioning PRs
- #9168
  - Cache for system protection
- #9262 
  - Cache for system protection
- #9239 
- Critical PR to enable sending `TargetVersionChanged` flag for
Upgrade-on-CaN feature
- #9147 
- Tracks version drainage properly when version receives workflows via
`VersioningOverride`. Needed for automated worker controllers to
correctly scale versioned workers that received workflows via
`VersioningOverride`.
- #9300 
- Needed for `approximate_backlog_count` metric to track Current and
Ramping version tasks correctly
- #9316 
- Needed for `approximate_backlog_count` metric to track Current and
Ramping version tasks correctly
- #8957 
- Contains minor metric improvement. Included because it adds a test
harness that is used in the two metrics PRs above
- #9250 
  - Bug fix of task rescheduling edge case during AutoUpgrade Transition

## Why?
For OSS v1.30.2

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





---

> [!NOTE]
> **Medium Risk**
> Touches history/matching worker-versioning paths, adding new caches
and changing workflow task/start handling and backlog metric emission;
incorrect caching or signaling could affect dispatch/upgrade behavior
and observability.
> 
> **Overview**
> Adds new worker-versioning protections and upgrade signaling: workflow
task started events now persist a
`workflow_task_target_worker_deployment_version_changed` flag (and emit
a new `workflow_target_version_changed_count` metric) under a new
`EnableSendTargetVersionChanged` dynamic config.
> 
> Introduces two new caches with metrics and dynamic config knobs: a
`RoutingInfoCache` to avoid repeated `GetTaskQueueUserData` lookups
during activity start/transition logic, and a `ReactivationSignalCache`
plus `EnableVersionReactivationSignals` to dedupe and asynchronously
send “reactivation” signals when workflows are pinned (via
start/signal-with-start/reset/update-options) to potentially
drained/inactive worker versions.
> 
> Extends matching backlog metrics to support version-attributed
reporting by adding `BacklogMetricsEmitInterval` and switching queue DB
emission to *physical* backlog gauges (`physical_approximate_backlog_*`)
when attribution is enabled, while keeping legacy gauges when disabled.
> 
> Adds frontend scaffolding for a new visibility RPC `CountSchedules`
(client plumbing, interception/metadata/quota wiring) but leaves the
frontend handler unimplemented, and bumps `go.temporal.io/api` to
`v1.62.2`.
> 
> <sup>Written by [Cursor
Bugbot]() for commit
cb8ae14c86666654c51dd66ae91cb225722e1ba7. This will update automatically
on new commits. Configure
[here]().</sup>

---------

Co-authored-by: Shahab Tajik <[email protected]>
Co-authored-by: Shivam <[email protected]>
2026-03-17 17:19:47 -05:00

212 lines
7.4 KiB
Go

package configs
import (
"reflect"
"slices"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/quotas"
"go.temporal.io/server/common/testing/temporalapi"
)
var (
testRateBurstFn = quotas.NewDefaultIncomingRateBurst(func() float64 { return 5 })
testOperatorRPSRatioFn = func() float64 { return 0.2 }
)
type (
quotasSuite struct {
suite.Suite
*require.Assertions
}
)
func TestQuotasSuite(t *testing.T) {
s := new(quotasSuite)
suite.Run(t, s)
}
func (s *quotasSuite) SetupSuite() {
}
func (s *quotasSuite) TearDownSuite() {
}
func (s *quotasSuite) SetupTest() {
s.Assertions = require.New(s.T())
}
func (s *quotasSuite) TearDownTest() {
}
func (s *quotasSuite) TestExecutionAPIToPriorityMapping() {
for _, priority := range APIToPriority {
index := slices.Index(ExecutionAPIPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestVisibilityAPIToPriorityMapping() {
for _, priority := range VisibilityAPIToPriority {
index := slices.Index(VisibilityAPIPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestNamespaceReplicationInducingAPIToPriorityMapping() {
for _, priority := range NamespaceReplicationInducingAPIToPriority {
index := slices.Index(NamespaceReplicationInducingAPIPrioritiesOrdered, priority)
s.NotEqual(-1, index)
}
}
func (s *quotasSuite) TestExecutionAPIPrioritiesOrdered() {
for idx := range ExecutionAPIPrioritiesOrdered[1:] {
s.True(ExecutionAPIPrioritiesOrdered[idx] < ExecutionAPIPrioritiesOrdered[idx+1])
}
}
func (s *quotasSuite) TestVisibilityAPIPrioritiesOrdered() {
for idx := range VisibilityAPIPrioritiesOrdered[1:] {
s.True(VisibilityAPIPrioritiesOrdered[idx] < VisibilityAPIPrioritiesOrdered[idx+1])
}
}
func (s *quotasSuite) TestNamespaceReplicationInducingAPIPrioritiesOrdered() {
for idx := range NamespaceReplicationInducingAPIPrioritiesOrdered[1:] {
s.True(NamespaceReplicationInducingAPIPrioritiesOrdered[idx] < NamespaceReplicationInducingAPIPrioritiesOrdered[idx+1])
}
}
func (s *quotasSuite) TestVisibilityAPIs() {
apis := map[string]struct{}{
"/temporal.api.workflowservice.v1.WorkflowService/GetWorkflowExecution": {},
"/temporal.api.workflowservice.v1.WorkflowService/CountWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ScanWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListOpenWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListClosedWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListArchivedWorkflowExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListWorkers": {},
"/temporal.api.workflowservice.v1.WorkflowService/DescribeWorker": {},
"/temporal.api.workflowservice.v1.WorkflowService/GetWorkerTaskReachability": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListSchedules": {},
"/temporal.api.workflowservice.v1.WorkflowService/CountSchedules": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListBatchOperations": {},
"/temporal.api.workflowservice.v1.WorkflowService/DescribeTaskQueueWithReachability": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListDeployments": {},
"/temporal.api.workflowservice.v1.WorkflowService/GetDeploymentReachability": {},
"/temporal.api.workflowservice.v1.WorkflowService/CountActivityExecutions": {},
"/temporal.api.workflowservice.v1.WorkflowService/ListActivityExecutions": {},
}
var service workflowservice.WorkflowServiceServer
t := reflect.TypeOf(&service).Elem()
apiToPriority := make(map[string]int, t.NumMethod())
for i := 0; i < t.NumMethod(); i++ {
apiName := "/temporal.api.workflowservice.v1.WorkflowService/" + t.Method(i).Name
if t.Method(i).Name == "DescribeTaskQueue" {
apiName += "WithReachability"
}
if _, ok := apis[apiName]; ok {
apiToPriority[apiName] = VisibilityAPIToPriority[apiName]
}
}
s.Equal(apiToPriority, VisibilityAPIToPriority)
}
func (s *quotasSuite) TestNamespaceReplicationInducingAPIs() {
apis := map[string]struct{}{
"/temporal.api.workflowservice.v1.WorkflowService/RegisterNamespace": {},
"/temporal.api.workflowservice.v1.WorkflowService/UpdateNamespace": {},
"/temporal.api.workflowservice.v1.WorkflowService/UpdateWorkerBuildIdCompatibility": {},
"/temporal.api.workflowservice.v1.WorkflowService/UpdateWorkerVersioningRules": {},
}
var service workflowservice.WorkflowServiceServer
t := reflect.TypeOf(&service).Elem()
apiToPriority := make(map[string]int, t.NumMethod())
for i := 0; i < t.NumMethod(); i++ {
apiName := "/temporal.api.workflowservice.v1.WorkflowService/" + t.Method(i).Name
if _, ok := apis[apiName]; ok {
apiToPriority[apiName] = NamespaceReplicationInducingAPIToPriority[apiName]
}
}
s.Equal(apiToPriority, NamespaceReplicationInducingAPIToPriority)
}
func (s *quotasSuite) TestAllAPIs() {
apisWithPriority := make(map[string]struct{})
for api := range APIToPriority {
apisWithPriority[api] = struct{}{}
}
for api := range VisibilityAPIToPriority {
apisWithPriority[api] = struct{}{}
}
for api := range NamespaceReplicationInducingAPIToPriority {
apisWithPriority[api] = struct{}{}
}
var service workflowservice.WorkflowServiceServer
temporalapi.WalkExportedMethods(&service, func(m reflect.Method) {
_, ok := apisWithPriority["/temporal.api.workflowservice.v1.WorkflowService/"+m.Name]
s.True(ok, "missing priority for API: %v", m.Name)
})
_, ok := apisWithPriority[DispatchNexusTaskByNamespaceAndTaskQueueAPIName]
s.Truef(ok, "missing priority for API: %q", DispatchNexusTaskByNamespaceAndTaskQueueAPIName)
_, ok = apisWithPriority[DispatchNexusTaskByEndpointAPIName]
s.Truef(ok, "missing priority for API: %q", DispatchNexusTaskByEndpointAPIName)
_, ok = apisWithPriority[CompleteNexusOperation]
s.Truef(ok, "missing priority for API: %q", CompleteNexusOperation)
}
func (s *quotasSuite) TestOperatorPriority_Execution() {
limiter := NewExecutionPriorityRateLimiter(testRateBurstFn, testOperatorRPSRatioFn)
s.testOperatorPrioritized(limiter, "DescribeWorkflowExecution")
}
func (s *quotasSuite) TestOperatorPriority_Visibility() {
limiter := NewVisibilityPriorityRateLimiter(testRateBurstFn, testOperatorRPSRatioFn)
s.testOperatorPrioritized(limiter, "ListOpenWorkflowExecutions")
}
func (s *quotasSuite) TestOperatorPriority_NamespaceReplicationInducing() {
limiter := NewNamespaceReplicationInducingAPIPriorityRateLimiter(testRateBurstFn, testOperatorRPSRatioFn)
s.testOperatorPrioritized(limiter, "RegisterNamespace")
}
func (s *quotasSuite) testOperatorPrioritized(limiter quotas.RequestRateLimiter, api string) {
operatorRequest := quotas.NewRequest(
api,
1,
"test-namespace",
headers.CallerTypeOperator,
-1,
"")
apiRequest := quotas.NewRequest(
api,
1,
"test-namespace",
headers.CallerTypeAPI,
-1,
"")
requestTime := time.Now()
limitCount := 0
for i := 0; i < 12; i++ {
if !limiter.Allow(requestTime, apiRequest) {
limitCount++
s.True(limiter.Allow(requestTime, operatorRequest))
}
}
s.Equal(2, limitCount)
}