Interface Context
-
- All Known Subinterfaces:
ObjectContext
@NotThreadSafe public interface ContextThis interface exposes the Restate functionalities to Restate services. It can be used to interact with other Restate services, record side effects, execute timers and synchronize with external systems.All methods of this interface, and related interfaces, throws either
TerminalExceptionorAbortedExecutionException, where the former can be caught and acted upon, while the latter MUST NOT be caught, but simply propagated for clean up purposes.NOTE: This interface MUST NOT be accessed concurrently since it can lead to different orderings of user actions, corrupting the execution of the invocation.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <T> Awakeable<T>awakeable(dev.restate.sdk.common.Serde<T> serde)Create anAwakeable, addressable throughAwakeable.id().AwakeableHandleawakeableHandle(java.lang.String id)Create a newAwakeableHandlefor the provided identifier.default Awaitable<byte[]>call(dev.restate.sdk.common.Target target, byte[] parameter)Likecall(Target, Serde, Serde, Object)with raw input/output.<T,R>
Awaitable<R>call(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, dev.restate.sdk.common.Serde<R> outputSerde, T parameter)Invoke another Restate service method.<T,R>
Awaitable<R>call(io.grpc.MethodDescriptor<T,R> methodDescriptor, T parameter)Invoke another Restate service method.static Contextcurrent()Create aObjectContext.default voiddelayedCall(dev.restate.sdk.common.Target target, byte[] parameter, java.time.Duration delay)LikedelayedCall(Target, Serde, Object, Duration)with raw input.<T> voiddelayedCall(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, T parameter, java.time.Duration delay)Invoke another Restate service without waiting for the response after the provideddelayhas elapsed.<T> voiddelayedCall(io.grpc.MethodDescriptor<T,?> methodDescriptor, T parameter, java.time.Duration delay)Invoke another Restate service without waiting for the response after the provideddelayhas elapsed.static ContextfromSyscalls(dev.restate.sdk.common.syscalls.Syscalls syscalls)Build a RestateContext from the underlyingSyscallsobject.default io.grpc.ChannelgrpcChannel()Create aChannelto use with generated blocking stubs to invoke other Restate services.default voidoneWayCall(dev.restate.sdk.common.Target target, byte[] parameter)LikeoneWayCall(Target, Serde, Object)with raw input.<T> voidoneWayCall(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, T parameter)Invoke another Restate service without waiting for the response.<T> voidoneWayCall(io.grpc.MethodDescriptor<T,?> methodDescriptor, T parameter)Invoke another Restate service without waiting for the response.RestateRandomrandom()default voidsideEffect(dev.restate.sdk.common.function.ThrowingRunnable runnable)LikesideEffect(Serde, ThrowingSupplier), but without returning a value.<T> TsideEffect(dev.restate.sdk.common.Serde<T> serde, dev.restate.sdk.common.function.ThrowingSupplier<T> action)Execute a non-deterministic closure, recording the result value in the journal.default voidsleep(java.time.Duration duration)Causes the current execution of the function invocation to sleep for the given duration.Awaitable<java.lang.Void>timer(java.time.Duration duration)Causes the start of a timer for the given duration.
-
-
-
Method Detail
-
call
<T,R> Awaitable<R> call(io.grpc.MethodDescriptor<T,R> methodDescriptor, T parameter)
Invoke another Restate service method.- Parameters:
methodDescriptor- The method descriptor of the method to invoke. This is found in the generated `*Grpc` class.parameter- the invocation request parameter.- Returns:
- an
Awaitablethat wraps the Restate service method result.
-
call
<T,R> Awaitable<R> call(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, dev.restate.sdk.common.Serde<R> outputSerde, T parameter)
Invoke another Restate service method.- Parameters:
target- the address of the calleeinputSerde- Input serdeoutputSerde- Output serdeparameter- the invocation request parameter.- Returns:
- an
Awaitablethat wraps the Restate service method result.
-
call
default Awaitable<byte[]> call(dev.restate.sdk.common.Target target, byte[] parameter)
Likecall(Target, Serde, Serde, Object)with raw input/output.
-
grpcChannel
default io.grpc.Channel grpcChannel()
Create aChannelto use with generated blocking stubs to invoke other Restate services.The returned
Channelwill execute the requests using thecall(MethodDescriptor, Object)method.Please note that errors will be propagated as
TerminalExceptionand not asStatusRuntimeException.- Returns:
- a
Channelto send requests through Restate.
-
oneWayCall
<T> void oneWayCall(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, T parameter)Invoke another Restate service without waiting for the response.- Parameters:
target- the address of the calleeinputSerde- Input serdeparameter- the invocation request parameter.
-
oneWayCall
default void oneWayCall(dev.restate.sdk.common.Target target, byte[] parameter)LikeoneWayCall(Target, Serde, Object)with raw input.
-
oneWayCall
<T> void oneWayCall(io.grpc.MethodDescriptor<T,?> methodDescriptor, T parameter)Invoke another Restate service without waiting for the response.- Parameters:
methodDescriptor- The method descriptor of the method to invoke. This is found in the generated `*Grpc` class.parameter- the invocation request parameter.
-
delayedCall
<T> void delayedCall(dev.restate.sdk.common.Target target, dev.restate.sdk.common.Serde<T> inputSerde, T parameter, java.time.Duration delay)Invoke another Restate service without waiting for the response after the provideddelayhas elapsed.This method returns immediately, as the timer is executed and awaited on Restate.
- Parameters:
target- the address of the calleeinputSerde- Input serdeparameter- the invocation request parameter.delay- time to wait before executing the call.
-
delayedCall
default void delayedCall(dev.restate.sdk.common.Target target, byte[] parameter, java.time.Duration delay)LikedelayedCall(Target, Serde, Object, Duration)with raw input.
-
delayedCall
<T> void delayedCall(io.grpc.MethodDescriptor<T,?> methodDescriptor, T parameter, java.time.Duration delay)Invoke another Restate service without waiting for the response after the provideddelayhas elapsed.This method returns immediately, as the timer is executed and awaited on Restate.
- Parameters:
methodDescriptor- The method descriptor of the method to invoke. This is found in the generated*Grpcclass.parameter- the invocation request parameter.delay- time to wait before executing the call.
-
sleep
default void sleep(java.time.Duration duration)
Causes the current execution of the function invocation to sleep for the given duration.- Parameters:
duration- for which to sleep.
-
timer
Awaitable<java.lang.Void> timer(java.time.Duration duration)
Causes the start of a timer for the given duration. You can await on the timer end by invokingAwaitable.await().- Parameters:
duration- for which to sleep.
-
sideEffect
<T> T sideEffect(dev.restate.sdk.common.Serde<T> serde, dev.restate.sdk.common.function.ThrowingSupplier<T> action) throws dev.restate.sdk.common.TerminalExceptionExecute a non-deterministic closure, recording the result value in the journal. The result value will be re-played in case of re-invocation (e.g. because of failure recovery or suspension point) without re-executing the closure. Use this feature if you want to perform non-deterministic operations.The closure should tolerate retries, that is Restate might re-execute the closure multiple times until it records a result.
Error handling
Errors occurring within this closure won't be propagated to the caller, unless they areTerminalException. Consider the following code:
To propagate side effects failures to the side effect call-site, make sure to wrap them in// Bad usage of try-catch outside the side effect try { ctx.sideEffect(() -> { throw new IllegalStateException(); }); } catch (IllegalStateException e) { // This will never be executed, // but the error will be retried by Restate, // following the invocation retry policy. } // Good usage of try-catch outside the side effect try { ctx.sideEffect(() -> { throw new TerminalException("my error"); }); } catch (TerminalException e) { // This is invoked }TerminalException.- Type Parameters:
T- type of the return value.- Parameters:
serde- the type tag of the return value, used to serialize/deserialize it.action- to execute for its side effects.- Returns:
- value of the side effect operation.
- Throws:
dev.restate.sdk.common.TerminalException
-
sideEffect
default void sideEffect(dev.restate.sdk.common.function.ThrowingRunnable runnable) throws dev.restate.sdk.common.TerminalExceptionLikesideEffect(Serde, ThrowingSupplier), but without returning a value.- Throws:
dev.restate.sdk.common.TerminalException
-
awakeable
<T> Awakeable<T> awakeable(dev.restate.sdk.common.Serde<T> serde)
Create anAwakeable, addressable throughAwakeable.id().You can use this feature to implement external asynchronous systems interactions, for example you can send a Kafka record including the
Awakeable.id(), and then let another service consume from Kafka the responses of given external system interaction by usingawakeableHandle(String).
-
awakeableHandle
AwakeableHandle awakeableHandle(java.lang.String id)
Create a newAwakeableHandlefor the provided identifier. You can use it toAwakeableHandle.resolve(Serde, Object)orAwakeableHandle.reject(String)the linkedAwakeable.- See Also:
Awakeable
-
random
RestateRandom random()
- See Also:
RestateRandom
-
current
static Context current()
Create aObjectContext. This will look up the thread-local/async-context storage for the underlying context implementation, so make sure to call it always from the same context where the service is executed.
-
fromSyscalls
static Context fromSyscalls(dev.restate.sdk.common.syscalls.Syscalls syscalls)
Build a RestateContext from the underlyingSyscallsobject.
-
-