[ 
https://issues.apache.org/jira/browse/BEAM-10706?focusedWorklogId=476434&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-476434
 ]

ASF GitHub Bot logged work on BEAM-10706:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 31/Aug/20 08:36
            Start Date: 31/Aug/20 08:36
    Worklog Time Spent: 10m 
      Work Description: iemejia commented on a change in pull request #12583:
URL: https://github.com/apache/beam/pull/12583#discussion_r479974236



##########
File path: 
sdks/java/io/amazon-web-services/src/test/java/org/apache/beam/sdk/io/aws/dynamodb/DynamoDBIOTest.java
##########
@@ -186,6 +192,10 @@ public void testRetries() throws Throwable {
 
     final List<WriteRequest> writeRequests = 
DynamoDBIOTestHelper.generateWriteRequests(numOfItems);
 
+    final List<String> overwriteByPKeys = new ArrayList<>();

Review comment:
       nit: `Arrays.asList` will make the code more concise.

##########
File path: 
sdks/java/io/amazon-web-services/src/main/java/org/apache/beam/sdk/io/aws/dynamodb/DynamoDBIO.java
##########
@@ -342,6 +343,8 @@ public boolean test(Throwable throwable) {
 
     abstract @Nullable SerializableFunction<T, KV<String, WriteRequest>> 
getWriteItemMapperFn();
 
+    abstract @Nullable List<String> getOverwriteByPKeys();

Review comment:
       Can you please remove the `@Nullable` here and set an empty collection 
as the default value on the `build()` method. This will simplify the 
validations in the rest of the code and will make it simpler.

##########
File path: 
sdks/java/io/amazon-web-services/src/test/java/org/apache/beam/sdk/io/aws/dynamodb/DynamoDBIOTest.java
##########
@@ -199,7 +209,8 @@ public void testRetries() throws Throwable {
                         writeRequest -> KV.of(tableName, writeRequest))
                 .withRetryConfiguration(
                     DynamoDBIO.RetryConfiguration.create(4, 
Duration.standardSeconds(10)))
-                
.withAwsClientsProvider(AwsClientsProviderMock.of(amazonDynamoDBMock)));
+                
.withAwsClientsProvider(AwsClientsProviderMock.of(amazonDynamoDBMock))
+                .withOverwriteByPKeys(overwriteByPKeys));

Review comment:
       Can we do this in an independent test to not change the original write 
test, e.g. `testDeduplicateWriteItems` and produce some duplicate items that 
test the correct removal of keys behaviour. I am assuming that for the batch 
case the direct runner will batch them together, but I will let a further note 
when I confirm this 100%.

##########
File path: 
sdks/java/io/amazon-web-services/src/main/java/org/apache/beam/sdk/io/aws/dynamodb/DynamoDBIO.java
##########
@@ -450,12 +459,35 @@ public void startBundle(StartBundleContext context) {
       public void processElement(ProcessContext context) throws Exception {
         final KV<String, WriteRequest> writeRequest =
             (KV<String, WriteRequest>) 
spec.getWriteItemMapperFn().apply(context.element());
+        if (spec.getOverwriteByPKeys() != null) {
+          removeDupPKeysRequestsIfAny(writeRequest.getValue());
+        }
         batch.add(writeRequest);
         if (batch.size() >= BATCH_SIZE) {
           flushBatch();
         }
       }
 
+      private void removeDupPKeysRequestsIfAny(WriteRequest request) {

Review comment:
       Can we better restructure this method to instead of removing from the 
batch every time we just skip adding the value if it already is part of the 
batch. It could make the code simpler.

##########
File path: 
sdks/java/io/amazon-web-services/src/main/java/org/apache/beam/sdk/io/aws/dynamodb/DynamoDBIO.java
##########
@@ -450,12 +459,35 @@ public void startBundle(StartBundleContext context) {
       public void processElement(ProcessContext context) throws Exception {
         final KV<String, WriteRequest> writeRequest =
             (KV<String, WriteRequest>) 
spec.getWriteItemMapperFn().apply(context.element());
+        if (spec.getOverwriteByPKeys() != null) {
+          removeDupPKeysRequestsIfAny(writeRequest.getValue());
+        }
         batch.add(writeRequest);
         if (batch.size() >= BATCH_SIZE) {
           flushBatch();
         }
       }
 
+      private void removeDupPKeysRequestsIfAny(WriteRequest request) {
+        Map<String, AttributeValue> pKeyValueNew = extractPkeyValues(request);
+        batch.removeIf(item -> 
extractPkeyValues(item.getValue()).equals(pKeyValueNew));

Review comment:
       I am wondering if this will get the existing key removed too? Maybe 
worth to validate this in the tests.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 476434)
    Time Spent: 1h  (was: 50m)

> DynamoDBIO fail to write to the same key in short consecution
> -------------------------------------------------------------
>
>                 Key: BEAM-10706
>                 URL: https://issues.apache.org/jira/browse/BEAM-10706
>             Project: Beam
>          Issue Type: Bug
>          Components: io-java-aws
>    Affects Versions: 2.23.0
>            Reporter: Dennis Yung
>            Assignee: Dennis Yung
>            Priority: P2
>          Time Spent: 1h
>  Remaining Estimate: 0h
>
> Internally, DynamoDBIO.Write uses the batchWriteItem method from the AWS SDK 
> to sink items. However, there is a limitation in the AWS SDK that a call to 
> batchWriteItem cannot contain duplicate keys.
> Currently DynamoDBIO.Write performs no key deduplication before flushing a 
> batch, which could cause ValidationException: Provided list of item keys 
> contains duplicates, if consecutive updates to a single key is within the 
> batch size (currently hardcoded to be 25). 
> To fix this bug, the batch of write requests need to be deduplicated before 
> being sent to batchRequest.addRequestItemsEntry



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to