@@ -3,25 +3,20 @@ package streams.config
33import kotlinx.coroutines.runBlocking
44import kotlinx.coroutines.sync.Mutex
55import kotlinx.coroutines.sync.withLock
6- import org.neo4j.dbms.api.DatabaseManagementService
76import org.neo4j.kernel.internal.GraphDatabaseAPI
87import org.neo4j.logging.Log
98import org.neo4j.logging.internal.LogService
109import org.neo4j.plugin.configuration.ConfigurationLifecycle
1110import org.neo4j.plugin.configuration.ConfigurationLifecycleUtils
1211import org.neo4j.plugin.configuration.EventType
1312import org.neo4j.plugin.configuration.listners.ConfigurationLifecycleListener
14- import streams.extensions.databaseManagementService
15- import streams.extensions.getDefaultDbName
16- import streams.extensions.isAvailable
1713import streams.utils.Neo4jUtils
18- import streams.utils.ProcedureUtils
1914import streams.utils.StreamsUtils
2015import java.io.File
2116import java.util.concurrent.ConcurrentHashMap
2217import java.util.concurrent.atomic.AtomicReference
2318
24- class StreamsConfig (private val log : Log , private val dbms : DatabaseManagementService ) {
19+ class StreamsConfig (private val log : Log ) {
2520
2621 companion object {
2722 private const val SUN_JAVA_COMMAND = " sun.java.command"
@@ -41,6 +36,8 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
4136 const val POLL_INTERVAL = " streams.sink.poll.interval"
4237 const val INSTANCE_WAIT_TIMEOUT = " streams.wait.timeout"
4338 const val INSTANCE_WAIT_TIMEOUT_VALUE = 120000L
39+ const val CONFIG_WAIT_FOR_AVAILABLE = " streams.wait.for.available"
40+ const val CONFIG_WAIT_FOR_AVAILABLE_VALUE = true
4441
4542 private const val DEFAULT_TRIGGER_PERIOD : Int = 10000
4643
@@ -59,8 +56,8 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
5956
6057 fun getInstance (db : GraphDatabaseAPI ): StreamsConfig = cache.computeIfAbsent(StreamsUtils .getName(db)) {
6158 StreamsConfig (log = db.dependencyResolver
62- .resolveDependency(LogService ::class .java)
63- .getUserLog(StreamsConfig ::class .java), db.databaseManagementService( ))
59+ .resolveDependency(LogService ::class .java)
60+ .getUserLog(StreamsConfig ::class .java))
6461 }
6562
6663 fun removeInstance (db : GraphDatabaseAPI ) {
@@ -83,11 +80,13 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
8380 fun getSystemDbWaitTimeout (config : Map <String , Any ?>) = config.getOrDefault(SYSTEM_DB_WAIT_TIMEOUT , SYSTEM_DB_WAIT_TIMEOUT_VALUE ).toString().toLong()
8481
8582 fun getInstanceWaitTimeout (config : Map <String , Any ?>) = config.getOrDefault(INSTANCE_WAIT_TIMEOUT , INSTANCE_WAIT_TIMEOUT_VALUE ).toString().toLong()
83+
84+ fun isWaitForAvailable (config : Map <String , Any ?>) = config.getOrDefault(CONFIG_WAIT_FOR_AVAILABLE , CONFIG_WAIT_FOR_AVAILABLE_VALUE ).toString().toBoolean()
8685 }
8786
8887 private val configLifecycle: ConfigurationLifecycle
8988
90- private enum class Status {RUNNING , STOPPED , CLOSED , UNKNOWN }
89+ enum class Status {RUNNING , STARTING , STOPPED , CLOSED , UNKNOWN }
9190
9291 private val status = AtomicReference (Status .UNKNOWN )
9392
@@ -100,37 +99,30 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
10099 true , log, true , " streams." , " kafka." )
101100 }
102101
103- fun start () = runBlocking {
102+ fun start (db : GraphDatabaseAPI ) = runBlocking {
104103 if (log.isDebugEnabled) {
105104 log.debug(" Starting StreamsConfig" )
106105 }
107106 mutex.withLock {
108- if (status.get() == Status . RUNNING ) return @runBlocking
107+ if (setOf ( Status . RUNNING , Status . STARTING ).contains( status.get()) ) return @runBlocking
109108 try {
110- // wait for all database to be ready
111- val isInstanceReady = StreamsUtils .blockUntilFalseOrTimeout(getInstanceWaitTimeout()) {
112- if (log.isDebugEnabled) {
113- log.debug(" Waiting for the Neo4j instance to be ready..." )
114- }
115- dbms.isAvailable(100 )
116- }
117- if (! isInstanceReady) {
118- log.warn(" ${getInstanceWaitTimeout()} ms have passed and the instance is not online, the Streams plugin will not started" )
119- return @runBlocking
120- }
121- if (ProcedureUtils .isCluster(dbms)) {
122- log.info(" We're in cluster instance waiting for the ${StreamsUtils .LEADER } s to be elected in each database" )
123- // in case is a cluster we wait for the correct cluster formation => LEADER elected
124- Neo4jUtils .waitForTheLeaders(dbms, log) { configStart() }
109+ if (isWaitForAvailable()) {
110+ status.set(Status .STARTING )
111+ Neo4jUtils .waitForAvailable(db, log, getInstanceWaitTimeout(), { status.set(Status .UNKNOWN ) }) { configStart() }
125112 } else {
126113 configStart()
127114 }
128115 } catch (e: Exception ) {
129116 log.warn(" Cannot start StreamsConfig because of the following exception:" , e)
117+ status.set(Status .UNKNOWN )
130118 }
131119 }
132120 }
133121
122+ fun startEager () = runBlocking {
123+ configStart()
124+ }
125+
134126 private fun configStart () = try {
135127 configLifecycle.start()
136128 status.set(Status .RUNNING )
@@ -139,14 +131,16 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
139131 log.error(" Cannot start the StreamsConfig because of the following exception" , e)
140132 }
141133
134+ fun status (): Status = status.get()
135+
142136 fun stop (shutdown : Boolean = false) = runBlocking {
143137 if (log.isDebugEnabled) {
144138 log.debug(" Stopping StreamsConfig" )
145139 }
146140 mutex.withLock {
147- val status = getStopStatus(shutdown)
148- if (this @StreamsConfig. status.get() == status ) return @runBlocking
149- configStop(shutdown, status )
141+ val stopStatus = getStopStatus(shutdown)
142+ if (status.get() == stopStatus ) return @runBlocking
143+ configStop(shutdown, stopStatus )
150144 }
151145 }
152146
@@ -201,10 +195,6 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
201195
202196 fun getConfiguration (): Map <String , Any > = ConfigurationLifecycleUtils .toMap(configLifecycle.configuration)
203197
204- fun defaultDbName () = this .dbms.getDefaultDbName()
205-
206- fun isDefaultDb (dbName : String ) = this .defaultDbName() == dbName
207-
208198 fun isSourceGloballyEnabled () = Companion .isSourceGloballyEnabled(getConfiguration())
209199
210200 fun isSourceEnabled (dbName : String ) = Companion .isSourceEnabled(getConfiguration(), dbName)
@@ -221,4 +211,6 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
221211
222212 fun getInstanceWaitTimeout () = Companion .getInstanceWaitTimeout(getConfiguration())
223213
214+ fun isWaitForAvailable () = Companion .isWaitForAvailable(getConfiguration())
215+
224216}
0 commit comments