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

RFC: Implement clean shutdown and in-memory document GC #233

Merged
merged 22 commits into from
Jul 12, 2024

Conversation

rbtying
Copy link
Contributor

@rbtying rbtying commented Jun 28, 2024

I noticed in #228 that the y-sweet binary is currently missing at least best-effort persistence-before-shutdown, and GCing documents out of memory. I understand that y-sweet is currently mostly deployed as CloudFlare workers, so both of those are a bit moot - CloudFlare will kill the process entirely once there aren't any connections for long enough -- but I figured that it might be nice to have something for outside-CloudFlare as well.

This PR implements:

  • slightly more verbose errors (rather than throwing away error details, they're propagated to the response)
  • a shutdown process which attempts to persist all of the documents prior to kiling the process, skipping checkpoint_freq throttling (there's a comment which suggests this is intended?), prior to finishing shutdown
  • a simple garbage collection task per document which looks at the number of copies of the Awareness struct, which is held one per connection + one in the docs map. This is a little janky, but I didn't want to modify y-sweet-core with implementation details of the server. The garbage collection task holds the cancellation token for the persistence worker, so in graceful shutdown scenarios it should always persist the current state.
Copy link

vercel bot commented Jun 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
y-sweet-demos ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2024 0:40am
y-sweet-gendocs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 12, 2024 0:40am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
y-sweet-debugger ⬜️ Ignored (Inspect) Visit Preview Jul 12, 2024 0:40am
Copy link

vercel bot commented Jun 28, 2024

@rbtying is attempting to deploy a commit to the drifting-corp Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@paulgb paulgb left a comment

Choose a reason for hiding this comment

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

Thanks for this! I like the use of the connection token, that seems like a good fit here.

If you rebase on main, the integration tests should pass -- they were breaking because of an unrelated cloudflare workers version issue.

crates/y-sweet/src/server.rs Show resolved Hide resolved
crates/y-sweet/src/server.rs Outdated Show resolved Hide resolved
crates/y-sweet/src/server.rs Show resolved Hide resolved
crates/y-sweet/src/server.rs Outdated Show resolved Hide resolved
crates/y-sweet/src/server.rs Show resolved Hide resolved
crates/y-sweet/src/server.rs Outdated Show resolved Hide resolved
crates/y-sweet/src/server.rs Outdated Show resolved Hide resolved
rbtying and others added 4 commits July 1, 2024 09:01
When the server receives a ctrl-c, stop taking new requests, immediately
trigger a save on all open documents, wait for all the documents to
persist, and then exit.
Copy link
Member

@paulgb paulgb left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for this! One question/comment, and then I'll merge.

Comment on lines 326 to 329
.layer(middleware::from_fn_with_state(
redact_errors,
Self::redact_error_middleware,
));
Copy link
Member

Choose a reason for hiding this comment

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

I like the middleware approach here, but since the redact error middleware is a no-op if redact_errors = false, should we just conditionally not add the middleware in that case and avoid passing through the boolean state?

@rbtying
Copy link
Contributor Author

rbtying commented Jul 8, 2024

Unfortunately it looks like yrs is not thread-safe (y-crdt/y-crdt#373), so I might tinker a bit to see if there's a way to do the yrs subscription drops single-threaded before merging

@rbtying
Copy link
Contributor Author

rbtying commented Jul 11, 2024

Unfortunately it looks like yrs is not thread-safe (y-crdt/y-crdt#373), so I might tinker a bit to see if there's a way to do the yrs subscription drops single-threaded before merging

I think this is unblocked w/ the 0.19 update!

@paulgb paulgb merged commit 665800d into jamsocket:main Jul 12, 2024
8 checks passed
@paulgb
Copy link
Member

paulgb commented Jul 12, 2024

Unfortunately it looks like yrs is not thread-safe (y-crdt/y-crdt#373), so I might tinker a bit to see if there's a way to do the yrs subscription drops single-threaded before merging

I think this is unblocked w/ the 0.19 update!

Merged! Thanks for all your work on getting this across the finish line!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants