Transition to SQLAlchemy 2.0

Bug #1962153 reported by Rodolfo Alonso
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
neutron
Fix Released
High
Rodolfo Alonso

Bug Description

The ``Session.autocommit`` parameter is deprecated and will be removed in SQLAlchemy version 2.0. The Session now features ``autobegin`` behavior such that the Session.begin() method may be called if a transaction has not yet been started yet. See the section session_explicit_begin for background.

In Neutron/neutron-lib, the property ``Session.is_active`` [1] is used to determine if a session has an active transaction. With "autocommit=False", the transaction is never closed once created. That means the property will return "True" even when the transaction has flushed all pending commands.

In order to mimic the previous behaviour, we need to find an alternative method to decide if the session has any pending command to be flushed.

Related topic: https://review.opendev.org/q/topic:sqlalchemy-20

[1]https://github.com/sqlalchemy/sqlalchemy/blob/878c37614efd311794aa50467dbb9e3fe972fdff/lib/sqlalchemy/orm/session.py#L3809-L3837

Tags: db
description: updated
Changed in neutron:
assignee: nobody → Rodolfo Alonso (rodolfo-alonso-hernandez)
Changed in neutron:
status: New → In Progress
Revision history for this message
Rodolfo Alonso (rodolfo-alonso-hernandez) wrote :

Launchpad/gerrit sync seems not to work. Patches:
- Neutron: https://review.opendev.org/c/openstack/neutron/+/828739
- neutron-lib: https://review.opendev.org/c/openstack/neutron-lib/+/828738

tags: added: db
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to neutron (master)

Reviewed: https://review.opendev.org/c/openstack/neutron/+/828739
Committed: https://opendev.org/openstack/neutron/commit/982986507371e78d9c9f5e1d0112efd645c09df9
Submitter: "Zuul (22348)"
Branch: master

commit 982986507371e78d9c9f5e1d0112efd645c09df9
Author: Rodolfo Alonso Hernandez <email address hidden>
Date: Sat Feb 5 04:58:20 2022 +0000

    Refactor session "is_active" handling for sqlalchemy-20

    Since sqlalchemy 1.4, "session.autocommit" is False by default; in
    sqlalchemy 2.0 this will be the only value accepted.

    The ``_orm.Session`` is considered active when [1]:
    - there is a transaction and this transaction is active
    - there is no transaction [2], the class ``_orm.Session`` will
       autobegin when it is first used.

    The second one breaks the way Neutron considers a session is active:
    only when a transaction is in place, Neutron considers a session is
    active.

    [1]https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4/lib/sqlalchemy/orm/session.py#L3918-L3950
    [2]https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4/lib/sqlalchemy/orm/session.py#L3930-L3932

    Partial-Bug: #1962153
    Topic: sqlalchemy-20

    Change-Id: Iabaee4e556afb3dc75a82d99dc4a597fe4d7dd21

Changed in neutron:
importance: Undecided → High
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to neutron-lib (master)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to neutron-lib (master)

Reviewed: https://review.opendev.org/c/openstack/neutron-lib/+/828738
Committed: https://opendev.org/openstack/neutron-lib/commit/ef1e8abb89141db39e62eaa1cbdb73aca1ceb647
Submitter: "Zuul (22348)"
Branch: master

commit ef1e8abb89141db39e62eaa1cbdb73aca1ceb647
Author: Rodolfo Alonso Hernandez <email address hidden>
Date: Sun Feb 6 09:54:23 2022 +0000

    Refactor session "is_active" handling for sqlalchemy-20

    Since sqlalchemy 1.4, "session.autocommit" is False by default; in
    sqlalchemy 2.0 this will be the only value accepted.

    The ``_orm.Session`` is considered active when [1]:
    - there is a transaction and this transaction is active
    - there is no transaction [2], the class ``_orm.Session`` will
      autobegin when it is first used.

    The second one breaks the way Neutron considers a session is active:
    only when a transaction is in place, Neutron considers a session is
    active.

    Depends-On: https://review.opendev.org/c/openstack/neutron/+/833247

    [1]https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4/lib/sqlalchemy/orm/session.py#L3918-L3950
    [2]https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4/lib/sqlalchemy/orm/session.py#L3930-L3932

    Closes-Bug: #1962153
    Topic: sqlalchemy-20

    Change-Id: Iabaee4e556afb3dc75a82d99dc4a597fe4d7dd21

Changed in neutron:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/neutron-lib 2.21.0

This issue was fixed in the openstack/neutron-lib 2.21.0 release.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to neutron (master)

Related fix proposed to branch: master
Review: https://review.opendev.org/c/openstack/neutron/+/841246

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to neutron (master)

Reviewed: https://review.opendev.org/c/openstack/neutron/+/841246
Committed: https://opendev.org/openstack/neutron/commit/88fb5416f7169bd3542bca6af0192f935c8cc18e
Submitter: "Zuul (22348)"
Branch: master

commit 88fb5416f7169bd3542bca6af0192f935c8cc18e
Author: Rodolfo Alonso Hernandez <email address hidden>
Date: Fri May 6 21:00:48 2022 +0000

    Bump neutron-lib to 2.21.0

    This new neutron-lib version contains the patch [1], that provides
    compatibility with SQLAlchemy 2.0.

    [1]https://review.opendev.org/c/openstack/neutron-lib/+/828738

    Related-Bug: #1962153
    Topic: sqlalchemy-20
    Change-Id: Ib2880b8fa61ae7691c527b837045cfa15226072c

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.