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

Ktor on Native Image - Doesn't work #163

Open
atrianac opened this issue Sep 16, 2020 · 13 comments
Open

Ktor on Native Image - Doesn't work #163

atrianac opened this issue Sep 16, 2020 · 13 comments
Assignees

Comments

@atrianac
Copy link

atrianac commented Sep 16, 2020

Steps to Reproduce

  1. Create a Ktor/Kotlin application using Native Image. The docker file looks like:
FROM oracle/graalvm-ce:20.1.0-java11 as graalvm
RUN gu install native-image

COPY . /home/app/micronaut-api
WORKDIR /home/app/micronaut-api

RUN native-image --verbose --no-server --no-fallback --install-exit-handlers --verbose -J-Xmx8G -J-Xms4G -H:+ReportUnsupportedElementsAtRuntime -H:+ReportExceptionStackTraces -H:+TraceClassInitialization -H:+PrintClassInitialization -H:+RemoveSaturatedTypeFlows -cp build/libs/micronaut-api-*-all.jar

FROM frolvlad/alpine-glibc
RUN apk update && apk add libstdc++
EXPOSE 8080
COPY --from=graalvm /home/app/micronaut-api/micronaut-api /app/micronaut-api
ENTRYPOINT ["/app/micronaut-api"]
  1. Create the docker image running:
docker build . -t micronaut-api
  1. Launch a container:
 docker run -p 8080:8080 micronaut-api
  1. Inspect the docker logs
docker logs {container.id}

The output looks like this:

01:24:18.763 [main] INFO  Application - No ktor.deployment.watch patterns specified, automatic reload is not active
01:24:18.766 [main] INFO  Application - Responding at http://0.0.0.0:8080
01:24:18.768 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 4378ms. Server Running: http://0.0.0.0:8080
  1. Inspect the response, from any endpoint using curl:
curl http://localhost:8080/{path}

An empty response from the server is returned.

Expected Behaviour

Using the same code project, but on Hotspot runtime, the server returns a response:

FROM amazoncorretto:11.0.8-alpine

COPY ./build/libs/micronaut-api-*-all.jar /home/app/micronaut-api.jar
WORKDIR /home/app

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "micronaut-api.jar"]

Actual Behaviour

An empty response from the server is returned.

Environment Information

  • Operating System: MacOS Catalina 10.15.6
  • Micronaut Version: 2.0.1
  • JDK Version: 11.0.8
  • Graal VM: 20.1.0

Example Application

@atrianac
Copy link
Author

I uploaded the complete repository: micronaut-ktor-api

@willbuck
Copy link
Contributor

So @atrianac I was able to reproduce the issue as you describe it, however I think we need to elevate this issue to ktor itself. I was also able to reproduce the issue by replacing the main function of the Application.kt file in your micronaut-ktor-api with the bare-bones "Hello World" example from the ktor docs and see the exact same problem (see below for replaced Application.kt.

So I think likely this is a graal + ktor related issue, and not really related to micronaut.

@atrianac would you feel comfortable filing an issue w/ the ktor folks? I'm afraid I don't have a great deal of ktor experience but I can help with filing the issue if needed, just let me know!

package com.adidas.ys.http
import io.ktor.server.netty.NettyApplicationEngine
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

object Application {
    @JvmStatic
    fun main(args: Array<String>) {
        val server = embeddedServer(Netty, port = 8080) {
            routing {
                get("/") {
                    call.respondText("Hello World!", ContentType.Text.Plain)
                }
                get("/demo") {
                    call.respondText("HELLO WORLD!")
                }
            }
        }
        server.start(wait = true)
    }
}

@atrianac
Copy link
Author

@willbuck Thanks, I will do!

@willbuck willbuck added the status: awaiting third-party Awaiting changes to a third party library label Nov 13, 2020
@atrianac
Copy link
Author

The issue in Ktor Project is reported here: https://youtrack.jetbrains.com/issue/KTOR-1307

@atrianac
Copy link
Author

@willbuck The Ktor team answered the question. It is necessary to add --initialize-at-build-time=io.ktor,kotlinx,kotlin option to native-image command and this snippet to your `reflection-config.json``

[
  {
    "name": "kotlin.reflect.jvm.internal.ReflectionFactoryImpl",
    "allDeclaredConstructors":true
  },
  {
    "name": "kotlin.KotlinVersion",
    "allPublicMethods": true,
    "allDeclaredFields":true,
    "allDeclaredMethods":true,
    "allDeclaredConstructors":true
  },
  {
    "name": "kotlin.KotlinVersion[]"
  },
  {
    "name": "kotlin.KotlinVersion$Companion"
  },
  {
    "name": "kotlin.KotlinVersion$Companion[]"
  },
  {
    "name": "kotlin.internal.jdk8.JDK8PlatformImplementations",
    "allPublicMethods": true,
    "allDeclaredFields":true,
    "allDeclaredMethods":true,
    "allDeclaredConstructors":true
  }
]

I tested and It is already working. Where do we add those change for the micronaut-kotlinproject?

@willbuck
Copy link
Contributor

@atrianac I will take a look later this week!

@atrianac
Copy link
Author

@willbuck thanks. I just updated the original repository with the right changes.

@willbuck
Copy link
Contributor

@atrianac I took a look at this and I think this would actually require a change in https://github.com/micronaut-projects/micronaut-starter/ , probably to the apply method of:

the Ktor feature: https://github.com/micronaut-projects/micronaut-starter/blob/2.3.x/starter-core/src/main/java/io/micronaut/starter/feature/kotlin/Ktor.java#L99

and/or the GraalVM feature (maybe? not sure this is the right spot): https://github.com/micronaut-projects/micronaut-starter/blob/2.3.x/starter-core/src/main/java/io/micronaut/starter/feature/graalvm/GraalVM.java

I'm fairly engaged w/ client work at present so I don't know that I can get to it anytime soon, but contributions would be welcome if you'd like to give it a shot!

@graemerocher
Copy link
Contributor

I don’t believe it needs any of that. The reflection config and native image args can exist in this project in a file in the src/main/resources/META-INF/native-image directory

@graemerocher
Copy link
Contributor

See https://github.com/micronaut-projects/micronaut-r2dbc/blob/master/r2dbc-core/src/main/resources/META-INF/native-image/io.micronaut.r2dbc/micronaut-r2dbc-core/native-image.properties for example

The reflection json needs to be in a file called reflect-config.json in the same directory

@willbuck
Copy link
Contributor

My mistake @atrianac , thanks for correcting me Graeme!

@sdelamo
Copy link
Contributor

sdelamo commented Aug 30, 2023

@timyates can you verify if this issue is still present?

@timyates
Copy link
Member

@sdelamo @graemerocher I believe this issue is still present

At least, I cannot get a native Ktor application to return any content (even with the above changes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

5 participants