villebro commented on code in PR #36529: URL: https://github.com/apache/superset/pull/36529#discussion_r2614941430
########## superset/sql/execution/celery_task.py: ########## @@ -0,0 +1,473 @@ +# 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. +""" +Celery task for async SQL execution. + +This module provides the Celery task for executing SQL queries asynchronously. +It is used by SQLExecutor.execute_async() to run queries in the background. +""" + +from __future__ import annotations + +import dataclasses +import logging +import sys +import uuid +from sys import getsizeof +from typing import Any, cast, TYPE_CHECKING + +import backoff +import msgpack +from celery.exceptions import SoftTimeLimitExceeded +from flask import current_app as app, has_app_context +from flask_babel import gettext as __ + +from superset import ( + db, + results_backend, + results_backend_use_msgpack, + security_manager, +) +from superset.common.db_query_status import QueryStatus +from superset.constants import QUERY_CANCEL_KEY +from superset.dataframe import df_to_records +from superset.errors import ErrorLevel, SupersetError, SupersetErrorType +from superset.exceptions import ( + SupersetErrorException, + SupersetErrorsException, +) +from superset.extensions import celery_app +from superset.models.sql_lab import Query +from superset.result_set import SupersetResultSet +from superset.sql.execution.executor import execute_sql_with_cursor +from superset.sql.parse import SQLScript +from superset.sqllab.utils import write_ipc_buffer +from superset.utils import json +from superset.utils.core import override_user, zlib_compress +from superset.utils.dates import now_as_float +from superset.utils.decorators import stats_timing + +if TYPE_CHECKING: + pass + +logger = logging.getLogger(__name__) + +BYTES_IN_MB = 1024 * 1024 + + +def _get_query_backoff_handler(details: dict[Any, Any]) -> None: + """Handler for backoff retry logging.""" + stats_logger = app.config["STATS_LOGGER"] + query_id = details["kwargs"]["query_id"] + stats_logger.incr(f"error_attempting_orm_query_{details['tries'] - 1}") + logger.warning( + "Query with id `%s` could not be retrieved, retrying...", + str(query_id), + exc_info=True, + ) + + +def _get_query_giveup_handler(_: Any) -> None: + """Handler for backoff giveup logging.""" + stats_logger = app.config["STATS_LOGGER"] + stats_logger.incr("error_failed_at_getting_orm_query") + + [email protected]_exception( + backoff.constant, + Exception, + interval=1, + on_backoff=_get_query_backoff_handler, + on_giveup=_get_query_giveup_handler, + max_tries=5, +) +def _get_query(query_id: int) -> Query: + """Attempt to get the query with retry logic.""" + return db.session.query(Query).filter_by(id=query_id).one() Review Comment: I wonder what types of exceptions we could run into here? I think typically if this raises an Exception, there's some more fundamental issue going on (metastore down, query object missing or similar), which likely won't change by doing 5 x 1 sec constant backoff. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
