Files
tasks/service/history/api/replication/generate_task.go
Yichao YangandGitHub 1ea3b7d5e7 CHASM: Enforce archetype check (#8011)
## 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)
2025-07-17 15:39:01 -07:00

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
}