Repository navigation
fix: honor Options.Parallelism when fetching built-in policy definitions - #253
Yingjie Chen (Y1ngJ1eChen) wants to merge 3 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #253 +/- ##
==========================================
+ Coverage 55.87% 56.02% +0.15%
==========================================
Files 56 58 +2
Lines 5264 5580 +316
==========================================
+ Hits 2941 3126 +185
- Misses 2003 2134 +131
Partials 320 320 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I ran a preliminary local A/B benchmark comparing ALZ provider 0.22.1 with a locally built provider based on d5242f754628e1f1c39be194f4b79c612a82ad2b, using alzlib PR #253 head 8c42f3f (binary SHA-256: 61F21CB31DD359887022AA7A796A4E9FED8FF01A989CB189B687FCC29FD7D4C2). Across three sequential pairs with randomized starting order, the candidate was faster in each pair: Median total plan time: 206 s vs 270 s (~24% lower) |
…-definition-fetch
Summary
Options.Parallelismis documented as "the number of parallel requests to make to Azure APIs when getting policy definitions and policy set definitions", but it was only validated inInit()and never used. Built-in policy definitions, policy set definitions and the definitions they reference were fetched one request at a time, andHierarchy.FromArchitecturefetched one management group at a time. Building thealzarchitecture without a cache makes ~700 ARM requests, soalz_architecturereads took minutes.This PR fetches them concurrently, bounded by
Options.Parallelism(default 10, which terraform-provider-alz uses), without changing which requests are made or what is loaded.Fixes Azure/Azure-Landing-Zones#4310
Changes
alzlib
getBuiltInPolicies,getBuiltInPolicySetsandensureReferencedPolicyDefinitionsrun witherrgroup+SetLimit(Parallelism).AllowOverwrite=false).az.mu; the lock only guards map access through the existing accessors.fetchMuserialises concurrentGetDefinitionsFromAzurecallers sharing oneAlzLib(e.g. severalalz_architecturedata sources in the provider). Previously they raced to fetch and add the same definitions.**Clientinitialisation, which would race under concurrency.az.policyDefinitions[name].Versions()without the lock and panicked on a missing entry; it now uses a locked accessor.Parallelism < 1is treated as 1 internally;Init()still rejects 0.Options.Parallelismdoc: same-name requests are serial;1restores serial fetching.golang.org/x/sync v0.18.0becomes a direct dependency. It is already selected by MVS, and thegodirective is unchanged.deployment
FromArchitectureprefetches the definitions of the whole architecture in oneGetDefinitionsFromAzurecall, in a deterministic order, so requests are spread across the configured parallelism instead of one management group at a time. Assignments that cannot be resolved are skipped by the prefetch and still reported byaddManagementGroupwith the existing error. The per-management-group call remains as a no-op fallback.tests
internal/testutil/fakearm: an in-process fake of the ARM built-in policy API (latency, failure injection, request and peak-concurrency counters).BenchmarkGetDefinitionsFromAzureSimulatedLatency.Behaviour notes
FromArchitecturefetch errors are now wrapped asHierarchy.FromArchitecture: getting policy definitions from Azure for architecture ...(inner error unchanged). terraform-provider-alz does not parse these strings.FromArchitecturenow happens before any management group is added. Previously the groups processed before the failing one remained in the hierarchy; callers discard the hierarchy on error.FromArchitecturefetches is now deterministic. Previously child management groups were visited in map order, so the number of requests (and, for a definition needed both as latest and as a constraint, which version was loaded first) could vary between runs.Parallelismabove 10 is not recommended: ARM throttling and per-request latency increase.Validation
go test ./...(incl. read-only Azure integration tests),go vetgolangci-lintv2.11.4 with Go 1.25.2 (--new-from-rev)-count=20go test -race -count=3on.,./deployment,./cache(Windows, MinGW-w64)make testraceis left to CIfetchMu/ remove name groupingTestGetDefinitionsFromAzureConcurrentCallers/...SharedDefinitionsFetchedOncemain(cbf351d)main(incl. a panic on the missing-version path); all 18 guards pass on bothLive Azure, read-only (uncached,
alzarchitecture,mainvs. this branch):mainBenchmarkFromArchitectureWithClient, benchstatalz_architectureReadDataSource interraform plan(provider v0.22.0, platform/alz 2026.10.0)alz_architecturedata sources in one planpolicy definition db048e65-…@1.*.* … does not exist in the library(concurrent-caller race)--from-cache)alzlibtool generate architectureoutput is byte-identical tomain: 334/334 files forintegrationtest/testdata/alzlib-2025-09-0and 320/320 forplatform/alz@2026.10.0, each with and without--for-alz-bicep.Follow-ups (not in this PR)
parallelismprovider attribute (it is hard-coded to 10) for throttled or constrained networks.