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

chore: Remove unnecessary autoflush from tagging and key/value workflows #26009

Merged

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Nov 16, 2023

SUMMARY

This PR addresses this comment associated with #24776. After speaking with @michael-s-molina and @villebro it seems like the autoflush logic was likely not necessary.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@john-bodley john-bodley force-pushed the john-bodley--remove-autoflush branch from 59c2463 to 2bba0c5 Compare November 16, 2023 22:46
@john-bodley john-bodley changed the title chore: Remove autoflush chore: Remove unnecessary autoflush from tagging workflow Nov 16, 2023
@john-bodley john-bodley changed the title chore: Remove unnecessary autoflush from tagging workflow chore: Remove unnecessary autoflush from tagging and key/value workflows Nov 16, 2023
session = Session(bind=connection)

try:
with Session(bind=connection) as session:
Copy link
Member Author

Choose a reason for hiding this comment

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

Opting for a context manager which closes the session on exit.

@@ -111,7 +111,7 @@ class TaggedObject(Model, AuditMixinNullable):
tag = relationship("Tag", back_populates="objects", overlaps="tags")


def get_tag(name: str, session: Session, type_: TagType) -> Tag:
def get_tag(name: str, session: orm.Session, type_: TagType) -> Tag:
Copy link
Member Author

Choose a reason for hiding this comment

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

We redefine the Session class on line 38 so the previous type hint wasn't completely accurate.

@john-bodley john-bodley force-pushed the john-bodley--remove-autoflush branch from 2bba0c5 to 03e5729 Compare November 16, 2023 23:03
@john-bodley john-bodley marked this pull request as ready for review November 17, 2023 00:07
@john-bodley john-bodley requested a review from villebro November 17, 2023 00:07
@villebro
Copy link
Member

@john-bodley so those closed sessions in the event handlers were caused by those explicit finally: session.close() calls?

@john-bodley
Copy link
Member Author

john-bodley commented Nov 17, 2023

@villebro I think the error you might have been seeing, per what I was witnessing earlier when trying the session = inspect(target).session approach, was actually,

This transaction is closed

i.e., it was transaction as opposed to session related. The reason being (as mentioned in Slack) was that in the SQLAlchemy event documentation it states:

Mapper-level flush events only allow very limited operations, on attributes local to the row being operated upon only, as well as allowing any SQL to be emitted on the given Connection.

So when trying to leverage the Flask-SQLAlchemy session—via either inspect(target).session or db.session—the associated transaction was closed because said code was trying to perform operations outside of operations limited to the row (target) being operated on, i.e., the after_insert event listener is associated with a chart and we then try to create an associated tag. Creating a new session from the connection results in a new transaction for us to emit any SQL operation (per the SQLAlchemy statement).

Note in [SIP-99B] Proposal for (re)defining a "unit of work" this type of operation will not be allowed because it violates the "unit of work".

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

Very nice 👍 So I assume one of the next steps is to start moving those event listener based mutations into the parent commands?

@villebro
Copy link
Member

@villebro I think the error you might have been seeing, per what I was witnessing earlier when trying the session = inspect(target).session approach, was actually,

This transaction is closed

i.e., it was transaction as opposed to session related. The reason being (as mentioned in Slack) was that in the SQLAlchemy event documentation it states:

Mapper-level flush events only allow very limited operations, on attributes local to the row being operated upon only, as well as allowing any SQL to be emitted on the given Connection.

So when trying to leverage the Flask-SQLAlchemy session—via either inspect(target).session or db.session—the associated transaction was closed because said code was trying to perform operations outside of operations limited to the row (target) being operated on, i.e., the after_insert event listener is associated with a chart and we then try to create an associated tag. Creating a new session from the connection results in a new transaction for us to emit any SQL operation (per the SQLAlchemy statement).

Note in [SIP-99B] Proposal for (re)defining a "unit of work" this type of operation will not be allowed because it violates the "unit of work".

@john-bodley got it, this is all finally starting to make sense. Thanks for taking this on - once finished, this will have a big positive impact on the backend code quality, performance, maintainability etc..

@john-bodley
Copy link
Member Author

@villebro in terms of next steps, today's discussion (in conjunction with this PR) helped to solidify what is viable with SQLAlchemy event handlers and thus I updated SIP-99B with regards to how this should be handled moving forward. The TL;DR is ideally we should move said logic either to the database (if said events previously were handling logic which could be captured via a CASCADE constraint) or Command.

@john-bodley john-bodley merged commit bece2ea into apache:master Nov 17, 2023
49 checks passed
@john-bodley john-bodley deleted the john-bodley--remove-autoflush branch November 17, 2023 03:23
josedev-union pushed a commit to Ortege-xyz/studio that referenced this pull request Jan 22, 2024
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.1.0 labels Mar 8, 2024
sfirke pushed a commit to sfirke/superset that referenced this pull request Mar 22, 2024
vinothkumar66 pushed a commit to vinothkumar66/superset that referenced this pull request Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 3.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants