-
Notifications
You must be signed in to change notification settings - Fork 15
Issue 1759 - Periodically execute lambdas to keep a warm pool ready #1925
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
Merged
ab295382
merged 42 commits into
develop
from
1759-periodically-execute-lambdas-to-keep-a-warm-pool-ready
Apr 2, 2024
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
6060ad9
Adding support to keep lambdas warm
ab295382 d01353b
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 e9c0f54
Revert config back and added new config for the warm lambda stack
ab295382 610775e
Fixing properties test
ab295382 9953e91
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 e3055ae
Removing KeepLambdaWarmStack from default optional stacks, changed t…
ab295382 20f85b1
Merge branch '1759-periodically-execute-lambdas-to-keep-a-warm-pool-r…
ab295382 b9c39bf
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 91d8dc9
Adding support for Sleeper Types in the WarmQueryExecutorLambda
ab295382 e15a0f9
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 a95941d
Adding documentation for the KeepLambdaWarmStack
ab295382 31bf46b
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 1efb9c8
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 974903a
Updating documentation and renaming properties to be consistent.
ab295382 63ed404
Updating the generated instance properties examples
ab295382 af10f45
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 845bb1b
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 ac03a9a
public class WarmQueryExecutorLambdaIT {
ab295382 571bbf1
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 a89e94f
Updating comments to Given / When / Then
ab295382 573fd8a
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 7d5896c
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 08400d1
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 ab178c0
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 d825584
Merged conflict fixes
ab295382 cd57204
Updated assert to be more explicit for WarmQueryExecutorLambdaIT
ab295382 ff23f38
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 ac44bce
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 dce4068
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 38f90dc
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 fd499a1
Checkstyle fixes
ab295382 f2b3650
More checkstyle fixes
ab295382 299ac0b
Changed WarmQueryExecutorLambda to exact range query
ab295382 f8def4d
Updated WarmQueryExecutorLambda to create a range for each row key
ab295382 a743d8f
Adding unittest for WarmQueryExecutorLambda and condensed teh IT tests
ab295382 ac6e397
Merge branch 'develop' of github.com:gchq/sleeper into 1759-periodica…
ab295382 daa9276
Moving the multiple row keys test to be a unit test
ab295382 7444552
Changing the way the expected regions are created for the tests
ab295382 c57d8d0
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 59c6326
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 ff2869d
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 924b12b
Merge branch 'develop' into 1759-periodically-execute-lambdas-to-keep…
ab295382 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
java/cdk/src/main/java/sleeper/cdk/stack/KeepLambdaWarmStack.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022-2024 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sleeper.cdk.stack; | ||
|
||
import software.amazon.awscdk.CfnOutput; | ||
import software.amazon.awscdk.CfnOutputProps; | ||
import software.amazon.awscdk.Duration; | ||
import software.amazon.awscdk.NestedStack; | ||
import software.amazon.awscdk.services.events.Rule; | ||
import software.amazon.awscdk.services.events.Schedule; | ||
import software.amazon.awscdk.services.events.targets.LambdaFunction; | ||
import software.amazon.awscdk.services.lambda.IFunction; | ||
import software.amazon.awscdk.services.lambda.Runtime; | ||
import software.amazon.awscdk.services.s3.Bucket; | ||
import software.amazon.awscdk.services.s3.IBucket; | ||
import software.constructs.Construct; | ||
|
||
import sleeper.cdk.Utils; | ||
import sleeper.cdk.jars.BuiltJar; | ||
import sleeper.cdk.jars.BuiltJars; | ||
import sleeper.cdk.jars.LambdaCode; | ||
import sleeper.configuration.properties.SleeperScheduleRule; | ||
import sleeper.configuration.properties.instance.InstanceProperties; | ||
|
||
import java.util.Collections; | ||
import java.util.Locale; | ||
|
||
import static sleeper.cdk.Utils.createLambdaLogGroup; | ||
import static sleeper.cdk.Utils.shouldDeployPaused; | ||
import static sleeper.configuration.properties.instance.CdkDefinedInstanceProperty.QUERY_WARM_LAMBDA_CLOUDWATCH_RULE; | ||
import static sleeper.configuration.properties.instance.CommonProperty.ID; | ||
import static sleeper.configuration.properties.instance.QueryProperty.QUERY_PROCESSOR_LAMBDA_MEMORY_IN_MB; | ||
import static sleeper.configuration.properties.instance.QueryProperty.QUERY_PROCESSOR_LAMBDA_TIMEOUT_IN_SECONDS; | ||
import static sleeper.configuration.properties.instance.QueryProperty.QUERY_WARM_LAMBDA_EXECUTION_PERIOD_IN_MINUTES; | ||
|
||
/* | ||
* A {@link NestedStack} to handle keeping lambdas warm. This consists of a {@link Rule} that runs periodically triggering | ||
* a lambda {@link Function} to create the queries that are placed on the Query {@link Queue} for processing. | ||
* This will trigger the query lambdas thus keeping them warm. | ||
*/ | ||
public class KeepLambdaWarmStack extends NestedStack { | ||
|
||
public KeepLambdaWarmStack(Construct scope, | ||
String id, | ||
InstanceProperties instanceProperties, | ||
BuiltJars jars, | ||
CoreStacks coreStacks, | ||
QueryQueueStack queryQueueStack) { | ||
super(scope, id); | ||
|
||
String functionName = Utils.truncateTo64Characters(String.join("-", "sleeper", | ||
instanceProperties.get(ID).toLowerCase(Locale.ROOT), "warm-query-executor")); | ||
|
||
IBucket jarsBucket = Bucket.fromBucketName(this, "JarsBucket", jars.bucketName()); | ||
LambdaCode queryJar = jars.lambdaCode(BuiltJar.QUERY, jarsBucket); | ||
|
||
// Keep lambda warm function | ||
IFunction handler = queryJar.buildFunction(this, "WarmQueryExecutorLambda", builder -> builder | ||
.functionName(functionName) | ||
.description("Sends a message to query-executor lambda in order for it to stay warm") | ||
.runtime(Runtime.JAVA_11) | ||
.memorySize(instanceProperties.getInt(QUERY_PROCESSOR_LAMBDA_MEMORY_IN_MB)) | ||
.timeout(Duration.seconds(instanceProperties.getInt(QUERY_PROCESSOR_LAMBDA_TIMEOUT_IN_SECONDS))) | ||
.handler("sleeper.query.lambda.WarmQueryExecutorLambda::handleRequest") | ||
.environment(Utils.createDefaultEnvironment(instanceProperties)) | ||
.reservedConcurrentExecutions(1) | ||
.logGroup(createLambdaLogGroup(this, id + "LogGroup", functionName, instanceProperties))); | ||
|
||
// Cloudwatch rule to trigger this lambda | ||
Rule rule = Rule.Builder | ||
.create(this, "QueryExecutionPeriodicTrigger") | ||
.ruleName(SleeperScheduleRule.QUERY_WARM_LAMBDA.buildRuleName(instanceProperties)) | ||
.description("A rule to periodically trigger the query execution lambda") | ||
.enabled(!shouldDeployPaused(this)) | ||
.schedule(Schedule.rate(Duration.minutes(instanceProperties | ||
.getInt(QUERY_WARM_LAMBDA_EXECUTION_PERIOD_IN_MINUTES)))) | ||
.targets(Collections.singletonList(LambdaFunction.Builder | ||
.create(handler) | ||
.build())) | ||
.build(); | ||
|
||
queryQueueStack.grantSendMessages(handler); | ||
|
||
coreStacks.grantReadInstanceConfig(handler); | ||
coreStacks.grantReadTablesAndData(handler); | ||
|
||
instanceProperties.set(QUERY_WARM_LAMBDA_CLOUDWATCH_RULE, rule.getRuleName()); | ||
CfnOutputProps ruleArn = new CfnOutputProps.Builder() | ||
.value(rule.getRuleArn()) | ||
.exportName(instanceProperties.get(ID) + "-" + "QueryExecutionPeriodicTriggerRuleARN") | ||
.build(); | ||
new CfnOutput(this, "QueryExecutionPeriodicTriggerRuleARN", ruleArn); | ||
|
||
Utils.addStackTagIfSet(this, instanceProperties); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.