Skip to content

Clamp PosixEndpointImpl adaptive read target to max read chunk size - #42234

Closed
SaidKec wants to merge 4 commits into
grpc:masterfrom
SaidKec:buffer-limit-fix
Closed

SaidKec wants to merge 4 commits into
grpc:masterfrom
SaidKec:buffer-limit-fix

Conversation

@SaidKec

@SaidKec SaidKec commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Solution for #42194

In our production environment, applying this patch locally significantly reduced memory usage in services with many long-lived active gRPC connections.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Apr 24, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: SaidKec / name: SaidKec (e23e75b)

@SaidKec

SaidKec commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

/release-note none

@SaidKec

SaidKec commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

@pawbhard pls check this PR, i have no rights to change labels to pass Mergeable

@markdroth markdroth assigned murgatroid99 and unassigned pawbhard May 5, 2026
@SaidKec

SaidKec commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99 gentle ping

@murgatroid99 murgatroid99 added kokoro:run release notes: no Indicates if PR should not be in release notes labels May 22, 2026
@SaidKec

SaidKec commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99 i fixed import that caused tests failure

@murgatroid99

Copy link
Copy Markdown
Member

There are some formatting errors, see this log for details. Please either run tools/distrib/clang_format_code.sh or correct the formatting manually.

@SaidKec

SaidKec commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

used tools/distrib/clang_format_code.sh

@SaidKec

SaidKec commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99
I was able to run the failing test locally 50 times, but only with --incompatible_enable_cc_toolchain_resolution because my local machine has Xcode 26.5 and Bazel 8.0.1 otherwise crashes before executing the test in apple_support/local_config_apple_cc:

InvalidDottedVersionException: got 'None'
at XcodeLocalEnvProvider.rewriteLocalEnv

Command used:

bazel test
--incompatible_enable_cc_toolchain_resolution
--config=python
--config=python_single_threaded_unary_stream
//src/python/grpcio_tests/tests_aio/unit:call_test
--test_filter=TestUnaryStreamCall.test_late_cancel_unary_stream
--runs_per_test=50

Result:
PASSED 50/50 runs.

This suggests the Kokoro failure may be flaky and just rerun is needed

@SaidKec

SaidKec commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99 thx for approve, do i need to do something for merge?

@Vignesh2208

Copy link
Copy Markdown
Collaborator

I don't think we want to do this change. It limits the total number of bytes read per Endpoint read to be bounded by the max chunk size which can have a severe performance impact if the max chunk size is not tuned correctly and further there's no easy way to define a default for all workloads. Currently we maintain target_ as a moving average of bytes read during each previous TCP Endpoint read on the connection. So its not expected to grow unbounded. Eventually the plan is to use min_progress_size to decide how much to allocate and not rely on the target_ estimates.

I think it might be better to tie the slice sizes: https://github.com/grpc/grpc/blob/master/src/core/lib/event_engine/posix_engine/posix_endpoint.cc#L539 to the min and max chunk size respectively to control how much bytes are read per socket read rather than per endpoint read. We can change the default min and max chunk sizes to 8KB and 64KB respectively: https://github.com/grpc/grpc/blob/master/src/core/lib/event_engine/posix_engine/tcp_socket_utils.h#L48

@SaidKec

SaidKec commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@Vignesh2208 thanks, that makes sense. I agree that using tcp_max_read_chunk_size as a cap for target_length_ may be too broad, since tcp_min/max_read_chunk_size is probably better kept focused on slice / chunk sizing.

I think the issue here is slightly different though. The memory growth we are seeing comes from the total read buffer size allocated by MaybeMakeReadSlices(): it can set allocate_length to target_length_, and target_length_ can grow after large reads. Limiting individual slice sizes does not cap the total amount allocated for incoming_buffer_; it only changes how that buffer is split into slices.

Also, I don’t think target_length_ behaves like a regular moving average. The EMA path is used only when bytes_read_this_round_ <= 0.8 * target_length_. Otherwise, the estimator jumps to max(2 * target_length_, bytes_read_this_round_), so it behaves more like a fast-growth / slow-decay estimate.

