Skip to content

Commit 607b508

Browse files
Rename ctx.sideEffect in ctx.run (#267)
1 parent b8f4bd3 commit 607b508

File tree

17 files changed

+61
-69
lines changed

17 files changed

+61
-69
lines changed

examples/src/main/java/my/restate/sdk/examples/LoanWorkflow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void run(WorkflowContext ctx, LoanRequest loanRequest) {
103103
LOG.info("Loan request submitted");
104104

105105
// 2. Ask human approval
106-
ctx.sideEffect(() -> askHumanApproval(ctx.workflowKey()));
106+
ctx.run(() -> askHumanApproval(ctx.workflowKey()));
107107
ctx.set(STATUS, Status.WAITING_HUMAN_APPROVAL);
108108

109109
// 3. Wait human approval

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/ContextImpl.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ internal class ContextImpl internal constructor(private val syscalls: Syscalls)
130130
}
131131
}
132132

133-
override suspend fun <T : Any?> sideEffect(
134-
serde: Serde<T>,
135-
sideEffectAction: suspend () -> T
136-
): T {
133+
override suspend fun <T : Any?> run(serde: Serde<T>, sideEffectAction: suspend () -> T): T {
137134
val exitResult =
138135
suspendCancellableCoroutine { cont: CancellableContinuation<CompletableDeferred<ByteString>>
139136
->

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/api.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import kotlin.time.Duration
1717

1818
/**
1919
* This interface exposes the Restate functionalities to Restate services. It can be used to
20-
* interact with other Restate services, record side effects, execute timers and synchronize with
21-
* external systems.
20+
* interact with other Restate services, record non-deterministic closures, execute timers and
21+
* synchronize with external systems.
2222
*
2323
* To use it within your Restate service, implement [RestateKtComponent] and get an instance with
2424
* [RestateKtComponent.restateContext].
@@ -114,9 +114,9 @@ sealed interface Context {
114114
* Errors occurring within this closure won't be propagated to the caller, unless they are
115115
* [TerminalException]. Consider the following code:
116116
* ```
117-
* // Bad usage of try-catch outside the side effect
117+
* // Bad usage of try-catch outside the run
118118
* try {
119-
* ctx.sideEffect {
119+
* ctx.run {
120120
* throw IllegalStateException();
121121
* };
122122
* } catch (e: IllegalStateException) {
@@ -125,29 +125,28 @@ sealed interface Context {
125125
* // following the invocation retry policy.
126126
* }
127127
*
128-
* // Good usage of try-catch outside the side effect
128+
* // Good usage of try-catch outside the run
129129
* try {
130-
* ctx.sideEffect {
130+
* ctx.run {
131131
* throw TerminalException("my error");
132132
* };
133133
* } catch (e: TerminalException) {
134134
* // This is invoked
135135
* }
136136
* ```
137137
*
138-
* To propagate side effects failures to the side effect call-site, make sure to wrap them in
139-
* [TerminalException].
138+
* To propagate failures to the run call-site, make sure to wrap them in [TerminalException].
140139
*
141140
* @param serde the type tag of the return value, used to serialize/deserialize it.
142-
* @param action to execute for its side effects.
141+
* @param action closure to execute.
143142
* @param T type of the return value.
144-
* @return value of the side effect operation.
143+
* @return value of the run operation.
145144
*/
146-
suspend fun <T : Any?> sideEffect(serde: Serde<T>, sideEffectAction: suspend () -> T): T
145+
suspend fun <T : Any?> run(serde: Serde<T>, sideEffectAction: suspend () -> T): T
147146

148-
/** Like [sideEffect] without a return value. */
149-
suspend fun sideEffect(sideEffectAction: suspend () -> Unit) {
150-
sideEffect(KtSerdes.UNIT, sideEffectAction)
147+
/** Like [run] without a return value. */
148+
suspend fun run(sideEffectAction: suspend () -> Unit) {
149+
run(KtSerdes.UNIT, sideEffectAction)
151150
}
152151

153152
/**
@@ -177,9 +176,9 @@ sealed interface Context {
177176
*
178177
* This instance is useful to generate identifiers, idempotency keys, and for uniform sampling
179178
* from a set of options. If a cryptographically secure value is needed, please generate that
180-
* externally using [sideEffect].
179+
* externally using [run].
181180
*
182-
* You MUST NOT use this [Random] instance inside a [sideEffect].
181+
* You MUST NOT use this [Random] instance inside a [run].
183182
*
184183
* @return the [Random] instance.
185184
*/
@@ -234,7 +233,7 @@ class RestateRandom(seed: Long, private val syscalls: Syscalls) : Random() {
234233
private val r = Random(seed)
235234

236235
override fun nextBits(bitCount: Int): Int {
237-
check(!syscalls.isInsideSideEffect) { "You can't use RestateRandom inside a side effect!" }
236+
check(!syscalls.isInsideSideEffect) { "You can't use RestateRandom inside ctx.run!" }
238237
return r.nextBits(bitCount)
239238
}
240239

sdk-api-kotlin/src/test/kotlin/dev/restate/sdk/kotlin/RandomTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RandomTest : RandomTestSuite() {
2121

2222
override fun randomInsideSideEffect(): TestInvocationBuilder =
2323
testDefinitionForService<Unit, Int>("RandomInsideSideEffect") { ctx, _: Unit ->
24-
ctx.sideEffect { ctx.random().nextInt() }
24+
ctx.run { ctx.random().nextInt() }
2525
throw IllegalStateException("This should not unreachable")
2626
}
2727

sdk-api-kotlin/src/test/kotlin/dev/restate/sdk/kotlin/SideEffectTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class SideEffectTest : SideEffectTestSuite() {
2323

2424
override fun sideEffect(sideEffectOutput: String): TestInvocationBuilder =
2525
testDefinitionForService("SideEffect") { ctx, _: Unit ->
26-
val result = ctx.sideEffect(CoreSerdes.JSON_STRING) { sideEffectOutput }
26+
val result = ctx.run(CoreSerdes.JSON_STRING) { sideEffectOutput }
2727
"Hello $result"
2828
}
2929

3030
override fun consecutiveSideEffect(sideEffectOutput: String): TestInvocationBuilder =
3131
testDefinitionForService("ConsecutiveSideEffect") { ctx, _: Unit ->
32-
val firstResult = ctx.sideEffect(CoreSerdes.JSON_STRING) { sideEffectOutput }
32+
val firstResult = ctx.run(CoreSerdes.JSON_STRING) { sideEffectOutput }
3333
val secondResult =
34-
ctx.sideEffect(CoreSerdes.JSON_STRING) { firstResult.uppercase(Locale.getDefault()) }
34+
ctx.run(CoreSerdes.JSON_STRING) { firstResult.uppercase(Locale.getDefault()) }
3535
"Hello $secondResult"
3636
}
3737

@@ -43,9 +43,7 @@ class SideEffectTest : SideEffectTestSuite() {
4343
Dispatchers.Unconfined + CoroutineName("CheckContextSwitchingTestCoroutine"))) {
4444
handler("run") { ctx, _: Unit ->
4545
val sideEffectCoroutine =
46-
ctx.sideEffect(CoreSerdes.JSON_STRING) {
47-
coroutineContext[CoroutineName]!!.name
48-
}
46+
ctx.run(CoreSerdes.JSON_STRING) { coroutineContext[CoroutineName]!!.name }
4947
check(sideEffectCoroutine == "CheckContextSwitchingTestCoroutine") {
5048
"Side effect thread is not running within the same coroutine context of the handler method: $sideEffectCoroutine"
5149
}
@@ -56,7 +54,7 @@ class SideEffectTest : SideEffectTestSuite() {
5654

5755
override fun sideEffectGuard(): TestInvocationBuilder =
5856
testDefinitionForService<Unit, String>("SideEffectGuard") { ctx, _: Unit ->
59-
ctx.sideEffect { ctx.send(GREETER_SERVICE_TARGET, KtSerdes.json(), "something") }
57+
ctx.run { ctx.send(GREETER_SERVICE_TARGET, KtSerdes.json(), "something") }
6058
throw IllegalStateException("This point should not be reached")
6159
}
6260
}

sdk-api-kotlin/src/test/kotlin/dev/restate/sdk/kotlin/StateMachineFailuresTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class StateMachineFailuresTest : StateMachineFailuresTestSuite() {
4747

4848
override fun sideEffectFailure(serde: Serde<Int>): TestInvocationBuilder =
4949
testDefinitionForService("SideEffectFailure") { ctx, _: Unit ->
50-
ctx.sideEffect(serde) { 0 }
50+
ctx.run(serde) { 0 }
5151
"Francesco"
5252
}
5353
}

sdk-api-kotlin/src/test/kotlin/dev/restate/sdk/kotlin/UserFailuresTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class UserFailuresTest : UserFailuresTestSuite() {
2626
): TestInvocationBuilder =
2727
testDefinitionForService<Unit, Unit>("SideEffectThrowIllegalStateException") { ctx, _: Unit ->
2828
try {
29-
ctx.sideEffect { throw IllegalStateException("Whatever") }
29+
ctx.run { throw IllegalStateException("Whatever") }
3030
} catch (e: Throwable) {
3131
if (e !is CancellationException && e !is TerminalException) {
3232
nonTerminalExceptionsSeen.addAndGet(1)
@@ -44,7 +44,7 @@ class UserFailuresTest : UserFailuresTestSuite() {
4444

4545
override fun sideEffectThrowTerminalException(code: Int, message: String): TestInvocationBuilder =
4646
testDefinitionForService<Unit, Unit>("SideEffectThrowTerminalException") { ctx, _: Unit ->
47-
ctx.sideEffect { throw TerminalException(code, message) }
47+
ctx.run { throw TerminalException(code, message) }
4848
throw IllegalStateException("Not expected to reach this point")
4949
}
5050
}

sdk-api/src/main/java/dev/restate/sdk/Context.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
/**
1717
* This interface exposes the Restate functionalities to Restate services. It can be used to
18-
* interact with other Restate services, record side effects, execute timers and synchronize with
19-
* external systems.
18+
* interact with other Restate services, record non-deterministic closures, execute timers and
19+
* synchronize with external systems.
2020
*
2121
* <p>All methods of this interface, and related interfaces, throws either {@link TerminalException}
2222
* or {@link AbortedExecutionException}, where the former can be caught and acted upon, while the
@@ -109,9 +109,9 @@ default void sleep(Duration duration) {
109109
* TerminalException}. Consider the following code:
110110
*
111111
* <pre>{@code
112-
* // Bad usage of try-catch outside the side effect
112+
* // Bad usage of try-catch outside the run
113113
* try {
114-
* ctx.sideEffect(() -> {
114+
* ctx.run(() -> {
115115
* throw new IllegalStateException();
116116
* });
117117
* } catch (IllegalStateException e) {
@@ -120,29 +120,29 @@ default void sleep(Duration duration) {
120120
* // following the invocation retry policy.
121121
* }
122122
*
123-
* // Good usage of try-catch outside the side effect
123+
* // Good usage of try-catch outside the run
124124
* try {
125-
* ctx.sideEffect(() -> {
125+
* ctx.run(() -> {
126126
* throw new TerminalException("my error");
127127
* });
128128
* } catch (TerminalException e) {
129129
* // This is invoked
130130
* }
131131
* }</pre>
132132
*
133-
* To propagate side effects failures to the side effect call-site, make sure to wrap them in
134-
* {@link TerminalException}.
133+
* To propagate run failures to the call-site, make sure to wrap them in {@link
134+
* TerminalException}.
135135
*
136136
* @param serde the type tag of the return value, used to serialize/deserialize it.
137-
* @param action to execute for its side effects.
137+
* @param action closure to execute.
138138
* @param <T> type of the return value.
139-
* @return value of the side effect operation.
139+
* @return value of the run operation.
140140
*/
141-
<T> T sideEffect(Serde<T> serde, ThrowingSupplier<T> action) throws TerminalException;
141+
<T> T run(Serde<T> serde, ThrowingSupplier<T> action) throws TerminalException;
142142

143-
/** Like {@link #sideEffect(Serde, ThrowingSupplier)}, but without returning a value. */
144-
default void sideEffect(ThrowingRunnable runnable) throws TerminalException {
145-
sideEffect(
143+
/** Like {@link #run(Serde, ThrowingSupplier)}, but without returning a value. */
144+
default void run(ThrowingRunnable runnable) throws TerminalException {
145+
run(
146146
CoreSerdes.VOID,
147147
() -> {
148148
runnable.run();

sdk-api/src/main/java/dev/restate/sdk/ContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public <T> void send(Target target, Serde<T> inputSerde, T parameter, Duration d
110110
}
111111

112112
@Override
113-
public <T> T sideEffect(Serde<T> serde, ThrowingSupplier<T> action) {
113+
public <T> T run(Serde<T> serde, ThrowingSupplier<T> action) {
114114
CompletableFuture<CompletableFuture<ByteString>> enterFut = new CompletableFuture<>();
115115
syscalls.enterSideEffectBlock(
116116
new EnterSideEffectSyscallCallback() {

sdk-api/src/main/java/dev/restate/sdk/RestateRandom.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
*
2222
* <p>This instance is useful to generate identifiers, idempotency keys, and for uniform sampling
2323
* from a set of options. If a cryptographically secure value is needed, please generate that
24-
* externally using {@link ObjectContext#sideEffect(Serde, ThrowingSupplier)}.
24+
* externally using {@link ObjectContext#run(Serde, ThrowingSupplier)}.
2525
*
26-
* <p>You MUST NOT use this object inside a {@link ObjectContext#sideEffect(Serde,
27-
* ThrowingSupplier)}.
26+
* <p>You MUST NOT use this object inside a {@link ObjectContext#run(Serde, ThrowingSupplier)}.
2827
*/
2928
public class RestateRandom extends Random {
3029

@@ -58,7 +57,7 @@ public UUID nextUUID() {
5857
@Override
5958
protected int next(int bits) {
6059
if (this.syscalls.isInsideSideEffect()) {
61-
throw new IllegalStateException("You can't use RestateRandom inside a side effect!");
60+
throw new IllegalStateException("You can't use RestateRandom inside ctx.run!");
6261
}
6362

6463
return super.next(bits);

0 commit comments

Comments
 (0)