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

Handle Reactions Without Users When Sending Notifications #5820

Conversation

abdellani
Copy link
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

I tried to fix #5508 by checking the reaction owners' existence in the database when creating reaction_siblings. The problem is that the query becomes slower compared to the original code.
If this solution is not acceptable, could you please give me some guidance to improve it?

Related Tickets & Documents

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

Added to documentation?

  • docs.dev.to
  • readme
  • no documentation needed

[optional] What gif best describes this PR or how it makes you feel?

alt_text

@abdellani abdellani requested a review from rhymes January 31, 2020 09:43
@rhymes rhymes requested review from mstruve and lightalloy and removed request for rhymes January 31, 2020 11:29
@rhymes
Copy link
Contributor

rhymes commented Jan 31, 2020

Thanks @abdellani for taking this on!

The first thing that pops in my mind is: we should write a test that makes sure this solves the problem for good, what do you think?

You also mention the query becomes slower, can you post some data about it?

@abdellani
Copy link
Contributor Author

abdellani commented Jan 31, 2020

I used the following code before the after the new code :

u=User.first
rxn = Reaction.create(
user_id: User.last.id,
category: "like",
reactable: u.articles.first
)

Notification.send_reaction_notification_without_delay(rxn, u)

Form the last instruction, I got the following results using the original code:

(byebug) reaction_siblings
  Reaction Load (3.4ms)  SELECT  "reactions".* FROM "reactions" WHERE "reactions"."reactable_id" = $1 AND "reactions"."reactable_type" = $2 AND "reactions"."user_id" != $3 ORDER BY created_at DESC LIMIT $4  [["reactable_id", 3], ["reactable_type", "Article"], ["user_id", 1], ["LIMIT", 11]]
  Article Load (1.2ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1  [["id", 3]]
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1  [["id", 10]]

After the update, I had :

(byebug) reaction_siblings
SQL (6.1ms) ...
  Article Load (0.8ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1  [["id", 3]]
#<ActiveRecord::Relation [#<Reaction id: 1, category: "like", created_at: "2020-01-31 12:52:53", points: 1.0, reactable_id: 3, reactable_type: "Article", status: "valid", updated_at: "2020-01-31 12:52:53", user_id: 10>]>

User Load (0.9ms) and Reaction Load (3.4ms) are replaced with one instruction SQL (6.1ms). The database contains 25 articles, 10 users and 1 reaction.
I noticed that the SQL execution time variates. I tried to called reaction_siblings several times without changing anything in the database, and every time I get a different duration which can take up to 14ms under exactly the same context.
I wonder how it will perform under a bigger database.

Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

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

I second @rhymes, let's write a test for this otherwise I think the solution LGTM!

@mstruve mstruve changed the title [WIP] Handle Reactions Without Users When Sending Notifications Handle Reactions Without Users When Sending Notifications Feb 7, 2020
@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Feb 7, 2020
Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for adding the spec! 🚀

@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Feb 7, 2020
@mstruve mstruve requested a review from rhymes February 7, 2020 16:16
Copy link
Contributor

@benhalpern benhalpern left a comment

Choose a reason for hiding this comment

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

This looks good to me. We'll see in prod if it's meaningfully slower.

@benhalpern benhalpern merged commit faad0b8 into forem:master Feb 7, 2020
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: reviewed-approved bot applied label for PR's where reviewer approves changes labels Feb 7, 2020
@abdellani abdellani deleted the 5508_Handle_Reactions_Without_Users_When_Sending_Notifications branch February 7, 2020 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle Reactions Without Users When Sending Notifications
4 participants