Skip to content

Serialize Concurrent Load Balancer Resource Operations Using a Mutex#142

Open
alakae wants to merge 7 commits into
masterfrom
alain/lb-operations-mux
Open

Serialize Concurrent Load Balancer Resource Operations Using a Mutex#142
alakae wants to merge 7 commits into
masterfrom
alain/lb-operations-mux

Conversation

@alakae

@alakae alakae commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

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.

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.

poolID := d.Get("pool_uuid").(string)
globalMu.Lock(lbPoolLockKey(poolID))
defer globalMu.Unlock(lbPoolLockKey(poolID))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

alakae added 6 commits July 1, 2026 16:18
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants