GitHub user wenjin272 edited a discussion: [Discussion] Native Timer Support 
for Flink Agents

## 1. Motivation and Goals

Flink Agents uses Flink's native timers to support delayed execution, covering 
the following use cases:

- **Approval timeout cleanup:** Register a timeout task when requesting 
approval. Cancel it if the user responds in time; otherwise, release resources 
or update the request status.
- **Periodic job status checks:** Query the status of an external job and check 
again later if it has not finished.
- **Delayed business retries:** Retry a business operation at a specified time 
when it cannot proceed immediately.

This design supports both processing-time and event-time timers. A 
processing-time timer can continue execution within the current agent run or 
start a new run. An event-time timer starts a new run. Waiting within the 
current run retains its context and prevents subsequent inputs with the same 
key from being processed until the run finishes. Starting a new run allows the 
original run to finish and subsequent inputs to proceed. Neither mode occupies 
an Action execution thread while waiting. Delayed tasks use Flink state 
management and the recovery mechanisms of Flink Agents.

## 2. Comparison of Two Designs

When a Flink timer fires, it invokes a callback on `ActionExecutionOperator`. 
Flink Agents must then determine which user function to execute. The 
integration therefore needs to define how users register callbacks and how the 
runtime dispatches to the corresponding functions.

Both designs below use native timers. They differ in whether they introduce a 
separate mechanism for registering and dispatching user callbacks or reuse the 
existing Event → Action mechanism.

### 2.1 Option 1: Expose the Flink Timer API with User Callback Registration 
and Dispatch

Expose Flink's native Timer API to user Actions and provide an entry point for 
registering user callbacks. Users register and delete timers within Actions and 
implement delayed business logic in the registered callbacks.

```text
An Action registers a timer through the Flink Timer API
    → The Action completes normally
    → The timer fires
    → The runtime invokes the user's timer callback
    → The callback performs timeout cleanup, status checks, or other business 
logic
```

This design requires two additional mechanisms:

1. **Callback registration:** Define how users declare functions that timers 
can invoke, for example by registering a handler ID for a business function.
2. **Dispatch on firing:** Store the association between each timer, its 
handler ID, and business parameters. Look up and invoke the corresponding 
function from the operator's timer callback. Multiple tasks with the same key 
and deadline must remain distinguishable.

This design is close to Flink's native programming model, but requires new 
callback registration and dispatch mechanisms. To use Memory, durable 
execution, and scheduling for the same key in Flink Agents, callbacks must also 
be integrated with the corresponding Action execution context.

### 2.2 Option 2: Provide a Flink Agents Timer API That Delivers Events

Flink Agents provides APIs to register and cancel timers. Users implement 
delayed business logic as ordinary Actions and specify the business Event to 
deliver when registering a timer. The underlying implementation still uses 
Flink's native timers.

This design has two stages: defining Actions and registering timers.

**Stage 1: Register Actions when defining the Agent and building the Agent 
plan.**

Use the existing Action registration mechanism to establish Event-to-Action 
bindings in advance. For example:

```text
ApprovalRequested → Action that requests approval
ApprovalReceived  → Action that handles the approval response
ApprovalTimeout   → Action that performs timeout cleanup
```

At this stage, the timeout cleanup Action is registered and bound to the 
`ApprovalTimeout` Event type. Different approval requests create different 
instances of that Event type, all handled by the same Action definition.

**Stage 2: Register a specific delayed Event while the Agent is running.**

The Action that requests approval calls `register_timer()` to deliver 
`ApprovalTimeout(request_id, version)` after 30 minutes. It selects 
`TimerMode.TRIGGER_AFTER_CURRENT_RUN` to start a new run for that Event. The 
Action definition and Event type binding are already established; the specific 
Event, trigger time, and execution mode are chosen at runtime.

```text
An Action registers a delayed Event through the Flink Agents API
    → The Action completes; the runtime stores the Event and registers a native 
Flink timer
    → The timer fires; the runtime retrieves the stored Event
    → CURRENT_RUN_WAIT_FOR_TRIGGER: deliver the Event to the original run
      TRIGGER_AFTER_CURRENT_RUN: enqueue the Event to start a new run for the 
same key
    → Schedule the corresponding Action using the Event → Action binding in the 
Agent plan
```

Processing-time timers support both execution modes. Event-time timers always 
start a new run.

**Flink handles time and timer scheduling. Flink Agents provides the API, 
stores Events, and schedules the corresponding Actions when timers fire.**

Delayed business logic uses the same Memory, durable execution, scheduling, and 
tracing capabilities as ordinary Actions. Business parameters are stored in 
serializable Events or persistent state that subsequent Actions can read.

### 2.3 Comparison and Decision

| Aspect | Option 1: Expose the Flink Timer API | Option 2: Provide a Flink 
Agents Timer API That Delivers Events |
|---|---|---|
| Registration | An Action uses the native Timer API and associates the timer 
with a separately registered user callback. | An Action uses the Flink Agents 
Timer API and specifies the Event to deliver. |
| Function registration and selection | Add callback registration, store the 
timer-to-handler and parameter association, and dispatch when the timer fires. 
| Register Event-to-Action bindings when building the Agent plan. At runtime, 
register delayed Events and dispatch through those bindings. |
| Delayed logic | Implemented in user timer callbacks. | Implemented in 
ordinary Actions that handle business Events. |
| Execution on firing | The runtime routes to a user callback whose execution 
context must be defined. | The runtime delivers an Event and schedules Actions 
through the existing mechanism. |
| Flink Agents capabilities | Define restrictions or integrate callbacks with 
Memory, durable execution, and scheduling. | Reuse the ordinary Action 
execution lifecycle. |
| Programming model | Flink timer callbacks coexist with the Flink Agents 
Action model. | Delayed execution uses the Event → Action model. |

**This design adopts Option 2**, reusing the existing Event-to-Action binding, 
dispatch, and execution mechanisms.

## 3. Initial Scope

The initial version supports:

- Processing-time timers with relative delays or absolute deadlines.
- Event-time timers with absolute event-time deadlines, triggered by watermark 
advancement.
- Processing-time timers that either continue within the current run or start a 
new run. Event-time timers only start new runs.
- Named timer registration, replacement, and cancellation. Each timer belongs 
to the current Flink Agents operator and business key.

## 4. API Design and Usage Examples

### 4.1 API Definitions

Add the following APIs to `RunnerContext`. `timer_id` identifies a task for 
replacement and cancellation, and `event` specifies the business Event to 
deliver when the timer fires. `registerTimer` / `register_timer` and 
`registerTimerAt` / `register_timer_at` use processing time. 
`registerEventTimeTimer` / `register_event_time_timer` use event time.

Java:

```java
enum TimerMode {
    TRIGGER_AFTER_CURRENT_RUN,
    CURRENT_RUN_WAIT_FOR_TRIGGER
}

void registerTimer(
        String timerId, Duration delay, Event event, TimerMode mode);
void registerTimerAt(
        String timerId, Instant deadline, Event event, TimerMode mode);
void registerEventTimeTimer(String timerId, Instant timestamp, Event event);
void cancelTimer(String timerId);
```

Python:

```python
class TimerMode(Enum):
    CURRENT_RUN_WAIT_FOR_TRIGGER = "current_run_wait_for_trigger"
    TRIGGER_AFTER_CURRENT_RUN = "trigger_after_current_run"

def register_timer(
    self, timer_id: str, delay: timedelta, event: Event, mode: TimerMode
) -> None: ...

def register_timer_at(
    self, timer_id: str, deadline: datetime, event: Event, mode: TimerMode
) -> None: ...

def register_event_time_timer(
    self, timer_id: str, timestamp: datetime, event: Event
) -> None: ...

def cancel_timer(self, timer_id: str) -> None: ...
```

The `mode` parameter has type `TimerMode` and applies only to processing-time 
timers. It specifies how the delayed task relates to the current run:

| Mode | Execution when the timer fires | Subsequent inputs with the same key 
while waiting |
|---|---|---|
| `CURRENT_RUN_WAIT_FOR_TRIGGER` | The Event triggers Actions within the 
original run, sharing its context and Sensory Memory. The run finishes only 
after those subsequent Actions have completed. | Wait for the original run to 
finish. |
| `TRIGGER_AFTER_CURRENT_RUN` | The Event is queued to start a new run with new 
Sensory Memory. | Can proceed once the original run finishes. |

The event-time API has no `mode` parameter. Its Events always start new runs. 
If the current run waited for an event-time timer while watermark advancement 
waited for that run to finish, the two would wait for each other. This 
combination is therefore not supported.

