Repository navigation
Record unbind history outside StateDB #7818
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * Record deployment history for `bundle deployment unbind` when deployment history is enabled. ([#7818](https://github.com/databricks/cli/pull/7818)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
|
|
||
| >>> [CLI] bundle deployment unbind spec1 | ||
| Warn: Failed to record deployment history for unbind: failed to create deployment version: operations.resource_key must have a known resource type prefix (e.g. 'jobs.', 'pipelines.'): feature_specs.spec1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,6 @@ resources: | |
| jobs: | ||
| foo: | ||
| name: foo | ||
| permissions: | ||
| - group_name: users | ||
| level: CAN_VIEW | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| bundle: | ||
| name: dms-unbind-upload-failure-$UNIQUE_NAME | ||
|
|
||
| resources: | ||
| jobs: | ||
| upload_failed: | ||
| name: upload_failed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Cloud = false | ||
| EnvMatrix.DMS = ["true"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
|
|
||
| === A remote state upload failure fails the unbind version | ||
|
|
||
| >>> fault.py POST /api/2.0/workspace-files/import-file/**resources.json 400 0 1 | ||
|
|
||
| >>> musterr [CLI] bundle deployment unbind upload_failed | ||
| Error: Fault injected by test. (400 INJECTED) | ||
|
|
||
| Endpoint: POST [DATABRICKS_URL]/api/2.0/workspace-files/import-file/Workspace%2FUsers%2F[USERNAME]%2F.bundle%2Fdms-unbind-upload-failure-[UNIQUE_NAME]%2Fdefault%2Fstate%2Fresources.json?overwrite=true | ||
| HTTP Status: 400 Bad Request | ||
| API error_code: INJECTED | ||
| API message: Fault injected by test. | ||
|
|
||
|
|
||
| >>> print_requests.py --dms //complete --oneline | ||
| {"method": "POST", "path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/4/complete", "body": {"completion_reason": "VERSION_COMPLETE_FAILURE"}} | ||
|
|
||
| === Remote state still contains the binding whose upload failed | ||
|
|
||
| >>> [CLI] bundle summary -o json | ||
| { | ||
| "upload_failed": "[JOB_ID]" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| envsubst < databricks.yml.tmpl > databricks.yml | ||
|
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. [nice to have] This script mirrors |
||
| job_id=$($CLI jobs create --json '{"name": "unbind-target-'$UNIQUE_NAME'"}' | jq -r '.job_id') | ||
| add_repl "$job_id" JOB_ID | ||
| $CLI bundle deployment bind upload_failed "$job_id" --auto-approve &> LOG.setup | ||
| rm -f "$OUT_REQUESTS" | ||
|
|
||
| title "A remote state upload failure fails the unbind version" | ||
| trace fault.py "POST /api/2.0/workspace-files/import-file/**resources.json" 400 0 1 | ||
| trace musterr $CLI bundle deployment unbind upload_failed | ||
| trace print_requests.py --dms //complete --oneline | ||
|
|
||
| rm -rf .databricks | ||
| title "Remote state still contains the binding whose upload failed" | ||
| trace $CLI bundle summary -o json | jq '.resources.jobs | map_values(.id // null)' | ||
| rm -f "$OUT_REQUESTS" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Cloud = false # Inject a remote state-upload failure. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "os" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/bundle/config" | ||
|
|
@@ -42,6 +43,12 @@ type DMSBindMetadata struct { | |
| ResourceState dstate.ResourceEntry | ||
| } | ||
|
|
||
| // DMSUnbindMetadata holds removed keys to publish after the unbind commits. | ||
| type DMSUnbindMetadata struct { | ||
| Serial int | ||
| ResourceKeys []string | ||
| } | ||
|
|
||
| // BindResult contains the result of a bind operation including any detected changes. | ||
| type BindResult struct { | ||
| // DMSMetadata is nil when deployment history is disabled. | ||
|
|
@@ -269,18 +276,40 @@ func (result *BindResult) Cancel() { | |
|
|
||
| // Unbind removes a resource from direct engine state without deleting | ||
| // the workspace resource. Also removes associated permissions/grants entries. | ||
| func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey string) error { | ||
| // It returns nil DMS metadata when deployment history is disabled. | ||
| func (b *DeploymentBundle) Unbind(ctx context.Context, configRoot *config.Root, statePath, resourceKey string) (*DMSUnbindMetadata, error) { | ||
| if err := b.StateDB.Open(ctx, statePath, dstate.ReadingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{}); err != nil { | ||
| return err | ||
| return nil, err | ||
| } | ||
| var configured *bool | ||
|
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. [nice to have] This block reads
Contributor
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. 🤖 Should fix +1, and the two copies have already drifted. Since this PR was cut, #7817 changed I checked by cherry-picking this commit onto the current #7817 branch and applying |
||
| if configRoot.Experimental != nil { | ||
| configured = configRoot.Experimental.DeploymentHistory | ||
| } | ||
| if b.StateDB.RecordsHistory() || bundleenv.RecordsDeploymentHistory(ctx, configured) { | ||
| if b.StateDB.Data.Features == nil { | ||
| b.StateDB.Data.Features = make(map[string]struct{}) | ||
| } | ||
| b.StateDB.Data.Features[dstate.FeatureDeploymentHistoryShadow] = struct{}{} | ||
| } | ||
| if err := b.StateDB.UpgradeToWrite(); err != nil { | ||
| return err | ||
| return nil, err | ||
| } | ||
| var metadata *DMSUnbindMetadata | ||
| if b.StateDB.RecordsHistory() { | ||
| metadata = &DMSUnbindMetadata{Serial: b.StateDB.Data.Serial} | ||
| // Finalize commits the removals below as one existing WAL transaction. | ||
|
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. [nit] The comment doesn't say why the serial only advances when |
||
| if b.StateDB.Data.State != nil { | ||
| metadata.Serial++ | ||
| } | ||
| if _, exists := b.StateDB.GetResourceEntry(resourceKey); exists { | ||
| metadata.ResourceKeys = append(metadata.ResourceKeys, resourceKey) | ||
| } | ||
| } | ||
|
|
||
| // Delete the main resource | ||
| err := b.StateDB.DeleteState(ctx, resourceKey, false) | ||
| if err != nil { | ||
| return err | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Infof(ctx, "Unbound %s", resourceKey) | ||
|
|
@@ -294,12 +323,18 @@ func (b *DeploymentBundle) Unbind(ctx context.Context, statePath, resourceKey st | |
| if key == permissionsKey || key == grantsKey || strings.HasPrefix(key, resourceKey+".") { | ||
| err = b.StateDB.DeleteState(ctx, key, false) | ||
| if err != nil { | ||
| return err | ||
| return nil, err | ||
| } | ||
| if metadata != nil { | ||
| metadata.ResourceKeys = append(metadata.ResourceKeys, key) | ||
| } | ||
| log.Infof(ctx, "Unbound %s", key) | ||
| } | ||
| } | ||
|
|
||
| if metadata != nil { | ||
| slices.Sort(metadata.ResourceKeys) | ||
| } | ||
| _, err = b.StateDB.Finalize(ctx) | ||
| return err | ||
| return metadata, err | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[AI Generated] This unbind runs with
DATABRICKS_BUNDLE_DEPLOYMENT_HISTORY=true, so it passes even ifUnbindignored the loaded shadow marker, and no other test covers that. Clear the variable here, like the rebind at line 47?