Skip to content

feat(streaming): adapt Event Hubs cache memory usage - #10316

Open
ReubenBond wants to merge 28 commits into
dotnet:mainfrom
ReubenBond:reubenbond-adaptive-eventhub-cache
Open

ReubenBond wants to merge 28 commits into
dotnet:mainfrom
ReubenBond:reubenbond-adaptive-eventhub-cache

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Event Hubs currently incurs large first-touch cache allocations per partition and retains peak cache capacity indefinitely. With systems containing hundreds or thousands of sparse partitions, this can consume substantial memory even when little stream data is active.

This change makes Event Hubs cache storage adaptive and bounded. Cached-message metadata and payload buffers now start small and grow as needed, idle payload retention is capped, and active cache memory is controlled across all partitions for a provider. Memory-pressure recovery reclaims one oldest payload buffer at a time while preserving slow-consumer protection and custom buffer-pool compatibility.

When memory pressure stops reads, the pulling agent publishes the settled delivery boundary and retries capacity. Event Hubs reclaims data through that boundary synchronously, allowing caught-up subscriptions to remain registered while ingestion resumes. Pending registrations, handshakes, and in-flight deliveries retain their required data. Recovery progresses even when paused ingestion keeps the cached enqueue-time span constant.

The default provider-wide active-cache high watermark is 512 MiB and the idle payload-pool limit is 64 MiB. Batches already in flight may temporarily exceed the watermark while their events remain available for ordered delivery.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings July 31, 2026 23:19

Copilot AI left a comment

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.

Pull request overview

This PR reduces and bounds Event Hubs streaming-cache memory usage by introducing provider-wide memory accounting, adaptive buffer sizing/pooling, and adaptive cached-message metadata blocks, with new configuration options and targeted tests validating the new behavior.

Changes:

  • Introduces provider-wide active-cache memory accounting for Event Hubs and an adaptive payload buffer pool with bounded idle retention.
  • Makes cached-message metadata blocks adaptive (start small, grow up to a max, shrink on reset) and adds active-metadata size tracking.
  • Adds EventHubStreamCacheMemoryOptions (+ validation and builder extensions) and new tests covering adaptive behavior and defaults.
Show a summary per file
File Description
test/Orleans.Streaming.Tests/OrleansRuntime/Streams/PooledQueueCacheTests.cs Adds a regression test ensuring adaptive metadata blocks are released when the cache drains.
test/Orleans.Streaming.Tests/OrleansRuntime/Streams/FixedSizeBufferTests.cs Adds a test verifying the new object-pool retention cap behavior.
test/Orleans.Streaming.Tests/OrleansRuntime/Streams/CachedMessageBlockTests.cs Adds a test validating CachedMessageBlock geometric growth and shrink-on-reset behavior.
test/Extensions/Orleans.Streaming.EventHubs.Tests/CacheMemoryTests.cs Adds integration-style tests validating default bounds, size classes, aggregation across partitions, and custom pool compatibility.
src/Orleans.Streaming/Common/PooledCache/PooledQueueCache.cs Adds adaptive cached-message block support, tracks active metadata allocation, and implements IDisposable.
src/Orleans.Streaming/Common/PooledCache/ObjectPool.cs Adds maxRetainedObjects to bound retained pooled objects and adjusts monitoring counters accordingly.
src/Orleans.Streaming/Common/PooledCache/ChronologicalEvictionStrategy.cs Adds explicit buffer release APIs and IDisposable support for releasing in-use buffers.
src/Orleans.Streaming/Common/PooledCache/CachedMessagePool.cs Updates block allocation/reuse to support adaptive sizing and bounded retention; reports allocation deltas.
src/Orleans.Streaming/Common/PooledCache/CachedMessageBlock.cs Implements adaptive growth/shrink behavior and exposes allocated-size accounting.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubStreamOptions.cs Introduces EventHubStreamCacheMemoryOptions and validator with bounded defaults.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubStreamBuilder.cs Adds ConfigureCacheMemory and registers options+validator for logging/validation.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubQueueCacheFactory.cs Uses a shared memory controller and adaptive buffer pool; adds a memory-pressure eviction strategy.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubQueueCache.cs Integrates metadata accounting and memory-pressure behavior; adapts message-block sizing and buffer sizing.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubCacheMemoryController.cs Adds new controller and adaptive buffer pool implementation with size classes and bounded idle memory.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubAdapterReceiver.cs Ensures purging is triggered for Event Hubs caches experiencing memory pressure.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventHubAdapterFactory.cs Wires named EventHubStreamCacheMemoryOptions through to cache factory creation.
src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventDataGeneratorStreamConfigurator.cs Registers cache-memory options and validator for the generator provider configuration surface.
src/Azure/Orleans.Streaming.EventHubs/Providers/EventDataGeneratorStreamProvider/EventDataGeneratorAdapterFactory.cs Wires cache-memory options through for the generator adapter factory.
src/api/Orleans.Streaming/Orleans.Streaming.cs Updates public API surface for adaptive blocks, bounded pools, and new IDisposable members.
src/api/Azure/Orleans.Streaming.EventHubs/Orleans.Streaming.EventHubs.cs Updates public API surface for Event Hubs cache-memory options and new constructor overloads.

Copilot's findings

  • Files reviewed: 20/20 changed files
  • Comments generated: 2

@ReubenBond
ReubenBond force-pushed the reubenbond-adaptive-eventhub-cache branch 3 times, most recently from 8515add to f50ca83 Compare August 10, 2026 23:34
@ReubenBond
ReubenBond force-pushed the reubenbond-adaptive-eventhub-cache branch 2 times, most recently from d457abb to f2226e2 Compare August 11, 2026 21:43
Copilot AI review requested due to automatic review settings August 11, 2026 21:43

Copilot AI left a comment

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.

Review details

Suppressed comments (1)

src/Azure/Orleans.Streaming.EventHubs/Providers/Streams/EventHub/EventDataGeneratorStreamConfigurator.cs:47

  • The service-registration fluent chain has irregular spacing/indentation (notably the large gap before .ConfigureNamedOptionForLogging) which makes this block hard to read and can generate formatting noise in future diffs. Reformat the chain so each call is on its own consistently-indented line.
            this.ConfigureDelegate(services => services            .ConfigureNamedOptionForLogging<EventHubOptions>(name)
            .ConfigureNamedOptionForLogging<EventHubReceiverOptions>(name)
            .ConfigureNamedOptionForLogging<EventHubStreamCachePressureOptions>(name)
            .ConfigureNamedOptionForLogging<EventHubStreamCacheMemoryOptions>(name)
            .AddTransient<IConfigurationValidator>(sp => new EventHubStreamCacheMemoryOptionsValidator(sp.GetOptionsByName<EventHubStreamCacheMemoryOptions>(name), name))
  • Files reviewed: 20/20 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@ReubenBond
ReubenBond marked this pull request as ready for review August 21, 2026 22:27
Copilot AI review requested due to automatic review settings August 21, 2026 23:02
@ReubenBond
ReubenBond force-pushed the reubenbond-adaptive-eventhub-cache branch from f2226e2 to f4d8085 Compare August 21, 2026 23:02

Copilot AI left a comment

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.

Review details

  • Files reviewed: 20/20 changed files
  • Comments generated: 1
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings August 22, 2026 09:20

Copilot AI left a comment

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.

Review details

Suppressed comments (3)

Previously missed (3) — in code that hasn't changed since the last review.

src/Orleans.Streaming/Common/PooledCache/ObjectPool.cs:15

  • DefaultPoolCapacity is now unused. Keeping unused constants can trigger analyzers (and fail the build when code style is enforced). Remove it (or reintroduce a capacity check if it’s still intended).
        private const int DefaultPoolCapacity = 1 << 10; // 1k

test/Orleans.Streaming.Tests/OrleansRuntime/Streams/FixedSizeBufferTests.cs:3

  • using Orleans.Streams; is unused in this test file and can trigger IDE0005 (unused using) under enforced code style. Remove the unused using directive.
using Orleans.Streams;

test/Extensions/Orleans.Streaming.EventHubs.Tests/CacheMemoryTests.cs:91

  • This test is only marked with [Fact], so it won’t be included when running BVT via Category=BVT filters (other tests in this file explicitly add TestCategory("BVT")). Add the missing category so it runs in the same CI selection.
    [Fact]
  • Files reviewed: 20/20 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings August 24, 2026 04:19
@ReubenBond
ReubenBond force-pushed the reubenbond-adaptive-eventhub-cache branch from 8a0ebb9 to 631c7ff Compare August 24, 2026 04:19
@ReubenBond
ReubenBond force-pushed the reubenbond-adaptive-eventhub-cache branch from 631c7ff to 9ab466e Compare August 24, 2026 04:23

Copilot AI left a comment

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.

Review details

  • Files reviewed: 22/22 changed files
  • Comments generated: 1
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings August 24, 2026 04:23

Copilot AI left a comment

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.

Review details

  • Files reviewed: 22/22 changed files
  • Comments generated: 2
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings August 24, 2026 04:40

Copilot AI left a comment

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.

🔵 Needs a closer look

Three unresolved moderate findings affect pressure recovery correctness and performance.

2 open issues
2 resolved since last review

Review effort: Lite

ReubenBond and others added 28 commits October 19, 2026 14:15
Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 7046c02b-4a9b-4623-9183-99472770f6fd
Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 7046c02b-4a9b-4623-9183-99472770f6fd

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues

Review effort: Lite

This branch has not been deployed

No deployments
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