Skip to content

fix: improve peer reconciliation #1901

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

Merged
merged 21 commits into from
Apr 16, 2025
Merged

fix: improve peer reconciliation #1901

merged 21 commits into from
Apr 16, 2025

Conversation

gdorsi
Copy link
Contributor

@gdorsi gdorsi commented Apr 11, 2025

fixes #1871

Copy link

vercel bot commented Apr 11, 2025

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

Name Status Preview Comments Updated (UTC)
clerk-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
file-upload-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
form-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
gcmp-homepage ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
image-upload-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-chat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-chat-1 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-chat-2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-homepage ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-inspector ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-multi-cursors ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-pets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
jazz-todo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
music-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
passkey-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
passphrase-auth-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
passwords-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am
reactions-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2025 9:11am

const prevPeer = this.peers[peer.id];
const peerState = new PeerState(peer, prevPeer?.knownStates);
this.peers[peer.id] = peerState;

if (prevPeer && !prevPeer.closed) {
prevPeer.gracefulShutdown();
await prevPeer.processMessages?.catch((e) => {});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to wait for the previous peer to process all the incoming messages before starting to process the new peer messages.

This is probably why, on frequent reconnections, the sync server starts to have troubles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quick fix, doesn't handle more than one peers in line to be added.

Will improve in the final version of the PR

value: coValue.knownState(),
});
}
}

addPeer(peer: Peer) {
async startPeerReconciliation(peer: PeerState) {
Copy link
Contributor Author

@gdorsi gdorsi Apr 11, 2025

Choose a reason for hiding this comment

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

Before we were sending the content without waiting for the known state from the server.

Done some changes so we just send a load request for each available coValue to the server.

Added some logic to make the server request the content if they notice that they miss some content from the client.

The downside is that we now wait for the server response before sending the content, but we should send less messages around (in theory)

{
from: "client",
msg: map.core.newContentSince(undefined)?.[0],
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We send the same content twice 🤔

action: "content",
...map.core.newContentSince(knownStateBefore)?.[0],
},
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We send the same content twice 🤔


await account.waitForAllCoValuesSync();

return account;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed this change because the content reconciliation now requires more than one roundtrip.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for catching it

Copy link
Contributor

Jazz pre-release

Packages:

{
    "cojson": "https://pkg.pr.new/garden-co/jazz/cojson@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "cojson-storage": "https://pkg.pr.new/garden-co/jazz/cojson-storage@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "cojson-storage-sqlite": "https://pkg.pr.new/garden-co/jazz/cojson-storage-sqlite@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "create-jazz-app": "https://pkg.pr.new/garden-co/jazz/create-jazz-app@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "cojson-transport-ws": "https://pkg.pr.new/garden-co/jazz/cojson-transport-ws@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "cursor-docs": "https://pkg.pr.new/garden-co/jazz/cursor-docs@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "cojson-storage-indexeddb": "https://pkg.pr.new/garden-co/jazz/cojson-storage-indexeddb@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "hash-slash": "https://pkg.pr.new/garden-co/jazz/hash-slash@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-auth-clerk": "https://pkg.pr.new/garden-co/jazz/jazz-auth-clerk@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-browser": "https://pkg.pr.new/garden-co/jazz/jazz-browser@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-browser-media-images": "https://pkg.pr.new/garden-co/jazz/jazz-browser-media-images@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-expo": "https://pkg.pr.new/garden-co/jazz/jazz-expo@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-inspector": "https://pkg.pr.new/garden-co/jazz/jazz-inspector@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-nodejs": "https://pkg.pr.new/garden-co/jazz/jazz-nodejs@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react": "https://pkg.pr.new/garden-co/jazz/jazz-react@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react-auth-clerk": "https://pkg.pr.new/garden-co/jazz/jazz-react-auth-clerk@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react-core": "https://pkg.pr.new/garden-co/jazz/jazz-react-core@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react-native": "https://pkg.pr.new/garden-co/jazz/jazz-react-native@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react-native-core": "https://pkg.pr.new/garden-co/jazz/jazz-react-native-core@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-react-native-media-images": "https://pkg.pr.new/garden-co/jazz/jazz-react-native-media-images@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-run": "https://pkg.pr.new/garden-co/jazz/jazz-run@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-svelte": "https://pkg.pr.new/garden-co/jazz/jazz-svelte@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-tools": "https://pkg.pr.new/garden-co/jazz/jazz-tools@fa1f15d3afac9a6573699d252cbd8e416094cfbc",
    "jazz-vue": "https://pkg.pr.new/garden-co/jazz/jazz-vue@fa1f15d3afac9a6573699d252cbd8e416094cfbc"
}

View Commit

@gdorsi gdorsi merged commit 4296069 into main Apr 16, 2025
34 checks passed
@gdorsi gdorsi deleted the fix/peer-reconciliation branch April 16, 2025 13:43
@github-project-automation github-project-automation bot moved this to Done in 🚢 Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Improve the reconciliation between peers when calling addPeer
3 participants