Serialize Concurrent Load Balancer Resource Operations Using a Mutex#142
Open
alakae wants to merge 7 commits into
Open
Serialize Concurrent Load Balancer Resource Operations Using a Mutex#142alakae wants to merge 7 commits into
alakae wants to merge 7 commits into
Conversation
The cloudscale API fails on many concurrent create/delete operations against the same parent resource. Lock on the parent UUID for pools (keyed on LB), pool members, and listeners (both keyed on pool) to prevent API errors when Terraform parallelizes these operations.
mweibel
reviewed
Jun 26, 2026
|
|
||
| poolID := d.Get("pool_uuid").(string) | ||
| globalMu.Lock(lbPoolLockKey(poolID)) | ||
| defer globalMu.Unlock(lbPoolLockKey(poolID)) |
Contributor
There was a problem hiding this comment.
nit: in the other cases we lock/unlock at the top of the function, here right before the .Create call. I think it would be better to pick one style.
Introduce withLock, a decorator that wraps Create/Update/Delete with a mutex key derived from ResourceData. Replaces the inline globalMu approach and closes the gap in the Update path, which could not derive the lock key from rId alone. Applied to LB pool, pool member, listener, and volume snapshot; the health monitor stays unlocked since only one per pool is allowed.
Make getDeleteOperation generic and introduce idFunc so deleteFunc receives a typed rId instead of *schema.ResourceData, matching the contract of updateFunc. Folds keyFunc into both generic operations, removing the withLock double-wrapping from Update/Delete registrations.
Replace globalMu.Lock with LockContext, which polls TryLock in a select loop and returns an error when the caller's context is cancelled, preventing indefinite blocking past a Terraform timeout. Switch all update/delete CRUD hooks to context-aware variants and propagate ctx into all API calls. Thread ctx through StateRefreshFunc closures, polling helpers (waitForStatus, waitForDeleted), and status-wait functions so cancellation is respected throughout.
Switch all resources from the deprecated Create:/Read: fields to CreateContext:/ReadContext:, updating standalone create functions to the diag.Diagnostics signature. Migrate the server resource's hand-rolled update path from Update: to UpdateContext: and thread ctx into its API calls. Thread ctx through datasource listXxx functions via dataSourceResourceRead/getFetchFunc.
Replace three hand-rolled mutexKeyFunc implementations with a generic factory in resources.go. Returns ok=false and logs a warning when the Required UUID attribute is absent rather than acquiring a lock on an empty key.
Introduce getCreateOperation, a builder wrapping a CreateFunc with optional mutex locking (nil = no lock), matching the signature and nil guard of the other two builders. withLock is removed. Extend the pattern to all resources: each resource now declares a resourceXxxCreate var in its var block alongside Read/Update/Delete, and the implementation function is renamed to createXxx to match updateXxx / deleteXxx.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The cloudscale API fails on many concurrent create/delete operations against the same parent resource. Lock on the parent UUID for pools (keyed on LB), pool members, and listeners (both keyed on pool) to prevent API errors when Terraform parallelizes these operations.