|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package history |
| 26 | + |
| 27 | +import ( |
| 28 | + "go.uber.org/fx" |
| 29 | + |
| 30 | + "go.temporal.io/server/common/log" |
| 31 | + "go.temporal.io/server/common/log/tag" |
| 32 | + "go.temporal.io/server/common/metrics" |
| 33 | + ctasks "go.temporal.io/server/common/tasks" |
| 34 | + "go.temporal.io/server/service/history/archival" |
| 35 | + "go.temporal.io/server/service/history/configs" |
| 36 | + "go.temporal.io/server/service/history/queues" |
| 37 | + "go.temporal.io/server/service/history/shard" |
| 38 | + "go.temporal.io/server/service/history/tasks" |
| 39 | + "go.temporal.io/server/service/history/workflow" |
| 40 | + wcache "go.temporal.io/server/service/history/workflow/cache" |
| 41 | +) |
| 42 | + |
| 43 | +const ( |
| 44 | + // archivalQueuePersistenceMaxRPSRatio is the hard-coded ratio of archival queue persistence max RPS to the total |
| 45 | + // persistence max RPS. |
| 46 | + // In this case, the archival queue may not send requests at a rate higher than 15% of the global persistence max |
| 47 | + // RPS. |
| 48 | + archivalQueuePersistenceMaxRPSRatio = 0.15 |
| 49 | +) |
| 50 | + |
| 51 | +var ( |
| 52 | + // ArchivalTaskPriorities is the map of task priority to weight for the archival queue. |
| 53 | + // The archival queue only uses the low task priority, so we only define a weight for that priority. |
| 54 | + ArchivalTaskPriorities = configs.ConvertWeightsToDynamicConfigValue(map[ctasks.Priority]int{ |
| 55 | + ctasks.PriorityLow: 10, |
| 56 | + }) |
| 57 | +) |
| 58 | + |
| 59 | +type ( |
| 60 | + // ArchivalQueueFactoryParams contains the necessary params to create a new archival queue factory. |
| 61 | + ArchivalQueueFactoryParams struct { |
| 62 | + // fx.In allows fx to construct this object without an explicitly defined constructor. |
| 63 | + fx.In |
| 64 | + |
| 65 | + // QueueFactoryBaseParams contains common params for all queue factories. |
| 66 | + QueueFactoryBaseParams |
| 67 | + // Archiver is the archival client used to archive history events and visibility records. |
| 68 | + Archiver archival.Archiver |
| 69 | + // RelocatableAttributesFetcher is the client used to fetch the memo and search attributes of a workflow. |
| 70 | + RelocatableAttributesFetcher workflow.RelocatableAttributesFetcher |
| 71 | + } |
| 72 | + |
| 73 | + // archivalQueueFactory implements QueueFactory for the archival queue. |
| 74 | + archivalQueueFactory struct { |
| 75 | + QueueFactoryBase |
| 76 | + ArchivalQueueFactoryParams |
| 77 | + } |
| 78 | +) |
| 79 | + |
| 80 | +// NewArchivalQueueFactory creates a new QueueFactory to construct archival queues. |
| 81 | +func NewArchivalQueueFactory( |
| 82 | + params ArchivalQueueFactoryParams, |
| 83 | +) QueueFactory { |
| 84 | + hostScheduler := newScheduler(params) |
| 85 | + queueFactoryBase := newQueueFactoryBase(params, hostScheduler) |
| 86 | + return &archivalQueueFactory{ |
| 87 | + ArchivalQueueFactoryParams: params, |
| 88 | + QueueFactoryBase: queueFactoryBase, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// newScheduler creates a new task scheduler for tasks on the archival queue. |
| 93 | +func newScheduler(params ArchivalQueueFactoryParams) queues.Scheduler { |
| 94 | + return queues.NewPriorityScheduler( |
| 95 | + queues.PrioritySchedulerOptions{ |
| 96 | + WorkerCount: params.Config.ArchivalProcessorSchedulerWorkerCount, |
| 97 | + EnableRateLimiter: params.Config.TaskSchedulerEnableRateLimiter, |
| 98 | + MaxDispatchThrottleDuration: HostSchedulerMaxDispatchThrottleDuration, |
| 99 | + Weight: func() map[string]any { |
| 100 | + return ArchivalTaskPriorities |
| 101 | + }, |
| 102 | + }, |
| 103 | + params.SchedulerRateLimiter, |
| 104 | + params.TimeSource, |
| 105 | + params.Logger, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +// newQueueFactoryBase creates a new QueueFactoryBase for the archival queue, which contains common configurations |
| 110 | +// like the task scheduler, task priority assigner, and rate limiters. |
| 111 | +func newQueueFactoryBase(params ArchivalQueueFactoryParams, hostScheduler queues.Scheduler) QueueFactoryBase { |
| 112 | + return QueueFactoryBase{ |
| 113 | + HostScheduler: hostScheduler, |
| 114 | + HostPriorityAssigner: queues.NewPriorityAssigner(), |
| 115 | + HostRateLimiter: NewQueueHostRateLimiter( |
| 116 | + params.Config.ArchivalProcessorMaxPollHostRPS, |
| 117 | + params.Config.PersistenceMaxQPS, |
| 118 | + archivalQueuePersistenceMaxRPSRatio, |
| 119 | + ), |
| 120 | + HostReaderRateLimiter: queues.NewReaderPriorityRateLimiter( |
| 121 | + NewHostRateLimiterRateFn( |
| 122 | + params.Config.ArchivalProcessorMaxPollHostRPS, |
| 123 | + params.Config.PersistenceMaxQPS, |
| 124 | + archivalQueuePersistenceMaxRPSRatio, |
| 125 | + ), |
| 126 | + params.Config.QueueMaxReaderCount(), |
| 127 | + ), |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// CreateQueue creates a new archival queue for the given shard. |
| 132 | +func (f *archivalQueueFactory) CreateQueue( |
| 133 | + shard shard.Context, |
| 134 | + workflowCache wcache.Cache, |
| 135 | +) queues.Queue { |
| 136 | + executor := f.newArchivalTaskExecutor(shard, workflowCache) |
| 137 | + return f.newScheduledQueue(shard, executor) |
| 138 | +} |
| 139 | + |
| 140 | +// newArchivalTaskExecutor creates a new archival task executor for the given shard. |
| 141 | +func (f *archivalQueueFactory) newArchivalTaskExecutor(shard shard.Context, workflowCache wcache.Cache) queues.Executor { |
| 142 | + return NewArchivalQueueTaskExecutor( |
| 143 | + f.Archiver, |
| 144 | + shard, |
| 145 | + workflowCache, |
| 146 | + f.RelocatableAttributesFetcher, |
| 147 | + f.MetricsHandler, |
| 148 | + f.Logger, |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +// newScheduledQueue creates a new scheduled queue for the given shard with archival-specific configurations. |
| 153 | +func (f *archivalQueueFactory) newScheduledQueue(shard shard.Context, executor queues.Executor) queues.Queue { |
| 154 | + logger := log.With(shard.GetLogger(), tag.ComponentArchivalQueue) |
| 155 | + return queues.NewScheduledQueue( |
| 156 | + shard, |
| 157 | + tasks.CategoryArchival, |
| 158 | + f.HostScheduler, |
| 159 | + f.HostPriorityAssigner, |
| 160 | + executor, |
| 161 | + &queues.Options{ |
| 162 | + ReaderOptions: queues.ReaderOptions{ |
| 163 | + BatchSize: f.Config.ArchivalTaskBatchSize, |
| 164 | + MaxPendingTasksCount: f.Config.QueuePendingTaskMaxCount, |
| 165 | + PollBackoffInterval: f.Config.ArchivalProcessorPollBackoffInterval, |
| 166 | + }, |
| 167 | + MonitorOptions: queues.MonitorOptions{ |
| 168 | + PendingTasksCriticalCount: f.Config.QueuePendingTaskCriticalCount, |
| 169 | + ReaderStuckCriticalAttempts: f.Config.QueueReaderStuckCriticalAttempts, |
| 170 | + SliceCountCriticalThreshold: f.Config.QueueCriticalSlicesCount, |
| 171 | + }, |
| 172 | + MaxPollRPS: f.Config.ArchivalProcessorMaxPollRPS, |
| 173 | + MaxPollInterval: f.Config.ArchivalProcessorMaxPollInterval, |
| 174 | + MaxPollIntervalJitterCoefficient: f.Config.ArchivalProcessorMaxPollIntervalJitterCoefficient, |
| 175 | + CheckpointInterval: f.Config.ArchivalProcessorUpdateAckInterval, |
| 176 | + CheckpointIntervalJitterCoefficient: f.Config.ArchivalProcessorUpdateAckIntervalJitterCoefficient, |
| 177 | + MaxReaderCount: f.Config.QueueMaxReaderCount, |
| 178 | + TaskMaxRetryCount: f.Config.ArchivalProcessorRetryWarningLimit, |
| 179 | + }, |
| 180 | + f.HostReaderRateLimiter, |
| 181 | + logger, |
| 182 | + f.MetricsHandler.WithTags(metrics.OperationTag(metrics.OperationArchivalQueueProcessorScope)), |
| 183 | + ) |
| 184 | +} |
0 commit comments