Skip to content

Commit

Permalink
Run SHOW DATABASE on system and accept standalone.
Browse files Browse the repository at this point in the history
This is to maintain the old non-clustering behaviour, tested by TTLTest.
  • Loading branch information
Lojjs committed Apr 7, 2022
1 parent 780662a commit 49514d8
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions core/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
import java.util.zip.ZipInputStream;
import javax.lang.model.SourceVersion;

import org.neo4j.configuration.Config;
import org.neo4j.configuration.connectors.BoltConnector;
import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.GraphDatabaseService;
Expand All @@ -89,6 +92,7 @@
import static apoc.ApocConfig.apocConfig;
import static apoc.util.DateFormatUtil.getOrCreate;
import static java.lang.String.format;
import static org.neo4j.configuration.GraphDatabaseSettings.SYSTEM_DATABASE_NAME;

/**
* @author mh
Expand Down Expand Up @@ -695,24 +699,13 @@ public static String withMapping(Stream<String> columns, Function<String, String
return with.isEmpty() ? with : " WITH "+with+" ";
}

public static boolean isWriteableInstance(GraphDatabaseAPI db) {
try {
try {
Class hadb = Class.forName("org.neo4j.kernel.ha.HighlyAvailableGraphDatabase");
boolean isSlave = hadb.isInstance(db) && !((Boolean)hadb.getMethod("isMaster").invoke(db));
if (isSlave) return false;
} catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
/* ignore */
}

String role = db.executeTransactionally("CALL dbms.cluster.role($databaseName)",
Collections.singletonMap("databaseName", db.databaseName()),
result -> Iterators.single(result.columnAs("role")));
return role.equalsIgnoreCase("LEADER");
} catch(QueryExecutionException e) {
if (e.getStatusCode().equalsIgnoreCase("Neo.ClientError.Procedure.ProcedureNotFound")) return true;
throw e;
}
public static boolean isWriteableInstance( GraphDatabaseAPI db )
{
var socketAddress = db.getDependencyResolver().resolveDependency( Config.class ).get( BoltConnector.advertised_address ).toString();
GraphDatabaseService systemDb = db.getDependencyResolver().resolveDependency( DatabaseManagementService.class ).database( SYSTEM_DATABASE_NAME );
String role = systemDb.executeTransactionally( "SHOW DATABASE $databaseName WHERE address = $socketAddress",
Map.of( "databaseName", db.databaseName(), "socketAddress", socketAddress ), result -> Iterators.single( result.columnAs( "role" ) ) );
return role.equalsIgnoreCase( "LEADER" ) || role.equalsIgnoreCase( "standalone" );
}

/**
Expand Down

0 comments on commit 49514d8

Please sign in to comment.