Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #558: Check for neo4j version being compatible with neo version #2700

Merged
merged 4 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/RegisterComponentFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package apoc;

import apoc.trigger.TriggerHandler;
import org.neo4j.annotations.service.ServiceProvider;
import org.neo4j.kernel.api.procedure.GlobalProcedures;
import org.neo4j.kernel.extension.ExtensionFactory;
import org.neo4j.kernel.extension.ExtensionType;
Expand All @@ -17,14 +17,14 @@
/**
* NOTE: this is a GLOBAL component, so only once per DBMS
*/
@ServiceProvider
public class RegisterComponentFactory extends ExtensionFactory<RegisterComponentFactory.Dependencies> {

private Log log;
private GlobalProcedures globalProceduresRegistry;

public RegisterComponentFactory() {
super(ExtensionType.GLOBAL,
"ApocRegisterComponent");
super(ExtensionType.GLOBAL, "ApocRegisterComponent");
}

@Override
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/apoc/cypher/CypherInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import apoc.ApocConfig;
import apoc.util.Util;
import apoc.version.Version;
import org.apache.commons.configuration2.Configuration;
import org.neo4j.common.DependencyResolver;
import org.neo4j.configuration.Config;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.internal.helpers.collection.Iterators;
import org.neo4j.kernel.api.procedure.GlobalProcedures;
Expand All @@ -15,6 +17,7 @@
import java.util.Collections;

import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand All @@ -24,6 +27,7 @@ public class CypherInitializer implements AvailabilityListener {
private final Log userLog;
private final GlobalProcedures procs;
private final DependencyResolver dependencyResolver;
private final String defaultDb;

/**
* indicates the status of the initializer, to be used for tests to ensure initializer operations are already done
Expand All @@ -35,6 +39,7 @@ public CypherInitializer(GraphDatabaseAPI db, Log userLog) {
this.userLog = userLog;
this.dependencyResolver = db.getDependencyResolver();
this.procs = dependencyResolver.resolveDependency(GlobalProcedures.class);
this.defaultDb = dependencyResolver.resolveDependency(Config.class).get(GraphDatabaseSettings.default_database);
}

public boolean isFinished() {
Expand All @@ -57,6 +62,17 @@ public void available() {
if (!isSystemDatabase) {
awaitApocProceduresRegistered();
}

if (defaultDb.equals(db.databaseName())) {
final List<String> versions = db.executeTransactionally("CALL dbms.components", Collections.emptyMap(),
r -> (List<String>) r.next().get("versions"));
final String apocFullVersion = Version.class.getPackage().getImplementationVersion();
if (isVersionDifferent(versions, apocFullVersion)) {
userLog.warn("The apoc version (%s) and the Neo4j DBMS versions %s are incompatible. \n" +
"See the compatibility matrix in https://neo4j.com/labs/apoc/4.4/installation/ to see the correct version",
apocFullVersion, versions.toString());
}
}
Configuration config = dependencyResolver.resolveDependency(ApocConfig.class).getConfig();

for (String query : collectInitializers(isSystemDatabase, config)) {
Expand All @@ -75,6 +91,11 @@ public void available() {
}).start();
}

// only for testing purpose
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you refer to that it is public for testing purposes and could be private otherwise? In that case, maybe the comment can explicitly say so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. Added it and fixed the failed test as well

public static boolean isVersionDifferent(List<String> versions, String apocFullVersion) {
return versions.stream().noneMatch(apocFullVersion::startsWith);
}

private Collection<String> collectInitializers(boolean isSystemDatabase, Configuration config) {
Map<String, String> initializers = new TreeMap<>();

Expand Down
20 changes: 20 additions & 0 deletions core/src/test/java/apoc/cypher/CypherIsVersionDifferentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package apoc.cypher;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class CypherIsVersionDifferentTest {

@Test
public void shouldReturnFalseOnlyWithCompatibleVersion() {
assertTrue(CypherInitializer.isVersionDifferent(List.of("3.5"), "4.4.0.2"));
assertTrue(CypherInitializer.isVersionDifferent(List.of("5_0"), "4.4.0.2"));

// we expect that APOC versioning is always consistent to Neo4j versioning
assertFalse(CypherInitializer.isVersionDifferent(List.of("5_0"), "5_0_0_1"));
}
}
6 changes: 6 additions & 0 deletions docs/asciidoc/modules/ROOT/pages/installation/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ The version compatibility matrix explains the mapping between Neo4j and APOC ver

// end::version-matrix[]

If by mistake a jar not compatible with the neo4j version is inserted, a `log.warn` like this will appear in `neo4j.log`:
```
The apoc version (<APOC_VERSION>) and the Neo4j DBMS versions [NEO4J_VERSION] are incompatible.
See the compatibility matrix in https://neo4j.com/labs/apoc/4.4/installation/ to see the correct version
```

[[docker]]
== Docker

Expand Down