diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java index bcef869017..1560ec0b8f 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java @@ -29,6 +29,7 @@ import org.apache.hugegraph.HugeGraph; import org.apache.hugegraph.HugeGraphParams; import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.id.IdGenerator; import org.apache.hugegraph.backend.page.PageInfo; import org.apache.hugegraph.backend.query.Condition; import org.apache.hugegraph.backend.query.ConditionQuery; @@ -104,7 +105,9 @@ public synchronized boolean close() { public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) { E.checkArgument(nodeInfo != null, "The global node info can't be null"); - Id serverId = nodeInfo.nodeId(); + this.globalNodeInfo = nodeInfo; + + Id serverId = this.selfNodeId(); HugeServerInfo existed = this.serverInfo(serverId); if (existed != null && existed.alive()) { final long now = DateUtil.now().getTime(); @@ -137,8 +140,6 @@ public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) { } while (page != null); } - this.globalNodeInfo = nodeInfo; - // TODO: save ServerInfo to AuthServer this.saveServerInfo(this.selfNodeId(), this.selfNodeRole()); } @@ -162,7 +163,9 @@ public Id selfNodeId() { if (this.globalNodeInfo == null) { return null; } - return this.globalNodeInfo.nodeId(); + // Scope server id to graph to avoid cross-graph collision + return IdGenerator.of(this.graph.spaceGraphName() + "/" + + this.globalNodeInfo.nodeId().asString()); } public NodeRole selfNodeRole() { 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 ce249a967e..f9542ecac7 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 @@ -45,6 +45,7 @@ import org.apache.hugegraph.unit.core.RowLockTest; import org.apache.hugegraph.unit.core.SecurityManagerTest; import org.apache.hugegraph.unit.core.SerialEnumTest; +import org.apache.hugegraph.unit.core.ServerInfoManagerTest; import org.apache.hugegraph.unit.core.SystemSchemaStoreTest; import org.apache.hugegraph.unit.core.TraversalUtilTest; import org.apache.hugegraph.unit.id.EdgeIdTest; @@ -125,6 +126,7 @@ TraversalUtilTest.class, PageStateTest.class, SystemSchemaStoreTest.class, + ServerInfoManagerTest.class, RoleElectionStateMachineTest.class, HugeGraphAuthProxyTest.class, diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/ServerInfoManagerTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/ServerInfoManagerTest.java new file mode 100644 index 0000000000..85815bbc19 --- /dev/null +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/ServerInfoManagerTest.java @@ -0,0 +1,76 @@ +/* + * 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.concurrent.ExecutorService; + +import org.apache.hugegraph.HugeGraphParams; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.masterelection.GlobalMasterInfo; +import org.apache.hugegraph.task.ServerInfoManager; +import org.apache.hugegraph.testutil.Assert; +import org.apache.hugegraph.testutil.Whitebox; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class ServerInfoManagerTest { + + private ServerInfoManager sysGraphManager; + private ServerInfoManager hugegraphManager; + + @Before + public void setup() { + HugeGraphParams sysGraphParams = Mockito.mock(HugeGraphParams.class); + Mockito.when(sysGraphParams.spaceGraphName()) + .thenReturn("DEFAULT-~sys_graph"); + + HugeGraphParams hugegraphParams = Mockito.mock(HugeGraphParams.class); + Mockito.when(hugegraphParams.spaceGraphName()) + .thenReturn("DEFAULT-hugegraph"); + + ExecutorService executor = Mockito.mock(ExecutorService.class); + + this.sysGraphManager = new ServerInfoManager(sysGraphParams, executor); + this.hugegraphManager = new ServerInfoManager(hugegraphParams, executor); + } + + @Test + public void testSelfNodeIdScopedByGraphWithSameNodeId() { + GlobalMasterInfo nodeInfo = GlobalMasterInfo.master("server-1"); + + Whitebox.setInternalState(this.sysGraphManager, + "globalNodeInfo", nodeInfo); + Whitebox.setInternalState(this.hugegraphManager, + "globalNodeInfo", nodeInfo); + + Id sysGraphNodeId = this.sysGraphManager.selfNodeId(); + Id hugegraphNodeId = this.hugegraphManager.selfNodeId(); + + Assert.assertEquals("DEFAULT-~sys_graph/server-1", + sysGraphNodeId.asString()); + Assert.assertEquals("DEFAULT-hugegraph/server-1", + hugegraphNodeId.asString()); + Assert.assertFalse(sysGraphNodeId.equals(hugegraphNodeId)); + } + + @Test + public void testSelfNodeIdReturnsNullWhenNotInitialized() { + Assert.assertNull(this.sysGraphManager.selfNodeId()); + } +}