From 201c5582457dc0cdf85033a5e2271c6d069c9a24 Mon Sep 17 00:00:00 2001 From: Andras Slemmer Date: Wed, 19 Apr 2017 12:24:38 +0100 Subject: [PATCH] Shutdown copycat server and h2 database on Node shutdown --- .../kotlin/net/corda/node/internal/Node.kt | 6 +++++- .../transactions/RaftUniquenessProvider.kt | 20 ++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/internal/Node.kt b/node/src/main/kotlin/net/corda/node/internal/Node.kt index b8dfd9e5e2..05f3fdaa34 100644 --- a/node/src/main/kotlin/net/corda/node/internal/Node.kt +++ b/node/src/main/kotlin/net/corda/node/internal/Node.kt @@ -196,7 +196,10 @@ class Node(override val configuration: FullNodeConfiguration, override fun makeUniquenessProvider(type: ServiceType): UniquenessProvider { return when (type) { RaftValidatingNotaryService.type, RaftNonValidatingNotaryService.type -> with(configuration) { - RaftUniquenessProvider(baseDirectory, notaryNodeAddress!!, notaryClusterAddresses, database, configuration) + val provider = RaftUniquenessProvider(baseDirectory, notaryNodeAddress!!, notaryClusterAddresses, database, configuration) + provider.start() + runOnStop += Runnable { provider.stop() } + provider } else -> PersistentUniquenessProvider() } @@ -224,6 +227,7 @@ class Node(override val configuration: FullNodeConfiguration, "-tcpAllowOthers", "-tcpDaemon", "-key", "node", databaseName) + runOnStop += Runnable { server.stop() } val url = server.start().url printBasicNodeInfo("Database connection url is", "jdbc:h2:$url/node") } diff --git a/node/src/main/kotlin/net/corda/node/services/transactions/RaftUniquenessProvider.kt b/node/src/main/kotlin/net/corda/node/services/transactions/RaftUniquenessProvider.kt index c90cb6d19c..6067f20593 100644 --- a/node/src/main/kotlin/net/corda/node/services/transactions/RaftUniquenessProvider.kt +++ b/node/src/main/kotlin/net/corda/node/services/transactions/RaftUniquenessProvider.kt @@ -42,14 +42,20 @@ import javax.annotation.concurrent.ThreadSafe * @param config SSL configuration */ @ThreadSafe -class RaftUniquenessProvider(storagePath: Path, myAddress: HostAndPort, clusterAddresses: List, - db: Database, config: SSLConfiguration) : UniquenessProvider, SingletonSerializeAsToken() { +class RaftUniquenessProvider( + val storagePath: Path, + val myAddress: HostAndPort, + val clusterAddresses: List, + val db: Database, + val config: SSLConfiguration +) : UniquenessProvider, SingletonSerializeAsToken() { companion object { private val log = loggerFor() private val DB_TABLE_NAME = "notary_committed_states" } - private val _clientFuture: CompletableFuture + private lateinit var _clientFuture: CompletableFuture + private lateinit var server: CopycatServer /** * Copycat clients are responsible for connecting to the cluster and submitting commands and queries that operate * on the cluster's replicated state machine. @@ -57,7 +63,7 @@ class RaftUniquenessProvider(storagePath: Path, myAddress: HostAndPort, clusterA private val client: CopycatClient get() = _clientFuture.get() - init { + fun start() { log.info("Creating Copycat server, log stored in: ${storagePath.toFile()}") val stateMachineFactory = { DistributedImmutableMap(db, DB_TABLE_NAME) } val address = Address(myAddress.host, myAddress.port) @@ -65,7 +71,7 @@ class RaftUniquenessProvider(storagePath: Path, myAddress: HostAndPort, clusterA val transport = buildTransport(config) val serializer = Serializer() - val server = CopycatServer.builder(address) + server = CopycatServer.builder(address) .withStateMachine(stateMachineFactory) .withStorage(storage) .withServerTransport(transport) @@ -89,6 +95,10 @@ class RaftUniquenessProvider(storagePath: Path, myAddress: HostAndPort, clusterA _clientFuture = serverFuture.thenCompose { client.connect(address) } } + fun stop() { + server.shutdown() + } + private fun buildStorage(storagePath: Path): Storage? { return Storage.builder() .withDirectory(storagePath.toFile())