-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Deduplicate allocation stats calls #123246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elasticsearchmachine
merged 4 commits into
elastic:main
from
DaveCTurner:2025/02/24/deduplicate-allocation-stats
Feb 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 123246 | ||
| summary: Deduplicate allocation stats calls | ||
| area: Allocation | ||
| type: bug | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,12 @@ | |
| import java.util.EnumSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CyclicBarrier; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.hamcrest.Matchers.anEmptyMap; | ||
| import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
|
|
@@ -112,4 +116,49 @@ public void testReturnsOnlyRequestedStats() throws Exception { | |
| assertNull(response.getDiskThresholdSettings()); | ||
| } | ||
| } | ||
|
|
||
| public void testDeduplicatesStatsComputations() throws InterruptedException { | ||
| final var requestCounter = new AtomicInteger(); | ||
| final var isExecuting = new AtomicBoolean(); | ||
| when(allocationStatsService.stats()).thenAnswer(invocation -> { | ||
| try { | ||
| assertTrue(isExecuting.compareAndSet(false, true)); | ||
| return Map.of(Integer.toString(requestCounter.incrementAndGet()), NodeAllocationStatsTests.randomNodeAllocationStats()); | ||
| } finally { | ||
| Thread.yield(); | ||
| assertTrue(isExecuting.compareAndSet(true, false)); | ||
| } | ||
| }); | ||
|
|
||
| final var threads = new Thread[between(1, 5)]; | ||
| final var startBarrier = new CyclicBarrier(threads.length); | ||
| for (int i = 0; i < threads.length; i++) { | ||
| threads[i] = new Thread(() -> { | ||
| safeAwait(startBarrier); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: could use org.elasticsearch.test.ESTestCase#startInParallel here ? :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cute TIL |
||
|
|
||
| final var minRequestIndex = requestCounter.get(); | ||
|
|
||
| final TransportGetAllocationStatsAction.Response response = safeAwait( | ||
| l -> action.masterOperation( | ||
| mock(Task.class), | ||
| new TransportGetAllocationStatsAction.Request( | ||
| TEST_REQUEST_TIMEOUT, | ||
| TaskId.EMPTY_TASK_ID, | ||
| EnumSet.of(Metric.ALLOCATIONS) | ||
| ), | ||
| ClusterState.EMPTY_STATE, | ||
| l | ||
| ) | ||
| ); | ||
|
|
||
| final var requestIndex = Integer.valueOf(response.getNodeAllocationStats().keySet().iterator().next()); | ||
| assertThat(requestIndex, greaterThanOrEqualTo(minRequestIndex)); // did not get a stale result | ||
| }, "thread-" + i); | ||
| threads[i].start(); | ||
| } | ||
|
|
||
| for (final var thread : threads) { | ||
| thread.join(); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wouldn't want to fork anymore with the deduplicator would you? Only fork in case you actually do the computation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically yes that's right of course. I'm always in two minds about adding more non-forking actions: they increase the risk of serious pain in future (for nontechnical reasons) at the cost of a little more latency right now. I'll let you have this one without prejudice tho 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd think forking lowers the risk of pain the future, but under load already are at the other end of this.
Forking is far more expensive than just the fork in the real world. You also need to account for the fact that you'll allocate a buffer off of the channel's thread and release that buffer which comes with contention very quickly unfortunately. Just to illustrate this a little :)