node: Add extra Driver checks for node shutdown, extend try/finally scope of cleanup

This commit is contained in:
Andras Slemmer 2016-08-04 14:55:21 +01:00
parent 2e6de61ad0
commit df4413ab68
3 changed files with 40 additions and 33 deletions

View File

@ -39,6 +39,8 @@ import java.util.concurrent.TimeoutException
* TODO The network map service bootstrap is hacky (needs to fake the service's public key in order to retrieve the true one), needs some thought.
*/
private val log: Logger = LoggerFactory.getLogger(DriverDSL::class.java)
/**
* This is the interface that's exposed to
*/
@ -66,8 +68,6 @@ sealed class PortAllocation {
}
}
private val log: Logger = LoggerFactory.getLogger("Driver")
/**
* [driver] allows one to start up nodes like this:
* driver {
@ -127,19 +127,22 @@ fun <DI : DriverDSLExposedInterface, D : DriverDSLInternalInterface, A> genericD
dsl: DI.() -> A,
with: (DriverHandle, A) -> Unit
): A {
driverDsl.start()
val returnValue = dsl(coerce(driverDsl))
val shutdownHook = Thread({
driverDsl.shutdown()
})
Runtime.getRuntime().addShutdownHook(shutdownHook)
var shutdownHook: Thread? = null
try {
driverDsl.start()
val returnValue = dsl(coerce(driverDsl))
shutdownHook = Thread({
driverDsl.shutdown()
})
Runtime.getRuntime().addShutdownHook(shutdownHook)
with(DriverHandle(driverDsl), returnValue)
return returnValue
} finally {
driverDsl.shutdown()
Runtime.getRuntime().removeShutdownHook(shutdownHook)
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook)
}
}
return returnValue
}
private fun getTimestampAsDirectoryName(): String {
@ -149,6 +152,28 @@ private fun getTimestampAsDirectoryName(): String {
return df.format(Date())
}
fun addressMustBeBound(hostAndPort: HostAndPort) {
poll {
try {
Socket(hostAndPort.hostText, hostAndPort.port).close()
Unit
} catch (_exception: SocketException) {
null
}
}
}
fun addressMustNotBeBound(hostAndPort: HostAndPort) {
poll {
try {
Socket(hostAndPort.hostText, hostAndPort.port).close()
null
} catch (_exception: SocketException) {
Unit
}
}
}
class DriverHandle(private val driverDsl: DriverDSLInternalInterface) {
val messagingService = driverDsl.messagingService
val networkMapCache = driverDsl.networkMapCache
@ -228,6 +253,10 @@ class DriverDSL(
}
}
messagingService.stop()
// Check that we shut down properly
addressMustNotBeBound(messagingService.myHostPort)
addressMustNotBeBound((networkMapNodeInfo.address as ArtemisMessagingService.Address).hostAndPort)
}
/**

View File

@ -23,7 +23,7 @@ import java.nio.file.Paths
import java.security.PublicKey
import java.util.*
private val log: Logger = LoggerFactory.getLogger("NodeRunner")
private val log: Logger = LoggerFactory.getLogger(NodeRunner::class.java)
class NodeRunner {
companion object {

View File

@ -14,28 +14,6 @@ import java.net.SocketException
class DriverTests {
companion object {
fun addressMustBeBound(hostAndPort: HostAndPort) {
poll {
try {
Socket(hostAndPort.hostText, hostAndPort.port).close()
Unit
} catch (_exception: SocketException) {
null
}
}
}
fun addressMustNotBeBound(hostAndPort: HostAndPort) {
poll {
try {
Socket(hostAndPort.hostText, hostAndPort.port).close()
null
} catch (_exception: SocketException) {
Unit
}
}
}
fun nodeMustBeUp(networkMapCache: NetworkMapCache, nodeInfo: NodeInfo, nodeName: String) {
val address = nodeInfo.address as ArtemisMessagingComponent.Address
// Check that the node is registered in the network map