fix/use latest doc on persist & correct queueing #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes two potential issues:
debouncedPersistDocMap
, which could lead to data missing ifdebouncedPersist
is called when persisting. (Detail explained below)The current flow looks like this:

If the stream is scheduled for persistence, another call to
debouncedPersist
will do nothing but update the doc for persistence.But the updated doc would be dropped if the call to

debouncedPersist
is during the persistence stage:This is because the doc to persist is retrieved at the start of the persistence stage, but the stream key is still in the queue. Calling
debouncedPersist
now would only updatedebouncedPersistDocMap
without scheduling a new persistence operation. The newly updated doc will be cleared after the previous persistence without being saved.In this PR, I restructured the queueing and persisting algorithm to the following:

Where when persisting, we always use the latest doc in the cache. Once the doc to persist is retrieved, we dequeue the current stream key immediately to allow the queueing of new persistence operations.