Best2Two commented on code in PR #10497:
URL: https://github.com/apache/seatunnel/pull/10497#discussion_r2867061798
##########
seatunnel-connectors-v2/connector-amazondynamodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/amazondynamodb/sink/DynamoDbSinkClient.java:
##########
@@ -64,34 +74,111 @@ private void tryInit() {
initialize = true;
}
- public synchronized void write(PutItemRequest putItemRequest) {
- tryInit();
- batchList.add(
- WriteRequest.builder()
-
.putRequest(PutRequest.builder().item(putItemRequest.item()).build())
- .build());
- if (amazondynamodbConfig.getBatchSize() > 0
- && batchList.size() >= amazondynamodbConfig.getBatchSize()) {
- flush();
+ public void write(PutItemRequest putItemRequest, String tableName) {
+ List<WriteRequest> toFlush = null;
+
+ synchronized (lock) {
+ tryInit();
+
+ batchListByTable.computeIfAbsent(tableName, k -> new
ArrayList<>());
+ batchListByTable
+ .get(tableName)
+ .add(
+ WriteRequest.builder()
+ .putRequest(
+ PutRequest.builder()
+
.item(putItemRequest.item())
+ .build())
+ .build());
+
+ if (amazondynamodbConfig.getBatchSize() > 0
+ && batchListByTable.get(tableName).size()
+ >= amazondynamodbConfig.getBatchSize()) {
+ // Copy batch and remove from map inside lock (fast)
+ toFlush = new ArrayList<>(batchListByTable.get(tableName));
+ batchListByTable.remove(tableName);
+ }
+ }
+
+ // Execute network I/O outside lock (other threads can continue)
+ if (toFlush != null) {
+ flushTable(tableName, toFlush);
}
}
- public synchronized void close() {
- if (dynamoDbClient != null) {
- flush();
- dynamoDbClient.close();
+ public void close() {
+ flush();
+ synchronized (lock) {
+ if (dynamoDbClient != null) {
+ dynamoDbClient.close();
+ }
}
}
- synchronized void flush() {
- if (batchList.isEmpty()) {
- return;
+ void flush() {
+ Map<String, List<WriteRequest>> batchToFlush = new HashMap<>();
+
+ synchronized (lock) {
+ if (batchListByTable.isEmpty()) {
+ return;
+ }
+ batchToFlush.putAll(batchListByTable);
+ batchListByTable.clear();
+ }
+
+ for (Map.Entry<String, List<WriteRequest>> entry :
batchToFlush.entrySet()) {
+ flushTable(entry.getKey(), entry.getValue());
+ }
+ }
+
+ private void flushTable(String tableName, List<WriteRequest> requests) {
+ if (!requests.isEmpty()) {
+ flushWithRetry(tableName, requests);
+ }
+ }
+
+ private void flushWithRetry(String tableName, List<WriteRequest> requests)
{
+ List<WriteRequest> pendingRequests = new ArrayList<>(requests);
+
+ int maxRetries = amazondynamodbConfig.getMaxRetries();
+ long baseDelayMs = amazondynamodbConfig.getRetryBaseDelayMs();
+ long maxDelayMs = amazondynamodbConfig.getRetryMaxDelayMs();
+
+ int retryCount = 0;
+
+ while (!pendingRequests.isEmpty() && retryCount < maxRetries) {
+ Map<String, List<WriteRequest>> requestItems = new HashMap<>(1);
+ requestItems.put(tableName, pendingRequests);
+
+ BatchWriteItemResponse response =
+ dynamoDbClient.batchWriteItem(
+
BatchWriteItemRequest.builder().requestItems(requestItems).build());
+
+ Map<String, List<WriteRequest>> unprocessedKeys =
response.unprocessedItems();
+ pendingRequests = unprocessedKeys.getOrDefault(tableName, new
ArrayList<>());
+
+ if (!pendingRequests.isEmpty()) {
+ retryCount++;
+
+ long delay = Math.min(baseDelayMs * (1L << retryCount),
maxDelayMs);
+
+ long jitter = (long) (delay * Math.random() * 0.5);
+ delay += jitter;
+
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]