Skip to content

Commit

Permalink
Rename UnkeyedContext to Context (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Feb 13, 2024
1 parent 5e4c94a commit 65e7729
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 70 deletions.
4 changes: 2 additions & 2 deletions examples/src/main/java/my/restate/sdk/examples/Loan.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.restate.sdk.UnkeyedContext;
import dev.restate.sdk.Context;
import dev.restate.sdk.annotation.Service;
import dev.restate.sdk.annotation.ServiceType;
import dev.restate.sdk.annotation.Shared;
Expand Down Expand Up @@ -221,7 +221,7 @@ private static void askHumanApproval(String workflowKey) throws InterruptedExcep

private static class MockBank extends BankRestate.BankRestateImplBase {
@Override
public TransferResult transfer(UnkeyedContext context, TransferRequest request)
public TransferResult transfer(Context context, TransferRequest request)
throws TerminalException {
boolean shouldAccept = context.random().nextInt(3) != 1;
if (shouldAccept) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private ServiceContext buildServiceContext(
// Resolve context type
serviceContext.contextType =
serviceProto.getOptions().getExtension(Ext.serviceType) == ServiceType.UNKEYED
? "UnkeyedContext"
? "Context"
: "KeyedContext";

// Resolve javadoc
Expand Down
16 changes: 8 additions & 8 deletions protoc-gen-restate/src/main/resources/javaStub.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package {{packageName}};
{{/packageName}}

import dev.restate.sdk.UnkeyedContext;
import dev.restate.sdk.Context;
import dev.restate.sdk.KeyedContext;
import dev.restate.sdk.Awaitable;
import dev.restate.sdk.common.syscalls.Syscalls;
Expand All @@ -18,15 +18,15 @@ public class {{className}} {
/**
* Create a new client from the given {@link KeyedContext}.
*/
public static {{serviceName}}RestateClient newClient(UnkeyedContext ctx) {
public static {{serviceName}}RestateClient newClient(Context ctx) {
return new {{serviceName}}RestateClient(ctx);
}

{{{apidoc}}}
public static final class {{serviceName}}RestateClient {
private final UnkeyedContext ctx;
private final Context ctx;
{{serviceName}}RestateClient(UnkeyedContext ctx) {
{{serviceName}}RestateClient(Context ctx) {
this.ctx = ctx;
}

Expand All @@ -51,9 +51,9 @@ public class {{className}} {
}

public static final class {{serviceName}}RestateOneWayClient {
private final UnkeyedContext ctx;
private final Context ctx;
{{serviceName}}RestateOneWayClient(UnkeyedContext ctx) {
{{serviceName}}RestateOneWayClient(Context ctx) {
this.ctx = ctx;
}

Expand All @@ -68,10 +68,10 @@ public class {{className}} {
}

public static final class {{serviceName}}RestateDelayedClient {
private final UnkeyedContext ctx;
private final Context ctx;
private final Duration delay;
{{serviceName}}RestateDelayedClient(UnkeyedContext ctx, Duration delay) {
{{serviceName}}RestateDelayedClient(Context ctx, Duration delay) {
this.ctx = ctx;
this.delay = delay;
}
Expand Down
12 changes: 6 additions & 6 deletions protoc-gen-restate/src/main/resources/ktStub.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package {{packageName}};
{{/packageName}}

import dev.restate.sdk.kotlin.UnkeyedContext;
import dev.restate.sdk.kotlin.Context;
import dev.restate.sdk.kotlin.KeyedContext;
import dev.restate.sdk.kotlin.Awaitable;
import dev.restate.sdk.kotlin.RestateKtService;
Expand All @@ -18,14 +18,14 @@ import kotlin.time.Duration
public object {{className}} {
/**
* Create a new client from the given [UnkeyedContext].
* Create a new client from the given [Context].
*/
fun newClient(ctx: UnkeyedContext): {{serviceName}}RestateKtClient {
fun newClient(ctx: Context): {{serviceName}}RestateKtClient {
return {{serviceName}}RestateKtClient(ctx);
}

{{{javadoc}}}
public class {{serviceName}}RestateKtClient(private val ctx: UnkeyedContext) {
public class {{serviceName}}RestateKtClient(private val ctx: Context) {
// Create a variant of this client to execute oneWay calls.
public fun oneWay(): {{serviceName}}RestateKtOneWayClient {
return {{serviceName}}RestateKtOneWayClient(ctx);
Expand All @@ -46,7 +46,7 @@ public object {{className}} {
{{/methods}}
}

public class {{serviceName}}RestateKtOneWayClient(private val ctx: UnkeyedContext) {
public class {{serviceName}}RestateKtOneWayClient(private val ctx: Context) {
{{#methods}}
{{#deprecated}}@Deprecated{{/deprecated}}
{{{javadoc}}}
Expand All @@ -57,7 +57,7 @@ public object {{className}} {
{{/methods}}
}

public class {{serviceName}}RestateKtDelayedClient(private val ctx: UnkeyedContext, private val delay: Duration) {
public class {{serviceName}}RestateKtDelayedClient(private val ctx: Context, private val delay: Duration) {
{{#methods}}
{{#deprecated}}@Deprecated{{/deprecated}}
{{{javadoc}}}
Expand Down
22 changes: 11 additions & 11 deletions sdk-api-gen/src/main/resources/templates/RestateClient.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.restate.sdk.common.StateKey;
import dev.restate.sdk.common.Serde;
import dev.restate.sdk.Awaitable;
import dev.restate.sdk.UnkeyedContext;
import dev.restate.sdk.Context;
import dev.restate.sdk.workflow.impl.WorkflowCodegenUtil;
import java.util.Optional;
import java.time.Duration;
Expand All @@ -21,36 +21,36 @@ public class {{className}}RestateClient {
{{^outputEmpty}}private static final Serde<{{{outputFqcn}}}> {{outputSerdeFieldName}} = {{{outputSerdeDecl}}};{{/outputEmpty}}
{{/methods}}

private final UnkeyedContext restateContext;
private final Context ctx;
private final String workflowKey;

public {{className}}RestateClient(UnkeyedContext restateContext, String workflowKey) {
this.restateContext = restateContext;
public {{className}}RestateClient(Context ctx, String workflowKey) {
this.ctx = ctx;
this.workflowKey = workflowKey;
}

{{#methods}}{{#if isWorkflow}}
public Awaitable<dev.restate.sdk.workflow.generated.WorkflowExecutionState> submit({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
return WorkflowCodegenUtil.RestateClient.submit(restateContext, WF_SUBMIT_METHOD_DESC, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
return WorkflowCodegenUtil.RestateClient.submit(ctx, WF_SUBMIT_METHOD_DESC, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
}

public Awaitable<Boolean> isCompleted() {
return WorkflowCodegenUtil.RestateClient.isCompleted(restateContext, WF_MANAGER_GET_OUTPUT_METHOD_DESC, workflowKey);
return WorkflowCodegenUtil.RestateClient.isCompleted(ctx, WF_MANAGER_GET_OUTPUT_METHOD_DESC, workflowKey);
}

{{^outputEmpty}}
public Awaitable<Optional<{{{outputFqcn}}}>> getOutput() {
return WorkflowCodegenUtil.RestateClient.getOutput(restateContext, WF_MANAGER_GET_OUTPUT_METHOD_DESC, workflowKey, {{outputSerdeFieldName}});
return WorkflowCodegenUtil.RestateClient.getOutput(ctx, WF_MANAGER_GET_OUTPUT_METHOD_DESC, workflowKey, {{outputSerdeFieldName}});
}{{/outputEmpty}}
{{/if}}{{/methods}}

public <T> Awaitable<Optional<T>> getState(StateKey<T> key) {
return WorkflowCodegenUtil.RestateClient.getState(restateContext, WF_MANAGER_GET_STATE_METHOD_DESC, workflowKey, key);
return WorkflowCodegenUtil.RestateClient.getState(ctx, WF_MANAGER_GET_STATE_METHOD_DESC, workflowKey, key);
}

{{#methods}}{{#if isShared}}
public {{#if outputEmpty}}Awaitable<Void>{{else}}Awaitable<{{{outputFqcn}}}>{{/if}} {{name}}({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
Awaitable<com.google.protobuf.Value> response = WorkflowCodegenUtil.RestateClient.invokeShared(restateContext, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
Awaitable<com.google.protobuf.Value> response = WorkflowCodegenUtil.RestateClient.invokeShared(ctx, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
{{#if outputEmpty}}
return response.map(v -> { return null; });
{{else}}
Expand All @@ -71,7 +71,7 @@ public class {{className}}RestateClient {

{{#methods}}{{#if isShared}}
public void {{name}}({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
WorkflowCodegenUtil.RestateClient.invokeSharedOneWay(restateContext, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
WorkflowCodegenUtil.RestateClient.invokeSharedOneWay(ctx, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}});
}
{{/if}}{{/methods}}

Expand All @@ -87,7 +87,7 @@ public class {{className}}RestateClient {

{{#methods}}{{#if isShared}}
public void {{name}}({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
WorkflowCodegenUtil.RestateClient.invokeSharedDelayed(restateContext, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}}, delay);
WorkflowCodegenUtil.RestateClient.invokeSharedDelayed(ctx, {{descFieldName}}, workflowKey, {{#if inputEmpty}}null{{else}}WorkflowCodegenUtil.tToValue({{inputSerdeFieldName}}, req){{/if}}, delay);
}
{{/if}}{{/methods}}

Expand Down
13 changes: 6 additions & 7 deletions sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import kotlin.time.Duration
* NOTE: This interface MUST NOT be accessed concurrently since it can lead to different orderings
* of user actions, corrupting the execution of the invocation.
*/
sealed interface UnkeyedContext {
sealed interface Context {

/**
* Causes the current execution of the function invocation to sleep for the given duration.
Expand Down Expand Up @@ -191,26 +191,25 @@ sealed interface UnkeyedContext {
companion object {

/**
* Create a [UnkeyedContext]. This will look up the thread-local/async-context storage for the
* Create a [Context]. 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.
*/
fun current(): UnkeyedContext {
fun current(): Context {
return fromSyscalls(Syscalls.current())
}

/** Build a context from the underlying [Syscalls] object. */
fun fromSyscalls(syscalls: Syscalls): UnkeyedContext {
fun fromSyscalls(syscalls: Syscalls): Context {
return ContextImpl(syscalls)
}
}
}

/**
* This interface extends [UnkeyedContext] adding access to the service instance key-value state
* storage.
* This interface extends [Context] adding access to the service instance key-value state storage.
*/
sealed interface KeyedContext : UnkeyedContext {
sealed interface KeyedContext : Context {

/**
* Gets the state stored under key, deserializing the raw value using the registered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ class RestateCodegenTest : RestateCodegenTestSuite() {
}

private class Codegen : CodegenRestateKtImplBase() {
override suspend fun emptyInput(context: UnkeyedContext): MyMessage {
override suspend fun emptyInput(context: Context): MyMessage {
val client = CodegenRestateKt.newClient(context)
return client.emptyInput().await()
}

override suspend fun emptyOutput(context: UnkeyedContext, request: MyMessage) {
override suspend fun emptyOutput(context: Context, request: MyMessage) {
val client = CodegenRestateKt.newClient(context)
client.emptyOutput(request).await()
}

override suspend fun emptyInputOutput(context: UnkeyedContext) {
override suspend fun emptyInputOutput(context: Context) {
val client = CodegenRestateKt.newClient(context)
client.emptyInputOutput().await()
}

override suspend fun oneWay(context: UnkeyedContext, request: MyMessage): MyMessage {
override suspend fun oneWay(context: Context, request: MyMessage): MyMessage {
val client = CodegenRestateKt.newClient(context)
return client._oneWay(request).await()
}

override suspend fun delayed(context: UnkeyedContext, request: MyMessage): MyMessage {
override suspend fun delayed(context: Context, request: MyMessage): MyMessage {
val client = CodegenRestateKt.newClient(context)
return client._delayed(request).await()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* orderings of user actions, corrupting the execution of the invocation.
*/
@NotThreadSafe
public interface UnkeyedContext {
public interface Context {

/**
* Invoke another Restate service method.
Expand Down Expand Up @@ -185,12 +185,12 @@ default void sideEffect(ThrowingRunnable runnable) throws TerminalException {
* underlying context implementation, so make sure to call it always from the same context where
* the service is executed.
*/
static UnkeyedContext current() {
static Context current() {
return fromSyscalls(Syscalls.current());
}

/** Build a RestateContext from the underlying {@link Syscalls} object. */
static UnkeyedContext fromSyscalls(Syscalls syscalls) {
static Context fromSyscalls(Syscalls syscalls) {
return new ContextImpl(syscalls);
}
}
4 changes: 2 additions & 2 deletions sdk-api/src/main/java/dev/restate/sdk/GrpcChannelAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* <p>Keep in mind that this channel should be used only with generated blocking stubs.
*/
public class GrpcChannelAdapter extends Channel {
private final UnkeyedContext ctx;
private final Context ctx;

GrpcChannelAdapter(UnkeyedContext ctx) {
GrpcChannelAdapter(Context ctx) {
this.ctx = ctx;
}

Expand Down
8 changes: 4 additions & 4 deletions sdk-api/src/main/java/dev/restate/sdk/KeyedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import javax.annotation.concurrent.NotThreadSafe;

/**
* This interface extends {@link UnkeyedContext} adding access to the service instance key-value
* state storage
* This interface extends {@link Context} adding access to the service instance key-value state
* storage
*
* @see UnkeyedContext
* @see Context
*/
@NotThreadSafe
public interface KeyedContext extends UnkeyedContext {
public interface KeyedContext extends Context {

/**
* Gets the state stored under key, deserializing the raw value using the {@link Serde} in the
Expand Down
10 changes: 5 additions & 5 deletions sdk-api/src/test/java/dev/restate/sdk/RestateCodegenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ protected BindableService greeterWithRestateClientAndServerCodegen() {
private static class Codegen extends CodegenRestate.CodegenRestateImplBase {

@Override
public MyMessage emptyInput(UnkeyedContext context) {
public MyMessage emptyInput(Context context) {
CodegenRestate.CodegenRestateClient client = CodegenRestate.newClient(context);
return client.emptyInput().await();
}

@Override
public void emptyOutput(UnkeyedContext context, MyMessage request) {
public void emptyOutput(Context context, MyMessage request) {
CodegenRestate.CodegenRestateClient client = CodegenRestate.newClient(context);
client.emptyOutput(request).await();
}

@Override
public void emptyInputOutput(UnkeyedContext context) {
public void emptyInputOutput(Context context) {
CodegenRestate.CodegenRestateClient client = CodegenRestate.newClient(context);
client.emptyInputOutput().await();
}

@Override
public MyMessage oneWay(UnkeyedContext context, MyMessage request) {
public MyMessage oneWay(Context context, MyMessage request) {
CodegenRestate.CodegenRestateClient client = CodegenRestate.newClient(context);
return client._oneWay(request).await();
}

@Override
public MyMessage delayed(UnkeyedContext context, MyMessage request) {
public MyMessage delayed(Context context, MyMessage request) {
CodegenRestate.CodegenRestateClient client = CodegenRestate.newClient(context);
return client._delayed(request).await();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.workflow;

import dev.restate.sdk.UnkeyedContext;
import dev.restate.sdk.Context;
import dev.restate.sdk.common.StateKey;
import java.util.Optional;

public interface WorkflowSharedContext extends UnkeyedContext {
public interface WorkflowSharedContext extends Context {

String workflowKey();

Expand Down
Loading

0 comments on commit 65e7729

Please sign in to comment.