-
Notifications
You must be signed in to change notification settings - Fork 917
Internal classes and RequestBatchManager Impelementation #5418
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
joviegas
merged 19 commits into
feature/master/sqs-batch-manager
from
joviegas/batchManager-internalclaases
Aug 6, 2024
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
615d6c0
Added Internal classes required for BatchManager Implementation
joviegas 1a3b8e1
Added Batch Send Implementation
joviegas b0f3136
Handled review comments
joviegas 2d46514
Handled review comments
joviegas dbb085a
Handled review comments
joviegas 960eef8
Made RequestBatchManager class a Abstract class
joviegas efad445
Checkstyle issues
joviegas 863e00d
Removed unused methods
joviegas d836642
New lines removed
joviegas 7bfc706
Made public static to private state for sqsBatch functions
joviegas 98eda34
Constants added
joviegas 976b40d
Sonar cloud issues fixed
joviegas af4cfb1
commit to check why test on codebuild
joviegas 56ca6be
Increased Timeouts for get
joviegas 450a40e
Added abstract methods
joviegas 10dde26
Handled comments to remove Builders
joviegas 225ed37
Handled comments to take care when batchmanager closed while pending …
joviegas e02b6b7
Handled comments
joviegas 1cdd4f9
Checkstyle issue
joviegas 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
32 changes: 32 additions & 0 deletions
32
...src/main/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchAndSend.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,32 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
/** | ||
* Takes a list of identified requests in addition to a batchKey and batches the requests into a batch request. | ||
* It then sends the batch request and returns a CompletableFuture of the response. | ||
* @param <RequestT> the type of an outgoing request. | ||
* @param <BatchResponseT> the type of an outgoing batch response. | ||
*/ | ||
@FunctionalInterface | ||
@SdkInternalApi | ||
public interface BatchAndSend<RequestT, BatchResponseT> { | ||
CompletableFuture<BatchResponseT> batchAndSend(List<IdentifiableMessage<RequestT>> identifiedRequests, String batchKey); | ||
} |
60 changes: 60 additions & 0 deletions
60
...in/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchConfiguration.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,60 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.services.sqs.batchmanager.BatchOverrideConfiguration; | ||
|
||
@SdkInternalApi | ||
public final class BatchConfiguration { | ||
|
||
public static final int DEFAULT_MAX_BATCH_ITEMS = 10; | ||
public static final int DEFAULT_MAX_BATCH_KEYS = 100; | ||
public static final int DEFAULT_MAX_BUFFER_SIZE = 500; | ||
public static final Duration DEFAULT_MAX_BATCH_OPEN_IN_MS = Duration.ofMillis(200); | ||
|
||
private final Integer maxBatchItems; | ||
private final Integer maxBatchKeys; | ||
private final Integer maxBufferSize; | ||
private final Duration maxBatchOpenInMs; | ||
|
||
public BatchConfiguration(BatchOverrideConfiguration overrideConfiguration) { | ||
Optional<BatchOverrideConfiguration> configuration = Optional.ofNullable(overrideConfiguration); | ||
this.maxBatchItems = configuration.flatMap(BatchOverrideConfiguration::maxBatchItems).orElse(DEFAULT_MAX_BATCH_ITEMS); | ||
this.maxBatchKeys = configuration.flatMap(BatchOverrideConfiguration::maxBatchKeys).orElse(DEFAULT_MAX_BATCH_KEYS); | ||
this.maxBufferSize = configuration.flatMap(BatchOverrideConfiguration::maxBufferSize).orElse(DEFAULT_MAX_BUFFER_SIZE); | ||
this.maxBatchOpenInMs = configuration.flatMap(BatchOverrideConfiguration::maxBatchOpenInMs) | ||
.orElse(DEFAULT_MAX_BATCH_OPEN_IN_MS); | ||
} | ||
|
||
public Duration maxBatchOpenInMs() { | ||
return maxBatchOpenInMs; | ||
} | ||
|
||
public int maxBatchItems() { | ||
return maxBatchItems; | ||
} | ||
|
||
public int maxBatchKeys() { | ||
return maxBatchKeys; | ||
} | ||
|
||
public int maxBufferSize() { | ||
return maxBufferSize; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...c/main/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchKeyMapper.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,28 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
/** | ||
* Takes a request and extracts a batchKey as determined by the caller. | ||
* @param <RequestT> the request. | ||
*/ | ||
@FunctionalInterface | ||
@SdkInternalApi | ||
public interface BatchKeyMapper<RequestT> { | ||
String getBatchKey(RequestT request); | ||
} |
75 changes: 75 additions & 0 deletions
75
...n/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchManagerBuilder.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,75 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.function.Consumer; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.services.sqs.batchmanager.BatchOverrideConfiguration; | ||
|
||
@SdkInternalApi | ||
public interface BatchManagerBuilder<RequestT, ResponseT, BatchResponseT, B> { | ||
|
||
/** | ||
* Defines overrides to the default BatchManager configuration that should be used. | ||
* | ||
* @param overrideConfiguration the override configuration. | ||
* @return a reference to this object so that method calls can be chained together. | ||
*/ | ||
B overrideConfiguration(BatchOverrideConfiguration overrideConfiguration); | ||
|
||
|
||
B overrideConfiguration(Consumer<BatchOverrideConfiguration.Builder> overrideConfigurationConsumer); | ||
|
||
|
||
/** | ||
* Adds a {@link ScheduledExecutorService} to be used by the BatchManager to schedule periodic flushes of the underlying | ||
* buffers. | ||
* <p> | ||
* Creating a BatchManager directly from a service client will use the service client's scheduled executor. If supplied by | ||
* the user, this ScheduledExecutorService must be closed by the caller when it is ready to be shut down. | ||
* | ||
* @param scheduledExecutor the provided scheduled executor. | ||
* @return a reference to this object so that method calls can be chained together. | ||
*/ | ||
B scheduledExecutor(ScheduledExecutorService scheduledExecutor); | ||
|
||
/** | ||
* Adds a function that defines how requests should be batched together into the appropriate batch response. | ||
* | ||
* @param batchFunction the provided function. | ||
* @return a reference to this object so that method calls can be chained together. | ||
*/ | ||
B batchFunction(BatchAndSend<RequestT, BatchResponseT> batchFunction); | ||
|
||
/** | ||
* Adds a function that defines how a batch response should be extracted and transformed into its individual responses. | ||
* | ||
* @param responseMapper the provided function. | ||
* @return a reference to this object so that method calls can be chained together. | ||
*/ | ||
B responseMapper(BatchResponseMapper<BatchResponseT, ResponseT> responseMapper); | ||
|
||
/** | ||
* Adds a function that calculates an appropriate batchKey from a given request. | ||
* | ||
* @param batchKeyMapper the provided function. | ||
* @return a reference to this object so that method calls can be chained together. | ||
*/ | ||
B batchKeyMapper(BatchKeyMapper<RequestT> batchKeyMapper); | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...n/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchResponseMapper.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,32 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.util.List; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.utils.Either; | ||
|
||
/** | ||
* Unpacks the batch response, then transforms individual entries to the appropriate response type. Each entry's batch ID | ||
* is mapped to the individual response entry. | ||
* @param <BatchResponseT> the type of an outgoing batch response. | ||
* @param <ResponseT> the type of an outgoing response. | ||
*/ | ||
@FunctionalInterface | ||
@SdkInternalApi | ||
public interface BatchResponseMapper<BatchResponseT, ResponseT> { | ||
List<Either<IdentifiableMessage<ResponseT>, IdentifiableMessage<Throwable>>> mapBatchResponse(BatchResponseT batchResponse); | ||
} |
39 changes: 39 additions & 0 deletions
39
...a/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchingExecutionContext.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,39 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
@SdkInternalApi | ||
public final class BatchingExecutionContext<RequestT, ResponseT> { | ||
|
||
private final RequestT request; | ||
private final CompletableFuture<ResponseT> response; | ||
|
||
public BatchingExecutionContext(RequestT request, CompletableFuture<ResponseT> response) { | ||
this.request = request; | ||
this.response = response; | ||
} | ||
|
||
public RequestT request() { | ||
return request; | ||
} | ||
|
||
public CompletableFuture<ResponseT> response() { | ||
return response; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
.../src/main/java/software/amazon/awssdk/services/sqs/internal/batchmanager/BatchingMap.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,89 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.sqs.internal.batchmanager; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
/** | ||
* Outer map maps a batchKey (ex. queueUrl, overrideConfig etc.) to a nested BatchingGroupMap map. | ||
* | ||
* @param <RequestT> the type of an outgoing response | ||
*/ | ||
@SdkInternalApi | ||
public final class BatchingMap<RequestT, ResponseT> { | ||
|
||
private final int maxBatchKeys; | ||
private final int maxBufferSize; | ||
|
||
private final BiFunction<Integer, ScheduledFuture<?>, RequestBatchBuffer<RequestT, ResponseT>> bufferSupplier; | ||
private final Map<String, RequestBatchBuffer<RequestT, ResponseT>> batchContextMap; | ||
|
||
public BatchingMap(int maxBatchKeys, int maxBufferSize, | ||
BiFunction<Integer, ScheduledFuture<?>, RequestBatchBuffer<RequestT, ResponseT>> bufferSupplier) { | ||
this.batchContextMap = new ConcurrentHashMap<>(); | ||
this.maxBatchKeys = maxBatchKeys; | ||
this.maxBufferSize = maxBufferSize; | ||
this.bufferSupplier = bufferSupplier; | ||
} | ||
|
||
public void put(String batchKey, Supplier<ScheduledFuture<?>> scheduleFlush, RequestT request, | ||
CompletableFuture<ResponseT> response) throws IllegalStateException { | ||
batchContextMap.computeIfAbsent(batchKey, k -> { | ||
if (batchContextMap.size() == maxBatchKeys) { | ||
throw new IllegalStateException("Reached MaxBatchKeys of: " + maxBatchKeys); | ||
} | ||
return bufferSupplier.apply(maxBufferSize, scheduleFlush.get()); | ||
}).put(request, response); | ||
} | ||
|
||
public void putScheduledFlush(String batchKey, ScheduledFuture<?> scheduledFlush) { | ||
batchContextMap.get(batchKey).putScheduledFlush(scheduledFlush); | ||
} | ||
|
||
public void forEach(BiConsumer<String, RequestBatchBuffer<RequestT, ResponseT>> action) { | ||
batchContextMap.forEach(action); | ||
} | ||
|
||
public Map<String, BatchingExecutionContext<RequestT, ResponseT>> flushableRequests(String batchKey, | ||
int maxBatchItems) { | ||
return batchContextMap.get(batchKey).flushableRequests(maxBatchItems); | ||
} | ||
|
||
public Map<String, BatchingExecutionContext<RequestT, ResponseT>> flushableScheduledRequests(String batchKey, | ||
int maxBatchItems) { | ||
return batchContextMap.get(batchKey).flushableScheduledRequests(maxBatchItems); | ||
} | ||
|
||
public void cancelScheduledFlush(String batchKey) { | ||
batchContextMap.get(batchKey).cancelScheduledFlush(); | ||
} | ||
|
||
public void clear() { | ||
for (Map.Entry<String, RequestBatchBuffer<RequestT, ResponseT>> entry : batchContextMap.entrySet()) { | ||
String key = entry.getKey(); | ||
entry.getValue().clear(); | ||
batchContextMap.remove(key); | ||
} | ||
batchContextMap.clear(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.