Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IntegrityError during webserver startup #27297

Merged
merged 1 commit into from Oct 27, 2022

Conversation

ephraimbuddy
Copy link
Contributor

@ephraimbuddy ephraimbuddy commented Oct 26, 2022

When starting up the webserver, we create all the gunicorn workers needed and each of these workers creates the webserver app instead of getting a copy of the app. This results in all the workers trying to write to the database at the same time causing a lot of IntegrityError in the database.

The fix was to preload the app before forking workers

closes: #23512

When starting up the webserver, we create all the `gunicorn` workers needed
and each of these workers creates the webserver app instead of getting a copy of the app.
This results in all the workers trying to write to the database at the same time
causing a lot of IntegrityError in the database.

The fix was to preload the app before forking workers
@boring-cyborg boring-cyborg bot added area:CLI area:webserver Webserver related Issues labels Oct 26, 2022
@ephraimbuddy ephraimbuddy requested review from ashb, mik-laj, jedcunningham, jhtimmins and potiuk and removed request for ashb and mik-laj October 26, 2022 14:26
@ephraimbuddy ephraimbuddy added the type:bug-fix Changelog: Bug Fixes label Oct 26, 2022
@ephraimbuddy ephraimbuddy added this to the Airflow 2.4.3 milestone Oct 26, 2022
@potiuk
Copy link
Member

potiuk commented Oct 26, 2022

No issues with plugins any more ?

@ephraimbuddy
Copy link
Contributor Author

No issues with plugins any more ?

Yes. What is loaded at that point is web UI plugin. The ensure_plugins_loaded which calls the main airflow plugin is not loaded with the app. The problem before was whether it could be a problem with lazy_load_plugin config but it's not as only the web UI plugins are loaded during loading of the app:

def init_plugins(app):
"""Integrate Flask and FAB with plugins"""
from airflow import plugins_manager
plugins_manager.initialize_web_ui_plugins()
appbuilder = app.appbuilder
for view in plugins_manager.flask_appbuilder_views:
name = view.get('name')
if name:
log.debug("Adding view %s with menu", name)
appbuilder.add_view(view["view"], name, category=view["category"])
else:
# if 'name' key is missing, intent is to add view without menu
log.debug("Adding view %s without menu", str(type(view["view"])))
appbuilder.add_view_no_menu(view["view"])
for menu_link in sorted(plugins_manager.flask_appbuilder_menu_links, key=lambda x: x["name"]):
log.debug("Adding menu link %s to %s", menu_link["name"], menu_link["href"])
appbuilder.add_link(**menu_link)
for blue_print in plugins_manager.flask_blueprints:
log.debug("Adding blueprint %s:%s", blue_print["name"], blue_print["blueprint"].import_name)
app.register_blueprint(blue_print["blueprint"])

The lazy_load_plugin config is called during startup

if not settings.LAZY_LOAD_PLUGINS:
from airflow import plugins_manager
plugins_manager.ensure_plugins_loaded()
which loads the main airflow plugin(not just UI)

@potiuk
Copy link
Member

potiuk commented Oct 26, 2022

Cool!

@uranusjr uranusjr merged commit 8f99c79 into apache:main Oct 27, 2022
@uranusjr uranusjr deleted the fix-integrity-error-webserver branch October 27, 2022 04:25
@zachliu
Copy link
Contributor

zachliu commented Oct 27, 2022

just tested, this works!
👍 👍 👍
👍 👍
👍

@atrbgithub
Copy link
Contributor

@ephraimbuddy Thanks for this!

Will this also help where there are multiple webservers being started on different instances?

We originally mitigated this issue by slowing scaling up the workers on each instance as described here. That worked fine for when there was only one webserver instance, but not when there were multiple.

With this fix, when two webservers both start up and attempt the preload, is there a chance they will hit the same issue?

@potiuk potiuk mentioned this pull request May 2, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:CLI area:webserver Webserver related Issues type:bug-fix Changelog: Bug Fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random "duplicate key value violates unique constraint" errors when initializing the postgres database
5 participants