Files
tasks/service/history/transferQueueProcessorBase.go

131 lines
4.1 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/persistence"
"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 (
maxReadLevel func() int64
updateTransferAckLevel func(ackLevel int64) error
transferQueueShutdown func() error
transferQueueProcessorBase struct {
shard shard.Context
options *QueueProcessorOptions
executionManager persistence.ExecutionManager
maxReadLevel maxReadLevel
updateTransferAckLevel updateTransferAckLevel
transferQueueShutdown transferQueueShutdown
logger log.Logger
}
)
func newTransferQueueProcessorBase(
shard shard.Context,
options *QueueProcessorOptions,
maxReadLevel maxReadLevel,
updateTransferAckLevel updateTransferAckLevel,
transferQueueShutdown transferQueueShutdown,
logger log.Logger,
) *transferQueueProcessorBase {
return &transferQueueProcessorBase{
shard: shard,
options: options,
executionManager: shard.GetExecutionManager(),
maxReadLevel: maxReadLevel,
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.maxReadLevel()),
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 newTransferTaskShardScheduler(
shard shard.Context,
rateLimiter queues.SchedulerRateLimiter,
logger log.Logger,
) queues.Scheduler {
config := shard.GetConfig()
return queues.NewPriorityScheduler(
queues.PrioritySchedulerOptions{
WorkerCount: config.TransferTaskWorkerCount,
EnableRateLimiter: config.TaskSchedulerEnableRateLimiter,
MaxDispatchThrottleDuration: ShardSchedulerMaxDispatchThrottleDuration,
},
rateLimiter,
shard.GetTimeSource(),
logger,
)
}
func newQueueIOContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), queueIOTimeout)
ctx = headers.SetCallerInfo(ctx, headers.SystemBackgroundCallerInfo)
return ctx, cancel
}