From a41516f1801b48b0255b38b3741603faa79c2c3b Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 30 Mar 2021 15:36:40 -0700 Subject: [PATCH] Fix reload calculation incorrect --- buntdb.go | 1 + buntdb_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 61 insertions(+) diff --git a/buntdb.go b/buntdb.go index 243e873..9f17322 100644 --- a/buntdb.go +++ b/buntdb.go @@ -861,6 +861,7 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) (n int64, err error) { } // copy string parts = append(parts, string(data[:n])) + cmdByteSize += int64(n + 2) } // finished reading the command diff --git a/buntdb_test.go b/buntdb_test.go index 57664ad..e7262de 100644 --- a/buntdb_test.go +++ b/buntdb_test.go @@ -12,6 +12,8 @@ import ( "sync" "testing" "time" + + "github.com/tidwall/lotsa" ) func testOpen(t testing.TB) *DB { @@ -2773,3 +2775,58 @@ func TestTransactionLeak(t *testing.T) { t.Fatal(err) } } + +func TestReloadNotInvalid(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + os.RemoveAll("data.db") + defer os.RemoveAll("data.db") + start := time.Now() + ii := 0 + for time.Since(start) < time.Second*5 { + func() { + db, err := Open("data.db") + if err != nil { + t.Fatal(err) + } + defer func() { + if err := db.Close(); err != nil { + panic(err) + } + // truncate at a random point in the file + f, err := os.OpenFile("data.db", os.O_RDWR, 0666) + if err != nil { + panic(err) + } + defer f.Close() + sz, err := f.Seek(0, 2) + if err != nil { + panic(err) + } + n := sz/2 + int64(rand.Intn(int(sz/2))) + err = f.Truncate(n) + if err != nil { + panic(err) + } + }() + N := 500 + lotsa.Ops(N, 16, func(i, t int) { + if i == N/2 && ii&7 == 0 { + if err := db.Shrink(); err != nil { + panic(err) + } + } + err := db.Update(func(tx *Tx) error { + _, _, err := tx.Set(fmt.Sprintf("key:%d", i), fmt.Sprintf("val:%d", i), &SetOptions{ + Expires: true, + TTL: 30, + }) + return err + }) + if err != nil { + panic(err) + } + }) + }() + ii++ + } +} diff --git a/go.mod b/go.mod index 85a2d8e..cd77790 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/tidwall/btree v0.4.2 github.com/tidwall/gjson v1.7.4 github.com/tidwall/grect v0.1.1 + github.com/tidwall/lotsa v1.0.2 github.com/tidwall/match v1.0.3 github.com/tidwall/rtred v0.1.2 ) diff --git a/go.sum b/go.sum index 38dd895..8cf5f84 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/tidwall/gjson v1.7.4 h1:19cchw8FOxkG5mdLRkGf9jqIqEyqdZhPqW60XfyFxk8= github.com/tidwall/gjson v1.7.4/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= github.com/tidwall/grect v0.1.1 h1:+kMEkxhoqB7rniVXzMEIA66XwU07STgINqxh+qVIndY= github.com/tidwall/grect v0.1.1/go.mod h1:CzvbGiFbWUwiJ1JohXLb28McpyBsI00TK9Y6pDWLGRQ= +github.com/tidwall/lotsa v1.0.2 h1:dNVBH5MErdaQ/xd9s769R31/n2dXavsQ0Yf4TMEHHw8= +github.com/tidwall/lotsa v1.0.2/go.mod h1:X6NiU+4yHA3fE3Puvpnn1XMDrFZrE9JO2/w+UMuqgR8= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=