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

feat: fallback to NoopProvider if OOM happens #485

Merged
merged 15 commits into from
Mar 26, 2024

Conversation

kirillzyusko
Copy link
Contributor

@kirillzyusko kirillzyusko commented Feb 29, 2024

Details

A follow up for #439

This PR accumulates al changes from #483 and #475 and adds a usage of MemoryOnlyProvider in unit-tests.

Originally #483 and #475 were reverted - in this PR I added fixes:

  • c461b99 <- fixes a crash when this is undefined (which prevented a synchronization between tabs).
  • 42c42ea <- fixes infinite initialization

In this PR I added [1/3]:

  • all calls to providers are managed via additional Storage layer (in future this layer will intercept errors and will substitute provider to InMemory);
  • synchronization between tabs is not a responsibility of provider anymore - now it's handled in separate InstanceSync class/object - such approach allows us to avoid code duplication if we add InMemory provider on web;
  • all providers are initialized lazily (all providers got a new init method);

In this PR I added [2/3]:

  • name property to all providers (used in logger if we ran into OOM);
  • added NoopProvider that is used as a fallback one when OOM happens;
  • added tryOrDegradePerformance function that attempt to perform operation and in case of specific error substitutes the storage to a fallback one;

In this PR I added [3/3]:

  • MemoryOnlyProvider implementation that stores all info in memory;
  • re-worked mocks and used MemoryOnlyProvider there;

Related Issues

Expensify/App#29403

Automated Tests

This PR changes the way we use the storage but the functionality of the library to the outer world is the same. Therefore no new tests were added.

Manual Tests

Verify that no flows and functionality were broken by the changes. Check for console errors regarding Onyx.

Author Checklist

  • I linked the correct issue in the ### Related 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 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 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 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
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • 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 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 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.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
MacOS: Desktop

@kirillzyusko kirillzyusko requested a review from a team as a code owner February 29, 2024 13:17
@melvin-bot melvin-bot bot requested review from hayata-suenaga and removed request for a team February 29, 2024 13:17
@kirillzyusko kirillzyusko force-pushed the feat/split-up-in-memory-pr-part-3 branch from 3e8ee71 to 6ba2711 Compare February 29, 2024 14:24
@hayata-suenaga
Copy link
Contributor

@marcaaron @tgolen I assigned you as you were reviewer of the closed PR and because you're familiar with the Onyx library

@kirillzyusko
Copy link
Contributor Author

It's not needed to review now, because it contains changes from part 2. Let's review & merge part 2 and after that I'll rebase this Pr and we can review it 👀

@kirillzyusko kirillzyusko force-pushed the feat/split-up-in-memory-pr-part-3 branch 2 times, most recently from 7725163 to d400ef1 Compare March 4, 2024 19:01
@kirillzyusko
Copy link
Contributor Author

@danieldoglas @tgolen this PR is ready for review 😊

marcaaron
marcaaron previously approved these changes Mar 4, 2024
Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

