Skip to content

Commit 66e5339

Browse files
authored
fix(server): different graph name share the same backend (#3027)
## Main Changes Change `ServerInfoManager.selfNodeId()` which returns "server-1" previously to "{graphname}/server-1" ## Upgrade impact This change namespaces the server id by graph name, so old unfinished tasks in the non-PD local scheduler may still reference the previous bare server id, for example `server-1`. Those historical tasks can remain visible, but they may not be restored or cancelled by the new namespaced server id after upgrade. The impact is limited to unfinished local-scheduler tasks that already existed before upgrading; newly scheduled tasks use the new namespaced id. To avoid this compatibility edge case, finish or cancel pending local tasks before upgrading.
1 parent f69ca66 commit 66e5339

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.hugegraph.HugeGraph;
3030
import org.apache.hugegraph.HugeGraphParams;
3131
import org.apache.hugegraph.backend.id.Id;
32+
import org.apache.hugegraph.backend.id.IdGenerator;
3233
import org.apache.hugegraph.backend.page.PageInfo;
3334
import org.apache.hugegraph.backend.query.Condition;
3435
import org.apache.hugegraph.backend.query.ConditionQuery;
@@ -104,7 +105,9 @@ public synchronized boolean close() {
104105
public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) {
105106
E.checkArgument(nodeInfo != null, "The global node info can't be null");
106107

107-
Id serverId = nodeInfo.nodeId();
108+
this.globalNodeInfo = nodeInfo;
109+
110+
Id serverId = this.selfNodeId();
108111
HugeServerInfo existed = this.serverInfo(serverId);
109112
if (existed != null && existed.alive()) {
110113
final long now = DateUtil.now().getTime();
@@ -137,8 +140,6 @@ public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) {
137140
} while (page != null);
138141
}
139142

140-
this.globalNodeInfo = nodeInfo;
141-
142143
// TODO: save ServerInfo to AuthServer
143144
this.saveServerInfo(this.selfNodeId(), this.selfNodeRole());
144145
}
@@ -162,7 +163,9 @@ public Id selfNodeId() {
162163
if (this.globalNodeInfo == null) {
163164
return null;
164165
}
165-
return this.globalNodeInfo.nodeId();
166+
// Scope server id to graph to avoid cross-graph collision
167+
return IdGenerator.of(this.graph.spaceGraphName() + "/" +
168+
this.globalNodeInfo.nodeId().asString());
166169
}
167170

168171
public NodeRole selfNodeRole() {

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.apache.hugegraph.unit.core.RowLockTest;
4646
import org.apache.hugegraph.unit.core.SecurityManagerTest;
4747
import org.apache.hugegraph.unit.core.SerialEnumTest;
48+
import org.apache.hugegraph.unit.core.ServerInfoManagerTest;
4849
import org.apache.hugegraph.unit.core.SystemSchemaStoreTest;
4950
import org.apache.hugegraph.unit.core.TraversalUtilTest;
5051
import org.apache.hugegraph.unit.id.EdgeIdTest;
@@ -125,6 +126,7 @@
125126
TraversalUtilTest.class,
126127
PageStateTest.class,
127128
SystemSchemaStoreTest.class,
129+
ServerInfoManagerTest.class,
128130
RoleElectionStateMachineTest.class,
129131
HugeGraphAuthProxyTest.class,
130132

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.unit.core;
19+
20+
import java.util.concurrent.ExecutorService;
21+
22+
import org.apache.hugegraph.HugeGraphParams;
23+
import org.apache.hugegraph.backend.id.Id;
24+
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
25+
import org.apache.hugegraph.task.ServerInfoManager;
26+
import org.apache.hugegraph.testutil.Assert;
27+
import org.apache.hugegraph.testutil.Whitebox;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.mockito.Mockito;
31+
32+
public class ServerInfoManagerTest {
33+
34+
private ServerInfoManager sysGraphManager;
35+
private ServerInfoManager hugegraphManager;
36+
37+
@Before
38+
public void setup() {
39+
HugeGraphParams sysGraphParams = Mockito.mock(HugeGraphParams.class);
40+
Mockito.when(sysGraphParams.spaceGraphName())
41+
.thenReturn("DEFAULT-~sys_graph");
42+
43+
HugeGraphParams hugegraphParams = Mockito.mock(HugeGraphParams.class);
44+
Mockito.when(hugegraphParams.spaceGraphName())
45+
.thenReturn("DEFAULT-hugegraph");
46+
47+
ExecutorService executor = Mockito.mock(ExecutorService.class);
48+
49+
this.sysGraphManager = new ServerInfoManager(sysGraphParams, executor);
50+
this.hugegraphManager = new ServerInfoManager(hugegraphParams, executor);
51+
}
52+
53+
@Test
54+
public void testSelfNodeIdScopedByGraphWithSameNodeId() {
55+
GlobalMasterInfo nodeInfo = GlobalMasterInfo.master("server-1");
56+
57+
Whitebox.setInternalState(this.sysGraphManager,
58+
"globalNodeInfo", nodeInfo);
59+
Whitebox.setInternalState(this.hugegraphManager,
60+
"globalNodeInfo", nodeInfo);
61+
62+
Id sysGraphNodeId = this.sysGraphManager.selfNodeId();
63+
Id hugegraphNodeId = this.hugegraphManager.selfNodeId();
64+
65+
Assert.assertEquals("DEFAULT-~sys_graph/server-1",
66+
sysGraphNodeId.asString());
67+
Assert.assertEquals("DEFAULT-hugegraph/server-1",
68+
hugegraphNodeId.asString());
69+
Assert.assertFalse(sysGraphNodeId.equals(hugegraphNodeId));
70+
}
71+
72+
@Test
73+
public void testSelfNodeIdReturnsNullWhenNotInitialized() {
74+
Assert.assertNull(this.sysGraphManager.selfNodeId());
75+
}
76+
}

0 commit comments

Comments
 (0)