Skip to content

Commit

Permalink
ConfigurationManagementGraph to throw Exception with custom ID setting
Browse files Browse the repository at this point in the history
Fixes JanusGraph#4535

Signed-off-by: Boxuan Li <[email protected]>
  • Loading branch information
li-boxuan committed Nov 11, 2024
1 parent ac7fddb commit 97cdbb4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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));
}
}

0 comments on commit 97cdbb4

Please sign in to comment.