Files
tasks/service/history/timer_queue_factory.go
1b9e81198e Clean inactive namespace channels from InterleavedWeightedRoundRobinScheduler (#6718)
## What changed?
Clean inactive namespace channels from
InterleavedWeightedRoundRobinScheduler after a specified delay.
This delay can be specified through the dynamic config option
history.taskSchedulerInactiveNamespaceChannelDeletionDelay.

## Why?
Older namespace stay in the flattened list forever. This is making task
scheduler slow.

## How did you test it?
Unit test

## Potential risks

## Documentation

## Is hotfix candidate?
No

---------

Co-authored-by: Yichao Yang <[email protected]>
2024-11-02 01:07:55 +00:00

279 lines
8.3 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 history
import (
"context"
historypb "go.temporal.io/api/history/v1"
historyspb "go.temporal.io/server/api/history/v1"
"go.temporal.io/server/client"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence/visibility/manager"
"go.temporal.io/server/common/resource"
"go.temporal.io/server/common/xdc"
deletemanager "go.temporal.io/server/service/history/deletemanager"
"go.temporal.io/server/service/history/queues"
"go.temporal.io/server/service/history/replication/eventhandler"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
wcache "go.temporal.io/server/service/history/workflow/cache"
"go.uber.org/fx"
)
const (
timerQueuePersistenceMaxRPSRatio = 0.3
)
type (
timerQueueFactoryParams struct {
fx.In
QueueFactoryBaseParams
ClientBean client.Bean
MatchingRawClient resource.MatchingRawClient
VisibilityManager manager.VisibilityManager
}
timerQueueFactory struct {
timerQueueFactoryParams
QueueFactoryBase
}
)
func NewTimerQueueFactory(
params timerQueueFactoryParams,
) QueueFactory {
return &timerQueueFactory{
timerQueueFactoryParams: params,
QueueFactoryBase: QueueFactoryBase{
HostScheduler: queues.NewScheduler(
params.ClusterMetadata.GetCurrentClusterName(),
queues.SchedulerOptions{
WorkerCount: params.Config.TimerProcessorSchedulerWorkerCount,
ActiveNamespaceWeights: params.Config.TimerProcessorSchedulerActiveRoundRobinWeights,
StandbyNamespaceWeights: params.Config.TimerProcessorSchedulerStandbyRoundRobinWeights,
InactiveNamespaceDeletionDelay: params.Config.TaskSchedulerInactiveChannelDeletionDelay,
},
params.NamespaceRegistry,
params.Logger,
),
HostPriorityAssigner: queues.NewPriorityAssigner(),
HostReaderRateLimiter: queues.NewReaderPriorityRateLimiter(
NewHostRateLimiterRateFn(
params.Config.TimerProcessorMaxPollHostRPS,
params.Config.PersistenceMaxQPS,
timerQueuePersistenceMaxRPSRatio,
),
int64(params.Config.TimerQueueMaxReaderCount()),
),
},
}
}
func (f *timerQueueFactory) CreateQueue(
shardContext shard.Context,
workflowCache wcache.Cache,
) queues.Queue {
logger := log.With(shardContext.GetLogger(), tag.ComponentTimerQueue)
metricsHandler := f.MetricsHandler.WithTags(metrics.OperationTag(metrics.OperationTimerQueueProcessorScope))
currentClusterName := f.ClusterMetadata.GetCurrentClusterName()
workflowDeleteManager := deletemanager.NewDeleteManager(
shardContext,
workflowCache,
f.Config,
shardContext.GetTimeSource(),
f.VisibilityManager,
)
var shardScheduler = f.HostScheduler
if f.Config.TaskSchedulerEnableRateLimiter() {
shardScheduler = queues.NewRateLimitedScheduler(
f.HostScheduler,
queues.RateLimitedSchedulerOptions{
EnableShadowMode: f.Config.TaskSchedulerEnableRateLimiterShadowMode,
StartupDelay: f.Config.TaskSchedulerRateLimiterStartupDelay,
},
currentClusterName,
f.NamespaceRegistry,
f.SchedulerRateLimiter,
f.TimeSource,
logger,
metricsHandler,
)
}
rescheduler := queues.NewRescheduler(
shardScheduler,
shardContext.GetTimeSource(),
logger,
metricsHandler,
)
activeExecutor := newTimerQueueActiveTaskExecutor(
shardContext,
workflowCache,
workflowDeleteManager,
logger,
f.MetricsHandler,
f.Config,
f.MatchingRawClient,
)
eventImporter := eventhandler.NewEventImporter(
f.RemoteHistoryFetcher,
func(ctx context.Context, namespaceID namespace.ID, workflowID string) (shard.Engine, error) {
return shardContext.GetEngine(ctx)
},
f.Serializer,
logger,
)
resendHandler := eventhandler.NewResendHandler(
f.NamespaceRegistry,
f.ClientBean,
f.Serializer,
f.ClusterMetadata,
func(ctx context.Context, namespaceID namespace.ID, workflowID string) (shard.Engine, error) {
return shardContext.GetEngine(ctx)
},
f.RemoteHistoryFetcher,
eventImporter,
logger,
f.Config,
)
standbyExecutor := newTimerQueueStandbyTaskExecutor(
shardContext,
workflowCache,
workflowDeleteManager,
xdc.NewNDCHistoryResender(
shardContext.GetNamespaceRegistry(),
f.ClientBean,
func(
ctx context.Context,
sourceClusterName string,
namespaceId namespace.ID,
workflowId string,
runId string,
events [][]*historypb.HistoryEvent,
versionHistory []*historyspb.VersionHistoryItem,
) error {
engine, err := shardContext.GetEngine(ctx)
if err != nil {
return err
}
return engine.ReplicateHistoryEvents(
ctx,
definition.WorkflowKey{
NamespaceID: namespaceId.String(),
WorkflowID: workflowId,
RunID: runId,
},
nil,
versionHistory,
events,
nil,
"",
)
},
shardContext.GetPayloadSerializer(),
f.Config.StandbyTaskReReplicationContextTimeout,
logger,
f.Config,
),
resendHandler,
f.MatchingRawClient,
logger,
f.MetricsHandler,
// note: the cluster name is for calculating time for standby tasks,
// here we are basically using current cluster time
// this field will be deprecated soon, currently exists so that
// we have the option of revert to old behavior
currentClusterName,
f.Config,
)
executor := queues.NewActiveStandbyExecutor(
currentClusterName,
f.NamespaceRegistry,
activeExecutor,
standbyExecutor,
logger,
)
if f.ExecutorWrapper != nil {
executor = f.ExecutorWrapper.Wrap(executor)
}
factory := queues.NewExecutableFactory(
executor,
shardScheduler,
rescheduler,
f.HostPriorityAssigner,
shardContext.GetTimeSource(),
shardContext.GetNamespaceRegistry(),
shardContext.GetClusterMetadata(),
logger,
metricsHandler,
f.DLQWriter,
f.Config.TaskDLQEnabled,
f.Config.TaskDLQUnexpectedErrorAttempts,
f.Config.TaskDLQInternalErrors,
f.Config.TaskDLQErrorPattern,
)
return queues.NewScheduledQueue(
shardContext,
tasks.CategoryTimer,
shardScheduler,
rescheduler,
factory,
&queues.Options{
ReaderOptions: queues.ReaderOptions{
BatchSize: f.Config.TimerTaskBatchSize,
MaxPendingTasksCount: f.Config.QueuePendingTaskMaxCount,
PollBackoffInterval: f.Config.TimerProcessorPollBackoffInterval,
MaxPredicateSize: f.Config.QueueMaxPredicateSize,
},
MonitorOptions: queues.MonitorOptions{
PendingTasksCriticalCount: f.Config.QueuePendingTaskCriticalCount,
ReaderStuckCriticalAttempts: f.Config.QueueReaderStuckCriticalAttempts,
SliceCountCriticalThreshold: f.Config.QueueCriticalSlicesCount,
},
MaxPollRPS: f.Config.TimerProcessorMaxPollRPS,
MaxPollInterval: f.Config.TimerProcessorMaxPollInterval,
MaxPollIntervalJitterCoefficient: f.Config.TimerProcessorMaxPollIntervalJitterCoefficient,
CheckpointInterval: f.Config.TimerProcessorUpdateAckInterval,
CheckpointIntervalJitterCoefficient: f.Config.TimerProcessorUpdateAckIntervalJitterCoefficient,
MaxReaderCount: f.Config.TimerQueueMaxReaderCount,
},
f.HostReaderRateLimiter,
logger,
metricsHandler,
)
}