Skip to content

Commit

Permalink
Merge pull request #406 from gregschohn/DelintingRuntimeExceptions
Browse files Browse the repository at this point in the history
Delinting runtime exceptions
  • Loading branch information
gregschohn authored Nov 13, 2023
2 parents a50dc63 + 8f268f2 commit d7c3cdf
Show file tree
Hide file tree
Showing 43 changed files with 184 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
working-directory: TrafficCapture

- name: Run Tests with Coverage
run: ./gradlew allTests jacocoTestReport --info
run: ./gradlew test :trafficReplayer:longTest jacocoTestReport --info
working-directory: TrafficCapture

- name: Upload to Codecov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.protobuf.Timestamp;
import io.netty.buffer.Unpooled;
import lombok.AllArgsConstructor;
import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -202,6 +203,11 @@ public void testEmptyPacketIsHandledForSmallCodedOutputStream()
Assertions.assertEquals(0, reconstitutedTrafficStreamPart2.getSubStream(0).getWrite().getData().size());
}

private static class TestException extends RuntimeException {
public TestException(String message) {
super(message);}
}

@Test
public void testThatReadCanBeDeserialized() throws IOException, ExecutionException, InterruptedException {
final var referenceTimestamp = Instant.now(Clock.systemUTC());
Expand All @@ -218,8 +224,8 @@ public void testThatReadCanBeDeserialized() throws IOException, ExecutionExcepti
serializer.addReadEvent(referenceTimestamp, bb);
bb.clear();
serializer.addReadEvent(referenceTimestamp, bb);
serializer.addExceptionCaughtEvent(referenceTimestamp, new RuntimeException(FAKE_EXCEPTION_DATA));
serializer.addExceptionCaughtEvent(referenceTimestamp, new RuntimeException(""));
serializer.addExceptionCaughtEvent(referenceTimestamp, new TestException(FAKE_EXCEPTION_DATA));
serializer.addExceptionCaughtEvent(referenceTimestamp, new TestException(""));
serializer.addEndOfFirstLineIndicator(17);
serializer.addEndOfHeadersIndicator(72);
serializer.commitEndOfHttpMessageIndicator(referenceTimestamp);
Expand Down Expand Up @@ -322,7 +328,7 @@ public CodedOutputStreamHolder createStream() {
@Override
protected CompletableFuture<Object> kickoffCloseStream(CodedOutputStreamHolder outputStreamHolder, int index) {
if (!(outputStreamHolder instanceof CodedOutputStreamAndByteBufferWrapper)) {
throw new RuntimeException("Unknown outputStreamHolder sent back to StreamManager: " +
throw new IllegalStateException("Unknown outputStreamHolder sent back to StreamManager: " +
outputStreamHolder);
}
var osh = (CodedOutputStreamAndByteBufferWrapper) outputStreamHolder;
Expand All @@ -340,7 +346,7 @@ protected CompletableFuture<Object> kickoffCloseStream(CodedOutputStreamHolder o
log.trace("Adding " + StandardCharsets.UTF_8.decode(bb.duplicate()));
outputBuffers.add(bb);
} catch (IOException e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
}).thenApply(x->null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Stream<TrafficStream> getRecordedTrafficStreamsStream() {
try {
return TrafficStream.parseFrom(rts.data);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
throw new IllegalStateException(e);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.stream.Collectors;

public class TrafficStreamUtils {

private TrafficStreamUtils() {}

public static Instant instantFromProtoTimestamp(Timestamp timestampProto) {
return Instant.ofEpochSecond(timestampProto.getSeconds(), timestampProto.getNanos());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.ReferenceCountUtil;
import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.migrations.trafficcapture.IChannelConnectionCaptureSerializer;

Expand Down Expand Up @@ -33,7 +34,7 @@ protected void channelFinishedReadingAnHttpMessage(ChannelHandlerContext ctx, Ob
try {
super.channelFinishedReadingAnHttpMessage(ctx, msg, httpRequest);
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import lombok.Getter;
import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.migrations.trafficcapture.IChannelConnectionCaptureSerializer;
import org.opensearch.migrations.coreutils.MetricsLogger;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
try {
super.channelUnregistered(ctx);
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
}
});
Expand All @@ -123,7 +124,7 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
try {
super.channelUnregistered(ctx);
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
super.handlerRemoved(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -43,22 +44,21 @@ public CodedOutputStreamAndByteBufferWrapper createStream() {
return new CodedOutputStreamAndByteBufferWrapper(1024*1024);
}

@SneakyThrows
@Override
public CompletableFuture<Object>
kickoffCloseStream(CodedOutputStreamHolder outputStreamHolder, int index) {
if (!(outputStreamHolder instanceof CodedOutputStreamAndByteBufferWrapper)) {
throw new RuntimeException("Unknown outputStreamHolder sent back to StreamManager: " +
throw new IllegalStateException("Unknown outputStreamHolder sent back to StreamManager: " +
outputStreamHolder);
}
var osh = (CodedOutputStreamAndByteBufferWrapper) outputStreamHolder;
CodedOutputStream cos = osh.getOutputStream();
try {
cos.flush();
byteBufferAtomicReference.set(osh.getByteBuffer().flip().asReadOnlyBuffer());
log.error("byteBufferAtomicReference.get="+byteBufferAtomicReference.get());
} catch (IOException e) {
throw new RuntimeException();
}

cos.flush();
byteBufferAtomicReference.set(osh.getByteBuffer().flip().asReadOnlyBuffer());
log.error("byteBufferAtomicReference.get="+byteBufferAtomicReference.get());

return CompletableFuture.completedFuture(flushCount.incrementAndGet());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensearch.migrations.testutils;

import lombok.Lombok;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void interceptTestMethod(InvocationInterceptor.Invocation<Void> invocatio
ExtensionContext extensionContext) throws Throwable {

var selfInstance =
invocationContext.getTarget().orElseThrow(() -> new RuntimeException("Target instance not found"));
invocationContext.getTarget().orElseThrow(() -> new IllegalStateException("Target instance not found"));
wrapWithLeakChecks(extensionContext,
()-> {
Method m = invocationContext.getExecutable();
Expand All @@ -66,7 +67,7 @@ public void interceptTestTemplateMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
var selfInstance =
invocationContext.getTarget().orElseThrow(() -> new RuntimeException("Target instance not found"));
invocationContext.getTarget().orElseThrow(() -> new IllegalStateException("Target instance not found"));
wrapWithLeakChecks(extensionContext,
()->{
{
Expand All @@ -84,7 +85,7 @@ private static Void wrapProceed(Invocation<Void> invocation) throws Exception {
} catch (Exception e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
throw Lombok.sneakyThrow(t);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.opensearch.migrations.testutils;


import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
Expand All @@ -9,14 +11,19 @@
* Helper class to keep retrying ports against a Consumer until the
* Consumer doesn't throw an exception.
*/
@Slf4j
public class PortFinder {

private PortFinder() {}

private static final int MAX_PORT_TRIES = 100;
private static final Random random = new Random();

public static class ExceededMaxPortAssigmentAttemptException extends Exception {}
public static class ExceededMaxPortAssigmentAttemptException extends Exception {
public ExceededMaxPortAssigmentAttemptException(Throwable cause) {
super(cause);
}
}

public static int retryWithNewPortUntilNoThrow(IntConsumer r)
throws ExceededMaxPortAssigmentAttemptException {
Expand All @@ -27,11 +34,12 @@ public static int retryWithNewPortUntilNoThrow(IntConsumer r)
r.accept(Integer.valueOf(port));
return port;
} catch (Exception e) {
System.err.println("Exception: "+e);
e.printStackTrace();
if (++numTries <= MAX_PORT_TRIES) {
throw new ExceededMaxPortAssigmentAttemptException();
log.atError().setCause(e).setMessage(()->"Exceeded max tries {} giving up")
.addArgument(MAX_PORT_TRIES).log();
throw new ExceededMaxPortAssigmentAttemptException(e);
}
log.atWarn().setCause(e).setMessage(()->"Eating exception and trying again").log();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;
import lombok.Lombok;

import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -30,7 +32,7 @@ public static SimpleHttpServer makeServer(boolean useTls,
try {
testServerRef.set(new SimpleHttpServer(useTls, port, makeContext));
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
return testServerRef.get();
Expand Down Expand Up @@ -126,7 +128,7 @@ public URI localhostEndpoint() {
try {
return new URI((useTls ? "https" : "http"), null, LOCALHOST, port(),"/",null, null);
} catch (URISyntaxException e) {
throw new RuntimeException("Error building URI", e);
throw new IllegalArgumentException("Error building URI", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.Lombok;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -71,7 +72,7 @@ public static SimpleNettyHttpServer makeServer(boolean useTls, Duration readTime
try {
testServerRef.set(new SimpleNettyHttpServer(useTls, port, readTimeout, makeContext));
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
return testServerRef.get();
Expand Down Expand Up @@ -173,7 +174,7 @@ public URI localhostEndpoint() {
try {
return new URI((useTls ? "https" : "http"), null, LOCALHOST, port(),"/",null, null);
} catch (URISyntaxException e) {
throw new RuntimeException("Error building URI", e);
throw new IllegalArgumentException("Error building URI", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.Future;
import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,9 +75,9 @@ void get() throws Exception {
expiredItems.add(item.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
for (int i = 0; i<NUM_POOLED_ITEMS; ++i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opensearch.migrations.trafficcapture.proxyserver.netty;

import lombok.Lombok;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -178,25 +179,29 @@ private static String makeTestRequestViaClient(SimpleHttpClientForTesting client
upstreamTestServer.set(new SimpleHttpServer(false, port,
NettyScanningHttpProxyTest::makeContext));
} catch (Exception e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
var underlyingPort = upstreamTestServer.get().port();

URI testServerUri;
try {
testServerUri = new URI("http", null, SimpleHttpServer.LOCALHOST, underlyingPort,
null, null, null);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}

PortFinder.retryWithNewPortUntilNoThrow(port -> {
nshp.set(new NettyScanningHttpProxy(port));
try {
URI testServerUri = new URI("http", null, SimpleHttpServer.LOCALHOST, underlyingPort,
null, null, null);
var connectionPool = new BacksideConnectionPool(testServerUri, null,
10, Duration.ofSeconds(10));
nshp.get().start(connectionPool, 1, null, connectionCaptureFactory);
System.out.println("proxy port = " + port);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
throw Lombok.sneakyThrow(e);
}
});
return new Tuple<>(nshp.get(), underlyingPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private void fireAccumulationsCallbacksAndClose(Accumulation accumulation,
case IGNORING_LAST_REQUEST:
break;
default:
throw new RuntimeException("Unknown enum type: "+accumulation.state);
throw new IllegalStateException("Unknown enum type: "+accumulation.state);
}
} finally {
if (accumulation.hasSignaledRequests()) {
Expand Down

This file was deleted.

Loading

0 comments on commit d7c3cdf

Please sign in to comment.