-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce deterministic random #178
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec35d36
Introduce deterministic random
slinkydeveloper 6ffd956
Improve random seed generation
slinkydeveloper 84fee68
Introduce Restate random in sdk-api
slinkydeveloper fad18b5
Add inside side effect guard
slinkydeveloper 8422410
Add test for Random
slinkydeveloper 332895f
Introduce similar RestateRandom to Kotlin API
slinkydeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
sdk-api-kotlin/src/test/kotlin/dev/restate/sdk/kotlin/RandomTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk.kotlin | ||
|
||
import dev.restate.sdk.core.RandomTestSuite | ||
import dev.restate.sdk.core.testservices.GreeterGrpcKt | ||
import dev.restate.sdk.core.testservices.GreetingRequest | ||
import dev.restate.sdk.core.testservices.GreetingResponse | ||
import dev.restate.sdk.core.testservices.greetingResponse | ||
import io.grpc.BindableService | ||
import kotlin.random.Random | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
class RandomTest : RandomTestSuite() { | ||
private class RandomShouldBeDeterministic : | ||
GreeterGrpcKt.GreeterCoroutineImplBase(Dispatchers.Unconfined), RestateKtService { | ||
|
||
override suspend fun greet(request: GreetingRequest): GreetingResponse { | ||
val number = restateContext().random().nextInt() | ||
return greetingResponse { message = number.toString() } | ||
} | ||
} | ||
|
||
override fun randomShouldBeDeterministic(): BindableService { | ||
return RandomShouldBeDeterministic() | ||
} | ||
|
||
private class RandomInsideSideEffect : | ||
GreeterGrpcKt.GreeterCoroutineImplBase(Dispatchers.Unconfined), RestateKtService { | ||
override suspend fun greet(request: GreetingRequest): GreetingResponse { | ||
val ctx = restateContext() | ||
ctx.sideEffect { ctx.random().nextInt() } | ||
throw IllegalStateException("This should not unreachable") | ||
} | ||
} | ||
|
||
override fun randomInsideSideEffect(): BindableService { | ||
return RandomInsideSideEffect() | ||
} | ||
|
||
override fun getExpectedInt(seed: Long): Int { | ||
return Random(seed).nextInt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk; | ||
|
||
import dev.restate.sdk.common.InvocationId; | ||
import dev.restate.sdk.common.Serde; | ||
import dev.restate.sdk.common.function.ThrowingSupplier; | ||
import dev.restate.sdk.common.syscalls.Syscalls; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Subclass of {@link Random} inherently predictable, seeded on the {@link InvocationId}, which is | ||
* not secret. | ||
* | ||
* <p>This instance is useful to generate identifiers, idempotency keys, and for uniform sampling | ||
* from a set of options. If a cryptographically secure value is needed, please generate that | ||
* externally using {@link RestateContext#sideEffect(Serde, ThrowingSupplier)}. | ||
* | ||
* <p>You MUST NOT use this object inside a {@link RestateContext#sideEffect(Serde, | ||
* ThrowingSupplier)}. | ||
*/ | ||
public class RestateRandom extends Random { | ||
|
||
private final Syscalls syscalls; | ||
private boolean seedInitialized = false; | ||
|
||
RestateRandom(long randomSeed, Syscalls syscalls) { | ||
super(randomSeed); | ||
this.syscalls = syscalls; | ||
} | ||
|
||
/** | ||
* @throws UnsupportedOperationException You cannot set the seed on RestateRandom | ||
*/ | ||
@Override | ||
public synchronized void setSeed(long seed) { | ||
if (seedInitialized) { | ||
throw new UnsupportedOperationException("You cannot set the seed on RestateRandom"); | ||
} | ||
super.setSeed(seed); | ||
this.seedInitialized = true; | ||
} | ||
|
||
/** | ||
* @return a UUID generated using this RNG. | ||
*/ | ||
public UUID nextUUID() { | ||
return new UUID(this.nextLong(), this.nextLong()); | ||
} | ||
|
||
@Override | ||
protected int next(int bits) { | ||
if (this.syscalls.isInsideSideEffect()) { | ||
throw new IllegalStateException("You can't use RestateRandom inside a side effect!"); | ||
} | ||
|
||
return super.next(bits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH | ||
// | ||
// This file is part of the Restate Java SDK, | ||
// which is released under the MIT license. | ||
// | ||
// You can find a copy of the license in file LICENSE in the root | ||
// directory of this repository or package, or at | ||
// https://github.com/restatedev/sdk-java/blob/main/LICENSE | ||
package dev.restate.sdk; | ||
|
||
import dev.restate.sdk.common.TerminalException; | ||
import dev.restate.sdk.core.RandomTestSuite; | ||
import dev.restate.sdk.core.testservices.GreeterRestate; | ||
import dev.restate.sdk.core.testservices.GreetingRequest; | ||
import dev.restate.sdk.core.testservices.GreetingResponse; | ||
import io.grpc.BindableService; | ||
import java.util.Random; | ||
|
||
public class RandomTest extends RandomTestSuite { | ||
|
||
private static class RandomShouldBeDeterministic extends GreeterRestate.GreeterRestateImplBase { | ||
@Override | ||
public GreetingResponse greet(RestateContext context, GreetingRequest request) | ||
throws TerminalException { | ||
return GreetingResponse.newBuilder() | ||
.setMessage(Integer.toString(context.random().nextInt())) | ||
.build(); | ||
} | ||
} | ||
|
||
@Override | ||
protected BindableService randomShouldBeDeterministic() { | ||
return new RandomShouldBeDeterministic(); | ||
} | ||
|
||
private static class RandomInsideSideEffect extends GreeterRestate.GreeterRestateImplBase { | ||
@Override | ||
public GreetingResponse greet(RestateContext context, GreetingRequest request) | ||
throws TerminalException { | ||
context.sideEffect(() -> context.random().nextInt()); | ||
throw new IllegalStateException("This should not unreachable"); | ||
} | ||
} | ||
|
||
@Override | ||
protected BindableService randomInsideSideEffect() { | ||
return new RandomInsideSideEffect(); | ||
} | ||
|
||
@Override | ||
protected int getExpectedInt(long seed) { | ||
return new Random(seed).nextInt(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should this be just
BooleanSupplier
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to use Syscalls here, it makes a bit more clear this code.