Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/air/list-skip-purged-runs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `air list --all-status` no longer fails when a listed run has been purged. ([#7517](https://github.com/databricks/cli/pull/7517))
8 changes: 6 additions & 2 deletions cmd/air/joblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func fetchJobRun(ctx context.Context, w *databricks.WorkspaceClient, runID int64

// hydrateJobRuns fetches the given run ids concurrently via runs/get, preserving
// input order. runs/get enforces per-run view ACLs, so an id the caller can't
// view (403) or that has been purged (404) is dropped; any other error is
// view (403) or that has been purged (404, or the 400 "Run <id> does not exist."
// that the SDK maps to ErrResourceDoesNotExist) is dropped; any other error is
// systemic and fails the whole batch.
func hydrateJobRuns(ctx context.Context, w *databricks.WorkspaceClient, ids []int64) ([]*jobs.Run, error) {
runs := make([]*jobs.Run, len(ids))
Expand All @@ -156,11 +157,14 @@ func hydrateJobRuns(ctx context.Context, w *databricks.WorkspaceClient, ids []in
g.Go(func() error {
run, err := fetchJobRun(gctx, w, id)
if err != nil {
if errors.Is(err, apierr.ErrResourceDoesNotExist) {
return nil // purged: drop this id
}
if apiErr, ok := errors.AsType[*apierr.APIError](err); ok &&
(apiErr.StatusCode == http.StatusForbidden || apiErr.StatusCode == http.StatusNotFound) {
return nil // not viewable or purged: drop this id
}
return fmt.Errorf("failed to get run %d: %w", id, err)
return err
}
runs[i] = run
return nil
Expand Down
74 changes: 74 additions & 0 deletions cmd/air/joblist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,3 +46,76 @@ func TestFetchJobRunParsesAiRuntimeTask(t *testing.T) {
assert.Equal(t, "GPU_1xA10", gpuType)
assert.Equal(t, 1, count)
}

type runGetResponse struct {
status int
body string
}

// runGetByIDServer serves runs/get per run_id and a stub for the SDK config
// probe; ids missing from responses return a run with that id.
func runGetByIDServer(t *testing.T, responses map[string]runGetResponse) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/2.2/jobs/runs/get" {
_, _ = w.Write([]byte(`{}`))
return
}
id := r.URL.Query().Get("run_id")
resp, ok := responses[id]
if !ok {
resp = runGetResponse{status: http.StatusOK, body: `{"run_id": ` + id + `}`}
}
w.WriteHeader(resp.status)
_, _ = w.Write([]byte(resp.body))
}))
t.Cleanup(srv.Close)
return srv
}

func runIDs(runs []*jobs.Run) []int64 {
ids := make([]int64, len(runs))
for i, r := range runs {
ids[i] = r.RunId
}
return ids
}

func TestHydrateJobRunsDropsUnviewableAndPurgedRuns(t *testing.T) {
srv := runGetByIDServer(t, map[string]runGetResponse{
"2": {http.StatusBadRequest, `{"error_code": "INVALID_PARAMETER_VALUE", "message": "Run 2 does not exist."}`},
"4": {http.StatusForbidden, `{"error_code": "PERMISSION_DENIED", "message": "denied"}`},
"5": {http.StatusNotFound, `{"error_code": "NOT_FOUND", "message": "not found"}`},
})

runs, err := hydrateJobRuns(t.Context(), newTestWorkspaceClient(t, srv.URL), []int64{1, 2, 3, 4, 5, 6})
require.NoError(t, err)
assert.Equal(t, []int64{1, 3, 6}, runIDs(runs))
}

func TestHydrateJobRunsFailsOnOtherErrors(t *testing.T) {
tests := []struct {
name string
resp runGetResponse
wantErr string
}{
{
name: "internal error",
resp: runGetResponse{http.StatusInternalServerError, `{"error_code": "INTERNAL_ERROR", "message": "boom"}`},
wantErr: "failed to get run 2: boom",
},
{
name: "other invalid parameter",
resp: runGetResponse{http.StatusBadRequest, `{"error_code": "INVALID_PARAMETER_VALUE", "message": "run_id must be positive"}`},
wantErr: "failed to get run 2: run_id must be positive",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := runGetByIDServer(t, map[string]runGetResponse{"2": tt.resp})

_, err := hydrateJobRuns(t.Context(), newTestWorkspaceClient(t, srv.URL), []int64{1, 2, 3})
assert.EqualError(t, err, tt.wantErr)
})
}
}
Loading