Skip to content

Commit 0f9f2bf

Browse files
authored
Use optimized checksumming when loading database (#441)
1 parent 2e78724 commit 0f9f2bf

File tree

8 files changed

+36
-20
lines changed

8 files changed

+36
-20
lines changed

.github/workflows/push.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ jobs:
4141
go-version-file: 'go.mod'
4242

4343
- name: apt install
44-
run: sudo apt install -y fuse3 libsqlite3-dev consul
44+
run: |
45+
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
46+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
47+
sudo apt update && sudo apt install -y fuse3 libsqlite3-dev consul
4548
4649
- name: check fuse version
4750
run: fusermount -V
@@ -75,7 +78,10 @@ jobs:
7578
go-version-file: 'go.mod'
7679

7780
- name: apt install
78-
run: sudo apt install -y fuse3 libsqlite3-dev consul
81+
run: |
82+
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
83+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
84+
sudo apt update && sudo apt install -y fuse3 libsqlite3-dev consul
7985
8086
- name: Start consul in dev mode
8187
run: consul agent -dev &

cmd/litefs/export_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// go:build linux
1+
//go:build linux
2+
23
package main_test
34

45
import (

cmd/litefs/import_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// go:build linux
1+
//go:build linux
2+
23
package main_test
34

45
import (

cmd/litefs/mount_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// go:build linux
1+
//go:build linux
2+
23
package main
34

45
import (

cmd/litefs/mount_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// go:build linux
1+
//go:build linux
2+
23
package main_test
34

45
import (

db.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -930,20 +930,26 @@ func (db *DB) initDatabaseFile() error {
930930
// Build per-page checksum map for existing pages. The database could be
931931
// short compared to the page count in the header so just checksum what we
932932
// can. The database may recover in applyLTX() so we'll do validation then.
933-
buf := make([]byte, db.pageSize)
934933
db.chksums.pages = make([]ltx.Checksum, db.PageN())
935934
db.chksums.blocks = make([]ltx.Checksum, pageChksumBlock(db.PageN()))
936-
for pgno := uint32(1); pgno <= db.PageN(); pgno++ {
937-
offset := int64(pgno-1) * int64(db.pageSize)
938-
if _, err := internal.ReadFullAt(f, buf, offset); err == io.EOF || err == io.ErrUnexpectedEOF {
939-
log.Printf("database checksum ending early at page %d of %d ", pgno-1, db.PageN())
940-
break
941-
} else if err != nil {
942-
return fmt.Errorf("read database page %d: %w", pgno, err)
943-
}
944935

945-
chksum := ltx.ChecksumPage(pgno, buf)
946-
db.setDatabasePageChecksum(pgno, chksum)
936+
lastGoodPage, err := ltx.ChecksumPages(db.DatabasePath(), db.pageSize, db.PageN(), 0, db.chksums.pages)
937+
938+
// lastGoodPage tells us how far we got before the first error. Most likely
939+
// is that we got an EOF because the db was short, in which case no
940+
// subsequent checksums would have been written. To be cautious though,
941+
// zero out any checksums after the last good page.
942+
clear(db.chksums.pages[lastGoodPage:])
943+
944+
// Always overwrite the lock page as a zero checksum.
945+
if lockPage := ltx.LockPgno(db.pageSize); len(db.chksums.pages) >= int(lockPage) {
946+
db.chksums.pages[lockPage-1] = 0
947+
}
948+
949+
if err == io.EOF || err == io.ErrUnexpectedEOF {
950+
log.Printf("database checksum ending early at page %d of %d ", lastGoodPage, db.PageN())
951+
} else if err != nil {
952+
return fmt.Errorf("read database page %d: %w", lastGoodPage+1, err)
947953
}
948954

949955
return nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/mattn/go-sqlite3 v1.14.16-0.20220918133448-90900be5db1a
1010
github.com/prometheus/client_golang v1.13.0
1111
github.com/superfly/litefs-go v0.0.0-20230227231337-34ea5dcf1e0b
12-
github.com/superfly/ltx v0.3.13
12+
github.com/superfly/ltx v0.3.14
1313
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
1414
golang.org/x/net v0.17.0
1515
golang.org/x/sync v0.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
308308
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
309309
github.com/superfly/litefs-go v0.0.0-20230227231337-34ea5dcf1e0b h1:+WuhtZFB8fNdPeaMUtuB/U8aknXBXdDW/mBm/HTYJNg=
310310
github.com/superfly/litefs-go v0.0.0-20230227231337-34ea5dcf1e0b/go.mod h1:h+GUx1V2s0C5nY73ZN82760eWEJrpMaiDweF31VmJKk=
311-
github.com/superfly/ltx v0.3.13 h1:IbuocKJ6sY2jYvZbpUGMYmTkvaLSGUderEZwmaIUmJ0=
312-
github.com/superfly/ltx v0.3.13/go.mod h1:ly+Dq7UVacQVEI5/b0r6j+PSNy9ibwx1yikcWAaSkhE=
311+
github.com/superfly/ltx v0.3.14 h1:u/w2NJsyaAyh04+Oy2x5tQpWJzpzf9iQbY1wgfMZnW4=
312+
github.com/superfly/ltx v0.3.14/go.mod h1:ly+Dq7UVacQVEI5/b0r6j+PSNy9ibwx1yikcWAaSkhE=
313313
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
314314
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c h1:u6SKchux2yDvFQnDHS3lPnIRmfVJ5Sxy3ao2SIdysLQ=
315315
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=

0 commit comments

Comments
 (0)