Files
tasks/service/history/replication/executable_sync_hsm_task.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

201 lines
5.4 KiB
Go

package replication
import (
"context"
"errors"
"time"
"go.temporal.io/api/serviceerror"
enumsspb "github.com/hanzoai/tasks/api/enums/v1"
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
replicationspb "github.com/hanzoai/tasks/api/replication/v1"
"github.com/hanzoai/tasks/common/definition"
"github.com/hanzoai/tasks/common/headers"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/metrics"
"github.com/hanzoai/tasks/common/namespace"
serviceerrors "github.com/hanzoai/tasks/common/serviceerror"
ctasks "github.com/hanzoai/tasks/common/tasks"
"github.com/hanzoai/tasks/service/history/consts"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"google.golang.org/protobuf/types/known/timestamppb"
)
// This is mostly copied from ExecutableActivityStateTask
// The 4 replication executable task implemenatations are quite similar
// we may want to do some refactoring later.
type (
ExecutableSyncHSMTask struct {
ProcessToolBox
definition.WorkflowKey
ExecutableTask
taskAttr *replicationspb.SyncHSMAttributes
}
)
var _ ctasks.Task = (*ExecutableSyncHSMTask)(nil)
var _ TrackableExecutableTask = (*ExecutableSyncHSMTask)(nil)
// var _ BatchableTask = (*ExecutableSyncHSMTask)(nil)
func NewExecutableSyncHSMTask(
processToolBox ProcessToolBox,
taskID int64,
taskCreationTime time.Time,
task *replicationspb.SyncHSMAttributes,
sourceClusterName string,
sourceShardKey ClusterShardKey,
replicationTask *replicationspb.ReplicationTask,
) *ExecutableSyncHSMTask {
return &ExecutableSyncHSMTask{
ProcessToolBox: processToolBox,
WorkflowKey: definition.NewWorkflowKey(task.NamespaceId, task.WorkflowId, task.RunId),
ExecutableTask: NewExecutableTask(
processToolBox,
taskID,
metrics.SyncHSMTaskScope,
taskCreationTime,
time.Now().UTC(),
sourceClusterName,
sourceShardKey,
replicationTask,
),
taskAttr: task,
}
}
func (e *ExecutableSyncHSMTask) QueueID() any {
return e.WorkflowKey
}
func (e *ExecutableSyncHSMTask) Execute() error {
if e.TerminalState() {
return nil
}
e.MarkExecutionStart()
callerInfo := getReplicaitonCallerInfo(e.GetPriority())
namespaceName, apply, nsError := e.GetNamespaceInfo(headers.SetCallerInfo(
context.Background(),
callerInfo,
), e.NamespaceID, e.WorkflowID)
if nsError != nil {
return nsError
} else if !apply {
e.Logger.Warn("Skipping the replication task",
tag.WorkflowNamespaceID(e.NamespaceID),
tag.WorkflowID(e.WorkflowID),
tag.WorkflowRunID(e.RunID),
tag.TaskID(e.ExecutableTask.TaskID()),
)
metrics.ReplicationTasksSkipped.With(e.MetricsHandler).Record(
1,
metrics.OperationTag(metrics.SyncHSMTaskScope),
metrics.NamespaceTag(namespaceName),
)
return nil
}
ctx, cancel := newTaskContext(namespaceName, e.Config.ReplicationTaskApplyTimeout(), callerInfo)
defer cancel()
shardContext, err := e.ShardController.GetShardByNamespaceWorkflow(
namespace.ID(e.NamespaceID),
e.WorkflowID,
)
if err != nil {
return err
}
engine, err := shardContext.GetEngine(ctx)
if err != nil {
return err
}
return engine.SyncHSM(ctx, &historyi.SyncHSMRequest{
WorkflowKey: e.WorkflowKey,
StateMachineNode: e.taskAttr.StateMachineNode,
EventVersionHistory: e.taskAttr.VersionHistory,
})
}
func (e *ExecutableSyncHSMTask) HandleErr(err error) error {
metrics.ReplicationTasksErrorByType.With(e.MetricsHandler).Record(
1,
metrics.OperationTag(metrics.SyncHSMTaskScope),
metrics.NamespaceTag(e.NamespaceName()),
metrics.ServiceErrorTypeTag(err),
)
if errors.Is(err, consts.ErrDuplicate) {
e.MarkTaskDuplicated()
return nil
}
callerInfo := getReplicaitonCallerInfo(e.GetPriority())
switch retryErr := err.(type) {
case nil, *serviceerror.NotFound:
return nil
case *serviceerrors.RetryReplication:
namespaceName, _, nsError := e.GetNamespaceInfo(headers.SetCallerInfo(
context.Background(),
callerInfo,
), e.NamespaceID, e.WorkflowID)
if nsError != nil {
return err
}
ctx, cancel := newTaskContext(namespaceName, e.Config.ReplicationTaskApplyTimeout(), callerInfo)
defer cancel()
if doContinue, resendErr := e.Resend(
ctx,
e.ExecutableTask.SourceClusterName(),
retryErr,
ResendAttempt,
); resendErr != nil || !doContinue {
return err
}
return e.Execute()
default:
e.Logger.Error("Sync HSM replication task encountered error",
tag.WorkflowNamespaceID(e.NamespaceID),
tag.WorkflowID(e.WorkflowID),
tag.WorkflowRunID(e.RunID),
tag.TaskID(e.ExecutableTask.TaskID()),
tag.Error(err),
)
return err
}
}
func (e *ExecutableSyncHSMTask) MarkPoisonPill() error {
if e.ReplicationTask().GetRawTaskInfo() == nil {
e.ReplicationTask().RawTaskInfo = &persistencespb.ReplicationTaskInfo{
NamespaceId: e.NamespaceID,
WorkflowId: e.WorkflowID,
RunId: e.RunID,
TaskType: enumsspb.TASK_TYPE_REPLICATION_SYNC_HSM,
TaskId: e.ExecutableTask.TaskID(),
VisibilityTime: timestamppb.New(e.TaskCreationTime()),
}
}
return e.ExecutableTask.MarkPoisonPill()
}
// TODO: implement the following methods to batch syncHSM task if needed
// if not implemented the task will be treated as unbatchable
// func (e *ExecutableSyncHSMTask) BatchWith(incomingTask BatchableTask) (TrackableExecutableTask, bool) {
// panic("not implemented")
// }
// func (e *ExecutableSyncHSMTask) CanBatch() bool {
// panic("not implemented")
// }
// func (e *ExecutableSyncHSMTask) MarkUnbatchable() {
// panic("not implemented")
// }