[Java] Remove Batch.EMPTY Shared Objects#2495
Merged
Merged
Conversation
Since the introduction of `batch.getHeader()` by #2481, we must create a new instance per request. However, to reduce allocations, the underlying buffer is a shared empty object. This PR fixes a problem where concurrent requests with `reply.len == 0` were overriding each other's headers.
sentientwaffle
previously approved these changes
Nov 21, 2024
sentientwaffle
left a comment
Member
There was a problem hiding this comment.
LGTM -- thanks for tracking this down!
The original bug can be reproduced with the integration test:
@Test
public void testRace() throws Throwable {
try (final var client2 = new Client(clusterId, new String[] {server.getAddress()})) {
for (int i = 0; i < 1000; i++) {
var r1 = client.lookupAccountsAsync(new IdBatch(UInt128.id()));
var r2 = client2.lookupAccountsAsync(new IdBatch(UInt128.id()));
var t1 = r1.get().getHeader().getTimestamp();
var t2 = r2.get().getHeader().getTimestamp();
if (t1 == t2) {
throw new Exception("BUG: " + String.format("timestamp: %d = %d", t1, t2));
}
}
}
}...Though that is niche enough that it probably isn't worth adding to the integration test suite.
| */ | ||
| @Test | ||
| public void testEmptyReply() throws Throwable { | ||
| final int TASKS_COUNT = 1_000; |
Member
There was a problem hiding this comment.
If we are going to add this to the integration test suite, let's change the task count to 4 or so... it didn't take that many repetitions to hit the bug, and I don't want to make the test suite slower 😄
Contributor
Author
There was a problem hiding this comment.
Cool, it's about 300ms now!
batiati
enabled auto-merge
November 21, 2024 15:58
sentientwaffle
approved these changes
Nov 21, 2024
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Nov 21, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since the introduction of
batch.getHeader()by #2481, we must create a new instance per request instead of sharing the sameBatch.EMPTYinstance.However, to reduce allocations, the underlying buffer is still a shared empty object.
This PR fixes a problem where concurrent requests with
reply.len == 0were overriding each other's headers.