Java and Python use epoch milliseconds internally. Absolute times in Python 
must be timezone-aware. Deadlines with sub-millisecond precision are rounded up 
to the next millisecond. Negative delays, empty timer IDs, timestamp overflow, 
and non-serializable Events are rejected. For processing-time timers, a zero 
delay or a deadline in the past makes the task eligible for scheduling after 
the registration is committed. Event-time timers become due according to the 
watermark. Business Actions are executed by the scheduler, never synchronously 
on the API call stack.

### 4.2 Usage Examples

#### 4.2.1 Approval Timeout

The Agent has registered Actions for requesting approval, handling approval 
responses, and handling `ApprovalTimeout`.

The Action that requests approval saves the request state and registers a 
timeout Event:

```python
ctx.register_timer(
    timer_id=f"approval:{request_id}",
    delay=timedelta(minutes=30),
    event=ApprovalTimeout(
        request_id=request_id,
        version=request_version,
    ),
    mode=TimerMode.TRIGGER_AFTER_CURRENT_RUN,
)
```

`TRIGGER_AFTER_CURRENT_RUN` allows the current run to finish normally so that 
the user's approval response can be processed as a subsequent input with the 
same key.

The Action that handles the approval response updates the request state and 
cancels the timer:

```python
ctx.cancel_timer(timer_id=f"approval:{request_id}")
```

The Action handling `ApprovalTimeout` performs cleanup only if the request 
still exists, is awaiting approval, and has a matching version. Cancellation 
does not retract an Event that has already been enqueued, so the Action must 
still check the business state.

#### 4.2.2 Periodic Status Checks

Consider checking an external job's status every minute. Register the following 
bindings when building the Agent plan:

```text
JobSubmitted   → start_observing Action
CheckJobStatus → check_job_status Action
```

Both Event types carry `job_id`. The following code shows the two Action 
bodies, omitting Event class definitions and Agent registration. 
`query_job_status(job_id)` is a user-provided function that queries the 
external job and returns a status string.

```python
from datetime import timedelta


def start_observing(event: JobSubmitted, ctx: RunnerContext):
    ctx.register_timer(
        timer_id=f"job-status:{event.job_id}",
        delay=timedelta(minutes=1),
        event=CheckJobStatus(job_id=event.job_id),
        mode=TimerMode.TRIGGER_AFTER_CURRENT_RUN,
    )


def check_job_status(event: CheckJobStatus, ctx: RunnerContext):
    status = ctx.durable_execute(query_job_status, event.job_id)

    if status in {"FINISHED", "FAILED", "CANCELED"}:
        return

    ctx.register_timer(
        timer_id=f"job-status:{event.job_id}",
        delay=timedelta(minutes=1),
        event=CheckJobStatus(job_id=event.job_id),
        mode=TimerMode.TRIGGER_AFTER_CURRENT_RUN,
    )
```

Each timer triggers one check. After checking the status, the Action decides 
whether to register another timer. No run is kept waiting between checks, and 
other inputs with the same key can be processed normally. `job-status:{job_id}` 
identifies the observation task for that job; each subsequent check reuses the 
same timer ID.

For example, if the first check starts at 10:01:00 and the Action completes at 
10:01:05, committing the next timer, the next check can start no earlier than 
10:02:05. This means waiting one minute after the previous check completes. It 
is not a fixed-rate schedule and does not replay every missed interval after 
downtime.

#### 4.2.3 Delayed Execution Within the Same Run

Use `CURRENT_RUN_WAIT_FOR_TRIGGER` when a business operation needs to continue 
after ten seconds and still needs the current run's context. Bind 
`ContinueWork` to the Action that continues the operation when building the 
Agent plan:

```python
ctx.register_timer(
    timer_id=f"continue:{request_id}",
    delay=timedelta(seconds=10),
    event=ContinueWork(request_id=request_id),
    mode=TimerMode.CURRENT_RUN_WAIT_FOR_TRIGGER,
)
# The Action returns normally; it does not sleep or wait for the timer.
```

The timer takes effect when the Action completes. The execution thread is 
released, while the current run retains its context and Sensory Memory. After 
ten seconds, `ContinueWork` triggers the corresponding Action within the 
original run. The run finishes only after all subsequent Actions complete and 
no other timers belonging to that run remain pending. The next input with the 
same key can then start processing.

