You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tubemq.apache.org by go...@apache.org on 2020/12/24 06:06:17 UTC

[incubator-tubemq] branch TUBEMQ-455 updated: [TUBEMQ-477] implement state machine for tasks and jobs

This is an automated email from the ASF dual-hosted git repository.

gosonzhang pushed a commit to branch TUBEMQ-455
in repository https://gitbox.apache.org/repos/asf/incubator-tubemq.git


The following commit(s) were added to refs/heads/TUBEMQ-455 by this push:
     new 1ccae5c  [TUBEMQ-477] implement state machine for tasks and jobs
1ccae5c is described below

commit 1ccae5c8026a2012ce53996cefd0f0060ab87e87
Author: yuanbo <yu...@apache.org>
AuthorDate: Thu Dec 24 10:49:44 2020 +0800

    [TUBEMQ-477] implement state machine for tasks and jobs
---
 .../tubemq/agent/state/AbstractStateWrapper.java   | 92 ++++++++++++++++++++++
 .../java/org/apache/tubemq/agent/state/State.java  | 23 ++++++
 .../apache/tubemq/agent/state/StateCallback.java   | 28 +++++++
 .../tubemq/agent/state/StateTransferException.java | 24 ++++++
 4 files changed, 167 insertions(+)

diff --git a/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/AbstractStateWrapper.java b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/AbstractStateWrapper.java
new file mode 100644
index 0000000..ea0699e
--- /dev/null
+++ b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/AbstractStateWrapper.java
@@ -0,0 +1,92 @@
+/**
+ * Licensed 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.tubemq.agent.state;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class AbstractStateWrapper implements Runnable {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStateWrapper.class);
+
+    private final Map<Pair<State, State>, StateCallback> callBacks = new HashMap<>();
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+    private State currentState = State.ACCEPTED;
+
+    public AbstractStateWrapper() {
+        addCallbacks();
+    }
+
+    /**
+     * add callback for state change
+     */
+    public abstract void addCallbacks();
+
+
+    public AbstractStateWrapper addCallback(State begin, State end, StateCallback callback) {
+        callBacks.put(new ImmutablePair<>(begin, end), callback);
+        return this;
+    }
+
+    /**
+     * change state and execute callback functions
+     *
+     * @param nextState - next state
+     */
+    public synchronized void doChangeState(State nextState) {
+        lock.writeLock().lock();
+        try {
+            Pair<State, State> statePair = new ImmutablePair<>(currentState, nextState);
+            StateCallback callback = callBacks.get(statePair);
+            if (callback != null) {
+                callback.call(currentState, nextState);
+            }
+            currentState = nextState;
+
+        } finally {
+            lock.writeLock().unlock();
+        }
+
+    }
+
+    /**
+     * whether is in exception
+     *
+     * @return - true if in exception else false
+     */
+    public boolean isException() {
+        State tmpState = currentState;
+        return State.KILLED.equals(tmpState) || State.FAILED.equals(tmpState) || State.FATAL.equals(tmpState);
+    }
+
+    public boolean isFinished() {
+        State tmpState = currentState;
+        return State.FATAL.equals(tmpState) || State.SUCCEEDED.equals(tmpState) || State.KILLED.equals(tmpState);
+    }
+
+    public boolean isSuccess() {
+        return State.SUCCEEDED.equals(currentState);
+    }
+
+    public boolean isFailed() {
+        return State.FAILED.equals(currentState);
+    }
+
+}
diff --git a/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/State.java b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/State.java
new file mode 100644
index 0000000..a95c282
--- /dev/null
+++ b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/State.java
@@ -0,0 +1,23 @@
+/**
+ * Licensed 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.tubemq.agent.state;
+
+public enum State {
+    ACCEPTED,
+    RUNNING,
+    SUCCEEDED,
+    FAILED,
+    KILLED,
+    FATAL
+}
diff --git a/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateCallback.java b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateCallback.java
new file mode 100644
index 0000000..517caed
--- /dev/null
+++ b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateCallback.java
@@ -0,0 +1,28 @@
+/**
+ * Licensed 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.tubemq.agent.state;
+
+/**
+ * state call back
+ */
+public interface StateCallback {
+
+    /**
+     * state change call back
+     *
+     * @param before - before state
+     * @param after - after state
+     */
+    void call(State before, State after);
+}
diff --git a/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateTransferException.java b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateTransferException.java
new file mode 100644
index 0000000..ca1e9af
--- /dev/null
+++ b/tubemq-agent/agent-common/src/main/java/org/apache/tubemq/agent/state/StateTransferException.java
@@ -0,0 +1,24 @@
+/**
+ * Licensed 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.tubemq.agent.state;
+
+/**
+ * state transfer exception
+ */
+public class StateTransferException extends RuntimeException {
+
+    public StateTransferException(State begin, State end) {
+        super(String.format("%s -> %s not allowed", begin, end));
+    }
+}