diff --git a/.nextchanges/air/7616.md b/.nextchanges/air/7616.md new file mode 100644 index 00000000000..24597e015c7 --- /dev/null +++ b/.nextchanges/air/7616.md @@ -0,0 +1 @@ +* `air list --all-status` requests larger index pages, so it makes far fewer round trips once the server supports `page_size`. ([#7616](https://github.com/databricks/cli/pull/7616)) diff --git a/cmd/air/aitraining.go b/cmd/air/aitraining.go index c13db8e2538..704daea6e17 100644 --- a/cmd/air/aitraining.go +++ b/cmd/air/aitraining.go @@ -18,6 +18,12 @@ import ( // with a raw client.Do because the SDK does not model the AiTrainingService. const aiTrainingWorkflowsPath = "/api/2.0/ai-training/workflows" +// aiTrainingWorkflowsPageSize is how many workloads the index scans per page. +// The server scans the whole workspace and returns only the caller's runs, so +// a larger page means fewer sequential round trips. It caps the value at its +// own read limit, and servers that predate page_size ignore it. +const aiTrainingWorkflowsPageSize = 1000 + const computeOptionsPath = "/api/2.0/ai-training/compute-options" type computeOption struct { @@ -81,7 +87,7 @@ func listAiTrainingWorkflows(ctx context.Context, w *databricks.WorkspaceClient, seenIDs := map[int64]bool{} var pageToken string for { - query := map[string]any{} + query := map[string]any{"page_size": aiTrainingWorkflowsPageSize} if activeOnly { query["active_only"] = true } diff --git a/cmd/air/aitraining_test.go b/cmd/air/aitraining_test.go index fdcb250cbab..f9e3aec2f90 100644 --- a/cmd/air/aitraining_test.go +++ b/cmd/air/aitraining_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "net/url" "testing" "github.com/stretchr/testify/assert" @@ -88,6 +89,33 @@ func TestListAiTrainingWorkflowsPaginates(t *testing.T) { assert.Equal(t, int64(2), refs[1].jobRunID) } +func TestListAiTrainingWorkflowsSendsPageSize(t *testing.T) { + var queries []url.Values + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != aiTrainingWorkflowsPath { + _, _ = w.Write([]byte(`{}`)) + return + } + queries = append(queries, r.URL.Query()) + if len(queries) == 1 { + _, _ = w.Write([]byte(`{"training_workflows":[{"job_run_id":"1"}],"next_page_token":"tok"}`)) + return + } + _, _ = w.Write([]byte(`{"training_workflows":[{"job_run_id":"2"}]}`)) + })) + t.Cleanup(srv.Close) + + _, err := listAiTrainingWorkflows(t.Context(), newTestWorkspaceClient(t, srv.URL), false) + require.NoError(t, err) + require.Len(t, queries, 2) + // Every page, not just the first, asks for the larger page size. + for _, q := range queries { + assert.Equal(t, "1000", q.Get("page_size")) + } + assert.Empty(t, queries[0].Get("page_token")) + assert.Equal(t, "tok", queries[1].Get("page_token")) +} + func TestListAiTrainingWorkflowsStopsOnRepeatedToken(t *testing.T) { // A cursor that always returns the same token must not loop forever. The // repeated id is also deduped, so only one ref survives.