#### 4.2.4 Event-Time Execution

Use an event-time timer when business logic needs to check state after event 
time reaches a deadline. In this example, `event.occurred_at` is a 
timezone-aware business timestamp carried by the input, and `CheckAtDeadline` 
is already bound to a state-checking Action:

```python
ctx.register_event_time_timer(
    timer_id=f"deadline:{event.request_id}",
    timestamp=event.occurred_at + timedelta(minutes=5),
    event=CheckAtDeadline(request_id=event.request_id),
)
```

If the business timestamp is 10:00, the timer deadline is 10:05. After the 
original run completes, the runtime fires the timer when processing a watermark 
at or beyond 10:05, and the Event starts a new run. The wall clock reaching 
10:05 does not itself fire the timer; the input stream must produce watermarks.

## 5. Behavioral Contract

| Aspect | Semantics |
|---|---|
| Timer identity | `(Flink Agents operator, business key, timer_id)`. All time 
domains and execution modes share this ID space. |
| Registration with the same ID | At most one pending registration exists 
within the same scope. A new registration replaces the old one, including its 
time domain and execution mode. Different IDs are delivered separately even if 
their deadlines are identical. |
| Event contents | Registration captures an Event snapshot. Later changes to 
the original object do not affect the registered task. |
| When operations take effect | API calls record registration or cancellation 
operations. They are applied when the Action completes successfully and commits 
its result. An intermediate yield or await does not apply them. |
| Starting point for relative delays | When the Action completes successfully 
and first commits the registration, the deadline is calculated as the current 
processing time plus the delay. Recovery reuses the saved deadline. |
| Cancellation | Idempotent. Cancels a registration whose Event has not yet 
been delivered. Events already queued or executing are unaffected. |
| Current run | `CURRENT_RUN_WAIT_FOR_TRIGGER` keeps the original run open and 
delivers the Event to that run when the timer fires. Cancellation and 
replacement update the timers that the run is waiting for. |
| New run | `TRIGGER_AFTER_CURRENT_RUN` and event-time timers do not require 
the original run to remain open. Their Events enter the normal run queue for 
the same key and do not preempt the current run. |
| Ordering | No global order is guaranteed across keys, nor is an order 
guaranteed among timers with identical deadlines. |
| Execution time | A timer becoming due makes its task eligible for scheduling; 
it is not a strict execution deadline. Load, other work for the same key, and 
downtime can delay execution. |
| Memory | Execution within the current run retains its Sensory Memory. A new 
run creates new Sensory Memory. Short-Term Memory is read as it exists at 
execution time and remains subject to its TTL. |

Multiple timer operations from one Action are applied in call order when the 
Action completes successfully. For example, registering and then canceling a 
timer leaves no pending registration. The runtime checks whether the run can 
finish only after applying the Action's complete result.

Events registered with `CURRENT_RUN_WAIT_FOR_TRIGGER` may carry attachments, 
which remain available in the current run's Sensory Memory. Events that start a 
new run (`TRIGGER_AFTER_CURRENT_RUN` and all event-time timers) must not carry 
attachments; registration rejects such Events. Use serializable Event 
attributes to pass data to a new run. Timer registration state does not use the 
TTL of Short-Term Memory.

## 6. Runtime Integration

### 6.1 Operator State and Timer Service

Add the following two components to `ActionExecutionOperator`.

**1. Keyed state for timer registrations.**

Create a `MapState<String, TimerRegistration>` using a `MapStateDescriptor`:

```text
registrations: timer_id → {
    time_domain,   // Processing time or event time
    deadline,      // Absolute deadline
    mode,          // CURRENT_RUN_WAIT_FOR_TRIGGER or TRIGGER_AFTER_CURRENT_RUN
    owner_run_id,  // Original run ID for CURRENT_RUN_WAIT_FOR_TRIGGER
    event          // Serialized business Event
}
```

This state is isolated by business key, so different keys can use the same 
timer ID. Event-time registrations always use `TRIGGER_AFTER_CURRENT_RUN` 
internally and are not associated with the original run.

**2. Flink's native `InternalTimerService` for tracking time and invoking timer 
callbacks.**

The operator implements `Triggerable` and obtains the timer service during 
initialization:

```java
timerService = getInternalTimerService(
        "timers",
        StringSerializer.INSTANCE,
        this);
```