Changes LGTM - I agree with @hayata-suenaga that we do not need to measure the size of the store (or at least can't think of any useful reason to do it).

lib/storage/providers/MemoryOnlyProvider.ts Show resolved Hide resolved
lib/storage/providers/MemoryOnlyProvider.ts Show resolved Hide resolved
@marcaaron marcaaron dismissed their stale review March 4, 2024 23:14

Hit the wrong button here :sweat-smile:

@kirillzyusko
Copy link
Contributor Author

I agree with @hayata-suenaga that we do not need to measure the size of the store (or at least can't think of any useful reason to do it).

@marcaaron so do you want to return a mocked value from getDatabaseSize without object-sizeof usage?

tgolen
tgolen previously approved these changes Mar 5, 2024
hayata-suenaga
hayata-suenaga previously approved these changes Mar 5, 2024
Copy link
Contributor

@hayata-suenaga hayata-suenaga left a comment

Choose a reason for hiding this comment

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

the code looks good to me

there is a pending question

danieldoglas
danieldoglas previously approved these changes Mar 5, 2024
@marcaaron
Copy link
Contributor

@marcaaron so do you want to return a mocked value from getDatabaseSize without object-sizeof usage?

yeah, it does not need to be accurate so I'd rather not add a dependency if we don't need one.

@kirillzyusko
Copy link
Contributor Author

yeah, it does not need to be accurate so I'd rather not add a dependency if we don't need one.

@marcaaron okay, make sense - I'll remove it 🙌

marcaaron
marcaaron previously approved these changes Mar 6, 2024
@kirillzyusko kirillzyusko force-pushed the feat/split-up-in-memory-pr-part-3 branch from 56e7087 to c461b99 Compare March 6, 2024 17:55
@kirillzyusko kirillzyusko marked this pull request as draft March 6, 2024 17:55
@kirillzyusko kirillzyusko changed the title feat: added MemoryOnlyProvider [3/3] feat: added MemoryOnlyProvider Mar 6, 2024
@hayata-suenaga
Copy link
Contributor

let us know when the RR becomes ready for review again 🙇

lib/storage/index.ts Outdated Show resolved Hide resolved
@tgolen
Copy link
Collaborator

tgolen commented Mar 8, 2024

I got word from QA this morning that they have begun testing the E/App PR. They will report any bugs they find on that PR and we can triage them from there.

@kirillzyusko kirillzyusko force-pushed the feat/split-up-in-memory-pr-part-3 branch 2 times, most recently from a2ea02e to ce04390 Compare March 15, 2024 18:32
@kirillzyusko kirillzyusko force-pushed the feat/split-up-in-memory-pr-part-3 branch from ce04390 to 8254d59 Compare March 25, 2024 13:12
@kirillzyusko
Copy link
Contributor Author

@mountiny @tgolen just a friendly ping to check the status of this PR 👀

I think QA have tested and didn't find anything new (only old issues that can be reproduced in any preview deploys - CORS issues).

Just curious whether it can this PR be merged or not?

@tgolen
Copy link
Collaborator

tgolen commented Mar 26, 2024

OK, I took one last look at the code and it looks good to me. I'm glad QA hasn't found any more issues with this and sounds like it's ready to be released.

@marcaaron @danieldoglas @hayata-suenaga I wouldn't mind having at least one more approval before merging this. Would one of you like the honors?

@hayata-suenaga
Copy link
Contributor

thank you for the review, Tim! Unfortunately, I don't have time to go over the code again, so I'd wait for Daniel or Marc 😄

@marcaaron
Copy link
Contributor

Sure, taking a look!

Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

I have a small note. I am not sure if it's worth blocking on and this PR has been open for quite a while so suggesting a follow up to address it instead.

* Degrade performance by removing the storage provider and only using cache
*/
function degradePerformance(error: Error) {
Logger.logAlert(`Error while using ${provider.name}. Falling back to only using cache and dropping storage.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB, it would be useful to log the error message as well. Without knowing anything about why we fell back to using the cache this alert is not really actionable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I always got tripped up on this too, but it is logged on the very next line 😁

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, but maybe you mean to send the error as part of logAlert()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep! That's what I meant. I do see the console.error() which is local (good) but might be desirable to have server logs.

I backpedaled here because I'm not sure if we are going to be tracking the frequency of these errors or doing anything about them.

That said, I also wonder if logging alert will lead to issues that nobody can action... I am not sure if the JS logs create issues or not. Maybe they should and if so - this could probably be a hmmm().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tgolen @marcaaron should I do these changes in follow up PR? 👀

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I think it would probably be good to change this to a hmmm() and also log the specific error. It would sure be nice to look at server logs to determine IF the fallback is happening and also WHY.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tgolen make sense! I've created a follow up PR: #524

@marcaaron marcaaron merged commit 879d653 into Expensify:main Mar 26, 2024
4 checks passed
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.

None yet

5 participants