From f9523ab442485b3cc882e16de0783a3cbe4933c1 Mon Sep 17 00:00:00 2001 From: iSaber <1346959878@qq.com> Date: Tue, 17 Nov 2026 15:04:36 +0800 Subject: [PATCH] refactor(server): use typed graph registry keys - Introduce GraphKey for graph-space and graph-name identity. - Migrate registry and lifecycle tracking to typed keys. - Preserve string boundaries and the live graph-name view. - Add registry regressions and adapt existing lifecycle fixtures. --- .../org/apache/hugegraph/core/GraphKey.java | 47 ++++ .../apache/hugegraph/core/GraphManager.java | 237 ++++++++++-------- .../hugegraph/GraphDropPendingTest.java | 13 +- .../apache/hugegraph/unit/UnitTestSuite.java | 2 + .../unit/api/space/GraphSpaceAPITest.java | 15 +- .../unit/cache/RequestCacheLifetimeTest.java | 4 +- .../core/GraphManagerClearGraphSpaceTest.java | 15 +- .../unit/core/GraphManagerConfigTest.java | 72 +++--- .../unit/core/GraphManagerGraphKeyTest.java | 127 ++++++++++ 9 files changed, 369 insertions(+), 163 deletions(-) create mode 100644 hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphKey.java create mode 100644 hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerGraphKeyTest.java diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphKey.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphKey.java new file mode 100644 index 0000000000..caad1a90ac --- /dev/null +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphKey.java @@ -0,0 +1,47 @@ +/* + * 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.core; + +import java.util.Objects; + +/** + * Graph identity within the Server registry. Combined names remain in use at + * metadata, RPC and Gremlin boundaries. + */ +public record GraphKey(String graphSpace, String graphName) { + + public GraphKey { + Objects.requireNonNull(graphSpace, "graphSpace"); + Objects.requireNonNull(graphName, "graphName"); + } + + public static GraphKey fromString(String name) { + // Graph names cannot contain '-', so the last separator belongs to the + // graph name even when a graph-space name contains separators. + int separator = name.lastIndexOf(GraphManager.DELIMITER); + if (separator < 0) { + // An unqualified name must not resolve to a graph in DEFAULT. + return new GraphKey("", name); + } + return new GraphKey(name.substring(0, separator), name.substring(separator + 1)); + } + + public String spaceGraphName() { + return this.graphSpace + GraphManager.DELIMITER + this.graphName; + } +} diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java index 8781c86001..6100b207de 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java @@ -25,6 +25,7 @@ import java.io.StringWriter; import java.text.ParseException; import java.util.AbstractMap; +import java.util.AbstractSet; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -32,6 +33,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -175,16 +177,15 @@ public final class GraphManager { // Guarded by the graphSpaces monitor; advances even when PUT mutates the same cached object. private long graphSpaceCacheVersion; private final Map services; - //FIXME: add one class like graphKey as key,which contains graphSpace and graphName - private final Map graphs; + private final Map graphs; private final Set localGraphs; - private final Set removingGraphs; + private final Set removingGraphs; // Guarded by this manager monitor; placement-event tokens are consumed independently. private final Set rpcRemovingOwners; - private final Set pendingGraphAdds; + private final Set pendingGraphAdds; private final Set failedPeerRemovals; - private final Map clearedGraphs; - private final Set creatingGraphs; + private final Map clearedGraphs; + private final Set creatingGraphs; private final HugeAuthenticator authenticator; private final AuthManager authManager; private final MetaManager metaManager = MetaManager.instance(); @@ -229,7 +230,6 @@ public GraphManager(HugeConfig conf, EventHub hub) { this.cluster = conf.get(ServerOptions.CLUSTER); this.graphSpaces = new ConcurrentHashMap<>(); this.services = new ConcurrentHashMap<>(); - // key is graphSpaceName + "-" + graphName this.graphs = new ConcurrentHashMap<>(); this.removingGraphs = ConcurrentHashMap.newKeySet(); this.rpcRemovingOwners = Collections.newSetFromMap(new IdentityHashMap<>()); @@ -720,9 +720,8 @@ public void init() { public void reload() { // Remove graphs from GraphManager - for (String graph : this.graphs.keySet()) { - String[] parts = graph.split(DELIMITER); - this.dropGraph(parts[0], parts[1], false, false); + for (GraphKey graph : this.graphs.keySet()) { + this.dropGraph(graph.graphSpace(), graph.graphName(), false, false); } int count = 0; while (!this.graphs.isEmpty() && count++ < 10) { @@ -1131,10 +1130,9 @@ public void getSpaceStorage(String graphSpace) { public void clearGraphSpace(String name) { // Clear all graphs - for (String key : this.graphs.keySet()) { - if (key.startsWith(name + DELIMITER)) { - String[] parts = key.split(DELIMITER); - this.dropGraph(parts[0], parts[1], true); + for (GraphKey key : this.graphs.keySet()) { + if (key.graphSpace().equals(name)) { + this.dropGraph(key.graphSpace(), key.graphName(), true); } } @@ -1332,7 +1330,7 @@ public HugeGraph cloneGraph(String graphspace, String name, String newName, Map< "The new graph name can't be null or empty"); String newGraphKey = spaceGraphName(graphspace, newName); - E.checkArgument(!this.graphs.containsKey(newGraphKey), + E.checkArgument(!this.graphs.containsKey(new GraphKey(graphspace, newName)), "The graph '%s' has existed in graphspace '%s'", newName, graphspace); // Get source graph configuration @@ -1420,7 +1418,7 @@ private static void checkCopiedSchema(Collection elemen private void loadGraph(Map> graphConfigs) { // Load graph for (Map.Entry> conf : graphConfigs.entrySet()) { - String[] parts = conf.getKey().split(DELIMITER); + GraphKey key = GraphKey.fromString(conf.getKey()); // When server registered graph space is not DEFAULT, only load graphs under its // registered graph space if (this.filterLoadGraphByServiceGraphSpace(conf.getKey())) { @@ -1432,9 +1430,9 @@ private void loadGraph(Map> graphConfigs) { Date createTime = parseDate(config.get("create_time")); Date updateTime = parseDate(config.get("update_time")); - HugeFactory.checkGraphName(parts[1], "meta server"); + HugeFactory.checkGraphName(key.graphName(), "meta server"); try { - HugeGraph graph = this.createGraph(parts[0], parts[1], + HugeGraph graph = this.createGraph(key.graphSpace(), key.graphName(), creator, config, false); graph.createTime(createTime); graph.updateTime(updateTime); @@ -1443,7 +1441,7 @@ private void loadGraph(Map> graphConfigs) { throw e; } LOG.error(String.format("Failed to load graph '%s' from " + - "meta server", parts[1]), e); + "meta server", key.graphName()), e); } } } @@ -1478,14 +1476,14 @@ private void loadGraphsFromMeta( } private boolean filterLoadGraphByServiceGraphSpace(String key) { - String[] parts = key.split(DELIMITER); + GraphKey graphKey = GraphKey.fromString(key); // server 注册的图空间不为 DEFAULT 时,只加载其注册的图空间下的图 if (!"DEFAULT".equals(this.serviceGraphSpace) && - !this.serviceGraphSpace.equals(parts[0])) { + !this.serviceGraphSpace.equals(graphKey.graphSpace())) { LOG.warn(String.format("Load graph [%s] was discarded, due to the graph " + "space [%s] registered by the current server does " + "not match [%s].", key, - this.serviceGraphSpace, parts[0])); + this.serviceGraphSpace, graphKey.graphSpace())); return true; } return false; @@ -1503,8 +1501,8 @@ private void checkOptions(HugeConfig config) { private void checkOptionUnique(HugeConfig config, TypedOption option) { Object incomingValue = config.get(option); - for (String graphName : this.graphs.keySet()) { - HugeGraph graph = this.graph(graphName); + for (GraphKey graphKey : this.graphs.keySet()) { + HugeGraph graph = this.graph(graphKey); assert graph != null; Object existedValue = graph.option(option); E.checkArgument(!incomingValue.equals(existedValue), @@ -1547,7 +1545,7 @@ private HugeGraph createGraphLocal(HugeConfig config, String name) { name, e.getMessage(), e); if (graph != null) { synchronized (this) { - if (this.graphs.get(graph.spaceGraphName()) == graph) { + if (this.graphs.get(GraphKey.fromString(graph.spaceGraphName())) == graph) { this.rpcRemovingOwners.add(graph); } } @@ -1556,15 +1554,15 @@ private HugeGraph createGraphLocal(HugeConfig config, String name) { HugeGraph candidate = graph; HugeGraphAuthProxy.runAsAdmin(() -> this.dropGraphLocal(candidate)); synchronized (this) { - this.graphs.remove(graph.spaceGraphName(), graph); + this.graphs.remove(GraphKey.fromString(graph.spaceGraphName()), graph); this.rpcRemovingOwners.remove(graph); } this.notifyEventLenient(Events.GRAPH_DROP, graph); } catch (Throwable cleanup) { e.addSuppressed(cleanup); synchronized (this) { - this.graphs.putIfAbsent(graph.spaceGraphName(), graph); - if (this.graphs.get(graph.spaceGraphName()) == graph) { + this.graphs.putIfAbsent(GraphKey.fromString(graph.spaceGraphName()), graph); + if (this.graphs.get(GraphKey.fromString(graph.spaceGraphName())) == graph) { this.rpcRemovingOwners.add(graph); } } @@ -1628,9 +1626,9 @@ public HugeGraph createGraph(String graphSpace, String name, String creator, "accepted", this.serviceGraphSpace, graphSpace)); } - String key = String.join(DELIMITER, graphSpace, name); + GraphKey key = new GraphKey(graphSpace, name); if (this.graphs.containsKey(key)) { - throw new ExistedException("graph", key); + throw new ExistedException("graph", key.spaceGraphName()); } boolean grpcThread = Thread.currentThread().getName().contains("grpc"); E.checkArgumentNotNull(name, "The graph name can't be null"); @@ -1716,7 +1714,7 @@ public HugeGraph createGraph(String graphSpace, String name, String creator, } if (init) { - this.creatingGraphs.add(graphName); + this.creatingGraphs.add(key); this.metaManager.addGraphConfig(graphSpace, name, configs); this.metaManager.notifyGraphAdd(graphSpace, name); } @@ -1738,11 +1736,33 @@ public HugeGraph createGraph(String graphSpace, String name, String creator, } public Set graphs() { - return Collections.unmodifiableSet(this.graphs.keySet()); + // Preserve the unmodifiable live view exposed to existing callers. + return Collections.unmodifiableSet(new AbstractSet() { + @Override + public Iterator iterator() { + return GraphManager.this.graphs.keySet().stream() + .map(GraphKey::spaceGraphName).iterator(); + } + + @Override + public int size() { + return GraphManager.this.graphs.size(); + } + + @Override + public boolean contains(Object value) { + return value instanceof String name && + GraphManager.this.graphs.containsKey(GraphKey.fromString(name)); + } + }); } public HugeGraph graph(String spaceGraphName) { - Graph graph = this.graphs.get(spaceGraphName); + return this.graph(GraphKey.fromString(spaceGraphName)); + } + + private HugeGraph graph(GraphKey key) { + Graph graph = this.graphs.get(key); if (graph == null) { return null; } else if (graph instanceof HugeGraph matchedHugeGraph) { @@ -1810,7 +1830,7 @@ public GlobalMasterInfo globalNodeRoleInfo() { } public void close() { - for (Map.Entry entry : this.graphs.entrySet()) { + for (Map.Entry entry : this.graphs.entrySet()) { Graph graph = entry.getValue(); try { graph.close(); @@ -1930,8 +1950,9 @@ private void closeTx(final Set graphSourceNamesToCloseTxOn, final Set graphsToCloseTxOn = new HashSet<>(); graphSourceNamesToCloseTxOn.forEach(name -> { - if (this.graphs.containsKey(name)) { - graphsToCloseTxOn.add(this.graphs.get(name)); + GraphKey key = GraphKey.fromString(name); + if (this.graphs.containsKey(key)) { + graphsToCloseTxOn.add(this.graphs.get(key)); } }); @@ -1987,8 +2008,9 @@ private void loadGraph(String name, String graphConfPath) { } private void rollbackGraph(String graphName, HugeGraph graph, Throwable failure) { + GraphKey key = GraphKey.fromString(graphName); synchronized (this) { - if (this.graphs.get(graphName) == graph) { + if (this.graphs.get(key) == graph) { this.rpcRemovingOwners.add(graph); } } @@ -2003,7 +2025,7 @@ private void rollbackGraph(String graphName, HugeGraph graph, Throwable failure) this.notifyEventLenient(Events.GRAPH_DROP, graph); synchronized (this) { HugeFactory.remove(graph); - this.graphs.remove(graphName, graph); + this.graphs.remove(key, graph); this.rpcRemovingOwners.remove(graph); } } catch (Throwable cleanup) { @@ -2012,8 +2034,8 @@ private void rollbackGraph(String graphName, HugeGraph graph, Throwable failure) } // Pending owners must keep their graph/factory identity for cleanup retry. synchronized (this) { - this.graphs.putIfAbsent(graphName, graph); - if (this.graphs.get(graphName) == graph) { + this.graphs.putIfAbsent(key, graph); + if (this.graphs.get(key) == graph) { this.rpcRemovingOwners.add(graph); } } @@ -2064,7 +2086,7 @@ private void waitGraphsReady() { return; } com.alipay.remoting.rpc.RpcServer remotingRpcServer = this.remotingRpcServer(); - for (String graphName : this.graphs.keySet()) { + for (GraphKey graphName : this.graphs.keySet()) { HugeGraph graph = this.graph(graphName); graph.waitReady(remotingRpcServer); } @@ -2184,11 +2206,12 @@ private void publishGraph(HugeGraph graph) { } private synchronized void publishGraph(String name, HugeGraph graph) { - if (this.graphs.get(name) == graph) { + GraphKey key = GraphKey.fromString(name); + if (this.graphs.get(key) == graph) { return; } this.registerRpcServices(graph); - this.graphs.put(name, graph); + this.graphs.put(key, graph); } private void registerRpcServices(HugeGraph graph) { @@ -2213,13 +2236,13 @@ private void unregisterRpcServices(HugeGraph graph) { } private synchronized boolean unpublishGraph(HugeGraph graph) { - String name = graph.spaceGraphName(); - if (this.graphs.get(name) != graph) { + GraphKey key = GraphKey.fromString(graph.spaceGraphName()); + if (this.graphs.get(key) != graph) { return false; } // A failed cleanup keeps the owner published so that drop can retry. this.unregisterRpcServices(graph); - boolean removed = this.graphs.remove(name, graph); + boolean removed = this.graphs.remove(key, graph); if (removed) { this.rpcRemovingOwners.remove(graph); } @@ -2333,7 +2356,7 @@ private HugeGraph createGraph(String graphSpace, HugeConfig config, HugeFactory.remove(graph); } catch (Exception e1) { e.addSuppressed(e1); - this.graphs.putIfAbsent(graph.spaceGraphName(), graph); + this.graphs.putIfAbsent(GraphKey.fromString(graph.spaceGraphName()), graph); } throw e; } @@ -2446,7 +2469,7 @@ public Set getServiceUrls(String graphSpace, String service, } public HugeGraph graph(String graphSpace, String name) { - String key = String.join(DELIMITER, graphSpace, name); + GraphKey key = new GraphKey(graphSpace, name); Graph graph = this.graphs.get(key); if (graph == null) { if (isPDEnabled()) { @@ -2454,12 +2477,12 @@ public HugeGraph graph(String graphSpace, String name) { this.metaManager.graphConfigs(graphSpace); // If current server registered graph space is not DEFAULT, only load graph creation // under registered graph space - if (!configs.containsKey(key) || + if (!configs.containsKey(key.spaceGraphName()) || (!"DEFAULT".equals(this.serviceGraphSpace) && !graphSpace.equals(this.serviceGraphSpace))) { return null; } - Map config = configs.get(key); + Map config = configs.get(key.spaceGraphName()); String creator = String.valueOf(config.get("creator")); Date createTime = parseDate(config.get("create_time")); Date updateTime = parseDate(config.get("update_time")); @@ -2519,8 +2542,9 @@ private void dropGraph(String graphSpace, String name, boolean clear, boolean re } private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph g, boolean replayPendingAdd) { + GraphKey key = new GraphKey(graphSpace, name); synchronized (this) { - if (g == null || this.graphs.get(spaceGraphName(graphSpace, name)) != g) { + if (g == null || this.graphs.get(key) != g) { return; } } @@ -2532,14 +2556,14 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph "want to delete it.", name); } - String graphName = spaceGraphName(graphSpace, name); - if (clear && g.closed() && this.clearedGraphs.get(graphName) != g) { + String graphName = key.spaceGraphName(); + if (clear && g.closed() && this.clearedGraphs.get(key) != g) { // A non-clearing close cannot certify that destructive deletion completed. throw new HugeException("Graph '%s' is closed without a successful backend clear; " + "retain its owner and complete backend cleanup before deletion", graphName); } // The same owner may await close after clear succeeded, even while a foreign tx remains open. - if (clear && !g.closed() && this.clearedGraphs.get(graphName) != g) { + if (clear && !g.closed() && this.clearedGraphs.get(key) != g) { // The auth proxy checks ADMIN before the local deletion fence. TaskScheduler scheduler = g.taskScheduler(); if (!scheduler.close()) { @@ -2559,7 +2583,7 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph synchronized (this) { this.rpcRemovingOwners.add(g); this.unregisterRpcServices(g); - this.removingGraphs.add(graphName); + this.removingGraphs.add(key); } boolean metadataRemoved = false; try { @@ -2571,9 +2595,9 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph synchronized (this) { if (!metadataRemoved) { // No local REMOVE was sent; a later peer REMOVE must still be handled. - this.removingGraphs.remove(graphName); + this.removingGraphs.remove(key); } - if (this.graphs.get(graphName) == g && !g.closed()) { + if (this.graphs.get(key) == g && !g.closed()) { try { this.registerRpcServices(g); this.rpcRemovingOwners.remove(g); @@ -2590,8 +2614,8 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph g.clearBackend(); synchronized (this) { - if (this.graphs.get(graphName) == g) { - this.clearedGraphs.put(graphName, g); + if (this.graphs.get(key) == g) { + this.clearedGraphs.put(key, g); } } } @@ -2604,7 +2628,7 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph // Serialize with publication, but leave graph close outside the manager gate. boolean replayAdd = false; synchronized (this) { - if (this.graphs.get(graphName) == g) { + if (this.graphs.get(key) == g) { this.unregisterRpcServices(g); GraphSpace gs = this.graphSpace(graphSpace); if (!grpcThread) { @@ -2623,17 +2647,17 @@ private void dropGraph(String graphSpace, String name, boolean clear, HugeGraph } // Remove the factory binding before permitting same-name creation. HugeFactory.remove(g); - this.graphs.remove(graphName, g); + this.graphs.remove(key, g); this.rpcRemovingOwners.remove(g); this.failedPeerRemovals.remove(g); if (replayPendingAdd) { - replayAdd = this.pendingGraphAdds.contains(graphName); + replayAdd = this.pendingGraphAdds.contains(key); } else { // Reload loads all current metadata after every old owner has been removed. - this.pendingGraphAdds.remove(graphName); + this.pendingGraphAdds.remove(key); } - if (this.clearedGraphs.get(graphName) == g) { - this.clearedGraphs.remove(graphName); + if (this.clearedGraphs.get(key) == g) { + this.clearedGraphs.remove(key); } } } @@ -2665,10 +2689,10 @@ private void checkOptionsUnique(String graphSpace, HugeConfig config, TypedOption option) { Object incomingValue = config.get(option); - for (Map.Entry entry : this.graphs.entrySet()) { - String[] parts = entry.getKey().split(DELIMITER); - if (!Objects.equals(graphSpace, parts[0]) || - !Objects.equals(incomingValue, parts[1])) { + for (Map.Entry entry : this.graphs.entrySet()) { + GraphKey key = entry.getKey(); + if (!Objects.equals(graphSpace, key.graphSpace()) || + !Objects.equals(incomingValue, key.graphName())) { continue; } Object existedValue = ((HugeGraph) entry.getValue()).option(option); @@ -2682,25 +2706,23 @@ public Set graphs(String graphSpace) { Set graphs = new HashSet<>(); if (!isPDEnabled()) { - for (String key : this.graphs.keySet()) { - String[] parts = key.split(DELIMITER); - if (parts[0].equals(graphSpace)) { - graphs.add(parts[1]); + for (GraphKey key : this.graphs.keySet()) { + if (key.graphSpace().equals(graphSpace)) { + graphs.add(key.graphName()); } } return graphs; } for (String key : this.metaManager.graphConfigs(graphSpace).keySet()) { - graphs.add(key.split(DELIMITER)[1]); + graphs.add(GraphKey.fromString(key).graphName()); } if (DEFAULT_GRAPH_SPACE_SERVICE_NAME.equals(graphSpace)) { - String prefix = spaceGraphName(graphSpace, ""); - for (String key : this.graphs.keySet()) { - if (!key.startsWith(prefix)) { + for (GraphKey key : this.graphs.keySet()) { + if (!key.graphSpace().equals(graphSpace)) { continue; } - String graph = key.substring(prefix.length()); + String graph = key.graphName(); if (SYS_GRAPH.equals(graph)) { continue; } @@ -2778,10 +2800,9 @@ public boolean isExistedGraphNickname(String graphSpace, String nickname) { if (!isPDEnabled()) { // In non-PD mode, metaManager is not initialized. // Check nickname against in-memory graphs (same pattern as graphs()). - for (Map.Entry entry : this.graphs.entrySet()) { - String key = entry.getKey(); - String[] parts = key.split(DELIMITER, 2); - if (parts.length == 2 && parts[0].equals(graphSpace) && + for (Map.Entry entry : this.graphs.entrySet()) { + GraphKey key = entry.getKey(); + if (key.graphSpace().equals(graphSpace) && entry.getValue() instanceof HugeGraph) { HugeGraph hg = (HugeGraph) entry.getValue(); if (nickname.equals(hg.nickname())) { @@ -2959,15 +2980,15 @@ private void graphAddHandler(T response) { List names = this.metaManager .extractGraphsFromResponse(response); for (String graphName : names) { - String[] parts = graphName.split(DELIMITER); - if (parts.length < 2) { + GraphKey key = GraphKey.fromString(graphName); + if (key.graphSpace().isEmpty() || key.graphName().isEmpty()) { LOG.error("The graph name format is incorrect: {}", graphName); continue; } // If the current server is not registered to the DEFAULT schema, // it will only receive graph creation events under the registered schemas. if (!"DEFAULT".equals(this.serviceGraphSpace) && - !parts[0].equals(this.serviceGraphSpace)) { + !key.graphSpace().equals(this.serviceGraphSpace)) { LOG.warn(String.format("Listen event: graph [%s] add was discarded because " + "it did not belong to the graph space [%s] " + "registered by the current server", @@ -2979,15 +3000,15 @@ private void graphAddHandler(T response) { LOG.info("Accept graph add signal from etcd for {}", graphName); HugeGraph retryOwner = null; synchronized (this) { - HugeGraph owner = (HugeGraph) this.graphs.get(graphName); + HugeGraph owner = (HugeGraph) this.graphs.get(key); boolean retryPeerCleanup = owner != null && this.failedPeerRemovals.contains(owner); - if (this.creatingGraphs.remove(graphName) && !retryPeerCleanup) { + if (this.creatingGraphs.remove(key) && !retryPeerCleanup) { // The acknowledgement of local creation is not a replacement ADD. continue; } if (owner != null) { // Independent key watches can deliver a replacement ADD before the old REMOVE. - this.pendingGraphAdds.add(graphName); + this.pendingGraphAdds.add(key); if (!retryPeerCleanup) { continue; } @@ -2997,7 +3018,7 @@ private void graphAddHandler(T response) { if (retryOwner != null) { try { // Retry the exact peer owner; a concurrent replacement must never be closed here. - this.dropGraph(parts[0], parts[1], false, retryOwner, true); + this.dropGraph(key.graphSpace(), key.graphName(), false, retryOwner, true); } catch (HugeException failure) { LOG.error("Failed to retry peer cleanup for graph '{}'", graphName, failure); } @@ -3015,16 +3036,16 @@ private void graphAddHandler(T response) { } private void loadAddedGraph(String graphName) { - String[] parts = graphName.split(DELIMITER); + GraphKey key = GraphKey.fromString(graphName); LOG.info("Not exist in cache, Starting construct graph {}", graphName); - Map config = this.metaManager.getGraphConfig(parts[0], parts[1]); + Map config = this.metaManager.getGraphConfig(key.graphSpace(), key.graphName()); HugeGraph graph = null; if (config != null) { Object objc = config.get("creator"); String creator = null == objc ? GraphSpace.DEFAULT_CREATOR_NAME : String.valueOf(objc); // Create graph without init; reread metadata instead of keeping an old event's configuration. - graph = this.createGraph(parts[0], parts[1], creator, config, false); - LOG.info("Add graph space:{} graph:{}", parts[0], parts[1]); + graph = this.createGraph(key.graphSpace(), key.graphName(), creator, config, false); + LOG.info("Add graph space:{} graph:{}", key.graphSpace(), key.graphName()); graph.started(true); if (graph.tx().isOpen()) { graph.tx().close(); @@ -3033,10 +3054,10 @@ private void loadAddedGraph(String graphName) { LOG.error("The graph config not exist: {}", graphName); } synchronized (this) { - if (this.graphs.get(graphName) == graph && + if (this.graphs.get(key) == graph && (graph == null || (!graph.closed() && !this.rpcRemovingOwners.contains(graph)))) { // Do not consume a notification newly retained by another cleanup owner. - this.pendingGraphAdds.remove(graphName); + this.pendingGraphAdds.remove(key); } } } @@ -3060,24 +3081,24 @@ private void graphRemoveHandler(T response) { List graphNames = this.metaManager .extractGraphsFromResponse(response); for (String graphName : graphNames) { - if (!this.graphs.containsKey(graphName) || - this.removingGraphs.contains(graphName)) { - this.removingGraphs.remove(graphName); + GraphKey key = GraphKey.fromString(graphName); + if (!this.graphs.containsKey(key) || + this.removingGraphs.contains(key)) { + this.removingGraphs.remove(key); continue; } // Remove graph without clear - String[] parts = graphName.split(DELIMITER); - if (parts.length < 2) { + if (key.graphSpace().isEmpty() || key.graphName().isEmpty()) { LOG.error("The graph name format is incorrect: {}", graphName); continue; } - HugeGraph owner = (HugeGraph) this.graphs.get(graphName); + HugeGraph owner = (HugeGraph) this.graphs.get(key); try { - this.dropGraph(parts[0], parts[1], false, owner, true); + this.dropGraph(key.graphSpace(), key.graphName(), false, owner, true); } catch (HugeException e) { synchronized (this) { - if (this.graphs.get(graphName) == owner) { + if (this.graphs.get(key) == owner) { this.failedPeerRemovals.add(owner); } } @@ -3091,8 +3112,9 @@ private void graphUpdateHandler(T response) { List graphNames = this.metaManager .extractGraphsFromResponse(response); for (String graphName : graphNames) { - if (this.graphs.containsKey(graphName)) { - Graph graph = this.graphs.get(graphName); + GraphKey key = GraphKey.fromString(graphName); + if (this.graphs.containsKey(key)) { + Graph graph = this.graphs.get(key); if (graph instanceof HugeGraph hugeGraph) { String[] values = graphName.split(MetaManager.META_PATH_JOIN); @@ -3122,8 +3144,9 @@ private void graphClearHandler(T response) { List graphNames = this.metaManager .extractGraphsFromResponse(response); for (String graphName : graphNames) { - if (this.graphs.containsKey(graphName)) { - Graph graph = this.graphs.get(graphName); + GraphKey key = GraphKey.fromString(graphName); + if (this.graphs.containsKey(key)) { + Graph graph = this.graphs.get(key); if (graph instanceof HugeGraph hugeGraph) { ((AbstractBackendStoreProvider) hugeGraph.storeProvider()) .notifyAndWaitEvent(Events.STORE_CLEAR); diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/GraphDropPendingTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/GraphDropPendingTest.java index a7a0c57125..71f3ea68a1 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/GraphDropPendingTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/GraphDropPendingTest.java @@ -33,6 +33,7 @@ import org.apache.hugegraph.config.CoreOptions; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.ServerOptions; +import org.apache.hugegraph.core.GraphKey; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.dist.RegisterUtil; import org.apache.hugegraph.event.EventHub; @@ -120,7 +121,7 @@ public void testConfiguredWaitAllowsShortTaskToFinishBeforePdDrop() throws Excep }).when(scheduler).close(); HugeGraph proxy = Mockito.mock(HugeGraph.class, org.mockito.AdditionalAnswers.delegatesTo(fixture.graph)); Mockito.doReturn(scheduler).when(proxy).taskScheduler(); - fixture.graphs.put(fixture.graph.spaceGraphName(), proxy); + fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy); ExecutorService caller = Executors.newSingleThreadExecutor(); try { Future deletion = caller.submit(fixture::drop); @@ -134,7 +135,7 @@ public void testConfiguredWaitAllowsShortTaskToFinishBeforePdDrop() throws Excep job.release.countDown(); caller.shutdown(); Assert.assertTrue(caller.awaitTermination(10L, TimeUnit.SECONDS)); - fixture.graphs.replace(fixture.graph.spaceGraphName(), proxy, fixture.graph); + fixture.graphs.replace(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy, fixture.graph); } } } @@ -147,7 +148,7 @@ public void testSchedulerCloseFailureDoesNotRemovePdMetadata() throws Exception TaskScheduler failed = Mockito.spy(fixture.scheduler); Mockito.doThrow(new IllegalStateException("scheduler close failed")).when(failed).close(); Mockito.doReturn(failed).when((HugeGraph) proxy).taskScheduler(); - fixture.graphs.put(fixture.graph.spaceGraphName(), proxy); + fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), proxy); try { RuntimeException error = Assert.assertThrows(RuntimeException.class, fixture::drop); Assert.assertContains("scheduler close failed", error.getMessage()); @@ -155,7 +156,7 @@ public void testSchedulerCloseFailureDoesNotRemovePdMetadata() throws Exception Assert.assertFalse(fixture.graph.closed()); Assert.assertTrue(Files.exists(fixture.file)); } finally { - fixture.graphs.put(fixture.graph.spaceGraphName(), fixture.graph); + fixture.graphs.put(GraphKey.fromString(fixture.graph.spaceGraphName()), fixture.graph); } } } @@ -189,7 +190,7 @@ private static final class Fixture implements AutoCloseable { private final Path directory; private final Path file; private final GraphManager manager; - private final Map graphs; + private final Map graphs; private final HugeGraph graph; private final HugeGraph neighbor; private final HugeGraphParams params; @@ -259,7 +260,7 @@ private HugeGraph open(String name) throws Exception { result.addVertex(T.id, "kept", T.label, "person"); result.tx().commit(); ((HugeGraphParams) Whitebox.getInternalState(result, "params")).closeTx(); - this.graphs.put(result.spaceGraphName(), result); + this.graphs.put(GraphKey.fromString(result.spaceGraphName()), result); return result; } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java index e3801ba49a..654ede7e28 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java @@ -92,6 +92,7 @@ import org.apache.hugegraph.unit.core.GraphManagerAdminInitTest; import org.apache.hugegraph.unit.core.GraphManagerClearGraphSpaceTest; import org.apache.hugegraph.unit.core.GraphManagerConfigTest; +import org.apache.hugegraph.unit.core.GraphManagerGraphKeyTest; import org.apache.hugegraph.unit.core.GraphManagerLocalGraphBindTest; import org.apache.hugegraph.unit.core.GraphManagerServiceNamesTest; import org.apache.hugegraph.unit.core.GroovyScriptEngineCompatibilityTest; @@ -252,6 +253,7 @@ GraphManagerAdminInitTest.class, GraphManagerClearGraphSpaceTest.class, GraphManagerConfigTest.class, + GraphManagerGraphKeyTest.class, GraphManagerLocalGraphBindTest.class, GraphManagerServiceNamesTest.class, HstoreSessionsTest.class, diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/GraphSpaceAPITest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/GraphSpaceAPITest.java index 6783ff2b7e..9ac57e9f54 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/GraphSpaceAPITest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/GraphSpaceAPITest.java @@ -43,6 +43,7 @@ import org.apache.hugegraph.auth.ResourceType; import org.apache.hugegraph.auth.HugeUser; import org.apache.hugegraph.auth.RolePermission; +import org.apache.hugegraph.core.GraphKey; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.config.HugeConfig; import org.apache.hugegraph.config.ServerOptions; @@ -252,9 +253,9 @@ public void testPdGraphsIncludesLoadedLocalConfigGraphs() { .thenReturn(Collections.emptyMap()); Whitebox.setInternalState(manager, "metaManager", metaManager); - Map graphs = new ConcurrentHashMap<>(); - graphs.put("DEFAULT-hugegraph", Mockito.mock(Graph.class)); - graphs.put("DEFAULT-" + SYS_GRAPH, Mockito.mock(Graph.class)); + Map graphs = new ConcurrentHashMap<>(); + graphs.put(GraphKey.fromString("DEFAULT-hugegraph"), Mockito.mock(Graph.class)); + graphs.put(GraphKey.fromString("DEFAULT-" + SYS_GRAPH), Mockito.mock(Graph.class)); Whitebox.setInternalState(manager, "graphs", graphs); Set result = manager.graphs("DEFAULT"); @@ -499,8 +500,8 @@ private static GraphManager managerWithDefaultRoleContext(String operator, GRAPHSPACE + "-" + GRAPH, Collections.emptyMap())); Whitebox.setInternalState(manager, "metaManager", metaManager); - Map graphs = new ConcurrentHashMap<>(); - graphs.put(GRAPHSPACE + "-" + GRAPH, Mockito.mock(HugeGraph.class)); + Map graphs = new ConcurrentHashMap<>(); + graphs.put(GraphKey.fromString(GRAPHSPACE + "-" + GRAPH), Mockito.mock(HugeGraph.class)); Whitebox.setInternalState(manager, "graphs", graphs); Mockito.when(authManager.isDefaultRole(GRAPHSPACE, GRAPH, TARGET, @@ -530,8 +531,8 @@ private static GraphManager managerWithLoadedDefaultGraph(String operator, spaces.put("DEFAULT", new GraphSpace("DEFAULT")); Whitebox.setInternalState(manager, "graphSpaces", spaces); - Map graphs = new ConcurrentHashMap<>(); - graphs.put("DEFAULT-hugegraph", Mockito.mock(HugeGraph.class)); + Map graphs = new ConcurrentHashMap<>(); + graphs.put(GraphKey.fromString("DEFAULT-hugegraph"), Mockito.mock(HugeGraph.class)); Whitebox.setInternalState(manager, "graphs", graphs); return manager; } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/cache/RequestCacheLifetimeTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/cache/RequestCacheLifetimeTest.java index 812343be30..16ad21bb23 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/cache/RequestCacheLifetimeTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/cache/RequestCacheLifetimeTest.java @@ -28,6 +28,7 @@ import org.apache.hugegraph.HugeGraphParams; import org.apache.hugegraph.api.graph.VertexAPI; import org.apache.hugegraph.api.graph.EdgeAPI; +import org.apache.hugegraph.core.GraphKey; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.util.JsonUtil; import org.apache.hugegraph.backend.cache.CachedGraphTransaction; @@ -175,7 +176,8 @@ private static void updateThroughApi(HugeGraph graph, Class api, String bodyC Field field = Unsafe.class.getDeclaredField("theUnsafe"); field.setAccessible(true); GraphManager manager = (GraphManager) ((Unsafe) field.get(null)).allocateInstance(GraphManager.class); - Whitebox.setInternalState(manager, "graphs", Collections.singletonMap(graph.spaceGraphName(), graph)); + Whitebox.setInternalState(manager, "graphs", + Collections.singletonMap(GraphKey.fromString(graph.spaceGraphName()), graph)); Class bodyType = Class.forName(api.getName() + "$" + bodyClass); Object body = JsonUtil.fromJson("{\"properties\":{\"added\":\"" + value + "\"}}", bodyType); Method update = api.getMethod("update", GraphManager.class, String.class, String.class, diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerClearGraphSpaceTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerClearGraphSpaceTest.java index 6cc9497998..b78cbfb553 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerClearGraphSpaceTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerClearGraphSpaceTest.java @@ -37,6 +37,7 @@ import org.apache.hugegraph.api.space.GraphSpaceAPI; import org.apache.hugegraph.config.CoreOptions; import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.core.GraphKey; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.event.EventHub; import org.apache.hugegraph.event.EventHub.NotifyResult; @@ -129,11 +130,11 @@ public void testClearKeepsGraphsOfSpacesNamedLikeIt() { for (String space : new String[]{"gs", "gs2", "gs_x"}) { spaces.put(space, new GraphSpace(space)); } - Map graphs = + Map graphs = Whitebox.getInternalState(manager, "graphs"); - graphs.put("gs-g", cleared); - graphs.put("gs2-g", kept); - graphs.put("gs_x-g", keptUnderscore); + graphs.put(GraphKey.fromString("gs-g"), cleared); + graphs.put(GraphKey.fromString("gs2-g"), kept); + graphs.put(GraphKey.fromString("gs_x-g"), keptUnderscore); Map services = Whitebox.getInternalState(manager, "services"); services.put("gs-s", Mockito.mock(Service.class)); @@ -145,9 +146,9 @@ public void testClearKeepsGraphsOfSpacesNamedLikeIt() { Mockito.verify(cleared).clearBackend(); Mockito.verify(kept, Mockito.never()).clearBackend(); Mockito.verify(keptUnderscore, Mockito.never()).clearBackend(); - Assert.assertFalse(graphs.containsKey("gs-g")); - Assert.assertTrue(graphs.containsKey("gs2-g")); - Assert.assertTrue(graphs.containsKey("gs_x-g")); + Assert.assertFalse(graphs.containsKey(GraphKey.fromString("gs-g"))); + Assert.assertTrue(graphs.containsKey(GraphKey.fromString("gs2-g"))); + Assert.assertTrue(graphs.containsKey(GraphKey.fromString("gs_x-g"))); Mockito.verify(meta).service("gs", "s"); Mockito.verify(meta, Mockito.never()).service("gs2", "s"); diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerConfigTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerConfigTest.java index eea6870048..909d1e7792 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerConfigTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerConfigTest.java @@ -71,6 +71,7 @@ import org.apache.hugegraph.config.OptionSpace; import org.apache.hugegraph.config.RpcOptions; import org.apache.hugegraph.config.ServerOptions; +import org.apache.hugegraph.core.GraphKey; import org.apache.hugegraph.core.GraphManager; import org.apache.hugegraph.event.EventHub; import org.apache.hugegraph.event.EventListener; @@ -815,8 +816,8 @@ private void checkLegacyDrop(boolean failCleanup, boolean clear, boolean failQuo throw error.getCause(); } }); - Map graphs = Whitebox.getInternalState(manager, "graphs"); - graphs.put(legacy.spaceGraphName(), legacy); + Map graphs = Whitebox.getInternalState(manager, "graphs"); + graphs.put(GraphKey.fromString(legacy.spaceGraphName()), legacy); DefaultGraphManager bindings = new DefaultGraphManager(new Settings()); bindings.putGraph(legacy.spaceGraphName(), legacy); CountDownLatch dropped = new CountDownLatch(1); @@ -1087,8 +1088,8 @@ private void checkReceivedSameNameAddAfterCleanup(boolean addDuringCleanup, int spaces.put("DEFAULT", space); if (lateSelfAdd) { // Local creation is still waiting for its own PD acknowledgement. - java.util.Set creating = Whitebox.getInternalState(manager, "creatingGraphs"); - creating.add(key); + java.util.Set creating = Whitebox.getInternalState(manager, "creatingGraphs"); + creating.add(GraphKey.fromString(key)); } String threadName = Thread.currentThread().getName(); if (peer) { @@ -1151,19 +1152,19 @@ private void checkReceivedSameNameAddAfterCleanup(boolean addDuringCleanup, int } Assert.assertEquals(peer && addDuringCleanup ? 3 : 2, cleanupAttempts.get()); Assert.assertNotSame(graph, factories.get(key)); - java.util.Set pending = Whitebox.getInternalState(manager, "pendingGraphAdds"); + java.util.Set pending = Whitebox.getInternalState(manager, "pendingGraphAdds"); if (replayFailure == 1) { Assert.assertNull(manager.graph(key)); Assert.assertNull(manager.graph("DEFAULT", name)); Assert.assertFalse(factories.containsKey(key)); - Assert.assertFalse(pending.contains(key)); + Assert.assertFalse(pending.contains(GraphKey.fromString(key))); return; } Assert.assertSame(newConfig, latest.get()); if (replayFailure == 2) { Assert.assertNull(manager.graph(key)); Assert.assertFalse(factories.containsKey(key)); - Assert.assertTrue(pending.contains(key)); + Assert.assertTrue(pending.contains(GraphKey.fromString(key))); Assert.assertEquals(1, singleConfigReads.get()); rejectReplay.set(false); Whitebox.invoke(GraphManager.class, new Class[]{Object.class}, @@ -1186,7 +1187,7 @@ private void checkReceivedSameNameAddAfterCleanup(boolean addDuringCleanup, int Assert.assertEquals("new_generation", replacement.nickname()); Assert.assertEquals(0L, replacement.traversal().V().count().next().longValue()); Assert.assertTrue(replacement.started()); - Assert.assertFalse(pending.contains(key)); + Assert.assertFalse(pending.contains(GraphKey.fromString(key))); Assert.assertSame(replacement, bindings.getGraph(key)); Assert.assertNotNull(bindings.getTraversalSource("__g_" + key)); Assert.assertSame(replacement, bindings.getAsBindings().get(key)); @@ -1291,25 +1292,26 @@ private void checkReloadAfterRestCreate(boolean selfAdd, boolean duplicateAdd) t Whitebox.invoke(GraphManager.class, new Class[]{Object.class}, "graphAddHandler", manager, "self-add"); } - java.util.Set pending = Whitebox.getInternalState(manager, "pendingGraphAdds"); + java.util.Set pending = Whitebox.getInternalState(manager, "pendingGraphAdds"); if (duplicateAdd) { Whitebox.invoke(GraphManager.class, new Class[]{Object.class}, "graphAddHandler", manager, "foreign-add"); Assert.assertSame(owner, manager.graph(key)); Assert.assertSame(otherOwner, manager.graph("DEFAULT-" + other)); - Assert.assertTrue(pending.contains(key)); - Assert.assertTrue(pending.contains("DEFAULT-" + other)); + Assert.assertTrue(pending.contains(GraphKey.fromString(key))); + Assert.assertTrue(pending.contains(GraphKey.fromString("DEFAULT-" + other))); } Mockito.clearInvocations(meta); long begin = System.nanoTime(); System.out.println(String.format("self-add reload before: selfAdd=%s pending=%s graphs=%s", - selfAdd, pending.contains(key), manager.graphs().size())); + selfAdd, pending.contains(GraphKey.fromString(key)), + manager.graphs().size())); try { api.reload(manager, Map.of("action", "reload")); } finally { System.out.println(String.format("self-add reload after: selfAdd=%s durationMs=%s pending=%s graphs=%s", selfAdd, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - begin), - pending.contains(key), manager.graphs().size())); + pending.contains(GraphKey.fromString(key)), manager.graphs().size())); } Assert.assertTrue(owner.closed()); Assert.assertTrue(otherOwner.closed()); @@ -1318,8 +1320,8 @@ private void checkReloadAfterRestCreate(boolean selfAdd, boolean duplicateAdd) t Assert.assertNotSame(owner, manager.graph(key)); Assert.assertNotSame(otherOwner, manager.graph("DEFAULT-" + other)); Assert.assertEquals(2, manager.graphs().size()); - Assert.assertFalse(pending.contains(key)); - Assert.assertFalse(pending.contains("DEFAULT-" + other)); + Assert.assertFalse(pending.contains(GraphKey.fromString(key))); + Assert.assertFalse(pending.contains(GraphKey.fromString("DEFAULT-" + other))); Mockito.verify(meta, Mockito.never()).getGraphConfig(Mockito.anyString(), Mockito.anyString()); Mockito.verify(meta).graphConfigs("DEFAULT"); Assert.assertSame(manager.graph(key), bindings.getGraph(key)); @@ -1511,12 +1513,12 @@ private void checkRpcStartupDuringDeletion(boolean consumeToken, boolean keepOth manager, "DEFAULT", name, "I'm sure to drop the graph")); Assert.assertTrue(metadataEntered.await(5, TimeUnit.SECONDS)); java.util.Set removing = Whitebox.getInternalState(manager, "removingGraphs"); - Assert.assertTrue(removing.contains(graph.spaceGraphName())); + Assert.assertTrue(removing.contains(GraphKey.fromString(graph.spaceGraphName()))); if (consumeToken) { Mockito.when(meta.extractGraphsFromResponse(Mockito.any())).thenReturn( java.util.Collections.singletonList(graph.spaceGraphName())); Whitebox.invoke(GraphManager.class, "graphRemoveHandler", manager, new Object()); - Assert.assertFalse(removing.contains(graph.spaceGraphName())); + Assert.assertFalse(removing.contains(GraphKey.fromString(graph.spaceGraphName()))); } Assert.assertFalse(graph.closed()); Assert.assertSame(graph, manager.graph(graph.spaceGraphName())); @@ -1929,12 +1931,12 @@ private void checkNonPdPublicDeleteRetry(boolean failCleanup, boolean rpcCleanup owner = manager.createGraphLocal(name, config + name + "\n"); HugeGraph graph = owner; Map factories = Whitebox.getInternalState(HugeFactory.class, "GRAPHS"); - Map managed = Whitebox.getInternalState(manager, "graphs"); + Map managed = Whitebox.getInternalState(manager, "graphs"); List retained = Whitebox.getInternalState(owner, "closeResources"); List rpc = Whitebox.getInternalState(owner, "rpcServiceCleanup"); File file = owner.configuration().file(); Assert.assertSame(owner, factories.get(owner.spaceGraphName())); - Assert.assertSame(owner, managed.get(owner.spaceGraphName())); + Assert.assertSame(owner, managed.get(GraphKey.fromString(owner.spaceGraphName()))); Assert.assertTrue(file.isFile()); Throwable controlled = fatal ? new AssertionError("fatal native cleanup boundary") : new IllegalStateException("persistent native cleanup boundary"); @@ -1973,7 +1975,7 @@ private void checkNonPdPublicDeleteRetry(boolean failCleanup, boolean rpcCleanup } boolean configRetained = file.isFile(); boolean factoryRetained = factories.get(owner.spaceGraphName()) == owner; - boolean managerRetained = managed.get(owner.spaceGraphName()) == owner; + boolean managerRetained = managed.get(GraphKey.fromString(owner.spaceGraphName())) == owner; boolean completed = Whitebox.getInternalState(owner, "closeCompleted"); int pending = retained.size(); int rpcPending = rpc.size(); @@ -2014,7 +2016,7 @@ private void checkNonPdPublicDeleteRetry(boolean failCleanup, boolean rpcCleanup Assert.assertEquals(0, retained.size()); Assert.assertFalse(file.exists()); Assert.assertFalse(factories.containsKey(owner.spaceGraphName())); - Assert.assertFalse(managed.containsKey(owner.spaceGraphName())); + Assert.assertFalse(managed.containsKey(GraphKey.fromString(owner.spaceGraphName()))); Assert.assertSame(keeper, manager.graph(keeper.spaceGraphName())); } finally { reject.set(false); @@ -2080,8 +2082,8 @@ private void checkPostCloseDrop(boolean failCleanup) throws Exception { throw error.getCause(); } }); - Map graphs = Whitebox.getInternalState(manager, "graphs"); - graphs.put(graph.spaceGraphName(), graph); + Map graphs = Whitebox.getInternalState(manager, "graphs"); + graphs.put(GraphKey.fromString(graph.spaceGraphName()), graph); DefaultGraphManager bindings = new DefaultGraphManager(new Settings()); bindings.putGraph(graph.spaceGraphName(), graph); EventHub hub = Whitebox.getInternalState(manager, "eventHub"); @@ -2805,8 +2807,8 @@ public void testClosedUnclearedOwnerRejectsRestClearRetry() throws Exception { () -> manager.dropGraph("DEFAULT", name, false)); Assert.assertSame(first, failedClose.getCause()); Assert.assertTrue(graph.closed()); - Map certificates = Whitebox.getInternalState(manager, "clearedGraphs"); - Assert.assertFalse(certificates.containsKey(graph.spaceGraphName())); + Map certificates = Whitebox.getInternalState(manager, "clearedGraphs"); + Assert.assertFalse(certificates.containsKey(GraphKey.fromString(graph.spaceGraphName()))); Mockito.doCallRealMethod().when(provider).close(); HugeException refused = Assert.assertThrows(HugeException.class, () -> @@ -2865,8 +2867,8 @@ private void checkClearedOwnerRetry(int retryMode) throws Exception { throw failure.getCause(); } }); - Map graphs = Whitebox.getInternalState(manager, "graphs"); - graphs.put(graph.spaceGraphName(), proxy); + Map graphs = Whitebox.getInternalState(manager, "graphs"); + graphs.put(GraphKey.fromString(graph.spaceGraphName()), proxy); MetaManager meta = Mockito.mock(MetaManager.class); Whitebox.setInternalState(manager, "metaManager", meta); Whitebox.setInternalState(manager, "PDExist", true); @@ -2892,19 +2894,19 @@ private void checkClearedOwnerRetry(int retryMode) throws Exception { Assert.assertTrue(refusal.getCause() instanceof IllegalStateException); Assert.assertFalse(graph.closed()); Assert.assertEquals(1, clears.get()); - Map progress = Whitebox.getInternalState(manager, "clearedGraphs"); - Assert.assertSame(proxy, progress.get(graph.spaceGraphName())); + Map progress = Whitebox.getInternalState(manager, "clearedGraphs"); + Assert.assertSame(proxy, progress.get(GraphKey.fromString(graph.spaceGraphName()))); Assert.assertSame(proxy, manager.graph(graph.spaceGraphName())); if (retryMode == 1) { Assert.assertThrows(HugeException.class, () -> manager.dropGraph("DEFAULT", name, false)); - Assert.assertSame(proxy, progress.get(graph.spaceGraphName())); + Assert.assertSame(proxy, progress.get(GraphKey.fromString(graph.spaceGraphName()))); Whitebox.invoke(GraphManager.class, "startRpcServer", manager); RpcServer server = Whitebox.getInternalState(manager, "rpcServer"); RpcClientProvider client = Whitebox.getInternalState(manager, "rpcClient"); assertRpcResources(server, client, 0); } else if (retryMode == 2) { manager.close(); - Assert.assertSame(proxy, progress.get(graph.spaceGraphName())); + Assert.assertSame(proxy, progress.get(GraphKey.fromString(graph.spaceGraphName()))); } release.countDown(); transaction.get(5, TimeUnit.SECONDS); @@ -2917,7 +2919,7 @@ private void checkClearedOwnerRetry(int retryMode) throws Exception { Assert.assertFalse(factory.containsKey(graph.spaceGraphName())); } Assert.assertTrue(graph.closed()); - Assert.assertFalse(progress.containsKey(graph.spaceGraphName())); + Assert.assertFalse(progress.containsKey(GraphKey.fromString(graph.spaceGraphName()))); Assert.assertEquals(1, clears.get()); Mockito.verify(meta).removeGraphConfig("DEFAULT", name); Mockito.verify(meta).notifyGraphRemove("DEFAULT", name); @@ -2927,11 +2929,11 @@ private void checkClearedOwnerRetry(int retryMode) throws Exception { BackendStoreProvider replacementProvider = Mockito.spy(replacement.storeProvider()); Whitebox.setInternalState(replacement, "storeProvider", replacementProvider); // Even a stale certificate for the same name must not certify this new identity. - progress.put(replacement.spaceGraphName(), proxy); + progress.put(GraphKey.fromString(replacement.spaceGraphName()), proxy); manager.dropGraph("DEFAULT", name, true); Mockito.verify(replacementProvider).clear(); Assert.assertTrue(replacement.closed()); - Assert.assertFalse(progress.containsKey(replacement.spaceGraphName())); + Assert.assertFalse(progress.containsKey(GraphKey.fromString(replacement.spaceGraphName()))); Mockito.verify(meta, Mockito.times(2)).removeGraphConfig("DEFAULT", name); Mockito.verify(meta, Mockito.times(2)).notifyGraphRemove("DEFAULT", name); } @@ -3206,7 +3208,7 @@ private void checkMetadataDeleteFailure(boolean notify, boolean failRestore, boo // Retry the fenced deletion; successful clear must not re-export the removed owner. if (notify) { java.util.Set removing = Whitebox.getInternalState(manager, "removingGraphs"); - Assert.assertTrue(removing.contains(graph.spaceGraphName())); + Assert.assertTrue(removing.contains(GraphKey.fromString(graph.spaceGraphName()))); } manager.dropGraph("DEFAULT", name, true); } diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerGraphKeyTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerGraphKeyTest.java new file mode 100644 index 0000000000..4cfaa8fb03 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/GraphManagerGraphKeyTest.java @@ -0,0 +1,127 @@ +/* + * 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.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.configuration2.PropertiesConfiguration; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.config.HugeConfig; +import org.apache.hugegraph.core.GraphKey; +import org.apache.hugegraph.core.GraphManager; +import org.apache.hugegraph.event.EventHub; +import org.apache.hugegraph.meta.MetaManager; +import org.apache.hugegraph.testutil.Assert; +import org.apache.hugegraph.testutil.Whitebox; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class GraphManagerGraphKeyTest { + + private GraphManager manager; + private Map graphs; + + @Before + public void setup() { + this.manager = new GraphManager(new HugeConfig(new PropertiesConfiguration()), + Mockito.mock(EventHub.class)); + this.graphs = Whitebox.getInternalState(this.manager, "graphs"); + } + + @After + public void teardown() { + if (this.manager != null) { + this.manager.close(); + } + } + + @Test + public void testSameGraphNameInDifferentSpaces() { + HugeGraph first = Mockito.mock(HugeGraph.class); + HugeGraph second = Mockito.mock(HugeGraph.class); + this.graphs.put(new GraphKey("team", "movies"), first); + this.graphs.put(new GraphKey("team_other", "movies"), second); + + Assert.assertSame(first, this.manager.graph("team", "movies")); + Assert.assertSame(first, this.manager.graph("team-movies")); + Assert.assertSame(second, this.manager.graph("team_other", "movies")); + Assert.assertEquals(Set.of("movies"), this.manager.graphs("team")); + Assert.assertEquals(Set.of("movies"), this.manager.graphs("team_other")); + Assert.assertNull(this.manager.graph("movies")); + Assert.assertNull(this.manager.graph("unknown-movies")); + } + + @Test + public void testGraphNamesViewRemainsLiveAndUnmodifiable() { + Set names = this.manager.graphs(); + Assert.assertTrue(names.isEmpty()); + this.graphs.put(new GraphKey("DEFAULT", "movies"), Mockito.mock(HugeGraph.class)); + + Assert.assertEquals(Set.of("DEFAULT-movies"), names); + Assert.assertTrue(names.contains("DEFAULT-movies")); + Assert.assertFalse(names.contains("movies")); + Assert.assertThrows(UnsupportedOperationException.class, () -> names.add("DEFAULT-other")); + Assert.assertThrows(UnsupportedOperationException.class, () -> names.remove("DEFAULT-movies")); + Assert.assertThrows(UnsupportedOperationException.class, names::clear); + Iterator iterator = names.iterator(); + iterator.next(); + Assert.assertThrows(UnsupportedOperationException.class, iterator::remove); + + this.graphs.remove(new GraphKey("DEFAULT", "movies")); + Assert.assertTrue(names.isEmpty()); + Assert.assertFalse(names.contains("DEFAULT-movies")); + } + + @Test + public void testCombinedNamesPreserveGraphSpaceSeparatorsAndSystemGraph() { + HugeGraph graph = Mockito.mock(HugeGraph.class); + HugeGraph system = Mockito.mock(HugeGraph.class); + this.graphs.put(new GraphKey("team-east", "movies"), graph); + this.graphs.put(new GraphKey("DEFAULT", "~sys_graph"), system); + + Assert.assertSame(graph, this.manager.graph("team-east", "movies")); + Assert.assertSame(graph, this.manager.graph("team-east-movies")); + Assert.assertEquals(Set.of("movies"), this.manager.graphs("team-east")); + Assert.assertSame(system, this.manager.graph("DEFAULT-~sys_graph")); + Assert.assertTrue(this.manager.graphs().contains("team-east-movies")); + } + + @Test + public void testPdLookupUsesExistingStringMetadataKeys() { + MetaManager meta = Mockito.mock(MetaManager.class); + Whitebox.setInternalState(this.manager, "metaManager", meta); + Whitebox.setInternalState(this.manager, "PDExist", true); + Mockito.when(meta.graphConfigs("team")).thenReturn(Map.of("team-movies", Map.of())); + HugeGraph graph = Mockito.mock(HugeGraph.class); + this.manager = Mockito.spy(this.manager); + Mockito.doAnswer(invocation -> { + this.graphs.put(new GraphKey("team", "movies"), graph); + return graph; + }).when(this.manager).createGraph(Mockito.eq("team"), Mockito.eq("movies"), + Mockito.anyString(), Mockito.anyMap(), Mockito.eq(false)); + + Assert.assertSame(graph, this.manager.graph("team", "movies")); + Assert.assertSame(graph, this.manager.graph("team-movies")); + Mockito.verify(meta).graphConfigs("team"); + } +}