Problem/Motivation
Currently, \Drupal\business_rules\Util\BusinessRulesProcessor stores information about every Event that has run in a page request in the database's key_value_expire table, with the collection business_rules.profile.
There are several issues with this approach:
- Critically, it doesn't qualify whose session the data belongs to, so two users who happen to make a page request that involves
BusinessRulesProcessorat the same time will run into issues... actions might not run at all, or might run twice. - It causes a lot of database traffic, which slows down the site (#3191811: Adding a dependent field with the Business Rules module makes the loading time and queries increase abnormally, #3168647: Business Rules Choking DB server with this query - key value pair)
- The teardown logic causes issues with tests (#3133687: Wrap keyvalue delete in BusinessRulesProcessor destruct) and Drupal 9 (#3187013: Error: Call to a member function prepare() on null after updating to Drupal 9.1.0 / PHP 7.4.11)
For context, note the current behavior was added in commit 1bcf53d by @yuriseki on Sat Apr 8 02:27:09 2017 -0300 (with the commit message "Revision 1"), i.e.: the third commit, i.e.: before the first release (8.x-1.0-alpha1).
Proposed resolution
Looking at where data is stored to business_rules.profile, it looks as if the data being loaded/stored is only valid for the request.
If this documentation is correct, then storing data in the key_value_expire table is the wrong place to store it. Sounds like drupal_static() would be a better choice, as that only sticks around for the request.
Remaining tasks
Determine what functionality thebusiness_rules.profileis intended to provide, and add testsWrite a patch- Review and feedback
- RTBC and feedback
- Commit
- Release
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | Schermata 2021-03-15 alle 18.54.57.png | 65.21 KB | kris77 |
| #17 | 3202612-17--cache-business-rules-processor-to-memory.patch | 10.95 KB | mparker17 |
Comments
Comment #2
mparker17(add related issues)
Comment #3
mparker17Found #3168647: Business Rules Choking DB server with this query - key value pair as a related issue (thanks @colan!)
Comment #4
mparker17Here's an initial patch with no tests.
Comment #5
mparker17Now that a bunch of stuff has been merged to 2.x, I've re-rolled the patch in #4.
No changes - just a simple re-roll - so no interdiff necessary.
Comment #6
bmustafa commentedi confirm that #5 has fixed the problem #3187013 for me on latest release of alpha2 of business rules.
Comment #7
mparker17Thanks to the fantastic test added by @Taran2L in #3120890: Configuration entities should provide dependencies (on other BR entities), I was able to write a test for the code paths in BusinessRulesProcessor which call
$this->util->getKeyValueExpirable('process');.Here is:
Comment #9
mparker17Lets try that again with an explicit (dummy) assertion at the end of the test case.
Comment #11
mparker17Does this work?
Comment #12
mparker17Apparently the PhpUnit syntax for mocking multiple calls to a method with the same name but different parameters and/or return values has changed.
Comment #15
mparker17Proving this worked before might be hard given I need the database... might have to switch my test from a Unit test to a Kernel test for the test-only patch.
Here's an attempt to fix the test fails in the patch anyway. (if it complains about drupal_static too, I might have to switch it to a Kernel test as well).
Comment #17
mparker17Lets try using Kernel tests instead.
Comment #18
mparker17Great! This is now ready for review.
If you're patching your site, use the
3202612-17--cache-business-rules-processor-to-memory.patch, as that contains the fix.I think at this point, given that...
3202612-17--test-only.patchis because thekey_value_expiretable is missing, and it is being created through some mechanism other thanhook_schema()which will be non-trivial for me to figure out; and;... I'm going to give up on the test-only patches for now, unless the module maintainer(s) specifically want me to put effort into it before merging the patch.
Comment #19
mparker17Update the issue summary now that I know the UI, API, and Data Model changes.
Comment #20
mparker17@bmustafa, could I request you review and test the
3202612-17--cache-business-rules-processor-to-memory.patchpatch in #17, and provide your feedback?Comment #21
bmustafa commented@mparker17,
i confirm patch #17 is perfect with me, and i have cleared my site cache, and the error does not appear, i am confirm all positive on my side.
Comment #22
kris77 commentedIn my content type I have 5 dependent fields to select the various car models.
So my selection is make, model, engine, etc ...(image attached.)
@mparker i have update from 8.x-1.0-beta13 to 2.0.0-alpha2, then apply your patch, but I haven't seen any improvements.
it always takes a long time.
Comment #23
kris77 commentedImage attached...
Comment #25
colanGiven the positive reports here and elsewhere, I'm merging this and cutting a new release.
#22: If you can prove that it's this module and not your set-up, please either reopen or create a new ticket.