2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-07 06:05:04 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-10-07 06:05:04 -07:00
|
|
|
|
2015-01-12 06:50:30 -07:00
|
|
|
package db
|
2014-10-07 06:05:04 -07:00
|
|
|
|
|
|
|
import (
|
2020-03-18 09:34:46 -07:00
|
|
|
"bytes"
|
2014-10-07 06:05:04 -07:00
|
|
|
"testing"
|
2018-03-10 03:42:01 -07:00
|
|
|
|
2019-11-29 01:11:52 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/db/backend"
|
2018-05-15 23:44:08 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2018-03-10 03:42:01 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2014-10-07 06:05:04 -07:00
|
|
|
)
|
|
|
|
|
2019-11-29 01:11:52 -07:00
|
|
|
func genBlocks(n int) []protocol.BlockInfo {
|
|
|
|
b := make([]protocol.BlockInfo, n)
|
|
|
|
for i := range b {
|
|
|
|
h := make([]byte, 32)
|
|
|
|
for j := range h {
|
|
|
|
h[j] = byte(i + j)
|
|
|
|
}
|
|
|
|
b[i].Size = int32(i)
|
|
|
|
b[i].Hash = h
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-06-24 00:50:18 -07:00
|
|
|
func TestIgnoredFiles(t *testing.T) {
|
2018-05-15 23:44:08 -07:00
|
|
|
ldb, err := openJSONS("testdata/v0.14.48-ignoredfiles.db.jsons")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-29 01:11:52 -07:00
|
|
|
db := NewLowlevel(ldb)
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2019-11-29 01:11:52 -07:00
|
|
|
if err := UpdateSchema(db); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-10 02:34:24 -07:00
|
|
|
|
2018-05-15 23:44:08 -07:00
|
|
|
fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
|
|
|
|
|
|
|
|
// The contents of the database are like this:
|
|
|
|
//
|
|
|
|
// fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), db)
|
|
|
|
// fs.Update(protocol.LocalDeviceID, []protocol.FileInfo{
|
|
|
|
// { // invalid (ignored) file
|
|
|
|
// Name: "foo",
|
|
|
|
// Type: protocol.FileInfoTypeFile,
|
|
|
|
// Invalid: true,
|
|
|
|
// Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1000}}},
|
|
|
|
// },
|
|
|
|
// { // regular file
|
|
|
|
// Name: "bar",
|
|
|
|
// Type: protocol.FileInfoTypeFile,
|
|
|
|
// Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1001}}},
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// fs.Update(protocol.DeviceID{42}, []protocol.FileInfo{
|
|
|
|
// { // invalid file
|
|
|
|
// Name: "baz",
|
|
|
|
// Type: protocol.FileInfoTypeFile,
|
|
|
|
// Invalid: true,
|
|
|
|
// Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1000}}},
|
|
|
|
// },
|
|
|
|
// { // regular file
|
|
|
|
// Name: "quux",
|
|
|
|
// Type: protocol.FileInfoTypeFile,
|
|
|
|
// Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1002}}},
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
|
2018-06-24 00:50:18 -07:00
|
|
|
// Local files should have the "ignored" bit in addition to just being
|
|
|
|
// generally invalid if we want to look at the simulation of that bit.
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
snap := fs.Snapshot()
|
|
|
|
defer snap.Release()
|
|
|
|
fi, ok := snap.Get(protocol.LocalDeviceID, "foo")
|
2018-05-15 23:44:08 -07:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("foo should exist")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if !fi.IsInvalid() {
|
2018-05-15 23:44:08 -07:00
|
|
|
t.Error("foo should be invalid")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if !fi.IsIgnored() {
|
|
|
|
t.Error("foo should be ignored")
|
|
|
|
}
|
2018-05-15 23:44:08 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
fi, ok = snap.Get(protocol.LocalDeviceID, "bar")
|
2018-05-15 23:44:08 -07:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("bar should exist")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if fi.IsInvalid() {
|
2018-05-15 23:44:08 -07:00
|
|
|
t.Error("bar should not be invalid")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if fi.IsIgnored() {
|
|
|
|
t.Error("bar should not be ignored")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remote files have the invalid bit as usual, and the IsInvalid() method
|
|
|
|
// should pick this up too.
|
2018-05-15 23:44:08 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
fi, ok = snap.Get(protocol.DeviceID{42}, "baz")
|
2018-05-15 23:44:08 -07:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("baz should exist")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if !fi.IsInvalid() {
|
|
|
|
t.Error("baz should be invalid")
|
|
|
|
}
|
|
|
|
if !fi.IsInvalid() {
|
2018-05-15 23:44:08 -07:00
|
|
|
t.Error("baz should be invalid")
|
|
|
|
}
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
fi, ok = snap.Get(protocol.DeviceID{42}, "quux")
|
2018-05-15 23:44:08 -07:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("quux should exist")
|
|
|
|
}
|
2018-06-24 00:50:18 -07:00
|
|
|
if fi.IsInvalid() {
|
|
|
|
t.Error("quux should not be invalid")
|
|
|
|
}
|
|
|
|
if fi.IsInvalid() {
|
2018-05-15 23:44:08 -07:00
|
|
|
t.Error("quux should not be invalid")
|
|
|
|
}
|
|
|
|
}
|
2018-06-02 06:08:32 -07:00
|
|
|
|
|
|
|
const myID = 1
|
|
|
|
|
|
|
|
var (
|
|
|
|
remoteDevice0, remoteDevice1 protocol.DeviceID
|
|
|
|
update0to3Folder = "UpdateSchema0to3"
|
|
|
|
invalid = "invalid"
|
|
|
|
slashPrefixed = "/notgood"
|
|
|
|
haveUpdate0to3 map[protocol.DeviceID]fileList
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
remoteDevice0, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
|
|
|
|
remoteDevice1, _ = protocol.DeviceIDFromString("I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU")
|
|
|
|
haveUpdate0to3 = map[protocol.DeviceID]fileList{
|
|
|
|
protocol.LocalDeviceID: {
|
|
|
|
protocol.FileInfo{Name: "a", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
|
|
|
|
protocol.FileInfo{Name: slashPrefixed, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}}, Blocks: genBlocks(1)},
|
|
|
|
},
|
|
|
|
remoteDevice0: {
|
|
|
|
protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)},
|
2018-06-24 00:50:18 -07:00
|
|
|
protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(5), RawInvalid: true},
|
2018-06-02 06:08:32 -07:00
|
|
|
protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(7)},
|
|
|
|
},
|
|
|
|
remoteDevice1: {
|
|
|
|
protocol.FileInfo{Name: "c", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1002}}}, Blocks: genBlocks(7)},
|
2018-06-24 00:50:18 -07:00
|
|
|
protocol.FileInfo{Name: "d", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1003}}}, Blocks: genBlocks(5), RawInvalid: true},
|
|
|
|
protocol.FileInfo{Name: invalid, Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1004}}}, Blocks: genBlocks(5), RawInvalid: true},
|
2018-06-02 06:08:32 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate0to3(t *testing.T) {
|
|
|
|
ldb, err := openJSONS("testdata/v0.14.45-update0to3.db.jsons")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-10 02:34:24 -07:00
|
|
|
|
2019-12-02 00:18:04 -07:00
|
|
|
db := NewLowlevel(ldb)
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2018-10-10 02:34:24 -07:00
|
|
|
updater := schemaUpdater{db}
|
2018-06-02 06:08:32 -07:00
|
|
|
|
|
|
|
folder := []byte(update0to3Folder)
|
|
|
|
|
lib/db: Deduplicate block lists in database (fixes #5898) (#6283)
* lib/db: Deduplicate block lists in database (fixes #5898)
This moves the block list in the database out from being just a field on
the FileInfo to being an object of its own. When putting a FileInfo we
marshal the block list separately and store it keyed by the sha256 of
the marshalled block list. When getting, if we are not doing a
"truncated" get, we do an extra read and unmarshal for the block list.
Old block lists are cleared out by a periodic GC sweep. The alternative
would be to use refcounting, but:
- There is a larger risk of getting that wrong and either dropping a
block list in error or keeping them around forever.
- It's tricky with our current database, as we don't have dirty reads.
This means that if we update two FileInfos with identical block lists in
the same transaction we can't just do read/modify/write for the ref
counters as we wouldn't see our own first update. See above about
tracking this and risks about getting it wrong.
GC uses a bloom filter for keys to avoid heavy RAM usage. GC can't run
concurrently with FileInfo updates so there is a new lock around those
operation at the lowlevel.
The end result is a much more compact database, especially for setups
with many peers where files get duplicated many times.
This is per-key-class stats for a large database I'm currently working
with, under the current schema:
```
0x00: 9138161 items, 870876 KB keys + 7397482 KB data, 95 B + 809 B avg, 1637651 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x08: 1349 items, 12 KB keys + 10 KB data, 9 B + 8 B avg, 17 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 7 B avg, 30 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
Total 10426475 items, 968490 KB keys + 9202925 KB data.
```
Note 7.4 GB of data in class 00, total size 9.2 GB. After running the
migration we get this instead:
```
0x00: 9138161 items, 870876 KB keys + 2611392 KB data, 95 B + 285 B avg, 4788 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 17 B avg, 51 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
0x0d: 44282 items, 1461 KB keys + 61081 KB data, 33 B + 1379 B avg, 1637399 B max
Total 10469408 items, 969939 KB keys + 4477905 KB data.
```
Class 00 is now down to 2.6 GB, with just 61 MB added in class 0d.
There will be some additional reads in some cases which theoretically
hurts performance, but this will be more than compensated for by smaller
writes and better compaction.
On my own home setup which just has three devices and a handful of
folders the difference is smaller in absolute numbers of course, but
still less than half the old size:
```
0x00: 297122 items, 20894 KB keys + 306860 KB data, 70 B + 1032 B avg, 103237 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
Total 1947412 items, 151268 KB keys + 337485 KB data.
```
to:
```
0x00: 297122 items, 20894 KB keys + 37038 KB data, 70 B + 124 B avg, 520 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
0x0d: 18041 items, 595 KB keys + 71964 KB data, 33 B + 3988 B avg, 101109 B max
Total 1965447 items, 151863 KB keys + 139628 KB data.
```
* wip
* wip
* wip
* wip
2020-01-24 00:35:44 -07:00
|
|
|
if err := updater.updateSchema0to1(0); err != nil {
|
2019-11-29 01:11:52 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-02 06:08:32 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
trans, err := db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-11 06:31:43 -07:00
|
|
|
defer trans.Release()
|
2020-01-21 10:23:08 -07:00
|
|
|
if _, ok, err := trans.getFile(folder, protocol.LocalDeviceID[:], []byte(slashPrefixed)); err != nil {
|
2019-11-29 01:11:52 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if ok {
|
2018-06-02 06:08:32 -07:00
|
|
|
t.Error("File prefixed by '/' was not removed during transition to schema 1")
|
|
|
|
}
|
|
|
|
|
2019-11-29 01:11:52 -07:00
|
|
|
key, err := db.keyer.GenerateGlobalVersionKey(nil, folder, []byte(invalid))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := db.Get(key); err != nil {
|
2018-06-02 06:08:32 -07:00
|
|
|
t.Error("Invalid file wasn't added to global list")
|
|
|
|
}
|
|
|
|
|
lib/db: Deduplicate block lists in database (fixes #5898) (#6283)
* lib/db: Deduplicate block lists in database (fixes #5898)
This moves the block list in the database out from being just a field on
the FileInfo to being an object of its own. When putting a FileInfo we
marshal the block list separately and store it keyed by the sha256 of
the marshalled block list. When getting, if we are not doing a
"truncated" get, we do an extra read and unmarshal for the block list.
Old block lists are cleared out by a periodic GC sweep. The alternative
would be to use refcounting, but:
- There is a larger risk of getting that wrong and either dropping a
block list in error or keeping them around forever.
- It's tricky with our current database, as we don't have dirty reads.
This means that if we update two FileInfos with identical block lists in
the same transaction we can't just do read/modify/write for the ref
counters as we wouldn't see our own first update. See above about
tracking this and risks about getting it wrong.
GC uses a bloom filter for keys to avoid heavy RAM usage. GC can't run
concurrently with FileInfo updates so there is a new lock around those
operation at the lowlevel.
The end result is a much more compact database, especially for setups
with many peers where files get duplicated many times.
This is per-key-class stats for a large database I'm currently working
with, under the current schema:
```
0x00: 9138161 items, 870876 KB keys + 7397482 KB data, 95 B + 809 B avg, 1637651 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x08: 1349 items, 12 KB keys + 10 KB data, 9 B + 8 B avg, 17 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 7 B avg, 30 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
Total 10426475 items, 968490 KB keys + 9202925 KB data.
```
Note 7.4 GB of data in class 00, total size 9.2 GB. After running the
migration we get this instead:
```
0x00: 9138161 items, 870876 KB keys + 2611392 KB data, 95 B + 285 B avg, 4788 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 17 B avg, 51 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
0x0d: 44282 items, 1461 KB keys + 61081 KB data, 33 B + 1379 B avg, 1637399 B max
Total 10469408 items, 969939 KB keys + 4477905 KB data.
```
Class 00 is now down to 2.6 GB, with just 61 MB added in class 0d.
There will be some additional reads in some cases which theoretically
hurts performance, but this will be more than compensated for by smaller
writes and better compaction.
On my own home setup which just has three devices and a handful of
folders the difference is smaller in absolute numbers of course, but
still less than half the old size:
```
0x00: 297122 items, 20894 KB keys + 306860 KB data, 70 B + 1032 B avg, 103237 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
Total 1947412 items, 151268 KB keys + 337485 KB data.
```
to:
```
0x00: 297122 items, 20894 KB keys + 37038 KB data, 70 B + 124 B avg, 520 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
0x0d: 18041 items, 595 KB keys + 71964 KB data, 33 B + 3988 B avg, 101109 B max
Total 1965447 items, 151863 KB keys + 139628 KB data.
```
* wip
* wip
* wip
* wip
2020-01-24 00:35:44 -07:00
|
|
|
if err := updater.updateSchema1to2(1); err != nil {
|
2019-11-29 01:11:52 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-02 06:08:32 -07:00
|
|
|
|
|
|
|
found := false
|
2020-01-21 10:23:08 -07:00
|
|
|
trans, err = db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-11 06:31:43 -07:00
|
|
|
defer trans.Release()
|
2020-01-21 10:23:08 -07:00
|
|
|
_ = trans.withHaveSequence(folder, 0, func(fi FileIntf) bool {
|
2018-06-02 06:08:32 -07:00
|
|
|
f := fi.(protocol.FileInfo)
|
|
|
|
l.Infoln(f)
|
|
|
|
if found {
|
|
|
|
t.Error("Unexpected additional file via sequence", f.FileName())
|
|
|
|
return true
|
|
|
|
}
|
2019-07-23 12:48:53 -07:00
|
|
|
if e := haveUpdate0to3[protocol.LocalDeviceID][0]; f.IsEquivalentOptional(e, 0, true, true, 0) {
|
2018-06-02 06:08:32 -07:00
|
|
|
found = true
|
|
|
|
} else {
|
|
|
|
t.Errorf("Wrong file via sequence, got %v, expected %v", f, e)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if !found {
|
|
|
|
t.Error("Local file wasn't added to sequence bucket", err)
|
|
|
|
}
|
|
|
|
|
lib/db: Deduplicate block lists in database (fixes #5898) (#6283)
* lib/db: Deduplicate block lists in database (fixes #5898)
This moves the block list in the database out from being just a field on
the FileInfo to being an object of its own. When putting a FileInfo we
marshal the block list separately and store it keyed by the sha256 of
the marshalled block list. When getting, if we are not doing a
"truncated" get, we do an extra read and unmarshal for the block list.
Old block lists are cleared out by a periodic GC sweep. The alternative
would be to use refcounting, but:
- There is a larger risk of getting that wrong and either dropping a
block list in error or keeping them around forever.
- It's tricky with our current database, as we don't have dirty reads.
This means that if we update two FileInfos with identical block lists in
the same transaction we can't just do read/modify/write for the ref
counters as we wouldn't see our own first update. See above about
tracking this and risks about getting it wrong.
GC uses a bloom filter for keys to avoid heavy RAM usage. GC can't run
concurrently with FileInfo updates so there is a new lock around those
operation at the lowlevel.
The end result is a much more compact database, especially for setups
with many peers where files get duplicated many times.
This is per-key-class stats for a large database I'm currently working
with, under the current schema:
```
0x00: 9138161 items, 870876 KB keys + 7397482 KB data, 95 B + 809 B avg, 1637651 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x08: 1349 items, 12 KB keys + 10 KB data, 9 B + 8 B avg, 17 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 7 B avg, 30 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
Total 10426475 items, 968490 KB keys + 9202925 KB data.
```
Note 7.4 GB of data in class 00, total size 9.2 GB. After running the
migration we get this instead:
```
0x00: 9138161 items, 870876 KB keys + 2611392 KB data, 95 B + 285 B avg, 4788 B max
0x01: 185656 items, 10388 KB keys + 1790909 KB data, 55 B + 9646 B avg, 924525 B max
0x02: 916890 items, 84795 KB keys + 3667 KB data, 92 B + 4 B avg, 192 B max
0x03: 384 items, 27 KB keys + 5 KB data, 72 B + 15 B avg, 87 B max
0x04: 1109 items, 17 KB keys + 17 KB data, 15 B + 15 B avg, 69 B max
0x06: 383 items, 3 KB keys + 0 KB data, 9 B + 2 B avg, 18 B max
0x07: 510 items, 4 KB keys + 12 KB data, 9 B + 24 B avg, 41 B max
0x09: 194 items, 0 KB keys + 123 KB data, 5 B + 634 B avg, 11484 B max
0x0a: 3 items, 0 KB keys + 0 KB data, 14 B + 17 B avg, 51 B max
0x0b: 181836 items, 2363 KB keys + 10694 KB data, 13 B + 58 B avg, 173 B max
0x0d: 44282 items, 1461 KB keys + 61081 KB data, 33 B + 1379 B avg, 1637399 B max
Total 10469408 items, 969939 KB keys + 4477905 KB data.
```
Class 00 is now down to 2.6 GB, with just 61 MB added in class 0d.
There will be some additional reads in some cases which theoretically
hurts performance, but this will be more than compensated for by smaller
writes and better compaction.
On my own home setup which just has three devices and a handful of
folders the difference is smaller in absolute numbers of course, but
still less than half the old size:
```
0x00: 297122 items, 20894 KB keys + 306860 KB data, 70 B + 1032 B avg, 103237 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
Total 1947412 items, 151268 KB keys + 337485 KB data.
```
to:
```
0x00: 297122 items, 20894 KB keys + 37038 KB data, 70 B + 124 B avg, 520 B max
0x01: 115299 items, 7738 KB keys + 17542 KB data, 67 B + 152 B avg, 419 B max
0x02: 1430537 items, 121223 KB keys + 5722 KB data, 84 B + 4 B avg, 253 B max
...
0x0d: 18041 items, 595 KB keys + 71964 KB data, 33 B + 3988 B avg, 101109 B max
Total 1965447 items, 151863 KB keys + 139628 KB data.
```
* wip
* wip
* wip
* wip
2020-01-24 00:35:44 -07:00
|
|
|
if err := updater.updateSchema2to3(2); err != nil {
|
2019-11-29 01:11:52 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-02 06:08:32 -07:00
|
|
|
|
|
|
|
need := map[string]protocol.FileInfo{
|
|
|
|
haveUpdate0to3[remoteDevice0][0].Name: haveUpdate0to3[remoteDevice0][0],
|
|
|
|
haveUpdate0to3[remoteDevice1][0].Name: haveUpdate0to3[remoteDevice1][0],
|
|
|
|
haveUpdate0to3[remoteDevice0][2].Name: haveUpdate0to3[remoteDevice0][2],
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
trans, err = db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-11 06:31:43 -07:00
|
|
|
defer trans.Release()
|
2020-01-21 10:23:08 -07:00
|
|
|
_ = trans.withNeed(folder, protocol.LocalDeviceID[:], false, func(fi FileIntf) bool {
|
2018-06-02 06:08:32 -07:00
|
|
|
e, ok := need[fi.FileName()]
|
|
|
|
if !ok {
|
|
|
|
t.Error("Got unexpected needed file:", fi.FileName())
|
|
|
|
}
|
|
|
|
f := fi.(protocol.FileInfo)
|
|
|
|
delete(need, f.Name)
|
2019-07-23 12:48:53 -07:00
|
|
|
if !f.IsEquivalentOptional(e, 0, true, true, 0) {
|
2018-06-02 06:08:32 -07:00
|
|
|
t.Errorf("Wrong needed file, got %v, expected %v", f, e)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
for n := range need {
|
|
|
|
t.Errorf(`Missing needed file "%v"`, n)
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 02:40:34 -07:00
|
|
|
|
2020-03-18 09:34:46 -07:00
|
|
|
// TestRepairSequence checks that a few hand-crafted messed-up sequence entries get fixed.
|
|
|
|
func TestRepairSequence(t *testing.T) {
|
|
|
|
db := NewLowlevel(backend.OpenMemory())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
folderStr := "test"
|
|
|
|
folder := []byte(folderStr)
|
|
|
|
id := protocol.LocalDeviceID
|
|
|
|
short := protocol.LocalDeviceID.Short()
|
|
|
|
|
|
|
|
files := []protocol.FileInfo{
|
2020-03-20 04:07:14 -07:00
|
|
|
{Name: "fine", Blocks: genBlocks(1)},
|
|
|
|
{Name: "duplicate", Blocks: genBlocks(2)},
|
|
|
|
{Name: "missing", Blocks: genBlocks(3)},
|
|
|
|
{Name: "overwriting", Blocks: genBlocks(4)},
|
|
|
|
{Name: "inconsistent", Blocks: genBlocks(5)},
|
2020-03-18 09:34:46 -07:00
|
|
|
}
|
|
|
|
for i, f := range files {
|
|
|
|
files[i].Version = f.Version.Update(short)
|
|
|
|
}
|
|
|
|
|
|
|
|
trans, err := db.newReadWriteTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer trans.close()
|
|
|
|
|
|
|
|
addFile := func(f protocol.FileInfo, seq int64) {
|
|
|
|
dk, err := trans.keyer.GenerateDeviceFileKey(nil, folder, id[:], []byte(f.Name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-20 04:07:14 -07:00
|
|
|
if err := trans.putFile(dk, f, false); err != nil {
|
2020-03-18 09:34:46 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sk, err := trans.keyer.GenerateSequenceKey(nil, folder, seq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := trans.Put(sk, dk); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plain normal entry
|
|
|
|
var seq int64 = 1
|
|
|
|
files[0].Sequence = 1
|
|
|
|
addFile(files[0], seq)
|
|
|
|
|
|
|
|
// Second entry once updated with original sequence still in place
|
|
|
|
f := files[1]
|
|
|
|
f.Sequence = int64(len(files) + 1)
|
|
|
|
addFile(f, f.Sequence)
|
|
|
|
// Original sequence entry
|
|
|
|
seq++
|
|
|
|
sk, err := trans.keyer.GenerateSequenceKey(nil, folder, seq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dk, err := trans.keyer.GenerateDeviceFileKey(nil, folder, id[:], []byte(f.Name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := trans.Put(sk, dk); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// File later overwritten thus missing sequence entry
|
|
|
|
seq++
|
|
|
|
files[2].Sequence = seq
|
|
|
|
addFile(files[2], seq)
|
|
|
|
|
|
|
|
// File overwriting previous sequence entry (no seq bump)
|
|
|
|
seq++
|
|
|
|
files[3].Sequence = seq
|
|
|
|
addFile(files[3], seq)
|
|
|
|
|
|
|
|
// Inconistent file
|
|
|
|
seq++
|
|
|
|
files[4].Sequence = 101
|
|
|
|
addFile(files[4], seq)
|
|
|
|
|
|
|
|
// And a sequence entry pointing at nothing because why not
|
|
|
|
sk, err = trans.keyer.GenerateSequenceKey(nil, folder, 100001)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dk, err = trans.keyer.GenerateDeviceFileKey(nil, folder, id[:], []byte("nonexisting"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := trans.Put(sk, dk); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := trans.Commit(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loading the metadata for the first time means a "re"calculation happens,
|
|
|
|
// along which the sequences get repaired too.
|
|
|
|
db.gcMut.RLock()
|
2020-03-19 07:58:32 -07:00
|
|
|
_ = db.loadMetadataTracker(folderStr)
|
2020-03-18 09:34:46 -07:00
|
|
|
db.gcMut.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the db
|
|
|
|
ro, err := db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ro.close()
|
|
|
|
|
|
|
|
it, err := ro.NewPrefixIterator([]byte{KeyTypeDevice})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer it.Release()
|
|
|
|
for it.Next() {
|
|
|
|
fi, err := ro.unmarshalTrunc(it.Value(), true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if sk, err = ro.keyer.GenerateSequenceKey(sk, folder, fi.SequenceNo()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dk, err := ro.Get(sk)
|
|
|
|
if backend.IsNotFound(err) {
|
|
|
|
t.Error("Missing sequence entry for", fi.FileName())
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(it.Key(), dk) {
|
|
|
|
t.Errorf("Wrong key for %v, expected %s, got %s", f.FileName(), it.Key(), dk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := it.Error(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
it, err = ro.NewPrefixIterator([]byte{KeyTypeSequence})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer it.Release()
|
|
|
|
for it.Next() {
|
2020-03-20 04:07:14 -07:00
|
|
|
intf, ok, err := ro.getFileTrunc(it.Value(), false)
|
2020-03-18 09:34:46 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-20 04:07:14 -07:00
|
|
|
fi := intf.(protocol.FileInfo)
|
2020-03-18 09:34:46 -07:00
|
|
|
seq := ro.keyer.SequenceFromSequenceKey(it.Key())
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Sequence entry %v points at nothing", seq)
|
|
|
|
} else if fi.SequenceNo() != seq {
|
|
|
|
t.Errorf("Inconsistent sequence entry for %v: %v != %v", fi.FileName(), fi.SequenceNo(), seq)
|
|
|
|
}
|
2020-03-20 04:07:14 -07:00
|
|
|
if len(fi.Blocks) == 0 {
|
|
|
|
t.Error("Missing blocks in", fi.FileName())
|
|
|
|
}
|
2020-03-18 09:34:46 -07:00
|
|
|
}
|
|
|
|
if err := it.Error(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
it.Release()
|
|
|
|
}
|
|
|
|
|
2018-06-26 02:40:34 -07:00
|
|
|
func TestDowngrade(t *testing.T) {
|
2019-11-29 01:11:52 -07:00
|
|
|
db := NewLowlevel(backend.OpenMemory())
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2019-11-29 01:11:52 -07:00
|
|
|
// sets the min version etc
|
|
|
|
if err := UpdateSchema(db); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-26 02:40:34 -07:00
|
|
|
|
2018-10-10 02:34:24 -07:00
|
|
|
// Bump the database version to something newer than we actually support
|
|
|
|
miscDB := NewMiscDataNamespace(db)
|
2019-11-29 01:11:52 -07:00
|
|
|
if err := miscDB.PutInt64("dbVersion", dbVersion+1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-26 02:40:34 -07:00
|
|
|
l.Infoln(dbVersion)
|
|
|
|
|
2018-10-10 02:34:24 -07:00
|
|
|
// Pretend we just opened the DB and attempt to update it again
|
|
|
|
err := UpdateSchema(db)
|
|
|
|
|
2018-06-26 02:40:34 -07:00
|
|
|
if err, ok := err.(databaseDowngradeError); !ok {
|
|
|
|
t.Fatal("Expected error due to database downgrade, got", err)
|
|
|
|
} else if err.minSyncthingVersion != dbMinSyncthingVersion {
|
|
|
|
t.Fatalf("Error has %v as min Syncthing version, expected %v", err.minSyncthingVersion, dbMinSyncthingVersion)
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 06:30:20 -07:00
|
|
|
|
|
|
|
func TestCheckGlobals(t *testing.T) {
|
|
|
|
db := NewLowlevel(backend.OpenMemory())
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
fs := NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeFake, ""), db)
|
|
|
|
|
|
|
|
// Add any file
|
|
|
|
name := "foo"
|
|
|
|
fs.Update(protocol.LocalDeviceID, []protocol.FileInfo{
|
|
|
|
{
|
|
|
|
Name: name,
|
|
|
|
Type: protocol.FileInfoTypeFile,
|
|
|
|
Version: protocol.Vector{Counters: []protocol.Counter{{ID: 1, Value: 1001}}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Remove just the file entry
|
|
|
|
if err := db.dropPrefix([]byte{KeyTypeDevice}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up global entry of the now missing file
|
|
|
|
if err := db.checkGlobals([]byte(fs.folder), fs.meta); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the global entry is gone
|
|
|
|
gk, err := db.keyer.GenerateGlobalVersionKey(nil, []byte(fs.folder), []byte(name))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = db.Get(gk)
|
|
|
|
if !backend.IsNotFound(err) {
|
|
|
|
t.Error("Expected key missing error, got", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 06:07:06 -07:00
|
|
|
|
|
|
|
func TestUpdateTo10(t *testing.T) {
|
|
|
|
ldb, err := openJSONS("./testdata/v1.4.0-updateTo10.json")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
db := NewLowlevel(ldb)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
UpdateSchema(db)
|
|
|
|
|
|
|
|
folder := "test"
|
|
|
|
|
|
|
|
meta := db.getMetaAndCheck(folder)
|
|
|
|
|
|
|
|
empty := Counts{}
|
|
|
|
|
|
|
|
c := meta.Counts(protocol.LocalDeviceID, needFlag)
|
|
|
|
if c.Files != 1 {
|
|
|
|
t.Error("Expected 1 needed file locally, got", c.Files)
|
|
|
|
}
|
|
|
|
c.Files = 0
|
|
|
|
if c.Deleted != 1 {
|
|
|
|
t.Error("Expected 1 needed deletion locally, got", c.Deleted)
|
|
|
|
}
|
|
|
|
c.Deleted = 0
|
|
|
|
if !c.Equal(empty) {
|
|
|
|
t.Error("Expected all counts to be zero, got", c)
|
|
|
|
}
|
|
|
|
c = meta.Counts(remoteDevice0, needFlag)
|
|
|
|
if !c.Equal(empty) {
|
|
|
|
t.Error("Expected all counts to be zero, got", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
trans, err := db.newReadOnlyTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer trans.Release()
|
|
|
|
// a
|
|
|
|
vl, err := trans.getGlobalVersions(nil, []byte(folder), []byte("a"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, v := range vl.Versions {
|
|
|
|
if !v.Deleted {
|
|
|
|
t.Error("Unexpected undeleted global version for a")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// b
|
|
|
|
vl, err = trans.getGlobalVersions(nil, []byte(folder), []byte("b"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !vl.Versions[0].Deleted {
|
|
|
|
t.Error("vl.Versions[0] not deleted for b")
|
|
|
|
}
|
|
|
|
if vl.Versions[1].Deleted {
|
|
|
|
t.Error("vl.Versions[1] deleted for b")
|
|
|
|
}
|
|
|
|
// c
|
|
|
|
vl, err = trans.getGlobalVersions(nil, []byte(folder), []byte("c"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if vl.Versions[0].Deleted {
|
|
|
|
t.Error("vl.Versions[0] deleted for c")
|
|
|
|
}
|
|
|
|
if !vl.Versions[1].Deleted {
|
|
|
|
t.Error("vl.Versions[1] not deleted for c")
|
|
|
|
}
|
|
|
|
}
|