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

Improve ChangePack creation logic for p.Clear() when document detach #1055

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/backend/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ type Database interface {
docRefKey types.DocRefKey,
) error

// FindChangeInfoByServerSeq returns the change by the given server sequence.
FindChangeInfoByServerSeq(
ctx context.Context,
docRefKey types.DocRefKey,
serverSeq int64,
) (*ChangeInfo, error)

// FindChangesBetweenServerSeqs returns the changes between two server sequences.
FindChangesBetweenServerSeqs(
ctx context.Context,
Expand Down
22 changes: 22 additions & 0 deletions server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,28 @@ func (d *DB) PurgeStaleChanges(
return nil
}

// FindChangeInfoByServerSeq returns the change by the given server sequence.
func (d *DB) FindChangeInfoByServerSeq(
_ context.Context,
docRefKey types.DocRefKey,
serverSeq int64,
) (*database.ChangeInfo, error) {
txn := d.db.Txn(false)
defer txn.Abort()
raw, err := txn.First(tblSnapshots, "doc_id_server_seq",
docRefKey.DocID.String(),
serverSeq,
)
if err != nil {
return nil, fmt.Errorf("find snapshot by serverSeq: %w", err)
}
if raw == nil {
return nil, fmt.Errorf("%s: %w", docRefKey, database.ErrChangeNotFound)
}

return raw.(*database.ChangeInfo).DeepCopy(), nil
}

// FindChangesBetweenServerSeqs returns the changes between two server sequences.
func (d *DB) FindChangesBetweenServerSeqs(
ctx context.Context,
Expand Down
27 changes: 27 additions & 0 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,33 @@ func (c *Client) PurgeStaleChanges(
return nil
}

// FindChangeInfoByServerSeq returns the change by the given server sequence.
func (c *Client) FindChangeInfoByServerSeq(
ctx context.Context,
docRefKey types.DocRefKey,
serverSeq int64,
) (*database.ChangeInfo, error) {
result := c.collection(ColChanges).FindOne(ctx, bson.M{
"project_id": docRefKey.ProjectID,
"doc_id": docRefKey.DocID,
"server_seq": serverSeq,
})

changeInfo := &database.ChangeInfo{}
if result.Err() == mongo.ErrNoDocuments {
return changeInfo, nil
}
if result.Err() != nil {
return nil, fmt.Errorf("find change: %w", result.Err())
}

if err := result.Decode(changeInfo); err != nil {
return nil, fmt.Errorf("decode change: %w", err)
}

return changeInfo, nil
}

// FindChangesBetweenServerSeqs returns the changes between two server sequences.
func (c *Client) FindChangesBetweenServerSeqs(
ctx context.Context,
Expand Down
34 changes: 19 additions & 15 deletions server/rpc/cluster_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"github.com/yorkie-team/yorkie/api/types"
api "github.com/yorkie-team/yorkie/api/yorkie/v1"
"github.com/yorkie-team/yorkie/pkg/document"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/change"
"github.com/yorkie-team/yorkie/pkg/document/innerpresence"
"github.com/yorkie-team/yorkie/pkg/document/presence"
"github.com/yorkie-team/yorkie/pkg/document/time"
"github.com/yorkie-team/yorkie/server/backend"
Expand Down Expand Up @@ -93,29 +94,32 @@ func (s *clusterServer) DetachDocument(
return nil, err
}

// TODO(hackerwins): BuildDocForCheckpoint is expensive because it reads the entire document.
// We need to optimize this by creating a ChangePack directly.
// 01. Create ChangePack with clear presence.
doc, err := packs.BuildDocForCheckpoint(ctx, s.backend, docInfo, clientInfo.Checkpoint(summary.ID), actorID)
// 02. Create changePack with presence clear change
cp := clientInfo.Checkpoint(summary.ID)
latestChange, err := s.backend.DB.FindChangeInfoByServerSeq(ctx, docRefKey, cp.ServerSeq)
if err != nil {
return nil, err
}

if err := doc.Update(func(root *json.Object, p *presence.Presence) error {
p.Clear()
return nil
}); err != nil {
return nil, err
}

// 02. PushPull with the created ChangePack.
changeCtx := change.NewContext(
change.NewID(cp.ClientSeq, cp.ServerSeq, latestChange.Lamport, actorID, latestChange.VersionVector).Next(),
"",
nil,
)
p := presence.New(changeCtx, innerpresence.NewPresence())
p.Clear()

changes := []*change.Change{changeCtx.ToChange()}
pack := change.NewPack(docInfo.Key, cp, changes, nil, nil)

// 03. PushPull with the created ChangePack.
if _, err := packs.PushPull(
ctx,
s.backend,
project,
clientInfo,
docInfo,
doc.CreateChangePack(),
//doc.CreateChangePack(),
pack,
packs.PushPullOptions{
Mode: types.SyncModePushPull,
Status: document.StatusDetached,
Expand Down
Loading