forked from longtao.wu/droboticflow
3683 lines
125 KiB
Go
3683 lines
125 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/k8s"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type testLauncher struct {
|
|
launch func(context.Context, k8s.LaunchInput) (*k8s.LaunchResult, error)
|
|
inspect func(context.Context, string, string) (*k8s.JobInspection, error)
|
|
}
|
|
|
|
func (launcher testLauncher) Launch(ctx context.Context, input k8s.LaunchInput) (*k8s.LaunchResult, error) {
|
|
if launcher.launch == nil {
|
|
return nil, fmt.Errorf("test launcher launch not configured")
|
|
}
|
|
return launcher.launch(ctx, input)
|
|
}
|
|
|
|
func (launcher testLauncher) Inspect(ctx context.Context, runID string, nodeID string) (*k8s.JobInspection, error) {
|
|
if launcher.inspect == nil {
|
|
return &k8s.JobInspection{State: k8s.JobStateMissing}, nil
|
|
}
|
|
return launcher.inspect(ctx, runID, nodeID)
|
|
}
|
|
|
|
func operatorRunnerPath(t *testing.T) string {
|
|
t.Helper()
|
|
path, err := filepath.Abs("../../backend/operator-runner/main.py")
|
|
if err != nil {
|
|
t.Fatalf("resolve operator runner: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func newTestRuntime(t *testing.T, dataStore store.Store) (*Runtime, error) {
|
|
t.Helper()
|
|
t.Setenv("OPERATOR_ALLOW_SHELL_COMMANDS", "true")
|
|
runnerPath := operatorRunnerPath(t)
|
|
return &Runtime{
|
|
store: dataStore,
|
|
operatorRunnerImage: "operator-runner:latest",
|
|
workspaceRoot: t.TempDir(),
|
|
launcher: testLauncher{
|
|
launch: func(ctx context.Context, input k8s.LaunchInput) (*k8s.LaunchResult, error) {
|
|
env := append([]string{}, os.Environ()...)
|
|
keys := make([]string, 0, len(input.Env))
|
|
for key := range input.Env {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
env = append(env,
|
|
"RUN_ID="+input.RunID,
|
|
"WORKFLOW_ID="+input.WorkflowID,
|
|
"NODE_ID="+input.NodeID,
|
|
"NODE_LABEL="+input.NodeLabel,
|
|
)
|
|
for _, key := range keys {
|
|
env = append(env, key+"="+input.Env[key])
|
|
}
|
|
cmd := exec.CommandContext(ctx, "python3", runnerPath)
|
|
cmd.Env = env
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", err, string(output))
|
|
}
|
|
return &k8s.LaunchResult{
|
|
Reference: &k8s.JobReference{
|
|
JobName: fmt.Sprintf("%s-%s", input.RunID, input.NodeID),
|
|
Namespace: "droboticflow",
|
|
},
|
|
Output: string(output),
|
|
}, nil
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func newTestRuntimeWithLauncher(t *testing.T, dataStore store.Store, launcher jobLauncher) *Runtime {
|
|
t.Helper()
|
|
t.Setenv("OPERATOR_ALLOW_SHELL_COMMANDS", "true")
|
|
return &Runtime{
|
|
store: dataStore,
|
|
operatorRunnerImage: "operator-runner:latest",
|
|
workspaceRoot: t.TempDir(),
|
|
launcher: launcher,
|
|
}
|
|
}
|
|
|
|
func mustNodeExecution(t *testing.T, run *domain.RunRecord, nodeID string) domain.NodeExecutionRecord {
|
|
t.Helper()
|
|
for _, item := range run.NodeExecutions {
|
|
if item.NodeID == nodeID {
|
|
return item
|
|
}
|
|
}
|
|
t.Fatalf("missing node execution for %s", nodeID)
|
|
return domain.NodeExecutionRecord{}
|
|
}
|
|
|
|
func mustArtifact(t *testing.T, execution domain.NodeExecutionRecord, name string) domain.ExecutionArtifact {
|
|
t.Helper()
|
|
for _, item := range execution.Artifacts {
|
|
if item.Name == name {
|
|
return item
|
|
}
|
|
}
|
|
t.Fatalf("missing artifact %s on node %s", name, execution.NodeID)
|
|
return domain.ExecutionArtifact{}
|
|
}
|
|
|
|
func mustArtifactPayload(t *testing.T, artifact domain.ExecutionArtifact) map[string]any {
|
|
t.Helper()
|
|
if artifact.Data != nil {
|
|
if dataRef, ok := artifact.Data["dataRef"].(map[string]any); ok {
|
|
if payloadPath, ok := dataRef["payloadPath"].(string); ok && strings.TrimSpace(payloadPath) != "" {
|
|
payload, err := os.ReadFile(payloadPath)
|
|
if err != nil {
|
|
t.Fatalf("read artifact payload %s: %v", payloadPath, err)
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(payload, &decoded); err != nil {
|
|
t.Fatalf("decode artifact payload %s: %v", payloadPath, err)
|
|
}
|
|
return decoded
|
|
}
|
|
}
|
|
return artifact.Data
|
|
}
|
|
if strings.TrimSpace(artifact.Path) != "" {
|
|
payload, err := os.ReadFile(artifact.Path)
|
|
if err == nil {
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(payload, &decoded); err == nil {
|
|
return decoded
|
|
}
|
|
}
|
|
}
|
|
t.Fatalf("artifact %s has no structured payload", artifact.Name)
|
|
return nil
|
|
}
|
|
|
|
func TestProcessRunFailsInvalidWorkflowWithDuplicateInputEdges(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法工作流项目",
|
|
Description: "duplicate input edges",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法工作流",
|
|
Description: "duplicate input edges",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source-a",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "上游 A",
|
|
Category: "IO",
|
|
Description: "source a",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
},
|
|
{
|
|
ID: "source-b",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "上游 B",
|
|
Category: "IO",
|
|
Description: "source b",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
},
|
|
{
|
|
ID: "upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "upload",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "edge-a", Source: "source-a", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
{ID: "edge-b", Source: "source-b", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
|
|
processed, err := runtime.processRun(ctx, run)
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
if processed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %s", processed.Status)
|
|
}
|
|
if !strings.Contains(processed.Error, "同一输入端口存在多条上游连线") {
|
|
t.Fatalf("expected duplicate input edge error, got %q", processed.Error)
|
|
}
|
|
if len(processed.NodeExecutions) != 0 {
|
|
t.Fatalf("expected no node executions for invalid workflow, got %#v", processed.NodeExecutions)
|
|
}
|
|
}
|
|
|
|
func TestProcessRunFailsInvalidIncrementalDirectoryDepth(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法目录深度项目",
|
|
Description: "invalid incremental depth",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法目录深度工作流",
|
|
Description: "invalid incremental depth",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
{Key: "INCREMENTAL_UNIT", Value: "DIRECTORY"},
|
|
{Key: "INCREMENTAL_DIRECTORY_DEPTH", Value: "zero"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual); err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
processed, err := runtime.ProcessOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
if processed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %s", processed.Status)
|
|
}
|
|
if !strings.Contains(processed.Error, "INCREMENTAL_DIRECTORY_DEPTH must be a positive integer") {
|
|
t.Fatalf("expected directory depth error, got %q", processed.Error)
|
|
}
|
|
execution := mustNodeExecution(t, processed, "source")
|
|
if execution.Status != domain.NodeExecutionStatusFailed {
|
|
t.Fatalf("expected failed source execution, got %s", execution.Status)
|
|
}
|
|
if !strings.Contains(execution.Error, "INCREMENTAL_DIRECTORY_DEPTH must be a positive integer") {
|
|
t.Fatalf("expected node directory depth error, got %q", execution.Error)
|
|
}
|
|
}
|
|
|
|
func TestProcessRunFailsInvalidIncrementalUnit(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法增量单位项目",
|
|
Description: "invalid incremental unit",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法增量单位工作流",
|
|
Description: "invalid incremental unit",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
{Key: "INCREMENTAL_UNIT", Value: "ROW"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual); err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
processed, err := runtime.ProcessOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
if processed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %s", processed.Status)
|
|
}
|
|
if !strings.Contains(processed.Error, "INCREMENTAL_UNIT must be OBJECT or DIRECTORY") {
|
|
t.Fatalf("expected incremental unit error, got %q", processed.Error)
|
|
}
|
|
execution := mustNodeExecution(t, processed, "source")
|
|
if execution.Status != domain.NodeExecutionStatusFailed {
|
|
t.Fatalf("expected failed source execution, got %s", execution.Status)
|
|
}
|
|
if !strings.Contains(execution.Error, "INCREMENTAL_UNIT must be OBJECT or DIRECTORY") {
|
|
t.Fatalf("expected node incremental unit error, got %q", execution.Error)
|
|
}
|
|
}
|
|
|
|
func TestProcessRunFailsInvalidScanMode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法扫描模式项目",
|
|
Description: "invalid scan mode",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法扫描模式工作流",
|
|
Description: "invalid scan mode",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "ROW"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual); err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
processed, err := runtime.ProcessOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
if processed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %s", processed.Status)
|
|
}
|
|
if !strings.Contains(processed.Error, "SCAN_MODE must be FULL or INCREMENTAL") {
|
|
t.Fatalf("expected scan mode error, got %q", processed.Error)
|
|
}
|
|
execution := mustNodeExecution(t, processed, "source")
|
|
if execution.Status != domain.NodeExecutionStatusFailed {
|
|
t.Fatalf("expected failed source execution, got %s", execution.Status)
|
|
}
|
|
if !strings.Contains(execution.Error, "SCAN_MODE must be FULL or INCREMENTAL") {
|
|
t.Fatalf("expected node scan mode error, got %q", execution.Error)
|
|
}
|
|
}
|
|
|
|
func TestProcessRunFailsInvalidDataSource(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法数据源项目",
|
|
Description: "invalid data source",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法数据源工作流",
|
|
Description: "invalid data source",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "ROW"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual); err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
processed, err := runtime.ProcessOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
if processed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %s", processed.Status)
|
|
}
|
|
if !strings.Contains(processed.Error, "DATA_SOURCE must be INLINE, LOCAL, BOS, or UPSTREAM") {
|
|
t.Fatalf("expected data source error, got %q", processed.Error)
|
|
}
|
|
execution := mustNodeExecution(t, processed, "source")
|
|
if execution.Status != domain.NodeExecutionStatusFailed {
|
|
t.Fatalf("expected failed source execution, got %s", execution.Status)
|
|
}
|
|
if !strings.Contains(execution.Error, "DATA_SOURCE must be INLINE, LOCAL, BOS, or UPSTREAM") {
|
|
t.Fatalf("expected node data source error, got %q", execution.Error)
|
|
}
|
|
}
|
|
|
|
func TestSourceStateFromCheckpointPrefersCheckpointFilePayload(t *testing.T) {
|
|
workflow := &domain.WorkflowRecord{ID: "workflow-1"}
|
|
run := &domain.RunRecord{ID: "run-1"}
|
|
checkpointPath := filepath.Join(t.TempDir(), "source-checkpoint.json")
|
|
writeFile(t, checkpointPath, `{
|
|
"sourceFingerprint": "fp-1",
|
|
"dataSource": "BOS",
|
|
"sourcePath": "bos://datastorage/algo/X-platform-test",
|
|
"bucket": "datastorage",
|
|
"prefix": "algo/X-platform-test",
|
|
"pattern": "",
|
|
"scanMode": "INCREMENTAL",
|
|
"directoryDepth": 1,
|
|
"seenFingerprints": ["a", "b"],
|
|
"listedCount": 28,
|
|
"newCount": 0,
|
|
"scanStartedAt": "2026-04-12T15:45:42Z",
|
|
"scanCompletedAt": "2026-04-12T15:45:55Z"
|
|
}`)
|
|
execution := domain.NodeExecutionRecord{
|
|
NodeID: "scan-source",
|
|
Artifacts: []domain.ExecutionArtifact{
|
|
{
|
|
Name: "source-checkpoint.json",
|
|
Path: checkpointPath,
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "fp-1",
|
|
"listedCount": 28.0,
|
|
"newCount": 28.0,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
state, err := sourceStateFromCheckpoint(workflow, run, execution, &execution.Artifacts[0])
|
|
if err != nil {
|
|
t.Fatalf("source state from checkpoint: %v", err)
|
|
}
|
|
if state == nil {
|
|
t.Fatalf("expected source state")
|
|
}
|
|
if state.LastNewCount != 0 {
|
|
t.Fatalf("expected checkpoint file payload to win, got lastNewCount=%d", state.LastNewCount)
|
|
}
|
|
if state.LastListedCount != 28 {
|
|
t.Fatalf("expected lastListedCount=28, got %d", state.LastListedCount)
|
|
}
|
|
if state.LastRunID != run.ID {
|
|
t.Fatalf("expected lastRunID=%s, got %s", run.ID, state.LastRunID)
|
|
}
|
|
}
|
|
|
|
func TestSourceStateFromCheckpointRejectsUnreadableOrInvalidCheckpointFiles(t *testing.T) {
|
|
workflow := &domain.WorkflowRecord{ID: "workflow-1"}
|
|
run := &domain.RunRecord{ID: "run-1"}
|
|
execution := domain.NodeExecutionRecord{NodeID: "scan-source"}
|
|
|
|
missingPath := filepath.Join(t.TempDir(), "missing-source-checkpoint.json")
|
|
_, err := sourceStateFromCheckpoint(workflow, run, execution, &domain.ExecutionArtifact{
|
|
Name: "source-checkpoint.json",
|
|
Path: missingPath,
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "stale-inline",
|
|
"listedCount": 1,
|
|
"newCount": 1,
|
|
},
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "read source checkpoint") {
|
|
t.Fatalf("expected unreadable checkpoint error, got %v", err)
|
|
}
|
|
|
|
invalidPath := filepath.Join(t.TempDir(), "source-checkpoint.json")
|
|
writeFile(t, invalidPath, `{`)
|
|
_, err = sourceStateFromCheckpoint(workflow, run, execution, &domain.ExecutionArtifact{
|
|
Name: "source-checkpoint.json",
|
|
Path: invalidPath,
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "stale-inline",
|
|
"listedCount": 1,
|
|
"newCount": 1,
|
|
},
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "parse source checkpoint") {
|
|
t.Fatalf("expected invalid checkpoint JSON error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSourceStateFromCheckpointRejectsLossyCheckpointCounts(t *testing.T) {
|
|
workflow := &domain.WorkflowRecord{ID: "workflow-1"}
|
|
run := &domain.RunRecord{ID: "run-1"}
|
|
execution := domain.NodeExecutionRecord{
|
|
NodeID: "scan-source",
|
|
Artifacts: []domain.ExecutionArtifact{{
|
|
Name: "source-checkpoint.json",
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "fp-lossy",
|
|
"directoryDepth": float64(1),
|
|
"listedCount": 28.5,
|
|
"newCount": 0.0,
|
|
},
|
|
}},
|
|
}
|
|
|
|
_, err := sourceStateFromCheckpoint(workflow, run, execution, &execution.Artifacts[0])
|
|
if err == nil || !strings.Contains(err.Error(), "listedCount must be an integer") {
|
|
t.Fatalf("expected lossy listedCount error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCommitSourceStatesPrefersPersistedRunSnapshot(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
workflow := &domain.WorkflowRecord{ID: "workflow-1"}
|
|
persistedRun := domain.RunRecord{
|
|
ID: "run-1",
|
|
WorkflowID: workflow.ID,
|
|
NodeExecutions: []domain.NodeExecutionRecord{{
|
|
NodeID: "scan-source",
|
|
Artifacts: []domain.ExecutionArtifact{{
|
|
Name: "source-checkpoint.json",
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "fp-1",
|
|
"dataSource": "BOS",
|
|
"sourcePath": "bos://datastorage/algo/X-platform-test",
|
|
"bucket": "datastorage",
|
|
"prefix": "algo/X-platform-test",
|
|
"scanMode": "INCREMENTAL",
|
|
"directoryDepth": 1.0,
|
|
"seenFingerprints": []any{"a", "b"},
|
|
"listedCount": 28.0,
|
|
"newCount": 0.0,
|
|
"scanStartedAt": "2026-04-12T15:45:42Z",
|
|
"scanCompletedAt": "2026-04-12T15:45:55Z",
|
|
},
|
|
}},
|
|
}},
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, persistedRun); err != nil {
|
|
t.Fatalf("upsert persisted run: %v", err)
|
|
}
|
|
inMemoryRun := &domain.RunRecord{
|
|
ID: "run-1",
|
|
WorkflowID: workflow.ID,
|
|
NodeExecutions: []domain.NodeExecutionRecord{{
|
|
NodeID: "scan-source",
|
|
Artifacts: []domain.ExecutionArtifact{{
|
|
Name: "source-checkpoint.json",
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "fp-1",
|
|
"directoryDepth": 1.0,
|
|
"listedCount": 28.0,
|
|
"newCount": 28.0,
|
|
},
|
|
}},
|
|
}},
|
|
}
|
|
|
|
if err := runtime.commitSourceStates(ctx, workflow, inMemoryRun); err != nil {
|
|
t.Fatalf("commit source states: %v", err)
|
|
}
|
|
state, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", "fp-1")
|
|
if err != nil {
|
|
t.Fatalf("get source state: %v", err)
|
|
}
|
|
if state == nil {
|
|
t.Fatalf("expected committed source state")
|
|
}
|
|
if state.LastNewCount != 0 {
|
|
t.Fatalf("expected committed source state to use persisted run snapshot, got %d", state.LastNewCount)
|
|
}
|
|
}
|
|
|
|
func TestCommitSourceStatesPreservesSeenFingerprintsFromTypedSlice(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
workflow := &domain.WorkflowRecord{ID: "workflow-typed-slice"}
|
|
persistedRun := domain.RunRecord{
|
|
ID: "run-typed-slice",
|
|
WorkflowID: workflow.ID,
|
|
NodeExecutions: []domain.NodeExecutionRecord{{
|
|
NodeID: "scan-source",
|
|
Artifacts: []domain.ExecutionArtifact{{
|
|
Name: "source-checkpoint.json",
|
|
Data: map[string]any{
|
|
"sourceFingerprint": "fp-typed-slice",
|
|
"dataSource": "BOS",
|
|
"sourcePath": "bos://datastorage/algo/X-platform-test/raw",
|
|
"bucket": "datastorage",
|
|
"prefix": "algo/X-platform-test/raw",
|
|
"scanMode": "INCREMENTAL",
|
|
"directoryDepth": 1.0,
|
|
"seenFingerprints": []string{"a", "b", "c"},
|
|
"listedCount": 4.0,
|
|
"newCount": 0.0,
|
|
},
|
|
}},
|
|
}},
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, persistedRun); err != nil {
|
|
t.Fatalf("upsert persisted run: %v", err)
|
|
}
|
|
|
|
if err := runtime.commitSourceStates(ctx, workflow, &persistedRun); err != nil {
|
|
t.Fatalf("commit source states: %v", err)
|
|
}
|
|
state, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", "fp-typed-slice")
|
|
if err != nil {
|
|
t.Fatalf("get source state: %v", err)
|
|
}
|
|
if state == nil {
|
|
t.Fatalf("expected committed source state")
|
|
}
|
|
if len(state.SeenFingerprints) != 3 {
|
|
t.Fatalf("expected 3 seen fingerprints, got %#v", state.SeenFingerprints)
|
|
}
|
|
if strings.Join(state.SeenFingerprints, ",") != "a,b,c" {
|
|
t.Fatalf("unexpected seen fingerprints %#v", state.SeenFingerprints)
|
|
}
|
|
}
|
|
|
|
func writeFile(t *testing.T, path string, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatalf("write %s: %v", path, err)
|
|
}
|
|
}
|
|
|
|
func createRosbagDB3(t *testing.T, path string) {
|
|
t.Helper()
|
|
command := exec.Command(
|
|
"python3",
|
|
"-c",
|
|
strings.Join([]string{
|
|
"import sqlite3, sys",
|
|
"conn = sqlite3.connect(sys.argv[1])",
|
|
"cur = conn.cursor()",
|
|
"cur.execute('create table topics (id integer primary key, name text, type text, serialization_format text)')",
|
|
"cur.execute('create table messages (id integer primary key, topic_id integer, timestamp integer, data blob)')",
|
|
"cur.execute(\"insert into topics (id, name, type, serialization_format) values (1, '/camera', 'sensor_msgs/msg/Image', 'cdr')\")",
|
|
"cur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 100, X'01')\")",
|
|
"cur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 200, X'02')\")",
|
|
"conn.commit()",
|
|
"conn.close()",
|
|
}, "; "),
|
|
path,
|
|
)
|
|
if output, err := command.CombinedOutput(); err != nil {
|
|
t.Fatalf("create rosbag db3 %s: %v: %s", path, err, string(output))
|
|
}
|
|
}
|
|
|
|
func TestChooseImagePrefersRuntimeOperatorRunnerForBuiltins(t *testing.T) {
|
|
node := domain.WorkflowNode{
|
|
Data: domain.WorkflowNodeData{
|
|
Image: "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:old-tag",
|
|
},
|
|
}
|
|
image := chooseImage(node, "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:new-tag")
|
|
if image != "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:new-tag" {
|
|
t.Fatalf("expected runtime image override for built-in operator-runner, got %q", image)
|
|
}
|
|
}
|
|
|
|
func createTestVideoMP4(t *testing.T, path string, pattern string, fps int, frames int, width int, height int) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
|
|
}
|
|
command := exec.Command(
|
|
"python3",
|
|
"-c",
|
|
strings.Join([]string{
|
|
"import cv2, numpy as np, sys",
|
|
"path, pattern, fps, frames, width, height = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5]), int(sys.argv[6])",
|
|
"fourcc = cv2.VideoWriter_fourcc(*'mp4v')",
|
|
"writer = cv2.VideoWriter(path, fourcc, fps, (width, height))",
|
|
"assert writer.isOpened(), 'video writer did not open'",
|
|
"for index in range(frames):",
|
|
" frame = np.zeros((height, width, 3), dtype=np.uint8)",
|
|
" if pattern == 'sharp':",
|
|
" x = int((index * 3) % max(1, width - 18))",
|
|
" cv2.rectangle(frame, (x, 8), (x + 16, 24), (255, 255, 255), -1)",
|
|
" cv2.putText(frame, str(index % 10), (6, height - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)",
|
|
" elif pattern == 'blur':",
|
|
" cv2.rectangle(frame, (16, 16), (width - 16, height - 16), (220, 220, 220), -1)",
|
|
" frame = cv2.GaussianBlur(frame, (23, 23), 0)",
|
|
" else:",
|
|
" x = int((index * 5) % max(1, width - 20))",
|
|
" frame[:] = (40, 60, 90)",
|
|
" cv2.circle(frame, (x + 10, height // 2), 10, (80, 140, 190), -1)",
|
|
" writer.write(frame)",
|
|
"writer.release()",
|
|
"capture = cv2.VideoCapture(path)",
|
|
"assert capture.isOpened(), 'video capture did not open'",
|
|
"capture.release()",
|
|
"print(path)",
|
|
}, "\n"),
|
|
path,
|
|
pattern,
|
|
fmt.Sprintf("%d", fps),
|
|
fmt.Sprintf("%d", frames),
|
|
fmt.Sprintf("%d", width),
|
|
fmt.Sprintf("%d", height),
|
|
)
|
|
if output, err := command.CombinedOutput(); err != nil {
|
|
t.Fatalf("create test mp4 %s: %v: %s", path, err, string(output))
|
|
}
|
|
}
|
|
|
|
func createXPlatformDataset(t *testing.T) string {
|
|
t.Helper()
|
|
root := filepath.Join(t.TempDir(), "algo", "X-platform-test")
|
|
fps := 10
|
|
width := 96
|
|
height := 64
|
|
|
|
type videoSpec struct {
|
|
Name string
|
|
Pattern string
|
|
Frames int
|
|
Duration float64
|
|
}
|
|
type collectionSpec struct {
|
|
Name string
|
|
Type string
|
|
Place string
|
|
Date string
|
|
DeviceID string
|
|
HasIntrinsics bool
|
|
VideoDirectory string
|
|
Videos []videoSpec
|
|
}
|
|
|
|
collections := []collectionSpec{
|
|
{
|
|
Name: "BJ_001_0001_OsmoNano_2026-03-19_14-51-43",
|
|
Type: "家庭",
|
|
Place: "蓟门里小区",
|
|
Date: "20260319",
|
|
DeviceID: "C00000",
|
|
HasIntrinsics: true,
|
|
VideoDirectory: "DJI_001",
|
|
Videos: []videoSpec{
|
|
{Name: "DJI_0001_D.mp4", Pattern: "sharp", Frames: 40, Duration: 4.0},
|
|
{Name: "DJI_0002_D.mp4", Pattern: "blur", Frames: 20, Duration: 2.0},
|
|
},
|
|
},
|
|
{
|
|
Name: "BJ_002_0002_OsmoNano_2026-03-20_10-30-00",
|
|
Type: "办公",
|
|
Place: "中关村",
|
|
Date: "20260320",
|
|
DeviceID: "C00001",
|
|
HasIntrinsics: false,
|
|
VideoDirectory: "DJI_001",
|
|
Videos: []videoSpec{
|
|
{Name: "DJI_0001_D.mp4", Pattern: "sharp", Frames: 20, Duration: 2.0},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, collection := range collections {
|
|
collectionRoot := filepath.Join(root, collection.Name)
|
|
videoRoot := filepath.Join(collectionRoot, collection.VideoDirectory)
|
|
videoMetaRows := make([]map[string]any, 0, len(collection.Videos))
|
|
for index, video := range collection.Videos {
|
|
videoPath := filepath.Join(videoRoot, video.Name)
|
|
createTestVideoMP4(t, videoPath, video.Pattern, fps, video.Frames, width, height)
|
|
videoMetaRows = append(videoMetaRows, map[string]any{
|
|
"filename": video.Name,
|
|
"codec": "mp4v",
|
|
"resolution": fmt.Sprintf("%dx%d", width, height),
|
|
"frameRate": float64(fps),
|
|
"bitrate": 2000000 + index*100000,
|
|
"duration": video.Duration,
|
|
"startTime": 1773904218 + index*60,
|
|
})
|
|
}
|
|
metaJSON, err := json.Marshal(map[string]any{
|
|
"type": collection.Type,
|
|
"place": collection.Place,
|
|
"mission": "无",
|
|
"project_id": collection.Name,
|
|
"date": collection.Date,
|
|
"device_id": collection.DeviceID,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal meta json: %v", err)
|
|
}
|
|
writeFile(t, filepath.Join(collectionRoot, "meta.json"), string(metaJSON))
|
|
videoMetaJSON, err := json.Marshal(videoMetaRows)
|
|
if err != nil {
|
|
t.Fatalf("marshal video_meta json: %v", err)
|
|
}
|
|
writeFile(t, filepath.Join(collectionRoot, "video_meta.json"), string(videoMetaJSON))
|
|
writeFile(t, filepath.Join(videoRoot, "DJI_meta.json"), `{"ok":true}`)
|
|
if collection.HasIntrinsics {
|
|
writeFile(t, filepath.Join(collectionRoot, "intrinsics.json"), `{"fx": 1000, "fy": 1000}`)
|
|
}
|
|
}
|
|
|
|
return root
|
|
}
|
|
|
|
func waitForRunStatus(t *testing.T, ctx context.Context, dataStore *store.MemoryStore, runID string, expected domain.RunStatus, timeout time.Duration) *domain.RunRecord {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
for time.Now().Before(deadline) {
|
|
run, err := dataStore.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatalf("get run %s: %v", runID, err)
|
|
}
|
|
if run != nil && run.Status == expected {
|
|
return run
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
run, err := dataStore.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatalf("get run %s: %v", runID, err)
|
|
}
|
|
t.Fatalf("timed out waiting for run %s to become %s, got %#v", runID, expected, run)
|
|
return nil
|
|
}
|
|
|
|
func waitForRunningNodeCount(t *testing.T, ctx context.Context, dataStore *store.MemoryStore, runID string, expected int, timeout time.Duration) *domain.RunRecord {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
for time.Now().Before(deadline) {
|
|
run, err := dataStore.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatalf("get run %s: %v", runID, err)
|
|
}
|
|
if run == nil {
|
|
time.Sleep(100 * time.Millisecond)
|
|
continue
|
|
}
|
|
running := 0
|
|
for _, execution := range run.NodeExecutions {
|
|
if execution.Status == domain.NodeExecutionStatusRunning {
|
|
running++
|
|
}
|
|
}
|
|
if running == expected {
|
|
return run
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
run, err := dataStore.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatalf("get run %s: %v", runID, err)
|
|
}
|
|
t.Fatalf("timed out waiting for run %s to have %d running nodes, got %#v", runID, expected, run)
|
|
return nil
|
|
}
|
|
|
|
func TestRuntimeProcessesBatchRunToCompletion(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Worker 项目", Description: "worker"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "批处理工作流",
|
|
Description: "complete path",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "OSS 数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Inputs: []domain.PortDefinition{},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "out-1", Name: "file", Type: domain.PortTypeFile},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "INLINE"},
|
|
{Key: "SOURCE_TEXT", Value: "worker-runtime-test"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "sink",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "sink",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in-1", Name: "json", Type: domain.PortTypeJSON},
|
|
},
|
|
Outputs: []domain.PortDefinition{},
|
|
},
|
|
Position: domain.Position{X: 100, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "source", SourceHandle: "out-1", Target: "sink", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
if completed == nil || completed.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("expected completed run, got %#v", completed)
|
|
}
|
|
for _, item := range completed.NodeExecutions {
|
|
if item.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected completed node execution, got %s", item.Status)
|
|
}
|
|
}
|
|
if len(completed.Artifacts) == 0 {
|
|
t.Fatalf("expected artifacts")
|
|
}
|
|
if completed.Artifacts[0].Path == "" {
|
|
t.Fatalf("expected real artifact path")
|
|
}
|
|
if len(completed.ResourceSeries) == 0 {
|
|
t.Fatalf("expected resource samples from operator runner")
|
|
}
|
|
}
|
|
|
|
func TestRuntimeStopsGracefullyWithoutDispatchingNewNodes(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "停止项目", Description: "graceful"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "停止工作流",
|
|
Description: "graceful stop path",
|
|
Type: domain.WorkflowTypeBatch,
|
|
MaxConcurrentRuns: 1,
|
|
NodeTimeoutSeconds: 30,
|
|
RunTimeoutSeconds: 60,
|
|
MaxRetryAttempts: 0,
|
|
RetryBackoffSeconds: 0,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "payload", Name: "payload", Type: domain.PortTypeJSON},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "COMMAND", Value: `sleep 1; printf '{"value":1}' > payload.json`},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "sink",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "sink",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "input", Name: "input", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 120, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "source", SourceHandle: "payload", Target: "sink", TargetHandle: "input"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
_, processErr := runtime.ProcessOnce(ctx)
|
|
done <- processErr
|
|
}()
|
|
|
|
waitForRunningNodeCount(t, ctx, dataStore, run.ID, 1, 5*time.Second)
|
|
stored, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get running run: %v", err)
|
|
}
|
|
stored.Status = domain.RunStatus("stopping")
|
|
stored.StopMode = domain.RunStopModeGraceful
|
|
stored.StopRequestedAt = nowISO()
|
|
stored.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *stored); err != nil {
|
|
t.Fatalf("request graceful stop: %v", err)
|
|
}
|
|
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatalf("process run: %v", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatalf("timed out waiting for graceful stop")
|
|
}
|
|
|
|
finalRun, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get final run: %v", err)
|
|
}
|
|
if finalRun == nil || finalRun.Status != domain.RunStatusCancelled {
|
|
t.Fatalf("expected cancelled run after graceful stop, got %#v", finalRun)
|
|
}
|
|
if sink := mustNodeExecution(t, finalRun, "source"); sink.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected source node to complete before stop, got %s", sink.Status)
|
|
}
|
|
for _, item := range finalRun.NodeExecutions {
|
|
if item.NodeID == "sink" {
|
|
t.Fatalf("expected sink node not to start after graceful stop, got %#v", item)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRuntimePausesForAuditAndResumes(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Audit 项目", Description: "audit"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "审核工作流",
|
|
Description: "audit path",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "review",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "人工审核 (Human Review)",
|
|
Category: "HUMAN",
|
|
Description: "review",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in-1", Name: "json", Type: domain.PortTypeJSON},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "approved", Name: "审核通过", Type: domain.PortTypeJSON},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "sink",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in-1", Name: "json", Type: domain.PortTypeJSON},
|
|
},
|
|
Outputs: []domain.PortDefinition{},
|
|
},
|
|
Position: domain.Position{X: 100, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "e1", Source: "review", SourceHandle: "approved", Target: "upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
paused, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get paused run: %v", err)
|
|
}
|
|
if paused == nil || paused.Status != domain.RunStatusWaitingForAudit {
|
|
t.Fatalf("expected waiting_for_audit run, got %#v", paused)
|
|
}
|
|
|
|
tasks, err := dataStore.ListAuditTasks(ctx, "all")
|
|
if err != nil {
|
|
t.Fatalf("list audit tasks: %v", err)
|
|
}
|
|
var task *domain.AuditTaskRecord
|
|
for _, item := range tasks {
|
|
if item.RunID == run.ID && item.Status == domain.AuditStatusPending {
|
|
copied := item
|
|
task = &copied
|
|
break
|
|
}
|
|
}
|
|
if task == nil {
|
|
t.Fatalf("expected pending audit task")
|
|
}
|
|
|
|
task.Status = domain.AuditStatusApproved
|
|
task.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertAuditTask(ctx, *task); err != nil {
|
|
t.Fatalf("approve audit task: %v", err)
|
|
}
|
|
paused.Status = domain.RunStatusQueued
|
|
paused.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *paused); err != nil {
|
|
t.Fatalf("queue paused run: %v", err)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("resume process once: %v", err)
|
|
}
|
|
|
|
resumed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get resumed run: %v", err)
|
|
}
|
|
if resumed == nil || resumed.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("expected completed resumed run, got %#v", resumed)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeRoutesArtifactsByOutputHandle(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Route 项目", Description: "route"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "条件分流工作流",
|
|
Description: "route by handle",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "out-manifest", Name: "manifest", Type: domain.PortTypeJSON},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "COMMAND", Value: `printf '{"items":[{"kind":"keep","value":1},{"kind":"drop","value":2}]}' > routed.json`},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "route",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "条件分流",
|
|
Category: "LOGIC",
|
|
Description: "route",
|
|
Inputs: []domain.PortDefinition{
|
|
{ID: "in-1", Name: "input", Type: domain.PortTypeJSON},
|
|
},
|
|
Outputs: []domain.PortDefinition{
|
|
{ID: "matched", Name: "matched", Type: domain.PortTypeJSON},
|
|
{ID: "unmatched", Name: "unmatched", Type: domain.PortTypeJSON},
|
|
},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "FIELD_PATH", Value: "kind"},
|
|
{Key: "OPERATOR", Value: "eq"},
|
|
{Key: "EXPECTED_JSON", Value: `"keep"`},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 200, Y: 0},
|
|
},
|
|
{
|
|
ID: "keep-upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "keep sink",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 420, Y: -80},
|
|
},
|
|
{
|
|
ID: "drop-upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "drop sink",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 420, Y: 80},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-route", Source: "source", SourceHandle: "out-manifest", Target: "route", TargetHandle: "in-1"},
|
|
{ID: "route-keep", Source: "route", SourceHandle: "matched", Target: "keep-upload", TargetHandle: "in-1"},
|
|
{ID: "route-drop", Source: "route", SourceHandle: "unmatched", Target: "drop-upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
if completed == nil || completed.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("expected completed run, got %#v", completed)
|
|
}
|
|
|
|
routeExecution := mustNodeExecution(t, completed, "route")
|
|
matched := mustArtifact(t, routeExecution, "matched.json")
|
|
unmatched := mustArtifact(t, routeExecution, "unmatched.json")
|
|
if matched.PortID != "matched" || unmatched.PortID != "unmatched" {
|
|
t.Fatalf("expected output port IDs on split artifacts, got matched=%q unmatched=%q", matched.PortID, unmatched.PortID)
|
|
}
|
|
|
|
keepUpload := mustNodeExecution(t, completed, "keep-upload")
|
|
dropUpload := mustNodeExecution(t, completed, "drop-upload")
|
|
keepSummary := mustArtifact(t, keepUpload, "uploaded-results.json")
|
|
dropSummary := mustArtifact(t, dropUpload, "uploaded-results.json")
|
|
keepSummaryPayload := mustArtifactPayload(t, keepSummary)
|
|
dropSummaryPayload := mustArtifactPayload(t, dropSummary)
|
|
keepItems, _ := keepSummaryPayload["items"].([]any)
|
|
dropItems, _ := dropSummaryPayload["items"].([]any)
|
|
if len(keepItems) != 1 || len(dropItems) != 1 {
|
|
t.Fatalf("expected one routed payload per sink, got keep=%d drop=%d", len(keepItems), len(dropItems))
|
|
}
|
|
|
|
keepPayload, _ := keepItems[0].(map[string]any)
|
|
dropPayload, _ := dropItems[0].(map[string]any)
|
|
keepRows, _ := keepPayload["items"].([]any)
|
|
dropRows, _ := dropPayload["items"].([]any)
|
|
if len(keepRows) != 1 || len(dropRows) != 1 {
|
|
t.Fatalf("expected one row in each routed payload, got keep=%d drop=%d", len(keepRows), len(dropRows))
|
|
}
|
|
if keepRow, _ := keepRows[0].(map[string]any); keepRow != nil {
|
|
if keepRow["kind"] != "keep" {
|
|
t.Fatalf("unexpected keep row: %#v", keepRow)
|
|
}
|
|
}
|
|
if dropRow, _ := dropRows[0].(map[string]any); dropRow != nil {
|
|
if dropRow["kind"] != "drop" {
|
|
t.Fatalf("unexpected drop row: %#v", dropRow)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRuntimeSupportsPreviewAndDatasetBuiltins(t *testing.T) {
|
|
t.Run("command previews text and csv outputs", func(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Preview 项目", Description: "preview"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "文件预览工作流",
|
|
Description: "preview files",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "command",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "command",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "COMMAND", Value: "printf 'name,score\\nalpha,1\\nbeta,2\\n' > report.csv && printf 'hello\\nworld\\n' > notes.txt"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
commandExecution := mustNodeExecution(t, completed, "command")
|
|
report := mustArtifact(t, commandExecution, "report.csv")
|
|
notes := mustArtifact(t, commandExecution, "notes.txt")
|
|
reportPreview, _ := report.Data["preview"].(map[string]any)
|
|
notesPreview, _ := notes.Data["preview"].(map[string]any)
|
|
if reportPreview["kind"] != "table" {
|
|
t.Fatalf("expected table preview for csv artifact, got %#v", reportPreview)
|
|
}
|
|
if notesPreview["kind"] != "text" {
|
|
t.Fatalf("expected text preview for text artifact, got %#v", notesPreview)
|
|
}
|
|
})
|
|
|
|
t.Run("lerobot dataset summary", func(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
datasetRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(datasetRoot, "meta", "info.json"), `{"robot_type":"aloha","fps":30}`)
|
|
writeFile(t, filepath.Join(datasetRoot, "meta", "stats.json"), `{"episodes":2}`)
|
|
writeFile(t, filepath.Join(datasetRoot, "meta", "tasks.jsonl"), "{\"task\":\"pick\"}\n{\"task\":\"place\"}\n")
|
|
writeFile(t, filepath.Join(datasetRoot, "data", "episode_000.parquet"), "PAR1")
|
|
writeFile(t, filepath.Join(datasetRoot, "videos", "episode_000.mp4"), "video")
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "LeRobot 项目", Description: "lerobot"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "LeRobot 概览",
|
|
Description: "lerobot summary",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: datasetRoot},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "summary",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "LeRobot 数据集概览",
|
|
Category: "ROBOTICS",
|
|
Description: "lerobot",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "summary", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 240, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-summary", Source: "source", SourceHandle: "out-1", Target: "summary", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
summary := mustArtifact(t, mustNodeExecution(t, completed, "summary"), "lerobot-summary.json")
|
|
summaryPayload := mustArtifactPayload(t, summary)
|
|
preview, _ := summaryPayload["preview"].(map[string]any)
|
|
if preview["format"] != "lerobot" {
|
|
t.Fatalf("expected lerobot preview, got %#v", preview)
|
|
}
|
|
if summaryPayload["datasetType"] != "lerobot" {
|
|
t.Fatalf("expected datasetType lerobot, got %#v", summaryPayload)
|
|
}
|
|
})
|
|
|
|
t.Run("rosbag sqlite summary", func(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
datasetRoot := t.TempDir()
|
|
dbPath := filepath.Join(datasetRoot, "bag_0.db3")
|
|
createRosbagDB3(t, dbPath)
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "ROS 项目", Description: "rosbag"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "ROSBag 概览",
|
|
Description: "rosbag summary",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: dbPath},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "summary",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "ROSBag 数据概览",
|
|
Category: "ROBOTICS",
|
|
Description: "rosbag",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "summary", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "SOURCE_PATH", Value: dbPath}},
|
|
},
|
|
Position: domain.Position{X: 240, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-summary", Source: "source", SourceHandle: "out-1", Target: "summary", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
summary := mustArtifact(t, mustNodeExecution(t, completed, "summary"), "rosbag-summary.json")
|
|
summaryPayload := mustArtifactPayload(t, summary)
|
|
preview, _ := summaryPayload["preview"].(map[string]any)
|
|
if preview["format"] != "rosbag2" {
|
|
t.Fatalf("expected rosbag2 preview, got %#v", preview)
|
|
}
|
|
topics, _ := preview["topics"].([]any)
|
|
if len(topics) != 1 {
|
|
t.Fatalf("expected one topic summary, got %#v", topics)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRuntimeProcessesXPlatformPipeline(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
datasetRoot := createXPlatformDataset(t)
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "XPlatform 项目", Description: "x-platform"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "XPlatform 处理流水线",
|
|
Description: "scan -> validate -> quality -> filter -> slice -> annotate -> package -> sample",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: datasetRoot},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "index",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "X 平台数据集索引",
|
|
Category: "DATASET",
|
|
Description: "index dataset",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "dataset", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 220, Y: 0},
|
|
},
|
|
{
|
|
ID: "validate",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "采集目录校验",
|
|
Category: "DATASET",
|
|
Description: "validate dataset",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "validated", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 440, Y: 0},
|
|
},
|
|
{
|
|
ID: "quality",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "视频质检",
|
|
Category: "VIDEO",
|
|
Description: "quality",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "quality", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "MIN_CLARITY", Value: "0.8"},
|
|
{Key: "MAX_SHAKE", Value: "0.1"},
|
|
{Key: "MAX_HAND_OCCLUSION", Value: "0.1"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 660, Y: 0},
|
|
},
|
|
{
|
|
ID: "filter",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "数据过滤",
|
|
Category: "VIDEO",
|
|
Description: "filter dataset",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "filtered", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATE_START", Value: "2026-02-01"},
|
|
{Key: "DATE_END", Value: "2026-04-05"},
|
|
{Key: "PLACE", Value: "蓟门里小区"},
|
|
{Key: "TYPE", Value: "家庭"},
|
|
{Key: "MIN_CLARITY", Value: "0.8"},
|
|
{Key: "MAX_SHAKE", Value: "0.1"},
|
|
{Key: "MAX_HAND_OCCLUSION", Value: "0.1"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 880, Y: 0},
|
|
},
|
|
{
|
|
ID: "slice",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "视频切片",
|
|
Category: "VIDEO",
|
|
Description: "slice video",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "clips", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "SEGMENT_DURATION_SECONDS", Value: "2"}},
|
|
},
|
|
Position: domain.Position{X: 1100, Y: 0},
|
|
},
|
|
{
|
|
ID: "annotate",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "视频标注",
|
|
Category: "VIDEO",
|
|
Description: "annotate clips",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "annotations", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "FRAME_INTERVAL_SECONDS", Value: "0.5"}},
|
|
},
|
|
Position: domain.Position{X: 1320, Y: 0},
|
|
},
|
|
{
|
|
ID: "package",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "成品打包",
|
|
Category: "VIDEO",
|
|
Description: "package results",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "packages", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 1540, Y: 0},
|
|
},
|
|
{
|
|
ID: "sample",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "抽检采样",
|
|
Category: "QA",
|
|
Description: "sample packages",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "samples", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "RULES_JSON", Value: `[{"id":"clarity-check","field":"quality.clarity","operator":"lt","value":1,"samplePercent":100},{"id":"confidence-check","field":"annotation.minConfidence","operator":"lt","value":1,"samplePercent":100}]`},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 1760, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-index", Source: "source", SourceHandle: "out-1", Target: "index", TargetHandle: "in-1"},
|
|
{ID: "index-validate", Source: "index", SourceHandle: "out-1", Target: "validate", TargetHandle: "in-1"},
|
|
{ID: "validate-quality", Source: "validate", SourceHandle: "out-1", Target: "quality", TargetHandle: "in-1"},
|
|
{ID: "quality-filter", Source: "quality", SourceHandle: "out-1", Target: "filter", TargetHandle: "in-1"},
|
|
{ID: "filter-slice", Source: "filter", SourceHandle: "out-1", Target: "slice", TargetHandle: "in-1"},
|
|
{ID: "slice-annotate", Source: "slice", SourceHandle: "out-1", Target: "annotate", TargetHandle: "in-1"},
|
|
{ID: "annotate-package", Source: "annotate", SourceHandle: "out-1", Target: "package", TargetHandle: "in-1"},
|
|
{ID: "package-sample", Source: "package", SourceHandle: "out-1", Target: "sample", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
completed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get run: %v", err)
|
|
}
|
|
if completed == nil || completed.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("expected completed run, got %#v", completed)
|
|
}
|
|
|
|
indexArtifact := mustArtifact(t, mustNodeExecution(t, completed, "index"), "dataset-index.json")
|
|
indexPayload := mustArtifactPayload(t, indexArtifact)
|
|
if indexPayload["collectionCount"] != float64(2) {
|
|
t.Fatalf("expected collectionCount=2, got %#v", indexPayload)
|
|
}
|
|
if indexPayload["videoCount"] != float64(3) {
|
|
t.Fatalf("expected videoCount=3, got %#v", indexPayload)
|
|
}
|
|
|
|
validateArtifact := mustArtifact(t, mustNodeExecution(t, completed, "validate"), "dataset-validation.json")
|
|
validatePayload := mustArtifactPayload(t, validateArtifact)
|
|
if validatePayload["validCollectionCount"] != float64(2) {
|
|
t.Fatalf("expected validCollectionCount=2, got %#v", validatePayload)
|
|
}
|
|
|
|
qualityArtifact := mustArtifact(t, mustNodeExecution(t, completed, "quality"), "quality-report.json")
|
|
qualityPayload := mustArtifactPayload(t, qualityArtifact)
|
|
if qualityPayload["videoCount"] != float64(3) {
|
|
t.Fatalf("expected quality videoCount=3, got %#v", qualityPayload)
|
|
}
|
|
if qualityPayload["passedCount"] != float64(2) {
|
|
t.Fatalf("expected passedCount=2, got %#v", qualityPayload)
|
|
}
|
|
|
|
filterArtifact := mustArtifact(t, mustNodeExecution(t, completed, "filter"), "filtered-videos.json")
|
|
filterPayload := mustArtifactPayload(t, filterArtifact)
|
|
if filterPayload["count"] != float64(1) {
|
|
t.Fatalf("expected filter count=1, got %#v", filterPayload)
|
|
}
|
|
|
|
sliceExecution := mustNodeExecution(t, completed, "slice")
|
|
clipsArtifact := mustArtifact(t, sliceExecution, "clips-manifest.json")
|
|
clipsPayload := mustArtifactPayload(t, clipsArtifact)
|
|
if clipsPayload["clipCount"] != float64(2) {
|
|
t.Fatalf("expected clipCount=2, got %#v", clipsPayload)
|
|
}
|
|
clipVideos := 0
|
|
for _, artifact := range sliceExecution.Artifacts {
|
|
if strings.HasSuffix(artifact.Name, ".mp4") {
|
|
clipVideos++
|
|
}
|
|
}
|
|
if clipVideos != 2 {
|
|
t.Fatalf("expected 2 clip videos, got %d", clipVideos)
|
|
}
|
|
|
|
annotateExecution := mustNodeExecution(t, completed, "annotate")
|
|
annotationsArtifact := mustArtifact(t, annotateExecution, "annotations-manifest.json")
|
|
annotationsPayload := mustArtifactPayload(t, annotationsArtifact)
|
|
if annotationsPayload["annotationCount"] != float64(2) {
|
|
t.Fatalf("expected annotationCount=2, got %#v", annotationsPayload)
|
|
}
|
|
|
|
packageExecution := mustNodeExecution(t, completed, "package")
|
|
packagesArtifact := mustArtifact(t, packageExecution, "packages-manifest.json")
|
|
packagesPayload := mustArtifactPayload(t, packagesArtifact)
|
|
if packagesPayload["packageCount"] != float64(2) {
|
|
t.Fatalf("expected packageCount=2, got %#v", packagesPayload)
|
|
}
|
|
packageFiles := 0
|
|
for _, artifact := range packageExecution.Artifacts {
|
|
if strings.Contains(artifact.Path, "/packages/") && (strings.HasSuffix(artifact.Name, ".json") || strings.HasSuffix(artifact.Name, ".mp4")) {
|
|
packageFiles++
|
|
}
|
|
}
|
|
if packageFiles < 8 {
|
|
t.Fatalf("expected packaged files to be emitted, got %d artifacts", packageFiles)
|
|
}
|
|
|
|
sampleArtifact := mustArtifact(t, mustNodeExecution(t, completed, "sample"), "audit-samples.json")
|
|
samplePayload := mustArtifactPayload(t, sampleArtifact)
|
|
if samplePayload["sampledCount"] != float64(2) {
|
|
t.Fatalf("expected sampledCount=2, got %#v", samplePayload)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeCommitsIncrementalSourceStateOnlyOnSuccessfulRuns(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
t.Run("commits state after successful run and next run only emits new files", func(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "增量项目", Description: "incremental"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "增量源工作流",
|
|
Description: "incremental source",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
runOne, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create first run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process first run: %v", err)
|
|
}
|
|
|
|
completedOne, err := dataStore.GetRun(ctx, runOne.ID)
|
|
if err != nil {
|
|
t.Fatalf("get first run: %v", err)
|
|
}
|
|
manifestOne := mustArtifact(t, mustNodeExecution(t, completedOne, "source"), "source-manifest.json")
|
|
manifestOnePayload := mustArtifactPayload(t, manifestOne)
|
|
if manifestOnePayload["newCount"] != float64(1) {
|
|
t.Fatalf("expected newCount=1 on first run, got %#v", manifestOnePayload)
|
|
}
|
|
|
|
state, err := dataStore.GetSourceState(ctx, workflow.ID, "source", string(manifestOnePayload["sourceFingerprint"].(string)))
|
|
if err != nil {
|
|
t.Fatalf("get source state: %v", err)
|
|
}
|
|
if state == nil || len(state.SeenFingerprints) != 1 {
|
|
t.Fatalf("expected committed source state after first run, got %#v", state)
|
|
}
|
|
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-b", "meta.json"), `{"project":"B"}`)
|
|
runTwo, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled)
|
|
if err != nil {
|
|
t.Fatalf("create second run: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process second run: %v", err)
|
|
}
|
|
|
|
completedTwo, err := dataStore.GetRun(ctx, runTwo.ID)
|
|
if err != nil {
|
|
t.Fatalf("get second run: %v", err)
|
|
}
|
|
manifestTwo := mustArtifact(t, mustNodeExecution(t, completedTwo, "source"), "source-manifest.json")
|
|
manifestTwoPayload := mustArtifactPayload(t, manifestTwo)
|
|
if manifestTwoPayload["listedCount"] != float64(2) {
|
|
t.Fatalf("expected listedCount=2 on second run, got %#v", manifestTwoPayload)
|
|
}
|
|
if manifestTwoPayload["newCount"] != float64(1) {
|
|
t.Fatalf("expected newCount=1 on second run, got %#v", manifestTwoPayload)
|
|
}
|
|
entries, ok := manifestTwoPayload["entries"].([]any)
|
|
if !ok || len(entries) != 1 {
|
|
t.Fatalf("expected one emitted entry on second run, got %#v", manifestTwoPayload["entries"])
|
|
}
|
|
entry, ok := entries[0].(map[string]any)
|
|
if !ok || !strings.Contains(fmt.Sprint(entry["path"]), "batch-b/meta.json") {
|
|
t.Fatalf("expected second run to emit only newly added file, got %#v", entries[0])
|
|
}
|
|
|
|
state, err = dataStore.GetSourceState(ctx, workflow.ID, "source", string(manifestTwoPayload["sourceFingerprint"].(string)))
|
|
if err != nil {
|
|
t.Fatalf("get source state after second run: %v", err)
|
|
}
|
|
if state == nil || len(state.SeenFingerprints) != 2 {
|
|
t.Fatalf("expected 2 seen fingerprints after second run, got %#v", state)
|
|
}
|
|
if state.LastNewCount != 1 || state.LastListedCount != 2 {
|
|
t.Fatalf("unexpected source state stats after second run: %#v", state)
|
|
}
|
|
})
|
|
|
|
t.Run("directory incremental mode emits entire newly discovered directories", func(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "目录增量项目", Description: "incremental-directory"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "DJI_001", "clip-a.mp4"), "a")
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "目录增量源工作流",
|
|
Description: "incremental directory source",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
{Key: "INCREMENTAL_UNIT", Value: "DIRECTORY"},
|
|
{Key: "INCREMENTAL_DIRECTORY_DEPTH", Value: "1"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
runOne, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create first run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process first run: %v", err)
|
|
}
|
|
|
|
completedOne, err := dataStore.GetRun(ctx, runOne.ID)
|
|
if err != nil {
|
|
t.Fatalf("get first run: %v", err)
|
|
}
|
|
manifestOne := mustArtifact(t, mustNodeExecution(t, completedOne, "source"), "source-manifest.json")
|
|
manifestOnePayload := mustArtifactPayload(t, manifestOne)
|
|
if manifestOnePayload["newCount"] != float64(2) {
|
|
t.Fatalf("expected first run to emit full batch-a directory, got %#v", manifestOnePayload["newCount"])
|
|
}
|
|
if manifestOnePayload["incrementalUnit"] != "DIRECTORY" {
|
|
t.Fatalf("expected directory incremental unit, got %#v", manifestOnePayload["incrementalUnit"])
|
|
}
|
|
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "DJI_001", "clip-b.mp4"), "b")
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-b", "meta.json"), `{"project":"B"}`)
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-b", "DJI_001", "clip-c.mp4"), "c")
|
|
runTwo, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled)
|
|
if err != nil {
|
|
t.Fatalf("create second run: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process second run: %v", err)
|
|
}
|
|
|
|
completedTwo, err := dataStore.GetRun(ctx, runTwo.ID)
|
|
if err != nil {
|
|
t.Fatalf("get second run: %v", err)
|
|
}
|
|
manifestTwo := mustArtifact(t, mustNodeExecution(t, completedTwo, "source"), "source-manifest.json")
|
|
manifestTwoPayload := mustArtifactPayload(t, manifestTwo)
|
|
if manifestTwoPayload["listedCount"] != float64(5) {
|
|
t.Fatalf("expected listedCount=5 on second run, got %#v", manifestTwoPayload["listedCount"])
|
|
}
|
|
if manifestTwoPayload["newCount"] != float64(2) {
|
|
t.Fatalf("expected newCount=2 on second run, got %#v", manifestTwoPayload["newCount"])
|
|
}
|
|
entries, ok := manifestTwoPayload["entries"].([]any)
|
|
if !ok || len(entries) != 2 {
|
|
t.Fatalf("expected two emitted entries for new directory, got %#v", manifestTwoPayload["entries"])
|
|
}
|
|
for _, rawEntry := range entries {
|
|
entry, ok := rawEntry.(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("unexpected entry payload: %#v", rawEntry)
|
|
}
|
|
if !strings.Contains(fmt.Sprint(entry["path"]), "batch-b/") {
|
|
t.Fatalf("expected second run to emit only newly discovered directory contents, got %#v", entry["path"])
|
|
}
|
|
}
|
|
|
|
state, err := dataStore.GetSourceState(ctx, workflow.ID, "source", string(manifestTwoPayload["sourceFingerprint"].(string)))
|
|
if err != nil {
|
|
t.Fatalf("get source state after second run: %v", err)
|
|
}
|
|
if state == nil || len(state.SeenFingerprints) != 2 {
|
|
t.Fatalf("expected two seen directory keys after second run, got %#v", state)
|
|
}
|
|
if state.IncrementalUnit != "DIRECTORY" || state.DirectoryDepth != 1 {
|
|
t.Fatalf("expected committed directory incremental state, got %#v", state)
|
|
}
|
|
})
|
|
|
|
t.Run("does not commit state when downstream run fails", func(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "失败增量项目", Description: "incremental-fail"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
sourceRoot := t.TempDir()
|
|
writeFile(t, filepath.Join(sourceRoot, "batch-a", "meta.json"), `{"project":"A"}`)
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "增量失败工作流",
|
|
Description: "incremental fail",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "LOCAL"},
|
|
{Key: "SOURCE_PATH", Value: sourceRoot},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "fail",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "fail",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "echo boom >&2; exit 1"}},
|
|
},
|
|
Position: domain.Position{X: 240, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-fail", Source: "source", SourceHandle: "out-1", Target: "fail", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process failed run: %v", err)
|
|
}
|
|
|
|
failedRun, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get failed run: %v", err)
|
|
}
|
|
if failedRun == nil || failedRun.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %#v", failedRun)
|
|
}
|
|
manifest := mustArtifact(t, mustNodeExecution(t, failedRun, "source"), "source-manifest.json")
|
|
manifestPayload := mustArtifactPayload(t, manifest)
|
|
state, err := dataStore.GetSourceState(ctx, workflow.ID, "source", string(manifestPayload["sourceFingerprint"].(string)))
|
|
if err != nil {
|
|
t.Fatalf("get source state after failed run: %v", err)
|
|
}
|
|
if state != nil {
|
|
t.Fatalf("expected no committed source state after failed run, got %#v", state)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPrepareExecutionNodeResolvesStorageConnectionSecrets(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 主连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://gz.bcebos.com",
|
|
AccessKeyID: "ak-runtime-1234",
|
|
SecretAccessKey: "sk-runtime-5678",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
workflow := &domain.WorkflowRecord{
|
|
ID: "workflow-scan",
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
ScanMode: "INCREMENTAL",
|
|
},
|
|
}
|
|
node := domain.WorkflowNode{
|
|
ID: "node-bos-scan-runtime",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "BOS"},
|
|
},
|
|
},
|
|
}
|
|
|
|
prepared, err := runtime.prepareExecutionNode(ctx, workflow, node)
|
|
if err != nil {
|
|
t.Fatalf("prepare execution node: %v", err)
|
|
}
|
|
params := nodeHyperparamsMap(prepared)
|
|
if params["BOS_ENDPOINT"] != "https://gz.bcebos.com" {
|
|
t.Fatalf("expected resolved endpoint, got %q", params["BOS_ENDPOINT"])
|
|
}
|
|
if params["BOS_ACCESS_KEY_ID"] != "ak-runtime-1234" || params["BOS_SECRET_ACCESS_KEY"] != "sk-runtime-5678" {
|
|
t.Fatalf("expected resolved bos secrets, got %#v", params)
|
|
}
|
|
if params["SOURCE_PATH"] != "bos://datastorage/algo/X-platform-test" {
|
|
t.Fatalf("expected source path from bucket/prefix, got %q", params["SOURCE_PATH"])
|
|
}
|
|
}
|
|
|
|
func TestPrepareExecutionNodeUsesRuntimeGlobalBOSEndpointForConnectionWithoutEndpoint(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 全局 endpoint 连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
AccessKeyID: "ak-runtime-default",
|
|
SecretAccessKey: "sk-runtime-default",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
runtime.globalBOSEndpoint = "https://gz.bcebos.com"
|
|
|
|
workflow := &domain.WorkflowRecord{
|
|
ID: "workflow-default-bos",
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/default-endpoint",
|
|
ScanMode: "INCREMENTAL",
|
|
},
|
|
}
|
|
node := domain.WorkflowNode{
|
|
ID: "node-default-bos",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Hyperparams: []domain.HyperParam{{Key: "DATA_SOURCE", Value: "BOS"}},
|
|
},
|
|
}
|
|
|
|
prepared, err := runtime.prepareExecutionNode(ctx, workflow, node)
|
|
if err != nil {
|
|
t.Fatalf("prepare execution node: %v", err)
|
|
}
|
|
params := nodeHyperparamsMap(prepared)
|
|
if params["BOS_ENDPOINT"] != "https://gz.bcebos.com" {
|
|
t.Fatalf("expected runtime global bos endpoint, got %q", params["BOS_ENDPOINT"])
|
|
}
|
|
if params["BOS_ACCESS_KEY_ID"] != "ak-runtime-default" || params["BOS_SECRET_ACCESS_KEY"] != "sk-runtime-default" {
|
|
t.Fatalf("expected resolved bos secrets, got %#v", params)
|
|
}
|
|
}
|
|
|
|
func TestPrepareExecutionNodeKeepsEmptyEndpointWithoutGlobalBOSFallback(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 无 endpoint 连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
AccessKeyID: "ak-runtime-empty-endpoint",
|
|
SecretAccessKey: "sk-runtime-empty-endpoint",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
workflow := &domain.WorkflowRecord{
|
|
ID: "workflow-empty-bos-endpoint",
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/empty-endpoint",
|
|
ScanMode: "INCREMENTAL",
|
|
},
|
|
}
|
|
node := domain.WorkflowNode{
|
|
ID: "node-empty-bos-endpoint",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Hyperparams: []domain.HyperParam{{Key: "DATA_SOURCE", Value: "BOS"}},
|
|
},
|
|
}
|
|
|
|
prepared, err := runtime.prepareExecutionNode(ctx, workflow, node)
|
|
if err != nil {
|
|
t.Fatalf("prepare execution node: %v", err)
|
|
}
|
|
params := nodeHyperparamsMap(prepared)
|
|
if params["BOS_ENDPOINT"] != "" {
|
|
t.Fatalf("expected empty endpoint without global fallback, got %q", params["BOS_ENDPOINT"])
|
|
}
|
|
if params["BOS_ACCESS_KEY_ID"] != "ak-runtime-empty-endpoint" || params["BOS_SECRET_ACCESS_KEY"] != "sk-runtime-empty-endpoint" {
|
|
t.Fatalf("expected resolved bos secrets, got %#v", params)
|
|
}
|
|
}
|
|
|
|
func TestPrepareExecutionNodeResolvesNodeLevelStorageConnectionSecrets(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 节点连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://su.bcebos.com",
|
|
AccessKeyID: "ak-node-1234",
|
|
SecretAccessKey: "sk-node-5678",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
node := domain.WorkflowNode{
|
|
ID: "node-oss-source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "OSS 数据源",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
Prefix: "factory/output",
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "SOURCE_PATH", Value: ""},
|
|
{Key: "DATA_SOURCE", Value: "BOS"},
|
|
},
|
|
},
|
|
}
|
|
|
|
prepared, err := runtime.prepareExecutionNode(ctx, nil, node)
|
|
if err != nil {
|
|
t.Fatalf("prepare execution node: %v", err)
|
|
}
|
|
params := nodeHyperparamsMap(prepared)
|
|
if params["DATA_SOURCE"] != "BOS" {
|
|
t.Fatalf("expected BOS data source, got %q", params["DATA_SOURCE"])
|
|
}
|
|
if params["BOS_ENDPOINT"] != "https://su.bcebos.com" {
|
|
t.Fatalf("expected resolved endpoint, got %q", params["BOS_ENDPOINT"])
|
|
}
|
|
if params["BOS_ACCESS_KEY_ID"] != "ak-node-1234" || params["BOS_SECRET_ACCESS_KEY"] != "sk-node-5678" {
|
|
t.Fatalf("expected resolved node bos secrets, got %#v", params)
|
|
}
|
|
if params["SOURCE_PATH"] != "bos://datastorage/factory/output" {
|
|
t.Fatalf("expected source path from node bucket/prefix, got %q", params["SOURCE_PATH"])
|
|
}
|
|
}
|
|
|
|
func TestPrepareExecutionNodeResolvesMongoConnectionSecrets(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务 MongoDB",
|
|
URI: "mongodb://business-mongo:27017",
|
|
Database: "business_data",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
|
|
node := domain.WorkflowNode{
|
|
ID: "node-mongo-write",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "MongoDB 写入",
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "MONGO_CONNECTION_ID", Value: connection.ID},
|
|
{Key: "MONGO_COLLECTION", Value: "qualityReports"},
|
|
},
|
|
},
|
|
}
|
|
|
|
prepared, err := runtime.prepareExecutionNode(ctx, nil, node)
|
|
if err != nil {
|
|
t.Fatalf("prepare execution node: %v", err)
|
|
}
|
|
params := nodeHyperparamsMap(prepared)
|
|
if params["MONGO_URI"] != "mongodb://business-mongo:27017" {
|
|
t.Fatalf("expected resolved mongo uri, got %q", params["MONGO_URI"])
|
|
}
|
|
if params["MONGO_DB"] != "business_data" {
|
|
t.Fatalf("expected resolved mongo db, got %q", params["MONGO_DB"])
|
|
}
|
|
if params["MONGO_TARGET"] != "business" {
|
|
t.Fatalf("expected mongo target business, got %q", params["MONGO_TARGET"])
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessAvailableDispatchesUpToGlobalLimit(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
settings.Worker.MaxConcurrentRuns = 2
|
|
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
|
t.Fatalf("upsert settings: %v", err)
|
|
}
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Dispatch 项目", Description: "dispatch"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
createWorkflow := func(name string) *domain.WorkflowRecord {
|
|
t.Helper()
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: name,
|
|
Description: "dispatch test",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "command",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "command",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow %s: %v", name, err)
|
|
}
|
|
return workflow
|
|
}
|
|
|
|
workflowA := createWorkflow("并发工作流 A")
|
|
workflowB := createWorkflow("并发工作流 B")
|
|
runA, err := dataStore.CreateRun(ctx, workflowA.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run A: %v", err)
|
|
}
|
|
runB, err := dataStore.CreateRun(ctx, workflowB.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run B: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available: %v", err)
|
|
}
|
|
|
|
runs, err := dataStore.ListRuns(ctx, "")
|
|
if err != nil {
|
|
t.Fatalf("list runs: %v", err)
|
|
}
|
|
runningCount := 0
|
|
for _, run := range runs {
|
|
if run.Status == domain.RunStatusRunning {
|
|
runningCount++
|
|
}
|
|
}
|
|
if runningCount != 2 {
|
|
t.Fatalf("expected 2 running runs after dispatch, got %d", runningCount)
|
|
}
|
|
|
|
waitForRunStatus(t, ctx, dataStore, runA.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
waitForRunStatus(t, ctx, dataStore, runB.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
}
|
|
|
|
func TestRuntimeProcessAvailableRespectsWorkflowConcurrencyOverride(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
settings.Worker.MaxConcurrentRuns = 3
|
|
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
|
t.Fatalf("upsert settings: %v", err)
|
|
}
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Override 项目", Description: "override"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
limitedWorkflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "受限工作流",
|
|
Description: "limited",
|
|
Type: domain.WorkflowTypeBatch,
|
|
MaxConcurrentRuns: 1,
|
|
Nodes: []domain.WorkflowNode{{
|
|
ID: "command",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "command",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create limited workflow: %v", err)
|
|
}
|
|
freeWorkflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "自由工作流",
|
|
Description: "free",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{{
|
|
ID: "command",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "command",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create free workflow: %v", err)
|
|
}
|
|
|
|
runLimitedA, err := dataStore.CreateRun(ctx, limitedWorkflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create limited run A: %v", err)
|
|
}
|
|
runLimitedB, err := dataStore.CreateRun(ctx, limitedWorkflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create limited run B: %v", err)
|
|
}
|
|
runFree, err := dataStore.CreateRun(ctx, freeWorkflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create free run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available: %v", err)
|
|
}
|
|
|
|
limitedRunAState, err := dataStore.GetRun(ctx, runLimitedA.ID)
|
|
if err != nil {
|
|
t.Fatalf("get limited run A: %v", err)
|
|
}
|
|
limitedRunBState, err := dataStore.GetRun(ctx, runLimitedB.ID)
|
|
if err != nil {
|
|
t.Fatalf("get limited run B: %v", err)
|
|
}
|
|
freeRunState, err := dataStore.GetRun(ctx, runFree.ID)
|
|
if err != nil {
|
|
t.Fatalf("get free run: %v", err)
|
|
}
|
|
if limitedRunAState.Status != domain.RunStatusRunning {
|
|
t.Fatalf("expected first limited run to be running, got %s", limitedRunAState.Status)
|
|
}
|
|
if limitedRunBState.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected second limited run to remain queued, got %s", limitedRunBState.Status)
|
|
}
|
|
if freeRunState.Status != domain.RunStatusRunning {
|
|
t.Fatalf("expected free workflow run to be running, got %s", freeRunState.Status)
|
|
}
|
|
|
|
waitForRunStatus(t, ctx, dataStore, runLimitedA.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
waitForRunStatus(t, ctx, dataStore, runFree.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available second pass: %v", err)
|
|
}
|
|
waitForRunStatus(t, ctx, dataStore, runLimitedB.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
}
|
|
|
|
func TestRuntimeProcessAvailableSkipsStreamingWorkflowWithMissingProject(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "有效项目", Description: "valid"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
validWorkflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "正常工作流",
|
|
Description: "valid workflow",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "command",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "command",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create valid workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, validWorkflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create queued run: %v", err)
|
|
}
|
|
|
|
orphanWorkflow := domain.WorkflowRecord{
|
|
ID: "workflow-orphan-streaming",
|
|
ProjectID: "project-missing",
|
|
Name: "孤儿增量工作流",
|
|
Description: "orphan streaming workflow",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
Schedule: &domain.WorkflowSchedule{Interval: 30},
|
|
Nodes: []domain.WorkflowNode{},
|
|
Edges: []domain.WorkflowEdge{},
|
|
Status: domain.WorkflowStatusDraft,
|
|
CreatedAt: nowISO(),
|
|
UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertWorkflow(ctx, orphanWorkflow); err != nil {
|
|
t.Fatalf("upsert orphan workflow: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available should skip orphan workflow instead of failing: %v", err)
|
|
}
|
|
|
|
waitForRunStatus(t, ctx, dataStore, run.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
}
|
|
|
|
func TestShouldEnqueueScheduledRunEveryHoursUsesAnchorTime(t *testing.T) {
|
|
workflow := domain.WorkflowRecord{
|
|
ID: "workflow-hours",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
Schedule: &domain.WorkflowSchedule{
|
|
Mode: domain.WorkflowScheduleModeEveryHours,
|
|
IntervalHours: 4,
|
|
AnchorAt: "2026-04-21T10:15:00Z",
|
|
},
|
|
}
|
|
|
|
if !shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 21, 10, 15, 0, 0, time.UTC)) {
|
|
t.Fatalf("expected first hourly window to enqueue at anchor time")
|
|
}
|
|
|
|
workflow.LastRunAt = "2026-04-21T10:15:00Z"
|
|
if shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 21, 14, 14, 59, 0, time.UTC)) {
|
|
t.Fatalf("expected hourly schedule to wait until full interval elapsed")
|
|
}
|
|
if !shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 21, 14, 15, 0, 0, time.UTC)) {
|
|
t.Fatalf("expected hourly schedule to enqueue on the next anchored slot")
|
|
}
|
|
}
|
|
|
|
func TestShouldEnqueueScheduledRunEveryDaysAtFixedTimeUsesAnchorDate(t *testing.T) {
|
|
workflow := domain.WorkflowRecord{
|
|
ID: "workflow-days",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
Schedule: &domain.WorkflowSchedule{
|
|
Mode: domain.WorkflowScheduleModeEveryDaysAtTime,
|
|
DayInterval: 2,
|
|
TimeOfDay: "18:30",
|
|
Timezone: "Asia/Shanghai",
|
|
AnchorAt: "2026-04-21T09:00:00Z",
|
|
},
|
|
}
|
|
|
|
if shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 21, 10, 29, 59, 0, time.UTC)) {
|
|
t.Fatalf("expected daily schedule to wait until configured local time")
|
|
}
|
|
if !shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 21, 10, 30, 0, 0, time.UTC)) {
|
|
t.Fatalf("expected daily schedule to enqueue at configured local time")
|
|
}
|
|
|
|
workflow.LastRunAt = "2026-04-21T10:30:00Z"
|
|
if shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 23, 10, 29, 59, 0, time.UTC)) {
|
|
t.Fatalf("expected next two-day slot to remain pending before local HH:mm")
|
|
}
|
|
if !shouldEnqueueScheduledRun(workflow, time.Date(2026, time.April, 23, 10, 30, 0, 0, time.UTC)) {
|
|
t.Fatalf("expected next two-day slot to enqueue at configured local HH:mm")
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessRunResumesQueuedPartialRunFromCompletedDependencies(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "恢复项目", Description: "resume"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "恢复工作流",
|
|
Description: "resume",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "payload", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "DATA_SOURCE", Value: "INLINE"}, {Key: "SOURCE_TEXT", Value: "{\"items\":[{\"name\":\"sample\"}],\"count\":1}"}},
|
|
},
|
|
},
|
|
{
|
|
ID: "transform",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "JSON 映射重命名",
|
|
Category: "DATA",
|
|
Description: "transform",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "FROM_KEY", Value: "items"}, {Key: "TO_KEY", Value: "entries"}},
|
|
},
|
|
},
|
|
{
|
|
ID: "upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "sink",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "printf '{\"ok\":true}' > done.json"}},
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-transform", Source: "source", SourceHandle: "out-1", Target: "transform", TargetHandle: "in-1"},
|
|
{ID: "transform-upload", Source: "transform", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Status = domain.RunStatusQueued
|
|
run.CurrentNodeID = ""
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{
|
|
{
|
|
NodeID: "source",
|
|
NodeLabel: "真实数据源",
|
|
Status: domain.NodeExecutionStatusCompleted,
|
|
StartedAt: nowISO(),
|
|
EndedAt: nowISO(),
|
|
Logs: []domain.LogEntryRecord{},
|
|
Artifacts: []domain.ExecutionArtifact{{ID: "artifact-source", RunID: run.ID, NodeID: "source", PortID: "out-1", Name: "payload.json", Type: domain.PortTypeJSON, Path: filepath.Join(t.TempDir(), "payload.json"), Data: map[string]any{"items": []any{map[string]any{"name": "sample"}}, "count": 1}}},
|
|
DataRefs: []domain.DataRefRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
},
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
completed := waitForRunStatus(t, ctx, dataStore, run.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
if execution := mustNodeExecution(t, completed, "transform"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected transform to complete after resume, got %s", execution.Status)
|
|
}
|
|
if execution := mustNodeExecution(t, completed, "upload"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected upload to complete after resume, got %s", execution.Status)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeReconcileActiveRunsRequeuesClaimedRunWithoutJobs(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "恢复项目", Description: "recover"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "僵尸运行恢复",
|
|
Description: "recover",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{{ID: "source", Type: "operator", Data: domain.WorkflowNodeData{Label: "真实数据源", Outputs: []domain.PortDefinition{{ID: "out-1", Name: "payload", Type: domain.PortTypeJSON}}, Hyperparams: []domain.HyperParam{{Key: "DATA_SOURCE", Value: "INLINE"}, {Key: "SOURCE_TEXT", Value: "{\"ok\":true}"}}}}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.StartedAt = time.Now().UTC().Add(-2 * time.Minute).Format(time.RFC3339Nano)
|
|
run.UpdatedAt = run.StartedAt
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{}
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
runtime := newTestRuntimeWithLauncher(t, dataStore, testLauncher{})
|
|
if err := runtime.reconcileActiveRuns(ctx); err != nil {
|
|
t.Fatalf("reconcile active runs: %v", err)
|
|
}
|
|
|
|
recovered, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get recovered run: %v", err)
|
|
}
|
|
if recovered == nil || recovered.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected run to be re-queued, got %#v", recovered)
|
|
}
|
|
if recovered.StartedAt != "" {
|
|
t.Fatalf("expected startedAt cleared after requeue, got %q", recovered.StartedAt)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeReconcileActiveRunsRecoversSucceededNodeAndResumesDownstream(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "恢复项目", Description: "recover success"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "成功节点恢复",
|
|
Description: "recover success",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "真实数据源",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "payload", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "DATA_SOURCE", Value: "INLINE"}, {Key: "SOURCE_TEXT", Value: "{\"items\":[{\"name\":\"sample\"}],\"count\":1}"}},
|
|
},
|
|
},
|
|
{
|
|
ID: "annotate",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "视频标注",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "annotations", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{{Key: "MODEL", Value: "stub"}},
|
|
},
|
|
},
|
|
{
|
|
ID: "upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "printf '{\"ok\":true}' > done.json"}},
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-annotate", Source: "source", SourceHandle: "out-1", Target: "annotate", TargetHandle: "in-1"},
|
|
{ID: "annotate-upload", Source: "annotate", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
sourceArtifact := domain.ExecutionArtifact{
|
|
ID: "artifact-source",
|
|
RunID: run.ID,
|
|
NodeID: "source",
|
|
PortID: "out-1",
|
|
Name: "payload.json",
|
|
Type: domain.PortTypeJSON,
|
|
Path: filepath.Join(t.TempDir(), "payload.json"),
|
|
Data: map[string]any{"items": []any{map[string]any{"name": "sample"}}, "count": 1},
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.CurrentNodeID = "annotate"
|
|
run.StartedAt = time.Now().UTC().Add(-2 * time.Minute).Format(time.RFC3339Nano)
|
|
run.UpdatedAt = run.StartedAt
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{
|
|
{
|
|
NodeID: "source",
|
|
NodeLabel: "真实数据源",
|
|
Status: domain.NodeExecutionStatusCompleted,
|
|
StartedAt: run.StartedAt,
|
|
EndedAt: run.StartedAt,
|
|
Logs: []domain.LogEntryRecord{},
|
|
Artifacts: []domain.ExecutionArtifact{sourceArtifact},
|
|
DataRefs: []domain.DataRefRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
},
|
|
{
|
|
NodeID: "annotate",
|
|
NodeLabel: "视频标注",
|
|
Status: domain.NodeExecutionStatusRunning,
|
|
StartedAt: run.StartedAt,
|
|
Logs: []domain.LogEntryRecord{{Timestamp: run.StartedAt, Level: "INFO", Message: "Starting node execution", Operator: "视频标注"}},
|
|
Artifacts: []domain.ExecutionArtifact{},
|
|
DataRefs: []domain.DataRefRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
},
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
launcher := testLauncher{
|
|
launch: func(ctx context.Context, input k8s.LaunchInput) (*k8s.LaunchResult, error) {
|
|
env := append([]string{}, os.Environ()...)
|
|
keys := make([]string, 0, len(input.Env))
|
|
for key := range input.Env {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
env = append(env, "RUN_ID="+input.RunID, "WORKFLOW_ID="+input.WorkflowID, "NODE_ID="+input.NodeID, "NODE_LABEL="+input.NodeLabel)
|
|
for _, key := range keys {
|
|
env = append(env, key+"="+input.Env[key])
|
|
}
|
|
cmd := exec.CommandContext(ctx, "python3", operatorRunnerPath(t))
|
|
cmd.Env = env
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", err, string(output))
|
|
}
|
|
return &k8s.LaunchResult{
|
|
Reference: &k8s.JobReference{JobName: fmt.Sprintf("%s-%s", input.RunID, input.NodeID), Namespace: "droboticflow"},
|
|
Output: string(output),
|
|
}, nil
|
|
},
|
|
inspect: func(ctx context.Context, runID string, nodeID string) (*k8s.JobInspection, error) {
|
|
if runID == run.ID && nodeID == "annotate" {
|
|
return &k8s.JobInspection{
|
|
Reference: &k8s.JobReference{JobName: "job-annotate", Namespace: "droboticflow"},
|
|
State: k8s.JobStateSucceeded,
|
|
Output: "DROBOTICFLOW_RESULT={\"artifacts\":[{\"id\":\"annotation-1\",\"runId\":\"" + run.ID + "\",\"nodeId\":\"annotate\",\"portId\":\"out-1\",\"name\":\"annotation.json\",\"type\":\"json\",\"size\":\"1 KB\",\"path\":\"/tmp/annotation.json\",\"data\":{\"items\":[{\"label\":\"walking\"}],\"count\":1}}],\"logs\":[],\"resourceSeries\":[]}",
|
|
}, nil
|
|
}
|
|
return &k8s.JobInspection{State: k8s.JobStateMissing}, nil
|
|
},
|
|
}
|
|
|
|
runtime := newTestRuntimeWithLauncher(t, dataStore, launcher)
|
|
if err := runtime.reconcileActiveRuns(ctx); err != nil {
|
|
t.Fatalf("reconcile active runs: %v", err)
|
|
}
|
|
recovered, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get recovered run: %v", err)
|
|
}
|
|
if recovered == nil || recovered.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected recovered run queued, got %#v", recovered)
|
|
}
|
|
if execution := mustNodeExecution(t, recovered, "annotate"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected annotate completed after recovery, got %s", execution.Status)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once after recovery: %v", err)
|
|
}
|
|
completed := waitForRunStatus(t, ctx, dataStore, run.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
if execution := mustNodeExecution(t, completed, "upload"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected downstream upload completed, got %s", execution.Status)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessRunExecutesIndependentNodesInParallel(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
settings.Worker.MaxConcurrentRuns = 3
|
|
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
|
t.Fatalf("upsert settings: %v", err)
|
|
}
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Parallel DAG 项目", Description: "parallel dag"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "并行分支工作流",
|
|
Description: "parallel dag",
|
|
Type: domain.WorkflowTypeBatch,
|
|
MaxConcurrentRuns: 3,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "OSS 数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "payload", Type: domain.PortTypeJSON}},
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "INLINE"},
|
|
{Key: "SOURCE_TEXT", Value: "parallel"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "branch-a",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "branch a",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result-a", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"branch\":\"a\"}' > result-a.json"}},
|
|
},
|
|
Position: domain.Position{X: 200, Y: -120},
|
|
},
|
|
{
|
|
ID: "branch-b",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "branch b",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result-b", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"branch\":\"b\"}' > result-b.json"}},
|
|
},
|
|
Position: domain.Position{X: 200, Y: 0},
|
|
},
|
|
{
|
|
ID: "branch-c",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "branch c",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result-c", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 2 && printf '{\"branch\":\"c\"}' > result-c.json"}},
|
|
},
|
|
Position: domain.Position{X: 200, Y: 120},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "source-a", Source: "source", SourceHandle: "out-1", Target: "branch-a", TargetHandle: "in-1"},
|
|
{ID: "source-b", Source: "source", SourceHandle: "out-1", Target: "branch-b", TargetHandle: "in-1"},
|
|
{ID: "source-c", Source: "source", SourceHandle: "out-1", Target: "branch-c", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available: %v", err)
|
|
}
|
|
|
|
runningRun := waitForRunningNodeCount(t, ctx, dataStore, run.ID, 3, 5*time.Second)
|
|
if runningRun.Status != domain.RunStatusRunning {
|
|
t.Fatalf("expected run to be running while branches execute, got %s", runningRun.Status)
|
|
}
|
|
|
|
completed := waitForRunStatus(t, ctx, dataStore, run.ID, domain.RunStatusCompleted, 10*time.Second)
|
|
for _, nodeID := range []string{"branch-a", "branch-b", "branch-c"} {
|
|
if execution := mustNodeExecution(t, completed, nodeID); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected node %s to complete, got %s", nodeID, execution.Status)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRuntimeRerunSubtreeOnlyExecutesSelectedSubgraph(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "子链路重跑项目", Description: "rerun subtree"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "子链路重跑工作流",
|
|
Description: "rerun subtree",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "start-a",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "FLOW",
|
|
Description: "branch a start",
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: `printf '{"branch":"a"}' > branch-a.json`}},
|
|
},
|
|
},
|
|
{
|
|
ID: "downstream-a",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "FLOW",
|
|
Description: "branch a end",
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: `printf '{"branch":"a-downstream"}' > branch-a-downstream.json`}},
|
|
},
|
|
},
|
|
{
|
|
ID: "start-b",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "FLOW",
|
|
Description: "branch b start",
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: `printf '{"branch":"b"}' > branch-b.json`}},
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "a-to-downstream", Source: "start-a", Target: "downstream-a"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Trigger = domain.RunTriggerRerunSubtree
|
|
run.RestartFromNodeID = "start-a"
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{}
|
|
run.Status = domain.RunStatusQueued
|
|
run.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert rerun subtree run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available: %v", err)
|
|
}
|
|
|
|
completed := waitForRunStatus(t, ctx, dataStore, run.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
if execution := mustNodeExecution(t, completed, "start-a"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected start-a to complete, got %s", execution.Status)
|
|
}
|
|
if execution := mustNodeExecution(t, completed, "downstream-a"); execution.Status != domain.NodeExecutionStatusCompleted {
|
|
t.Fatalf("expected downstream-a to complete, got %s", execution.Status)
|
|
}
|
|
for _, execution := range completed.NodeExecutions {
|
|
if execution.NodeID == "start-b" {
|
|
t.Fatalf("expected unrelated branch start-b not to execute, got %#v", execution)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessRunKeepsClaimedRunRunningWhenExistingNodeExecutionIsActive(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
runtime := &Runtime{
|
|
store: dataStore,
|
|
operatorRunnerImage: "runner:latest",
|
|
workspaceRoot: "/workspace",
|
|
launcher: testLauncher{},
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: "",
|
|
Name: "resume-active-node",
|
|
Description: "resume active execution",
|
|
Nodes: []domain.WorkflowNode{{ID: "source", Type: "operator", Data: domain.WorkflowNodeData{Label: "Source"}}},
|
|
Edges: []domain.WorkflowEdge{},
|
|
Type: domain.WorkflowTypeBatch,
|
|
Kind: domain.WorkflowKindStandard,
|
|
Schedule: nil,
|
|
ScanConfig: nil,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Status = domain.RunStatusQueued
|
|
run.CurrentNodeID = "source"
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{
|
|
{
|
|
NodeID: "source",
|
|
NodeLabel: "Source",
|
|
Status: domain.NodeExecutionStatusRunning,
|
|
StartedAt: nowISO(),
|
|
},
|
|
}
|
|
run.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
processed, err := runtime.ProcessOnce(ctx)
|
|
if err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
if processed == nil {
|
|
t.Fatalf("expected processed run")
|
|
}
|
|
if processed.Status != domain.RunStatusRunning {
|
|
t.Fatalf("expected running status, got %s", processed.Status)
|
|
}
|
|
if processed.CurrentNodeID != "source" {
|
|
t.Fatalf("expected currentNodeID=source, got %s", processed.CurrentNodeID)
|
|
}
|
|
if execution := mustNodeExecution(t, processed, "source"); execution.Status != domain.NodeExecutionStatusRunning {
|
|
t.Fatalf("expected source execution running, got %s", execution.Status)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessRunFailsWhenWorkflowTimeoutReached(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Timeout 项目", Description: "timeout"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "超时工作流",
|
|
Description: "timeout",
|
|
Type: domain.WorkflowTypeBatch,
|
|
RunTimeoutSeconds: 1,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "slow-node",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "slow",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 3 && printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
failed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get failed run: %v", err)
|
|
}
|
|
if failed == nil || failed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %#v", failed)
|
|
}
|
|
if !strings.Contains(failed.Error, "超时") {
|
|
t.Fatalf("expected timeout error, got %q", failed.Error)
|
|
}
|
|
execution := mustNodeExecution(t, failed, "slow-node")
|
|
if execution.Status != domain.NodeExecutionStatusFailed {
|
|
t.Fatalf("expected failed node execution, got %s", execution.Status)
|
|
}
|
|
|
|
storedWorkflow, err := dataStore.GetWorkflow(ctx, workflow.ID)
|
|
if err != nil {
|
|
t.Fatalf("get workflow: %v", err)
|
|
}
|
|
if storedWorkflow == nil || storedWorkflow.Status != domain.WorkflowStatusFailed {
|
|
t.Fatalf("expected failed workflow, got %#v", storedWorkflow)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessRunAutoRetriesFailedWorkflows(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
markerPath := filepath.Join(t.TempDir(), "retry-marker")
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Retry 项目", Description: "retry"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "自动重试工作流",
|
|
Description: "retry",
|
|
Type: domain.WorkflowTypeBatch,
|
|
MaxRetryAttempts: 1,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "flaky-node",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "flaky",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{
|
|
Key: "COMMAND",
|
|
Value: "if [ ! -f '" + markerPath + "' ]; then touch '" + markerPath + "' && exit 1; fi; printf '{\"ok\":true}' > result.json",
|
|
}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
initialRun, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create initial run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("first process once: %v", err)
|
|
}
|
|
|
|
firstAttempt, err := dataStore.GetRun(ctx, initialRun.ID)
|
|
if err != nil {
|
|
t.Fatalf("get first attempt: %v", err)
|
|
}
|
|
if firstAttempt == nil || firstAttempt.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected first attempt to fail, got %#v", firstAttempt)
|
|
}
|
|
if firstAttempt.Attempt != 1 {
|
|
t.Fatalf("expected first attempt number to be 1, got %d", firstAttempt.Attempt)
|
|
}
|
|
|
|
runs, err := dataStore.ListRuns(ctx, "")
|
|
if err != nil {
|
|
t.Fatalf("list runs: %v", err)
|
|
}
|
|
if len(runs) != 2 {
|
|
t.Fatalf("expected original + retry run, got %d", len(runs))
|
|
}
|
|
|
|
var retryRun domain.RunRecord
|
|
foundRetry := false
|
|
for _, candidate := range runs {
|
|
if candidate.ID == initialRun.ID {
|
|
continue
|
|
}
|
|
retryRun = candidate
|
|
foundRetry = true
|
|
break
|
|
}
|
|
if !foundRetry {
|
|
t.Fatalf("expected retry run to be created")
|
|
}
|
|
if retryRun.Trigger != domain.RunTriggerRetry {
|
|
t.Fatalf("expected retry trigger, got %s", retryRun.Trigger)
|
|
}
|
|
if retryRun.Attempt != 2 {
|
|
t.Fatalf("expected retry attempt number to be 2, got %d", retryRun.Attempt)
|
|
}
|
|
if retryRun.ParentRunID != initialRun.ID {
|
|
t.Fatalf("expected retry parent run %s, got %s", initialRun.ID, retryRun.ParentRunID)
|
|
}
|
|
if retryRun.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected retry run to be queued, got %s", retryRun.Status)
|
|
}
|
|
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("second process once: %v", err)
|
|
}
|
|
|
|
completedRetry, err := dataStore.GetRun(ctx, retryRun.ID)
|
|
if err != nil {
|
|
t.Fatalf("get retry run: %v", err)
|
|
}
|
|
if completedRetry == nil || completedRetry.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("expected retry run to complete, got %#v", completedRetry)
|
|
}
|
|
|
|
finalWorkflow, err := dataStore.GetWorkflow(ctx, workflow.ID)
|
|
if err != nil {
|
|
t.Fatalf("get final workflow: %v", err)
|
|
}
|
|
if finalWorkflow == nil || finalWorkflow.Status != domain.WorkflowStatusCompleted {
|
|
t.Fatalf("expected workflow to recover and complete, got %#v", finalWorkflow)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessRunFailsWhenNodeTimeoutReached(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Node Timeout 项目", Description: "node timeout"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "节点超时工作流",
|
|
Description: "node timeout",
|
|
Type: domain.WorkflowTypeBatch,
|
|
NodeTimeoutSeconds: 1,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "slow-node",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "slow",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{Key: "COMMAND", Value: "sleep 3 && printf '{\"ok\":true}' > result.json"}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("process once: %v", err)
|
|
}
|
|
|
|
failed, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get failed run: %v", err)
|
|
}
|
|
if failed == nil || failed.Status != domain.RunStatusFailed {
|
|
t.Fatalf("expected failed run, got %#v", failed)
|
|
}
|
|
if !strings.Contains(failed.Error, "节点执行超时") {
|
|
t.Fatalf("expected node timeout error, got %q", failed.Error)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeProcessAvailableHonorsRetryBackoff(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
markerPath := filepath.Join(t.TempDir(), "retry-backoff-marker")
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Retry Backoff 项目", Description: "retry backoff"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "重试延迟工作流",
|
|
Description: "retry backoff",
|
|
Type: domain.WorkflowTypeBatch,
|
|
MaxRetryAttempts: 1,
|
|
RetryBackoffSeconds: 2,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "flaky-node",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "Shell 命令执行",
|
|
Category: "UTIL",
|
|
Description: "flaky",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeFile}},
|
|
Hyperparams: []domain.HyperParam{{
|
|
Key: "COMMAND",
|
|
Value: "if [ ! -f '" + markerPath + "' ]; then touch '" + markerPath + "' && exit 1; fi; printf '{\"ok\":true}' > result.json",
|
|
}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
initialRun, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create initial run: %v", err)
|
|
}
|
|
|
|
runtime, err := newTestRuntime(t, dataStore)
|
|
if err != nil {
|
|
t.Fatalf("create runtime: %v", err)
|
|
}
|
|
if _, err := runtime.ProcessOnce(ctx); err != nil {
|
|
t.Fatalf("first process once: %v", err)
|
|
}
|
|
|
|
runs, err := dataStore.ListRuns(ctx, "")
|
|
if err != nil {
|
|
t.Fatalf("list runs: %v", err)
|
|
}
|
|
if len(runs) != 2 {
|
|
t.Fatalf("expected original + retry run, got %d", len(runs))
|
|
}
|
|
var retryRun domain.RunRecord
|
|
for _, candidate := range runs {
|
|
if candidate.ID != initialRun.ID {
|
|
retryRun = candidate
|
|
break
|
|
}
|
|
}
|
|
if retryRun.AvailableAt == "" {
|
|
t.Fatalf("expected retry run to carry availableAt")
|
|
}
|
|
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available before backoff expires: %v", err)
|
|
}
|
|
stillQueued, err := dataStore.GetRun(ctx, retryRun.ID)
|
|
if err != nil {
|
|
t.Fatalf("get retry run before backoff: %v", err)
|
|
}
|
|
if stillQueued == nil || stillQueued.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected retry run to remain queued before backoff expires, got %#v", stillQueued)
|
|
}
|
|
|
|
time.Sleep(2200 * time.Millisecond)
|
|
if err := runtime.ProcessAvailable(ctx); err != nil {
|
|
t.Fatalf("process available after backoff expires: %v", err)
|
|
}
|
|
completedRetry := waitForRunStatus(t, ctx, dataStore, retryRun.ID, domain.RunStatusCompleted, 8*time.Second)
|
|
if completedRetry.Attempt != 2 {
|
|
t.Fatalf("expected retry attempt number to stay 2, got %d", completedRetry.Attempt)
|
|
}
|
|
}
|