Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

‼️ Critical:initServerInfo() 中读写路径 ID 不一致(阻塞性问题)

此处修改后 selfNodeId() 返回命名空间化的 ID(如 DEFAULT-hugegraph/server-1),但调用方 initServerInfo() 的存在性检查仍使用原始裸 ID,两条路径的 key 不同:

// initServerInfo()(本 PR 未修改这部分)
Id serverId = nodeInfo.nodeId();                        // ❌ 裸 ID:"server-1"
HugeServerInfo existed = this.serverInfo(serverId);    // ❌ 用 "server-1" 查询
// ...
this.globalNodeInfo = nodeInfo;                        // line 140,赋值后 selfNodeId() 才变
this.saveServerInfo(this.selfNodeId(), ...);            // ✅ 保存 "DEFAULT-hugegraph/server-1"

后果:

  • lines 109–122 的 alive 检查(防止同名节点重复启动的保护)永远查不到已命名空间化的旧记录,保护逻辑被静默跳过
  • 重启时旧的命名空间记录不会被正确识别,会直接写入新记录,旧记录成为孤儿数据积累在 backend 中

建议修法:initServerInfo() 入口先设置 globalNodeInfo,再统一用 selfNodeId() 做后续查询,保持读写 key 一致:

public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) {
    E.checkArgument(nodeInfo != null, "The global node info can't be null");
    this.globalNodeInfo = nodeInfo;   // 先赋值,selfNodeId() 立即返回命名空间 ID
    Id serverId = this.selfNodeId();  // ✅ 与 save / remove 路径使用同一个 key
    HugeServerInfo existed = this.serverInfo(serverId);
    if (existed != null && existed.alive()) {
        // ... 存活检查 & 等待逻辑不变 ...
    }
    E.checkArgument(existed == null || !existed.alive(),
                    "The server with name '%s' already in cluster", serverId);
    // master 唯一性检查不变 ...
    this.saveServerInfo(serverId, this.selfNodeRole());
}

这样读取和写入始终使用同一个 ID,alive 检查和 master 唯一性保护才能真正生效。


public NodeRole selfNodeRole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,6 +126,7 @@
TraversalUtilTest.class,
PageStateTest.class,
SystemSchemaStoreTest.class,
ServerInfoManagerTest.class,
RoleElectionStateMachineTest.class,
HugeGraphAuthProxyTest.class,

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading