bitflicker64 commented on code in PR #3351:
URL: https://github.com/apache/hugegraph/pull/3351#discussion_r4278837587


##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerLocalGraphBindTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.hugegraph.unit.core;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.configuration2.PropertiesConfiguration;
+import org.apache.commons.io.FileUtils;
+import org.apache.hugegraph.HugeFactory;
+import org.apache.hugegraph.HugeGraph;
+import org.apache.hugegraph.auth.ContextGremlinServer;
+import org.apache.hugegraph.config.HugeConfig;
+import org.apache.hugegraph.core.GraphManager;
+import org.apache.hugegraph.event.EventHub;
+import org.apache.hugegraph.event.EventListener;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.util.Events;
+import org.apache.tinkerpop.gremlin.server.Settings;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Gremlin Server opens conf/graphs on its own, once, and drops a graph whose
+ * open fails there. The REST load of the same graph has to offer it to
+ * Gremlin again, or that Server answers REST while every Gremlin call on the
+ * graph fails until it restarts (#3228).
+ */
+public class GraphManagerLocalGraphBindTest {
+
+    private File graphsDir;
+    private EventHub hub;
+    private GraphManager manager;
+    private ContextGremlinServer gremlin;
+    private List<String> created;
+    private EventListener listener;
+
+    @Before
+    public void setup() throws IOException {
+        this.graphsDir = 
Files.createTempDirectory("local-graph-bind").toFile();
+        this.hub = new EventHub("local-graph-bind-test");
+        this.manager = new GraphManager(
+                new HugeConfig(new PropertiesConfiguration()), this.hub);
+        this.created = new CopyOnWriteArrayList<>();
+        this.listener = event -> {
+            event.checkArgs(HugeGraph.class);
+            this.created.add(((HugeGraph) event.args()[0]).spaceGraphName());
+            return null;
+        };
+        this.hub.listen(Events.GRAPH_CREATE, this.listener);
+    }
+
+    @After
+    public void teardown() throws Exception {
+        List<HugeGraph> graphs = new ArrayList<>();
+        for (String name : this.manager.graphs()) {
+            graphs.add(this.manager.graph(name));
+        }
+        try {
+            if (this.gremlin != null) {
+                this.gremlin.stop().get(30, TimeUnit.SECONDS);
+            }
+            this.hub.unlisten(Events.GRAPH_CREATE, this.listener);
+            this.manager.close();
+        } finally {
+            graphs.forEach(HugeFactory::remove);
+            FileUtils.deleteQuietly(this.graphsDir);
+        }
+    }
+
+    @Test
+    public void testLocalLoadOffersGraphToGremlin() throws IOException {
+        String path = this.writeGraphConf("bind_local", "bind_local");
+
+        this.manager.loadGraphsFromLocal(ImmutableMap.of("bind_local", path));
+
+        Assert.assertEquals(1, this.created.size());
+        Assert.assertEquals("DEFAULT-bind_local", this.created.get(0));
+        Assert.assertNotNull(this.manager.graph("DEFAULT", "bind_local"));
+    }
+
+    @Test
+    public void testLoadingTwiceRebindsTheSameGraph() throws IOException {
+        String path = this.writeGraphConf("bind_twice", "bind_twice");
+        Map<String, String> confs = ImmutableMap.of("bind_twice", path);
+
+        this.manager.loadGraphsFromLocal(confs);
+        HugeGraph first = this.manager.graph("DEFAULT", "bind_twice");
+        this.manager.loadGraphsFromLocal(confs);
+
+        Assert.assertEquals(2, this.created.size());
+        Assert.assertSame(first, this.manager.graph("DEFAULT", "bind_twice"));
+    }
+
+    @Test
+    public void testGraphNamedDifferentlyByConfigIsNotRebound() throws 
IOException {
+        String path = this.writeGraphConf("bind_file", "bind_store");
+
+        this.manager.loadGraphsFromLocal(ImmutableMap.of("bind_file", path));
+
+        Assert.assertTrue(this.created.isEmpty());
+        Assert.assertNotNull(this.manager.graph("DEFAULT", "bind_file"));
+    }
+
+    @Test
+    public void testGraphMissingFromGremlinGetsBound() throws IOException {
+        String path = this.writeGraphConf("bind_missing", "bind_missing");
+        // No settings.graphs entry stands in for a failed static open
+        this.gremlin = new ContextGremlinServer(newSettings(), this.hub);
+        org.apache.tinkerpop.gremlin.server.GraphManager bindings =
+                this.gremlin.getServerGremlinExecutor().getGraphManager();
+        Assert.assertNull(bindings.getGraph("DEFAULT-bind_missing"));
+
+        this.manager.loadGraphsFromLocal(ImmutableMap.of("bind_missing", 
path));
+
+        Assert.assertSame(this.manager.graph("DEFAULT", "bind_missing"),
+                          bindings.getGraph("DEFAULT-bind_missing"));
+        
Assert.assertNotNull(bindings.getTraversalSource("__g_DEFAULT-bind_missing"));
+    }
+
+    @Test
+    public void testGraphOpenedByGremlinKeepsItsInstance() throws IOException {
+        String path = this.writeGraphConf("bind_static", "bind_static");
+        Settings settings = newSettings();
+        settings.graphs.put("DEFAULT-bind_static", path);
+        this.gremlin = new ContextGremlinServer(settings, this.hub);
+        org.apache.tinkerpop.gremlin.server.GraphManager bindings =
+                this.gremlin.getServerGremlinExecutor().getGraphManager();
+        Graph opened = bindings.getGraph("DEFAULT-bind_static");
+        Assert.assertNotNull(opened);
+
+        this.manager.loadGraphsFromLocal(ImmutableMap.of("bind_static", path));
+
+        Assert.assertSame(opened, bindings.getGraph("DEFAULT-bind_static"));

Review Comment:
   Important: this test never reaches `bindLocalGraph()`, so both assertions 
pass without any rebind. Gremlin opens `bind_static` from a path, which goes 
through `HugeFactory.open(Configuration)` to `new HugeConfig(Configuration)`, 
and that constructor leaves `configPath` null. The REST 
`GraphFactory.open(config)` then gets that cached instance back. Line 1827 of 
`GraphManager.loadGraph()`, `assert 
graphConfPath.equals(Objects.requireNonNull(graphConfig.file()).getPath())`, 
throws a NullPointerException, because surefire runs with assertions on. 
`loadGraphsFromLocal()` catches it and logs `Graph 'bind_static' can't be 
loaded`. Line 1824 had already put the graph into the REST map and Gremlin's 
binding was never touched, so line 160 and line 161 both hold. The exact-head 
`build-server / Server memory tests (Java 17)` job (run 38656802390, job 
116168796677) logs this ERROR with `java.lang.NullPointerException at 
GraphManager.loadGraph(GraphManager.java:1827)` called from `GraphManagerL
 
ocalGraphBindTest.testGraphOpenedByGremlinKeepsItsInstance(GraphManagerLocalGraphBindTest.java:158)`.
 That is why only 3 of the 5 tests fail when the `bindLocalGraph()` call is 
removed. Production startup runs without `-ea` and does reach the bind, but no 
test covers that path, which is the one the PR describes as harmless. Could you 
make that assert null-safe (skip the comparison when `graphConfig.file()` is 
null, since a graph Gremlin opened first never has a file), and have this test 
check that the rebind ran, for example 
`Assert.assertEquals(ImmutableList.of("DEFAULT-bind_static"), this.created)` 
plus a check that `__g_DEFAULT-bind_static` still resolves to a traversal of 
`opened`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to