[CORDA-2591] Prevent spurious logs in AttachmentVersionNumberMigration (#4765)

* Handle case where file containing network parameters is missing

* Improve logging if attachments are not available and the file path doesn't exist

* Get logs the right way around

* Fix build error in tests
This commit is contained in:
JamesHR3 2019-02-16 07:54:07 +00:00 committed by Gavin Thomas
parent d43d0c9640
commit 092d66ac45
3 changed files with 14 additions and 8 deletions

View File

@ -29,11 +29,15 @@ class AttachmentVersionNumberMigration : CustomTaskChange {
logger.info("Start executing...")
var networkParameters: NetworkParameters?
val baseDir = System.getProperty(SchemaMigration.NODE_BASE_DIR_KEY)
val availableAttachments = getAttachmentsWithDefaultVersion(connection)
if (baseDir != null) {
val path = Paths.get(baseDir) / NETWORK_PARAMS_FILE_NAME
networkParameters = getNetworkParametersFromFile(path)
if (networkParameters != null) {
logger.info("$msg using network parameters from $path, whitelistedContractImplementations: ${networkParameters.whitelistedContractImplementations}.")
} else if (availableAttachments.isEmpty()){
logger.info("$msg skipped, network parameters not found in $path, but there are no available attachments to migrate.")
return
} else {
logger.warn("$msg skipped, network parameters not found in $path.")
return
@ -43,7 +47,6 @@ class AttachmentVersionNumberMigration : CustomTaskChange {
return
}
val availableAttachments = getAttachmentsWithDefaultVersion(connection)
if (availableAttachments.isEmpty()) {
logger.info("$msg skipped, no attachments not found.")
return
@ -84,8 +87,13 @@ class AttachmentVersionNumberMigration : CustomTaskChange {
}
private fun getNetworkParametersFromFile(path: Path): NetworkParameters? {
val networkParametersBytes = path?.readObject<SignedNetworkParameters>()
return networkParametersBytes?.raw?.deserialize()
return try {
val networkParametersBytes = path?.readObject<SignedNetworkParameters>()
networkParametersBytes?.raw?.deserialize()
} catch (e: Exception) {
// This condition is logged in the calling function, so no need to do that here.
null
}
}
private fun getAttachmentsWithDefaultVersion(connection: JdbcConnection): List<String> =

View File

@ -30,7 +30,7 @@ class SchemaMigration(
private val databaseConfig: DatabaseConfig,
private val classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
private val currentDirectory: Path?,
private val ourName: CordaX500Name? = null) {
private val ourName: CordaX500Name) {
companion object {
private val logger = contextLogger()
@ -102,9 +102,7 @@ class SchemaMigration(
if (path != null) {
System.setProperty(NODE_BASE_DIR_KEY, path) // base dir for any custom change set which may need to load a file (currently AttachmentVersionNumberMigration)
}
if (ourName != null) {
System.setProperty(NODE_X500_NAME, ourName.toString())
}
System.setProperty(NODE_X500_NAME, ourName.toString())
val customResourceAccessor = CustomResourceAccessor(dynamicInclude, changelogList, classLoader)
val liquibase = Liquibase(dynamicInclude, customResourceAccessor, getLiquibaseDatabase(JdbcConnection(connection)))

View File

@ -1093,7 +1093,7 @@ fun createCordaPersistence(databaseConfig: DatabaseConfig,
return CordaPersistence(databaseConfig, schemaService.schemaOptions.keys, jdbcUrl, cacheFactory, attributeConverters, customClassLoader)
}
fun CordaPersistence.startHikariPool(hikariProperties: Properties, databaseConfig: DatabaseConfig, schemas: Set<MappedSchema>, metricRegistry: MetricRegistry? = null, classloader: ClassLoader = Thread.currentThread().contextClassLoader, currentDir: Path? = null, ourName: CordaX500Name? = null) {
fun CordaPersistence.startHikariPool(hikariProperties: Properties, databaseConfig: DatabaseConfig, schemas: Set<MappedSchema>, metricRegistry: MetricRegistry? = null, classloader: ClassLoader = Thread.currentThread().contextClassLoader, currentDir: Path? = null, ourName: CordaX500Name) {
try {
val dataSource = DataSourceFactory.createDataSource(hikariProperties, metricRegistry = metricRegistry)
val schemaMigration = SchemaMigration(schemas, dataSource, databaseConfig, classloader, currentDir, ourName)