diff --git a/.nextchanges/bundles/4104.md b/.nextchanges/bundles/4104.md new file mode 100644 index 00000000000..8abed8ef989 --- /dev/null +++ b/.nextchanges/bundles/4104.md @@ -0,0 +1 @@ +* Validate retry settings on `for_each_task` parent tasks, reporting unsupported retries as errors and ignored retry settings as warnings. ([#4104](https://github.com/databricks/cli/pull/4104)) diff --git a/acceptance/bundle/validate/for_each_task/databricks.yml b/acceptance/bundle/validate/for_each_task/databricks.yml new file mode 100644 index 00000000000..f19549065bc --- /dev/null +++ b/acceptance/bundle/validate/for_each_task/databricks.yml @@ -0,0 +1,68 @@ +bundle: + name: "for_each_task_validation" + +resources: + jobs: + aaa_job: + name: "Job A" + tasks: + - task_key: "parent_task" + for_each_task: + inputs: "[1, 2, 3]" + task: + task_key: "child_task" + notebook_task: + notebook_path: "test.py" + test_job: + name: "Test Job" + tasks: + - task_key: "parent_task" + for_each_task: + inputs: "[1, 2, 3]" + task: + task_key: "child_task" + notebook_task: + notebook_path: "test.py" + +targets: + max_retries: + resources: + jobs: + aaa_job: + tasks: + - task_key: "parent_task" + max_retries: 3 + test_job: + tasks: + - task_key: "parent_task" + max_retries: 3 + + retry_fields: + resources: + jobs: + test_job: + tasks: + - task_key: "parent_task" + min_retry_interval_millis: 1000 + retry_on_timeout: true + + valid: + resources: + jobs: + test_job: + tasks: + - task_key: "parent_task" + max_retries: 0 + min_retry_interval_millis: 0 + retry_on_timeout: false + for_each_task: + task: + max_retries: 3 + min_retry_interval_millis: 1000 + retry_on_timeout: true + - task_key: "regular_task" + max_retries: 3 + min_retry_interval_millis: 1000 + retry_on_timeout: true + notebook_task: + notebook_path: "test.py" diff --git a/acceptance/bundle/validate/for_each_task/out.test.toml b/acceptance/bundle/validate/for_each_task/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/validate/for_each_task/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/validate/for_each_task/output.txt b/acceptance/bundle/validate/for_each_task/output.txt new file mode 100644 index 00000000000..9d5b65b9686 --- /dev/null +++ b/acceptance/bundle/validate/for_each_task/output.txt @@ -0,0 +1,55 @@ + +>>> [CLI] bundle validate -t max_retries +Error: max_retries is not supported on a for_each_task parent task + at resources.jobs.aaa_job.tasks[0].max_retries + in databricks.yml:34:28 + +When using for_each_task, max_retries must be defined on the nested task (for_each_task.task.max_retries), not on the parent task. + +Error: max_retries is not supported on a for_each_task parent task + at resources.jobs.test_job.tasks[0].max_retries + in databricks.yml:38:28 + +When using for_each_task, max_retries must be defined on the nested task (for_each_task.task.max_retries), not on the parent task. + +Name: for_each_task_validation +Target: max_retries +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/for_each_task_validation/max_retries + +Found 2 errors + +Exit code: 1 + +>>> [CLI] bundle validate -t retry_fields +Warning: min_retry_interval_millis has no effect on a for_each_task parent task + at resources.jobs.test_job.tasks[0].min_retry_interval_millis + in databricks.yml:46:42 + +min_retry_interval_millis is ignored on a for_each_task parent task. +Define it on the nested task (for_each_task.task.min_retry_interval_millis) instead. + +Warning: retry_on_timeout has no effect on a for_each_task parent task + at resources.jobs.test_job.tasks[0].retry_on_timeout + in databricks.yml:47:33 + +retry_on_timeout is ignored on a for_each_task parent task. +Define it on the nested task (for_each_task.task.retry_on_timeout) instead. + +Name: for_each_task_validation +Target: retry_fields +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/for_each_task_validation/retry_fields + +Found 2 warnings + +>>> [CLI] bundle validate -t valid +Name: for_each_task_validation +Target: valid +Workspace: + User: [USERNAME] + Path: /Workspace/Users/[USERNAME]/.bundle/for_each_task_validation/valid + +Validation OK! diff --git a/acceptance/bundle/validate/for_each_task/script b/acceptance/bundle/validate/for_each_task/script new file mode 100644 index 00000000000..6e7edeeafa6 --- /dev/null +++ b/acceptance/bundle/validate/for_each_task/script @@ -0,0 +1,3 @@ +errcode trace $CLI bundle validate -t max_retries +trace $CLI bundle validate -t retry_fields +trace $CLI bundle validate -t valid diff --git a/acceptance/bundle/validate/for_each_task/test.py b/acceptance/bundle/validate/for_each_task/test.py new file mode 100644 index 00000000000..4d1620a14e1 --- /dev/null +++ b/acceptance/bundle/validate/for_each_task/test.py @@ -0,0 +1,2 @@ +# Databricks notebook source +print("test") diff --git a/bundle/config/validate/fast_validate.go b/bundle/config/validate/fast_validate.go index d01eb8c1491..d7d2c8eab72 100644 --- a/bundle/config/validate/fast_validate.go +++ b/bundle/config/validate/fast_validate.go @@ -29,6 +29,7 @@ func (f *fastValidate) Apply(ctx context.Context, rb *bundle.Bundle) diag.Diagno // Fast mutators with only in-memory checks JobClusterKeyDefined(), JobTaskClusterSpec(), + ForEachTask(), // Blocking mutators. Deployments will fail if these checks fail. ValidateArtifactPath(), diff --git a/bundle/config/validate/for_each_task.go b/bundle/config/validate/for_each_task.go new file mode 100644 index 00000000000..1337fd78a85 --- /dev/null +++ b/bundle/config/validate/for_each_task.go @@ -0,0 +1,82 @@ +package validate + +import ( + "cmp" + "context" + "fmt" + "slices" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/structs/structpath" + "github.com/databricks/databricks-sdk-go/service/jobs" +) + +// ForEachTask reports retry settings set on a for_each_task parent instead of its nested task. +func ForEachTask() bundle.ReadOnlyMutator { + return &forEachTask{} +} + +type forEachTask struct{ bundle.RO } + +func (v *forEachTask) Name() string { + return "validate:for_each_task" +} + +func (v *forEachTask) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { + diags := diag.Diagnostics{} + + for resourceName, job := range b.Config.Resources.Jobs { + for taskIndex, task := range job.Tasks { + if task.ForEachTask != nil { + taskPath := structpath.NewPath(nil, "resources", "jobs", resourceName, "tasks", taskIndex) + diags = diags.Extend(validateForEachTask(b, task, taskPath)) + } + } + } + + // Map iteration order is randomized; sort by path for stable output. + slices.SortFunc(diags, func(x, y diag.Diagnostic) int { + return cmp.Compare(x.Paths[0].String(), y.Paths[0].String()) + }) + + return diags +} + +func validateForEachTask(b *bundle.Bundle, task jobs.Task, taskPath *structpath.PathNode) diag.Diagnostics { + diags := diag.Diagnostics{} + + // The Jobs API rejects nonzero max_retries on the parent. It accepts the other + // retry settings, but they have no effect because the parent cannot be retried. + if task.MaxRetries != 0 { + diags = diags.Append(invalidRetryFieldDiag(b, taskPath, "max_retries", diag.Error)) + } + + if task.MinRetryIntervalMillis != 0 { + diags = diags.Append(invalidRetryFieldDiag(b, taskPath, "min_retry_interval_millis", diag.Warning)) + } + + if task.RetryOnTimeout { + diags = diags.Append(invalidRetryFieldDiag(b, taskPath, "retry_on_timeout", diag.Warning)) + } + + return diags +} + +func invalidRetryFieldDiag(b *bundle.Bundle, taskPath *structpath.PathNode, fieldName string, severity diag.Severity) diag.Diagnostic { + fieldPath := structpath.NewDotString(taskPath, fieldName) + summary := fieldName + " is not supported on a for_each_task parent task" + detail := fmt.Sprintf("When using for_each_task, %s must be defined on the nested task (for_each_task.task.%s), not on the parent task.", fieldName, fieldName) + if severity == diag.Warning { + summary = fieldName + " has no effect on a for_each_task parent task" + detail = fmt.Sprintf("%s is ignored on a for_each_task parent task.\nDefine it on the nested task (for_each_task.task.%s) instead.", fieldName, fieldName) + } + + return diag.Diagnostic{ + Severity: severity, + Summary: summary, + Detail: detail, + Locations: b.Config.GetLocationsOf(fieldPath), + Paths: []*structpath.PathNode{fieldPath}, + } +}