@@ -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.
126126type 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.
465465func (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.
514518type 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
1005968func (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