Skip to content

Remove deleted rules from caches on full refresh - #6876

Open
officialasishkumar wants to merge 1 commit into
plausible:masterfrom
officialasishkumar:fix-stale-cache-entries-after-full-refresh
Open

officialasishkumar wants to merge 1 commit into
plausible:masterfrom
officialasishkumar:fix-stale-cache-entries-after-full-refresh

Conversation

@officialasishkumar

Copy link
Copy Markdown

Changes

Fixes #6858

A full cache refresh did not remove everything that was deleted from the database, so deleted Shields rules kept applying until the app restarted:

  1. PageRuleCache and HostnameRuleCache are ETS bags keyed by domain. The refresh inserted each site's current rules and then deleted only the keys that were no longer present. A site that still had any rule kept its key, and a deleted rule stayed in the bag next to the remaining ones.
  2. merge_items([], _) returned early, before stale keys were deleted. When the last row of a table was removed (for example the only IP rule on a small instance), the full refresh found no rows and kept the old entries.

What changed:

  • New Plausible.Cache.Adapter.delete_stale/2, used by merge_items/2. For regular tables it removes the keys that are no longer present, as before. For bag tables it compares individual objects and removes the ones that are not among the refreshed items. New objects are inserted first, so readers never see a site without its rules during a refresh.
  • The empty-list shortcut in merge_items/2 is removed. An empty result of a full refresh now empties the cache. delete_stale_items?: false (used by the "recently updated" refresh) still leaves existing entries alone.
  • The two tests that asserted that merging no items keeps the old cache now assert the new behaviour, and cover delete_stale_items?: false for the old one. The dummy site in the gate keeper test, added as a workaround for this limitation, is no longer needed and is removed.
  • New tests for bag caches in Plausible.CacheTest, and for removed page, hostname and IP rules in their cache tests.

Tests

  • Automated tests have been added

Changelog

  • Entry has been added to changelog

Documentation

  • This change does not need a documentation update

Dark mode

  • This PR does not change the UI

A full cache refresh did not remove everything that was deleted from the
database, so deleted Shields rules kept applying until the app restarted:

1. `PageRuleCache` and `HostnameRuleCache` are ETS bags keyed by domain.
   The refresh inserted the current rules of each site and then deleted
   only the keys that were no longer present. A site that still had any
   rule kept its key, and a deleted rule stayed in the bag next to the
   remaining ones.

2. `merge_items([], _)` returned early, before stale keys were deleted.
   When the last row of a table was removed (for example the only IP rule
   on a small instance), the full refresh found no rows and left the old
   entries in the cache.

`Plausible.Cache.Adapter.delete_stale/2` now cleans up after a merge. For
regular tables it removes the keys that are no longer present, as before.
For bag tables it compares individual objects and removes the ones that
are not among the refreshed items. The new objects are inserted first, so
readers never observe a site without its rules during a refresh.

The empty-list shortcut is gone: an empty result of a full refresh now
empties the cache, while `delete_stale_items?: false` (used by the
recently-updated refresh) still keeps existing entries. This also makes
the dummy-site workaround in the gate keeper test unnecessary.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shields: deleted rules can stay in the cache until a restart

2 participants