This is an automated email from the ASF dual-hosted git repository.
DImuthuUpe pushed a commit to branch workflow-pause-on-auth
in repository https://gitbox.apache.org/repos/asf/airavata.git
The following commit(s) were added to refs/heads/workflow-pause-on-auth by this
push:
new c8e01ced7d Further refactoting orchestration workflows
c8e01ced7d is described below
commit c8e01ced7d3ac9d72d63d226b211e16b3ff60966
Author: DImuthuUpe <[email protected]>
AuthorDate: Sat Nov 7 07:41:10 2026 -0500
Further refactoting orchestration workflows
---
api/process/repository/task_status.go | 9 ++++
api/process/service/launch.go | 3 ++
internal/app/app.go | 2 +-
internal/orchestration/engine.go | 49 ++++++++++----------
internal/orchestration/execution/tracker.go | 28 +++++++++++-
internal/orchestration/execution/tracker_test.go | 52 ++++++++++++++++++++--
internal/orchestration/tasks/batch/batch.go | 2 +-
internal/orchestration/tasks/data/data.go | 2 +-
.../orchestration/tasks/monitoring/monitoring.go | 2 +-
internal/server/server_test.go | 48 ++++++++++++++++++++
10 files changed, 166 insertions(+), 31 deletions(-)
diff --git a/api/process/repository/task_status.go
b/api/process/repository/task_status.go
index 9992ea4064..3f1002a2f3 100644
--- a/api/process/repository/task_status.go
+++ b/api/process/repository/task_status.go
@@ -76,6 +76,15 @@ func (r *TaskStatusRepository) FindByProcessID(ctx
context.Context, processID st
return out, err
}
+// HasStatus reports whether one task of one process has ever recorded status.
+func (r *TaskStatusRepository) HasStatus(ctx context.Context, taskID,
processID string, status model.TaskStatusType) (bool, error) {
+ var n int64
+ err := r.db.WithContext(ctx).Model(&model.TaskStatus{}).
+ Where("task_id = ? AND process_id = ? AND status = ?", taskID,
processID, status).
+ Count(&n).Error
+ return n > 0, err
+}
+
// Create inserts a status row. BeforeCreate assigns its id.
func (r *TaskStatusRepository) Create(ctx context.Context, s
*model.TaskStatus) error {
return r.db.WithContext(ctx).Create(s).Error
diff --git a/api/process/service/launch.go b/api/process/service/launch.go
index f2ed4bf40c..0dc914db44 100644
--- a/api/process/service/launch.go
+++ b/api/process/service/launch.go
@@ -65,6 +65,7 @@ type LaunchService struct {
jobSubmissionTasks *repository.JobSubmissionTaskRepository
monitoringTasks *repository.JobMonitoringTaskRepository
interactiveTasks *repository.InteractiveCommandTaskRepository
+ processWorkflows *repository.WorkflowRepository
executionEngine *orchestration.ExecutionEngine
}
@@ -86,6 +87,7 @@ func NewLaunchService(
jobSubmissionTasks *repository.JobSubmissionTaskRepository,
monitoringTasks *repository.JobMonitoringTaskRepository,
interactiveTasks *repository.InteractiveCommandTaskRepository,
+ processWorkflows *repository.WorkflowRepository,
executionEngine *orchestration.ExecutionEngine,
) *LaunchService {
@@ -104,6 +106,7 @@ func NewLaunchService(
jobSubmissionTasks: jobSubmissionTasks,
monitoringTasks: monitoringTasks,
interactiveTasks: interactiveTasks,
+ processWorkflows: processWorkflows,
executionEngine: executionEngine,
}
}
diff --git a/internal/app/app.go b/internal/app/app.go
index 9e2bf0d534..2b45ef27a2 100644
--- a/internal/app/app.go
+++ b/internal/app/app.go
@@ -266,7 +266,7 @@ func NewServices(cfg config.Config, db *gorm.DB, repos
*Repositories, workflowBa
Launch: processsvc.NewLaunchService(db, processSvs,
batchDeploymentSvc,
templateSvc, slurmClusterSvc, slurmClusterConfigSvc,
sshCredentialSvc, scpDataStorageSvc, dataProductSvc,
statusSvc, stagingTasks, submissionTasks,
- monitoringTasks, commandTasks, executionEngine),
+ monitoringTasks, commandTasks, processWorkflows,
executionEngine),
ExecutionEngine: executionEngine,
}
diff --git a/internal/orchestration/engine.go b/internal/orchestration/engine.go
index f75dd30a20..2f76c27f7c 100644
--- a/internal/orchestration/engine.go
+++ b/internal/orchestration/engine.go
@@ -26,12 +26,14 @@ package orchestration
import (
"context"
"errors"
+ "fmt"
"log/slog"
"time"
"github.com/cschleiden/go-workflows/backend"
"github.com/cschleiden/go-workflows/client"
"github.com/cschleiden/go-workflows/worker"
+ "github.com/cschleiden/go-workflows/workflow"
"github.com/google/uuid"
applicationrepo "github.com/apache/airavata/api/application/repository"
@@ -201,33 +203,34 @@ func (w *ExecutionEngine) HandleBatchJobEmailResponse(ctx
context.Context, email
var ErrNoWorkflowBackend = errors.New("execution engine was built without a
workflow backend")
func (w *ExecutionEngine) LaunchBatchJobSubmission(ctx context.Context,
processID string) (string, error) {
- if w.orchestrator == nil {
- return "", ErrNoWorkflowBackend
- }
-
- workflowId := uuid.NewString()
-
- _, err := w.orchestrator.CreateWorkflowInstance(ctx,
client.WorkflowInstanceOptions{
- InstanceID: workflowId,
- }, w.workflows.HandleBatchJobSubmission, processID)
- if err != nil {
- return "", err
- }
- return workflowId, nil
+ workflowID := uuid.NewString()
+ return workflowID, w.LaunchWorkflow(ctx, workflowID, processID,
model.ProcessWorkflowTypeBatchJobSubmission)
}
func (w *ExecutionEngine) LaunchBatchJobCompletion(ctx context.Context,
processID string) (string, error) {
- if w.orchestrator == nil {
- return "", ErrNoWorkflowBackend
- }
+ workflowID := uuid.NewString()
+ return workflowID, w.LaunchWorkflow(ctx, workflowID, processID,
model.ProcessWorkflowTypeJobCompletion)
+}
- workflowId := uuid.NewString()
+// LaunchWorkflow starts the workflow of the given type for a process under an
instance
+// id the caller chose, so a caller can record the workflow before it starts.
+func (w *ExecutionEngine) LaunchWorkflow(ctx context.Context, workflowID,
processID string,
+ workflowType model.ProcessWorkflowType) error {
- _, err := w.orchestrator.CreateWorkflowInstance(ctx,
client.WorkflowInstanceOptions{
- InstanceID: workflowId,
- }, w.workflows.HandleBatchJobCompletion, processID)
- if err != nil {
- return "", err
+ if w.orchestrator == nil {
+ return ErrNoWorkflowBackend
+ }
+ var wf workflow.Workflow
+ switch workflowType {
+ case model.ProcessWorkflowTypeBatchJobSubmission:
+ wf = w.workflows.HandleBatchJobSubmission
+ case model.ProcessWorkflowTypeJobCompletion:
+ wf = w.workflows.HandleBatchJobCompletion
+ default:
+ return fmt.Errorf("no workflow runs %s", workflowType)
}
- return workflowId, nil
+ _, err := w.orchestrator.CreateWorkflowInstance(ctx,
client.WorkflowInstanceOptions{
+ InstanceID: workflowID,
+ }, wf, processID)
+ return err
}
diff --git a/internal/orchestration/execution/tracker.go
b/internal/orchestration/execution/tracker.go
index c8b47c8fa8..cf4d33b6a3 100644
--- a/internal/orchestration/execution/tracker.go
+++ b/internal/orchestration/execution/tracker.go
@@ -47,11 +47,22 @@ func NewTaskTracker(statuses
*processrepo.TaskStatusRepository) *TaskTracker {
// is replayed and must not write. Each retry of the activity is an attempt of
its own,
// so a task retried twice shows two FAILED entries before its last outcome.
//
+// A task that has already completed is not run again: Track hands in back
unchanged
+// and records nothing. That is what lets a retried process re-run a failed
workflow
+// without repeating the steps that got through — staging a file twice, or
submitting
+// the job again. A completed status is enough to go by, rather than the
latest one,
+// because two statuses of one attempt can share a timestamp.
+//
// A status that cannot be written is logged and dropped rather than failing
the task:
// the history describes the work, and losing an entry of it is no reason to
redo work
// that succeeded.
func (t *TaskTracker) Track(ctx context.Context, processID, taskID string,
taskType model.TaskType,
- run func() (*Context, error)) (*Context, error) {
+ in *Context, run func() (*Context, error)) (*Context, error) {
+
+ if t.completed(ctx, processID, taskID) {
+ slog.Info("Skipping task that has already completed", "taskId",
taskID, "processId", processID, "taskType", taskType)
+ return in, nil
+ }
t.Record(ctx, processID, taskID, taskType,
model.TaskStatusTypeExecuting, nil)
@@ -64,6 +75,21 @@ func (t *TaskTracker) Track(ctx context.Context, processID,
taskID string, taskT
return out, nil
}
+// completed reports whether the task has already completed. A failure to read
is
+// logged and taken as not completed: running a step again is recoverable, and
skipping
+// one that never ran is not.
+func (t *TaskTracker) completed(ctx context.Context, processID, taskID string)
bool {
+ if t == nil || t.statuses == nil {
+ return false
+ }
+ done, err := t.statuses.HasStatus(ctx, taskID, processID,
model.TaskStatusTypeCompleted)
+ if err != nil {
+ slog.Error("Failed to read task status history", "taskId",
taskID, "processId", processID, "error", err)
+ return false
+ }
+ return done
+}
+
// Record appends one status to a task's history, logging rather than
returning a
// failure to write it.
func (t *TaskTracker) Record(ctx context.Context, processID, taskID string,
taskType model.TaskType,
diff --git a/internal/orchestration/execution/tracker_test.go
b/internal/orchestration/execution/tracker_test.go
index 7cfae54248..26af661b92 100644
--- a/internal/orchestration/execution/tracker_test.go
+++ b/internal/orchestration/execution/tracker_test.go
@@ -71,7 +71,7 @@ func TestTrackRecordsExecutingThenCompleted(t *testing.T) {
}
want := &execution.Context{SomeData: "carried"}
- got, err := tracker.Track(context.Background(), proc.ID, "task-1",
model.TaskTypeDataStaging,
+ got, err := tracker.Track(context.Background(), proc.ID, "task-1",
model.TaskTypeDataStaging, nil,
func() (*execution.Context, error) { return want, nil })
if err != nil {
t.Fatalf("track: %v", err)
@@ -94,7 +94,7 @@ func TestTrackRecordsFailureWithItsError(t *testing.T) {
}
boom := errors.New("scp refused")
- _, err := tracker.Track(context.Background(), proc.ID, "task-2",
model.TaskTypeJobSubmission,
+ _, err := tracker.Track(context.Background(), proc.ID, "task-2",
model.TaskTypeJobSubmission, nil,
func() (*execution.Context, error) { return nil, boom })
if !errors.Is(err, boom) {
t.Fatalf("Track error = %v, want the run's own error", err)
@@ -120,7 +120,7 @@ func TestTrackSurvivesUnwritableStatus(t *testing.T) {
tracker, repo, _ := newTracker(t)
// No such process, so the foreign key rejects every status row.
- _, err := tracker.Track(context.Background(), "missing-process",
"task-3", model.TaskTypeJobMonitoring,
+ _, err := tracker.Track(context.Background(), "missing-process",
"task-3", model.TaskTypeJobMonitoring, nil,
func() (*execution.Context, error) { return
&execution.Context{}, nil })
if err != nil {
t.Fatalf("Track error = %v, want the run's success to stand",
err)
@@ -151,3 +151,49 @@ func TestDeletingTaskRemovesItsStatuses(t *testing.T) {
t.Errorf("statuses = %v, want none after the task was deleted",
statuses)
}
}
+
+// A task that has completed is not run again, so a retried workflow passes
over the
+// steps that already got through and hands their input on unchanged.
+func TestTrackSkipsCompletedTask(t *testing.T) {
+ tracker, repo, gdb := newTracker(t)
+ proc := &model.Process{}
+ if err := gdb.Create(proc).Error; err != nil {
+ t.Fatalf("create process: %v", err)
+ }
+ ctx := context.Background()
+ if _, err := tracker.Track(ctx, proc.ID, "task-4",
model.TaskTypeDataStaging, nil,
+ func() (*execution.Context, error) { return
&execution.Context{}, nil }); err != nil {
+ t.Fatalf("first run: %v", err)
+ }
+
+ in := &execution.Context{SomeData: "handed on"}
+ ran := false
+ got, err := tracker.Track(ctx, proc.ID, "task-4",
model.TaskTypeDataStaging, in,
+ func() (*execution.Context, error) { ran = true; return nil,
errors.New("must not run") })
+ if err != nil {
+ t.Fatalf("second run: %v", err)
+ }
+ if ran {
+ t.Error("a completed task ran again")
+ }
+ if got != in {
+ t.Errorf("Track returned %v, want the input context", got)
+ }
+ if got := statusesOf(t, repo, "task-4"); len(got) != 2 {
+ t.Errorf("statuses = %v, want only the first run's EXECUTING
and COMPLETED", got)
+ }
+
+ // A failed task is not completed, so it runs again.
+ if _, err := tracker.Track(ctx, proc.ID, "task-5",
model.TaskTypeDataStaging, nil,
+ func() (*execution.Context, error) { return nil,
errors.New("boom") }); err == nil {
+ t.Fatal("failing run returned no error")
+ }
+ ran = false
+ if _, err := tracker.Track(ctx, proc.ID, "task-5",
model.TaskTypeDataStaging, nil,
+ func() (*execution.Context, error) { ran = true; return
&execution.Context{}, nil }); err != nil {
+ t.Fatalf("rerun of failed task: %v", err)
+ }
+ if !ran {
+ t.Error("a failed task was skipped")
+ }
+}
diff --git a/internal/orchestration/tasks/batch/batch.go
b/internal/orchestration/tasks/batch/batch.go
index 4c89a4ddd6..89d51ac3b5 100644
--- a/internal/orchestration/tasks/batch/batch.go
+++ b/internal/orchestration/tasks/batch/batch.go
@@ -85,7 +85,7 @@ type SubmitBatchJobParameters struct {
// SubmitBatchJob builds the process's submission script, uploads it and
submits it,
// recording the task's status as it goes.
func (a *Tasks) SubmitBatchJob(ctx context.Context, executionContext
*execution.Context, processID string, taskID string) (*execution.Context,
error) {
- return a.tracker.Track(ctx, processID, taskID,
model.TaskTypeJobSubmission, func() (*execution.Context, error) {
+ return a.tracker.Track(ctx, processID, taskID,
model.TaskTypeJobSubmission, executionContext, func() (*execution.Context,
error) {
return a.submitBatchJob(ctx, executionContext, processID,
taskID)
})
}
diff --git a/internal/orchestration/tasks/data/data.go
b/internal/orchestration/tasks/data/data.go
index 60e0d652bd..3317018148 100644
--- a/internal/orchestration/tasks/data/data.go
+++ b/internal/orchestration/tasks/data/data.go
@@ -71,7 +71,7 @@ func New(processes *processrepo.ProcessRepository,
// CopyData stages one file between the task's two storages, recording the
task's status
// as it goes.
func (a *Tasks) CopyData(ctx context.Context, executionContext
*execution.Context, processId string, dataStagingTaskId string)
(*execution.Context, error) {
- return a.tracker.Track(ctx, processId, dataStagingTaskId,
model.TaskTypeDataStaging, func() (*execution.Context, error) {
+ return a.tracker.Track(ctx, processId, dataStagingTaskId,
model.TaskTypeDataStaging, executionContext, func() (*execution.Context, error)
{
return a.copyData(ctx, executionContext, processId,
dataStagingTaskId)
})
}
diff --git a/internal/orchestration/tasks/monitoring/monitoring.go
b/internal/orchestration/tasks/monitoring/monitoring.go
index fee98e51e5..0454d40426 100644
--- a/internal/orchestration/tasks/monitoring/monitoring.go
+++ b/internal/orchestration/tasks/monitoring/monitoring.go
@@ -48,7 +48,7 @@ func New(processes *processrepo.ProcessRepository,
jobMonitoringTasks *processre
// MonitorBatchJob checks the outcome of a finished batch job, recording the
task's
// status as it goes.
func (a *Tasks) MonitorBatchJob(ctx context.Context, executionContext
*execution.Context, processID string, taskID string) (*execution.Context,
error) {
- return a.tracker.Track(ctx, processID, taskID,
model.TaskTypeJobMonitoring, func() (*execution.Context, error) {
+ return a.tracker.Track(ctx, processID, taskID,
model.TaskTypeJobMonitoring, executionContext, func() (*execution.Context,
error) {
return a.monitorBatchJob(ctx, executionContext, processID,
taskID)
})
}
diff --git a/internal/server/server_test.go b/internal/server/server_test.go
index e5d3ea2867..4c724b6399 100644
--- a/internal/server/server_test.go
+++ b/internal/server/server_test.go
@@ -2690,3 +2690,51 @@ func TestProcessWorkflowsAreReadThroughTheirProcess(t
*testing.T) {
t.Errorf("workflows left after deleting their process = %d,
want 0", left)
}
}
+
+// Retrying is owner-scoped and refused unless the latest run of some workflow
failed and
+// none is running. Retrying starts a new workflow, which this harness has no
backend
+// for, so the accepted case shows up here as the new workflow being recorded
and then
+// marked failed when it could not start.
+func TestRetryingAProcessRunsItsFailedWorkflow(t *testing.T) {
+ h := newHarness(t)
+ processID := h.seedProcess("retry", tokenAlice)
+ ctx := context.Background()
+ path := "/api/v1/processes/" + processID + "/retry"
+
+ if rec := h.do(http.MethodPost, path, tokenAlice, nil); rec.Code !=
http.StatusConflict {
+ t.Errorf("retry before launch: status = %d, want 409", rec.Code)
+ }
+
+ if _, err := h.svcs.ProcessWorkflow.Record(ctx, processID,
"submission-1", "BATCH_JOB_SUBMISSION", "RUNNING"); err != nil {
+ t.Fatalf("record submission: %v", err)
+ }
+ if rec := h.do(http.MethodPost, path, tokenAlice, nil); rec.Code !=
http.StatusConflict {
+ t.Errorf("retry while running: status = %d, want 409", rec.Code)
+ }
+
+ if _, err := h.svcs.ProcessWorkflow.Record(ctx, processID,
"submission-1", "BATCH_JOB_SUBMISSION", "FAILED"); err != nil {
+ t.Fatalf("fail submission: %v", err)
+ }
+ if rec := h.do(http.MethodPost, path, tokenBob, nil); rec.Code !=
http.StatusForbidden {
+ t.Errorf("retry as another user: status = %d, want 403",
rec.Code)
+ }
+ if rec := h.do(http.MethodPost, path, "", nil); rec.Code !=
http.StatusUnauthorized {
+ t.Errorf("retry anonymously: status = %d, want 401", rec.Code)
+ }
+ if rec := h.do(http.MethodPost,
"/api/v1/processes/no-such-process/retry", tokenAlice, nil); rec.Code !=
http.StatusNotFound {
+ t.Errorf("retry unknown process: status = %d, want 404",
rec.Code)
+ }
+
+ h.do(http.MethodPost, path, tokenAlice, nil)
+ workflows := h.list("/api/v1/processes/"+processID+"/workflows",
tokenAlice)
+ if len(workflows) != 2 {
+ t.Fatalf("workflows after retry = %v, want the failed one and
its retry", workflows)
+ }
+ retried := workflows[1]
+ if retried["workflowId"] == "submission-1" || retried["workflowType"]
!= "BATCH_JOB_SUBMISSION" {
+ t.Errorf("retried workflow = %v, want a new submission
workflow", retried)
+ }
+ if retried["status"] != "FAILED" {
+ t.Errorf("retried workflow status = %v, want FAILED since it
could not start here", retried["status"])
+ }
+}