You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/02/06 10:34:47 UTC

[GitHub] [airflow] shcherbin opened a new pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

shcherbin opened a new pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375
 
 
   ---
   Issue link: WILL BE INSERTED BY [boring-cyborg](https://github.com/kaxil/boring-cyborg)
   
   Make sure to mark the boxes below before creating PR: [x]
   
   - [x] Description above provides context of the change
   - [x] Commit message/PR title starts with `[AIRFLOW-NNNN]`. AIRFLOW-NNNN = JIRA ID<sup>*</sup>
   - [x] Unit tests coverage for changes (not needed for documentation changes)
   - [x] Commits follow "[How to write a good git commit message](http://chris.beams.io/posts/git-commit/)"
   - [x] Relevant documentation is updated including usage instructions.
   - [x] I will engage committers as explained in [Contribution Workflow Example](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#contribution-workflow-example).
   
   <sup>*</sup> For document-only changes commit message can start with `[AIRFLOW-XXXX]`.
   
   ---
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/master/UPDATING.md).
   Read the [Pull Request Guidelines](https://github.com/apache/airflow/blob/master/CONTRIBUTING.rst#pull-request-guidelines) for more information.
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376953306
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +611,18 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+
+        # We can only send a maximum of 1000 keys per request.
+        # For details see:
+        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.delete_objects
+        for chunk in chunks(keys, chunk_size=1000):
+            response = s3.delete_objects(
+                Bucket=bucket,
+                Delete={"Objects": [{"Key": k} for k in chunk]}
+            )
+            deleted_keys = [x['Key'] for x in response.get("Deleted", [])]
+            self.log.info("Deleted: %s", deleted_keys)
+            if "Errors" in response:
+                errors_keys = [x['Key'] for x in response.get("Errors", [])]
+                raise AirflowException("Errors when deleting: {}".format(errors_keys))
 
 Review comment:
   Done

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376723680
 
 

 ##########
 File path: tests/providers/amazon/aws/hooks/test_s3.py
 ##########
 @@ -0,0 +1,65 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License 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.
+#
+
+import pytest
 
 Review comment:
   Done

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376385616
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +610,15 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
 
 Review comment:
   Can you create a constant and add a comment to explain the reason for this change?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r375771232
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -606,11 +606,22 @@ def delete_objects(self, bucket, keys):
             When ``keys`` is a list, it's supposed to be the list of the
             keys to delete.
         :type keys: str or list
+
+        :return: False on failure to delete at least one batch and True on success.
+        :rtype: bool
         """
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
+            try:
+                s3.delete_objects(
+                    Bucket=bucket,
+                    Delete={"Objects": [{"Key": k} for k in keys[i: i + batch]]}
+                )
+            except ClientError as e:
+                self.log.error(e.response["Error"]["Message"])
+                return False
 
 Review comment:
   I am not sure if we should return False on any `ClientError`. Can you be more specific?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r375772414
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -606,11 +606,22 @@ def delete_objects(self, bucket, keys):
             When ``keys`` is a list, it's supposed to be the list of the
             keys to delete.
         :type keys: str or list
+
+        :return: False on failure to delete at least one batch and True on success.
+        :rtype: bool
         """
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
+            try:
+                s3.delete_objects(
+                    Bucket=bucket,
+                    Delete={"Objects": [{"Key": k} for k in keys[i: i + batch]]}
+                )
+            except ClientError as e:
+                self.log.error(e.response["Error"]["Message"])
+                return False
 
 Review comment:
   And maybe then add a test to validate your `try-except` feature :) It should be a noted.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376398563
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +610,15 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
 
 Review comment:
   I've added a comment and used the chunks method liked you asked. But I'm not going to make a constant out of the batch size, as it's an implementation detail specific only to the `S3Hook.delete_objects` method and will not be re-used elsewhere. 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376398706
 
 

 ##########
 File path: tests/providers/amazon/aws/hooks/test_s3.py
 ##########
 @@ -0,0 +1,65 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License 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.
+#
+
+import pytest
 
 Review comment:
   Sure. 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] mik-laj merged pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
mik-laj merged pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375
 
 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r375785220
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -606,11 +606,22 @@ def delete_objects(self, bucket, keys):
             When ``keys`` is a list, it's supposed to be the list of the
             keys to delete.
         :type keys: str or list
+
+        :return: False on failure to delete at least one batch and True on success.
+        :rtype: bool
         """
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
+            try:
+                s3.delete_objects(
+                    Bucket=bucket,
+                    Delete={"Objects": [{"Key": k} for k in keys[i: i + batch]]}
+                )
+            except ClientError as e:
+                self.log.error(e.response["Error"]["Message"])
+                return False
 
 Review comment:
   I've changed the logic a bit due to failing tests in S3DeleteObjectsOperator.
   ClientError is raised in any technical error (e.g. the specified bucked does not exist or the request is throttled).
   I've also updated 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-585223066
 
 
   @mik-laj, would you please have another look? @feluelle approved the PR.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] codecov-io commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
codecov-io commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-583760267
 
 
   # [Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=h1) Report
   > Merging [#7375](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=desc) into [master](https://codecov.io/gh/apache/airflow/commit/976ec4d7076ffa1ac35eb3050e2169367c7ef96c?src=pr&el=desc) will **decrease** coverage by `0.11%`.
   > The diff coverage is `100%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/airflow/pull/7375/graphs/tree.svg?width=650&token=WdLKlKHOAU&height=150&src=pr)](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=tree)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #7375      +/-   ##
   ==========================================
   - Coverage   86.24%   86.13%   -0.12%     
   ==========================================
     Files         871      871              
     Lines       40627    40660      +33     
   ==========================================
   - Hits        35039    35021      -18     
   - Misses       5588     5639      +51
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=tree) | Coverage Δ | |
   |---|---|---|
   | [airflow/providers/amazon/aws/hooks/s3.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvYW1hem9uL2F3cy9ob29rcy9zMy5weQ==) | `95.85% <100%> (+0.13%)` | :arrow_up: |
   | [...roviders/amazon/aws/operators/s3\_delete\_objects.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvYW1hem9uL2F3cy9vcGVyYXRvcnMvczNfZGVsZXRlX29iamVjdHMucHk=) | `100% <100%> (+9.52%)` | :arrow_up: |
   | [...w/providers/apache/hive/operators/mysql\_to\_hive.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvYXBhY2hlL2hpdmUvb3BlcmF0b3JzL215c3FsX3RvX2hpdmUucHk=) | `35.84% <0%> (-64.16%)` | :arrow_down: |
   | [airflow/kubernetes/volume\_mount.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3ZvbHVtZV9tb3VudC5weQ==) | `44.44% <0%> (-55.56%)` | :arrow_down: |
   | [airflow/kubernetes/volume.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3ZvbHVtZS5weQ==) | `52.94% <0%> (-47.06%)` | :arrow_down: |
   | [airflow/security/kerberos.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9zZWN1cml0eS9rZXJiZXJvcy5weQ==) | `30.43% <0%> (-45.66%)` | :arrow_down: |
   | [airflow/kubernetes/pod\_launcher.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3BvZF9sYXVuY2hlci5weQ==) | `47.18% <0%> (-45.08%)` | :arrow_down: |
   | [airflow/providers/mysql/operators/mysql.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvbXlzcWwvb3BlcmF0b3JzL215c3FsLnB5) | `55% <0%> (-45%)` | :arrow_down: |
   | [airflow/kubernetes/refresh\_config.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3JlZnJlc2hfY29uZmlnLnB5) | `50.98% <0%> (-23.53%)` | :arrow_down: |
   | [...viders/cncf/kubernetes/operators/kubernetes\_pod.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvY25jZi9rdWJlcm5ldGVzL29wZXJhdG9ycy9rdWJlcm5ldGVzX3BvZC5weQ==) | `70.21% <0%> (-23.41%)` | :arrow_down: |
   | ... and [14 more](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree-more) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=continue).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=footer). Last update [976ec4d...ef02138](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] mik-laj commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
mik-laj commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-583781045
 
 
   I am not an AWS expert. I will wait for @feluelle 's review.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r375785220
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -606,11 +606,22 @@ def delete_objects(self, bucket, keys):
             When ``keys`` is a list, it's supposed to be the list of the
             keys to delete.
         :type keys: str or list
+
+        :return: False on failure to delete at least one batch and True on success.
+        :rtype: bool
         """
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
+            try:
+                s3.delete_objects(
+                    Bucket=bucket,
+                    Delete={"Objects": [{"Key": k} for k in keys[i: i + batch]]}
+                )
+            except ClientError as e:
+                self.log.error(e.response["Error"]["Message"])
+                return False
 
 Review comment:
   I've changed the logic a bit due to failing tests in S3DeleteObjectsOperator.
   ClientError is raised in case of any technical error (e.g. the specified bucked does not exist or the request is throttled).
   I've also updated 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376390669
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +610,15 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
 
 Review comment:
   I didn't know that one existed. Thanks! :)

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] potiuk commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
potiuk commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-584101079
 
 
   Please rebase to latest master @shcherbin  - we had a six drama on Travis but it's solved now.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376385213
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +610,15 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
 
 Review comment:
   Why do you think about using airflow.helpers.chunks here?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r375794960
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -606,11 +606,22 @@ def delete_objects(self, bucket, keys):
             When ``keys`` is a list, it's supposed to be the list of the
             keys to delete.
         :type keys: str or list
+
+        :return: False on failure to delete at least one batch and True on success.
+        :rtype: bool
         """
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+        batch = 1000
+        for i in range(0, len(keys), batch):
+            try:
+                s3.delete_objects(
+                    Bucket=bucket,
+                    Delete={"Objects": [{"Key": k} for k in keys[i: i + batch]]}
+                )
+            except ClientError as e:
+                self.log.error(e.response["Error"]["Message"])
+                return False
 
 Review comment:
   Ok, I don't see any other method in the code that deals with such technical issues. As long as the task will fail on any exception, it's fine with me.
   Removing try-except.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] shcherbin commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
shcherbin commented on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-583349147
 
 
   Hi @feluelle, would you mind having another look at this PR?
   I admit I've initiated it a bit too early. I thought it would be a much easier change.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376386374
 
 

 ##########
 File path: tests/providers/amazon/aws/hooks/test_s3.py
 ##########
 @@ -0,0 +1,65 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License 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.
+#
+
+import pytest
 
 Review comment:
   Can you move [this file](https://github.com/apache/airflow/blob/master/tests/hooks/test_s3_hook.py) also?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] codecov-io edited a comment on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
codecov-io edited a comment on issue #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#issuecomment-583760267
 
 
   # [Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=h1) Report
   > Merging [#7375](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=desc) into [master](https://codecov.io/gh/apache/airflow/commit/976ec4d7076ffa1ac35eb3050e2169367c7ef96c?src=pr&el=desc) will **increase** coverage by `0.06%`.
   > The diff coverage is `100%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/airflow/pull/7375/graphs/tree.svg?width=650&token=WdLKlKHOAU&height=150&src=pr)](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=tree)
   
   ```diff
   @@            Coverage Diff            @@
   ##           master   #7375      +/-   ##
   =========================================
   + Coverage   86.24%   86.3%   +0.06%     
   =========================================
     Files         871     871              
     Lines       40627   40660      +33     
   =========================================
   + Hits        35039   35093      +54     
   + Misses       5588    5567      -21
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=tree) | Coverage Δ | |
   |---|---|---|
   | [airflow/providers/amazon/aws/hooks/s3.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvYW1hem9uL2F3cy9ob29rcy9zMy5weQ==) | `95.85% <100%> (+0.13%)` | :arrow_up: |
   | [...roviders/amazon/aws/operators/s3\_delete\_objects.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvYW1hem9uL2F3cy9vcGVyYXRvcnMvczNfZGVsZXRlX29iamVjdHMucHk=) | `100% <100%> (+9.52%)` | :arrow_up: |
   | [airflow/kubernetes/volume\_mount.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3ZvbHVtZV9tb3VudC5weQ==) | `44.44% <0%> (-55.56%)` | :arrow_down: |
   | [airflow/kubernetes/volume.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3ZvbHVtZS5weQ==) | `52.94% <0%> (-47.06%)` | :arrow_down: |
   | [airflow/kubernetes/pod\_launcher.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3BvZF9sYXVuY2hlci5weQ==) | `47.18% <0%> (-45.08%)` | :arrow_down: |
   | [airflow/kubernetes/refresh\_config.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9rdWJlcm5ldGVzL3JlZnJlc2hfY29uZmlnLnB5) | `50.98% <0%> (-23.53%)` | :arrow_down: |
   | [...viders/cncf/kubernetes/operators/kubernetes\_pod.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9wcm92aWRlcnMvY25jZi9rdWJlcm5ldGVzL29wZXJhdG9ycy9rdWJlcm5ldGVzX3BvZC5weQ==) | `70.21% <0%> (-23.41%)` | :arrow_down: |
   | [airflow/api/common/experimental/\_\_init\_\_.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy9hcGkvY29tbW9uL2V4cGVyaW1lbnRhbC9fX2luaXRfXy5weQ==) | `92.59% <0%> (-7.41%)` | :arrow_down: |
   | [airflow/www/views.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy93d3cvdmlld3MucHk=) | `76.22% <0%> (+0.14%)` | :arrow_up: |
   | [airflow/utils/dag\_processing.py](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree#diff-YWlyZmxvdy91dGlscy9kYWdfcHJvY2Vzc2luZy5weQ==) | `88.12% <0%> (+0.19%)` | :arrow_up: |
   | ... and [10 more](https://codecov.io/gh/apache/airflow/pull/7375/diff?src=pr&el=tree-more) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=continue).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=footer). Last update [976ec4d...ef02138](https://codecov.io/gh/apache/airflow/pull/7375?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [airflow] feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method

Posted by GitBox <gi...@apache.org>.
feluelle commented on a change in pull request #7375: [AIRFLOW-5231] Fix S3Hook.delete_objects method
URL: https://github.com/apache/airflow/pull/7375#discussion_r376772484
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/s3.py
 ##########
 @@ -610,7 +611,18 @@ def delete_objects(self, bucket, keys):
         if isinstance(keys, str):
             keys = [keys]
 
-        delete_dict = {"Objects": [{"Key": k} for k in keys]}
-        response = self.get_conn().delete_objects(Bucket=bucket, Delete=delete_dict)
-
-        return response
+        s3 = self.get_conn()
+
+        # We can only send a maximum of 1000 keys per request.
+        # For details see:
+        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.delete_objects
+        for chunk in chunks(keys, chunk_size=1000):
+            response = s3.delete_objects(
+                Bucket=bucket,
+                Delete={"Objects": [{"Key": k} for k in chunk]}
+            )
+            deleted_keys = [x['Key'] for x in response.get("Deleted", [])]
+            self.log.info("Deleted: %s", deleted_keys)
+            if "Errors" in response:
+                errors_keys = [x['Key'] for x in response.get("Errors", [])]
+                raise AirflowException("Errors when deleting: {}".format(errors_keys))
 
 Review comment:
   LGTM, but please mention in the Updating.md that you removed the return statement so that people are aware of this when updating :)
   
   Some users might have implemented their own operator based on this `delete_objects` method.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services