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

Http endpoint #64

Merged
merged 2 commits into from
Aug 30, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

# Ignore Gradle build output directory
build

.restate
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ This will populate your local image registry with the various function container
### Picking the container image for testing

In order to always pull the runtime image you can set the environment variable `E2E_IMAGE_PULL_POLICY=always`.
If you want to test against a runtime image that is available locally, unset this environment variable to let testcontainers pick the local image.
If you want to test against a runtime image that is available locally, unset this environment variable to let testcontainers pick the local image.

### Use Restate CLI for local testing outside docker

If you want to test the function containers outside docker, you can use the [Restate CLI](https://github.com/restatedev/cli).

Running `gradle build` will add the compiled descriptor in `.restate/descriptors`, where `restate manifest create` will pick it up.
5 changes: 0 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ subprojects {
TestLogEvent.STANDARD_OUT)
exceptionFormat = TestExceptionFormat.FULL
}
environment =
environment +
mapOf(
"CONTAINER_LOGS_DIR" to "$buildDir/test-results/container-logs",
"RESTATE_RUNTIME_CONTAINER" to "ghcr.io/restatedev/runtime:main")
}

tasks.withType<JavaCompile>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import com.google.protobuf.gradle.*

plugins {
`java-library`
idea
java
alias(libs.plugins.protobuf)
}

dependencies {
compileOnly(libs.javax.annotation.api)

api(libs.grpc.stub)
api(libs.grpc.protobuf)

protobuf(libs.restate.sdk)

compileOnly(libs.javax.annotation.api)
}

