Add coalescing fetcher to deduplicate concurrent HTTP fetch requests #69
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.
Summary
This PR adds request deduplication for the HTTP fetcher path in bb-remote-asset. When multiple concurrent requests arrive for the same uncached URI, they now share a single upstream fetch instead of triggering duplicate HTTP requests.
Problem
When using the HTTP fetcher, concurrent
FetchBlob/FetchDirectoryrequests for the same uncached URI would all independently miss the cache and trigger separate upstream fetches. This is wasteful and problematic when:The
remote_executionfetcher path doesn't have this issue because it goes through bb-remote-execution's scheduler, which already deduplicates by action digest. The flow comparison:Remote Execution Fetcher (already deduplicated):
HTTP Fetcher (before this PR):
HTTP Fetcher (after this PR):
Solution
Added a
CoalescingFetcherdecorator that deduplicates in-flight requests using a pure stdlib approach (sync.Mutex+map+ channels). This follows the same pattern used in bb-remote-execution'sInMemoryBuildQueuescheduler, which uses aninFlightDeduplicationMapto coalesce concurrent requests for the same action.Fetcher chain order:
Key Changes
pkg/fetch/coalescing_fetcher.go- New coalescing fetcher withFetchBlob/FetchDirectorydeduplicationpkg/configuration/new_fetcher.go- WireCoalescingFetcherfor HTTP fetcher path onlyTest Coverage
TestCoalescingFetcherBlobDeduplication- Verifies 10 concurrent requests result in 1 fetchTestCoalescingFetcherDirectoryDeduplication- Same for directory fetchesTestCoalescingFetcherDifferentURIsNotCoalesced- Different URIs don't coalesceTestCoalescingFetcherVolatileQualifiersIgnored- Auth headers don't affect coalescingTestCoalescingFetcherDifferentChecksumsNotCoalesced- Different checksum.sri values don't coalesce (consistent with caching)TestCoalescingFetcherDifferentResourceTypeNotCoalesced- Different resource_type values don't coalesceTestCoalescingFetcherContextCancellation- Waiters can bail out if their context is cancelledTestCoalescingFetcherErrorPropagation- Errors propagate to all waitersCoalescing Key Design
The coalescing key uses the same qualifier filtering as caching via
RemoveVolatileQualifiers(). This ensures consistent behavior between what gets cached and what gets coalesced.Excluded qualifiers (volatile):
http_header_url:*- Per-URL HTTP headers (typically auth tokens)bazel.auth_headers- Legacy Bazel authentication headersIncluded qualifiers (stable):
checksum.sri- Treated as stable, consistent with caching behaviorresource_type- Affects how content is processedRationale:
Using the same rules as caching keeps the behavior predictable and avoids subtle inconsistencies between cache keys and coalescing keys.
Notes