The string `timer_id` serves as the namespace, and `this` refers to the 
operator. Flink invokes the operator's `onProcessingTime()` when a 
processing-time timer fires and `onEventTime()` when watermark advancement 
makes an event-time timer due. Both callbacks look up the business Event in 
keyed state.

The timer service and registration state are linked by business key and timer 
ID. For example, registering `approval:123` for `user-1` produces:

| Component | Stored information | Purpose |
|---|---|---|
| `InternalTimerService` | A processing-time timer: `(key=user-1, 
namespace=approval:123, deadline)`. | Notify the operator when the deadline is 
reached. |
| `registrations` | Under `user-1`: `approval:123 → {time_domain, deadline, 
mode=TRIGGER_AFTER_CURRENT_RUN, event=ApprovalTimeout(...)}`. | Store the 
Event, execution mode, and information needed for cancellation or replacement. |

Events that start new runs reuse the existing `pendingInputEvents` queue. This 
queue currently buffers `InputEvent` instances created from external inputs, 
using `ListState<Event>` as its underlying type. This design extends it to also 
accept business Events produced by timers. For `CURRENT_RUN_WAIT_FOR_TRIGGER`, 
the Event enters the original run's Action scheduling flow directly, rather 
than the input queue for new runs.

### 6.2 Registration, Replacement, and Cancellation

When an Action completes successfully, the runtime calculates deadlines for 
relative delays and preserves user-specified absolute deadlines. A 
`CURRENT_RUN_WAIT_FOR_TRIGGER` registration also records the current run's ID. 
The runtime then saves the registration under the corresponding business key 
and invokes the native API for its time domain:

```text
Processing time → registerProcessingTimeTimer(timer_id, deadline)
Event time      → registerEventTimeTimer(timer_id, deadline)
```

Replacing a registration follows this sequence: delete the old native timer → 
replace the registration record → register the new native timer. The runtime 
uses the old record's time domain and deadline to select the appropriate 
deletion API. Events that have already entered the execution flow are 
unaffected.

To cancel a timer, the runtime retrieves its time domain and deadline from 
`registrations`, deletes the corresponding native timer, and removes the 
registration record. If the record does not exist, no action is required. 
Replacement and cancellation also update the timers that affected runs are 
waiting for. Run completion is checked after the Action's complete result has 
been applied.

### 6.3 Timers Within the Same Run

**The underlying timer remains a native Flink timer. This mode changes when the 
run can finish and where the Event is delivered.**

Previously, a run could finish once all its Actions had completed. With 
`CURRENT_RUN_WAIT_FOR_TRIGGER`, the run can finish only when it has no queued 
or executing Actions and no pending timers belonging to it. The runtime uses 
`owner_run_id` in the registration records to track the timers each run is 
waiting for, and rebuilds these relationships from the records during recovery.

The Action that registers a timer can complete normally and release its 
execution thread. While waiting, the original run retains its context and 
Sensory Memory. Subsequent external inputs with the same key remain queued.

```text
An Action in run A registers a CURRENT_RUN_WAIT_FOR_TRIGGER timer
    → The Action completes and commits the registration
    → Run A waits for the timer, retaining its context without occupying an 
Action thread
    → The native Flink timer fires; the operator finds run A through 
owner_run_id
    → The Event triggers subsequent Actions within run A through the existing 
bindings
    → All subsequent Actions complete, with no other timers pending for run A
    → Run A finishes; the next input with the same key starts processing
```

The Event must enter the original run's Event → Action scheduling flow. If it 
were placed in `pendingInputEvents`, it would wait for the original run to 
finish while that run waited for the Event, preventing progress.

When a timer fires, the runtime first adds its Event to the original run's 
pending work, then removes the timer registration, and then checks whether the 
run can finish. Cancellation releases the corresponding wait. Replacement 
adjusts the wait according to the new registration's execution mode. A 
`TRIGGER_AFTER_CURRENT_RUN` timer that has not yet fired does not keep the 
original run open.

### 6.4 Scheduling and Executing Events from Timers

When a timer fires, Flink provides its key, namespace, and deadline, and sets 
the corresponding current key. The runtime finds the registration using the 
namespace, checks its time domain and deadline, and dispatches according to the 
execution mode:

```text
CURRENT_RUN_WAIT_FOR_TRIGGER
    → Add the Event to the pending work of the run identified by owner_run_id
    → Remove the registration record
    → Continue scheduling Actions in the original run

TRIGGER_AFTER_CURRENT_RUN (including all event-time timers)
    → Store the Event in pendingInputEvents for the same key
    → Remove the registration record
    → Start a new run through the ordinary input scheduling flow
```

The callback only updates state and schedules work. The Action executor handles 
the business logic. The Event retains its registered business type, such as 
`ApprovalTimeout`, and the Agent plan's bindings identify the corresponding 
Action.

**New runs execute in queue order for each key.** For example, if input A is 
being processed, input B is already queued, and a timer produces Event T for a 
new run, the execution order is A → B → T. An Event using 
`CURRENT_RUN_WAIT_FOR_TRIGGER` is part of A's subsequent work and executes 
before A finishes.

**Actions triggered by timers follow the existing concurrency limits.** Work 
that cannot execute immediately remains in its pending execution state. Waiting 
within the current run retains run state without occupying an Action execution 
thread.

**All timer state operations execute on the operator thread.** Action API calls 
record operations, which the operator applies when committing the Action 
result. Timer callbacks access registration records and pending work on the 
same thread. The existing execution lock protects current-key switching and 
state access.

**Java and Python share the same timer service.** The Python API passes 
registration and cancellation operations to the Java operator. Business Events 
are stored using the existing cross-language serialization mechanism and 
dispatched through the existing Java or Python Action execution path.

### 6.5 Event Time and Watermarks

Event-time timers are driven by upstream watermarks. When Flink Agents receives 
a watermark, it fires the timers that are due, waits for the business 
processing triggered by those timers to finish, and then forwards the watermark 
downstream.

#### 6.5.1 Processing a Watermark

Suppose the operator receives `Watermark = 100` and an event-time timer with 
deadline `80` has already been registered. Processing follows this sequence:

```text
Wait for inputs that arrived before Watermark 100 to finish processing
    → Advance the internal Timer Service to 100, firing timer 80
    → The timer callback delivers an Event that starts run A
    → Wait for run A and all its subsequent Actions to complete and emit their 
results
    → Forward Watermark 100 downstream
```

The first step uses arrival order at the operator, not the inputs' event 
timestamps. If those inputs belong to runs waiting for processing-time timers 
in `CURRENT_RUN_WAIT_FOR_TRIGGER` mode, those runs must also finish first.

When the internal Timer Service has processed watermark 100, run A may still be 
executing, so watermark 100 cannot yet be forwarded. The runtime retains the 
watermark awaiting emission and tracks the runs it triggered until their work 
is complete. An event-time timer with deadline 200 is not yet due and does not 
need to be awaited.

#### 6.5.2 Registering Another Timer That Is Already Due

Continuing the example, suppose an Action in run A registers an event-time 
timer with deadline `90` when it completes. The internal Timer Service has 
already processed watermark 100, so the new timer is already due.

