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

[PR] fix: add retry logic in case of google auth refresh credential error [airflow]

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

   <!--
    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/
   -->
   
   Occasionally, acquire impersonated credentials can fail with a 502 HTTP (Server error). It raises a `google.auth.exceptions.RefreshError`. Using task retries is sometimes not a good option as, e.g. a submitted BigQuey query should not be submitted again. 
   
   Occasionally, the error occur in tasks running in deferred mode where acquiring impersonated credentials happens frequently every x seconds depending on the task's poll_interval.
   
   In this case we should have some retry to acquire impersonated credentials.
   
   _Note:_ The deferable version of `BigQueryInsertJobOperator` is using the the synchronous client call with the event loop's run_in_executor() method. https://github.com/apache/airflow/issues/35833
   
   closes: #38532 
   
   
   <!-- 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] fix: add retry logic in case of google auth refresh credential error [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38961:
URL: https://github.com/apache/airflow/pull/38961#discussion_r1566892855


##########
airflow/providers/google/common/hooks/base_google.py:
##########
@@ -454,6 +474,24 @@ def decorator(fun: T):
 
         return decorator
 
+    @staticmethod
+    def refresh_credentials_retry(*args, **kwargs) -> Callable[[T], T]:
+        """Provide a mechanism to repeat requests in response to a temporary refresh credential issue."""
+
+        def decorator(fun: T):

Review Comment:
   Nice. Thanks for the 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.

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

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


Re: [PR] fix: add retry logic in case of google auth refresh credential error [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38961:
URL: https://github.com/apache/airflow/pull/38961#discussion_r1566799340


##########
tests/providers/google/common/hooks/test_base_google.py:
##########
@@ -98,6 +98,46 @@ def test_raise_exception_on_non_quota_exception(self):
             )
 
 
+class TestRefreshCredentialsRetry:
+    @pytest.mark.parametrize(
+        "exc, retryable",
+        [
+            (RefreshError("Other error", "test body"), False),
+            (RefreshError("Unable to acquire impersonated credentials", "test body"), True),
+            (ValueError(), False),
+        ],
+    )
+    def test_is_refresh_credentials_exception(self, exc, retryable):
+        assert is_refresh_credentials_exception(exc) is retryable
+
+    def test_do_nothing_on_non_error(self):
+        @hook.GoogleBaseHook.refresh_credentials_retry()
+        def func():
+            return 42
+
+        assert func(), 42

Review Comment:
   Should it be `func() == 42` instead?



##########
airflow/providers/google/common/hooks/base_google.py:
##########
@@ -454,6 +474,24 @@ def decorator(fun: T):
 
         return decorator
 
+    @staticmethod
+    def refresh_credentials_retry(*args, **kwargs) -> Callable[[T], T]:
+        """Provide a mechanism to repeat requests in response to a temporary refresh credential issue."""
+
+        def decorator(fun: T):

Review Comment:
   nitpick: `func` seems to be a more commonly used convention in airflow 
   
   ```suggestion
           def decorator(func: T):
   ```



-- 
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] fix: add retry logic in case of google auth refresh credential error [airflow]

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


##########
airflow/providers/google/common/hooks/base_google.py:
##########
@@ -454,6 +474,24 @@ def decorator(fun: T):
 
         return decorator
 
+    @staticmethod
+    def refresh_credentials_retry(*args, **kwargs) -> Callable[[T], T]:
+        """Provide a mechanism to repeat requests in response to a temporary refresh credential issue."""
+
+        def decorator(fun: T):

Review Comment:
   Changed. Changed also for all other decorator in `base_google.py`



-- 
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] fix: add retry logic in case of google auth refresh credential error [airflow]

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


##########
tests/providers/google/common/hooks/test_base_google.py:
##########
@@ -98,6 +98,46 @@ def test_raise_exception_on_non_quota_exception(self):
             )
 
 
+class TestRefreshCredentialsRetry:
+    @pytest.mark.parametrize(
+        "exc, retryable",
+        [
+            (RefreshError("Other error", "test body"), False),
+            (RefreshError("Unable to acquire impersonated credentials", "test body"), True),
+            (ValueError(), False),
+        ],
+    )
+    def test_is_refresh_credentials_exception(self, exc, retryable):
+        assert is_refresh_credentials_exception(exc) is retryable
+
+    def test_do_nothing_on_non_error(self):
+        @hook.GoogleBaseHook.refresh_credentials_retry()
+        def func():
+            return 42
+
+        assert func(), 42

Review Comment:
   Good catch. Changed.



-- 
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] fix: add retry logic in case of google auth refresh credential error [airflow]

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

   I'm planning on merging this one later today. Please let me know if anyone wants to take a deeper look 🙂 


-- 
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] fix: add retry logic in case of google auth refresh credential error [airflow]

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


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