airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Enable detection of errors in Variable.get() such as absence of key method and connection failure to Metastore

Open tyama711 opened this issue 9 months ago • 2 comments

Description

When attempting to retrieve a value by specifying a key using the Variable.get() method, there are cases where the retrieval fails, such as:

  • When the specified key does not exist in environment variables or Metastore
  • When there is a connection failure to Metastore or some other error

However, in the current implementation of Airflow, all exception handling is being suppressed in the following part, making it impossible for users to differentiate these cases: https://github.com/apache/airflow/blob/defe4590e9c94a9b5157143d618a143cb30ade78/airflow/models/variable.py#L286-L291

I believe that Variable.get() should return None if the specified key does not exist, and it should raise an exception for unexpected errors such as connection errors.

Use case/motivation

With this feature, it becomes possible to handle errors specific to each cause as follows:

  • When the specified key does not exist in environment variables or Metastore --> Use some default value
  • When there is a connection failure to Metastore or some other error --> Retry several times, and if unsuccessful, fail the job.

Related issues

No response

Are you willing to submit a PR?

  • [x] Yes I am willing to submit a PR!

Code of Conduct

tyama711 avatar May 12 '24 14:05 tyama711

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

boring-cyborg[bot] avatar May 12 '24 14:05 boring-cyborg[bot]

Hi @tyama711 thanks for opening this issue. You can give the get function a default value if the variable is not exist.

Variable.get(key='dummy_key', default_var='default_value')

As you said you can retry for any exception you get so I don't think we need to return more specific exception, let me know what you think.

romsharon98 avatar May 12 '24 19:05 romsharon98

This issue has been automatically marked as stale because it has been open for 14 days with no response from the author. It will be closed in next 7 days if no further activity occurs from the issue author.

github-actions[bot] avatar Jun 01 '24 00:06 github-actions[bot]

This issue has been closed because it has not received response from the issue author.

github-actions[bot] avatar Jun 09 '24 00:06 github-actions[bot]