From c9f0c7b7ef8167eee2ee186d34f9d6ca28d73538 Mon Sep 17 00:00:00 2001 From: vaughn Date: Wed, 1 Mar 2023 13:47:56 +0800 Subject: [PATCH] chore: improve stable --- .../apache/hugegraph/api/job/GremlinAPI.java | 1 - .../apache/hugegraph/api/job/RebuildAPI.java | 1 - .../apache/hugegraph/config/CoreOptions.java | 12 ++-- .../StandardRoleElectionStateMachine.java | 6 ++ .../election/StandardRoleTypeDataAdapter.java | 60 ++++++++++++------- .../StandardStateMachineCallback.java | 7 --- 6 files changed, 51 insertions(+), 36 deletions(-) diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java index c343d2df2a..62e7913678 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/GremlinAPI.java @@ -59,7 +59,6 @@ @Path("graphs/{graph}/jobs/gremlin") @Singleton @Tag(name = "GremlinAPI") -@RedirectFilter.RedirectMasterRole public class GremlinAPI extends API { private static final Logger LOG = Log.logger(GremlinAPI.class); diff --git a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java index 0ac93811ef..0fe87ee0fd 100644 --- a/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java +++ b/hugegraph-api/src/main/java/org/apache/hugegraph/api/job/RebuildAPI.java @@ -43,7 +43,6 @@ @Path("graphs/{graph}/jobs/rebuild") @Singleton @Tag(name = "RebuildAPI") -@RedirectFilter.RedirectMasterRole public class RebuildAPI extends API { private static final Logger LOG = Log.logger(RebuildAPI.class); diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/config/CoreOptions.java b/hugegraph-core/src/main/java/org/apache/hugegraph/config/CoreOptions.java index d6cfcf0bc9..347d6f4877 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/config/CoreOptions.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/config/CoreOptions.java @@ -632,7 +632,7 @@ public static synchronized CoreOptions instance() { "server.role.fail_count", "The role state machine fail count exceeds", rangeInt(0, Integer.MAX_VALUE), - 2 + 5 ); public static final ConfigOption NODE_EXTERNAL_URL = @@ -648,7 +648,7 @@ public static synchronized CoreOptions instance() { "server.role.random_timeout", "The role state machine random timeout millisecond time", rangeInt(0, Integer.MAX_VALUE), - 400 + 1000 ); public static final ConfigOption HEARTBEAT_INTERVAL_SECOUND = @@ -656,16 +656,16 @@ public static synchronized CoreOptions instance() { "server.role.heartbeat_interval", "The role state machine heartbeat interval second time", rangeInt(0, Integer.MAX_VALUE), - 1 + 2 ); public static final ConfigOption EXCEEDS_WORKER_COUNT = new ConfigOption<>( "server.role.worker_count", "Check the number of times that the master node does not initiate " + - "the heartbeat threshold", + "the heartbeat threshold", rangeInt(0, Integer.MAX_VALUE), - 5 + 10 ); public static final ConfigOption BASE_TIMEOUT_MILLISECOND = @@ -673,6 +673,6 @@ public static synchronized CoreOptions instance() { "server.role.base_timeout", "The role state machine candidate state base timeout time", rangeInt(0, Integer.MAX_VALUE), - 100 + 500 ); } diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleElectionStateMachine.java b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleElectionStateMachine.java index 0af2483bd3..726bd8865d 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleElectionStateMachine.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleElectionStateMachine.java @@ -22,6 +22,8 @@ import java.util.concurrent.locks.LockSupport; import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; public class StandardRoleElectionStateMachine implements RoleElectionStateMachine { @@ -30,6 +32,8 @@ public class StandardRoleElectionStateMachine implements RoleElectionStateMachin private volatile RoleState state; private final RoleTypeDataAdapter roleTypeDataAdapter; + private static final Logger LOG = Log.logger(StandardRoleElectionStateMachine.class); + public StandardRoleElectionStateMachine(Config config, RoleTypeDataAdapter adapter) { this.config = config; this.roleTypeDataAdapter = adapter; @@ -49,7 +53,9 @@ public void apply(StateMachineCallback stateMachineCallback) { while (!this.shutdown) { E.checkArgumentNotNull(this.state, "State don't be null"); try { + RoleState pre = this.state; this.state = state.transform(context); + LOG.trace("server {} epoch {} role state change {} to {}", context.node(), context.epoch(), pre.getClass().getSimpleName(), this.state.getClass().getSimpleName()); Callback runnable = this.state.callback(stateMachineCallback); runnable.call(context); failCount = 0; diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleTypeDataAdapter.java b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleTypeDataAdapter.java index 7fde15b965..2e3a4c1b7d 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleTypeDataAdapter.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardRoleTypeDataAdapter.java @@ -34,46 +34,55 @@ import org.apache.hugegraph.type.HugeType; import org.apache.hugegraph.type.define.DataType; import org.apache.hugegraph.type.define.HugeKeys; +import org.apache.hugegraph.util.Log; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.T; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.slf4j.Logger; public class StandardRoleTypeDataAdapter implements RoleTypeDataAdapter { private final HugeGraphParams graphParams; private final Schema schema; + private static final Logger LOG = Log.logger(StandardRoleTypeDataAdapter.class); + + private boolean first; + public StandardRoleTypeDataAdapter(HugeGraphParams graph) { this.graphParams = graph; this.schema = new Schema(graph); this.schema.initSchemaIfNeeded(); + this.first = true; } @Override public boolean updateIfNodePresent(RoleTypeData stateData) { // if epoch increase, update and return true // if epoch equal, ignore different node, return false - try { - Optional oldTypeDataOpt = this.queryVertex(); - if (oldTypeDataOpt.isPresent()) { - RoleTypeData oldTypeData = this.from(oldTypeDataOpt.get()); - if (stateData.epoch() < oldTypeData.epoch()) { - return false; - } - - if (stateData.epoch() == oldTypeData.epoch() && - !Objects.equals(stateData.node(), oldTypeData.node())) { - return false; - } - - this.graphParams.systemTransaction().removeVertex((HugeVertex) oldTypeDataOpt.get()); - this.graphParams.systemTransaction().commitOrRollback(); + Optional oldTypeDataOpt = this.queryVertex(); + if (oldTypeDataOpt.isPresent()) { + RoleTypeData oldTypeData = this.from(oldTypeDataOpt.get()); + if (stateData.epoch() < oldTypeData.epoch()) { + return false; } + if (stateData.epoch() == oldTypeData.epoch() && + !Objects.equals(stateData.node(), oldTypeData.node())) { + return false; + } + LOG.trace("Server {} epoch {} begin remove data old epoch {}, ", stateData.node(), stateData.epoch(), oldTypeData.epoch()); + this.graphParams.systemTransaction().removeVertex((HugeVertex) oldTypeDataOpt.get()); + this.graphParams.systemTransaction().commitOrRollback(); + LOG.trace("Server {} epoch {} success remove data old epoch {}, ", stateData.node(), stateData.epoch(), oldTypeData.epoch()); + } + try { GraphTransaction tx = this.graphParams.systemTransaction(); tx.doUpdateIfAbsent(this.constructEntry(stateData)); tx.commitOrRollback(); + LOG.trace("Server {} epoch {} success update data", stateData.node(), stateData.epoch()); } catch (Throwable ignore){ + LOG.trace("Server {} epoch {} fail update data", stateData.node(), stateData.epoch()); return false; } @@ -109,6 +118,16 @@ private BackendEntry constructEntry(RoleTypeData stateData) { @Override public Optional query() { Optional vertex = this.queryVertex(); + if (!vertex.isPresent() && !this.first) { + // If query nothing, retry once + try { + Thread.sleep(200); + } catch (InterruptedException ignore) { + } + + vertex = this.queryVertex(); + } + this.first = false; return vertex.map(this::from); } @@ -131,7 +150,6 @@ private Optional queryVertex() { query.showHidden(true); Iterator vertexIterator = tx.queryVertices(query); if (vertexIterator.hasNext()) { - return Optional.of(vertexIterator.next()); } @@ -171,11 +189,11 @@ public void initSchemaIfNeeded() { String[] properties = this.initProperties(); VertexLabel label = this.schema().vertexLabel(this.label) - .enableLabelIndex(true) - .usePrimaryKeyId() - .primaryKeys(P.TYPE) - .properties(properties) - .build(); + .enableLabelIndex(true) + .usePrimaryKeyId() + .primaryKeys(P.TYPE) + .properties(properties) + .build(); this.graph.schemaTransaction().addVertexLabel(label); } diff --git a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardStateMachineCallback.java b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardStateMachineCallback.java index a572ffd08c..d72172c8dd 100644 --- a/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardStateMachineCallback.java +++ b/hugegraph-core/src/main/java/org/apache/hugegraph/election/StandardStateMachineCallback.java @@ -60,13 +60,6 @@ public void onAsRoleWorker(StateMachineContext context) { @Override public void onAsRoleCandidate(StateMachineContext context) { - if (isMaster) { - this.taskManager.onAsRoleWorker(); - this.initGlobalMasterInfo(context); - LOG.info("Server {} change to worker role", context.config().node()); - } - - isMaster = false; } @Override