diff --git a/.nextchanges/air/list-filter-validation.md b/.nextchanges/air/list-filter-validation.md new file mode 100644 index 00000000000..4606ee66255 --- /dev/null +++ b/.nextchanges/air/list-filter-validation.md @@ -0,0 +1 @@ +* Support full workspace accelerator types in `air list` filters while preserving family matching, and reject invalid filter values. ([#7515](https://github.com/databricks/cli/pull/7515)) diff --git a/acceptance/air/help/output.txt b/acceptance/air/help/output.txt index 29ab1a40dbe..f52e589f702 100644 --- a/acceptance/air/help/output.txt +++ b/acceptance/air/help/output.txt @@ -52,7 +52,9 @@ List active runs for the current user. Filters: Use --filter KEY=VALUE per condition. A run must match every condition. - accelerator_type Accelerator type, such as A10 or H100. + accelerator_type Accelerator family or full YAML type. Examples: + A10, H100, GPU_1xA10, GPU_8xH100. + Workspace types: databricks air run -h config.compute.accelerator_type experiment Case-insensitive glob matched against the experiment name. num_accelerators Total GPU count. user Run creator identifier. @@ -63,6 +65,8 @@ Usage: Examples: databricks air list --filter accelerator_type=H100 + databricks air list --filter accelerator_type=GPU_8xH100 + databricks air list --all-status \ --filter 'experiment=qwen*' \ --filter num_accelerators=8 \ diff --git a/cmd/air/list.go b/cmd/air/list.go index 61cfe3d9b3b..835292ed1ff 100644 --- a/cmd/air/list.go +++ b/cmd/air/list.go @@ -96,12 +96,16 @@ func newListCommand() *cobra.Command { Filters: Use --filter KEY=VALUE per condition. A run must match every condition. - accelerator_type Accelerator type, such as A10 or H100. + accelerator_type Accelerator family or full YAML type. Examples: + A10, H100, GPU_1xA10, GPU_8xH100. + Workspace types: databricks air run -h config.compute.accelerator_type experiment Case-insensitive glob matched against the experiment name. num_accelerators Total GPU count. user Run creator identifier.`, Example: ` databricks air list --filter accelerator_type=H100 + databricks air list --filter accelerator_type=GPU_8xH100 + databricks air list --all-status \ --filter 'experiment=qwen*' \ --filter num_accelerators=8 \ @@ -127,6 +131,9 @@ Filters: if err != nil { return err } + if f.AcceleratorType != "" { + configureAcceleratorFilter(&f) + } // An explicit user= filter wins; otherwise default to the current user // unless --all-users is set. runs/list has no creator param, so the @@ -161,6 +168,11 @@ Filters: limit: limit, workspaceID: workspaceID, }) + if f.AcceleratorType != "" { + fetcher.validateEmpty = func() error { + return validateEmptyAcceleratorFilter(ctx, w, f.AcceleratorType) + } + } // JSON prints the newest `limit` runs once. Text renders the table: // navigable in a terminal (paging in older runs on demand), printed once @@ -179,6 +191,15 @@ Filters: return cmd } +func validateEmptyAcceleratorFilter(ctx context.Context, w *databricks.WorkspaceClient, value string) error { + options, err := listWorkspaceComputeOptions(ctx, w) + if err != nil { + log.Debugf(ctx, "air list: could not validate accelerator filter against workspace compute options: %v", err) + return fmt.Errorf("no runs matched --filter accelerator_type=%q\nRetry or run 'databricks air run -h config.compute.accelerator_type' to check available types", value) + } + return validateAcceleratorFilter(value, options) +} + // listStrategy is a source of matching runs, pulled in batches. Two implement it: // jobsScanStrategy pages runs/list; indexStrategy hydrates the AiTrainingService // index. The fetcher wraps whichever is chosen. @@ -195,13 +216,16 @@ type listStrategy interface { // the interactive table's lazy paging. It wraps a listStrategy and adds the // shared tail: MLflow enrichment (text only) and row projection. type runFetcher struct { - ctx context.Context - w *databricks.WorkspaceClient - fetchMLflow bool - strategy listStrategy - workspaceID int64 - - exhausted bool + ctx context.Context + w *databricks.WorkspaceClient + fetchMLflow bool + strategy listStrategy + workspaceID int64 + validateEmpty func() error + + exhausted bool + matchedAny bool + validatedEmpty bool } func newRunFetcher(ctx context.Context, w *databricks.WorkspaceClient, q listQuery) *runFetcher { @@ -241,6 +265,15 @@ func (f *runFetcher) next(want int) ([]listRow, error) { return nil, err } f.exhausted = f.strategy.done() + if len(entries) > 0 { + f.matchedAny = true + } + if len(entries) == 0 && f.exhausted && !f.matchedAny && f.validateEmpty != nil && !f.validatedEmpty { + f.validatedEmpty = true + if err := f.validateEmpty(); err != nil { + return nil, err + } + } // MLflow links appear only in the text table, so the per-run get-output // lookups are skipped for JSON output (which omits the column anyway). diff --git a/cmd/air/list_filter.go b/cmd/air/list_filter.go index bd2bf8dff6e..9b5df111098 100644 --- a/cmd/air/list_filter.go +++ b/cmd/air/list_filter.go @@ -3,6 +3,7 @@ package aircmd import ( "fmt" "path" + "slices" "strconv" "strings" @@ -26,9 +27,14 @@ type listFilters struct { User string // Experiment is a case-insensitive glob Experiment string - // AcceleratorType is a case-insensitive substring matched against the - // display GPU name (e.g. "H100"). + // AcceleratorType is the user-provided family or full type. AcceleratorType string + // ResolvedAcceleratorType is the canonical-looking user input used for an + // exact full-type match against runs. + ResolvedAcceleratorType string + // AcceleratorFamilyMatch preserves the legacy case-insensitive substring + // match against the display GPU family. + AcceleratorFamilyMatch bool // NumAccelerators is an exact match against the GPU count. NumAccelerators *int } @@ -40,10 +46,20 @@ func parseListFilters(raw []string) (listFilters, error) { if !ok || key == "" { return listFilters{}, fmt.Errorf("invalid --filter %q: expected KEY=VALUE", item) } + if !slices.Contains(supportedFilterKeys, key) { + return listFilters{}, fmt.Errorf("unsupported --filter key %q: supported keys are %s", key, strings.Join(supportedFilterKeys, ", ")) + } + value = strings.TrimSpace(value) + if value == "" { + return listFilters{}, fmt.Errorf("invalid --filter %s=%q: value cannot be empty", key, value) + } switch key { case "user": f.User = value case "experiment": + if _, err := path.Match(value, ""); err != nil { + return listFilters{}, fmt.Errorf("invalid --filter experiment=%q: invalid glob pattern: %w", value, err) + } f.Experiment = value case "accelerator_type": f.AcceleratorType = value @@ -53,13 +69,95 @@ func parseListFilters(raw []string) (listFilters, error) { return listFilters{}, fmt.Errorf("invalid --filter num_accelerators=%q: must be a positive integer", value) } f.NumAccelerators = &n - default: - return listFilters{}, fmt.Errorf("unsupported --filter key %q: supported keys are %s", key, strings.Join(supportedFilterKeys, ", ")) } } return f, nil } +// configureAcceleratorFilter selects exact full-type matching for YAML-style +// values and preserves legacy family substring matching for everything else. +func configureAcceleratorFilter(f *listFilters) { + if strings.HasPrefix(strings.ToUpper(f.AcceleratorType), "GPU_") { + f.ResolvedAcceleratorType = f.AcceleratorType + return + } + f.AcceleratorFamilyMatch = true +} + +// validateAcceleratorFilter checks a filter that produced no runs against the +// accelerator types currently available in the workspace. +func validateAcceleratorFilter(value string, options []computeOption) error { + for _, option := range options { + if strings.EqualFold(value, option.HardwareAccelerator) { + return nil + } + } + + if !strings.HasPrefix(strings.ToUpper(value), "GPU_") { + for _, option := range options { + family := acceleratorFamily(option.HardwareAccelerator) + if strings.Contains(strings.ToLower(family), strings.ToLower(value)) { + return nil + } + } + } + if fullType := suggestedAcceleratorType(value, options); fullType != "" { + return fmt.Errorf("invalid --filter accelerator_type=%q: use %q", value, fullType) + } + return fmt.Errorf("invalid --filter accelerator_type=%q: %s", value, validAcceleratorFilterHint(options)) +} + +// acceleratorFamily extracts the hardware family from a canonical type without +// a client-side type list. +func acceleratorFamily(acceleratorType string) string { + prefix, family, ok := strings.Cut(acceleratorShorthand(acceleratorType), "x") + if ok { + if _, err := strconv.Atoi(prefix); err == nil && family != "" { + return family + } + } + return acceleratorType +} + +func acceleratorShorthand(acceleratorType string) string { + return strings.TrimPrefix(acceleratorType, "GPU_") +} + +func acceleratorRunFamily(acceleratorType string) string { + family := acceleratorFamily(acceleratorType) + if family != acceleratorType { + return family + } + return gpuDisplayName(acceleratorType) +} + +func suggestedAcceleratorType(value string, options []computeOption) string { + compact := strings.Join(strings.Fields(value), "") + for _, option := range options { + if strings.EqualFold(compact, acceleratorShorthand(option.HardwareAccelerator)) { + return option.HardwareAccelerator + } + } + return "" +} + +func validAcceleratorFilterHint(options []computeOption) string { + var families, fullTypes []string + for _, option := range options { + family := acceleratorFamily(option.HardwareAccelerator) + if !slices.ContainsFunc(families, func(v string) bool { return strings.EqualFold(v, family) }) { + families = append(families, family) + } + fullTypes = append(fullTypes, option.HardwareAccelerator) + } + if len(fullTypes) == 0 { + return "no accelerator types are available in this workspace" + } + parts := []string{"families (" + strings.Join(families, ", ") + ")"} + parts = append(parts, "full types ("+strings.Join(fullTypes, ", ")+")") + return "valid values for this workspace include " + strings.Join(parts, ", ") +} + // filterFields are the task-filter inputs, cached alongside the row so a cache // hit can be re-filtered without re-fetching the run. type filterFields struct { @@ -95,8 +193,12 @@ func (f listFilters) matchesFields(fields filterFields) bool { } if f.AcceleratorType != "" { - display := strings.ToLower(gpuDisplayName(fields.GPUType)) - if !strings.Contains(display, strings.ToLower(f.AcceleratorType)) { + if f.AcceleratorFamilyMatch { + family := strings.ToLower(acceleratorRunFamily(fields.GPUType)) + if !strings.Contains(family, strings.ToLower(f.AcceleratorType)) { + return false + } + } else if !strings.EqualFold(f.ResolvedAcceleratorType, fields.GPUType) { return false } } diff --git a/cmd/air/list_filter_test.go b/cmd/air/list_filter_test.go index 381f5e3a62d..6369b48b685 100644 --- a/cmd/air/list_filter_test.go +++ b/cmd/air/list_filter_test.go @@ -1,6 +1,8 @@ package aircmd import ( + "fmt" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -29,6 +31,12 @@ func TestParseListFilters(t *testing.T) { assert.Contains(t, err.Error(), "unsupported --filter key") }) + t.Run("unknown key with empty value", func(t *testing.T) { + _, err := parseListFilters([]string{"acclerator_type="}) + require.Error(t, err) + assert.Contains(t, err.Error(), "unsupported --filter key") + }) + t.Run("malformed pair", func(t *testing.T) { _, err := parseListFilters([]string{"experiment"}) require.Error(t, err) @@ -46,6 +54,87 @@ func TestParseListFilters(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "positive integer") }) + + for _, filter := range []string{"user=", "experiment=", "accelerator_type=", "num_accelerators="} { + t.Run("empty "+filter, func(t *testing.T) { + _, err := parseListFilters([]string{filter}) + key, _, _ := strings.Cut(filter, "=") + require.EqualError(t, err, fmt.Sprintf("invalid --filter %s=%q: value cannot be empty", key, "")) + }) + } + + t.Run("trim values", func(t *testing.T) { + f, err := parseListFilters([]string{"user= me@example.com ", "experiment= qwen* ", "num_accelerators= 8 "}) + require.NoError(t, err) + assert.Equal(t, "me@example.com", f.User) + assert.Equal(t, "qwen*", f.Experiment) + require.NotNil(t, f.NumAccelerators) + assert.Equal(t, 8, *f.NumAccelerators) + }) + + t.Run("malformed experiment glob", func(t *testing.T) { + _, err := parseListFilters([]string{"experiment=["}) + require.Error(t, err) + assert.Contains(t, err.Error(), "invalid glob pattern") + }) + + for _, acceleratorType := range []string{"1xA10", "8xH100", "GPU_1xA10", "GPU_1xH100", "GPU_8xH100"} { + t.Run("accelerator type "+acceleratorType, func(t *testing.T) { + f, err := parseListFilters([]string{"accelerator_type=" + acceleratorType}) + require.NoError(t, err) + assert.Equal(t, acceleratorType, f.AcceleratorType) + }) + } +} + +func TestValidateAcceleratorFilter(t *testing.T) { + options := []computeOption{ + {HardwareAccelerator: "GPU_1xA10", DisplayName: "1x A10"}, + {HardwareAccelerator: "GPU_1xH100", DisplayName: "1x H100"}, + {HardwareAccelerator: "GPU_8xH100", DisplayName: "8x H100"}, + {HardwareAccelerator: "GPU_8xB300", DisplayName: "8x B300"}, + } + + for _, value := range []string{"H100", "GPU_1xA10", "B300"} { + t.Run("valid "+value, func(t *testing.T) { + require.NoError(t, validateAcceleratorFilter(value, options)) + }) + } + + for value, fullType := range map[string]string{ + "1xA10": "GPU_1xA10", + "1x A10": "GPU_1xA10", + "1xH100": "GPU_1xH100", + "1x H100": "GPU_1xH100", + "8xH100": "GPU_8xH100", + "8x H100": "GPU_8xH100", + "8x B300": "GPU_8xB300", + } { + t.Run("suggest full type for "+value, func(t *testing.T) { + err := validateAcceleratorFilter(value, options) + require.EqualError(t, err, fmt.Sprintf("invalid --filter accelerator_type=%q: use %q", value, fullType)) + }) + } + + t.Run("unsupported", func(t *testing.T) { + err := validateAcceleratorFilter("GPU_1xV100", options) + require.Error(t, err) + assert.Contains(t, err.Error(), "valid values for this workspace") + assert.Contains(t, err.Error(), "A10, H100, B300") + assert.Contains(t, err.Error(), "GPU_8xB300") + }) + + t.Run("no available types", func(t *testing.T) { + err := validateAcceleratorFilter("H100", nil) + require.Error(t, err) + assert.Contains(t, err.Error(), "no accelerator types are available") + }) +} + +func TestLegacyAcceleratorSubstring(t *testing.T) { + f := listFilters{AcceleratorType: "H1"} + configureAcceleratorFilter(&f) + assert.True(t, f.matchesFields(filterFields{GPUType: "h100_80gb"})) } func TestListFiltersMatches(t *testing.T) { @@ -61,8 +150,10 @@ func TestListFiltersMatches(t *testing.T) { {"experiment suffix glob", listFilters{Experiment: "*-eval"}, true}, {"experiment case-insensitive", listFilters{Experiment: "QWEN*"}, true}, {"experiment no match", listFilters{Experiment: "llama*"}, false}, - {"accelerator type substring", listFilters{AcceleratorType: "h100"}, true}, - {"accelerator type no match", listFilters{AcceleratorType: "a10"}, false}, + {"accelerator family", listFilters{AcceleratorType: "h100", AcceleratorFamilyMatch: true}, true}, + {"accelerator full type", listFilters{AcceleratorType: "gpu_8xh100", ResolvedAcceleratorType: "GPU_8xH100"}, true}, + {"accelerator full type other topology", listFilters{AcceleratorType: "GPU_1xH100", ResolvedAcceleratorType: "GPU_1xH100"}, false}, + {"accelerator type no match", listFilters{AcceleratorType: "GPU_1xA10", ResolvedAcceleratorType: "GPU_1xA10"}, false}, {"num accelerators match", listFilters{NumAccelerators: new(8)}, true}, {"num accelerators no match", listFilters{NumAccelerators: new(4)}, false}, } @@ -71,4 +162,10 @@ func TestListFiltersMatches(t *testing.T) { assert.Equal(t, c.want, c.f.matches(&run)) }) } + + t.Run("canonical type uses derived family", func(t *testing.T) { + fields := filterFields{GPUType: "GPU_8xB300"} + assert.True(t, (listFilters{AcceleratorType: "B300", AcceleratorFamilyMatch: true}).matchesFields(fields)) + assert.False(t, (listFilters{AcceleratorType: "8xB300", AcceleratorFamilyMatch: true}).matchesFields(fields)) + }) } diff --git a/cmd/air/list_test.go b/cmd/air/list_test.go index a7700d162f0..0928769393d 100644 --- a/cmd/air/list_test.go +++ b/cmd/air/list_test.go @@ -9,6 +9,7 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" "github.com/databricks/databricks-sdk-go/experimental/mocks" "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" @@ -137,6 +138,10 @@ func TestRunFetcherResumesAcrossCalls(t *testing.T) { } srv := runsServer(t, runsListBody(t, "", runs...)) f := newRunFetcher(t.Context(), newTestWorkspaceClient(t, srv.URL), listQuery{activeOnly: true}) + f.validateEmpty = func() error { + t.Fatal("validation should not run after an earlier page matched") + return nil + } first, err := f.next(2) require.NoError(t, err) @@ -252,3 +257,76 @@ func TestListInvalidFilter(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "unsupported --filter key") } + +func TestListInvalidAcceleratorFilter(t *testing.T) { + jobsCalled := false + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/.well-known/databricks-config": + _, _ = w.Write([]byte(`{}`)) + case "/api/2.2/jobs/runs/list": + jobsCalled = true + _, _ = w.Write([]byte(runsListBody(t, ""))) + case computeOptionsPath: + _, _ = w.Write([]byte(`{"compute_options":[{"hardware_accelerator":"GPU_8xH100","display_name":"8x H100"}]}`)) + default: + _, _ = w.Write([]byte(`{}`)) + } + })) + t.Cleanup(srv.Close) + + ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), newTestWorkspaceClient(t, srv.URL)) + cmd := withOutput(newListCommand(), flags.OutputText) + cmd.SetContext(ctx) + require.NoError(t, cmd.Flags().Set("all-users", "true")) + require.NoError(t, cmd.Flags().Set("filter", "accelerator_type=GPU_1xV100")) + + err := cmd.RunE(cmd, nil) + require.Error(t, err) + assert.Contains(t, err.Error(), "valid values for this workspace") + assert.Contains(t, err.Error(), "GPU_8xH100") + assert.True(t, jobsCalled) +} + +func TestListValidatesAcceleratorOnlyAfterNoMatches(t *testing.T) { + for _, tc := range []struct { + name string + runs string + wantErr bool + }{ + {"matching run", runsListBody(t, "", airBaseRun(1, "me@example.com", "GPU_1xA10", 1, "exp")), false}, + {"no matching run", runsListBody(t, ""), true}, + } { + t.Run(tc.name, func(t *testing.T) { + computeOptionsCalled := false + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case computeOptionsPath: + computeOptionsCalled = true + http.Error(w, "unavailable", http.StatusNotFound) + case "/api/2.2/jobs/runs/list": + _, _ = w.Write([]byte(tc.runs)) + default: + _, _ = w.Write([]byte(`{}`)) + } + })) + t.Cleanup(srv.Close) + + ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), newTestWorkspaceClient(t, srv.URL)) + cmd := withOutput(newListCommand(), flags.OutputJSON) + cmd.SetContext(ctx) + require.NoError(t, cmd.Flags().Set("all-users", "true")) + require.NoError(t, cmd.Flags().Set("filter", "accelerator_type=A10")) + + err := cmd.RunE(cmd, nil) + if tc.wantErr { + require.Error(t, err) + assert.Equal(t, "no runs matched --filter accelerator_type=\"A10\"\nRetry or run 'databricks air run -h config.compute.accelerator_type' to check available types", err.Error()) + assert.True(t, computeOptionsCalled) + } else { + require.NoError(t, err) + assert.False(t, computeOptionsCalled) + } + }) + } +}