Skip to content

Commit 97cdbb4

Browse files
committed
ConfigurationManagementGraph to throw Exception with custom ID setting
Fixes #4535 Signed-off-by: Boxuan Li <[email protected]>
1 parent ac7fddb commit 97cdbb4

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.janusgraph.graphdb.database.management.ManagementSystem;
3131
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphAlreadyInstantiatedException;
3232
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphNotEnabledException;
33+
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536

@@ -77,6 +78,11 @@ public class ConfigurationManagementGraph {
7778
// but for now, leaving open for testing purposes
7879
public ConfigurationManagementGraph(StandardJanusGraph graph) {
7980
initialize();
81+
if (graph.getConfiguration().allowVertexIdSetting()) {
82+
final String errMsg = "ConfigurationManagementGraph prohibits usage of custom vertex ID config. " +
83+
"Note: ConfigurationManagementGraph and actual graphs can have different configs.";
84+
throw new ConfigurationManagementGraphSettingException(errMsg);
85+
}
8086
this.graph = graph;
8187
createIndexIfDoesNotExist(GRAPH_NAME_INDEX, PROPERTY_GRAPH_NAME, String.class, true);
8288
createIndexIfDoesNotExist(TEMPLATE_INDEX, PROPERTY_TEMPLATE, Boolean.class, false);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2024 JanusGraph Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
package org.janusgraph.graphdb.management.utils;
17+
18+
public class ConfigurationManagementGraphSettingException extends RuntimeException {
19+
public ConfigurationManagementGraphSettingException(String message) {
20+
super(message);
21+
}
22+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2024 JanusGraph Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package org.janusgraph.core.inmemory;
16+
17+
import org.apache.commons.configuration2.MapConfiguration;
18+
import org.apache.tinkerpop.gremlin.server.Settings;
19+
import org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration;
20+
import org.janusgraph.graphdb.configuration.builder.GraphDatabaseConfigurationBuilder;
21+
import org.janusgraph.graphdb.database.StandardJanusGraph;
22+
import org.janusgraph.graphdb.management.ConfigurationManagementGraph;
23+
import org.janusgraph.graphdb.management.JanusGraphManager;
24+
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException;
25+
import org.janusgraph.util.system.ConfigurationUtil;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_CUSTOM_VERTEX_ID_TYPES;
32+
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_SETTING_VERTEX_ID;
33+
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.STORAGE_BACKEND;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
36+
/**
37+
* This class is dedicated to test a common configuration error people consistently make
38+
* when they aim to use ConfiguredGraphFactory: their management graph config contains
39+
* `graph.set-vertex-id=true` which leads to runtime error because ConfigurationManagementGraph
40+
* is managed by JanusGraph internally and doesn't support custom ID
41+
*/
42+
public class MisconfiguredGraphFactoryTest {
43+
44+
private static JanusGraphManager gm;
45+
46+
protected MapConfiguration getManagementConfig() {
47+
final Map<String, Object> map = new HashMap<>();
48+
map.put(STORAGE_BACKEND.toStringWithoutRoot(), "inmemory");
49+
50+
// management graph config is not allowed to use custom vertex id
51+
map.put(ALLOW_SETTING_VERTEX_ID.toStringWithoutRoot(), "true");
52+
map.put(ALLOW_CUSTOM_VERTEX_ID_TYPES.toStringWithoutRoot(), "true");
53+
return ConfigurationUtil.loadMapConfiguration(map);
54+
}
55+
56+
@Test
57+
public void shouldRejectInvalidConfig() throws Exception {
58+
gm = new JanusGraphManager(new Settings());
59+
final MapConfiguration config = getManagementConfig();
60+
final StandardJanusGraph graph = new StandardJanusGraph(new GraphDatabaseConfigurationBuilder().build(new CommonsConfiguration(config)));
61+
62+
// Cannot instantiate the ConfigurationManagementGraph Singleton because custom vertex ID is not allowed
63+
assertThrows(ConfigurationManagementGraphSettingException.class, () -> new ConfigurationManagementGraph(graph));
64+
}
65+
}
66+

0 commit comments

Comments
 (0)