From 170fde9d71adaacd0198673ae4ba9750243c052e Mon Sep 17 00:00:00 2001 From: Christian Sailer <christian.sailer@r3.com> Date: Thu, 26 Oct 2017 16:09:58 +0100 Subject: [PATCH] Code rewiew: - add comments - clearer variable names - use constants instead of literal --- .../node/services/config/NodeConfiguration.kt | 4 ++++ .../statemachine/StateMachineManagerImpl.kt | 5 ++++- .../corda/node/utilities/NamedThreadFactory.kt | 18 ++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt b/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt index d5c93dafbb..2fcba96f70 100644 --- a/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt +++ b/node/src/main/kotlin/net/corda/node/services/config/NodeConfiguration.kt @@ -36,6 +36,10 @@ interface NodeConfiguration : NodeSSLConfiguration { val notary: NotaryConfig? val activeMQServer: ActiveMqServerConfiguration val additionalNodeInfoPollingFrequencyMsec: Long + + companion object { + val disableCheckpointCheckerFlag = "disableCheckpointChecker" + } } fun NodeConfiguration.isDevModeOptionsFlagSet(flag: String):Boolean{ diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManagerImpl.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManagerImpl.kt index be4f991313..ed559f3411 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManagerImpl.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManagerImpl.kt @@ -31,6 +31,7 @@ import net.corda.node.internal.InitiatedFlowFactory import net.corda.node.services.api.Checkpoint import net.corda.node.services.api.CheckpointStorage import net.corda.node.services.api.ServiceHubInternal +import net.corda.node.services.config.NodeConfiguration import net.corda.node.services.config.isDevModeOptionsFlagSet import net.corda.node.services.messaging.ReceivedMessage import net.corda.node.services.messaging.TopicSession @@ -89,7 +90,9 @@ class StateMachineManagerImpl( private val scheduler = FiberScheduler() private val mutex = ThreadBox(InnerState()) // This thread (only enabled in dev mode) deserialises checkpoints in the background to shake out bugs in checkpoint restore. - private val checkpointCheckerThread = if (serviceHub.configuration.devMode && !serviceHub.configuration.isDevModeOptionsFlagSet("disableCheckpointChecking")) newNamedSingleThreadExecutor("CheckpointChecker") else null + private val checkpointCheckerThread = if (serviceHub.configuration.devMode + && !serviceHub.configuration.isDevModeOptionsFlagSet(NodeConfiguration.disableCheckpointCheckerFlag)) + newNamedSingleThreadExecutor("CheckpointChecker") else null @Volatile private var unrestorableCheckpoints = false diff --git a/node/src/main/kotlin/net/corda/node/utilities/NamedThreadFactory.kt b/node/src/main/kotlin/net/corda/node/utilities/NamedThreadFactory.kt index bd93f57470..c7fd6c0186 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/NamedThreadFactory.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/NamedThreadFactory.kt @@ -5,15 +5,25 @@ import java.util.concurrent.Executors import java.util.concurrent.ThreadFactory import java.util.concurrent.atomic.AtomicInteger + +/** + * Utility class that allows to give threads arbitrary name prefixes when they are created + * via an executor. It will use an underlying thread factory to create the actual thread + * and then override the thread name with the prefix and an ever increasing number + */ class NamedThreadFactory(private val name:String, private val underlyingFactory: ThreadFactory) : ThreadFactory{ val threadNumber = AtomicInteger(1) - override fun newThread(r: Runnable?): Thread { - val t = underlyingFactory.newThread(r) - t.name = name + "-" + threadNumber.getAndIncrement() - return t + override fun newThread(runnable: Runnable?): Thread { + val thread = underlyingFactory.newThread(runnable) + thread.name = name + "-" + threadNumber.getAndIncrement() + return thread } } +/** + * Create a single thread executor with a NamedThreadFactory based on the default thread factory + * defined in java.util.concurrent.Executors + */ fun newNamedSingleThreadExecutor(name: String): ExecutorService { return Executors.newSingleThreadExecutor(NamedThreadFactory(name, Executors.defaultThreadFactory())) }