Skip to content

Commit a25a86a

Browse files
committed
CBG-5052 send noRev if history can't be parsed
1 parent 5f215d6 commit a25a86a

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

db/blip_handler.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,25 @@ func (bsc *BlipSyncContext) sendRevAsDelta(ctx context.Context, sender *blip.Sen
928928
var history []string
929929
var revTreeProperty []string
930930
if !bsc.useHLV() {
931-
history = toHistory(redactedRev.History, knownRevs, maxHistory)
931+
var err error
932+
history, err = toHistory(redactedRev.History, knownRevs, maxHistory)
933+
if err != nil {
934+
err := base.RedactErrorf("Could not get rev tree history for replacement rev in sendRevAsDelta %s %s: %w, sending a noRev to skip this revision for replication at sequence %d.", base.UD(docID), revID, err, seq)
935+
base.WarnfCtx(ctx, "%s", err)
936+
return bsc.sendNoRev(sender, docID, revID, collectionIdx, seq, err)
937+
}
932938
} else {
933939
history = append(history, redactedRev.HlvHistory)
934940
}
935941
if bsc.sendRevTreeProperty() {
936942
revTreeProperty = append(revTreeProperty, redactedRev.RevID)
937-
revTreeProperty = append(revTreeProperty, toHistory(redactedRev.History, knownRevs, maxHistory)...)
943+
history, err := toHistory(redactedRev.History, knownRevs, maxHistory)
944+
if err != nil {
945+
err := base.RedactErrorf("Could not get rev tree history for replacement rev when sending in sendRevAsDelta %s %s: %w, sending a noRev to skip this document for replication at sequence %d.", base.UD(docID), redactedRev.RevID, err, seq)
946+
base.WarnfCtx(ctx, "%s", err)
947+
return bsc.sendNoRev(sender, docID, revID, collectionIdx, seq, err)
948+
}
949+
revTreeProperty = append(revTreeProperty, history...)
938950
}
939951

940952
properties, err := blipRevMessageProperties(history, redactedRev.Deleted, seq, "", revTreeProperty)

db/blip_sync_context.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,12 @@ func (bsc *BlipSyncContext) sendRevision(ctx context.Context, sender *blip.Sende
765765
}
766766
var history []string
767767
if !bsc.useHLV() || localIsLegacyRev {
768-
history = toHistory(docRev.History, knownRevs, maxHistory)
768+
var err error
769+
history, err = toHistory(docRev.History, knownRevs, maxHistory)
770+
if err != nil {
771+
err := base.RedactErrorf("Could not get rev tree history for %s %s: %w, sending a noRev to skip this revision for replication at sequence %d.", base.UD(docID), revID, err, seq)
772+
return bsc.sendNoRev(sender, docID, revID, collectionIdx, seq, err)
773+
}
769774
} else {
770775
if docRev.HlvHistory != "" {
771776
history = append(history, docRev.HlvHistory)
@@ -779,7 +784,12 @@ func (bsc *BlipSyncContext) sendRevision(ctx context.Context, sender *blip.Sende
779784
// the full rev tree history above to send in the history property.
780785
if remoteIsLegacyRev && !localIsLegacyRev {
781786
// append current revID and rest of rev tree after hlv history
782-
revTreeHistory := toHistory(docRev.History, knownRevs, maxHistory)
787+
revTreeHistory, err := toHistory(docRev.History, knownRevs, maxHistory)
788+
if err != nil {
789+
err := base.RedactErrorf("Could not get rev tree history for %s %s when remote revision is a legacy rev and the local is hlv aware: %w, sending a noRev to skip this revision for replication at sequence %d.", base.UD(docID), revID, err, seq)
790+
base.WarnfCtx(ctx, "%v", err)
791+
return bsc.sendNoRev(sender, docID, revID, collectionIdx, seq, err)
792+
}
783793
history = append(history, docRev.RevID)
784794
history = append(history, revTreeHistory...)
785795
} else if bsc.sendRevTreeProperty() && !localIsLegacyRev {
@@ -788,7 +798,13 @@ func (bsc *BlipSyncContext) sendRevision(ctx context.Context, sender *blip.Sende
788798
// should only be sent if the local + remote revisions are not a legacy revisions as the rev tree in this case will be
789799
// sent in history property.
790800
revTreeHistoryProperty = append(revTreeHistoryProperty, docRev.RevID) // we need current rev
791-
revTreeHistoryProperty = append(revTreeHistoryProperty, toHistory(docRev.History, knownRevs, maxHistory)...)
801+
history, err := toHistory(docRev.History, knownRevs, maxHistory)
802+
if err != nil {
803+
err := base.RedactErrorf("Could not get rev tree history for %s %s when local and remote revision are hlv aware: %w, sending a noRev to skip this revision for replication at sequence %d.", base.UD(docID), revID, err, seq)
804+
base.WarnfCtx(ctx, "%v", err)
805+
return bsc.sendNoRev(sender, docID, revID, collectionIdx, seq, err)
806+
}
807+
revTreeHistoryProperty = append(revTreeHistoryProperty, history...)
792808
}
793809

794810
properties, err := blipRevMessageProperties(history, docRev.Deleted, seq, replacedRevID, revTreeHistoryProperty)
@@ -815,9 +831,14 @@ func digests(meta []AttachmentStorageMeta) []string {
815831
return digests
816832
}
817833

818-
func toHistory(revisions Revisions, knownRevs map[string]bool, maxHistory int) []string {
834+
// toHistory returns an array of revision IDs in a descending array. Truncates array at maxHistory length, or at the first rev in knownRevs. Returns an error with invalid history.
835+
func toHistory(revisions Revisions, knownRevs map[string]bool, maxHistory int) ([]string, error) {
819836
// Get the revision's history as a descending array of ancestor revIDs:
820-
history := revisions.ParseRevisions()[1:]
837+
revs := revisions.ParseRevisions()
838+
if len(revisions) == 0 {
839+
return nil, fmt.Errorf("invalid revision history, no revisions found")
840+
}
841+
history := revs[1:]
821842
for i, rev := range history {
822843
if knownRevs[rev] || (maxHistory > 0 && i+1 >= maxHistory) {
823844
history = history[0 : i+1]
@@ -826,7 +847,7 @@ func toHistory(revisions Revisions, knownRevs map[string]bool, maxHistory int) [
826847
knownRevs[rev] = true
827848
}
828849
}
829-
return history
850+
return history, nil
830851
}
831852

832853
// timeElapsedForStatsReporting will return true if enough time has passed since the previous report.

0 commit comments

Comments
 (0)