Skip to content

Commit

Permalink
[#noissue] Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 1, 2024
1 parent 087e5a7 commit 268987d
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTraceField;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.common.util.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -139,18 +140,8 @@ private ExpectedTrace createServerRootTrace(HelloWorldServer server) {
}

private void clearResources(HelloWorldClient client, HelloWorldServer server) {
try {
if (client != null) {
client.shutdown();
}
} catch (Exception e) {
}
try {
if (server != null) {
server.stop();
}
} catch (Exception e) {
}
IOUtils.closeQuietly(client);
IOUtils.closeQuietly(server);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import java.io.Closeable;

/**
* @author Taejin Koo
*/
public interface HelloWorldClient {

void shutdown() throws Exception;
public interface HelloWorldClient extends Closeable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import java.io.Closeable;
import java.io.IOException;

/**
* @author Taejin Koo
*/
public interface HelloWorldServer {
public interface HelloWorldServer extends Closeable {

void start() throws IOException;

void stop() throws InterruptedException;

int getBindPort();

String getMethodName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import com.navercorp.pinpoint.it.plugin.utils.ExecutorUtils;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.examples.helloworld.GreeterGrpc;
Expand All @@ -25,7 +26,6 @@
import io.grpc.stub.MetadataUtils;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -73,11 +73,10 @@ private static ManagedChannel newChannel(String host, int port, NioEventLoopGrou
}

@Override
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
Future<?> future = eventExecutors.shutdownGracefully(500, 500, TimeUnit.MILLISECONDS);
future.await(1000);
workerExecutor.shutdownNow();
public void close() {
ShutdownUtils.shutdownChannel(channel);
ShutdownUtils.shutdownEventExecutor(eventExecutors);
ExecutorUtils.shutdownAndAwaitTermination(workerExecutor, 3, TimeUnit.SECONDS);
}

public String greet(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import com.navercorp.pinpoint.it.plugin.utils.ExecutorUtils;
import com.navercorp.pinpoint.testcase.util.SocketUtils;
import io.grpc.Server;
import io.grpc.examples.helloworld.GreeterGrpc;
Expand All @@ -25,7 +26,6 @@
import io.grpc.stub.StreamObserver;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

Expand Down Expand Up @@ -78,26 +78,20 @@ public void start() throws IOException {
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
try {
HelloWorldSimpleServer.this.stop();
} catch (InterruptedException e) {
}
HelloWorldSimpleServer.this.close();
System.err.println("*** server shut down");
}
});
}

@PreDestroy
public void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

Future<?> future = eventExecutors.shutdownGracefully(500, 500, TimeUnit.MILLISECONDS);
future.await(1000);
workerExecutor.shutdownNow();
public void close() {
ShutdownUtils.shutdownServer(server);
ShutdownUtils.shutdownEventExecutor(eventExecutors);
ExecutorUtils.shutdownAndAwaitTermination(workerExecutor, 3, TimeUnit.SECONDS);
}


@Override
public int getBindPort() {
return bindPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import com.navercorp.pinpoint.it.plugin.utils.ExecutorUtils;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.examples.manualflowcontrol.HelloReply;
import io.grpc.examples.manualflowcontrol.HelloRequest;
import io.grpc.examples.manualflowcontrol.StreamingGreeterGrpc;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.ClientCallStreamObserver;
Expand All @@ -26,7 +29,6 @@
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;

import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -76,12 +78,10 @@ private static ManagedChannel newChannel(String host, int port, EventLoopGroup e
}

@Override
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);

Future<?> future = eventExecutors.shutdownGracefully(500, 500, TimeUnit.MILLISECONDS);
future.await(1000);
workerExecutor.shutdownNow();
public void close() {
ShutdownUtils.shutdownChannel(channel);
ShutdownUtils.shutdownEventExecutor(eventExecutors);
ExecutorUtils.shutdownAndAwaitTermination(workerExecutor, 3, TimeUnit.SECONDS);
}

/**
Expand All @@ -91,13 +91,13 @@ public void greet(final int callCount) throws InterruptedException {

final CountDownLatch done = new CountDownLatch(1);

ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply> clientResponseObserver =
new ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply>() {
ClientResponseObserver<HelloRequest, HelloReply> clientResponseObserver =
new ClientResponseObserver<HelloRequest, HelloReply>() {

ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream;
ClientCallStreamObserver<HelloRequest> requestStream;

@Override
public void beforeStart(final ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream) {
public void beforeStart(final ClientCallStreamObserver<HelloRequest> requestStream) {
this.requestStream = requestStream;
// Set up manual flow control for the response stream. It feels backwards to configure the response
// stream's flow control using the request stream's observer, but this is the way it is.
Expand Down Expand Up @@ -128,7 +128,7 @@ public void run() {
// Send more messages if there are more messages to send.
String name = iterator.next();
logger.info("--> " + name);
io.grpc.examples.manualflowcontrol.HelloRequest request = io.grpc.examples.manualflowcontrol.HelloRequest.newBuilder().setName(name).build();
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
requestStream.onNext(request);
count++;
} else {
Expand All @@ -141,7 +141,7 @@ public void run() {
}

@Override
public void onNext(io.grpc.examples.manualflowcontrol.HelloReply value) {
public void onNext(HelloReply value) {
logger.info("<-- " + value.getMessage());
// Signal the sender to send one message.
requestStream.request(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import com.navercorp.pinpoint.it.plugin.utils.ExecutorUtils;
import com.navercorp.pinpoint.testcase.util.SocketUtils;
import io.grpc.Server;
import io.grpc.Status;
Expand All @@ -27,7 +28,6 @@
import io.grpc.stub.StreamObserver;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

Expand Down Expand Up @@ -182,15 +182,14 @@ public int getRequestCount() {
}

@PreDestroy
public void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

Future<?> future = eventExecutors.shutdownGracefully(500, 500, TimeUnit.MILLISECONDS);
future.await(1000);
workerExecutor.shutdownNow();
@Override
public void close() throws IOException {
ShutdownUtils.shutdownServer(server);
ShutdownUtils.shutdownEventExecutor(eventExecutors);
ExecutorUtils.shutdownAndAwaitTermination(workerExecutor, 3, TimeUnit.SECONDS);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.navercorp.pinpoint.it.plugin.grpc;

import io.grpc.ManagedChannel;
import io.grpc.Server;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.Future;

import java.util.concurrent.TimeUnit;

public final class ShutdownUtils {
private ShutdownUtils() {
}

public static boolean shutdownServer(Server server) {
if (server == null) {
return false;
}
try {
return server.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

public static boolean shutdownEventExecutor(EventExecutorGroup eventExecutors) {
if (eventExecutors == null) {
return false;
}
try {
Future<?> future = eventExecutors.shutdownGracefully(500, 500, TimeUnit.MILLISECONDS);
future.await(3000);
return future.isSuccess();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

public static boolean shutdownChannel(ManagedChannel channel) {
if (channel == null) {
return false;
}
try {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedTraceField;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.common.util.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -139,18 +140,8 @@ private ExpectedTrace createServerRootTrace(HelloWorldServer server) {
}

private void clearResources(HelloWorldClient client, HelloWorldServer server) {
try {
if (client != null) {
client.shutdown();
}
} catch (Exception e) {
}
try {
if (server != null) {
server.stop();
}
} catch (Exception e) {
}
IOUtils.closeQuietly(client);
IOUtils.closeQuietly(server);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import java.io.Closeable;

/**
* @author Taejin Koo
*/
public interface HelloWorldClient {

void shutdown() throws Exception;
public interface HelloWorldClient extends Closeable {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

package com.navercorp.pinpoint.it.plugin.grpc;

import java.io.Closeable;
import java.io.IOException;

/**
* @author Taejin Koo
*/
public interface HelloWorldServer {
public interface HelloWorldServer extends Closeable {

void start() throws IOException;

void stop() throws InterruptedException;

int getBindPort();

String getMethodName();
Expand Down
Loading

0 comments on commit 268987d

Please sign in to comment.