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

Enforce READ COMMITTED isolation when using mysql #15714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions airflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ def prepare_engine_args(disable_connection_pool=False):
engine_args['pool_recycle'] = pool_recycle
engine_args['pool_pre_ping'] = pool_pre_ping
engine_args['max_overflow'] = max_overflow

# The default isolation level for MySQL (REPEATABLE READ) can introduce inconsistencies when
# running multiple schedulers, as repeated queries on the same session may read from stale snapshots.
# 'READ COMMITTED' is the default value for PostgreSQL.
# More information here:
# https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html"
if SQL_ALCHEMY_CONN.startswith('mysql'):
engine_args['isolation_level'] = 'READ COMMITTED'

return engine_args


Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_sqlalchemy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_configure_orm_with_default_values(
pool_pre_ping=True,
pool_recycle=1800,
pool_size=5,
isolation_level='READ COMMITTED',
)

@patch('airflow.settings.setup_event_handlers')
Expand All @@ -75,11 +76,15 @@ def test_sql_alchemy_connect_args(
}
with conf_vars(config):
settings.configure_orm()
engine_args = {}
if settings.SQL_ALCHEMY_CONN.startswith('mysql'):
engine_args['isolation_level'] = 'READ COMMITTED'
mock_create_engine.assert_called_once_with(
settings.SQL_ALCHEMY_CONN,
connect_args=SQL_ALCHEMY_CONNECT_ARGS,
poolclass=NullPool,
encoding='utf-8',
**engine_args,
)

@patch('airflow.settings.setup_event_handlers')
Expand Down
2 changes: 2 additions & 0 deletions tests/www/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ def debug_view():
def test_should_set_sqlalchemy_engine_options(self):
app = application.cached_app(testing=True)
engine_params = {'pool_size': 3, 'pool_recycle': 120, 'pool_pre_ping': True, 'max_overflow': 5}
if app.config['SQLALCHEMY_DATABASE_URI'].startswith('mysql'):
engine_params['isolation_level'] = 'READ COMMITTED'
assert app.config['SQLALCHEMY_ENGINE_OPTIONS'] == engine_params

@conf_vars(
Expand Down