protobuf {
Expand All @@ -23,5 +22,14 @@ protobuf {

plugins { id("grpc") { artifact = "io.grpc:protoc-gen-grpc-java:${libs.versions.grpc.get()}" } }

generateProtoTasks { ofSourceSet("main").forEach { it.plugins { id("grpc") } } }
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins { id("grpc") }
// Generate descriptors
it.generateDescriptorSet = true
it.descriptorSetOptions.includeImports = true
it.descriptorSetOptions.path =
"${rootProject.projectDir}/.restate/descriptors/${project.name}.descriptor"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ plugins {
dependencies {
implementation(libs.restate.sdk)

implementation(project(":contracts"))
implementation(project(":functions:utils"))
implementation(project(":functions:collections:contract"))

implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ plugins {
dependencies {
implementation(libs.restate.sdk)

implementation(project(":contracts"))
implementation(project(":functions:utils"))
implementation(project(":functions:collections:contract"))
implementation(project(":functions:coordinator:contract"))

implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
27 changes: 0 additions & 27 deletions functions/coordinator/contract/build.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ plugins {
dependencies {
implementation(libs.restate.sdk)

implementation(project(":contracts"))
implementation(project(":functions:utils"))
implementation(project(":functions:counter:contract"))

implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
27 changes: 0 additions & 27 deletions functions/counter/contract/build.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ plugins {
dependencies {
implementation(libs.restate.sdk)

implementation(project(":contracts"))
implementation(project(":functions:utils"))
implementation(project(":functions:errors:contract"))

implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
27 changes: 0 additions & 27 deletions functions/errors/contract/build.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ plugins {
dependencies {
implementation(libs.restate.sdk)

implementation(project(":contracts"))
implementation(project(":functions:utils"))
implementation(project(":functions:externalcall:contract"))

implementation(libs.log4j.api)
implementation(libs.log4j.core)
Expand Down
27 changes: 0 additions & 27 deletions functions/externalcall/contract/build.gradle.kts

This file was deleted.

3 changes: 1 addition & 2 deletions functions/http-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ plugins {
}

dependencies {
implementation(project(":functions:externalcall:contract"))

implementation(project(":contracts"))
implementation(libs.grpc.netty.shaded)

implementation(platform(libs.jackson.bom))
Expand Down
16 changes: 6 additions & 10 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
rootProject.name = "restate-e2e"

include(
"functions:collections:contract",
"functions:collections:impl",
"functions:coordinator:contract",
"functions:coordinator:impl",
"functions:counter:contract",
"functions:counter:impl",
"functions:externalcall:contract",
"functions:externalcall:impl",
"functions:errors:contract",
"functions:errors:impl",
"contracts",
"functions:collections",
"functions:coordinator",
"functions:counter",
"functions:externalcall",
"functions:errors",
"functions:http-server",
"functions:utils",
"test-utils",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.restate.e2e.utils

@Target(AnnotationTarget.VALUE_PARAMETER) annotation class InjectHttpEndpointURL
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ private constructor(
private val additionalContainers: Map<String, GenericContainer<*>>,
private val additionalConfig: Map<String, Any>,
private val runtimeContainerName: String,
private val descriptorFile: String?,
) {

companion object {
private const val RESTATE_RUNTIME_CONTAINER_ENV = "RESTATE_RUNTIME_CONTAINER"
private const val DEFAULT_RUNTIME_CONTAINER =
"restatedev.jfrog.io/restatedev-docker-local/runtime"
private const val RUNTIME_GRPC_ENDPOINT = 8090

private const val IMAGE_PULL_POLICY = "E2E_IMAGE_PULL_POLICY"
private const val CONTAINER_LOGS_DIR_ENV = "CONTAINER_LOGS_DIR"

private const val IMAGE_PULL_POLICY_ENV = "E2E_IMAGE_PULL_POLICY"
private const val ALWAYS_PULL = "always"

private const val DESCRIPTORS_FILE_ENV = "DESCRIPTORS_FILE"

private const val RUNTIME_GRPC_ENDPOINT = 8090
private const val RUNTIME_HTTP_ENDPOINT = 8091

private val logger = LogManager.getLogger(RestateDeployer::class.java)

@JvmStatic
Expand All @@ -58,6 +65,7 @@ private constructor(
private var additionalConfig: MutableMap<String, Any> = mutableMapOf(),
private var runtimeContainer: String =
System.getenv(RESTATE_RUNTIME_CONTAINER_ENV) ?: DEFAULT_RUNTIME_CONTAINER,
private var descriptorDirectory: String? = System.getenv(DESCRIPTORS_FILE_ENV),
) {

fun withFunction(functionSpec: FunctionSpec) = apply { this.functions.add(functionSpec) }
Expand Down Expand Up @@ -96,7 +104,12 @@ private constructor(

fun build() =
RestateDeployer(
runtimeDeployments, functions, additionalContainers, additionalConfig, runtimeContainer)
runtimeDeployments,
functions,
additionalContainers,
additionalConfig,
runtimeContainer,
descriptorDirectory)
}

fun deploy(testClass: Class<*>) {
Expand Down Expand Up @@ -141,6 +154,10 @@ private constructor(
config["consensus"] = mapOf("storage_type" to "Memory")
config["grpc_port"] = RUNTIME_GRPC_ENDPOINT
config["services"] = functionContainers.values.flatMap { it.first.toManifests(mapper) }
if (descriptorFile != null) {
config["http_endpoint"] =
mapOf("port" to RUNTIME_HTTP_ENDPOINT, "descriptors_path" to "/contracts.descriptor")
}
config.putAll(additionalConfig)

mapper.writeValue(configFile, config)
Expand All @@ -163,9 +180,15 @@ private constructor(
MountableFile.forHostPath(configFile.toPath()), "/restate.yaml")
.withCommand("--id 1 --configuration-file /restate.yaml")

if (System.getenv(IMAGE_PULL_POLICY) == ALWAYS_PULL) {
if (System.getenv(IMAGE_PULL_POLICY_ENV) == ALWAYS_PULL) {
runtimeContainer!!.withImagePullPolicy(PullPolicy.alwaysPull())
}
if (descriptorFile != null) {
runtimeContainer!!
.withCopyFileToContainer(
MountableFile.forHostPath(descriptorFile), "/contracts.descriptor")
.addExposedPort(RUNTIME_HTTP_ENDPOINT)
}

runtimeContainer!!.start()

Expand All @@ -187,6 +210,18 @@ private constructor(
"Runtime is not configured, as RestateDeployer::deploy has not been invoked")
}

fun getRuntimeHttpEndpointUrl(): URL {
checkNotNull(descriptorFile) {
"No descriptor directory is configured to start the HTTP Endpoint. " +
"Make sure when running tests you have the $DESCRIPTORS_FILE_ENV environment variable correctly configured"
}
return runtimeContainer?.getMappedPort(RUNTIME_HTTP_ENDPOINT)?.let {
URL("http", "127.0.0.1", it, "/")
}
?: throw java.lang.IllegalStateException(
"Runtime is not configured, as RestateDeployer::deploy has not been invoked")
}

fun getAdditionalContainerExposedPort(hostName: String, port: Int): String {
return additionalContainers[hostName]?.let { "${it.host}:${it.getMappedPort(port)}" }
?: throw java.lang.IllegalStateException(
Expand All @@ -196,7 +231,7 @@ private constructor(
private fun computeContainerTestLogsDir(testClass: Class<*>): Path {
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")
return Path.of(
System.getenv("CONTAINER_LOGS_DIR")!!,
System.getenv(CONTAINER_LOGS_DIR_ENV)!!,
"${testClass.canonicalName}_${LocalDateTime.now().format(formatter)}")
}
}
Loading