Best2Two commented on code in PR #10497:
URL: https://github.com/apache/seatunnel/pull/10497#discussion_r2867055325
##########
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:
In the flush method if no write() happens batchListByTable will be empty so
it will return so NPE won't potentially happen.
It will be safer to put it inside this null guard but this will violate your
previous feedback about doing I/O operations outside locks which was already
flagged earlier as a risk!
So what do you think about that? Thank you for your review though!!
--
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]