diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java index cc76af9af2..567c782e05 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java @@ -30,6 +30,7 @@ import org.janusgraph.graphdb.database.management.ManagementSystem; import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphAlreadyInstantiatedException; import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphNotEnabledException; +import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,6 +78,11 @@ public class ConfigurationManagementGraph { // but for now, leaving open for testing purposes public ConfigurationManagementGraph(StandardJanusGraph graph) { initialize(); + if (graph.getConfiguration().allowVertexIdSetting()) { + final String errMsg = "ConfigurationManagementGraph prohibits usage of custom vertex ID config. " + + "Note: ConfigurationManagementGraph and actual graphs can have different configs."; + throw new ConfigurationManagementGraphSettingException(errMsg); + } this.graph = graph; createIndexIfDoesNotExist(GRAPH_NAME_INDEX, PROPERTY_GRAPH_NAME, String.class, true); createIndexIfDoesNotExist(TEMPLATE_INDEX, PROPERTY_TEMPLATE, Boolean.class, false); diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/utils/ConfigurationManagementGraphSettingException.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/utils/ConfigurationManagementGraphSettingException.java new file mode 100644 index 0000000000..6df139e5e4 --- /dev/null +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/management/utils/ConfigurationManagementGraphSettingException.java @@ -0,0 +1,22 @@ +// Copyright 2024 JanusGraph Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +package org.janusgraph.graphdb.management.utils; + +public class ConfigurationManagementGraphSettingException extends RuntimeException { + public ConfigurationManagementGraphSettingException(String message) { + super(message); + } +} diff --git a/janusgraph-inmemory/src/test/java/org/janusgraph/core/inmemory/MisconfiguredGraphFactoryTest.java b/janusgraph-inmemory/src/test/java/org/janusgraph/core/inmemory/MisconfiguredGraphFactoryTest.java new file mode 100644 index 0000000000..0234d7bc3f --- /dev/null +++ b/janusgraph-inmemory/src/test/java/org/janusgraph/core/inmemory/MisconfiguredGraphFactoryTest.java @@ -0,0 +1,66 @@ +// Copyright 2024 JanusGraph Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.janusgraph.core.inmemory; + +import org.apache.commons.configuration2.MapConfiguration; +import org.apache.tinkerpop.gremlin.server.Settings; +import org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration; +import org.janusgraph.graphdb.configuration.builder.GraphDatabaseConfigurationBuilder; +import org.janusgraph.graphdb.database.StandardJanusGraph; +import org.janusgraph.graphdb.management.ConfigurationManagementGraph; +import org.janusgraph.graphdb.management.JanusGraphManager; +import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException; +import org.janusgraph.util.system.ConfigurationUtil; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_CUSTOM_VERTEX_ID_TYPES; +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_SETTING_VERTEX_ID; +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.STORAGE_BACKEND; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * This class is dedicated to test a common configuration error people consistently make + * when they aim to use ConfiguredGraphFactory: their management graph config contains + * `graph.set-vertex-id=true` which leads to runtime error because ConfigurationManagementGraph + * is managed by JanusGraph internally and doesn't support custom ID + */ +public class MisconfiguredGraphFactoryTest { + + private static JanusGraphManager gm; + + protected MapConfiguration getManagementConfig() { + final Map map = new HashMap<>(); + map.put(STORAGE_BACKEND.toStringWithoutRoot(), "inmemory"); + + // management graph config is not allowed to use custom vertex id + map.put(ALLOW_SETTING_VERTEX_ID.toStringWithoutRoot(), "true"); + map.put(ALLOW_CUSTOM_VERTEX_ID_TYPES.toStringWithoutRoot(), "true"); + return ConfigurationUtil.loadMapConfiguration(map); + } + + @Test + public void shouldRejectInvalidConfig() throws Exception { + gm = new JanusGraphManager(new Settings()); + final MapConfiguration config = getManagementConfig(); + final StandardJanusGraph graph = new StandardJanusGraph(new GraphDatabaseConfigurationBuilder().build(new CommonsConfiguration(config))); + + // Cannot instantiate the ConfigurationManagementGraph Singleton because custom vertex ID is not allowed + assertThrows(ConfigurationManagementGraphSettingException.class, () -> new ConfigurationManagementGraph(graph)); + } +} +