diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt index c404ece804..d0f2110f79 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -304,7 +304,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, servicesForResolution.start(netParams) networkMapCache.start(netParams.notaries) - startDatabase() + startDatabase(metricRegistry) val (identity, identityKeyPair) = obtainIdentity(notaryConfig = null) identityService.start(trustRoot, listOf(identity.certificate, nodeCa)) @@ -728,10 +728,10 @@ abstract class AbstractNode(val configuration: NodeConfiguration, // Specific class so that MockNode can catch it. class DatabaseConfigurationException(msg: String) : CordaException(msg) - protected open fun startDatabase() { + protected open fun startDatabase(metricRegistry: MetricRegistry? = null) { val props = configuration.dataSourceProperties if (props.isEmpty) throw DatabaseConfigurationException("There must be a database configured.") - database.startHikariPool(props, configuration.database, schemaService.internalSchemas()) + database.startHikariPool(props, configuration.database, schemaService.internalSchemas(), metricRegistry) // Now log the vendor string as this will also cause a connection to be tested eagerly. logVendorString(database, log) } @@ -1014,9 +1014,9 @@ fun createCordaPersistence(databaseConfig: DatabaseConfig, return CordaPersistence(databaseConfig, schemaService.schemaOptions.keys, jdbcUrl, attributeConverters) } -fun CordaPersistence.startHikariPool(hikariProperties: Properties, databaseConfig: DatabaseConfig, schemas: Set) { +fun CordaPersistence.startHikariPool(hikariProperties: Properties, databaseConfig: DatabaseConfig, schemas: Set, metricRegistry: MetricRegistry? = null) { try { - val dataSource = DataSourceFactory.createDataSource(hikariProperties) + val dataSource = DataSourceFactory.createDataSource(hikariProperties, metricRegistry = metricRegistry) val schemaMigration = SchemaMigration(schemas, dataSource, databaseConfig) schemaMigration.nodeStartup(dataSource.connection.use { DBCheckpointStorage().getCheckpointCount(it) != 0L }) start(dataSource) @@ -1037,4 +1037,4 @@ fun clientSslOptionsCompatibleWith(nodeRpcOptions: NodeRpcOptions): ClientRpcSsl } // Here we're using the node's RPC key store as the RPC client's trust store. return ClientRpcSslOptions(trustStorePath = nodeRpcOptions.sslConfig!!.keyStorePath, trustStorePassword = nodeRpcOptions.sslConfig!!.keyStorePassword) -} \ No newline at end of file +} diff --git a/node/src/main/kotlin/net/corda/node/internal/DataSourceFactory.kt b/node/src/main/kotlin/net/corda/node/internal/DataSourceFactory.kt index d4cad1ff10..e418813989 100644 --- a/node/src/main/kotlin/net/corda/node/internal/DataSourceFactory.kt +++ b/node/src/main/kotlin/net/corda/node/internal/DataSourceFactory.kt @@ -1,5 +1,6 @@ package net.corda.node.internal +import com.codahale.metrics.MetricRegistry import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import com.zaxxer.hikari.util.PropertyElf @@ -32,10 +33,14 @@ object DataSourceFactory { }.set(null, SynchronizedGetPutRemove()) } - fun createDataSource(hikariProperties: Properties, pool: Boolean = true): DataSource { + fun createDataSource(hikariProperties: Properties, pool: Boolean = true, metricRegistry: MetricRegistry? = null): DataSource { val config = HikariConfig(hikariProperties) return if (pool) { - HikariDataSource(config) + val dataSource = HikariDataSource(config) + if (metricRegistry != null) { + dataSource.metricRegistry = metricRegistry + } + dataSource } else { // Basic init for the one test that wants to go via this API but without starting a HikariPool: (Class.forName(hikariProperties.getProperty("dataSourceClassName")).newInstance() as DataSource).also { 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 d32b399256..0216158b2f 100644 --- a/node/src/main/kotlin/net/corda/node/internal/Node.kt +++ b/node/src/main/kotlin/net/corda/node/internal/Node.kt @@ -331,7 +331,7 @@ open class Node(configuration: NodeConfiguration, * This is not using the H2 "automatic mixed mode" directly but leans on many of the underpinnings. For more details * on H2 URLs and configuration see: http://www.h2database.com/html/features.html#database_url */ - override fun startDatabase() { + override fun startDatabase(metricRegistry: MetricRegistry?) { val databaseUrl = configuration.dataSourceProperties.getProperty("dataSource.url") val h2Prefix = "jdbc:h2:file:" @@ -368,7 +368,7 @@ open class Node(configuration: NodeConfiguration, } } - super.startDatabase() + super.startDatabase(metricRegistry) database.closeOnStop() } @@ -486,4 +486,4 @@ open class Node(configuration: NodeConfiguration, log.info("Shutdown complete") } -} \ No newline at end of file +} diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt index e571ee1cfd..ae0a4d00f4 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt @@ -1,5 +1,6 @@ package net.corda.testing.node.internal +import com.codahale.metrics.MetricRegistry import com.google.common.jimfs.Configuration.unix import com.google.common.jimfs.Jimfs import com.nhaarman.mockito_kotlin.doReturn @@ -404,8 +405,8 @@ open class InternalMockNetwork(defaultParameters: MockNetworkParameters = MockNe get() = _serializationWhitelists private var dbCloser: (() -> Any?)? = null - override fun startDatabase() { - super.startDatabase() + override fun startDatabase(metricRegistry: MetricRegistry?) { + super.startDatabase(metricRegistry) dbCloser = database::close runOnStop += dbCloser!! }