Skip to content

[CORE] Preserve Java exceptions from memory callbacks - #13563

Open
felipepessoto wants to merge 6 commits into
apache:mainfrom
felipepessoto:felipepessoto-jni-memory-callback-preservation
Open

felipepessoto wants to merge 6 commits into
apache:mainfrom
felipepessoto:felipepessoto-jni-memory-callback-preservation

Conversation

@felipepessoto

@felipepessoto felipepessoto commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 3ef35d12f123b5a5481cc82c448f8abe67a69423

Dimension Assessment Rationale
Change confidence 9/10 ↑ A real JVM received the identical pending throwable after both mandatory and best-effort growth, with and without reclaimed capacity. Neither retry invoked Java reserve.
↑ Normal charge traces matched the exact base; Spark 3.5 and 4.1 passed with shared and isolated C++ runtimes.
↑ Fork x86 (attempt 2), ARM, and Kafka CI passed on this revision.
↓ No maintainer has reviewed this revision yet.
Diagnosis confidence 9.5/10 ↑ The base converted a throwing operator reservation and a real Spark off-heap OOM to GlutenException. On b654a76a1, maybeReserve returned normally to Java after consuming a pre-existing error; the mandatory control retained it.
Evidence fit 9/10 ↑ The controls exercise the native operator, a real JVM listener, the pinned maybeReserve implementation and object identity at the JNI return.
↓ Compound capture, rollback and allocation failures were injected rather than observed in production.

What changes are proposed in this pull request?

