Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Dec 10, 2024
1 parent 97d679e commit aea43ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 10 additions & 4 deletions pkg/document/change/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func (id ID) SyncClocks(other ID) ID {
lamport = other.lamport + 1
}

// NOTE(chacha912): Handle changes from legacy SDK (v0.5.2 or below) which have empty version vectors.
// Add lamport to version vector to include this change's information.
// NOTE(chacha912): For changes created by legacy SDK prior to v0.5.2 that lack version
// vectors, document's version vector was not being properly accumlated. To address this,
// we generate a version vector using the lamport timestamp when no version vector exists.
otherVV := other.versionVector
if len(otherVV) == 0 {
otherVV = otherVV.DeepCopy()
Expand All @@ -125,8 +126,13 @@ func (id ID) SetClocks(otherLamport int64, vector time.VersionVector) ID {
lamport = otherLamport + 1
}

// NOTE(chacha912): Snapshot's version vector from server contains initialActorID.
// Remove it as it's not an actual participating client.
// NOTE(chacha912): Documents created by server may have an InitialActorID
// in their version vector. Although server is not an actual client, it
// generates document snapshots from changes by participating with an
// InitialActorID during document instance creation and accumulating stored
// changes in DB.
// Semantically, including a non-client actor in the VersionVector is
// problematic. To address this, we remove the InitialActorID from snapshots.
vector.Unset(time.InitialActorID)

newID := NewID(id.clientSeq, id.serverSeq, lamport, id.actorID, id.versionVector.Max(vector))
Expand Down
8 changes: 7 additions & 1 deletion pkg/document/internal_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,13 @@ func (d *InternalDocument) applySnapshot(snapshot []byte, vector time.VersionVec
d.root = crdt.NewRoot(rootObj)
d.presences = presences

// TODO(chacha912): We need to check we can use max lamport as lamport timestamp.
// NOTE(chacha912): Documents created from snapshots were experiencing edit
// restrictions due to low lamport values.
// Previously, the code attempted to generate document Lamport from ServerSeq.
// However, after aligning lamport logic with the original research paper,
// ServerSeq could potentially become smaller than the Lamport value.
// To resolve this, we initialize Document's lamport by using the highest
// lamport value stored in the VersionVector as the starting point.
d.changeID = d.changeID.SetClocks(vector.MaxLamport(), vector)

return nil
Expand Down

0 comments on commit aea43ba

Please sign in to comment.