Node startup minor refactor (#4342)

* Slight refactor

* Move side effect out of method
This commit is contained in:
Anthony Keenan
2018-12-03 09:42:16 +00:00
committed by GitHub
parent 00865a9456
commit fab09c74d5
3 changed files with 10 additions and 14 deletions

View File

@ -126,7 +126,7 @@ open class NodeStartup : NodeStartupLogging {
// Step 2. We do the single node check before we initialise logging so that in case of a double-node start it // Step 2. We do the single node check before we initialise logging so that in case of a double-node start it
// doesn't mess with the running node's logs. // doesn't mess with the running node's logs.
enforceSingleNodeIsRunning(cmdLineOptions.baseDirectory) if (!isNodeRunningAt(cmdLineOptions.baseDirectory)) return ExitCodes.FAILURE
// Step 3. Register all cryptography [Provider]s. // Step 3. Register all cryptography [Provider]s.
// Required to install our [SecureRandom] before e.g., UUID asks for one. // Required to install our [SecureRandom] before e.g., UUID asks for one.
@ -253,7 +253,7 @@ open class NodeStartup : NodeStartupLogging {
} }
} }
internal fun enforceSingleNodeIsRunning(baseDirectory: Path) { fun isNodeRunningAt(baseDirectory: Path): Boolean {
// Write out our process ID (which may or may not resemble a UNIX process id - to us it's just a string) to a // Write out our process ID (which may or may not resemble a UNIX process id - to us it's just a string) to a
// file that we'll do our best to delete on exit. But if we don't, it'll be overwritten next time. If it already // file that we'll do our best to delete on exit. But if we don't, it'll be overwritten next time. If it already
// exists, we try to take the file lock first before replacing it and if that fails it means we're being started // exists, we try to take the file lock first before replacing it and if that fails it means we're being started
@ -267,7 +267,7 @@ open class NodeStartup : NodeStartupLogging {
if (pidFileLock == null) { if (pidFileLock == null) {
println("It appears there is already a node running with the specified data directory $baseDirectory") println("It appears there is already a node running with the specified data directory $baseDirectory")
println("Shut that other node down and try again. It may have process ID ${pidFile.readText()}") println("Shut that other node down and try again. It may have process ID ${pidFile.readText()}")
System.exit(1) return false
} }
pidFile.deleteOnExit() pidFile.deleteOnExit()
// Avoid the lock being garbage collected. We don't really need to release it as the OS will do so for us // Avoid the lock being garbage collected. We don't really need to release it as the OS will do so for us
@ -282,8 +282,9 @@ open class NodeStartup : NodeStartupLogging {
val appUser = System.getProperty("user.name") val appUser = System.getProperty("user.name")
println("Application user '$appUser' does not have necessary permissions for Node base directory '$baseDirectory'.") println("Application user '$appUser' does not have necessary permissions for Node base directory '$baseDirectory'.")
println("Corda Node process in now exiting. Please check directory permissions and try starting the Node again.") println("Corda Node process in now exiting. Please check directory permissions and try starting the Node again.")
System.exit(1) return false
} }
return true
} }
private fun lookupMachineNameAndMaybeWarn(): String { private fun lookupMachineNameAndMaybeWarn(): String {

View File

@ -1,6 +1,7 @@
package net.corda.node.internal package net.corda.node.internal
import com.google.common.io.Files import com.google.common.io.Files
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test import org.junit.Test
import java.nio.channels.OverlappingFileLockException import java.nio.channels.OverlappingFileLockException
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
@ -16,7 +17,7 @@ class NodeStartupTest {
thread(start = true) { thread(start = true) {
val node = NodeStartup() val node = NodeStartup()
node.enforceSingleNodeIsRunning(dir) assertThat(node.isNodeRunningAt(dir)).isTrue()
latch.countDown() latch.countDown()
} }
@ -25,6 +26,6 @@ class NodeStartupTest {
// Check that I can't start up another node in the same directory // Check that I can't start up another node in the same directory
val anotherNode = NodeStartup() val anotherNode = NodeStartup()
assertFailsWith<OverlappingFileLockException> { anotherNode.enforceSingleNodeIsRunning(dir) } assertFailsWith<OverlappingFileLockException> { anotherNode.isNodeRunningAt(dir) }
} }
} }

View File

@ -121,14 +121,8 @@ class DriverTests {
baseDirectory baseDirectory
} }
if ((baseDirectory / "process-id").exists()) { // Should be able to start another node up in that directory
// The addShutdownHook call doesn't reliably get called on Windows (even on graceful node shutdown), so at least check assertThat(NodeStartup().isNodeRunningAt(baseDirectory)).isTrue()
// that the lock has been released, to make sure the node has been killed
val pidFile = (baseDirectory / "process-id").toFile()
val pidFileRw = RandomAccessFile(pidFile, "rw")
pidFileRw.channel.tryLock()
pidFileRw.close()
}
} }
@Test @Test