You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hugegraph.apache.org by "javeme (via GitHub)" <gi...@apache.org> on 2023/02/26 13:17:18 UTC

[GitHub] [incubator-hugegraph] javeme commented on a diff in pull request #2130: feat: support task auto manage by server role state machine

javeme commented on code in PR #2130:
URL: https://github.com/apache/incubator-hugegraph/pull/2130#discussion_r1118081443


##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -428,13 +443,43 @@ private void serverStarted(HugeConfig config) {
                         "The server role can't be null or empty");
         this.server = IdGenerator.of(server);
         this.role = NodeRole.valueOf(role.toUpperCase());
+
+        initRoleStateMachine(config, server);

Review Comment:
   keep prefix this.initRoleStateMachine



##########
hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java:
##########
@@ -87,6 +88,12 @@ public AuthManager authManager() {
                   "AuthManager is unsupported by ConfigAuthenticator");
     }
 
+    @Override
+    public HugeGraph graph() {
+        throw new NotImplementedException(
+                  "AuthManager is unsupported by ConfigAuthenticator");

Review Comment:
   graph()



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -82,6 +91,9 @@ public final class GraphManager {
     private final RpcClientProvider rpcClient;
     private final HugeConfig conf;
 
+    private RoleElectionStateMachine roleStateMachine;
+    private Executor applyThread;

Review Comment:
   prefer roleStateWorker



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -428,13 +443,43 @@ private void serverStarted(HugeConfig config) {
                         "The server role can't be null or empty");
         this.server = IdGenerator.of(server);
         this.role = NodeRole.valueOf(role.toUpperCase());
+
+        initRoleStateMachine(config, server);
+
         for (String graph : this.graphs()) {
             HugeGraph hugegraph = this.graph(graph);
             assert hugegraph != null;
             hugegraph.serverStarted(this.server, this.role);
         }
     }
 
+    private void initRoleStateMachine(HugeConfig config, String server) {
+        try {
+            if (!(this.authenticator() instanceof StandardAuthenticator)) {
+                LOG.info("Current not support role state machine");
+                return;
+            }
+        } catch (IllegalStateException e) {
+            LOG.info("Current not support role state machine");
+            return;
+        }
+
+        E.checkArgument(this.roleStateMachine == null, "Repetition init");
+        Config roleStateMachineConfig = new HugeRoleStateMachineConfig(server,
+                                            config.get(ServerOptions.EXCEEDS_FAIL_COUNT),
+                                            config.get(ServerOptions.RANDOM_TIMEOUT_MILLISECOND),
+                                            config.get(ServerOptions.HEARTBEAT_INTERVAL_SECOUND),
+                                            config.get(ServerOptions.EXCEEDS_WORKER_COUNT),
+                                            config.get(ServerOptions.BASE_TIMEOUT_MILLISECOND));
+        StandardHugeGraph graph = (StandardHugeGraph) this.authenticator().graph().hugegraph();
+        RoleTypeDataAdapter adapter = new RoleTypeDataAdapterImpl(graph.hugeGraphParams());
+        this.roleStateMachine = new RoleElectionStateMachineImpl(roleStateMachineConfig, adapter);
+        applyThread = Executors.newSingleThreadExecutor();

Review Comment:
   this.applyThread



##########
hugegraph-core/src/main/java/org/apache/hugegraph/task/ServerInfoManager.java:
##########
@@ -52,7 +51,7 @@
 import org.apache.hugegraph.util.Log;
 import com.google.common.collect.ImmutableMap;
 
-public class ServerInfoManager {
+public class ServerInfoManager{

Review Comment:
   expect a space



##########
hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java:
##########
@@ -318,6 +318,10 @@ protected synchronized void scheduleTasks() {
                     continue;
                 }
 
+                if (!this.serverManager.master()) {
+                    return;

Review Comment:
   can add some comments for why and when?



##########
hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java:
##########
@@ -1063,6 +1063,10 @@ public String toString() {
         return StringFactory.graphString(this, this.name());
     }
 
+    public HugeGraphParams hugeGraphParams() {

Review Comment:
   it's not allowed to pass HugeGraphParams out, we need to prevent the underlying method from being called by gremlin without authorization.



##########
hugegraph-core/src/main/java/org/apache/hugegraph/task/TaskManager.java:
##########
@@ -58,6 +59,8 @@ public final class TaskManager {
     private final ExecutorService serverInfoDbExecutor;
     private final PausableScheduledThreadPool schedulerExecutor;
 
+    private boolean useRoleStateMachine = false;

Review Comment:
   rename to enableRoleElected?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/StateMachineCallbackImpl.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.core;
+
+import org.apache.hugegraph.election.StateMachineCallback;
+import org.apache.hugegraph.election.StateMachineContext;
+import org.apache.hugegraph.task.TaskManager;
+import org.apache.hugegraph.util.Log;
+import org.slf4j.Logger;
+
+public class StateMachineCallbackImpl implements StateMachineCallback {
+
+    private static final Logger LOG = Log.logger(StateMachineCallbackImpl.class);
+
+    private final TaskManager taskManager;
+
+    boolean isMaster = false;
+
+    public StateMachineCallbackImpl(TaskManager taskManager) {
+        this.taskManager = taskManager;
+        this.taskManager.useRoleStateMachine(true);
+    }
+    @Override
+    public void master(StateMachineContext context) {

Review Comment:
   onAsRoleMaster?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/config/ServerOptions.java:
##########
@@ -64,6 +64,47 @@ public static synchronized ServerOptions instance() {
                     "master"
             );
 
+    public static final ConfigOption<Integer> EXCEEDS_FAIL_COUNT =
+            new ConfigOption<>(
+                    "server.role.fail.count",

Review Comment:
   fail_count



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/StateMachineCallbackImpl.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.core;
+
+import org.apache.hugegraph.election.StateMachineCallback;
+import org.apache.hugegraph.election.StateMachineContext;
+import org.apache.hugegraph.task.TaskManager;
+import org.apache.hugegraph.util.Log;
+import org.slf4j.Logger;
+
+public class StateMachineCallbackImpl implements StateMachineCallback {
+
+    private static final Logger LOG = Log.logger(StateMachineCallbackImpl.class);
+
+    private final TaskManager taskManager;
+
+    boolean isMaster = false;
+
+    public StateMachineCallbackImpl(TaskManager taskManager) {
+        this.taskManager = taskManager;
+        this.taskManager.useRoleStateMachine(true);
+    }
+    @Override
+    public void master(StateMachineContext context) {
+        if (!isMaster) {
+            this.taskManager.onMaster();

Review Comment:
   onAsRoleMaster?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -428,13 +443,43 @@ private void serverStarted(HugeConfig config) {
                         "The server role can't be null or empty");
         this.server = IdGenerator.of(server);
         this.role = NodeRole.valueOf(role.toUpperCase());
+
+        initRoleStateMachine(config, server);
+
         for (String graph : this.graphs()) {
             HugeGraph hugegraph = this.graph(graph);
             assert hugegraph != null;
             hugegraph.serverStarted(this.server, this.role);
         }
     }
 
+    private void initRoleStateMachine(HugeConfig config, String server) {
+        try {
+            if (!(this.authenticator() instanceof StandardAuthenticator)) {
+                LOG.info("Current not support role state machine");
+                return;
+            }
+        } catch (IllegalStateException e) {
+            LOG.info("Current not support role state machine");

Review Comment:
   ditto



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -428,13 +443,43 @@ private void serverStarted(HugeConfig config) {
                         "The server role can't be null or empty");
         this.server = IdGenerator.of(server);
         this.role = NodeRole.valueOf(role.toUpperCase());
+
+        initRoleStateMachine(config, server);
+
         for (String graph : this.graphs()) {
             HugeGraph hugegraph = this.graph(graph);
             assert hugegraph != null;
             hugegraph.serverStarted(this.server, this.role);
         }
     }
 
+    private void initRoleStateMachine(HugeConfig config, String server) {
+        try {
+            if (!(this.authenticator() instanceof StandardAuthenticator)) {
+                LOG.info("Current not support role state machine");

Review Comment:
   XX authenticator does not support role election currently



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/RoleTypeDataAdapterImpl.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.core;

Review Comment:
   add a `serverrole` package?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -428,13 +443,43 @@ private void serverStarted(HugeConfig config) {
                         "The server role can't be null or empty");
         this.server = IdGenerator.of(server);
         this.role = NodeRole.valueOf(role.toUpperCase());
+
+        initRoleStateMachine(config, server);
+
         for (String graph : this.graphs()) {
             HugeGraph hugegraph = this.graph(graph);
             assert hugegraph != null;
             hugegraph.serverStarted(this.server, this.role);
         }
     }
 
+    private void initRoleStateMachine(HugeConfig config, String server) {

Review Comment:
   can move this method to RoleElectionStateMachine class?



##########
hugegraph-core/src/main/java/org/apache/hugegraph/election/HugeRoleStateMachineConfig.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.election;
+
+public class HugeRoleStateMachineConfig implements Config {
+
+    String node;

Review Comment:
   mark private for members



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/StateMachineCallbackImpl.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.core;
+
+import org.apache.hugegraph.election.StateMachineCallback;
+import org.apache.hugegraph.election.StateMachineContext;
+import org.apache.hugegraph.task.TaskManager;
+import org.apache.hugegraph.util.Log;
+import org.slf4j.Logger;
+
+public class StateMachineCallbackImpl implements StateMachineCallback {
+
+    private static final Logger LOG = Log.logger(StateMachineCallbackImpl.class);
+
+    private final TaskManager taskManager;
+
+    boolean isMaster = false;

Review Comment:
   mark private volatile?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/StateMachineCallbackImpl.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.core;
+
+import org.apache.hugegraph.election.StateMachineCallback;
+import org.apache.hugegraph.election.StateMachineContext;
+import org.apache.hugegraph.task.TaskManager;
+import org.apache.hugegraph.util.Log;
+import org.slf4j.Logger;
+
+public class StateMachineCallbackImpl implements StateMachineCallback {
+
+    private static final Logger LOG = Log.logger(StateMachineCallbackImpl.class);
+
+    private final TaskManager taskManager;
+
+    boolean isMaster = false;
+
+    public StateMachineCallbackImpl(TaskManager taskManager) {
+        this.taskManager = taskManager;
+        this.taskManager.useRoleStateMachine(true);
+    }

Review Comment:
   expect a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/core/StateMachineCallbackImpl.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.core;
+
+import org.apache.hugegraph.election.StateMachineCallback;
+import org.apache.hugegraph.election.StateMachineContext;
+import org.apache.hugegraph.task.TaskManager;
+import org.apache.hugegraph.util.Log;
+import org.slf4j.Logger;
+
+public class StateMachineCallbackImpl implements StateMachineCallback {

Review Comment:
   not submit StateMachineCallback file?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org