Skip to content

Commit 3cbc9df

Browse files
Remove some slop from the interface rust <-> java
1 parent f5ef901 commit 3cbc9df

4 files changed

Lines changed: 574 additions & 498 deletions

File tree

sdk-core/src/main/java23/dev/restate/sdk/core/statemachine/ffm/FfmEncoding.java

Lines changed: 18 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import dev.restate.sdk.common.RetryPolicy;
1313
import dev.restate.sdk.common.TerminalException;
1414
import dev.restate.sdk.core.statemachine.StateMachine;
15-
import dev.restate.sdk.core.statemachine.ffm.generated.NonEmptyValueAbi;
1615
import dev.restate.sdk.core.statemachine.ffm.generated.SharedCoreNative;
1716
import dev.restate.sdk.core.statemachine.ffm.generated.Slice;
1817
import dev.restate.sdk.core.statemachine.ffm.generated.TargetAbi;
@@ -86,6 +85,11 @@ static long len(MemorySegment seg) {
8685
return seg.byteSize();
8786
}
8887

88+
/** Allocates a result {@link Slice} struct out-param (for calls that write a bare Slice). */
89+
static MemorySegment allocateSliceStruct(SegmentAllocator alloc) {
90+
return Slice.allocate(alloc);
91+
}
92+
8993
// -------------------------------------------------------------------------
9094
// Result Slice reading (outputs: owned by Rust, must be freed)
9195
// -------------------------------------------------------------------------
@@ -136,22 +140,10 @@ static dev.restate.common.Slice wrapOwnedSlice(MemorySegment sliceStruct) {
136140
return new MemorySegmentSlice(seg);
137141
}
138142

139-
/**
140-
* Releases an owned result {@link Slice} we are not consuming (e.g. the unused {@code value}/
141-
* {@code extra} arm of a notification). No-op for the null/empty slice.
142-
*/
143-
static void freeSlice(MemorySegment sliceStruct) {
144-
MemorySegment ptr = Slice.ptr(sliceStruct);
145-
long len = Slice.len(sliceStruct);
146-
if (ptr.address() != 0 && len != 0) {
147-
SharedCoreNative.free_buffer(ptr, len);
148-
}
149-
}
150-
151143
private static final byte[] EMPTY_BYTES = new byte[0];
152144

153145
// -------------------------------------------------------------------------
154-
// TargetAbi & NonEmptyValueAbi (passed by value as a MemorySegment)
146+
// TargetAbi (passed by value as a MemorySegment)
155147
// -------------------------------------------------------------------------
156148

157149
/**
@@ -189,30 +181,6 @@ static MemorySegment buildTarget(
189181
return t;
190182
}
191183

192-
/** Builds a success-valued {@link NonEmptyValueAbi} struct, copying {@code value} zero-hop. */
193-
static MemorySegment buildSuccessValue(SegmentAllocator alloc, dev.restate.common.Slice value) {
194-
MemorySegment v = NonEmptyValueAbi.allocate(alloc);
195-
NonEmptyValueAbi.is_failure(v, 0);
196-
MemorySegment val = allocateSlice(alloc, value);
197-
NonEmptyValueAbi.value_ptr(v, val);
198-
NonEmptyValueAbi.value_len(v, len(val));
199-
NonEmptyValueAbi.failure_ptr(v, MemorySegment.NULL);
200-
NonEmptyValueAbi.failure_len(v, 0L);
201-
return v;
202-
}
203-
204-
/** Builds a failure-valued {@link NonEmptyValueAbi} struct. */
205-
static MemorySegment buildFailureValue(SegmentAllocator alloc, TerminalException failure) {
206-
MemorySegment v = NonEmptyValueAbi.allocate(alloc);
207-
NonEmptyValueAbi.is_failure(v, 1);
208-
NonEmptyValueAbi.value_ptr(v, MemorySegment.NULL);
209-
NonEmptyValueAbi.value_len(v, 0L);
210-
MemorySegment fail = allocateBytes(alloc, encodeFailure(failure));
211-
NonEmptyValueAbi.failure_ptr(v, fail);
212-
NonEmptyValueAbi.failure_len(v, len(fail));
213-
return v;
214-
}
215-
216184
// -------------------------------------------------------------------------
217185
// Blob encoders (little-endian, matching the Rust decode_* functions)
218186
// -------------------------------------------------------------------------
@@ -307,38 +275,20 @@ private static void encodeFailureInto(LeBuffer buf, TerminalException failure) {
307275
}
308276

309277
/**
310-
* Encodes the {@code propose_run_completion} params buffer: {@code u64 attempt_duration_millis}
311-
* followed by, for a retryable failure, {@code u16 code, str message, u8 has_stacktrace, [str
312-
* stacktrace]}; for a terminal failure, the {@code decode_failure} layout; then the retry-policy
313-
* blob. The leading {@code value} (run success bytes) is passed separately.
314-
*
315-
* @param resultKind 0 success, 1 terminal failure, 2 retryable failure.
278+
* Encodes the {@code propose_run_completion_retryable_failure} params buffer: {@code u16 code,
279+
* str message, u8 has_stacktrace, [str stacktrace]}, then the retry-policy blob. The attempt
280+
* duration is passed as a direct arg.
316281
*/
317-
static byte[] encodeRunCompletionParams(
318-
int resultKind,
319-
long attemptDurationMillis,
320-
@Nullable TerminalException terminalFailure,
321-
@Nullable String retryableMessage,
322-
@Nullable String retryableStacktrace,
323-
@Nullable RetryPolicy retryPolicy) {
282+
static byte[] encodeRetryableRunParams(
283+
String message, @Nullable String stacktrace, @Nullable RetryPolicy retryPolicy) {
324284
LeBuffer buf = new LeBuffer();
325-
buf.putU64(attemptDurationMillis);
326-
switch (resultKind) {
327-
case 0 -> {
328-
// success: no failure payload in params
329-
}
330-
case 1 -> encodeFailureInto(buf, terminalFailure);
331-
default -> {
332-
// retryable failure
333-
buf.putU16(500);
334-
buf.putStr(retryableMessage != null ? retryableMessage : "");
335-
if (retryableStacktrace != null) {
336-
buf.putU8(1);
337-
buf.putStr(retryableStacktrace);
338-
} else {
339-
buf.putU8(0);
340-
}
341-
}
285+
buf.putU16(500);
286+
buf.putStr(message != null ? message : "");
287+
if (stacktrace != null) {
288+
buf.putU8(1);
289+
buf.putStr(stacktrace);
290+
} else {
291+
buf.putU8(0);
342292
}
343293
encodeRetryPolicyInto(buf, retryPolicy);
344294
return buf.toByteArray();

0 commit comments

Comments
 (0)