-
Notifications
You must be signed in to change notification settings - Fork 177
bundle/deploy/lock: introduce DeploymentManager interface aligned with DMS versioning model #5314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b12b028
bundle: extract DeploymentLock interface + workspace filesystem impl
shreyas-goenka 37a4549
bundle/deploy/lock: own *locker.Locker on the lock object, not on bun…
shreyas-goenka 7044d79
bundle/deploy/lock: decouple workspaceFilesystemLock from bundle.Bundle
shreyas-goenka 1c466f3
bundle/deploy/lock: drop the permission-error callback indirection
shreyas-goenka ba40b6e
bundle/deploy/lock: drop redundant Errorf on failed lock acquire
shreyas-goenka 709380f
bundle/phases: surface Release errors via logdiag (restore pre-refact…
shreyas-goenka 584602f
bundle/deploy/lock: lift the *bundle.Bundle reference off workspaceFi…
shreyas-goenka 36523ea
acceptance/pipelines: drop duplicate "Error: Failed to acquire deploy…
shreyas-goenka 57288ab
Revert: restore "Failed to acquire deployment lock" log line + goldens
shreyas-goenka 8ae888f
bundle/deploy/lock: only init WorkspaceClient when locking is enabled
shreyas-goenka 7a99e7d
bundle/deploy/lock: rename to DeploymentManager with CreateVersion/Cl…
shreyas-goenka a9c1407
bundle/deploy/lock: rename CloseVersion to CompleteVersion
shreyas-goenka ee860e8
bundle/deploy/lock: replace DeploymentManager interface with concrete…
shreyas-goenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/permissions" | ||
| "github.com/databricks/cli/libs/diag" | ||
| ) | ||
|
|
||
| // Goal describes the purpose of a deployment operation. | ||
| type Goal string | ||
|
|
||
| const ( | ||
| GoalBind = Goal("bind") | ||
| GoalUnbind = Goal("unbind") | ||
| GoalDeploy = Goal("deploy") | ||
| GoalDestroy = Goal("destroy") | ||
| ) | ||
|
|
||
| // DeploymentStatus indicates whether the deployment operation succeeded or failed. | ||
| type DeploymentStatus int | ||
|
|
||
| const ( | ||
| DeploymentSuccess DeploymentStatus = iota | ||
| DeploymentFailure | ||
| ) | ||
|
|
||
| // DeploymentLock manages the lifecycle of a bundle deployment. | ||
| // The workspace-filesystem lock serializes concurrent deployments. | ||
| // DMS version tracking will be added additively — see deployment_metadata_service.go. | ||
| type DeploymentLock struct { | ||
| wfs workspaceFilesystemLock | ||
| } | ||
|
|
||
| // NewDeploymentLock returns a DeploymentLock for the bundle. | ||
| // Captures everything it needs from the bundle at construction time | ||
| // so the lock does not retain a *bundle.Bundle reference. The | ||
| // workspace client is only initialized when locking is enabled. | ||
| func NewDeploymentLock(ctx context.Context, b *bundle.Bundle, goal Goal) *DeploymentLock { | ||
| enabled := b.Config.Bundle.Deployment.Lock.IsEnabled() | ||
| l := &DeploymentLock{ | ||
| wfs: workspaceFilesystemLock{ | ||
| user: b.Config.Workspace.CurrentUser.UserName, | ||
| statePath: b.Config.Workspace.StatePath, | ||
| enabled: enabled, | ||
| force: b.Config.Bundle.Deployment.Lock.Force, | ||
| goal: goal, | ||
| reportPermissionError: func(ctx context.Context, path string) diag.Diagnostics { | ||
| return permissions.ReportPossiblePermissionDenied(ctx, b, path) | ||
| }, | ||
| }, | ||
| } | ||
| if enabled { | ||
| l.wfs.client = b.WorkspaceClient(ctx) | ||
| } | ||
| return l | ||
| } | ||
|
|
||
| // Acquire acquires the deployment lock. | ||
| func (l *DeploymentLock) Acquire(ctx context.Context) error { | ||
| return l.wfs.acquire(ctx) | ||
| } | ||
|
|
||
| // Release releases the deployment lock. | ||
| func (l *DeploymentLock) Release(ctx context.Context, status DeploymentStatus) error { | ||
| return l.wfs.release(ctx, status) | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io/fs" | ||
|
|
||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/locker" | ||
| "github.com/databricks/cli/libs/log" | ||
| "github.com/databricks/databricks-sdk-go" | ||
| ) | ||
|
|
||
| // workspaceFilesystemLock holds the state for the workspace-filesystem lock. | ||
| // Methods are unexported; callers use DeploymentLock.Acquire / DeploymentLock.Release. | ||
| type workspaceFilesystemLock struct { | ||
| client *databricks.WorkspaceClient | ||
| user string | ||
| statePath string | ||
| enabled bool | ||
| force bool | ||
|
|
||
| // reportPermissionError produces the user-facing permission diagnostic | ||
| // when the workspace API returns ErrPermission/ErrNotExist from Lock. | ||
| // Lifted to a callback so this struct does not pin a *bundle.Bundle. | ||
| reportPermissionError func(ctx context.Context, path string) diag.Diagnostics | ||
|
|
||
| locker *locker.Locker | ||
| goal Goal | ||
| } | ||
|
|
||
| func (l *workspaceFilesystemLock) acquire(ctx context.Context) error { | ||
| // Return early if locking is disabled. | ||
| if !l.enabled { | ||
| log.Infof(ctx, "Skipping; locking is disabled") | ||
| return nil | ||
| } | ||
|
|
||
| lk, err := locker.CreateLocker(l.user, l.statePath, l.client) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| l.locker = lk | ||
|
|
||
| log.Infof(ctx, "Acquiring deployment lock (force: %v)", l.force) | ||
| err = lk.Lock(ctx, l.force) | ||
| if err != nil { | ||
| log.Errorf(ctx, "Failed to acquire deployment lock: %v", err) | ||
|
|
||
| // If we get a permission or "doesn't exist" error from the API this | ||
| // indicates we either don't have permissions or the path is invalid. | ||
| if errors.Is(err, fs.ErrPermission) || errors.Is(err, fs.ErrNotExist) { | ||
| return l.reportPermissionError(ctx, l.statePath).Error() | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *workspaceFilesystemLock) release(ctx context.Context, _ DeploymentStatus) error { | ||
| // Return early if locking is disabled. | ||
| if !l.enabled { | ||
| log.Infof(ctx, "Skipping; locking is disabled") | ||
| return nil | ||
| } | ||
|
|
||
| // Return early if the locker is not set. | ||
| // It is likely an error occurred prior to initialization of the locker instance. | ||
| if l.locker == nil { | ||
| log.Warnf(ctx, "Unable to release lock if locker is not configured") | ||
| return nil | ||
| } | ||
|
|
||
| log.Infof(ctx, "Releasing deployment lock") | ||
| if l.goal == GoalDestroy { | ||
| // AllowLockFileNotExist because the destroy phase deletes the remote | ||
| // state directory, which includes the lock file itself. | ||
| return l.locker.Unlock(ctx, locker.AllowLockFileNotExist) | ||
| } | ||
| return l.locker.Unlock(ctx) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,20 @@ import ( | |
| func Bind(ctx context.Context, b *bundle.Bundle, opts *terraform.BindOptions, engine engine.EngineType) { | ||
| log.Info(ctx, "Phase: bind") | ||
|
|
||
| bundle.ApplyContext(ctx, b, lock.Acquire()) | ||
| if logdiag.HasError(ctx) { | ||
| dl := lock.NewDeploymentLock(ctx, b, lock.GoalBind) | ||
| if err := dl.Acquire(ctx); err != nil { | ||
| logdiag.LogError(ctx, err) | ||
| return | ||
| } | ||
|
|
||
| defer func() { | ||
| bundle.ApplyContext(ctx, b, lock.Release(lock.GoalBind)) | ||
| status := lock.DeploymentSuccess | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. status is not used for now - but will be used with deployment metadata service. |
||
| if logdiag.HasError(ctx) { | ||
| status = lock.DeploymentFailure | ||
| } | ||
| if err := dl.Release(ctx, status); err != nil { | ||
| logdiag.LogError(ctx, err) | ||
| } | ||
| }() | ||
|
|
||
| if engine.IsDirect() { | ||
|
|
@@ -119,13 +126,20 @@ func jsonDump(ctx context.Context, v any, field string) string { | |
| func Unbind(ctx context.Context, b *bundle.Bundle, bundleType, tfResourceType, resourceKey string, engine engine.EngineType) { | ||
| log.Info(ctx, "Phase: unbind") | ||
|
|
||
| bundle.ApplyContext(ctx, b, lock.Acquire()) | ||
| if logdiag.HasError(ctx) { | ||
| dl := lock.NewDeploymentLock(ctx, b, lock.GoalUnbind) | ||
| if err := dl.Acquire(ctx); err != nil { | ||
| logdiag.LogError(ctx, err) | ||
| return | ||
| } | ||
|
|
||
| defer func() { | ||
| bundle.ApplyContext(ctx, b, lock.Release(lock.GoalUnbind)) | ||
| status := lock.DeploymentSuccess | ||
| if logdiag.HasError(ctx) { | ||
| status = lock.DeploymentFailure | ||
| } | ||
| if err := dl.Release(ctx, status); err != nil { | ||
| logdiag.LogError(ctx, err) | ||
| } | ||
| }() | ||
|
|
||
| if engine.IsDirect() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.