Skip to content

feat(amber): supporting consistent operator stats retrieval - #3557

Merged
shengquan-ni merged 9 commits into
masterfrom
shengquan-consistent-stats
Jul 12, 2025
Merged

shengquan-ni merged 9 commits into
masterfrom
shengquan-consistent-stats

Conversation

@shengquan-ni

@shengquan-ni shengquan-ni commented Jul 11, 2025 •

Copy link
Copy Markdown
Contributor

This PR introduces consistent operator retrieval in QueryStats by adopting a reverse-topological strategy.

Problem

Previously, we retrieved operator statistics by sending direct control messages to each worker independently. This approach could result in inconsistent snapshots: for example, a downstream operator might appear as completed while its upstream is still running, which is logically impossible in a pipelined execution.

Consistent Retrieval via Reverse-Topological Order

We now compute a layered topological order of operators, where operators in the same layer have the same rank, and retrieve stats layer by layer in reverse topological order. Only after all relevant stats are retrieved do we update the execution state and send the update to the frontend. This guarantees that downstream stats are not visible before their upstream stats are available, maintaining a consistent global view.

This approach increases the time complexity to O(# of layers), compared to the previous O(1) method. Therefore, we should avoid issuing stats queries too frequently, as doing so may flood the control message queue and delay other control operations.

SubDAG Query and Race Condition Mitigation

In some scenarios—such as querying the stats of a completed worker—we now query the entire subDAG rooted at that operator to ensure upstream context is also retrieved. This avoids inconsistencies in localized queries.

However, this introduces the possibility of a race condition:

  1. A global query stats request is fired.
  2. Operator A's stats are retrieved at timestamp T₀.
  3. A subDAG stats query (including operator A) is fired.
  4. Operator A's stats are retrieved again at timestamp T₁.
  5. The subDAG query finishes and updates the execution state with timestamp T₁.
  6. The earlier global query finishes and overwrites A's stats with older data from T₀.

To prevent this, we now attach a nanosecond-level timestamp to each execution state update, and only allow updates with newer timestamps to overwrite the existing state.

@shengquan-ni shengquan-ni self-assigned this Jul 11, 2025
@aglinxinyuan

Copy link
Copy Markdown
Contributor

Need more testing. Will do it offline.

@shengquan-ni shengquan-ni changed the title feat(engine): supporting consistent operator stats retrieval feat(amer): supporting consistent operator stats retrieval Jul 12, 2025
@shengquan-ni shengquan-ni changed the title feat(amer): supporting consistent operator stats retrieval feat(amber): supporting consistent operator stats retrieval Jul 12, 2025
@shengquan-ni
shengquan-ni merged commit 875708d into master Jul 12, 2025
@shengquan-ni
shengquan-ni deleted the shengquan-consistent-stats branch July 12, 2025 21:07
SarahAsad23 pushed a commit to madisonmlin/texera that referenced this pull request May 20, 2026
)

This PR introduces consistent operator retrieval in `QueryStats` by
adopting a reverse-topological strategy.

## Problem
Previously, we retrieved operator statistics by sending direct control
messages to each worker independently. This approach could result in
inconsistent snapshots: for example, a downstream operator might appear
as completed while its upstream is still running, which is logically
impossible in a pipelined execution.

## Consistent Retrieval via Reverse-Topological Order
We now compute a layered topological order of operators, where operators
in the same layer have the same rank, and retrieve stats layer by layer
in reverse topological order. Only after all relevant stats are
retrieved do we update the execution state and send the update to the
frontend. This guarantees that downstream stats are not visible before
their upstream stats are available, maintaining a consistent global
view.

This approach increases the time complexity to O(# of layers), compared
to the previous O(1) method. Therefore, we should avoid issuing stats
queries too frequently, as doing so may flood the control message queue
and delay other control operations.

## SubDAG Query and Race Condition Mitigation
In some scenarios—such as querying the stats of a completed worker—we
now query the entire subDAG rooted at that operator to ensure upstream
context is also retrieved. This avoids inconsistencies in localized
queries.

However, this introduces the possibility of a race condition:

1. A global query stats request is fired.
2. Operator A's stats are retrieved at timestamp T₀.
3. A subDAG stats query (including operator A) is fired.
4. Operator A's stats are retrieved again at timestamp T₁.
5. The subDAG query finishes and updates the execution state with
timestamp T₁.
6. The earlier global query finishes and overwrites A's stats with older
data from T₀.

To prevent this, we now attach a **nanosecond-level** timestamp to each
execution state update, and only allow updates with newer timestamps to
overwrite the existing state.
yangzhang75 pushed a commit to yangzhang75/texera that referenced this pull request Jun 22, 2026
)

