Files
tasks/service/history/transferQueueProcessorBase.go

138 lines
4.5 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"
"time"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/persistence"
ctasks "go.temporal.io/server/common/tasks"
"go.temporal.io/server/service/history/configs"
"go.temporal.io/server/service/history/queues"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
)
const (
queueIOTimeout = 5 * time.Second
)
type (
maxReadAckLevel func() int64
updateTransferAckLevel func(ackLevel int64) error
transferQueueShutdown func() error
transferQueueProcessorBase struct {
shard shard.Context
options *QueueProcessorOptions
executionManager persistence.ExecutionManager
maxReadAckLevel maxReadAckLevel
updateTransferAckLevel updateTransferAckLevel
transferQueueShutdown transferQueueShutdown
logger log.Logger
}
)
func newTransferQueueProcessorBase(
shard shard.Context,
options *QueueProcessorOptions,
maxReadAckLevel maxReadAckLevel,
updateTransferAckLevel updateTransferAckLevel,
transferQueueShutdown transferQueueShutdown,
logger log.Logger,
) *transferQueueProcessorBase {
return &transferQueueProcessorBase{
shard: shard,
options: options,
executionManager: shard.GetExecutionManager(),
maxReadAckLevel: maxReadAckLevel,
updateTransferAckLevel: updateTransferAckLevel,
transferQueueShutdown: transferQueueShutdown,
logger: logger,
}
}
func (t *transferQueueProcessorBase) readTasks(
readLevel int64,
) ([]tasks.Task, bool, error) {
ctx, cancel := newQueueIOContext()
defer cancel()
response, err := t.executionManager.GetHistoryTasks(ctx, &persistence.GetHistoryTasksRequest{
ShardID: t.shard.GetShardID(),
TaskCategory: tasks.CategoryTransfer,
InclusiveMinTaskKey: tasks.NewImmediateKey(readLevel + 1),
ExclusiveMaxTaskKey: tasks.NewImmediateKey(t.maxReadAckLevel() + 1),
BatchSize: t.options.BatchSize(),
})
if err != nil {
return nil, false, err
}
return response.Tasks, len(response.NextPageToken) != 0, nil
}
func (t *transferQueueProcessorBase) updateAckLevel(
ackLevel int64,
) error {
return t.updateTransferAckLevel(ackLevel)
}
func (t *transferQueueProcessorBase) queueShutdown() error {
return t.transferQueueShutdown()
}
func newTransferTaskScheduler(
shard shard.Context,
logger log.Logger,
metricProvider metrics.MetricsHandler,
) queues.Scheduler {
config := shard.GetConfig()
return queues.NewScheduler(
queues.NewNoopPriorityAssigner(),
queues.SchedulerOptions{
ParallelProcessorOptions: ctasks.ParallelProcessorOptions{
WorkerCount: config.TransferTaskWorkerCount,
QueueSize: config.TransferTaskBatchSize(),
},
InterleavedWeightedRoundRobinSchedulerOptions: ctasks.InterleavedWeightedRoundRobinSchedulerOptions{
PriorityToWeight: configs.ConvertDynamicConfigValueToWeights(config.TransferProcessorSchedulerRoundRobinWeights(), logger),
},
},
metricProvider,
logger,
)
}
func newQueueIOContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), queueIOTimeout)
ctx = headers.SetCallerInfo(ctx, headers.NewCallerInfo(headers.CallerTypeBackground))
return ctx, cancel
}