11package org .code .javabuilder ;
22
33import static org .code .javabuilder .InternalFacingExceptionTypes .INVALID_INPUT ;
4- import static org .code .protocol .InternalExceptionKey .*;
54import static org .code .protocol .LoggerNames .MAIN_LOGGER ;
65
76import com .amazonaws .client .builder .AwsClientBuilder ;
109import com .amazonaws .services .apigatewaymanagementapi .model .DeleteConnectionRequest ;
1110import com .amazonaws .services .apigatewaymanagementapi .model .GetConnectionRequest ;
1211import com .amazonaws .services .apigatewaymanagementapi .model .GoneException ;
12+ import com .amazonaws .services .dynamodbv2 .AmazonDynamoDB ;
13+ import com .amazonaws .services .dynamodbv2 .AmazonDynamoDBClientBuilder ;
1314import com .amazonaws .services .lambda .runtime .Context ;
1415import com .amazonaws .services .lambda .runtime .RequestHandler ;
1516import com .amazonaws .services .s3 .AmazonS3 ;
2627import java .util .UUID ;
2728import java .util .logging .Handler ;
2829import java .util .logging .Logger ;
30+ import org .code .javabuilder .UnhealthyContainerChecker .ShutdownTrigger ;
2931import org .code .javabuilder .util .LambdaUtils ;
3032import org .code .protocol .*;
3133import org .code .validation .support .UserTestOutputAdapter ;
@@ -47,11 +49,18 @@ public class LambdaRequestHandler implements RequestHandler<Map<String, String>,
4749 private static final String CONTENT_BUCKET_NAME = System .getenv ("CONTENT_BUCKET_NAME" );
4850 private static final String CONTENT_BUCKET_URL = System .getenv ("CONTENT_BUCKET_URL" );
4951 private static final String API_ENDPOINT = System .getenv ("API_ENDPOINT" );
52+ private static final String UNHEALTHY_CONTAINERS_TABLE_NAME =
53+ System .getenv ("UNHEALTHY_CONTAINERS_TABLE_NAME" );
5054
5155 // Creating these clients here rather than in the request handler method allows us to use
5256 // provisioned concurrency to decrease cold boot time by 3-10 seconds, depending on the lambda
5357 private static final AmazonSQS SQS_CLIENT = AmazonSQSClientBuilder .defaultClient ();
5458 private static final AmazonS3 S3_CLIENT = AmazonS3ClientBuilder .standard ().build ();
59+ private static final AmazonDynamoDB DYNAMO_DB_CLIENT =
60+ AmazonDynamoDBClientBuilder .defaultClient ();
61+
62+ // Used to check whether this container has been marked unhealthy.
63+ private final UnhealthyContainerChecker unhealthyContainerChecker ;
5564
5665 // Controls whether the current invocation session has been initialized. This should be reset on
5766 // every invocation.
@@ -71,6 +80,8 @@ public LambdaRequestHandler() {
7180 .withEndpointConfiguration (
7281 new AwsClientBuilder .EndpointConfiguration (API_ENDPOINT , "us-east-1" ))
7382 .build ();
83+ this .unhealthyContainerChecker =
84+ new UnhealthyContainerChecker (DYNAMO_DB_CLIENT , UNHEALTHY_CONTAINERS_TABLE_NAME );
7485 }
7586
7687 /**
@@ -177,6 +188,9 @@ public String handleRequest(Map<String, String> lambdaInput, Context context) {
177188 * creating global objects
178189 */
179190 private void initialize (Map <String , String > lambdaInput , String connectionId , Context context ) {
191+ // Check container health status and exit early if container has been marked unhealthy.
192+ this .shutdownContainerIfUnhealthy (ShutdownTrigger .START );
193+
180194 final boolean canAccessDashboardAssets =
181195 Boolean .parseBoolean (lambdaInput .get ("canAccessDashboardAssets" ));
182196
@@ -320,6 +334,9 @@ private void shutDown(
320334 System .exit (LambdaErrorCodes .LOW_DISK_SPACE_ERROR_CODE );
321335 }
322336
337+ // Check container health status and exit if the container has been marked unhealthy.
338+ this .shutdownContainerIfUnhealthy (ShutdownTrigger .END );
339+
323340 this .isSessionInitialized = false ;
324341 }
325342
@@ -401,4 +418,14 @@ private void verifyApiClient(String connectionId) {
401418 .build ();
402419 }
403420 }
421+
422+ /**
423+ * Checks if this container has been marked unhealthy and if so, forces a shutdown via
424+ * System.exit().
425+ */
426+ private void shutdownContainerIfUnhealthy (ShutdownTrigger trigger ) {
427+ if (this .unhealthyContainerChecker .shouldForceShutdownContainer (LAMBDA_ID , trigger )) {
428+ System .exit (LambdaErrorCodes .UNHEALTHY_CONTAINER_ERROR_CODE );
429+ }
430+ }
404431}
0 commit comments