Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move server info into GlobalMasterInfo #2370

Merged
merged 4 commits into from
Dec 4, 2023
Merged
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 @@ -82,16 +82,16 @@
return;
}

GlobalMasterInfo globalMasterInfo = manager.globalMasterInfo();
if (globalMasterInfo == null || !globalMasterInfo.isFeatureSupport()) {
GlobalMasterInfo globalNodeInfo = manager.globalNodeRoleInfo();
if (globalNodeInfo == null || !globalNodeInfo.supportElection()) {
return;
}
GlobalMasterInfo.NodeInfo masterNodeInfo = globalMasterInfo.nodeInfo();
if (masterNodeInfo == null || masterNodeInfo.isMaster() ||
StringUtils.isEmpty(masterNodeInfo.url())) {
GlobalMasterInfo.NodeInfo masterInfo = globalNodeInfo.masterInfo();
if (masterInfo == null || masterInfo.isMaster() ||
StringUtils.isEmpty(masterInfo.nodeUrl())) {
return;
}
String url = masterNodeInfo.url();
String url = masterInfo.nodeUrl();

Check warning on line 94 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/RedirectFilter.java#L94

Added line #L94 was not covered by tests

URI redirectUri;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.hugegraph.exception.NotSupportException;
import org.apache.hugegraph.iterator.FilterIterator;
import org.apache.hugegraph.iterator.MapperIterator;
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.rpc.RpcServiceConfig4Client;
import org.apache.hugegraph.rpc.RpcServiceConfig4Server;
Expand All @@ -78,7 +79,6 @@
import org.apache.hugegraph.type.Nameable;
import org.apache.hugegraph.type.define.GraphMode;
import org.apache.hugegraph.type.define.GraphReadMode;
import org.apache.hugegraph.type.define.NodeRole;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.Log;
import org.apache.hugegraph.util.RateLimiter;
Expand Down Expand Up @@ -669,9 +669,9 @@
}

@Override
public void serverStarted(Id serverId, NodeRole serverRole) {
public void serverStarted(GlobalMasterInfo nodeInfo) {
this.verifyAdminPermission();
this.hugegraph.serverStarted(serverId, serverRole);
this.hugegraph.serverStarted(nodeInfo);
}

@Override
Expand Down Expand Up @@ -776,9 +776,9 @@
}

@Override
public void create(String configPath, Id server, NodeRole role) {
public void create(String configPath, GlobalMasterInfo nodeInfo) {
this.verifyPermission(HugePermission.WRITE, ResourceType.STATUS);
this.hugegraph.create(configPath, server, role);
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 @@ -23,8 +23,6 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -41,7 +39,6 @@
import org.apache.hugegraph.backend.BackendException;
import org.apache.hugegraph.backend.cache.Cache;
import org.apache.hugegraph.backend.cache.CacheManager;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.id.IdGenerator;
import org.apache.hugegraph.backend.store.BackendStoreInfo;
import org.apache.hugegraph.config.CoreOptions;
Expand All @@ -53,7 +50,7 @@
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.masterelection.RoleElectionOptions;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.masterelection.StandardStateMachineCallback;
import org.apache.hugegraph.masterelection.StandardRoleListener;
import org.apache.hugegraph.metrics.MetricsUtil;
import org.apache.hugegraph.metrics.ServerReporter;
import org.apache.hugegraph.rpc.RpcClientProvider;
Expand Down Expand Up @@ -88,14 +85,11 @@
private final HugeAuthenticator authenticator;
private final RpcServer rpcServer;
private final RpcClientProvider rpcClient;
private final HugeConfig conf;

private RoleElectionStateMachine roleStateWorker;
private GlobalMasterInfo globalMasterInfo;

private Id server;
private NodeRole role;
private RoleElectionStateMachine roleStateMachine;
private GlobalMasterInfo globalNodeRoleInfo;

private final HugeConfig conf;
private final EventHub eventHub;

public GraphManager(HugeConfig conf, EventHub hub) {
Expand All @@ -104,6 +98,10 @@
this.authenticator = HugeAuthenticator.loadAuthenticator(conf);
this.rpcServer = new RpcServer(conf);
this.rpcClient = new RpcClientProvider(conf);

this.roleStateMachine = null;
this.globalNodeRoleInfo = new GlobalMasterInfo();

this.eventHub = hub;
this.conf = conf;
}
Expand Down Expand Up @@ -141,8 +139,7 @@
}
}

public HugeGraph cloneGraph(String name, String newName,
String configText) {
public HugeGraph cloneGraph(String name, String newName, String configText) {
/*
* 0. check and modify params
* 1. create graph instance
Expand Down Expand Up @@ -270,6 +267,10 @@
return this.authenticator().authManager();
}

public GlobalMasterInfo globalNodeRoleInfo() {
return this.globalNodeRoleInfo;
}

public void close() {
for (Graph graph : this.graphs.values()) {
try {
Expand All @@ -280,8 +281,8 @@
}
this.destroyRpcServer();
this.unlistenChanges();
if (this.roleStateWorker != null) {
this.roleStateWorker.shutdown();
if (this.roleStateMachine != null) {
this.roleStateMachine.shutdown();

Check warning on line 285 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java#L285

Added line #L285 was not covered by tests
}
}

Expand Down Expand Up @@ -414,8 +415,7 @@
LOG.info("RpcServer is not enabled, skip wait graphs ready");
return;
}
com.alipay.remoting.rpc.RpcServer remotingRpcServer =
this.remotingRpcServer();
com.alipay.remoting.rpc.RpcServer remotingRpcServer = this.remotingRpcServer();
for (String graphName : this.graphs.keySet()) {
HugeGraph graph = this.graph(graphName);
graph.waitReady(remotingRpcServer);
Expand All @@ -433,7 +433,7 @@
if (this.requireAuthentication()) {
String token = config.get(ServerOptions.AUTH_ADMIN_TOKEN);
try {
this.authenticator.initAdminUser(token);
this.authenticator().initAdminUser(token);
} catch (Exception e) {
throw new BackendException(
"The backend store of '%s' can't " +
Expand All @@ -455,65 +455,57 @@
}

private void serverStarted(HugeConfig config) {
String server = config.get(ServerOptions.SERVER_ID);
String id = config.get(ServerOptions.SERVER_ID);
String role = config.get(ServerOptions.SERVER_ROLE);
E.checkArgument(StringUtils.isNotEmpty(server),
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");
this.server = IdGenerator.of(server);
this.role = NodeRole.valueOf(role.toUpperCase());

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

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

for (String graph : this.graphs()) {
HugeGraph hugegraph = this.graph(graph);
assert hugegraph != null;
hugegraph.serverStarted(this.server, this.role);
hugegraph.serverStarted(this.globalNodeRoleInfo);
}

if (supportRoleStateWorker) {
this.initRoleStateWorker();
if (supportRoleElection) {
this.initRoleStateMachine();
}
}

private void initRoleStateWorker() {
E.checkArgument(this.roleStateWorker == null, "Repetition init");
Executor applyThread = Executors.newSingleThreadExecutor();
this.roleStateWorker = this.authenticator().graph().roleElectionStateMachine();
this.globalMasterInfo = new GlobalMasterInfo();
StandardStateMachineCallback stateMachineCallback = new StandardStateMachineCallback(
TaskManager.instance(),
this.globalMasterInfo);
applyThread.execute(() -> {
this.roleStateWorker.apply(stateMachineCallback);
});
}

public GlobalMasterInfo globalMasterInfo() {
return this.globalMasterInfo;
private void initRoleStateMachine() {
E.checkArgument(this.roleStateMachine == null,
"Repeated initialization of role state worker");
this.globalNodeRoleInfo.supportElection(true);
this.roleStateMachine = this.authenticator().graph().roleElectionStateMachine();
StandardRoleListener listener = new StandardRoleListener(TaskManager.instance(),
this.globalNodeRoleInfo);
this.roleStateMachine.start(listener);
}

private boolean supportRoleStateWorker() {
if (this.role.computer()) {
return false;
}

private boolean supportRoleElection() {
try {
if (!(this.authenticator() instanceof StandardAuthenticator)) {
LOG.info("{} authenticator does not support role election currently",
this.authenticator().getClass().getSimpleName());
return false;
}
return true;
} catch (IllegalStateException e) {
LOG.info("Unconfigured StandardAuthenticator, not support role election currently");
LOG.info("{}, does not support role election currently", e.getMessage());

Check warning on line 506 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java#L506

Added line #L506 was not covered by tests
return false;
}

return true;
}

private void addMetrics(HugeConfig config) {
Expand Down Expand Up @@ -591,7 +583,7 @@
graph = (HugeGraph) GraphFactory.open(config);

// Init graph and start it
graph.create(this.graphsDir, this.server, this.role);
graph.create(this.graphsDir, this.globalNodeRoleInfo);

Check warning on line 586 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java#L586

Added line #L586 was not covered by tests
} catch (Throwable e) {
LOG.error("Failed to create graph '{}' due to: {}",
name, e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.hugegraph.backend.store.raft.RaftGroupManager;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.TypedOption;
import org.apache.hugegraph.masterelection.GlobalMasterInfo;
import org.apache.hugegraph.masterelection.RoleElectionStateMachine;
import org.apache.hugegraph.rpc.RpcServiceConfig4Client;
import org.apache.hugegraph.rpc.RpcServiceConfig4Server;
Expand All @@ -44,12 +45,11 @@
import org.apache.hugegraph.task.TaskScheduler;
import org.apache.hugegraph.traversal.optimize.HugeCountStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeGraphStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy;
import org.apache.hugegraph.traversal.optimize.HugePrimaryKeyStrategy;
import org.apache.hugegraph.traversal.optimize.HugeVertexStepStrategy;
import org.apache.hugegraph.type.HugeType;
import org.apache.hugegraph.type.define.GraphMode;
import org.apache.hugegraph.type.define.GraphReadMode;
import org.apache.hugegraph.type.define.NodeRole;
import org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
Expand Down Expand Up @@ -201,7 +201,7 @@ public interface HugeGraph extends Graph {

void waitReady(RpcServer rpcServer);

void serverStarted(Id serverId, NodeRole serverRole);
void serverStarted(GlobalMasterInfo nodeInfo);

boolean started();

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

void resumeSnapshot();

void create(String configPath, Id server, NodeRole role);
void create(String configPath, GlobalMasterInfo nodeInfo);

void drop();

Expand Down
Loading
Loading