Repository navigation
Accept messages that exactly fill the send buffer #346
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
vickenty
merged 4 commits into
DataDog:master
from
mar-kolya:mar-kolya/fix-exact-fit-buffer-overflow
Nov 17, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c23e25e
Accept messages that exactly fill the send buffer
mar-kolya 5e8e788
Test exact-fit writes on StatsDProcessor directly
mar-kolya 4c35740
Extract the UTF-8 encoder and test it directly
mar-kolya 4c7094f
Move the + 4 comment next to the + 4
mar-kolya 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
104 changes: 104 additions & 0 deletions
104
src/test/java/com/timgroup/statsd/StatsDProcessorTest.java
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,104 @@ | ||
| package com.timgroup.statsd; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
||
| public class StatsDProcessorTest { | ||
|
|
||
| private static final int MAX_PACKET_SIZE = 64; | ||
|
|
||
| private final RecordingErrorHandler errors = new RecordingErrorHandler(); | ||
| private StatsDProcessor processor; | ||
|
|
||
| @After | ||
| public void stop() throws InterruptedException { | ||
| if (processor != null) { | ||
| processor.shutdown(false); | ||
| } | ||
| } | ||
|
|
||
| @Test(timeout = 5000L) | ||
| public void writes_message_that_exactly_fills_the_packet() throws Exception { | ||
| String packet = packet("", MAX_PACKET_SIZE); | ||
|
|
||
| assertEquals(Arrays.asList(packet), process(new ChunkedMessage(packet), 1)); | ||
| } | ||
|
|
||
| @Test(timeout = 5000L) | ||
| public void writes_multibyte_message_that_exactly_fills_the_packet() throws Exception { | ||
| // One character short of the limit, but 'é' takes two bytes in UTF-8. | ||
| String packet = packet("é", MAX_PACKET_SIZE - 1); | ||
|
|
||
| assertEquals(Arrays.asList(packet), process(new ChunkedMessage(packet), 1)); | ||
| } | ||
|
|
||
| @Test(timeout = 5000L) | ||
| public void writes_every_chunk_when_one_exactly_fills_the_packet() throws Exception { | ||
| String first = packet("", MAX_PACKET_SIZE); | ||
| String second = packet("", 10); | ||
|
|
||
| assertEquals(Arrays.asList(first, second), process(new ChunkedMessage(first, second), 2)); | ||
| } | ||
|
|
||
| /** A packet of {@code length} chars that starts with {@code head} and ends with a newline. */ | ||
| private static String packet(String head, int length) { | ||
| char[] filler = new char[length - head.length() - 1]; | ||
| Arrays.fill(filler, 'x'); | ||
| return head + new String(filler) + "\n"; | ||
| } | ||
|
|
||
| private List<String> process(Message message, int expectedPackets) throws Exception { | ||
| processor = | ||
| new StatsDNonBlockingProcessor( | ||
| 16, errors, MAX_PACKET_SIZE, 8, 1, 0, 1, Executors.defaultThreadFactory()); | ||
| processor.startWorkers("StatsDProcessorTest-"); | ||
| assertTrue(processor.send(message)); | ||
|
|
||
| List<String> packets = new ArrayList<>(); | ||
| while (packets.size() < expectedPackets) { | ||
| List<Exception> exceptions = errors.getExceptions(); | ||
| if (!exceptions.isEmpty()) { | ||
| throw new AssertionError("processor reported errors: " + exceptions); | ||
| } | ||
| ByteBuffer buffer = processor.getOutboundQueue().poll(10, TimeUnit.MILLISECONDS); | ||
| // An exact fit makes the processor flush its current buffer first, even when it is | ||
| // still empty. | ||
| if (buffer == null || buffer.position() == 0) { | ||
| continue; | ||
| } | ||
| buffer.flip(); | ||
| packets.add(StandardCharsets.UTF_8.decode(buffer).toString()); | ||
| } | ||
| return packets; | ||
| } | ||
|
|
||
| /** Writes one prepared chunk per call, like a multi-valued message split across packets. */ | ||
| private static final class ChunkedMessage extends Message { | ||
| private final String[] chunks; | ||
| private int next; | ||
|
|
||
| ChunkedMessage(String... chunks) { | ||
| super(Message.Type.DISTRIBUTION, TagsCardinality.DEFAULT); | ||
| this.chunks = chunks; | ||
| } | ||
|
|
||
| @Override | ||
| boolean writeTo(StringBuilder builder, int capacity) { | ||
| builder.append(chunks[next++]); | ||
| return next < chunks.length; | ||
| } | ||
|
|
||
| @Override | ||
| public void aggregate(Message message) {} | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
These tests need to touch every aspect of the processor just to test one condition. Would it be possible to test just the fixed method directly? We can isolate it in a new class (for example
StatsDProcessor.Encoder) to make testing it easier.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.
Done in 4c35740. The UTF-8 loop now lives in
StatsDProcessor.Encoder, unchanged apart from the fix, andStatsDProcessorEncoderTestcalls it directly with a plainByteBuffer. It covers exact fits made of 1-, 2-, 3- and 4-byte chars, which all fail with>=, plus three overflow cases.StatsDProcessorTestis gone.🤖 Generated by Claude Opus 5.5 with Claude Code