Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Java SDK
on:
pull_request:
paths:
- rivetkit-java/**
- rivetkit-rust/packages/**
- engine/**
- Cargo.toml
- Cargo.lock
- rust-toolchain.toml
- .github/workflows/java.yml
push:
branches:
- main
paths:
- rivetkit-java/**
- rivetkit-rust/packages/**
- engine/**
- Cargo.toml
- Cargo.lock
- rust-toolchain.toml
- .github/workflows/java.yml
concurrency:
group: java-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
defaults:
run:
shell: bash -e {0}
env:
CARGO_TERM_COLOR: always
jobs:
test:
name: Rust + Java tests (incl. end to end)
runs-on: depot-ubuntu-24.04-8
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
# Pins java, gradle, rust, cbindgen and jextract from rivetkit-java/mise.toml.
- uses: jdx/mise-action@v2
with:
working_directory: rivetkit-java
- uses: Swatinem/rust-cache@v2
with:
shared-key: java-sdk
cache-on-failure: true
- uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ hashFiles('rivetkit-java/**/*.kts', 'rivetkit-java/gradle/libs.versions.toml') }}
- name: Build the engine (the end-to-end tests spawn it)
run: cargo build -p rivet-engine --bin rivet-engine
- name: Generated header and bindings are current
working-directory: rivetkit-java
run: mise run bindings:check
- name: Exactly one unsafe block
working-directory: rivetkit-java
run: mise run unsafe:check
- name: Tests
working-directory: rivetkit-java
env:
RIVET_ENGINE_BINARY_PATH: ${{ github.workspace }}/target/debug/rivet-engine
run: mise run test
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ members = [
"rivetkit-rust/packages/inspector-protocol",
"rivetkit-rust/packages/rivetkit",
"rivetkit-rust/packages/rivetkit-core",
"rivetkit-rust/packages/rivetkit-ffi",
"rivetkit-rust/packages/rivetkit-host-adapter",
"rivetkit-rust/packages/shared-types",
"rivetkit-typescript/packages/rivetkit-napi",
"rivetkit-typescript/packages/rivetkit-wasm",
Expand Down Expand Up @@ -534,6 +536,10 @@ members = [
path = "rivetkit-rust/packages/client"
version = "=3.1.2-rc.9"

[workspace.dependencies.rivetkit-host-adapter]
path = "rivetkit-rust/packages/rivetkit-host-adapter"
version = "=3.1.2-rc.9"

[workspace.dependencies.rivetkit-actor-persist]
path = "rivetkit-rust/packages/actor-persist"
version = "=3.1.2-rc.9"
Expand Down
4 changes: 4 additions & 0 deletions rivetkit-java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gradle/
build/
**/build/
.kotlin/
62 changes: 62 additions & 0 deletions rivetkit-java/BRIEF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Java SDK for RivetKit: brief (v2)

* **Status:** v2 built. Every test passes; nothing is published yet.
* **Reference:** [`DESIGN.md`](DESIGN.md) is the current design: API, turns, the boundary, and
how to keep it up to date. This brief records the decisions and results.

## What it is

A Java 25 SDK for Rivet Actors that covers every non-deprecated actor and client feature. It
is a thin Java layer over the Rust core:
- **`rivetkit-host-adapter` (new):** the actor host logic, independent of any language binding.
napi, wasm and the Rust SDK can move onto it later; they are unchanged.
- **`rivetkit-ffi`:** a C ABI of 7 functions over the adapter.
- **Java side:** calls that C ABI through FFM.

## Decisions

| Decision | Choice | Why |
|---|---|---|
| Runtime | Java 25, FFM, virtual threads | FFM is final, virtual threads no longer pin on `synchronized`, `ScopedValue` is final. |
| Data model | No actor state; SQLite plus fields | State, connection state, KV, queues and a plain `run` are deprecated in 3.0. This matches the Rust SDK. |
| API | Interface (the contract) + class (the actor) + typed proxy (the client) | No `Integer.class` tokens. javac catches wrong events, params, hooks, schedule and async arguments, and the wrong implementation (15 cases tested). This is the approach Restate's Java SDK moved to in 2.5; Dapr and Temporal use it too. |
| Threading | Turns: one thread at a time per actor; SQL keeps the turn, outbound waits release it | No locks in actor code and no A→B→A deadlock. Strictly safer than JavaScript's `await` interleaving. This is the Cloudflare Durable Objects input-gate rule. |
| Boundary | `u64` handles, a `u32` kind or op, a CBOR header, an untouched body | Payloads pass through and are never re-encoded. Bodies over 64 KiB and every stream chunk are leased, so Java reads them in place. One `unsafe` block. |
| Serialization | CBOR (already Rivet's format for user values) | Each host language needs one codec, not two. BARE, protobuf and FlatBuffers would add a toolchain per language for about 20 small headers. |
| Streams | Credit by lease release (256 KiB window) | The same model as the engine's envoy window updates. A slow reader pauses the sender. |
| Sync across languages | Header types defined on each side, golden fixtures, exhaustive matches, cbindgen enums read by Java as constants, an ABI version check at load | Drift fails a build or a test. Code generation isn't worth it at this size. |

The research behind these decisions surveyed:
- Confluent kafka-clients, Temporal sdk-core, the ScyllaDB Rust drivers, libsignal,
delta-kernel-rs, Arrow, and UniFFI and its users (cross-language data passing);
- Restate, Temporal, Dapr and Akka (JVM API design);
- an Opus review of the API, threading and footguns.

## Results

Benchmark: the same counter actor on each SDK, driven by one Rust client against one local
engine, mean of 2 rounds (`mise run bench:compare`).

| SDK | sequential calls/s | p50 | 20 actors × 100 parallel calls/s | RSS |
|---|---|---|---|---|
| Rust | 404 | 2426 µs | 1026 | 17 MB |
| **Java** | **392** (−3%) | 2514 µs | **993** (−3%) | 213 MB |
| TypeScript | 385 | 2543 µs | 918 | 152 MB |

Java is within 5% of Rust and ahead of TypeScript. Its memory is the JVM's default heap
sizing; set `-Xmx` to bring it down.

Tests (`mise run test`, about 15 s warm on a 32-core laptop: 4 s Rust, 11 s Java):
- **Rust:** 30 tests (wire behaviour, ordering, ownership, status hold-back, fixture drift, the ABI), plus Miri on the `unsafe` block.
- **Java unit:** codec, turns, API validation, fixtures, and 16 compile-safety cases (15 mistakes, one good program).
- **End to end:** 47 tests against a real engine on a free port, run concurrently on one shared
registry, covering actions, errors, async, events, connections, vars and SQL across sleep,
transactions, HTTP streaming, WebSockets, turn atomicity, A→B→A calls, schedules and cron.

## Next

1. Add `mise run miri` to CI (it needs a nightly toolchain, so it is not in the job yet).
2. Tests still to add: a TypeScript client calling a Java actor, cron firing, and abort on
sleep.
3. Publishing: native jars for each platform, Maven Central, Windows.
4. Move napi and wasm onto `rivetkit-host-adapter`.
Loading