Skip to content

Add --retry and --retry-delay to "airflow db check"#31836

Merged
potiuk merged 6 commits into
apache:mainfrom
xuganyu96:airflow-db-check-retry-and-delay
Jun 15, 2023
Merged

Add --retry and --retry-delay to "airflow db check"#31836
potiuk merged 6 commits into
apache:mainfrom
xuganyu96:airflow-db-check-retry-and-delay

Conversation

@xuganyu96

@xuganyu96 xuganyu96 commented Jun 10, 2023

Copy link
Copy Markdown
Contributor

This PR closes #31818. It is still a work in progress with many to-do's pending:

  • Adding unit tests if necessary
  • Run through unit tests and pre-commit

This PR made significant refactor to airflow.utils.db.check function, which I felt safe to do because I found this function to only be used by the db check command, and which I felt it not an API used externally. Please let me know if this approach is not satisfactory.


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
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.

@xuganyu96
xuganyu96 force-pushed the airflow-db-check-retry-and-delay branch from c0dfa07 to ea74388 Compare June 13, 2023 03:58
Comment thread airflow/cli/cli_config.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should give the database more time to breathe. :)

Would it make sense to increase the default value to 30 or something more?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The db check retry here is really targetted to see if the database is "up" already - nothing more.

I think 1s is quite good as a default. We do not do a LOT - we just try to connect to the database which takes milliseconds usually. And if it does not work, then it usually means that the database is not yet ready and we will simply fail on failed "connection" establishing - nothing that makes it "heavy".

The longer the delay is, the longer we will delay startup if this command is used as a prerequisite to start airflow component (and this is the intention).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense. :)

Comment thread airflow/cli/commands/db_command.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. using tenacity (especially that we use it, is a good idea here).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. Will work on incorporating tenacity right away.

@xuganyu96
xuganyu96 force-pushed the airflow-db-check-retry-and-delay branch 2 times, most recently from 24a2503 to a6ae9be Compare June 13, 2023 17:56
Comment thread airflow/cli/commands/db_command.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@potiuk @utkarsharma2
Since tenacity.retry relies on unhandled exceptions for indicating whether a function call (or a code block in this case) is successful or not, I've reverted the implementation of airflow.utils.db.check back to its original state (letting the SQLAlchemy session raise unhandled OperationalError or else). Consequently, there is no longer any need to unit test this util function.

I chose to set reraise=True so that the original error from airflow.utils.db.check can be surfaced. Please advise whether we should raise RetryError or the original error. Thank you.

Last but not least, the unit test for db_command.check is updated accordingly. I am not 100% sure if testing the retry logic implemented in tenacity.retry is all that helpful, but the test itself still mocks time.sleep and utils.db.check and test that they are called for the correct number of times.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, there is no need to test for tenacity code.

Also, reraising the original exception is vital as it gives users a hint of what needs to be done next, whereas RetryError would be meaningless to them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's the nice thing about using small and proven utils. No need to test them :)

@xuganyu96
xuganyu96 force-pushed the airflow-db-check-retry-and-delay branch from a6ae9be to 1055ec2 Compare June 13, 2023 18:17
@xuganyu96
xuganyu96 force-pushed the airflow-db-check-retry-and-delay branch from 511d55b to 74834f6 Compare June 15, 2023 04:19

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice, smalll improvement . Good job @xuganyu96

@potiuk
potiuk merged commit 1b35a07 into apache:main Jun 15, 2023
@boring-cyborg

boring-cyborg Bot commented Jun 15, 2023

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

@potiuk

potiuk commented Jun 15, 2023

Copy link
Copy Markdown
Member

🎉 @xuganyu96

@ephraimbuddy ephraimbuddy added this to the Airflow 2.7.0 milestone Jul 6, 2023
@ephraimbuddy ephraimbuddy added the type:new-feature Changelog: New Features label Jul 6, 2023
@xuganyu96
xuganyu96 deleted the airflow-db-check-retry-and-delay branch July 16, 2023 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:CLI type:new-feature Changelog: New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add retry + timeout to Airflow db check

4 participants