-
Notifications
You must be signed in to change notification settings - Fork 140
CBG-3690 don't re-read document for resync #7892
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
b98dc00
eb809bf
b417eba
798488e
3ce0f72
5081439
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ const ( | |
| ExistingVersionLegacyRev | ||
| ExistingVersionWithUpdateToHLV | ||
| NoHLVUpdateForTest | ||
| Resync | ||
| ) | ||
|
|
||
| type DocUpdateType uint32 | ||
|
|
@@ -1822,97 +1823,70 @@ func (db *DatabaseCollectionWithUser) getResyncedDocument(ctx context.Context, d | |
| return doc, shouldUpdate, updatedExpiry, doc.Sequence, updatedUnusedSequences, nil | ||
| } | ||
|
|
||
| func (db *DatabaseCollectionWithUser) resyncDocument(ctx context.Context, docid, key string, regenerateSequences bool, unusedSequences []uint64) (updatedHighSeq uint64, updatedUnusedSequences []uint64, err error) { | ||
| func (db *DatabaseCollectionWithUser) resyncDocument(ctx context.Context, docid string, previousDoc *sgbucket.BucketDocument, regenerateSequences bool) (updatedUnusedSequences []uint64, err error) { | ||
| var updatedDoc *Document | ||
| var shouldUpdate bool | ||
| var updatedExpiry *uint32 | ||
| if db.UseXattrs() { | ||
| writeUpdateFunc := func(currentValue []byte, currentXattrs map[string][]byte, cas uint64) (sgbucket.UpdatedDoc, error) { | ||
| // There's no scenario where a doc should from non-deleted to deleted during UpdateAllDocChannels processing, | ||
| // so deleteDoc is always returned as false. | ||
| if currentValue == nil || len(currentValue) == 0 { | ||
| return sgbucket.UpdatedDoc{}, base.ErrUpdateCancel | ||
| } | ||
| doc, err := db.unmarshalDocumentWithXattrs(ctx, docid, currentValue, currentXattrs, cas, DocUnmarshalAll) | ||
| if err != nil { | ||
| return sgbucket.UpdatedDoc{}, err | ||
| } | ||
| updatedDoc, shouldUpdate, updatedExpiry, updatedHighSeq, unusedSequences, err = db.getResyncedDocument(ctx, doc, regenerateSequences, unusedSequences) | ||
| if err != nil { | ||
| return sgbucket.UpdatedDoc{}, err | ||
| } | ||
| if !shouldUpdate { | ||
| return sgbucket.UpdatedDoc{}, base.ErrUpdateCancel | ||
| } | ||
| base.TracefCtx(ctx, base.KeyAccess, "Saving updated channels and access grants of %q", base.UD(docid)) | ||
| if updatedExpiry != nil { | ||
| updatedDoc.UpdateExpiry(*updatedExpiry) | ||
| } | ||
| doc.SetCrc32cUserXattrHash() | ||
| writeUpdateFunc := func(currentValue []byte, currentXattrs map[string][]byte, cas uint64) (sgbucket.UpdatedDoc, error) { | ||
| // resyncDocument is not called on tombstoned documents, so this value will only be empty if the document was | ||
| // deleted between DCP event and calling this function. In any case, we do not need to update it. | ||
| if len(currentValue) == 0 { | ||
| return sgbucket.UpdatedDoc{}, base.ErrUpdateCancel | ||
| } | ||
| doc, err := db.unmarshalDocumentWithXattrs(ctx, docid, currentValue, currentXattrs, cas, DocUnmarshalAll) | ||
| if err != nil { | ||
| return sgbucket.UpdatedDoc{}, err | ||
| } | ||
| updatedDoc, shouldUpdate, updatedExpiry, _, updatedUnusedSequences, err = db.getResyncedDocument(ctx, doc, regenerateSequences, nil) | ||
| if err != nil { | ||
| return sgbucket.UpdatedDoc{}, err | ||
| } | ||
| if !shouldUpdate { | ||
| return sgbucket.UpdatedDoc{}, base.ErrUpdateCancel | ||
| } | ||
| base.TracefCtx(ctx, base.KeyAccess, "Saving updated channels and access grants of %q", base.UD(docid)) | ||
| if updatedExpiry != nil { | ||
| updatedDoc.UpdateExpiry(*updatedExpiry) | ||
| } | ||
| doc.SetCrc32cUserXattrHash() | ||
|
|
||
| // Update MetadataOnlyUpdate based on previous Cas, MetadataOnlyUpdate | ||
| if db.useMou() { | ||
| doc.MetadataOnlyUpdate = computeMetadataOnlyUpdate(doc.Cas, doc.RevSeqNo, doc.MetadataOnlyUpdate) | ||
| } | ||
| // Update MetadataOnlyUpdate based on previous Cas, MetadataOnlyUpdate | ||
| doc.MetadataOnlyUpdate = computeMetadataOnlyUpdate(doc.Cas, doc.RevSeqNo, doc.MetadataOnlyUpdate) | ||
| mouMatch := false // after writing this document, mou.cas != doc.cas since otherwise shouldUpdate would be false | ||
|
||
| doc, err = db.updateHLV(ctx, doc, Resync, mouMatch) | ||
|
||
| if err != nil { | ||
| return sgbucket.UpdatedDoc{}, err | ||
| } | ||
|
|
||
| _, rawSyncXattr, rawVvXattr, rawMouXattr, rawGlobalXattr, err := updatedDoc.MarshalWithXattrs() | ||
| updatedDoc := sgbucket.UpdatedDoc{ | ||
| Doc: nil, // Resync does not require document body update | ||
| Xattrs: map[string][]byte{ | ||
| base.SyncXattrName: rawSyncXattr, | ||
| base.VvXattrName: rawVvXattr, | ||
| }, | ||
| Expiry: updatedExpiry, | ||
| } | ||
| if db.useMou() { | ||
| updatedDoc.Xattrs[base.MouXattrName] = rawMouXattr | ||
| if doc.MetadataOnlyUpdate.HexCAS == expandMacroCASValueString { | ||
| updatedDoc.Spec = append(updatedDoc.Spec, sgbucket.NewMacroExpansionSpec(XattrMouCasPath(), sgbucket.MacroCas)) | ||
| } | ||
| } | ||
| if rawGlobalXattr != nil { | ||
| updatedDoc.Xattrs[base.GlobalXattrName] = rawGlobalXattr | ||
| } | ||
| return updatedDoc, err | ||
| _, rawSyncXattr, rawVvXattr, rawMouXattr, rawGlobalXattr, err := updatedDoc.MarshalWithXattrs() | ||
| updatedDoc := sgbucket.UpdatedDoc{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: This declaration here shadows the declaration on line 1827 |
||
| Doc: nil, // Resync does not require document body update | ||
| Xattrs: map[string][]byte{ | ||
| base.SyncXattrName: rawSyncXattr, | ||
| base.VvXattrName: rawVvXattr, | ||
| base.MouXattrName: rawMouXattr, | ||
| }, | ||
| Expiry: updatedExpiry, | ||
| } | ||
| opts := &sgbucket.MutateInOptions{ | ||
| MacroExpansion: macroExpandSpec(base.SyncXattrName), | ||
| if doc.MetadataOnlyUpdate.HexCAS == expandMacroCASValueString { | ||
| updatedDoc.Spec = append(updatedDoc.Spec, sgbucket.NewMacroExpansionSpec(XattrMouCasPath(), sgbucket.MacroCas)) | ||
| } | ||
| _, err = db.dataStore.WriteUpdateWithXattrs(ctx, key, db.syncGlobalSyncMouRevSeqNoAndUserXattrKeys(), 0, nil, opts, writeUpdateFunc) | ||
| } else { | ||
| _, err = db.dataStore.Update(key, 0, func(currentValue []byte) ([]byte, *uint32, bool, error) { | ||
| // Be careful: this block can be invoked multiple times if there are races! | ||
| if currentValue == nil { | ||
| return nil, nil, false, base.ErrUpdateCancel // someone deleted it?! | ||
| } | ||
| doc, err := unmarshalDocument(docid, currentValue) | ||
| if err != nil { | ||
| return nil, nil, false, err | ||
| } | ||
| updatedDoc, shouldUpdate, updatedExpiry, updatedHighSeq, unusedSequences, err = db.getResyncedDocument(ctx, doc, regenerateSequences, unusedSequences) | ||
| if err != nil { | ||
| return nil, nil, false, err | ||
| } | ||
| if shouldUpdate { | ||
| base.TracefCtx(ctx, base.KeyAccess, "Saving updated channels and access grants of %q", base.UD(docid)) | ||
| if updatedExpiry != nil { | ||
| updatedDoc.UpdateExpiry(*updatedExpiry) | ||
| } | ||
|
|
||
| updatedBytes, marshalErr := base.JSONMarshal(updatedDoc) | ||
| return updatedBytes, updatedExpiry, false, marshalErr | ||
| } else { | ||
| return nil, nil, false, base.ErrUpdateCancel | ||
| } | ||
| }) | ||
| if rawGlobalXattr != nil { | ||
| updatedDoc.Xattrs[base.GlobalXattrName] = rawGlobalXattr | ||
| } | ||
| return updatedDoc, err | ||
| } | ||
| opts := &sgbucket.MutateInOptions{ | ||
| MacroExpansion: macroExpandSpec(base.SyncXattrName), | ||
| } | ||
| _, err = db.dataStore.WriteUpdateWithXattrs(ctx, docid, db.syncGlobalSyncMouRevSeqNoAndUserXattrKeys(), 0, previousDoc, opts, writeUpdateFunc) | ||
| if err == nil { | ||
| base.Audit(ctx, base.AuditIDDocumentResync, base.AuditFields{ | ||
| base.AuditFieldDocID: docid, | ||
| base.AuditFieldDocVersion: updatedDoc.CVOrRevTreeID(), | ||
| }) | ||
| } | ||
| return updatedHighSeq, unusedSequences, err | ||
| return updatedUnusedSequences, err | ||
| } | ||
|
|
||
| // invalidateAllPrincipals invalidates computed channels and roles for all users/roles, for the specified collections: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment explains that
mouMatchshould be false, but doesn't clearly explain whyshouldUpdatebeing false would makemou.cas == doc.cas. Consider clarifying that whenshouldUpdateis false, no resync occurs and the function returns early viaErrUpdateCancel, so at this pointshouldUpdatemust be true.