Skip to content

[HOLD for payment 2024-05-09] HIGH: [API Reliability] Prevent simultaneous calls to GetMissingOnyxMessages using a deferredUpdates queue #38748

Closed

Description

@danieldoglas

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Background:

An onyxUpdate is content we receive through https responses or Pusher that will update the state the local data. They will only bring a smaller piece of the information that was updated, instead of constantly doing full fetches to update everything.

To guarantee that the data is in a correct state, we need those updates to be applied in order. So every update that will modify local data will have previousUpdateID and lastUpdateID. For every new update we receive, we check the received previousUpdateID with the local lastUpdateID.

If they are the same, that means that we can apply that update and we'll have a correct state of the data. If they're not, then we need to fetch the missing updates (using GetMissingOnyxMessages with an updateIDFrom with the local lastUpdateID and updateIDTo with the previousUpdateID that was received from the server.

Once we have those updates applied, we can then apply the received update.

We have onyxUpdates being applied both through http responses and Pusher (calls here and here). When we call saveUpdateInformation, we we're triggering this callback function that will pause the SequentialQueue (so we don't receive more onyxUpdates until this is finished), fetch the missing updates, apply the pending updates, and then unpause the SequentialQueue. But we can't do that with onyxUpdates that comes from pusher.

Problem:

When we receive onyxUpdates, we'll trigger one GetMissingOnyxMessages per update out of order that comes by.

Scenario:

  1. Alice posts to #big-room to create updateID=10
  2. Bob posts to #small-room two times, to create updateID=11 and 12
  3. Cathy is part of both rooms, and has a local lastUpdateID=9
  4. Cathy receives 11 -- sends GetMissingOnyxUpdates. This call might takes a few seconds.
  5. Cathy receives 12 before (4) is finished. So the client sends GetMissingOnyxUpdates again
  6. Once (4) finishes and it's applied locally, the response for GetMissingOnyxUpdates on (5) will be discarded.

In this scenario, we did 2 GetMissingOnyxMessages, when 1 would be enough. Please note that this is not limited to 2 calls today: if we had received 10 messages, it would do 10 API calls.

In this scenario, we did 3 GetMissingOnyxMessages.

Possible solution:

What I'm proposing is that we implement 3 changes.

  1. Only execute one GetMissingOnyxMessages call at a time;
  2. Implement a deferredUpdates queue that will hold all updates that can't be applied immediately that will arrive while the call on (1) is being executed on an ordered manner. Then apply them if the pre-requisites are filled OR do another GetMissingOnyxMessages if we still find gaps on them
  3. Change the way we deal with received updates. If we can apply it immediately, we should trigger a check on the deferredUpdates queue to confirm if any of the held updates can be applied after we apply the received one.

By doing those, the expected behavior for the same scenario is:

  1. Alice posts to #big-room to create updateID=10
  2. Bob posts to #small-room three times, to create updateID=11, 12 and 13
  3. Cathy is part of both rooms, and has a local lastUpdateID=9
  4. Cathy receives 11 -- sends GetMissingOnyxUpdates. This call takes a few seconds.
  5. Cathy receives 12 and 13 before (4) is finished. So the client stores those updates in the deferredUpdates queue. It's important to note that these updates can arrive unordered.
  6. Once (4) finishes and it's applied locally, we get all updates stored in deferredUpdates and apply them, in order. If we detect any gaps in the received updates, we need to go back to step (4)

In this scenario, we only did 1 GetMissingOnyxUpdates.

Issue OwnerCurrent Issue Owner: @lschurr

Activity

  1. Szymon20000 commented on Mar 21, 2024

    @Szymon20000
    Contributor

    Comment :)

  2. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Mar 21, 2024
  3. changed the title [-][HIGH] Rework the current logic for GetMissingOnyxMessages[/-] [+][$500] [HIGH] Rework the current logic for GetMissingOnyxMessages[/+] on Mar 21, 2024
  4. melvin-bot commented on Mar 21, 2024

    @melvin-bot
  5. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Mar 21, 2024
  6. melvin-bot commented on Mar 21, 2024

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @sobitneupane (External)

  7. danieldoglas commented on Mar 21, 2024

    @danieldoglas
    ContributorAuthor

    No need for C+ on this one!

  8. changed the title [-][$500] [HIGH] Rework the current logic for GetMissingOnyxMessages[/-] [+][HIGH] Rework the current logic for GetMissingOnyxMessages[/+] on Mar 21, 2024
  9. 48 remaining items

  10. changed the title [-] HIGH: [Reliability] Prevent simultaneous calls to `GetMissingOnyxMessages` using a `deferredUpdates` queue[/-] [+] HIGH: [API Reliability] Prevent simultaneous calls to `GetMissingOnyxMessages` using a `deferredUpdates` queue[/+] on Apr 27, 2024
  11. danieldoglas commented on Apr 29, 2024

    @danieldoglas
    ContributorAuthor

    Hopefully we'll merge this PR again tomorrow, and we won't face any issues this time 🙏🏾

  12. added
    Awaiting PaymentAuto-added when associated PR is deployed to production
    and removed on Apr 29, 2024
  13. changed the title [-] HIGH: [API Reliability] Prevent simultaneous calls to `GetMissingOnyxMessages` using a `deferredUpdates` queue[/-] [+][HOLD for payment 2024-05-09] HIGH: [API Reliability] Prevent simultaneous calls to `GetMissingOnyxMessages` using a `deferredUpdates` queue[/+] on May 2, 2024
  14. melvin-bot commented on May 2, 2024

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 1.4.69-2 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2024-05-09. 🎊

    For reference, here are some details about the assignees on this issue:

    • @hungvu193 requires payment (Needs manual offer from BZ)
    • @chrispader does not require payment (Contractor)
    • @eh2077 requires payment (Needs manual offer from BZ)
  15. melvin-bot commented on May 2, 2024

    @melvin-bot

    BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    • [@hungvu193 / @eh2077] The PR that introduced the bug has been identified. Link to the PR:
    • [@hungvu193 / @eh2077] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:
    • [@hungvu193 / @eh2077] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:
    • [@hungvu193 / @eh2077] Determine if we should create a regression test for this bug.
    • [@hungvu193 / @eh2077] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
    • [@lschurr] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:
  16. lschurr commented on May 8, 2024

    @lschurr
    Contributor

    Payment summary:

    There were two reviewers on this PR. Both are due $500

  17. lschurr commented on May 9, 2024

    @lschurr
    Contributor

    All paid!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions