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

SearchKit - Refresh DB entities via table-swap #31767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

totten
Copy link
Member

@totten totten commented Jan 13, 2025

Overview

This updates the handling of "DB Entity" (table-mode). In this mode, you need some mechanism to update the content of the table (e.g. the "refresh" operation). This revises the mechanism.

(This is an alternative to #31768. ping @colemanw)

Before

Clear the table (TRUNCATE) and re-fill with regenerated data (INSERT) .

In the period between the truncation and the last insertion, the content of the table is ill-defined.

After

Make a new table with a temporary name and fill that. The original table remains available during the build. Once ready, swap the tables atomically.

Comments

I'm kind of ambivalent about whether to do a straight policy-change or to configurable policy-change. This PR is a straight change-- which is simpler. In theory, new algo sounds better -- but it's clearly not proven yet. There may be some trade-offs (eg space-efficiency), and there may be other modes the future. shrug

Overview
---------

This updates the handling of "DB Entity" (table-mode). In this mode, you need some mechanism
to update the content of the table (e.g. the "refresh" operation). This updates the mechanism.

Before
------

TRUNCATE the table and re-INSERTs with regenerated data.

In the period between the truncation and regenerated data, the content of the table
is ill-defined.

After
-----

CREATE a new table with a temporary name and fill that. Once ready,
swap the old table and new table (atomicly).
Copy link

civibot bot commented Jan 13, 2025

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@colemanw
Copy link
Member

I agree this this is all-around better and there's no need to clutter/confuse the admin UX with one-more-freaking-setting-to-configure when there's no known downside to always doing it this way.

@colemanw colemanw added the merge ready PR will be merged after a few days if there are no objections label Jan 13, 2025
// Only one process should actually refresh this entity (at a given time).
$lock = \Civi::lockManager()->acquire("data.skentity." . $display['id'], 1);
if (!$lock->isAcquired()) {
throw new \Civi\Search\Exception\RefreshInProgressException(sprintf('Refresh (%s) is already in progress', $this->getEntityName()));
Copy link
Member Author

Choose a reason for hiding this comment

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

@colemanw You probably know the auto-refresh flag and existing callers better than me. Will emitting this kind of error cause random trouble?

IMHO, in a greenfield, this is good style. But it a brownfield, it might require sprinkling some try/catch expressions?

Copy link
Member

Choose a reason for hiding this comment

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

Well, the SearchKit UI calls this function every time you hit the "Save" button. It does so asynchronously so if this action fails it doesn't prevent the rest of the save from happening...
but I'd imagine if you repeatedly hit the "Save" button while rapidly making changes to the search, you'd manage to trigger the failure, which means your latest changes to the savedsearch wouldn't be reflected in the skentity table...
until the next time you hit the Save button or it gets refreshed via cron.
That's not a disastrous outcome by any means, but a nicer behavior would be like a tail-end debounce where e.g. if you click the button 5 times in quick succession, then refreshes 1-4 get cancelled and the 5th one gets to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
master merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants