Skip to content

Remove unused fields in ActionTransportException #80692

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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,56 @@

package org.elasticsearch.transport;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.transport.TransportAddress;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
* An action invocation failure.
*
*
*/
public class ActionTransportException extends TransportException {

private final TransportAddress address;

private final String action;

public ActionTransportException(StreamInput in) throws IOException {
super(in);
address = in.readOptionalWriteable(TransportAddress::new);
action = in.readOptionalString();
if (in.getVersion().before(Version.V_8_1_0)) {
in.readOptionalWriteable(TransportAddress::new);
in.readOptionalString();
}
}

public ActionTransportException(String name, TransportAddress address, String action, Throwable cause) {
super(buildMessage(name, address, action, null), cause);
this.address = address;
this.action = action;
this(name, address, action, null, cause);
}

public ActionTransportException(String name, TransportAddress address, String action, String msg, Throwable cause) {
this(name, address == null ? null : address.address(), action, msg, cause);
}

public ActionTransportException(String name, InetSocketAddress address, String action, String msg, Throwable cause) {
super(buildMessage(name, address, action, msg), cause);
this.address = address;
this.action = action;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalWriteable(address);
out.writeOptionalString(action);
}

/**
* The target address to invoke the action on.
*/
public TransportAddress address() {
return address;
}

/**
* The action to invoke.
*/
public String action() {
return action;
if (out.getVersion().before(Version.V_8_1_0)) {
out.writeBoolean(false); // optional transport address
out.writeBoolean(false); // optional action
}
}

private static String buildMessage(String name, TransportAddress address, String action, String msg) {
private static String buildMessage(String name, InetSocketAddress address, String action, String msg) {
StringBuilder sb = new StringBuilder();
if (name != null) {
sb.append('[').append(name).append(']');
}
if (address != null) {
sb.append('[').append(address).append(']');
sb.append('[').append(NetworkAddress.format(address)).append(']');
}
if (action != null) {
sb.append('[').append(action).append(']');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.elasticsearch.common.network.CloseableChannel;
import org.elasticsearch.common.recycler.Recycler;
import org.elasticsearch.common.transport.NetworkExceptionHelper;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -143,8 +142,7 @@ void sendErrorResponse(
final Exception error
) throws IOException {
Version version = Version.min(this.version, nodeVersion);
TransportAddress address = new TransportAddress(channel.getLocalAddress());
RemoteTransportException tx = new RemoteTransportException(nodeName, address, action, error);
RemoteTransportException tx = new RemoteTransportException(nodeName, channel.getLocalAddress(), action, error);
OutboundMessage.Response message = new OutboundMessage.Response(threadPool.getThreadContext(), tx, version, requestId, false, null);
ActionListener<Void> listener = ActionListener.wrap(() -> messageListener.onResponseSent(requestId, action, error));
sendMessage(channel, message, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import org.elasticsearch.common.transport.TransportAddress;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
* A remote exception for an action. A wrapper exception around the actual remote cause and does not fill the
* stack trace.
*
*
*/
public class RemoteTransportException extends ActionTransportException implements ElasticsearchWrapperException {

Expand All @@ -30,6 +29,10 @@ public RemoteTransportException(String name, TransportAddress address, String ac
super(name, address, action, cause);
}

public RemoteTransportException(String name, InetSocketAddress address, String action, Throwable cause) {
super(name, address, action, null, cause);
}

public RemoteTransportException(StreamInput in) throws IOException {
super(in);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,7 @@ public void testInvalidIndexTemplateException() throws IOException {
public void testActionTransportException() throws IOException {
TransportAddress transportAddress = buildNewFakeTransportAddress();
ActionTransportException ex = serialize(new ActionTransportException("name?", transportAddress, "ACTION BABY!", "message?", null));
assertEquals("ACTION BABY!", ex.action());
assertEquals(transportAddress, ex.address());
assertEquals("[name?][" + transportAddress.toString() + "][ACTION BABY!] message?", ex.getMessage());
assertEquals("[name?][" + transportAddress + "][ACTION BABY!] message?", ex.getMessage());
}

public void testSearchContextMissingException() throws IOException {
Expand Down Expand Up @@ -411,15 +409,13 @@ public void testConnectTransportException() throws IOException {
TransportAddress transportAddress = buildNewFakeTransportAddress();
DiscoveryNode node = new DiscoveryNode("thenode", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
ConnectTransportException ex = serialize(new ConnectTransportException(node, "msg", "action", null));
assertEquals("[][" + transportAddress.toString() + "][action] msg", ex.getMessage());
assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
assertEquals(node, ex.node());
assertEquals("action", ex.action());
assertNull(ex.getCause());

ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
assertEquals(node, ex.node());
assertEquals("action", ex.action());
assertTrue(ex.getCause() instanceof NullPointerException);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.common.bytes.ReleasableBytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.network.NetworkAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.util.PageCacheRecycler;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand All @@ -46,6 +47,8 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;

public class OutboundHandlerTests extends ESTestCase {
Expand Down Expand Up @@ -300,8 +303,10 @@ public void onResponseSent(long requestId, String action, Exception error) {
RemoteTransportException remoteException = tuple.v2().streamInput().readException();
assertThat(remoteException.getCause(), instanceOf(ElasticsearchException.class));
assertEquals(remoteException.getCause().getMessage(), "boom");
assertEquals(action, remoteException.action());
assertEquals(channel.getLocalAddress(), remoteException.address().address());
assertThat(
remoteException.getMessage(),
allOf(containsString('[' + NetworkAddress.format(channel.getLocalAddress()) + ']'), containsString('[' + action + ']'))
);

assertEquals("header_value", header.getHeaders().v1().get("header"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,9 +2753,10 @@ public void handleException(TransportException exp) {
TransportException exception = receivedException.get();
assertNotNull(exception);
BytesStreamOutput streamOutput = new BytesStreamOutput();
streamOutput.setVersion(version0);
exception.writeTo(streamOutput);
String failedMessage = "Unexpected read bytes size. The transport exception that was received=" + exception;
// 49 bytes are the non-exception message bytes that have been received. It should include the initial
// 53 bytes are the non-exception message bytes that have been received. It should include the initial
// handshake message and the header, version, etc bytes in the exception message.
assertEquals(failedMessage, 53 + streamOutput.bytes().length(), stats.getRxSize().getBytes());
assertEquals(111, stats.getTxSize().getBytes());
Expand Down