Skip to content

Re-enable sun.misc.Unsafe by default on Java 24+#15296

Merged
chrisvest merged 1 commit intonetty:4.1from
chrisvest:4.1-revert-j24-unsafe
May 30, 2025
Merged

Re-enable sun.misc.Unsafe by default on Java 24+#15296
chrisvest merged 1 commit intonetty:4.1from
chrisvest:4.1-revert-j24-unsafe

Conversation

@chrisvest
Copy link
Copy Markdown
Member

@chrisvest chrisvest commented May 30, 2025

See https://github.com/netty/netty/wiki/Java-24-and-sun.misc.Unsafe for more information.

Motivation:
In #14943 we disabled Unsafe by default on Java 24+, to avoid producing warnings from the JVM. However, disabling the use of Unsafe also disables the hacks we use to access ByteBuffer cleaners to eagerly deallocate them. This can have dire performance consequences, particularly for busy applications.

Disabling the warnings from Unsafe also did nothing to stem the warnings produced by our JNI code, so many systems still see warnings anyway.

We should instead revert the changed default, and just tolerate the warning in Netty 4.1.

In Netty 4.2 we have #15231 as a long-term fix, where we avoid using Unsafe but will have a way to eagerly release the memory of our native ByteBuffer instances.

Modification:
Change the default for our Unsafe usage to no longer be disabled on Java 24+.

Result:
More consistent performance across Java versions.

Motivation:
In netty#14943 we disabled Unsafe by default on Java 24+, to avoid producing warnings from the JVM.
However, disabling the use of Unsafe also disables the hacks we use to access `ByteBuffer` cleaners to eagerly deallocate them.
This can have dire performance consequences, particularly for busy applications.

Disabling the warnings from Unsafe also did nothing to stem the warnings produced by our JNI code, so many systems still see warnings anyway.

We should instead revert the changed default, and just tolerate the warning in Netty 4.1.

In Netty 4.2 we have netty#15231 as a long-term fix, where we avoid using Unsafe but will have a way to eagerly release the memory of our native ByteBuffer instances.

Modification:
Change the default for our Unsafe usage to no longer be disabled on Java 24+.

Result:
More consistent performance across Java versions.
@chrisvest chrisvest requested a review from normanmaurer May 30, 2025 17:10
@normanmaurer normanmaurer added this to the 4.1.122.Final milestone May 30, 2025
@chrisvest chrisvest merged commit 7d6b939 into netty:4.1 May 30, 2025
15 of 16 checks passed
@chrisvest chrisvest deleted the 4.1-revert-j24-unsafe branch May 30, 2025 20:36
LuciferYang pushed a commit to apache/spark that referenced this pull request Jun 11, 2025
…e to 2.0.72.Final

### What changes were proposed in this pull request?

This PR aims to Upgrade Netty to 4.1.122.Final and Netty-tcnative to 2.0.72.Final.

### Why are the changes needed?

To bring the Java 24 related update and the latest bug fixes.
> We decided to enable sun.misc.Unsafe by default again when using JDK24+ (when possible) as disabling it by default on this java versions did come with quite a performance regression.

- https://netty.io/news/2025/06/03/4-1-122-Final.html
    - netty/netty#15296
    - netty/netty#15248
    - netty/netty#15122

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs.

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

No.

Closes #51145 from dongjoon-hyun/SPARK-52438.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
eolivelli added a commit to eolivelli/herddb that referenced this pull request Apr 26, 2026
…rect buffers (#298)

## Summary

Netty 4.1.120 and 4.1.121 disabled their internal `sun.misc.Unsafe`
usage by default
([netty/netty#14975](netty/netty#14975)) to
silence JEP 498 warnings on JDK 24+. With Unsafe disabled,
`PlatformDependent.useDirectBufferNoCleaner()` returns `false`: every
Netty direct buffer (pooled **and** unpooled) falls back to
`ByteBuffer.allocateDirect` → `java.nio.Bits.reserveMemory`, which is
bounded by `-XX:MaxDirectMemorySize`. The result is `OutOfMemoryError`
under heavy ingestion even though `-Dio.netty.maxDirectMemory` was set
generously.

Netty 4.1.122
([netty/netty#15296](netty/netty#15296)) reverts
the default. Until HerdDB upgrades (commit c0cf458 downgraded us
4.1.130 → 4.1.121 for issue #274), add
`--sun-misc-unsafe-memory-access=allow` to:

- `herddb-services/src/main/resources/bin/setenv.sh` — applies to every
JVM launched via `bin/service` (server, indexing-service, file-server,
bookkeeper) and to the CLI tools that inherit `JDK_JAVA_OPTIONS`
(`herddb-cli.sh`, `vector-bench.sh`, `indexing-admin.sh`).
- `pom.xml` — both Surefire `argLine`s (default and `ci` profile) so
unit tests on JDK 25 don't take the slow `Bits.reserveMemory` path
either.

## Verification

Reproduced locally on Eclipse Temurin **JDK 25.0.2** with the in-tree
Netty **4.1.121**.

**Before** (no flag): server saturates `-XX:MaxDirectMemorySize=1g`,
OOMs at 160k/1M rows even with `-Dio.netty.maxDirectMemory=4g`.
`netty_used_direct_memory_bytes` Prometheus gauge stuck at `-1` (counter
inactive).

**After** (with flag): server peak direct memory ~300 MiB, **0
OutOfMemoryErrors** through the full gist1m bench (1M vectors × 960-dim,
8 ingest threads, 5000 batch, post-ingest checkpoint, recall queries).
`netty_used_direct_memory_bytes` reports actual usage (counter active).

| Metric | Before | After |
|---|---|---|
| `netty_used_direct_memory_bytes` | `-1` | active (~134 MiB at end) |
| Server direct mem peak | hits 1024/1024 MiB cap | ~300 MiB / 4 GiB |
| OOMs in 1M-row ingest | 30–66 | **0** |
| Ingest throughput | bench fails | 20,332 rows/s |
| Recall@10 | bench fails | 0.91 |

## Also included

`herddb-services/examples/local-bench/install.sh` got a small batch of
bench-tuning changes that emerged from this investigation:

- `-Dherddb.file.requirefsync=false` for both server and
indexing-service (benchmark-only — never use this in production).
- Explicit `-XX:MaxDirectMemorySize` and `-Dio.netty.maxDirectMemory`
for both services (was leaving Netty at `=0`/JVM-bound; now explicit and
consistent).
- Indexing-service heap reduced from 40g → 30g so the `(server) + (IS)`
budget fits in a 61 GiB host with headroom for the OS.
- Documentation comments explaining the layout.

These are isolated to the `examples/local-bench` directory and don't
affect production defaults.

## Test plan

- [ ] CI runs all unit tests with the new Surefire `argLine` on JDK 25
- [ ] Smoke-test the `local-bench` example: `./teardown.sh &&
./install.sh && ./scripts/run-bench.sh --dataset gist1m -n 1000000
--ingest-threads 8 --batch-size 5000 --checkpoint --wait-for-indexes`

## Long-term follow-up

When HerdDB is upgraded back to **Netty 4.1.122+** (or **4.2.x**, which
is FFM-based and Unsafe-free), the
`--sun-misc-unsafe-memory-access=allow` flag in `setenv.sh` becomes
redundant and the comment block can be removed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants