aws/serverless-java-container

Spring Async initialization still initializing twice, timing out, wrong docs/sample

melinaschweizer opened this issue · 4 comments

To help us debug your issue fill in the basic information below using the options provided

Serverless Java Container version: 2.0.0-M2

Implementations: Spring

Framework version: Spring 6.0.9

Frontend service: api gateway REST API

Deployment method: console

Scenario

Trying to follow the Spring example but not able to get a response from the Lambda after adding Hibernate, but ran into 3 issues:

  1. Figured out I needed the async initialization so I copied the code from the example in https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring:
public StreamLambdaHandler() throws ContainerInitializationException {
        System.out.println("StreamLambdaHandler start");
        long startTime = Instant.now().toEpochMilli();
        handler = new SpringProxyHandlerBuilder()
                .defaultProxy()
                .asyncInit(startTime)
                .configurationClasses(PetStoreSpringAppConfig.class)
                .buildAndInitialize();
        System.out.println("StreamLambdaHandler end");
    }

.. but then I get

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project unicorn: Compilation failure
[ERROR] /Users/mschweiz/AWS-GameDay-Quests/quests/warm_stables/team_lambda_source/src/main/java/com/gameday/StreamLambdaHandler.java:[52,17] cannot find symbol
[ERROR]   symbol:   method configurationClasses(java.lang.Class<com.gameday.PetStoreSpringAppConfig>)
[ERROR]   location: class com.amazonaws.serverless.proxy.internal.servlet.ServletLambdaContainerHandlerBuilder

  1. I thought maybe I don't have the latest aws-serverless-java-container-spring. I was in fact running 1.9.3, so I updated it to what the samples app has in https://github.com/awslabs/aws-serverless-java-container/blob/main/samples/spring/pet-store/pom.xml:
<dependency>
            <groupId>com.amazonaws.serverless</groupId>
            <artifactId>aws-serverless-java-container-spring</artifactId>
            <version>[2.0-SNAPSHOT,)</version>
</dependency>

but then I get error:
[ERROR] Failed to execute goal on project unicorn: Could not resolve dependencies for project com.gameday:unicorn:jar:1.0-SNAPSHOT: Failed to collect dependencies at com.amazonaws.serverless:aws-serverless-java-container-spring:jar:[2.0-SNAPSHOT,): No versions available for com.amazonaws.serverless:aws-serverless-java-container-spring:jar:[2.0-SNAPSHOT,) within specified range -> [Help 1]

  1. So I looked up what was actually available in maven https://mvnrepository.com/artifact/com.amazonaws.serverless/aws-serverless-java-container-spring, and it's either 2.0.0-M2 or 2.0.0-M1.

I tried both, but still get error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project unicorn: Compilation failure
[ERROR] /Users/mschweiz/AWS-GameDay-Quests/quests/warm_stables/team_lambda_source/src/main/java/com/gameday/StreamLambdaHandler.java:[52,17] cannot find symbol
[ERROR]   symbol:   method configurationClasses(java.lang.Class<com.gameday.PetStoreSpringAppConfig>)
[ERROR]   location: class com.amazonaws.serverless.proxy.internal.servlet.ServletLambdaContainerHandlerBuilder
[ERROR] 

Finally, I got it to compile and run by adding <AwsProxyRequest> to new SpringProxyHandlerBuilder<AwsProxyRequest>().

After doing this change, the lambda runs again. However, I'm still seeing initialization twice 10s apart followed by a timeout:
2023-08-30T15:36:21.189+02:00 | INIT_START Runtime Version: java:17.v9
2023-08-30T15:36:21.378+02:00CopyStreamLambdaHandler start | StreamLambdaHandler start
2023-08-30T15:36:22.789+02:00CopyINSIDE STATIC PetStoreSpringAppConfig | INSIDE STATIC PetStoreSpringAppConfig
2023-08-30T15:36:22.929+02:00CopyConnected to datasource! | Connected to datasource!
2023-08-30T15:36:31.368+02:00 | StreamLambdaHandler start
2023-08-30T15:36:32.624+02:00 | INSIDE STATIC PetStoreSpringAppConfig
2023-08-30T15:36:32.762+02:00 | Connected to datasource!
2023-08-30T15:36:41.221+02:00 START RequestId: 71448892-4923-4b6c-855a-4ea7c538a8ac Version: $LATEST
2023-08-30T15:36:41.222+02:00 | INSIDE HANDLEREQUEST
2023-08-30T15:36:41.222+02:00 | inputStream:java.io.ByteArrayInputStream@cd1d761
2023-08-30T15:37:01.239+02:00 | com.amazonaws.serverless.exceptions.ContainerInitializationException: Could not initialize framework within the 20000ms timeout

.. and no, i have no static block anymore in the StreamLambdaHandler, I only have the constructor with the async initialization.

Expected behavior

  1. I'd expect the sample's pom.xml to have a working aws-serverless-java-container-spring dependency: https://github.com/awslabs/aws-serverless-java-container/blob/main/samples/spring/pet-store/pom.xml
  2. and for the async doc to be updated: https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring
  3. and not see initialization happening twice, which is helping the timeout

Actual behavior

compilation errors described above, timeouts

Steps to reproduce

Provide code samples we can use to reproduce the issue as part of our integration tests. If there is a public repository for the misbehaving application link to it here

The working, but still twice initializing async constructor is:
public StreamLambdaHandler() throws ContainerInitializationException {
System.out.println("StreamLambdaHandler start");
long startTime = Instant.now().toEpochMilli();
handler = new SpringProxyHandlerBuilder()
.defaultProxy()
.asyncInit(startTime)
.configurationClasses(PetStoreSpringAppConfig.class)
.buildAndInitialize();
System.out.println("StreamLambdaHandler end");
}

and the dependency i'm now using is:

com.amazonaws.serverless
aws-serverless-java-container-spring
2.0.0-M2

Full log output

see above

logs

Thanks for raising the issue. We are looking into it.

@melinaschweizer, SpringProxyHandlerBuilder.asyncInit(startTime) is deprecated in favor of the parameters-less one asyncInit(). Please try

handler = new SpringProxyHandlerBuilder()
                .defaultProxy()
                .asyncInit()
                .configurationClasses(PetStoreSpringAppConfig.class)
                .buildAndInitialize();

and let me know if you encounter any issue.

deki commented

Doc should be updated similar to https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot3#asynchronous-initialization - AWS_SERVERLESS_JAVA_CONTAINER_INIT_GRACE_TIME and AWS_SERVERLESS_JAVA_CONTAINER_MAX_INIT_TIMEOUT may help here as well.

deki commented

@melinaschweizer please retry with our latest release 2.0.0 where asyncInit is now enabled by default. The documentation has been adapted as well.