Skip to content
This repository was archived by the owner on Jul 22, 2026. It is now read-only.

Avoid per-request HashSet allocation in RelativeLoadBalancerStrategy - #1170

Merged
zhihaodev merged 1 commit into
linkedin:masterfrom
idarmans:idarmans/relative-lb-allocation
May 12, 2026
Merged

Avoid per-request HashSet allocation in RelativeLoadBalancerStrategy#1170
zhihaodev merged 1 commit into
linkedin:masterfrom
idarmans:idarmans/relative-lb-allocation

Conversation

@idarmans

Copy link
Copy Markdown
Contributor

JFR identified the HashSet copy constructor call in RelativeLoadBalancerStrategy.getTrackerClient to be a hotspot since this is executed once per request. The copied set is only consumed by the executor task on cluster changes, not by the steady-state path, such that the construction is wasted most of the time.

Since the map that backs the TrackerClient values are not mutated and have no duplicates, defer the construction of the set to when it's actually necessary.

Bonus: fix flaky test in StateUpdaterTest

Comment on lines +102 to +104
_stateUpdater.updateState(
_enableRelativeStrategyDeferredAllocation ? trackerClients.values() : new HashSet<>(trackerClients.values()),
partitionId, clusterGenerationId, shouldForceUpdate);

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.

After @TylerHorth 's recent changes (#1151), it seems that the map of clients is also mutated from an async thread. In general too, may not be a good idea to assume that the map of tracker clients supplied here is immutable.

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.

Actually it appears that this map will be immutable. Is it possible to use ImmutableMap for making this clear?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the potential client cache, yes it's immutable as well, and it does a value swap when the lb state is updated, such that it's safe for us to do a .values here. Unfortunately we don't have access to any lib to indicate immutability in the interface (guava is not pulled into the classpath for example)

@zhihaodev
zhihaodev merged commit cef7cd3 into linkedin:master May 12, 2026
6 of 10 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants