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

Lambda Snapstart database connection #31401

Open
RickVerkuijlen opened this issue Feb 24, 2023 · 7 comments
Open

Lambda Snapstart database connection #31401

RickVerkuijlen opened this issue Feb 24, 2023 · 7 comments
Labels
area/amazon-lambda kind/bug Something isn't working

Comments

@RickVerkuijlen
Copy link

Describe the bug

Hi all,
Recently in one of our projects, we introduced Lambda Snapstart with the help of the Quarkus Snapstart optimisation. The problem is that, with using Hibernate ORM together with Panache, I couldn't find a way to re-establish the database connection for my entities. Snapstart stores the state of my Lambda, with the database connection. This connection goes stale after some time. When using the state created by Snapstart, the connection is used again resulting in connection problems. After a few seconds, when the connection is re-established, I can do another request and everything works as expected.

Without Snapstart, it takes around 10 seconds to start up the Lambda and finish the request. With Snapstart, this is reduced to 2 seconds, but the first connection with the database cannot be made. This wasn't a problem without Snapstart.

Any tips?

Expected behavior

Executing calls from a Snapstart state to a database for the first time.

Actual behavior

A stale database connection which fails the first time, but succeeds after a few seconds of being warm.

How to Reproduce?

  1. Enable Snapstart on a Lambda that makes a call do a database
  2. Use the Snapstart state on next request
  3. First request should fail and after a few seconds, requests are succeeding

Output of uname -a or ver

No response

Output of java -version

openjdk version "17.0.5" 2022-10-18

GraalVM version (if different from Java)

No response

Quarkus version or git rev

2.16.3.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)

Additional information

No response

@RickVerkuijlen RickVerkuijlen added the kind/bug Something isn't working label Feb 24, 2023
@quarkus-bot
Copy link

quarkus-bot bot commented Feb 24, 2023

/cc @matejvasek (amazon-lambda), @patriot1burke (amazon-lambda)

@codecr
Copy link

codecr commented Mar 3, 2023

Hi!

I'm also having the same problem. Any tips will be appreciated.

@patriot1burke
Copy link
Contributor

patriot1burke commented Mar 3, 2023

Quarkus integration with SnapStart does a full quarkus boot during the init phase. So if connection pooling is enabled and a DB interaction is done during startup, then the connections will be stale. You'll also want quarkus.datasource.jdbc.min-size to be zero.

I think you can also register a callback to flush the connection pool on Snapstart restore:

https://docs.aws.amazon.com/lambda/latest/dg/snapstart-runtime-hooks.html

Try this maybe:

import org.crac.Resource;
import org.crac.Core;

@ApplicationScoped
public class FlushPool implements Resource {

   @Inject
   AgroalDataSource dataSource;

   void onStart(@Observes StartupEvent ev) {    
        Core.getGlobalContext().register(this);           
    }

    @Override
    public void beforeCheckpoint(org.crac.Context<? extends Resource> context)
        throws Exception {
      // noop
    }
    @Override
    public void afterRestore(org.crac.Context<? extends Resource> context)
        throws Exception {
           dataSource.flush()
    }
}

@RickVerkuijlen
Copy link
Author

Yes, this is exactly what solves my issue! Thank you for the support!!

@codecr
Copy link

codecr commented Mar 13, 2023

Thank you! I also confirm the solution.

@patriot1burke patriot1burke reopened this Mar 13, 2023
@patriot1burke
Copy link
Contributor

Reopening issue, because IMO, Quarkus should be automatically flushing the pool.

@RickVerkuijlen
Copy link
Author

@patriot1burke Is there any updates on the support from Quarkus?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/amazon-lambda kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants