-
Notifications
You must be signed in to change notification settings - Fork 11
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
Issue 3689 - Lambda code for compaction job dispatch #3749
Merged
patchwork01
merged 12 commits into
develop
from
3689-lambda-code-for-compaction-job-dispatch
Nov 21, 2024
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
67de5e0
Refactor expiryTime to createTime
rtjd6554 1dd930f
Delete unused LocalStack service declaration
patchwork01 98707f2
Set up CompactionJobsDispatchLambdaIT
patchwork01 cb3317a
Adjust CompactionJobDispatchLambda.dispatcher
patchwork01 3a41f30
Provide functionality for the readBatch and SendJob
rtjd6554 56c764e
Add functionality to return to pending queue and associated newproperty
rtjd6554 6ae5da3
Rename test class to match production class
patchwork01 e019a85
Add lambda handler in CompactionJobDispatchLambda
patchwork01 9712e4f
Refactor CompactionJobDispatchLambda.returnToQueue
patchwork01 0ea7dcd
Inline expiry time calculation in CompactionJobDispatcher
patchwork01 276210a
Merge branch 'develop' into 3689-lambda-code-for-compaction-job-dispatch
patchwork01 8535987
Add Javadoc to CompactionJobDispatchLambda
patchwork01 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 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 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 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
4 changes: 2 additions & 2 deletions
4
...spatch/CompactionJobDispatchRequestSerDeTest.shouldConvertRequestToFromJson.approved.json
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"tableId": "test-table", | ||
"batchKey": "s3a://test-bucket/test-table/compactions/test-batch.json", | ||
"expiryTime": 1731931350000 | ||
} | ||
"createTime": 1731931260000 | ||
} |
This file contains 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
98 changes: 98 additions & 0 deletions
98
...bda/src/main/java/sleeper/compaction/job/creation/lambda/CompactionJobDispatchLambda.java
This file contains 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,98 @@ | ||
/* | ||
* 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.compaction.job.creation.lambda; | ||
|
||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | ||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.amazonaws.services.sqs.AmazonSQS; | ||
import com.amazonaws.services.sqs.AmazonSQSClientBuilder; | ||
import com.amazonaws.services.sqs.model.SendMessageRequest; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
import sleeper.compaction.core.job.CompactionJobSerDe; | ||
import sleeper.compaction.core.job.dispatch.CompactionJobDispatchRequestSerDe; | ||
import sleeper.compaction.core.job.dispatch.CompactionJobDispatcher; | ||
import sleeper.compaction.core.job.dispatch.CompactionJobDispatcher.ReadBatch; | ||
import sleeper.compaction.core.job.dispatch.CompactionJobDispatcher.ReturnRequestToPendingQueue; | ||
import sleeper.compaction.core.job.dispatch.CompactionJobDispatcher.SendJob; | ||
import sleeper.configuration.properties.S3InstanceProperties; | ||
import sleeper.configuration.properties.S3TableProperties; | ||
import sleeper.core.properties.instance.InstanceProperties; | ||
import sleeper.parquet.utils.HadoopConfigurationProvider; | ||
import sleeper.statestore.StateStoreFactory; | ||
|
||
import java.time.Instant; | ||
import java.util.function.Supplier; | ||
|
||
import static sleeper.core.properties.instance.CdkDefinedInstanceProperty.COMPACTION_JOB_QUEUE_URL; | ||
import static sleeper.core.properties.instance.CdkDefinedInstanceProperty.COMPACTION_PENDING_QUEUE_URL; | ||
import static sleeper.core.properties.instance.CdkDefinedInstanceProperty.CONFIG_BUCKET; | ||
|
||
public class CompactionJobDispatchLambda implements RequestHandler<SQSEvent, Void> { | ||
|
||
private final CompactionJobDispatcher dispatcher; | ||
private final CompactionJobDispatchRequestSerDe serDe = new CompactionJobDispatchRequestSerDe(); | ||
|
||
private CompactionJobDispatchLambda() { | ||
AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); | ||
AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.defaultClient(); | ||
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); | ||
String configBucket = System.getenv(CONFIG_BUCKET.toEnvironmentVariable()); | ||
InstanceProperties instanceProperties = S3InstanceProperties.loadFromBucket(s3, configBucket); | ||
Configuration conf = HadoopConfigurationProvider.getConfigurationForLambdas(instanceProperties); | ||
dispatcher = dispatcher(s3, dynamoDB, sqs, conf, instanceProperties, Instant::now); | ||
} | ||
|
||
@Override | ||
public Void handleRequest(SQSEvent event, Context context) { | ||
event.getRecords().forEach(message -> dispatcher.dispatch(serDe.fromJson(message.getBody()))); | ||
return null; | ||
} | ||
|
||
public static CompactionJobDispatcher dispatcher( | ||
AmazonS3 s3, AmazonDynamoDB dynamoDB, AmazonSQS sqs, Configuration conf, InstanceProperties instanceProperties, Supplier<Instant> timeSupplier) { | ||
CompactionJobSerDe compactionJobSerDe = new CompactionJobSerDe(); | ||
return new CompactionJobDispatcher(instanceProperties, | ||
S3TableProperties.createProvider(instanceProperties, s3, dynamoDB), | ||
StateStoreFactory.createProvider(instanceProperties, s3, dynamoDB, conf), | ||
readBatch(s3, compactionJobSerDe), | ||
sendJob(instanceProperties, sqs, compactionJobSerDe), | ||
returnToQueue(instanceProperties, sqs), timeSupplier); | ||
} | ||
|
||
private static SendJob sendJob(InstanceProperties instanceProperties, AmazonSQS sqs, CompactionJobSerDe compactionJobSerDe) { | ||
return compactionJob -> sqs.sendMessage( | ||
instanceProperties.get(COMPACTION_JOB_QUEUE_URL), | ||
compactionJobSerDe.toJson(compactionJob)); | ||
} | ||
|
||
private static ReadBatch readBatch(AmazonS3 s3, CompactionJobSerDe compactionJobSerDe) { | ||
return (bucketName, key) -> compactionJobSerDe.batchFromJson(s3.getObjectAsString(bucketName, key)); | ||
} | ||
|
||
private static ReturnRequestToPendingQueue returnToQueue(InstanceProperties instanceProperties, AmazonSQS sqs) { | ||
CompactionJobDispatchRequestSerDe serDe = new CompactionJobDispatchRequestSerDe(); | ||
return (request, delaySeconds) -> sqs.sendMessage(new SendMessageRequest() | ||
.withQueueUrl(instanceProperties.get(COMPACTION_PENDING_QUEUE_URL)) | ||
.withMessageBody(serDe.toJson(request)) | ||
.withDelaySeconds(delaySeconds)); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to have some Javadoc for this class.