mirror of
https://github.com/corda/corda.git
synced 2025-01-21 03:55:00 +00:00
node: Add port allocation strategies to driver dsl, add jvm debug port
This commit is contained in:
parent
8cf635cf74
commit
35da6b5388
@ -7,9 +7,9 @@ import com.r3corda.core.node.NodeInfo
|
|||||||
import com.r3corda.core.node.services.ServiceType
|
import com.r3corda.core.node.services.ServiceType
|
||||||
import com.r3corda.node.services.config.NodeConfiguration
|
import com.r3corda.node.services.config.NodeConfiguration
|
||||||
import com.r3corda.node.services.messaging.ArtemisMessagingClient
|
import com.r3corda.node.services.messaging.ArtemisMessagingClient
|
||||||
import com.r3corda.node.services.messaging.ArtemisMessagingComponent
|
|
||||||
import com.r3corda.node.services.network.InMemoryNetworkMapCache
|
import com.r3corda.node.services.network.InMemoryNetworkMapCache
|
||||||
import com.r3corda.node.services.network.NetworkMapService
|
import com.r3corda.node.services.network.NetworkMapService
|
||||||
|
import java.net.ServerSocket
|
||||||
import java.net.Socket
|
import java.net.Socket
|
||||||
import java.net.SocketException
|
import java.net.SocketException
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
@ -45,11 +45,33 @@ interface DriverDSLInterface {
|
|||||||
fun startNode(advertisedServices: Set<ServiceType>, providedName: String? = null): NodeInfo
|
fun startNode(advertisedServices: Set<ServiceType>, providedName: String? = null): NodeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <A> driver(baseDirectory: String? = null, quasarPath: String? = null, dsl: DriverDSL.() -> A): Pair<DriverHandle, A> {
|
sealed class PortAllocation {
|
||||||
|
abstract fun nextPort(): Int
|
||||||
|
fun nextHostAndPort() = HostAndPort.fromParts("localhost", nextPort())
|
||||||
|
|
||||||
|
class Incremental(private var portCounter: Int) : PortAllocation() {
|
||||||
|
override fun nextPort() = portCounter++
|
||||||
|
}
|
||||||
|
class RandomFree(): PortAllocation() {
|
||||||
|
override fun nextPort() = ServerSocket(0).use { it.localPort }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: remove quasarJarPath once we have a proper way of bundling quasar
|
||||||
|
*/
|
||||||
|
fun <A> driver(
|
||||||
|
baseDirectory: String = "build/${getTimestampAsDirectoryName()}",
|
||||||
|
quasarJarPath: String = "lib/quasar.jar",
|
||||||
|
portAllocation: PortAllocation = PortAllocation.Incremental(10000),
|
||||||
|
debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005),
|
||||||
|
dsl: DriverDSL.() -> A
|
||||||
|
): Pair<DriverHandle, A> {
|
||||||
val driverDsl = DriverDSL(
|
val driverDsl = DriverDSL(
|
||||||
portCounter = 10000,
|
portAllocation = portAllocation,
|
||||||
baseDirectory = baseDirectory ?: "build/${getTimestampAsDirectoryName()}",
|
debugPortAllocation = debugPortAllocation,
|
||||||
quasarPath = quasarPath ?: "lib/quasar.jar"
|
baseDirectory = baseDirectory,
|
||||||
|
quasarJarPath = quasarJarPath
|
||||||
)
|
)
|
||||||
driverDsl.start()
|
driverDsl.start()
|
||||||
val returnValue = dsl(driverDsl)
|
val returnValue = dsl(driverDsl)
|
||||||
@ -95,13 +117,16 @@ fun <A> poll(f: () -> A?): A {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
class DriverDSL(private var portCounter: Int, val baseDirectory: String, val quasarPath: String) : DriverDSLInterface {
|
class DriverDSL(
|
||||||
|
private val portAllocation: PortAllocation,
|
||||||
fun nextLocalHostAndPort() = HostAndPort.fromParts("localhost", nextPort())
|
private val debugPortAllocation: PortAllocation,
|
||||||
|
val baseDirectory: String,
|
||||||
|
val quasarJarPath: String
|
||||||
|
) : DriverDSLInterface {
|
||||||
|
|
||||||
val networkMapCache = InMemoryNetworkMapCache(null)
|
val networkMapCache = InMemoryNetworkMapCache(null)
|
||||||
private val networkMapName = "NetworkMapService"
|
private val networkMapName = "NetworkMapService"
|
||||||
private val networkMapAddress = nextLocalHostAndPort()
|
private val networkMapAddress = portAllocation.nextHostAndPort()
|
||||||
private lateinit var networkMapNodeInfo: NodeInfo
|
private lateinit var networkMapNodeInfo: NodeInfo
|
||||||
private val registeredProcesses = LinkedList<Process>()
|
private val registeredProcesses = LinkedList<Process>()
|
||||||
|
|
||||||
@ -115,15 +140,9 @@ class DriverDSL(private var portCounter: Int, val baseDirectory: String, val qua
|
|||||||
override val trustStorePassword = "trustpass"
|
override val trustStorePassword = "trustpass"
|
||||||
},
|
},
|
||||||
serverHostPort = networkMapAddress,
|
serverHostPort = networkMapAddress,
|
||||||
myHostPort = nextLocalHostAndPort()
|
myHostPort = portAllocation.nextHostAndPort()
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun nextPort(): Int {
|
|
||||||
val nextPort = portCounter
|
|
||||||
portCounter++
|
|
||||||
return nextPort
|
|
||||||
}
|
|
||||||
|
|
||||||
fun registerProcess(process: Process) = registeredProcesses.push(process)
|
fun registerProcess(process: Process) = registeredProcesses.push(process)
|
||||||
|
|
||||||
internal fun waitForAllNodesToFinish() {
|
internal fun waitForAllNodesToFinish() {
|
||||||
@ -151,7 +170,9 @@ class DriverDSL(private var portCounter: Int, val baseDirectory: String, val qua
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun startNode(advertisedServices: Set<ServiceType>, providedName: String?): NodeInfo {
|
override fun startNode(advertisedServices: Set<ServiceType>, providedName: String?): NodeInfo {
|
||||||
val messagingAddress = nextLocalHostAndPort()
|
val messagingAddress = portAllocation.nextHostAndPort()
|
||||||
|
val apiAddress = portAllocation.nextHostAndPort()
|
||||||
|
val debugPort = debugPortAllocation.nextPort()
|
||||||
val name = providedName ?: "${pickA(name)}-${messagingAddress.port}"
|
val name = providedName ?: "${pickA(name)}-${messagingAddress.port}"
|
||||||
val nearestCity = pickA(city)
|
val nearestCity = pickA(city)
|
||||||
|
|
||||||
@ -161,12 +182,12 @@ class DriverDSL(private var portCounter: Int, val baseDirectory: String, val qua
|
|||||||
networkMapPublicKey = networkMapNodeInfo.identity.owningKey,
|
networkMapPublicKey = networkMapNodeInfo.identity.owningKey,
|
||||||
networkMapAddress = networkMapAddress,
|
networkMapAddress = networkMapAddress,
|
||||||
messagingAddress = messagingAddress,
|
messagingAddress = messagingAddress,
|
||||||
apiAddress = nextLocalHostAndPort(),
|
apiAddress = apiAddress,
|
||||||
baseDirectory = baseDirectory,
|
baseDirectory = baseDirectory,
|
||||||
nearestCity = nearestCity,
|
nearestCity = nearestCity,
|
||||||
legalName = name
|
legalName = name
|
||||||
)
|
)
|
||||||
registerProcess(startNode(driverCliParams, quasarPath))
|
registerProcess(startNode(driverCliParams, quasarJarPath, debugPort))
|
||||||
|
|
||||||
return poll {
|
return poll {
|
||||||
networkMapCache.partyNodes.forEach {
|
networkMapCache.partyNodes.forEach {
|
||||||
@ -204,19 +225,21 @@ class DriverDSL(private var portCounter: Int, val baseDirectory: String, val qua
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startNetworkMapService() {
|
private fun startNetworkMapService() {
|
||||||
|
val apiAddress = portAllocation.nextHostAndPort()
|
||||||
|
val debugPort = debugPortAllocation.nextPort()
|
||||||
val driverCliParams = NodeRunner.CliParams(
|
val driverCliParams = NodeRunner.CliParams(
|
||||||
services = setOf(NetworkMapService.Type),
|
services = setOf(NetworkMapService.Type),
|
||||||
networkMapName = null,
|
networkMapName = null,
|
||||||
networkMapPublicKey = null,
|
networkMapPublicKey = null,
|
||||||
networkMapAddress = null,
|
networkMapAddress = null,
|
||||||
messagingAddress = networkMapAddress,
|
messagingAddress = networkMapAddress,
|
||||||
apiAddress = nextLocalHostAndPort(),
|
apiAddress = apiAddress,
|
||||||
baseDirectory = baseDirectory,
|
baseDirectory = baseDirectory,
|
||||||
nearestCity = pickA(city),
|
nearestCity = pickA(city),
|
||||||
legalName = networkMapName
|
legalName = networkMapName
|
||||||
)
|
)
|
||||||
println("Starting network-map-service")
|
println("Starting network-map-service")
|
||||||
registerProcess(startNode(driverCliParams, quasarPath))
|
registerProcess(startNode(driverCliParams, quasarJarPath, debugPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@ -235,13 +258,15 @@ class DriverDSL(private var portCounter: Int, val baseDirectory: String, val qua
|
|||||||
)
|
)
|
||||||
fun <A> pickA(array: Array<A>): A = array[Math.abs(Random().nextInt()) % array.size]
|
fun <A> pickA(array: Array<A>): A = array[Math.abs(Random().nextInt()) % array.size]
|
||||||
|
|
||||||
private fun startNode(cliParams: NodeRunner.CliParams, quasarPath: String): Process {
|
private fun startNode(cliParams: NodeRunner.CliParams, quasarJarPath: String, debugPort: Int): Process {
|
||||||
val className = NodeRunner::class.java.canonicalName
|
val className = NodeRunner::class.java.canonicalName
|
||||||
val separator = System.getProperty("file.separator")
|
val separator = System.getProperty("file.separator")
|
||||||
val classpath = System.getProperty("java.class.path")
|
val classpath = System.getProperty("java.class.path")
|
||||||
val path = System.getProperty("java.home") + separator + "bin" + separator + "java"
|
val path = System.getProperty("java.home") + separator + "bin" + separator + "java"
|
||||||
val javaArgs = listOf(path) +
|
val javaArgs = listOf(path) +
|
||||||
listOf("-Dname=${cliParams.legalName}", "-javaagent:$quasarPath", "-cp", classpath, className) +
|
listOf("-Dname=${cliParams.legalName}", "-javaagent:$quasarJarPath",
|
||||||
|
"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$debugPort",
|
||||||
|
"-cp", classpath, className) +
|
||||||
cliParams.toCliArguments()
|
cliParams.toCliArguments()
|
||||||
val builder = ProcessBuilder(javaArgs)
|
val builder = ProcessBuilder(javaArgs)
|
||||||
builder.redirectError(Paths.get("error.$className.log").toFile())
|
builder.redirectError(Paths.get("error.$className.log").toFile())
|
||||||
|
Loading…
Reference in New Issue
Block a user