Fix reload calculation incorrect

This commit is contained in:
tidwall 2021-03-30 15:36:40 -07:00
parent d7ed6a747a
commit a41516f180
4 changed files with 61 additions and 0 deletions

View File

@ -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

View File

@ -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++
}
}

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=