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


##########
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:
   Could this recovery mail also use execute()? Although 
tryProcessActionTaskForKey() forwards failures through its own error handler, 
acquiring parallelExecutionLock and evaluating resolveContextKey(key) happen 
outside that protection. Failures there are captured in the ignored Future.
   
   On the non-continuation parallel execution path, I reproduced this by 
restoring a run with no queued Actions and a pending current-run timer, then 
injecting InterruptedException from acquireByMain(). The current code swallows 
the exception; execute() propagates it.



##########
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:
   Could we use mailboxExecutor.execute() here, or explicitly propagate 
failures from the returned Future? submit() captures exceptions in that Future, 
but it is ignored.
   
   I reproduced this in an operator-harness test by advancing the watermark, 
registering an already-due event-time timer while a current-run timer keeps the 
run open, and injecting an IOException when onEventTime() reads the timer 
registration. The native timer has already been dequeued, but no Event is 
admitted and the exception never escapes the mail. Changing submit() to 
execute() makes the failure propagate.



##########
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:
   Could timer Event snapshot/restore use the job's serializer configuration? 
ActionExecutionOperator.open() creates its Event serializer from 
getExecutionConfig().getSerializerConfig(), while this helper uses a fresh 
default configuration, ignoring user-configured serializers.
   
   Is there a specific constraint requiring this difference? Otherwise, could 
we use consistently configured serializers for both snapshot and restore, with 
a custom-serializer round-trip test?



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