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

Fix missing document detachments when client is deactivated #907

Merged
merged 23 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
17 changes: 17 additions & 0 deletions server/backend/database/client_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
ErrDocumentNeverAttached = errors.New("client has never attached the document")
ErrDocumentAlreadyAttached = errors.New("document already attached")
ErrDocumentAlreadyDetached = errors.New("document already detached")
ErrAttachedDocumentExists = errors.New("attached document exits when deactivated")
)

// Below are statuses of the client.
Expand Down Expand Up @@ -223,6 +224,22 @@ func (i *ClientInfo) EnsureDocumentAttached(docID types.ID) error {
return nil
}

// EnsureDocumentsDetachedWhenDeactivated checks if there are no documents attached to the deactivated client.
func (i *ClientInfo) EnsureDocumentsDetachedWhenDeactivated() error {
for docID := range i.Documents {
isAttached, err := i.IsAttached(docID)
if err != nil {
return err
}

if isAttached {
return ErrAttachedDocumentExists
}
}

return nil
}
hackerwins marked this conversation as resolved.
Show resolved Hide resolved

// DeepCopy returns a deep copy of this client info.
func (i *ClientInfo) DeepCopy() *ClientInfo {
if i == nil {
Expand Down
17 changes: 17 additions & 0 deletions server/backend/database/client_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,21 @@ func TestClientInfo(t *testing.T) {
err = clientInfo.AttachDocument(dummyDocID, false)
assert.ErrorIs(t, err, database.ErrDocumentAlreadyAttached)
})

t.Run("document detached when client deactivate test", func(t *testing.T) {
clientInfo := database.ClientInfo{
Status: database.ClientActivated,
}

err := clientInfo.AttachDocument(dummyDocID, false)
assert.NoError(t, err)
isAttached, err := clientInfo.IsAttached(dummyDocID)
assert.NoError(t, err)
assert.True(t, isAttached)

clientInfo.Deactivate()

err = clientInfo.EnsureDocumentsDetachedWhenDeactivated()
assert.Equal(t, database.ErrAttachedDocumentExists, err)
})
}
31 changes: 25 additions & 6 deletions server/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Deactivate(
return nil, err
}

for docID, clientDocInfo := range clientInfo.Documents {
for docID := range clientInfo.Documents {
isAttached, err := clientInfo.IsAttached(docID)
if err != nil {
return nil, err
Expand All @@ -63,15 +63,34 @@ func Deactivate(
continue
}

docInfo, err := db.FindDocInfoByRefKey(ctx, types.DocRefKey{
ProjectID: refKey.ProjectID,
DocID: docID,
})
if err != nil {
return nil, err
}

if err := clientInfo.DetachDocument(docID); err != nil {
return nil, err
}

// TODO(hackerwins): We need to remove the presence of the client from the document.
// Be careful that housekeeping is executed by the leader. And documents are sharded
// by the servers in the cluster. So, we need to consider the case where the leader is
// not the same as the server that handles the document.
if err := db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo); err != nil {
return nil, err
}
}

updatedClientInfo, err := db.DeactivateClient(ctx, refKey)
if err != nil {
return nil, err
}

// TODO(hackerwins): We need to remove the presence of the client from the document.
// Be careful that housekeeping is executed by the leader. And documents are sharded
// by the servers in the cluster. So, we need to consider the case where the leader is
// not the same as the server that handles the document.

for docID, clientDocInfo := range clientInfo.Documents {
if err := db.UpdateSyncedSeq(
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
ctx,
clientInfo,
Expand All @@ -85,7 +104,7 @@ func Deactivate(
}
}

return db.DeactivateClient(ctx, refKey)
return updatedClientInfo, err
}

// FindActiveClientInfo find the active client info by the given ref key.
Expand Down
Loading