Skip to content

Commit 336e45d

Browse files
Open deployment state for reading and defer write setup
Addresses denik review comment 4387774406. Remove Open mode selection; use loaded feature markers for the resource source and UpgradeToWrite for write policy. Nil DMS metadata retains file-only compatibility checks for bind and unbind.
1 parent cf469be commit 336e45d

8 files changed

Lines changed: 211 additions & 219 deletions

File tree

‎bundle/direct/bind.go‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
6565
// Check if the resource is already managed (bound to a different ID)
6666
// Bind imports file state without creating a deployment history version.
6767
var checkStateDB dstate.DeploymentState
68-
if err := checkStateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil {
68+
if err := checkStateDB.Open(ctx, statePath, dstate.WithRecovery(true)); err != nil {
6969
// State that cannot be read is not the same as state without a binding:
7070
// the resource may well be managed already, and binding on top of it
7171
// would take over a resource whose ownership was never checked. A state
7272
// file that does not exist yet opens successfully as an empty one.
7373
return nil, fmt.Errorf("cannot check whether %s is already bound: %w", resourceKey, err)
7474
}
75+
if checkStateDB.IsDeploymentMetadataService() {
76+
return nil, errors.New("bind is not supported for a bundle target that records deployment history")
77+
}
7578
existingID := checkStateDB.GetResourceID(resourceKey)
7679
if _, err := checkStateDB.Finalize(ctx); err != nil {
7780
log.Warnf(ctx, "failed to finalize state: %v", err)
@@ -94,7 +97,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
9497
}
9598

9699
// Open temp state
97-
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{})
100+
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(false))
101+
if err == nil {
102+
err = b.StateDB.UpgradeToWrite()
103+
}
98104
if err != nil {
99105
os.Remove(tmpStatePath)
100106
return nil, err
@@ -117,7 +123,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
117123
log.Infof(ctx, "Bound %s to id=%s (in temp state)", resourceKey, resourceID)
118124

119125
// First plan + update: populate state with resolved config
120-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
126+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true))
121127
if err != nil {
122128
os.Remove(tmpStatePath)
123129
return nil, err
@@ -166,7 +172,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
166172
return nil, fmt.Errorf("compacting state: %w", err)
167173
}
168174

169-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
175+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true))
176+
if err == nil {
177+
err = b.StateDB.UpgradeToWrite()
178+
}
170179
if err != nil {
171180
os.Remove(tmpStatePath)
172181
return nil, err
@@ -186,7 +195,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
186195
}
187196

