Skip to content

Commit 4d88efe

Browse files
Add explicit deployment state modes and optional DMS history (#7151)
## Changes This PR adds modes to StateDB setting the stage for recording history in a best effort way. This supported is added via new mode (WritingFileWithHistoryEnforced) that'll be made the default for dpeloyment history in a followup PR. Existing tests pass - purely refactor.
1 parent f009852 commit 4d88efe

9 files changed

Lines changed: 447 additions & 184 deletions

File tree

‎bundle/direct/bind.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
6666
// The state is opened without a DMS client, so the writes below record nothing;
6767
// phases.Bind and phases.Unbind refuse to run at all when recording is enabled.
6868
var checkStateDB dstate.DeploymentState
69-
if err := checkStateDB.Open(ctx, statePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{}); err != nil {
69+
if err := checkStateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil {
7070
// State that cannot be read is not the same as state without a binding:
7171
// the resource may well be managed already, and binding on top of it
7272
// would take over a resource whose ownership was never checked. A state
@@ -95,7 +95,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
9595
}
9696

9797
// Open temp state
98-
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(false), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
98+
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{})
9999
if err != nil {
100100
os.Remove(tmpStatePath)
101101
return nil, err
@@ -118,7 +118,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
118118
log.Infof(ctx, "Bound %s to id=%s (in temp state)", resourceKey, resourceID)
119119

120120
// First plan + update: populate state with resolved config
121-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
121+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
122122
if err != nil {
123123
os.Remove(tmpStatePath)
124124
return nil, err
@@ -167,7 +167,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
167167
return nil, fmt.Errorf("compacting state: %w", err)
168168
}
169169

170-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
170+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
171171
if err != nil {
172172
os.Remove(tmpStatePath)
173173
return nil, err
@@ -187,7 +187,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
187187
}
188188

189189
// Second plan: this is the plan to present to the user (change between remote resource and config)
190-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), dstate.WithWrite(false), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
190+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
191191
if err != nil {
192192
os.Remove(tmpStatePath)
193193
return nil, err
@@ -237,7 +237,7 @@ func (result *BindResult) Cancel() {
237237
// Unbind removes a resource from direct engine state without deleting
238238
// the workspace resource. Also removes associated permissions/grants entries.
239239
func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error {
240-
err := b.StateDB.Open(ctx, statePath, dstate.WithRecovery(true), dstate.WithWrite(true), dstate.WithDeploymentHistory(false), dstate.OpenDmsArgs{})
240+
err := b.StateDB.Open(ctx, statePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
241241
if err != nil {
242242
if errors.Is(err, dstate.ErrUnsettingRecording) {
243243
return errors.New("unbind is not supported for a bundle target that records deployment history")

‎bundle/direct/dstate/mode.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dstate
2+
3+
// Mode selects the source of resource state and the persistence required for writes.
4+
type Mode int
5+
6+
const (
7+
// ReadingFile reads resources.json and optionally recovers a leftover WAL.
8+
// State writes are disabled.
9+
ReadingFile Mode = iota
10+
11+
// WritingFile records state changes in WAL.
12+
// Finalize merges them into resources.json. DMS is unused.
13+
WritingFile
14+
15+
// WritingDMS reads and writes resource state through DMS without creating a WAL.
16+
// DMS errors fail the deployment.
17+
WritingDMS
18+
19+
// WritingFileWithHistory writes state through WAL to resources.json and records DMS history.
20+
// DMS errors and drain timeouts let the deployment continue.
21+
WritingFileWithHistory
22+
23+
// WritingFileWithHistoryEnforced writes state through WAL to resources.json and records DMS history.
24+
// DMS errors and drain timeouts let the deployment continue.
25+
// Persists deployment_history_best_effort to reject clients that cannot honor history recording.
26+
WritingFileWithHistoryEnforced
27+
)
28+
29+
func (m Mode) isWritable() bool {
30+
switch m {
31+
case WritingFile, WritingDMS, WritingFileWithHistory, WritingFileWithHistoryEnforced:
32+
return true
33+
default:
34+
return false
35+
}
36+
}

0 commit comments

Comments
 (0)