@@ -3,25 +3,20 @@ package streams.config
3
3
import kotlinx.coroutines.runBlocking
4
4
import kotlinx.coroutines.sync.Mutex
5
5
import kotlinx.coroutines.sync.withLock
6
- import org.neo4j.dbms.api.DatabaseManagementService
7
6
import org.neo4j.kernel.internal.GraphDatabaseAPI
8
7
import org.neo4j.logging.Log
9
8
import org.neo4j.logging.internal.LogService
10
9
import org.neo4j.plugin.configuration.ConfigurationLifecycle
11
10
import org.neo4j.plugin.configuration.ConfigurationLifecycleUtils
12
11
import org.neo4j.plugin.configuration.EventType
13
12
import org.neo4j.plugin.configuration.listners.ConfigurationLifecycleListener
14
- import streams.extensions.databaseManagementService
15
- import streams.extensions.getDefaultDbName
16
- import streams.extensions.isAvailable
17
13
import streams.utils.Neo4jUtils
18
- import streams.utils.ProcedureUtils
19
14
import streams.utils.StreamsUtils
20
15
import java.io.File
21
16
import java.util.concurrent.ConcurrentHashMap
22
17
import java.util.concurrent.atomic.AtomicReference
23
18
24
- class StreamsConfig (private val log : Log , private val dbms : DatabaseManagementService ) {
19
+ class StreamsConfig (private val log : Log ) {
25
20
26
21
companion object {
27
22
private const val SUN_JAVA_COMMAND = " sun.java.command"
@@ -41,6 +36,8 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
41
36
const val POLL_INTERVAL = " streams.sink.poll.interval"
42
37
const val INSTANCE_WAIT_TIMEOUT = " streams.wait.timeout"
43
38
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
44
41
45
42
private const val DEFAULT_TRIGGER_PERIOD : Int = 10000
46
43
@@ -59,8 +56,8 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
59
56
60
57
fun getInstance (db : GraphDatabaseAPI ): StreamsConfig = cache.computeIfAbsent(StreamsUtils .getName(db)) {
61
58
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))
64
61
}
65
62
66
63
fun removeInstance (db : GraphDatabaseAPI ) {
@@ -83,11 +80,13 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
83
80
fun getSystemDbWaitTimeout (config : Map <String , Any ?>) = config.getOrDefault(SYSTEM_DB_WAIT_TIMEOUT , SYSTEM_DB_WAIT_TIMEOUT_VALUE ).toString().toLong()
84
81
85
82
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()
86
85
}
87
86
88
87
private val configLifecycle: ConfigurationLifecycle
89
88
90
- private enum class Status {RUNNING , STOPPED , CLOSED , UNKNOWN }
89
+ enum class Status {RUNNING , STARTING , STOPPED , CLOSED , UNKNOWN }
91
90
92
91
private val status = AtomicReference (Status .UNKNOWN )
93
92
@@ -100,37 +99,30 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
100
99
true , log, true , " streams." , " kafka." )
101
100
}
102
101
103
- fun start () = runBlocking {
102
+ fun start (db : GraphDatabaseAPI ) = runBlocking {
104
103
if (log.isDebugEnabled) {
105
104
log.debug(" Starting StreamsConfig" )
106
105
}
107
106
mutex.withLock {
108
- if (status.get() == Status . RUNNING ) return @runBlocking
107
+ if (setOf ( Status . RUNNING , Status . STARTING ).contains( status.get()) ) return @runBlocking
109
108
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() }
125
112
} else {
126
113
configStart()
127
114
}
128
115
} catch (e: Exception ) {
129
116
log.warn(" Cannot start StreamsConfig because of the following exception:" , e)
117
+ status.set(Status .UNKNOWN )
130
118
}
131
119
}
132
120
}
133
121
122
+ fun startEager () = runBlocking {
123
+ configStart()
124
+ }
125
+
134
126
private fun configStart () = try {
135
127
configLifecycle.start()
136
128
status.set(Status .RUNNING )
@@ -139,14 +131,16 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
139
131
log.error(" Cannot start the StreamsConfig because of the following exception" , e)
140
132
}
141
133
134
+ fun status (): Status = status.get()
135
+
142
136
fun stop (shutdown : Boolean = false) = runBlocking {
143
137
if (log.isDebugEnabled) {
144
138
log.debug(" Stopping StreamsConfig" )
145
139
}
146
140
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 )
150
144
}
151
145
}
152
146
@@ -201,10 +195,6 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
201
195
202
196
fun getConfiguration (): Map <String , Any > = ConfigurationLifecycleUtils .toMap(configLifecycle.configuration)
203
197
204
- fun defaultDbName () = this .dbms.getDefaultDbName()
205
-
206
- fun isDefaultDb (dbName : String ) = this .defaultDbName() == dbName
207
-
208
198
fun isSourceGloballyEnabled () = Companion .isSourceGloballyEnabled(getConfiguration())
209
199
210
200
fun isSourceEnabled (dbName : String ) = Companion .isSourceEnabled(getConfiguration(), dbName)
@@ -221,4 +211,6 @@ class StreamsConfig(private val log: Log, private val dbms: DatabaseManagementSe
221
211
222
212
fun getInstanceWaitTimeout () = Companion .getInstanceWaitTimeout(getConfiguration())
223
213
214
+ fun isWaitForAvailable () = Companion .isWaitForAvailable(getConfiguration())
215
+
224
216
}
0 commit comments