Sxnan commented on code in PR #138:
URL: https://github.com/apache/flink-agents/pull/138#discussion_r2348857687


##########
runtime/src/main/java/org/apache/flink/agents/runtime/actionstate/ActionStateUtil.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.flink.agents.runtime.actionstate;
+
+import org.apache.flink.agents.api.Event;
+import org.apache.flink.agents.plan.Action;
+
+import java.nio.charset.StandardCharsets;
+import java.util.UUID;
+
+/** Utility class for action state related operations. */
+public class ActionStateUtil {
+
+    public static String generateKey(Object key, Action action, Event event) {
+        return key
+                + "-"
+                + event.getId().toString()

Review Comment:
   Correct me if I am wrong, the ID (UUID) of the event is randomly generated 
while instantiating the event, which means that the ID will be different when 
the same event is reprocessed during recovery. 
   
   I think we need to generate the ID based on the content of the event. cc 
@xintongsong 



##########
runtime/src/main/java/org/apache/flink/agents/runtime/context/RunnerContextImpl.java:
##########
@@ -119,4 +124,12 @@ public ReadableConfiguration getConfig() {
     public String getActionName() {
         return actionName;
     }
+
+    @Override
+    public List<MemoryUpdate> getAllMemoryUpdates() {
+        mailboxThreadChecker.run();
+        List<MemoryUpdate> memoryUpdatesCopy = List.copyOf(memoryUpdates);
+        memoryUpdates.clear();

Review Comment:
   Why do we need to clear updates when getting? It feels unintuitive and 
unnecessary at the same time, as the runner context is instantiated on every 
org.apache.flink.agents.runtime.operator.ActionTask#invoke. 



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -301,20 +311,48 @@ private void processActionTaskForKey(Object key) throws 
Exception {
 
         // 2. Invoke the action task.
         createAndSetRunnerContext(actionTask);
-        ActionTask.ActionTaskResult actionTaskResult = actionTask.invoke();
-        for (Event actionOutputEvent : actionTaskResult.getOutputEvents()) {
+
+        boolean isFinished;
+        List<Event> outputEvents;
+        Optional<ActionTask> generatedActionTaskOpt;
+        ActionState actionState = maybeGetActionState(key, actionTask.action, 
actionTask.event);
+        if (actionState != null && actionState.getGeneratedActionTask() == 
null) {
+            isFinished = !actionState.getGeneratedActionTask().isPresent();

Review Comment:
   Is it always true?



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -392,6 +430,22 @@ public void close() throws Exception {
         super.close();
     }
 
+    @Override
+    public void initializeState(StateInitializationContext context) throws 
Exception {

Review Comment:
   I am wondering whether we need this and the `snapshotState` methods here, as 
it just call the super's.



##########
api/src/main/java/org/apache/flink/agents/api/context/RunnerContext.java:
##########
@@ -73,4 +75,12 @@ public interface RunnerContext {
      * @return the configuration for Flink Agents.
      */
     ReadableConfiguration getConfig();
+
+    /**
+     * Gets all the updates made to this MemoryObject since it was created or 
the last time this
+     * method was called.
+     *
+     * @return list of memory updates
+     */
+    List<MemoryUpdate> getAllMemoryUpdates();

Review Comment:
   The `RunnerContext` is a user-facing API, and this method is intended to be 
invoked by the framework only. To avoid the user accidentally calling this 
method, we may introduce an internal interface for the framework.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -301,20 +311,48 @@ private void processActionTaskForKey(Object key) throws 
Exception {
 
         // 2. Invoke the action task.
         createAndSetRunnerContext(actionTask);
-        ActionTask.ActionTaskResult actionTaskResult = actionTask.invoke();
-        for (Event actionOutputEvent : actionTaskResult.getOutputEvents()) {
+
+        boolean isFinished;
+        List<Event> outputEvents;
+        Optional<ActionTask> generatedActionTaskOpt;
+        ActionState actionState = maybeGetActionState(key, actionTask.action, 
actionTask.event);
+        if (actionState != null && actionState.getGeneratedActionTask() == 
null) {

Review Comment:
   Should be `actionState.getGeneratedActionTask().isEmpty()`



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -301,20 +309,47 @@ private void processActionTaskForKey(Object key) throws 
Exception {
 
         // 2. Invoke the action task.
         createAndSetRunnerContext(actionTask);
-        ActionTask.ActionTaskResult actionTaskResult = actionTask.invoke();
-        for (Event actionOutputEvent : actionTaskResult.getOutputEvents()) {
+
+        boolean isFinished = false;
+        List<Event> outputEvents;
+        Optional<ActionTask> generatedActionTaskOpt;
+        ActionState actionState = actionStateStore.get(key, actionTask.action);
+        if (actionState != null) {
+            isFinished = actionState.getGeneratedActionTask().isPresent();
+            outputEvents = actionState.getOutputEvents();
+            generatedActionTaskOpt = actionState.getGeneratedActionTask();
+            for (MemoryUpdate memoryUpdate : actionState.getMemoryUpdates()) {
+                actionTask
+                        .getRunnerContext()
+                        .getShortTermMemory()
+                        .set(memoryUpdate.getPath(), memoryUpdate.getValue());
+            }
+        } else {
+            initActionState(key, actionTask.action, actionTask.event);
+            ActionTask.ActionTaskResult actionTaskResult = actionTask.invoke();
+            persistTaskResult(
+                    key,
+                    actionTask.action,
+                    actionTask.getRunnerContext().getShortTermMemory(),
+                    actionTaskResult);
+            isFinished = actionTaskResult.isFinished();
+            outputEvents = actionTaskResult.getOutputEvents();
+            generatedActionTaskOpt = actionTaskResult.getGeneratedActionTask();
+        }
+
+        for (Event actionOutputEvent : outputEvents) {
             processEvent(key, actionOutputEvent);
         }
 
         boolean currentInputEventFinished = false;
-        if (actionTaskResult.isFinished()) {
+        if (isFinished) {
             builtInMetrics.markActionExecuted(actionTask.action.getName());
             currentInputEventFinished = !currentKeyHasMoreActionTask();
         } else {
-            // If the action task not finished, we should get a new action 
task to execute continue.
-            Optional<ActionTask> generatedActionTaskOpt = 
actionTaskResult.getGeneratedActionTask();
+            // If the action task is not finished, we should get a new action 
task to continue the
+            // execution.
             checkNotNull(
-                    generatedActionTaskOpt.isPresent(),
+                    generatedActionTaskOpt.get(),

Review Comment:
   Good catch!



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to