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

De-dupe network requests #40059

Closed
wants to merge 19 commits into from
Closed

De-dupe network requests #40059

wants to merge 19 commits into from

Conversation

roryabraham
Copy link
Contributor

@roryabraham roryabraham commented Apr 11, 2024

Details

This fixes a few problems:

  • If there are no serialized requests, DeleteComment will "conflict" with itself, causing it to not be executed.
  • If there is a pending AddComment request, and you try to delete it, the delete will not run.
  • If you are offline and try to delete a comment that you sent when you were online, it will not appear strikethrough.

Fixed Issues

$ #39432
$ #39307
PROPOSAL:

Tests / Offline Tests / QA Steps

  1. Open a DM with someone

  2. Send a comment

  3. Delete it

  4. Verify the comment deletes. Open a different report and come back - verify it does not come back.

  5. Send another comment

  6. Go offline

  7. Delete the comment.

  8. Verify that the comment appears with strikethrough

  9. Come back online

  10. Verify that the comment deletes. Open a different report and come back - verify it does not come back.

  11. Go offline

  12. (web) open the network tab in the dev tools and clear the requests

  13. Send a comment

  14. Delete the comment

  15. Verify that the comment disappears (no strikethrough)

  16. Come back online

  17. (web) Verify that no AddComment or DeleteComment requests were sent. Open a different report and come back - verify the comment does not appear.

  18. (web) In the network tab, click on the throttling dropdown, then add

    image
  19. (web) add a profile with 5-10 seconds of latency (enough time that an AddComment request will still be pending when you go to delete it)

  20. Enable the new profile and clear the network requests

  21. Send a comment and quickly delete it. Verify it appears and then delete soon thereafter

  22. Verify that the comment is added and then disappears as expected.

  23. Verify that both the AddComment and DeleteComment request are processed.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Only did screenshots on web for #urgency and because I'm close to the end of my day, and because this is a platform agnostic issue.

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
web.mov
web.mov
image
MacOS: Desktop

@roryabraham roryabraham self-assigned this Apr 11, 2024
@roryabraham roryabraham marked this pull request as ready for review April 11, 2024 02:05
@roryabraham roryabraham requested a review from a team as a code owner April 11, 2024 02:05
@melvin-bot melvin-bot bot requested review from hungvu193 and removed request for a team April 11, 2024 02:05
Copy link

melvin-bot bot commented Apr 11, 2024

@hungvu193 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@hungvu193
Copy link
Contributor

hungvu193 commented Apr 11, 2024

Update: I can't reproduce anymore

Last testcase is failing for me:

  1. (web) add a profile with 5-10 seconds of latency (enough time that an AddComment request will still be pending when you go to delete it)
  2. Enable the new profile and clear the network requests
  3. Send a comment and quickly delete it. Verify it appears and then delete soon thereafter
  4. Verify that the comment is added and then disappears as expected.
  5. Verify that both the AddComment and DeleteComment request are processed.

Now GetOlderActions get called repeatedly.

Screen.Recording.2024-04-11.at.09.20.34.mov

@hungvu193
Copy link
Contributor

Also test is failing @roryabraham

@hungvu193
Copy link
Contributor

OK, I can reproduce this, consistently now. We need to repeat step 1 to step 17 few times.

Test 1
Test1.mov
Test 2
Test2.mov

// Add request to Persisted Requests so that it can be retried if it fails
PersistedRequests.save(request);
if (!hasConflict || !shouldIncludeCurrentRequestOnConflict) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Because we flip the condition from shouldExcludeCurrentRequestOnConflict to shouldIncludeCurrentRequestOnConflict, we should remove the negation too.

Suggested change
if (!hasConflict || !shouldIncludeCurrentRequestOnConflict) {
if (!hasConflict || shouldIncludeCurrentRequestOnConflict) {

I think we also need to set the default value for shouldIncludeCurrentRequestOnConflict to true.

Copy link
Contributor

@hungvu193 hungvu193 Apr 11, 2024

Choose a reason for hiding this comment

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

@bernhardoj applying those changes doesn't seem to fix the issue I mentioned above:

Screen.Recording.2024-04-11.at.10.29.22.mov

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, the suggestion is to have the name match what it does. shouldIncludeCurrentRequestOnConflict tells whether we should include the current request even if there is a conflict. If we negate it (!), then it's read as, don't include the current request when there is a conflict, but inside the if, we include the request.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, the issue above also happened on Staging, it doesn't relate to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the problem is that the name is confusing. I see why too - right now shouldIncludeCurrentRequestOnConflict=true means something like:

"if this request conflicts with other serialized requests, cancel it along with those other requests"

and shouldIncludeCurrentRequestOnConflict=false means something like:

"if this request conflicts with other serialized requests, cancel those other requests but still send this one"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went ahead and renamed this param to shouldSkipThisRequestOnConflict, which feels more clear to me:

  • If hasConflict && shouldSkipThisRequestOnConflict then the request is skipped
  • if hasConflict && !shouldSkipThisRequestOnConflict, then the request is not skipped

) as string[];
return persistedRequests.filter((request) => conflictingCommands.includes(request.command) && request.data?.reportActionID === reportActionID);
},
handleConflictingRequest: () => Onyx.update(successData),
shouldIncludeCurrentRequest: !isDeletedParentAction,
shouldIncludeCurrentRequestOnConflict: !isDeletedParentAction,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with above, we need to remove the negation here.

Suggested change
shouldIncludeCurrentRequestOnConflict: !isDeletedParentAction,
shouldIncludeCurrentRequestOnConflict: isDeletedParentAction,

isDeletedParentAction
? [WRITE_COMMANDS.UPDATE_COMMENT]
: [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_ATTACHMENT, WRITE_COMMANDS.UPDATE_COMMENT, WRITE_COMMANDS.DELETE_COMMENT]
isDeletedParentAction ? [WRITE_COMMANDS.UPDATE_COMMENT] : [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_ATTACHMENT, WRITE_COMMANDS.UPDATE_COMMENT]
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, do we not want to conditionally add UPDATE_COMMENT only when isOptimisticAction is true? (just like I did in my PR)

Suggested change
isDeletedParentAction ? [WRITE_COMMANDS.UPDATE_COMMENT] : [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_ATTACHMENT, WRITE_COMMANDS.UPDATE_COMMENT]
isDeletedParentAction ? [WRITE_COMMANDS.UPDATE_COMMENT] : [WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_ATTACHMENT, reportAction.isOptimisticAction ? WRITE_COMMANDS.UPDATE_COMMENT : '']

What will happen if we always include it?

  1. Add a comment while online (added to server)
  2. Go offline
  3. Edit the comment. At this point, the persisted request contains UPDATE_COMMENT
  4. Delete the comment.
    Both edit and delete request is cancelled out, the message is optimistically deleted, but the message will reappears when reopening the chat.


it('should always ignore any requests that have already been sent', async () => {
expect(PersistedRequests.getAll().length).toBe(1);
SequentialQueue.flush();
Copy link
Contributor

Choose a reason for hiding this comment

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

@roryabraham You may need to mock flush as it will never resolve in tests.

@shubham1206agra
Copy link
Contributor

@roryabraham Can you add an automated test?

  1. Create an AddComment with a 5-second response time, which has been started processing.
  2. After 1 second, an EditComment API call was made.
  3. After another 1 second, a DeleteComment API call was made.

Result: Only AddComment and DeleteComment calls will be made.

@shubham1206agra
Copy link
Contributor

@roryabraham Can you define the ideal behavior in this case?

  1. Go offline.
  2. Add a comment in the report.
  3. Create a thread on the comment added in the previous step.
  4. Add a comment on the thread.
  5. Delete the comment added in step 2.
  6. Delete the comment added in step 4.
  7. Go online and observe the Network logs.

@roryabraham
Copy link
Contributor Author

thanks for the comments here, jumping back in

@roryabraham
Copy link
Contributor Author

Also test is failing @roryabraham

I broke this when fixing lint, should be fixed now.

@roryabraham
Copy link
Contributor Author

hmmm just thought of another case that's not covered correctly though:

  1. Create a comment (online)
  2. Go offline
  3. Edit the comment
  4. Delete the comment
  5. Come back online

In this case we'd want the UpdateComment request to be skipped and replaced by the DeleteComment request. So we'd want shouldSkipThisRequestOnConflict to be false.

But in this case:

  1. Go offline
  2. Create a comment
  3. Delete the comment
  4. Come back online

we'd want both the AddComment and DeleteComment requests to be skipped. So we'd want shouldSkipThisRequestOnConflict to be true.

@roryabraham
Copy link
Contributor Author

I fixed that issue by making shouldSkipThisRequestOnConflict a function, and ensuring that it runs after getConflictingRequests and handleConflictingRequest. That way, you can know which request(s) conflict and use it to determine if the new request should be skipped or not.

@shubham1206agra
Copy link
Contributor

@roryabraham We might want to add a new API in this.
PR #39007

@hungvu193
Copy link
Contributor

hmmm just thought of another case that's not covered correctly though:

  1. Create a comment (online)
  2. Go offline
  3. Edit the comment
  4. Delete the comment
  5. Come back online

In this case we'd want the UpdateComment request to be skipped and replaced by the DeleteComment request. So we'd want shouldSkipThisRequestOnConflict to be false.

But in this case:

  1. Go offline
  2. Create a comment
  3. Delete the comment
  4. Come back online

we'd want both the AddComment and DeleteComment requests to be skipped. So we'd want shouldSkipThisRequestOnConflict to be true.

Screen.Recording.2024-04-12.at.10.29.41.mov

I think the first case you mentioned here is still failing. It skipped both update and delete comment API.

@roryabraham
Copy link
Contributor Author

Thanks for all the reviews everyone. We've agreed that this is too risky / too low-level a PR to CP. Instead, we're going to proceed with a revert of all request de-duping features, CP that, then proceed with this PR on a less-rushed timeline, ensuring that everything works as expected and it receives full QA on staging

@shubham1206agra
Copy link
Contributor

@roryabraham You can close this PR

@roryabraham
Copy link
Contributor Author

@shubham1206agra why do you say that? We reverted the request de-duping logic completely with the intention of completing this PR and having it go through full regression tests

@shubham1206agra
Copy link
Contributor

Oh
I thought you were going to open a new PR to do that.

@shubham1206agra
Copy link
Contributor

@roryabraham Can we start again to push this PR through?

@roryabraham roryabraham changed the title Fix DeleteComment De-dupe network requests Aug 21, 2024
@roryabraham
Copy link
Contributor Author

Closing in favor of #47913

@roryabraham roryabraham deleted the Rory-FixDeleteComment branch September 17, 2024 18:56
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.

4 participants