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/bundles/8068.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix `bundle deploy` and `bundle destroy` with the direct engine sometimes failing with "Cluster ... does not exist" when a cluster in the state was already deleted. ([#8068](https://github.com/databricks/cli/pull/8068))
4 changes: 2 additions & 2 deletions acceptance/bundle/migrate/auto/artifacts/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
=== Redeploy with direct default: auto-migration runs after the build, so library refs are resolved

>>> [CLI] bundle deploy
Warn: unable to get spark version for cluster 0717-132531-5opeqon1, err: Not Found
Warn: unable to get spark version for cluster 0717-132531-5opeqon1, err: Cluster 0717-132531-5opeqon1 does not exist
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
Uploading dist/my_test_code-0.0.1-py3-none-any.whl...
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Expand Down Expand Up @@ -39,7 +39,7 @@ ID Type Language Path
=== Subsequent deploy is a no-op on direct

>>> [CLI] bundle deploy
Warn: unable to get spark version for cluster 0717-132531-5opeqon1, err: Not Found
Warn: unable to get spark version for cluster 0717-132531-5opeqon1, err: Cluster 0717-132531-5opeqon1 does not exist
Uploading dist/my_test_code-0.0.1-py3-none-any.whl...
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Files: 0 uploaded, 0 deleted
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bundle:
name: test-bundle

resources:
clusters:
foo:
cluster_name: foo
spark_version: 13.3.x-scala2.12
node_type_id: i3.xlarge
num_workers: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Created clusters.foo
Files: 1 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

=== deploy: the cluster is still in config, so it is created again

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Created clusters.foo
Files: 2 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

>>> print_requests.py //clusters
{
"method": "POST",
"path": "/api/2.1/clusters/create",
"body": {
"autotermination_minutes": 60,
"cluster_name": "foo",
"node_type_id": "[NODE_TYPE_ID]",
"num_workers": 1,
"spark_version": "13.3.x-scala2.12"
}
}

=== saved plan: the delete was planned while the cluster existed

>>> [CLI] bundle plan -o json

>>> [CLI] bundle deploy --plan plan.json --auto-approve
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Warn: destroying resources.clusters.foo: Treating resources.clusters.foo id=[FOO_ID_2] as already deleted despite delete error: Cluster [FOO_ID_2] does not exist
Deleted clusters.foo
Files: 3 uploaded, 0 deleted
Resources: 0 created, 0 changed, 1 deleted, 0 unchanged

>>> print_state.py
[]

>>> print_requests.py //clusters
{
"method": "POST",
"path": "/api/2.1/clusters/permanent-delete",
"body": {
"cluster_id": "[FOO_ID_2]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# A cluster is permanently deleted out of band. The backend then answers the cluster GET,
# library-status and permanent-delete for it with 400 INVALID_PARAMETER_VALUE "does not exist".
trace $CLI bundle deploy
cluster_id=$(read_id.py foo)
print_requests.py > LOG.requests_deploy

title "deploy: the cluster is still in config, so it is created again"
$CLI clusters permanent-delete $cluster_id
print_requests.py > LOG.requests_oob_delete
trace $CLI bundle deploy
trace print_requests.py //clusters

title "saved plan: the delete was planned while the cluster existed"
update_file.py databricks.yml " clusters:
foo:
cluster_name: foo
spark_version: 13.3.x-scala2.12
node_type_id: i3.xlarge
num_workers: 1
" " {}
"
trace $CLI bundle plan -o json > plan.json
$CLI clusters permanent-delete $(read_id.py foo)
print_requests.py > LOG.requests_plan
trace $CLI bundle deploy --plan plan.json --auto-approve
trace print_state.py | jq -c ".state | keys"
trace print_requests.py //clusters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = false
RecordRequests = true
Ignore = [".databricks/", "plan.json"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bundle:
name: test-bundle

resources:
clusters:
foo:
cluster_name: foo
spark_version: 13.3.x-scala2.12
node_type_id: i3.xlarge
num_workers: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

=== destroy

>>> [CLI] bundle destroy --auto-approve
All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default

Destroy: 0 deleted

>>> print_state.py

>>> print_requests.py --get --sort //clusters //libraries
{
"method": "GET",
"path": "/api/2.0/libraries/cluster-status",
"q": {
"cluster_id": "1126-000000-deleted0"
}
}
{
"method": "GET",
"path": "/api/2.1/clusters/get",
"q": {
"cluster_id": "1126-000000-deleted0"
}
}

=== deploy: the cluster is removed from config

>>> [CLI] bundle deploy --auto-approve
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Files: 2 uploaded, 0 deleted
Resources: 0 created, 0 changed, 0 deleted, 0 unchanged

>>> print_state.py
[]

>>> print_requests.py --get --sort //clusters //libraries
{
"method": "GET",
"path": "/api/2.0/libraries/cluster-status",
"q": {
"cluster_id": "1126-000000-deleted0"
}
}
{
"method": "GET",
"path": "/api/2.1/clusters/get",
"q": {
"cluster_id": "1126-000000-deleted0"
}
}
30 changes: 30 additions & 0 deletions acceptance/bundle/resources/clusters/deleted-out-of-band/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# The state points at a cluster that was deleted out of band. The backend answers clusters/get
# and libraries/cluster-status for it with 400 INVALID_PARAMETER_VALUE "does not exist"; the
# stub in test.toml delays the cluster GET so the library-status GET answers first.
seed_state() {
mkdir -p .databricks/bundle/default
cp state.json .databricks/bundle/default/resources.json
# MSYS_NO_PATHCONV=1 keeps Git Bash on Windows from rewriting the workspace path.
MSYS_NO_PATHCONV=1 $CLI workspace mkdirs /Workspace/Users/$CURRENT_USER_NAME/.bundle/test-bundle/default
}

# Destroy runs first: a deploy pushes a newer state that the destroy would pull instead.
title "destroy"
seed_state
trace $CLI bundle destroy --auto-approve
trace print_state.py | jq -c ".state | keys"
trace print_requests.py --get --sort //clusters //libraries

title "deploy: the cluster is removed from config"
seed_state
update_file.py databricks.yml " clusters:
foo:
cluster_name: foo
spark_version: 13.3.x-scala2.12
node_type_id: i3.xlarge
num_workers: 1
" " {}
"
trace $CLI bundle deploy --auto-approve
trace print_state.py | jq -c ".state | keys"
trace print_requests.py --get --sort //clusters //libraries
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"state_version": 3,
"lineage": "test-lineage",
"serial": 1,
"state": {
"resources.clusters.foo": {
"__id__": "1126-000000-deleted0",
"state": {
"autotermination_minutes": 60,
"cluster_name": "foo",
"node_type_id": "i3.xlarge",
"num_workers": 1,
"spark_version": "13.3.x-scala2.12"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Cloud = false
RecordRequests = true
Ignore = [".databricks/"]

[[Server]]
Pattern = "GET /api/2.1/clusters/get"
Delay = "300ms"
Response.StatusCode = 400
Response.Body = '''{"error_code": "INVALID_PARAMETER_VALUE", "message": "Cluster 1126-000000-deleted0 does not exist"}'''
35 changes: 23 additions & 12 deletions bundle/direct/dresources/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"

"github.com/databricks/cli/bundle/config/resources"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/databricks/databricks-sdk-go/marshal"
"github.com/databricks/databricks-sdk-go/retries"
"github.com/databricks/databricks-sdk-go/service/compute"
"golang.org/x/sync/errgroup"
)

// librariesWaitTimeout bounds how long we poll for libraries to finish installing.
Expand Down Expand Up @@ -153,19 +153,30 @@ func (r *ResourceCluster) DoRead(ctx context.Context, id string) (*ClusterRemote
var libraries []compute.Library

// The cluster GET and the library-status GET are independent, so run them concurrently.
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
var err error
details, err = r.client.Clusters.GetByClusterId(ctx, id)
return err
// The cluster GET error takes precedence: for a deleted cluster both endpoints fail with
// 400 INVALID_PARAMETER_VALUE, but the SDK maps only the cluster GET one to
// ErrResourceDoesNotExist, so a library-status error that came first would make a missing
// cluster look like a read failure.
librariesCtx, cancelLibraries := context.WithCancel(ctx)
defer cancelLibraries()
var detailsErr, librariesErr error
var wg sync.WaitGroup
wg.Go(func() {
details, detailsErr = r.client.Clusters.GetByClusterId(ctx, id)
if detailsErr != nil {
// The library status is not needed once the cluster GET failed.
cancelLibraries()
}
})
g.Go(func() error {
var err error
libraries, err = r.readLibraries(ctx, id)
return err
wg.Go(func() {
libraries, librariesErr = r.readLibraries(librariesCtx, id)
})
if err := g.Wait(); err != nil {
return nil, err
wg.Wait()
if detailsErr != nil {
return nil, detailsErr
}
if librariesErr != nil {
return nil, librariesErr
}

remote := &ClusterRemote{
Expand Down
25 changes: 25 additions & 0 deletions bundle/direct/dresources/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package dresources

import (
"context"
"net/http"
"testing"
"time"

"github.com/databricks/cli/bundle/deployplan"
"github.com/databricks/cli/libs/structs/structpath"
"github.com/databricks/cli/libs/testserver"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -66,3 +72,22 @@ func TestClusterOverrideChangeDescLibraries(t *testing.T) {
})
}
}
Comment thread
denik marked this conversation as resolved.
Resolved

func TestClusterDoReadCancelsLibrariesWhenClusterIsMissing(t *testing.T) {
server := testserver.New(t)
server.Handle("GET", "/api/2.0/libraries/cluster-status", func(req testserver.Request) any {
// Answers only once the client gives up on the request.
<-req.Context.Done()
return testserver.Response{StatusCode: http.StatusServiceUnavailable}
})
testserver.AddDefaultHandlers(server)
client, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "testtoken"})
require.NoError(t, err)
r := (&ResourceCluster{}).New(client).(*ResourceCluster)

ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
_, err = r.DoRead(ctx, "1126-000000-abcdefgh")
assert.True(t, apierr.IsMissing(err), "expected a not-found error, got %v", err)
assert.NoError(t, ctx.Err(), "DoRead waited for the library status after the cluster GET failed")
}
18 changes: 17 additions & 1 deletion libs/testserver/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testserver
import (
"encoding/json"
"fmt"
"net/http"
"os"

"github.com/databricks/cli/libs/structs/structaccess"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (s *FakeWorkspace) ClustersGet(req Request, clusterId string) any {

cluster, ok := s.Clusters[clusterId]
if !ok {
return Response{StatusCode: 404}
return clusterDoesNotExist(clusterId)
}

// Simulate cluster startup: transition PENDING → RUNNING so that WaitGetClusterRunning
Expand Down Expand Up @@ -276,6 +277,21 @@ func (s *FakeWorkspace) ClustersPermanentDelete(req Request) any {
}

defer s.LockUnlock()()
if _, ok := s.Clusters[request.ClusterId]; !ok {
return clusterDoesNotExist(request.ClusterId)
}
delete(s.Clusters, request.ClusterId)
return Response{}
}

// clusterDoesNotExist is how the cloud answers clusters/get, clusters/permanent-delete and
// libraries/cluster-status for a missing cluster: 400 INVALID_PARAMETER_VALUE rather than 404.
func clusterDoesNotExist(clusterId string) Response {
return Response{
StatusCode: http.StatusBadRequest,
Body: map[string]string{
"error_code": "INVALID_PARAMETER_VALUE",
"message": "Cluster " + clusterId + " does not exist",
},
}
}
2 changes: 1 addition & 1 deletion libs/testserver/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *FakeWorkspace) LibrariesClusterStatus(req Request, clusterId string) an
defer s.LockUnlock()()

if _, ok := s.Clusters[clusterId]; !ok {
return Response{StatusCode: http.StatusNotFound}
return clusterDoesNotExist(clusterId)
}

installed := s.ClusterLibraries[clusterId]
Expand Down
Loading