|
12 | 12 | import dev.restate.sdk.common.RetryPolicy; |
13 | 13 | import dev.restate.sdk.common.TerminalException; |
14 | 14 | import dev.restate.sdk.core.statemachine.StateMachine; |
15 | | -import dev.restate.sdk.core.statemachine.ffm.generated.NonEmptyValueAbi; |
16 | 15 | import dev.restate.sdk.core.statemachine.ffm.generated.SharedCoreNative; |
17 | 16 | import dev.restate.sdk.core.statemachine.ffm.generated.Slice; |
18 | 17 | import dev.restate.sdk.core.statemachine.ffm.generated.TargetAbi; |
@@ -86,6 +85,11 @@ static long len(MemorySegment seg) { |
86 | 85 | return seg.byteSize(); |
87 | 86 | } |
88 | 87 |
|
| 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 | + |
89 | 93 | // ------------------------------------------------------------------------- |
90 | 94 | // Result Slice reading (outputs: owned by Rust, must be freed) |
91 | 95 | // ------------------------------------------------------------------------- |
@@ -136,22 +140,10 @@ static dev.restate.common.Slice wrapOwnedSlice(MemorySegment sliceStruct) { |
136 | 140 | return new MemorySegmentSlice(seg); |
137 | 141 | } |
138 | 142 |
|
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 | | - |
151 | 143 | private static final byte[] EMPTY_BYTES = new byte[0]; |
152 | 144 |
|
153 | 145 | // ------------------------------------------------------------------------- |
154 | | - // TargetAbi & NonEmptyValueAbi (passed by value as a MemorySegment) |
| 146 | + // TargetAbi (passed by value as a MemorySegment) |
155 | 147 | // ------------------------------------------------------------------------- |
156 | 148 |
|
157 | 149 | /** |
@@ -189,30 +181,6 @@ static MemorySegment buildTarget( |
189 | 181 | return t; |
190 | 182 | } |
191 | 183 |
|
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 | | - |
216 | 184 | // ------------------------------------------------------------------------- |
217 | 185 | // Blob encoders (little-endian, matching the Rust decode_* functions) |
218 | 186 | // ------------------------------------------------------------------------- |
@@ -307,38 +275,20 @@ private static void encodeFailureInto(LeBuffer buf, TerminalException failure) { |
307 | 275 | } |
308 | 276 |
|
309 | 277 | /** |
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. |
316 | 281 | */ |
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) { |
324 | 284 | 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); |
342 | 292 | } |
343 | 293 | encodeRetryPolicyInto(buf, retryPolicy); |
344 | 294 | return buf.toByteArray(); |
|
0 commit comments