2016-07-04 10:40:29 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-23 12:46:31 +00:00
|
|
|
"encoding/binary"
|
2021-02-01 08:27:34 +00:00
|
|
|
"encoding/json"
|
2016-07-23 12:46:31 +00:00
|
|
|
"errors"
|
2016-07-04 10:40:29 +00:00
|
|
|
"fmt"
|
2018-02-25 08:39:00 +00:00
|
|
|
"runtime"
|
2016-08-06 13:05:59 +00:00
|
|
|
"time"
|
2016-07-23 12:46:31 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
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 07:35:44 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2016-07-04 10:40:29 +00:00
|
|
|
)
|
|
|
|
|
2016-09-02 06:45:46 +00:00
|
|
|
const (
|
2019-01-17 19:48:43 +00:00
|
|
|
SyntheticDirectorySize = 128
|
|
|
|
HelloMessageMagic uint32 = 0x2EA7D90B
|
|
|
|
Version13HelloMagic uint32 = 0x9F79BC40 // old
|
2016-07-04 10:40:29 +00:00
|
|
|
)
|
|
|
|
|
2020-05-30 07:50:23 +00:00
|
|
|
// FileIntf is the set of methods implemented by both FileInfo and
|
|
|
|
// db.FileInfoTruncated.
|
|
|
|
type FileIntf interface {
|
|
|
|
FileSize() int64
|
|
|
|
FileName() string
|
|
|
|
FileLocalFlags() uint32
|
|
|
|
IsDeleted() bool
|
|
|
|
IsInvalid() bool
|
|
|
|
IsIgnored() bool
|
|
|
|
IsUnsupported() bool
|
|
|
|
MustRescan() bool
|
|
|
|
IsReceiveOnlyChanged() bool
|
|
|
|
IsDirectory() bool
|
|
|
|
IsSymlink() bool
|
|
|
|
ShouldConflict() bool
|
|
|
|
HasPermissionBits() bool
|
|
|
|
SequenceNo() int64
|
|
|
|
BlockSize() int
|
|
|
|
FileVersion() Vector
|
|
|
|
FileType() FileInfoType
|
|
|
|
FilePermissions() uint32
|
|
|
|
FileModifiedBy() ShortID
|
|
|
|
ModTime() time.Time
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (Hello) Magic() uint32 {
|
2016-07-04 10:40:29 +00:00
|
|
|
return HelloMessageMagic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) String() string {
|
2016-12-09 18:02:18 +00:00
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeDirectory:
|
2022-07-26 06:24:58 +00:00
|
|
|
return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, VersionHash:%x, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, Platform:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.VersionHash, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.Platform)
|
2016-12-09 18:02:18 +00:00
|
|
|
case FileInfoTypeFile:
|
2022-07-26 06:24:58 +00:00
|
|
|
return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, VersionHash:%x, Length:%d, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, BlockSize:%d, Blocks:%v, BlocksHash:%x, Platform:%v}",
|
|
|
|
f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.VersionHash, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize, f.Blocks, f.BlocksHash, f.Platform)
|
2020-10-02 06:07:05 +00:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeSymlinkDirectory, FileInfoTypeSymlinkFile:
|
2022-07-26 06:24:58 +00:00
|
|
|
return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, VersionHash:%x, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q, Platform:%v}",
|
|
|
|
f.Name, f.Type, f.Sequence, f.Version, f.VersionHash, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.SymlinkTarget, f.Platform)
|
2016-12-09 18:02:18 +00:00
|
|
|
default:
|
|
|
|
panic("mystery file type detected")
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsDeleted() bool {
|
|
|
|
return f.Deleted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) IsInvalid() bool {
|
2018-06-24 07:50:18 +00:00
|
|
|
return f.RawInvalid || f.LocalFlags&LocalInvalidFlags != 0
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) IsUnsupported() bool {
|
|
|
|
return f.LocalFlags&FlagLocalUnsupported != 0
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
func (f FileInfo) IsIgnored() bool {
|
|
|
|
return f.LocalFlags&FlagLocalIgnored != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) MustRescan() bool {
|
|
|
|
return f.LocalFlags&FlagLocalMustRescan != 0
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) IsReceiveOnlyChanged() bool {
|
|
|
|
return f.LocalFlags&FlagLocalReceiveOnly != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) IsDirectory() bool {
|
|
|
|
return f.Type == FileInfoTypeDirectory
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
func (f FileInfo) ShouldConflict() bool {
|
|
|
|
return f.LocalFlags&LocalConflictFlags != 0
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) IsSymlink() bool {
|
|
|
|
switch f.Type {
|
2020-10-02 06:07:05 +00:00
|
|
|
case FileInfoTypeSymlink, FileInfoTypeSymlinkDirectory, FileInfoTypeSymlinkFile:
|
2016-07-04 10:40:29 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) HasPermissionBits() bool {
|
|
|
|
return !f.NoPermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileSize() int64 {
|
2016-09-02 06:45:46 +00:00
|
|
|
if f.Deleted {
|
|
|
|
return 0
|
|
|
|
}
|
2016-12-09 09:38:36 +00:00
|
|
|
if f.IsDirectory() || f.IsSymlink() {
|
2016-09-02 06:45:46 +00:00
|
|
|
return SyntheticDirectorySize
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
return f.Size
|
|
|
|
}
|
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
func (f FileInfo) BlockSize() int {
|
2021-08-04 21:12:01 +00:00
|
|
|
if f.RawBlockSize < MinBlockSize {
|
2018-04-16 18:08:50 +00:00
|
|
|
return MinBlockSize
|
|
|
|
}
|
2021-08-04 21:12:01 +00:00
|
|
|
return f.RawBlockSize
|
2018-04-16 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (f FileInfo) FileName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func (f FileInfo) FileLocalFlags() uint32 {
|
|
|
|
return f.LocalFlags
|
|
|
|
}
|
|
|
|
|
2016-08-06 13:05:59 +00:00
|
|
|
func (f FileInfo) ModTime() time.Time {
|
|
|
|
return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (f FileInfo) SequenceNo() int64 {
|
|
|
|
return f.Sequence
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
func (f FileInfo) FileVersion() Vector {
|
|
|
|
return f.Version
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:25:12 +00:00
|
|
|
func (f FileInfo) FileType() FileInfoType {
|
|
|
|
return f.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FilePermissions() uint32 {
|
|
|
|
return f.Permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f FileInfo) FileModifiedBy() ShortID {
|
|
|
|
return f.ModifiedBy
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// WinsConflict returns true if "f" is the one to choose when it is in
|
|
|
|
// conflict with "other".
|
2020-05-30 07:50:23 +00:00
|
|
|
func WinsConflict(f, other FileIntf) bool {
|
2018-07-12 08:15:57 +00:00
|
|
|
// If only one of the files is invalid, that one loses.
|
2018-02-10 18:40:57 +00:00
|
|
|
if f.IsInvalid() != other.IsInvalid() {
|
|
|
|
return !f.IsInvalid()
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
// If a modification is in conflict with a delete, we pick the
|
|
|
|
// modification.
|
|
|
|
if !f.IsDeleted() && other.IsDeleted() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if f.IsDeleted() && !other.IsDeleted() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The one with the newer modification time wins.
|
2016-08-06 13:05:59 +00:00
|
|
|
if f.ModTime().After(other.ModTime()) {
|
2016-07-04 10:40:29 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-08-06 13:05:59 +00:00
|
|
|
if f.ModTime().Before(other.ModTime()) {
|
2016-07-04 10:40:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The modification times were equal. Use the device ID in the version
|
|
|
|
// vector as tie breaker.
|
2020-05-30 07:50:23 +00:00
|
|
|
return f.FileVersion().Compare(other.FileVersion()) == ConcurrentGreater
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 06:24:58 +00:00
|
|
|
type FileInfoComparison struct {
|
|
|
|
ModTimeWindow time.Duration
|
|
|
|
IgnorePerms bool
|
|
|
|
IgnoreBlocks bool
|
|
|
|
IgnoreFlags uint32
|
|
|
|
IgnoreOwnership bool
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func (f FileInfo) IsEquivalent(other FileInfo, modTimeWindow time.Duration) bool {
|
2022-07-26 06:24:58 +00:00
|
|
|
return f.isEquivalent(other, FileInfoComparison{ModTimeWindow: modTimeWindow})
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 06:24:58 +00:00
|
|
|
func (f FileInfo) IsEquivalentOptional(other FileInfo, comp FileInfoComparison) bool {
|
|
|
|
return f.isEquivalent(other, comp)
|
2018-07-12 08:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isEquivalent checks that the two file infos represent the same actual file content,
|
2018-02-25 08:39:00 +00:00
|
|
|
// i.e. it does purposely not check only selected (see below) struct members.
|
|
|
|
// Permissions (config) and blocks (scanning) can be excluded from the comparison.
|
|
|
|
// Any file info is not "equivalent", if it has different
|
2022-07-26 06:24:58 +00:00
|
|
|
// - type
|
|
|
|
// - deleted flag
|
|
|
|
// - invalid flag
|
|
|
|
// - permissions, unless they are ignored
|
|
|
|
//
|
2018-02-25 08:39:00 +00:00
|
|
|
// A file is not "equivalent", if it has different
|
2022-07-26 06:24:58 +00:00
|
|
|
// - modification time (difference bigger than modTimeWindow)
|
|
|
|
// - size
|
|
|
|
// - blocks, unless there are no blocks to compare (scanning)
|
|
|
|
// - os data
|
|
|
|
//
|
2018-02-25 08:39:00 +00:00
|
|
|
// A symlink is not "equivalent", if it has different
|
2022-07-26 06:24:58 +00:00
|
|
|
// - target
|
|
|
|
//
|
2018-02-25 08:39:00 +00:00
|
|
|
// A directory does not have anything specific to check.
|
2022-07-26 06:24:58 +00:00
|
|
|
func (f FileInfo) isEquivalent(other FileInfo, comp FileInfoComparison) bool {
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.MustRescan() || other.MustRescan() {
|
|
|
|
// These are per definition not equivalent because they don't
|
|
|
|
// represent a valid state, even if both happen to have the
|
|
|
|
// MustRescan bit set.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
// Mask out the ignored local flags before checking IsInvalid() below
|
2022-07-26 06:24:58 +00:00
|
|
|
f.LocalFlags &^= comp.IgnoreFlags
|
|
|
|
other.LocalFlags &^= comp.IgnoreFlags
|
2018-07-12 08:15:57 +00:00
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.IsInvalid() != other.IsInvalid() {
|
2018-02-25 08:39:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-26 06:24:58 +00:00
|
|
|
// OS data comparison is special: we consider a difference only if an
|
|
|
|
// entry for the same OS exists on both sides and they are different.
|
|
|
|
// Otherwise a file would become different as soon as it's synced from
|
|
|
|
// Windows to Linux, as Linux would add a new POSIX entry for the file.
|
|
|
|
if !comp.IgnoreOwnership && f.Platform != other.Platform {
|
|
|
|
if f.Platform.Unix != nil && other.Platform.Unix != nil {
|
|
|
|
if *f.Platform.Unix != *other.Platform.Unix {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.Platform.Windows != nil && other.Platform.Windows != nil {
|
|
|
|
if *f.Platform.Windows != *other.Platform.Windows {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !comp.IgnorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
|
2018-02-25 08:39:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Type {
|
|
|
|
case FileInfoTypeFile:
|
2022-07-26 06:24:58 +00:00
|
|
|
return f.Size == other.Size && ModTimeEqual(f.ModTime(), other.ModTime(), comp.ModTimeWindow) && (comp.IgnoreBlocks || f.BlocksEqual(other))
|
2018-02-25 08:39:00 +00:00
|
|
|
case FileInfoTypeSymlink:
|
|
|
|
return f.SymlinkTarget == other.SymlinkTarget
|
|
|
|
case FileInfoTypeDirectory:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:48:53 +00:00
|
|
|
func ModTimeEqual(a, b time.Time, modTimeWindow time.Duration) bool {
|
|
|
|
if a.Equal(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
diff := a.Sub(b)
|
|
|
|
if diff < 0 {
|
|
|
|
diff *= -1
|
|
|
|
}
|
|
|
|
return diff < modTimeWindow
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func PermsEqual(a, b uint32) bool {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
// There is only writeable and read only, represented for user, group
|
|
|
|
// and other equally. We only compare against user.
|
|
|
|
return a&0600 == b&0600
|
|
|
|
default:
|
|
|
|
// All bits count
|
|
|
|
return a&0777 == b&0777
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:46:49 +00:00
|
|
|
// BlocksEqual returns true when the two files have identical block lists.
|
|
|
|
func (f FileInfo) BlocksEqual(other FileInfo) bool {
|
2020-06-29 07:33:07 +00:00
|
|
|
// If both sides have blocks hashes and they match, we are good. If they
|
|
|
|
// don't match still check individual block hashes to catch differences
|
|
|
|
// in weak hashes only (e.g. after switching weak hash algo).
|
|
|
|
if len(f.BlocksHash) > 0 && len(other.BlocksHash) > 0 && bytes.Equal(f.BlocksHash, other.BlocksHash) {
|
|
|
|
return true
|
2020-03-10 13:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actually compare the block lists in full.
|
|
|
|
return blocksEqual(f.Blocks, other.Blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// blocksEqual returns whether two slices of blocks are exactly the same hash
|
2018-02-25 08:39:00 +00:00
|
|
|
// and index pair wise.
|
2020-03-10 13:46:49 +00:00
|
|
|
func blocksEqual(a, b []BlockInfo) bool {
|
2018-02-25 08:39:00 +00:00
|
|
|
if len(b) != len(a) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, sblk := range a {
|
|
|
|
if !bytes.Equal(sblk.Hash, b[i].Hash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:39 +00:00
|
|
|
func (f *FileInfo) SetMustRescan() {
|
|
|
|
f.setLocalFlags(FlagLocalMustRescan)
|
2018-06-24 07:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:39 +00:00
|
|
|
func (f *FileInfo) SetIgnored() {
|
|
|
|
f.setLocalFlags(FlagLocalIgnored)
|
2018-06-24 07:50:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:39 +00:00
|
|
|
func (f *FileInfo) SetUnsupported() {
|
|
|
|
f.setLocalFlags(FlagLocalUnsupported)
|
2020-02-19 15:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileInfo) SetDeleted(by ShortID) {
|
2018-06-24 07:50:18 +00:00
|
|
|
f.ModifiedBy = by
|
2020-02-19 15:58:09 +00:00
|
|
|
f.Deleted = true
|
|
|
|
f.Version = f.Version.Update(by)
|
|
|
|
f.ModifiedS = time.Now().Unix()
|
|
|
|
f.setNoContent()
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:39 +00:00
|
|
|
func (f *FileInfo) setLocalFlags(flags uint32) {
|
2020-02-19 15:58:09 +00:00
|
|
|
f.RawInvalid = false
|
|
|
|
f.LocalFlags = flags
|
|
|
|
f.setNoContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileInfo) setNoContent() {
|
2017-11-11 19:18:17 +00:00
|
|
|
f.Blocks = nil
|
2020-02-19 15:58:09 +00:00
|
|
|
f.BlocksHash = nil
|
|
|
|
f.Size = 0
|
2017-11-11 19:18:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (b BlockInfo) String() string {
|
2016-12-14 23:30:29 +00:00
|
|
|
return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the block is a full block of zeroes.
|
|
|
|
func (b BlockInfo) IsEmpty() bool {
|
2018-04-16 18:08:50 +00:00
|
|
|
if v, ok := sha256OfEmptyBlock[int(b.Size)]; ok {
|
|
|
|
return bytes.Equal(b.Hash, v[:])
|
|
|
|
}
|
|
|
|
return false
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2016-07-23 12:46:31 +00:00
|
|
|
|
|
|
|
type IndexID uint64
|
|
|
|
|
|
|
|
func (i IndexID) String() string {
|
2020-03-17 06:40:52 +00:00
|
|
|
return fmt.Sprintf("0x%016X", uint64(i))
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i IndexID) Marshal() ([]byte, error) {
|
|
|
|
bs := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(bs, uint64(i))
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IndexID) Unmarshal(bs []byte) error {
|
|
|
|
if len(bs) != 8 {
|
|
|
|
return errors.New("incorrect IndexID length")
|
|
|
|
}
|
|
|
|
*i = IndexID(binary.BigEndian.Uint64(bs))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexID() IndexID {
|
2020-06-17 08:43:58 +00:00
|
|
|
return IndexID(rand.Uint64())
|
2016-07-23 12:46:31 +00:00
|
|
|
}
|
2016-11-22 07:36:14 +00:00
|
|
|
|
|
|
|
func (f Folder) Description() string {
|
|
|
|
// used by logging stuff
|
2016-12-19 09:12:06 +00:00
|
|
|
if f.Label == "" {
|
|
|
|
return f.ID
|
|
|
|
}
|
2016-11-22 07:36:14 +00:00
|
|
|
return fmt.Sprintf("%q (%s)", f.Label, f.ID)
|
|
|
|
}
|
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 07:35:44 +00:00
|
|
|
|
|
|
|
func BlocksHash(bs []BlockInfo) []byte {
|
|
|
|
h := sha256.New()
|
|
|
|
for _, b := range bs {
|
|
|
|
_, _ = h.Write(b.Hash)
|
2020-06-29 07:33:07 +00:00
|
|
|
_ = binary.Write(h, binary.BigEndian, b.WeakHash)
|
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 07:35:44 +00:00
|
|
|
}
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
2020-05-13 12:28:42 +00:00
|
|
|
|
|
|
|
func VectorHash(v Vector) []byte {
|
|
|
|
h := sha256.New()
|
|
|
|
for _, c := range v.Counters {
|
|
|
|
if err := binary.Write(h, binary.BigEndian, c.ID); err != nil {
|
|
|
|
panic("impossible: failed to write c.ID to hash function: " + err.Error())
|
|
|
|
}
|
|
|
|
if err := binary.Write(h, binary.BigEndian, c.Value); err != nil {
|
|
|
|
panic("impossible: failed to write c.Value to hash function: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
2021-02-01 08:27:34 +00:00
|
|
|
|
|
|
|
func (x *FileInfoType) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(x.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *FileInfoType) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, ok := FileInfoType_value[s]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid value: " + s)
|
|
|
|
}
|
|
|
|
*x = FileInfoType(n)
|
|
|
|
return nil
|
|
|
|
}
|