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 freelist corruption on tx.WriteTo #67

Merged
merged 2 commits into from
Nov 16, 2017
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
60 changes: 60 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"hash/fnv"
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -588,6 +589,65 @@ func TestDB_BeginRW(t *testing.T) {
}
}

// TestDB_Concurrent_WriteTo checks that issuing WriteTo operations concurrently
// with commits does not produce corrupted db files.
func TestDB_Concurrent_WriteTo(t *testing.T) {
o := &bolt.Options{NoFreelistSync: false}
db := MustOpenWithOption(o)
defer db.MustClose()

var wg sync.WaitGroup
wtxs, rtxs := 5, 5
wg.Add(wtxs * rtxs)
f := func(tx *bolt.Tx) {
defer wg.Done()
f, err := ioutil.TempFile("", "bolt-")
if err != nil {
panic(err)
}
time.Sleep(time.Duration(rand.Intn(20)+1) * time.Millisecond)
tx.WriteTo(f)
tx.Rollback()
f.Close()
snap := &DB{nil, f.Name(), o}
snap.MustReopen()
defer snap.MustClose()
snap.MustCheck()
}

tx1, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if _, err := tx1.CreateBucket([]byte("abc")); err != nil {
t.Fatal(err)
}
if err := tx1.Commit(); err != nil {
t.Fatal(err)
}

for i := 0; i < wtxs; i++ {
tx, err := db.Begin(true)
if err != nil {
t.Fatal(err)
}
if err := tx.Bucket([]byte("abc")).Put([]byte{0}, []byte{0}); err != nil {
t.Fatal(err)
}
for j := 0; j < rtxs; j++ {
rtx, rerr := db.Begin(false)
if rerr != nil {
t.Fatal(rerr)
}
go f(rtx)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
}
wg.Wait()
}

// Ensure that opening a transaction while the DB is closed returns an error.
func TestDB_BeginRW_Closed(t *testing.T) {
var db bolt.DB
Expand Down
6 changes: 3 additions & 3 deletions freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func (f *freelist) free(txid txid, p *page) {
allocTxid, ok := f.allocs[p.id]
if ok {
delete(f.allocs, p.id)
} else if (p.flags & (freelistPageFlag | metaPageFlag)) != 0 {
// Safe to claim txid as allocating since these types are private to txid.
allocTxid = txid
} else if (p.flags & freelistPageFlag) != 0 {
// Freelist is always allocated by prior tx.
allocTxid = txid - 1
Comment on lines 133 to +137
Copy link
Member

@ahrtr ahrtr Jun 27, 2024

Choose a reason for hiding this comment

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

It can fix the issue, but it's a little complicated. No need to handle freelist as a special case. I think it can be as simple as below.

$ git diff
diff --git a/freelist.go b/freelist.go
index fdf8a36..4a15832 100644
--- a/freelist.go
+++ b/freelist.go
@@ -118,9 +118,6 @@ func (f *freelist) free(txid common.Txid, p *common.Page) {
        allocTxid, ok := f.allocs[p.Id()]
        if ok {
                delete(f.allocs, p.Id())
-       } else if p.IsFreelistPage() {
-               // Freelist is always allocated by prior tx.
-               allocTxid = txid - 1
        }
 
        for id := p.Id(); id <= p.Id()+common.Pgid(p.Overflow()); id++ {

}

for id := p.id; id <= p.id+pgid(p.overflow); id++ {
Expand Down