CORDA-1634 - Destroy child processes when parent exits. (#3368) (#3604)

* [CORDA-1634] Destroy child processes when parent exits.

* Add comment.

* Register Shutdownhook for processes regardless of whether the Driver was initialized with

* Add comment.

* Revert "Add comment."

This reverts commit a5e78c379f.

* Add comment.

* Add shutdown hook in ShutdownManager.registerProcessShutdown.

* Initialize the ShutdownManager with a shutdown hook to ensure that  is called.

* Add comment.
This commit is contained in:
Katelyn Baker 2018-07-13 18:15:05 +01:00 committed by GitHub
parent 143379dad7
commit a7e1381086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -95,6 +95,7 @@ class DriverDSLImpl(
val compatibilityZone: CompatibilityZoneParams?, val compatibilityZone: CompatibilityZoneParams?,
val networkParameters: NetworkParameters val networkParameters: NetworkParameters
) : InternalDriverDSL { ) : InternalDriverDSL {
private var _executorService: ScheduledExecutorService? = null private var _executorService: ScheduledExecutorService? = null
val executorService get() = _executorService!! val executorService get() = _executorService!!
private var _shutdownManager: ShutdownManager? = null private var _shutdownManager: ShutdownManager? = null
@ -699,6 +700,12 @@ class DriverDSLImpl(
val debugPort = if (isDebug) debugPortAllocation.nextPort() else null val debugPort = if (isDebug) debugPortAllocation.nextPort() else null
val monitorPort = if (jmxPolicy.startJmxHttpServer) jmxPolicy.jmxHttpServerPortAllocation?.nextPort() else null val monitorPort = if (jmxPolicy.startJmxHttpServer) jmxPolicy.jmxHttpServerPortAllocation?.nextPort() else null
val process = startOutOfProcessNode(config, quasarJarPath, debugPort, jolokiaJarPath, monitorPort, systemProperties, cordappPackages, maximumHeapSize) val process = startOutOfProcessNode(config, quasarJarPath, debugPort, jolokiaJarPath, monitorPort, systemProperties, cordappPackages, maximumHeapSize)
// Destroy the child process when the parent exits.This is needed even when `waitForAllNodesToFinish` is
// true because we don't want orphaned processes in the case that the parent process is terminated by the
// user, for example when the `tools:explorer:runDemoNodes` gradle task is stopped with CTRL-C.
shutdownManager.registerProcessShutdown(process)
if (waitForAllNodesToFinish) { if (waitForAllNodesToFinish) {
state.locked { state.locked {
processes += object : Waitable { processes += object : Waitable {
@ -707,8 +714,6 @@ class DriverDSLImpl(
} }
} }
} }
} else {
shutdownManager.registerProcessShutdown(process)
} }
val p2pReadyFuture = addressMustBeBoundFuture(executorService, config.corda.p2pAddress, process) val p2pReadyFuture = addressMustBeBoundFuture(executorService, config.corda.p2pAddress, process)
return p2pReadyFuture.flatMap { return p2pReadyFuture.flatMap {

View File

@ -7,12 +7,18 @@ import net.corda.core.utilities.Try
import net.corda.core.utilities.contextLogger import net.corda.core.utilities.contextLogger
import net.corda.core.utilities.getOrThrow import net.corda.core.utilities.getOrThrow
import net.corda.core.utilities.seconds import net.corda.core.utilities.seconds
import net.corda.nodeapi.internal.addShutdownHook
import java.util.concurrent.ExecutorService import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.TimeoutException import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
class ShutdownManager(private val executorService: ExecutorService) { class ShutdownManager(private val executorService: ExecutorService) {
init {
addShutdownHook { shutdown() }
}
private class State { private class State {
val registeredShutdowns = ArrayList<CordaFuture<() -> Unit>>() val registeredShutdowns = ArrayList<CordaFuture<() -> Unit>>()
var isShuttingDown = false var isShuttingDown = false