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

Add a workaround to prevent retrieving removed documents from MemDB #565

Merged
merged 1 commit into from
Jun 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func New(
}

logging.DefaultLogger().Infof(
"backend created: id: %s, rpc: %s: db: %s",
"backend created: id: %s, rpc: %s",
serverInfo.ID,
dbInfo,
)
Expand Down
15 changes: 14 additions & 1 deletion server/backend/database/memory/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,20 @@ func (d *DB) FindDocInfoByKeyAndOwner(
txn := d.db.Txn(true)
defer txn.Abort()

raw, err := txn.First(tblDocuments, "project_id_key_removed_at", projectID.String(), key.String(), gotime.Time{})
// TODO(hackerwins): Removed documents should be filtered out by the query, but
// somehow it does not work. This is a workaround.
// val, err := txn.First(tblDocuments, "project_id_key_removed_at", projectID.String(), key.String(), gotime.Time{})
iter, err := txn.Get(tblDocuments, "project_id_key_removed_at", projectID.String(), key.String(), gotime.Time{})
if err != nil {
return nil, fmt.Errorf("find document by key: %w", err)
}
var raw interface{}
for val := iter.Next(); val != nil; val = iter.Next() {
if val != nil && val.(*database.DocInfo).RemovedAt.IsZero() {
raw = val
}
}

if err != nil {
return nil, fmt.Errorf("find document by key: %w", err)
}
Expand Down
36 changes: 13 additions & 23 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ func RunFindDocInfosByQueryTest(
keys = append(keys, info.Key.String())
}

// sort.Strings(keys)

assert.EqualValues(t, []string{
"test", "test abc", "test$0", "test$3", "test-search",
"test0", "test1", "test10", "test11", "test2"}, keys)
Expand Down Expand Up @@ -627,49 +625,41 @@ func RunFindDocInfosByPagingTest(t *testing.T, db database.Database, projectID t
func RunCreateChangeInfosTest(t *testing.T, db database.Database, projectID types.ID) {
t.Run("set RemovedAt in docInfo test", func(t *testing.T) {
ctx := context.Background()
docKey := key.Key(fmt.Sprintf("tests$%s", t.Name()))
docKey := helper.TestDocKey(t)

// 01. Create a client and a document then attach the document to the client.
clientInfo, _ := db.ActivateClient(ctx, projectID, t.Name())
docInfo, _ := db.FindDocInfoByKeyAndOwner(ctx, projectID, clientInfo.ID, docKey, true)
assert.NoError(t, clientInfo.AttachDocument(docInfo.ID))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo, docInfo))

doc := document.New(key.Key(t.Name()))
pack := doc.CreateChangePack()

// Set RemovedAt in docInfo and store changes
err := db.CreateChangeInfos(ctx, projectID, docInfo, 0, pack.Changes, true)
// 02. Remove the document and check the document is removed.
err := db.CreateChangeInfos(ctx, projectID, docInfo, 0, []*change.Change{}, true)
assert.NoError(t, err)

// Check whether RemovedAt is set in docInfo
docInfo, err = db.FindDocInfoByID(ctx, projectID, docInfo.ID)
assert.NoError(t, err)
assert.Equal(t, false, docInfo.RemovedAt.IsZero())
})

t.Run("reuse same key to create docInfo test ", func(t *testing.T) {
ctx := context.Background()
docKey := key.Key(fmt.Sprintf("tests$%s", t.Name()))
docKey := helper.TestDocKey(t)

// 01. Create a client and a document then attach the document to the client.
clientInfo1, _ := db.ActivateClient(ctx, projectID, t.Name())
docInfo1, _ := db.FindDocInfoByKeyAndOwner(ctx, projectID, clientInfo1.ID, docKey, true)
assert.NoError(t, clientInfo1.AttachDocument(docInfo1.ID))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo1, docInfo1))

doc := document.New(key.Key(t.Name()))
pack := doc.CreateChangePack()

// Set RemovedAt in docInfo and store changes
err := db.CreateChangeInfos(ctx, projectID, docInfo1, 0, pack.Changes, true)
// 02. Remove the document.
assert.NoError(t, clientInfo1.RemoveDocument(docInfo1.ID))
err := db.CreateChangeInfos(ctx, projectID, docInfo1, 0, []*change.Change{}, true)
assert.NoError(t, err)

// Use same key to create docInfo
clientInfo2, _ := db.ActivateClient(ctx, projectID, t.Name())
docInfo2, _ := db.FindDocInfoByKeyAndOwner(ctx, projectID, clientInfo2.ID, docKey, true)
assert.NoError(t, clientInfo2.AttachDocument(docInfo2.ID))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo2, docInfo2))

// Check they have same key but different id
// 03. Create a document with same key and check they have same key but different id.
docInfo2, _ := db.FindDocInfoByKeyAndOwner(ctx, projectID, clientInfo1.ID, docKey, true)
assert.NoError(t, clientInfo1.AttachDocument(docInfo2.ID))
assert.NoError(t, db.UpdateClientInfoAfterPushPull(ctx, clientInfo1, docInfo2))
assert.Equal(t, docInfo1.Key, docInfo2.Key)
assert.NotEqual(t, docInfo1.ID, docInfo2.ID)
})
Expand Down