Skip to content

sync: read fetch chunks ahead and cut per-fetch round trips - #120

Open
thecodrr wants to merge 1 commit into
masterfrom
sync/fetch-latency
Open

thecodrr wants to merge 1 commit into
masterfrom
sync/fetch-latency

Conversation

@thecodrr

Copy link
Copy Markdown
Contributor

After #116–#118, the fetch tail in prod stayed where it was. Two things in MongoDB's slow-op log explain most of it:

  • Reads wait on the disk. A fetch read a chunk, sent it, and waited for the client before reading the next one, so every chunk paid its storage reads (~270ms for a batch of 230 cold documents) on top of the client's time.
  • Device documents conflict. Every push and fetch wrote the device's LastAccessTime. A device's concurrent calls conflicted on that one document, with ~10 write conflicts per slow update and ~434ms on average.

The fetch duration metric couldn't tell any of this apart from a slow client, because it includes the time the client spends applying each chunk.

Changes

  • FetchSender (new): the chunked sending moved out of the hub, along with sending monographs and inbox items.
    • The next chunk is read while the client applies the current one.
    • Once the client accepts a chunk, the next one is sent before the accepted ids are handed back, so the hub clears them from pending while the client applies the next chunk instead of keeping it waiting.
    • A chunk's items are still not held while the client applies it, so a fetch holds at most two chunks (one being sent, one being read).
  • Metrics: sync.v2.fetch_server_duration and sync.v2.fetch_client_duration, next to the existing sync.v2.fetch_duration, all labelled kind = full or incremental.
  • LastAccessTime: written at most every 5 minutes. Devices are only pruned after months without a sync. On fetch, the call is skipped entirely when the device just loaded is recent.
  • Fewer round trips per fetch:
    • the device read by the hub is reused instead of being read again;
    • monograph and inbox queries are skipped on an incremental fetch with no ids of that type;
    • IsSyncReset is only cleared when it was set. This also stops a fetch from clearing the flag of a device that re-registered while it ran.

Nothing changes for clients: the hub protocol and the messages sent are the same.

Testing

  • dotnet test tests/Notesnook.API.Tests -c Release: 153/153 pass, three runs in a row. New tests cover:
    • FetchSenderTests: read-ahead, a chunk not kept alive while the client applies it, the next chunk going out before ids are handed back, rejected chunks and other items, and client timing.
    • An incremental fetch across several collections that sends every pending item once and clears pending.
    • The new metrics and their kind label.
    • The LastAccessTime throttle.

- move chunked sending into FetchSender, which reads the next chunk while
  the client applies the current one and sends it before handing back the
  accepted ids, so clearing pending ids no longer stalls the client
- split fetch duration into server and client time, labelled full or
  incremental
- write a device's LastAccessTime at most every 5 minutes, and skip the
  call when the loaded device is recent
- reuse the loaded device instead of reading it again, skip monograph and
  inbox queries when there are no ids of that type, and only clear
  IsSyncReset when it was set
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.

1 participant