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

[GraphQL Replication] Only create a new push sequence if necessary #3627

Merged
merged 5 commits into from Jan 16, 2022

Conversation

hdwatts
Copy link
Contributor

@hdwatts hdwatts commented Jan 16, 2022

This PR contains:

  • A bugfix

Describe the problem you have without this PR

I was working on a project using GraphQL replication without subscriptions and I left a browser tab open for an ungodly amount of time. To my surprise, my local storage had gotten bloated to the point where I couldn't access it anymore. While investigating I noticed that thousands of push-checkpoints were being created.

image

Indeed, I was able to confirm that a new push-checkpoint was made on every sync operation. @pubkey confirmed in Gitter that this is indeed a bug.

My solution is to surround the setLastPushSequence with the same check that is in the catch block above. This works well in my testing. I'll note that this doesn't technically solve the memory leak and is only kicking it down the road to when thousands of pushes have been required. I would think there should be a cleanup operation on successful pulls that remove old and irrelevant push checkpoints - but this is early in my tooling with rxdb and I'm very unfamiliar with both this codebase as well the intricacies of pouchdb.

In my testing, this is still working great and the records are no longer being created unnecessarily.

@pubkey wouldn't mind your assistance on if this is the right solution and what the best practice for the test should be

Todos

  • Testing
  • Changelog

@pubkey
Copy link
Owner

pubkey commented Jan 16, 2022

Change looks good.
We need at least one unit test.
Maybe you could subscribe to collection.$ and count the writes to local documents. Then in the test you can make sure to not do more writes to local documents then needed.

Also we should apply the same change to the replication primitives https://github.com/pubkey/rxdb/blob/master/src/plugins/replication/index.ts#L397

This will not solve the problem of pouchdb filling up the disc space with previous revisions of documents. That can be solved by running a compaction https://pouchdb.com/guides/compact-and-destroy.html#compacting-a-database or by setting auto_compaction: true

@hdwatts
Copy link
Contributor Author

hdwatts commented Jan 16, 2022

Thanks @pubkey - I've added tests that check to make sure the lastPushSequence isn't getting updated in both the replication primitives and graphql test suites. I've also implemented a potential fix in the replication primitives codebase.

Separately - thank youfor the compacting docs! This is exactly what I was looking for. As stated in my PR I'm still dipping my toes into PouchDB in general, so this is very helpful insight.

Let me know how else I can assist and I hope you have a nice Sunday

@pubkey
Copy link
Owner

pubkey commented Jan 16, 2022

Code looks good.
Somehow the CI is failing now at another test, I am an not sure why.

@hdwatts
Copy link
Contributor Author

hdwatts commented Jan 16, 2022

I'm investigating - it does appear to be failing for a legitimate reason caused by my code change.

@hdwatts
Copy link
Contributor Author

hdwatts commented Jan 16, 2022

@pubkey I've adjusted the logic and now all tests are passing locally

REPLICATION_IDENTIFIER_TEST
);
// call .run() often
await Promise.all(
Copy link
Owner

@pubkey pubkey Jan 16, 2022

Choose a reason for hiding this comment

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

Multiple calls to run() are automatically de-duplicated so that run() does never run in parallel.
Instead of using Promise.all() you should await each call before starting the next one.

for(var i = 0; i < 3; i++){
    await replicationState.run();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks - made the change and will push it once the current CI is finished

@pubkey pubkey merged commit b595d38 into pubkey:master Jan 16, 2022
pubkey added a commit that referenced this pull request Jan 16, 2022
@pubkey
Copy link
Owner

pubkey commented Jan 16, 2022

Great work, I merged it. Thank you.

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

2 participants