jason810496 commented on code in PR #51586:
URL: https://github.com/apache/airflow/pull/51586#discussion_r2142863942


##########
airflow-core/tests/unit/core/test_db_discovery.py:
##########
@@ -0,0 +1,165 @@
+# 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.
+from __future__ import annotations
+
+import contextlib
+import logging
+import os
+import shutil
+import socket
+import time
+from unittest.mock import patch
+
+import pytest
+from sqlalchemy import text
+
+from airflow import settings
+from airflow.utils import db_discovery
+from airflow.utils.db_discovery import DbDiscoveryStatus
+
+from tests_common.test_utils.config import conf_vars
+
+log = logging.getLogger(__name__)
+
+
+def dispose_connection_pool():
+    """Dispose any cached sockets so that the next query will force a new 
connect."""
+    settings.engine.dispose()
+    # Wait for SqlAlchemy.
+    time.sleep(0.5)
+
+
+def make_db_test_call():
+    """
+    Create a session and execute a query.
+
+    It will establish a new connection if there isn't one available.
+    New connections use DNS lookup.
+    """
+    from airflow.utils.session import create_session
+
+    with create_session() as session:
+        session.execute(text("SELECT 1"))
+
+
+def assert_query_raises_exc(expected_error_msg: str, expected_status: str, 
expected_retry_num: int):
+    with pytest.raises(socket.gaierror, match=expected_error_msg):
+        make_db_test_call()
+
+    assert len(db_discovery.db_health_status) == 2
+
+    assert db_discovery.db_health_status[0] == expected_status
+    assert db_discovery.db_retry_count == expected_retry_num
+
+
+@pytest.mark.backend("postgres")
+class TestDbDiscoveryIntegration:

Review Comment:
   How about adding 2 fixtures:
   
   1. for `db_discovery` module setup/ teardown, maybe called ( 
`prepare_db_discovery` )
       Somehow like:
       ```python
       dispose_connection_pool()
       yield
       # Reset the values for the next tests.
       db_discovery.db_health_status = (DbDiscoveryStatus.OK, 0.0)
       db_discovery.db_retry_count = 0
       ```
   
   2. another fixture is for dns resolution config
   
   Then all the case can use these fixture with `@pytest.mark.usefixtures` if 
needed.



##########
airflow-core/src/airflow/utils/session.py:
##########
@@ -38,6 +40,20 @@ def create_session(scoped: bool = True) -> 
Generator[SASession, None, None]:
         Session = getattr(settings, "NonScopedSession", None)
     if Session is None:
         raise RuntimeError("Session must be set before!")
+
+    check_db_discovery = conf.getboolean("database", "check_db_discovery")
+    db_discovery_retries = conf.getint("database", "max_db_discovery_retries")
+    db_discovery_initial_wait = conf.getfloat("database", 
"db_discovery_initial_wait_time")
+    db_discovery_max_wait = conf.getfloat("database", 
"db_discovery_max_wait_time")
+    # If there is an exception, it will be raised
+    # in order to prevent the session from unnecessarily being created.
+    if check_db_discovery:
+        check_db_discovery_with_retries(
+            retry_num=db_discovery_retries,
+            initial_retry_wait=db_discovery_initial_wait,
+            max_retry_wait=db_discovery_max_wait,
+        )

Review Comment:
   nit:
   ```suggestion
       # If there is an exception, it will be raised
       # in order to prevent the session from unnecessarily being created.
       if conf.getboolean("database", "check_db_discovery"):
           check_db_discovery_with_retries(
               retry_num=conf.getint("database", "max_db_discovery_retries"),
               initial_retry_wait=conf.getfloat("database", 
"db_discovery_initial_wait_time"),
               max_retry_wait=conf.getfloat("database", 
"db_discovery_max_wait_time"),
           )
   ```



##########
airflow-core/src/airflow/utils/db_discovery.py:
##########
@@ -0,0 +1,127 @@
+# 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.
+
+from __future__ import annotations
+
+import logging
+import socket
+import time
+
+import tenacity
+from sqlalchemy.engine.url import make_url
+
+from airflow.configuration import conf
+
+logger = logging.getLogger(__name__)
+
+
+class DbDiscoveryStatus:
+    """Enum with the return value for `check_db_discovery_if_needed()`."""
+
+    # The hostname resolves.
+    OK = "ok"
+    # There has been some temporary DNS lookup blip and the connection will 
probably recover.
+    # Causes: a dns timeout or a temporary network issue.
+    TEMPORARY_ERROR = "dns_temporary_failure"
+    # Unknown hostname or service, this is permanent and the connection can't 
be recovered.
+    # Causes: a cmd or config typo, a hostname that doesn't exist.
+    UNKNOWN_HOSTNAME = "unknown_hostname"
+    # Unknown hostname or service, this is permanent and the connection can't 
be recovered.
+    # Causes: Failed DNS server or config typo.
+    PERMANENT_ERROR = "dns_permanent_failure"
+    # Some other error.
+    UNKNOWN_ERROR = "unknown_error"
+
+
+# db status - how long ago it was retrieved
+db_health_status: tuple[str, float] = (DbDiscoveryStatus.OK, 0.0)
+
+# TODO: For now, this is used for testing
+#  but it can also be used to add stats.
+db_retry_count: int = 0
+
+
+def _is_temporary_dns_error(ex: BaseException) -> bool:
+    return isinstance(ex, socket.gaierror) and ex.errno == socket.EAI_AGAIN

Review Comment:
   How about make this util a lambda ?



-- 
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-unsubscr...@airflow.apache.org

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

Reply via email to