mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
## What changed? - Enforce archetype when loading mutable state ## Why? - Avoid workflow operation get applied to non-workflow archetypes. ## How did you test it? - [x] built - [ ] run locally and tested manually - [x] covered by existing tests - [ ] added new unit test(s) - [ ] added new functional test(s)
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package replication
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.temporal.io/server/api/historyservice/v1"
|
|
"go.temporal.io/server/chasm"
|
|
"go.temporal.io/server/common/definition"
|
|
"go.temporal.io/server/common/locks"
|
|
"go.temporal.io/server/common/namespace"
|
|
"go.temporal.io/server/common/persistence"
|
|
"go.temporal.io/server/service/history/api"
|
|
historyi "go.temporal.io/server/service/history/interfaces"
|
|
"go.temporal.io/server/service/history/tasks"
|
|
)
|
|
|
|
func GenerateTask(
|
|
ctx context.Context,
|
|
request *historyservice.GenerateLastHistoryReplicationTasksRequest,
|
|
shard historyi.ShardContext,
|
|
workflowConsistencyChecker api.WorkflowConsistencyChecker,
|
|
) (_ *historyservice.GenerateLastHistoryReplicationTasksResponse, retError error) {
|
|
namespaceEntry, err := api.GetNamespace(shard, namespace.ID(request.GetNamespaceId()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
namespaceID := namespaceEntry.ID()
|
|
|
|
chasmLease, err := workflowConsistencyChecker.GetChasmLease(
|
|
ctx,
|
|
nil,
|
|
definition.NewWorkflowKey(
|
|
namespaceID.String(),
|
|
request.Execution.WorkflowId,
|
|
request.Execution.RunId,
|
|
),
|
|
chasm.ArchetypeAny, // GenerateMigrationTasks works for all Archetypes.
|
|
locks.PriorityHigh,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { chasmLease.GetReleaseFn()(retError) }()
|
|
|
|
mutableState := chasmLease.GetMutableState()
|
|
replicationTasks, stateTransitionCount, err := mutableState.GenerateMigrationTasks(request.GetTargetClusters())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = shard.AddTasks(ctx, &persistence.AddHistoryTasksRequest{
|
|
ShardID: shard.GetShardID(),
|
|
// RangeID is set by shard
|
|
NamespaceID: string(namespaceID),
|
|
WorkflowID: request.Execution.WorkflowId,
|
|
Tasks: map[tasks.Category][]tasks.Task{
|
|
tasks.CategoryReplication: replicationTasks,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
historyLength := max(mutableState.GetNextEventID()-1, 0)
|
|
return &historyservice.GenerateLastHistoryReplicationTasksResponse{
|
|
StateTransitionCount: stateTransitionCount,
|
|
HistoryLength: historyLength,
|
|
}, nil
|
|
}
|