You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "eilon246810 (via GitHub)" <gi...@apache.org> on 2024/10/15 16:01:28 UTC

[PR] added MultipleFilesWebHdfsSensor [airflow]

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

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   I added `MultipleFilesWebHdfsSensor` class in `providers.apache.hdfs.sensors.web_hdfs`.
   
   The current existing `WebHdfsSensor` can check if one file exists, which requires many tasks to check many files (in my org we had 350+ sensors for a single DAG).
   
   The new `MultipleFilesWebHdfsSensor` can list a whole directory and succeeds only when all the expected files landed in the hdfs.
   
   This is my first contribution so I would greatly appreciate any guidance :)
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.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] added MultipleFilesWebHdfsSensor [airflow]

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

   Great first PR!
   I added a small comment about the testing and notice your static checks is failing.
   you can read about configuring pre-commit [here](https://github.com/apache/airflow/blob/main/contributing-docs/03_contributors_quick_start.rst#configuring-pre-commit)
   


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

Posted by "romsharon98 (via GitHub)" <gi...@apache.org>.
romsharon98 merged PR #43045:
URL: https://github.com/apache/airflow/pull/43045


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

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

   This was an interesting one - seems that for some reason the CI workflow DID NOT run at all - only build image workflow did. that's why it was "green".


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

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


##########
providers/src/airflow/providers/apache/hdfs/sensors/web_hdfs.py:
##########
@@ -41,3 +43,31 @@ def poke(self, context: Context) -> bool:
         hook = WebHDFSHook(self.webhdfs_conn_id)
         self.log.info("Poking for file %s", self.filepath)
         return hook.check_for_path(hdfs_path=self.filepath)
+
+
+class MultipleFilesWebHdfsSensor(BaseSensorOperator):
+    """Waits for multiple files in a folder to land in HDFS."""
+
+    template_fields: Sequence[str] = ("directory_path", "expected_filenames")
+
+    def __init__(self, *, directory_path: str, expected_filenames: Sequence[str],
+                 webhdfs_conn_id: str = "webhdfs_default", **kwargs: Any) -> None:
+        super().__init__(**kwargs)
+        self.directory_path = directory_path
+        self.expected_filenames = set(expected_filenames)

Review Comment:
   ```suggestion
           self.expected_filenames = expected_filenames
   ```
   
   You defined `expected_filenames` as a templated field, which means that [it must be assigned directly](https://airflow.apache.org/docs/apache-airflow/stable/howto/custom-operator.html#limitations). If needed, you could apply it within the `poke()` 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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


Re: [PR] added MultipleFilesWebHdfsSensor [airflow]

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


##########
providers/src/airflow/providers/apache/hdfs/sensors/web_hdfs.py:
##########
@@ -41,3 +43,31 @@ def poke(self, context: Context) -> bool:
         hook = WebHDFSHook(self.webhdfs_conn_id)
         self.log.info("Poking for file %s", self.filepath)
         return hook.check_for_path(hdfs_path=self.filepath)
+
+
+class MultipleFilesWebHdfsSensor(BaseSensorOperator):
+    """Waits for multiple files in a folder to land in HDFS."""
+
+    template_fields: Sequence[str] = ("directory_path", "expected_filenames")
+
+    def __init__(self, *, directory_path: str, expected_filenames: Sequence[str],
+                 webhdfs_conn_id: str = "webhdfs_default", **kwargs: Any) -> None:
+        super().__init__(**kwargs)
+        self.directory_path = directory_path
+        self.expected_filenames = set(expected_filenames)

Review Comment:
   Fixed! Thank you.



-- 
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] added MultipleFilesWebHdfsSensor [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #43045:
URL: https://github.com/apache/airflow/pull/43045#issuecomment-2419723328

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #43045:
URL: https://github.com/apache/airflow/pull/43045#issuecomment-2414427309

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

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


##########
providers/tests/apache/hdfs/sensors/test_web_hdfs.py:
##########
@@ -55,3 +58,41 @@ def test_poke_should_return_false_for_non_existing_table(self, mock_hook):
 
         mock_hook.return_value.check_for_path.assert_called_once_with(hdfs_path=TEST_HDFS_PATH)
         mock_hook.assert_called_once_with(TEST_HDFS_CONN)
+
+
+class TestMultipleFilesWebHdfsSensor:
+    @mock.patch("airflow.providers.apache.hdfs.hooks.webhdfs.WebHDFSHook")
+    def test_poke(self, mock_hook):
+        mock_hook.return_value.get_conn.return_value.list.return_value = TEST_HDFS_FILENAMES
+
+        sensor = MultipleFilesWebHdfsSensor(
+            task_id="test_task",
+            webhdfs_conn_id=TEST_HDFS_CONN,
+            directory_path=TEST_HDFS_DIRECTORY,
+            expected_filenames=TEST_HDFS_FILENAMES
+        )
+        result = sensor.poke(dict())
+
+        assert result
+
+        mock_hook.return_value.get_conn.return_value.list.assert_called_once_with(TEST_HDFS_DIRECTORY)
+        mock_hook.return_value.get_conn.assert_called_once()
+        mock_hook.assert_called_once_with(TEST_HDFS_CONN)
+
+    @mock.patch("airflow.providers.apache.hdfs.hooks.webhdfs.WebHDFSHook")
+    def test_poke_should_return_false_for_missing_file(self, mock_hook):
+        mock_hook.return_value.get_conn.return_value.list.return_value = TEST_HDFS_FILENAMES[0]
+
+        sensor = MultipleFilesWebHdfsSensor(
+            task_id="test_task",
+            webhdfs_conn_id=TEST_HDFS_CONN,
+            directory_path=TEST_HDFS_DIRECTORY,
+            expected_filenames=TEST_HDFS_FILENAMES
+        )
+        exists = sensor.poke(dict())
+
+        assert not exists
+
+        mock_hook.return_value.get_conn.return_value.list.assert_called_once_with(TEST_HDFS_DIRECTORY)
+        mock_hook.return_value.get_conn.assert_called_once()
+        mock_hook.assert_called_once_with(TEST_HDFS_CONN)

Review Comment:
   I think it will be nice if we add a test that validate the log of the missing files.
   you can do it by getting the fixture `caplog` and access the text by `caplog.text`



-- 
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] added MultipleFilesWebHdfsSensor [airflow]

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

   This PR has caused failures both static and tests. I am fixing them in https://github.com/apache/airflow/pull/43122
   


-- 
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] added MultipleFilesWebHdfsSensor [airflow]

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

   This is a long known issue with GitHub that I raised to them 3 years ago - unfortunately there is a race condition that makes the PR "green" if the workflow have not started at all, or when it is just starting.... Very poor design IMHO for GitHub Actions @kaxil @romsharon98
   
   The only way I found to prevent such accidental merges of "green-but-incomplete" PRs  is to look at the number of checks that "passed". When there are < 10, something is WRONG. But it's not really obvious and happened to me more than once to merge such 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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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


Re: [PR] added MultipleFilesWebHdfsSensor [airflow]

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


##########
providers/tests/apache/hdfs/sensors/test_web_hdfs.py:
##########
@@ -55,3 +58,41 @@ def test_poke_should_return_false_for_non_existing_table(self, mock_hook):
 
         mock_hook.return_value.check_for_path.assert_called_once_with(hdfs_path=TEST_HDFS_PATH)
         mock_hook.assert_called_once_with(TEST_HDFS_CONN)
+
+
+class TestMultipleFilesWebHdfsSensor:
+    @mock.patch("airflow.providers.apache.hdfs.hooks.webhdfs.WebHDFSHook")
+    def test_poke(self, mock_hook):
+        mock_hook.return_value.get_conn.return_value.list.return_value = TEST_HDFS_FILENAMES
+
+        sensor = MultipleFilesWebHdfsSensor(
+            task_id="test_task",
+            webhdfs_conn_id=TEST_HDFS_CONN,
+            directory_path=TEST_HDFS_DIRECTORY,
+            expected_filenames=TEST_HDFS_FILENAMES
+        )
+        result = sensor.poke(dict())
+
+        assert result
+
+        mock_hook.return_value.get_conn.return_value.list.assert_called_once_with(TEST_HDFS_DIRECTORY)
+        mock_hook.return_value.get_conn.assert_called_once()
+        mock_hook.assert_called_once_with(TEST_HDFS_CONN)
+
+    @mock.patch("airflow.providers.apache.hdfs.hooks.webhdfs.WebHDFSHook")
+    def test_poke_should_return_false_for_missing_file(self, mock_hook):
+        mock_hook.return_value.get_conn.return_value.list.return_value = TEST_HDFS_FILENAMES[0]
+
+        sensor = MultipleFilesWebHdfsSensor(
+            task_id="test_task",
+            webhdfs_conn_id=TEST_HDFS_CONN,
+            directory_path=TEST_HDFS_DIRECTORY,
+            expected_filenames=TEST_HDFS_FILENAMES
+        )
+        exists = sensor.poke(dict())
+
+        assert not exists
+
+        mock_hook.return_value.get_conn.return_value.list.assert_called_once_with(TEST_HDFS_DIRECTORY)
+        mock_hook.return_value.get_conn.assert_called_once()
+        mock_hook.assert_called_once_with(TEST_HDFS_CONN)

Review Comment:
   Added! 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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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