Shutdown SSH executors upon SshDaemon stop

When Gerrit SSH Daemon is stopped, there is no value in keeping its
executors threads alive as it would only consume precious resources
we do need during our Integration Tests suite.

Apache SSHD does not manage the shutdown of the internal executors
by himself, so we need to close them manually.

Change-Id: I09a62759769bbb222abd4a3ea60be8b8c5571ac9
This commit is contained in:
Luca Milanesio 2017-03-29 08:41:13 +01:00 committed by David Pursehouse
parent 483baef151
commit 5f8c7f5e5d

View File

@ -60,6 +60,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.sshd.common.BaseBuilder;
@ -72,7 +73,9 @@ import org.apache.sshd.common.file.FileSystemFactory;
import org.apache.sshd.common.forward.DefaultTcpipForwarderFactory;
import org.apache.sshd.common.future.CloseFuture;
import org.apache.sshd.common.future.SshFutureListener;
import org.apache.sshd.common.io.AbstractIoServiceFactory;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoServiceFactory;
import org.apache.sshd.common.io.IoServiceFactoryFactory;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.io.mina.MinaServiceFactoryFactory;
@ -349,6 +352,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
if (daemonAcceptor != null) {
try {
daemonAcceptor.close(true).await();
shutdownExecutors();
sshDaemonLog.info("Stopped Gerrit SSHD");
} catch (IOException e) {
sshDaemonLog.warn("Exception caught while closing", e);
@ -358,6 +362,25 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
}
}
private void shutdownExecutors() {
if (executor != null) {
executor.shutdownNow();
}
IoServiceFactory serviceFactory = getIoServiceFactory();
if (serviceFactory instanceof AbstractIoServiceFactory) {
shutdownServiceFactoryExecutor((AbstractIoServiceFactory) serviceFactory);
}
}
private void shutdownServiceFactoryExecutor(AbstractIoServiceFactory ioServiceFactory) {
ioServiceFactory.close(true);
ExecutorService serviceFactoryExecutor = ioServiceFactory.getExecutorService();
if (serviceFactoryExecutor != null && serviceFactoryExecutor != executor) {
serviceFactoryExecutor.shutdownNow();
}
}
@Override
protected void checkConfig() {
super.checkConfig();