← Back to Pull requests

fix(reconciler): eliminate lock contention in TF and DCL schema updates using atomic.Pointer #15075

Open
unknown opened this pull request with 314 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 06, 2026 00:29 UTC
Author

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.go and pkg/controller/dcl/controller.go, DoReconcile / Reconcile historically held r.schemaRefMu.RLock() for the entire duration of resource reconciliation. Because GCP operations can take up to several minutes, and Go's sync.RWMutex grants writer priority (blocking new readers as soon as a writer requests the lock), whenever UpdateSchema(crd) was called:
1. UpdateSchema called r.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 UpdateSchema waiting for RLock().
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.RWMutex with lock-free atomic.Pointer[k8s.SchemaReference] copy-on-write snapshotting:
1. Lock-Free Read Snapshots:
- At the beginning of DoReconcile / Reconcile, the reconciler atomically loads schemaRef := 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.SchemaReference via k8s.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.go covering NewSchemaReference, NewUpdatedSchemaReference (immutability and GVK validation), and UpdateSchema.
- Added unit tests in pkg/controller/tf/controller_test.go and pkg/controller/dcl/controller_test.go verifying UpdateSchema success 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.
``

Diff: 314 lines (+235 / -79) across 3 files Reviewer: sdowell Review duration: 35945m
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
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.3% (MEDIUM) · Higher = larger blast radius
40.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 43.0% · Lower = rushed or shallow review
43.0%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 23% · Safe to merge
23%