Skip to content

Commit

Permalink
Replace Deployment* with EndpointManifestSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
tillrohrmann committed May 19, 2024
1 parent ecde439 commit 32e0b7d
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

final class DeploymentManifest {
final class EndpointManifest {

private static final Input EMPTY_INPUT = new Input();
private static final Output EMPTY_OUTPUT = new Output().withSetContentTypeIfEmpty(false);

private final DeploymentManifestSchema manifest;
private final EndpointManifestSchema manifest;

public DeploymentManifest(
DeploymentManifestSchema.ProtocolMode protocolMode, Stream<ServiceDefinition<?>> components) {
public EndpointManifest(
EndpointManifestSchema.ProtocolMode protocolMode, Stream<ServiceDefinition<?>> components) {
this.manifest =
new DeploymentManifestSchema()
new EndpointManifestSchema()
.withMinProtocolVersion(MIN_SERVICE_PROTOCOL_VERSION.getNumber())
.withMaxProtocolVersion(MAX_SERVICE_PROTOCOL_VERSION.getNumber())
.withProtocolMode(protocolMode)
Expand All @@ -43,12 +43,12 @@ public DeploymentManifest(
.withTy(convertServiceType(svc.getServiceType()))
.withHandlers(
svc.getHandlers().stream()
.map(DeploymentManifest::convertHandler)
.map(EndpointManifest::convertHandler)
.collect(Collectors.toList())))
.collect(Collectors.toList()));
}

public DeploymentManifestSchema manifest() {
public EndpointManifestSchema manifest() {
return this.manifest;
}

Expand Down
18 changes: 9 additions & 9 deletions sdk-core/src/main/java/dev/restate/sdk/core/RestateEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import dev.restate.sdk.common.BindableServiceFactory;
import dev.restate.sdk.common.syscalls.HandlerDefinition;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.core.manifest.Service;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
Expand All @@ -34,18 +34,18 @@ public class RestateEndpoint {
private final Map<String, ServiceAndOptions<?>> services;
private final Tracer tracer;
private final RequestIdentityVerifier requestIdentityVerifier;
private final DeploymentManifest deploymentManifest;
private final EndpointManifest deploymentManifest;

private RestateEndpoint(
DeploymentManifestSchema.ProtocolMode protocolMode,
EndpointManifestSchema.ProtocolMode protocolMode,
Map<String, ServiceAndOptions<?>> services,
Tracer tracer,
RequestIdentityVerifier requestIdentityVerifier) {
this.services = services;
this.tracer = tracer;
this.requestIdentityVerifier = requestIdentityVerifier;
this.deploymentManifest =
new DeploymentManifest(protocolMode, services.values().stream().map(c -> c.service));
new EndpointManifest(protocolMode, services.values().stream().map(c -> c.service));

this.logCreation();
}
Expand Down Expand Up @@ -99,8 +99,8 @@ public ResolvedEndpointHandler resolve(
return new ResolvedEndpointHandlerImpl(stateMachine, handler, svc.options, syscallExecutor);
}

public DeploymentManifestSchema handleDiscoveryRequest() {
DeploymentManifestSchema response = this.deploymentManifest.manifest();
public EndpointManifestSchema handleDiscoveryRequest() {
EndpointManifestSchema response = this.deploymentManifest.manifest();
LOG.info(
"Replying to discovery request with services [{}]",
response.getServices().stream().map(Service::getName).collect(Collectors.joining(",")));
Expand All @@ -113,18 +113,18 @@ private void logCreation() {

// -- Builder

public static Builder newBuilder(DeploymentManifestSchema.ProtocolMode protocolMode) {
public static Builder newBuilder(EndpointManifestSchema.ProtocolMode protocolMode) {
return new Builder(protocolMode);
}

public static class Builder {

private final List<ServiceAndOptions<?>> services = new ArrayList<>();
private final DeploymentManifestSchema.ProtocolMode protocolMode;
private final EndpointManifestSchema.ProtocolMode protocolMode;
private RequestIdentityVerifier requestIdentityVerifier;
private Tracer tracer = OpenTelemetry.noop().getTracer("NOOP");

public Builder(DeploymentManifestSchema.ProtocolMode protocolMode) {
public Builder(EndpointManifestSchema.ProtocolMode protocolMode) {
this.protocolMode = protocolMode;
}

Expand Down
15 changes: 9 additions & 6 deletions sdk-core/src/main/java/dev/restate/sdk/core/ServiceProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.restate.generated.service.discovery.Discovery;
import dev.restate.generated.service.protocol.Protocol;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -40,7 +40,8 @@ public static String serviceProtocolVersionToHeaderValue(
if (Objects.requireNonNull(version) == Protocol.ServiceProtocolVersion.V1) {
return "application/vnd.restate.invocation.v1";
}
throw new IllegalArgumentException(String.format("Service protocol version '%s' has no header value", version.getNumber()));
throw new IllegalArgumentException(
String.format("Service protocol version '%s' has no header value", version.getNumber()));
}

public static boolean is_supported(Protocol.ServiceProtocolVersion serviceProtocolVersion) {
Expand All @@ -60,8 +61,8 @@ public static boolean is_supported(
* Selects the highest supported service protocol version from a list of supported versions.
*
* @param acceptedVersionsString A comma-separated list of accepted service protocol versions.
* @return The highest supported service protocol version, otherwise {@link
* Protocol.ServiceProtocolVersion.SERVICE_PROTOCOL_VERSION_UNSPECIFIED}
* @return The highest supported service protocol version, otherwise
* Protocol.ServiceProtocolVersion.SERVICE_PROTOCOL_VERSION_UNSPECIFIED
*/
public static Discovery.ServiceDiscoveryProtocolVersion
selectSupportedServiceDiscoveryProtocolVersion(String acceptedVersionsString) {
Expand Down Expand Up @@ -105,7 +106,9 @@ public static String serviceDiscoveryProtocolVersionToHeaderValue(
if (Objects.requireNonNull(version) == Discovery.ServiceDiscoveryProtocolVersion.V1) {
return "application/vnd.restate.endpointmanifest.v1+json";
}
throw new IllegalArgumentException(String.format("Service discovery protocol version '%s' has no header value", version.getNumber()));
throw new IllegalArgumentException(
String.format(
"Service discovery protocol version '%s' has no header value", version.getNumber()));
}

public static class DiscoveryResponseSerializer {
Expand All @@ -122,7 +125,7 @@ public DiscoveryResponseSerializer(
this.serviceDiscoveryProtocolVersion = serviceDiscoveryProtocolVersion;
}

public byte[] serialize(DeploymentManifestSchema response) throws Exception {
public byte[] serialize(EndpointManifestSchema response) throws Exception {
if (this.serviceDiscoveryProtocolVersion == Discovery.ServiceDiscoveryProtocolVersion.V1) {
return MANIFEST_OBJECT_MAPPER.writeValueAsBytes(response);
}
Expand Down
22 changes: 11 additions & 11 deletions sdk-core/src/test/java/dev/restate/sdk/core/AssertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import dev.restate.generated.service.protocol.Protocol;
import dev.restate.sdk.common.BindableService;
import dev.restate.sdk.common.TerminalException;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.core.manifest.Handler;
import dev.restate.sdk.core.manifest.Service;
import java.util.Arrays;
Expand Down Expand Up @@ -68,10 +68,10 @@ public static Consumer<? super MessageLite> protocolExceptionErrorMessage(int co
.startsWith(ProtocolException.class.getCanonicalName()));
}

public static DeploymentManifestSchemaAssert assertThatDiscovery(Object... services) {
return new DeploymentManifestSchemaAssert(
new DeploymentManifest(
DeploymentManifestSchema.ProtocolMode.BIDI_STREAM,
public static EndpointManifestSchemaAssert assertThatDiscovery(Object... services) {
return new EndpointManifestSchemaAssert(
new EndpointManifest(
EndpointManifestSchema.ProtocolMode.BIDI_STREAM,
Arrays.stream(services)
.flatMap(
svc -> {
Expand All @@ -85,14 +85,14 @@ public static DeploymentManifestSchemaAssert assertThatDiscovery(Object... servi
.stream();
}))
.manifest(),
DeploymentManifestSchemaAssert.class);
EndpointManifestSchemaAssert.class);
}

public static class DeploymentManifestSchemaAssert
extends AbstractObjectAssert<DeploymentManifestSchemaAssert, DeploymentManifestSchema> {
public DeploymentManifestSchemaAssert(
DeploymentManifestSchema deploymentManifestSchema, Class<?> selfType) {
super(deploymentManifestSchema, selfType);
public static class EndpointManifestSchemaAssert
extends AbstractObjectAssert<EndpointManifestSchemaAssert, EndpointManifestSchema> {
public EndpointManifestSchemaAssert(
EndpointManifestSchema endpointManifestSchema, Class<?> selfType) {
super(endpointManifestSchema, selfType);
}

public ServiceAssert extractingService(String service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import dev.restate.sdk.common.syscalls.HandlerDefinition;
import dev.restate.sdk.common.syscalls.HandlerSpecification;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema.ProtocolMode;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.core.manifest.Service;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -27,9 +26,9 @@ class ComponentDiscoveryHandlerTest {

@Test
void handleWithMultipleServices() {
DeploymentManifest deploymentManifest =
new DeploymentManifest(
ProtocolMode.REQUEST_RESPONSE,
EndpointManifest deploymentManifest =
new EndpointManifest(
EndpointManifestSchema.ProtocolMode.REQUEST_RESPONSE,
Stream.of(
ServiceDefinition.of(
"MyGreeter",
Expand All @@ -40,9 +39,10 @@ void handleWithMultipleServices() {
"greet", HandlerType.EXCLUSIVE, CoreSerdes.VOID, CoreSerdes.VOID),
null)))));

DeploymentManifestSchema manifest = deploymentManifest.manifest();
EndpointManifestSchema manifest = deploymentManifest.manifest();

assertThat(manifest.getServices()).extracting(Service::getName).containsOnly("MyGreeter");
assertThat(manifest.getProtocolMode()).isEqualTo(ProtocolMode.REQUEST_RESPONSE);
assertThat(manifest.getProtocolMode())
.isEqualTo(EndpointManifestSchema.ProtocolMode.REQUEST_RESPONSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.google.protobuf.MessageLite;
import dev.restate.sdk.common.BindableService;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -47,7 +47,7 @@ public void executeTest(TestDefinitions.TestDefinition definition) {

// Prepare server
RestateEndpoint.Builder builder =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.BIDI_STREAM)
RestateEndpoint.newBuilder(EndpointManifestSchema.ProtocolMode.BIDI_STREAM)
.bind(serviceDefinition.get(0), bindableService.options());
RestateEndpoint server = builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.TestDefinitions.TestDefinition;
import dev.restate.sdk.core.TestDefinitions.TestExecutor;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import java.time.Duration;
import java.util.List;
import org.apache.logging.log4j.ThreadContext;
Expand Down Expand Up @@ -45,7 +45,7 @@ public void executeTest(TestDefinition definition) {

// Prepare server
RestateEndpoint.Builder builder =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.BIDI_STREAM)
RestateEndpoint.newBuilder(EndpointManifestSchema.ProtocolMode.BIDI_STREAM)
.bind(serviceDefinition.get(0), bindableService.options());
RestateEndpoint server = builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import dev.restate.sdk.core.ResolvedEndpointHandler;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.ServiceProtocol;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.version.Version;
import io.netty.util.AsciiString;
import io.opentelemetry.api.OpenTelemetry;
Expand Down Expand Up @@ -196,7 +196,7 @@ private void handleDiscoveryRequest(HttpServerRequest request) {
.end(errorMessage);
} else {
// Compute response and write it back
DeploymentManifestSchema response = this.restateEndpoint.handleDiscoveryRequest();
EndpointManifestSchema response = this.restateEndpoint.handleDiscoveryRequest();

Buffer responseBuffer;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import dev.restate.sdk.common.BindableService;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import io.opentelemetry.api.OpenTelemetry;
import io.vertx.core.AsyncResult;
import io.vertx.core.Vertx;
Expand Down Expand Up @@ -43,7 +43,7 @@ public class RestateHttpEndpointBuilder {

private final Vertx vertx;
private final RestateEndpoint.Builder endpointBuilder =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.BIDI_STREAM);
RestateEndpoint.newBuilder(EndpointManifestSchema.ProtocolMode.BIDI_STREAM);
private OpenTelemetry openTelemetry = OpenTelemetry.noop();
private HttpServerOptions options =
new HttpServerOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import dev.restate.generated.service.protocol.Protocol.*
import dev.restate.sdk.common.CoreSerdes
import dev.restate.sdk.core.ProtoUtils.*
import dev.restate.sdk.core.ServiceProtocol
import dev.restate.sdk.core.manifest.DeploymentManifestSchema
import dev.restate.sdk.core.manifest.EndpointManifestSchema
import dev.restate.sdk.http.vertx.testservices.BlockingGreeter
import dev.restate.sdk.http.vertx.testservices.greeter
import io.netty.handler.codec.http.HttpResponseStatus
Expand Down Expand Up @@ -232,8 +232,8 @@ internal class RestateHttpEndpointTest {
// Parse response
val responseBody = response.body().coAwait()
// Compute response and write it back
val discoveryResponse: DeploymentManifestSchema =
ObjectMapper().readValue(responseBody.bytes, DeploymentManifestSchema::class.java)
val discoveryResponse: EndpointManifestSchema =
ObjectMapper().readValue(responseBody.bytes, EndpointManifestSchema::class.java)

assertThat(discoveryResponse.services)
.map<String> { it.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import dev.restate.sdk.core.ResolvedEndpointHandler;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.ServiceProtocol;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.version.Version;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand Down Expand Up @@ -206,7 +206,7 @@ private APIGatewayProxyResponseEvent handleDiscovery(String acceptVersionsString
return response;
} else {
// Compute response and write it back
DeploymentManifestSchema responseManifest = this.restateEndpoint.handleDiscoveryRequest();
EndpointManifestSchema responseManifest = this.restateEndpoint.handleDiscoveryRequest();
byte[] serializedManifest;
try {
serializedManifest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import dev.restate.sdk.common.BindableService;
import dev.restate.sdk.common.syscalls.ServiceDefinition;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import io.opentelemetry.api.OpenTelemetry;

/** Endpoint builder for a Restate AWS Lambda Endpoint, to serve Restate service. */
public final class RestateLambdaEndpointBuilder {

private final RestateEndpoint.Builder restateEndpoint =
RestateEndpoint.newBuilder(DeploymentManifestSchema.ProtocolMode.REQUEST_RESPONSE);
RestateEndpoint.newBuilder(EndpointManifestSchema.ProtocolMode.REQUEST_RESPONSE);
private OpenTelemetry openTelemetry = OpenTelemetry.noop();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import dev.restate.generated.service.protocol.Protocol;
import dev.restate.sdk.core.ProtoUtils;
import dev.restate.sdk.core.ServiceProtocol;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.core.manifest.EndpointManifestSchema;
import dev.restate.sdk.core.manifest.Service;
import dev.restate.sdk.lambda.testservices.JavaCounterDefinitions;
import dev.restate.sdk.lambda.testservices.MyServicesHandler;
Expand Down Expand Up @@ -111,8 +111,8 @@ public void testDiscovery() throws IOException {
assertThat(response.getIsBase64Encoded()).isTrue();
byte[] decodedStringResponse = Base64.getDecoder().decode(response.getBody());
// Compute response and write it back
DeploymentManifestSchema discoveryResponse =
new ObjectMapper().readValue(decodedStringResponse, DeploymentManifestSchema.class);
EndpointManifestSchema discoveryResponse =
new ObjectMapper().readValue(decodedStringResponse, EndpointManifestSchema.class);

assertThat(discoveryResponse.getServices())
.map(Service::getName)
Expand Down

0 comments on commit 32e0b7d

Please sign in to comment.