Skip to content

Commit 42a331c

Browse files
Remove Unused Features Field on StreamOutput (#44667)
* Remove Unused Features Field on StreamOutput * Ever since b15d62c this field and all the methods around it seem completely unused (that commit removed the only use of the getter) and are in fact wasting some allocations => removed it
1 parent 02ff060 commit 42a331c

File tree

9 files changed

+21
-73
lines changed

9 files changed

+21
-73
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.apache.lucene.util.BytesRefBuilder;
3030
import org.elasticsearch.ElasticsearchException;
3131
import org.elasticsearch.Version;
32-
import org.elasticsearch.cluster.ClusterState;
33-
import org.elasticsearch.cluster.metadata.MetaData;
3432
import org.elasticsearch.common.CharArrays;
3533
import org.elasticsearch.common.Nullable;
3634
import org.elasticsearch.common.bytes.BytesArray;
@@ -70,7 +68,6 @@
7068
import java.util.LinkedHashMap;
7169
import java.util.List;
7270
import java.util.Map;
73-
import java.util.Set;
7471
import java.util.concurrent.TimeUnit;
7572
import java.util.function.IntFunction;
7673

@@ -109,7 +106,6 @@ public abstract class StreamOutput extends OutputStream {
109106
}
110107

111108
private Version version = Version.CURRENT;
112-
private Set<String> features = Collections.emptySet();
113109

114110
/**
115111
* The version of the node on the other side of this stream.
@@ -125,27 +121,6 @@ public void setVersion(Version version) {
125121
this.version = version;
126122
}
127123

128-
/**
129-
* Test if the stream has the specified feature. Features are used when serializing {@link ClusterState.Custom} or
130-
* {@link MetaData.Custom}; see also {@link ClusterState.FeatureAware}.
131-
*
132-
* @param feature the feature to test
133-
* @return true if the stream has the specified feature
134-
*/
135-
public boolean hasFeature(final String feature) {
136-
return this.features.contains(feature);
137-
}
138-
139-
/**
140-
* Set the features on the stream. See {@link StreamOutput#hasFeature(String)}.
141-
*
142-
* @param features the features on the stream
143-
*/
144-
public void setFeatures(final Set<String> features) {
145-
assert this.features.isEmpty() : this.features;
146-
this.features = Set.copyOf(features);
147-
}
148-
149124
public long position() throws IOException {
150125
throw new UnsupportedOperationException();
151126
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.elasticsearch.threadpool.ThreadPool;
4141

4242
import java.io.IOException;
43-
import java.util.Set;
4443

4544
final class OutboundHandler {
4645

@@ -95,13 +94,12 @@ void sendRequest(final DiscoveryNode node, final TcpChannel channel, final long
9594
* Sends the response to the given channel. This method should be used to send {@link TransportResponse}
9695
* objects back to the caller.
9796
*
98-
* @see #sendErrorResponse(Version, Set, TcpChannel, long, String, Exception) for sending error responses
97+
* @see #sendErrorResponse(Version, TcpChannel, long, String, Exception) for sending error responses
9998
*/
100-
void sendResponse(final Version nodeVersion, final Set<String> features, final TcpChannel channel,
101-
final long requestId, final String action, final TransportResponse response,
102-
final boolean compress, final boolean isHandshake) throws IOException {
99+
void sendResponse(final Version nodeVersion, final TcpChannel channel, final long requestId, final String action,
100+
final TransportResponse response, final boolean compress, final boolean isHandshake) throws IOException {
103101
Version version = Version.min(this.version, nodeVersion);
104-
OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), features, response, version,
102+
OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), response, version,
105103
requestId, isHandshake, compress);
106104
ActionListener<Void> listener = ActionListener.wrap(() -> messageListener.onResponseSent(requestId, action, response));
107105
sendMessage(channel, message, listener);
@@ -110,12 +108,12 @@ void sendResponse(final Version nodeVersion, final Set<String> features, final T
110108
/**
111109
* Sends back an error response to the caller via the given channel
112110
*/
113-
void sendErrorResponse(final Version nodeVersion, final Set<String> features, final TcpChannel channel, final long requestId,
114-
final String action, final Exception error) throws IOException {
111+
void sendErrorResponse(final Version nodeVersion, final TcpChannel channel, final long requestId, final String action,
112+
final Exception error) throws IOException {
115113
Version version = Version.min(this.version, nodeVersion);
116114
TransportAddress address = new TransportAddress(channel.getLocalAddress());
117115
RemoteTransportException tx = new RemoteTransportException(nodeName, address, action, error);
118-
OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), features, tx, version, requestId,
116+
OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), tx, version, requestId,
119117
false, false);
120118
ActionListener<Void> listener = ActionListener.wrap(() -> messageListener.onResponseSent(requestId, action, error));
121119
sendMessage(channel, message, listener);

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import org.elasticsearch.common.bytes.BytesReference;
2424
import org.elasticsearch.common.bytes.CompositeBytesReference;
2525
import org.elasticsearch.common.io.stream.BytesStreamOutput;
26-
import org.elasticsearch.common.io.stream.StreamOutput;
2726
import org.elasticsearch.common.io.stream.Writeable;
2827
import org.elasticsearch.common.util.concurrent.ThreadContext;
2928

3029
import java.io.IOException;
31-
import java.util.Set;
3230

33-
abstract class OutboundMessage extends NetworkMessage implements Writeable {
31+
abstract class OutboundMessage extends NetworkMessage {
3432

3533
private final Writeable message;
3634

@@ -49,15 +47,14 @@ BytesReference serialize(BytesStreamOutput bytesStream) throws IOException {
4947
try (CompressibleBytesOutputStream stream = new CompressibleBytesOutputStream(bytesStream, TransportStatus.isCompress(status))) {
5048
stream.setVersion(version);
5149
threadContext.writeTo(stream);
52-
writeTo(stream);
5350
reference = writeMessage(stream);
5451
}
5552
bytesStream.seek(0);
5653
TcpHeader.writeHeader(bytesStream, requestId, status, version, reference.length() - TcpHeader.HEADER_SIZE);
5754
return reference;
5855
}
5956

60-
private BytesReference writeMessage(CompressibleBytesOutputStream stream) throws IOException {
57+
protected BytesReference writeMessage(CompressibleBytesOutputStream stream) throws IOException {
6158
final BytesReference zeroCopyBuffer;
6259
if (message instanceof BytesTransportRequest) {
6360
BytesTransportRequest bRequest = (BytesTransportRequest) message;
@@ -96,9 +93,10 @@ static class Request extends OutboundMessage {
9693
}
9794

9895
@Override
99-
public void writeTo(StreamOutput out) throws IOException {
96+
protected BytesReference writeMessage(CompressibleBytesOutputStream out) throws IOException {
10097
out.writeStringArray(features);
10198
out.writeString(action);
99+
return super.writeMessage(out);
102100
}
103101

104102
private static byte setStatus(boolean compress, boolean isHandshake, Writeable message) {
@@ -117,17 +115,8 @@ private static byte setStatus(boolean compress, boolean isHandshake, Writeable m
117115

118116
static class Response extends OutboundMessage {
119117

120-
private final Set<String> features;
121-
122-
Response(ThreadContext threadContext, Set<String> features, Writeable message, Version version, long requestId,
123-
boolean isHandshake, boolean compress) {
118+
Response(ThreadContext threadContext, Writeable message, Version version, long requestId, boolean isHandshake, boolean compress) {
124119
super(threadContext, version, setStatus(compress, isHandshake, message), requestId, message);
125-
this.features = features;
126-
}
127-
128-
@Override
129-
public void writeTo(StreamOutput out) throws IOException {
130-
out.setFeatures(features);
131120
}
132121

133122
private static byte setStatus(boolean compress, boolean isHandshake, Writeable message) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public TcpTransport(Settings settings, Version version, ThreadPool threadPool, P
156156
(node, channel, requestId, v) -> outboundHandler.sendRequest(node, channel, requestId,
157157
TransportHandshaker.HANDSHAKE_ACTION_NAME, new TransportHandshaker.HandshakeRequest(version),
158158
TransportRequestOptions.EMPTY, v, false, true),
159-
(v, features1, channel, response, requestId) -> outboundHandler.sendResponse(v, features1, channel, requestId,
159+
(v, features1, channel, response, requestId) -> outboundHandler.sendResponse(v, channel, requestId,
160160
TransportHandshaker.HANDSHAKE_ACTION_NAME, response, false, true));
161161
InboundMessage.Reader reader = new InboundMessage.Reader(version, namedWriteableRegistry, threadPool.getThreadContext());
162162
this.keepAlive = new TransportKeepAlive(threadPool, this.outboundHandler::sendBytes);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public String getProfileName() {
6161
@Override
6262
public void sendResponse(TransportResponse response) throws IOException {
6363
try {
64-
outboundHandler.sendResponse(version, features, channel, requestId, action, response, compressResponse, false);
64+
outboundHandler.sendResponse(version, channel, requestId, action, response, compressResponse, false);
6565
} finally {
6666
release(false);
6767
}
@@ -70,7 +70,7 @@ public void sendResponse(TransportResponse response) throws IOException {
7070
@Override
7171
public void sendResponse(Exception exception) throws IOException {
7272
try {
73-
outboundHandler.sendErrorResponse(version, features, channel, requestId, action, exception);
73+
outboundHandler.sendErrorResponse(version, channel, requestId, action, exception);
7474
} finally {
7575
release(true);
7676
}

server/src/test/java/org/elasticsearch/cluster/FeatureAwareTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import java.io.IOException;
3232
import java.util.Arrays;
33-
import java.util.Collections;
3433
import java.util.EnumSet;
3534
import java.util.Optional;
3635

@@ -111,19 +110,14 @@ public void testVersion() {
111110
final BytesStreamOutput out = new BytesStreamOutput();
112111
final Version afterVersion = randomVersionBetween(random(), version, Version.CURRENT);
113112
out.setVersion(afterVersion);
114-
if (custom.getRequiredFeature().isPresent()) {
115-
out.setFeatures(Collections.singleton(custom.getRequiredFeature().get()));
116-
}
113+
custom.getRequiredFeature();
117114
assertTrue(FeatureAware.shouldSerialize(out, custom));
118115
}
119116
{
120117
final BytesStreamOutput out = new BytesStreamOutput();
121118
final Version beforeVersion =
122119
randomVersionBetween(random(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(version));
123120
out.setVersion(beforeVersion);
124-
if (custom.getRequiredFeature().isPresent() && randomBoolean()) {
125-
out.setFeatures(Collections.singleton(custom.getRequiredFeature().get()));
126-
}
127121
assertFalse(FeatureAware.shouldSerialize(out, custom));
128122
}
129123
}
@@ -138,7 +132,6 @@ public void testFeature() {
138132
final BytesStreamOutput out = new BytesStreamOutput();
139133
out.setVersion(afterVersion);
140134
assertTrue(custom.getRequiredFeature().isPresent());
141-
out.setFeatures(Collections.singleton(custom.getRequiredFeature().get()));
142135
assertTrue(FeatureAware.shouldSerialize(out, custom));
143136
}
144137
}

server/src/test/java/org/elasticsearch/persistent/PersistentTasksCustomMetaDataTests.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656
import java.util.ArrayList;
5757
import java.util.Arrays;
5858
import java.util.Collections;
59-
import java.util.HashSet;
6059
import java.util.Optional;
61-
import java.util.Set;
6260

6361
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_GATEWAY;
6462
import static org.elasticsearch.cluster.metadata.MetaData.CONTEXT_MODE_SNAPSHOT;
@@ -269,11 +267,6 @@ public void testMinVersionSerialization() throws IOException {
269267
final BytesStreamOutput out = new BytesStreamOutput();
270268

271269
out.setVersion(streamVersion);
272-
Set<String> features = new HashSet<>();
273-
if (randomBoolean()) {
274-
features.add("test");
275-
}
276-
out.setFeatures(features);
277270
tasks.build().writeTo(out);
278271

279272
final StreamInput input = out.bytes().streamInput();

server/src/test/java/org/elasticsearch/transport/InboundMessageTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testReadResponse() throws IOException {
9090
boolean compress = randomBoolean();
9191
threadContext.putHeader("header", "header_value");
9292
Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
93-
OutboundMessage.Response request = new OutboundMessage.Response(threadContext, features, message, version, requestId, isHandshake,
93+
OutboundMessage.Response request = new OutboundMessage.Response(threadContext, message, version, requestId, isHandshake,
9494
compress);
9595
BytesReference reference;
9696
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
@@ -126,7 +126,7 @@ public void testReadErrorResponse() throws IOException {
126126
boolean compress = randomBoolean();
127127
threadContext.putHeader("header", "header_value");
128128
Version version = randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion());
129-
OutboundMessage.Response request = new OutboundMessage.Response(threadContext, features, exception, version, requestId,
129+
OutboundMessage.Response request = new OutboundMessage.Response(threadContext, exception, version, requestId,
130130
isHandshake, compress);
131131
BytesReference reference;
132132
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
@@ -185,7 +185,7 @@ public void testEnsureVersionCompatibility() throws IOException {
185185

186186
public void testThrowOnNotCompressed() throws Exception {
187187
OutboundMessage.Response request = new OutboundMessage.Response(
188-
threadContext, Collections.emptySet(), new Message(randomAlphaOfLength(10)), Version.CURRENT, randomLong(), false, false);
188+
threadContext, new Message(randomAlphaOfLength(10)), Version.CURRENT, randomLong(), false, false);
189189
BytesReference reference;
190190
try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
191191
reference = request.serialize(streamOutput);

server/src/test/java/org/elasticsearch/transport/OutboundHandlerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void onResponseSent(long requestId, String action, TransportResponse resp
192192
responseRef.set(response);
193193
}
194194
});
195-
handler.sendResponse(version, Collections.emptySet(), channel, requestId, action, response, compress, isHandshake);
195+
handler.sendResponse(version, channel, requestId, action, response, compress, isHandshake);
196196

197197
BytesReference reference = channel.getMessageCaptor().get();
198198
ActionListener<Void> sendListener = channel.getListenerCaptor().get();
@@ -256,7 +256,7 @@ public void onResponseSent(long requestId, String action, Exception error) {
256256
responseRef.set(error);
257257
}
258258
});
259-
handler.sendErrorResponse(version, Collections.emptySet(), channel, requestId, action, error);
259+
handler.sendErrorResponse(version, channel, requestId, action, error);
260260

261261
BytesReference reference = channel.getMessageCaptor().get();
262262
ActionListener<Void> sendListener = channel.getListenerCaptor().get();

0 commit comments

Comments
 (0)