This PR introduces consistent operator retrieval in `QueryStats` by
adopting a reverse-topological strategy.

## Problem
Previously, we retrieved operator statistics by sending direct control
messages to each worker independently. This approach could result in
inconsistent snapshots: for example, a downstream operator might appear
as completed while its upstream is still running, which is logically
impossible in a pipelined execution.

## Consistent Retrieval via Reverse-Topological Order
We now compute a layered topological order of operators, where operators
in the same layer have the same rank, and retrieve stats layer by layer
in reverse topological order. Only after all relevant stats are
retrieved do we update the execution state and send the update to the
frontend. This guarantees that downstream stats are not visible before
their upstream stats are available, maintaining a consistent global
view.

This approach increases the time complexity to O(# of layers), compared
to the previous O(1) method. Therefore, we should avoid issuing stats
queries too frequently, as doing so may flood the control message queue
and delay other control operations.

## SubDAG Query and Race Condition Mitigation
In some scenarios—such as querying the stats of a completed worker—we
now query the entire subDAG rooted at that operator to ensure upstream
context is also retrieved. This avoids inconsistencies in localized
queries.

However, this introduces the possibility of a race condition:

1. A global query stats request is fired.
2. Operator A's stats are retrieved at timestamp T₀.
3. A subDAG stats query (including operator A) is fired.
4. Operator A's stats are retrieved again at timestamp T₁.
5. The subDAG query finishes and updates the execution state with
timestamp T₁.
6. The earlier global query finishes and overwrites A's stats with older
data from T₀.

To prevent this, we now attach a **nanosecond-level** timestamp to each
execution state update, and only allow updates with newer timestamps to
overwrite the existing state.
renovate-bot pushed a commit to renovate-bot/apache-_-texera that referenced this pull request Jul 26, 2026
### What changes were proposed in this PR?

A fast **source** operator stayed **orange (RUNNING)** in the editor
after the run finished (issue apache#6010).

**Why it's a bug.** The controller reconstructs each worker's state from
several unordered channels (source `RUNNING` via the `startWorker`
response, non-source `RUNNING` via `workerStateUpdated`,
`COMPLETED`/`PAUSED` via `queryStatistics`/`pauseWorker` responses) and
reconciled them with **last-`System.nanoTime()`-wins** in
`WorkerExecution`. Worker state, however, is single-writer and strictly
ordered causally. For a tiny source the run finishes almost instantly,
so the `startWorker` response — carrying the stale `RUNNING` it sampled
at launch — can reach the controller *after* `COMPLETED` was recorded;
its later receipt timestamp wins and clobbers `COMPLETED`. Results
render fine (separate path), so only the border is stuck.

This PR orders worker state **causally** instead of by wall clock:

| Before | After |
| --- | --- |
| `WorkerExecution.update(nanoTime, state)` — last-write-wins by receipt
time | `updateState(version, state)` — newest **logical version** wins |
| stale late RUNNING clobbers COMPLETED | RUNNING (v2) < COMPLETED (v3)
⇒ COMPLETED kept |
| — | terminal states (COMPLETED/TERMINATED) are absorbing |

- `WorkerStateManager` bumps a monotonic `stateVersion` on every
`transitTo` (its state-machine logical clock).
- The version rides on every state report: `WorkerStateResponse`,
`WorkerStateUpdatedRequest`, `WorkerMetrics` (3 new proto fields).
- The controller applies a state only when its version is newer. Stats
stay timestamp-ordered (monotonic snapshots can share a state version).
- A single per-worker counter ⇒ no cross-process clock-sync assumptions
(why not worker timestamps).
- **pyamber parity:** the Python worker also reports state
(start/pause/resume + query-statistics). Its `StateManager` now bumps
the same monotonic version and the four handlers include it; otherwise
version-0 reports would be dropped after the first and reconfiguration
(RUNNING→PAUSED→RUNNING) would hang.

### Any related issues, documentation, discussions?

Closes apache#6010. The timestamp-based `update` was introduced in apache#3557.

### How was this PR tested?

JDK 17. Scala unit + Scala/Python integration + Python unit:

```
sbt "WorkflowExecutionService/testOnly \
  *WorkerExecutionSpec *WorkerStateManagerSpec *OperatorExecutionSpec \
  *RegionExecutionCoordinatorSpec *WorkflowExecutionCoordinatorSpec \
  *RegionExecutionSpec *WorkflowExecutionSpec"

# Scala-Python reconfiguration end-to-end (spawns Python UDF workers):
sbt "WorkflowExecutionService/testOnly *ReconfigurationIntegrationSpec"   # 3 passed

# Python unit:
cd amber && pytest src/test/python/core/architecture/managers/test_state_manager.py   # 9 passed
```

- `WorkerExecutionSpec`: version ordering (positive +
stale/equal-version negatives), terminal-state absorption,
`forceTerminate`, independent stats-vs-state ordering, and a named
regression for apache#6010 (`COMPLETED` survives a late `RUNNING`). Verified
the regression goes **red** when `updateState` is reverted to
last-write-wins.
- `WorkerStateManagerSpec` / `test_state_manager.py`: version starts at
0, bumps per successful transition, no bump on no-op self-transition or
rejected transition.
- `ReconfigurationIntegrationSpec` reproduced the failure (timeout)
before the pyamber fix and passes after.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8 (1M context), via Claude Code

---------

Co-authored-by: Claude Fable 5 <[email protected]>
Yicong-Huang added a commit to Yicong-Huang/texera that referenced this pull request Jul 30, 2026
### What changes were proposed in this PR?

A fast **source** operator stayed **orange (RUNNING)** in the editor
after the run finished (issue apache#6010).

**Why it's a bug.** The controller reconstructs each worker's state from
several unordered channels (source `RUNNING` via the `startWorker`
response, non-source `RUNNING` via `workerStateUpdated`,
`COMPLETED`/`PAUSED` via `queryStatistics`/`pauseWorker` responses) and
reconciled them with **last-`System.nanoTime()`-wins** in
`WorkerExecution`. Worker state, however, is single-writer and strictly
ordered causally. For a tiny source the run finishes almost instantly,
so the `startWorker` response — carrying the stale `RUNNING` it sampled
at launch — can reach the controller *after* `COMPLETED` was recorded;
its later receipt timestamp wins and clobbers `COMPLETED`. Results
render fine (separate path), so only the border is stuck.

This PR orders worker state **causally** instead of by wall clock:

| Before | After |
| --- | --- |
| `WorkerExecution.update(nanoTime, state)` — last-write-wins by receipt
time | `updateState(version, state)` — newest **logical version** wins |
| stale late RUNNING clobbers COMPLETED | RUNNING (v2) < COMPLETED (v3)
⇒ COMPLETED kept |
| — | terminal states (COMPLETED/TERMINATED) are absorbing |

- `WorkerStateManager` bumps a monotonic `stateVersion` on every
`transitTo` (its state-machine logical clock).
- The version rides on every state report: `WorkerStateResponse`,
`WorkerStateUpdatedRequest`, `WorkerMetrics` (3 new proto fields).
- The controller applies a state only when its version is newer. Stats
stay timestamp-ordered (monotonic snapshots can share a state version).
- A single per-worker counter ⇒ no cross-process clock-sync assumptions
(why not worker timestamps).
- **pyamber parity:** the Python worker also reports state
(start/pause/resume + query-statistics). Its `StateManager` now bumps
the same monotonic version and the four handlers include it; otherwise
version-0 reports would be dropped after the first and reconfiguration
(RUNNING→PAUSED→RUNNING) would hang.

### Any related issues, documentation, discussions?

Closes apache#6010. The timestamp-based `update` was introduced in apache#3557.

### How was this PR tested?

JDK 17. Scala unit + Scala/Python integration + Python unit:

```
sbt "WorkflowExecutionService/testOnly \
  *WorkerExecutionSpec *WorkerStateManagerSpec *OperatorExecutionSpec \
  *RegionExecutionCoordinatorSpec *WorkflowExecutionCoordinatorSpec \
  *RegionExecutionSpec *WorkflowExecutionSpec"

# Scala-Python reconfiguration end-to-end (spawns Python UDF workers):
sbt "WorkflowExecutionService/testOnly *ReconfigurationIntegrationSpec"   # 3 passed

# Python unit:
cd amber && pytest src/test/python/core/architecture/managers/test_state_manager.py   # 9 passed
```

- `WorkerExecutionSpec`: version ordering (positive +
stale/equal-version negatives), terminal-state absorption,
`forceTerminate`, independent stats-vs-state ordering, and a named
regression for apache#6010 (`COMPLETED` survives a late `RUNNING`). Verified
the regression goes **red** when `updateState` is reverted to
last-write-wins.
- `WorkerStateManagerSpec` / `test_state_manager.py`: version starts at
0, bumps per successful transition, no bump on no-op self-transition or
rejected transition.
- `ReconfigurationIntegrationSpec` reproduced the failure (timeout)
before the pyamber fix and passes after.

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8 (1M context), via Claude Code

---------

(backported from commit f521750)

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants