Skip to content

Commit 2eaca3e

Browse files
committed
feat: binary search for first version if legacy pruning is broken
1 parent 3f21c8b commit 2eaca3e

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

nodedb.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,14 @@ func (ndb *nodeDB) deleteVersionsTo(toVersion int64) error {
709709
if err := ndb.deleteLegacyVersions(legacyLatestVersion); err != nil {
710710
ndb.logger.Error("Error deleting legacy versions", "err", err)
711711
}
712-
first = legacyLatestVersion + 1
712+
// NOTE: When pruning is broken for legacy versions we need to find the
713+
// latest non legacy version in the store
714+
// TODO: Make sure legacy pruning works as expected and does not fail
715+
firstNonLegacyVersion, err := ndb.getFirstNonLegacyVersion()
716+
if err != nil {
717+
return err
718+
}
719+
first = firstNonLegacyVersion
713720

714721
// reset the legacy latest version forcibly to avoid multiple calls
715722
ndb.resetLegacyLatestVersion(-1)
@@ -752,6 +759,35 @@ func (ndb *nodeDB) legacyRootKey(version int64) []byte {
752759
return legacyRootKeyFormat.Key(version)
753760
}
754761

762+
// getFirstNonLegacyVersion binary searches the store for the first non-legacy version
763+
func (ndb *nodeDB) getFirstNonLegacyVersion() (int64, error) {
764+
ndb.mtx.Lock()
765+
firstVersion := ndb.firstVersion
766+
ndb.mtx.Unlock()
767+
768+
// Find the first version
769+
latestVersion, err := ndb.getLatestVersion()
770+
if err != nil {
771+
return 0, err
772+
}
773+
for firstVersion < latestVersion {
774+
version := (latestVersion + firstVersion) >> 1
775+
has, err := ndb.hasVersion(version)
776+
if err != nil {
777+
return 0, err
778+
}
779+
if has {
780+
latestVersion = version
781+
} else {
782+
firstVersion = version + 1
783+
}
784+
}
785+
786+
ndb.resetFirstVersion(latestVersion)
787+
788+
return latestVersion, nil
789+
}
790+
755791
func (ndb *nodeDB) getFirstVersion() (int64, error) {
756792
ndb.mtx.Lock()
757793
firstVersion := ndb.firstVersion

0 commit comments

Comments
 (0)