Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79a572d
commit 0457a6d
Showing
8 changed files
with
732 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
// 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" | ||
"errors" | ||
"fmt" | ||
|
||
enumspb "go.temporal.io/api/enums/v1" | ||
|
||
carchiver "go.temporal.io/server/common/archiver" | ||
"go.temporal.io/server/common/log" | ||
"go.temporal.io/server/common/log/tag" | ||
"go.temporal.io/server/common/metrics" | ||
"go.temporal.io/server/common/persistence" | ||
"go.temporal.io/server/common/primitives" | ||
"go.temporal.io/server/service/history/archival" | ||
"go.temporal.io/server/service/history/queues" | ||
"go.temporal.io/server/service/history/shard" | ||
"go.temporal.io/server/service/history/tasks" | ||
"go.temporal.io/server/service/history/workflow" | ||
) | ||
|
||
type archivalQueueTaskExecutor struct { | ||
archiver archival.Archiver | ||
shardContext shard.Context | ||
workflowCache workflow.Cache | ||
logger log.Logger | ||
metricsHandler metrics.MetricsHandler | ||
} | ||
|
||
func newArchivalQueueTaskExecutor( | ||
archiver archival.Archiver, | ||
shardContext shard.Context, | ||
workflowCache workflow.Cache, | ||
metricsHandler metrics.MetricsHandler, | ||
logger log.Logger, | ||
) *archivalQueueTaskExecutor { | ||
return &archivalQueueTaskExecutor{ | ||
archiver: archiver, | ||
shardContext: shardContext, | ||
workflowCache: workflowCache, | ||
logger: logger, | ||
metricsHandler: metricsHandler, | ||
} | ||
} | ||
|
||
func (e *archivalQueueTaskExecutor) Execute( | ||
ctx context.Context, | ||
executable queues.Executable, | ||
) (tags []metrics.Tag, | ||
isActive bool, err error) { | ||
task := executable.GetTask() | ||
taskType := queues.GetArchivalTaskTypeTagValue(task) | ||
tags = []metrics.Tag{ | ||
getNamespaceTagByID(e.shardContext.GetNamespaceRegistry(), task.GetNamespaceID()), | ||
metrics.TaskTypeTag(taskType), | ||
} | ||
switch task := task.(type) { | ||
case *tasks.ArchiveExecutionTask: | ||
err = e.processArchiveExecutionTask(ctx, task) | ||
default: | ||
err = fmt.Errorf("task with invalid type sent to archival queue: %+v", task) | ||
} | ||
return tags, true, err | ||
} | ||
|
||
func (e *archivalQueueTaskExecutor) processArchiveExecutionTask(ctx context.Context, task *tasks.ArchiveExecutionTask) (err error) { | ||
weContext, release, err := getWorkflowExecutionContextForTask(ctx, e.workflowCache, task) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { release(err) }() | ||
|
||
mutableState, err := weContext.LoadMutableState(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if mutableState == nil || mutableState.IsWorkflowExecutionRunning() { | ||
return nil | ||
} | ||
lastWriteVersion, err := mutableState.GetLastWriteVersion() | ||
if err != nil { | ||
return err | ||
} | ||
namespaceEntry := mutableState.GetNamespaceEntry() | ||
err = CheckTaskVersion( | ||
e.shardContext, | ||
e.logger, | ||
namespaceEntry, | ||
lastWriteVersion, | ||
task.Version, | ||
task, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
namespaceName := namespaceEntry.Name() | ||
nextEventID := mutableState.GetNextEventID() | ||
executionInfo := mutableState.GetExecutionInfo() | ||
workflowTypeName := executionInfo.GetWorkflowTypeName() | ||
|
||
executionState := mutableState.GetExecutionState() | ||
memo := getWorkflowMemo(copyMemo(executionInfo.Memo)) | ||
closeTime, err := mutableState.GetWorkflowCloseTime(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if closeTime == nil { | ||
return errors.New("can't archive workflow with nil close time") | ||
} | ||
branchToken, err := mutableState.GetCurrentBranchToken() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var targets []archival.Target | ||
if e.shardContext.GetArchivalMetadata().GetVisibilityConfig().ClusterConfiguredForArchival() && | ||
namespaceEntry.VisibilityArchivalState().State == enumspb.ARCHIVAL_STATE_ENABLED { | ||
targets = append(targets, archival.TargetVisibility) | ||
} | ||
if e.shardContext.GetArchivalMetadata().GetHistoryConfig().ClusterConfiguredForArchival() && | ||
namespaceEntry.HistoryArchivalState().State == enumspb.ARCHIVAL_STATE_ENABLED { | ||
targets = append(targets, archival.TargetHistory) | ||
} | ||
|
||
historyURI, err := carchiver.NewURI(namespaceEntry.HistoryArchivalState().URI) | ||
if err != nil { | ||
e.logger.Error( | ||
"Failed to parse history URI.", | ||
tag.ArchivalURI(namespaceEntry.HistoryArchivalState().URI), | ||
tag.Error(err), | ||
) | ||
return nil | ||
} | ||
visibilityURI, err := carchiver.NewURI(namespaceEntry.VisibilityArchivalState().URI) | ||
if err != nil { | ||
e.logger.Error( | ||
"Failed to parse visibility URI.", | ||
tag.ArchivalURI(namespaceEntry.VisibilityArchivalState().URI), | ||
tag.Error(err), | ||
) | ||
return nil | ||
} | ||
_, err = e.archiver.Archive(ctx, &archival.Request{ | ||
ShardID: e.shardContext.GetShardID(), | ||
NamespaceID: task.NamespaceID, | ||
Namespace: namespaceName.String(), | ||
WorkflowID: task.WorkflowID, | ||
RunID: task.RunID, | ||
BranchToken: branchToken, | ||
NextEventID: nextEventID, | ||
CloseFailoverVersion: lastWriteVersion, | ||
HistoryURI: historyURI, | ||
VisibilityURI: visibilityURI, | ||
WorkflowTypeName: workflowTypeName, | ||
StartTime: executionInfo.GetStartTime(), | ||
ExecutionTime: executionInfo.GetExecutionTime(), | ||
CloseTime: closeTime, | ||
Status: executionState.Status, | ||
HistoryLength: nextEventID - 1, | ||
Memo: memo, | ||
SearchAttributes: getSearchAttributes(copySearchAttributes(executionInfo.SearchAttributes)), | ||
Targets: targets, | ||
CallerService: primitives.HistoryService, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
taskGenerator := workflow.NewTaskGenerator(e.shardContext.GetNamespaceRegistry(), mutableState, | ||
e.shardContext.GetConfig()) | ||
err = taskGenerator.GenerateDeleteHistoryEventTask(*closeTime, true) | ||
if err != nil { | ||
return err | ||
} | ||
err = e.shardContext.AddTasks(ctx, &persistence.AddHistoryTasksRequest{ | ||
ShardID: e.shardContext.GetShardID(), | ||
NamespaceID: namespaceEntry.ID().String(), | ||
WorkflowID: task.WorkflowID, | ||
RunID: task.RunID, | ||
Tasks: mutableState.PopTasks(), | ||
}) | ||
return err | ||
} |
Oops, something went wrong.