diff --git a/.nextchanges/air/run-url-workspace-id.md b/.nextchanges/air/run-url-workspace-id.md new file mode 100644 index 00000000000..aad532732f5 --- /dev/null +++ b/.nextchanges/air/run-url-workspace-id.md @@ -0,0 +1 @@ +* `air run` now prints the job run URL with the `?o=` suffix, matching `air get` and `air list`; `dashboard_url` in `air run -o json` and the `--watch` events gains it too. When `air list` cannot resolve the workspace ID, its links now omit `?o=` instead of ending in `?o=0`. ([#7573](https://github.com/databricks/cli/pull/7573)) diff --git a/acceptance/air/run-submit-databricks-ai/output.txt b/acceptance/air/run-submit-databricks-ai/output.txt index 0635ef3d307..5e19f717249 100644 --- a/acceptance/air/run-submit-databricks-ai/output.txt +++ b/acceptance/air/run-submit-databricks-ai/output.txt @@ -4,7 +4,7 @@ >>> [CLI] air run -f run.yaml Submitting experiment: databricks-ai-v5-smoke Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: diff --git a/acceptance/air/run-submit-deps/output.txt b/acceptance/air/run-submit-deps/output.txt index 68f58e64c65..ae716c0bdfd 100644 --- a/acceptance/air/run-submit-deps/output.txt +++ b/acceptance/air/run-submit-deps/output.txt @@ -4,7 +4,7 @@ >>> [CLI] air run -f run.yaml Submitting experiment: deps-smoke Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: diff --git a/acceptance/air/run-submit-overrides/output.txt b/acceptance/air/run-submit-overrides/output.txt index f3acef3852d..3e1f5e69ed3 100644 --- a/acceptance/air/run-submit-overrides/output.txt +++ b/acceptance/air/run-submit-overrides/output.txt @@ -7,7 +7,7 @@ Override: changing parameters.model.hidden_size from 1024 to 2048 Override: setting parameters.optimizer.learning_rate to 0.001 Submitting experiment: overrides-smoke Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: diff --git a/acceptance/air/run-submit/output.txt b/acceptance/air/run-submit/output.txt index 3119c788b51..1ca32b0df45 100644 --- a/acceptance/air/run-submit/output.txt +++ b/acceptance/air/run-submit/output.txt @@ -5,7 +5,7 @@ Submitting experiment: submit-smoke Uploading [SNAPSHOT_TARBALL]... Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: @@ -112,7 +112,7 @@ Stream logs after submission using: >>> [CLI] air run -f b300.yaml Submitting experiment: b300-submit-smoke Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: @@ -163,7 +163,7 @@ Stream logs after submission using: >>> [CLI] air run -f rtx6000.yaml Submitting experiment: rtx6000-submit-smoke Submitted workload with Job Run ID: 555 -View job run at: [DATABRICKS_URL]/jobs/runs/555 +View job run at: [DATABRICKS_URL]/jobs/runs/555?o=[WORKSPACE_ID] Tip: use --watch when submitting a run to stream logs to your terminal. Stream logs after submission using: diff --git a/cmd/air/format.go b/cmd/air/format.go index 89e2420fd3b..68c956ad4c0 100644 --- a/cmd/air/format.go +++ b/cmd/air/format.go @@ -254,9 +254,14 @@ func roundMillisToSeconds(ms int64) int64 { // dashboardURL builds {host}/jobs/runs/{id}?o={workspace_id}, matching Python // (cli_entrypoint.py:1911). The ?o= workspace id deep-links to the right -// workspace on multi-workspace accounts. +// workspace on multi-workspace accounts; an unknown (zero) workspace id omits +// it rather than linking to ?o=0. func dashboardURL(host string, runID, workspaceID int64) string { - return fmt.Sprintf("%s/jobs/runs/%d?o=%d", strings.TrimRight(host, "/"), runID, workspaceID) + url := fmt.Sprintf("%s/jobs/runs/%d", strings.TrimRight(host, "/"), runID) + if workspaceID == 0 { + return url + } + return fmt.Sprintf("%s?o=%d", url, workspaceID) } // formatDuration turns a number of seconds into a compact human string such as diff --git a/cmd/air/format_test.go b/cmd/air/format_test.go index 7b4bbc80154..c76260d28cb 100644 --- a/cmd/air/format_test.go +++ b/cmd/air/format_test.go @@ -170,6 +170,7 @@ func TestDurationSeconds(t *testing.T) { func TestDashboardURL(t *testing.T) { // The ?o= workspace id and a trailing-slash-trimmed host, matching Python. assert.Equal(t, "https://example.test/jobs/runs/123?o=42", dashboardURL("https://example.test/", 123, 42)) + assert.Equal(t, "https://example.test/jobs/runs/123", dashboardURL("https://example.test/", 123, 0)) } func TestLatestAttemptNumber(t *testing.T) { diff --git a/cmd/air/runsubmit.go b/cmd/air/runsubmit.go index f01198d2b7a..df79fc3a9ca 100644 --- a/cmd/air/runsubmit.go +++ b/cmd/air/runsubmit.go @@ -17,6 +17,7 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/filer" + "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/client" "github.com/databricks/databricks-sdk-go/service/compute" @@ -330,9 +331,10 @@ func stageRunArtifacts(ctx context.Context, launchWriter fileWriter, items []upl // submitWorkload runs the submit happy path: validate the config while uploading // the launch artifacts, ensure the experiment directory, assemble the Jobs // payload, and submit it. It -// returns the new run_id and its dashboard URL. showProgress enables the stderr +// returns the new run_id and its dashboard URL, which carries the ?o= workspace +// hint only when the workspace ID could be resolved. showProgress enables the stderr // staging spinner (text mode only). -func submitWorkload(ctx context.Context, w *databricks.WorkspaceClient, cfg *runConfig, configPath, idempotencyKey string, showProgress bool) (runID int64, dashboardURL string, err error) { +func submitWorkload(ctx context.Context, w *databricks.WorkspaceClient, cfg *runConfig, configPath, idempotencyKey string, showProgress bool) (runID int64, runURL string, err error) { start := time.Now() var snap snapshotResult defer func() { logRunEvent(ctx, cfg, snap, runID, time.Since(start), err) }() @@ -387,6 +389,11 @@ func submitWorkload(ctx context.Context, w *databricks.WorkspaceClient, cfg *run return stageErr }) }) + var workspaceID int64 + group.Go(func() error { + workspaceID = workspaceIDForRunLink(stagingCtx, w) + return nil + }) if err := group.Wait(); err != nil { return 0, "", err } @@ -446,8 +453,30 @@ func submitWorkload(ctx context.Context, w *databricks.WorkspaceClient, cfg *run return 0, "", err } - dashboardURL = strings.TrimRight(w.Config.Host, "/") + "/jobs/runs/" + strconv.FormatInt(runID, 10) - return runID, dashboardURL, nil + return runID, dashboardURL(w.Config.Host, runID, workspaceID), nil +} + +// workspaceIDLookupTimeout caps workspaceIDForRunLink. +const workspaceIDLookupTimeout = 10 * time.Second + +// workspaceIDForRunLink returns the workspace ID for the run link's ?o= hint, +// or 0 when it cannot be resolved. The link is a convenience, so the lookup +// never fails a submission; it is capped, and callers run it before creating +// the run, so that it cannot hold back a new run's ID either. +func workspaceIDForRunLink(ctx context.Context, w *databricks.WorkspaceClient) int64 { + ctx, cancel := context.WithTimeout(ctx, workspaceIDLookupTimeout) + defer cancel() + id, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + log.Debugf(ctx, "air run: could not resolve workspace ID for the run link: %v", err) + return 0 + } + workspaceID, err := strconv.ParseInt(id, 10, 64) + if err != nil { + log.Debugf(ctx, "air run: workspace ID %q for the run link is not numeric", id) + return 0 + } + return workspaceID } func submittedContainers(cfg *runConfig, funcDir string) []submittedContainer { diff --git a/cmd/air/runsubmit_test.go b/cmd/air/runsubmit_test.go index 441a1c12d26..e101bbc28af 100644 --- a/cmd/air/runsubmit_test.go +++ b/cmd/air/runsubmit_test.go @@ -7,6 +7,7 @@ import ( "io/fs" "path" "path/filepath" + "strconv" "strings" "sync" "sync/atomic" @@ -353,6 +354,8 @@ func TestStageRunArtifactsOverlapsLaunchAndSnapshot(t *testing.T) { } func TestSubmitWorkload(t *testing.T) { + // A workspace ID in the developer's environment would replace the test server's in the run link. + t.Setenv("DATABRICKS_WORKSPACE_ID", "") server := testserver.New(t) t.Cleanup(server.Close) @@ -382,7 +385,7 @@ func TestSubmitWorkload(t *testing.T) { runID, dashboardURL, err := submitWorkload(t.Context(), w, cfg, cfgPath, "idem-key", false) require.NoError(t, err) assert.Equal(t, int64(777), runID) - assert.Contains(t, dashboardURL, "/jobs/runs/777") + assert.Equal(t, server.URL+"/jobs/runs/777?o="+strconv.Itoa(testserver.TestWorkspaceID), dashboardURL) assert.Equal(t, "idem-key", validatedToken) // The submitted payload is a native ai_runtime_task pointing at the uploaded @@ -714,6 +717,45 @@ containers: } } +// An unresolved workspace ID yields a link without ?o=, looked up before the run is created. +func TestSubmitWorkloadWithoutWorkspaceID(t *testing.T) { + server := testserver.New(t) + t.Cleanup(server.Close) + + // Register before AddDefaultHandlers: the router is first-wins, so these must claim the routes ahead of the default handlers. + var workspaceIDLookups, lookupsBeforeSubmit atomic.Int32 + server.Handle("GET", "/api/2.0/preview/scim/v2/Me", func(req testserver.Request) any { + // The workspace ID lookup is the /Me call without attributes; answering it + // without the X-Databricks-Org-Id header makes the lookup fail. + if req.URL.Query().Get("attributes") == "" { + workspaceIDLookups.Add(1) + } + return req.Workspace.MeUser(req.Token) + }) + server.Handle("POST", "/api/2.2/jobs/runs/submit", func(req testserver.Request) any { + lookupsBeforeSubmit.Store(workspaceIDLookups.Load()) + return jobs.SubmitRunResponse{RunId: 777} + }) + stubValidateConfig(server) + testserver.AddDefaultHandlers(server) + + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "token"}) + require.NoError(t, err) + // The test server's host metadata supplies a workspace ID; drop it to force the lookup. + w.Config.WorkspaceID = "" + + cfgPath := writeConfigFile(t, "run.yaml", minimalConfig) + cfg, err := loadRunConfig(cfgPath) + require.NoError(t, err) + + runID, runURL, err := submitWorkload(t.Context(), w, cfg, cfgPath, "idem-key", false) + require.NoError(t, err) + assert.Equal(t, int64(777), runID) + assert.Equal(t, server.URL+"/jobs/runs/777", runURL) + assert.Equal(t, int32(1), lookupsBeforeSubmit.Load(), "the workspace ID must be resolved before the run is created") + assert.Equal(t, int32(1), workspaceIDLookups.Load()) +} + // A working-tree code_source is packaged into a tarball, uploaded via libs/filer, // and its remote code_source_path attached to the submitted task. func TestSubmitWorkloadWithCodeSource(t *testing.T) {