mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? <!-- Describe what has changed in this PR --> Added dynamic configs to allow controlling the metrics breakdown based on task queue name, partition ID, and Build ID. Refactored all the places in the code that used these tags to consider the dc value. ## Why? <!-- Tell your future self why have you made these changes --> - Task Queue cardinality may be a problem for some self-hosted users. These configs allow to reduce cardinality. - The dynamic config is per task queue so it allows to turn it on for one or few task queues under investigation while disabled for everything else. - Simply excluding the tag value is not a good enough solution because the gauges such as backlog size or age will be mixed across task queues and become invalid. In this change, I disable the gauge completely if the configs do not allow necessary breakdown. ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> Added few unit tests. ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) -->
210 lines
7.2 KiB
Go
210 lines
7.2 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 (
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/server/common/tqid"
|
|
)
|
|
|
|
const (
|
|
// nonRootPartitionPrefix is the prefix for all mangled task queue names.
|
|
nonRootPartitionPrefix = "/_sys/"
|
|
partitionDelimiter = "/"
|
|
versionSetDelimiter = ":"
|
|
buildIdDelimiter = "#"
|
|
)
|
|
|
|
type (
|
|
// PhysicalTaskQueueKey Each task queue partition corresponds to one or more "physical" (aka DB-level) task queues,
|
|
// each of which has a distinct physicalTaskQueueManager in memory in matching service, as well as a
|
|
// distinct identity in persistence.
|
|
//
|
|
// Physical task queues with a version set or build ID are called "versioned". The ones without a version set
|
|
// or build ID are called "unversioned". A physical queue cannot have both version set and build ID.
|
|
PhysicalTaskQueueKey struct {
|
|
partition tqid.Partition
|
|
versionSet string // version set id
|
|
// BuildId and VersionSet are mutually exclusive
|
|
buildId string
|
|
}
|
|
)
|
|
|
|
var (
|
|
ErrInvalidPersistenceName = errors.New("invalid persistence name")
|
|
)
|
|
|
|
func (q *PhysicalTaskQueueKey) NamespaceId() string {
|
|
return q.partition.NamespaceId()
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) TaskQueueFamily() *tqid.TaskQueueFamily {
|
|
return q.partition.TaskQueue().Family()
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) TaskType() enumspb.TaskQueueType {
|
|
return q.partition.TaskType()
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) Partition() tqid.Partition {
|
|
return q.partition
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) BuildId() string {
|
|
return q.buildId
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) VersionSet() string {
|
|
return q.versionSet
|
|
}
|
|
|
|
// UnversionedQueueKey returns the unversioned PhysicalTaskQueueKey of a task queue partition
|
|
func UnversionedQueueKey(p tqid.Partition) *PhysicalTaskQueueKey {
|
|
return &PhysicalTaskQueueKey{
|
|
partition: p,
|
|
}
|
|
}
|
|
|
|
// VersionSetQueueKey returns a PhysicalTaskQueueKey of a task queue partition with the given version set id.
|
|
func VersionSetQueueKey(p tqid.Partition, versionSet string) *PhysicalTaskQueueKey {
|
|
return &PhysicalTaskQueueKey{
|
|
partition: p,
|
|
versionSet: versionSet,
|
|
}
|
|
}
|
|
|
|
// BuildIdQueueKey returns a PhysicalTaskQueueKey of a task queue partition with the given build ID.
|
|
func BuildIdQueueKey(p tqid.Partition, buildId string) *PhysicalTaskQueueKey {
|
|
return &PhysicalTaskQueueKey{
|
|
partition: p,
|
|
buildId: buildId,
|
|
}
|
|
}
|
|
|
|
// PersistenceName returns the unique name for this DB queue to be used in persistence.
|
|
//
|
|
// Unversioned DB use the RPC name of the partition, i.e.:
|
|
//
|
|
// sticky: <sticky name>
|
|
// unversioned and root: <base name>
|
|
// unversioned: /_sys/<base name>/<partition id>
|
|
//
|
|
// All versioned DB queues use mangled names, using the following format:
|
|
//
|
|
// with build ID: /_sys/<base name>/<build ID base64 URL encoded>#<partition id>
|
|
// with version set: /_sys/<base name>/<version set id>:<partition id>
|
|
func (q *PhysicalTaskQueueKey) PersistenceName() string {
|
|
switch p := q.Partition().(type) {
|
|
case *tqid.StickyPartition:
|
|
return p.StickyName()
|
|
case *tqid.NormalPartition:
|
|
baseName := q.TaskQueueFamily().Name()
|
|
|
|
if len(q.versionSet) > 0 {
|
|
return nonRootPartitionPrefix + baseName + partitionDelimiter + q.versionSet + versionSetDelimiter + strconv.Itoa(p.PartitionId())
|
|
}
|
|
|
|
if len(q.buildId) > 0 {
|
|
encodedBuildId := base64.URLEncoding.EncodeToString([]byte(q.buildId))
|
|
return nonRootPartitionPrefix + baseName + partitionDelimiter + encodedBuildId + buildIdDelimiter + strconv.Itoa(p.PartitionId())
|
|
}
|
|
|
|
// unversioned
|
|
if p.IsRoot() {
|
|
return baseName
|
|
}
|
|
return nonRootPartitionPrefix + baseName + partitionDelimiter + strconv.Itoa(p.PartitionId())
|
|
default:
|
|
panic("unsupported partition kind: " + p.Kind().String())
|
|
}
|
|
}
|
|
|
|
// ParsePhysicalTaskQueueKey takes the persistence name of a DB task queue and returns a PhysicalTaskQueueKey. Returns an error if the
|
|
// given name is not a valid persistence name.
|
|
func ParsePhysicalTaskQueueKey(persistenceName string, namespaceId string, taskType enumspb.TaskQueueType) (*PhysicalTaskQueueKey, error) {
|
|
baseName := persistenceName
|
|
partitionId := 0
|
|
versionSet := ""
|
|
buildId := ""
|
|
|
|
if strings.HasPrefix(persistenceName, nonRootPartitionPrefix) {
|
|
suffixOff := strings.LastIndex(persistenceName, partitionDelimiter)
|
|
if suffixOff <= len(nonRootPartitionPrefix) {
|
|
return nil, fmt.Errorf("%w: %s", ErrInvalidPersistenceName, persistenceName)
|
|
}
|
|
baseName = persistenceName[len(nonRootPartitionPrefix):suffixOff]
|
|
suffix := persistenceName[suffixOff+1:]
|
|
var err error
|
|
partitionId, versionSet, buildId, err = parseSuffix(persistenceName, suffix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
f, err := tqid.NewTaskQueueFamily(namespaceId, baseName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &PhysicalTaskQueueKey{
|
|
partition: f.TaskQueue(taskType).NormalPartition(partitionId),
|
|
versionSet: versionSet,
|
|
buildId: buildId,
|
|
}, nil
|
|
}
|
|
|
|
func parseSuffix(persistenceName string, suffix string) (partition int, versionSet string, buildId string, err error) {
|
|
if partitionOff := strings.LastIndex(suffix, buildIdDelimiter); partitionOff == 0 {
|
|
return 0, "", "", fmt.Errorf("%w: %s", ErrInvalidPersistenceName, persistenceName)
|
|
} else if partitionOff > 0 {
|
|
buildIdBytes, err := base64.URLEncoding.DecodeString(suffix[:partitionOff])
|
|
if err != nil {
|
|
return 0, "", "", fmt.Errorf("%w: %s", ErrInvalidPersistenceName, persistenceName)
|
|
}
|
|
buildId = string(buildIdBytes)
|
|
suffix = suffix[partitionOff+1:]
|
|
} else if partitionOff := strings.LastIndex(suffix, versionSetDelimiter); partitionOff == 0 {
|
|
return 0, "", "", fmt.Errorf("%w: %s", ErrInvalidPersistenceName, persistenceName)
|
|
} else if partitionOff > 0 {
|
|
// pull out version set
|
|
versionSet, suffix = suffix[:partitionOff], suffix[partitionOff+1:]
|
|
}
|
|
|
|
partition, err = strconv.Atoi(suffix)
|
|
if err != nil || partition < 0 || (partition == 0 && len(versionSet) == 0 && len(buildId) == 0) {
|
|
return 0, "", "", fmt.Errorf("%w: %s", ErrInvalidPersistenceName, persistenceName)
|
|
}
|
|
return partition, versionSet, buildId, err
|
|
}
|
|
|
|
func (q *PhysicalTaskQueueKey) IsVersioned() bool {
|
|
return q.versionSet != "" || q.buildId != ""
|
|
}
|