Best2Two commented on code in PR #10497:
URL: https://github.com/apache/seatunnel/pull/10497#discussion_r2867056238
##########
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();
Review Comment:
I have a fix for that in my mind which is
```
public void close() {
synchronized (lock) {
if (dynamoDbClient == null) {
return;
}
}
flush();
synchronized (lock) {
dynamoDbClient.close();
}
}
```
--
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]