Files
tasks/common/persistence/health_signal_aggregator.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

179 lines
5.0 KiB
Go

package persistence
import (
"sync"
"sync/atomic"
"time"
"go.temporal.io/server/common"
"go.temporal.io/server/common/aggregate"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
)
const (
emitMetricsInterval = 30 * time.Second
)
type (
HealthSignalAggregator interface {
Record(callerSegment int32, namespace string, latency time.Duration, err error)
AverageLatency() float64
ErrorRatio() float64
Start()
Stop()
}
HealthSignalAggregatorImpl struct {
status int32
shutdownCh chan struct{}
// map of shardID -> map of namespace -> request count
requestCounts map[int32]map[string]int64
requestsLock sync.Mutex
aggregationEnabled bool
latencyAverage aggregate.MovingWindowAverage
errorRatio aggregate.MovingWindowAverage
metricsHandler metrics.Handler
emitMetricsTimer *time.Ticker
perShardRPSWarnLimit dynamicconfig.IntPropertyFn
perShardPerNsRPSWarnLimit dynamicconfig.FloatPropertyFn
logger log.Logger
}
)
func NewHealthSignalAggregatorImpl(
aggregationEnabled bool,
windowSize time.Duration,
maxBufferSize int,
metricsHandler metrics.Handler,
perShardRPSWarnLimit dynamicconfig.IntPropertyFn,
perShardPerNsRPSWarnLimit dynamicconfig.FloatPropertyFn,
logger log.Logger,
) *HealthSignalAggregatorImpl {
ret := &HealthSignalAggregatorImpl{
status: common.DaemonStatusInitialized,
shutdownCh: make(chan struct{}),
requestCounts: make(map[int32]map[string]int64),
metricsHandler: metricsHandler,
emitMetricsTimer: time.NewTicker(emitMetricsInterval),
perShardRPSWarnLimit: perShardRPSWarnLimit,
perShardPerNsRPSWarnLimit: perShardPerNsRPSWarnLimit,
logger: logger,
aggregationEnabled: aggregationEnabled,
}
if aggregationEnabled {
ret.latencyAverage = aggregate.NewMovingWindowAvgImpl(windowSize, maxBufferSize)
ret.errorRatio = aggregate.NewMovingWindowAvgImpl(windowSize, maxBufferSize)
} else {
ret.latencyAverage = aggregate.NoopMovingWindowAverage
ret.errorRatio = aggregate.NoopMovingWindowAverage
}
return ret
}
func (s *HealthSignalAggregatorImpl) Start() {
if !atomic.CompareAndSwapInt32(&s.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
go s.emitMetricsLoop()
}
func (s *HealthSignalAggregatorImpl) Stop() {
if !atomic.CompareAndSwapInt32(&s.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
return
}
close(s.shutdownCh)
s.emitMetricsTimer.Stop()
}
func (s *HealthSignalAggregatorImpl) Record(callerSegment int32, namespace string, latency time.Duration, err error) {
if s.aggregationEnabled {
s.latencyAverage.Record(latency.Milliseconds())
if isUnhealthyError(err) {
s.errorRatio.Record(1)
} else {
s.errorRatio.Record(0)
}
}
if callerSegment != CallerSegmentMissing {
s.incrementShardRequestCount(callerSegment, namespace)
}
}
func (s *HealthSignalAggregatorImpl) AverageLatency() float64 {
return s.latencyAverage.Average()
}
func (s *HealthSignalAggregatorImpl) ErrorRatio() float64 {
return s.errorRatio.Average()
}
func (s *HealthSignalAggregatorImpl) incrementShardRequestCount(shardID int32, namespace string) {
s.requestsLock.Lock()
defer s.requestsLock.Unlock()
if s.requestCounts[shardID] == nil {
s.requestCounts[shardID] = make(map[string]int64)
}
s.requestCounts[shardID][namespace]++
}
func (s *HealthSignalAggregatorImpl) emitMetricsLoop() {
for {
select {
case <-s.shutdownCh:
return
case <-s.emitMetricsTimer.C:
s.requestsLock.Lock()
requestCounts := s.requestCounts
s.requestCounts = make(map[int32]map[string]int64, len(requestCounts))
s.requestsLock.Unlock()
for shardID, requestCountPerNS := range requestCounts {
shardRequestCount := int64(0)
for namespace, count := range requestCountPerNS {
shardRequestCount += count
shardRPSPerNS := int64(float64(count) / emitMetricsInterval.Seconds())
if s.perShardPerNsRPSWarnLimit() > 0.0 && shardRPSPerNS > int64(s.perShardPerNsRPSWarnLimit()*float64(s.perShardRPSWarnLimit())) {
s.logger.Warn("Per shard per namespace RPS warn limit exceeded", tag.ShardID(shardID), tag.WorkflowNamespace(namespace), tag.RPS(shardRPSPerNS))
}
}
shardRPS := int64(float64(shardRequestCount) / emitMetricsInterval.Seconds())
s.metricsHandler.Histogram(metrics.PersistenceShardRPS.Name(), metrics.PersistenceShardRPS.Unit()).Record(shardRPS)
if shardRPS > int64(s.perShardRPSWarnLimit()) {
s.logger.Warn("Per shard RPS warn limit exceeded", tag.ShardID(shardID), tag.RPS(shardRPS))
}
}
}
}
}
func isUnhealthyError(err error) bool {
if err == nil {
return false
}
if common.IsContextCanceledErr(err) {
return true
}
if common.IsContextDeadlineExceededErr(err) {
return true
}
switch err.(type) {
case *AppendHistoryTimeoutError,
*TimeoutError:
return true
}
return false
}