@@ -8,7 +8,6 @@ import org.neo4j.dbms.api.DatabaseManagementService
88import org.neo4j.graphdb.QueryExecutionException
99import org.neo4j.kernel.internal.GraphDatabaseAPI
1010import org.neo4j.logging.Log
11- import org.neo4j.logging.internal.LogService
1211import streams.extensions.execute
1312import java.lang.reflect.InvocationTargetException
1413import kotlin.streams.toList
@@ -26,7 +25,7 @@ object Neo4jUtils {
2625 return false
2726 }
2827
29- return availableAction() && getMemberRole (db).equals(StreamsUtils .LEADER , ignoreCase = true )
28+ return availableAction() && ProcedureUtils .clusterMemberRole (db).equals(StreamsUtils .LEADER , ignoreCase = true )
3029 } catch (e: QueryExecutionException ) {
3130 if (e.statusCode.equals(" Neo.ClientError.Procedure.ProcedureNotFound" , ignoreCase = true )) {
3231 return availableAction()
@@ -44,36 +43,7 @@ object Neo4jUtils {
4443 false
4544 }
4645
47- fun getLogService (db : GraphDatabaseAPI ): LogService = db.dependencyResolver
48- .resolveDependency(LogService ::class .java)
49-
50- fun isCluster (db : GraphDatabaseAPI , log : Log ? = null): Boolean {
51- try {
52- return db.execute(" CALL dbms.cluster.overview()" ) {
53- if (it.hasNext()) {
54- if (log?.isDebugEnabled == true ) {
55- log?.debug(it.resultAsString())
56- }
57- }
58- true
59- }
60- } catch (e: QueryExecutionException ) {
61- if (e.statusCode.equals(" Neo.ClientError.Procedure.ProcedureNotFound" , ignoreCase = true )) {
62- return false
63- }
64- throw e
65- }
66- }
67-
68- fun isCluster (dbms : DatabaseManagementService , log : Log ? = null): Boolean = dbms.listDatabases()
69- .firstOrNull { it != StreamsUtils .SYSTEM_DATABASE_NAME }
70- ?.let { dbms.database(it) as GraphDatabaseAPI }
71- ?.let { isCluster(it, log) } ? : false
72-
73- private fun getMemberRole (db : GraphDatabaseAPI ) = db.execute(" CALL dbms.cluster.role(\$ database)" ,
74- mapOf (" database" to db.databaseName())) { it.columnAs<String >(" role" ).next() }
75-
76- fun clusterHasLeader (db : GraphDatabaseAPI ): Boolean = try {
46+ private fun clusterHasLeader (db : GraphDatabaseAPI ): Boolean = try {
7747 db.execute("""
7848 |CALL dbms.cluster.overview() YIELD databases
7949 |RETURN databases[${' $' } database] AS role
@@ -98,34 +68,6 @@ object Neo4jUtils {
9868 null
9969 }
10070
101- fun executeInLeader (db : GraphDatabaseAPI ,
102- log : Log ,
103- timeout : Long = 120000,
104- availableAction : () -> Boolean ,
105- action : () -> Unit ) {
106- GlobalScope .launch(Dispatchers .IO ) {
107- val start = System .currentTimeMillis()
108- val delay: Long = 2000
109- while (! clusterHasLeader(db) && System .currentTimeMillis() - start < timeout) {
110- log.info(" ${StreamsUtils .LEADER } not found, new check comes in $delay milliseconds..." )
111- delay(delay)
112- }
113- executeInWriteableInstance(db, availableAction, action)
114- }
115- }
116-
117- fun waitForTheLeader (db : GraphDatabaseAPI , log : Log , timeout : Long = 120000, action : () -> Unit ) {
118- GlobalScope .launch(Dispatchers .IO ) {
119- val start = System .currentTimeMillis()
120- val delay: Long = 2000
121- while (! clusterHasLeader(db) && System .currentTimeMillis() - start < timeout) {
122- log.info(" ${StreamsUtils .LEADER } not found, new check comes in $delay milliseconds..." )
123- delay(delay)
124- }
125- action()
126- }
127- }
128-
12971 fun isClusterCorrectlyFormed (dbms : DatabaseManagementService ) = dbms.listDatabases()
13072 .filterNot { it == StreamsUtils .SYSTEM_DATABASE_NAME }
13173 .map { dbms.database(it) as GraphDatabaseAPI }
0 commit comments