Skip to content

Commit

Permalink
[th2-3019] Fix: shutdown underlying EventLoopGroup and ExecutorService (
Browse files Browse the repository at this point in the history
  • Loading branch information
lumber1000 authored Jan 31, 2022
1 parent 096ebce commit e9bbcef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (3.32.0)
# th2 common library (Java) (3.32.1)

## Usage

Expand Down Expand Up @@ -288,11 +288,14 @@ dependencies {

## Release notes

### 3.32.1

+ Fixed: gRPC router didn't shut down underlying Netty's EventLoopGroup and ExecutorService

### 3.32.0

+ Added new test utils for assertion of **AnyMessage** or **Groups** of messages


### 3.31.6

+ Update Cradle version from 2.20.0 to [2.20.2](https://github.com/th2-net/cradleapi/releases/tag/2.20.2)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

release_version=3.32.0
release_version=3.32.1

description = 'th2 common library (Java)'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 Exactpro (Exactpro Systems Limited)
* Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -24,6 +24,7 @@
import io.grpc.netty.NettyServerBuilder;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.EventExecutorGroup;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,6 +36,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;

/**
* Abstract implementation for {@link GrpcRouter}
Expand All @@ -52,7 +54,9 @@ public abstract class AbstractGrpcRouter implements GrpcRouter {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGrpcRouter.class);
private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("grpc-router-server-pool-%d").build();
protected List<Server> servers = new ArrayList<>();
protected final List<Server> servers = new ArrayList<>();
protected final List<EventExecutorGroup> loopGroups = new ArrayList<>();
protected final List<ExecutorService> executors = new ArrayList<>();
protected GrpcConfiguration configuration;

@Override
Expand Down Expand Up @@ -97,6 +101,8 @@ public Server startServer(BindableService... services) {

var server = builder.build();

executors.add(executor);
loopGroups.add(eventLoop);
servers.add(server);

return server;
Expand Down Expand Up @@ -125,5 +131,24 @@ public void close() {
LOGGER.error("Failed to shutdown server: {}", server, e);
}
}

loopGroups.forEach(EventExecutorGroup::shutdownGracefully);
loopGroups.forEach(group -> {
if (!group.terminationFuture().awaitUninterruptibly(SERVER_SHUTDOWN_TIMEOUT_MS)) {
LOGGER.error("Failed to shutdown event loop '{}' in {} ms.", group, SERVER_SHUTDOWN_TIMEOUT_MS);
}
});

executors.forEach(ExecutorService::shutdown);
executors.forEach(executor -> {
try {
if (!executor.awaitTermination(SERVER_SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
LOGGER.warn("Failed to shutdown executor service '{}' in {} ms. Forcing shutdown...", executor, SERVER_SHUTDOWN_TIMEOUT_MS);
executor.shutdownNow();
}
} catch (Exception e) {
LOGGER.error("Failed to shutdown executor service: {}", executor, e);
}
});
}
}
}

0 comments on commit e9bbcef

Please sign in to comment.