Skip to content

Commit

Permalink
Merge pull request #1324 from anyproto/go-3129-localstore-badger-reco…
Browse files Browse the repository at this point in the history
…very

GO-3129 reinit badger localstore in case of known corruption errors
  • Loading branch information
requilence committed Jul 1, 2024
2 parents 2d08469 + 1976cc4 commit 60a1e0f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/lib/datastore/clientds/clientds.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ func openBadgerWithRecover(opts badger.Options) (db *badger.DB, err error) {
return db, err
}

func isBadgerCorrupted(err error) bool {
if strings.Contains(err.Error(), "checksum mismatch") {
return true
}
if strings.Contains(err.Error(), "checksum is empty") {
return true
}
if strings.Contains(err.Error(), "EOF") {
return true
}
if strings.Contains(err.Error(), "file does not exist") {
return true
}
if strings.Contains(err.Error(), "Unable to parse log") {
return true
}
if strings.Contains(err.Error(), "Level validation err") {
return true
}
if strings.Contains(err.Error(), "failed to read index") {
return true
}
return false
}

func (r *clientds) Init(a *app.App) (err error) {
// TODO: looks like we do a lot of stuff on Init here. We should consider moving it to the Run
r.closing = make(chan struct{})
Expand Down Expand Up @@ -144,7 +169,7 @@ func (r *clientds) Init(a *app.App) (err error) {

r.localstoreDS, err = openBadgerWithRecover(opts)
err = oserror.TransformError(err)
if err != nil && strings.Contains(err.Error(), "checksum mismatch") {
if err != nil && isBadgerCorrupted(err) {
// because localstore contains mostly recoverable info (with th only exception of objects' lastOpenedDate)
// we can just remove and recreate it
err2 := os.Rename(opts.Dir, filepath.Join(opts.Dir, "-corrupted"))
Expand Down

0 comments on commit 60a1e0f

Please sign in to comment.