diff --git a/.nextchanges/air/7626.md b/.nextchanges/air/7626.md new file mode 100644 index 00000000000..16b428d3deb --- /dev/null +++ b/.nextchanges/air/7626.md @@ -0,0 +1 @@ +* `air get` overlaps its config and environment lookups with the run output request, which makes the text view about 30% faster. ([#7626](https://github.com/databricks/cli/pull/7626)) diff --git a/cmd/air/get.go b/cmd/air/get.go index bac778ded51..74ff816c68e 100644 --- a/cmd/air/get.go +++ b/cmd/air/get.go @@ -167,6 +167,13 @@ JOB_RUN_ID is the job run ID returned by "databricks air run".`, fmt.Errorf("failed to get status for run %d: %w", runID, err)) } + // The text view's run-only lookups start now so they overlap the + // get-output and MLflow requests below. A sweep renders no run box. + var pre *runTextPrefetch + if root.OutputType(cmd) == flags.OutputText && findForEachTask(run) == nil { + pre = startRunTextPrefetch(ctx, w, run) + } + // Prefer the configured workspace ID; only look it up when it is unset. id, err := auth.ResolveWorkspaceID(ctx, w) if err != nil { @@ -224,7 +231,7 @@ JOB_RUN_ID is the job run ID returned by "databricks air run".`, } data.DisplayStatus = detailedDisplayRunStatus(run, statusMessage) - renderRunText(ctx, out, w, run, &data, ids) + renderRunText(ctx, out, w, run, &data, ids, pre) return nil } diff --git a/cmd/air/get_test.go b/cmd/air/get_test.go index c6e984c3b44..783a073abf9 100644 --- a/cmd/air/get_test.go +++ b/cmd/air/get_test.go @@ -7,9 +7,11 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "sync/atomic" "testing" "text/template" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdctx" @@ -199,6 +201,66 @@ func getRunServer(t *testing.T, meCalls *atomic.Int32) *httptest.Server { return srv } +// The text view's run-only lookups (the environment, via a second runs/get, and +// the config export) must not wait for get-output. The get-output handler only +// answers once both have arrived, so the test times out if they run after it. +func TestGetTextOverlapsRunOnlyLookupsWithGetOutput(t *testing.T) { + run := jobs.Run{ + RunId: 7, + State: &jobs.RunState{LifeCycleState: jobs.RunLifeCycleStateTerminated, ResultState: jobs.RunResultStateSuccess}, + Tasks: []jobs.RunTask{{ + RunId: 70, + AiRuntimeTask: &jobs.AiRuntimeTask{ + Experiment: "/Users/me@example.com/exp", + Deployments: []jobs.DeploymentSpec{{ + CommandPath: "/Workspace/run/command.sh", + Compute: jobs.ComputeSpec{AcceleratorType: jobs.ComputeSpecAcceleratorTypeGpu1xA10, AcceleratorCount: 1}, + }}, + }, + }}, + } + var runGets atomic.Int32 + exported := make(chan struct{}) + markExported := sync.OnceFunc(func() { close(exported) }) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/api/2.2/jobs/runs/get": + runGets.Add(1) + _ = json.NewEncoder(w).Encode(run) + case "/api/2.0/workspace/export": + markExported() + _, _ = w.Write([]byte("command: python train.py\n")) + case "/api/2.2/jobs/runs/get-output": + select { + case <-exported: + case <-time.After(5 * time.Second): + t.Errorf("get-output answered before the config export started") + } + for deadline := time.Now().Add(5 * time.Second); runGets.Load() < 2 && time.Now().Before(deadline); { + time.Sleep(time.Millisecond) + } + if runGets.Load() < 2 { + t.Errorf("get-output answered before the environment lookup started") + } + _, _ = w.Write([]byte(`{"ai_runtime_task_output":{"mlflow_experiment_id":"1","mlflow_run_id":"r1"}}`)) + default: + _, _ = w.Write([]byte(`{}`)) + } + })) + t.Cleanup(srv.Close) + + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: srv.URL, Token: "token", WorkspaceID: "123"}) + require.NoError(t, err) + var out bytes.Buffer + ctx := cmdctx.SetWorkspaceClient(cmdio.MockDiscard(t.Context()), w) + cmd := withOutput(newGetCommand(), flags.OutputText) + cmd.SetContext(ctx) + cmd.SetOut(&out) + + require.NoError(t, cmd.RunE(cmd, []string{"7"})) + assert.Contains(t, out.String(), "command: python train.py") +} + func runGetJSON(t *testing.T, w *databricks.WorkspaceClient) (string, error) { t.Helper() var buf bytes.Buffer diff --git a/cmd/air/list_detail.go b/cmd/air/list_detail.go index 2045bb819d5..f740438013c 100644 --- a/cmd/air/list_detail.go +++ b/cmd/air/list_detail.go @@ -21,6 +21,7 @@ func runDetailText(ctx context.Context, w *databricks.WorkspaceClient, runID int } return "", fmt.Errorf("failed to fetch run: %w", err) } + pre := startRunTextPrefetch(ctx, w, run) data := buildGetData(run) // As in `air get`: an ai_runtime_task run keeps its experiment, accelerators @@ -36,7 +37,7 @@ func runDetailText(ctx context.Context, w *databricks.WorkspaceClient, runID int } var buf bytes.Buffer - renderRunText(ctx, &buf, w, run, &data, ids) + renderRunText(ctx, &buf, w, run, &data, ids, pre) return buf.String(), nil } diff --git a/cmd/air/render.go b/cmd/air/render.go index 755e8540fb9..3dc57624e2b 100644 --- a/cmd/air/render.go +++ b/cmd/air/render.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" "strings" + "sync" "github.com/charmbracelet/lipgloss" "github.com/databricks/cli/libs/auth" @@ -92,7 +93,39 @@ type runView struct { // completed progress bar, and a field list, separated by blank lines. It is a // one-shot renderer — it builds the full string and writes it once, with no // streaming, spinner, or redraw. -func renderRunText(ctx context.Context, out io.Writer, w *databricks.WorkspaceClient, run *jobs.Run, data *getData, ids *mlflowIdentifiers) { +// runTextPrefetch holds the text view's lookups that depend only on the run, so +// they can start before the get-output and MLflow requests and overlap them. +type runTextPrefetch struct { + environment func() string + config func() string +} + +// startRunTextPrefetch starts the environment-version and training-config +// fetches in the background. +func startRunTextPrefetch(ctx context.Context, w *databricks.WorkspaceClient, run *jobs.Run) *runTextPrefetch { + return &runTextPrefetch{ + environment: inBackground(func() string { return aiRuntimeEnvironmentVersion(ctx, w, run.RunId) }), + config: inBackground(func() string { + // The config path is derived from the run alone, the same way the + // caller derives it before rendering. + data := buildGetData(run) + if genAIComputeTask(run) == nil { + enrichFromAiRuntimeTask(run, &data) + } + return resolveConfigYAML(ctx, w, run, &data) + }), + } +} + +// inBackground runs f in a goroutine and returns a function that waits for its +// result. +func inBackground(f func() string) func() string { + done := make(chan string, 1) + go func() { done <- f() }() + return sync.OnceValue(func() string { return <-done }) +} + +func renderRunText(ctx context.Context, out io.Writer, w *databricks.WorkspaceClient, run *jobs.Run, data *getData, ids *mlflowIdentifiers, pre *runTextPrefetch) { // cmdio.NewRenderer sets the Ascii color profile when color is off, which // emits no SGR codes; combined with the link fallback below this gives clean, // un-escaped output under --no-color / NO_COLOR / piped stdout. @@ -102,7 +135,11 @@ func renderRunText(ctx context.Context, out io.Writer, w *databricks.WorkspaceCl // The serverless environment version lives on the run's environments[].spec, // which the typed SDK Run drops, so read it with a raw request. Empty (a run // with no serverless environment) stays "N/A". - data.EnvironmentDisplay = orNA(aiRuntimeEnvironmentVersion(ctx, w, run.RunId)) + var mlflowName func() string + if ids != nil { + mlflowName = inBackground(func() string { return fetchMLflowRunName(ctx, w, ids.RunID) }) + } + data.EnvironmentDisplay = orNA(pre.environment()) view := runView{ runID: data.RunID, @@ -125,12 +162,12 @@ func renderRunText(ctx context.Context, out io.Writer, w *databricks.WorkspaceCl if ids != nil { // The box grows to fit its fields, so the label is shown whole. - view.mlflowLabel = mlflowRunLabel(fetchMLflowRunName(ctx, w, ids.RunID), ids.RunID, 0) + ids.retryNote() + view.mlflowLabel = mlflowRunLabel(mlflowName(), ids.RunID, 0) + ids.retryNote() view.mlflowURL = mlflowRunURL(w.Config.Host, ids) } var sections []string - if body := colorizeConfig(p, resolveConfigYAML(ctx, w, run, data)); body != "" { + if body := colorizeConfig(p, pre.config()); body != "" { sections = append(sections, renderBox(p, configBoxTitle, body)) } sections = append(sections, renderBox(p, metadataBoxTitle, renderFields(p, colorOn, view)))