Skip to content

Commit 8b0dce0

Browse files
authored
Improve MockTcpTransport memory usage (#35402) (#35430)
The MockTcpTransport is not friendly in regards to memory usage. It must allocate multiple byte arrays for every message. This improves the memory situation by failing fast if the message is improperly formatted. Additionally, it uses reusable big arrays for at least half of the allocated byte arrays.
1 parent 4232e77 commit 8b0dce0

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

server/src/main/java/org/elasticsearch/transport/TcpTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public abstract class TcpTransport extends AbstractLifecycleComponent implements
198198
protected final ScheduledPing scheduledPing;
199199
private final TimeValue pingSchedule;
200200
protected final ThreadPool threadPool;
201-
private final BigArrays bigArrays;
201+
protected final BigArrays bigArrays;
202202
protected final NetworkService networkService;
203203
protected final Set<ProfileSettings> profileSettings;
204204

test/framework/src/main/java/org/elasticsearch/transport/MockTcpTransport.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
*/
1919
package org.elasticsearch.transport;
2020

21-
import org.elasticsearch.core.internal.io.IOUtils;
2221
import org.elasticsearch.Version;
2322
import org.elasticsearch.action.ActionListener;
2423
import org.elasticsearch.cluster.node.DiscoveryNode;
2524
import org.elasticsearch.common.bytes.BytesReference;
2625
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2726
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
2827
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
28+
import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput;
2929
import org.elasticsearch.common.io.stream.StreamInput;
3030
import org.elasticsearch.common.network.NetworkService;
3131
import org.elasticsearch.common.settings.Settings;
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.common.util.CancellableThreads;
3636
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
3737
import org.elasticsearch.common.util.concurrent.EsExecutors;
38+
import org.elasticsearch.core.internal.io.IOUtils;
3839
import org.elasticsearch.indices.breaker.CircuitBreakerService;
3940
import org.elasticsearch.mocksocket.MockServerSocket;
4041
import org.elasticsearch.mocksocket.MockSocket;
@@ -153,19 +154,20 @@ private void readMessage(MockChannel mockChannel, StreamInput input) throws IOEx
153154
if (msgSize == -1) {
154155
socket.getOutputStream().flush();
155156
} else {
156-
BytesStreamOutput output = new BytesStreamOutput();
157-
final byte[] buffer = new byte[msgSize];
158-
input.readFully(buffer);
159-
output.write(minimalHeader);
160-
output.writeInt(msgSize);
161-
output.write(buffer);
162-
final BytesReference bytes = output.bytes();
163-
if (TcpTransport.validateMessageHeader(bytes)) {
164-
InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
165-
messageReceived(bytes.slice(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE, msgSize),
166-
mockChannel, mockChannel.profile, remoteAddress, msgSize);
167-
} else {
168-
// ping message - we just drop all stuff
157+
try (BytesStreamOutput output = new ReleasableBytesStreamOutput(msgSize, bigArrays)) {
158+
final byte[] buffer = new byte[msgSize];
159+
input.readFully(buffer);
160+
output.write(minimalHeader);
161+
output.writeInt(msgSize);
162+
output.write(buffer);
163+
final BytesReference bytes = output.bytes();
164+
if (TcpTransport.validateMessageHeader(bytes)) {
165+
InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
166+
messageReceived(bytes.slice(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE, msgSize),
167+
mockChannel, mockChannel.profile, remoteAddress, msgSize);
168+
} else {
169+
// ping message - we just drop all stuff
170+
}
169171
}
170172
}
171173
}

0 commit comments

Comments
 (0)