Prevent extra peformance hit by fillInStackTrace() when create a new annotated connect exception.

Motivation:

To make it easier to debug connect exceptions we create new exceptions which also contain the remote address. For this we basically created a new instance and call setStackTrace(...). When doing this we pay an extra penality because it calls fillInStackTrace() when calling the super constructor.

Modifications:

Create special sub-classes of Exceptions that override the fillInStackTrace() method and so eliminate the overhead.

Result:

Less overhead when "annotate" connect exceptions.
This commit is contained in:
Norman Maurer 2016-08-18 20:47:33 +02:00
parent 2d111e0980
commit 8ce7e73e78

View File

@ -939,17 +939,13 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
*/ */
protected final Throwable annotateConnectException(Throwable cause, SocketAddress remoteAddress) { protected final Throwable annotateConnectException(Throwable cause, SocketAddress remoteAddress) {
if (cause instanceof ConnectException) { if (cause instanceof ConnectException) {
Throwable newT = new ConnectException(cause.getMessage() + ": " + remoteAddress); return new AnnotatedConnectException((ConnectException) cause, remoteAddress);
newT.setStackTrace(cause.getStackTrace()); }
cause = newT; if (cause instanceof NoRouteToHostException) {
} else if (cause instanceof NoRouteToHostException) { return new AnnotatedNoRouteToHostException((NoRouteToHostException) cause, remoteAddress);
Throwable newT = new NoRouteToHostException(cause.getMessage() + ": " + remoteAddress); }
newT.setStackTrace(cause.getStackTrace()); if (cause instanceof SocketException) {
cause = newT; return new AnnotatedSocketException((SocketException) cause, remoteAddress);
} else if (cause instanceof SocketException) {
Throwable newT = new SocketException(cause.getMessage() + ": " + remoteAddress);
newT.setStackTrace(cause.getStackTrace());
cause = newT;
} }
return cause; return cause;
@ -1062,4 +1058,49 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return super.trySuccess(); return super.trySuccess();
} }
} }
private static final class AnnotatedConnectException extends ConnectException {
private static final long serialVersionUID = 3901958112696433556L;
AnnotatedConnectException(ConnectException exception, SocketAddress remoteAddress) {
super(exception.getMessage() + ": " + remoteAddress);
setStackTrace(exception.getStackTrace());
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
private static final class AnnotatedNoRouteToHostException extends NoRouteToHostException {
private static final long serialVersionUID = -6801433937592080623L;
AnnotatedNoRouteToHostException(NoRouteToHostException exception, SocketAddress remoteAddress) {
super(exception.getMessage() + ": " + remoteAddress);
setStackTrace(exception.getStackTrace());
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
private static final class AnnotatedSocketException extends SocketException {
private static final long serialVersionUID = 3896743275010454039L;
AnnotatedSocketException(SocketException exception, SocketAddress remoteAddress) {
super(exception.getMessage() + ": " + remoteAddress);
setStackTrace(exception.getStackTrace());
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
} }