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

Perform unmap when mlock fails or both meta pages corrupted #433

Merged
merged 3 commits into from
Mar 23, 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
20 changes: 17 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (db *DB) hasSyncedFreelist() bool {

// mmap opens the underlying memory-mapped file and initializes the meta references.
// minsz is the minimum size that the new mmap can be.
func (db *DB) mmap(minsz int) error {
func (db *DB) mmap(minsz int) (err error) {
db.mmaplock.Lock()
defer db.mmaplock.Unlock()

Expand Down Expand Up @@ -423,17 +423,27 @@ func (db *DB) mmap(minsz int) error {
}

// Unmap existing data before continuing.
if err := db.munmap(); err != nil {
if err = db.munmap(); err != nil {
return err
}

// Memory-map the data file as a byte slice.
// gofail: var mapError string
// return errors.New(mapError)
if err := mmap(db, size); err != nil {
if err = mmap(db, size); err != nil {
return err
}

// Perform unmmap on any error to reset all data fields:
// dataref, data, datasz, meta0 and meta1.
defer func() {
if err != nil {
if unmapErr := db.munmap(); unmapErr != nil {
err = fmt.Errorf("%w; unmap failed: %v", err, unmapErr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahrtr

  1. I would be explicit in that error that it's 'rollback unmap also failed'.

  2. I had some doubts whether we should execute here unmap or just db.invalidate.
    Ideally cleanup for failing mmap() (like rollback unmap) should be handled within the mmap() function's logic, rather than assumed externally.

  3. If we suspect the failing rollbacks might be reason for the double-linking issues,
    by submitting this patch, we are making it more difficult to build repro (i.e. we need to test with & without that patch). It would be good to split failpoints (helping with repro) from the 'fix' PR.

  4. Next time please ping me or @serathius a little longer for business logic change - to not create precedences.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I would be explicit in that error that it's 'rollback unmap also failed'.

Thanks for the suggestion. Will fix it separately.

  1. I had some doubts whether we should execute here unmap or just db.invalidate.
    Ideally cleanup for failing mmap() (like rollback unmap) should be handled within the mmap() function's logic, rather than assumed externally.

Definitely we should execute db.unmap instead of db.invalidate. Note that the deferred function will only be executed when mmap already succeeded but (1) mlock somehow failed or (2) both meta pages are corrupted.

  1. If we suspect the failing rollbacks might be reason for the double-linking issues,
    by submitting this patch, we are making it more difficult to build repro (i.e. we need to test with & without that patch). It would be good to split failpoints (helping with repro) from the 'fix' PR.

Not sure what do you mean by "double-linking issues", could you clarify this? I found the issue with the new added test cases in this PR, so I fixed it in the same PR. I plan to backport both failpoints and all bug fixes to release-1.3, please see #436. Please let me know if you still have any concern.

  1. Next time please ping me or @serathius a little longer for business logic change - to not create precedences.

Sorry to merge this PR without another maintainer's approval. I do think it's a safe change, and It's already reviewed by three experienced contributors. But anyway, will try to follow the process next time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. thx
  2. You are right.
  3. I mean corruptions like Branch page items link to already released pages #402. There is slim chance there are caused by transaction rollback and follow up commit. If we find repro without this PR, we have a higher confidence we found the root issue.
    If the PR indeed fixes the problem. You might struggle to repro problem that got already fixed.
    I'm not saying I have concerns about this PR, just flagging.
  4. Thx.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the PR indeed fixes the problem. You might struggle to repro problem that got already fixed.
I'm not saying I have concerns about this PR, just flagging.

Thanks for the reminder.

}
}
}()

if db.Mlock {
// Don't allow swapping of data file
if err := db.mlock(fileSize); err != nil {
Expand Down Expand Up @@ -517,13 +527,17 @@ func (db *DB) mmapSize(size int) (int, error) {
}

func (db *DB) munlock(fileSize int) error {
// gofail: var munlockError string
// return errors.New(munlockError)
if err := munlock(db, fileSize); err != nil {
return fmt.Errorf("munlock error: " + err.Error())
}
return nil
}

func (db *DB) mlock(fileSize int) error {
// gofail: var mlockError string
// return errors.New(mlockError)
if err := mlock(db, fileSize); err != nil {
return fmt.Errorf("mlock error: " + err.Error())
}
Expand Down
48 changes: 48 additions & 0 deletions tests/failpoint/db_failpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package failpoint

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"

bolt "go.etcd.io/bbolt"
"go.etcd.io/bbolt/internal/btesting"
gofail "go.etcd.io/gofail/runtime"
)

Expand Down Expand Up @@ -46,3 +48,49 @@ func TestFailpoint_UnmapFail_DbClose(t *testing.T) {
err = db.Close()
require.NoError(t, err)
}

func TestFailpoint_mLockFail(t *testing.T) {
err := gofail.Enable("mlockError", `return("mlock somehow failed")`)
require.NoError(t, err)

f := filepath.Join(t.TempDir(), "db")
_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
require.Error(t, err)
require.ErrorContains(t, err, "mlock somehow failed")

// It should work after disabling the failpoint.
err = gofail.Disable("mlockError")
require.NoError(t, err)

_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
require.NoError(t, err)
}

func TestFailpoint_mLockFail_When_remap(t *testing.T) {
db := btesting.MustCreateDB(t)
db.Mlock = true

err := gofail.Enable("mlockError", `return("mlock somehow failed in allocate")`)
require.NoError(t, err)

err = db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
func(tx int, k int) []byte { return make([]byte, 100) },
)

require.Error(t, err)
require.ErrorContains(t, err, "mlock somehow failed in allocate")

// It should work after disabling the failpoint.
err = gofail.Disable("mlockError")
require.NoError(t, err)
db.MustClose()
db.MustReopen()

err = db.Fill([]byte("data"), 1, 10000,
func(tx int, k int) []byte { return []byte(fmt.Sprintf("%04d", k)) },
func(tx int, k int) []byte { return make([]byte, 100) },
)

require.NoError(t, err)
}