You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "atrbgithub (via GitHub)" <gi...@apache.org> on 2023/12/08 19:08:37 UTC

[PR] Only perform list if delimiter specified [airflow]

atrbgithub opened a new pull request, #36130:
URL: https://github.com/apache/airflow/pull/36130

   This fixes the issue raised [here](https://github.com/apache/airflow/pull/34919#issuecomment-1846993082) against https://github.com/apache/airflow/pull/34919
   
   When testing locally the list only appears to be required when a delimiter is passed in. 
   
   A sample test can be performed with:
   
   ```python
   from airflow.providers.google.cloud.hooks.gcs import GCSHook
   res = GCSHook().list(
           bucket_name='a-testbucket',
           prefix='a/prefix/in/the/bucket/'
   )
   print(res)
   ```
   
   With this fix, the code no longer fails with:
   
   ```
   ValueError: ('Iterator has already started', <google.api_core.page_iterator.HTTPIterator object at 0x7efe067c3880>)
   ```
   
   To confirm that this block:
   
   ```
                   if delimiter:
                       list(blobs)
   ```
   
   Is still required, when this is removed, and this is ran:
   ```
   res = GCSHook().list(
           bucket_name='a-testbucket',
           prefix='a/prefix/in/the/bucket/',
           delimiter='.csv'
   )
   print(res)
   ```
   
   No results are returned. Everything works as expected however once the block is in place. 
   
   
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+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 a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #36130:
URL: https://github.com/apache/airflow/pull/36130#discussion_r1421434993


##########
airflow/providers/google/cloud/hooks/gcs.py:
##########
@@ -821,7 +821,8 @@ def _list(
                     delimiter=delimiter,
                     versions=versions,
                 )
-                list(blobs)
+                if delimiter:
+                    list(blobs)

Review Comment:
   IMHO, there is a problem with the original PR (#34919) that added this statement, where we create the generators to avoid loading all the data in the memory, but by applying `list` on the blobs generator, it looks like you are forcing the generator to loop over all the pages and load them in memory.
   
   Instead, the solution should be based on a loop on the generator elements similar to what we do here:
   https://github.com/apache/airflow/blob/5d74ffb32095d534866f029d085198bc783d82c2/airflow/providers/google/cloud/hooks/gcs.py#L941-L945



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub closed pull request #36130: GCSHook only perform list if delimiter specified
URL: https://github.com/apache/airflow/pull/36130


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub commented on PR #36130:
URL: https://github.com/apache/airflow/pull/36130#issuecomment-1850004352

   The old existing code before this file was refactor seems to work fine for `match_glob` and `delimiter`, perhaps we just revert to that?
   
   https://github.com/apache/airflow/blob/907909329195c6655d1e2989b05609466ef50563/airflow/providers/google/cloud/hooks/gcs.py#L828-L838


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Only perform list if delimiter specified [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 commented on PR #36130:
URL: https://github.com/apache/airflow/pull/36130#issuecomment-1847709408

   Good catch! 
   If it's not too hard, could you try adding tests for both conditions? (e.g., by patching `builtins.list` function)
   I wouldn't be too strict about it - but if it's possible, it would be quite nice to have (for example, if we make any changes to the `delimiter` param in the future).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub commented on PR #36130:
URL: https://github.com/apache/airflow/pull/36130#issuecomment-1847691555

   @eladkal @shahar1 Would you be able to take a look at this if you get a chance?
   
   Without this, https://github.com/apache/airflow/pull/34919 should not be released (https://github.com/apache/airflow/issues/36117)


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub commented on code in PR #36130:
URL: https://github.com/apache/airflow/pull/36130#discussion_r1425238050


##########
airflow/providers/google/cloud/hooks/gcs.py:
##########
@@ -821,7 +821,8 @@ def _list(
                     delimiter=delimiter,
                     versions=versions,
                 )
-                list(blobs)
+                if delimiter:
+                    list(blobs)

Review Comment:
   When a delimiter is used, the iterator must be started so that `blobs.prefixes` is populated, which is how the blob names are then captured. 
   
   Whereas, when `match_glob` is used, `blobs.prefixes` is never populated and instead you must iterate over blobs and access each `blob.name`. 
   
   I'm not sure why this is the case. The original version of the code started the iterator by using a for loop to iterate over blobs no matter whether a delimiter had been passed in or not. 
   
   https://github.com/apache/airflow/blob/907909329195c6655d1e2989b05609466ef50563/airflow/providers/google/cloud/hooks/gcs.py#L828-L838
   
   When delimiter is used, if you modify the code to be the following:
   
   ```
   for blob in blobs:
        print("here")
        blob_names.append(blob.name) 
   ```
   
   Nothing is printed. Simply by starting the iterator `blobs.prefixes` is populated. 
   
   The code has been modified so rather than using a `list(`, it will use `next(`. I don't think it will make any difference since blobs appears to be an empty list. 
   
   The code has been tested against a bucket with over 100k files, the same number of files is returned when either `delimiter` or `match_glob` is passed in. 
   
   You also make an interesting point with referencing the code in `list_by_timespan`, I don't believe that this method would have ever filtered the files based on timespan when delimiter has been passed in:
   
   https://github.com/apache/airflow/blob/5d74ffb32095d534866f029d085198bc783d82c2/airflow/providers/google/cloud/hooks/gcs.py#L938-L945
   
   It would simply add all the prefixes without doing any filtering. 
   



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub commented on PR #36130:
URL: https://github.com/apache/airflow/pull/36130#issuecomment-1853789035

   I think we should close this in favour of https://github.com/apache/airflow/pull/36202
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GCSHook only perform list if delimiter specified [airflow]

Posted by "atrbgithub (via GitHub)" <gi...@apache.org>.
atrbgithub commented on PR #36130:
URL: https://github.com/apache/airflow/pull/36130#issuecomment-1849574212

   @hussein-awala I agree I think that would be better. 
   
   I found an issue where this would fail if a prefix is passed in which does not exist, it would throw the `Iterator has already started` exception. To fix that, and iterate over the generator rather than listing all objects into memory, how does something like this look?
   
   ```python
               else:
                   blobs = bucket.list_blobs(
                       max_results=max_results,
                       page_token=page_token,
                       prefix=prefix,
                       delimiter=delimiter,
                       versions=versions,
                   )
                   if delimiter:
                       blobs_iter = next(blobs, None)
   
               if blobs.prefixes:
                   ids.extend(blobs.prefixes)
               else:
                   if(delimiter):
                       if(blobs_iter):
                           ids.extend(blob.name for blob in blobs_iter)
                   else:
                       ids.extend(blob.name for blob in blobs)
   
               page_token = blobs.next_page_token
               if page_token is None:
                   # empty next page token
                   break
           return ids
   ```
   
   I don't understand why the google storage client is behaving differently when a delimiter is used, and when results exist. The issue seems to be stemming from that. 
   
   The above snippet however is able to list objects when a delimiter is passed in, handles prefixes with no objects and avoids using a `list`. The correct number of objects are also returned. 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org