**Flink Agents must handle this explicitly.** Flink's native 
`registerEventTimeTimer()` only inserts the timer into a queue. Timers fire 
within the watermark-processing loop. By the time an asynchronous Action 
completes, the original loop has already returned, so registration alone does 
not immediately invoke a callback. [Flink 
implementation](https://github.com/apache/flink/blob/release-2.2/flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceImpl.java)

After committing all timer operations from the Action, if a newly registered 
timer has a deadline no greater than the watermark currently being processed, 
the runtime runs the internal Timer Service's due-timer check again with that 
same watermark on the operator thread:

```text
The Action in run A completes and commits timer 90
    → Check for due timers again using Watermark 100
    → Fire timer 90 and deliver an Event that starts run B
    → Wait for run B to finish
```

Watermark 100 remains pending throughout this process. If subsequent Actions 
register more timers with deadlines no greater than 100, the process repeats. 
Watermark 100 can be forwarded only after the due-timer check has completed and 
all triggered runs have finished. Both the check and the runs it triggers must 
be included in the completion condition; finishing run A alone must not cause 
the watermark to be forwarded early.

#### 6.5.3 Output Timestamps

For a run started by an ordinary input, `sourceTimestamp` stores the input 
record's event time. This timestamp propagates through the Actions and Events 
triggered by that input and ultimately becomes the output record's event time. 
If the input record has no timestamp, `sourceTimestamp` is null.

A `CURRENT_RUN_WAIT_FOR_TRIGGER` timer continues within the original run and 
therefore preserves its `sourceTimestamp`. A new run started by a timer has no 
corresponding new input record, so its timestamp is determined by the timer 
type:

| Execution path | `sourceTimestamp` and the output record's event time |
|---|---|
| Run started by an ordinary input | Use the input record's event time. |
| `CURRENT_RUN_WAIT_FOR_TRIGGER` timer continuing within the original run | 
Preserve the original run's timestamp. For a run started by an ordinary input, 
this is the original input record's event time. |
| New run started by a processing-time timer | Leave `sourceTimestamp` null and 
do not assign event time to the output record. |
| New run started by an event-time timer | Use the timer's deadline as event 
time, following [Flink's native 
behavior](https://github.com/apache/flink/blob/master/flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/KeyedProcessOperator.java#L60-L63).
 |

The timestamp determines the event time associated with the result. The 
scheduling sequence in the preceding sections ensures that results are emitted 
before the watermark. If watermark 100 has already been forwarded and a timer 
with deadline 80 is registered afterward, its results are treated as late data; 
the downstream watermark is not moved backward.

## 7. Recovery and Durable Execution

Flink restores native timers and registration records from checkpoints. Events 
already handed to runs or pending queues use the existing recovery flow. This 
section describes how Flink Agents records and replays timer operations.

### 7.1 Recording and Applying Timer Operations

Registration and cancellation API calls only record operations. When an Action 
completes successfully, the runtime commits these operations in call order. 
Intermediate yields or awaits do not register or cancel native timers.

For a relative delay, the runtime calculates `deadline = currentProcessingTime 
+ delay` when first committing the registration. Absolute-time registration 
uses the user-specified deadline directly. When ActionState persistence is 
enabled, timer operations and their resolved time domains, deadlines, execution 
modes, Events, and Event IDs are saved in the completed Action's result before 
being applied to managed keyed state and `InternalTimerService`. Operations 
using `CURRENT_RUN_WAIT_FOR_TRIGGER` also save the original run's identity.

For example, an Action calls `register_timer(delay=timedelta(minutes=30), ...)` 
at 10:00, then completes successfully and commits the registration at 10:10. 
The deadline is 10:40. Recovery replay continues to use 10:40.

### 7.2 Restoring Timer Operations When Reusing Completed Actions

An Action result may already have been written to the ActionState journal while 
its timer registrations or cancellations are not yet included in the latest 
completed checkpoint. During recovery, Flink Agents reuses the completed Action 
result without executing the Action's code again.

**Like `MemoryUpdate`, timer operations are saved as part of the Action result 
and reapplied when that result is reused.** Recovery replays Memory updates, 
emitted Events, and timer operations. Replaying timer operations updates both 
the registration records in managed keyed state and the native timers in 
`InternalTimerService`.

For example, if an Action registered an approval timeout timer before 
completing, recovery restores the saved Event and deadline and registers the 
native timer even though it skips the Action's code. A cancellation operation 
removes the corresponding registration record and native timer. Replay uses the 
saved deadline, execution mode, Event, and Event ID; it does not recalculate 
the delay or create a new Event. Operations follow the recovery order of Action 
results and the original call order within each Action.

For `CURRENT_RUN_WAIT_FOR_TRIGGER`, recovery restores the original run's 
context and pending work, then rebuilds the timers it is waiting for from the 
registration records. Recovery scheduling first applies the results of Actions 
already present at the recovery point, including their timer operations, and 
then dispatches Events from due timers. These Events continue within the 
original run; processing them must not wait for the entire original run to 
finish. If a registration has already transitioned from a timer to a pending 
Event, recovery follows the Event's existing execution state.

### 7.3 Retrying Incomplete Actions

If an Action fails before completing successfully, its recorded timer 
operations have not taken effect. Recovery re-executes the Action and applies 
its timer operations only after successful completion. Relative delays are 
calculated from that commit time.

When ActionState persistence is disabled, Actions executed after the checkpoint 
may also run again, and relative deadlines are calculated from the new commit 
time. If a business operation requires a fixed deadline, obtain an absolute 
time from the input or persistent state and use `register_timer_at()`. The 
event-time API already uses absolute time.


GitHub link: https://github.com/apache/flink-agents/discussions/1420

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to