Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/java/org/apache/cassandra/db/ReadResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.apache.cassandra.net.MessagingService;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.ExpMovingAverage;
import org.apache.cassandra.utils.MovingAverage;

import static org.apache.cassandra.db.RepairedDataInfo.NO_OP_REPAIRED_DATA_INFO;

Expand Down Expand Up @@ -224,6 +226,9 @@ public boolean isDigestResponse()
// built on the owning node responding to a query
private static class LocalDataResponse extends DataResponse
{
// Moving average of response sizes, used to set initial size of output buffer.
private static final MovingAverage estimatedResponseBytes = ExpMovingAverage.decayBy1000();

private LocalDataResponse(UnfilteredPartitionIterator iter, ReadCommand command, RepairedDataInfo rdi)
{
super(build(iter, command.columnFilter()),
Expand All @@ -239,10 +244,17 @@ private LocalDataResponse(UnfilteredPartitionIterator iter, ColumnFilter selecti

private static ByteBuffer build(UnfilteredPartitionIterator iter, ColumnFilter selection)
{
try (DataOutputBuffer buffer = new DataOutputBuffer())
// Size output buffer to 10% above the moving average to absorb minor variance and limit rebuffering.
double estimatedResponseSize = estimatedResponseBytes.get();
double bufferSizeEstimate = Double.isNaN(estimatedResponseSize) ? 128 : estimatedResponseSize;
int initialBufferSize = (int) (bufferSizeEstimate * 1.1);

try (DataOutputBuffer buffer = new DataOutputBuffer(initialBufferSize))
{
UnfilteredPartitionIterators.serializerForIntraNode().serialize(iter, selection, buffer, MessagingService.current_version);
return buffer.buffer();
estimatedResponseBytes.update(buffer.position());

return buffer.buffer(false);
}
catch (IOException e)
{
Expand Down