Skip to content

Ensure places/origins are removed when a bookmark with no visits etc is removed.#5653

Merged
mhammond merged 4 commits intomozilla:mainfrom
mhammond:bookmarks-and-origins
Jun 27, 2023
Merged

Ensure places/origins are removed when a bookmark with no visits etc is removed.#5653
mhammond merged 4 commits intomozilla:mainfrom
mhammond:bookmarks-and-origins

Conversation

@mhammond
Copy link
Member

@mhammond mhammond commented Jun 9, 2023

Fixes a few reports we have on Fenix for this, such as this one and I'm sure other dupes exist.

Lina, sorry to do this to you, but you are probably the best reviewer for this :( I'll also ask Ben to help grow more knowledge about places. This is by no means urgent.

@mhammond mhammond requested review from bendk and linabutler June 9, 2023 18:56
@codecov-commenter
Copy link

codecov-commenter commented Jun 9, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (e0b1d5b) 84.02% compared to head (482f1f9) 84.02%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5653   +/-   ##
=======================================
  Coverage   84.02%   84.02%           
=======================================
  Files         106      106           
  Lines       11272    11272           
=======================================
  Hits         9471     9471           
  Misses       1801     1801           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +7 to +13
-- markh doesn't understand why this trigger is needed. It seems to be doing roughly
-- the same work as moz_updateoriginsinsert_afterdelete_trigger and so we have 2 very
-- similar triggers for the same insert. A difference between them though is some
-- frecency stuff - and if you remove this filter, all tests pass except one obscure
-- one in matcher.rs, where a test site ends up with a frecency of zero rather than the
-- expected 1999. Both triggers exist in desktop too.
-- So if anyone can explain this, please update this comment :)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also confused about why this trigger works this way!

Desktop's moz_places_afterinsert_trigger doesn't update moz_origins directly; it inserts rows into moz_updateoriginsinsert_temp, and does the actual updates to moz_origins inside moz_updateoriginsinsert_afterdelete_trigger, like you noticed: https://searchfox.org/mozilla-central/rev/962a843f6d96283c45162c788dc72bf217fca7df/toolkit/components/places/nsPlacesTriggers.h#104-139

IOW, Desktop's moz_places_afterinsert_trigger looks identical to our moz_places_afterinsert_trigger_origins.

I did some spelunking, and found https://bugzilla.mozilla.org/show_bug.cgi?id=1414892. It looks like that's when the TEMP table was introduced—except, back then, the table was still called moz_hosts, not moz_origins.

I'm also unsure if the "expected" frecency of 1999 is correct. I might have changed -1 to 1999 in c27b45f, because the important part was that it wasn't -1, not that it was specifically 1999. But 0 doesn't sound right, either...

I stood up an xpcshell test in Desktop, and it reports a frecency of 2000 for a freshly inserted typed visit—but Desktop's algorithm has changed a few times since we ported it to Application Services. Building an early-2018 Firefox, when we ported history, is definitely a challenge 😅

Copy link
Contributor

@linabutler linabutler Jun 15, 2023

Choose a reason for hiding this comment

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

@mhammond I figured it out—the order of the deletes in delete_pending_temp_tables is off! 🤯

I think the order of those deletes has to be:

DELETE FROM moz_updateoriginsinsert_temp;
DELETE FROM moz_updateoriginsupdate_temp;
DELETE FROM moz_updateoriginsdelete_temp;

Specifically, it's important that DELETE FROM moz_updateoriginsinsert_temp runs before DELETE FROM moz_updateoriginsupdate_temp. Here's what I think is happening, and where the bug is:

When apply_observation_direct adds visits for a never-before-visited URL, it does three things:

  1. Inserts a row into moz_places for the new URL.
  2. Inserts rows into moz_historyvisits for the new visits.
  3. Updates moz_places.frecency for the new URL to account for the visits.

(1) fires the moz_places_afterinsert_trigger_origins trigger, and (3) fires the moz_places_afterupdate_frecency_trigger.

Currently, delete_pending_temp_tables does DELETE FROM moz_updateoriginsupdate_temp first. That'll fire the moz_updateoriginsupdate_afterdelete_trigger, which will try to update moz_origins.frecency for the new URL's origin.

But, there isn't a row for the new URL in moz_origins yet—because that row is inserted by the moz_updateoriginsinsert_afterdelete_trigger, and that trigger won't fire until we do DELETE FROM moz_updateoriginsinsert_temp!

So this UPDATE moz_origins statement won't do anything, and the initial frecency for the new URL's origin—2000, same as Desktop—will be silently dropped.

When DELETE FROM moz_updateoriginsinsert_temp eventually runs, it'll insert a row for the new URL's origin (via the moz_updateoriginsinsert_afterdelete_trigger), but with an initial moz_origins.frecency of 0.

Desktop's behavior is correct here, here, and here. It always processes inserts for new origins before frecency updates for those origins.

With the order of the deletes in delete_pending_temp_tables fixed, the frecency for the test site's origin in matcher.rs also changes to match Desktop's! 🎉

TL;DR: I think there are three small fixes:

  1. Change the order of the deletes in delete_pending_temp_tables.
  2. Delete the moz_places_afterinsert_trigger.
  3. Change the expected frecency in the api::matcher::tests::search test from 1999 to 2000.

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yes, thank you! I actually started with a theory that the order of deletions was wrong, but then convinced myself I was confused about that.

Copy link
Contributor

@linabutler linabutler left a comment

Choose a reason for hiding this comment

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

Thank you so much for looking at this! 🏆

I wouldn't mind having a fresh look with the origin frecency trigger changes, but your new moz_cleanup_origin_bookmark_deleted_trigger and tests look great!

WHERE id = OLD.fk
AND foreign_count = 0
AND last_visit_date_local = 0
AND last_visit_date_remote = 0;
Copy link
Contributor

@linabutler linabutler Jun 15, 2023

Choose a reason for hiding this comment

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

I wonder if we should do the equivalent of delete_pending_temp_tables here—or at least a DELETE FROM moz_updateoriginsdelete_temp—after deleting the place.

I think that would mean the origin would get cleaned up as soon as the bookmark is deleted, and you could avoid the call to crate::storage::history::delete_everything(&conn)? in the bookmarks test.

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't quite understand this suggestion. First of all though, you are correct that the new call I added to delete_everything can be removed immediately.

But ISTM that the new trigger already is forcing that delete_pending_temp_tables dance - the trigger on moz_bookmarks causes a deletion of moz_places, which causes the moz_places_afterdelete_trigger_origins trigger. So sorry for being dense, but do you mind spelling it out :)

Copy link
Contributor

@linabutler linabutler Jun 15, 2023

Choose a reason for hiding this comment

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

Oh, rad!

the trigger on moz_bookmarks causes a deletion of moz_places, which causes the moz_places_afterdelete_trigger_origins trigger.

Yep, you're right!

But the moz_places_afterdelete_trigger_origins trigger just inserts a row for the soon-to-be-deleted origin into moz_updateoriginsdelete_temp. It doesn't actually delete the origin from moz_origins, or update the global frecency stats in moz_meta, until you do DELETE FROM moz_updateoriginsdelete_temp to fire the moz_updateoriginsdelete_afterdelete_trigger.

At first, I thought this would leave the origin behind, because doing DELETE FROM moz_places by itself isn't enough to force the delete_pending_temp_tables dance. And that's why I thought we needed to force it by doing a DELETE FROM moz_updateoriginsdelete_temp in your new trigger.

But then I realized that delete_bookmark calls delete_pending_temp_tables! So it's not necessary to do it in your new trigger, because the delete_pending_temp_tables call here will take care of it.

So this is all working correctly now—if in a slightly indirect way that I didn't think all the way through—and you can ignore my suggestion! 😅

mhammond added 3 commits June 15, 2023 10:04
When deleting a bookmark that has no visits, that origin will remain in
moz_origins until a complete history wipe is done. This is the same
behaviour as desktop, sadly considered a bug, and described in
bug 1650511.
@mhammond mhammond force-pushed the bookmarks-and-origins branch from 243d281 to a932f0a Compare June 15, 2023 14:06
@mhammond mhammond force-pushed the bookmarks-and-origins branch from a932f0a to 482f1f9 Compare June 15, 2023 14:10
@mhammond
Copy link
Member Author

New version up with all suggested changes except the one I don't understand :)

Copy link
Contributor

@linabutler linabutler left a comment

Choose a reason for hiding this comment

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

Thanks! My comment was totally a non-issue, sorry to muddle things there 🙈 Land away!

@mhammond mhammond added this pull request to the merge queue Jun 27, 2023
Merged via the queue into mozilla:main with commit f2e7ee5 Jun 27, 2023
@mhammond mhammond deleted the bookmarks-and-origins branch June 27, 2023 18:58
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.

3 participants