For example, after 99 Endpoint reads of 100KB and then one Endpoint read of 10MB, a simple average would be around 201KB, and a 1% EMA update from ~138KB would move to ~239KB. But the current logic sets target_length_ directly to 10MB. If the workload then goes back to 100KB reads, target_length_ decays by only 1% per read, so after another 99 reads it would still be around 3.8MB.

What do you think about adding a separate opt-in setting for this instead, e.g. tcp_max_read_buffer_size?

It would be unset by default, so existing users keep the same behavior. When explicitly set, it would cap the adaptive read target / preallocated read buffer size, similar to what this PR currently does, but without changing the meaning of tcp_max_read_chunk_size.

This would give memory-sensitive deployments a simple way to avoid large high-watermark read buffers after occasional big reads, without changing defaults for everyone else. In our production workload, this kind of cap gives a noticeable reduction in resource usage.

@SaidKec

SaidKec commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99 for me it looks like a flaky tests problem again, but maybe i'm missing smth, can you please recheck/rerun?

@SaidKec

SaidKec commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

@murgatroid99 @Vignesh2208 gentle ping

grohman-hub added a commit to grohman-hub/grpc that referenced this pull request Jul 27, 2026
* upstream/master:
  [PSM Interop] : add cleanup for the orphaned resources left for SPIFFE cluster. (grpc#43038)
  [PH2][Trivial] Build (grpc#43069)
  [PH2][Bug][Client] Bug fix
  [Security] Plumb SSL key exchange group to auth context. (grpc#42917)
  [CI] Update debian-based docker images to use cmake 3.25. (grpc#42795)
  [xDS e2e tests] clean up API for verifying server status notification changes (grpc#43052)
  Clamp PosixEndpointImpl adaptive read target to max read chunk size (grpc#42234)
  [tls] Move to grpc_core::PemKeyCertPair and grpc_core::KeyCertPairsOrSelector (grpc#42992)
  [build] Add grpc-proto submodule (grpc#43035)
  fix detecting the number of online CPU (grpc#41585)
  [core][promise-based-filter] Fix for metadata nullptr crash on repoll (grpc#43037)
  skip tls1.3 and <1.1.1 in telemetry test (grpc#43042)
grohman-hub pushed a commit to grohman-hub/grpc that referenced this pull request Jul 28, 2026
…rpc#42234)

Solution for grpc#42194

In our production environment, applying this patch locally significantly reduced memory usage in services with many long-lived active gRPC connections.

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes grpc#42234

COPYBARA_INTEGRATE_REVIEW=grpc#42234 from SaidKec:buffer-limit-fix fc924e0
PiperOrigin-RevId: 953004629
grohman-hub added a commit to grohman-hub/grpc that referenced this pull request Jul 28, 2026
…thon_apic_clients

* upstream/master:
  [PSM Interop] : add cleanup for the orphaned resources left for SPIFFE cluster. (grpc#43038)
  [PH2][Trivial] Build (grpc#43069)
  [PH2][Bug][Client] Bug fix
  [Security] Plumb SSL key exchange group to auth context. (grpc#42917)
  [CI] Update debian-based docker images to use cmake 3.25. (grpc#42795)
  [xDS e2e tests] clean up API for verifying server status notification changes (grpc#43052)
  Clamp PosixEndpointImpl adaptive read target to max read chunk size (grpc#42234)
  [tls] Move to grpc_core::PemKeyCertPair and grpc_core::KeyCertPairsOrSelector (grpc#42992)
asheshvidyut pushed a commit to PeterZhizhin/grpc that referenced this pull request Nov 12, 2026
…rpc#42234)

Solution for grpc#42194

In our production environment, applying this patch locally significantly reduced memory usage in services with many long-lived active gRPC connections.

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes grpc#42234

COPYBARA_INTEGRATE_REVIEW=grpc#42234 from SaidKec:buffer-limit-fix fc924e0
PiperOrigin-RevId: 953004629
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants