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

Lock watchDocuments depending on the client #479

Merged
merged 3 commits into from
Mar 3, 2023
Merged

Conversation

chacha912
Copy link
Contributor

What this PR does / why we need it:

If a client requests to attach to multiple documents simultaneously,
the existing connection will be disconnected and reconnected.

Depending on the order of the published events, problems may occur on the client.
If the watched event is published before the unwatched event, the test will fail
since the final state is watched, but the unwatched event arrives later.
(please refer to the yorkie-js-sdk test).

image

This image shows the server log when the watched event is published first.

To resolve this, I added a lock for each client in the watchDocument.
This ensures that a lock is set when a client establishes a stream and released when the client unwatches.

Which issue(s) this PR fixes:

Fixes yorkie-team/yorkie-js-sdk#464 (comment)

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

@chacha912 chacha912 requested a review from hackerwins March 2, 2023 12:06
@codecov
Copy link

codecov bot commented Mar 2, 2023

Codecov Report

Merging #479 (d4b6758) into main (bc3f59b) will decrease coverage by 0.06%.
The diff coverage is 22.72%.

@@            Coverage Diff             @@
##             main     #479      +/-   ##
==========================================
- Coverage   46.56%   46.51%   -0.06%     
==========================================
  Files          69       69              
  Lines        5788     5805      +17     
==========================================
+ Hits         2695     2700       +5     
- Misses       2785     2797      +12     
  Partials      308      308              
Impacted Files Coverage Δ
server/backend/sync/memory/pubsub.go 47.11% <0.00%> (-0.46%) ⬇️
server/rpc/yorkie_server.go 46.42% <0.00%> (-1.72%) ⬇️
pkg/document/key/key.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@hackerwins hackerwins 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 your contribution.

I left a few comments.

Comment on lines 395 to 415
locker, err := s.backend.Coordinator.NewLocker(
stream.Context(),
sync.NewKey(cli.ID.String()),
)
if err != nil {
return err
}
if err := locker.Lock(stream.Context()); err != nil {
return err
}

subscription, peersMap, err := s.watchDocs(stream.Context(), *cli, docKeys)
if err != nil {
logging.From(stream.Context()).Error(err)
return err
}
defer func() {
s.unwatchDocs(docKeys, subscription)
if err := locker.Unlock(stream.Context()); err != nil {
logging.DefaultLogger().Error(err)
}
Copy link
Member

@hackerwins hackerwins Mar 3, 2023

Choose a reason for hiding this comment

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

  1. If an error is returned by s.watchDocs, releasing the lock is not executed. So, How about moving locker.Unlock to just below locker.lock with defer?

  2. locker.Unlock may not be executed when the stream is terminated and the context is in the cancel state. How about using context.Background instead of stream.Context? https://pkg.go.dev/context#pkg-overview

  3. We need to separate the key of the lock with snapshot-* or pushpull-*. How about creating a key with the prefix watchdocs-*?

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@hackerwins hackerwins merged commit dde4f22 into main Mar 3, 2023
@hackerwins hackerwins deleted the watch-documents branch March 3, 2023 09:24
chacha912 added a commit to yorkie-team/yorkie-js-sdk that referenced this pull request Mar 6, 2023
The event order has been modified to be guaranteed
yorkie-team/yorkie#479
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.

2 participants