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

Deprecate asyncInit & account for other initialization types #659

Merged
merged 2 commits into from
Oct 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@
* seconds has already been used up.
*/
public class AsyncInitializationWrapper extends InitializationWrapper {

private static final int DEFAULT_INIT_GRACE_TIME_MS = 150;
private static final String INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME = "AWS_SERVERLESS_JAVA_CONTAINER_INIT_GRACE_TIME";
private static final String INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME = "AWS_LAMBDA_INITIALIZATION_TYPE";
private static final String INITIALIZATION_TYPE_ON_DEMAND = "on-demand";
private static final String INITIALIZATION_TYPE = System.getenv().getOrDefault(INITIALIZATION_TYPE_ENVIRONMENT_VARIABLE_NAME,INITIALIZATION_TYPE_ON_DEMAND);
private static final boolean ASYNC_INIT_DISABLED = !INITIALIZATION_TYPE.equals(INITIALIZATION_TYPE_ON_DEMAND);
private static final int INIT_GRACE_TIME_MS = Integer.parseInt(System.getenv().getOrDefault(
INIT_GRACE_TIME_ENVIRONMENT_VARIABLE_NAME, Integer.toString(DEFAULT_INIT_GRACE_TIME_MS)));
private static final int LAMBDA_MAX_INIT_TIME_MS = 10_000;
Expand All @@ -48,6 +53,7 @@ public class AsyncInitializationWrapper extends InitializationWrapper {
private final long actualStartTime;
private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class);


/**
* Creates a new instance of the async initializer.
* @param startTime The epoch ms start time of the Lambda function, this should be measured as close as possible to
Expand All @@ -67,6 +73,11 @@ public AsyncInitializationWrapper() {

@Override
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
if(ASYNC_INIT_DISABLED){
log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE);
super.start(handler);
return;
}
initializationLatch = new CountDownLatch(1);
AsyncInitializer initializer = new AsyncInitializer(initializationLatch, handler);
Thread initThread = new Thread(initializer);
Expand Down Expand Up @@ -96,6 +107,9 @@ public long getActualStartTimeMs() {

@Override
public CountDownLatch getInitializationLatch() {
if(ASYNC_INIT_DISABLED){
return super.getInitializationLatch();
}
return initializationLatch;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void validate() throws ContainerInitializationException {
* @return A populated builder
*/
public Builder defaultProxy() {
initializationWrapper(new InitializationWrapper())
initializationWrapper(new AsyncInitializationWrapper())
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsProxyHttpServletRequestReader())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter())
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsProxySecurityContextWriter())
Expand All @@ -108,7 +108,7 @@ public Builder defaultProxy() {
* @return A populated builder
*/
public Builder defaultHttpApiV2Proxy() {
initializationWrapper(new InitializationWrapper())
initializationWrapper(new AsyncInitializationWrapper())
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsHttpApiV2HttpServletRequestReader())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter(true))
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsHttpApiV2SecurityContextWriter())
Expand Down Expand Up @@ -165,7 +165,7 @@ public Builder responseTypeClass(Class<ResponseType> responseType) {
/**
* Uses an async initializer with the given start time to calculate the 10 seconds timeout.
*
* @deprecated As of release 1.5 this method is deprecated in favor of the parameters-less one {@link ServletLambdaContainerHandlerBuilder#asyncInit()}.
* @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand.
* @param actualStartTime An epoch in milliseconds that should be used to calculate the 10 seconds timeout since the start of the application
* @return A builder configured to use the async initializer
*/
Expand All @@ -178,6 +178,7 @@ public Builder asyncInit(long actualStartTime) {
/**
* Uses a new {@link AsyncInitializationWrapper} with the no-parameter constructor that takes the actual JVM
* start time
* @deprecated As of release 2.0.0 this method is deprecated. Initializer is always async if running in on-demand.
* @return A builder configured to use an async initializer
*/
public Builder asyncInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public LambdaHandler() throws ContainerInitializationException {
long startTime = Instant.now().toEpochMilli();
handler = new SpringProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.asyncInit()
.configurationClasses(SlowAppConfig.class)
.buildAndInitialize();
constructorTime = Instant.now().toEpochMilli() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public LambdaHandler() {
System.out.println("startCall: " + startTime);
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.asyncInit()
.springBootApplication(SlowTestApplication.class)
.buildAndInitialize();
constructorTime = Instant.now().toEpochMilli() - startTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class StreamLambdaHandler implements RequestStreamHandler {
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
// For applications that take longer than 10 seconds to start, use the async builder:
// handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
// .defaultProxy()
// .asyncInit()
// .springBootApplication(Application.class)
// .buildAndInitialize();
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ public class StreamLambdaHandler implements RequestStreamHandler {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);

// For applications that take longer than 10 seconds to start, use the async builder:
// handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
// .defaultProxy()
// .asyncInit()
// .springBootApplication(Application.class)
// .buildAndInitialize();

// we use the onStartup method of the handler to register our custom filter
handler.onStartup(servletContext -> {
FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);
Expand Down