Create nodeInfoDirectory in NodeInfoWatcher initialization, and make NodeInfoWatcher logging less verbose and less frequent (#1857)

Let NodeInfoWatcher create the directory it wants to poll at startup.
Also log failure in creating the directory, but don't log anything if it can be found at poll time.
This commit is contained in:
Alberto Arri 2017-10-11 12:54:30 +01:00 committed by GitHub
parent af44c96c14
commit d0d0f132df
2 changed files with 23 additions and 11 deletions

View File

@ -56,11 +56,12 @@ class NodeInfoWatcherTest : NodeBasedTest() {
@Test
fun `save a NodeInfo`() {
assertEquals(0, folder.root.list().size)
assertEquals(0, folder.root.list().filter { it.matches(nodeInfoFileRegex) }.size)
NodeInfoWatcher.saveToFile(folder.root.toPath(), nodeInfo, keyManagementService)
assertEquals(1, folder.root.list().size)
val fileName = folder.root.list()[0]
val nodeInfoFiles = folder.root.list().filter { it.matches(nodeInfoFileRegex) }
assertEquals(1, nodeInfoFiles.size)
val fileName = nodeInfoFiles.first()
assertTrue(fileName.matches(nodeInfoFileRegex))
val file = (folder.root.path / fileName).toFile()
// Just check that something is written, another tests verifies that the written value can be read back.

View File

@ -12,6 +12,7 @@ import net.corda.core.utilities.seconds
import rx.Observable
import rx.Scheduler
import rx.schedulers.Schedulers
import java.io.IOException
import java.nio.file.Path
import java.util.concurrent.TimeUnit
import kotlin.streams.toList
@ -32,7 +33,8 @@ class NodeInfoWatcher(private val nodePath: Path,
private val scheduler: Scheduler = Schedulers.io()) {
private val nodeInfoDirectory = nodePath / CordformNode.NODE_INFO_DIRECTORY
private val pollFrequencyMsec: Long
private val pollFrequencyMsec: Long = maxOf(pollFrequencyMsec, 5.seconds.toMillis())
private val successfullyProcessedFiles = mutableSetOf<Path>()
companion object {
private val logger = loggerFor<NodeInfoWatcher>()
@ -61,7 +63,13 @@ class NodeInfoWatcher(private val nodePath: Path,
}
init {
this.pollFrequencyMsec = maxOf(pollFrequencyMsec, 5.seconds.toMillis())
if (!nodeInfoDirectory.isDirectory()) {
try {
nodeInfoDirectory.createDirectories()
} catch (e: IOException) {
logger.info("Failed to create $nodeInfoDirectory", e)
}
}
}
/**
@ -71,8 +79,7 @@ class NodeInfoWatcher(private val nodePath: Path,
* We simply list the directory content every 5 seconds, the Java implementation of WatchService has been proven to
* be unreliable on MacOs and given the fairly simple use case we have, this simple implementation should do.
*
* @return an [Observable] returning [NodeInfo]s, there is no guarantee that the same value isn't returned more
* than once.
* @return an [Observable] returning [NodeInfo]s, at most one [NodeInfo] is returned for each processed file.
*/
fun nodeInfoUpdates(): Observable<NodeInfo> {
return Observable.interval(pollFrequencyMsec, TimeUnit.MILLISECONDS, scheduler)
@ -87,16 +94,20 @@ class NodeInfoWatcher(private val nodePath: Path,
*/
private fun loadFromDirectory(): List<NodeInfo> {
if (!nodeInfoDirectory.isDirectory()) {
logger.info("$nodeInfoDirectory isn't a Directory, not loading NodeInfo from files")
return emptyList()
}
val result = nodeInfoDirectory.list { paths ->
paths.filter { it.isRegularFile() }
.map { processFile(it) }
paths.filter { it !in successfullyProcessedFiles }
.filter { it.isRegularFile() }
.map { path ->
processFile(path)?.apply { successfullyProcessedFiles.add(path) }
}
.toList()
.filterNotNull()
}
logger.info("Successfully read ${result.size} NodeInfo files.")
if (result.isNotEmpty()) {
logger.info("Successfully read ${result.size} NodeInfo files from disk.")
}
return result
}