Skip to content
Merged
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/convert-to-dabs-root-path-retries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `air convert-to-dabs` now explains the `--output-dir` workaround when `code_source.root_path` resolves to the bundle root, and notes when the generated job carries the default `max_retries`. ([#7579](https://github.com/databricks/cli/pull/7579))
12 changes: 12 additions & 0 deletions acceptance/air/convert-to-dabs/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ persistent job that is not garbage-collected. When you are done, remove the
job and its uploaded files with:
[CLI] bundle destroy

Note: the YAML does not set max_retries, so the job uses the `air run` default
(max_retries: 3, retry_on_timeout: true). Edit databricks.yml to change it.

=== emitted databricks.yml (code_source packaged as a tgz artifact; no code is copied)

>>> cat databricks.yml
Expand Down Expand Up @@ -111,6 +114,9 @@ persistent job that is not garbage-collected. When you are done, remove the
job and its uploaded files with:
[CLI] bundle destroy

Note: the YAML does not set max_retries, so the job uses the `air run` default
(max_retries: 3, retry_on_timeout: true). Edit databricks.yml to change it.

=== convert a B300 run using the current SDK

>>> [CLI] air convert-to-dabs b300.yaml --output-dir generated-b300
Expand All @@ -133,6 +139,9 @@ persistent job that is not garbage-collected. When you are done, remove the
job and its uploaded files with:
[CLI] bundle destroy

Note: the YAML does not set max_retries, so the job uses the `air run` default
(max_retries: 3, retry_on_timeout: true). Edit databricks.yml to change it.

=== the B300 bundle preserves its accelerator and AI Runtime version

>>> cat generated-b300/databricks.yml
Expand Down Expand Up @@ -198,6 +207,9 @@ persistent job that is not garbage-collected. When you are done, remove the
job and its uploaded files with:
[CLI] bundle destroy

Note: the YAML does not set max_retries, so the job uses the `air run` default
(max_retries: 3, retry_on_timeout: true). Edit databricks.yml to change it.

>>> grep -E environment_version|base_environment databricks.yml
base_environment: workspace-base-environments/databricks_ai_v5

Expand Down
30 changes: 25 additions & 5 deletions cmd/air/convert_to_dabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ does not contact the workspace.`,
}

printConvertNextSteps(ctx, dir, written, bundleResourceKey(cfg.ExperimentName))
if note := retryDefaultsNote(cfg); note != "" {
cmdio.LogString(ctx, "")
cmdio.LogString(ctx, note)
}
return nil
}

Expand Down Expand Up @@ -203,10 +207,16 @@ func codeArtifactFor(cfg *runConfig, codeDirPath string) (*codeArtifact, error)
// A code dir that resolves to the bundle root has no basename to nest under: the
// archive would sit directly at /databricks/code_source (no <dir>), and an include
// rooted at "." would also sweep the bundle's own generated files (databricks.yml,
// generated_artifacts/, the output tarball) into it. Reject rather than emit that;
// the user should point root_path at a subdirectory.
// generated_artifacts/, the output tarball) into it. Reject rather than emit that.
if codeDirRel == "." {
return nil, fmt.Errorf("code_source root_path %q resolves to the bundle root; convert-to-dabs cannot package code there (no code directory to nest under %s/<dir>). Point root_path at a subdirectory", snap.RootPath, runtimeCodeSourceRoot)
remedy := "move the code into a subdirectory (for example ./src) and point root_path at it"
// A bundle rooted at the parent only works for a working-tree snapshot: a git
// snapshot is archived from the bundle root, which would then sit outside the
// repository (or inside an unrelated one).
if snap.Git == nil {
remedy += ", or pass --output-dir with a dedicated parent directory that contains the code directory"
}
return nil, fmt.Errorf("code_source root_path %q resolves to the bundle root, so there is no code directory to nest under %s/<dir>; %s", snap.RootPath, runtimeCodeSourceRoot, remedy)
}
dirName := path.Base(codeDirRel)
art := &codeArtifact{
Expand Down Expand Up @@ -558,8 +568,8 @@ func writeBundle(ctx context.Context, cfg *runConfig, configPath, dir string, fo
// Refuse to clobber an existing file, with one consistent message. A user
// re-running convert into the same dir gets a clear error rather than a silent
// overwrite of edits they may have made. The hint names --force rather than
// --output-dir: the code_source must live inside the bundle dir, so redirecting
// the output usually isn't a usable escape hatch for an in-place conversion.
// --output-dir: a re-run is replacing a bundle that already sits where the
// code_source needs it, so writing a second copy elsewhere isn't what the user wants.
artifactDir := filepath.Join(dir, generatedArtifactsDir)
if err := os.MkdirAll(artifactDir, 0o700); err != nil {
return nil, err
Expand Down Expand Up @@ -647,6 +657,16 @@ func printConvertNextSteps(ctx context.Context, dir string, written []string, jo
cmdio.LogString(ctx, " "+self+" bundle destroy")
}

// retryDefaultsNote explains retry settings the bundle carries but the YAML omits:
// `air run` applies them implicitly, so written down they look invented by the
// conversion. Returns "" when the YAML sets max_retries.
func retryDefaultsNote(cfg *runConfig) string {
if cfg.MaxRetries != nil {
return ""
}
return fmt.Sprintf("Note: the YAML does not set max_retries, so the job uses the `air run` default\n(max_retries: %d, retry_on_timeout: %t). Edit databricks.yml to change it.", defaultMaxRetries, defaultMaxRetries > 0)
}

// cliInvocation is how the user should spell this binary in a follow-up command.
// A path-qualified argv[0] (./dbcli, ../dbcli) is kept as typed so copy-paste works
// from the same cwd; a bare name resolved via PATH is reported as "databricks".
Expand Down
79 changes: 73 additions & 6 deletions cmd/air/convert_to_dabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -628,13 +629,17 @@ func TestConvertToDabsIncludePathsDirectories(t *testing.T) {

// root_path "." (the code source is the whole bundle directory) has no basename to
// nest the archive under, and an include rooted at "." would sweep the bundle's own
// generated files into the tarball — so a git/include snapshot there is rejected with
// a message pointing at a subdirectory. (A plain snapshot with root_path "." is not an
// artifact case and is unaffected.)
// generated files into the tarball, so every snapshot there is rejected. Only a
// working-tree snapshot is offered --output-dir: a git snapshot is archived from the
// bundle root, which a parent directory would move outside the repository.
func TestConvertToDabsDotRootPathRejected(t *testing.T) {
for _, tc := range []struct{ name, snap string }{
{"git", "code_source:\n type: snapshot\n snapshot:\n root_path: .\n git:\n commit: abc123\n"},
{"include", "code_source:\n type: snapshot\n snapshot:\n root_path: .\n include_paths:\n - foo\n"},
for _, tc := range []struct {
name, snap string
wantOutputDirHint bool
}{
{"plain", "code_source:\n type: snapshot\n snapshot:\n root_path: .\n", true},
{"include", "code_source:\n type: snapshot\n snapshot:\n root_path: .\n include_paths:\n - foo\n", true},
{"git", "code_source:\n type: snapshot\n snapshot:\n root_path: .\n git:\n commit: abc123\n", false},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
Expand All @@ -648,6 +653,68 @@ func TestConvertToDabsDotRootPathRejected(t *testing.T) {

_, _, err = convertToDabs(t.Context(), loaded, path, dir)
require.ErrorContains(t, err, "resolves to the bundle root")
require.ErrorContains(t, err, "subdirectory")
if tc.wantOutputDirHint {
require.ErrorContains(t, err, "--output-dir")
} else {
require.NotContains(t, err.Error(), "--output-dir")
}
})
}
}

// The --output-dir remedy the rejection suggests: with the bundle rooted at the
// parent, the code dir has a basename to nest under and converts.
func TestConvertToDabsDotRootPathWithParentOutputDir(t *testing.T) {
parent := t.TempDir()
dir := filepath.Join(parent, "proj")
require.NoError(t, os.MkdirAll(dir, 0o700))
cfg := "experiment_name: dot\ncommand: python train.py\n" +
"compute: {accelerator_type: GPU_1xA10, num_accelerators: 1}\n" +
"code_source:\n type: snapshot\n snapshot:\n root_path: .\n"
path := filepath.Join(dir, "run.yaml")
require.NoError(t, os.WriteFile(path, []byte(cfg), 0o600))
loaded, err := loadRunConfig(path)
require.NoError(t, err)

root, _, err := convertToDabs(t.Context(), loaded, path, parent)
require.NoError(t, err)

a := "artifacts." + codeSourceArtifactKey
assert.Equal(t, ".", get(t, root, a+".path").MustString())
inc := get(t, root, a+".include").MustSequence()
require.Len(t, inc, 1)
assert.Equal(t, "proj", inc[0].MustString())
}

func TestRetryDefaultsNote(t *testing.T) {
note := retryDefaultsNote(&runConfig{})
assert.Contains(t, note, "max_retries: 3")
assert.Contains(t, note, "retry_on_timeout: true")

// An explicit value, including 0, is the user's own: nothing to explain.
assert.Empty(t, retryDefaultsNote(&runConfig{MaxRetries: new(0)}))
assert.Empty(t, retryDefaultsNote(&runConfig{MaxRetries: new(2)}))
}

func TestConvertToDabsCommandPrintsRetryNoteOnlyWhenUnset(t *testing.T) {
for _, tc := range []struct {
name, extra string
wantNote bool
}{
{"unset", "", true},
{"zero", "max_retries: 0\n", false},
{"set", "max_retries: 2\n", false},
} {
t.Run(tc.name, func(t *testing.T) {
path := writeConfigFile(t, "run.yaml", minimalConfig+tc.extra)
ctx, stderr := cmdio.NewTestContextWithStderr(t.Context())
cmd := newConvertToDabsCommand()
cmd.SetContext(ctx)
require.NoError(t, cmd.RunE(cmd, []string{path}))

assert.Contains(t, stderr.String(), "Wrote a Databricks Asset Bundle")
assert.Equal(t, tc.wantNote, strings.Contains(stderr.String(), "does not set max_retries"))
})
}
}
Expand Down
Loading