Preserve the Java throwable from a Velox memory callback. On main, a failed ReservationListener.reserve can reach Spark as a message-only GlutenException because the Velox driver sees an ordinary C++ exception. Following the JNI input iterator pattern (#13561), Velox supplies the JNI listener for task and global memory managers and wraps callback errors before the driver sees them. Direct JNI unreserve continues to return the original throwable.

In a guarded JNI entry, a pre-existing throwable stays pending for Java. New growth is rejected without invoking Java, even when Velox's best-effort maybeReserve catches the native error. Repayment still runs: save the throwable locally, clear it for the callback, then restore it. This applies to ordinary cleanup and arbitration rollback. If JNI cannot reinstall the throwable, use the JNI fallback rather than treating a successful repayment as rejected.

When rollback also throws, a captured or still-pending reservation error stays primary. On a worker without a JNI return boundary, a failed capture leaves no pending throwable, so a subsequent rollback failure can be reported instead. If capture succeeded but allocating the usual Velox wrapper fails, the preallocated system-error wrapper retains the original as a nested cause.

No shared accounting change: BlockAllocationListener, ListenableMemoryAllocator, and the Java ReservationListener contract are unchanged from main. This PR does not reconcile balances, decide whether a throwing callback repaid memory, or introduce a new rule for future allocations.

How was this patch tested?

  • Under the same Spark 4.1 suite, the exact base (73dae76e1) returned GlutenException for the throwing native operator reservation and Spark off-heap OOM. On this revision, all 11 suite cases passed on Spark 3.5 and 4.1, both with shared libraries and with the JNI listener in a separately initialized private C++ runtime.
  • The new regression creates a pending error by failing capture on a first optional reservation. A required or optional retry now retains that same error without calling Java again. Real-JVM controls confirmed identity with and without capacity to return; clean-entry controls still succeeded. The previous head lost the earlier throwable on the optional retry.
  • Guarded cleanup and low-memory tests cover secondary failures, restoration and fallback-wrapper selection. Ordinary next-charge traces still matched main, including its residual native estimate after a throwing callback. 30 native exception tests, 12 adjacent memory tests, 14 JNI bridge tests, and all nine listener cases in the isolated-runtime harness passed. Format, license and scalastyle checks passed.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: GitHub Copilot 1.0.102-0.unstable.r40949665530.gab1eef5 (GPT-6 Astra)

Generated-by: GitHub Copilot 1.0.106-8.unstable.r43162459260.gbfc9f50 (GPT-6 Astra; post-merge maintenance)

Generated-by: GitHub Copilot 1.0.115-7 (GPT-6.2 Astra; standalone port)

Generated-by: GitHub Copilot 1.0.115-7 (Claude Opus 5.5; rebuild on the backend factory pattern)

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from 3ff29f8 to 2984ce1 Compare November 6, 2026 17:33
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from 2984ce1 to 6b1bf2f Compare November 6, 2026 22:29
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@LuciferYang

Copy link
Copy Markdown
Contributor

@felipepessoto The red x86 and Kafka ... in fork checks here aren't from your change. Every fork job that started after 23:19 UTC yesterday failed at "Set up job" with "The actions actions/checkout@v7, actions/download-artifact@v8, and actions/upload-artifact@v7 are not allowed in felipepessoto/gluten because all actions must be from a repository owned by felipepessoto." Your runs for #13561 this morning got past that step, so the setting looks fixed now. Could you rerun the failed jobs of the x86 and Kafka runs in your fork?

@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from cc65fbe to 8ea3b66 Compare November 14, 2026 19:26
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from 8ea3b66 to ddd98ec Compare November 14, 2026 20:58
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@felipepessoto

Copy link
Copy Markdown
Contributor Author

/delta-test

@github-actions

Copy link
Copy Markdown

🔄 Delta Spark UT started by @felipepessoto (~2.5 h). View run

@felipepessoto

felipepessoto commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@felipepessoto The red x86 and Kafka ... in fork checks here aren't from your change. Every fork job that started after 23:19 UTC yesterday failed at "Set up job" with "The actions actions/checkout@v7, actions/download-artifact@v8, and actions/upload-artifact@v7 are not allowed in felipepessoto/gluten because all actions must be from a repository owned by felipepessoto." Your runs for #13561 this morning got past that step, so the setting looks fixed now. Could you rerun the failed jobs of the x86 and Kafka runs in your fork?

@LuciferYang I can't see any ... in fork does it only show to contributors? The only way I found to check the CI failure is opening the CI page in my fork's Actions and look by Title + Commit Hash.

For example, for this PR is seems ok to me:

image

But when I manually check in my fork, it is actually failing:

image

@LuciferYang

Copy link
Copy Markdown
Contributor

@felipepessoto You're right, and it isn't only for contributors. The three checks were attached to an older PR Bot run on this commit, and the merge box didn't list them. Your rerun also never showed up: reading its jobs kept timing out on GitHub's side. #13644 reports fork runs as commit statuses instead, which show like "Gluten ClickHouse CI" does, and keeps reporting a run whose jobs can't be read. Until it's merged, your fork's Actions tab is the place to check.

LuciferYang added a commit that referenced this pull request Nov 17, 2026
…o allow all actions (#13644)

## What changes are proposed in this pull request?

Fixes for #13535's fork status sync, from its first days:

- Fork runs are reported as commit statuses, not check runs. A check run joins another workflow run's check suite. On #13563 the three checks sat under an older PR Bot run, and the merge box didn't list them. Check runs from earlier passes are marked neutral after their statuses are written.
- If a fork run's jobs can't be read, the status still follows the run. On #13563 each read got a 502, so a rerun was never reported. Jobs are read 30 at a time, and 5xx responses are retried.
- A queued run with a started job shows as running.
- A fork run with no relevant change no longer checks out a merged commit it never published, which failed docs-only PRs such as #13413.
- `CONTRIBUTING.md` asks forks to allow all actions, after #13561's fork allowed only its own.

## How was this patch tested?

A unit check of the script with a stubbed GitHub client covers each case above. In luciferyang-ci-demo/gluten, which plays apache/gluten, the statuses show on a [passing PR](https://github.com/luciferyang-ci-demo/gluten/pull/1) and a [failing PR](https://github.com/luciferyang-ci-demo/gluten/pull/3). A fork run with no relevant change now passes `detect-changes`.

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

Yes. Generated-by: Claude Opus 5.5


Related issue: #13541
@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from ddd98ec to 68cb0f8 Compare November 17, 2026 20:32
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

ReemaAlzaid pushed a commit to ReemaAlzaid/gluten that referenced this pull request Nov 23, 2026
…o allow all actions (apache#13644)

## What changes are proposed in this pull request?

Fixes for apache#13535's fork status sync, from its first days:

- Fork runs are reported as commit statuses, not check runs. A check run joins another workflow run's check suite. On apache#13563 the three checks sat under an older PR Bot run, and the merge box didn't list them. Check runs from earlier passes are marked neutral after their statuses are written.
- If a fork run's jobs can't be read, the status still follows the run. On apache#13563 each read got a 502, so a rerun was never reported. Jobs are read 30 at a time, and 5xx responses are retried.
- A queued run with a started job shows as running.
- A fork run with no relevant change no longer checks out a merged commit it never published, which failed docs-only PRs such as apache#13413.
- `CONTRIBUTING.md` asks forks to allow all actions, after apache#13561's fork allowed only its own.

## How was this patch tested?

A unit check of the script with a stubbed GitHub client covers each case above. In luciferyang-ci-demo/gluten, which plays apache/gluten, the statuses show on a [passing PR](https://github.com/luciferyang-ci-demo/gluten/pull/1) and a [failing PR](https://github.com/luciferyang-ci-demo/gluten/pull/3). A fork run with no relevant change now passes `detect-changes`.

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

Yes. Generated-by: Claude Opus 5.5


Related issue: apache#13541
@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from 68cb0f8 to 6c84299 Compare November 26, 2026 20:09
@github-actions github-actions Bot added BOLT and removed CORE works for Gluten Core labels Nov 26, 2026
Velox supplies the JNI allocation listener through a backend factory, as it does for the JNI input iterator, and wraps reserve and unreserve failures so the Java throwable survives the driver. The global listener uses the same factory.

If returning reclaimed capacity also fails after a failed reservation, keep the reservation failure and log the second error. The JNI listener never calls Java while an exception is pending: a pending exception fails a reservation, and an unreserve still repays before restoring or propagating it.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@felipepessoto
felipepessoto force-pushed the felipepessoto-jni-memory-callback-preservation branch from 7b4d2d0 to 4bc07d5 Compare November 27, 2026 02:42
@felipepessoto felipepessoto changed the title [VL] Preserve Java exceptions from memory callbacks [CORE] Preserve Java exceptions from memory callbacks Nov 27, 2026
felipepessoto and others added 5 commits November 26, 2026 20:02
Keep irreversible Arrow frees separate from Java block charges, and preserve the first failure across rollback, JNI restoration, and low-memory wrapper construction. Exercise each failure at its caller, including reuse of an outstanding block charge.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
An unreserve exception can arrive after Java has returned some or all of a block. Keep that credit unavailable until the drained pool and Java balance confirm settlement, then require a fresh charge. If the balance is positive or cannot be read, propagate failure rather than guessing.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Use the original shared allocators and Java reservation contract. Keep the Velox JNI provider and error precedence on rollback without tracking charged, disputed, or repaid bytes. Compare the Java charge after a callback repays and throws with the plain listener.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve the pending throwable in a guarded JNI entry during ordinary cleanup and arbitration rollback. Keep a completed repayment distinct from a failure to reinstall the throwable, and exercise the worker capture limit and the preallocated-wrapper path.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Reject positive growth with the preallocated Velox system error rather than moving an earlier pending throwable into a native exception. A failed best-effort reservation can then return false inside Velox without erasing the exception owed to Java. Cover both optional and required retries after a failed capture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@felipepessoto
felipepessoto marked this pull request as ready for review November 27, 2026 21:36
Copilot AI balanced review requested due to automatic review settings November 27, 2026 21:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

JNI exception restoration, low-memory fallbacks, and cross-runtime exception transport require maintainer validation of lifecycle and cleanup guarantees despite reported CI success.

No open issues 🎉

What changed in this PR

Extends the Java exception preservation introduced for native iterators in #13561 to Velox memory callbacks, allowing Spark to receive the original throwable rather than a message-only GlutenException.

Changes:

  • Adds backend-specific JNI allocation listener factories and wires Velox listeners into task and global memory managers.
  • Preserves pending exceptions through growth rejection, repayment, rollback, and wrapper allocation failures without changing shared memory accounting.
  • Adds native and Spark regression tests, reusable allocation-failure injection, and documentation.
File Description
docs/​developers/​NewToGluten.md Documents memory callback exception preservation and failure precedence.
cpp/​velox/​tests/​VeloxJniAllocationListenerTest.cc Tests callback wrapping, pending errors, rollback, and allocation failures.
cpp/​velox/​tests/​CMakeLists.txt Registers the new listener regression test executable.
cpp/​velox/​memory/​VeloxMemoryManager.cc Uses exception-aware reservation rollback.
cpp/​velox/​jni/​VeloxJniWrapper.cc Creates the Velox listener for global memory reservations.
cpp/​velox/​jni/​VeloxJavaException.h Adds fallback wrapping when wrapper allocation fails.
cpp/​velox/​jni/​VeloxAllocationListener.h Declares the Velox listener factory and rollback helper.
cpp/​velox/​jni/​VeloxAllocationListener.cc Implements callback wrapping, pending-error preservation, and rollback handling.
cpp/​velox/​compute/​VeloxBackend.cc Registers the Velox JNI allocation listener factory.
cpp/​velox/​CMakeLists.txt Includes the new listener implementation in the build.
cpp/​core/​tests/​utils/​TestAllocationFailure.h Declares the shared allocation-failure injection flag.
cpp/​core/​tests/​utils/​TestAllocationFailure.cc Implements allocation-failure injection for dedicated test executables.
cpp/​core/​tests/​JavaExceptionAllocationTest.cc Reuses the extracted allocation-failure test utility.
cpp/​core/​tests/​CMakeLists.txt Links the shared utility into existing exception tests.
cpp/​core/​jni/​JniWrapper.cc Creates task memory listeners through the backend factory.
cpp/​core/​jni/​JniCommon.h Exposes listener factory APIs and reusable callback methods.
cpp/​core/​jni/​JniCommon.cc Implements factory registration and guarded JNI environment lookup.
backends-velox/​src/​test/​scala/​org/​apache/​spark/​sql/​execution/​GlutenJniExceptionSuite.scala Adds throwable-identity, direct unreserve, and off-heap exhaustion regressions.

Review effort: Balanced

@felipepessoto

Copy link
Copy Markdown
Contributor Author

@zhztheplayer @rdtr, this is ready for review. The change follows the JNI input iterator pattern and keeps shared memory accounting unchanged. I would appreciate your review, especially of how the original Java throwable is preserved during reservation and rollback.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants