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

Remove lodashUnionWith as it breaks the sequential queue processing #14608

Merged
merged 1 commit into from
Jan 31, 2023

Conversation

marcaaron
Copy link
Contributor

@marcaaron marcaaron commented Jan 26, 2023

Details

While investigating this issue I discovered that the SequentialQueue is not working correctly and duplicate actions taken by a user are actually getting removed by the code we added here:

persistedRequests = lodashUnionWith(persistedRequests, retryableRequests, _.isEqual);

This is a pretty big oversight. As it means that if a user takes an action that is identical to some other action they've taken (i.e. identical according to _.isEqual()) then it will be "unioned" or removed from the persisted requests array. In actuality new requests should always be concatenated and never deduplicated like this.

I think this code predates the sequential queue and it sounds like the reasoning is some kind of unplanned deduplication effort: 5cd8b4a

Fixed Issues

$ #14584

Tests

Same as QA

  • Verify that no errors appear in the JS console

Offline tests

The test is an offline test

QA Steps

  1. Go offline
  2. Pin a chat
  3. Unpin a chat
  4. Pin a chat again
  5. Go online
  6. Verify that the chat ends up in the correct state of "pinned"
  • 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 / Chrome
    • iOS / native
    • iOS / 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 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 correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • 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 a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • 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(themeColors.componentBG)
  • 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 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.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web

2023-01-26_11-02-39

Mobile Web - Chrome 2023-01-26_12-26-06
Mobile Web - Safari 2023-01-26_11-29-58
Desktop

2023-01-26_12-28-52

iOS 2023-01-26_11-20-09
Android 2023-01-26_12-22-56

@marcaaron marcaaron self-assigned this Jan 26, 2023
@marcaaron marcaaron marked this pull request as ready for review January 26, 2023 22:31
@marcaaron marcaaron requested a review from a team as a code owner January 26, 2023 22:31
@melvin-bot melvin-bot bot requested review from puneetlath and sobitneupane and removed request for a team January 26, 2023 22:32
@melvin-bot
Copy link

melvin-bot bot commented Jan 26, 2023

@sobitneupane @puneetlath One of you needs to 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]

@sobitneupane
Copy link
Contributor

Screenshots/Videos

Web
Screen.Recording.2023-01-27.at.13.17.39.mov
Mobile Web - Chrome
Screen.Recording.2023-01-27.at.13.42.40.mov
Mobile Web - Safari
Screen.Recording.2023-01-27.at.13.44.59.mov
Desktop
Screen.Recording.2023-01-27.at.13.26.00.mov
iOS
Screen.Recording.2023-01-27.at.13.34.49.mov
Android
Screen.Recording.2023-01-27.at.13.39.30.mov

@sobitneupane
Copy link
Contributor

In the screen records above, it can be noticed that if a user is offline and toggles pin/unpin button of a report multiple times, then the report shuffles multiple times in LHN after user is reconnected to the internet. Just noting it here to make sure it is expected.

@marcaaron
Copy link
Contributor Author

the report shuffles multiple times in LHN after user is reconnected to the internet

Yep! It's how the sequential queue is supposed to work (for now). We are mainly concerned that the state ends up correctly after the queue is emptied fully. This could be improved in the future, but it's how we designed this stuff to work.

@puneetlath
Copy link
Contributor

@marcaaron I'm curious though, I think this bug only happens because we only have one function for pinning/unpinning - togglePinnedState. If we instead had separate functions for pinning and unpinning, with separate API calls, then that would also solve this bug right? And we could still de-dupe the persisted requests.

@marcaaron
Copy link
Contributor Author

I think this bug only happens because we only have one function for pinning/unpinning - togglePinnedState

I understand the alternate solution and it's something I hadn't thought about. Though, I don't think that's the reason why the bug happens. Like, we could also de-duplicate from the end of the queue rather than the beginning and the bug would not happen.

And we could still de-dupe the persisted requests.

I think we need to have a wider conversation about that. The SequentialQueue design is such that we will replay actions a user took back in the order they were taken. De-duplicating them might be a good idea - but it's not clear that we ever intended for this to happen. Not saying we can't change that, but I'm just trying to make the app work how we planned it should work.

@puneetlath
Copy link
Contributor

Ok that logic makes sense to me. Let's make it work as intended 😅

Copy link
Contributor

@puneetlath puneetlath left a comment

Choose a reason for hiding this comment

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

Code changes look good to me. @sobitneupane will you be able to complete the reviewer checklist?

Copy link
Contributor

@sobitneupane sobitneupane left a comment

Choose a reason for hiding this comment

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

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (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 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
    • 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 correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • 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 verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • 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
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • 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(themeColors.componentBG)
  • 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 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.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

@puneetlath puneetlath merged commit 703b5c4 into main Jan 31, 2023
@puneetlath puneetlath deleted the marcaaron-stopDeduplicatingQueeudRequests branch January 31, 2023 15:20
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions
Copy link
Contributor

Performance Comparison Report 📊

Significant Changes To Duration

There are no entries

Meaningless Changes To Duration

Show entries
Name Duration
App start TTI 702.973 ms → 717.812 ms (+14.839 ms, +2.1%)
App start nativeLaunch 19.733 ms → 19.968 ms (+0.234 ms, +1.2%)
App start regularAppStart 0.015 ms → 0.015 ms (+0.001 ms, +3.8%)
Open Search Page TTI 604.601 ms → 598.536 ms (-6.065 ms, -1.0%)
App start runJsBundle 204.344 ms → 195.125 ms (-9.219 ms, -4.5%)
Show details
Name Duration
App start TTI Baseline
Mean: 702.973 ms
Stdev: 33.515 ms (4.8%)
Runs: 630.3232450000942 630.4082689993083 658.4138229992241 668.3118089996278 674.2243009991944 680.4224420003593 684.5566709991544 685.2210019994527 685.5108490008861 685.8698640000075 686.1957430001348 688.900054000318 692.3156659994274 697.3062820006162 697.9917510002851 702.2433429993689 702.822929000482 703.4082249999046 705.0091069992632 705.5275260005146 707.9993549995124 710.7326059993356 715.2509630005807 726.4326399993151 727.2760279998183 729.0939370002598 733.1658079996705 736.2572830002755 736.6018010005355 748.7758670002222 776.6769709996879 781.8774309996516

Current
Mean: 717.812 ms
Stdev: 24.009 ms (3.3%)
Runs: 684.4697140008211 684.4713829997927 684.7364739999175 687.084036000073 691.8932499997318 692.3085110001266 693.396135000512 696.4026229996234 706.2016419991851 706.2810309994966 708.9739699997008 708.9804229997098 709.8097779992968 710.7174389995635 710.9385059997439 714.5384679995477 719.8512510005385 719.8665709998459 719.8853420000523 725.2251720000058 727.1407490000129 728.4859469998628 729.0576579999179 735.8869490008801 738.8113509994 753.2293790001422 756.8011659998447 761.4213830009103 763.6107889991254 763.8757380004972
App start nativeLaunch Baseline
Mean: 19.733 ms
Stdev: 1.861 ms (9.4%)
Runs: 17 18 18 18 18 18 18 18 18 19 19 19 19 19 19 19 19 20 20 20 20 21 21 21 21 22 22 22 24 25

Current
Mean: 19.968 ms
Stdev: 2.024 ms (10.1%)
Runs: 18 18 18 18 18 18 19 19 19 19 19 19 19 19 19 19 19 19 19 20 20 20 20 21 22 22 22 23 24 24 26
App start regularAppStart Baseline
Mean: 0.015 ms
Stdev: 0.001 ms (6.3%)
Runs: 0.012653999030590057 0.013103000819683075 0.01342800073325634 0.013631001114845276 0.0138349998742342 0.013915998861193657 0.013916000723838806 0.013996999710798264 0.014038000255823135 0.01411999948322773 0.014159999787807465 0.014242000877857208 0.01428299956023693 0.014364000409841537 0.01452699862420559 0.014527000486850739 0.014649000018835068 0.014649000018835068 0.01476999931037426 0.014771001413464546 0.014893000945448875 0.014932999387383461 0.015015000477433205 0.015015000477433205 0.015217998996376991 0.015217998996376991 0.015380999073386192 0.01550300046801567 0.015706000849604607 0.01619499921798706 0.016683001071214676 0.01684499904513359

Current
Mean: 0.015 ms
Stdev: 0.001 ms (6.5%)
Runs: 0.013426998630166054 0.013631001114845276 0.013793999329209328 0.014037998393177986 0.014159999787807465 0.014160998165607452 0.014200998470187187 0.014281999319791794 0.014527000486850739 0.014606999233365059 0.014649000018835068 0.014649000018835068 0.01469000056385994 0.014770999550819397 0.014973999932408333 0.015013998374342918 0.01505499891936779 0.01521800085902214 0.015341000631451607 0.015463000163435936 0.015665998682379723 0.015705998986959457 0.0157880000770092 0.01603200100362301 0.016071999445557594 0.016276000067591667 0.016357000917196274 0.01635800115764141 0.016479000449180603 0.016683001071214676 0.016764000058174133 0.017211999744176865
Open Search Page TTI Baseline
Mean: 604.601 ms
Stdev: 17.811 ms (2.9%)
Runs: 579.3542480003089 586.6837160009891 586.8225100003183 587.4712729994208 587.6013589985669 588.6502289995551 591.4942220002413 592.0214440003037 592.3866379987448 592.659872001037 593.8135180007666 594.9883219990879 595.6680909991264 595.9581710007042 596.0096439998597 597.4600419998169 598.3486330006272 600.3970950003713 601.9958089999855 607.0628660004586 608.0507000014186 610.1480299998075 612.2789709996432 612.3621419984847 613.7672939989716 618.2233480010182 623.2325440011919 626.2617600001395 628.050578000024 630.6298830006272 632.8953460007906 664.4768889993429

Current
Mean: 598.536 ms
Stdev: 21.741 ms (3.6%)
Runs: 560.4016929995269 570.7407229989767 571.6582849994302 573.6770019996911 574.4995929989964 575.501180998981 576.581664999947 578.4412840008736 578.727660998702 583.6716719996184 585.7487389985472 586.4422609992325 587.5176599994302 589.7054860014468 594.2486990001053 595.2033700011671 596.3233639989048 597.5727540012449 599.8557540010661 601.5423990003765 602.0139979999512 602.5798749998212 611.8054610006511 615.2024329993874 617.7163490001112 617.9875489994884 618.3414709996432 618.3424479998648 618.4837650004774 623.8656819984317 638.3688150011003 642.0340580008924 646.8836669996381
App start runJsBundle Baseline
Mean: 204.344 ms
Stdev: 28.557 ms (14.0%)
Runs: 160 167 171 172 172 173 181 181 181 187 190 191 192 192 195 197 198 204 206 207 213 215 217 219 223 225 230 240 255 257 262 266

Current
Mean: 195.125 ms
Stdev: 17.394 ms (8.9%)
Runs: 166 175 175 175 176 177 181 181 182 182 186 190 190 191 191 192 194 195 195 195 195 197 200 201 206 213 214 217 222 223 223 244

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/puneetlath in version: 1.2.63-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Feb 1, 2023

🚀 Deployed to production by https://github.com/thienlnam in version: 1.2.63-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@kidroca
Copy link
Contributor

kidroca commented Feb 7, 2023

@puneetlath you're spot on:

@marcaaron I'm curious though, I think this bug only happens because we only have one function for pinning/unpinning - togglePinnedState. If we instead had separate functions for pinning and unpinning, with separate API calls, then that would also solve this bug right? And we could still de-dupe the persisted requests.

I think at the time when the original code was implemented we've went over in slack about why we're using union instead of concatenating all the request

In short it's because a lot of duplicate requests accumulated over even a brief period of offline time and then when network has a chance to "breathe" the bandwidth gets eaten up by duplicate requests

I think actions like pin/unpin should be distinct (or at least have a parameter)

If you have a metric about usage in "poor network conditions" you might see if it degraded after merging the current PR

@marcaaron
Copy link
Contributor Author

Thanks for chiming in @kidroca

I think actions like pin/unpin should be distinct (or at least have a parameter)

For what it's worth - the command does have a parameter:

API.write('TogglePinnedChat', {
reportID: report.reportID,
pinnedValue,

It seems like there was already an issue with the implementation as were de-duplicating the requests by preferring the older actions vs. the most recent.

I generally agree with the sentiment that we are not yet finished improving the way this stuff works and it can always be better.

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.

5 participants