From 91d7438e498f963cb1ceb8f39ad639c7c7b76c22 Mon Sep 17 00:00:00 2001 From: Stephan Ewen Date: Mon, 23 Nov 2026 13:54:18 +0100 Subject: [PATCH] Let services and handlers declare themselves non-preemptable Adds a `preemptable` option on services and handlers (default true, the handler setting overrides the service one), sent in the endpoint manifest under service discovery protocol V6. When false, the runtime never eagerly suspends invocations of the handler to free concurrency capacity; they still suspend on the inactivity timeout. For runtimes that negotiate an older discovery version the field is dropped instead of failing discovery, since those runtimes never preempt invocations. --- .../restate/sdk/kotlin/endpoint/endpoint.kt | 50 +++++++++++ .../definition/HandlerDefinition.java | 55 ++++++++++++ .../definition/ServiceDefinition.java | 54 ++++++++++++ .../restate/sdk/core/DiscoveryProtocol.java | 53 ++++++------ .../restate/sdk/core/EndpointManifest.java | 6 +- .../dev/restate/service/discovery.proto | 4 + .../endpoint_manifest_schema.json | 8 ++ .../core/ComponentDiscoveryHandlerTest.java | 86 +++++++++++++++++++ .../RestateComponentProperties.java | 72 ++++++++++++++++ .../RestateComponentsProperties.java | 28 ++++++ .../RestateEndpointConfiguration.java | 9 ++ .../springboot/RestateHandlerProperties.java | 68 +++++++++++++++ 12 files changed, 466 insertions(+), 27 deletions(-) diff --git a/sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/endpoint/endpoint.kt b/sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/endpoint/endpoint.kt index f552747b0..3f42bd124 100644 --- a/sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/endpoint/endpoint.kt +++ b/sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/endpoint/endpoint.kt @@ -165,6 +165,31 @@ var ServiceDefinition.Configurator.ingressPrivate: Boolean? this.ingressPrivate(value) } +/** + * When set to `false`, Restate never preempts invocations to this service, with all its handlers. + * Preemption means that when the restate-server runs out of concurrency capacity, it can eagerly + * suspend an invocation that is waiting on an await point (e.g. a timer, a call response, an + * awakeable), to free up capacity for other invocations. Non-preemptable invocations still suspend + * when the [inactivityTimeout] fires. + * + * Use this when suspending and later replaying invocations of this service is undesirable, e.g. + * because resuming them is expensive. + * + * If unset, the restate-server treats the service as preemptable (`true`). A handler can override + * this value with [HandlerDefinition.Configurator.preemptable]. + * + * *NOTE:* This setting requires a restate-server supporting the service discovery protocol V6. When + * registering against an older restate-server, this setting is ignored, as older restate-server + * versions never preempt invocations. + */ +var ServiceDefinition.Configurator.preemptable: Boolean? + get() { + return this.preemptable() + } + set(value) { + this.preemptable(value) + } + /** * Retry policy used by Restate when invoking this service. * @@ -315,6 +340,31 @@ var HandlerDefinition.Configurator.ingressPrivate: Boolean? this.ingressPrivate(value) } +/** + * When set to `false`, Restate never preempts invocations to this handler. Preemption means that + * when the restate-server runs out of concurrency capacity, it can eagerly suspend an invocation + * that is waiting on an await point (e.g. a timer, a call response, an awakeable), to free up + * capacity for other invocations. Non-preemptable invocations still suspend when the + * [inactivityTimeout] fires. + * + * Use this when suspending and later replaying invocations of this handler is undesirable, e.g. + * because resuming them is expensive. + * + * This overrides the value set for the service. If unset both here and on the service, the + * restate-server treats the handler as preemptable (`true`). + * + * *NOTE:* This setting requires a restate-server supporting the service discovery protocol V6. When + * registering against an older restate-server, this setting is ignored, as older restate-server + * versions never preempt invocations. + */ +var HandlerDefinition.Configurator.preemptable: Boolean? + get() { + return this.preemptable() + } + set(value) { + this.preemptable(value) + } + /** * When set to `true`, lazy state will be enabled for all invocations to this handler. This is * relevant only for workflows and virtual objects. diff --git a/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/HandlerDefinition.java b/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/HandlerDefinition.java index 04d3669f0..ee8dfb4d4 100644 --- a/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/HandlerDefinition.java +++ b/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/HandlerDefinition.java @@ -34,6 +34,7 @@ public final class HandlerDefinition { private final @Nullable Duration workflowRetention; private final @Nullable Duration journalRetention; private final @Nullable Boolean ingressPrivate; + private final @Nullable Boolean preemptable; private final @Nullable Boolean enableLazyState; private final @Nullable InvocationRetryPolicy invocationRetryPolicy; @@ -52,6 +53,7 @@ public final class HandlerDefinition { @Nullable Duration workflowRetention, @Nullable Duration journalRetention, @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, @Nullable Boolean enableLazyState, @Nullable InvocationRetryPolicy invocationRetryPolicy) { this.name = name; @@ -68,6 +70,7 @@ public final class HandlerDefinition { this.workflowRetention = workflowRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.invocationRetryPolicy = invocationRetryPolicy; } @@ -176,6 +179,15 @@ public HandlerRunner getRunner() { return ingressPrivate; } + /** + * @return false if the restate-server must never preempt (eagerly suspend) invocations to this + * handler. Null if unset, meaning the service-level setting applies. + * @see Configurator#preemptable(Boolean) + */ + public @Nullable Boolean getPreemptable() { + return preemptable; + } + /** * @return true if this handler will use lazy state. * @see Configurator#enableLazyState(Boolean) @@ -208,6 +220,7 @@ public HandlerDefinition withAcceptContentType(String acceptContentTyp workflowRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -228,6 +241,7 @@ public HandlerDefinition withDocumentation(@Nullable String documentat workflowRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -248,6 +262,7 @@ public HandlerDefinition withMetadata(Map metadata) { workflowRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -269,6 +284,7 @@ public HandlerDefinition configure( workflowRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); configurator.accept(configuratorObj); @@ -288,6 +304,7 @@ public HandlerDefinition configure( configuratorObj.workflowRetention, configuratorObj.journalRetention, configuratorObj.ingressPrivate, + configuratorObj.preemptable, configuratorObj.enableLazyState, configuratorObj.invocationRetryPolicy); } @@ -305,6 +322,7 @@ public static final class Configurator { private @Nullable Duration workflowRetention; private @Nullable Duration journalRetention; private @Nullable Boolean ingressPrivate; + private @Nullable Boolean preemptable; private @Nullable Boolean enableLazyState; private @Nullable InvocationRetryPolicy invocationRetryPolicy; @@ -319,6 +337,7 @@ private Configurator( @Nullable Duration workflowRetention, @Nullable Duration journalRetention, @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, @Nullable Boolean enableLazyState, @Nullable InvocationRetryPolicy invocationRetryPolicy) { this.handlerType = handlerType; @@ -331,6 +350,7 @@ private Configurator( this.workflowRetention = workflowRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.invocationRetryPolicy = invocationRetryPolicy; } @@ -553,6 +573,38 @@ public Configurator ingressPrivate(@Nullable Boolean ingressPrivate) { return this; } + /** + * @return configured preemptable. + * @see #preemptable(Boolean) + */ + public @Nullable Boolean preemptable() { + return preemptable; + } + + /** + * When set to {@code false}, Restate never preempts invocations to this handler. Preemption + * means that when the restate-server runs out of concurrency capacity, it can eagerly suspend + * an invocation that is waiting on an await point (e.g. a timer, a call response, an + * awakeable), to free up capacity for other invocations. Non-preemptable invocations still + * suspend when the {@link #inactivityTimeout(Duration)} fires. + * + *

Use this when suspending and later replaying invocations of this handler is undesirable, + * e.g. because resuming them is expensive. + * + *

This overrides the value set for the service. If unset both here and on the service, the + * restate-server treats the handler as preemptable ({@code true}). + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + * + * @return this + */ + public Configurator preemptable(@Nullable Boolean preemptable) { + this.preemptable = preemptable; + return this; + } + /** * @return configured enable lazy state. * @see #enableLazyState(Boolean) @@ -629,6 +681,7 @@ public static HandlerDefinition of( null, null, null, + null, null); } @@ -649,6 +702,7 @@ && getHandlerType() == that.getHandlerType() && Objects.equals(getWorkflowRetention(), that.getWorkflowRetention()) && Objects.equals(getJournalRetention(), that.getJournalRetention()) && Objects.equals(getIngressPrivate(), that.getIngressPrivate()) + && Objects.equals(getPreemptable(), that.getPreemptable()) && Objects.equals(getEnableLazyState(), that.getEnableLazyState()) && Objects.equals(getInvocationRetryPolicy(), that.getInvocationRetryPolicy()); } @@ -670,6 +724,7 @@ public int hashCode() { getWorkflowRetention(), getJournalRetention(), getIngressPrivate(), + getPreemptable(), getEnableLazyState(), getInvocationRetryPolicy()); } diff --git a/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/ServiceDefinition.java b/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/ServiceDefinition.java index 7eb3d35f4..e3f8fcf7c 100644 --- a/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/ServiceDefinition.java +++ b/sdk-common/src/main/java/dev/restate/sdk/endpoint/definition/ServiceDefinition.java @@ -28,6 +28,7 @@ public final class ServiceDefinition { private final @Nullable Duration idempotencyRetention; private final @Nullable Duration journalRetention; private final @Nullable Boolean ingressPrivate; + private final @Nullable Boolean preemptable; private final @Nullable Boolean enableLazyState; private final @Nullable InvocationRetryPolicy invocationRetryPolicy; @@ -42,6 +43,7 @@ private ServiceDefinition( @Nullable Duration idempotencyRetention, @Nullable Duration journalRetention, @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, @Nullable Boolean enableLazyState, @Nullable InvocationRetryPolicy invocationRetryPolicy) { this.serviceName = serviceName; @@ -54,6 +56,7 @@ private ServiceDefinition( this.idempotencyRetention = idempotencyRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.invocationRetryPolicy = invocationRetryPolicy; } @@ -142,6 +145,15 @@ public Map getMetadata() { return ingressPrivate; } + /** + * @return false if the restate-server must never preempt (eagerly suspend) invocations to this + * service, with all its handlers. Null if unset, meaning preemptable. + * @see Configurator#preemptable(Boolean) + */ + public @Nullable Boolean getPreemptable() { + return preemptable; + } + /** * @return true if the service, with all its handlers, will use lazy state. * @see Configurator#enableLazyState(Boolean) @@ -170,6 +182,7 @@ public ServiceDefinition withDocumentation(@Nullable String documentation) { idempotencyRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -186,6 +199,7 @@ public ServiceDefinition withMetadata(Map metadata) { idempotencyRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -204,6 +218,7 @@ public ServiceDefinition configure(Consumer configurator) { idempotencyRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); configurator.accept(configuratorObj); @@ -218,6 +233,7 @@ public ServiceDefinition configure(Consumer configurator) { configuratorObj.idempotencyRetention, configuratorObj.journalRetention, configuratorObj.ingressPrivate, + configuratorObj.preemptable, configuratorObj.enableLazyState, configuratorObj.invocationRetryPolicy); } @@ -233,6 +249,7 @@ public static final class Configurator { private @Nullable Duration idempotencyRetention; private @Nullable Duration journalRetention; private @Nullable Boolean ingressPrivate; + private @Nullable Boolean preemptable; private @Nullable Boolean enableLazyState; private @Nullable InvocationRetryPolicy invocationRetryPolicy; @@ -245,6 +262,7 @@ private Configurator( @Nullable Duration idempotencyRetention, @Nullable Duration journalRetention, @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, @Nullable Boolean enableLazyState, @Nullable InvocationRetryPolicy invocationRetryPolicy) { this.handlers = new HashMap<>(handlers); @@ -255,6 +273,7 @@ private Configurator( this.idempotencyRetention = idempotencyRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.invocationRetryPolicy = invocationRetryPolicy; } @@ -467,6 +486,38 @@ public Configurator ingressPrivate(@Nullable Boolean ingressPrivate) { return this; } + /** + * @return configured preemptable. + * @see #preemptable(Boolean) + */ + public @Nullable Boolean preemptable() { + return preemptable; + } + + /** + * When set to {@code false}, Restate never preempts invocations to this service, with all its + * handlers. Preemption means that when the restate-server runs out of concurrency capacity, it + * can eagerly suspend an invocation that is waiting on an await point (e.g. a timer, a call + * response, an awakeable), to free up capacity for other invocations. Non-preemptable + * invocations still suspend when the {@link #inactivityTimeout(Duration)} fires. + * + *

Use this when suspending and later replaying invocations of this service is undesirable, + * e.g. because resuming them is expensive. + * + *

If unset, the restate-server treats the service as preemptable ({@code true}). A handler + * can override this value with {@link HandlerDefinition.Configurator#preemptable(Boolean)}. + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + * + * @return this + */ + public Configurator preemptable(@Nullable Boolean preemptable) { + this.preemptable = preemptable; + return this; + } + /** * @return configured enable lazy state. * @see #enableLazyState(Boolean) @@ -548,6 +599,7 @@ && getServiceType() == that.getServiceType() && Objects.equals(idempotencyRetention, that.idempotencyRetention) && Objects.equals(journalRetention, that.journalRetention) && Objects.equals(ingressPrivate, that.ingressPrivate) + && Objects.equals(preemptable, that.preemptable) && Objects.equals(enableLazyState, that.enableLazyState) && Objects.equals(invocationRetryPolicy, that.invocationRetryPolicy); } @@ -565,6 +617,7 @@ public int hashCode() { idempotencyRetention, journalRetention, ingressPrivate, + preemptable, enableLazyState, invocationRetryPolicy); } @@ -584,6 +637,7 @@ public static ServiceDefinition of( null, null, null, + null, null); } } diff --git a/sdk-core/src/main/java/dev/restate/sdk/core/DiscoveryProtocol.java b/sdk-core/src/main/java/dev/restate/sdk/core/DiscoveryProtocol.java index 67172ecde..e9a6effa1 100644 --- a/sdk-core/src/main/java/dev/restate/sdk/core/DiscoveryProtocol.java +++ b/sdk-core/src/main/java/dev/restate/sdk/core/DiscoveryProtocol.java @@ -16,10 +16,10 @@ import dev.restate.sdk.core.generated.manifest.EndpointManifestSchema; import dev.restate.sdk.core.generated.manifest.Handler; import dev.restate.sdk.core.generated.manifest.Service; +import java.util.HashSet; import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; import java.util.stream.Stream; public class DiscoveryProtocol { @@ -27,7 +27,11 @@ public enum Version { V1("application/vnd.restate.endpointmanifest.v1+json"), V2("application/vnd.restate.endpointmanifest.v2+json"), V3("application/vnd.restate.endpointmanifest.v3+json"), - V4("application/vnd.restate.endpointmanifest.v4+json"); + V4("application/vnd.restate.endpointmanifest.v4+json"), + // V5 adds eagerStateKeysWhitelist, which this SDK doesn't implement. It's not supported, so + // runtimes offering at most V5 keep negotiating V4. Declared because getNumber() is ordinal. + V5("application/vnd.restate.endpointmanifest.v5+json"), + V6("application/vnd.restate.endpointmanifest.v6+json"); private final String header; @@ -44,12 +48,11 @@ public int getNumber() { } public boolean isSupported() { - // We support all versions so far - return true; + return this != V5; } public static final Version MIN = Version.V1; - public static final Version MAX = Version.V4; + public static final Version MAX = Version.V6; public static Optional fromHeader(String headerValue) { String trimmed = headerValue.trim(); @@ -117,6 +120,8 @@ static Version selectSupportedServiceDiscoveryProtocolVersion(String acceptedVer "retryPolicyMaxAttempts", "retryPolicyExponentiationFactor", "retryPolicyOnMaxAttempts"); + // Runtimes older than V6 never preempt invocations, so dropping this field for them is safe. + static final Set DISCOVERY_FIELDS_ADDED_IN_V6 = Set.of("preemptable"); @JsonFilter("DiscoveryFieldsFilter") interface FieldsMixin {} @@ -131,28 +136,24 @@ static byte[] serializeManifest( Version serviceDiscoveryProtocolVersion, EndpointManifestSchema response) throws ProtocolException { try { - SimpleBeanPropertyFilter filter; - if (serviceDiscoveryProtocolVersion == Version.V1) { - filter = - SimpleBeanPropertyFilter.serializeAllExcept( - Stream.concat( - Stream.concat( - DISCOVERY_FIELDS_ADDED_IN_V2.stream(), - DISCOVERY_FIELDS_ADDED_IN_V3.stream()), - DISCOVERY_FIELDS_ADDED_IN_V4.stream()) - .collect(Collectors.toSet())); - } else if (serviceDiscoveryProtocolVersion == Version.V2) { - filter = - SimpleBeanPropertyFilter.serializeAllExcept( - Stream.concat( - DISCOVERY_FIELDS_ADDED_IN_V3.stream(), - DISCOVERY_FIELDS_ADDED_IN_V4.stream()) - .collect(Collectors.toSet())); - } else if (serviceDiscoveryProtocolVersion == Version.V3) { - filter = SimpleBeanPropertyFilter.serializeAllExcept(DISCOVERY_FIELDS_ADDED_IN_V4); - } else { - filter = SimpleBeanPropertyFilter.serializeAll(); + int version = serviceDiscoveryProtocolVersion.getNumber(); + Set excludedFields = new HashSet<>(); + if (version < Version.V2.getNumber()) { + excludedFields.addAll(DISCOVERY_FIELDS_ADDED_IN_V2); } + if (version < Version.V3.getNumber()) { + excludedFields.addAll(DISCOVERY_FIELDS_ADDED_IN_V3); + } + if (version < Version.V4.getNumber()) { + excludedFields.addAll(DISCOVERY_FIELDS_ADDED_IN_V4); + } + if (version < Version.V6.getNumber()) { + excludedFields.addAll(DISCOVERY_FIELDS_ADDED_IN_V6); + } + SimpleBeanPropertyFilter filter = + excludedFields.isEmpty() + ? SimpleBeanPropertyFilter.serializeAll() + : SimpleBeanPropertyFilter.serializeAllExcept(excludedFields); return MANIFEST_OBJECT_MAPPER .writer(new SimpleFilterProvider().addFilter("DiscoveryFieldsFilter", filter)) diff --git a/sdk-core/src/main/java/dev/restate/sdk/core/EndpointManifest.java b/sdk-core/src/main/java/dev/restate/sdk/core/EndpointManifest.java index 9017850bc..f8237869f 100644 --- a/sdk-core/src/main/java/dev/restate/sdk/core/EndpointManifest.java +++ b/sdk-core/src/main/java/dev/restate/sdk/core/EndpointManifest.java @@ -40,7 +40,9 @@ EndpointManifestSchema manifest( .withMinProtocolVersion(5L) .withMaxProtocolVersion(StateMachineFactory.maxSupportedProtocolVersion()) .withServices(this.services); - // Verify that the user didn't set fields that we don't support in the discovery version we set + // Verify that the user didn't set fields that we don't support in the discovery version we set. + // Fields that are safe to ignore on older runtimes (e.g. preemptable) are not verified here, + // they're dropped by DiscoveryProtocol.serializeManifest instead. for (var service : manifest.getServices()) { if (version.getNumber() < DiscoveryProtocol.Version.V2.getNumber()) { verifyFieldNotSet( @@ -107,6 +109,7 @@ private static Service convertService(ServiceDefinition svc) { .withAbortTimeout(svc.getAbortTimeout() != null ? svc.getAbortTimeout().toMillis() : null) .withEnableLazyState(svc.getEnableLazyState()) .withIngressPrivate(svc.getIngressPrivate()) + .withPreemptable(svc.getPreemptable()) .withRetryPolicyInitialInterval( svc.getInvocationRetryPolicy() != null && svc.getInvocationRetryPolicy().initialInterval() != null @@ -185,6 +188,7 @@ private static Handler convertHandler(HandlerDefinition handler) { handler.getAbortTimeout() != null ? handler.getAbortTimeout().toMillis() : null) .withEnableLazyState(handler.getEnableLazyState()) .withIngressPrivate(handler.getIngressPrivate()) + .withPreemptable(handler.getPreemptable()) .withRetryPolicyInitialInterval( handler.getInvocationRetryPolicy() != null && handler.getInvocationRetryPolicy().initialInterval() != null diff --git a/sdk-core/src/main/service-protocol/dev/restate/service/discovery.proto b/sdk-core/src/main/service-protocol/dev/restate/service/discovery.proto index 519dc4e54..557d62bba 100644 --- a/sdk-core/src/main/service-protocol/dev/restate/service/discovery.proto +++ b/sdk-core/src/main/service-protocol/dev/restate/service/discovery.proto @@ -24,4 +24,8 @@ enum ServiceDiscoveryProtocolVersion { V3 = 3; // add lambda compression V4 = 4; + // add alwaysEagerStateKeys (eager-preload whitelist when lazy state is enabled) + V5 = 5; + // add preemptable (opt out of eager suspension when concurrency capacity runs out) + V6 = 6; } diff --git a/sdk-core/src/main/service-protocol/endpoint_manifest_schema.json b/sdk-core/src/main/service-protocol/endpoint_manifest_schema.json index e45699b09..b8486240f 100644 --- a/sdk-core/src/main/service-protocol/endpoint_manifest_schema.json +++ b/sdk-core/src/main/service-protocol/endpoint_manifest_schema.json @@ -180,6 +180,10 @@ "type": "boolean", "description": "If true, the service cannot be invoked from the HTTP nor Kafka ingress." }, + "preemptable": { + "type": "boolean", + "description": "If false, the runtime never preempts invocations of this handler, i.e. it doesn't eagerly suspend them while they are waiting on an await point to free up concurrency capacity for other invocations. Invocations still suspend when the inactivity timeout fires. If unset, the service-level setting applies. Available since discovery protocol V6." + }, "retryPolicyInitialInterval": { "type": "integer", "minimum": 0, @@ -249,6 +253,10 @@ "type": "boolean", "description": "If true, the service cannot be invoked from the HTTP nor Kafka ingress." }, + "preemptable": { + "type": "boolean", + "description": "If false, the runtime never preempts invocations of this service, i.e. it doesn't eagerly suspend them while they are waiting on an await point to free up concurrency capacity for other invocations. Invocations still suspend when the inactivity timeout fires. Defaults to true. Available since discovery protocol V6." + }, "retryPolicyInitialInterval": { "type": "integer", "minimum": 0, diff --git a/sdk-core/src/test/java/dev/restate/sdk/core/ComponentDiscoveryHandlerTest.java b/sdk-core/src/test/java/dev/restate/sdk/core/ComponentDiscoveryHandlerTest.java index 2c4a72f24..88faef16c 100644 --- a/sdk-core/src/test/java/dev/restate/sdk/core/ComponentDiscoveryHandlerTest.java +++ b/sdk-core/src/test/java/dev/restate/sdk/core/ComponentDiscoveryHandlerTest.java @@ -10,6 +10,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.fasterxml.jackson.databind.JsonNode; import dev.restate.sdk.core.generated.manifest.EndpointManifestSchema; import dev.restate.sdk.core.generated.manifest.Service; import dev.restate.sdk.endpoint.definition.HandlerDefinition; @@ -19,6 +20,7 @@ import dev.restate.serde.Serde; import java.util.List; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.junit.jupiter.api.Test; class ComponentDiscoveryHandlerTest { @@ -44,4 +46,88 @@ void handleWithMultipleServices() { assertThat(manifest.getProtocolMode()) .isEqualTo(EndpointManifestSchema.ProtocolMode.REQUEST_RESPONSE); } + + @Test + void negotiateDiscoveryVersion() { + // Runtimes send all the versions they support, comma separated + assertThat( + DiscoveryProtocol.selectSupportedServiceDiscoveryProtocolVersion( + "application/vnd.restate.endpointmanifest.v4+json, " + + "application/vnd.restate.endpointmanifest.v5+json, " + + "application/vnd.restate.endpointmanifest.v6+json")) + .isEqualTo(DiscoveryProtocol.Version.V6) + .isEqualTo(DiscoveryProtocol.Version.MAX) + .extracting(DiscoveryProtocol.Version::getHeader) + .isEqualTo("application/vnd.restate.endpointmanifest.v6+json"); + assertThat( + DiscoveryProtocol.selectSupportedServiceDiscoveryProtocolVersion( + "application/vnd.restate.endpointmanifest.v4+json, " + + "application/vnd.restate.endpointmanifest.v5+json")) + .isEqualTo(DiscoveryProtocol.Version.V4); + } + + @Test + void preemptableIsEmittedOnlyWhenSetAndSupported() throws Exception { + EndpointManifest endpointManifest = + new EndpointManifest( + Stream.of( + ServiceDefinition.of( + "NonPreemptable", + ServiceType.SERVICE, + List.of( + HandlerDefinition.of( + "overridden", HandlerType.EXCLUSIVE, Serde.VOID, Serde.VOID, null), + HandlerDefinition.of( + "inherited", HandlerType.EXCLUSIVE, Serde.VOID, Serde.VOID, null))) + .configure( + c -> + c.preemptable(false) + .configureHandler("overridden", h -> h.preemptable(true))), + ServiceDefinition.of( + "Default", + ServiceType.SERVICE, + List.of( + HandlerDefinition.of( + "greet", HandlerType.EXCLUSIVE, Serde.VOID, Serde.VOID, null)))), + false); + + // V6: emitted only where explicitly set + JsonNode v6 = serialize(endpointManifest, DiscoveryProtocol.Version.V6); + JsonNode nonPreemptable = findByName(v6.get("services"), "NonPreemptable"); + assertThat(nonPreemptable.get("preemptable").asBoolean(true)).isFalse(); + assertThat( + findByName(nonPreemptable.get("handlers"), "overridden").get("preemptable").asBoolean()) + .isTrue(); + assertThat(findByName(nonPreemptable.get("handlers"), "inherited").has("preemptable")) + .isFalse(); + JsonNode defaultService = findByName(v6.get("services"), "Default"); + assertThat(defaultService.has("preemptable")).isFalse(); + assertThat(findByName(defaultService.get("handlers"), "greet").has("preemptable")).isFalse(); + + // Below V6: silently dropped + for (DiscoveryProtocol.Version version : + List.of(DiscoveryProtocol.Version.V3, DiscoveryProtocol.Version.V4)) { + JsonNode manifest = serialize(endpointManifest, version); + assertThat(manifest.findValues("preemptable")).as("version %s", version).isEmpty(); + assertThat(findByName(manifest.get("services"), "NonPreemptable").get("handlers")) + .as("version %s", version) + .hasSize(2); + } + } + + private static JsonNode serialize( + EndpointManifest endpointManifest, DiscoveryProtocol.Version version) throws Exception { + return DiscoveryProtocol.MANIFEST_OBJECT_MAPPER.readTree( + DiscoveryProtocol.serializeManifest( + version, + endpointManifest.manifest( + version, EndpointManifestSchema.ProtocolMode.REQUEST_RESPONSE))); + } + + private static JsonNode findByName(JsonNode array, String name) { + return StreamSupport.stream(array.spliterator(), false) + .filter(n -> name.equals(n.get("name").asText())) + .findFirst() + .orElseThrow(); + } } diff --git a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentProperties.java b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentProperties.java index 0dcd42a5b..e9ccf4c21 100644 --- a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentProperties.java +++ b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentProperties.java @@ -29,6 +29,7 @@ public class RestateComponentProperties { @Nullable private Duration workflowRetention; @Nullable private Duration journalRetention; @Nullable private Boolean ingressPrivate; + @Nullable private Boolean preemptable; @Nullable private Boolean enableLazyState; @Nullable private RetryPolicyProperties retryPolicy; @Nullable private Map handlers; @@ -48,6 +49,36 @@ public RestateComponentProperties( @Nullable Boolean enableLazyState, @Nullable RetryPolicyProperties retryPolicy, @Nullable Map handlers) { + this( + executor, + documentation, + metadata, + inactivityTimeout, + abortTimeout, + idempotencyRetention, + workflowRetention, + journalRetention, + ingressPrivate, + null, + enableLazyState, + retryPolicy, + handlers); + } + + public RestateComponentProperties( + @Nullable String executor, + @Nullable String documentation, + @Nullable Map metadata, + @Nullable Duration inactivityTimeout, + @Nullable Duration abortTimeout, + @Nullable Duration idempotencyRetention, + @Nullable Duration workflowRetention, + @Nullable Duration journalRetention, + @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, + @Nullable Boolean enableLazyState, + @Nullable RetryPolicyProperties retryPolicy, + @Nullable Map handlers) { this.executor = executor; this.documentation = documentation; this.metadata = metadata; @@ -57,6 +88,7 @@ public RestateComponentProperties( this.workflowRetention = workflowRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.retryPolicy = retryPolicy; this.handlers = handlers; @@ -278,6 +310,42 @@ public void setIngressPrivate(@Nullable Boolean ingressPrivate) { this.ingressPrivate = ingressPrivate; } + /** + * When set to {@code false}, Restate never preempts invocations to this service, with all its + * handlers. Preemption means that when the restate-server runs out of concurrency capacity, it + * can eagerly suspend an invocation that is waiting on an await point (e.g. a timer, a call + * response, an awakeable), to free up capacity for other invocations. Non-preemptable invocations + * still suspend when the inactivity timeout fires. + * + *

If unset, the restate-server treats the service as preemptable ({@code true}). Can be + * overridden per-handler in {@link #getHandlers()}. + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + */ + public @Nullable Boolean getPreemptable() { + return preemptable; + } + + /** + * When set to {@code false}, Restate never preempts invocations to this service, with all its + * handlers. Preemption means that when the restate-server runs out of concurrency capacity, it + * can eagerly suspend an invocation that is waiting on an await point (e.g. a timer, a call + * response, an awakeable), to free up capacity for other invocations. Non-preemptable invocations + * still suspend when the inactivity timeout fires. + * + *

If unset, the restate-server treats the service as preemptable ({@code true}). Can be + * overridden per-handler in {@link #getHandlers()}. + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + */ + public void setPreemptable(@Nullable Boolean preemptable) { + this.preemptable = preemptable; + } + /** * When set to {@code true}, lazy state will be enabled for all invocations to this service. This * is relevant only for workflows and virtual objects. @@ -342,6 +410,7 @@ public boolean equals(Object o) { && Objects.equals(getWorkflowRetention(), that.getWorkflowRetention()) && Objects.equals(getJournalRetention(), that.getJournalRetention()) && Objects.equals(getIngressPrivate(), that.getIngressPrivate()) + && Objects.equals(getPreemptable(), that.getPreemptable()) && Objects.equals(getEnableLazyState(), that.getEnableLazyState()) && Objects.equals(getRetryPolicy(), that.getRetryPolicy()) && Objects.equals(getHandlers(), that.getHandlers()); @@ -359,6 +428,7 @@ public int hashCode() { getWorkflowRetention(), getJournalRetention(), getIngressPrivate(), + getPreemptable(), getEnableLazyState(), getRetryPolicy(), getHandlers()); @@ -387,6 +457,8 @@ public String toString() { + journalRetention + ", ingressPrivate=" + ingressPrivate + + ", preemptable=" + + preemptable + ", enableLazyState=" + enableLazyState + ", retryPolicy=" diff --git a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentsProperties.java b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentsProperties.java index 979efa58f..ea880617f 100644 --- a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentsProperties.java +++ b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateComponentsProperties.java @@ -36,6 +36,7 @@ * restate.components.MyService.idempotency-retention=1d * restate.components.MyService.journal-retention=7d * restate.components.MyService.ingress-private=false + * restate.components.MyService.preemptable=false * restate.components.MyService.enable-lazy-state=true * restate.components.MyService.documentation=My service description * restate.components.MyService.metadata.version=1.0 @@ -65,6 +66,7 @@ public class RestateComponentsProperties { @Nullable private Duration workflowRetention; @Nullable private Duration journalRetention; @Nullable private Boolean ingressPrivate; + @Nullable private Boolean preemptable; @Nullable private Boolean enableLazyState; @Nullable private RetryPolicyProperties retryPolicy; @@ -275,6 +277,32 @@ public void setIngressPrivate(@Nullable Boolean ingressPrivate) { this.ingressPrivate = ingressPrivate; } + /** + * Default preemptable setting for all services. Can be overridden per-service in {@link + * #getComponents()}. + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + * + * @see RestateComponentProperties#getPreemptable() + */ + public @Nullable Boolean getPreemptable() { + return preemptable; + } + + /** + * Default preemptable setting for all services. Can be overridden per-service in {@link + * #getComponents()}. + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + */ + public void setPreemptable(@Nullable Boolean preemptable) { + this.preemptable = preemptable; + } + /** * Default lazy-state setting for all services. Can be overridden per-service in {@link * #getComponents()}. diff --git a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateEndpointConfiguration.java b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateEndpointConfiguration.java index 74afa8ff1..fb751e63a 100644 --- a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateEndpointConfiguration.java +++ b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateEndpointConfiguration.java @@ -206,6 +206,9 @@ public static void configureService( if (properties.getIngressPrivate() != null) { configurator.ingressPrivate(properties.getIngressPrivate()); } + if (properties.getPreemptable() != null) { + configurator.preemptable(properties.getPreemptable()); + } if (properties.getEnableLazyState() != null) { configurator.enableLazyState(properties.getEnableLazyState()); } @@ -245,6 +248,9 @@ private static void configureServiceDefaults( if (properties.getIngressPrivate() != null) { configurator.ingressPrivate(properties.getIngressPrivate()); } + if (properties.getPreemptable() != null) { + configurator.preemptable(properties.getPreemptable()); + } if (properties.getEnableLazyState() != null) { configurator.enableLazyState(properties.getEnableLazyState()); } @@ -279,6 +285,9 @@ private static void configureHandler( if (properties.getIngressPrivate() != null) { configurator.ingressPrivate(properties.getIngressPrivate()); } + if (properties.getPreemptable() != null) { + configurator.preemptable(properties.getPreemptable()); + } if (properties.getEnableLazyState() != null) { configurator.enableLazyState(properties.getEnableLazyState()); } diff --git a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateHandlerProperties.java b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateHandlerProperties.java index dd0b317cc..2a6a94da9 100644 --- a/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateHandlerProperties.java +++ b/sdk-spring-boot/src/main/java/dev/restate/sdk/springboot/RestateHandlerProperties.java @@ -29,6 +29,7 @@ public class RestateHandlerProperties { @Nullable private Duration workflowRetention; @Nullable private Duration journalRetention; @Nullable private Boolean ingressPrivate; + @Nullable private Boolean preemptable; @Nullable private Boolean enableLazyState; @Nullable private RetryPolicyProperties retryPolicy; @@ -45,6 +46,32 @@ public RestateHandlerProperties( @Nullable Boolean ingressPrivate, @Nullable Boolean enableLazyState, @Nullable RetryPolicyProperties retryPolicy) { + this( + documentation, + metadata, + inactivityTimeout, + abortTimeout, + idempotencyRetention, + workflowRetention, + journalRetention, + ingressPrivate, + null, + enableLazyState, + retryPolicy); + } + + public RestateHandlerProperties( + @Nullable String documentation, + @Nullable Map metadata, + @Nullable Duration inactivityTimeout, + @Nullable Duration abortTimeout, + @Nullable Duration idempotencyRetention, + @Nullable Duration workflowRetention, + @Nullable Duration journalRetention, + @Nullable Boolean ingressPrivate, + @Nullable Boolean preemptable, + @Nullable Boolean enableLazyState, + @Nullable RetryPolicyProperties retryPolicy) { this.documentation = documentation; this.metadata = metadata; this.inactivityTimeout = inactivityTimeout; @@ -53,6 +80,7 @@ public RestateHandlerProperties( this.workflowRetention = workflowRetention; this.journalRetention = journalRetention; this.ingressPrivate = ingressPrivate; + this.preemptable = preemptable; this.enableLazyState = enableLazyState; this.retryPolicy = retryPolicy; } @@ -249,6 +277,42 @@ public void setIngressPrivate(@Nullable Boolean ingressPrivate) { this.ingressPrivate = ingressPrivate; } + /** + * When set to {@code false}, Restate never preempts invocations to this handler. Preemption means + * that when the restate-server runs out of concurrency capacity, it can eagerly suspend an + * invocation that is waiting on an await point (e.g. a timer, a call response, an awakeable), to + * free up capacity for other invocations. Non-preemptable invocations still suspend when the + * inactivity timeout fires. + * + *

This overrides the value set for the service. If unset both here and on the service, the + * restate-server treats the handler as preemptable ({@code true}). + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + */ + public @Nullable Boolean getPreemptable() { + return preemptable; + } + + /** + * When set to {@code false}, Restate never preempts invocations to this handler. Preemption means + * that when the restate-server runs out of concurrency capacity, it can eagerly suspend an + * invocation that is waiting on an await point (e.g. a timer, a call response, an awakeable), to + * free up capacity for other invocations. Non-preemptable invocations still suspend when the + * inactivity timeout fires. + * + *

This overrides the value set for the service. If unset both here and on the service, the + * restate-server treats the handler as preemptable ({@code true}). + * + *

NOTE: This setting requires a restate-server supporting the service discovery + * protocol V6. When registering against an older restate-server, this setting is ignored, as + * older restate-server versions never preempt invocations. + */ + public void setPreemptable(@Nullable Boolean preemptable) { + this.preemptable = preemptable; + } + /** * When set to {@code true}, lazy state will be enabled for all invocations to this handler. This * is relevant only for workflows and virtual objects. @@ -302,6 +366,7 @@ public boolean equals(Object o) { && Objects.equals(getWorkflowRetention(), that.getWorkflowRetention()) && Objects.equals(getJournalRetention(), that.getJournalRetention()) && Objects.equals(getIngressPrivate(), that.getIngressPrivate()) + && Objects.equals(getPreemptable(), that.getPreemptable()) && Objects.equals(getEnableLazyState(), that.getEnableLazyState()) && Objects.equals(getRetryPolicy(), that.getRetryPolicy()); } @@ -317,6 +382,7 @@ public int hashCode() { getWorkflowRetention(), getJournalRetention(), getIngressPrivate(), + getPreemptable(), getEnableLazyState(), getRetryPolicy()); } @@ -341,6 +407,8 @@ public String toString() { + journalRetention + ", ingressPrivate=" + ingressPrivate + + ", preemptable=" + + preemptable + ", enableLazyState=" + enableLazyState + ", retryPolicy="