188197
// Second plan: this is the plan to present to the user (change between remote resource and config)
189-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
198+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true))
190199
if err != nil {
191200
os.Remove(tmpStatePath)
192201
return nil, err
@@ -236,16 +245,18 @@ func (result *BindResult) Cancel() {
236245
// Unbind removes a resource from direct engine state without deleting
237246
// the workspace resource. Also removes associated permissions/grants entries.
238247
func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error {
239-
err := b.StateDB.Open(ctx, statePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
240-
if err != nil {
241-
if errors.Is(err, dstate.ErrUnsettingRecording) {
242-
return errors.New("unbind is not supported for a bundle target that records deployment history")
243-
}
248+
if err := b.StateDB.Open(ctx, statePath, dstate.WithRecovery(true)); err != nil {
249+
return err
250+
}
251+
if b.StateDB.IsDeploymentMetadataService() {
252+
return errors.New("unbind is not supported for a bundle target that records deployment history")
253+
}
254+
if err := b.StateDB.UpgradeToWrite(); err != nil {
244255
return err
245256
}
246257

247258
// Delete the main resource
248-
err = b.StateDB.DeleteState(ctx, resourceKey, false)
259+
err := b.StateDB.DeleteState(ctx, resourceKey, false)
249260
if err != nil {
250261
return err
251262
}

‎bundle/direct/bind_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package direct
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/databricks/cli/bundle/direct/dstate"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestBindAndUnbindRejectDMSAuthoritativeState(t *testing.T) {
15+
for _, operation := range []string{"bind", "unbind"} {
16+
t.Run(operation, func(t *testing.T) {
17+
path := filepath.Join(t.TempDir(), "resources.json")
18+
state := dstate.NewDatabase("lineage", 1)
19+
state.Features = map[string]struct{}{dstate.FeatureDeploymentHistory: {}}
20+
data, err := json.Marshal(state)
21+
require.NoError(t, err)
22+
require.NoError(t, os.WriteFile(path, data, 0o600))
23+
24+
var db DeploymentBundle
25+
// Reject file-only operations before they need a resource API client.
26+
if operation == "bind" {
27+
_, err = db.Bind(t.Context(), nil, nil, path, "resources.jobs.foo", "123")
28+
} else {
29+
err = db.Unbind(t.Context(), path, "resources.jobs.foo")
30+
}
31+
require.ErrorContains(t, err, operation+" is not supported")
32+
assert.NoFileExists(t, path+".wal")
33+
persisted, err := os.ReadFile(path)
34+
require.NoError(t, err)
35+
assert.Equal(t, data, persisted)
36+
})
37+
}
38+
}

‎bundle/direct/dstate/dms.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package dstate
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67

78
"github.com/databricks/cli/bundle/deployplan"
9+
"github.com/databricks/cli/libs/cmdctx"
810
"github.com/databricks/cli/libs/dms"
11+
"github.com/databricks/cli/libs/retry"
12+
"github.com/databricks/databricks-sdk-go/service/bundledeployments"
913
)
1014

1115
// RecordedState is what the CLI serializes into the DMS operation's state field. It wraps the
@@ -16,6 +20,43 @@ type RecordedState struct {
1620
DependsOn []deployplan.DependsOnEntry `json:"depends_on,omitempty"`
1721
}
1822

23+
// LoadDMSState loads authoritative resources into an already-open read-only checkpoint.
24+
// The service version must be at least as recent as the file's cached serial.
25+
func (db *DeploymentState) LoadDMSState(ctx context.Context, deploymentID string, lastVersionID int) error {
26+
db.mu.Lock()
27+
defer db.mu.Unlock()
28+
db.AssertOpenedForRead()
29+
30+
if db.Data.Serial > lastVersionID {
31+
return fmt.Errorf("inconsistent deployment state: resource state serial (%d) is ahead of deployment history version (%d)", db.Data.Serial, lastVersionID)
32+
}
33+
if deploymentID != "" {
34+
w := cmdctx.WorkspaceClient(ctx)
35+
recorded, err := retry.With(ctx, retry.IsTransientRead, func() ([]bundledeployments.Resource, error) {
36+
return w.BundleDeployments.ListResourcesAll(ctx, bundledeployments.ListResourcesRequest{
37+
Parent: dms.DeploymentName(deploymentID),
38+
})
39+
})
40+
if err != nil {
41+
return fmt.Errorf("listing resources from the deployment history service: %w", err)
42+
}
43+
resources := make([]dms.Resource, len(recorded))
44+
for i, res := range recorded {
45+
// DMS keys drop the "resources." prefix; add it back for local state.
46+
resources[i] = dms.Resource{
47+
Key: dms.StatePrefix + res.ResourceKey,
48+
ID: res.ResourceId,
49+
State: res.State,
50+
}
51+
}
52+
if err := db.applyDMSState(resources); err != nil {
53+
return err
54+
}
55+
}
56+
db.DeploymentID, db.VersionID = deploymentID, lastVersionID
57+
return nil
58+
}
59+
1960
// applyDMSState fills in the resource state from what DMS recorded. For a deployment that uses DMS
2061
// resources.json does not track state. The service is queried for state for resources and then they
2162
// are filled in-place in DeploymentState.Data.State:

0 commit comments

Comments
 (0)