Files
tasks/service/history/transferQueueProcessorBase.go

153 lines
4.6 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"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
)
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) {
response, err := t.executionManager.GetHistoryTasks(context.TODO(), &persistence.GetHistoryTasksRequest{
ShardID: t.shard.GetShardID(),
TaskCategory: tasks.CategoryTransfer,
InclusiveMinTaskKey: tasks.Key{
TaskID: readLevel + 1,
},
ExclusiveMaxTaskKey: tasks.Key{
TaskID: 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 getTransferTaskMetricsScope(
task tasks.Task,
isActive bool,
) int {
switch task.(type) {
case *tasks.ActivityTask:
if isActive {
return metrics.TransferActiveTaskActivityScope
}
return metrics.TransferStandbyTaskActivityScope
case *tasks.WorkflowTask:
if isActive {
return metrics.TransferActiveTaskWorkflowTaskScope
}
return metrics.TransferStandbyTaskWorkflowTaskScope
case *tasks.CloseExecutionTask:
if isActive {
return metrics.TransferActiveTaskCloseExecutionScope
}
return metrics.TransferStandbyTaskCloseExecutionScope
case *tasks.CancelExecutionTask:
if isActive {
return metrics.TransferActiveTaskCancelExecutionScope
}
return metrics.TransferStandbyTaskCancelExecutionScope
case *tasks.SignalExecutionTask:
if isActive {
return metrics.TransferActiveTaskSignalExecutionScope
}
return metrics.TransferStandbyTaskSignalExecutionScope
case *tasks.StartChildExecutionTask:
if isActive {
return metrics.TransferActiveTaskStartChildExecutionScope
}
return metrics.TransferStandbyTaskStartChildExecutionScope
case *tasks.ResetWorkflowTask:
if isActive {
return metrics.TransferActiveTaskResetWorkflowScope
}
return metrics.TransferStandbyTaskResetWorkflowScope
default:
if isActive {
return metrics.TransferActiveQueueProcessorScope
}
return metrics.TransferStandbyQueueProcessorScope
}
}