Skip to content

Commit

Permalink
enhence ServerInfoManager.heartbeat()
Browse files Browse the repository at this point in the history
Change-Id: I6932893c4be8331547f3b721083ca00430f85e58
  • Loading branch information
javeme committed Dec 3, 2023
1 parent b9dd3a1 commit cd05642
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ public void waitReady(RpcServer rpcServer) {
}

@Override
public void serverStarted(GlobalMasterInfo serverInfo) {
public void serverStarted(GlobalMasterInfo nodeInfo) {
this.verifyAdminPermission();
this.hugegraph.serverStarted(serverInfo);
this.hugegraph.serverStarted(nodeInfo);
}

@Override
Expand Down Expand Up @@ -776,9 +776,9 @@ public void resumeSnapshot() {
}

@Override
public void create(String configPath, GlobalMasterInfo serverInfo) {
public void create(String configPath, GlobalMasterInfo nodeInfo) {
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
this.hugegraph.create(configPath, serverInfo);
this.hugegraph.create(configPath, nodeInfo);

Check warning on line 781 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java#L781

Added line #L781 was not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,23 +455,23 @@ private void checkBackendVersionOrExit(HugeConfig config) {
}

private void serverStarted(HugeConfig config) {
String serverId = config.get(ServerOptions.SERVER_ID);
String id = config.get(ServerOptions.SERVER_ID);
String role = config.get(ServerOptions.SERVER_ROLE);
E.checkArgument(StringUtils.isNotEmpty(serverId),
E.checkArgument(StringUtils.isNotEmpty(id),
"The server name can't be null or empty");
E.checkArgument(StringUtils.isNotEmpty(role),
"The server role can't be null or empty");

NodeRole serverRole = NodeRole.valueOf(role.toUpperCase());
boolean supportRoleElection = !serverRole.computer() &&
NodeRole nodeRole = NodeRole.valueOf(role.toUpperCase());
boolean supportRoleElection = !nodeRole.computer() &&
this.supportRoleElection();
if (supportRoleElection) {
// Init any server as Worker role, then do role election
serverRole = NodeRole.WORKER;
nodeRole = NodeRole.WORKER;
}

this.globalNodeRoleInfo.serverId(IdGenerator.of(serverId));
this.globalNodeRoleInfo.initServerRole(serverRole);
this.globalNodeRoleInfo.initNodeId(IdGenerator.of(id));
this.globalNodeRoleInfo.initNodeRole(nodeRole);

for (String graph : this.graphs()) {
HugeGraph hugegraph = this.graph(graph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public interface HugeGraph extends Graph {

void waitReady(RpcServer rpcServer);

void serverStarted(GlobalMasterInfo serverInfo);
void serverStarted(GlobalMasterInfo nodeInfo);

boolean started();

Expand All @@ -221,7 +221,7 @@ public interface HugeGraph extends Graph {

void resumeSnapshot();

void create(String configPath, GlobalMasterInfo serverInfo);
void create(String configPath, GlobalMasterInfo nodeInfo);

void drop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ public BackendFeatures backendStoreFeatures() {
}

@Override
public void serverStarted(GlobalMasterInfo serverInfo) {
public void serverStarted(GlobalMasterInfo nodeInfo) {
LOG.info("Init system info for graph '{}'", this.name);
this.initSystemInfo();

LOG.info("Init server info [{}-{}] for graph '{}'...",
serverInfo.serverId(), serverInfo.serverRole(), this.name);
this.serverInfoManager().initServerInfo(serverInfo);
nodeInfo.nodeId(), nodeInfo.nodeRole(), this.name);
this.serverInfoManager().initServerInfo(nodeInfo);

this.initRoleStateMachine(serverInfo.serverId());
this.initRoleStateMachine(nodeInfo.nodeId());

// TODO: check necessary?
LOG.info("Check olap property-key tables for graph '{}'", this.name);
Expand Down Expand Up @@ -401,7 +401,7 @@ public void truncateBackend() {
try {
this.storeProvider.truncate();
// TODO: remove this after serverinfo saved in etcd
this.serverStarted(this.serverInfoManager().serverInfo());
this.serverStarted(this.serverInfoManager().globalNodeRoleInfo());
} finally {
LockUtil.unlock(this.name, LockUtil.GRAPH_LOCK);
}
Expand Down Expand Up @@ -975,9 +975,9 @@ public synchronized void close() throws Exception {
}

@Override
public void create(String configPath, GlobalMasterInfo serverInfo) {
public void create(String configPath, GlobalMasterInfo nodeInfo) {
this.initBackend();
this.serverStarted(serverInfo);
this.serverStarted(nodeInfo);

Check warning on line 980 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java#L980

Added line #L980 was not covered by tests

// Write config to disk file
String confPath = ConfigUtil.writeToFile(configPath, this.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public final class GlobalMasterInfo {
private volatile boolean supportElection;
private volatile NodeInfo masterNodeInfo;

private volatile Id serverId;
private volatile NodeRole serverRole;
private volatile Id nodeId;
private volatile NodeRole nodeRole;

public GlobalMasterInfo() {
this(NO_MASTER);
Expand All @@ -41,8 +41,8 @@ public GlobalMasterInfo(NodeInfo masterInfo) {
this.supportElection = false;
this.masterNodeInfo = masterInfo;

this.serverId = null;
this.serverRole = null;
this.nodeId = null;
this.nodeRole = null;
}

public void supportElection(boolean featureSupport) {
Expand All @@ -66,36 +66,36 @@ public NodeInfo masterInfo() {
return this.masterNodeInfo;
}

public Id serverId() {
return this.serverId;
public Id nodeId() {
return this.nodeId;
}

public NodeRole serverRole() {
return this.serverRole;
public NodeRole nodeRole() {
return this.nodeRole;
}

public void serverId(Id id) {
this.serverId = id;
public void initNodeId(Id id) {
this.nodeId = id;
}

public void initServerRole(NodeRole role) {
public void initNodeRole(NodeRole role) {
E.checkArgument(role != null, "The server role can't be null");
E.checkArgument(this.serverRole == null,
E.checkArgument(this.nodeRole == null,
"The server role can't be init twice");
this.serverRole = role;
this.nodeRole = role;
}

public void changeServerRole(NodeRole role) {
public void changeNodeRole(NodeRole role) {
E.checkArgument(role != null, "The server role can't be null");
this.serverRole = role;
this.nodeRole = role;
}

public static GlobalMasterInfo master(String serverId) {
NodeInfo masterInfo = new NodeInfo(true, serverId);
GlobalMasterInfo serverInfo = new GlobalMasterInfo(masterInfo);
serverInfo.serverId = IdGenerator.of(serverId);
serverInfo.serverRole = NodeRole.MASTER;
return serverInfo;
public static GlobalMasterInfo master(String nodeId) {
NodeInfo masterInfo = new NodeInfo(true, nodeId);
GlobalMasterInfo nodeInfo = new GlobalMasterInfo(masterInfo);
nodeInfo.nodeId = IdGenerator.of(nodeId);
nodeInfo.nodeRole = NodeRole.MASTER;
return nodeInfo;
}

public static class NodeInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class ServerInfoManager {
private final HugeGraphParams graph;
private final ExecutorService dbExecutor;

private GlobalMasterInfo globalServerInfo;
private volatile GlobalMasterInfo globalNodeInfo;

private volatile boolean onlySingleNode;
private volatile boolean closed;
Expand All @@ -75,7 +75,7 @@ public ServerInfoManager(HugeGraphParams graph,
this.graph = graph;
this.dbExecutor = dbExecutor;

this.globalServerInfo = null;
this.globalNodeInfo = null;

this.onlySingleNode = false;
this.closed = false;
Expand All @@ -85,7 +85,7 @@ public void init() {
HugeServerInfo.schema(this.graph).initSchemaIfNeeded();
}

public boolean close() {
public synchronized boolean close() {
this.closed = true;
if (!this.dbExecutor.isShutdown()) {
this.removeSelfServerInfo();
Expand All @@ -102,16 +102,16 @@ public boolean close() {
return true;
}

public synchronized void initServerInfo(GlobalMasterInfo serverInfo) {
E.checkArgument(serverInfo != null, "The global node info can't be null");
this.globalServerInfo = serverInfo;
Id serverId = this.globalServerInfo.serverId();
public synchronized void initServerInfo(GlobalMasterInfo nodeInfo) {
E.checkArgument(nodeInfo != null, "The global node info can't be null");

Id serverId = nodeInfo.nodeId();
HugeServerInfo existed = this.serverInfo(serverId);
E.checkArgument(existed == null || !existed.alive(),
"The server with name '%s' already in cluster",
serverId);
if (this.globalServerInfo.serverRole().master()) {

if (nodeInfo.nodeRole().master()) {
String page = this.supportsPaging() ? PageInfo.PAGE_NONE : null;
do {
Iterator<HugeServerInfo> servers = this.serverInfos(PAGE_SIZE, page);
Expand All @@ -127,55 +127,72 @@ public synchronized void initServerInfo(GlobalMasterInfo serverInfo) {
} while (page != null);
}

// TODO: save ServerInfo at AuthServer
this.saveServerInfo(this.selfServerId(), this.selfServerRole());
this.globalNodeInfo = nodeInfo;

// TODO: save ServerInfo to AuthServer
this.saveServerInfo(this.selfNodeId(), this.selfNodeRole());
}

public synchronized void changeServerRole(NodeRole serverRole) {
public synchronized void changeServerRole(NodeRole nodeRole) {
if (this.closed) {
return;

Check warning on line 138 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L138 was not covered by tests
}

this.globalServerInfo.changeServerRole(serverRole);
this.globalNodeInfo.changeNodeRole(nodeRole);

this.saveServerInfo(this.selfServerId(), this.selfServerRole());
// TODO: save ServerInfo to AuthServer
this.saveServerInfo(this.selfNodeId(), this.selfNodeRole());
}

public GlobalMasterInfo serverInfo() {
return this.globalServerInfo;
public GlobalMasterInfo globalNodeRoleInfo() {
return this.globalNodeInfo;
}

public Id selfServerId() {
if (this.globalServerInfo == null) {
public Id selfNodeId() {
if (this.globalNodeInfo == null) {
return null;
}
return this.globalServerInfo.serverId();
return this.globalNodeInfo.nodeId();
}

public NodeRole selfServerRole() {
if (this.globalServerInfo == null) {
public NodeRole selfNodeRole() {
if (this.globalNodeInfo == null) {
return null;

Check warning on line 160 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L160 was not covered by tests
}
return this.globalServerInfo.serverRole();
return this.globalNodeInfo.nodeRole();
}

public boolean master() {
return this.selfServerRole() != null && this.selfServerRole().master();
return this.selfNodeRole() != null && this.selfNodeRole().master();
}

public boolean onlySingleNode() {
// Only has one master node
return this.onlySingleNode;
}

public void heartbeat() {
public synchronized void heartbeat() {
assert this.graphReady();

HugeServerInfo serverInfo = this.selfServerInfo();
if (serverInfo == null && this.selfServerId() != null &&
this.selfServerRole() != NodeRole.MASTER) {
serverInfo = this.saveServerInfo(this.selfServerId(), this.selfServerRole());
if (serverInfo != null) {
// Update heartbeat time for this server
serverInfo.updateTime(DateUtil.now());
this.save(serverInfo);
return;
}
serverInfo.updateTime(DateUtil.now());
this.save(serverInfo);

// ServerInfo is missing
if (this.selfNodeId() == null) {
LOG.info("ServerInfo is missing: {}, maybe not be initialized yet");
return;

Check warning on line 188 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L187 - L188 were not covered by tests
}
/*
* Missing server info, may be caused by graph truncated on master node.
* TODO: we just patch it here currently, to be improved.
*/
serverInfo = this.saveServerInfo(this.selfNodeId(), this.selfNodeRole());
assert serverInfo != null;
}

public synchronized void decreaseLoad(int load) {
Expand Down Expand Up @@ -244,8 +261,8 @@ private GraphTransaction tx() {
return this.graph.systemTransaction();
}

private HugeServerInfo saveServerInfo(Id server, NodeRole role) {
HugeServerInfo serverInfo = new HugeServerInfo(server, role);
private HugeServerInfo saveServerInfo(Id nodeId, NodeRole nodeRole) {
HugeServerInfo serverInfo = new HugeServerInfo(nodeId, nodeRole);
serverInfo.maxLoad(this.calcMaxLoad());
this.save(serverInfo);

Expand Down Expand Up @@ -312,16 +329,16 @@ private <V> V call(Callable<V> callable) {
}

private HugeServerInfo selfServerInfo() {
HugeServerInfo selfServerInfo = this.serverInfo(this.selfServerId());
if (selfServerInfo == null) {
LOG.warn("ServerInfo is missing: {}", this.selfServerId());
HugeServerInfo selfServerInfo = this.serverInfo(this.selfNodeId());
if (selfServerInfo == null && this.selfNodeId() != null) {
LOG.warn("ServerInfo is missing: {}", this.selfNodeId());
}
return selfServerInfo;
}

private HugeServerInfo serverInfo(Id server) {
private HugeServerInfo serverInfo(Id serverId) {
return this.call(() -> {
Iterator<Vertex> vertices = this.tx().queryVertices(server);
Iterator<Vertex> vertices = this.tx().queryVertices(serverId);
Vertex vertex = QueryResults.one(vertices);
if (vertex == null) {
return null;
Expand All @@ -337,19 +354,19 @@ private HugeServerInfo removeSelfServerInfo() {
* backend store, initServerInfo() is not called in this case, so
* this.selfServerId is null at this time.
*/
if (this.selfServerId() != null && this.graph.initialized()) {
return this.removeServerInfo(this.selfServerId());
if (this.selfNodeId() != null && this.graph.initialized()) {
return this.removeServerInfo(this.selfNodeId());
}
return null;
}

private HugeServerInfo removeServerInfo(Id server) {
if (server == null) {
private HugeServerInfo removeServerInfo(Id serverId) {
if (serverId == null) {
return null;
}
LOG.info("Remove server info: {}", server);
LOG.info("Remove server info: {}", serverId);
return this.call(() -> {
Iterator<Vertex> vertices = this.tx().queryVertices(server);
Iterator<Vertex> vertices = this.tx().queryVertices(serverId);
Vertex vertex = QueryResults.one(vertices);
if (vertex == null) {
return null;
Expand Down
Loading

0 comments on commit cd05642

Please sign in to comment.