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

fix rollback panic bug #153

Merged
merged 1 commit into from
Jun 8, 2019
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
22 changes: 22 additions & 0 deletions freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,28 @@ func (f *freelist) reload(p *page) {
f.readIDs(a)
}

// noSyncReload reads the freelist from pgids and filters out pending items.
func (f *freelist) noSyncReload(pgids []pgid) {
Copy link
Contributor

Choose a reason for hiding this comment

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

freelist knows its own type. we can do the branch inside the reload func.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type of freelist is hashmap or array. It is irrelevant with sync or not-sync.

// Build a cache of only pending pages.
pcache := make(map[pgid]bool)
for _, txp := range f.pending {
for _, pendingID := range txp.ids {
pcache[pendingID] = true
}
}

// Check each page in the freelist and build a new available freelist
// with any pages not in the pending lists.
var a []pgid
for _, id := range pgids {
if !pcache[id] {
a = append(a, id)
}
}

f.readIDs(a)
}

// reindex rebuilds the free cache based on available and pending free lists.
func (f *freelist) reindex() {
ids := f.getFreePageIDs()
Expand Down
23 changes: 21 additions & 2 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,36 @@ func (tx *Tx) Rollback() error {
if tx.db == nil {
return ErrTxClosed
}
tx.rollback()
tx.nonPhysicalRollback()
return nil
}

// nonPhysicalRollback is called when user calls Rollback directly, in this case we do not need to reload the free pages from disk.
func (tx *Tx) nonPhysicalRollback() {
if tx.db == nil {
return
}
if tx.writable {
tx.db.freelist.rollback(tx.meta.txid)
}
tx.close()
}

// rollback needs to reload the free pages from disk in case some system error happens like fsync error.
func (tx *Tx) rollback() {
if tx.db == nil {
return
}
if tx.writable {
tx.db.freelist.rollback(tx.meta.txid)
tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))
if !tx.db.hasSyncedFreelist() {
// Reconstruct free page list by scanning the DB to get the whole free page list.
// Note: scaning the whole db is heavy if your db size is large in NoSyncFreeList mode.
tx.db.freelist.noSyncReload(tx.db.freepages())
} else {
// Read free page list from freelist page.
tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))
}
}
tx.close()
}
Expand Down
51 changes: 51 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,57 @@ func TestTx_CopyFile_Error_Normal(t *testing.T) {
}
}

// TestTx_Rollback ensures there is no error when tx rollback whether we sync freelist or not.
func TestTx_Rollback(t *testing.T) {
for _, isSyncFreelist := range []bool{false, true} {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
if err != nil {
log.Fatal(err)
}
defer os.Remove(db.Path())
db.NoFreelistSync = isSyncFreelist

tx, err := db.Begin(true)
if err != nil {
t.Fatalf("Error starting tx: %v", err)
}
bucket := []byte("mybucket")
if _, err := tx.CreateBucket(bucket); err != nil {
t.Fatalf("Error creating bucket: %v", err)
}
if err := tx.Commit(); err != nil {
t.Fatalf("Error on commit: %v", err)
}

tx, err = db.Begin(true)
if err != nil {
t.Fatalf("Error starting tx: %v", err)
}
b := tx.Bucket(bucket)
if err := b.Put([]byte("k"), []byte("v")); err != nil {
t.Fatalf("Error on put: %v", err)
}
// Imagine there is an error and tx needs to be rolled-back
if err := tx.Rollback(); err != nil {
t.Fatalf("Error on rollback: %v", err)
}

tx, err = db.Begin(false)
if err != nil {
t.Fatalf("Error starting tx: %v", err)
}
b = tx.Bucket(bucket)
if v := b.Get([]byte("k")); v != nil {
t.Fatalf("Value for k should not have been stored")
}
if err := tx.Rollback(); err != nil {
t.Fatalf("Error on rollback: %v", err)
}

}
}

// TestTx_releaseRange ensures db.freePages handles page releases
// correctly when there are transaction that are no longer reachable
// via any read/write transactions and are "between" ongoing read
Expand Down