Skip to content
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

Merge send methods and rename with in bind #263

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public CounterUpdateResult getAndAdd(ObjectContext ctx, Long request) {
}

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder().with(new Counter()).buildAndListen();
RestateHttpEndpointBuilder.builder().bind(new Counter()).buildAndListen();
}

public static class CounterUpdateResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void register(RestateLambdaEndpointBuilder builder) {
System.getenv("LAMBDA_FACTORY_SERVICE_CLASS"), Counter.class.getCanonicalName())
.split(Pattern.quote(","))) {
if (Counter.class.getCanonicalName().equals(serviceClass)) {
builder.with(new Counter());
builder.bind(new Counter());
} else if (CounterKt.class.getCanonicalName().equals(serviceClass)) {
builder.with(new CounterKt());
builder.bind(new CounterKt());
} else {
throw new IllegalArgumentException(
"Bad \"LAMBDA_FACTORY_SERVICE_CLASS\" env: " + serviceClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public void rejectLoan(WorkflowSharedContext ctx) {

public static void main(String[] args) {
RestateHttpEndpointBuilder.builder()
.with(new LoanWorkflow())
.with(new MockBank())
.bind(new LoanWorkflow())
.bind(new MockBank())
.buildAndListen();

// Register the service in the meantime!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ class CounterKt {
}

fun main() {
RestateHttpEndpointBuilder.builder().with(CounterKt()).buildAndListen()
RestateHttpEndpointBuilder.builder().bind(CounterKt()).buildAndListen()
}
22 changes: 6 additions & 16 deletions sdk-api-gen/src/main/resources/templates/Client.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,28 @@ public class {{generatedClassSimpleName}} {
}{{/handlers}}

public Send send() {
return new Send();
return new Send(null);
}

public SendDelayed sendDelayed(Duration delay) {
return new SendDelayed(delay);
public Send send(Duration delay) {
return new Send(delay);
}

public class Send {
{{#handlers}}
public void {{name}}({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
ContextClient.this.ctx.send(
{{#if isObject}}Target.virtualObject(COMPONENT_NAME, ContextClient.this.key, "{{name}}"){{else}}Target.service(COMPONENT_NAME, "{{name}}"){{/if}},
{{inputSerdeFieldName}},
{{#if inputEmpty}}null{{else}}req{{/if}});
}{{/handlers}}
}

public class SendDelayed {

private final Duration delay;

SendDelayed(Duration delay) {
Send(Duration delay) {
this.delay = delay;
}

{{#handlers}}
public void {{name}}({{^inputEmpty}}{{{inputFqcn}}} req{{/inputEmpty}}) {
ContextClient.this.ctx.sendDelayed(
ContextClient.this.ctx.send(
{{#if isObject}}Target.virtualObject(COMPONENT_NAME, ContextClient.this.key, "{{name}}"){{else}}Target.service(COMPONENT_NAME, "{{name}}"){{/if}},
{{inputSerdeFieldName}},
{{#if inputEmpty}}null{{else}}req{{/if}},
this.delay);
delay);
}{{/handlers}}
}
}
Expand Down
23 changes: 4 additions & 19 deletions sdk-api-kotlin-gen/src/main/resources/templates/Client.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,18 @@ object {{generatedClassSimpleName}} {
{{#if inputEmpty}}Unit{{else}}req{{/if}})
}{{/handlers}}

fun send(): Send {
return Send()
}

fun sendDelayed(delay: Duration): SendDelayed {
return SendDelayed(delay)
fun send(delay: Duration = Duration.ZERO): Send {
return Send(delay)
}

inner class Send {
inner class Send(private val delay: Duration) {
{{#handlers}}
suspend fun {{name}}({{^inputEmpty}}req: {{{inputFqcn}}}{{/inputEmpty}}) {
[email protected](
{{#if isObject}}Target.virtualObject(COMPONENT_NAME, [email protected], "{{name}}"){{else}}Target.service(COMPONENT_NAME, "{{name}}"){{/if}},
{{inputSerdeFieldName}},
{{#if inputEmpty}}Unit{{else}}req{{/if}});
}{{/handlers}}
}

inner class SendDelayed(private val delay: Duration) {

{{#handlers}}
suspend fun {{name}}({{^inputEmpty}}req: {{{inputFqcn}}}{{/inputEmpty}}) {
[email protected](
{{#if isObject}}Target.virtualObject(COMPONENT_NAME, [email protected], "{{name}}"){{else}}Target.service(COMPONENT_NAME, "{{name}}"){{/if}},
{{inputSerdeFieldName}},
{{#if inputEmpty}}Unit{{else}}req{{/if}},
this.delay);
delay);
}{{/handlers}}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,7 @@ internal class ContextImpl internal constructor(private val syscalls: Syscalls)
return SingleSerdeAwaitableImpl(syscalls, deferred, outputSerde)
}

override suspend fun <T : Any> send(target: Target, inputSerde: Serde<T>, parameter: T) {
val input = inputSerde.serializeWrappingException(syscalls, parameter)

return suspendCancellableCoroutine { cont: CancellableContinuation<Unit> ->
syscalls.send(target, input, null, completingUnitContinuation(cont))
}
}

override suspend fun <T : Any> sendDelayed(
override suspend fun <T : Any> send(
target: Target,
inputSerde: Serde<T>,
parameter: T,
Expand Down
16 changes: 2 additions & 14 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 @@ -91,25 +91,13 @@ sealed interface Context {
* @param target the address of the callee
* @param inputSerde Input serde
* @param parameter the invocation request parameter.
*/
suspend fun <T : Any> send(target: Target, inputSerde: Serde<T>, parameter: T)

/**
* Invoke another Restate service without waiting for the response after the provided `delay` has
* elapsed.
*
* This method returns immediately, as the timer is executed and awaited on Restate.
*
* @param target the address of the callee
* @param inputSerde Input serde
* @param parameter the invocation request parameter.
* @param delay time to wait before executing the call
*/
suspend fun <T : Any> sendDelayed(
suspend fun <T : Any> send(
target: Target,
inputSerde: Serde<T>,
parameter: T,
delay: Duration
delay: Duration = Duration.ZERO
)

/**
Expand Down
8 changes: 4 additions & 4 deletions sdk-api/src/main/java/dev/restate/sdk/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ default void send(Target target, byte[] parameter) {
* @param parameter the invocation request parameter.
* @param delay time to wait before executing the call.
*/
<T> void sendDelayed(Target target, Serde<T> inputSerde, T parameter, Duration delay);
<T> void send(Target target, Serde<T> inputSerde, T parameter, Duration delay);

/** Like {@link #sendDelayed(Target, Serde, Object, Duration)} with raw input. */
default void sendDelayed(Target target, byte[] parameter, Duration delay) {
sendDelayed(target, CoreSerdes.RAW, parameter, delay);
/** Like {@link #send(Target, Serde, Object, Duration)} with raw input. */
default void send(Target target, byte[] parameter, Duration delay) {
send(target, CoreSerdes.RAW, parameter, delay);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion sdk-api/src/main/java/dev/restate/sdk/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public <T> void send(Target target, Serde<T> inputSerde, T parameter) {
}

@Override
public <T> void sendDelayed(Target target, Serde<T> inputSerde, T parameter, Duration delay) {
public <T> void send(Target target, Serde<T> inputSerde, T parameter, Duration delay) {
ByteString input = Util.serializeWrappingException(syscalls, inputSerde, parameter);
Util.<Void>blockOnSyscall(cb -> syscalls.send(target, input, delay, cb));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Builder(DeploymentManifestSchema.ProtocolMode protocolMode) {
this.protocolMode = protocolMode;
}

public <O> Builder with(ComponentDefinition<O> component, O options) {
public <O> Builder bind(ComponentDefinition<O> component, O options) {
this.components.add(new ComponentAndOptions<>(component, options));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void send(
if (target.getKey() != null) {
builder.setKey(target.getKey());
}
if (delay != null) {
if (delay != null && !delay.isZero()) {
builder.setInvokeTime(Instant.now().toEpochMilli() + delay.toMillis());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void executeTest(TestDefinitions.TestDefinition definition) {
// Prepare server
RestateEndpoint.Builder builder =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.BIDI_STREAM)
.with(componentDefinition.get(0), bindableComponent.options());
.bind(componentDefinition.get(0), bindableComponent.options());
RestateEndpoint server = builder.build();

// Start invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void executeTest(TestDefinition definition) {
// Prepare server
RestateEndpoint.Builder builder =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.BIDI_STREAM)
.with(componentDefinition.get(0), bindableComponent.options());
.bind(componentDefinition.get(0), bindableComponent.options());
RestateEndpoint server = builder.build();

// Start invocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* <pre>
* public static void main(String[] args) {
* RestateHttpEndpointBuilder.builder()
* .with(new Counter())
* .bind(new Counter())
* .buildAndListen();
* }
* </pre>
Expand Down Expand Up @@ -73,31 +73,31 @@ public RestateHttpEndpointBuilder withOptions(HttpServerOptions options) {
* based on the class name.
*
* <p>You can also manually instantiate the {@link BindableComponent} using {@link
* #with(BindableComponent)}.
* #bind(BindableComponent)}.
*/
public RestateHttpEndpointBuilder with(Object component) {
return this.with(RestateEndpoint.discoverBindableComponentFactory(component).create(component));
public RestateHttpEndpointBuilder bind(Object component) {
return this.bind(RestateEndpoint.discoverBindableComponentFactory(component).create(component));
}

/**
* Add a Restate bindable component to the endpoint.
*
* <p>To override the options, use {@link #with(BindableComponent, Object)}.
* <p>To override the options, use {@link #bind(BindableComponent, Object)}.
*/
public RestateHttpEndpointBuilder with(BindableComponent<?> component) {
public RestateHttpEndpointBuilder bind(BindableComponent<?> component) {
for (ComponentDefinition<?> componentDefinition : component.definitions()) {
//noinspection unchecked
this.endpointBuilder.with(
this.endpointBuilder.bind(
(ComponentDefinition<Object>) componentDefinition, component.options());
}

return this;
}

/** Add a Restate bindable component to the endpoint, overriding the options. */
public <O> RestateHttpEndpointBuilder with(BindableComponent<O> component, O options) {
public <O> RestateHttpEndpointBuilder bind(BindableComponent<O> component, O options) {
for (ComponentDefinition<O> componentDefinition : component.definitions()) {
this.endpointBuilder.with(componentDefinition, options);
this.endpointBuilder.bind(componentDefinition, options);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HttpVertxTestExecutor(private val vertx: Vertx) : TestExecutor {
val server =
RestateHttpEndpointBuilder.builder(vertx)
.withOptions(HttpServerOptions().setPort(0))
.with(definition.component)
.bind(definition.component)
.build()
server.listen().coAwait()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ internal class RestateHttpEndpointTest {
@Timeout(value = 1, timeUnit = TimeUnit.SECONDS)
@Test
fun endpointWithNonBlockingService(vertx: Vertx): Unit =
greetTest(vertx, "KtGreeter") { it.with(greeter()) }
greetTest(vertx, "KtGreeter") { it.bind(greeter()) }

@Timeout(value = 1, timeUnit = TimeUnit.SECONDS)
@Test
fun endpointWithBlockingService(vertx: Vertx): Unit =
greetTest(vertx, BlockingGreeter::class.simpleName!!) { it.with(BlockingGreeter()) }
greetTest(vertx, BlockingGreeter::class.simpleName!!) { it.bind(BlockingGreeter()) }

private fun greetTest(
vertx: Vertx,
Expand Down Expand Up @@ -153,7 +153,7 @@ internal class RestateHttpEndpointTest {
runBlocking(vertx.dispatcher()) {
val endpointPort: Int =
RestateHttpEndpointBuilder.builder(vertx)
.with(BlockingGreeter())
.bind(BlockingGreeter())
.withOptions(HttpServerOptions().setPort(0))
.build()
.listen()
Expand Down Expand Up @@ -188,7 +188,7 @@ internal class RestateHttpEndpointTest {
runBlocking(vertx.dispatcher()) {
val endpointPort: Int =
RestateHttpEndpointBuilder.builder(vertx)
.with(BlockingGreeter())
.bind(BlockingGreeter())
.withOptions(HttpServerOptions().setPort(0))
.build()
.listen()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public final class RestateLambdaEndpointBuilder {
* Add a Restate entity to the endpoint, specifying the {@code executor} where to run the entity
* code.
*/
public RestateLambdaEndpointBuilder with(Object service) {
return this.with(RestateEndpoint.discoverBindableComponentFactory(service).create(service));
public RestateLambdaEndpointBuilder bind(Object service) {
return this.bind(RestateEndpoint.discoverBindableComponentFactory(service).create(service));
}

/** Add a Restate bindable component to the endpoint. */
public RestateLambdaEndpointBuilder with(BindableComponent<?> component) {
public RestateLambdaEndpointBuilder bind(BindableComponent<?> component) {
for (ComponentDefinition<?> componentDefinition : component.definitions()) {
//noinspection unchecked
this.restateEndpoint.with(
this.restateEndpoint.bind(
(ComponentDefinition<Object>) componentDefinition, component.options());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
public class MyServicesHandler extends BaseRestateLambdaHandler {
@Override
public void register(RestateLambdaEndpointBuilder builder) {
builder.with(new JavaCounterService()).with(KotlinCounterServiceKt.counter());
builder.bind(new JavaCounterService()).bind(KotlinCounterServiceKt.counter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public RestateRunnerBuilder withConfigFile(String configFile) {
* #with(BindableComponent)}.
*/
public RestateRunnerBuilder with(Object component) {
endpointBuilder.with(component);
endpointBuilder.bind(component);
return this;
}

Expand All @@ -62,13 +62,13 @@ public RestateRunnerBuilder with(Object component) {
* <p>To override the options, use {@link #with(BindableComponent, Object)}.
*/
public RestateRunnerBuilder with(BindableComponent<?> component) {
endpointBuilder.with(component);
endpointBuilder.bind(component);
return this;
}

/** Add a Restate bindable component to the endpoint, overriding the options. */
public <O> RestateRunnerBuilder with(BindableComponent<O> component, O options) {
endpointBuilder.with(component, options);
endpointBuilder.bind(component, options);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void invokeSharedSendDelayed(
String workflowKey,
@Nullable Object payload,
Duration delay) {
ctx.sendDelayed(
ctx.send(
Target.service(workflowName, handlerName),
WorkflowImpl.INVOKE_REQUEST_SERDE,
InvokeRequest.fromAny(workflowKey, payload),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ public <T> void send(Target target, Serde<T> inputSerde, T parameter) {
}

@Override
public <T> void sendDelayed(Target target, Serde<T> inputSerde, T parameter, Duration delay) {
ctx.sendDelayed(target, inputSerde, parameter, delay);
public <T> void send(Target target, Serde<T> inputSerde, T parameter, Duration delay) {
ctx.send(target, inputSerde, parameter, delay);
}

@Override
Expand Down
Loading