fix(reconciler): eliminate lock contention in TF and DCL schema updates using atomic.Pointer #15075
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.40) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 314 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.344
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #15075"
general
weight: 0.35
sdowell
Description
Follow-up to / complementary to PR #15034.
Context
See https://github.com/GoogleCloudPlatform/k8s-config-connector/pull/15034#discussion_r4485289558
Problem
In
pkg/controller/tf/controller.goandpkg/controller/dcl/controller.go,DoReconcile/Reconcilehistorically heldr.schemaRefMu.RLock()for the entire duration of resource reconciliation. Because GCP operations can take up to several minutes, and Go'ssync.RWMutexgrants writer priority (blocking new readers as soon as a writer requests the lock), wheneverUpdateSchema(crd)was called:1.
UpdateSchemacalledr.schemaRefMu.Lock(), which blocked waiting for all in-flight reconciles for that resource kind to complete.2. In the meantime, any new reconciliation attempts for that same resource kind were blocked behind
UpdateSchemawaiting forRLock().3. If an in-flight reconcile blocked on a dependency or long-running operation, it caused cascading lock contention across all controller worker routines for that kind.
Solution
Replace
schemaRefMu sync.RWMutexwith lock-freeatomic.Pointer[k8s.SchemaReference]copy-on-write snapshotting:1. Lock-Free Read Snapshots:
- At the beginning of
DoReconcile/Reconcile, the reconciler atomically loadsschemaRef := r.schemaRef.Load().- The reconcile execution uses this immutable snapshot throughout its lifecycle without holding any lock during remote GCP API calls or waiting on network I/O.
2. Atomic Schema Updates:
-
UpdateSchema(crd)validates that the GVK matches and builds a new*k8s.SchemaReferenceviak8s.NewUpdatedSchemaReference.- It replaces the pointer via
r.schemaRef.Store(newRef), which executes in ~1ns with zero lock contention.- Any reconciles in-flight continue using their loaded snapshot safely without torn reads or race conditions.
3. Unit Tests (AAA Pattern):
- Added unit tests in
pkg/k8s/schemareference_test.gocoveringNewSchemaReference,NewUpdatedSchemaReference(immutability and GVK validation), andUpdateSchema.- Added unit tests in
pkg/controller/tf/controller_test.goandpkg/controller/dcl/controller_test.goverifyingUpdateSchemasuccess and mismatched GVK rejection.Does this PR add something which needs to be 'release noted'?
``
release-note
``Fixed potential lock contention in Terraform and DCL controllers during dynamic CRD schema updates by adopting lock-free atomic schema snapshotting.