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


##########
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:
   Sure.



##########
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:
   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.

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