wenjin272 commented on code in PR #1429:
URL: https://github.com/apache/flink-agents/pull/1429#discussion_r4664940391


##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -496,7 +531,65 @@ public void processWatermark(Watermark mark) throws 
Exception {
 
     private void processWatermarkInternal(Watermark mark) throws Exception {
         eventRouter.getKeySegmentQueue().addWatermark(mark);
-        eventRouter.processEligibleWatermarks(super::processWatermark);
+        processPendingWatermarks();
+    }
+
+    /** Advances event time after earlier inputs finish, then waits for the 
triggered runs. */
+    private void processPendingWatermarks() throws Exception {
+        if (processingWatermarks) {
+            return;
+        }
+        Object previousKey = getCurrentKey();
+        processingWatermarks = true;
+        try {
+            while (true) {
+                // Newly registered timers can already be due, even without 
another watermark.
+                if (eventTimersNeedCheck) {
+                    eventTimersNeedCheck = false;
+                    timeServiceManager.advanceWatermark(
+                            new Watermark(timerService.currentWatermark()));
+                }
+                if (pendingTimerWatermark != null) {
+                    if (!watermarkTimerEvents.isEmpty()) {
+                        return;
+                    }
+                    output.emitWatermark(pendingTimerWatermark);
+                    pendingTimerWatermark = null;
+                }
+                Watermark next = eventRouter.pollEligibleWatermark();
+                if (next == null) {
+                    return;
+                }
+                pendingTimerWatermark = next;
+                timeServiceManager.advanceWatermark(next);
+            }
+        } finally {
+            if (previousKey != null) {
+                setCurrentKey(previousKey);
+            }
+            processingWatermarks = false;
+        }
+    }
+
+    private void scheduleEventTimerCheck() {
+        eventTimersNeedCheck = true;
+        if (!eventTimerCheckScheduled) {
+            eventTimerCheckScheduled = true;
+            mailboxExecutor.submit(

Review Comment:
   Thanks, fixed in 46b4e93ef. Changed this mail to `execute()` so failures 
propagate. Added a regression test that injects an `IOException` when reading 
the timer registration and checks that it escapes the mail in both execution 
modes.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -1683,6 +1871,17 @@ private void tryResumeProcessActionTasks() throws 
Exception {
                     for (int i = 0; i < queued; i++) {
                         executionCoordinator.addTask(key);
                     }
+                    if (queued == 0) {
+                        // A checkpoint can capture a run waiting only for 
timers.
+                        // Guard its keyed-state access against other keys' 
worker Actions.
+                        mailboxExecutor.submit(

Review Comment:
   Fixed in 46b4e93ef. This recovery mail now uses `execute()` as well. Added a 
regression test that restores a run waiting only for a timer and verifies that 
an `InterruptedException` from `acquireByMain()` propagates.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/timer/TimerOperation.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.timer;
+
+import org.apache.flink.agents.api.Event;
+import org.apache.flink.agents.api.context.TimerMode;
+import org.apache.flink.agents.api.trace.TraceContext;
+import org.apache.flink.agents.runtime.utils.RuntimeIdentity;
+import org.apache.flink.api.common.serialization.SerializerConfigImpl;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.memory.DataInputDeserializer;
+import org.apache.flink.core.memory.DataOutputSerializer;
+import org.apache.flink.streaming.api.TimeDomain;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+/** An ordered Action effect. Relative times are resolved before journaling 
Action completion. */
+public class TimerOperation {
+    private String timerId;
+    private long time;
+    private boolean relative;
+    private byte[] eventBytes;
+    private TimerMode mode;
+    private TimeDomain timeDomain;
+    private byte[] ownerContextBytes;
+    private Long sourceTimestamp;
+    private String occurrenceId;
+
+    public TimerOperation() {}
+
+    public TimerOperation(
+            String timerId, long time, boolean relative, byte[] eventBytes, 
TimerMode mode) {
+        this(timerId, time, relative, eventBytes, mode, 
TimeDomain.PROCESSING_TIME);
+    }
+
+    public TimerOperation(
+            String timerId,
+            long time,
+            boolean relative,
+            byte[] eventBytes,
+            TimerMode mode,
+            TimeDomain timeDomain) {
+        this.timerId = timerId;
+        this.time = time;
+        this.relative = relative;
+        this.eventBytes = eventBytes;
+        this.mode = mode;
+        this.timeDomain = timeDomain;
+    }
+
+    public String getTimerId() {
+        return timerId;
+    }
+
+    public long getTime() {
+        return time;
+    }
+
+    public boolean isRelative() {
+        return relative;
+    }
+
+    public byte[] getEventBytes() {
+        return eventBytes;
+    }
+
+    public TimerMode getMode() {
+        return mode;
+    }
+
+    public TimeDomain getTimeDomain() {
+        return timeDomain;
+    }
+
+    public byte[] getOwnerContextBytes() {
+        return ownerContextBytes;
+    }
+
+    public Long getSourceTimestamp() {
+        return sourceTimestamp;
+    }
+
+    public String getOccurrenceId() {
+        return occurrenceId;
+    }
+
+    public TimerOperation resolve(
+            long now, TraceContext ownerContext, Long timestamp, String 
producerId, int ordinal)
+            throws Exception {
+        TimerOperation resolved =
+                new TimerOperation(
+                        timerId,
+                        relative ? Math.addExact(now, time) : time,
+                        false,
+                        eventBytes,
+                        mode,
+                        timeDomain);
+        if (eventBytes != null && mode == 
TimerMode.CURRENT_RUN_WAIT_FOR_TRIGGER) {
+            DataOutputSerializer out = new DataOutputSerializer(256);
+            contextSerializer().serialize(ownerContext, out);
+            resolved.ownerContextBytes = out.getCopyOfBuffer();
+            resolved.sourceTimestamp = timestamp;
+            resolved.occurrenceId =
+                    RuntimeIdentity.deriveEmittedEventOccurrenceId(producerId 
+ ":timer", ordinal);
+        }
+        return resolved;
+    }
+
+    /** Binds a replayed registration to the run executing the reused Action 
result. */
+    public TimerOperation withOwnerContext(TraceContext ownerContext) throws 
Exception {
+        TimerOperation copy =
+                new TimerOperation(timerId, time, relative, eventBytes, mode, 
timeDomain);
+        DataOutputSerializer out = new DataOutputSerializer(256);
+        contextSerializer().serialize(ownerContext, out);
+        copy.ownerContextBytes = out.getCopyOfBuffer();
+        copy.sourceTimestamp = sourceTimestamp;
+        copy.occurrenceId = occurrenceId;
+        return copy;
+    }
+
+    /** Serializes the Event after registration has prepared its attachment 
references. */
+    public static byte[] snapshot(Event event) {
+        try {
+            TypeSerializer<Event> serializer = serializer();
+            DataOutputSerializer out = new DataOutputSerializer(256);
+            serializer.serialize(event, out);
+            return out.getCopyOfBuffer();
+        } catch (Exception e) {
+            throw new IllegalArgumentException("Cannot snapshot timer Event", 
e);
+        }
+    }
+
+    public Event restoreEvent() throws Exception {
+        Event event = serializer().deserialize(new 
DataInputDeserializer(eventBytes));
+        event.setSourceTimestamp(
+                timeDomain == TimeDomain.EVENT_TIME ? Long.valueOf(time) : 
sourceTimestamp);
+        event.setOccurrenceId(occurrenceId);
+        return event;
+    }
+
+    public TraceContext restoreOwnerContext() throws Exception {
+        return contextSerializer().deserialize(new 
DataInputDeserializer(ownerContextBytes));
+    }
+
+    private static TypeSerializer<TraceContext> contextSerializer() {
+        return TypeInformation.of(TraceContext.class).createSerializer(new 
SerializerConfigImpl());
+    }
+
+    private static TypeSerializer<Event> serializer() {
+        return TypeInformation.of(Event.class).createSerializer(new 
SerializerConfigImpl());

Review Comment:
   There is no constraint requiring a separate configuration. Fixed in 
46b4e93ef: timer Event snapshot and restore now use the operator's 
job-configured Event serializer, passed into the Java and Python RunnerContext 
constructors. Added custom-serializer checkpoint round-trip tests covering 
processing-time timers in both modes and event-time timers; these pass on JDK 
11 and JDK 21.



-- 
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