Needs review
Project:
Drupal core
Version:
main
Component:
database system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
16 Mar 2023 at 22:06 UTC
Updated:
27 Oct 2026 at 13:53 UTC
Jump to comment: Most recent
Comments
Comment #2
mfbFor inspiration see https://github.com/laravel/framework/tree/10.x/src/Illuminate/Database/E...
Comment #4
mondrakeReparenting.
Comment #5
mondrakeOn this
Comment #7
mondrakeStarted something, fit nicely with latest improvements in #3364706: Refactor transactions. Looking forward to an early review - this sets the basis, more events are just more meat around this bone.
Comment #8
mondrakePostponed on #3384960: Strengthen TransactionManager
Comment #9
daffie commentedComment #10
mondrakerebased
Comment #11
mondrakeWe cannot do this now, #3405976: Transaction autocommit during shutdown relies on unreliable object destruction order (xdebug 3.3+ enabled) introduced
Database::commitAllOnShutdown()which runs at a time when the event dispatcher has been destroyed already, so events fail. One more reason to move towards explicit commit.Comment #12
mondrakeComment #13
mfbeesh, seems not-ideal to wait until most-everything is destroyed to commit transactions, i.e. difficult to respond to various transaction commit failure modes.
Comment #14
mondrakeComment #15
mondrakeComment #16
mondrakeComment #17
mondrakeAs long as we can accept that these events are not dispatched when the event dispatcher service is not available - that is: 1) early in the request before the full container is available, and 2) during shutdown when the service can have already been destructed - this can proceed.
Still needs some tests for commit/release/rollback failure events - not easy to create the context - so I will look into mocking.
But the biggest part here is already reviewable.
Comment #18
mondrakeWorking on the unit tests.
Comment #19
mondrakeComment #20
mondrakeComment #21
mondrakeComment #22
mondrakeComment #23
mondrakeI think we need to add an additional event for the autocommit on DDL, separate from the regular one, and still emit the regular one in the expected moment even if the autocommit already happened. This will make simpler moving post-transaction callbacks to event subscriptions.
AFK for a while, will come back later.
Comment #24
mondrakeComment #25
dcam commentedI did my best to review the MR. Unfortunately, I don't know enough about the database system to review it well from a technical perspective. My comments on the MR amount almost entirely to nitpicks on documentation. I'm sorry about that. For what it's worth, I did read through all the code change and it looks good to me.
Comment #26
dcam commentedI nearly forgot: since there are three new methods on
TransactionManagerInterfacewe'll need a change record, won't we?Comment #27
mondrakeThanks for review @dcam
Comment #28
mondrakeYes, a CR will be needed here. Let’s do that once we are close to finalize this issue - I’ve seen too many CR written too early that eventually were not reflecting the avtual changes.
Comment #29
nicxvan commentedJust curious why these are not hooks?
Comment #30
mfbDispatching an event is faster than invoking a hook (and uses less memory), so it doesn't make sense for low level code like the database layer to invoke extra hooks, especially in the context of performance analysis.
Comment #31
mondrake#30 + I also fancy one day we will be able to have events dispatched well before the container and modules are available (#3492391: Make the event dispatcher available before container full bootstrap), which will enable moving post-transaction callback execution to an event subscriber and deprecate execution on Transaction destruction (issue tbd).
See parent plan issue.
Comment #32
mfbAnd I wouldn't mind having some new events for instrumenting how slow the hook system is 🙃
Comment #33
nicxvan commentedDo you have a source for that? I don't see how that is possible.
We just decoupled hooks from events because of the performance problems. #3506930: Separate hooks from events
Event Dispatcher does not scale well, I think a hook here is better served.
Comment #34
mondrakeAlso, we already have events developed for statement execution, so the transaction ones here just extend that framework.
Comment #36
mondrake@jamyotte is there a reason for hiding the MR?
Comment #38
mfbIn my testing,
Drupal::moduleHandler()->invokeAll()is slower thanDrupal::service('event_dispatcher')->dispatch()Both the first call, and the second call are slower.
Events are of course heavily used in low-level code, for example, request events/event subscribers. So that seems like the obvious building block we'd use to allow for instrumenting various low-level subsystems. And we already are using as mondrake mentioned.
Comment #39
nicxvan commentedSorry to keep belaboring this:
Which hook did you invoke, how many implementations were there, what did that hook do?
Which event did you dispatch, how many events were implemented, and what did those events do?
Again I'm asking because this does not align at all with the numbers we saw. Removing event dispatcher from hooks showed the following improvements courtesy of @berdir:
If you share how you tested this I can try to replicate your findings.
Comment #40
mondrakeIMHO #39 could use a separate discussion and issue.
Comment #41
mfbI'm not sure how to dynamically register a hook implementation so that we can make a nice little test script, but we can add an event listener and dispatch an event, and then in comparison, invoke a hook that doesn't exist:
Comment #42
mfbLooks like
moduleHandler()->invokeAll()can make database queries - cache gets - which I can't imagine you'd want to do as part of dispatching events inside the database layer.Comment #43
mondrakeComment #44
smustgrave commentedThis is a very large MR, difficult to review I'd imagine (not sure I'm one to do that) hence leaving in review. But wanted to ask if could be broken up?
Comment #45
mondrake#44 it's hard for me to think about splitting this now, 3 years after initial commits - I see it has multiple aspects, but the journey to get to it was one that while developing the events I figured out the underlying improvements to transaction handling edge cases, and now I would find difficult to phase them.
Comment #46
mondrake#3584322: Cleanup transactions in try/catch blocks shows that we need also to work on managing failures of BEGIN TRANSACTION and SAVEPOINT statement executions.
Comment #47
mondrakeI've opened #3584465: Fix inconsistencies in the TransactionManager behavior to deal with fixes that are IMHO needed independently from here (the issue here helped find the issues, but those can be fixed without events...). That also addresses #44. Hope this helps.
Comment #48
mondrake