Skip to content

Commit 910e30f

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 1c97efb commit 910e30f

6 files changed

Lines changed: 133 additions & 155 deletions

File tree

‎acceptance/bundle/dms/bind-unbind-unsupported/output.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
1515

1616
>>> jq {features, state} .databricks/bundle/default/resources.json
1717
{
18-
"features": null,
18+
"features": {
19+
"deployment_history_shadow": {}
20+
},
1921
"state": {}
2022
}

‎bundle/direct/bind.go‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ type BindResult struct {
6363
// Call Finalize to commit the state or Cancel to discard.
6464
func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.WorkspaceClient, configRoot *config.Root, statePath, resourceKey, resourceID string) (*BindResult, error) {
6565
// Check if the resource is already managed (bound to a different ID)
66-
// The state is opened without a DMS client, so the writes below record nothing;
67-
// phases.Bind and phases.Unbind refuse to run at all when recording is enabled.
66+
// Bind uses file state without recording history.
67+
// Open rejects DMS-authoritative state because no DMS metadata is supplied.
6868
var checkStateDB dstate.DeploymentState
69-
if err := checkStateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil {
69+
if err := checkStateDB.Open(ctx, statePath, dstate.WithRecovery(true), nil); 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,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
9595
}
9696

9797
// Open temp state
98-
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(false), dstate.OpenDmsArgs{})
98+
err := b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(false), nil)
99+
if err == nil {
100+
err = b.StateDB.UpgradeToWrite()
101+
}
99102
if err != nil {
100103
os.Remove(tmpStatePath)
101104
return nil, err
@@ -118,7 +121,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
118121
log.Infof(ctx, "Bound %s to id=%s (in temp state)", resourceKey, resourceID)
119122

120123
// First plan + update: populate state with resolved config
121-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
124+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), nil)
122125
if err != nil {
123126
os.Remove(tmpStatePath)
124127
return nil, err
@@ -167,7 +170,10 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
167170
return nil, fmt.Errorf("compacting state: %w", err)
168171
}
169172

170-
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
173+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), nil)
174+
if err == nil {
175+
err = b.StateDB.UpgradeToWrite()
176+
}
171177
if err != nil {
172178
os.Remove(tmpStatePath)
173179
return nil, err
@@ -187,7 +193,7 @@ func (b *DeploymentBundle) Bind(ctx context.Context, client *databricks.Workspac
187193
}
188194

189195
// 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.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
196+
err = b.StateDB.Open(ctx, tmpStatePath, dstate.WithRecovery(true), nil)
191197
if err != nil {
192198
os.Remove(tmpStatePath)
193199
return nil, err
@@ -237,7 +243,10 @@ func (result *BindResult) Cancel() {
237243
// Unbind removes a resource from direct engine state without deleting
238244
// the workspace resource. Also removes associated permissions/grants entries.
239245
func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error {
240-
err := b.StateDB.Open(ctx, statePath, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})
246+
err := b.StateDB.Open(ctx, statePath, dstate.WithRecovery(true), nil)
247+
if err == nil {
248+
err = b.StateDB.UpgradeToWrite()
249+
}
241250
if err != nil {
242251
if errors.Is(err, dstate.ErrUnsettingRecording) {
243252
return errors.New("unbind is not supported for a bundle target that records deployment history")

‎bundle/direct/dstate/state.go‎

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ type DeploymentState struct {
121121
VersionID int
122122
}
123123

124-
// OpenDmsArgs identifies the recorded deployment Open reads from. The zero value means the
125-
// bundle does not record deployment history, or no deployment exists for it yet.
124+
// OpenDmsArgs identifies the authoritative DMS deployment Open reads from.
125+
// An empty value means no deployment exists for it yet.
126126
type OpenDmsArgs struct {
127127
// DeploymentID is the deployment's server-minted id.
128128
DeploymentID string
@@ -463,6 +463,10 @@ func (db *DeploymentState) GetSerial() int {
463463

464464
// isDeploymentMetadataService is IsDeploymentMetadataService for callers already holding db.mu.
465465
func (db *DeploymentState) isDeploymentMetadataService() bool {
466+
if db.mode == ReadingFile {
467+
_, only := db.Data.Features[FeatureDeploymentHistory]
468+
return only
469+
}
466470
return db.mode == WritingDeploymentHistoryOnly
467471
}
468472

@@ -481,8 +485,8 @@ func (db *DeploymentState) RecordsHistory() bool {
481485
defer db.mu.Unlock()
482486
switch db.mode {
483487
case ReadingFile:
484-
_, history := db.Data.Features[FeatureDeploymentHistoryShadow]
485-
return history
488+
_, shadow := db.Data.Features[FeatureDeploymentHistoryShadow]
489+
return shadow || db.isDeploymentMetadataService()
486490
case WritingDeploymentHistoryOnly, WritingDeploymentHistoryShadow:
487491
return true
488492
default:
@@ -513,32 +517,21 @@ func (db *DeploymentState) GetOrInitLineage() string {
513517
// If true, then Open reads the WAL and merges it in the state. If false, and WAL is present, Open returns an error.
514518
type WithRecovery bool
515519

516-
// Open loads deployment state and enables reads or writes according to mode:
517-
// - ReadingFile: reads resources.json; state writes are disabled.
518-
// - WritingFile: reads resources.json and opens a WAL for state writes.
519-
// - WritingDeploymentHistoryOnly: reads and writes resources through DMS; no WAL is opened.
520-
// - WritingDeploymentHistoryShadow: behaves like WritingFile and also records DMS history.
521-
// History errors do not fail the deployment.
522-
// - WritingDeploymentHistoryShadow: also persists a feature marker that rejects
523-
// older clients that do not support history recording.
524-
//
525-
// All modes read deployment metadata and feature markers from resources.json.
526-
// Open rejects modes that conflict with those markers.
520+
// Open loads resource state for reading. File state recovers a leftover WAL when
521+
// withRecovery is set. DMS-authoritative state rejects a WAL and loads resources
522+
// from DMS using dmsDeployment and the workspace client on ctx.
527523
//
528-
// File modes recover a leftover WAL only when withRecovery is set.
529-
// DMS modes reject any leftover WAL.
530-
//
531-
// DMS reads require a workspace client on ctx. DMS writes and history recording
532-
// start after a version exists and InitializeOperationBuffer is called.
533-
func (db *DeploymentState) Open(ctx context.Context, path string, mode Mode, withRecovery WithRecovery, dmsDeployment OpenDmsArgs) error {
524+
// Pass nil for dmsDeployment when the operation only supports file state.
525+
// Open never enables writes; call UpgradeToWrite after approval.
526+
func (db *DeploymentState) Open(ctx context.Context, path string, withRecovery WithRecovery, dmsDeployment *OpenDmsArgs) error {
534527
db.mu.Lock()
535528
defer db.mu.Unlock()
536529

537530
if db.Path != "" {
538531
panic(fmt.Sprintf("state already opened: %v, cannot open %v", db.Path, path))
539532
}
540533

541-
err := db.unlockedOpen(ctx, path, mode, withRecovery, dmsDeployment)
534+
err := db.unlockedOpen(ctx, path, withRecovery, dmsDeployment)
542535
if err != nil {
543536
// A failed open must leave the receiver closed. unlockedOpen assigns
544537
// db.Path before every fallible step, so without this the receiver stays
@@ -567,11 +560,14 @@ func (db *DeploymentState) reset() {
567560
}
568561
}
569562

570-
func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, mode Mode, withRecovery WithRecovery, dmsDeployment OpenDmsArgs) error {
563+
func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, withRecovery WithRecovery, dmsDeployment *OpenDmsArgs) error {
571564
db.Path = path
572-
db.mode = mode
573-
db.DeploymentID = dmsDeployment.DeploymentID
574-
db.VersionID = dmsDeployment.LastVersionID
565+
db.mode = ReadingFile
566+
db.DeploymentID, db.VersionID = "", 0
567+
if dmsDeployment != nil {
568+
db.DeploymentID = dmsDeployment.DeploymentID
569+
db.VersionID = dmsDeployment.LastVersionID
570+
}
575571

576572
// The state file is the source of truth for whether this deployment records history: read it
577573
// first.
@@ -592,6 +588,10 @@ func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, mode M
592588
return fmt.Errorf("migrating state %s: %w", path, err)
593589
}
594590

591+
if db.isDeploymentMetadataService() && dmsDeployment == nil {
592+
return ErrUnsettingRecording
593+
}
594+
595595
db.persistedFeatures = maps.Clone(db.Data.Features)
596596
db.stateIDs = make(map[string]string)
597597
for key, entry := range db.Data.State {
@@ -607,7 +607,7 @@ func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, mode M
607607
return fmt.Errorf("failed to stat WAL file %s: %w", walPath, err)
608608
default: // WAL exists
609609
switch {
610-
case mode == WritingDeploymentHistoryOnly:
610+
case db.isDeploymentMetadataService():
611611
// A recorded deployment writes no WAL, so finding one means this state was written by a
612612
// deployment that did not record history. Refuse rather than discard it: the file is the
613613
// only record of that deploy's writes, and recording is refused below anyway.
@@ -621,28 +621,10 @@ func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, mode M
621621
}
622622
}
623623

624-
// Reconcile the config against the state. A brand-new deployment is where recording begins, so
625-
// the config bootstraps the marker there. On an existing deployment the state is authoritative:
626-
// the config can neither start recording one that was not (its resources would be created a
627-
// second time) nor stop recording one that is (the service still holds it).
628624
_, recorded := db.Data.Features[FeatureDeploymentHistory]
629-
if mode == WritingDeploymentHistoryOnly && recorded && db.Data.Serial > dmsDeployment.LastVersionID {
625+
if recorded && db.Data.Serial > dmsDeployment.LastVersionID {
630626
return fmt.Errorf("inconsistent deployment state: resource state serial (%d) is ahead of deployment history version (%d)", db.Data.Serial, dmsDeployment.LastVersionID)
631627
}
632-
switch {
633-
case mode == WritingDeploymentHistoryOnly && !recorded && len(db.Data.State) == 0:
634-
if db.Data.Features == nil {
635-
db.Data.Features = make(map[string]struct{}, 1)
636-
}
637-
db.Data.Features[FeatureDeploymentHistory] = struct{}{}
638-
recorded = true
639-
case mode == WritingDeploymentHistoryOnly && !recorded:
640-
return errors.New(`enabling experimental.deployment_history for an existing deployment is not supported
641-
642-
Run "databricks bundle destroy" first, then deploy again with deployment history enabled`)
643-
case mode != WritingDeploymentHistoryOnly && recorded:
644-
return ErrUnsettingRecording
645-
}
646628

647629
if recorded {
648630
// The service is the source of truth for a recorded deployment; the file is a tombstone
@@ -678,14 +660,6 @@ Run "databricks bundle destroy" first, then deploy again with deployment history
678660
db.VersionID = dmsDeployment.LastVersionID
679661
}
680662

681-
switch mode {
682-
case ReadingFile:
683-
return nil
684-
case WritingDeploymentHistoryOnly:
685-
return nil
686-
case WritingFile, WritingDeploymentHistoryShadow:
687-
return db.setupWal()
688-
}
689663
return nil
690664
}
691665

@@ -955,17 +929,6 @@ func (db *DeploymentState) setupWal() error {
955929
CLIVersion: build.GetInfo().Version,
956930
Features: maps.Clone(db.Data.Features),
957931
}
958-
switch db.mode {
959-
case WritingDeploymentHistoryShadow:
960-
if walHead.Features == nil {
961-
walHead.Features = make(map[string]struct{})
962-
}
963-
walHead.Features[FeatureDeploymentHistoryShadow] = struct{}{}
964-
case WritingFile:
965-
walHead.Features = map[string]struct{}{}
966-
default:
967-
// Other modes preserve the existing markers.
968-
}
969932
if err := appendJSONLine(db.walFile, walHead); err != nil {
970933
// Remove the WAL created just above: O_EXCL means it is ours, and
971934
// without a header it carries nothing to recover. Leaving it makes
@@ -1004,9 +967,7 @@ func (db *DeploymentState) AssertOpenedForReadOrWrite() {
1004967

1005968
func (db *DeploymentState) AssertOpenedForRead() {
1006969
db.AssertOpenedForReadOrWrite()
1007-
// DMS has no separate read-only mode.
1008-
// Planning reads DMS state while it is already in WritingDeploymentHistoryOnly.
1009-
if db.mode.isWritable() && db.mode != WritingDeploymentHistoryOnly {
970+
if db.mode.isWritable() {
1010971
panic("internal error: DeploymentState must be opened in read mode")
1011972
}
1012973
}

0 commit comments

Comments
 (0)